/* (non-Javadoc)
* @see org.apache.mailet.Mailet#service(org.apache.mailet.Mail)
*/
public void service(Mail mail) throws MessagingException {
MimeMessage message = mail.getMessage();
Part strippedMessage = null;
log("Starting message decryption..");
if (message.isMimeType("application/x-pkcs7-mime") || message.isMimeType("application/pkcs7-mime")) {
try {
SMIMEEnveloped env = new SMIMEEnveloped(message);
Collection recipients = env.getRecipientInfos().getRecipients();
for (Iterator iter = recipients.iterator();iter.hasNext();) {
RecipientInformation info = (RecipientInformation) iter.next();
RecipientId id = info.getRID();
if (id.match(keyHolder.getCertificate())) {
try {
MimeBodyPart part = SMIMEUtil.toMimeBodyPart(info.getContent(keyHolder.getPrivateKey(), "BC"));
// strippedMessage contains the decrypted message.
strippedMessage = part;
log("Encrypted message decrypted");
} catch (Exception e) {
throw new MessagingException("Error during the decryption of the message", e); }
} else {
log("Found an encrypted message but it isn't encrypted for the supplied key");
}
}
} catch (CMSException e) { throw new MessagingException("Error during the decryption of the message",e); }
}
// if the decryption has been successful..
if (strippedMessage != null) {
// I put the private key's public certificate as a mailattribute.
// I create a list of certificate because I want to minic the
// behavior of the SMIMEVerifySignature mailet. In that way
// it is possible to reuse the same matchers to analyze
// the result of the operation.
ArrayList list = new ArrayList(1);
list.add(keyHolder.getCertificate());
mail.setAttribute(mailAttribute, list);
// I start the message stripping.
try {
MimeMessage newmex = new MimeMessage(message);
Object obj = strippedMessage.getContent();
if (obj instanceof Multipart) {
log("The message is multipart, content type "+((Multipart)obj).getContentType());
newmex.setContent((Multipart)obj);
} else {
newmex.setContent(obj, strippedMessage.getContentType());
newmex.setDisposition(null);
}
newmex.saveChanges();
mail.setMessage(newmex);
} catch (IOException e) {