Example usage for org.bouncycastle.cms.jcajce JcaSignerInfoGeneratorBuilder setDirectSignature

List of usage examples for org.bouncycastle.cms.jcajce JcaSignerInfoGeneratorBuilder setDirectSignature

Introduction

In this page you can find the example usage for org.bouncycastle.cms.jcajce JcaSignerInfoGeneratorBuilder setDirectSignature.

Prototype

public JcaSignerInfoGeneratorBuilder setDirectSignature(boolean hasNoSignedAttributes) 

Source Link

Document

If the passed in flag is true, the signer signature will be based on the data, not a collection of signed attributes, and no signed attributes will be included.

Usage

From source file:com.zotoh.crypto.CryptoUte.java

License:Open Source License

/**
 * @param key// w  w  w. ja  v a 2  s  .  c om
 * @param certs
 * @param algo
 * @param data
 * @return
 * @throws NoSuchAlgorithmException
 * @throws InvalidAlgorithmParameterException
 * @throws CertStoreException
 * @throws IOException
 * @throws CertificateEncodingException
 * @throws GeneralSecurityException
 */
public static byte[] pkcsDigSig(PrivateKey key, Certificate[] certs, SigningAlgo algo, StreamData data)
        throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, CertStoreException, IOException,
        CertificateEncodingException, GeneralSecurityException {

    tstObjArg("input-content", data);
    tstObjArg("private-key", key);

    CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
    Provider prov = Crypto.getInstance().getProvider();
    List<Certificate> lst = asList(true, certs);
    CMSTypedData cms;
    X509Certificate cert = (X509Certificate) lst.get(0);

    try {
        ContentSigner cs = new JcaContentSignerBuilder(algo.toString()).setProvider(prov).build(key);

        JcaSignerInfoGeneratorBuilder bdr = new JcaSignerInfoGeneratorBuilder(
                new JcaDigestCalculatorProviderBuilder().setProvider(prov).build());
        bdr.setDirectSignature(true);

        gen.addSignerInfoGenerator(bdr.build(cs, cert));
        gen.addCertificates(new JcaCertStore(lst));

        if (data.isDiskFile()) {
            cms = new CMSProcessableFile(data.getFileRef());
        } else {
            cms = new CMSProcessableByteArray(data.getBytes());
        }

        return gen.generate(cms, false).getEncoded();
    } catch (OperatorCreationException e) {
        throw new GeneralSecurityException(e);
    } catch (CMSException e) {
        throw new GeneralSecurityException(e);
    }

}

From source file:com.zotoh.crypto.CryptoUte.java

License:Open Source License

private static SMIMESignedGenerator makeSignerGentor(PrivateKey key, Certificate[] certs, SigningAlgo algo)
        throws CertStoreException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        GeneralSecurityException, CertificateEncodingException {

    SMIMESignedGenerator gen = new SMIMESignedGenerator("base64");
    List<Certificate> lst = asList(true, certs);

    ASN1EncodableVector signedAttrs = new ASN1EncodableVector();
    SMIMECapabilityVector caps = new SMIMECapabilityVector();

    caps.addCapability(SMIMECapability.dES_EDE3_CBC);
    caps.addCapability(SMIMECapability.rC2_CBC, 128);
    caps.addCapability(SMIMECapability.dES_CBC);

    signedAttrs.add(new SMIMECapabilitiesAttribute(caps));

    X509Certificate x0 = (X509Certificate) certs[0];
    X509Certificate issuer = x0;/*  ww  w. j a va  2s  .c o  m*/
    X500Principal issuerDN;

    if (certs.length > 1) {
        issuer = (X509Certificate) certs[1];
    }

    issuerDN = issuer.getSubjectX500Principal();
    x0 = (X509Certificate) certs[0];

    //
    // add an encryption key preference for encrypted responses -
    // normally this would be different from the signing certificate...
    //

    IssuerAndSerialNumber issAndSer = new IssuerAndSerialNumber(X500Name.getInstance(issuerDN.getEncoded()),
            x0.getSerialNumber());
    Provider prov = Crypto.getInstance().getProvider();

    signedAttrs.add(new SMIMEEncryptionKeyPreferenceAttribute(issAndSer));

    try {
        JcaSignerInfoGeneratorBuilder bdr = new JcaSignerInfoGeneratorBuilder(
                new JcaDigestCalculatorProviderBuilder().setProvider(prov).build());
        bdr.setDirectSignature(true);

        ContentSigner cs = new JcaContentSignerBuilder(algo.toString()).setProvider(prov).build(key);

        bdr.setSignedAttributeGenerator(
                new DefaultSignedAttributeTableGenerator(new AttributeTable(signedAttrs)));

        gen.addSignerInfoGenerator(bdr.build(cs, x0));
        gen.addCertificates(new JcaCertStore(lst));

        return gen;
    } catch (OperatorCreationException e) {
        throw new GeneralSecurityException(e);
    }
}