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 if (_idType == SubjectIdentifierType.SubjectKeyIdentifier)
89 public EnvelopedCms (SubjectIdentifierType recipientIdentifierType, ContentInfo contentInfo, AlgorithmIdentifier encryptionAlgorithm)
90 : this (contentInfo, encryptionAlgorithm)
92 _idType = recipientIdentifierType;
93 if (_idType == SubjectIdentifierType.SubjectKeyIdentifier)
99 public X509Certificate2Collection Certificates {
100 get { return _certs; }
103 public AlgorithmIdentifier ContentEncryptionAlgorithm {
105 if (_identifier == null)
106 _identifier = new AlgorithmIdentifier ();
111 public ContentInfo ContentInfo {
113 if (_content == null) {
114 Oid oid = new Oid (PKCS7.Oid.data);
115 _content = new ContentInfo (oid, new byte [0]);
121 public RecipientInfoCollection RecipientInfos {
122 get { return _recipients; }
125 public CryptographicAttributeObjectCollection UnprotectedAttributes {
126 get { return _uattribs; }
130 get { return _version; }
135 private X509IssuerSerial GetIssuerSerial (string issuer, byte[] serial)
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 ();
147 public void Decode (byte[] encodedMessage)
149 if (encodedMessage == null)
150 throw new ArgumentNullException ("encodedMessage");
152 PKCS7.ContentInfo ci = new PKCS7.ContentInfo (encodedMessage);
153 if (ci.ContentType != PKCS7.Oid.envelopedData)
154 throw new Exception ("");
156 PKCS7.EnvelopedData ed = new PKCS7.EnvelopedData (ci.Content);
158 Oid oid = new Oid (ed.ContentInfo.ContentType);
159 _content = new ContentInfo (oid, new byte [0]); //ed.ContentInfo.Content.Value);
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);
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);
173 KeyTransRecipientInfo _keyTrans = new KeyTransRecipientInfo (ri.Key, ai, si, ri.Version);
174 _recipients.Add (_keyTrans);
177 // TODO - Certificates
178 // TODO - UnprotectedAttributes
180 _version = ed.Version;
184 public void Decrypt ()
186 throw new InvalidOperationException ("not encrypted");
190 public void Decrypt (RecipientInfo recipientInfo)
192 if (recipientInfo == null)
193 throw new ArgumentNullException ("recipientInfo");
198 public void Decrypt (RecipientInfo recipientInfo, X509Certificate2Collection extraStore)
200 if (recipientInfo == null)
201 throw new ArgumentNullException ("recipientInfo");
202 if (extraStore == null)
203 throw new ArgumentNullException ("extraStore");
208 public void Decrypt (X509Certificate2Collection extraStore)
210 if (extraStore == null)
211 throw new ArgumentNullException ("extraStore");
216 public byte[] Encode ()
218 throw new InvalidOperationException ("not encrypted");
222 public void Encrypt ()
224 if ((_content == null) || (_content.Content == null) || (_content.Content.Length == 0))
225 throw new CryptographicException ("no content to encrypt");
229 public void Encrypt (CmsRecipient recipient)
231 if (recipient == null)
232 throw new ArgumentNullException ("recipient");
238 public void Encrypt (CmsRecipientCollection recipients)
240 if (recipients == null)
241 throw new ArgumentNullException ("recipients");
242 // ? foreach on Encrypt CmsRecipient ?