1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.mailet.crypto.mailet;
23
24 import org.apache.mailet.Mail;
25
26 import javax.mail.MessagingException;
27 import javax.mail.internet.MimeBodyPart;
28 import javax.mail.internet.MimeMessage;
29 import javax.mail.internet.MimeMultipart;
30
31 import java.io.IOException;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class SMIMESign extends Sign {
56
57
58
59
60
61
62 public String getMailetInfo() {
63 return "SMIME Signature Mailet";
64 }
65
66
67
68
69 protected String[] getAllowedInitParameters() {
70 String[] allowedArray = {
71 "debug",
72 "keyStoreFileName",
73 "keyStorePassword",
74 "keyStoreType",
75 "keyAlias",
76 "keyAliasPassword",
77 "signerName",
78 "postmasterSigns",
79 "rebuildFrom",
80 "explanationText"
81 };
82 return allowedArray;
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public String getExplanationText() {
109 String explanationText = super.getExplanationText();
110 if (explanationText == null) {
111 explanationText = "The message this file is attached to has been signed on the server by\r\n"
112 + "\t\"[signerName]\" <[signerAddress]>"
113 + "\r\nto certify that the sender is known and truly has the following address (reverse-path):\r\n"
114 + "\t[reversePath]"
115 + "\r\nand that the original message has the following message headers:\r\n"
116 + "\r\n[headers]"
117 + "\r\n\r\nThe signature envelopes this attachment too."
118 + "\r\nPlease check the signature integrity."
119 + "\r\n\r\n"
120 + "\t\"[signerName]\" <[signerAddress]>";
121 }
122
123 return explanationText;
124 }
125
126
127
128
129
130 protected void initKeyHolderClass() throws MessagingException {
131 String keyHolderClassName = "org.apache.james.security.SMIMEKeyHolder";
132 try {
133 setKeyHolderClass(Class.forName(keyHolderClassName));
134 } catch (ClassNotFoundException cnfe) {
135 throw new MessagingException(keyHolderClassName + "does not exist.");
136 }
137 if (isDebug()) {
138 log("keyHolderClass: " + getKeyHolderClass());
139 }
140 }
141
142
143
144
145 protected void initPostmasterSigns() {
146 setPostmasterSigns((getInitParameter("postmasterSigns") == null) ? true : new Boolean(getInitParameter("postmasterSigns")).booleanValue());
147 }
148
149
150
151
152 protected void initRebuildFrom() throws MessagingException {
153 setRebuildFrom((getInitParameter("rebuildFrom") == null) ? true : new Boolean(getInitParameter("rebuildFrom")).booleanValue());
154 if (isDebug()) {
155 if (isRebuildFrom()) {
156 log("Will modify the \"From:\" header.");
157 } else {
158 log("Will leave the \"From:\" header unchanged.");
159 }
160 }
161 }
162
163
164
165
166
167
168
169
170
171 protected MimeBodyPart getWrapperBodyPart(Mail mail) throws MessagingException, IOException {
172
173 String explanationText = getExplanationText();
174
175
176 if (explanationText == null) {
177 return null;
178 }
179
180 MimeMessage originalMessage = mail.getMessage();
181
182 MimeBodyPart messagePart = new MimeBodyPart();
183 MimeBodyPart signatureReason = new MimeBodyPart();
184
185 String contentType = originalMessage.getContentType();
186 Object content = originalMessage.getContent();
187
188 if (contentType != null && content != null) {
189 messagePart.setContent(content, contentType);
190 } else {
191 throw new MessagingException("Either the content type or the content is null");
192 }
193
194 String headers = getMessageHeaders(originalMessage);
195
196 signatureReason.setText(getReplacedExplanationText(getExplanationText(),
197 getSignerName(),
198 getKeyHolder().getSignerAddress(),
199 mail.getSender().toString(),
200 headers));
201
202 signatureReason.setFileName("SignatureExplanation.txt");
203
204 MimeMultipart wrapperMultiPart = new MimeMultipart();
205
206 wrapperMultiPart.addBodyPart(messagePart);
207 wrapperMultiPart.addBodyPart(signatureReason);
208
209 MimeBodyPart wrapperBodyPart = new MimeBodyPart();
210
211 wrapperBodyPart.setContent(wrapperMultiPart);
212
213 return wrapperBodyPart;
214 }
215
216 }
217