5a36ff9e25a0e3063dc118e54bca1d55841a8a95
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Pkcs / EnvelopedPkcs7.cs
1 //
2 // EnvelopedPkcs7.cs - System.Security.Cryptography.Pkcs.EnvelopedPkcs7
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 #if NET_2_0
11
12 using System;
13 using System.Collections;
14 using System.Security.Cryptography.X509Certificates;
15 using System.Security.Cryptography.Xml;
16 using System.Text;
17
18 using Mono.Security;
19
20 namespace System.Security.Cryptography.Pkcs {
21
22         // References
23         // a.   PKCS #7: Cryptographic Message Syntax, Version 1.5, Section 10
24         //      http://www.faqs.org/rfcs/rfc2315.html
25
26         public class EnvelopedPkcs7 {
27
28                 private ContentInfo _content;
29                 private AlgorithmIdentifier _identifier;
30                 private X509CertificateExCollection _certs;
31                 private RecipientInfoCollection _recipients;
32                 private Pkcs9AttributeCollection _uattribs;
33                 private SubjectIdentifierType _idType;
34                 private int _version;
35
36                 // constructors
37
38                 public EnvelopedPkcs7 () 
39                 {
40                         _certs = new X509CertificateExCollection ();
41                         _recipients = new RecipientInfoCollection ();
42                         _uattribs = new Pkcs9AttributeCollection ();
43                 }
44
45                 public EnvelopedPkcs7 (ContentInfo content) : this ()
46                 {
47                         if (content == null)
48                                 throw new ArgumentNullException ("content");
49
50                         _content = content;
51                 }
52
53                 public EnvelopedPkcs7 (ContentInfo contentInfo, AlgorithmIdentifier encryptionAlgorithm)
54                         : this (contentInfo) 
55                 {
56                         if (encryptionAlgorithm == null)
57                                 throw new ArgumentNullException ("encryptionAlgorithm");
58
59                         _identifier = encryptionAlgorithm;
60                 }
61
62                 public EnvelopedPkcs7 (SubjectIdentifierType recipientIdentifierType, ContentInfo contentInfo) 
63                         : this (contentInfo) 
64                 {
65                         _idType = recipientIdentifierType;
66                         _version = ((_idType == SubjectIdentifierType.SubjectKeyIdentifier) ? 2 : 0);
67                 }
68
69                 public EnvelopedPkcs7 (SubjectIdentifierType recipientIdentifierType, ContentInfo contentInfo, AlgorithmIdentifier encryptionAlgorithm)
70                         : this (contentInfo, encryptionAlgorithm) 
71                 {
72                         _idType = recipientIdentifierType;
73                         _version = ((_idType == SubjectIdentifierType.SubjectKeyIdentifier) ? 2 : 0);
74                 }
75
76                 // properties
77
78                 public X509CertificateExCollection Certificates {
79                         get { return _certs; }
80                 }
81
82                 public AlgorithmIdentifier ContentEncryptionAlgorithm {
83                         get { 
84                                 if (_identifier == null)
85                                         _identifier = new AlgorithmIdentifier ();
86                                 return _identifier; 
87                         }
88                 } 
89
90                 public ContentInfo ContentInfo {
91                         get { 
92                                 if (_content == null) {
93                                         Oid oid = new Oid (PKCS7.data);
94                                         _content = new ContentInfo (oid, new byte [0]);
95                                 }
96                                 return _content; 
97                         }
98                 }
99
100                 public RecipientInfoCollection RecipientInfos {
101                         get { return _recipients; }
102                 }
103
104                 public Pkcs9AttributeCollection UnprotectedAttributes { 
105                         get { return _uattribs; }
106                 }
107
108                 public int Version {
109                         get { return _version; }
110                 }
111
112                 // methods
113
114                 private X509IssuerSerial GetIssuerSerial (string issuer, byte[] serial) 
115                 {
116                         X509IssuerSerial xis = new X509IssuerSerial ();
117                         xis.IssuerName = issuer;
118                         StringBuilder sb = new StringBuilder ();
119                         foreach (byte b in serial)
120                                 sb.Append (b.ToString ("X2"));
121                         xis.SerialNumber = sb.ToString ();
122                         return xis;
123                 }
124
125                 [MonoTODO]
126                 public void Decode (byte[] encodedMessage)
127                 {
128                         if (encodedMessage == null)
129                                 throw new ArgumentNullException ("encodedMessage");
130
131                         PKCS7.ContentInfo ci = new PKCS7.ContentInfo (encodedMessage);
132                         if (ci.ContentType != PKCS7.envelopedData)
133                                 throw new Exception ("");
134
135                         PKCS7.EnvelopedData ed = new PKCS7.EnvelopedData (ci.Content);
136
137                         Oid oid = new Oid (ed.ContentInfo.ContentType);
138                         _content = new ContentInfo (oid, new byte [0]); //ed.ContentInfo.Content.Value);
139
140                         foreach (PKCS7.RecipientInfo ri in ed.RecipientInfos) {
141                                 Oid o = new Oid (ri.Oid);
142                                 AlgorithmIdentifier ai = new AlgorithmIdentifier (o);
143                                 SubjectIdentifier si = null;
144                                 if (ri.SubjectKeyIdentifier != null) {
145                                         si = new SubjectIdentifier (SubjectIdentifierType.SubjectKeyIdentifier, ri.SubjectKeyIdentifier);
146                                 }
147                                 else if ((ri.Issuer != null) && (ri.Serial != null)) {
148                                         X509IssuerSerial xis = GetIssuerSerial (ri.Issuer, ri.Serial);
149                                         si = new SubjectIdentifier (SubjectIdentifierType.IssuerAndSerialNumber, (object)xis);
150                                 }
151                                 
152                                 KeyTransRecipientInfo _keyTrans = new KeyTransRecipientInfo (ri.Key, ai, si, ri.Version);
153                                 _recipients.Add (_keyTrans);
154                         }
155
156                         // TODO - Certificates
157                         // TODO - UnprotectedAttributes 
158
159                         _version = ed.Version;
160                 }
161
162                 [MonoTODO]
163                 public void Decrypt () 
164                 {
165                         throw new InvalidOperationException ("not encrypted");
166                 }
167
168                 [MonoTODO]
169                 public void Decrypt (RecipientInfo recipientInfo) 
170                 {
171                         if (recipientInfo == null)
172                                 throw new ArgumentNullException ("recipientInfo");
173                         Decrypt ();
174                 }
175
176                 [MonoTODO]
177                 public void Decrypt (RecipientInfo recipientInfo, X509CertificateExCollection extraStore)
178                 {
179                         if (recipientInfo == null)
180                                 throw new ArgumentNullException ("recipientInfo");
181                         if (extraStore == null)
182                                 throw new ArgumentNullException ("extraStore");
183                         Decrypt ();
184                 }
185
186                 [MonoTODO]
187                 public void Decrypt (X509CertificateExCollection extraStore) 
188                 {
189                         if (extraStore == null)
190                                 throw new ArgumentNullException ("extraStore");
191                         Decrypt ();
192                 }
193
194                 [MonoTODO]
195                 public byte[] Encode ()
196                 {
197                         throw new InvalidOperationException ("not encrypted");
198                 }
199
200                 [MonoTODO]
201                 public void Encrypt () 
202                 {
203                         if ((_content.Content == null) || (_content.Content.Length == 0))
204                                 throw new CryptographicException ("no content to encrypt");
205                 }
206
207                 [MonoTODO]
208                 public void Encrypt (Pkcs7Recipient recipient)
209                 {
210                         if (recipient == null)
211                                 throw new ArgumentNullException ("recipient");
212                         // TODO
213                         Encrypt ();
214                 }
215
216                 [MonoTODO]
217                 public void Encrypt (Pkcs7RecipientCollection recipients)
218                 {
219                         if (recipients == null)
220                                 throw new ArgumentNullException ("recipients");
221                         // ? foreach on Encrypt Pkcs7Recipient ?
222                 }
223         }
224 }
225
226 #endif