Search in sources :

Example 1 with SMIMEException

use of org.bouncycastle.mail.smime.SMIMEException in project nhin-d by DirectProject.

the class SigTest method make.

private MimeMultipart make(MimeBodyPart content, String sigProvider) throws NoSuchAlgorithmException, NoSuchProviderException, SMIMEException {
    try {
        MimeBodyPart sig = new MimeBodyPart();
        sig.setContent(new ContentSigner(content, false, sigProvider), DETACHED_SIGNATURE_TYPE);
        //sig.setContent(new ContentSigner(content, true, sigProvider), DETACHED_SIGNATURE_TYPE);
        sig.addHeader("Content-Type", DETACHED_SIGNATURE_TYPE);
        sig.addHeader("Content-Disposition", "attachment; filename=\"smime.p7s\"");
        sig.addHeader("Content-Description", "S/MIME Cryptographic Signature");
        sig.addHeader("Content-Transfer-Encoding", encoding);
        //
        // build the multipart header
        //
        StringBuffer header = new StringBuffer("signed; protocol=\"application/pkcs7-signature\"");
        addHashHeader(header, _signers);
        MimeMultipart mm = new MimeMultipart(header.toString());
        mm.addBodyPart(content);
        mm.addBodyPart(sig);
        return mm;
    } catch (MessagingException e) {
        throw new SMIMEException("exception putting multi-part together.", e);
    }
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) SMIMEException(org.bouncycastle.mail.smime.SMIMEException) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 2 with SMIMEException

use of org.bouncycastle.mail.smime.SMIMEException in project nhin-d by DirectProject.

the class SigTest method makeContentBodyPart.

private MimeBodyPart makeContentBodyPart(MimeBodyPart content) throws SMIMEException {
    //
    try {
        MimeMessage msg = new MimeMessage((Session) null);
        Enumeration e = content.getAllHeaders();
        msg.setDataHandler(content.getDataHandler());
        while (e.hasMoreElements()) {
            Header hdr = (Header) e.nextElement();
            msg.setHeader(hdr.getName(), hdr.getValue());
        }
        msg.saveChanges();
        //
        // we do this to make sure at least the default headers are
        // set in the body part.
        //
        e = msg.getAllHeaders();
        while (e.hasMoreElements()) {
            Header hdr = (Header) e.nextElement();
            if (Strings.toLowerCase(hdr.getName()).startsWith("content-")) {
                content.setHeader(hdr.getName(), hdr.getValue());
            }
        }
    } catch (MessagingException e) {
        throw new SMIMEException("exception saving message state.", e);
    }
    return content;
}
Also used : Enumeration(java.util.Enumeration) Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) SMIMEException(org.bouncycastle.mail.smime.SMIMEException)

Example 3 with SMIMEException

use of org.bouncycastle.mail.smime.SMIMEException in project jmeter by apache.

the class SMIMEAssertion method getResult.

public static AssertionResult getResult(SMIMEAssertionTestElement testElement, SampleResult response, String name) {
    checkForBouncycastle();
    AssertionResult res = new AssertionResult(name);
    try {
        MimeMessage msg;
        final int msgPos = testElement.getSpecificMessagePositionAsInt();
        if (msgPos < 0) {
            // means counting from end
            SampleResult[] subResults = response.getSubResults();
            final int pos = subResults.length + msgPos;
            log.debug("Getting message number: {} of {}", pos, subResults.length);
            msg = getMessageFromResponse(response, pos);
        } else {
            log.debug("Getting message number: {}", msgPos);
            msg = getMessageFromResponse(response, msgPos);
        }
        SMIMESignedParser s = null;
        if (log.isDebugEnabled()) {
            log.debug("Content-type: {}", msg.getContentType());
        }
        if (msg.isMimeType("multipart/signed")) {
            // $NON-NLS-1$
            MimeMultipart multipart = (MimeMultipart) msg.getContent();
            s = new SMIMESignedParser(new BcDigestCalculatorProvider(), multipart);
        } else if (// $NON-NLS-1$
        msg.isMimeType("application/pkcs7-mime") || msg.isMimeType("application/x-pkcs7-mime")) {
            // $NON-NLS-1$
            s = new SMIMESignedParser(new BcDigestCalculatorProvider(), msg);
        }
        if (null != s) {
            log.debug("Found signature");
            if (testElement.isNotSigned()) {
                res.setFailure(true);
                res.setFailureMessage("Mime message is signed");
            } else if (testElement.isVerifySignature() || !testElement.isSignerNoCheck()) {
                res = verifySignature(testElement, s, name);
            }
        } else {
            log.debug("Did not find signature");
            if (!testElement.isNotSigned()) {
                res.setFailure(true);
                res.setFailureMessage("Mime message is not signed");
            }
        }
    } catch (MessagingException e) {
        String msg = "Cannot parse mime msg: " + e.getMessage();
        log.warn(msg, e);
        res.setFailure(true);
        res.setFailureMessage(msg);
    } catch (CMSException e) {
        res.setFailure(true);
        res.setFailureMessage("Error reading the signature: " + e.getMessage());
    } catch (SMIMEException e) {
        res.setFailure(true);
        res.setFailureMessage("Cannot extract signed body part from signature: " + e.getMessage());
    } catch (IOException e) {
        // should never happen
        log.error("Cannot read mime message content: {}", e.getMessage(), e);
        res.setError(true);
        res.setFailureMessage(e.getMessage());
    }
    return res;
}
Also used : BcDigestCalculatorProvider(org.bouncycastle.operator.bc.BcDigestCalculatorProvider) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SMIMESignedParser(org.bouncycastle.mail.smime.SMIMESignedParser) SMIMEException(org.bouncycastle.mail.smime.SMIMEException) SampleResult(org.apache.jmeter.samplers.SampleResult) CMSException(org.bouncycastle.cms.CMSException)

Aggregations

MessagingException (javax.mail.MessagingException)3 SMIMEException (org.bouncycastle.mail.smime.SMIMEException)3 MimeMessage (javax.mail.internet.MimeMessage)2 MimeMultipart (javax.mail.internet.MimeMultipart)2 IOException (java.io.IOException)1 Enumeration (java.util.Enumeration)1 Header (javax.mail.Header)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 SampleResult (org.apache.jmeter.samplers.SampleResult)1 CMSException (org.bouncycastle.cms.CMSException)1 SMIMESignedParser (org.bouncycastle.mail.smime.SMIMESignedParser)1 BcDigestCalculatorProvider (org.bouncycastle.operator.bc.BcDigestCalculatorProvider)1