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

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

Introduction

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

Prototype

public SignerInfoGenerator build(String algorithmName, PrivateKey privateKey, byte[] keyIdentifier)
            throws OperatorCreationException 

Source Link

Usage

From source file:com.mycompany.mavenproject1.Signer.java

public void init(P12KeyContainer keyContainer)
        throws CertificateEncodingException, OperatorCreationException, CMSException {

    Security.addProvider(new BouncyCastleProvider());

    List certList = new ArrayList();
    X509Certificate cert = (X509Certificate) keyContainer.certificate;
    certList.add(cert);//from www . j  a va  2  s  . co  m
    Store certsStore = new JcaCertStore(certList);
    generator = new CMSSignedDataGenerator();

    JcaSimpleSignerInfoGeneratorBuilder genInfo = new JcaSimpleSignerInfoGeneratorBuilder();
    genInfo.setProvider("BC");
    genInfo.setDirectSignature(true);

    SignerInfoGenerator signerInfoGenerator = genInfo.build("GOST3411withECGOST3410",
            (PrivateKey) keyContainer.privateKey, cert);

    generator.addSignerInfoGenerator(signerInfoGenerator);
    generator.addCertificates(certsStore);
}

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 va  2  s. c o m*/
        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;
}