Example usage for org.bouncycastle.cms CMSSignedData getOtherRevocationInfo

List of usage examples for org.bouncycastle.cms CMSSignedData getOtherRevocationInfo

Introduction

In this page you can find the example usage for org.bouncycastle.cms CMSSignedData getOtherRevocationInfo.

Prototype

public Store getOtherRevocationInfo(ASN1ObjectIdentifier otherRevocationInfoFormat) 

Source Link

Document

Return any OtherRevocationInfo OtherRevInfo objects of the type indicated by otherRevocationInfoFormat in this SignedData structure.

Usage

From source file:eu.europa.ec.markt.dss.signature.cades.CAdESLevelBaselineLT.java

License:Open Source License

protected CMSSignedData postExtendCMSSignedData(CMSSignedData cmsSignedData,
        SignerInformation signerInformation, SignatureParameters parameters) {
    CAdESSignature cadesSignature = new CAdESSignature(cmsSignedData, signerInformation);
    cadesSignature.setDetachedContents(parameters.getDetachedContent());
    final ValidationContext validationContext = cadesSignature
            .getSignatureValidationContext(certificateVerifier);

    Store certificatesStore = cmsSignedData.getCertificates();
    final Store attributeCertificatesStore = cmsSignedData.getAttributeCertificates();
    Store crlsStore = cmsSignedData.getCRLs();
    Store otherRevocationInfoFormatStoreBasic = cmsSignedData
            .getOtherRevocationInfo(OCSPObjectIdentifiers.id_pkix_ocsp_basic);
    Store otherRevocationInfoFormatStoreOcsp = cmsSignedData
            .getOtherRevocationInfo(CMSObjectIdentifiers.id_ri_ocsp_response);

    final Set<CertificateToken> certificates = cadesSignature.getCertificatesForInclusion(validationContext);
    final Collection<X509CertificateHolder> newCertificateStore = new HashSet<X509CertificateHolder>(
            certificatesStore.getMatches(null));
    for (final CertificateToken certificateToken : certificates) {
        final X509CertificateHolder x509CertificateHolder = DSSUtils.getX509CertificateHolder(certificateToken);
        newCertificateStore.add(x509CertificateHolder);
    }/*  w ww. ja v  a  2 s .  com*/

    certificatesStore = new CollectionStore(newCertificateStore);

    final Collection<X509CRLHolder> newCrlsStore = new HashSet<X509CRLHolder>(crlsStore.getMatches(null));
    final DefaultAdvancedSignature.RevocationDataForInclusion revocationDataForInclusion = cadesSignature
            .getRevocationDataForInclusion(validationContext);
    for (final CRLToken crlToken : revocationDataForInclusion.crlTokens) {
        final X509CRLHolder x509CRLHolder = crlToken.getX509CrlHolder();
        newCrlsStore.add(x509CRLHolder);
    }
    crlsStore = new CollectionStore(newCrlsStore);

    final Collection<ASN1Primitive> newOtherRevocationInfoFormatStore = new HashSet<ASN1Primitive>(
            otherRevocationInfoFormatStoreBasic.getMatches(null));
    for (final OCSPToken ocspToken : revocationDataForInclusion.ocspTokens) {
        final BasicOCSPResp basicOCSPResp = ocspToken.getBasicOCSPResp();
        newOtherRevocationInfoFormatStore.add(DSSASN1Utils.toASN1Primitive(DSSUtils.getEncoded(basicOCSPResp)));
    }
    otherRevocationInfoFormatStoreBasic = new CollectionStore(newOtherRevocationInfoFormatStore);

    final CMSSignedDataBuilder cmsSignedDataBuilder = new CMSSignedDataBuilder(certificateVerifier);
    cmsSignedData = cmsSignedDataBuilder.regenerateCMSSignedData(cmsSignedData, parameters, certificatesStore,
            attributeCertificatesStore, crlsStore, otherRevocationInfoFormatStoreBasic,
            otherRevocationInfoFormatStoreOcsp);
    return cmsSignedData;
}

From source file:eu.europa.ec.markt.dss.signature.cades.CMSSignedDataBuilder.java

License:Open Source License

/**
 * Note://ww  w. ja  va2s. co m
 * Section 5.1 of RFC 3852 [4] requires that, the CMS SignedData version be set to 3 if certificates from
 * SignedData is present AND (any version 1 attribute certificates are present OR any SignerInfo structures
 * are version 3 OR eContentType from encapContentInfo is other than id-data). Otherwise, the CMS
 * SignedData version is required to be set to 1.
 * ---> CMS SignedData Version is handled automatically by BouncyCastle.
 *
 * @param parameters                 set of the driving signing parameters
 * @param contentSigner              the contentSigned to get the hash of the data to be signed
 * @param signerInfoGeneratorBuilder true if the unsigned attributes must be included
 * @param originalSignedData         the original signed data if extending an existing signature. null otherwise.  @return the bouncycastle signed data generator which will
 *                                   sign
 *                                   the document and add the required signed and unsigned CMS attributes
 * @throws eu.europa.ec.markt.dss.exception.DSSException
 */
