SignedCms.cs 8.6 KB

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