CmsSignerTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // CmsSignerTest.cs - NUnit tests for CmsSigner
  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 NUnit.Framework;
  31. using System;
  32. using System.Collections;
  33. using System.Security.Cryptography;
  34. using System.Security.Cryptography.Pkcs;
  35. using System.Security.Cryptography.X509Certificates;
  36. namespace MonoTests.System.Security.Cryptography.Pkcs {
  37. [TestFixture]
  38. public class CmsSignerTest : Assertion {
  39. static byte[] asnNull = { 0x05, 0x00 };
  40. static string sha1Oid = "1.3.14.3.2.26";
  41. static string sha1Name = "sha1";
  42. static string rsaOid = "1.2.840.113549.1.1.1";
  43. static string rsaName = "RSA";
  44. [Test]
  45. public void ConstructorEmpty ()
  46. {
  47. CmsSigner ps = new CmsSigner ();
  48. // default properties
  49. AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
  50. AssertNull ("Certificate", ps.Certificate);
  51. AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
  52. AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
  53. AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
  54. AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
  55. AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
  56. }
  57. [Test]
  58. public void ConstructorIssuerAndSerialNumber ()
  59. {
  60. CmsSigner ps = new CmsSigner (SubjectIdentifierType.IssuerAndSerialNumber);
  61. // default properties
  62. AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
  63. AssertNull ("Certificate", ps.Certificate);
  64. AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
  65. AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
  66. AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
  67. AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
  68. AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
  69. }
  70. [Test]
  71. public void ConstructorSubjectKeyIdentifier ()
  72. {
  73. CmsSigner ps = new CmsSigner (SubjectIdentifierType.SubjectKeyIdentifier);
  74. // default properties
  75. AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
  76. AssertNull ("Certificate", ps.Certificate);
  77. AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
  78. AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
  79. AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
  80. AssertEquals ("SignerIdentifierType", SubjectIdentifierType.SubjectKeyIdentifier, ps.SignerIdentifierType);
  81. AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
  82. }
  83. [Test]
  84. public void ConstructorUnknown ()
  85. {
  86. CmsSigner ps = new CmsSigner (SubjectIdentifierType.Unknown);
  87. // default properties
  88. AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
  89. AssertNull ("Certificate", ps.Certificate);
  90. AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
  91. AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
  92. AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
  93. // Unknown is converted to IssuerAndSerialNumber
  94. AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
  95. AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
  96. }
  97. // TODO: return valid x509 certifiate with private key
  98. private X509Certificate2 GetValidCertificateWithPrivateKey ()
  99. {
  100. X509Certificate2 x509 = new X509Certificate2 ();
  101. return x509;
  102. }
  103. [Test]
  104. public void ConstructorX509CertificateEx ()
  105. {
  106. X509Certificate2 x509 = GetValidCertificateWithPrivateKey ();
  107. CmsSigner ps = new CmsSigner (x509);
  108. // default properties
  109. AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
  110. AssertNotNull ("Certificate", ps.Certificate);
  111. AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
  112. AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
  113. AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
  114. AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
  115. AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
  116. }
  117. [Test]
  118. public void ConstructorX509CertificateExEmpty ()
  119. {
  120. X509Certificate2 x509 = new X509Certificate2 (); // empty
  121. CmsSigner ps = new CmsSigner (x509);
  122. // default properties
  123. AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
  124. AssertNotNull ("Certificate", ps.Certificate);
  125. AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
  126. AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
  127. AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
  128. AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
  129. AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
  130. }
  131. [Test]
  132. //BUG [ExpectedException (typeof (ArgumentNullException))]
  133. public void ConstructorX509CertificateExNull ()
  134. {
  135. X509Certificate2 x509 = null;
  136. CmsSigner ps = new CmsSigner (x509);
  137. // default properties
  138. AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
  139. AssertNull ("Certificate", ps.Certificate);
  140. AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
  141. AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
  142. AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
  143. AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
  144. AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
  145. }
  146. [Test]
  147. public void ConstructorIssuerAndSerialNumberX509CertificateEx ()
  148. {
  149. X509Certificate2 x509 = GetValidCertificateWithPrivateKey ();
  150. CmsSigner ps = new CmsSigner (SubjectIdentifierType.IssuerAndSerialNumber, x509);
  151. // default properties
  152. AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
  153. AssertNotNull ("Certificate", ps.Certificate);
  154. AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
  155. AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
  156. AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
  157. AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
  158. AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
  159. }
  160. [Test]
  161. public void ConstructorSubjectKeyIdentifierX509CertificateEx ()
  162. {
  163. X509Certificate2 x509 = GetValidCertificateWithPrivateKey ();
  164. CmsSigner ps = new CmsSigner (SubjectIdentifierType.SubjectKeyIdentifier, x509);
  165. // default properties
  166. AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
  167. AssertNotNull ("Certificate", ps.Certificate);
  168. AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
  169. AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
  170. AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
  171. AssertEquals ("SignerIdentifierType", SubjectIdentifierType.SubjectKeyIdentifier, ps.SignerIdentifierType);
  172. AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
  173. }
  174. [Test]
  175. public void ConstructorUnknownX509CertificateEx ()
  176. {
  177. X509Certificate2 x509 = GetValidCertificateWithPrivateKey ();
  178. CmsSigner ps = new CmsSigner (SubjectIdentifierType.Unknown, x509);
  179. // default properties
  180. AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
  181. AssertNotNull ("Certificate", ps.Certificate);
  182. AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
  183. AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
  184. AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
  185. // Unknown is converted to IssuerAndSerialNumber
  186. AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
  187. AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
  188. }
  189. [Test]
  190. //BUG [ExpectedException (typeof (ArgumentNullException))]
  191. public void ConstructorIssuerAndSerialNumberX509CertificateExNull ()
  192. {
  193. CmsSigner ps = new CmsSigner (SubjectIdentifierType.IssuerAndSerialNumber, null);
  194. // default properties
  195. AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
  196. AssertNull ("Certificate", ps.Certificate);
  197. AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
  198. AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
  199. AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
  200. AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
  201. AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
  202. }
  203. [Test]
  204. public void SignedAttributes ()
  205. {
  206. CmsSigner ps = new CmsSigner ();
  207. AssertEquals ("SignedAttributes=0", 0, ps.SignedAttributes.Count);
  208. ps.SignedAttributes.Add (new Pkcs9DocumentDescription ("mono"));
  209. AssertEquals ("SignedAttributes=1", 1, ps.SignedAttributes.Count);
  210. }
  211. [Test]
  212. public void Certificate ()
  213. {
  214. CmsSigner ps = new CmsSigner ();
  215. AssertNull ("Certificate=default(null)", ps.Certificate);
  216. ps.Certificate = GetValidCertificateWithPrivateKey ();
  217. AssertNotNull ("Certificate!=null", ps.Certificate);
  218. ps.Certificate = null;
  219. AssertNull ("Certificate=null", ps.Certificate);
  220. }
  221. [Test]
  222. public void Digest ()
  223. {
  224. CmsSigner ps = new CmsSigner ();
  225. ps.DigestAlgorithm = new Oid ("1.2.840.113549.2.5");
  226. AssertEquals ("DigestAlgorithm.FriendlyName", "md5", ps.DigestAlgorithm.FriendlyName);
  227. AssertEquals ("DigestAlgorithm.Value", "1.2.840.113549.2.5", ps.DigestAlgorithm.Value);
  228. ps.DigestAlgorithm = null;
  229. AssertNull ("DigestAlgorithm=null", ps.DigestAlgorithm);
  230. }
  231. [Test]
  232. public void IncludeOption ()
  233. {
  234. CmsSigner ps = new CmsSigner ();
  235. ps.IncludeOption = X509IncludeOption.EndCertOnly;
  236. AssertEquals ("EndCertOnly", X509IncludeOption.EndCertOnly, ps.IncludeOption);
  237. ps.IncludeOption = X509IncludeOption.ExcludeRoot;
  238. AssertEquals ("ExcludeRoot", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
  239. ps.IncludeOption = X509IncludeOption.None;
  240. AssertEquals ("None", X509IncludeOption.None, ps.IncludeOption);
  241. ps.IncludeOption = X509IncludeOption.WholeChain;
  242. AssertEquals ("WholeChain", X509IncludeOption.WholeChain, ps.IncludeOption);
  243. }
  244. [Test]
  245. public void SubjectIdentifierTypeProperty ()
  246. {
  247. CmsSigner ps = new CmsSigner ();
  248. ps.SignerIdentifierType = SubjectIdentifierType.IssuerAndSerialNumber;
  249. AssertEquals ("IssuerAndSerialNumber", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
  250. ps.SignerIdentifierType = SubjectIdentifierType.SubjectKeyIdentifier;
  251. AssertEquals ("SubjectKeyIdentifier", SubjectIdentifierType.SubjectKeyIdentifier, ps.SignerIdentifierType);
  252. }
  253. [Test]
  254. [ExpectedException (typeof (ArgumentException))]
  255. public void SubjectIdentifierTypeUnknown ()
  256. {
  257. CmsSigner ps = new CmsSigner ();
  258. ps.SignerIdentifierType = SubjectIdentifierType.Unknown;
  259. }
  260. [Test]
  261. public void UnauthenticatedAttributes ()
  262. {
  263. CmsSigner ps = new CmsSigner ();
  264. AssertEquals ("UnsignedAttributes=0", 0, ps.UnsignedAttributes.Count);
  265. ps.UnsignedAttributes.Add (new Pkcs9DocumentDescription ("mono"));
  266. AssertEquals ("UnsignedAttributes=1", 1, ps.UnsignedAttributes.Count);
  267. }
  268. }
  269. }
  270. #endif