KickJava   Java API By Example, From Geeks To Geeks.

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


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.DERSequencePr;
13 import java.security.cert.X509Certificate JavaDoc;
14 import org.enhydra.oyster.crypto.AsymmetricEncryption;
15
16
17 /**
18  * KeyTransRecipientInfo class is DER encoded object represented in ASN.1
19  * notation according to RFC2630. It is used for representing information
20  * about particular recipient and for transport encrypted symmetric key.
21  * This class presents one way of transport symmetric key (they are
22  * two more ways). KeyTransRecipientInfo information in ASN.1 notation is
23  * represented as element named RecipientInfo (withouth s at the end!) which
24  * is inner element of RecipientInfos (for details look at RecipientInfo class).<BR>
25  * <BR>
26  * <DL>
27  * KeyTransRecipientInfo ::= SEQUENCE {<BR>
28  * <DD> version CMSVersion, -- always set to 0 or 2<BR>
29  * <DD> rid RecipientIdentifier,<BR>
30  * <DD> keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,<BR>
31  * <DD> encryptedKey EncryptedKey }<BR>
32  * </DL>
33  */

34 public class KeyTransRecipientInfo extends DERSequencePr {
35
36 /**
37  * Storage for symmetric key.
38  */

39   private byte[] symmetricKey;
40
41 /**
42  * Disable adding more than one recipient.
43  */

44   private int enable = 0;
45
46 /**
47  * Symmetric key is only important parameter, other can be null.
48  * @param symKey0 symmetric key represented as byte array
49  * @exception SMIMEException thrown by super class constructor.
50  */

51   public KeyTransRecipientInfo (byte[] symKey0) throws SMIMEException
52   {
53     symmetricKey = symKey0; // Before constructing this object symmetric key for encrypting of message content must already exist
54
}
55
56 /**
57  * Adds recipient information. This method can be used just one time in one
58  * instance of this class.
59  * @param recip0 X509 certificate of the recipient
60  * @exception SMIMEException if recipient was already added. Also, exception
61  * could be thrown by super class addContent method.
62  */

63   public void addRecipient (X509Certificate JavaDoc recip0) throws SMIMEException {
64     if (enable == 1)
65       throw new SMIMEException(this, 1022);
66     super.addContent(new CMSVersion(0).getDEREncoded()); // Setting CMS version to 1
67
super.addContent(new RecipientIdentifier(recip0).getDEREncoded()); // Adding issuer distinguish name + certificate serial number
68
super.addContent(new AlgorithmIdentifier("RSA", "NAME_STRING").getDEREncoded()); // Adding asymmetric algorythm object identifier
69
AsymmetricEncryption encrAsymmetric = new AsymmetricEncryption(); // Encrypting symmetric key
70
encrAsymmetric.encrypt(recip0.getPublicKey(), symmetricKey);
71     EncryptedKey encKey = new EncryptedKey(encrAsymmetric.getEncryptedValue());
72     super.addContent(encKey.getDEREncoded()); // Adding encrypted symmetric key
73
enable = 1;
74   }
75 }
76
77
78
79
Popular Tags