@@ -36,6 +36,8 @@ type stubPivKey struct {
36
36
certMap map [piv.Slot ]* x509.Certificate
37
37
signerMap map [piv.Slot ]interface {}
38
38
keyOptionsMap map [piv.Slot ]piv.Key
39
+ serial uint32
40
+ serialErr error
39
41
closeErr error
40
42
}
41
43
@@ -93,15 +95,16 @@ func newStubPivKey(t *testing.T, alg symmetricAlgorithm) *stubPivKey {
93
95
t .Fatal (errors .New ("unknown alg" ))
94
96
}
95
97
96
- serialNumber , err := asn1 .Marshal (112233 )
98
+ sn := 112233
99
+ snAsn1 , err := asn1 .Marshal (sn )
97
100
if err != nil {
98
101
t .Fatal (err )
99
102
}
100
103
attCert , err := attestCA .Sign (& x509.Certificate {
101
104
Subject : pkix.Name {CommonName : "attested certificate" },
102
105
PublicKey : attSigner .Public (),
103
106
ExtraExtensions : []pkix.Extension {
104
- {Id : oidYubicoSerialNumber , Value : serialNumber },
107
+ {Id : oidYubicoSerialNumber , Value : snAsn1 },
105
108
},
106
109
})
107
110
if err != nil {
@@ -132,6 +135,7 @@ func newStubPivKey(t *testing.T, alg symmetricAlgorithm) *stubPivKey {
132
135
piv .SlotSignature : userSigner , // 9c
133
136
},
134
137
keyOptionsMap : map [piv.Slot ]piv.Key {},
138
+ serial : uint32 (sn ),
135
139
}
136
140
}
137
141
@@ -220,6 +224,13 @@ func (s *stubPivKey) Close() error {
220
224
return s .closeErr
221
225
}
222
226
227
+ func (s * stubPivKey ) Serial () (uint32 , error ) {
228
+ if s .serialErr != nil {
229
+ return 0 , s .serialErr
230
+ }
231
+ return s .serial , nil
232
+ }
233
+
223
234
func TestRegister (t * testing.T ) {
224
235
pCards := pivCards
225
236
t .Cleanup (func () {
@@ -1029,6 +1040,37 @@ func TestYubiKey_CreateAttestation(t *testing.T) {
1029
1040
}
1030
1041
}
1031
1042
1043
+ func TestYubiKey_Serial (t * testing.T ) {
1044
+ yk1 := newStubPivKey (t , RSA )
1045
+ yk2 := newStubPivKey (t , RSA )
1046
+ yk2 .serialErr = errors .New ("some error" )
1047
+
1048
+ tests := []struct {
1049
+ name string
1050
+ yk pivKey
1051
+ want string
1052
+ wantErr bool
1053
+ }{
1054
+ {"ok" , yk1 , "112233" , false },
1055
+ {"fail" , yk2 , "" , true },
1056
+ }
1057
+ for _ , tt := range tests {
1058
+ t .Run (tt .name , func (t * testing.T ) {
1059
+ k := & YubiKey {
1060
+ yk : tt .yk ,
1061
+ }
1062
+ got , err := k .Serial ()
1063
+ if (err != nil ) != tt .wantErr {
1064
+ t .Errorf ("YubiKey.Serial() error = %v, wantErr %v" , err , tt .wantErr )
1065
+ return
1066
+ }
1067
+ if ! reflect .DeepEqual (got , tt .want ) {
1068
+ t .Errorf ("YubiKey.Serial() = %v, want %v" , got , tt .want )
1069
+ }
1070
+ })
1071
+ }
1072
+ }
1073
+
1032
1074
func TestYubiKey_Close (t * testing.T ) {
1033
1075
yk1 := newStubPivKey (t , ECDSA )
1034
1076
yk2 := newStubPivKey (t , RSA )
@@ -1061,7 +1103,7 @@ func TestYubiKey_Close(t *testing.T) {
1061
1103
}
1062
1104
}
1063
1105
1064
- func Test_getSerialNumber (t * testing.T ) {
1106
+ func Test_getAttestedSerial (t * testing.T ) {
1065
1107
serialNumber , err := asn1 .Marshal (112233 )
1066
1108
if err != nil {
1067
1109
t .Fatal (err )
@@ -1107,8 +1149,8 @@ func Test_getSerialNumber(t *testing.T) {
1107
1149
}
1108
1150
for _ , tt := range tests {
1109
1151
t .Run (tt .name , func (t * testing.T ) {
1110
- if got := getSerialNumber (tt .args .cert ); got != tt .want {
1111
- t .Errorf ("getSerialNumber () = %v, want %v" , got , tt .want )
1152
+ if got := getAttestedSerial (tt .args .cert ); got != tt .want {
1153
+ t .Errorf ("getAttestedSerial () = %v, want %v" , got , tt .want )
1112
1154
}
1113
1155
})
1114
1156
}
0 commit comments