(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Security / System.Security.Cryptography.Pkcs / EnvelopedCms.cs
blob6b1536e2516f49960d7aff5f9087c4d9f2754872
1 //
2 // System.Security.Cryptography.Pkcs.EnvelopedCms
3 //
4 // Author:
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 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.
30 #if NET_2_0
32 using System;
33 using System.Collections;
34 using System.Security.Cryptography.X509Certificates;
35 using System.Security.Cryptography.Xml;
36 using System.Text;
38 using Mono.Security;
40 namespace System.Security.Cryptography.Pkcs {
42 // References
43 // a. PKCS #7: Cryptographic Message Syntax, Version 1.5, Section 10
44 // http://www.faqs.org/rfcs/rfc2315.html
46 public sealed class EnvelopedCms {
48 private ContentInfo _content;
49 private AlgorithmIdentifier _identifier;
50 private X509CertificateExCollection _certs;
51 private RecipientInfoCollection _recipients;
52 private CryptographicAttributeCollection _uattribs;
53 private SubjectIdentifierType _idType;
54 private int _version;
56 // constructors
58 public EnvelopedCms ()
60 _certs = new X509CertificateExCollection ();
61 _recipients = new RecipientInfoCollection ();
62 _uattribs = new CryptographicAttributeCollection ();
65 public EnvelopedCms (ContentInfo content) : this ()
67 if (content == null)
68 throw new ArgumentNullException ("content");
70 _content = content;
73 public EnvelopedCms (ContentInfo contentInfo, AlgorithmIdentifier encryptionAlgorithm)
74 : this (contentInfo)
76 if (encryptionAlgorithm == null)
77 throw new ArgumentNullException ("encryptionAlgorithm");
79 _identifier = encryptionAlgorithm;
82 public EnvelopedCms (SubjectIdentifierType recipientIdentifierType, ContentInfo contentInfo)
83 : this (contentInfo)
85 _idType = recipientIdentifierType;
86 _version = ((_idType == SubjectIdentifierType.SubjectKeyIdentifier) ? 2 : 0);
89 public EnvelopedCms (SubjectIdentifierType recipientIdentifierType, ContentInfo contentInfo, AlgorithmIdentifier encryptionAlgorithm)
90 : this (contentInfo, encryptionAlgorithm)
92 _idType = recipientIdentifierType;
93 _version = ((_idType == SubjectIdentifierType.SubjectKeyIdentifier) ? 2 : 0);
96 // properties
98 public X509CertificateExCollection Certificates {
99 get { return _certs; }
102 public AlgorithmIdentifier ContentEncryptionAlgorithm {
103 get {
104 if (_identifier == null)
105 _identifier = new AlgorithmIdentifier ();
106 return _identifier;
110 public ContentInfo ContentInfo {
111 get {
112 if (_content == null) {
113 Oid oid = new Oid (PKCS7.Oid.data);
114 _content = new ContentInfo (oid, new byte [0]);
116 return _content;
120 public RecipientInfoCollection RecipientInfos {
121 get { return _recipients; }
124 public CryptographicAttributeCollection UnprotectedAttributes {
125 get { return _uattribs; }
128 public int Version {
129 get { return _version; }
132 // methods
134 private X509IssuerSerial GetIssuerSerial (string issuer, byte[] serial)
136 X509IssuerSerial xis = new X509IssuerSerial ();
137 xis.IssuerName = issuer;
138 StringBuilder sb = new StringBuilder ();
139 foreach (byte b in serial)
140 sb.Append (b.ToString ("X2"));
141 xis.SerialNumber = sb.ToString ();
142 return xis;
145 [MonoTODO]
146 public void Decode (byte[] encodedMessage)
148 if (encodedMessage == null)
149 throw new ArgumentNullException ("encodedMessage");
151 PKCS7.ContentInfo ci = new PKCS7.ContentInfo (encodedMessage);
152 if (ci.ContentType != PKCS7.Oid.envelopedData)
153 throw new Exception ("");
155 PKCS7.EnvelopedData ed = new PKCS7.EnvelopedData (ci.Content);
157 Oid oid = new Oid (ed.ContentInfo.ContentType);
158 _content = new ContentInfo (oid, new byte [0]); //ed.ContentInfo.Content.Value);
160 foreach (PKCS7.RecipientInfo ri in ed.RecipientInfos) {
161 Oid o = new Oid (ri.Oid);
162 AlgorithmIdentifier ai = new AlgorithmIdentifier (o);
163 SubjectIdentifier si = null;
164 if (ri.SubjectKeyIdentifier != null) {
165 si = new SubjectIdentifier (SubjectIdentifierType.SubjectKeyIdentifier, ri.SubjectKeyIdentifier);
167 else if ((ri.Issuer != null) && (ri.Serial != null)) {
168 X509IssuerSerial xis = GetIssuerSerial (ri.Issuer, ri.Serial);
169 si = new SubjectIdentifier (SubjectIdentifierType.IssuerAndSerialNumber, (object)xis);
172 KeyTransRecipientInfo _keyTrans = new KeyTransRecipientInfo (ri.Key, ai, si, ri.Version);
173 _recipients.Add (_keyTrans);
176 // TODO - Certificates
177 // TODO - UnprotectedAttributes
179 _version = ed.Version;
182 [MonoTODO]
183 public void Decrypt ()
185 throw new InvalidOperationException ("not encrypted");
188 [MonoTODO]
189 public void Decrypt (RecipientInfo recipientInfo)
191 if (recipientInfo == null)
192 throw new ArgumentNullException ("recipientInfo");
193 Decrypt ();
196 [MonoTODO]
197 public void Decrypt (RecipientInfo recipientInfo, X509CertificateExCollection extraStore)
199 if (recipientInfo == null)
200 throw new ArgumentNullException ("recipientInfo");
201 if (extraStore == null)
202 throw new ArgumentNullException ("extraStore");
203 Decrypt ();
206 [MonoTODO]
207 public void Decrypt (X509CertificateExCollection extraStore)
209 if (extraStore == null)
210 throw new ArgumentNullException ("extraStore");
211 Decrypt ();
214 [MonoTODO]
215 public byte[] Encode ()
217 throw new InvalidOperationException ("not encrypted");
220 [MonoTODO]
221 public void Encrypt ()
223 if ((_content.Content == null) || (_content.Content.Length == 0))
224 throw new CryptographicException ("no content to encrypt");
227 [MonoTODO]
228 public void Encrypt (CmsRecipient recipient)
230 if (recipient == null)
231 throw new ArgumentNullException ("recipient");
232 // TODO
233 Encrypt ();
236 [MonoTODO]
237 public void Encrypt (CmsRecipientCollection recipients)
239 if (recipients == null)
240 throw new ArgumentNullException ("recipients");
241 // ? foreach on Encrypt CmsRecipient ?
246 #endif