1 7 8 package org.enhydra.oyster.smime; 9 10 import org.enhydra.oyster.activation.CMSEnvelopedDataSource; 11 import org.enhydra.oyster.activation.CMSSignedDataSource; 12 import org.enhydra.oyster.exception.SMIMEException; 13 import org.enhydra.oyster.crypto.consts.EnvelopedConstants; 14 import org.enhydra.oyster.util.MimeAssist; 15 import javax.mail.Multipart ; 16 import javax.mail.internet.MimeMessage ; 17 import javax.mail.internet.MimeBodyPart ; 18 import javax.mail.internet.MimeMultipart ; 19 import javax.activation.DataHandler ; 20 import java.util.Vector ; 21 import java.util.TimeZone ; 22 import java.util.GregorianCalendar ; 23 import java.io.FileInputStream ; 24 import java.io.InputStream ; 25 import java.security.PrivateKey ; 26 import java.security.KeyStore ; 27 import java.security.cert.X509Certificate ; 28 29 130 public class SignedAndEnvelopedSMIME extends BaseSignedSMIMEObject implements EnvelopedConstants 131 { 132 138 protected SignedAndEnvelopedSMIME () 139 { 140 super(); 141 } 142 143 168 public SignedAndEnvelopedSMIME (String smtpHost, String fromAddress, String subject, 169 String content, String charset) throws SMIMEException 170 { 171 super(smtpHost, fromAddress, subject, content, charset); 172 } 173 174 175 194 public SignedAndEnvelopedSMIME (String smtpHost, String fromAddress, String subject, 195 String charset) throws SMIMEException 196 { 197 super(smtpHost, fromAddress, subject, null, charset); 198 } 199 200 201 218 public SignedAndEnvelopedSMIME (MimeMessage mimeMessage) throws SMIMEException 219 { 220 super(mimeMessage); 221 } 222 223 224 235 public void addRecipient (String recipientAddress, String type, String cerFileName) 236 throws SMIMEException 237 { 238 super.addRecipient(recipientAddress, type, cerFileName); 239 } 240 241 242 260 public void addRecipient (String recipientAddress, String type, KeyStore kStore, String alias) 261 throws SMIMEException 262 { 263 super.addRecipient(recipientAddress, type, kStore, alias); 264 } 265 266 267 295 public void addRecipient (String recipientAddress, String type, String ksPath, 296 String ksType, String password, String alias ) throws SMIMEException 297 { 298 super.addRecipient (recipientAddress, type, ksPath, ksType, password, alias ); 299 } 300 301 302 313 public void signingAndEnveloping (String type) throws SMIMEException 314 { 315 this.signingAndEnveloping("RC2_CBC", 40, type); } 317 318 331 public void signingAndEnveloping (String algorithmName, int keyLength, String type) throws SMIMEException 332 { 333 try { 334 if ( (!type.equalsIgnoreCase("SIGN_FIRST")) && (!type.equalsIgnoreCase("ENCRYPT_FIRST")) ) 335 throw new SMIMEException(this, 1046); 336 337 if (super.indicatorTo != true) 338 throw new SMIMEException(this, 1043); 339 340 if(!super.externalMessagePresence) { 342 if (super.contentPresence & super.bodyPartArray.size() == 1) { if(super.bodyPartArray.elementAt(0) instanceof MimeBodyPart ) { MimeBodyPart contentBody = (MimeBodyPart )super.bodyPartArray.elementAt(0); 345 super.message.setContent((String )contentBody.getContent(), contentBody.getContentType()); 346 super.message.setDisposition(super.message.INLINE); 347 } 348 else super.message.setContent((MimeMultipart )super.bodyPartArray.elementAt(0)); 350 } 351 else if ( super.bodyPartArray.size() != 0) { 352 Multipart mp = new MimeMultipart (); 353 for (int i = 0; i != super.bodyPartArray.size(); i++) { 354 if(super.bodyPartArray.elementAt(i) instanceof MimeMultipart ) { 355 MimeBodyPart forMulti = new MimeBodyPart (); 356 forMulti.setContent((MimeMultipart )super.bodyPartArray.elementAt(i)); 357 mp.addBodyPart(forMulti); 358 } 359 else 360 mp.addBodyPart((MimeBodyPart )super.bodyPartArray.elementAt(i)); 361 } 362 super.message.setContent(mp); 363 } 364 else 365 throw new SMIMEException(this, 1044); 366 } 367 CMSSignedDataSource sigDataSource = null; 368 CMSEnvelopedDataSource envDataSource = null; 369 370 if (type.equalsIgnoreCase("SIGN_FIRST")) { 371 sigDataSource = new CMSSignedDataSource(super.message, false); 372 for (int i = 0; i < super.ksArray.size(); i++) { 373 boolean[] incl = (boolean[])super.including.elementAt(i); 374 for (int j = 6*i; j != (6*(i + 1)) && super.capabilities.elementAt(j) != null; j = j + 2) { 375 int[] capabil = (int[])super.capabilities.elementAt(j + 1); 376 sigDataSource.setCapabilities((String )super.capabilities.elementAt(j), capabil[0], capabil[1], capabil[2], capabil[3], capabil[4]); 377 } 378 sigDataSource.addSigner((KeyStore )super.ksArray.elementAt(i), incl[0], incl[1], (String )super.digestArray.elementAt(i)); 379 } 380 for (int i = 0; i < super.certChainArray.size(); i++) { 381 boolean[] incl2 = (boolean[])super.including2.elementAt(i); 382 for (int j = 6*i; j != (6*(i + 1)) && super.capabilities2.elementAt(j) != null; j = j + 2) { 383 int[] capabil = (int[])super.capabilities2.elementAt(j + 1); 384 sigDataSource.setCapabilities((String )super.capabilities2.elementAt(j), capabil[0], capabil[1], capabil[2], capabil[3], capabil[4]); 385 } 386 sigDataSource.addSigner((X509Certificate [])super.certChainArray.elementAt(i), (PrivateKey )super.privKeyArray.elementAt(i), incl2[0], incl2[1], (String )super.digestArray2.elementAt(i)); 387 } 388 for (int i = 0; i < super.aditionalCerts.size(); i++) { 389 sigDataSource.addCertificate((X509Certificate )super.aditionalCerts.elementAt(i)); 390 } 391 392 super.message.setDataHandler(new DataHandler (sigDataSource)); 393 super.message.saveChanges(); 394 envDataSource = new CMSEnvelopedDataSource(MimeAssist.messageConvertor(super.message), 395 algorithmName, keyLength); 396 397 for (int i = 0; i != super.certArray.size(); i++) { 398 X509Certificate cert = (X509Certificate )super.certArray.elementAt(i); 399 envDataSource.addRecipient(cert); 400 } 401 super.message.setDataHandler(new DataHandler (envDataSource)); 402 super.message.saveChanges(); 403 super.message.setDescription("Signed and Enveloped SMIME message."); 404 } 405 else { 406 envDataSource = new CMSEnvelopedDataSource(super.message, algorithmName, keyLength); 407 for (int i = 0; i != super.certArray.size(); i++) { 408 X509Certificate cert = (X509Certificate )super.certArray.elementAt(i); 409 envDataSource.addRecipient(cert); 410 } 411 412 super.message.setDataHandler(new DataHandler (envDataSource)); 413 super.message.saveChanges(); 414 sigDataSource = new CMSSignedDataSource(MimeAssist.messageConvertor(super.message), 415 false); 416 417 for (int i = 0; i < super.ksArray.size(); i++) { 418 boolean[] incl = (boolean[])super.including.elementAt(i); 419 for (int j = 6*i; j != (6*(i + 1)) && super.capabilities.elementAt(j) != null; j = j + 2) { 420 int[] capabil = (int[])super.capabilities.elementAt(j + 1); 421 sigDataSource.setCapabilities((String )super.capabilities.elementAt(j), capabil[0], capabil[1], capabil[2], capabil[3], capabil[4]); 422 } 423 sigDataSource.addSigner((KeyStore )super.ksArray.elementAt(i), incl[0], incl[1], (String )super.digestArray.elementAt(i)); 424 } 425 for (int i = 0; i < super.certChainArray.size(); i++) { 426 boolean[] incl2 = (boolean[])super.including2.elementAt(i); 427 for (int j = 6*i; j != (6*(i + 1)) && super.capabilities2.elementAt(j) != null; j = j + 2) { 428 int[] capabil = (int[])super.capabilities2.elementAt(j + 1); 429 sigDataSource.setCapabilities((String )super.capabilities2.elementAt(j), capabil[0], capabil[1], capabil[2], capabil[3], capabil[4]); 430 } 431 sigDataSource.addSigner((X509Certificate [])super.certChainArray.elementAt(i), (PrivateKey )super.privKeyArray.elementAt(i), incl2[0], incl2[1], (String )super.digestArray2.elementAt(i)); 432 } 433 for (int i = 0; i < super.aditionalCerts.size(); i++) { 434 sigDataSource.addCertificate((X509Certificate )super.aditionalCerts.elementAt(i)); 435 } 436 super.message.setDataHandler(new DataHandler (sigDataSource)); 437 super.message.saveChanges(); 438 super.message.setDescription("Enveloped and Signed SMIME message."); 439 } 440 441 super.message.setDisposition(super.message.ATTACHMENT); 442 TimeZone tz = TimeZone.getDefault(); 443 GregorianCalendar cal = new GregorianCalendar (tz); 444 super.message.setSentDate(cal.getTime()); 445 clean(); 446 } 447 catch (Exception e) { 448 throw SMIMEException.getInstance(this, e, "signingAndEnveloping"); 449 } 450 } 451 452 458 public MimeMessage getSignedAndEnvelopedSMimeMessage () { 459 return super.message; 460 } 461 462 463 466 private void clean () { 467 super.reset(); 468 System.gc(); } 470 } 471 472 473 474 | Popular Tags |