protected CMSSignedDataGenerator createCMSSignedDataGenerator(final SignatureParameters parameters,
        final ContentSigner contentSigner, final SignerInfoGeneratorBuilder signerInfoGeneratorBuilder,
        final CMSSignedData originalSignedData) throws DSSException {
    try {

        final X509Certificate signingCertificate = parameters.getSigningCertificate();

        final CMSSignedDataGenerator generator = new CMSSignedDataGenerator();

        final X509CertificateHolder certHolder = DSSUtils.getX509CertificateHolder(signingCertificate);
        final SignerInfoGenerator signerInfoGenerator = signerInfoGeneratorBuilder.build(contentSigner,
                certHolder);

        generator.addSignerInfoGenerator(signerInfoGenerator);

        final Set<X509Certificate> newCertificateChain = new HashSet<X509Certificate>();

        if (originalSignedData != null) {

            generator.addSigners(originalSignedData.getSignerInfos());
            generator.addAttributeCertificates(originalSignedData.getAttributeCertificates());
            generator.addCRLs(originalSignedData.getCRLs());
            generator.addOtherRevocationInfo(OCSPObjectIdentifiers.id_pkix_ocsp_basic,
                    originalSignedData.getOtherRevocationInfo(OCSPObjectIdentifiers.id_pkix_ocsp_basic));
            generator.addOtherRevocationInfo(CMSObjectIdentifiers.id_ri_ocsp_response,
                    originalSignedData.getOtherRevocationInfo(CMSObjectIdentifiers.id_ri_ocsp_response));

            final Store certificates = originalSignedData.getCertificates();
            final Collection<X509CertificateHolder> certificatesMatches = certificates.getMatches(null);
            for (final X509CertificateHolder certificatesMatch : certificatesMatches) {
                newCertificateChain.add(DSSUtils.getCertificate(certificatesMatch));
            }
        }
        final List<X509Certificate> certificateChain = parameters.getCertificateChain();
        newCertificateChain.addAll(certificateChain);
        final Store jcaCertStore = getJcaCertStore(signingCertificate, newCertificateChain);
        generator.addCertificates(jcaCertStore);
        return generator;

    } catch (CMSException e) {
        throw new DSSException(e);
    } catch (OperatorCreationException e) {
        throw new DSSException(e);
    }
}

From source file:eu.europa.esig.dss.cades.signature.CAdESLevelBaselineLT.java

License:Open Source License

@Override
protected CMSSignedData postExtendCMSSignedData(CMSSignedData cmsSignedData,
        SignerInformation signerInformation, CAdESSignatureParameters parameters) {
    CAdESSignature cadesSignature = new CAdESSignature(cmsSignedData, signerInformation);
    cadesSignature.setDetachedContents(parameters.getDetachedContent());
    final ValidationContext validationContext = cadesSignature
            .getSignatureValidationContext(certificateVerifier);

    Store<X509CertificateHolder> certificatesStore = cmsSignedData.getCertificates();
    final Set<CertificateToken> certificates = cadesSignature.getCertificatesForInclusion(validationContext);
    final Collection<X509CertificateHolder> newCertificateStore = new HashSet<X509CertificateHolder>(
            certificatesStore.getMatches(null));
    for (final CertificateToken certificateToken : certificates) {
        final X509CertificateHolder x509CertificateHolder = DSSASN1Utils
                .getX509CertificateHolder(certificateToken);
        newCertificateStore.add(x509CertificateHolder);
    }/*  w  w w  . ja va  2s.c  om*/
    certificatesStore = new CollectionStore<X509CertificateHolder>(newCertificateStore);

    Store<X509CRLHolder> crlsStore = cmsSignedData.getCRLs();
    final Collection<X509CRLHolder> newCrlsStore = new HashSet<X509CRLHolder>(crlsStore.getMatches(null));
    final DefaultAdvancedSignature.RevocationDataForInclusion revocationDataForInclusion = cadesSignature
            .getRevocationDataForInclusion(validationContext);
    for (final CRLToken crlToken : revocationDataForInclusion.crlTokens) {
        final X509CRLHolder x509CRLHolder = getX509CrlHolder(crlToken);
        newCrlsStore.add(x509CRLHolder);
    }
    crlsStore = new CollectionStore<X509CRLHolder>(newCrlsStore);

    Store otherRevocationInfoFormatStoreBasic = cmsSignedData
            .getOtherRevocationInfo(OCSPObjectIdentifiers.id_pkix_ocsp_basic);
    final Collection<ASN1Primitive> newOtherRevocationInfoFormatStore = new HashSet<ASN1Primitive>(
            otherRevocationInfoFormatStoreBasic.getMatches(null));
    for (final OCSPToken ocspToken : revocationDataForInclusion.ocspTokens) {
        final BasicOCSPResp basicOCSPResp = ocspToken.getBasicOCSPResp();
        newOtherRevocationInfoFormatStore
                .add(DSSASN1Utils.toASN1Primitive(DSSASN1Utils.getEncoded(basicOCSPResp)));
    }
    otherRevocationInfoFormatStoreBasic = new CollectionStore(newOtherRevocationInfoFormatStore);

    Store attributeCertificatesStore = cmsSignedData.getAttributeCertificates();
    Store otherRevocationInfoFormatStoreOcsp = cmsSignedData
            .getOtherRevocationInfo(CMSObjectIdentifiers.id_ri_ocsp_response);

    final CMSSignedDataBuilder cmsSignedDataBuilder = new CMSSignedDataBuilder(certificateVerifier);
    cmsSignedData = cmsSignedDataBuilder.regenerateCMSSignedData(cmsSignedData, parameters, certificatesStore,
            attributeCertificatesStore, crlsStore, otherRevocationInfoFormatStoreBasic,
            otherRevocationInfoFormatStoreOcsp);
    return cmsSignedData;
}

