1 7 8 package org.enhydra.oyster.smime; 9 10 import org.enhydra.oyster.exception.SMIMEException; 11 import org.enhydra.oyster.activation.CMSSignedDataSource; 12 import javax.mail.internet.MimeMessage ; 13 import javax.mail.Multipart ; 14 import javax.mail.internet.MimeMessage ; 15 import javax.mail.internet.MimeBodyPart ; 16 import javax.mail.internet.MimeMultipart ; 17 import javax.activation.DataHandler ; 18 import java.util.Vector ; 19 import java.util.TimeZone ; 20 import java.util.GregorianCalendar ; 21 import java.io.FileInputStream ; 22 import java.security.PrivateKey ; 23 import java.security.KeyStore ; 24 import java.security.cert.X509Certificate ; 25 26 104 public class SignedSMIME extends BaseSignedSMIMEObject 105 { 106 107 113 protected SignedSMIME () 114 { 115 super(); 116 } 117 118 119 144 public SignedSMIME (String smtpHost, String fromAddress, String subject, 145 String content, String charset) throws SMIMEException 146 { 147 super(smtpHost, fromAddress, subject, content, charset); 148 } 149 150 151 170 public SignedSMIME (String smtpHost, String fromAddress, String subject, 171 String charset) throws SMIMEException 172 { 173 super(smtpHost, fromAddress, subject, null, charset); 174 } 175 176 177 194 public SignedSMIME (MimeMessage mimeMessage) throws SMIMEException 195 { 196 super(mimeMessage); 197 } 198 199 200 209 public void addRecipient (String recipientAddress, String type) throws SMIMEException 210 { 211 super.addRecipient(recipientAddress, type, null); 212 } 213 214 221 public void signing() throws SMIMEException { 222 this.signing(false); 223 } 224 225 234 public void signing (boolean externalSignature) throws SMIMEException { 235 try { 236 if (super.indicatorTo != true) 237 throw new SMIMEException(this, 1043); 238 239 if(!super.externalMessagePresence) { 241 if (super.contentPresence & super.bodyPartArray.size() == 1) { if(super.bodyPartArray.elementAt(0) instanceof MimeBodyPart ) { MimeBodyPart contentBody = (MimeBodyPart )super.bodyPartArray.elementAt(0); 244 super.message.setContent((String )contentBody.getContent(), contentBody.getContentType()); 245 super.message.setDisposition(super.message.INLINE); 246 } 247 else super.message.setContent((MimeMultipart )super.bodyPartArray.elementAt(0)); 249 } 250 else if ( super.bodyPartArray.size() != 0) { 251 Multipart mp = new MimeMultipart (); 252 for (int i = 0; i != super.bodyPartArray.size(); i++) { 253 if(super.bodyPartArray.elementAt(i) instanceof MimeMultipart ) { 254 MimeBodyPart forMulti = new MimeBodyPart (); 255 forMulti.setContent((MimeMultipart )super.bodyPartArray.elementAt(i)); 256 mp.addBodyPart(forMulti); 257 } 258 else 259 mp.addBodyPart((MimeBodyPart )super.bodyPartArray.elementAt(i)); 260 } 261 super.message.setContent(mp); 262 } 263 else 264 throw new SMIMEException(this, 1044); 265 266 } 267 268 CMSSignedDataSource ss = new CMSSignedDataSource(super.message, externalSignature); 269 for (int i = 0; i < super.ksArray.size(); i++) { 270 boolean[] incl = (boolean[])super.including.elementAt(i); 271 for (int j = 6*i; j != (6*(i + 1)) && super.capabilities.elementAt(j) != null; j = j + 2) { 272 int[] capabil = (int[])super.capabilities.elementAt(j + 1); 273 ss.setCapabilities((String )super.capabilities.elementAt(j), capabil[0], capabil[1], capabil[2], capabil[3], capabil[4]); 274 } 275 ss.addSigner((KeyStore )super.ksArray.elementAt(i), incl[0], incl[1], (String )super.digestArray.elementAt(i)); 276 } 277 for (int i = 0; i < super.certChainArray.size(); i++) { 278 boolean[] incl2 = (boolean[])super.including2.elementAt(i); 279 for (int j = 6*i; j != (6*(i + 1)) && super.capabilities2.elementAt(j) != null; j = j + 2) { 280 int[] capabil = (int[])super.capabilities2.elementAt(j + 1); 281 ss.setCapabilities((String )super.capabilities2.elementAt(j), capabil[0], capabil[1], capabil[2], capabil[3], capabil[4]); 282 } 283 ss.addSigner((X509Certificate [])certChainArray.elementAt(i), (PrivateKey )super.privKeyArray.elementAt(i), incl2[0], incl2[1], (String )super.digestArray2.elementAt(i)); 284 } 285 for (int i = 0; i < super.aditionalCerts.size(); i++) { 286 ss.addCertificate((X509Certificate )super.aditionalCerts.elementAt(i)); 287 } 288 super.message.setDataHandler(new DataHandler (ss)); 289 super.message.saveChanges(); 290 super.message.setDescription("Signed SMIME message."); 291 292 super.message.setDisposition(super.message.ATTACHMENT); 293 TimeZone tz = TimeZone.getDefault(); GregorianCalendar cal = new GregorianCalendar (tz); 295 super.message.setSentDate(cal.getTime()); 296 clean(); 297 } 298 catch(Exception e) { 299 throw SMIMEException.getInstance(this, e, "signing"); 300 } 301 302 } 303 304 310 public MimeMessage getSignedMessage () { 311 return super.message; 312 } 313 314 315 318 private void clean () { 319 super.reset(); 320 System.gc(); } 322 } 323 324 325 326 | Popular Tags |