New test.
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Pkcs / EnvelopedCms.cs
1 //
2 // System.Security.Cryptography.Pkcs.EnvelopedCms class
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 using System.Collections;
33 using System.Security.Cryptography.X509Certificates;
34 using System.Security.Cryptography.Xml;
35 using System.Text;
36
37 using Mono.Security;
38
39 namespace System.Security.Cryptography.Pkcs {
40
41         // References
42         // a.   PKCS #7: Cryptographic Message Syntax, Version 1.5, Section 10
43         //      http://www.faqs.org/rfcs/rfc2315.html
44
45         public sealed class EnvelopedCms {
46
47                 private ContentInfo _content;
48                 private AlgorithmIdentifier _identifier;
49                 private X509Certificate2Collection _certs;
50                 private RecipientInfoCollection _recipients;
51                 private CryptographicAttributeObjectCollection _uattribs;
52                 private SubjectIdentifierType _idType;
53                 private int _version;
54
55                 // constructors
56
57                 public EnvelopedCms () 
58                 {
59                         _certs = new X509Certificate2Collection ();
60                         _recipients = new RecipientInfoCollection ();
61                         _uattribs = new CryptographicAttributeObjectCollection ();
62                 }
63
64                 public EnvelopedCms (ContentInfo content) : this ()
65                 {
66                         if (content == null)
67                                 throw new ArgumentNullException ("content");
68
69                         _content = content;
70                 }
71
72                 public EnvelopedCms (ContentInfo contentInfo, AlgorithmIdentifier encryptionAlgorithm)
73                         : this (contentInfo) 
74                 {
75                         if (encryptionAlgorithm == null)
76                                 throw new ArgumentNullException ("encryptionAlgorithm");
77
78                         _identifier = encryptionAlgorithm;
79                 }
80
81                 public EnvelopedCms (SubjectIdentifierType recipientIdentifierType, ContentInfo contentInfo) 
82                         : this (contentInfo) 
83                 {
84                         _idType = recipientIdentifierType;
85                         _version = ((_idType == SubjectIdentifierType.SubjectKeyIdentifier) ? 2 : 0);
86                 }
87
88                 public EnvelopedCms (SubjectIdentifierType recipientIdentifierType, ContentInfo contentInfo, AlgorithmIdentifier encryptionAlgorithm)
89                         : this (contentInfo, encryptionAlgorithm) 
90                 {
91                         _idType = recipientIdentifierType;
92                         _version = ((_idType == SubjectIdentifierType.SubjectKeyIdentifier) ? 2 : 0);
93                 }
94
95                 // properties
96
97                 public X509Certificate2Collection Certificates {
98                         get { return _certs; }
99                 }
100
101                 public AlgorithmIdentifier ContentEncryptionAlgorithm {
102                         get { 
103                                 if (_identifier == null)
104                                         _identifier = new AlgorithmIdentifier ();
105                                 return _identifier; 
106                         }
107                 } 
108
109                 public ContentInfo ContentInfo {
110                         get { 
111                                 if (_content == null) {
112                                         Oid oid = new Oid (PKCS7.Oid.data);
113                                         _content = new ContentInfo (oid, new byte [0]);
114                                 }
115                                 return _content; 
116                         }
117                 }
118
119                 public RecipientInfoCollection RecipientInfos {
120                         get { return _recipients; }
121                 }
122
123                 public CryptographicAttributeObjectCollection UnprotectedAttributes { 
124                         get { return _uattribs; }
125                 }
126
127                 public int Version {
128                         get { return _version; }
129                 }
130
131                 // methods
132
133                 private X509IssuerSerial GetIssuerSerial (string issuer, byte[] serial) 
134                 {
135                         X509IssuerSerial xis = new X509IssuerSerial ();
136                         xis.IssuerName = issuer;
137                         StringBuilder sb = new StringBuilder ();
138                         foreach (byte b in serial)
139                                 sb.Append (b.ToString ("X2"));
140                         xis.SerialNumber = sb.ToString ();
141                         return xis;
142                 }
143
144                 [MonoTODO]
145                 public void Decode (byte[] encodedMessage)
146                 {
147                         if (encodedMessage == null)
148                                 throw new ArgumentNullException ("encodedMessage");
149
150                         PKCS7.ContentInfo ci = new PKCS7.ContentInfo (encodedMessage);
151                         if (ci.ContentType != PKCS7.Oid.envelopedData)
152                                 throw new Exception ("");
153
154                         PKCS7.EnvelopedData ed = new PKCS7.EnvelopedData (ci.Content);
155
156                         Oid oid = new Oid (ed.ContentInfo.ContentType);
157                         _content = new ContentInfo (oid, new byte [0]); //ed.ContentInfo.Content.Value);
158
159                         foreach (PKCS7.RecipientInfo ri in ed.RecipientInfos) {
160                                 Oid o = new Oid (ri.Oid);
161                                 AlgorithmIdentifier ai = new AlgorithmIdentifier (o);
162                                 SubjectIdentifier si = null;
163                                 if (ri.SubjectKeyIdentifier != null) {
164                                         si = new SubjectIdentifier (SubjectIdentifierType.SubjectKeyIdentifier, ri.SubjectKeyIdentifier);
165                                 }
166                                 else if ((ri.Issuer != null) && (ri.Serial != null)) {
167                                         X509IssuerSerial xis = GetIssuerSerial (ri.Issuer, ri.Serial);
168                                         si = new SubjectIdentifier (SubjectIdentifierType.IssuerAndSerialNumber, (object)xis);
169                                 }
170                                 
171                                 KeyTransRecipientInfo _keyTrans = new KeyTransRecipientInfo (ri.Key, ai, si, ri.Version);
172                                 _recipients.Add (_keyTrans);
173                         }
174
175                         // TODO - Certificates
176                         // TODO - UnprotectedAttributes 
177
178                         _version = ed.Version;
179                 }
180
181                 [MonoTODO]
182                 public void Decrypt () 
183                 {
184                         throw new InvalidOperationException ("not encrypted");
185                 }
186
187                 [MonoTODO]
188                 public void Decrypt (RecipientInfo recipientInfo) 
189                 {
190                         if (recipientInfo == null)
191                                 throw new ArgumentNullException ("recipientInfo");
192                         Decrypt ();
193                 }
194
195                 [MonoTODO]
196                 public void Decrypt (RecipientInfo recipientInfo, X509Certificate2Collection extraStore)
197                 {
198                         if (recipientInfo == null)
199                                 throw new ArgumentNullException ("recipientInfo");
200                         if (extraStore == null)
201                                 throw new ArgumentNullException ("extraStore");
202                         Decrypt ();
203                 }
204
205                 [MonoTODO]
206                 public void Decrypt (X509Certificate2Collection extraStore) 
207                 {
208                         if (extraStore == null)
209                                 throw new ArgumentNullException ("extraStore");
210                         Decrypt ();
211                 }
212
213                 [MonoTODO]
214                 public byte[] Encode ()
215                 {
216                         throw new InvalidOperationException ("not encrypted");
217                 }
218
219                 [MonoTODO]
220                 public void Encrypt () 
221                 {
222                         if ((_content == null) || (_content.Content == null) || (_content.Content.Length == 0))
223                                 throw new CryptographicException ("no content to encrypt");
224                 }
225
226                 [MonoTODO]
227                 public void Encrypt (CmsRecipient recipient)
228                 {
229                         if (recipient == null)
230                                 throw new ArgumentNullException ("recipient");
231                         // TODO
232                         Encrypt ();
233                 }
234
235                 [MonoTODO]
236                 public void Encrypt (CmsRecipientCollection recipients)
237                 {
238                         if (recipients == null)
239                                 throw new ArgumentNullException ("recipients");
240                         // ? foreach on Encrypt CmsRecipient ?
241                 }
242         }
243 }
244
245 #endif