Example usage for java.security.cert X509CertSelector match

List of usage examples for java.security.cert X509CertSelector match

Introduction

In this page you can find the example usage for java.security.cert X509CertSelector match.

Prototype

public boolean match(Certificate cert) 

Source Link

Document

Decides whether a Certificate should be selected.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    X509CertSelector selec = new X509CertSelector();
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    FileInputStream in = new FileInputStream(args[0]);
    Certificate c = cf.generateCertificate(in);
    System.out.println(selec.match(c));
    selec.setIssuer("CN=Peter,OU=Network Center," + "O=University,L=ZB,ST=Vancouver,C=CN");

    System.out.println(selec.match(c));

    Calendar cld = Calendar.getInstance();
    int year = Integer.parseInt(args[1]);
    int month = Integer.parseInt(args[2]) - 1;
    int day = Integer.parseInt(args[3]);
    cld.set(year, month, day);//from   w ww.  ja  v a2s .c o  m
    Date d = cld.getTime();
    selec.setCertificateValid(d);

    System.out.println(selec.match(c));
    BigInteger sn = new BigInteger("1039056963");
    selec.setSerialNumber(sn);

    System.out.println(selec.match(c));
}

From source file:org.globus.gsi.util.CertificateLoadUtil.java

public static Collection<X509Certificate> getTrustedCertificates(KeyStore keyStore, X509CertSelector selector)
        throws KeyStoreException {

    Vector<X509Certificate> certificates = new Vector<X509Certificate>();
    Enumeration<String> aliases = keyStore.aliases();
    while (aliases.hasMoreElements()) {
        String alias = aliases.nextElement();
        if (keyStore.isCertificateEntry(alias)) {
            // If a specific impl of keystore requires refresh, this would be a
            // good place to add it.
            Certificate certificate = keyStore.getCertificate(alias);
            if (certificate instanceof X509Certificate) {
                X509Certificate x509Cert = (X509Certificate) certificate;
                if (selector == null) {
                    certificates.add(x509Cert);
                } else if (selector.match(certificate)) {
                    certificates.add(x509Cert);
                }/* w  w w.  ja  v a2s  .co  m*/
            }

        }
    }
    return certificates;
}

From source file:test.unit.be.fedict.eid.applet.service.signer.CMSTest.java

/**
 * CMS signature with external data and external certificate. The CMS only
 * contains the signature and some certificate selector.
 * /*from  w  w  w  . ja va 2s .c o  m*/
 * @throws Exception
 */
@Test
public void testBasicCmsSignature() throws Exception {
    // setup
    KeyPair keyPair = PkiTestUtils.generateKeyPair();
    DateTime notBefore = new DateTime();
    DateTime notAfter = notBefore.plusMonths(1);
    X509Certificate certificate = generateSelfSignedCertificate(keyPair, "CN=Test", notBefore, notAfter);
    byte[] toBeSigned = "hello world".getBytes();

    // operate
    CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
    generator.addSigner(keyPair.getPrivate(), certificate, CMSSignedDataGenerator.DIGEST_SHA1);
    CMSProcessable content = new CMSProcessableByteArray(toBeSigned);
    CMSSignedData signedData = generator.generate(content, false, (String) null);

    byte[] cmsSignature = signedData.getEncoded();
    LOG.debug("CMS signature: " + ASN1Dump.dumpAsString(new ASN1StreamParser(cmsSignature).readObject()));

    // verify
    signedData = new CMSSignedData(content, cmsSignature);
    SignerInformationStore signers = signedData.getSignerInfos();
    Iterator<SignerInformation> iter = signers.getSigners().iterator();
    while (iter.hasNext()) {
        SignerInformation signer = iter.next();
        SignerId signerId = signer.getSID();
        LOG.debug("signer: " + signerId);
        X509CertSelector signerConstraints = new JcaX509CertSelectorConverter().getCertSelector(signerId);
        LOG.debug("signerConstraints: " + signerConstraints);
        assertTrue(signerConstraints.match(certificate));
        assertTrue(signer.verify(keyPair.getPublic(), BouncyCastleProvider.PROVIDER_NAME));
    }
    LOG.debug("content type: " + signedData.getSignedContentTypeOID());
}

