[bcl] Remove more NET_2_0 checks from class libs
[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 SECURITY_DEP
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                         if (_idType == SubjectIdentifierType.SubjectKeyIdentifier)
86                                 _version = 2;
87                 }
88
89                 public EnvelopedCms (SubjectIdentifierType recipientIdentifierType, ContentInfo contentInfo, AlgorithmIdentifier encryptionAlgorithm)
90                         : this (contentInfo, encryptionAlgorithm) 
91                 {
92                         _idType = recipientIdentifierType;
93                         if (_idType == SubjectIdentifierType.SubjectKeyIdentifier)
94                                 _version = 2;
95                 }
96
97                 // properties
98
99                 public X509Certificate2Collection Certificates {
100                         get { return _certs; }
101                 }
102
103                 public AlgorithmIdentifier ContentEncryptionAlgorithm {
104                         get { 
105                                 if (_identifier == null)
106                                         _identifier = new AlgorithmIdentifier ();
107                                 return _identifier; 
108                         }
109                 } 
110
111                 public ContentInfo ContentInfo {
112                         get { 
113                                 if (_content == null) {
114                                         Oid oid = new Oid (PKCS7.Oid.data);
115                                         _content = new ContentInfo (oid, new byte [0]);
116                                 }
117                                 return _content; 
118                         }
119                 }
120
121                 public RecipientInfoCollection RecipientInfos {
122                         get { return _recipients; }
123                 }
124
125                 public CryptographicAttributeObjectCollection UnprotectedAttributes { 
126                         get { return _uattribs; }
127                 }
128
129                 public int Version {
130                         get { return _version; }
131                 }
132
133                 // methods
134
135                 private X509IssuerSerial GetIssuerSerial (string issuer, byte[] serial) 
136                 {
137                         X509IssuerSerial xis = new X509IssuerSerial ();
138                         xis.IssuerName = issuer;
139                         StringBuilder sb = new StringBuilder ();
140                         foreach (byte b in serial)
141                                 sb.Append (b.ToString ("X2"));
142                         xis.SerialNumber = sb.ToString ();
143                         return xis;
144                 }
145
146                 [MonoTODO]
147                 public void Decode (byte[] encodedMessage)
148                 {
149                         if (encodedMessage == null)
150                                 throw new ArgumentNullException ("encodedMessage");
151
152                         PKCS7.ContentInfo ci = new PKCS7.ContentInfo (encodedMessage);
153                         if (ci.ContentType != PKCS7.Oid.envelopedData)
154                                 throw new Exception ("");
155
156                         PKCS7.EnvelopedData ed = new PKCS7.EnvelopedData (ci.Content);
157
158                         Oid oid = new Oid (ed.ContentInfo.ContentType);
159                         _content = new ContentInfo (oid, new byte [0]); //ed.ContentInfo.Content.Value);
160
161                         foreach (PKCS7.RecipientInfo ri in ed.RecipientInfos) {
162                                 Oid o = new Oid (ri.Oid);
163                                 AlgorithmIdentifier ai = new AlgorithmIdentifier (o);
164                                 SubjectIdentifier si = null;
165                                 if (ri.SubjectKeyIdentifier != null) {
166                                         si = new SubjectIdentifier (SubjectIdentifierType.SubjectKeyIdentifier, ri.SubjectKeyIdentifier);
167                                 }
168                                 else if ((ri.Issuer != null) && (ri.Serial != null)) {
169                                         X509IssuerSerial xis = GetIssuerSerial (ri.Issuer, ri.Serial);
170                                         si = new SubjectIdentifier (SubjectIdentifierType.IssuerAndSerialNumber, (object)xis);
171                                 }
172                                 
173                                 KeyTransRecipientInfo _keyTrans = new KeyTransRecipientInfo (ri.Key, ai, si, ri.Version);
174                                 _recipients.Add (_keyTrans);
175                         }
176
177                         // TODO - Certificates
178                         // TODO - UnprotectedAttributes 
179
180                         _version = ed.Version;
181                 }
182
183                 [MonoTODO]
184                 public void Decrypt () 
185                 {
186                         throw new InvalidOperationException ("not encrypted");
187                 }
188
189                 [MonoTODO]
190                 public void Decrypt (RecipientInfo recipientInfo) 
191                 {
192                         if (recipientInfo == null)
193                                 throw new ArgumentNullException ("recipientInfo");
194                         Decrypt ();
195                 }
196
197                 [MonoTODO]
198                 public void Decrypt (RecipientInfo recipientInfo, X509Certificate2Collection extraStore)
199                 {
200                         if (recipientInfo == null)
201                                 throw new ArgumentNullException ("recipientInfo");
202                         if (extraStore == null)
203                                 throw new ArgumentNullException ("extraStore");
204                         Decrypt ();
205                 }
206
207                 [MonoTODO]
208                 public void Decrypt (X509Certificate2Collection extraStore) 
209                 {
210                         if (extraStore == null)
211                                 throw new ArgumentNullException ("extraStore");
212                         Decrypt ();
213                 }
214
215                 [MonoTODO]
216                 public byte[] Encode ()
217                 {
218                         throw new InvalidOperationException ("not encrypted");
219                 }
220
221                 [MonoTODO]
222                 public void Encrypt () 
223                 {
224                         if ((_content == null) || (_content.Content == null) || (_content.Content.Length == 0))
225                                 throw new CryptographicException ("no content to encrypt");
226                 }
227
228                 [MonoTODO]
229                 public void Encrypt (CmsRecipient recipient)
230                 {
231                         if (recipient == null)
232                                 throw new ArgumentNullException ("recipient");
233                         // TODO
234                         Encrypt ();
235                 }
236
237                 [MonoTODO]
238                 public void Encrypt (CmsRecipientCollection recipients)
239                 {
240                         if (recipients == null)
241                                 throw new ArgumentNullException ("recipients");
242                         // ? foreach on Encrypt CmsRecipient ?
243                 }
244         }
245 }
246
247 #endif