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);
}
}
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;
}
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;
}
Aggregations