EnvelopedPkcs7.cs 5.7 KB

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