org.bouncycastle.mail.smime.SMIMEException Java Examples

The following examples show how to use org.bouncycastle.mail.smime.SMIMEException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BCCryptoHelper.java    From OpenAs2App with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public MimeBodyPart compress(Message msg, MimeBodyPart mbp, String compressionType, String contentTxfrEncoding) throws SMIMEException, OpenAS2Exception {
    OutputCompressor compressor = null;
    if (compressionType != null) {
        if (compressionType.equalsIgnoreCase(ICryptoHelper.COMPRESSION_ZLIB)) {
            compressor = new ZlibCompressor();
        } else {
            throw new OpenAS2Exception("Unsupported compression type: " + compressionType);
        }
    }
    SMIMECompressedGenerator sCompGen = new SMIMECompressedGenerator();
    sCompGen.setContentTransferEncoding(getEncoding(contentTxfrEncoding));
    MimeBodyPart smime = sCompGen.generate(mbp, compressor);
    if (logger.isTraceEnabled()) {
        try {
            logger.trace("Compressed MIME msg AFTER COMPRESSION Content-Type:" + smime.getContentType());
            logger.trace("Compressed MIME msg AFTER COMPRESSION Content-Disposition:" + smime.getDisposition());
        } catch (MessagingException e) {
        }
    }
    return smime;
}
 
Example #2
Source File: SMIMEKeyHolder.java    From james-project with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an <CODE>SMIMESignedGenerator</CODE>. Includes a signer private key and certificate,
 * and a pool of certs and cerls (if any) to go with the signature.
 * @return The generated SMIMESignedGenerator.
 */
public SMIMESignedGenerator createGenerator() throws CertStoreException, SMIMEException, OperatorCreationException,
    CertificateEncodingException {
    
    // create the generator for creating an smime/signed message
    SMIMESignedGenerator generator = new SMIMESignedGenerator();
    
    // add a signer to the generator - this specifies we are using SHA1
    // the encryption algorithm used is taken from the key
    SignerInfoGenerator signerInfoGenerator = new JcaSimpleSignerInfoGeneratorBuilder()
        .setProvider("BC")
        .build("SHA1withRSA", privateKey, certificate);
    generator.addSignerInfoGenerator(signerInfoGenerator);
    
    // add our pool of certs and cerls (if any) to go with the signature
    generator.addCertificates(jcaCertStore);
    
    return generator;
    
}
 
Example #3
Source File: SMIMEKeyHolder.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a signed MimeMultipart from a MimeMessage.
 * @param message The message to sign.
 * @return The signed <CODE>MimeMultipart</CODE>.
 */    
@Override
public MimeMultipart generate(MimeMessage message) throws CertStoreException, NoSuchAlgorithmException, NoSuchProviderException,
    SMIMEException, OperatorCreationException, CertificateEncodingException {
    
    // create the generator for creating an smime/signed MimeMultipart
    SMIMESignedGenerator generator = createGenerator();
    
    // do it
    return generator.generate(message);
    
}
 
Example #4
Source File: SMIMEKeyHolder.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a signed MimeMultipart from a MimeBodyPart.
 * @param content The content to sign.
 * @return The signed <CODE>MimeMultipart</CODE>.
 */
@Override
public MimeMultipart generate(MimeBodyPart content) throws CertStoreException, NoSuchAlgorithmException, NoSuchProviderException,
    SMIMEException, OperatorCreationException, CertificateEncodingException {
    
    // create the generator for creating an smime/signed MimeMultipart
    SMIMESignedGenerator generator = createGenerator();
    
    // do it
    return generator.generate(content);
    
}
 
Example #5
Source File: SendMailService.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
private void processHTMLBodyWithBASE64Images(MimeMultipart multipart) throws ParserException,
        MessagingException, NoSuchAlgorithmException, SMIMEException, java.security.NoSuchProviderException {
    if (null != input.getBody() && input.getBody().contains(Encodings.BASE64)) {
        Parser parser = new Parser(input.getBody());
        NodeList nodeList = parser.parse(null);
        HtmlImageNodeVisitor htmlImageNodeVisitor = new HtmlImageNodeVisitor();
        nodeList.visitAllNodesWith(htmlImageNodeVisitor);
        input.setBody(nodeList.toHtml());

        addAllBase64ImagesToMimeMultipart(multipart, htmlImageNodeVisitor.getBase64Images());
    }
}
 
Example #6
Source File: SendMailService.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
private void addAllBase64ImagesToMimeMultipart(MimeMultipart multipart, Map<String, String> base64ImagesMap)
        throws MessagingException, NoSuchAlgorithmException, NoSuchProviderException, SMIMEException {
    for (String contentId : base64ImagesMap.keySet()) {
        MimeBodyPart imagePart = getImageMimeBodyPart(base64ImagesMap, contentId);
        imagePart = encryptMimeBodyPart(imagePart);
        multipart.addBodyPart(imagePart);
    }
}
 
