EnvelopedPkcs7.cs 6.8 KB

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