From source file:eu.europa.esig.dss.cades.signature.CMSSignedDataBuilder.java

License:Open Source License

/**
 * Note://from  www . ja  v  a 2 s  .c  o  m
 * Section 5.1 of RFC 3852 [4] requires that, the CMS SignedData version be set to 3 if certificates from
 * SignedData is present AND (any version 1 attribute certificates are present OR any SignerInfo structures
 * are version 3 OR eContentType from encapContentInfo is other than id-data). Otherwise, the CMS
 * SignedData version is required to be set to 1.
 * ---> CMS SignedData Version is handled automatically by BouncyCastle.
 *
 * @param parameters                 set of the driving signing parameters
 * @param contentSigner              the contentSigned to get the hash of the data to be signed
 * @param signerInfoGeneratorBuilder true if the unsigned attributes must be included
 * @param originalSignedData         the original signed data if extending an existing signature. null otherwise.
 * @return the bouncycastle signed data generator which signs the document and adds the required signed and unsigned CMS attributes
 * @throws eu.europa.esig.dss.DSSException
 */
protected CMSSignedDataGenerator createCMSSignedDataGenerator(final CAdESSignatureParameters parameters,
        final ContentSigner contentSigner, final SignerInfoGeneratorBuilder signerInfoGeneratorBuilder,
        final CMSSignedData originalSignedData) throws DSSException {
    try {

        final CertificateToken signingCertificate = parameters.getSigningCertificate();

        final CMSSignedDataGenerator generator = new CMSSignedDataGenerator();

        final X509CertificateHolder certHolder = DSSASN1Utils.getX509CertificateHolder(signingCertificate);
        final SignerInfoGenerator signerInfoGenerator = signerInfoGeneratorBuilder.build(contentSigner,
                certHolder);

        generator.addSignerInfoGenerator(signerInfoGenerator);

        final Set<CertificateToken> certificateChain = new HashSet<CertificateToken>();

        if (originalSignedData != null) {

            generator.addSigners(originalSignedData.getSignerInfos());
            generator.addAttributeCertificates(originalSignedData.getAttributeCertificates());
            generator.addCRLs(originalSignedData.getCRLs());
            generator.addOtherRevocationInfo(id_pkix_ocsp_basic,
                    originalSignedData.getOtherRevocationInfo(id_pkix_ocsp_basic));
            generator.addOtherRevocationInfo(id_ri_ocsp_response,
                    originalSignedData.getOtherRevocationInfo(id_ri_ocsp_response));

            final Store certificates = originalSignedData.getCertificates();
            final Collection<X509CertificateHolder> certificatesMatches = certificates.getMatches(null);
            for (final X509CertificateHolder certificatesMatch : certificatesMatches) {

                final CertificateToken x509Certificate = DSSASN1Utils.getCertificate(certificatesMatch);
                certificateChain.add(x509Certificate);
            }
        }
        certificateChain.add(parameters.getSigningCertificate());
        certificateChain.addAll(parameters.getCertificateChain());

        final boolean trustAnchorBPPolicy = parameters.bLevel().isTrustAnchorBPPolicy();
        final Store jcaCertStore = getJcaCertStore(certificateChain, trustAnchorBPPolicy);
        generator.addCertificates(jcaCertStore);
        return generator;
    } catch (CMSException e) {
        throw new DSSException(e);
    } catch (OperatorCreationException e) {
        throw new DSSException(e);
    }
}