PkitsTest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #if NET_2_0
  31. using NUnit.Framework;
  32. using System;
  33. using System.Collections;
  34. using System.IO;
  35. using System.Security.Cryptography;
  36. using System.Security.Cryptography.Pkcs;
  37. using System.Security.Cryptography.X509Certificates;
  38. using System.Text;
  39. namespace MonoTests.System.Security.Cryptography.Pkcs {
  40. /*
  41. * PKITS home page
  42. * http://csrs.nist.gov/pki/testing/x509paths.html
  43. *
  44. * Documentation is available at
  45. * http://csrc.nist.gov/pki/testing/PKITS.pdf
  46. *
  47. * Test data is available at
  48. * http://csrc.nist.gov/pki/testing/PKITS_data.zip
  49. *
  50. * License information are available at
  51. * http://cio.nist.gov/esd/emaildir/lists/pkits/msg00048.html
  52. */
  53. [Category ("PKITS")]
  54. public class PkitsTest {
  55. private string base_dir;
  56. private string certs_base_dir;
  57. private string smime_base_dir;
  58. private Hashtable cache;
  59. private Oid[] policies;
  60. [TestFixtureSetUp]
  61. public void FixtureSetUp ()
  62. {
  63. // reuse PKITS data installed in System (for X509Chain tests)
  64. base_dir = String.Format ("{0}{1}..{1}System{1}Test{1}System.Security.Cryptography.X509Certificates{1}pkits",
  65. Directory.GetCurrentDirectory (), Path.DirectorySeparatorChar);
  66. if (!Directory.Exists (base_dir))
  67. Assert.Ignore ("PKITS tests data not found under '{0}'.", new object[] { base_dir });
  68. certs_base_dir = Path.Combine (base_dir, "certs");
  69. smime_base_dir = Path.Combine (base_dir, "smime");
  70. cache = new Hashtable ();
  71. policies = new Oid[9];
  72. // any-policies
  73. policies[0] = new Oid ("2.5.29.32.0");
  74. // nist_test_policy_#
  75. for (int i=0; i < 9; i++)
  76. policies[i] = new Oid ("2.16.840.1.101.3.2.1.48." + i.ToString ());
  77. }
  78. [TestFixtureTearDown]
  79. public void FixtureTearDown ()
  80. {
  81. cache.Clear ();
  82. }
  83. public X509Certificate2 GetCertificate (string filename)
  84. {
  85. X509Certificate2 result = (cache[filename] as X509Certificate2);
  86. if (result == null) {
  87. string full_path = Path.Combine (certs_base_dir, filename);
  88. result = new X509Certificate2 (full_path);
  89. cache[filename] = result;
  90. }
  91. return result;
  92. }
  93. public byte[] GetData (string filename)
  94. {
  95. string full_path = Path.Combine (smime_base_dir, filename);
  96. using (StreamReader sr = new StreamReader (full_path)) {
  97. string s = sr.ReadLine ();
  98. while (!sr.EndOfStream) {
  99. if (s.Length == 0)
  100. break;
  101. s = sr.ReadLine ();
  102. }
  103. s = sr.ReadToEnd ();
  104. return Convert.FromBase64String (s);
  105. }
  106. }
  107. public X509Certificate2 TrustAnchorRoot {
  108. get { return GetCertificate ("TrustAnchorRootCertificate.crt"); }
  109. }
  110. public X509Certificate2 GoodCACert {
  111. get { return GetCertificate ("GoodCACert.crt"); }
  112. }
  113. // Sadly both SignedCms.CheckHash and SignedCms.CheckSignature returns void and throw an exception.
  114. // This makes it difficult to use in tests because we want to be sure that the "expected exception"
  115. // is being thrown at the "right" place. The next 2 methods hacks around that limitation.
  116. public bool CheckHash (SignedCms cms)
  117. {
  118. try {
  119. cms.CheckSignature (false);
  120. return true;
  121. }
  122. catch {
  123. }
  124. return false;
  125. }
  126. public bool CheckSignature (SignedCms cms)
  127. {
  128. try {
  129. cms.CheckSignature (false);
  130. return true;
  131. }
  132. catch {
  133. }
  134. return false;
  135. }
  136. public Oid AnyPolicy {
  137. get { return policies [0]; }
  138. }
  139. public Oid NistPolicy (int n)
  140. {
  141. return policies[n];
  142. }
  143. }
  144. }
  145. #endif