Example usage for org.bouncycastle.cms CMSAttributeTableGenerator CMSAttributeTableGenerator

List of usage examples for org.bouncycastle.cms CMSAttributeTableGenerator CMSAttributeTableGenerator

Introduction

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

Prototype

CMSAttributeTableGenerator

Source Link

Usage

From source file:eu.europa.ec.markt.dss.signature.pades.PadesCMSSignedDataBuilder.java

License:Open Source License

/**
 * @param parameters the parameters of the signature containing values for the attributes
 * @return a SignerInfoGeneratorBuilder that generate the signed and unsigned attributes according to the CAdESLevelBaselineB and
 * PAdESLevelBaselineB/*from  w w  w . j a  v  a 2s . c o  m*/
 */
protected SignerInfoGeneratorBuilder getSignerInfoGeneratorBuilder(final SignatureParameters parameters,
        final byte[] messageDigest) {

    final CAdESLevelBaselineB cAdESLevelBaselineB = new CAdESLevelBaselineB(true);
    final PAdESLevelBaselineB pAdESProfileEPES = new PAdESLevelBaselineB();

    final DigestCalculatorProvider digestCalculatorProvider = new BcDigestCalculatorProvider();

    SignerInfoGeneratorBuilder signerInfoGeneratorBuilder = new SignerInfoGeneratorBuilder(
            digestCalculatorProvider);
    signerInfoGeneratorBuilder = signerInfoGeneratorBuilder
            .setSignedAttributeGenerator(new CMSAttributeTableGenerator() {

                @SuppressWarnings("unchecked")
                @Override
                public AttributeTable getAttributes(@SuppressWarnings("rawtypes") Map params)
                        throws CMSAttributeTableGenerationException {

                    return pAdESProfileEPES.getSignedAttributes(params, cAdESLevelBaselineB, parameters,
                            messageDigest);
                }
            });

    signerInfoGeneratorBuilder.setUnsignedAttributeGenerator(new CMSAttributeTableGenerator() {
        @Override
        public AttributeTable getAttributes(Map params) throws CMSAttributeTableGenerationException {
            return pAdESProfileEPES.getUnsignedAttributes();
        }
    });

    return signerInfoGeneratorBuilder;
}

From source file:eu.europa.ec.markt.dss.signature.pades.PAdESProfileEPES.java

License:Open Source License

