KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > cms > SignerInfos


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email sending capabilities
4  * @Author Vladimir Radisic
5  * @Version 2.1.5
6  */

7
8
9 package org.enhydra.oyster.cms;
10
11 import org.enhydra.oyster.exception.SMIMEException;
12 import org.enhydra.oyster.der.DERSetPr;
13 import org.enhydra.oyster.der.DERSequence;
14 import java.security.cert.X509Certificate JavaDoc;
15 import java.security.PrivateKey JavaDoc;
16
17
18 /**
19  * SignerInfos class is DER encoded container, represented in ASN.1 notation
20  * according to RFC2630, used for storing individual information about each signer
21  * of the signed message. Beside other information, SignerInfos class contains
22  * signature of the message.<BR>
23  * <BR>
24  * SignerInfos ::= SET OF SignerInfo<BR>
25  * <BR>
26  * <DL>
27  * SignerInfo ::= SEQUENCE {<BR>
28  * <DD> version CMSVersion,<BR>
29  * <DD> sid SignerIdentifier,<BR>
30  * <DD> digestAlgorithm DigestAlgorithmIdentifier,<BR>
31  * <DD> signedAttrs [0] IMPLICIT SignedAttributes OPTIONAL,<BR>
32  * <DD> signatureAlgorithm SignatureAlgorithmIdentifier,<BR>
33  * <DD> signature SignatureValue,<BR>
34  * <DD> unsignedAttrs [1] IMPLICIT UnsignedAttributes OPTIONAL }<BR>
35  * </DL>
36  */

37 public class SignerInfos extends DERSetPr {
38
39 /**
40  * Number of added signers.
41  */

42   private int countIndicator = 0;
43
44 /**
45  * Constructs an empty SignerInfos container.
46  * @exception SMIMEException thrown in super class constructor.
47  */

48   public SignerInfos () throws SMIMEException
49   {
50   }
51
52 /**
53  * Adds particular signer to SignerInfos. This function must be performed at
54  * least once.
55  * @param message0 message which will be used in process of signing if
56  * parameter sAttr0 is null
57  * @param cert0 owners certificate
58  * @param privKey0 owners private key
59  * @param sAttr0 container for Signed Attributes
60  * @param signedAlg0 specifies signing 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 JavaDoc cert0, PrivateKey JavaDoc privKey0, SignedAttributes sAttr0, String JavaDoc signedAlg0) throws SMIMEException {
66     String JavaDoc digAlg = null;
67     String JavaDoc sigAlg = null;
68     if (signedAlg0.equalsIgnoreCase("SHA1_WITH_RSA")) {
69       digAlg = "SHA1";
70       sigAlg = "RSA";
71     }
72     else if (signedAlg0.equalsIgnoreCase("SHA1_WITH_DSA")) {
73       digAlg = "SHA1";
74       sigAlg = "DSA";
75     }
76     else if (signedAlg0.equalsIgnoreCase("MD2_WITH_RSA")) {
77       digAlg = "MD2";
78       sigAlg = "RSA";
79     }
80     else if (signedAlg0.equalsIgnoreCase("MD5_WITH_RSA")) {
81       digAlg = "MD5";
82       sigAlg = "RSA";
83     }
84     DERSequence signerInfo = new DERSequence();
85     signerInfo.addContent(new CMSVersion(1).getDEREncoded()); // Adding cms version
86
if (cert0 == null)
87       throw new SMIMEException(this, 1026);
88     SignerIdentifier signIdent = new SignerIdentifier(cert0);
89     signerInfo.addContent(signIdent.getSignIdentifier());
90     AlgorithmIdentifier digestAlg = new AlgorithmIdentifier(digAlg, "NAME_STRING");
91     digestAlg.addNullToAlgorithmId();
92     signerInfo.addContent(digestAlg.getDEREncoded()); // Adding digest algorythm identifier
93
if (sAttr0 != null)
94       signerInfo.addContent(sAttr0.getSignedAttribute()); // Adding signed attributes
95
AlgorithmIdentifier signedAlg = new AlgorithmIdentifier(sigAlg, "NAME_STRING");
96     signedAlg.addNullToAlgorithmId();
97     signerInfo.addContent(signedAlg.getDEREncoded()); // Adding signature algorythm identifier
98
if (privKey0 == null)
99       throw new SMIMEException(this, 1027);
100     SignatureValue sigVal = null;
101     if (sAttr0 == null) // If Signed attributes are null, message digest are formed from message content
102
sigVal = new SignatureValue(message0, privKey0, signedAlg0);
103     else {
104       byte[] tempByte = sAttr0.getSignedAttribute();
105       tempByte[0] = 49; // Creating digest dictates "set of" tag rather than implicit tag
106
sigVal = new SignatureValue(tempByte, privKey0, signedAlg0);
107     }
108     signerInfo.addContent(sigVal.getDEREncoded()); // Adding signature value
109
super.addContent(signerInfo.getDEREncoded()); // Adding one signers to SignerInfos
110
countIndicator++;
111   }
112 }
113
114
115
116
Popular Tags