1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
//
// X509SubjectKeyIdentifierExtensionTest.cs
// - NUnit tests for X509SubjectKeyIdentifierExtension
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2005, 2006 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace MonoTests.System.Security.Cryptography.X509Certificates {
[TestFixture]
public class X509SubjectKeyIdentifierExtensionTest {
private const string oid = "2.5.29.14";
private const string fname = "Subject Key Identifier";
private PublicKey pk1;
[TestFixtureSetUp]
public void FixtureSetUp ()
{
pk1 = new X509Certificate2 (Encoding.ASCII.GetBytes (X509Certificate2Test.base64_cert)).PublicKey;
}
[Test]
public void ConstructorEmpty ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ();
Assert.IsFalse (ski.Critical, "Critical");
Assert.IsNull (ski.RawData, "RawData");
Assert.AreEqual (oid, ski.Oid.Value, "Oid.Value");
// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
//Assert.AreEqual (fname, ski.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual (String.Empty, ski.Format (true), "Format(true)");
Assert.AreEqual (String.Empty, ski.Format (false), "Format(false)");
}
[Test]
public void ConstructorEmpty_SubjectKeyIdentifier ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ();
Assert.IsNull (ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
}
[Test]
public void ConstructorAsnEncodedData ()
{
AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x04, 0x08, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF });
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (aed, true);
Assert.IsTrue (ski.Critical, "Critical");
Assert.AreEqual (oid, ski.Oid.Value, "Oid.Value");
// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
//Assert.AreEqual (fname, ski.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual ("04-08-01-23-45-67-89-AB-CD-EF", BitConverter.ToString (ski.RawData), "RawData");
Assert.AreEqual ("0123456789ABCDEF", ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("01 23 45 67 89 ab cd ef" + Environment.NewLine, ski.Format (true), "Format(true)");
Assert.AreEqual ("01 23 45 67 89 ab cd ef", ski.Format (false), "Format(false)");
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void ConstructorAsnEncodedData_BadAsn ()
{
AsnEncodedData aed = new AsnEncodedData ("1.2.3", new byte[0]);
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (aed, true);
Assert.AreEqual (String.Empty, ski.Format (true), "Format(true)");
Assert.AreEqual (String.Empty, ski.Format (false), "Format(false)");
string s = ski.SubjectKeyIdentifier;
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void ConstructorAsnEncodedData_BadAsnTag ()
{
AsnEncodedData aed = new AsnEncodedData ("1.2.3", new byte[] { 0x05, 0x00 });
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (aed, true);
Assert.AreEqual ("0500", ski.Format (true), "Format(true)");
Assert.AreEqual ("0500", ski.Format (false), "Format(false)");
string s = ski.SubjectKeyIdentifier;
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void ConstructorAsnEncodedData_BadAsnLength ()
{
AsnEncodedData aed = new AsnEncodedData ("1.2.3", new byte[] { 0x30, 0x01 });
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (aed, true);
Assert.AreEqual ("3001", ski.Format (true), "Format(true)");
Assert.AreEqual ("3001", ski.Format (false), "Format(false)");
string s = ski.SubjectKeyIdentifier;
}
[Test]
public void ConstructorAsnEncodedData_SmallestValid ()
{
AsnEncodedData aed = new AsnEncodedData ("1.2.3", new byte[] { 0x04, 0x00 });
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (aed, true);
Assert.AreEqual (String.Empty, ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("04-00", BitConverter.ToString (ski.RawData), "RawData");
// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
//Assert.AreEqual ("Information Not Available", ski.Format (true), "Format(true)");
//Assert.AreEqual ("Information Not Available", ski.Format (false), "Format(false)");
}
[Test]
[ExpectedException (typeof (NullReferenceException))]
public void ConstructorAsnEncodedData_Null ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ((AsnEncodedData)null, true);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorByteArray_Null ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ((byte[])null, true);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ConstructorByteArray_Empty ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (new byte[0], true);
}
[Test]
public void ConstructorByteArray_20 ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (new byte[20], true);
Assert.IsTrue (ski.Critical, "Critical");
Assert.AreEqual ("04-14-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00", BitConverter.ToString (ski.RawData), "RawData");
Assert.AreEqual (oid, ski.Oid.Value, "Oid.Value");
// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
//Assert.AreEqual (fname, ski.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual ("0000000000000000000000000000000000000000", ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" + Environment.NewLine, ski.Format (true), "Format(true)");
Assert.AreEqual ("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00", ski.Format (false), "Format(false)");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorString_Null ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ((String)null, true);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ConstructorString_Empty ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (String.Empty, true);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ConstructorString_Single ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ("f", false);
}
[Test]
public void ConstructorString ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ("ffFFfFFf", false);
Assert.IsFalse (ski.Critical, "Critical");
Assert.AreEqual ("04-04-FF-FF-FF-FF", BitConverter.ToString (ski.RawData), "RawData");
Assert.AreEqual (oid, ski.Oid.Value, "Oid.Value");
// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
//Assert.AreEqual (fname, ski.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual ("FFFFFFFF", ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("ff ff ff ff" + Environment.NewLine, ski.Format (true), "Format(true)");
Assert.AreEqual ("ff ff ff ff", ski.Format (false), "Format(false)");
}
[Test]
public void ConstructorString_NotHex ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ("Mono::", true);
Assert.IsTrue (ski.Critical, "Critical");
Assert.AreEqual ("04-03-FF-FF-FF", BitConverter.ToString (ski.RawData), "RawData");
Assert.AreEqual (oid, ski.Oid.Value, "Oid.Value");
// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
//Assert.AreEqual (fname, ski.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual ("FFFFFF", ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("ff ff ff" + Environment.NewLine, ski.Format (true), "Format(true)");
Assert.AreEqual ("ff ff ff", ski.Format (false), "Format(false)");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorPublicKey_Null ()
{
new X509SubjectKeyIdentifierExtension ((PublicKey)null, true);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorPublicKeyHash_Null ()
{
new X509SubjectKeyIdentifierExtension (null, X509SubjectKeyIdentifierHashAlgorithm.Sha1, true);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ConstructorPublicKeyHash_BadX509SubjectKeyIdentifierHashAlgorithm ()
{
new X509SubjectKeyIdentifierExtension (pk1, (X509SubjectKeyIdentifierHashAlgorithm)Int32.MinValue, true);
}
[Test]
public void ConstructorPublicKeyHash_Critical ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (pk1, true);
Assert.IsTrue (ski.Critical, "Critical");
Assert.AreEqual ("2.5.29.14", ski.Oid.Value, "Oid");
Assert.AreEqual ("4A0200E2E8D33DBA05FC37BDC36DCF47212D77D1", ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("04-14-4A-02-00-E2-E8-D3-3D-BA-05-FC-37-BD-C3-6D-CF-47-21-2D-77-D1", BitConverter.ToString (ski.RawData), "RawData");
}
[Test]
public void ConstructorPublicKeyHash_Sha1_Critical ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (pk1, X509SubjectKeyIdentifierHashAlgorithm.Sha1, true);
Assert.IsTrue (ski.Critical, "Critical");
Assert.AreEqual ("2.5.29.14", ski.Oid.Value, "Oid");
Assert.AreEqual ("4A0200E2E8D33DBA05FC37BDC36DCF47212D77D1", ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("04-14-4A-02-00-E2-E8-D3-3D-BA-05-FC-37-BD-C3-6D-CF-47-21-2D-77-D1", BitConverter.ToString (ski.RawData), "RawData");
}
[Test]
public void ConstructorPublicKeyHash_ShortSha1_Critical ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (pk1, X509SubjectKeyIdentifierHashAlgorithm.ShortSha1, true);
Assert.IsTrue (ski.Critical, "Critical");
Assert.AreEqual ("2.5.29.14", ski.Oid.Value, "Oid");
Assert.AreEqual ("436DCF47212D77D1", ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("04-08-43-6D-CF-47-21-2D-77-D1", BitConverter.ToString (ski.RawData), "RawData");
}
[Test]
public void ConstructorPublicKeyHash_CapiSha1_Critical ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (pk1, X509SubjectKeyIdentifierHashAlgorithm.CapiSha1, true);
Assert.IsTrue (ski.Critical, "Critical");
Assert.AreEqual ("2.5.29.14", ski.Oid.Value, "Oid");
Assert.AreEqual ("0E73CE0E2E059378FC782707EBF0B4E7AEA652E1", ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("04-14-0E-73-CE-0E-2E-05-93-78-FC-78-27-07-EB-F0-B4-E7-AE-A6-52-E1", BitConverter.ToString (ski.RawData), "RawData");
}
[Test]
public void ConstructorPublicKeyHash ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (pk1, false);
Assert.IsFalse (ski.Critical, "Critical");
Assert.AreEqual ("2.5.29.14", ski.Oid.Value, "Oid");
Assert.AreEqual ("4A0200E2E8D33DBA05FC37BDC36DCF47212D77D1", ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("04-14-4A-02-00-E2-E8-D3-3D-BA-05-FC-37-BD-C3-6D-CF-47-21-2D-77-D1", BitConverter.ToString (ski.RawData), "RawData");
}
[Test]
public void ConstructorPublicKeyHash_Sha1 ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (pk1, X509SubjectKeyIdentifierHashAlgorithm.Sha1, false);
Assert.IsFalse (ski.Critical, "Critical");
Assert.AreEqual ("2.5.29.14", ski.Oid.Value, "Oid");
Assert.AreEqual ("4A0200E2E8D33DBA05FC37BDC36DCF47212D77D1", ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("04-14-4A-02-00-E2-E8-D3-3D-BA-05-FC-37-BD-C3-6D-CF-47-21-2D-77-D1", BitConverter.ToString (ski.RawData), "RawData");
}
[Test]
public void ConstructorPublicKeyHash_ShortSha1 ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (pk1, X509SubjectKeyIdentifierHashAlgorithm.ShortSha1, false);
Assert.IsFalse (ski.Critical, "Critical");
Assert.AreEqual ("2.5.29.14", ski.Oid.Value, "Oid");
Assert.AreEqual ("436DCF47212D77D1", ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("04-08-43-6D-CF-47-21-2D-77-D1", BitConverter.ToString (ski.RawData), "RawData");
}
[Test]
public void ConstructorPublicKeyHash_CapiSha1 ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (pk1, X509SubjectKeyIdentifierHashAlgorithm.CapiSha1, false);
Assert.IsFalse (ski.Critical, "Critical");
Assert.AreEqual ("2.5.29.14", ski.Oid.Value, "Oid");
Assert.AreEqual ("0E73CE0E2E059378FC782707EBF0B4E7AEA652E1", ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("04-14-0E-73-CE-0E-2E-05-93-78-FC-78-27-07-EB-F0-B4-E7-AE-A6-52-E1", BitConverter.ToString (ski.RawData), "RawData");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void WrongExtension_X509KeyUsageExtension ()
{
X509KeyUsageExtension ku = new X509KeyUsageExtension ();
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ();
ski.CopyFrom (ku);
}
[Test]
public void WrongExtension_X509Extension ()
{
X509Extension ex = new X509Extension ("1.2.3", new byte[0], true);
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ("www.go-mono.com", false); // odd length
Assert.IsFalse (ski.Critical, "Critical");
Assert.AreEqual ("FFFFFFFFFFFFFF", ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("ff ff ff ff ff ff ff" + Environment.NewLine, ski.Format (true), "Format(true)");
Assert.AreEqual ("ff ff ff ff ff ff ff", ski.Format (false), "Format(false)");
ski.CopyFrom (ex);
Assert.IsTrue (ski.Critical, "Critical");
Assert.AreEqual (String.Empty, BitConverter.ToString (ski.RawData), "RawData");
Assert.AreEqual ("1.2.3", ski.Oid.Value, "Oid.Value");
// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
//Assert.IsNull (ski.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual (String.Empty, ski.Format (true), "Format(true)");
Assert.AreEqual (String.Empty, ski.Format (false), "Format(false)");
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void WrongExtension_X509Extension_CertificateAuthority ()
{
X509Extension ex = new X509Extension ("1.2.3", new byte[0], true);
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ();
ski.CopyFrom (ex);
string s = ski.SubjectKeyIdentifier;
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WrongAsnEncodedData ()
{
AsnEncodedData aed = new AsnEncodedData (new byte[0]);
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ("www.mono-project.com", false);
ski.CopyFrom (aed); // note: not the same behaviour than using the constructor!
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void CopyFrom_Null ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ();
ski.CopyFrom (null);
}
[Test]
public void CopyFrom_Self ()
{
X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension ("ff", true);
Assert.IsTrue (ski.Critical, "Critical");
byte[] raw = ski.RawData;
Assert.AreEqual ("04-01-FF", BitConverter.ToString (raw), "RawData");
AsnEncodedData aed = new AsnEncodedData (raw);
X509SubjectKeyIdentifierExtension copy = new X509SubjectKeyIdentifierExtension (aed, false);
Assert.IsFalse (copy.Critical, "Critical");
Assert.AreEqual ("04-01-FF", BitConverter.ToString (copy.RawData), "copy.RawData");
Assert.AreEqual (oid, copy.Oid.Value, "Oid.Value");
// FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
//Assert.AreEqual (fname, copy.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual ("FF", copy.SubjectKeyIdentifier, "SubjectKeyIdentifier");
}
#if !MOBILE
[Test]
public void CreateViaCryptoConfig ()
{
// extensions can be created with CryptoConfig
AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x04, 0x00 });
X509SubjectKeyIdentifierExtension ski = (X509SubjectKeyIdentifierExtension) CryptoConfig.CreateFromName (oid, new object[2] { aed, true });
Assert.AreEqual (String.Empty, ski.SubjectKeyIdentifier, "SubjectKeyIdentifier");
Assert.AreEqual ("04-00", BitConverter.ToString (ski.RawData), "RawData");
}
#endif
}
}
#endif
|