AsnEncodedDataTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // AsnEncodedDataTest.cs - NUnit tests for AsnEncodedData
  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.Security.Cryptography;
  33. using System.Security.Cryptography.X509Certificates;
  34. namespace MonoTests.System.Security.Cryptography {
  35. [TestFixture]
  36. public class AsnEncodedDataTest {
  37. static byte[] asnNullBytes = { 0x05, 0x00 };
  38. static string asnNullString = "05 00";
  39. static byte[] asnLongBytes = { 0x30,0x5C,0x02,0x55,0x2D,0x58,0xE9,0xBF,0xF0,0x31,0xCD,0x79,0x06,0x50,0x5A,0xD5,0x9E,0x0E,0x2C,0xE6,0xC2,0xF7,0xF9,0xD2,0xCE,0x55,0x64,0x85,0xB1,0x90,0x9A,0x92,0xB3,0x36,0xC1,0xBC,0xEA,0xC8,0x23,0xB7,0xAB,0x3A,0xA7,0x64,0x63,0x77,0x5F,0x84,0x22,0x8E,0xE5,0xB6,0x45,0xDD,0x46,0xAE,0x0A,0xDD,0x00,0xC2,0x1F,0xBA,0xD9,0xAD,0xC0,0x75,0x62,0xF8,0x95,0x82,0xA2,0x80,0xB1,0x82,0x69,0xFA,0xE1,0xAF,0x7F,0xBC,0x7D,0xE2,0x7C,0x76,0xD5,0xBC,0x2A,0x80,0xFB,0x02,0x03,0x01,0x00,0x01 };
  40. static string asnLongString = "30 5c 02 55 2d 58 e9 bf f0 31 cd 79 06 50 5a d5 9e 0e 2c e6 c2 f7 f9 d2 ce 55 64 85 b1 90 9a 92 b3 36 c1 bc ea c8 23 b7 ab 3a a7 64 63 77 5f 84 22 8e e5 b6 45 dd 46 ae 0a dd 00 c2 1f ba d9 ad c0 75 62 f8 95 82 a2 80 b1 82 69 fa e1 af 7f bc 7d e2 7c 76 d5 bc 2a 80 fb 02 03 01 00 01";
  41. [Test]
  42. public void Constructor_StringData ()
  43. {
  44. AsnEncodedData aed = new AsnEncodedData ("oid", asnNullBytes);
  45. Assert.AreEqual ("oid", aed.Oid.Value, "Oid.Value");
  46. Assert.IsNull (aed.Oid.FriendlyName, "Oid.FriendlyName");
  47. Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData");
  48. Assert.AreEqual (asnNullString, aed.Format (true), "Format");
  49. }
  50. [Test]
  51. [ExpectedException (typeof (ArgumentNullException))]
  52. public void Constructor_StringNullData ()
  53. {
  54. string oid = null; // do not confuse compiler
  55. AsnEncodedData aed = new AsnEncodedData (oid, asnNullBytes);
  56. }
  57. [Test]
  58. [ExpectedException (typeof (ArgumentNullException))]
  59. public void Constructor_StringDataNull ()
  60. {
  61. AsnEncodedData aed = new AsnEncodedData ("oid", null);
  62. }
  63. [Test]
  64. public void Constructor_OidData ()
  65. {
  66. Oid o = new Oid ("1.0");
  67. AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes);
  68. Assert.AreEqual ("1.0", aed.Oid.Value, "Oid.Value");
  69. Assert.IsNull (aed.Oid.FriendlyName, "Oid.FriendlyName");
  70. Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData");
  71. Assert.AreEqual (asnNullString, aed.Format (true), "Format");
  72. }
  73. [Test]
  74. public void Constructor_OidNullData ()
  75. {
  76. // this is legal - http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=38336cfa-3b97-47da-ad4e-9522d557f001
  77. Oid o = null;
  78. AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes);
  79. Assert.IsNull (aed.Oid, "Oid");
  80. Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData");
  81. Assert.AreEqual (asnNullString, aed.Format (true), "Format");
  82. }
  83. [Test]
  84. [ExpectedException (typeof (ArgumentNullException))]
  85. public void Constructor_OidDataNull ()
  86. {
  87. Oid o = new Oid ("1.0");
  88. AsnEncodedData aed = new AsnEncodedData (o, null);
  89. }
  90. [Test]
  91. public void Constructor_Asn ()
  92. {
  93. AsnEncodedData aed = new AsnEncodedData ("oid", asnNullBytes);
  94. AsnEncodedData aed2 = new AsnEncodedData (aed);
  95. Assert.AreEqual (aed.Oid.Value, aed2.Oid.Value, "Oid.Value");
  96. Assert.AreEqual (aed.Oid.FriendlyName, aed2.Oid.FriendlyName, "Oid.FriendlyName");
  97. Assert.AreEqual (BitConverter.ToString (aed.RawData), BitConverter.ToString (aed2.RawData), "RawData");
  98. string s1 = aed.Format (false);
  99. string s2 = aed.Format (true);
  100. Assert.AreEqual (s1, s2, "Format");
  101. }
  102. [Test]
  103. [ExpectedException (typeof (ArgumentNullException))]
  104. public void Constructor_ByteArrayNull ()
  105. {
  106. byte[] array = null;
  107. AsnEncodedData aed = new AsnEncodedData (array);
  108. }
  109. [Test]
  110. [ExpectedException (typeof (ArgumentNullException))]
  111. public void Constructor_AsnNull ()
  112. {
  113. AsnEncodedData asn = null;
  114. AsnEncodedData aed = new AsnEncodedData (asn);
  115. }
  116. [Test]
  117. public void Oid_CreatedNull ()
  118. {
  119. AsnEncodedData aed = new AsnEncodedData ((Oid)null, asnNullBytes);
  120. Assert.IsNull (aed.Oid, "Oid 1");
  121. Oid o = new Oid ("1.2.3");
  122. aed.Oid = o;
  123. Assert.AreEqual ("1.2.3", aed.Oid.Value, "Oid 2");
  124. o.Value = "1.2.4";
  125. Assert.AreEqual ("1.2.3", aed.Oid.Value, "Oid 3"); // didn't change (copy)
  126. aed.Oid = null;
  127. Assert.IsNull (aed.Oid, "Oid 4");
  128. }
  129. [Test]
  130. public void Oid ()
  131. {
  132. AsnEncodedData aed = new AsnEncodedData ("1.2.3", asnNullBytes);
  133. Assert.AreEqual ("1.2.3", aed.Oid.Value, "Oid 1");
  134. aed.Oid.Value = "1.2.4";
  135. Assert.AreEqual ("1.2.4", aed.Oid.Value, "Oid 2"); // didn't change (copy)
  136. aed.Oid = null;
  137. Assert.IsNull (aed.Oid, "Oid 3");
  138. }
  139. [Test]
  140. public void RawData_CanModify ()
  141. {
  142. byte[] data = (byte[])asnNullBytes.Clone ();
  143. AsnEncodedData aed = new AsnEncodedData ("1.2.3", data);
  144. Assert.AreEqual (asnNullString, aed.Format (true), "Format 1");
  145. data[0] = 0x06;
  146. Assert.AreEqual (asnNullString, aed.Format (true), "Format 2"); ; // didn't change (copy)
  147. aed.RawData[0] = 0x07;
  148. Assert.AreEqual ("07 00", aed.Format (true), "Format 3"); // changed!
  149. }
  150. [Test]
  151. [ExpectedException (typeof (ArgumentNullException))]
  152. public void RawData ()
  153. {
  154. AsnEncodedData aed = new AsnEncodedData ((Oid)null, asnNullBytes);
  155. Assert.AreEqual (asnNullString, aed.Format (true), "Format 1");
  156. aed.RawData = null;
  157. }
  158. [Test]
  159. [ExpectedException (typeof (ArgumentNullException))]
  160. public void CopyFrom_Null ()
  161. {
  162. AsnEncodedData aed = new AsnEncodedData ((Oid)null, asnNullBytes);
  163. aed.CopyFrom (null);
  164. }
  165. [Test]
  166. public void CopyFrom ()
  167. {
  168. Oid o = new Oid ("1.2.3");
  169. byte[] data = (byte[])asnNullBytes.Clone ();
  170. AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes);
  171. AsnEncodedData copy = new AsnEncodedData ((Oid)null, new byte [0]);
  172. copy.CopyFrom (aed);
  173. Assert.AreEqual (aed.Oid.Value, copy.Oid.Value, "Oid 1");
  174. Assert.AreEqual (aed.Format (true), copy.Format (true), "Format 1");
  175. aed.Oid = new Oid ("1.2.4");
  176. aed.RawData = new byte[1];
  177. Assert.AreEqual ("1.2.3", copy.Oid.Value, "Oid 2");
  178. Assert.AreEqual (asnNullString, copy.Format (true), "Format 2");
  179. }
  180. [Test]
  181. public void Format ()
  182. {
  183. AsnEncodedData aed = new AsnEncodedData ("1.2.840.113549.1.1.1", asnLongBytes);
  184. Assert.AreEqual ("1.2.840.113549.1.1.1", aed.Oid.Value, "Oid.Value");
  185. Assert.AreEqual ("RSA", aed.Oid.FriendlyName, "Oid.FriendlyName");
  186. Assert.AreEqual (BitConverter.ToString (asnLongBytes), BitConverter.ToString (aed.RawData), "RawData");
  187. string result = aed.Format (false);
  188. Assert.AreEqual (asnLongString, result, "Format(false)");
  189. }
  190. [Test]
  191. public void FormatMultiline ()
  192. {
  193. AsnEncodedData aed = new AsnEncodedData ("1.2.840.113549.1.1.1", asnLongBytes);
  194. Assert.AreEqual ("1.2.840.113549.1.1.1", aed.Oid.Value, "Oid.Value");
  195. Assert.AreEqual ("RSA", aed.Oid.FriendlyName, "Oid.FriendlyName");
  196. Assert.AreEqual (BitConverter.ToString (asnLongBytes), BitConverter.ToString (aed.RawData), "RawData");
  197. string result = aed.Format (true);
  198. Assert.AreEqual (asnLongString, result, "Format(true)");
  199. }
  200. [Test]
  201. public void Build_X509EnhancedKeyUsageExtension ()
  202. {
  203. AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 });
  204. Assert.AreEqual ("30 05 06 03 2a 03 04", aed.Format (true), "Format(true)");
  205. Assert.AreEqual ("30 05 06 03 2a 03 04", aed.Format (false), "Format(false)");
  206. aed.Oid = new Oid ("2.5.29.37");
  207. // and now "AsnEncodedData" knows how to (magically) decode the data without involving the class
  208. Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, aed.Format (true), "aed.Format(true)");
  209. Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", aed.Format (false), "aed.Format(false)");
  210. // compare with the output of the "appropriate" class
  211. X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (aed, false);
  212. Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, eku.Format (true), "eku.Format(true)");
  213. Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", eku.Format (false), "eku.Format(false)");
  214. }
  215. [Test]
  216. // note: important to emulate in Mono because we need it for SSL/TLS
  217. public void Build_NetscapeCertTypeExtension ()
  218. {
  219. AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x03, 0x02, 0x01, 0x06 });
  220. Assert.AreEqual ("03 02 01 06", aed.Format (true), "Format(true)");
  221. Assert.AreEqual ("03 02 01 06", aed.Format (false), "Format(false)");
  222. aed.Oid = new Oid ("2.16.840.1.113730.1.1");
  223. // and now "AsnEncodedData" knows how to (magically) decode the data without involving the class
  224. Assert.AreEqual ("SSL CA, SMIME CA (06)", aed.Format (true), "aed.Format(true)");
  225. Assert.AreEqual ("SSL CA, SMIME CA (06)", aed.Format (false), "aed.Format(false)");
  226. // note that the Fx doesn't "really" support this extension
  227. // and strangely no NewLine is being appended to Format(true)
  228. // finally this also means that the Oid "knowns" about oid not used in the Fx itself
  229. Assert.AreEqual ("Netscape Cert Type", aed.Oid.FriendlyName, "FriendlyName");
  230. // anyway the answer is most probably CryptoAPI
  231. }
  232. [Test]
  233. // note: important to emulate in Mono because we need it for SSL/TLS
  234. public void Build_SubjectAltNameExtension ()
  235. {
  236. AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x30, 0x16, 0x82, 0x14, 0x77, 0x77, 0x77, 0x2E, 0x6D, 0x6F, 0x6E, 0x6F, 0x2D, 0x70, 0x72, 0x6F, 0x6A, 0x65, 0x63, 0x74, 0x2E, 0x63, 0x6F, 0x6D });
  237. Assert.AreEqual ("30 16 82 14 77 77 77 2e 6d 6f 6e 6f 2d 70 72 6f 6a 65 63 74 2e 63 6f 6d", aed.Format (true), "Format(true)");
  238. Assert.AreEqual ("30 16 82 14 77 77 77 2e 6d 6f 6e 6f 2d 70 72 6f 6a 65 63 74 2e 63 6f 6d", aed.Format (false), "Format(false)");
  239. aed.Oid = new Oid ("2.5.29.17");
  240. // and now "AsnEncodedData" knows how to (magically) decode the data without involving the class
  241. Assert.AreEqual ("DNS Name=www.mono-project.com" + Environment.NewLine, aed.Format (true), "aed.Format(true)");
  242. Assert.AreEqual ("DNS Name=www.mono-project.com", aed.Format (false), "aed.Format(false)");
  243. // note that the Fx doesn't "really" support this extension
  244. // finally this also means that the Oid "knowns" about oid not used in the Fx itself
  245. Assert.AreEqual ("Subject Alternative Name", aed.Oid.FriendlyName, "FriendlyName");
  246. // anyway the answer is most probably CryptoAPI
  247. }
  248. }
  249. }
  250. #endif