Example usage for org.bouncycastle.mail.smime SMIMESignedGenerator RFC3851_MICALGS

List of usage examples for org.bouncycastle.mail.smime SMIMESignedGenerator RFC3851_MICALGS

Introduction

In this page you can find the example usage for org.bouncycastle.mail.smime SMIMESignedGenerator RFC3851_MICALGS.

Prototype

Map RFC3851_MICALGS

To view the source code for org.bouncycastle.mail.smime SMIMESignedGenerator RFC3851_MICALGS.

Click Source Link

Usage

From source file:de.mendelson.util.security.BCCryptoHelper.java

/**
 * @param chain certificate chain, chain[0] is the signers certificate
 * itself Signs the data using S/MIME 3.1 - dont use if for S/MIME 3.2 or
 * higher/* ww w  .ja va2s .  c o m*/
 */
public MimeMultipart sign(MimeBodyPart body, Certificate[] chain, Key key, String digest) throws Exception {
    X509Certificate x509Cert = this.castCertificate(chain[0]);
    PrivateKey privKey = this.getPrivateKey(key);
    //call this generator with a S/MIME 3.1 compatible constructor as it defaults to RFC 5751 (other micalg values)
    SMIMESignedGenerator signedGenerator = new SMIMESignedGenerator(SMIMESignedGenerator.RFC3851_MICALGS);
    //add dont know
    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));
    if (digest.equalsIgnoreCase(ALGORITHM_SHA1)) {
        signedGenerator.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC")
                .setSignedAttributeGenerator(new AttributeTable(signedAttrs))
                .build("SHA1withRSA", privKey, x509Cert));
    } else if (digest.equalsIgnoreCase(ALGORITHM_SHA224)) {
        signedGenerator.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC")
                .setSignedAttributeGenerator(new AttributeTable(signedAttrs))
                .build("SHA224withRSA", privKey, x509Cert));
    } else if (digest.equalsIgnoreCase(ALGORITHM_SHA256)) {
        signedGenerator.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC")
                .setSignedAttributeGenerator(new AttributeTable(signedAttrs))
                .build("SHA256withRSA", privKey, x509Cert));
    } else if (digest.equalsIgnoreCase(ALGORITHM_SHA384)) {
        signedGenerator.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC")
                .setSignedAttributeGenerator(new AttributeTable(signedAttrs))
                .build("SHA384withRSA", privKey, x509Cert));
    } else if (digest.equalsIgnoreCase(ALGORITHM_SHA512)) {
        signedGenerator.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC")
                .setSignedAttributeGenerator(new AttributeTable(signedAttrs))
                .build("SHA512withRSA", privKey, x509Cert));
    } else if (digest.equalsIgnoreCase(ALGORITHM_MD5)) {
        signedGenerator.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC")
                .setSignedAttributeGenerator(new AttributeTable(signedAttrs))
                .build("MD5withRSA", privKey, x509Cert));
    } else {
        throw new Exception("sign: Signing digest " + digest + " not supported.");
    }
    //add cert store
    List<Certificate> certList = Arrays.asList(chain);
    Store certStore = new JcaCertStore(certList);
    signedGenerator.addCertificates(certStore);
    MimeMultipart signedPart = signedGenerator.generate(body);
    return (signedPart);
}

From source file:de.mendelson.util.security.BCCryptoHelper.java

/**
 * @param chain certificate chain, chain[0] is the signers certificate
 * itself Signs the data using S/MIME 3.1 - dont use if for S/MIME 3.2 or
 * higher/*w w  w  . j a  v  a  2 s  .  c  o  m*/
 */
public MimeMultipart sign(MimeMessage message, Certificate[] chain, Key key, String digest) throws Exception {
    if (message == null) {
        throw new Exception("sign: Message is absent");
    }
    X509Certificate x509Cert = this.castCertificate(chain[0]);
    PrivateKey privKey = this.getPrivateKey(key);
    SMIMESignedGenerator signedGenerator = new SMIMESignedGenerator(SMIMESignedGenerator.RFC3851_MICALGS);
    //add dont know
    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));
    if (digest.equalsIgnoreCase(ALGORITHM_SHA1)) {
        signedGenerator.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC")
                .setSignedAttributeGenerator(new AttributeTable(signedAttrs))
                .build("SHA1withRSA", privKey, x509Cert));
    } else if (digest.equalsIgnoreCase(ALGORITHM_SHA224)) {
        signedGenerator.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC")
                .setSignedAttributeGenerator(new AttributeTable(signedAttrs))
                .build("SHA224withRSA", privKey, x509Cert));
    } else if (digest.equalsIgnoreCase(ALGORITHM_SHA256)) {
        signedGenerator.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC")
                .setSignedAttributeGenerator(new AttributeTable(signedAttrs))
                .build("SHA256withRSA", privKey, x509Cert));
    } else if (digest.equalsIgnoreCase(ALGORITHM_SHA384)) {
        signedGenerator.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC")
                .setSignedAttributeGenerator(new AttributeTable(signedAttrs))
                .build("SHA384withRSA", privKey, x509Cert));
    } else if (digest.equalsIgnoreCase(ALGORITHM_SHA512)) {
        signedGenerator.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC")
                .setSignedAttributeGenerator(new AttributeTable(signedAttrs))
                .build("SHA512withRSA", privKey, x509Cert));
    } else if (digest.equalsIgnoreCase(ALGORITHM_MD5)) {
        signedGenerator.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC")
                .setSignedAttributeGenerator(new AttributeTable(signedAttrs))
                .build("MD5withRSA", privKey, x509Cert));
    } else {
        throw new Exception("sign: Signing digest " + digest + " not supported.");
    }
    //add cert store
    List<Certificate> certList = Arrays.asList(chain);
    Store certStore = new JcaCertStore(certList);
    signedGenerator.addCertificates(certStore);
    MimeMultipart multipart = signedGenerator.generate(message);
    return (multipart);
}