01: package net.suberic.pooka.crypto;
02:
03: import net.suberic.pooka.*;
04: import net.suberic.crypto.*;
05:
06: import javax.mail.internet.*;
07: import javax.mail.*;
08: import javax.activation.DataHandler;
09:
10: import java.security.Key;
11:
12: import java.io.*;
13:
14: /**
15: * A signed attachment.
16: */
17: public class SignedAttachment extends Attachment {
18:
19: boolean parsed = false;
20:
21: /**
22: * Creates a SignedAttachment out of a MimePart.
23: */
24: public SignedAttachment(MimePart mp) throws MessagingException {
25: super (mp);
26: }
27:
28: /**
29: * Returns if the signature matches.
30: */
31: public boolean checkSignature(EncryptionUtils utils, Key key)
32: throws MessagingException, java.io.IOException,
33: java.security.GeneralSecurityException {
34:
35: Object content = getDataHandler().getContent();
36: if (content instanceof MimeMultipart) {
37: return utils.checkSignature((MimeMultipart) content, key);
38: } else {
39: return false;
40: }
41: }
42:
43: /**
44: * Returns the content part of the signed attachment.
45: */
46: public MimeBodyPart getSignedPart()
47: throws javax.mail.MessagingException, java.io.IOException {
48: Object content = getDataHandler().getContent();
49: if (content instanceof MimeMultipart) {
50: MimeMultipart mm = (MimeMultipart) content;
51:
52: // this should be exactly two parts, one the content, the other the
53: // signature.
54: for (int i = 0; i < mm.getCount(); i++) {
55: // return the first one found.
56: MimeBodyPart mbp = (MimeBodyPart) mm.getBodyPart(i);
57: ContentType ct = new ContentType(mbp.getContentType());
58: if (!ct.getSubType().toLowerCase()
59: .endsWith("signature")) {
60: return mbp;
61: }
62: }
63: }
64:
65: return null;
66: }
67:
68: /**
69: * Returns the DataHandler for this Attachment.
70: */
71: public DataHandler getDataHandler() {
72: return super .getDataHandler();
73: }
74:
75: /**
76: * Returns the MimeType.
77: */
78: /*
79: public ContentType getMimeType() {
80: try {
81: return new ContentType("text/plain");
82: } catch (javax.mail.internet.ParseException pe) {
83: return null;
84: }
85: }
86: */
87: }
|