From source file:test.unit.be.fedict.eid.applet.service.signer.CMSTest.java

/**
 * CMS signature with embedded data and external certificate. The CMS only
 * contains the original content, signature and some certificate selector.
 * /*from w  w w. j a  v  a2  s.c o m*/
 * @throws Exception
 */
@Test
public void testCmsSignatureWithContent() throws Exception {
    // setup
    KeyPair keyPair = PkiTestUtils.generateKeyPair();
    DateTime notBefore = new DateTime();
    DateTime notAfter = notBefore.plusMonths(1);
    X509Certificate certificate = generateSelfSignedCertificate(keyPair, "CN=Test", notBefore, notAfter);
    byte[] toBeSigned = "hello world".getBytes();

    // operate
    CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
    generator.addSigner(keyPair.getPrivate(), certificate, CMSSignedDataGenerator.DIGEST_SHA1);
    CMSProcessable content = new CMSProcessableByteArray(toBeSigned);
    CMSSignedData signedData = generator.generate(content, true, (String) null);

    byte[] cmsSignature = signedData.getEncoded();
    LOG.debug("CMS signature: " + ASN1Dump.dumpAsString(new ASN1StreamParser(cmsSignature).readObject()));

    // verify
    signedData = new CMSSignedData(cmsSignature);
    SignerInformationStore signers = signedData.getSignerInfos();
    Iterator<SignerInformation> iter = signers.getSigners().iterator();
    while (iter.hasNext()) {
        SignerInformation signer = iter.next();
        SignerId signerId = signer.getSID();
        LOG.debug("signer: " + signerId);
        X509CertSelector signerConstraints = new JcaX509CertSelectorConverter().getCertSelector(signerId);
        LOG.debug("signerConstraints: " + signerConstraints);
        assertTrue(signerConstraints.match(certificate));
        assertTrue(signer.verify(keyPair.getPublic(), BouncyCastleProvider.PROVIDER_NAME));
    }
    byte[] data = (byte[]) signedData.getSignedContent().getContent();
    assertArrayEquals(toBeSigned, data);
    LOG.debug("content type: " + signedData.getSignedContentTypeOID());
}

From source file:test.unit.be.fedict.eid.applet.service.signer.CMSTest.java

/**
 * CMS signature with external data and embedded certificate. The CMS only
 * contains the signature, signing certificate and some certificate
 * selector./*from  w w w. j a  v  a 2 s.c  o  m*/
 * 
 * @throws Exception
 */
