PkitsTest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // PkitsTest.cs - NUnit tests for
  3. // NIST Public Key Interoperability Test Suite (PKITS)
  4. // Certificate Path Validation, Version 1.0, September 2, 2004
  5. //
  6. // Author:
  7. // Sebastien Pouliot <[email protected]>
  8. //
  9. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using NUnit.Framework;
  31. using System;
  32. using System.Collections;
  33. using System.IO;
  34. using System.Security.Cryptography;
  35. using System.Security.Cryptography.Pkcs;
  36. using System.Security.Cryptography.X509Certificates;
  37. using System.Text;
  38. namespace MonoTests.System.Security.Cryptography.Pkcs {
  39. /*
  40. * PKITS home page
  41. * http://csrs.nist.gov/pki/testing/x509paths.html
  42. *
  43. * Documentation is available at
  44. * http://csrc.nist.gov/pki/testing/PKITS.pdf
  45. *
  46. * Test data is available at
  47. * http://csrc.nist.gov/pki/testing/PKITS_data.zip
  48. *
  49. * License information are available at
  50. * http://cio.nist.gov/esd/emaildir/lists/pkits/msg00048.html
  51. */
  52. [Category ("PKITS")]
  53. public class PkitsTest {
  54. private string base_dir;
  55. private string certs_base_dir;
  56. private string smime_base_dir;
  57. private Hashtable cache;
  58. private Oid[] policies;
  59. [TestFixtureSetUp]
  60. public void FixtureSetUp ()
  61. {
  62. // reuse PKITS data installed in System (for X509Chain tests)
  63. base_dir = String.Format ("{0}{1}..{1}System{1}Test{1}System.Security.Cryptography.X509Certificates{1}pkits",
  64. Directory.GetCurrentDirectory (), Path.DirectorySeparatorChar);
  65. if (!Directory.Exists (base_dir))
  66. Assert.Ignore ("PKITS tests data not found under '{0}'.", new object[] { base_dir });
  67. certs_base_dir = Path.Combine (base_dir, "certs");
  68. smime_base_dir = Path.Combine (base_dir, "smime");
  69. cache = new Hashtable ();
  70. policies = new Oid[9];
  71. // any-policies
  72. policies[0] = new Oid ("2.5.29.32.0");
  73. // nist_test_policy_#
  74. for (int i=0; i < 9; i++)
  75. policies[i] = new Oid ("2.16.840.1.101.3.2.1.48." + i.ToString ());
  76. }
  77. [TestFixtureTearDown]
  78. public void FixtureTearDown ()
  79. {
  80. cache.Clear ();
  81. }
  82. public X509Certificate2 GetCertificate (string filename)
  83. {
  84. X509Certificate2 result = (cache[filename] as X509Certificate2);
  85. if (result == null) {
  86. string full_path = Path.Combine (certs_base_dir, filename);
  87. result = new X509Certificate2 (full_path);
  88. cache[filename] = result;
  89. }
  90. return result;
  91. }
  92. public byte[] GetData (string filename)
  93. {
  94. string full_path = Path.Combine (smime_base_dir, filename);
  95. using (StreamReader sr = new StreamReader (full_path)) {
  96. string s = sr.ReadLine ();
  97. while (!sr.EndOfStream) {
  98. if (s.Length == 0)
  99. break;
  100. s = sr.ReadLine ();
  101. }
  102. s = sr.ReadToEnd ();
  103. return Convert.FromBase64String (s);
  104. }
  105. }
  106. public X509Certificate2 TrustAnchorRoot {
  107. get { return GetCertificate ("TrustAnchorRootCertificate.crt"); }
  108. }
  109. public X509Certificate2 GoodCACert {
  110. get { return GetCertificate ("GoodCACert.crt"); }
  111. }
  112. // Sadly both SignedCms.CheckHash and SignedCms.CheckSignature returns void and throw an exception.
  113. // This makes it difficult to use in tests because we want to be sure that the "expected exception"
  114. // is being thrown at the "right" place. The next 2 methods hacks around that limitation.
  115. public bool CheckHash (SignedCms cms)
  116. {
  117. try {
  118. cms.CheckSignature (false);
  119. return true;
  120. }
  121. catch {
  122. }
  123. return false;
  124. }
  125. public bool CheckSignature (SignedCms cms)
  126. {
  127. try {
  128. cms.CheckSignature (false);
  129. return true;
  130. }
  131. catch {
  132. }
  133. return false;
  134. }
  135. public Oid AnyPolicy {
  136. get { return policies [0]; }
  137. }
  138. public Oid NistPolicy (int n)
  139. {
  140. return policies[n];
  141. }
  142. }
  143. }