Example usage for org.bouncycastle.asn1.ess OtherCertID OtherCertID

List of usage examples for org.bouncycastle.asn1.ess OtherCertID OtherCertID

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.ess OtherCertID OtherCertID.

Prototype

public OtherCertID(AlgorithmIdentifier algId, byte[] digest, IssuerSerial issuerSerial) 

Source Link

Usage

From source file:org.demoiselle.signer.policy.impl.cades.pkcs7.attribute.impl.CertificateRefs.java

License:Open Source License

@Override
public Attribute getValue() throws SignerException {

    try {/*from  ww w  . j  a  va  2  s. c o  m*/
        int chainSize = certificates.length - 1;
        OtherCertID[] arrayOtherCertID = new OtherCertID[chainSize];
        for (int i = 1; i <= chainSize; i++) {
            X509Certificate issuerCert = null;
            X509Certificate cert = (X509Certificate) certificates[i];
            if (i < chainSize) {
                issuerCert = (X509Certificate) certificates[i + 1];
            } else { // raiz
                issuerCert = (X509Certificate) certificates[i];
            }
            Digest digest = DigestFactory.getInstance().factoryDefault();
            digest.setAlgorithm(DigestAlgorithmEnum.SHA_256);
            byte[] certHash = digest.digest(cert.getEncoded());
            X500Name dirName = new X500Name(issuerCert.getSubjectX500Principal().getName());
            GeneralName name = new GeneralName(dirName);
            GeneralNames issuer = new GeneralNames(name);
            ASN1Integer serialNumber = new ASN1Integer(cert.getSerialNumber());
            IssuerSerial issuerSerial = new IssuerSerial(issuer, serialNumber);
            AlgorithmIdentifier algId = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256);
            OtherCertID otherCertID = new OtherCertID(algId, certHash, issuerSerial);
            arrayOtherCertID[i - 1] = otherCertID;
        }

        return new Attribute(new ASN1ObjectIdentifier(identifier),
                new DERSet(new ASN1Encodable[] { new DERSequence(arrayOtherCertID) }));
    } catch (CertificateEncodingException e) {
        throw new SignerException(e.getMessage());
    }
}