/
pkixnames_tests.cpp
2838 lines (2553 loc) · 107 KB
/
pkixnames_tests.cpp
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This code is made available to you under your choice of the following sets
* of licensing terms:
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* Copyright 2014 Mozilla Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "pkixgtest.h"
#include "mozpkix/pkixcheck.h"
#include "mozpkix/pkixder.h"
#include "mozpkix/pkixutil.h"
namespace mozilla { namespace pkix {
Result MatchPresentedDNSIDWithReferenceDNSID(Input presentedDNSID,
Input referenceDNSID,
/*out*/ bool& matches);
bool IsValidReferenceDNSID(Input hostname);
bool IsValidPresentedDNSID(Input hostname);
bool ParseIPv4Address(Input hostname, /*out*/ uint8_t (&out)[4]);
bool ParseIPv6Address(Input hostname, /*out*/ uint8_t (&out)[16]);
} } // namespace mozilla::pkix
using namespace mozilla::pkix;
using namespace mozilla::pkix::test;
struct PresentedMatchesReference
{
ByteString presentedDNSID;
ByteString referenceDNSID;
Result expectedResult;
bool expectedMatches; // only valid when expectedResult == Success
};
::std::ostream& operator<<(::std::ostream& os, const PresentedMatchesReference&)
{
return os << "TODO (bug 1318770)";
}
#define DNS_ID_MATCH(a, b) \
{ \
ByteString(reinterpret_cast<const uint8_t*>(a), sizeof(a) - 1), \
ByteString(reinterpret_cast<const uint8_t*>(b), sizeof(b) - 1), \
Success, \
true \
}
#define DNS_ID_MISMATCH(a, b) \
{ \
ByteString(reinterpret_cast<const uint8_t*>(a), sizeof(a) - 1), \
ByteString(reinterpret_cast<const uint8_t*>(b), sizeof(b) - 1), \
Success, \
false \
}
#define DNS_ID_BAD_DER(a, b) \
{ \
ByteString(reinterpret_cast<const uint8_t*>(a), sizeof(a) - 1), \
ByteString(reinterpret_cast<const uint8_t*>(b), sizeof(b) - 1), \
Result::ERROR_BAD_DER, \
false \
}
static const PresentedMatchesReference DNSID_MATCH_PARAMS[] =
{
DNS_ID_BAD_DER("", "a"),
DNS_ID_MATCH("a", "a"),
DNS_ID_MISMATCH("b", "a"),
DNS_ID_MATCH("*.b.a", "c.b.a"),
DNS_ID_MISMATCH("*.b.a", "b.a"),
DNS_ID_MISMATCH("*.b.a", "b.a."),
// We allow underscores for compatibility with existing practices.
DNS_ID_MATCH("a_b", "a_b"),
DNS_ID_MATCH("*.example.com", "uses_underscore.example.com"),
DNS_ID_MATCH("*.uses_underscore.example.com", "a.uses_underscore.example.com"),
// See bug 1139039
DNS_ID_MATCH("_.example.com", "_.example.com"),
DNS_ID_MATCH("*.example.com", "_.example.com"),
DNS_ID_MATCH("_", "_"),
DNS_ID_MATCH("___", "___"),
DNS_ID_MATCH("example_", "example_"),
DNS_ID_MATCH("_example", "_example"),
DNS_ID_MATCH("*._._", "x._._"),
// See bug 1139039
// A DNS-ID must not end in an all-numeric label. We don't consider
// underscores to be numeric.
DNS_ID_MATCH("_1", "_1"),
DNS_ID_MATCH("example._1", "example._1"),
DNS_ID_MATCH("example.1_", "example.1_"),
// Wildcard not in leftmost label
DNS_ID_MATCH("d.c.b.a", "d.c.b.a"),
DNS_ID_BAD_DER("d.*.b.a", "d.c.b.a"),
DNS_ID_BAD_DER("d.c*.b.a", "d.c.b.a"),
DNS_ID_BAD_DER("d.c*.b.a", "d.cc.b.a"),
// case sensitivity
DNS_ID_MATCH("abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
DNS_ID_MATCH("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"),
DNS_ID_MATCH("aBc", "Abc"),
// digits
DNS_ID_MATCH("a1", "a1"),
// A trailing dot indicates an absolute name. Absolute presented names are
// not allowed, but absolute reference names are allowed.
DNS_ID_MATCH("example", "example"),
DNS_ID_BAD_DER("example.", "example."),
DNS_ID_MATCH("example", "example."),
DNS_ID_BAD_DER("example.", "example"),
DNS_ID_MATCH("example.com", "example.com"),
DNS_ID_BAD_DER("example.com.", "example.com."),
DNS_ID_MATCH("example.com", "example.com."),
DNS_ID_BAD_DER("example.com.", "example.com"),
DNS_ID_BAD_DER("example.com..", "example.com."),
DNS_ID_BAD_DER("example.com..", "example.com"),
DNS_ID_BAD_DER("example.com...", "example.com."),
// xn-- IDN prefix
DNS_ID_BAD_DER("x*.b.a", "xa.b.a"),
DNS_ID_BAD_DER("x*.b.a", "xna.b.a"),
DNS_ID_BAD_DER("x*.b.a", "xn-a.b.a"),
DNS_ID_BAD_DER("x*.b.a", "xn--a.b.a"),
DNS_ID_BAD_DER("xn*.b.a", "xn--a.b.a"),
DNS_ID_BAD_DER("xn-*.b.a", "xn--a.b.a"),
DNS_ID_BAD_DER("xn--*.b.a", "xn--a.b.a"),
DNS_ID_BAD_DER("xn*.b.a", "xn--a.b.a"),
DNS_ID_BAD_DER("xn-*.b.a", "xn--a.b.a"),
DNS_ID_BAD_DER("xn--*.b.a", "xn--a.b.a"),
DNS_ID_BAD_DER("xn---*.b.a", "xn--a.b.a"),
// "*" cannot expand to nothing.
DNS_ID_BAD_DER("c*.b.a", "c.b.a"),
/////////////////////////////////////////////////////////////////////////////
// These are test cases adapted from Chromium's x509_certificate_unittest.cc.
// The parameter order is the opposite in Chromium's tests. Also, some tests
// were modified to fit into this framework or due to intentional differences
// between mozilla::pkix and Chromium.
DNS_ID_MATCH("foo.com", "foo.com"),
DNS_ID_MATCH("f", "f"),
DNS_ID_MISMATCH("i", "h"),
DNS_ID_MATCH("*.foo.com", "bar.foo.com"),
DNS_ID_MATCH("*.test.fr", "www.test.fr"),
DNS_ID_MATCH("*.test.FR", "wwW.tESt.fr"),
DNS_ID_BAD_DER(".uk", "f.uk"),
DNS_ID_BAD_DER("?.bar.foo.com", "w.bar.foo.com"),
DNS_ID_BAD_DER("(www|ftp).foo.com", "www.foo.com"), // regex!
DNS_ID_BAD_DER("www.foo.com\0", "www.foo.com"),
DNS_ID_BAD_DER("www.foo.com\0*.foo.com", "www.foo.com"),
DNS_ID_MISMATCH("ww.house.example", "www.house.example"),
DNS_ID_MISMATCH("www.test.org", "test.org"),
DNS_ID_MISMATCH("*.test.org", "test.org"),
DNS_ID_BAD_DER("*.org", "test.org"),
DNS_ID_BAD_DER("w*.bar.foo.com", "w.bar.foo.com"),
DNS_ID_BAD_DER("ww*ww.bar.foo.com", "www.bar.foo.com"),
DNS_ID_BAD_DER("ww*ww.bar.foo.com", "wwww.bar.foo.com"),
// Different than Chromium, matches NSS.
DNS_ID_BAD_DER("w*w.bar.foo.com", "wwww.bar.foo.com"),
DNS_ID_BAD_DER("w*w.bar.foo.c0m", "wwww.bar.foo.com"),
// '*' must be the only character in the wildcard label
DNS_ID_BAD_DER("wa*.bar.foo.com", "WALLY.bar.foo.com"),
// We require "*" to be the last character in a wildcard label, but
// Chromium does not.
DNS_ID_BAD_DER("*Ly.bar.foo.com", "wally.bar.foo.com"),
// Chromium does URL decoding of the reference ID, but we don't, and we also
// require that the reference ID is valid, so we can't test these two.
// DNS_ID_MATCH("www.foo.com", "ww%57.foo.com"),
// DNS_ID_MATCH("www&.foo.com", "www%26.foo.com"),
DNS_ID_MISMATCH("*.test.de", "www.test.co.jp"),
DNS_ID_BAD_DER("*.jp", "www.test.co.jp"),
DNS_ID_MISMATCH("www.test.co.uk", "www.test.co.jp"),
DNS_ID_BAD_DER("www.*.co.jp", "www.test.co.jp"),
DNS_ID_MATCH("www.bar.foo.com", "www.bar.foo.com"),
DNS_ID_MISMATCH("*.foo.com", "www.bar.foo.com"),
DNS_ID_BAD_DER("*.*.foo.com", "www.bar.foo.com"),
DNS_ID_BAD_DER("*.*.foo.com", "www.bar.foo.com"),
// Our matcher requires the reference ID to be a valid DNS name, so we cannot
// test this case.
//DNS_ID_BAD_DER("*.*.bar.foo.com", "*..bar.foo.com"),
DNS_ID_MATCH("www.bath.org", "www.bath.org"),
// Our matcher requires the reference ID to be a valid DNS name, so we cannot
// test these cases.
// DNS_ID_BAD_DER("www.bath.org", ""),
// DNS_ID_BAD_DER("www.bath.org", "20.30.40.50"),
// DNS_ID_BAD_DER("www.bath.org", "66.77.88.99"),
// IDN tests
DNS_ID_MATCH("xn--poema-9qae5a.com.br", "xn--poema-9qae5a.com.br"),
DNS_ID_MATCH("*.xn--poema-9qae5a.com.br", "www.xn--poema-9qae5a.com.br"),
DNS_ID_MISMATCH("*.xn--poema-9qae5a.com.br", "xn--poema-9qae5a.com.br"),
DNS_ID_BAD_DER("xn--poema-*.com.br", "xn--poema-9qae5a.com.br"),
DNS_ID_BAD_DER("xn--*-9qae5a.com.br", "xn--poema-9qae5a.com.br"),
DNS_ID_BAD_DER("*--poema-9qae5a.com.br", "xn--poema-9qae5a.com.br"),
// The following are adapted from the examples quoted from
// http://tools.ietf.org/html/rfc6125#section-6.4.3
// (e.g., *.example.com would match foo.example.com but
// not bar.foo.example.com or example.com).
DNS_ID_MATCH("*.example.com", "foo.example.com"),
DNS_ID_MISMATCH("*.example.com", "bar.foo.example.com"),
DNS_ID_MISMATCH("*.example.com", "example.com"),
// (e.g., baz*.example.net and *baz.example.net and b*z.example.net would
// be taken to match baz1.example.net and foobaz.example.net and
// buzz.example.net, respectively. However, we don't allow any characters
// other than '*' in the wildcard label.
DNS_ID_BAD_DER("baz*.example.net", "baz1.example.net"),
// Both of these are different from Chromium, but match NSS, becaues the
// wildcard character "*" is not the last character of the label.
DNS_ID_BAD_DER("*baz.example.net", "foobaz.example.net"),
DNS_ID_BAD_DER("b*z.example.net", "buzz.example.net"),
// Wildcards should not be valid for public registry controlled domains,
// and unknown/unrecognized domains, at least three domain components must
// be present. For mozilla::pkix and NSS, there must always be at least two
// labels after the wildcard label.
DNS_ID_MATCH("*.test.example", "www.test.example"),
DNS_ID_MATCH("*.example.co.uk", "test.example.co.uk"),
DNS_ID_BAD_DER("*.exmaple", "test.example"),
// The result is different than Chromium, because Chromium takes into account
// the additional knowledge it has that "co.uk" is a TLD. mozilla::pkix does
// not know that.
DNS_ID_MATCH("*.co.uk", "example.co.uk"),
DNS_ID_BAD_DER("*.com", "foo.com"),
DNS_ID_BAD_DER("*.us", "foo.us"),
DNS_ID_BAD_DER("*", "foo"),
// IDN variants of wildcards and registry controlled domains.
DNS_ID_MATCH("*.xn--poema-9qae5a.com.br", "www.xn--poema-9qae5a.com.br"),
DNS_ID_MATCH("*.example.xn--mgbaam7a8h", "test.example.xn--mgbaam7a8h"),
// RFC6126 allows this, and NSS accepts it, but Chromium disallows it.
// TODO: File bug against Chromium.
DNS_ID_MATCH("*.com.br", "xn--poema-9qae5a.com.br"),
DNS_ID_BAD_DER("*.xn--mgbaam7a8h", "example.xn--mgbaam7a8h"),
// Wildcards should be permissible for 'private' registry-controlled
// domains. (In mozilla::pkix, we do not know if it is a private registry-
// controlled domain or not.)
DNS_ID_MATCH("*.appspot.com", "www.appspot.com"),
DNS_ID_MATCH("*.s3.amazonaws.com", "foo.s3.amazonaws.com"),
// Multiple wildcards are not valid.
DNS_ID_BAD_DER("*.*.com", "foo.example.com"),
DNS_ID_BAD_DER("*.bar.*.com", "foo.bar.example.com"),
// Absolute vs relative DNS name tests. Although not explicitly specified
// in RFC 6125, absolute reference names (those ending in a .) should
// match either absolute or relative presented names. We don't allow
// absolute presented names.
// TODO: File errata against RFC 6125 about this.
DNS_ID_BAD_DER("foo.com.", "foo.com"),
DNS_ID_MATCH("foo.com", "foo.com."),
DNS_ID_BAD_DER("foo.com.", "foo.com."),
DNS_ID_BAD_DER("f.", "f"),
DNS_ID_MATCH("f", "f."),
DNS_ID_BAD_DER("f.", "f."),
DNS_ID_BAD_DER("*.bar.foo.com.", "www-3.bar.foo.com"),
DNS_ID_MATCH("*.bar.foo.com", "www-3.bar.foo.com."),
DNS_ID_BAD_DER("*.bar.foo.com.", "www-3.bar.foo.com."),
// We require the reference ID to be a valid DNS name, so we cannot test this
// case.
// DNS_ID_MISMATCH(".", "."),
DNS_ID_BAD_DER("*.com.", "example.com"),
DNS_ID_BAD_DER("*.com", "example.com."),
DNS_ID_BAD_DER("*.com.", "example.com."),
DNS_ID_BAD_DER("*.", "foo."),
DNS_ID_BAD_DER("*.", "foo"),
// The result is different than Chromium because we don't know that co.uk is
// a TLD.
DNS_ID_MATCH("*.co.uk", "foo.co.uk"),
DNS_ID_MATCH("*.co.uk", "foo.co.uk."),
DNS_ID_BAD_DER("*.co.uk.", "foo.co.uk"),
DNS_ID_BAD_DER("*.co.uk.", "foo.co.uk."),
DNS_ID_MISMATCH("*.example.com", "localhost"),
DNS_ID_MISMATCH("*.example.com", "localhost."),
// Note that we already have the testcase DNS_ID_BAD_DER("*", "foo") above
};
struct InputValidity
{
ByteString input;
bool isValidReferenceID;
bool isValidPresentedID;
};
::std::ostream& operator<<(::std::ostream& os, const InputValidity&)
{
return os << "TODO (bug 1318770)";
}
// str is null-terminated, which is why we subtract 1. str may contain embedded
// nulls (including at the end) preceding the null terminator though.
#define I(str, validReferenceID, validPresentedID) \
{ \
ByteString(reinterpret_cast<const uint8_t*>(str), sizeof(str) - 1), \
validReferenceID, \
validPresentedID, \
}
static const InputValidity DNSNAMES_VALIDITY[] =
{
I("a", true, true),
I("a.b", true, true),
I("a.b.c", true, true),
I("a.b.c.d", true, true),
// empty labels
I("", false, false),
I(".", false, false),
I("a", true, true),
I(".a", false, false),
I(".a.b", false, false),
I("..a", false, false),
I("a..b", false, false),
I("a...b", false, false),
I("a..b.c", false, false),
I("a.b..c", false, false),
I(".a.b.c.", false, false),
// absolute names (only allowed for reference names)
I("a.", true, false),
I("a.b.", true, false),
I("a.b.c.", true, false),
// absolute names with empty label at end
I("a..", false, false),
I("a.b..", false, false),
I("a.b.c..", false, false),
I("a...", false, false),
// Punycode
I("xn--", false, false),
I("xn--.", false, false),
I("xn--.a", false, false),
I("a.xn--", false, false),
I("a.xn--.", false, false),
I("a.xn--.b", false, false),
I("a.xn--.b", false, false),
I("a.xn--\0.b", false, false),
I("a.xn--a.b", true, true),
I("xn--a", true, true),
I("a.xn--a", true, true),
I("a.xn--a.a", true, true),
I("\xc4\x95.com", false, false), // UTF-8 ĕ
I("xn--jea.com", true, true), // punycode ĕ
I("xn--\xc4\x95.com", false, false), // UTF-8 ĕ, malformed punycode + UTF-8 mashup
// Surprising punycode
I("xn--google.com", true, true), // 䕮䕵䕶䕱.com
I("xn--citibank.com", true, true), // 岍岊岊岅岉岎.com
I("xn--cnn.com", true, true), // 䁾.com
I("a.xn--cnn", true, true), // a.䁾
I("a.xn--cnn.com", true, true), // a.䁾.com
I("1.2.3.4", false, false), // IPv4 address
I("1::2", false, false), // IPV6 address
// whitespace not allowed anywhere.
I(" ", false, false),
I(" a", false, false),
I("a ", false, false),
I("a b", false, false),
I("a.b 1", false, false),
I("a\t", false, false),
// Nulls not allowed
I("\0", false, false),
I("a\0", false, false),
I("example.org\0.example.com", false, false), // Hi Moxie!
I("\0a", false, false),
I("xn--\0", false, false),
// Allowed character set
I("a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z", true, true),
I("A.B.C.D.E.F.G.H.I.J.K.L.M.N.O.P.Q.R.S.T.U.V.W.X.Y.Z", true, true),
I("0.1.2.3.4.5.6.7.8.9.a", true, true), // "a" needed to avoid numeric last label
I("a-b", true, true), // hyphen (a label cannot start or end with a hyphen)
// Underscores
I("a_b", true, true),
// See bug 1139039
I("_", true, true),
I("a_", true, true),
I("_a", true, true),
I("_1", true, true),
I("1_", true, true),
I("___", true, true),
// An invalid character in various positions
I("!", false, false),
I("!a", false, false),
I("a!", false, false),
I("a!b", false, false),
I("a.!", false, false),
I("a.a!", false, false),
I("a.!a", false, false),
I("a.a!a", false, false),
I("a.!a.a", false, false),
I("a.a!.a", false, false),
I("a.a!a.a", false, false),
// Various other invalid characters
I("a!", false, false),
I("a@", false, false),
I("a#", false, false),
I("a$", false, false),
I("a%", false, false),
I("a^", false, false),
I("a&", false, false),
I("a*", false, false),
I("a(", false, false),
I("a)", false, false),
// last label can't be fully numeric
I("1", false, false),
I("a.1", false, false),
// other labels can be fully numeric
I("1.a", true, true),
I("1.2.a", true, true),
I("1.2.3.a", true, true),
// last label can be *partly* numeric
I("1a", true, true),
I("1.1a", true, true),
I("1-1", true, true),
I("a.1-1", true, true),
I("a.1-a", true, true),
// labels cannot start with a hyphen
I("-", false, false),
I("-1", false, false),
// labels cannot end with a hyphen
I("1-", false, false),
I("1-.a", false, false),
I("a-", false, false),
I("a-.a", false, false),
I("a.1-.a", false, false),
I("a.a-.a", false, false),
// labels can contain a hyphen in the middle
I("a-b", true, true),
I("1-2", true, true),
I("a.a-1", true, true),
// multiple consecutive hyphens allowed
I("a--1", true, true),
I("1---a", true, true),
I("a-----------------b", true, true),
// Wildcard specifications are not valid reference names, but are valid
// presented names if there are enough labels and if '*' is the only
// character in the wildcard label.
I("*.a", false, false),
I("a*", false, false),
I("a*.", false, false),
I("a*.a", false, false),
I("a*.a.", false, false),
I("*.a.b", false, true),
I("*.a.b.", false, false),
I("a*.b.c", false, false),
I("*.a.b.c", false, true),
I("a*.b.c.d", false, false),
// Multiple wildcards are not allowed.
I("a**.b.c", false, false),
I("a*b*.c.d", false, false),
I("a*.b*.c", false, false),
// Wildcards are only allowed in the first label.
I("a.*", false, false),
I("a.*.b", false, false),
I("a.b.*", false, false),
I("a.b*.c", false, false),
I("*.b*.c", false, false),
I(".*.a.b", false, false),
I(".a*.b.c", false, false),
// Wildcards must be at the *end* of the first label.
I("*a.b.c", false, false),
I("a*b.c.d", false, false),
// Wildcards not allowed with IDNA prefix
I("x*.a.b", false, false),
I("xn*.a.b", false, false),
I("xn-*.a.b", false, false),
I("xn--*.a.b", false, false),
I("xn--w*.a.b", false, false),
// Redacted labels from RFC6962bis draft 4
// https://tools.ietf.org/html/draft-ietf-trans-rfc6962-bis-04#section-3.2.2
I("(PRIVATE).foo", false, false),
// maximum label length is 63 characters
I("1234567890" "1234567890" "1234567890"
"1234567890" "1234567890" "1234567890" "abc", true, true),
I("1234567890" "1234567890" "1234567890"
"1234567890" "1234567890" "1234567890" "abcd", false, false),
// maximum total length is 253 characters
I("1234567890" "1234567890" "1234567890" "1234567890" "1234567890" "."
"1234567890" "1234567890" "1234567890" "1234567890" "1234567890" "."
"1234567890" "1234567890" "1234567890" "1234567890" "1234567890" "."
"1234567890" "1234567890" "1234567890" "1234567890" "1234567890" "."
"1234567890" "1234567890" "1234567890" "1234567890" "12345678" "a",
true, true),
I("1234567890" "1234567890" "1234567890" "1234567890" "1234567890" "."
"1234567890" "1234567890" "1234567890" "1234567890" "1234567890" "."
"1234567890" "1234567890" "1234567890" "1234567890" "1234567890" "."
"1234567890" "1234567890" "1234567890" "1234567890" "1234567890" "."
"1234567890" "1234567890" "1234567890" "1234567890" "123456789" "a",
false, false),
};
static const InputValidity DNSNAMES_VALIDITY_TURKISH_I[] =
{
// http://en.wikipedia.org/wiki/Dotted_and_dotless_I#In_computing
// IDN registration rules disallow "latin capital letter i with dot above,"
// but our checks aren't intended to enforce those rules.
I("I", true, true), // ASCII capital I
I("i", true, true), // ASCII lowercase i
I("\xC4\xB0", false, false), // latin capital letter i with dot above
I("\xC4\xB1", false, false), // latin small letter dotless i
I("xn--i-9bb", true, true), // latin capital letter i with dot above, in punycode
I("xn--cfa", true, true), // latin small letter dotless i, in punycode
I("xn--\xC4\xB0", false, false), // latin capital letter i with dot above, mashup
I("xn--\xC4\xB1", false, false), // latin small letter dotless i, mashup
};
static const uint8_t LOWERCASE_I_VALUE[1] = { 'i' };
static const uint8_t UPPERCASE_I_VALUE[1] = { 'I' };
static const Input LOWERCASE_I(LOWERCASE_I_VALUE);
static const Input UPPERCASE_I(UPPERCASE_I_VALUE);
template <unsigned int L>
struct IPAddressParams
{
ByteString input;
bool isValid;
uint8_t expectedValueIfValid[L];
};
template <unsigned int L>
::std::ostream& operator<<(::std::ostream& os, const IPAddressParams<L>&)
{
return os << "TODO (bug 1318770)";
}
#define IPV4_VALID(str, a, b, c, d) \
{ \
ByteString(reinterpret_cast<const uint8_t*>(str), sizeof(str) - 1), \
true, \
{ a, b, c, d } \
}
// The value of expectedValueIfValid must be ignored for invalid IP addresses.
// The value { 73, 73, 73, 73 } is used because it is unlikely to result in an
// accidental match, unlike { 0, 0, 0, 0 }, which is a value we actually test.
#define IPV4_INVALID(str) \
{ \
ByteString(reinterpret_cast<const uint8_t*>(str), sizeof(str) - 1), \
false, \
{ 73, 73, 73, 73 } \
}
static const IPAddressParams<4> IPV4_ADDRESSES[] =
{
IPV4_INVALID(""),
IPV4_INVALID("1"),
IPV4_INVALID("1.2"),
IPV4_INVALID("1.2.3"),
IPV4_VALID("1.2.3.4", 1, 2, 3, 4),
IPV4_INVALID("1.2.3.4.5"),
IPV4_INVALID("1.2.3.4a"), // a DNSName!
IPV4_INVALID("a.2.3.4"), // not even a DNSName!
IPV4_INVALID("1::2"), // IPv6 address
// Whitespace not allowed
IPV4_INVALID(" 1.2.3.4"),
IPV4_INVALID("1.2.3.4 "),
IPV4_INVALID("1 .2.3.4"),
IPV4_INVALID("\n1.2.3.4"),
IPV4_INVALID("1.2.3.4\n"),
// Nulls not allowed
IPV4_INVALID("\0"),
IPV4_INVALID("\0" "1.2.3.4"),
IPV4_INVALID("1.2.3.4\0"),
IPV4_INVALID("1.2.3.4\0.5"),
// Range
IPV4_VALID("0.0.0.0", 0, 0, 0, 0),
IPV4_VALID("255.255.255.255", 255, 255, 255, 255),
IPV4_INVALID("256.0.0.0"),
IPV4_INVALID("0.256.0.0"),
IPV4_INVALID("0.0.256.0"),
IPV4_INVALID("0.0.0.256"),
IPV4_INVALID("999.0.0.0"),
IPV4_INVALID("9999999999999999999.0.0.0"),
// All digits allowed
IPV4_VALID("0.1.2.3", 0, 1, 2, 3),
IPV4_VALID("4.5.6.7", 4, 5, 6, 7),
IPV4_VALID("8.9.0.1", 8, 9, 0, 1),
// Leading zeros not allowed
IPV4_INVALID("01.2.3.4"),
IPV4_INVALID("001.2.3.4"),
IPV4_INVALID("00000000001.2.3.4"),
IPV4_INVALID("010.2.3.4"),
IPV4_INVALID("1.02.3.4"),
IPV4_INVALID("1.2.03.4"),
IPV4_INVALID("1.2.3.04"),
// Empty components
IPV4_INVALID(".2.3.4"),
IPV4_INVALID("1..3.4"),
IPV4_INVALID("1.2..4"),
IPV4_INVALID("1.2.3."),
// Too many components
IPV4_INVALID("1.2.3.4.5"),
IPV4_INVALID("1.2.3.4.5.6"),
IPV4_INVALID("0.1.2.3.4"),
IPV4_INVALID("1.2.3.4.0"),
// Leading/trailing dot
IPV4_INVALID(".1.2.3.4"),
IPV4_INVALID("1.2.3.4."),
// Other common forms of IPv4 address
// http://en.wikipedia.org/wiki/IPv4#Address_representations
IPV4_VALID("192.0.2.235", 192, 0, 2, 235), // dotted decimal (control value)
IPV4_INVALID("0xC0.0x00.0x02.0xEB"), // dotted hex
IPV4_INVALID("0301.0000.0002.0353"), // dotted octal
IPV4_INVALID("0xC00002EB"), // non-dotted hex
IPV4_INVALID("3221226219"), // non-dotted decimal
IPV4_INVALID("030000001353"), // non-dotted octal
IPV4_INVALID("192.0.0002.0xEB"), // mixed
};
#define IPV6_VALID(str, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
{ \
ByteString(reinterpret_cast<const uint8_t*>(str), sizeof(str) - 1), \
true, \
{ a, b, c, d, \
e, f, g, h, \
i, j, k, l, \
m, n, o, p } \
}
#define IPV6_INVALID(str) \
{ \
ByteString(reinterpret_cast<const uint8_t*>(str), sizeof(str) - 1), \
false, \
{ 73, 73, 73, 73, \
73, 73, 73, 73, \
73, 73, 73, 73, \
73, 73, 73, 73 } \
}
static const IPAddressParams<16> IPV6_ADDRESSES[] =
{
IPV6_INVALID(""),
IPV6_INVALID("1234"),
IPV6_INVALID("1234:5678"),
IPV6_INVALID("1234:5678:9abc"),
IPV6_INVALID("1234:5678:9abc:def0"),
IPV6_INVALID("1234:5678:9abc:def0:1234:"),
IPV6_INVALID("1234:5678:9abc:def0:1234:5678:"),
IPV6_INVALID("1234:5678:9abc:def0:1234:5678:9abc:"),
IPV6_VALID("1234:5678:9abc:def0:1234:5678:9abc:def0",
0x12, 0x34, 0x56, 0x78,
0x9a, 0xbc, 0xde, 0xf0,
0x12, 0x34, 0x56, 0x78,
0x9a, 0xbc, 0xde, 0xf0),
IPV6_INVALID("1234:5678:9abc:def0:1234:5678:9abc:def0:"),
IPV6_INVALID(":1234:5678:9abc:def0:1234:5678:9abc:def0"),
IPV6_INVALID("1234:5678:9abc:def0:1234:5678:9abc:def0:0000"),
// Valid contractions
IPV6_VALID("::1",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01),
IPV6_VALID("::1234",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x12, 0x34),
IPV6_VALID("1234::",
0x12, 0x34, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00),
IPV6_VALID("1234::5678",
0x12, 0x34, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x56, 0x78),
IPV6_VALID("1234:5678::abcd",
0x12, 0x34, 0x56, 0x78,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xab, 0xcd),
IPV6_VALID("1234:5678:9abc:def0:1234:5678:9abc::",
0x12, 0x34, 0x56, 0x78,
0x9a, 0xbc, 0xde, 0xf0,
0x12, 0x34, 0x56, 0x78,
0x9a, 0xbc, 0x00, 0x00),
// Contraction in full IPv6 addresses not allowed
IPV6_INVALID("::1234:5678:9abc:def0:1234:5678:9abc:def0"), // start
IPV6_INVALID("1234:5678:9abc:def0:1234:5678:9abc:def0::"), // end
IPV6_INVALID("1234:5678::9abc:def0:1234:5678:9abc:def0"), // interior
// Multiple contractions not allowed
IPV6_INVALID("::1::"),
IPV6_INVALID("::1::2"),
IPV6_INVALID("1::2::"),
// Colon madness!
IPV6_INVALID(":"),
IPV6_INVALID("::"),
IPV6_INVALID(":::"),
IPV6_INVALID("::::"),
IPV6_INVALID(":::1"),
IPV6_INVALID("::::1"),
IPV6_INVALID("1:::2"),
IPV6_INVALID("1::::2"),
IPV6_INVALID("1:2:::"),
IPV6_INVALID("1:2::::"),
IPV6_INVALID("::1234:"),
IPV6_INVALID(":1234::"),
IPV6_INVALID("01234::"), // too many digits, even if zero
IPV6_INVALID("12345678::"), // too many digits or missing colon
// uppercase
IPV6_VALID("ABCD:EFAB::",
0xab, 0xcd, 0xef, 0xab,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00),
// miXeD CAse
IPV6_VALID("aBcd:eFAb::",
0xab, 0xcd, 0xef, 0xab,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00),
// IPv4-style
IPV6_VALID("::2.3.4.5",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x02, 0x03, 0x04, 0x05),
IPV6_VALID("1234::2.3.4.5",
0x12, 0x34, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x02, 0x03, 0x04, 0x05),
IPV6_VALID("::abcd:2.3.4.5",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xab, 0xcd,
0x02, 0x03, 0x04, 0x05),
IPV6_VALID("1234:5678:9abc:def0:1234:5678:252.253.254.255",
0x12, 0x34, 0x56, 0x78,
0x9a, 0xbc, 0xde, 0xf0,
0x12, 0x34, 0x56, 0x78,
252, 253, 254, 255),
IPV6_VALID("1234:5678:9abc:def0:1234::252.253.254.255",
0x12, 0x34, 0x56, 0x78,
0x9a, 0xbc, 0xde, 0xf0,
0x12, 0x34, 0x00, 0x00,
252, 253, 254, 255),
IPV6_INVALID("1234::252.253.254"),
IPV6_INVALID("::252.253.254"),
IPV6_INVALID("::252.253.254.300"),
IPV6_INVALID("1234::252.253.254.255:"),
IPV6_INVALID("1234::252.253.254.255:5678"),
// Contractions that don't contract
IPV6_INVALID("::1234:5678:9abc:def0:1234:5678:9abc:def0"),
IPV6_INVALID("1234:5678:9abc:def0:1234:5678:9abc:def0::"),
IPV6_INVALID("1234:5678:9abc:def0::1234:5678:9abc:def0"),
IPV6_INVALID("1234:5678:9abc:def0:1234:5678::252.253.254.255"),
// With and without leading zeros
IPV6_VALID("::123",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x23),
IPV6_VALID("::0123",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x23),
IPV6_VALID("::012",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x12),
IPV6_VALID("::0012",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x12),
IPV6_VALID("::01",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01),
IPV6_VALID("::001",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01),
IPV6_VALID("::0001",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01),
IPV6_VALID("::0",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00),
IPV6_VALID("::00",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00),
IPV6_VALID("::000",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00),
IPV6_VALID("::0000",
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00),
IPV6_INVALID("::01234"),
IPV6_INVALID("::00123"),
IPV6_INVALID("::000123"),
// Trailing zero
IPV6_INVALID("::12340"),
// Whitespace
IPV6_INVALID(" 1234:5678:9abc:def0:1234:5678:9abc:def0"),
IPV6_INVALID("\t1234:5678:9abc:def0:1234:5678:9abc:def0"),
IPV6_INVALID("\t1234:5678:9abc:def0:1234:5678:9abc:def0\n"),
IPV6_INVALID("1234 :5678:9abc:def0:1234:5678:9abc:def0"),
IPV6_INVALID("1234: 5678:9abc:def0:1234:5678:9abc:def0"),
IPV6_INVALID(":: 2.3.4.5"),
IPV6_INVALID("1234::252.253.254.255 "),
IPV6_INVALID("1234::252.253.254.255\n"),
IPV6_INVALID("1234::252.253. 254.255"),
// Nulls
IPV6_INVALID("\0"),
IPV6_INVALID("::1\0:2"),
IPV6_INVALID("::1\0"),
IPV6_INVALID("::1.2.3.4\0"),
IPV6_INVALID("::1.2\02.3.4"),
};
class pkixnames_MatchPresentedDNSIDWithReferenceDNSID
: public ::testing::Test
, public ::testing::WithParamInterface<PresentedMatchesReference>
{
public:
DefaultNameMatchingPolicy mNameMatchingPolicy;
};
TEST_P(pkixnames_MatchPresentedDNSIDWithReferenceDNSID,
MatchPresentedDNSIDWithReferenceDNSID)
{
const PresentedMatchesReference& param(GetParam());
SCOPED_TRACE(param.presentedDNSID.c_str());
SCOPED_TRACE(param.referenceDNSID.c_str());
Input presented;
ASSERT_EQ(Success, presented.Init(param.presentedDNSID.data(),
param.presentedDNSID.length()));
Input reference;
ASSERT_EQ(Success, reference.Init(param.referenceDNSID.data(),
param.referenceDNSID.length()));
// sanity check that test makes sense
ASSERT_TRUE(IsValidReferenceDNSID(reference));
bool matches;
ASSERT_EQ(param.expectedResult,
MatchPresentedDNSIDWithReferenceDNSID(presented, reference,
matches));
if (param.expectedResult == Success) {
ASSERT_EQ(param.expectedMatches, matches);
}
}
INSTANTIATE_TEST_CASE_P(pkixnames_MatchPresentedDNSIDWithReferenceDNSID,
pkixnames_MatchPresentedDNSIDWithReferenceDNSID,
testing::ValuesIn(DNSID_MATCH_PARAMS));
class pkixnames_Turkish_I_Comparison
: public ::testing::Test
, public ::testing::WithParamInterface<InputValidity>
{
public:
DefaultNameMatchingPolicy mNameMatchingPolicy;
};
TEST_P(pkixnames_Turkish_I_Comparison, MatchPresentedDNSIDWithReferenceDNSID)
{
// Make sure we don't have the similar problems that strcasecmp and others
// have with the other kinds of "i" and "I" commonly used in Turkish locales.
const InputValidity& inputValidity(GetParam());
SCOPED_TRACE(inputValidity.input.c_str());
Input input;
ASSERT_EQ(Success, input.Init(inputValidity.input.data(),
inputValidity.input.length()));
bool isASCII = InputsAreEqual(LOWERCASE_I, input) ||
InputsAreEqual(UPPERCASE_I, input);
{
bool matches;
ASSERT_EQ(inputValidity.isValidPresentedID ? Success
: Result::ERROR_BAD_DER,
MatchPresentedDNSIDWithReferenceDNSID(input, LOWERCASE_I,
matches));
if (inputValidity.isValidPresentedID) {
ASSERT_EQ(isASCII, matches);
}
}
{
bool matches;
ASSERT_EQ(inputValidity.isValidPresentedID ? Success
: Result::ERROR_BAD_DER,
MatchPresentedDNSIDWithReferenceDNSID(input, UPPERCASE_I,
matches));
if (inputValidity.isValidPresentedID) {
ASSERT_EQ(isASCII, matches);
}
}
}
INSTANTIATE_TEST_CASE_P(pkixnames_Turkish_I_Comparison,
pkixnames_Turkish_I_Comparison,
testing::ValuesIn(DNSNAMES_VALIDITY_TURKISH_I));