Pkcs7SignerTest.cs 12 KB

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