-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
X500DistinguishedNameTests.cs
515 lines (442 loc) · 19.2 KB
/
X500DistinguishedNameTests.cs
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.InteropServices;
using Test.Cryptography;
using Xunit;
namespace System.Security.Cryptography.X509Certificates.Tests
{
public static class X500DistinguishedNameTests
{
[Fact]
public static void PrintInvalidEncoding()
{
// One byte has been removed from the payload here. Since DER is length-prepended
// this will run out of data too soon, and report as invalid.
byte[] encoded = "3017311530130603550403130C436F6D6D6F6E204E616D65".HexToByteArray();
X500DistinguishedName dn = new X500DistinguishedName(encoded);
Assert.Equal("", dn.Decode(X500DistinguishedNameFlags.None));
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public static void PrintMultiComponentRdn(bool fromSpan)
{
byte[] encoded = (
"30223120300C060355040313054A616D65733010060355040A13094D6963726F" +
"736F6674").HexToByteArray();
const string expected = "CN=James + O=Microsoft";
X500DistinguishedName dn;
if (fromSpan)
{
dn = new X500DistinguishedName(new ReadOnlySpan<byte>(encoded));
}
else
{
dn = new X500DistinguishedName(encoded);
byte[] readBack = dn.RawData;
Assert.NotSame(readBack, encoded);
Assert.Equal(readBack, encoded);
}
Assert.Equal(expected, dn.Decode(X500DistinguishedNameFlags.None));
// It should not change ordering when reversed, since the two are one unit.
Assert.Equal(expected, dn.Decode(X500DistinguishedNameFlags.Reversed));
}
[Fact]
public static void PrintUnknownOidRdn()
{
byte[] encoded = (
"30183116301406052901020203130B496E76616C6964204F6964").HexToByteArray();
X500DistinguishedName dn = new X500DistinguishedName(encoded);
Assert.Equal("OID.1.1.1.2.2.3=Invalid Oid", dn.Decode(X500DistinguishedNameFlags.None));
}
[Theory]
[MemberData(nameof(WhitespaceBeforeCases))]
public static void QuoteWhitespaceBefore(string expected, string hexEncoded)
{
byte[] encoded = hexEncoded.HexToByteArray();
X500DistinguishedName dn = new X500DistinguishedName(encoded);
Assert.Equal(expected, dn.Decode(X500DistinguishedNameFlags.None));
}
[Theory]
[MemberData(nameof(WhitespaceBeforeCases))]
public static void NoQuoteWhitespaceBefore(string expectedQuoted, string hexEncoded)
{
string expected = expectedQuoted.Replace("\"", "");
byte[] encoded = hexEncoded.HexToByteArray();
X500DistinguishedName dn = new X500DistinguishedName(encoded);
Assert.Equal(expected, dn.Decode(X500DistinguishedNameFlags.DoNotUseQuotes));
}
[Theory]
[MemberData(nameof(WhitespaceAfterCases))]
public static void QuoteWhitespaceAfter(string expected, string hexEncoded)
{
byte[] encoded = hexEncoded.HexToByteArray();
X500DistinguishedName dn = new X500DistinguishedName(encoded);
Assert.Equal(expected, dn.Decode(X500DistinguishedNameFlags.None));
}
[Theory]
[MemberData(nameof(WhitespaceAfterCases))]
public static void NoQuoteWhitespaceAfter(string expectedQuoted, string hexEncoded)
{
string expected = expectedQuoted.Replace("\"", "");
byte[] encoded = hexEncoded.HexToByteArray();
X500DistinguishedName dn = new X500DistinguishedName(encoded);
Assert.Equal(expected, dn.Decode(X500DistinguishedNameFlags.DoNotUseQuotes));
}
[Theory]
[MemberData(nameof(QuotedContentsCases))]
public static void QuoteByContents(string expected, string hexEncoded)
{
byte[] encoded = hexEncoded.HexToByteArray();
X500DistinguishedName dn = new X500DistinguishedName(encoded);
Assert.Equal(expected, dn.Decode(X500DistinguishedNameFlags.None));
}
[Theory]
[MemberData(nameof(QuotedContentsCases))]
public static void NoQuoteByContents(string expectedQuoted, string hexEncoded)
{
string expected = expectedQuoted.Replace("\"", "");
byte[] encoded = hexEncoded.HexToByteArray();
X500DistinguishedName dn = new X500DistinguishedName(encoded);
Assert.Equal(expected, dn.Decode(X500DistinguishedNameFlags.DoNotUseQuotes));
}
[Theory]
[MemberData(nameof(InternallyQuotedRDNs))]
public static void QuotedWithQuotesAsAppropriate(string quoted, string notQuoted, string hexEncoded)
{
byte[] encoded = hexEncoded.HexToByteArray();
X500DistinguishedName dn = new X500DistinguishedName(encoded);
Assert.Equal(quoted, dn.Decode(X500DistinguishedNameFlags.None));
Assert.Equal(notQuoted, dn.Decode(X500DistinguishedNameFlags.DoNotUseQuotes));
}
[Theory]
[MemberData(nameof(T61Cases))]
public static void T61Strings(string expected, string hexEncoded)
{
byte[] encoded = hexEncoded.HexToByteArray();
X500DistinguishedName dn = new X500DistinguishedName(encoded);
Assert.Equal(expected, dn.Name);
}
[Fact]
public static void PrintComplexReversed()
{
byte[] encoded = MicrosoftDotComSubject.HexToByteArray();
X500DistinguishedName dn = new X500DistinguishedName(encoded);
const string expected =
"CN=www.microsoft.com, OU=MSCOM, O=Microsoft Corporation, STREET=1 Microsoft Way, " +
"L=Redmond, S=Washington, PostalCode=98052, C=US, SERIALNUMBER=600413485, ";
// Windows 8.1 would continue the string with some unknown OIDs, but OpenSSL 1.0.1 can decode
// at least businessCategory (2.5.4.15), and other Windows versions may do so in the future.
// "OID.2.5.4.15=Private Organization, OID.1.3.6.1.4.1.311.60.2.1.2=Washington, " +
// "OID.1.3.6.1.4.1.311.60.2.1.3=US";
Assert.StartsWith(expected, dn.Decode(X500DistinguishedNameFlags.Reversed), StringComparison.Ordinal);
}
[Fact]
public static void PrintComplexForwards()
{
byte[] encoded = MicrosoftDotComSubject.HexToByteArray();
X500DistinguishedName dn = new X500DistinguishedName(encoded);
const string expected =
", SERIALNUMBER=600413485, C=US, PostalCode=98052, S=Washington, L=Redmond, " +
"STREET=1 Microsoft Way, O=Microsoft Corporation, OU=MSCOM, CN=www.microsoft.com";
Assert.EndsWith(expected, dn.Decode(X500DistinguishedNameFlags.None), StringComparison.Ordinal);
}
[Fact]
public static void EdgeCaseEmptyFormat()
{
X500DistinguishedName dn = new X500DistinguishedName("");
Assert.Equal(string.Empty, dn.Format(true));
Assert.Equal(string.Empty, dn.Format(false));
}
[Fact]
public static void EdgeCaseUseCommaAndNewLines()
{
const string rname = "C=US, O=\"RSA Data Security, Inc.\", OU=Secure Server Certification Authority";
X500DistinguishedName dn = new X500DistinguishedName(rname, X500DistinguishedNameFlags.None);
Assert.Equal(rname, dn.Decode(X500DistinguishedNameFlags.UseCommas | X500DistinguishedNameFlags.UseNewLines));
}
[Fact]
public static void TpmIdentifiers()
{
// On Windows the X.500 name pretty printer is in crypt32, so it doesn't use our OidLookup.
// Windows 7 doesn't have the TPM OIDs mapped, so they come back as (e.g.) OID.2.23.133.2.3 still.
//
// Just skip this test there.
if (PlatformDetection.IsWindows7)
{
return;
}
X500DistinguishedName dn = new X500DistinguishedName("OID.2.23.133.2.3=id:0020065,OID.2.23.133.2.2=,OID.2.23.133.2.1=id:564D5700");
X500DistinguishedName dn2 = new X500DistinguishedName(dn.RawData);
Assert.Equal("TPMManufacturer=id:564D5700, TPMModel=\"\", TPMVersion=id:0020065", dn2.Decode(X500DistinguishedNameFlags.None));
}
[Fact]
public static void NameWithNumericString()
{
X500DistinguishedName dn = new X500DistinguishedName(
"30283117301506052901020203120C313233203635342037383930310D300B0603550403130454657374".HexToByteArray());
Assert.Equal("OID.1.1.1.2.2.3=123 654 7890, CN=Test", dn.Decode(X500DistinguishedNameFlags.None));
}
[Fact]
public static void OrganizationUnitMultiValueWithIncorrectlySortedDerSet()
{
X500DistinguishedName dn = new X500DistinguishedName(
"301C311A300B060355040B13047A7A7A7A300B060355040B130461616161".HexToByteArray());
Assert.Equal("OU=zzzz + OU=aaaa", dn.Decode(X500DistinguishedNameFlags.None));
}
[Fact]
public static void NameWithSTIdentifierForState()
{
X500DistinguishedName dn = new X500DistinguishedName("ST=VA, C=US");
Assert.Equal("C=US, S=VA", dn.Decode(X500DistinguishedNameFlags.None));
}
public static readonly object[][] WhitespaceBeforeCases =
{
// Regular space.
new object[]
{
"CN=\" Common Name\"",
"3017311530130603550403130C20436F6D6D6F6E204E616D65"
},
// Tab
new object[]
{
"CN=\"\tCommon Name\"",
"30233121301F06035504031E1800090043006F006D006D006F006E0020004E00" +
"61006D0065"
},
// Newline
new object[]
{
"CN=\"\nCommon Name\"",
"30233121301F06035504031E18000A0043006F006D006D006F006E0020004E00" +
"61006D0065"
},
// xUnit doesn't like \v in Assert.Equals, reports it as an invalid character.
//new object[]
//{
// "CN=\"\vCommon Name\"",
// "30233121301F06035504031E18000B0043006F006D006D006F006E0020004E00" +
// "61006D0065"
//},
// xUnit doesn't like FormFeed in Assert.Equals, reports it as an invalid character.
//new object[]
//{
// "CN=\"\u000cCommon Name\"",
// "30233121301F06035504031E18000C0043006F006D006D006F006E0020004E00" +
// "61006D0065"
//},
// Carriage return
new object[]
{
"CN=\"\rCommon Name\"",
"30233121301F06035504031E18000D0043006F006D006D006F006E0020004E00" +
"61006D0065"
},
// em quad. This is char.IsWhitespace, but is not quoted.
new object[]
{
"CN=\u2002Common Name",
"30233121301F06035504031E1820020043006F006D006D006F006E0020004E00" +
"61006D0065"
},
};
public static readonly object[][] WhitespaceAfterCases =
{
// Regular space.
new object[]
{
"CN=\"Common Name \"",
"3017311530130603550403130C436F6D6D6F6E204E616D6520"
},
// Newline
new object[]
{
"CN=\"Common Name\t\"",
"30233121301F06035504031E180043006F006D006D006F006E0020004E006100" +
"6D00650009"
},
// Newline
new object[]
{
"CN=\"Common Name\n\"",
"30233121301F06035504031E180043006F006D006D006F006E0020004E006100" +
"6D0065000A"
},
// xUnit doesn't like \v in Assert.Equals, reports it as an invalid character.
//new object[]
//{
// "CN=\"Common Name\v\"",
// "30233121301F06035504031E180043006F006D006D006F006E0020004E006100" +
// "6D0065000B"
//},
// xUnit doesn't like FormFeed in Assert.Equals, reports it as an invalid character.
//new object[]
//{
// "CN=\"Common Name\u000c\"",
// "30233121301F06035504031E180043006F006D006D006F006E0020004E006100" +
// "6D0065000C"
//},
// Carriage return
new object[]
{
"CN=\"Common Name\r\"",
"30233121301F06035504031E180043006F006D006D006F006E0020004E006100" +
"6D0065000D"
},
// em quad. This is char.IsWhitespace, but is not quoted.
new object[]
{
"CN=Common Name\u2002",
"30233121301F06035504031E180043006F006D006D006F006E0020004E006100" +
"6D00652002"
},
};
public static readonly object[][] QuotedContentsCases =
{
// Empty value
new object[]
{
"CN=\"\"",
"300B3109300706035504031300"
},
// Comma (RDN separator)
new object[]
{
"CN=\"Common,Name\"",
"3016311430120603550403130B436F6D6D6F6E2C4E616D65"
},
// Plus (RDN component separator)
new object[]
{
"CN=\"Common+Name\"",
"3016311430120603550403130B436F6D6D6F6E2B4E616D65"
},
// Equal (Key/Value separator)
new object[]
{
"CN=\"Common=Name\"",
"3016311430120603550403130B436F6D6D6F6E3D4E616D65"
},
// Note: Double Quote has been removed from this set, it's a dedicated test suite.
// Newline
new object[]
{
"CN=\"Common\nName\"",
"3021311F301D06035504031E160043006F006D006D006F006E000A004E006100" +
"6D0065"
},
// Carriage return is NOT quoted.
new object[]
{
"CN=Common\rName",
"3021311F301D06035504031E160043006F006D006D006F006E000D004E006100" +
"6D0065"
},
// Less-than
new object[]
{
"CN=\"Common<Name\"",
"3021311F301D06035504031E160043006F006D006D006F006E003C004E006100" +
"6D0065"
},
// Greater-than
new object[]
{
"CN=\"Common>Name\"",
"3021311F301D06035504031E160043006F006D006D006F006E003E004E006100" +
"6D0065"
},
// Octothorpe (Number Sign, Pound, Hash, whatever)
new object[]
{
"CN=\"Common#Name\"",
"3021311F301D06035504031E160043006F006D006D006F006E0023004E006100" +
"6D0065"
},
// Semi-colon
new object[]
{
"CN=\"Common;Name\"",
"3021311F301D06035504031E160043006F006D006D006F006E003B004E006100" +
"6D0065"
},
};
public static readonly object[][] InternallyQuotedRDNs =
{
// Interior Double Quote
new object[]
{
"CN=\"Common\"\"Name\"", // Quoted
"CN=Common\"Name", // Not-Quoted
"3021311F301D06035504031E160043006F006D006D006F006E0022004E006100" +
"6D0065"
},
// Starts with a double quote
new object[]
{
"CN=\"\"\"Common Name\"", // Quoted
"CN=\"Common Name", // Not-Quoted
"30233121301F06035504031E1800220043006F006D006D006F006E0020004E00" +
"61006D0065"
},
// Ends with a double quote
new object[]
{
"CN=\"Common Name\"\"\"", // Quoted
"CN=Common Name\"", // Not-Quoted
"30233121301F06035504031E180043006F006D006D006F006E0020004E006100" +
"6D00650022"
},
};
public static readonly object[][] T61Cases =
{
// https://github.com/dotnet/runtime/issues/25195
new object[]
{
"CN=GrapeCity inc., OU=Tools Development, O=GrapeCity inc., " +
"L=Sendai Izumi-ku, S=Miyagi, C=JP",
"308186310b3009060355040613024a50310f300d060355040813064d69796167" +
"69311830160603550407130f53656e64616920497a756d692d6b753117301506" +
"0355040a140e47726170654369747920696e632e311a3018060355040b141154" +
"6f6f6c7320446576656c6f706d656e74311730150603550403140e4772617065" +
"4369747920696e632e"
},
// Mono test case taken from old bug report
new object[]
{
"SERIALNUMBER=CVR:13471967-UID:121212121212, E=vhm@use.test.dk, " +
"CN=Hedeby's M\u00f8belhandel - Salgsafdelingen, " +
"O=Hedeby's M\u00f8belhandel // CVR:13471967, C=DK",
"3081B5310B300906035504061302444B312D302B060355040A14244865646562" +
"792773204DF862656C68616E64656C202F2F204356523A313334373139363731" +
"2F302D060355040314264865646562792773204DF862656C68616E64656C202D" +
"2053616C6773616664656C696E67656E311E301C06092A864886F70D01090116" +
"0F76686D407573652E746573742E646B312630240603550405131D4356523A31" +
"333437313936372D5549443A313231323132313231323132"
},
// Valid UTF-8 string is interpreted as UTF-8
new object[]
{
"C=\u00a2",
"300D310B300906035504061402C2A2"
},
// Invalid UTF-8 string with valid UTF-8 sequence is interpreted as ISO 8859-1
new object[]
{
"L=\u00c2\u00a2\u00f8",
"300E310C300A06035504071403C2A2F8"
},
};
private const string MicrosoftDotComSubject =
"3082010F31133011060B2B0601040182373C02010313025553311B3019060B2B" +
"0601040182373C0201020C0A57617368696E67746F6E311D301B060355040F13" +
"1450726976617465204F7267616E697A6174696F6E3112301006035504051309" +
"363030343133343835310B3009060355040613025553310E300C06035504110C" +
"0539383035323113301106035504080C0A57617368696E67746F6E3110300E06" +
"035504070C075265646D6F6E643118301606035504090C0F31204D6963726F73" +
"6F667420576179311E301C060355040A0C154D6963726F736F667420436F7270" +
"6F726174696F6E310E300C060355040B0C054D53434F4D311A30180603550403" +
"0C117777772E6D6963726F736F66742E636F6D";
}
}