-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdn_test.go
More file actions
2199 lines (2092 loc) · 62.5 KB
/
Copy pathdn_test.go
File metadata and controls
2199 lines (2092 loc) · 62.5 KB
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
package certkit
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/binary"
"encoding/hex"
"errors"
"net"
"net/url"
"slices"
"strings"
"testing"
"time"
"unicode/utf16"
)
// --- FormatEKUs tests ---
func TestFormatEKUs(t *testing.T) {
// WHY: FormatEKUs maps typed ExtKeyUsage values to display names; wrong mapping
// silently mislabels certificate capabilities in inspect/scan output.
t.Parallel()
tests := []struct {
name string
ekus []x509.ExtKeyUsage
want []string
}{
{
name: "all known EKUs",
ekus: []x509.ExtKeyUsage{
x509.ExtKeyUsageAny,
x509.ExtKeyUsageServerAuth,
x509.ExtKeyUsageClientAuth,
x509.ExtKeyUsageCodeSigning,
x509.ExtKeyUsageEmailProtection,
x509.ExtKeyUsageTimeStamping,
x509.ExtKeyUsageOCSPSigning,
x509.ExtKeyUsageMicrosoftServerGatedCrypto,
x509.ExtKeyUsageNetscapeServerGatedCrypto,
},
want: []string{
"Any",
"Server Authentication",
"Client Authentication",
"Code Signing",
"Email Protection",
"Time Stamping",
"OCSP Signing",
"Microsoft Server Gated Crypto",
"Netscape Server Gated Crypto",
},
},
{
name: "unknown EKU value",
ekus: []x509.ExtKeyUsage{x509.ExtKeyUsage(9999)},
want: []string{"Unknown (9999)"},
},
{
name: "empty input",
ekus: nil,
want: nil,
},
{
name: "mixed known and unknown",
ekus: []x509.ExtKeyUsage{
x509.ExtKeyUsageServerAuth,
x509.ExtKeyUsage(42),
x509.ExtKeyUsageOCSPSigning,
},
want: []string{"Server Authentication", "Unknown (42)", "OCSP Signing"},
},
{
name: "duplicate EKUs preserved",
ekus: []x509.ExtKeyUsage{
x509.ExtKeyUsageServerAuth,
x509.ExtKeyUsageServerAuth,
x509.ExtKeyUsageClientAuth,
},
want: []string{"Server Authentication", "Server Authentication", "Client Authentication"},
},
{
name: "empty but non-nil input returns empty",
ekus: []x509.ExtKeyUsage{},
want: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// WHY: Ensures FormatEKUs renders this EKU set correctly.
t.Parallel()
got := FormatEKUs(tt.ekus)
if !slices.Equal(got, tt.want) {
t.Errorf("FormatEKUs(%v) = %v, want %v", tt.ekus, got, tt.want)
}
})
}
}
// --- FormatEKUOIDs tests ---
func TestFormatEKUOIDs(t *testing.T) {
// WHY: FormatEKUOIDs parses raw ASN.1 EKU extension bytes from CSRs where Go
// doesn't populate typed fields; incorrect parsing silently drops EKU info.
t.Parallel()
tests := []struct {
name string
raw []byte
want []string
}{
{
name: "nil raw returns nil",
raw: nil,
want: nil,
},
{
name: "empty raw returns nil",
raw: []byte{},
want: nil,
},
{
name: "known OIDs",
raw: mustMarshalOIDs(t,
asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 1}, // serverAuth
asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 2}, // clientAuth
),
want: []string{"Server Authentication", "Client Authentication"},
},
{
name: "unknown OID falls back to string",
raw: mustMarshalOIDs(t,
asn1.ObjectIdentifier{1, 2, 3, 4, 5, 6, 7},
),
want: []string{"1.2.3.4.5.6.7"},
},
{
name: "mixed known and unknown",
raw: mustMarshalOIDs(t,
asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 3}, // codeSigning
asn1.ObjectIdentifier{1, 2, 99, 99},
),
want: []string{"Code Signing", "1.2.99.99"},
},
{
name: "duplicate OIDs preserved",
raw: mustMarshalOIDs(t,
asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 1},
asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 1},
),
want: []string{"Server Authentication", "Server Authentication"},
},
{
name: "invalid ASN.1 returns nil",
raw: []byte{0xFF, 0xFF, 0xFF},
want: nil,
},
{
name: "wrong ASN.1 element type returns nil",
raw: mustMarshalInts(t, 1, 2),
want: nil,
},
{
name: "mixed element types returns nil",
raw: mustMarshalSequence(t,
mustMarshalASN1Value(t, asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 1}),
mustMarshalASN1Value(t, 42),
),
want: nil,
},
{
name: "trailing bytes return nil",
raw: append(mustMarshalOIDs(t, asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 1}), 0x00),
want: nil,
},
{
name: "empty sequence returns nil",
raw: mustMarshalOIDs(t),
want: nil,
},
{
name: "all known OIDs",
raw: mustMarshalOIDs(t,
asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 1}, // serverAuth
asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 2}, // clientAuth
asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 3}, // codeSigning
asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 4}, // emailProtection
asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 8}, // timeStamping
asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 9}, // ocspSigning
asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 10, 3, 3}, // microsoftSGC
asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 4, 1}, // netscapeSGC
asn1.ObjectIdentifier{2, 5, 29, 37, 0}, // anyExtendedKeyUsage
),
want: []string{
"Server Authentication",
"Client Authentication",
"Code Signing",
"Email Protection",
"Time Stamping",
"OCSP Signing",
"Microsoft Server Gated Crypto",
"Netscape Server Gated Crypto",
"Any",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// WHY: Ensures FormatEKUOIDs renders this ASN.1 OID list correctly.
t.Parallel()
got := FormatEKUOIDs(tt.raw)
if !slices.Equal(got, tt.want) {
t.Errorf("FormatEKUOIDs() = %v, want %v", got, tt.want)
}
})
}
}
// mustMarshalOIDs marshals a SEQUENCE OF OID for FormatEKUOIDs test input.
func mustMarshalOIDs(t *testing.T, oids ...asn1.ObjectIdentifier) []byte {
t.Helper()
raw, err := asn1.Marshal(oids)
if err != nil {
t.Fatalf("marshal OIDs: %v", err)
}
return raw
}
func mustMarshalInts(t *testing.T, ints ...int) []byte {
t.Helper()
raw, err := asn1.Marshal(ints)
if err != nil {
t.Fatalf("marshal ints: %v", err)
}
return raw
}
func mustMarshalASN1Value(t *testing.T, value any) []byte {
t.Helper()
raw, err := asn1.Marshal(value)
if err != nil {
t.Fatalf("marshal ASN.1 value: %v", err)
}
return raw
}
func mustMarshalSequence(t *testing.T, elements ...[]byte) []byte {
t.Helper()
var content []byte
for _, element := range elements {
content = append(content, element...)
}
length := len(content)
switch {
case length < 0x80:
return append([]byte{0x30, byte(length)}, content...)
case length <= 0xff:
return append([]byte{0x30, 0x81, byte(length)}, content...)
case length <= 0xffff:
length16, err := checkedUint16Len(length, "ASN.1 sequence length")
if err != nil {
t.Fatal(err)
}
var lengthBytes [2]byte
binary.BigEndian.PutUint16(lengthBytes[:], length16)
return append([]byte{0x30, 0x82, lengthBytes[0], lengthBytes[1]}, content...)
default:
t.Fatalf("sequence too large: %d", length)
return nil
}
}
// --- FormatKeyUsage tests ---
func TestFormatKeyUsage(t *testing.T) {
// WHY: FormatKeyUsage maps KeyUsage bitmask to names; bit-order mistakes
// produce wrong labels (e.g. calling CertSign "Key Encipherment").
t.Parallel()
tests := []struct {
name string
ku x509.KeyUsage
want []string
}{
{
name: "single bit DigitalSignature",
ku: x509.KeyUsageDigitalSignature,
want: []string{"Digital Signature"},
},
{
name: "single bit CertSign",
ku: x509.KeyUsageCertSign,
want: []string{"Certificate Sign"},
},
{
name: "CA typical: CertSign and CRLSign",
ku: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
want: []string{"Certificate Sign", "CRL Sign"},
},
{
name: "leaf typical: DigitalSignature and KeyEncipherment",
ku: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
want: []string{"Digital Signature", "Key Encipherment"},
},
{
name: "all nine bits set",
ku: x509.KeyUsageDigitalSignature | x509.KeyUsageContentCommitment |
x509.KeyUsageKeyEncipherment | x509.KeyUsageDataEncipherment |
x509.KeyUsageKeyAgreement | x509.KeyUsageCertSign |
x509.KeyUsageCRLSign | x509.KeyUsageEncipherOnly |
x509.KeyUsageDecipherOnly,
want: []string{
"Digital Signature",
"Content Commitment",
"Key Encipherment",
"Data Encipherment",
"Key Agreement",
"Certificate Sign",
"CRL Sign",
"Encipher Only",
"Decipher Only",
},
},
{
name: "zero value returns nil",
ku: 0,
want: nil,
},
{
name: "unknown bits ignored",
ku: x509.KeyUsage(1 << 12),
want: nil,
},
{
name: "known and unknown bits",
ku: x509.KeyUsageDigitalSignature | x509.KeyUsage(1<<12),
want: []string{"Digital Signature"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// WHY: Ensures FormatKeyUsage renders this KeyUsage bitmask correctly.
t.Parallel()
got := FormatKeyUsage(tt.ku)
if !slices.Equal(got, tt.want) {
t.Errorf("FormatKeyUsage(%d) = %v, want %v", tt.ku, got, tt.want)
}
})
}
}
// --- FormatKeyUsageBitString tests ---
func TestFormatKeyUsageBitString(t *testing.T) {
// WHY: FormatKeyUsageBitString parses raw ASN.1 BIT STRING from CSR extensions;
// bit-order mismatch between ASN.1 BIT STRING and Go's KeyUsage would produce
// silently wrong output.
t.Parallel()
tests := []struct {
name string
raw []byte
want []string
}{
{
name: "nil raw returns nil",
raw: nil,
want: nil,
},
{
name: "empty raw returns nil",
raw: []byte{},
want: nil,
},
{
name: "zero-length bitstring returns nil",
raw: mustMarshalEmptyBitString(t),
want: nil,
},
{
name: "all zero bits returns nil",
raw: mustMarshalBitString(t, 9),
want: nil,
},
{
name: "DigitalSignature only",
raw: mustMarshalBitString(t, 9, 0),
want: []string{"Digital Signature"},
},
{
name: "CertSign and CRLSign",
raw: mustMarshalBitString(t, 9, 5, 6),
want: []string{"Certificate Sign", "CRL Sign"},
},
{
name: "DigitalSignature and KeyEncipherment",
raw: mustMarshalBitString(t, 9, 0, 2),
want: []string{"Digital Signature", "Key Encipherment"},
},
{
name: "all key usage bits",
raw: mustMarshalBitString(t, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8),
want: []string{
"Digital Signature",
"Content Commitment",
"Key Encipherment",
"Data Encipherment",
"Key Agreement",
"Certificate Sign",
"CRL Sign",
"Encipher Only",
"Decipher Only",
},
},
{
name: "invalid ASN.1 returns nil",
raw: []byte{0xFF, 0xFF},
want: nil,
},
{
name: "wrong ASN.1 element type returns nil",
raw: mustMarshalInts(t, 1),
want: nil,
},
{
name: "extra bits ignored",
raw: mustMarshalBitString(t, 12, 0, 10),
want: []string{"Digital Signature"},
},
{
name: "short bitstring length",
raw: mustMarshalBitString(t, 1, 0),
want: []string{"Digital Signature"},
},
{
name: "unused bits set returns nil",
raw: []byte{0x03, 0x03, 0x07, 0x80, 0x01},
want: nil,
},
{
name: "trailing bytes return nil",
raw: append(mustMarshalKeyUsageBitString(t, x509.KeyUsageDigitalSignature), 0x00),
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// WHY: Ensures FormatKeyUsageBitString parses this ASN.1 BIT STRING correctly.
t.Parallel()
got := FormatKeyUsageBitString(tt.raw)
if !slices.Equal(got, tt.want) {
t.Errorf("FormatKeyUsageBitString() = %v, want %v", got, tt.want)
}
})
}
}
// mustMarshalKeyUsageBitString builds a raw ASN.1 BIT STRING encoding for the
// given x509.KeyUsage bitmask, matching the encoding that crypto/x509 produces
// in the KeyUsage extension.
func mustMarshalKeyUsageBitString(t *testing.T, ku x509.KeyUsage) []byte {
t.Helper()
// Build a BitString with 9 bits (the number of defined key usage bits).
// Go's x509 package stores bit i of KeyUsage as bit i in the BIT STRING.
bs := asn1.BitString{
Bytes: make([]byte, 2),
BitLength: 9,
}
for i := range 9 {
if ku&(1<<uint(i)) != 0 {
// ASN.1 BIT STRING bit numbering: bit 0 is the MSB of byte 0.
byteIdx := i / 8
bitIdx := 7 - (i % 8)
bs.Bytes[byteIdx] |= 1 << uint(bitIdx)
}
}
raw, err := asn1.Marshal(bs)
if err != nil {
t.Fatalf("marshal BitString: %v", err)
}
return raw
}
func mustMarshalBitString(t *testing.T, bitLength int, setBits ...int) []byte {
t.Helper()
if bitLength <= 0 {
t.Fatalf("bitLength must be positive, got %d", bitLength)
}
bs := asn1.BitString{
Bytes: make([]byte, (bitLength+7)/8),
BitLength: bitLength,
}
for _, bit := range setBits {
if bit < 0 || bit >= bitLength {
t.Fatalf("bit %d out of range for length %d", bit, bitLength)
}
byteIdx := bit / 8
bitIdx := 7 - (bit % 8)
bs.Bytes[byteIdx] |= 1 << uint(bitIdx)
}
raw, err := asn1.Marshal(bs)
if err != nil {
t.Fatalf("marshal BitString: %v", err)
}
return raw
}
func mustMarshalEmptyBitString(t *testing.T) []byte {
t.Helper()
raw, err := asn1.Marshal(asn1.BitString{})
if err != nil {
t.Fatalf("marshal empty BitString: %v", err)
}
return raw
}
// --- ParseOtherNameSANs tests ---
func TestParseOtherNameSANs(t *testing.T) {
// WHY: ParseOtherNameSANs recovers SAN entries that Go silently drops
// (OtherName, DirName, RegisteredID); failure to parse means critical
// identity info (like UPN) is invisible in inspect output.
t.Parallel()
sanOID := asn1.ObjectIdentifier{2, 5, 29, 17}
upnOID := asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 20, 2, 3}
makeExts := func(value []byte) []pkix.Extension {
return []pkix.Extension{{Id: sanOID, Value: value}}
}
tests := []struct {
name string
build func(t *testing.T) ([]pkix.Extension, []string, bool)
}{
{
name: "OtherName with UPN",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
sanBytes := buildSANWithOtherName(t, upnOID, "user@example.com")
return makeExts(sanBytes), []string{"UPN:user@example.com"}, false
},
},
{
name: "OtherName with unknown OID falls back to OID string",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
sanBytes := buildSANWithOtherName(t, asn1.ObjectIdentifier{1, 2, 3, 4, 5}, "some-value")
return makeExts(sanBytes), []string{"1.2.3.4.5:some-value"}, false
},
},
{
name: "OtherName with non-string value falls back to hex",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
value := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagOctetString, Bytes: []byte{0xde, 0xad}}
valueBytes, err := asn1.Marshal(value)
if err != nil {
t.Fatalf("marshal OtherName value: %v", err)
}
otherNameGN := marshalOtherNameGeneralNameWithValue(t, upnOID, value)
sanBytes := buildSANWithGeneralNames(t, otherNameGN)
return makeExts(sanBytes), []string{"UPN:" + hex.EncodeToString(valueBytes)}, false
},
},
{
name: "DirectoryName",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
sanBytes := buildSANWithDirectoryName(t, pkix.Name{CommonName: "test-dir"})
return makeExts(sanBytes), []string{"DirName:CN=test-dir"}, false
},
},
{
name: "RegisteredID",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
sanBytes := buildSANWithRegisteredID(t, asn1.ObjectIdentifier{1, 2, 3, 4})
return makeExts(sanBytes), []string{"RegisteredID:1.2.3.4"}, false
},
},
{
name: "RegisteredID with invalid bytes is ignored",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
regIDGN := mustMarshalGeneralName(t, asn1.RawValue{
Class: asn1.ClassContextSpecific,
Tag: 8,
Bytes: []byte{0x80},
})
sanBytes := buildSANWithGeneralNames(t, regIDGN)
return makeExts(sanBytes), nil, true
},
},
{
name: "OtherName missing explicit value is ignored",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
oidBytes, err := asn1.Marshal(upnOID)
if err != nil {
t.Fatalf("marshal OID: %v", err)
}
otherNameGN := asn1.RawValue{
Class: asn1.ClassContextSpecific,
Tag: 0,
IsCompound: true,
Bytes: oidBytes,
}
gnBytes, err := asn1.Marshal(otherNameGN)
if err != nil {
t.Fatalf("marshal OtherName GN: %v", err)
}
sanBytes := buildSANWithGeneralNames(t, gnBytes)
return makeExts(sanBytes), nil, true
},
},
{
name: "OtherName with wrong explicit tag is ignored",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
oidBytes, err := asn1.Marshal(upnOID)
if err != nil {
t.Fatalf("marshal OID: %v", err)
}
valueBytes, err := asn1.Marshal(asn1.RawValue{
Class: asn1.ClassUniversal,
Tag: asn1.TagUTF8String,
Bytes: []byte("bad-tag"),
})
if err != nil {
t.Fatalf("marshal value: %v", err)
}
explicitWrapper := asn1.RawValue{
Class: asn1.ClassContextSpecific,
Tag: 1,
IsCompound: true,
Bytes: valueBytes,
}
explicitBytes, err := asn1.Marshal(explicitWrapper)
if err != nil {
t.Fatalf("marshal explicit wrapper: %v", err)
}
otherNameGN := asn1.RawValue{
Class: asn1.ClassContextSpecific,
Tag: 0,
IsCompound: true,
Bytes: append(oidBytes, explicitBytes...),
}
gnBytes, err := asn1.Marshal(otherNameGN)
if err != nil {
t.Fatalf("marshal OtherName GN: %v", err)
}
sanBytes := buildSANWithGeneralNames(t, gnBytes)
return makeExts(sanBytes), nil, true
},
},
{
name: "OtherName with non-compound explicit value is ignored",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
oidBytes, err := asn1.Marshal(upnOID)
if err != nil {
t.Fatalf("marshal OID: %v", err)
}
valueBytes, err := asn1.Marshal(asn1.RawValue{
Class: asn1.ClassUniversal,
Tag: asn1.TagUTF8String,
Bytes: []byte("not-compound"),
})
if err != nil {
t.Fatalf("marshal value: %v", err)
}
explicitWrapper := asn1.RawValue{
Class: asn1.ClassContextSpecific,
Tag: 0,
IsCompound: false,
Bytes: valueBytes,
}
explicitBytes, err := asn1.Marshal(explicitWrapper)
if err != nil {
t.Fatalf("marshal explicit wrapper: %v", err)
}
otherNameGN := asn1.RawValue{
Class: asn1.ClassContextSpecific,
Tag: 0,
IsCompound: true,
Bytes: append(oidBytes, explicitBytes...),
}
gnBytes, err := asn1.Marshal(otherNameGN)
if err != nil {
t.Fatalf("marshal OtherName GN: %v", err)
}
sanBytes := buildSANWithGeneralNames(t, gnBytes)
return makeExts(sanBytes), nil, true
},
},
{
name: "malformed registeredID before valid OtherName",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
badRegID := mustMarshalGeneralName(t, asn1.RawValue{
Class: asn1.ClassContextSpecific,
Tag: 8,
Bytes: []byte{0xff},
})
goodOther := marshalOtherNameGeneralName(t, upnOID, "later@example.com")
sanBytes := buildSANWithGeneralNames(t, badRegID, goodOther)
return makeExts(sanBytes), []string{"UPN:later@example.com"}, false
},
},
{
name: "malformed DirectoryName before valid OtherName",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
badDir := mustMarshalGeneralName(t, asn1.RawValue{
Class: asn1.ClassContextSpecific,
Tag: 4,
IsCompound: true,
Bytes: []byte{0xff},
})
goodOther := marshalOtherNameGeneralName(t, upnOID, "later@example.com")
sanBytes := buildSANWithGeneralNames(t, badDir, goodOther)
return makeExts(sanBytes), []string{"UPN:later@example.com"}, false
},
},
{
name: "mixed SAN entries",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
sanBytes := buildSANWithGeneralNames(t,
marshalOtherNameGeneralName(t, upnOID, "mix@example.com"),
marshalDirectoryNameGeneralName(t, pkix.Name{CommonName: "mixed-dir"}),
marshalRegisteredIDGeneralName(t, asn1.ObjectIdentifier{1, 2, 3, 4, 5}),
marshalDNSNameGeneralName(t, "example.com"),
)
want := []string{"UPN:mix@example.com", "DirName:CN=mixed-dir", "RegisteredID:1.2.3.4.5"}
return makeExts(sanBytes), want, false
},
},
{
name: "multiple SAN extensions",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
san1 := buildSANWithOtherName(t, upnOID, "first@example.com")
san2 := buildSANWithDirectoryName(t, pkix.Name{CommonName: "second-dir"})
exts := []pkix.Extension{{Id: sanOID, Value: san1}, {Id: sanOID, Value: san2}}
want := []string{"UPN:first@example.com", "DirName:CN=second-dir"}
return exts, want, false
},
},
{
name: "multiple SAN extensions with invalid first",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
san1 := []byte{0xff, 0xff, 0xff}
san2 := buildSANWithOtherName(t, upnOID, "second@example.com")
exts := []pkix.Extension{{Id: sanOID, Value: san1}, {Id: sanOID, Value: san2}}
return exts, []string{"UPN:second@example.com"}, false
},
},
{
name: "duplicate OtherName entries preserve order",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
san1 := buildSANWithOtherName(t, upnOID, "first@example.com")
san2 := buildSANWithOtherName(t, upnOID, "second@example.com")
exts := []pkix.Extension{{Id: sanOID, Value: san1}, {Id: sanOID, Value: san2}}
return exts, []string{"UPN:first@example.com", "UPN:second@example.com"}, false
},
},
{
name: "empty SAN sequence returns nil",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
sanBytes := buildSANWithGeneralNames(t)
return makeExts(sanBytes), nil, true
},
},
{
name: "no SAN extension returns nil",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
exts := []pkix.Extension{{
Id: asn1.ObjectIdentifier{2, 5, 29, 19},
Value: []byte{0x30, 0x00},
}}
return exts, nil, true
},
},
{
name: "nil extensions returns nil",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
return nil, nil, true
},
},
{
name: "SAN with only dNSName returns nil",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
sanBytes := buildSANWithDNSName(t, "example.com")
return makeExts(sanBytes), nil, true
},
},
{
name: "unknown GeneralName tag is ignored",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
ipGN := mustMarshalGeneralName(t, asn1.RawValue{
Class: asn1.ClassContextSpecific,
Tag: 7,
Bytes: []byte{127, 0, 0, 1},
})
sanBytes := buildSANWithGeneralNames(t, ipGN)
return makeExts(sanBytes), nil, true
},
},
{
name: "invalid SAN bytes returns nil",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
exts := []pkix.Extension{{Id: sanOID, Value: []byte{0xFF, 0xFF, 0xFF}}}
return exts, nil, true
},
},
{
name: "valid entry before malformed bytes",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
validGN := marshalOtherNameGeneralName(t, upnOID, "ok@example.com")
sanBytes := buildSANWithGeneralNames(t, validGN, []byte{0xFF})
return makeExts(sanBytes), []string{"UPN:ok@example.com"}, false
},
},
{
name: "malformed entry before valid bytes",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
malformedGN, err := asn1.Marshal(asn1.RawValue{
Class: asn1.ClassContextSpecific,
Tag: 0,
IsCompound: true,
Bytes: []byte{0x30, 0x00},
})
if err != nil {
t.Fatalf("marshal malformed OtherName: %v", err)
}
validGN := marshalOtherNameGeneralName(t, upnOID, "later@example.com")
sanBytes := buildSANWithGeneralNames(t, malformedGN, validGN)
return makeExts(sanBytes), []string{"UPN:later@example.com"}, false
},
},
{
name: "OtherName with IA5String",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
otherNameGN := marshalOtherNameGeneralNameWithValue(t, upnOID, asn1.RawValue{
Class: asn1.ClassUniversal,
Tag: asn1.TagIA5String,
Bytes: []byte("ia5@example.com"),
})
sanBytes := buildSANWithGeneralNames(t, otherNameGN)
return makeExts(sanBytes), []string{"UPN:ia5@example.com"}, false
},
},
{
name: "OtherName with PrintableString",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
otherNameGN := marshalOtherNameGeneralNameWithValue(t, upnOID, asn1.RawValue{
Class: asn1.ClassUniversal,
Tag: asn1.TagPrintableString,
Bytes: []byte("PRINTABLE"),
})
sanBytes := buildSANWithGeneralNames(t, otherNameGN)
return makeExts(sanBytes), []string{"UPN:PRINTABLE"}, false
},
},
{
name: "OtherName with BMPString",
build: func(t *testing.T) ([]pkix.Extension, []string, bool) {
t.Helper()
otherNameGN := marshalOtherNameGeneralNameWithValue(t, upnOID, asn1.RawValue{
Class: asn1.ClassUniversal,
Tag: asn1.TagBMPString,
Bytes: bmpStringBytes("BMP"),
})
sanBytes := buildSANWithGeneralNames(t, otherNameGN)
return makeExts(sanBytes), []string{"UPN:BMP"}, false
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// WHY: Confirms ParseOtherNameSANs handles this SAN mix correctly.
t.Parallel()
exts, want, wantNil := tt.build(t)
got := ParseOtherNameSANs(exts)
if wantNil {
if got != nil {
t.Errorf("expected nil, got %v", got)
}
return
}
if !slices.Equal(got, want) {
t.Errorf("got %v, want %v", got, want)
}
})
}
}
func TestParseOtherNameSANs_FromCertificate(t *testing.T) {
// WHY: End-to-end test using a real certificate with OtherName SAN to verify
// ParseOtherNameSANs works with actual x509.Certificate.Extensions, not just
// hand-crafted extension slices.
t.Parallel()
upnOID := asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 20, 2, 3}
sanBytes := buildSANWithGeneralNames(t,
marshalOtherNameGeneralName(t, upnOID, "admin@corp.example.com"),
marshalDirectoryNameGeneralName(t, pkix.Name{CommonName: "dir-name"}),
marshalRegisteredIDGeneralName(t, asn1.ObjectIdentifier{1, 2, 3, 4}),
)
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
template := &x509.Certificate{
SerialNumber: randomSerial(t),
Subject: pkix.Name{CommonName: "test"},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
ExtraExtensions: []pkix.Extension{{
Id: asn1.ObjectIdentifier{2, 5, 29, 17},
Value: sanBytes,
}},
}
certDER, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
t.Fatal(err)
}
cert, err := x509.ParseCertificate(certDER)
if err != nil {
t.Fatal(err)
}
got := ParseOtherNameSANs(cert.Extensions)
want := []string{"UPN:admin@corp.example.com", "DirName:CN=dir-name", "RegisteredID:1.2.3.4"}
if !slices.Equal(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}
func TestCollectCertificateExtensions(t *testing.T) {
// WHY: Users need visibility into every top-level extension on a
// certificate, including proprietary critical extensions that Go leaves
// in UnhandledCriticalExtensions.
t.Parallel()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
appleOID := asn1.ObjectIdentifier{1, 2, 840, 113635, 100, 6, 27, 3, 2}
unknownOID := asn1.ObjectIdentifier{1, 2, 3, 4, 5}
template := &x509.Certificate{
SerialNumber: randomSerial(t),
Subject: pkix.Name{CommonName: "extensions.example.com"},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
DNSNames: []string{"extensions.example.com"},
ExtraExtensions: []pkix.Extension{
{Id: appleOID, Critical: true, Value: []byte{0x05, 0x00}},
{Id: unknownOID, Value: []byte{0x05, 0x00}},
},
}
der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
t.Fatal(err)
}
cert, err := x509.ParseCertificate(der)
if err != nil {
t.Fatal(err)
}
got := CollectCertificateExtensions(cert)
if len(got) == 0 {