-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcommand_set_test.go
More file actions
318 lines (266 loc) · 9.63 KB
/
Copy pathcommand_set_test.go
File metadata and controls
318 lines (266 loc) · 9.63 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
package keycard
import (
"encoding/binary"
"errors"
"testing"
"github.com/keycard-tech/keycard-go/v4/apdu"
"github.com/keycard-tech/keycard-go/v4/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// mockSelectChannel is a mock APDU channel for testing CommandSet auto-detection.
type mockSelectChannel struct {
responses []*apdu.Response
index int
}
func (m *mockSelectChannel) Send(cmd *apdu.Command) (*apdu.Response, error) {
if m.index >= len(m.responses) {
return nil, errors.New("no more mock responses")
}
resp := m.responses[m.index]
m.index++
return resp, nil
}
// buildSelectResponsePreInit builds a SELECT response for an uninitialized card.
// Only contains TLV_PUB_KEY (0x80) with a valid uncompressed public key.
func buildSelectResponsePreInit(pubKey []byte) *apdu.Response {
data := make([]byte, 0, 2+1+len(pubKey))
data = append(data, 0x80, byte(len(pubKey)))
data = append(data, pubKey...)
return &apdu.Response{Data: data, Sw1: 0x90, Sw2: 0x00, Sw: 0x9000}
}
func TestCommandSet_NewCommandSet(t *testing.T) {
cs := NewCommandSet(nil)
assert.NotNil(t, cs)
assert.Equal(t, [][33]byte{DefaultCAPublicKey}, cs.caPublicKeys)
assert.Nil(t, cs.whitelistedKeys)
}
func TestCommandSet_NewCommandSetWithCA(t *testing.T) {
ca := [33]byte{0x02}
for i := 1; i < 33; i++ {
ca[i] = byte(i)
}
cs := NewCommandSetWithCA(nil, ca)
assert.NotNil(t, cs)
assert.Equal(t, [][33]byte{ca}, cs.caPublicKeys)
}
func TestCommandSet_NewCommandSetWithCAs(t *testing.T) {
ca1 := [33]byte{0x02}
ca2 := [33]byte{0x03}
cardKey := [33]byte{0x04}
cs := NewCommandSetWithCAs(nil, [][33]byte{ca1, ca2}, [][33]byte{cardKey})
assert.NotNil(t, cs)
assert.Equal(t, [][33]byte{ca1, ca2}, cs.caPublicKeys)
assert.Equal(t, [][33]byte{cardKey}, cs.whitelistedKeys)
}
func TestCommandSet_SecureChannelVersion_NoSelect(t *testing.T) {
cs := NewCommandSet(nil)
version, ok := cs.SecureChannelVersion()
assert.False(t, ok)
_ = version
}
func TestCommandSet_PairingPasswordToSecret(t *testing.T) {
secret1 := PairingPasswordToSecret("test_password")
secret2 := PairingPasswordToSecret("test_password")
secret3 := PairingPasswordToSecret("different_password")
assert.Equal(t, secret1, secret2, "same password should produce same secret")
assert.NotEqual(t, secret1, secret3, "different passwords should produce different secrets")
assert.Equal(t, 32, len(secret1), "secret should be 32 bytes")
}
func TestCommandSet_SetPairing(t *testing.T) {
cs := NewCommandSet(nil)
cs.sc = NewSecureChannel(nil) // V1 for pairing
key := [32]byte{1, 2, 3}
cs.SetPairing(types.NewPairing(key, 5))
pairing := cs.Pairing()
assert.NotNil(t, pairing)
assert.Equal(t, uint8(5), pairing.Index())
assert.Equal(t, key, pairing.Key())
}
func TestParseMnemonicResponse(t *testing.T) {
data := make([]byte, 0)
for _, idx := range []int{12, 3456, 7890} {
data = append(data, byte(idx>>8), byte(idx))
}
indexes, err := parseMnemonicResponse(data)
require.NoError(t, err)
assert.Equal(t, []int{12, 3456, 7890}, indexes)
}
func TestCommandSet_BuildInitData(t *testing.T) {
cs := NewCommandSet(nil)
// Basic: PIN || PUK || shared_secret
data := cs.buildInitData("123456", "123456789012", []byte{0xAB, 0xCD}, nil, 0, 0)
expected := make([]byte, 0, len("123456")+len("123456789012")+2)
expected = append(expected, []byte("123456")...)
expected = append(expected, []byte("123456789012")...)
expected = append(expected, 0xAB, 0xCD)
assert.Equal(t, expected, data)
// With retries
data = cs.buildInitData("123456", "123456789012", []byte{0xAB}, nil, 5, 10)
expected = make([]byte, 0, 20)
expected = append(expected, []byte("123456")...)
expected = append(expected, []byte("123456789012")...)
expected = append(expected, 0xAB, 0x05, 0x0A)
assert.Equal(t, expected, data)
// With alt PIN
altPin := "654321"
data = cs.buildInitData("123456", "123456789012", []byte{}, &altPin, 0, 0)
expected = make([]byte, 0, 30)
expected = append(expected, []byte("123456")...)
expected = append(expected, []byte("123456789012")...)
expected = append(expected, 0x00, 0x00)
expected = append(expected, []byte(altPin)...)
assert.Equal(t, expected, data)
}
func TestCommandSet_IsSecureChannelV2(t *testing.T) {
cs := NewCommandSet(nil)
cs.ApplicationInfo = &types.ApplicationInfo{}
cs.ApplicationInfo.Version = []byte{0x03, 0x00}
assert.False(t, cs.isSecureChannelV2())
cs.ApplicationInfo.Version = []byte{0x04, 0x00}
assert.True(t, cs.isSecureChannelV2())
cs.ApplicationInfo.Version = []byte{0x04, 0x02}
assert.True(t, cs.isSecureChannelV2())
cs.ApplicationInfo.Version = []byte{0x05, 0x00}
assert.True(t, cs.isSecureChannelV2())
}
func TestWrongPINError(t *testing.T) {
err := &WrongPINError{RemainingAttempts: 3}
assert.Contains(t, err.Error(), "wrong pin")
assert.Contains(t, err.Error(), "3")
}
func TestWrongPUKError(t *testing.T) {
err := &WrongPUKError{RemainingAttempts: 5}
assert.Contains(t, err.Error(), "wrong puk")
assert.Contains(t, err.Error(), "5")
}
// Test that the new command builders produce correct APDUs
func TestNewCommandBuilders(t *testing.T) {
t.Run("LoadLEEKey", func(t *testing.T) {
seed := []byte{0x01, 0x02, 0x03}
cmd := NewCommandLoadLEEKey(seed)
assert.Equal(t, uint8(0xD0), cmd.Ins)
assert.Equal(t, uint8(P1LoadKeyLEE), cmd.P1)
assert.Equal(t, seed, cmd.Data)
})
t.Run("ExportLEE", func(t *testing.T) {
path := []byte{0x00, 0x00, 0x00, 0x2C} // m/44
cmd := NewCommandExportLEE(0x00, path)
assert.Equal(t, uint8(InsExportLEE), cmd.Ins)
assert.Equal(t, uint8(0x00), cmd.P1)
assert.Equal(t, path, cmd.Data)
})
t.Run("ExportBIP85", func(t *testing.T) {
path := []byte{0x00, 0x00, 0x00, 0x2C}
cmd := NewCommandExportBIP85(32, path)
assert.Equal(t, uint8(InsExportBIP85), cmd.Ins)
assert.Equal(t, uint8(32), cmd.P1)
assert.Equal(t, path, cmd.Data)
})
t.Run("StoreDataWithOffset", func(t *testing.T) {
data := []byte{0x01, 0x02}
cmd := NewCommandStoreDataWithOffset(0x01, data, 440) // offset 440 -> P2 = 110
assert.Equal(t, uint8(InsStoreData), cmd.Ins)
assert.Equal(t, uint8(0x01), cmd.P1)
assert.Equal(t, uint8(110), cmd.P2)
assert.Equal(t, data, cmd.Data)
})
t.Run("GetChallenge", func(t *testing.T) {
cmd := NewCommandGetChallenge(32)
assert.Equal(t, uint8(InsGetChallenge), cmd.Ins)
assert.Equal(t, uint8(32), cmd.P1)
assert.Empty(t, cmd.Data)
})
t.Run("LoadKeyBIP32", func(t *testing.T) {
keyTLV := []byte{0xA1, 0x03, 0x80, 0x01, 0x01}
cmd := NewCommandLoadKeyBIP32(true, keyTLV)
assert.Equal(t, uint8(0xD0), cmd.Ins)
assert.Equal(t, uint8(P1LoadKeyECExtended), cmd.P1)
assert.Equal(t, keyTLV, cmd.Data)
cmd2 := NewCommandLoadKeyBIP32(false, keyTLV)
assert.Equal(t, uint8(P1LoadKeyEC), cmd2.P1)
})
}
func TestAppVersionParsing(t *testing.T) {
info := &types.ApplicationInfo{}
info.Version = []byte{0x01, 0x00}
assert.Equal(t, uint16(0x0100), info.AppVersion())
assert.Equal(t, "1.0", info.AppVersionString())
info.Version = []byte{0x04, 0x02}
assert.Equal(t, uint16(0x0402), info.AppVersion())
assert.Equal(t, "4.2", info.AppVersionString())
info.Version = []byte{}
assert.Equal(t, uint16(0), info.AppVersion())
}
func TestPINRetries(t *testing.T) {
info := &types.ApplicationInfo{
Version: []byte{0x04, 0x02},
AppStatus: 0x1D, // initialized (0x10) + 13 retries (0x0D)
}
retries, ok := info.PINRetries()
assert.True(t, ok)
assert.Equal(t, uint8(13), retries)
// V3 card should not report PIN retries
info.Version = []byte{0x03, 0x00}
retries, ok = info.PINRetries()
assert.False(t, ok)
assert.Equal(t, uint8(0), retries)
}
// Test that binary.BigEndian encoding is used for mnemonic indices
func TestMnemonicIndexEncoding(t *testing.T) {
// Simulate card response with 3 mnemonic indices
data := make([]byte, 6)
binary.BigEndian.PutUint16(data[0:2], 1234)
binary.BigEndian.PutUint16(data[2:4], 5678)
binary.BigEndian.PutUint16(data[4:6], 9012)
indexes, err := parseMnemonicResponse(data)
require.NoError(t, err)
assert.Equal(t, []int{1234, 5678, 9012}, indexes)
}
func TestCommandSet_PreInitSelect(t *testing.T) {
// Test that selecting an uninitialized card works
// and does not create a secure channel (no public key to derive from)
pubKey := make([]byte, 65)
pubKey[0] = 0x04
// Fill with zeros — GenerateSecret will fail, but pre-init cards
// have empty public keys in practice
// Use empty pub key (pre-init state)
resp := &apdu.Response{
Data: []byte{0x80, 0x00}, // empty public key
Sw: 0x9000,
}
ch := &mockSelectChannel{responses: []*apdu.Response{resp}}
cs := NewCommandSet(ch)
err := cs.Select()
require.NoError(t, err)
assert.False(t, cs.ApplicationInfo.Initialized)
// Pre-init cards have no secure channel capability (empty pub key)
assert.False(t, cs.ApplicationInfo.HasSecureChannelCapability())
}
func TestCommandSet_SendProtected_NotOpen(t *testing.T) {
// When the secure channel is not open, sendProtected should send plaintext
ch := &mockSelectChannel{responses: []*apdu.Response{
{Data: []byte{0x01, 0x02}, Sw: 0x9000},
}}
cs := NewCommandSet(ch)
cs.sc = NewSecureChannelV2(nil, nil)
resp, err := cs.sendProtected(0xC0, 0x01, 0x00, []byte{0xDE, 0xAD})
require.NoError(t, err)
assert.Equal(t, uint16(0x9000), resp.Sw)
assert.Equal(t, []byte{0x01, 0x02}, resp.Data)
}
func TestCommandSet_FactoryReset(t *testing.T) {
ch := &mockSelectChannel{responses: []*apdu.Response{
{Sw: 0x9000},
}}
cs := NewCommandSet(ch)
err := cs.FactoryReset()
require.NoError(t, err)
assert.Equal(t, 1, ch.index)
}
func TestErrNoAvailablePairingSlots(t *testing.T) {
assert.Contains(t, ErrNoAvailablePairingSlots.Error(), "no available pairing slots")
}
func TestErrBadChecksumSize(t *testing.T) {
assert.Contains(t, ErrBadChecksumSize.Error(), "bad checksum size")
}