View Javadoc
1 /* 2 * Title: S/MIME Project 3 * Description: S/MIME email sending capabilities 4 * @Author Vladimir Radisic 5 * @Version 2.0.1 6 */ 7 8 9 package org.webdocwf.util.smime.cms; 10 11 12 import org.webdocwf.util.smime.exception.SMIMEException; 13 import org.webdocwf.util.smime.der.DERSetPr; 14 import org.webdocwf.util.smime.der.DERSequence; 15 import java.security.cert.X509Certificate; 16 import java.security.PrivateKey; 17 18 19 /*** 20 * SignerInfos class is DER encoded container, represented in ASN.1 notation 21 * according to RFC2630, used for storing individual information about each signer 22 * of the signed message. Beside other information, SignerInfos class contains 23 * signature of the message.<BR> 24 * <BR> 25 * SignerInfos ::= SET OF SignerInfo<BR> 26 * <BR> 27 * <DL> 28 * SignerInfo ::= SEQUENCE {<BR> 29 * <DD> version CMSVersion,<BR> 30 * <DD> sid SignerIdentifier,<BR> 31 * <DD> digestAlgorithm DigestAlgorithmIdentifier,<BR> 32 * <DD> signedAttrs [0] IMPLICIT SignedAttributes OPTIONAL,<BR> 33 * <DD> signatureAlgorithm SignatureAlgorithmIdentifier,<BR> 34 * <DD> signature SignatureValue,<BR> 35 * <DD> unsignedAttrs [1] IMPLICIT UnsignedAttributes OPTIONAL }<BR> 36 * </DL> 37 */ 38 public class SignerInfos extends DERSetPr { 39 40 /*** 41 * Number of added signers. 42 */ 43 private int countIndicator = 0; 44 45 /*** 46 * Constructs an empty SignerInfos container. 47 * @exception SMIMEException thrown in super class constructor. 48 */ 49 public SignerInfos() throws SMIMEException {} 50 51 /*** 52 * Adds particular signer to SignerInfos. This function must be performed at 53 * least once. 54 * @param message0 message which will be used in process of signing if 55 * parameter sAttr0 is null 56 * @param cert0 owners certificate 57 * @param privKey0 owners private key 58 * @param sAttr0 container for Signed Attributes 59 * @param signedAlg0 specifies signing algorithm type 60 * @param digestAlg0 specifies digest algorithm type 61 * @exception SMIMEException in case of missing owners certificates or 62 * missing private keys. Also, exception could be thrown in super class 63 * addContent method. 64 */ 65 public void addSigner(byte[] message0, X509Certificate cert0, PrivateKey privKey0, SignedAttributes sAttr0, String signedAlg0) throws SMIMEException { 66 String digAlg = null; 67 String sigAlg = null; 68 69 if (signedAlg0.equalsIgnoreCase("SHA1_WITH_RSA")) { 70 digAlg = "SHA1"; 71 sigAlg = "RSA"; 72 } else if (signedAlg0.equalsIgnoreCase("SHA1_WITH_DSA")) { 73 digAlg = "SHA1"; 74 sigAlg = "DSA"; 75 } else if (signedAlg0.equalsIgnoreCase("MD2_WITH_RSA")) { 76 digAlg = "MD2"; 77 sigAlg = "RSA"; 78 } else if (signedAlg0.equalsIgnoreCase("MD5_WITH_RSA")) { 79 digAlg = "MD5"; 80 sigAlg = "RSA"; 81 } 82 DERSequence signerInfo = new DERSequence(); 83 84 signerInfo.addContent(new CMSVersion(1).getDEREncoded()); // Adding cms version 85 if (cert0 == null) 86 throw new SMIMEException(this, 1026); 87 SignerIdentifier signIdent = new SignerIdentifier(cert0); 88 89 signerInfo.addContent(signIdent.getSignIdentifier()); 90 AlgorithmIdentifier digestAlg = new AlgorithmIdentifier(digAlg, "NAME_STRING"); 91 92 digestAlg.addNullToAlgorithmId(); 93 signerInfo.addContent(digestAlg.getDEREncoded()); // Adding digest algorythm identifier 94 if (sAttr0 != null) 95 signerInfo.addContent(sAttr0.getSignedAttribute()); // Adding signed attributes 96 AlgorithmIdentifier signedAlg = new AlgorithmIdentifier(sigAlg, "NAME_STRING"); 97 98 signedAlg.addNullToAlgorithmId(); 99 signerInfo.addContent(signedAlg.getDEREncoded()); // Adding signature algorythm identifier 100 if (privKey0 == null) 101 throw new SMIMEException(this, 1027); 102 SignatureValue sigVal = null; 103 104 if (sAttr0 == null) // If Signed attributes are null, message digest are formed from message content 105 sigVal = new SignatureValue(message0, privKey0, signedAlg0); 106 else { 107 byte[] tempByte = sAttr0.getSignedAttribute(); 108 109 tempByte[0] = 49; // Creating digest dictates "set of" tag rather than implicit tag 110 sigVal = new SignatureValue(tempByte, privKey0, signedAlg0); 111 } 112 signerInfo.addContent(sigVal.getDEREncoded()); // Adding signature value 113 super.addContent(signerInfo.getDEREncoded()); // Adding one signers to SignerInfos 114 countIndicator++; 115 } 116 } 117

This page was automatically generated by Maven