EnvelopedCms.cs 6.8 KB

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