2 // System.Security.Cryptography.Pkcs.EnvelopedCms
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 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.
33 using System
.Collections
;
34 using System
.Security
.Cryptography
.X509Certificates
;
35 using System
.Security
.Cryptography
.Xml
;
40 namespace System
.Security
.Cryptography
.Pkcs
{
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
;
58 public EnvelopedCms ()
60 _certs
= new X509CertificateExCollection ();
61 _recipients
= new RecipientInfoCollection ();
62 _uattribs
= new CryptographicAttributeCollection ();
65 public EnvelopedCms (ContentInfo content
) : this ()
68 throw new ArgumentNullException ("content");
73 public EnvelopedCms (ContentInfo contentInfo
, AlgorithmIdentifier encryptionAlgorithm
)
76 if (encryptionAlgorithm
== null)
77 throw new ArgumentNullException ("encryptionAlgorithm");
79 _identifier
= encryptionAlgorithm
;
82 public EnvelopedCms (SubjectIdentifierType recipientIdentifierType
, ContentInfo 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);
98 public X509CertificateExCollection Certificates
{
99 get { return _certs; }
102 public AlgorithmIdentifier ContentEncryptionAlgorithm
{
104 if (_identifier
== null)
105 _identifier
= new AlgorithmIdentifier ();
110 public ContentInfo ContentInfo
{
112 if (_content
== null) {
113 Oid oid
= new Oid (PKCS7
.Oid
.data
);
114 _content
= new ContentInfo (oid
, new byte [0]);
120 public RecipientInfoCollection RecipientInfos
{
121 get { return _recipients; }
124 public CryptographicAttributeCollection UnprotectedAttributes
{
125 get { return _uattribs; }
129 get { return _version; }
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 ();
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
;
183 public void Decrypt ()
185 throw new InvalidOperationException ("not encrypted");
189 public void Decrypt (RecipientInfo recipientInfo
)
191 if (recipientInfo
== null)
192 throw new ArgumentNullException ("recipientInfo");
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");
207 public void Decrypt (X509CertificateExCollection extraStore
)
209 if (extraStore
== null)
210 throw new ArgumentNullException ("extraStore");
215 public byte[] Encode ()
217 throw new InvalidOperationException ("not encrypted");
221 public void Encrypt ()
223 if ((_content
.Content
== null) || (_content
.Content
.Length
== 0))
224 throw new CryptographicException ("no content to encrypt");
228 public void Encrypt (CmsRecipient recipient
)
230 if (recipient
== null)
231 throw new ArgumentNullException ("recipient");
237 public void Encrypt (CmsRecipientCollection recipients
)
239 if (recipients
== null)
240 throw new ArgumentNullException ("recipients");
241 // ? foreach on Encrypt CmsRecipient ?