SignedPkcs7.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // SignedPkcs7.cs - System.Security.Cryptography.Pkcs.SignedPkcs7
  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.Security.Cryptography.X509Certificates;
  32. using System.Security.Cryptography.Xml;
  33. using System.Text;
  34. using Mono.Security;
  35. using Mono.Security.X509;
  36. namespace System.Security.Cryptography.Pkcs {
  37. public sealed class SignedPkcs7 {
  38. private ContentInfo _content;
  39. private bool _detached;
  40. private SignerInfoCollection _info;
  41. private X509CertificateExCollection _certs;
  42. private SubjectIdentifierType _type;
  43. private int _version;
  44. // constructors
  45. public SignedPkcs7 ()
  46. {
  47. _certs = new X509CertificateExCollection ();
  48. _info = new SignerInfoCollection ();
  49. }
  50. public SignedPkcs7 (ContentInfo content) : this (content, false) {}
  51. public SignedPkcs7 (ContentInfo content, bool detached) : this ()
  52. {
  53. if (content == null)
  54. throw new ArgumentNullException ("content");
  55. _content = content;
  56. _detached = detached;
  57. }
  58. public SignedPkcs7 (SubjectIdentifierType signerIdentifierType) : this ()
  59. {
  60. _type = signerIdentifierType;
  61. _version = ((_type == SubjectIdentifierType.SubjectKeyIdentifier) ? 2 : 0);
  62. }
  63. public SignedPkcs7 (SubjectIdentifierType signerIdentifierType, ContentInfo content) : this (content, false)
  64. {
  65. _type = signerIdentifierType;
  66. _version = ((_type == SubjectIdentifierType.SubjectKeyIdentifier) ? 2 : 0);
  67. }
  68. public SignedPkcs7 (SubjectIdentifierType signerIdentifierType, ContentInfo content, bool detached) : this (content, detached)
  69. {
  70. _type = signerIdentifierType;
  71. _version = ((_type == SubjectIdentifierType.SubjectKeyIdentifier) ? 2 : 0);
  72. }
  73. // properties
  74. public X509CertificateExCollection Certificates {
  75. get { return _certs; }
  76. }
  77. public ContentInfo ContentInfo {
  78. get {
  79. if (_content == null) {
  80. Oid oid = new Oid (PKCS7.Oid.data);
  81. _content = new ContentInfo (oid, new byte [0]);
  82. }
  83. return _content;
  84. }
  85. }
  86. public bool Detached {
  87. get { return _detached; }
  88. }
  89. public SignerInfoCollection SignerInfos {
  90. get { return _info; }
  91. }
  92. public int Version {
  93. get { return _version; }
  94. }
  95. // methods
  96. public void CheckSignature (bool verifySignatureOnly)
  97. {
  98. foreach (SignerInfo si in _info) {
  99. si.CheckSignature (verifySignatureOnly);
  100. }
  101. }
  102. public void CheckSignature (X509CertificateExCollection extraStore, bool verifySignatureOnly)
  103. {
  104. foreach (SignerInfo si in _info) {
  105. si.CheckSignature (extraStore, verifySignatureOnly);
  106. }
  107. }
  108. [MonoTODO]
  109. public void ComputeSignature ()
  110. {
  111. throw new CryptographicException ("");
  112. }
  113. [MonoTODO]
  114. public void ComputeSignature (Pkcs7Signer signer)
  115. {
  116. ComputeSignature ();
  117. }
  118. private string ToString (byte[] array)
  119. {
  120. StringBuilder sb = new StringBuilder ();
  121. foreach (byte b in array)
  122. sb.Append (b.ToString ("X2"));
  123. return sb.ToString ();
  124. }
  125. private byte[] GetKeyIdentifier (Mono.Security.X509.X509Certificate x509)
  126. {
  127. // if present in certificate return value of the SubjectKeyIdentifier
  128. Mono.Security.X509.X509Extension extn = x509.Extensions ["2.5.29.14"];
  129. if (extn != null) {
  130. ASN1 bs = new ASN1 (extn.Value.Value);
  131. return bs.Value;
  132. }
  133. // strangely DEPRECATED keyAttributes isn't used here (like KeyUsage)
  134. // if not then we must calculate the SubjectKeyIdentifier ourselve
  135. // Note: MS does that hash on the complete subjectPublicKeyInfo (unlike PKIX)
  136. // http://groups.google.ca/groups?selm=e7RqM%24plCHA.1488%40tkmsftngp02&oe=UTF-8&output=gplain
  137. ASN1 subjectPublicKeyInfo = new ASN1 (0x30);
  138. ASN1 algo = subjectPublicKeyInfo.Add (new ASN1 (0x30));
  139. algo.Add (new ASN1 (CryptoConfig.EncodeOID (x509.KeyAlgorithm)));
  140. // FIXME: does it work for DSA certs (without an 2.5.29.14 extension ?)
  141. algo.Add (new ASN1 (x509.KeyAlgorithmParameters));
  142. byte[] pubkey = x509.PublicKey;
  143. byte[] bsvalue = new byte [pubkey.Length + 1]; // add unused bits (0) before the public key
  144. Array.Copy (pubkey, 0, bsvalue, 1, pubkey.Length);
  145. subjectPublicKeyInfo.Add (new ASN1 (0x03, bsvalue));
  146. SHA1 sha = SHA1.Create ();
  147. return sha.ComputeHash (subjectPublicKeyInfo.GetBytes ());
  148. }
  149. [MonoTODO("incomplete - missing attributes")]
  150. public void Decode (byte[] encodedMessage)
  151. {
  152. PKCS7.ContentInfo ci = new PKCS7.ContentInfo (encodedMessage);
  153. if (ci.ContentType != PKCS7.Oid.signedData)
  154. throw new Exception ("");
  155. PKCS7.SignedData sd = new PKCS7.SignedData (ci.Content);
  156. SubjectIdentifierType type = SubjectIdentifierType.Unknown;
  157. object o = null;
  158. X509CertificateEx x509 = null;
  159. if (sd.SignerInfo.Certificate != null) {
  160. x509 = new X509CertificateEx (sd.SignerInfo.Certificate.RawData);
  161. }
  162. else if ((sd.SignerInfo.IssuerName != null) && (sd.SignerInfo.SerialNumber != null)) {
  163. byte[] serial = sd.SignerInfo.SerialNumber;
  164. Array.Reverse (serial); // ???
  165. type = SubjectIdentifierType.IssuerAndSerialNumber;
  166. X509IssuerSerial xis = new X509IssuerSerial ();
  167. xis.IssuerName = sd.SignerInfo.IssuerName;
  168. xis.SerialNumber = ToString (serial);
  169. o = xis;
  170. // TODO: move to a FindCertificate (issuer, serial, collection)
  171. foreach (Mono.Security.X509.X509Certificate x in sd.Certificates) {
  172. if (x.IssuerName == sd.SignerInfo.IssuerName) {
  173. if (ToString (x.SerialNumber) == xis.SerialNumber) {
  174. x509 = new X509CertificateEx (x.RawData);
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. else if (sd.SignerInfo.SubjectKeyIdentifier != null) {
  181. string ski = ToString (sd.SignerInfo.SubjectKeyIdentifier);
  182. type = SubjectIdentifierType.SubjectKeyIdentifier;
  183. o = (object) ski;
  184. // TODO: move to a FindCertificate (ski, collection)
  185. foreach (Mono.Security.X509.X509Certificate x in sd.Certificates) {
  186. if (ToString (GetKeyIdentifier (x)) == ski) {
  187. x509 = new X509CertificateEx (x.RawData);
  188. break;
  189. }
  190. }
  191. }
  192. SignerInfo si = new SignerInfo (sd.SignerInfo.HashName, x509, type, o, sd.SignerInfo.Version);
  193. // si.AuthenticatedAttributes
  194. // si.UnauthenticatedAttributes
  195. _info.Add (si);
  196. ASN1 content = sd.ContentInfo.Content;
  197. Oid oid = new Oid (sd.ContentInfo.ContentType);
  198. _content = new ContentInfo (oid, content[0].Value);
  199. foreach (Mono.Security.X509.X509Certificate x in sd.Certificates) {
  200. _certs.Add (new X509CertificateEx (x.RawData));
  201. }
  202. _version = sd.Version;
  203. }
  204. [MonoTODO]
  205. public byte[] Encode ()
  206. {
  207. Mono.Security.X509.X509Certificate x509 = null;
  208. /* PKCS7.SignerInfo si = new PKCS7.SignerInfo ();
  209. switch (_type) {
  210. case SubjectIdentifierType.SubjectKeyIdentifier:
  211. si.SubjectKeyIdentifier = GetKeyIdentifier (x509);
  212. break;
  213. default:
  214. // SubjectIdentifierType.IssuerAndSerialNumber
  215. si.IssuerName = x509.IssuerName;
  216. si.SerialNumber = x509.SerialNumber;
  217. break;
  218. }
  219. PKCS7.SignedData sd = new PKCS7.SignedData ();
  220. sd.Version = _version;
  221. sd.SignerInfo = si;
  222. PKCS7.ContentInfo ci = new PKCS7.ContentInfo (PKCS7.signedData);
  223. ci.Content = sd.ASN1;
  224. return ci.GetBytes ();*/
  225. return null;
  226. }
  227. // counterSsignerInfo -> counterSignerInfo
  228. [MonoTODO]
  229. public void RemoveSignature (SignerInfo counterSsignerInfo)
  230. {
  231. }
  232. }
  233. }
  234. #endif