@Test
public void testCmsSignatureWithCertificate() throws Exception {
    // setup
    KeyPair keyPair = PkiTestUtils.generateKeyPair();
    DateTime notBefore = new DateTime();
    DateTime notAfter = notBefore.plusMonths(1);
    X509Certificate certificate = generateSelfSignedCertificate(keyPair, "CN=Test", notBefore, notAfter);
    byte[] toBeSigned = "hello world".getBytes();

    // operate
    CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
    /*
     * addSigner requires the certificate to be able to calculate the key
     * selector.
     */
    generator.addSigner(keyPair.getPrivate(), certificate, CMSSignedDataGenerator.DIGEST_SHA1);
    List<X509Certificate> certList = new LinkedList<X509Certificate>();
    certList.add(certificate);
    CertStore certStore = CertStore.getInstance("Collection", new CollectionCertStoreParameters(certList));
    generator.addCertificatesAndCRLs(certStore);
    CMSProcessable content = new CMSProcessableByteArray(toBeSigned);
    CMSSignedData signedData = generator.generate(content, false, (String) null);

    byte[] cmsSignature = signedData.getEncoded();
    LOG.debug("CMS signature: " + ASN1Dump.dumpAsString(new ASN1StreamParser(cmsSignature).readObject()));

    // verify
    signedData = new CMSSignedData(content, cmsSignature);
    certStore = signedData.getCertificatesAndCRLs("Collection", BouncyCastleProvider.PROVIDER_NAME);
    SignerInformationStore signers = signedData.getSignerInfos();
    Iterator<SignerInformation> iter = signers.getSigners().iterator();
    while (iter.hasNext()) {
        SignerInformation signer = iter.next();
        SignerId signerId = signer.getSID();
        LOG.debug("signer: " + signerId);
        X509CertSelector signerConstraints = new JcaX509CertSelectorConverter().getCertSelector(signerId);
        LOG.debug("signerConstraints: " + signerConstraints);
        assertTrue(signerConstraints.match(certificate));
        assertTrue(signer.verify(keyPair.getPublic(), BouncyCastleProvider.PROVIDER_NAME));

        X509Certificate storedCert = (X509Certificate) certStore // TODO FIXME
                .getCertificates(signerConstraints).iterator().next();
        assertEquals(certificate, storedCert);
    }
    LOG.debug("content type: " + signedData.getSignedContentTypeOID());
}

From source file:test.unit.be.fedict.eid.applet.service.signer.CMSTest.java

@Test
public void testRetrieveCMSDigestValue() throws Exception {
    // setup/*from  w  w w  .ja v  a  2 s  . c o m*/
    KeyPair keyPair = PkiTestUtils.generateKeyPair();
    DateTime notBefore = new DateTime();
    DateTime notAfter = notBefore.plusMonths(1);
    X509Certificate certificate = generateSelfSignedCertificate(keyPair, "CN=Test", notBefore, notAfter);
    byte[] toBeSigned = "hello world".getBytes();

    // operate
    CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
    generator.addSigner(keyPair.getPrivate(), certificate, CMSSignedDataGenerator.DIGEST_SHA1);
    CMSProcessable content = new CMSProcessableByteArray(toBeSigned);

    CMSTestProvider provider = new CMSTestProvider();
    generator.generate(content, false, provider);

    byte[] digestValue = SHA1WithRSASignature.getDigestValue();
    assertNotNull(digestValue);
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPrivate());
    byte[] digestInfoValue = ArrayUtils.addAll(PkiTestUtils.SHA1_DIGEST_INFO_PREFIX, digestValue);
    byte[] signatureValue = cipher.doFinal(digestInfoValue);
    SHA1WithRSASignature.setSignatureValue(signatureValue);

    generator = new CMSSignedDataGenerator();
    generator.addSigner(keyPair.getPrivate(), certificate, CMSSignedDataGenerator.DIGEST_SHA1);
    content = new CMSProcessableByteArray(toBeSigned);
    provider = new CMSTestProvider();

    CMSSignedData signedData = generator.generate(content, false, provider);

    byte[] cmsSignature = signedData.getEncoded();
    LOG.debug("CMS signature: " + ASN1Dump.dumpAsString(new ASN1StreamParser(cmsSignature).readObject()));

    // verify
    content = new CMSProcessableByteArray(toBeSigned);
    signedData = new CMSSignedData(content, cmsSignature);
    SignerInformationStore signers = signedData.getSignerInfos();
    Iterator<SignerInformation> iter = signers.getSigners().iterator();
    while (iter.hasNext()) {
        SignerInformation signer = iter.next();
        SignerId signerId = signer.getSID();
        LOG.debug("signer: " + signerId);
        X509CertSelector signerConstraints = new JcaX509CertSelectorConverter().getCertSelector(signerId);
        LOG.debug("signerConstraints: " + signerConstraints);
        assertTrue(signerConstraints.match(certificate));
        assertTrue(signer.verify(keyPair.getPublic(), BouncyCastleProvider.PROVIDER_NAME));
    }
    LOG.debug("content type: " + signedData.getSignedContentTypeOID());
}