1 // 2 // AsnEncodedDataTest.cs - NUnit tests for AsnEncodedData 3 // 4 // Author: 5 // Sebastien Pouliot <sebastien@ximian.com> 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 30 31 using NUnit.Framework; 32 33 using System; 34 using System.Security.Cryptography; 35 using System.Security.Cryptography.X509Certificates; 36 37 namespace MonoTests.System.Security.Cryptography { 38 39 [TestFixture] 40 public class AsnEncodedDataTest { 41 static byte[] asnNullBytes = { 0x05, 0x00 }; 42 static string asnNullString = "05 00"; 43 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 }; 44 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"; 45 46 [Test] Constructor_StringData()47 public void Constructor_StringData () 48 { 49 AsnEncodedData aed = new AsnEncodedData ("oid", asnNullBytes); 50 Assert.AreEqual ("oid", aed.Oid.Value, "Oid.Value"); 51 Assert.IsNull (aed.Oid.FriendlyName, "Oid.FriendlyName"); 52 Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData"); 53 Assert.AreEqual (asnNullString, aed.Format (true), "Format"); 54 } 55 56 [Test] 57 [ExpectedException (typeof (ArgumentNullException))] Constructor_StringNullData()58 public void Constructor_StringNullData () 59 { 60 string oid = null; // do not confuse compiler 61 AsnEncodedData aed = new AsnEncodedData (oid, asnNullBytes); 62 } 63 64 [Test] 65 [ExpectedException (typeof (ArgumentNullException))] Constructor_StringDataNull()66 public void Constructor_StringDataNull () 67 { 68 AsnEncodedData aed = new AsnEncodedData ("oid", null); 69 } 70 71 [Test] Constructor_OidData()72 public void Constructor_OidData () 73 { 74 Oid o = new Oid ("1.0"); 75 AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes); 76 Assert.AreEqual ("1.0", aed.Oid.Value, "Oid.Value"); 77 Assert.IsNull (aed.Oid.FriendlyName, "Oid.FriendlyName"); 78 Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData"); 79 Assert.AreEqual (asnNullString, aed.Format (true), "Format"); 80 } 81 82 [Test] Constructor_OidNullData()83 public void Constructor_OidNullData () 84 { 85 // this is legal - http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=38336cfa-3b97-47da-ad4e-9522d557f001 86 Oid o = null; 87 AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes); 88 Assert.IsNull (aed.Oid, "Oid"); 89 Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData"); 90 Assert.AreEqual (asnNullString, aed.Format (true), "Format"); 91 } 92 93 [Test] 94 [ExpectedException (typeof (ArgumentNullException))] Constructor_OidDataNull()95 public void Constructor_OidDataNull () 96 { 97 Oid o = new Oid ("1.0"); 98 AsnEncodedData aed = new AsnEncodedData (o, null); 99 } 100 101 [Test] Constructor_Asn()102 public void Constructor_Asn () 103 { 104 AsnEncodedData aed = new AsnEncodedData ("oid", asnNullBytes); 105 AsnEncodedData aed2 = new AsnEncodedData (aed); 106 Assert.AreEqual (aed.Oid.Value, aed2.Oid.Value, "Oid.Value"); 107 Assert.AreEqual (aed.Oid.FriendlyName, aed2.Oid.FriendlyName, "Oid.FriendlyName"); 108 Assert.AreEqual (BitConverter.ToString (aed.RawData), BitConverter.ToString (aed2.RawData), "RawData"); 109 string s1 = aed.Format (false); 110 string s2 = aed.Format (true); 111 Assert.AreEqual (s1, s2, "Format"); 112 } 113 114 [Test] 115 [ExpectedException (typeof (ArgumentNullException))] Constructor_ByteArrayNull()116 public void Constructor_ByteArrayNull () 117 { 118 byte[] array = null; 119 AsnEncodedData aed = new AsnEncodedData (array); 120 } 121 122 [Test] 123 [ExpectedException (typeof (ArgumentNullException))] Constructor_AsnNull()124 public void Constructor_AsnNull () 125 { 126 AsnEncodedData asn = null; 127 AsnEncodedData aed = new AsnEncodedData (asn); 128 } 129 130 [Test] Oid_CreatedNull()131 public void Oid_CreatedNull () 132 { 133 AsnEncodedData aed = new AsnEncodedData ((Oid)null, asnNullBytes); 134 Assert.IsNull (aed.Oid, "Oid 1"); 135 Oid o = new Oid ("1.2.3"); 136 aed.Oid = o; 137 Assert.AreEqual ("1.2.3", aed.Oid.Value, "Oid 2"); 138 o.Value = "1.2.4"; 139 Assert.AreEqual ("1.2.3", aed.Oid.Value, "Oid 3"); // didn't change (copy) 140 aed.Oid = null; 141 Assert.IsNull (aed.Oid, "Oid 4"); 142 } 143 144 [Test] Oid()145 public void Oid () 146 { 147 AsnEncodedData aed = new AsnEncodedData ("1.2.3", asnNullBytes); 148 Assert.AreEqual ("1.2.3", aed.Oid.Value, "Oid 1"); 149 aed.Oid.Value = "1.2.4"; 150 Assert.AreEqual ("1.2.4", aed.Oid.Value, "Oid 2"); // didn't change (copy) 151 aed.Oid = null; 152 Assert.IsNull (aed.Oid, "Oid 3"); 153 } 154 155 [Test] RawData_CanModify()156 public void RawData_CanModify () 157 { 158 byte[] data = (byte[])asnNullBytes.Clone (); 159 AsnEncodedData aed = new AsnEncodedData ("1.2.3", data); 160 Assert.AreEqual (asnNullString, aed.Format (true), "Format 1"); 161 data[0] = 0x06; 162 Assert.AreEqual (asnNullString, aed.Format (true), "Format 2"); ; // didn't change (copy) 163 aed.RawData[0] = 0x07; 164 Assert.AreEqual ("07 00", aed.Format (true), "Format 3"); // changed! 165 } 166 167 [Test] 168 [ExpectedException (typeof (ArgumentNullException))] RawData()169 public void RawData () 170 { 171 AsnEncodedData aed = new AsnEncodedData ((Oid)null, asnNullBytes); 172 Assert.AreEqual (asnNullString, aed.Format (true), "Format 1"); 173 aed.RawData = null; 174 } 175 176 [Test] 177 [ExpectedException (typeof (ArgumentNullException))] CopyFrom_Null()178 public void CopyFrom_Null () 179 { 180 AsnEncodedData aed = new AsnEncodedData ((Oid)null, asnNullBytes); 181 aed.CopyFrom (null); 182 } 183 184 [Test] CopyFrom()185 public void CopyFrom () 186 { 187 Oid o = new Oid ("1.2.3"); 188 byte[] data = (byte[])asnNullBytes.Clone (); 189 AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes); 190 AsnEncodedData copy = new AsnEncodedData ((Oid)null, new byte [0]); 191 copy.CopyFrom (aed); 192 193 Assert.AreEqual (aed.Oid.Value, copy.Oid.Value, "Oid 1"); 194 Assert.AreEqual (aed.Format (true), copy.Format (true), "Format 1"); 195 196 aed.Oid = new Oid ("1.2.4"); 197 aed.RawData = new byte[1]; 198 199 Assert.AreEqual ("1.2.3", copy.Oid.Value, "Oid 2"); 200 Assert.AreEqual (asnNullString, copy.Format (true), "Format 2"); 201 } 202 203 [Test] Format()204 public void Format () 205 { 206 AsnEncodedData aed = new AsnEncodedData ("1.2.840.113549.1.1.1", asnLongBytes); 207 Assert.AreEqual ("1.2.840.113549.1.1.1", aed.Oid.Value, "Oid.Value"); 208 Assert.AreEqual ("RSA", aed.Oid.FriendlyName, "Oid.FriendlyName"); 209 Assert.AreEqual (BitConverter.ToString (asnLongBytes), BitConverter.ToString (aed.RawData), "RawData"); 210 string result = aed.Format (false); 211 Assert.AreEqual (asnLongString, result, "Format(false)"); 212 } 213 214 [Test] FormatMultiline()215 public void FormatMultiline () 216 { 217 AsnEncodedData aed = new AsnEncodedData ("1.2.840.113549.1.1.1", asnLongBytes); 218 Assert.AreEqual ("1.2.840.113549.1.1.1", aed.Oid.Value, "Oid.Value"); 219 Assert.AreEqual ("RSA", aed.Oid.FriendlyName, "Oid.FriendlyName"); 220 Assert.AreEqual (BitConverter.ToString (asnLongBytes), BitConverter.ToString (aed.RawData), "RawData"); 221 string result = aed.Format (true); 222 Assert.AreEqual (asnLongString, result, "Format(true)"); 223 } 224 225 [Test] 226 [Category ("NotDotNet")] // FriendlyName should not only be English. Build_X509EnhancedKeyUsageExtension()227 public void Build_X509EnhancedKeyUsageExtension () 228 { 229 AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 }); 230 Assert.AreEqual ("30 05 06 03 2a 03 04", aed.Format (true), "Format(true)"); 231 Assert.AreEqual ("30 05 06 03 2a 03 04", aed.Format (false), "Format(false)"); 232 aed.Oid = new Oid ("2.5.29.37"); 233 // and now "AsnEncodedData" knows how to (magically) decode the data without involving the class 234 // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows. 235 Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, aed.Format (true), "aed.Format(true)"); 236 Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", aed.Format (false), "aed.Format(false)"); 237 // compare with the output of the "appropriate" class 238 X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (aed, false); 239 // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows. 240 Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, eku.Format (true), "eku.Format(true)"); 241 Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", eku.Format (false), "eku.Format(false)"); 242 } 243 244 [Test] 245 [Category ("NotDotNet")] // FriendlyName should not only be English. 246 // note: important to emulate in Mono because we need it for SSL/TLS Build_NetscapeCertTypeExtension()247 public void Build_NetscapeCertTypeExtension () 248 { 249 AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x03, 0x02, 0x01, 0x06 }); 250 Assert.AreEqual ("03 02 01 06", aed.Format (true), "Format(true)"); 251 Assert.AreEqual ("03 02 01 06", aed.Format (false), "Format(false)"); 252 aed.Oid = new Oid ("2.16.840.1.113730.1.1"); 253 // and now "AsnEncodedData" knows how to (magically) decode the data without involving the class 254 Assert.AreEqual ("SSL CA, SMIME CA (06)", aed.Format (true), "aed.Format(true)"); 255 Assert.AreEqual ("SSL CA, SMIME CA (06)", aed.Format (false), "aed.Format(false)"); 256 // note that the Fx doesn't "really" support this extension 257 // and strangely no NewLine is being appended to Format(true) 258 // finally this also means that the Oid "knowns" about oid not used in the Fx itself 259 // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows. 260 Assert.AreEqual ("Netscape Cert Type", aed.Oid.FriendlyName, "FriendlyName"); 261 // anyway the answer is most probably CryptoAPI 262 } 263 264 [Test] 265 [Category ("NotDotNet")] // FriendlyName should not only be English. 266 // note: important to emulate in Mono because we need it for SSL/TLS Build_SubjectAltNameExtension()267 public void Build_SubjectAltNameExtension () 268 { 269 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 }); 270 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)"); 271 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)"); 272 aed.Oid = new Oid ("2.5.29.17"); 273 // and now "AsnEncodedData" knows how to (magically) decode the data without involving the class 274 Assert.AreEqual ("DNS Name=www.mono-project.com" + Environment.NewLine, aed.Format (true), "aed.Format(true)"); 275 Assert.AreEqual ("DNS Name=www.mono-project.com", aed.Format (false), "aed.Format(false)"); 276 // note that the Fx doesn't "really" support this extension 277 // finally this also means that the Oid "knowns" about oid not used in the Fx itself 278 // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows. 279 Assert.AreEqual ("Subject Alternative Name", aed.Oid.FriendlyName, "FriendlyName"); 280 // anyway the answer is most probably CryptoAPI 281 } 282 } 283 } 284 285