Example usage for org.bouncycastle.cert AttributeCertificateHolder AttributeCertificateHolder

List of usage examples for org.bouncycastle.cert AttributeCertificateHolder AttributeCertificateHolder

Introduction

In this page you can find the example usage for org.bouncycastle.cert AttributeCertificateHolder AttributeCertificateHolder.

Prototype

public AttributeCertificateHolder(X500Name principal) 

Source Link

Document

Create a holder using the entityName option based on the passed in principal.

Usage

From source file:AAModulePackage.ACHelper.java

public static X509AttributeCertificateHolder generateAttributeCertificate(X509CertificateHolder issuerCert,
        X509CertificateHolder associatedCert, PrivateKey pk, String role, String record_id,
        String record_subject, String[] record_types, String[] actions_taken) {
    //Set up the validity period.
    Date startDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
    Date endDate = new Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000);

    //AttributeCertificateHolder is a wrapper class for AttributeCertificates, courtesy of the Legion of Bouncy Castle.
    AttributeCertificateIssuer certIssuer = new AttributeCertificateIssuer(issuerCert.getSubject());

    /*//from  w  w  w.  j ava2s. c  om
    Please note the distinction between AttributeCertificateHolder which appears to be the
    Entity in possession of the certificate, while X509AttributeCertificateHolder is a
    wrapper class for the actual certificate itself.
     */

    AttributeCertificateHolder holder = new AttributeCertificateHolder(associatedCert);
    X509v2AttributeCertificateBuilder builder = new X509v2AttributeCertificateBuilder(holder, certIssuer,
            BigInteger.valueOf(System.currentTimeMillis()), startDate, endDate);

    builder.addAttribute(NewAttributeIdentifiers.role, new DERGeneralString(role));
    builder.addAttribute(NewAttributeIdentifiers.record_id, new DERGeneralString(record_id));
    builder.addAttribute(NewAttributeIdentifiers.record_subject, new DERGeneralString(record_subject));
    builder.addAttribute(NewAttributeIdentifiers.time_stamp, new DERGeneralizedTime(new Date()));

    //record_types
    ArrayList<ASN1Encodable> rts = new ArrayList();
    for (String s : record_types) {
        rts.add(new DERGeneralString(s));
    }
    ASN1Encodable[] recTypes = rts.toArray(new DERGeneralString[rts.size()]);

    builder.addAttribute(NewAttributeIdentifiers.record_type, recTypes);

    //actions_taken
    ArrayList<ASN1Encodable> acts = new ArrayList();
    for (String s : actions_taken) {
        acts.add(new DERGeneralString(s));
    }
    ASN1Encodable[] actionsTaken = acts.toArray(new DERGeneralString[acts.size()]);
    builder.addAttribute(NewAttributeIdentifiers.actions_taken, actionsTaken);

    //Build the certificate
    X509AttributeCertificateHolder attrCert = null;
    try {
        //builds the attribute certificate, and signs it with the owner's private key.
        attrCert = builder
                .build(new JcaContentSignerBuilder("SHA256withRSAEncryption").setProvider("BC").build(pk));
    } catch (OperatorCreationException e) {
        e.printStackTrace();
    }

    System.out.println("ATTRIBUTE CERTIFICATE Successfully generated.");

    return attrCert;
}

From source file:org.xwiki.crypto.pkix.internal.BcStoreX509CertificateProvider.java

License:Open Source License

@Override
public Collection<CertifiedPublicKey> getCertificate(PrincipalIndentifier subject) {
    AttributeCertificateHolder selector = new AttributeCertificateHolder(BcUtils.getX500Name(subject));

    try {//from   w  w w. j  av a  2  s  .co m
        Collection<?> matches = this.store.getMatches(selector);
        Collection<CertifiedPublicKey> result = new ArrayList<CertifiedPublicKey>(matches.size());
        for (Object holder : matches) {
            if (holder instanceof X509CertificateHolder) {
                result.add(BcUtils.convertCertificate(this.factory, (X509CertificateHolder) holder));
            }
        }
        return (!result.isEmpty()) ? result : null;
    } catch (Throwable t) {
        return null;
    }
}