EnvelopedCms.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 SECURITY_DEP
  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 contentInfo) : this ()
  55. {
  56. if (contentInfo == null)
  57. throw new ArgumentNullException ("contentInfo");
  58. _content = contentInfo;
  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. if (_idType == SubjectIdentifierType.SubjectKeyIdentifier)
  72. _version = 2;
  73. }
  74. public EnvelopedCms (SubjectIdentifierType recipientIdentifierType, ContentInfo contentInfo, AlgorithmIdentifier encryptionAlgorithm)
  75. : this (contentInfo, encryptionAlgorithm)
  76. {
  77. _idType = recipientIdentifierType;
  78. if (_idType == SubjectIdentifierType.SubjectKeyIdentifier)
  79. _version = 2;
  80. }
  81. // properties
  82. public X509Certificate2Collection Certificates {
  83. get { return _certs; }
  84. }
  85. public AlgorithmIdentifier ContentEncryptionAlgorithm {
  86. get {
  87. if (_identifier == null)
  88. _identifier = new AlgorithmIdentifier ();
  89. return _identifier;
  90. }
  91. }
  92. public ContentInfo ContentInfo {
  93. get {
  94. if (_content == null) {
  95. Oid oid = new Oid (PKCS7.Oid.data);
  96. _content = new ContentInfo (oid, new byte [0]);
  97. }
  98. return _content;
  99. }
  100. }
  101. public RecipientInfoCollection RecipientInfos {
  102. get { return _recipients; }
  103. }
  104. public CryptographicAttributeObjectCollection UnprotectedAttributes {
  105. get { return _uattribs; }
  106. }
  107. public int Version {
  108. get { return _version; }
  109. }
  110. // methods
  111. private X509IssuerSerial GetIssuerSerial (string issuer, byte[] serial)
  112. {
  113. X509IssuerSerial xis = new X509IssuerSerial ();
  114. xis.IssuerName = issuer;
  115. StringBuilder sb = new StringBuilder ();
  116. foreach (byte b in serial)
  117. sb.Append (b.ToString ("X2"));
  118. xis.SerialNumber = sb.ToString ();
  119. return xis;
  120. }
  121. [MonoTODO]
  122. public void Decode (byte[] encodedMessage)
  123. {
  124. if (encodedMessage == null)
  125. throw new ArgumentNullException ("encodedMessage");
  126. PKCS7.ContentInfo ci = new PKCS7.ContentInfo (encodedMessage);
  127. if (ci.ContentType != PKCS7.Oid.envelopedData)
  128. throw new Exception ("");
  129. PKCS7.EnvelopedData ed = new PKCS7.EnvelopedData (ci.Content);
  130. Oid oid = new Oid (ed.ContentInfo.ContentType);
  131. _content = new ContentInfo (oid, new byte [0]); //ed.ContentInfo.Content.Value);
  132. foreach (PKCS7.RecipientInfo ri in ed.RecipientInfos) {
  133. Oid o = new Oid (ri.Oid);
  134. AlgorithmIdentifier ai = new AlgorithmIdentifier (o);
  135. SubjectIdentifier si = null;
  136. if (ri.SubjectKeyIdentifier != null) {
  137. si = new SubjectIdentifier (SubjectIdentifierType.SubjectKeyIdentifier, ri.SubjectKeyIdentifier);
  138. }
  139. else if ((ri.Issuer != null) && (ri.Serial != null)) {
  140. X509IssuerSerial xis = GetIssuerSerial (ri.Issuer, ri.Serial);
  141. si = new SubjectIdentifier (SubjectIdentifierType.IssuerAndSerialNumber, (object)xis);
  142. }
  143. KeyTransRecipientInfo _keyTrans = new KeyTransRecipientInfo (ri.Key, ai, si, ri.Version);
  144. _recipients.Add (_keyTrans);
  145. }
  146. // TODO - Certificates
  147. // TODO - UnprotectedAttributes
  148. _version = ed.Version;
  149. }
  150. [MonoTODO]
  151. public void Decrypt ()
  152. {
  153. throw new InvalidOperationException ("not encrypted");
  154. }
  155. [MonoTODO]
  156. public void Decrypt (RecipientInfo recipientInfo)
  157. {
  158. if (recipientInfo == null)
  159. throw new ArgumentNullException ("recipientInfo");
  160. Decrypt ();
  161. }
  162. [MonoTODO]
  163. public void Decrypt (RecipientInfo recipientInfo, X509Certificate2Collection extraStore)
  164. {
  165. if (recipientInfo == null)
  166. throw new ArgumentNullException ("recipientInfo");
  167. if (extraStore == null)
  168. throw new ArgumentNullException ("extraStore");
  169. Decrypt ();
  170. }
  171. [MonoTODO]
  172. public void Decrypt (X509Certificate2Collection extraStore)
  173. {
  174. if (extraStore == null)
  175. throw new ArgumentNullException ("extraStore");
  176. Decrypt ();
  177. }
  178. [MonoTODO]
  179. public byte[] Encode ()
  180. {
  181. throw new InvalidOperationException ("not encrypted");
  182. }
  183. [MonoTODO]
  184. public void Encrypt ()
  185. {
  186. if ((_content == null) || (_content.Content == null) || (_content.Content.Length == 0))
  187. throw new CryptographicException ("no content to encrypt");
  188. }
  189. [MonoTODO]
  190. public void Encrypt (CmsRecipient recipient)
  191. {
  192. if (recipient == null)
  193. throw new ArgumentNullException ("recipient");
  194. // TODO
  195. Encrypt ();
  196. }
  197. [MonoTODO]
  198. public void Encrypt (CmsRecipientCollection recipients)
  199. {
  200. if (recipients == null)
  201. throw new ArgumentNullException ("recipients");
  202. // ? foreach on Encrypt CmsRecipient ?
  203. }
  204. }
  205. }
  206. #endif