SignedCms.cs 8.5 KB

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