SignedCms.cs 8.5 KB

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