KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > activation > CMSEnvelopedDataSource


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.activation;
10
11 import org.enhydra.oyster.crypto.SymmetricEncryption;
12 import org.enhydra.oyster.cms.*;
13 import org.enhydra.oyster.util.MimeAssist;
14 import org.enhydra.oyster.util.MimeAssist;
15 import org.enhydra.oyster.exception.SMIMEException;
16 import org.enhydra.oyster.exception.SMIMEIOException;
17 import javax.mail.internet.MimeMessage JavaDoc;
18 import java.security.cert.X509Certificate JavaDoc;
19 import javax.activation.DataSource JavaDoc;
20 import java.io.*;
21
22 /**
23  * CMSEnvelopedDataSource represents implementation of DataSource interfaces. It
24  * is used within MimeMessage as a source of data. Also, object of this class is
25  * used to create DER encoded Cryptographic Message Syntax (CMS) object
26  * represented in ASN.1 notation according to RFC2630. This object (CMS) is used
27  * as the source of data for MimeMessage in the process of sending encrypted message.
28  */

29 public class CMSEnvelopedDataSource implements DataSource JavaDoc {
30
31 /**
32   * Container for encrypted content information
33   */

34   private EncryptedContentInfo encContInf;
35
36 /**
37   * Container for RecipientInfos
38   */

39   private RecipientInfos recInf;
40
41 /**
42  * Constructs CMS object for encryption with MIME Message in form of
43  * byte array and with given values for encryption algorithm.
44  * @param message0 message for encryption
45  * @param algType0 given symmetric algorithm for encryption
46  * @param keyLength0 key length in bits
47  * @exception SMIMEException in case of unrecognized type of symmetryc
48  * algorithm or invalid key length. Also, it can be caused by problems in
49  * construction or work with some inner objects instantiated from classes
50  * that belong to org.enhydra.oyster.der or org.enhydra.oyster.cms
51  * packages used in other CMSEnvelopedDataSource constructor.
52  */

53   public CMSEnvelopedDataSource (byte[] message0, String JavaDoc algType0, int keyLength0) throws SMIMEException
54   {
55     encContInf = new EncryptedContentInfo();
56     SymmetricEncryption symEnc = new SymmetricEncryption(algType0, keyLength0); // Engine for symmetric encryption - setting symetric algorythm type and algorythm parameters (key length)
57
symEnc.encrypt(message0); // algType0 can be one of following strings (representing symetric algorithm type): "DESede","RC2","DES"
58
Content encryptedContent = new Content(symEnc.getEncryptedValue(), false); // Creating the Encrypted Content
59
AlgorithmIdentifier contentEncryptAlgID;
60     if (algType0.equalsIgnoreCase("RC2_CBC")) // Using RC2 algorithm and appropriate parameter
61
{
62       contentEncryptAlgID = new AlgorithmIdentifier("RC2_CBC", "NAME_STRING");
63       RC2CBCParameter par = new RC2CBCParameter(symEnc.getKeyLength(), symEnc.getIV());
64       contentEncryptAlgID.addParamToAlgorithmId(par.getDEREncoded()); // Creating the Content Encryption Algorithm Identifier
65
}
66     else if (algType0.equalsIgnoreCase("DES_EDE3_CBC")) // Using DES EDE3 algorithm and appropriate parameter
67
{
68       contentEncryptAlgID = new AlgorithmIdentifier("DES_EDE3_CBC", "NAME_STRING");
69       DESede3CBCParameter par = new DESede3CBCParameter(symEnc.getIV());
70       contentEncryptAlgID.addParamToAlgorithmId(par.getDEREncoded()); // Creating the Content Encryption Algorithm Identifier
71
}
72     else if (algType0.equalsIgnoreCase("DES")) // Using DES algorithm and appropriate parameter
73
{
74       contentEncryptAlgID = new AlgorithmIdentifier("DES", "NAME_STRING");
75       DESede3CBCParameter par = new DESede3CBCParameter(symEnc.getIV()); // Same parameter as for DES EDE algorithm
76
contentEncryptAlgID.addParamToAlgorithmId(par.getDEREncoded()); // Creating the Content Encryption Algorithm Identifier
77
}
78     else
79       throw new SMIMEException(this, 1013);
80     ContentTypeIdentifier contentData = new ContentTypeIdentifier("ID_DATA", "NAME_STRING"); // Creating the Content Type
81
encContInf.addContentType(contentData.getDEREncoded());
82     encContInf.addEncryptAlgorithmID(contentEncryptAlgID.getDEREncoded());
83     encContInf.addEncryptContent(encryptedContent.getDEREncoded());
84     recInf = new RecipientInfos(symEnc.getSymmetricKey()); // Initialization of Recipient Infos with RC2 symetric key
85
}
86
87 /**
88  * Constructs CMS object for encryption with MIME Message in form of
89  * instance of MimeMessage class and with defined values for encryption algorithm.
90  * @param message0 message for encryption
91  * @param algType0 given symmetric algorithm for encryption
92  * @param keyLength0 key length in bits
93  * @exception SMIMEException in case of unrecognized type of symmetryc
94  * algorithm, invalid key length or in case of failure in MimeMessageConvertor
95  * class which performes transformation from MimeMessage object to byte array.
96  * Also, it can be caused by problems in construction or work with some
97  * inner objects instantiated from classes that belong to
98  * org.enhydra.oyster.der or org.enhydra.oyster.cms packages used
99  * in other CMSEnvelopedDataSource constructor.
100  */

101   public CMSEnvelopedDataSource (MimeMessage JavaDoc message0, String JavaDoc algType0, int keyLength0) throws SMIMEException
102   {
103     this(MimeAssist.messageConvertor(message0), algType0, keyLength0);
104   }
105
106 /**
107  * Adds recipient. This method must be performed at least once.
108  * @param cert0 recipient's X509Certificate (.cer file).
109  * @exception SMIMEException caused by addRecipient method of inner object
110  * which is instance of class RecipientInfos.
111  */

112   public void addRecipient (X509Certificate JavaDoc cert0) throws SMIMEException {
113     recInf.addRecipient(cert0);
114   }
115
116 /**
117  * Returns complete DER encoded CMS Enveloped Object.
118  * @return DER encoded CMS Enveloped Object represented as byte array
119  * @exception SMIMEException caused by problems in construction or work
120  * with some inner objects instantiated from classes that belong to
121  * org.enhydra.oyster.der or org.enhydra.oyster.cms packages.
122  */

123   public byte[] getCMSEnvelopedObject () throws SMIMEException {
124     ContentTypeIdentifier contentTypeEnvelopDataId = new ContentTypeIdentifier("ID_ENVELOPEDDATA", "NAME_STRING"); // Creating the Content Type
125
EnvelopedData envData = new EnvelopedData(); // Container for enveloped data sub object
126
envData.addCMSVersion(new CMSVersion(0).getDEREncoded());
127     envData.addRecipientInfos(recInf.getDEREncoded());
128     envData.addEncryptContentInfo(encContInf.getDEREncoded());
129     Content cont = new Content(envData.getDEREncoded(), true); // Filling enveloped data content in context specific DER object
130
ContentInfo cmsObjectEnvelopedData = new ContentInfo();
131     cmsObjectEnvelopedData.addContentType(contentTypeEnvelopDataId.getDEREncoded());
132     cmsObjectEnvelopedData.addContent(cont.getDEREncoded());
133     return cmsObjectEnvelopedData.getDEREncoded();
134   }
135
136 /**
137  * Returns complete DER encoded CMS Enveloped Object with BASE64 encoding
138  * @return DER encoded CMS Enveloped Object represented as byte array with
139  * performed BASE64 encoding
140  * @exception SMIMEException in case of failure in Base64 encoding performed
141  * on the generated SMIME message byte array by Base64ForMime class. Also, it
142  * can be caused by problems in construction or work with some inner objects
143  * instantiated from classes that belong to org.enhydra.oyster.der or
144  * org.enhydra.oyster.cms packages used in getCMSEnvelopedObject() method.
145  */

146   public byte[] getBASE64CMSEnvelopedObject () throws SMIMEException {
147     return MimeAssist.getBASE64WithBreakOn76(this.getCMSEnvelopedObject());
148   }
149
150 /**
151  * Implements getContentType method from DataSource interface
152  * @return Content-Type for MIME message header field
153  */

154   public String JavaDoc getContentType () {
155     // For new version of mail clients: "application/pkcs7-mime; smime-type=enveloped-data; name=\"smime.p7m\"";
156
return "application/x-pkcs7-mime; smime-type=enveloped-data; name=\"smime.p7m\"";
157   }
158
159 /**
160  * Implements getInputStream method from DataSource interface
161  * @return CMS enveloped object
162  * @exception SMIMEIOException thrown as result of SMIMEException
163  */

164   public InputStream getInputStream () throws SMIMEIOException {
165     try {
166       return new ByteArrayInputStream(getCMSEnvelopedObject());
167     } catch (SMIMEException e) {
168         throw new SMIMEIOException(e);
169     }
170   }
171
172 /**
173  * ImplementS getName method from DataSource interface
174  * @return Name: EnvelopedDataContentInfo
175  */

176   public String JavaDoc getName () {
177     return "EnvelopedDataContentInfo";
178   }
179
180 /**
181  * Implements getOutputStream method from DataSource interface. This method is
182  * not in use.
183  * @return nothing
184  * @exception IOException is always thrown when this method is used.
185  */

186   public OutputStream getOutputStream () throws IOException {
187     throw new IOException("EnvelopedDataContentInfo does not support getOutputStream()");
188   }
189 }
190
191
192
193
Popular Tags