Example usage for org.bouncycastle.asn1.pkcs IssuerAndSerialNumber getEncoded

List of usage examples for org.bouncycastle.asn1.pkcs IssuerAndSerialNumber getEncoded

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs IssuerAndSerialNumber getEncoded.

Prototype

public byte[] getEncoded() throws IOException 

Source Link

Document

Return the default BER or DER encoding for this object.

Usage

From source file:eu.europa.esig.dss.xades.signature.XAdESBuilder.java

License:Open Source License

/**
 * Incorporates the certificate's references as a child of the given parent node. The first element of the {@code X509Certificate} {@code List} MUST be the signing
 * certificate.//from   ww  w .  ja va  2 s  . c  o m
 *
 * @param signingCertificateDom DOM parent element
 * @param certificates          {@code List} of the certificates to be incorporated
 */
protected void incorporateCertificateRef(final Element signingCertificateDom,
        final List<CertificateToken> certificates) {

    for (final CertificateToken certificate : certificates) {

        final Element certDom = DSSXMLUtils.addElement(documentDom, signingCertificateDom, XAdES, XADES_CERT);

        final Element certDigestDom = DSSXMLUtils.addElement(documentDom, certDom, XAdES, XADES_CERT_DIGEST);

        final DigestAlgorithm signingCertificateDigestMethod = params.getSigningCertificateDigestMethod();
        incorporateDigestMethod(certDigestDom, signingCertificateDigestMethod);

        final InMemoryDocument inMemoryCertificate = new InMemoryDocument(certificate.getEncoded());
        incorporateDigestValue(certDigestDom, signingCertificateDigestMethod, inMemoryCertificate);

        if (params.isEn319132()) {
            try {
                final Element issuerSerialDom = DSSXMLUtils.addElement(documentDom, certDom, XAdES,
                        XADES_ISSUER_SERIAL_V2);

                String name = certificate.getCertificate().getIssuerX500Principal().getName();
                IssuerAndSerialNumber issuerAndSerial = new IssuerAndSerialNumber(new X500Name(name),
                        certificate.getCertificate().getSerialNumber());
                byte[] issuer = Base64.encodeBase64(issuerAndSerial.getEncoded());
                DSSXMLUtils.setTextNode(documentDom, issuerSerialDom, new String(issuer));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        } else {
            final Element issuerSerialDom = DSSXMLUtils.addElement(documentDom, certDom, XAdES,
                    XADES_ISSUER_SERIAL);

            final Element x509IssuerNameDom = DSSXMLUtils.addElement(documentDom, issuerSerialDom, XMLNS,
                    DS_X509_ISSUER_NAME);
            final String issuerX500PrincipalName = certificate.getIssuerX500Principal().getName();
            DSSXMLUtils.setTextNode(documentDom, x509IssuerNameDom, issuerX500PrincipalName);

            final Element x509SerialNumberDom = DSSXMLUtils.addElement(documentDom, issuerSerialDom, XMLNS,
                    DS_X509_SERIAL_NUMBER);
            final BigInteger serialNumber = certificate.getSerialNumber();
            final String serialNumberString = new String(serialNumber.toString());
            DSSXMLUtils.setTextNode(documentDom, x509SerialNumberDom, serialNumberString);
        }
    }
}