1 7 package org.enhydra.oyster.smime; 8 9 import org.enhydra.oyster.exception.SMIMEException; 10 import org.enhydra.oyster.util.PFXUtils; 11 import org.enhydra.oyster.crypto.consts.SignedConstants; 12 import java.util.Vector ; 13 import java.io.FileInputStream ; 14 import java.io.File ; 15 import java.security.PrivateKey ; 16 import java.security.KeyStore ; 17 import java.security.cert.X509Certificate ; 18 import java.security.cert.Certificate ; 19 import javax.mail.internet.MimeMessage ; 20 21 25 public class BaseSignedSMIMEObject extends BaseSMIMEObject implements SignedConstants 26 { 27 28 34 protected BaseSignedSMIMEObject () 35 { 36 super(); 37 } 38 39 40 65 protected BaseSignedSMIMEObject (String smtpHost, String fromAddress, String subject, 66 String content, String charset) throws SMIMEException 67 { 68 super(smtpHost, fromAddress, subject, content, charset); 69 } 70 71 72 91 protected BaseSignedSMIMEObject (String smtpHost, String fromAddress, String subject, 92 String charset) throws SMIMEException 93 { 94 super(smtpHost, fromAddress, subject, null, charset); 95 } 96 97 98 115 protected BaseSignedSMIMEObject (MimeMessage mimeMessage) throws SMIMEException 116 { 117 super(mimeMessage); 118 } 119 120 121 122 126 protected Vector ksArray = new Vector (0, 1); 127 128 132 protected Vector digestArray = new Vector (0, 1); 133 134 138 protected Vector including = new Vector (0, 1); 139 140 144 protected Vector certChainArray = new Vector (0, 1); 145 146 150 protected Vector privKeyArray = new Vector (0, 1); 151 152 156 protected Vector digestArray2 = new Vector (0, 1); 157 158 162 protected Vector including2 = new Vector (0, 1); 163 164 167 protected Vector aditionalCerts = new Vector (0, 1); 168 169 173 protected Vector capabilitiesTemp = new Vector (0, 1); 174 175 178 protected Vector capabilities = new Vector (0, 1); 179 180 183 protected Vector capabilities2 = new Vector (0, 1); 184 185 186 228 public void setCapabilities (String type0, int par10, int par20, int par30, 229 int par40, int par50) throws SMIMEException 230 { 231 int[] tempType = { par10, par20, par30, par40, par50 }; 232 capabilitiesTemp.addElement(type0); 233 capabilitiesTemp.addElement(tempType); 234 if (capabilitiesTemp.size() > 6) 235 throw new SMIMEException(this, 1045); 236 } 237 238 253 public void addSigner (String pfxfileName, String password, String signingAlg, 254 boolean includingCert, boolean includingSignAttrib) throws SMIMEException 255 { 256 try { 257 char[] paswCh = password.toCharArray(); 258 FileInputStream inPFX = new FileInputStream (pfxfileName); 259 KeyStore ks = KeyStore.getInstance("PKCS12", "BC"); 260 ks.load(inPFX, paswCh); 261 inPFX.close(); 262 boolean[] incl = { includingCert, includingSignAttrib }; 263 ksArray.addElement(ks); 264 digestArray.addElement(signingAlg); 265 including.addElement(incl); 266 if (capabilitiesTemp.size() != 0) { 267 for (int i = 0; i != capabilitiesTemp.size(); i++) 268 capabilities.addElement(capabilitiesTemp.elementAt(i)); 269 } 270 for (int i = 0; i != (6 - capabilitiesTemp.size()); i++) 271 capabilities.addElement(null); 272 capabilitiesTemp = new Vector (0, 1); 273 } 274 catch(Exception e) { 275 throw SMIMEException.getInstance(this, e, "addSigner"); 276 } 277 } 278 279 292 public void addSigner (X509Certificate [] chain, PrivateKey privKey, String signingAlg, 293 boolean includingCert, boolean includingSignAttrib) 294 { 295 boolean[] incl = { includingCert, includingSignAttrib }; 296 certChainArray.addElement(chain); 297 privKeyArray.addElement(privKey); 298 digestArray2.addElement(signingAlg); 299 including2.addElement(incl); 300 if (capabilitiesTemp.size() != 0) { 301 for (int i = 0; i != capabilitiesTemp.size(); i++) 302 capabilities2.addElement(capabilitiesTemp.elementAt(i)); 303 } 304 for (int i = 0; i != (6 - capabilitiesTemp.size()); i++) 305 capabilities2.addElement(null); 306 capabilitiesTemp = new Vector (0, 1); 307 } 308 309 327 public void addSigner (KeyStore kStore, String password, String alias, String signingAlg, 328 boolean includingCert, boolean includingSignAttrib) throws SMIMEException 329 { 330 try { 331 char[] paswCh = password.toCharArray(); 332 X509Certificate [] chain = null; 333 PrivateKey privKey = null; 334 335 if (alias != null) { 336 Certificate [] certs = kStore.getCertificateChain(alias); 337 if (certs != null && certs.length > 0) { 338 chain = new X509Certificate [certs.length]; 339 for (int i = 0; i != certs.length; i++) 340 chain[i] = (X509Certificate ) certs[i]; 341 } 342 privKey = (PrivateKey ) kStore.getKey(alias, paswCh); 343 } 344 else { 345 chain = PFXUtils.getCertificateChain(kStore); 346 if (chain == null) 347 chain = PFXUtils.getAllX509Certificate(kStore); 348 349 privKey = PFXUtils.getPrivateKey(kStore); 350 } 351 352 this.addSigner(chain, privKey, signingAlg, includingCert, includingSignAttrib); 353 } 354 catch (Exception e) { 355 throw SMIMEException.getInstance(this, e, "addSigner"); 356 } 357 } 358 359 385 public void addSigner (String ksPath, String ksType, String password, String alias, String signingAlg, 386 boolean includingCert, boolean includingSignAttrib) throws SMIMEException 387 { 388 char[] paswCh = password.toCharArray(); 389 File fks = new File (ksPath); 390 if (! (fks.exists() && fks.isFile())) 391 throw new SMIMEException(this, 1034); 392 393 try { 394 if (ksType == null) this.addSigner(ksPath, password, signingAlg, includingCert, includingSignAttrib); 396 else { 397 FileInputStream fis = new FileInputStream (fks); 398 KeyStore kStore = KeyStore.getInstance(ksType); 399 kStore.load(fis, paswCh); 400 fis.close(); 401 402 this.addSigner (kStore, password, alias, signingAlg, includingCert, includingSignAttrib); 403 } 404 } 405 catch (Exception e) { 406 throw SMIMEException.getInstance(this, e, "addSigner"); 407 } 408 } 409 410 414 public void addCertificate (X509Certificate cert) { 415 aditionalCerts.addElement(cert); 416 } 417 418 424 public void reset() { 425 super.reset(); 426 this.ksArray.removeAllElements(); 427 this.digestArray.removeAllElements(); 428 this.including.removeAllElements(); 429 this.certChainArray.removeAllElements(); 430 this.privKeyArray.removeAllElements(); 431 this.digestArray2.removeAllElements(); 432 this.including2.removeAllElements(); 433 this.aditionalCerts.removeAllElements(); 434 this.capabilitiesTemp.removeAllElements(); 435 this.capabilities.removeAllElements(); 436 this.capabilities2.removeAllElements(); 437 } 438 439 440 } | Popular Tags |