CMSSignedDataGenerator createCMSSignedDataGenerator(ContentSigner contentSigner,
        DigestCalculatorProvider digestCalculatorProvider, final SignatureParameters parameters,
        final byte[] messageDigest) throws IOException {
    try {/*from  ww  w  . j a  v a2  s .  c  om*/

        CMSSignedDataGenerator generator = new CMSSignedDataGenerator();

        X509Certificate signerCertificate = parameters.getSigningCertificate();

        X509CertificateHolder certHolder = new X509CertificateHolder(signerCertificate.getEncoded());

        SignerInfoGeneratorBuilder sigenb = new SignerInfoGeneratorBuilder(digestCalculatorProvider);

        final CAdESProfileEPES profile = new CAdESProfileEPES(true);

        sigenb = sigenb.setSignedAttributeGenerator(new CMSAttributeTableGenerator() {
            @Override
            public AttributeTable getAttributes(Map params) throws CMSAttributeTableGenerationException {
                Hashtable clone = (Hashtable) profile.getSignedAttributes(parameters).clone();

                if (!clone.containsKey(CMSAttributes.contentType)) {
                    DERObjectIdentifier contentType = (DERObjectIdentifier) params
                            .get(CMSAttributeTableGenerator.CONTENT_TYPE);

                    // contentType will be null if we're trying to generate a counter signature.
                    if (contentType != null) {
                        Attribute attr = new Attribute(CMSAttributes.contentType, new DERSet(contentType));
                        clone.put(attr.getAttrType(), attr);
                    }
                }

                if (!clone.containsKey(CMSAttributes.messageDigest)) {
                    System.out.println("Digest propos : "
                            + org.apache.commons.codec.binary.Hex.encodeHexString(messageDigest));
                    // byte[] messageDigest = (byte[]) params.get(CMSAttributeTableGenerator.DIGEST);
                    Attribute attr = new Attribute(CMSAttributes.messageDigest,
                            new DERSet(new DEROctetString(messageDigest)));
                    clone.put(attr.getAttrType(), attr);
                }

                if (parameters.getCommitmentTypeIndication() != null
                        && !parameters.getCommitmentTypeIndication().isEmpty()) {
                    ASN1EncodableVector vector = new ASN1EncodableVector();
                    for (String id : parameters.getCommitmentTypeIndication()) {
                        vector.add(new DERObjectIdentifier(id));
                    }
                    DERSet set = new DERSet(new DERSequence(vector));
                    Attribute attr = new Attribute(new DERObjectIdentifier("1.2.840.113549.1.9.16.2.16"), set);
                    clone.put(attr.getAttrType(), attr);
                }

                return new AttributeTable(clone);
            }
        });

        // sigenb.setUnsignedAttributeGenerator(new SimpleAttributeTableGenerator(new AttributeTable(
        // new Hashtable<ASN1ObjectIdentifier, ASN1Encodable>())));

        /*
         * We don't include a unsigned attribute table if not needed : a unsignedAttrs of signerInfo includes no
         * Attribute, UnsignedAttributes ::= SET SIZE (1..MAX) OF Attribute(defined in RFC3852).
         */
        SignerInfoGenerator sigen = sigenb.build(contentSigner, certHolder);

        generator.addSignerInfoGenerator(sigen);

        Collection<X509Certificate> certs = new ArrayList<X509Certificate>();
        if (parameters.getCertificateChain() == null
                || !parameters.getCertificateChain().contains(parameters.getSigningCertificate())) {
            certs.add(parameters.getSigningCertificate());
        }
        certs.addAll(parameters.getCertificateChain());
        JcaCertStore certStore = new JcaCertStore(certs);
        generator.addCertificates(certStore);

        System.out.println("Gnrator cr");
        return generator;

    } catch (CertificateException e) {
        throw new IOException(e);
    } catch (OperatorCreationException e) {
        throw new IOException(e);
    } catch (CMSException e) {
        throw new IOException(e);
    }

}

From source file:eu.europa.esig.dss.pades.signature.PadesCMSSignedDataBuilder.java

License:Open Source License

/**
 * @param parameters the parameters of the signature containing values for the attributes
 * @return a SignerInfoGeneratorBuilder that generate the signed and unsigned attributes according to the CAdESLevelBaselineB and
 * PAdESLevelBaselineB//from  w ww  .jav  a 2  s  .  c  o m
 */
protected SignerInfoGeneratorBuilder getSignerInfoGeneratorBuilder(final PAdESSignatureParameters parameters,
        final byte[] messageDigest) {

    final CAdESLevelBaselineB cAdESLevelBaselineB = new CAdESLevelBaselineB(true);
    final PAdESLevelBaselineB pAdESProfileB = new PAdESLevelBaselineB();

    final DigestCalculatorProvider digestCalculatorProvider = new BcDigestCalculatorProvider();

    SignerInfoGeneratorBuilder signerInfoGeneratorBuilder = new SignerInfoGeneratorBuilder(
            digestCalculatorProvider);

    signerInfoGeneratorBuilder = signerInfoGeneratorBuilder
            .setSignedAttributeGenerator(new CMSAttributeTableGenerator() {
                @Override
                public AttributeTable getAttributes(@SuppressWarnings("rawtypes") Map params)
                        throws CMSAttributeTableGenerationException {
                    return pAdESProfileB.getSignedAttributes(params, cAdESLevelBaselineB, parameters,
                            messageDigest);
                }
            });

    signerInfoGeneratorBuilder = signerInfoGeneratorBuilder
            .setUnsignedAttributeGenerator(new CMSAttributeTableGenerator() {
                @Override
                public AttributeTable getAttributes(@SuppressWarnings("rawtypes") Map params)
                        throws CMSAttributeTableGenerationException {
                    return pAdESProfileB.getUnsignedAttributes();
                }
            });

    return signerInfoGeneratorBuilder;
}