2 // System.Security.Cryptography.Pkcs.EnvelopedCms class
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Security.Cryptography.X509Certificates;
34 using System.Security.Cryptography.Xml;
39 namespace System.Security.Cryptography.Pkcs {
42 // a. PKCS #7: Cryptographic Message Syntax, Version 1.5, Section 10
43 // http://www.faqs.org/rfcs/rfc2315.html
45 public sealed class EnvelopedCms {
47 private ContentInfo _content;
48 private AlgorithmIdentifier _identifier;
49 private X509Certificate2Collection _certs;
50 private RecipientInfoCollection _recipients;
51 private CryptographicAttributeObjectCollection _uattribs;
52 private SubjectIdentifierType _idType;
57 public EnvelopedCms ()
59 _certs = new X509Certificate2Collection ();
60 _recipients = new RecipientInfoCollection ();
61 _uattribs = new CryptographicAttributeObjectCollection ();
64 public EnvelopedCms (ContentInfo content) : this ()
67 throw new ArgumentNullException ("content");
72 public EnvelopedCms (ContentInfo contentInfo, AlgorithmIdentifier encryptionAlgorithm)
75 if (encryptionAlgorithm == null)
76 throw new ArgumentNullException ("encryptionAlgorithm");
78 _identifier = encryptionAlgorithm;
81 public EnvelopedCms (SubjectIdentifierType recipientIdentifierType, ContentInfo contentInfo)
84 _idType = recipientIdentifierType;
85 _version = ((_idType == SubjectIdentifierType.SubjectKeyIdentifier) ? 2 : 0);
88 public EnvelopedCms (SubjectIdentifierType recipientIdentifierType, ContentInfo contentInfo, AlgorithmIdentifier encryptionAlgorithm)
89 : this (contentInfo, encryptionAlgorithm)
91 _idType = recipientIdentifierType;
92 _version = ((_idType == SubjectIdentifierType.SubjectKeyIdentifier) ? 2 : 0);
97 public X509Certificate2Collection Certificates {
98 get { return _certs; }
101 public AlgorithmIdentifier ContentEncryptionAlgorithm {
103 if (_identifier == null)
104 _identifier = new AlgorithmIdentifier ();
109 public ContentInfo ContentInfo {
111 if (_content == null) {
112 Oid oid = new Oid (PKCS7.Oid.data);
113 _content = new ContentInfo (oid, new byte [0]);
119 public RecipientInfoCollection RecipientInfos {
120 get { return _recipients; }
123 public CryptographicAttributeObjectCollection UnprotectedAttributes {
124 get { return _uattribs; }
128 get { return _version; }
133 private X509IssuerSerial GetIssuerSerial (string issuer, byte[] serial)
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 ();
145 public void Decode (byte[] encodedMessage)
147 if (encodedMessage == null)
148 throw new ArgumentNullException ("encodedMessage");
150 PKCS7.ContentInfo ci = new PKCS7.ContentInfo (encodedMessage);
151 if (ci.ContentType != PKCS7.Oid.envelopedData)
152 throw new Exception ("");
154 PKCS7.EnvelopedData ed = new PKCS7.EnvelopedData (ci.Content);
156 Oid oid = new Oid (ed.ContentInfo.ContentType);
157 _content = new ContentInfo (oid, new byte [0]); //ed.ContentInfo.Content.Value);
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);
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);
171 KeyTransRecipientInfo _keyTrans = new KeyTransRecipientInfo (ri.Key, ai, si, ri.Version);
172 _recipients.Add (_keyTrans);
175 // TODO - Certificates
176 // TODO - UnprotectedAttributes
178 _version = ed.Version;
182 public void Decrypt ()
184 throw new InvalidOperationException ("not encrypted");
188 public void Decrypt (RecipientInfo recipientInfo)
190 if (recipientInfo == null)
191 throw new ArgumentNullException ("recipientInfo");
196 public void Decrypt (RecipientInfo recipientInfo, X509Certificate2Collection extraStore)
198 if (recipientInfo == null)
199 throw new ArgumentNullException ("recipientInfo");
200 if (extraStore == null)
201 throw new ArgumentNullException ("extraStore");
206 public void Decrypt (X509Certificate2Collection extraStore)
208 if (extraStore == null)
209 throw new ArgumentNullException ("extraStore");
214 public byte[] Encode ()
216 throw new InvalidOperationException ("not encrypted");
220 public void Encrypt ()
222 if ((_content == null) || (_content.Content == null) || (_content.Content.Length == 0))
223 throw new CryptographicException ("no content to encrypt");
227 public void Encrypt (CmsRecipient recipient)
229 if (recipient == null)
230 throw new ArgumentNullException ("recipient");
236 public void Encrypt (CmsRecipientCollection recipients)
238 if (recipients == null)
239 throw new ArgumentNullException ("recipients");
240 // ? foreach on Encrypt CmsRecipient ?