SignedCms.cs 8.5 KB

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