Example usage for org.bouncycastle.x509 AttributeCertificateIssuer AttributeCertificateIssuer

List of usage examples for org.bouncycastle.x509 AttributeCertificateIssuer AttributeCertificateIssuer

Introduction

In this page you can find the example usage for org.bouncycastle.x509 AttributeCertificateIssuer AttributeCertificateIssuer.

Prototype

public AttributeCertificateIssuer(X509Principal principal) 

Source Link

Usage

From source file:org.candlepin.resource.test.cert.test.CertTest.java

License:Open Source License

@Test
public void testCertExample() throws Exception {

    Security.addProvider(new BouncyCastleProvider());

    ///*from   w  w  w  . j a v a  2s. c o m*/
    // set up the keys
    //
    KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
    PrivateKey caPrivKey = fact.generatePrivate(caPrivKeySpec);
    PublicKey caPubKey = fact.generatePublic(caPubKeySpec);
    //PrivateKey privKey =
    fact.generatePrivate(privKeySpec);
    PublicKey pubKey = fact.generatePublic(pubKeySpec);

    //
    // note in this case we are using the CA certificate for both the client
    // cetificate
    // and the attribute certificate. This is to make the vcode simpler to
    // read, in practice
    // the CA for the attribute certificate should be different to that of
    // the client certificate
    //
    X509Certificate caCert = AttrCertExample.createAcIssuerCert(caPubKey, caPrivKey);
    X509Certificate clientCert = AttrCertExample.createClientCert(pubKey, caPrivKey, caPubKey);
    // Instantiate a new AC generator
    X509V2AttributeCertificateGenerator acGen = new X509V2AttributeCertificateGenerator();

    acGen.reset();

    //
    // Holder: here we use the IssuerSerial form
    //
    acGen.setHolder(new AttributeCertificateHolder(clientCert));

    // set the Issuer
    acGen.setIssuer(new AttributeCertificateIssuer(caCert.getSubjectX500Principal()));

    //
    // serial number (as it's an example we don't have to keep track of the
    // serials anyway
    //
    acGen.setSerialNumber(BigInteger.ONE);

    // not Before
    acGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));

    // not After
    acGen.setNotAfter(new Date(System.currentTimeMillis() + 50000));

    // signature Algorithmus
    acGen.setSignatureAlgorithm("SHA1WithRSAEncryption");

    // the actual attributes
    GeneralName roleName = new GeneralName(GeneralName.rfc822Name, "DAU123456789");
    ASN1EncodableVector roleSyntax = new ASN1EncodableVector();
    roleSyntax.add(roleName);

    // roleSyntax OID: 2.5.24.72
    X509Attribute attributes = new X509Attribute("2.5.24.72", new DERSequence(roleSyntax));

    acGen.addAttribute(attributes);

    // finally create the AC
    X509V2AttributeCertificate att = (X509V2AttributeCertificate) acGen.generate(caPrivKey, "BC");

    //String encoded = new String(att.getEncoded());
    //System.out.println("CERT CERT: " + encoded);
    //KeyStore store = KeyStore.getInstance("PKCS12");
    //String pass = "redhat";

    /*FileOutputStream fout = new FileOutputStream("/tmp/foo.file");
    store.load(null, null);
    store.store(fout, pass.toCharArray());
    X509CertificateObject ccert = new
    X509CertificateObject(new X509CertificateStructure(new DERSequence(att)));*/
    //
    // starting here, we parse the newly generated AC
    //

    // Holder

    AttributeCertificateHolder h = att.getHolder();
    if (h.match(clientCert)) {
        if (h.getEntityNames() != null) {
            //                System.out.println(h.getEntityNames().length +
            //                    " entity names found");
        }
        if (h.getIssuer() != null) {
            //                System.out.println(h.getIssuer().length +
            //                    " issuer names found, serial number " +
            //                    h.getSerialNumber());
        }
        //            System.out.println("Matches original client x509 cert");
    }

    // Issuer

    AttributeCertificateIssuer issuer = att.getIssuer();
    if (issuer.match(caCert)) {
        if (issuer.getPrincipals() != null) {
            //                System.out.println(issuer.getPrincipals().length +
            //                    " entity names found");
        }
        //            System.out.println("Matches original ca x509 cert");
    }

    // Dates
    //        System.out.println("valid not before: " + att.getNotBefore());
    //        System.out.println("valid not before: " + att.getNotAfter());

    // check the dates, an exception is thrown in checkValidity()...

    try {
        att.checkValidity();
        att.checkValidity(new Date());
    } catch (Exception e) {
        System.out.println(e);
    }

    // verify

    try {
        att.verify(caPubKey, "BC");
    } catch (Exception e) {
        System.out.println(e);
    }

    // Attribute
    X509Attribute[] attribs = att.getAttributes();
    //        System.out.println("cert has " + attribs.length + " attributes:");
    for (int i = 0; i < attribs.length; i++) {
        X509Attribute a = attribs[i];
        //            System.out.println("OID: " + a.getOID());

        // currently we only check for the presence of a 'RoleSyntax'
        // attribute

        if (a.getOID().equals("2.5.24.72")) {
            //                System.out.println("rolesyntax read from cert!");
        }
    }
}

From source file:test.unit.be.fedict.trust.TrustTestUtils.java

License:Open Source License

public static X509V2AttributeCertificate createAttributeCertificate(X509Certificate holderCertificate,
        X509Certificate issuerCertificate, PrivateKey issuerPrivateKey, Date notBefore, Date notAfter)
        throws Exception {

    X509V2AttributeCertificateGenerator acGen = new X509V2AttributeCertificateGenerator();
    acGen.reset();//from w  w  w  .  ja  v a 2  s  . co  m
    acGen.setHolder(new AttributeCertificateHolder(holderCertificate));
    acGen.setIssuer(new AttributeCertificateIssuer(issuerCertificate.getSubjectX500Principal()));
    acGen.setSerialNumber(new BigInteger("1"));
    acGen.setNotBefore(notBefore);
    acGen.setNotAfter(notAfter);
    acGen.setSignatureAlgorithm("SHA512WithRSAEncryption");
    GeneralName roleName = new GeneralName(GeneralName.rfc822Name, "RoleName");
    ASN1EncodableVector roleSyntax = new ASN1EncodableVector();
    roleSyntax.add(roleName);
    X509Attribute attributes = new X509Attribute("2.5.24.72", new DERSequence(roleSyntax));
    acGen.addAttribute(attributes);

    return (X509V2AttributeCertificate) acGen.generate(issuerPrivateKey, "BC");

}