1 7 8 package org.enhydra.oyster.smime; 9 10 import org.enhydra.oyster.exception.SMIMEException; 11 import org.enhydra.oyster.activation.CMSEnvelopedDataSource; 12 import org.enhydra.oyster.crypto.consts.EnvelopedConstants; 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.TimeZone ; 19 import java.util.GregorianCalendar ; 20 import java.io.FileInputStream ; 21 import java.io.InputStream ; 22 import java.security.cert.X509Certificate ; 23 import java.security.KeyStore ; 24 25 88 public class EnvelopedSMIME extends BaseSMIMEObject implements EnvelopedConstants 89 { 90 91 97 protected EnvelopedSMIME () 98 { 99 super(); 100 } 101 102 103 128 public EnvelopedSMIME (String smtpHost, String fromAddress, String subject, 129 String content, String charset) throws SMIMEException 130 { 131 super(smtpHost, fromAddress, subject, content, charset); 132 } 133 134 135 154 public EnvelopedSMIME (String smtpHost, String fromAddress, String subject, 155 String charset) throws SMIMEException 156 { 157 super(smtpHost, fromAddress, subject, null, charset); 158 } 159 160 161 178 public EnvelopedSMIME (MimeMessage mimeMessage) throws SMIMEException 179 { 180 super(mimeMessage); 181 } 182 183 184 195 public void addRecipient (String recipientAddress, String type, String cerFileName) 196 throws SMIMEException 197 { 198 super.addRecipient(recipientAddress, type, cerFileName); 199 } 200 201 219 public void addRecipient (String recipientAddress, String type, KeyStore kStore, String alias) 220 throws SMIMEException 221 { 222 super.addRecipient(recipientAddress, type, kStore, alias); 223 } 224 225 226 254 public void addRecipient (String recipientAddress, String type, String ksPath, 255 String ksType, String password, String alias ) throws SMIMEException 256 { 257 super.addRecipient (recipientAddress, type, ksPath, ksType, password, alias ); 258 } 259 260 261 262 269 public void enveloping () throws SMIMEException 270 { 271 this.enveloping("RC2_CBC", 40); 272 } 273 274 283 public void enveloping (String algorithmName, int keyLength) throws SMIMEException 284 { 285 try { 286 if (super.indicatorTo != true) 287 throw new SMIMEException(this, 1043); 288 289 if(!super.externalMessagePresence) { 291 if (super.contentPresence & super.bodyPartArray.size() == 1) { if(super.bodyPartArray.elementAt(0) instanceof MimeBodyPart ) { MimeBodyPart contentBody = (MimeBodyPart )super.bodyPartArray.elementAt(0); 294 super.message.setContent((String )contentBody.getContent(), contentBody.getContentType()); 295 super.message.setDisposition(super.message.INLINE); 296 } 297 else super.message.setContent((MimeMultipart )super.bodyPartArray.elementAt(0)); 299 } 300 else if ( super.bodyPartArray.size() != 0) { 301 Multipart mp = new MimeMultipart (); 302 for (int i = 0; i != super.bodyPartArray.size(); i++) { 303 if(super.bodyPartArray.elementAt(i) instanceof MimeMultipart ) { 304 MimeBodyPart forMulti = new MimeBodyPart (); 305 forMulti.setContent((MimeMultipart )super.bodyPartArray.elementAt(i)); 306 mp.addBodyPart(forMulti); 307 } 308 else 309 mp.addBodyPart((MimeBodyPart )super.bodyPartArray.elementAt(i)); 310 } 311 super.message.setContent(mp); 312 } 313 else 314 throw new SMIMEException(this, 1044); 315 316 } 317 318 CMSEnvelopedDataSource es = new CMSEnvelopedDataSource(super.message, algorithmName, keyLength); 319 for (int i = 0; i != super.certArray.size(); i++) { 320 X509Certificate cert = (X509Certificate )super.certArray.elementAt(i); 321 es.addRecipient(cert); 322 } 323 324 super.message.setDataHandler(new DataHandler (es)); 325 super.message.saveChanges(); 326 327 super.message.setDescription("Enveloped SMIME message."); 328 super.message.setDisposition(super.message.ATTACHMENT); 329 TimeZone tz = TimeZone.getDefault(); GregorianCalendar cal = new GregorianCalendar (tz); 331 super.message.setSentDate(cal.getTime()); 332 333 clean(); 334 } 335 catch (Exception e) { 336 throw SMIMEException.getInstance(this, e, "enveloping"); 337 } 338 } 339 340 346 public MimeMessage getEnvelopedMessage () { 347 return super.message; 348 } 349 350 351 354 private void clean () { 355 super.reset(); 356 System.gc(); } 358 359 } 360 361 362 363 | Popular Tags |