Example usage for org.bouncycastle.mail.smime SMIMEException getUnderlyingException

List of usage examples for org.bouncycastle.mail.smime SMIMEException getUnderlyingException

Introduction

In this page you can find the example usage for org.bouncycastle.mail.smime SMIMEException getUnderlyingException.

Prototype

public Exception getUnderlyingException() 

Source Link

Usage

From source file:hk.hku.cecid.piazza.commons.security.SMimeMessage.java

License:Open Source License

/**
 * Signs the encapsulated MIME body part.  
 * // w w w .  ja  v a2  s  . com
 * @return an S/MIME message encapsulating the signed MIME body part. 
 * @throws SMimeException if unable to sign the body part.
 */
public SMimeMessage sign() throws SMimeException {
    try {
        if (privateKey == null) {
            throw new SMimeException("Private key not found");
        }

        try {
            setDefaults();

            /* Create the SMIMESignedGenerator */
            SMIMECapabilityVector capabilities = new SMIMECapabilityVector();
            capabilities.addCapability(SMIMECapability.dES_EDE3_CBC);
            capabilities.addCapability(SMIMECapability.rC2_CBC, 128);
            capabilities.addCapability(SMIMECapability.dES_CBC);

            ASN1EncodableVector attributes = new ASN1EncodableVector();
            attributes.add(new SMIMEEncryptionKeyPreferenceAttribute(new IssuerAndSerialNumber(
                    new X509Name(cert.getIssuerDN().getName()), cert.getSerialNumber())));
            attributes.add(new SMIMECapabilitiesAttribute(capabilities));

            SMIMESignedGenerator signer = new SMIMESignedGenerator();
            signer.setContentTransferEncoding(getContentTransferEncoding());
            signer.addSigner(privateKey, cert, getDigestAlgorithm(), new AttributeTable(attributes), null);

            /* Add the list of certs to the generator */
            ArrayList certList = new ArrayList();
            certList.add(cert);
            CertStore certs = CertStore.getInstance("Collection", new CollectionCertStoreParameters(certList),
                    SECURITY_PROVIDER);
            signer.addCertificatesAndCRLs(certs);

            /* Sign the body part */
            MimeMultipart mm = signer.generate(bodyPart, SECURITY_PROVIDER);

            InternetHeaders headers = new InternetHeaders();
            boolean isContentTypeFolded = new Boolean(System.getProperty("mail.mime.foldtext", "true"))
                    .booleanValue();
            headers.setHeader("Content-Type",
                    isContentTypeFolded ? mm.getContentType() : mm.getContentType().replaceAll("\\s", " "));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            mm.writeTo(baos);
            MimeBodyPart signedPart = new MimeBodyPart(headers, baos.toByteArray());

            return new SMimeMessage(signedPart, this);
        } catch (org.bouncycastle.mail.smime.SMIMEException ex) {
            throw new SMimeException(ex.getMessage(), ex.getUnderlyingException());
        }
    } catch (Exception e) {
        throw new SMimeException("Unable to sign body part", e);
    }
}

From source file:hk.hku.cecid.piazza.commons.security.SMimeMessage.java

License:Open Source License

/**
 * Encrypts the encapsulated MIME body part.
 * /*from   w w  w.  j  a va2  s  .c  o  m*/
 * @param cert the certificate for encryption.
 * @return an S/MIME message encapsulating the encrypted MIME body part. 
 * @throws SMimeException if unable to encrpyt the body part.
 */
public SMimeMessage encrypt(X509Certificate cert) throws SMimeException {
    try {
        try {
            if (cert == null) {
                throw new SMimeException("No certificate for encryption");
            }

            setDefaults();

            /* Create the encrypter */
            SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator();
            encrypter.setContentTransferEncoding(getContentTransferEncoding());
            encrypter.addKeyTransRecipient(cert);

            /* Encrypt the body part */
            MimeBodyPart encryptedPart = encrypter.generate(bodyPart, getEncryptAlgorithm(), SECURITY_PROVIDER);
            return new SMimeMessage(encryptedPart, this);
        } catch (org.bouncycastle.mail.smime.SMIMEException ex) {
            throw new SMimeException(ex.getMessage(), ex.getUnderlyingException());
        }
    } catch (Exception e) {
        throw new SMimeException("Unable to encrypt body part", e);
    }
}

From source file:hk.hku.cecid.piazza.commons.security.SMimeMessage.java

License:Open Source License

/**
 * Compresses the encapsulated MIME body part.
 * // www .  j  a  v a2 s.c o  m
 * @return an S/MIME message encapsulating the compressed MIME body part. 
 * @throws SMimeException if unable to compress the body part.
 */
public SMimeMessage compress() throws SMimeException {
    try {
        try {
            setDefaults();

            /* Create the generator for creating an smime/compressed body part */
            SMIMECompressedGenerator compressor = new SMIMECompressedGenerator();
            compressor.setContentTransferEncoding(getContentTransferEncoding());

            /* compress the body part */
            MimeBodyPart compressedPart = compressor.generate(bodyPart, SMIMECompressedGenerator.ZLIB);
            return new SMimeMessage(compressedPart, this);
        } catch (org.bouncycastle.mail.smime.SMIMEException ex) {
            throw new SMimeException(ex.getMessage(), ex.getUnderlyingException());
        }
    } catch (Exception e) {
        throw new SMimeException("Unable to compress body part", e);
    }
}