Example usage for org.bouncycastle.cms.jcajce JcaSimpleSignerInfoGeneratorBuilder setSignedAttributeGenerator

List of usage examples for org.bouncycastle.cms.jcajce JcaSimpleSignerInfoGeneratorBuilder setSignedAttributeGenerator

Introduction

In this page you can find the example usage for org.bouncycastle.cms.jcajce JcaSimpleSignerInfoGeneratorBuilder setSignedAttributeGenerator.

Prototype

public JcaSimpleSignerInfoGeneratorBuilder setSignedAttributeGenerator(AttributeTable attrTable) 

Source Link

Document

set up a DefaultSignedAttributeTableGenerator primed with the passed in AttributeTable.

Usage

From source file:net.markenwerk.utils.mail.smime.SmimeUtil.java

License:Open Source License

private static SignerInfoGenerator getInfoGenerator(SmimeKey smimeKey)
        throws OperatorCreationException, CertificateEncodingException {
    JcaSimpleSignerInfoGeneratorBuilder builder = new JcaSimpleSignerInfoGeneratorBuilder();
    builder.setSignedAttributeGenerator(new AttributeTable(getSignedAttributes(smimeKey)));
    builder.setProvider(BouncyCastleProvider.PROVIDER_NAME);

    PrivateKey privateKey = smimeKey.getPrivateKey();
    X509Certificate certificate = smimeKey.getCertificate();
    SignerInfoGenerator infoGenerator = builder.build("SHA256withRSA", privateKey, certificate);
    return infoGenerator;
}

From source file:org.votingsystem.signature.util.PDFContentSigner.java

License:Open Source License

public CMSSignedData genSignedData(byte[] signatureHash, CMSAttributeTableGenerator unsAttr) throws Exception {
    CMSProcessable content = new CMSProcessableByteArray(signatureHash);
    ByteArrayOutputStream out = null;
    if (content != null) {
        out = new ByteArrayOutputStream();
        content.write(out);//from   w  ww .  j  a va2 s.c  om
        out.close();
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(out.toByteArray());
    MessageDigest softwareDigestEngine = MessageDigest.getInstance(signatureDigestAlg);
    int bytesRead;
    byte[] dataBuffer = new byte[4096];
    while ((bytesRead = bais.read(dataBuffer)) >= 0) {
        softwareDigestEngine.update(dataBuffer, 0, bytesRead);
    }
    byte[] hash = softwareDigestEngine.digest();
    CertStore certsAndCRLs = CertStore.getInstance(CERT_STORE_TYPE,
            new CollectionCertStoreParameters(Arrays.asList(signerCertChain)), ContextVS.PROVIDER);
    addCertificatesAndCRLs(certsAndCRLs);
    CMSAttributeTableGenerator sAttr = new DefaultSignedAttributeTableGenerator();
    ASN1ObjectIdentifier contentTypeOID = new ASN1ObjectIdentifier(CMSSignedGenerator.DATA);
    Map parameters = getBaseParameters(contentTypeOID,
            new AlgorithmIdentifier(new DERObjectIdentifier(pdfDigestObjectIdentifier), new DERNull()), hash);
    AttributeTable attributeTable = sAttr.getAttributes(Collections.unmodifiableMap(parameters));
    //String signatureHashStr = new String(Base64.encode(signatureHash));
    JcaSimpleSignerInfoGeneratorBuilder jcaSignerInfoGeneratorBuilder = new JcaSimpleSignerInfoGeneratorBuilder();
    jcaSignerInfoGeneratorBuilder = jcaSignerInfoGeneratorBuilder.setProvider(ContextVS.PROVIDER);
    jcaSignerInfoGeneratorBuilder.setSignedAttributeGenerator(attributeTable);
    jcaSignerInfoGeneratorBuilder.setUnsignedAttributeGenerator(unsAttr);
    SignerInfoGenerator signerInfoGenerator = jcaSignerInfoGeneratorBuilder.build(signatureMechanism,
            privateKey, userCert);
    SignerInfo signerInfo = signerInfoGenerator.generate(contentTypeOID);
    List<SignerInfo> signerInfoList = new ArrayList<SignerInfo>();
    signerInfoList.add(signerInfo);
    log.info(" -- userCert: " + userCert.getSubjectDN().getName());
    CMSSignedData signedData = getCMSSignedData(CMSSignedGenerator.DATA, content, true,
            CMSUtils.getProvider("BC"), true, signerInfoList);
    return signedData;
}