Example #7
Source File: SendMailService.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
private MimeBodyPart encryptMimeBodyPart(MimeBodyPart mimeBodyPart) throws NoSuchAlgorithmException,
        NoSuchProviderException, SMIMEException {
    if (input.isEncryptedMessage()) {
        mimeBodyPart = gen.generate(mimeBodyPart, input.getEncryptionAlgorithm(), SecurityConstants.BOUNCY_CASTLE_PROVIDER);
    }
    return mimeBodyPart;
}
 
Example #8
Source File: BCCryptoHelper.java    From OpenAs2App with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public MimeBodyPart decrypt(MimeBodyPart part, Certificate cert, Key key) throws GeneralSecurityException, MessagingException, CMSException, IOException, SMIMEException {
    // Make sure the data is encrypted
    if (!isEncrypted(part)) {
        throw new GeneralSecurityException("Content-Type indicates data isn't encrypted");
    }

    // Cast parameters to what BC needs
    X509Certificate x509Cert = castCertificate(cert);

    // Parse the MIME body into an SMIME envelope object
    SMIMEEnveloped envelope = new SMIMEEnveloped(part);

    // Get the recipient object for decryption
    if (logger.isDebugEnabled()) {
        logger.debug("Extracted X500 info::  PRINCIPAL : " + x509Cert.getIssuerX500Principal() + " ::  NAME : " + x509Cert.getIssuerX500Principal().getName());
    }

    X500Name x500Name = new X500Name(x509Cert.getIssuerX500Principal().getName());
    KeyTransRecipientId certRecId = new KeyTransRecipientId(x500Name, x509Cert.getSerialNumber());
    RecipientInformationStore recipientInfoStore = envelope.getRecipientInfos();

    Collection<RecipientInformation> recipients = recipientInfoStore.getRecipients();

    if (recipients == null) {
        throw new GeneralSecurityException("Certificate recipients could not be extracted");
    }
    //RecipientInformation recipientInfo  = recipientInfoStore.get(recId);
    //Object recipient = null;        

    boolean foundRecipient = false;
    for (Iterator<RecipientInformation> iterator = recipients.iterator(); iterator.hasNext(); ) {
        RecipientInformation recipientInfo = iterator.next();
        //recipient = iterator.next();
        if (recipientInfo instanceof KeyTransRecipientInformation) {
            // X509CertificateHolder x509CertHolder = new X509CertificateHolder(x509Cert.getEncoded());

            //RecipientId rid = recipientInfo.getRID();
            if (certRecId.match(recipientInfo) && !foundRecipient) {
                foundRecipient = true;
                // byte[] decryptedData = recipientInfo.getContent(new JceKeyTransEnvelopedRecipient((PrivateKey)key).setProvider("BC"));
                byte[] decryptedData = recipientInfo.getContent(new BcRSAKeyTransEnvelopedRecipient(PrivateKeyFactory.createKey(PrivateKeyInfo.getInstance(key.getEncoded()))));

                return SMIMEUtil.toMimeBodyPart(decryptedData);
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Failed match on recipient ID's:\n     RID from msg:" + recipientInfo.getRID().toString() + "    \n     RID from priv cert: " + certRecId.toString());
                }
            }
        }
    }
    throw new GeneralSecurityException("Matching certificate recipient could not be found");
}
 
Example #9
Source File: BCCryptoHelper.java    From OpenAs2App with BSD 2-Clause "Simplified" License 3 votes vote down vote up
public MimeBodyPart encrypt(MimeBodyPart part, Certificate cert, String algorithm, String contentTxfrEncoding) throws GeneralSecurityException, SMIMEException, MessagingException {
    X509Certificate x509Cert = castCertificate(cert);


    SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
    gen.setContentTransferEncoding(getEncoding(contentTxfrEncoding));

    if (logger.isDebugEnabled()) {
        logger.debug("Encrypting on MIME part containing the following headers: " + AS2Util.printHeaders(part.getAllHeaders()));
    }

    gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(x509Cert).setProvider("BC"));

    return gen.generate(part, getOutputEncryptor(algorithm));
}
 
Example #10
Source File: ICryptoHelper.java    From OpenAs2App with BSD 2-Clause "Simplified" License votes vote down vote up
MimeBodyPart compress(Message msg, MimeBodyPart mbp, String compressionType, String contentTxfrEncoding) throws SMIMEException, OpenAS2Exception;