forked from keycard-tech/keycard-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.go
371 lines (330 loc) · 7.55 KB
/
commands.go
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
package keycard
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/GridPlus/keycard-go/apdu"
"github.com/GridPlus/keycard-go/derivationpath"
"github.com/GridPlus/keycard-go/globalplatform"
)
const (
InsInit = 0xFE
InsOpenSecureChannel = 0x10
InsMutuallyAuthenticate = 0x11
InsPair = 0x12
InsUnpair = 0x13
InsGetStatus = 0xF2
InsGenerateKey = 0xD4
InsRemoveKey = 0xD3
InsVerifyPIN = 0x20
InsChangePIN = 0x21
InsDeriveKey = 0xD1
InsExportKey = 0xC2
InsExportSeed = 0xC3
InsSign = 0xC0
InsSetPinlessPath = 0xC1
InsExportData = 0xC5
InsLoadKey = 0xD0
InsIdentifyCard = 0x14
InsExportCert = 0x16
P1PairingFirstStep = 0x00
P1PairingFinalStep = 0x01
P1GetStatusApplication = 0x00
P1GetStatusKeyPath = 0x01
P1DeriveKeyFromMaster = 0x00
P1DeriveKeyFromParent = 0x40
P1DeriveKeyFromCurrent = 0x80
P1ChangePinPIN = 0x00
P1ChangePinPUK = 0x01
P1ChangePinPairingSecret = 0x02
P1SignCurrentKey = 0x00
P1SignDerive = 0x01
P1SignDeriveAndMakeCurrent = 0x02
P1SignPinless = 0x03
P1ExportKeyCurrent = 0x00
P1ExportKeyDerive = 0x01
P1ExportKeyDeriveAndMakeCurrent = 0x02
P2ExportKeyPrivateAndPublic = 0x00
P2ExportKeyPublicOnly = 0x01
P1LoadKeySeed = 0x03
SwNoAvailablePairingSlots = 0x6A84
)
func NewCommandInit(data []byte) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsInit,
0,
0,
data,
)
}
func NewCommandPairFirstStep(challenge []byte) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsPair,
P1PairingFirstStep,
0,
challenge,
)
}
func NewCommandPairFinalStep(cryptogramHash []byte) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsPair,
P1PairingFinalStep,
0,
cryptogramHash,
)
}
func NewCommandUnpair(index uint8) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsUnpair,
index,
0,
[]byte{},
)
}
func NewCommandOpenSecureChannel(pairingIndex uint8, pubKey []byte) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsOpenSecureChannel,
pairingIndex,
0,
pubKey,
)
}
func NewCommandMutuallyAuthenticate(data []byte) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsMutuallyAuthenticate,
0,
0,
data,
)
}
func NewCommandGetStatus(p1 uint8) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsGetStatus,
p1,
0,
[]byte{},
)
}
func NewCommandGenerateKey() *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsGenerateKey,
0,
0,
[]byte{},
)
}
func NewCommandRemoveKey() *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsRemoveKey,
0,
0,
[]byte{},
)
}
func NewCommandVerifyPIN(pin string) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsVerifyPIN,
0,
0,
[]byte(pin),
)
}
func NewCommandChangePIN(pin string) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsChangePIN,
P1ChangePinPIN,
0,
[]byte(pin),
)
}
func NewCommandChangePUK(puk string) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsChangePIN,
P1ChangePinPUK,
0,
[]byte(puk),
)
}
func NewCommandChangePairingSecret(secret []byte) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsChangePIN,
P1ChangePinPairingSecret,
0,
secret,
)
}
func NewCommandLoadSeed(seed []byte) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsLoadKey,
P1LoadKeySeed,
0,
seed,
)
}
func NewCommandIdentifyCard(hash []byte) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsIdentifyCard,
0,
0,
hash,
)
}
func NewCommandExportCert() *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsExportCert,
0,
0,
[]byte{},
)
}
func NewCommandDeriveKey(pathStr string) (*apdu.Command, error) {
startingPoint, path, err := derivationpath.Decode(pathStr)
if err != nil {
return nil, err
}
p1, err := derivationP1FromStartingPoint(startingPoint)
if err != nil {
return nil, err
}
data := new(bytes.Buffer)
for _, segment := range path {
if err := binary.Write(data, binary.BigEndian, segment); err != nil {
return nil, err
}
}
return apdu.NewCommand(
globalplatform.ClaGp,
InsDeriveKey,
p1,
0,
data.Bytes(),
), nil
}
// Export a key
// @param {p1}
// 0x00: current key - returns the key that is currently loaded and ready for signing. Does not use derivation path
// 0x01: derive - returns derived key
// 0x02: derive and make current - returns derived key and also sets it to the current key
// @param {p2}
// 0x00: return public and private key pair
// 0x01: return only the public key
// @param {pathStr}
// Derivation path of format "m/x/x/x/x/x", e.g. "m/44'/0'/0'/0/0"
func NewCommandExportKey(p1 uint8, p2 uint8, pathStr string) (*apdu.Command, error) {
startingPoint, path, err := derivationpath.Decode(pathStr)
if err != nil {
return nil, err
}
deriveP1, err := derivationP1FromStartingPoint(startingPoint)
if err != nil {
return nil, err
}
data := new(bytes.Buffer)
for _, segment := range path {
if err := binary.Write(data, binary.BigEndian, segment); err != nil {
return nil, err
}
}
return apdu.NewCommand(
globalplatform.ClaGp,
InsExportKey,
p1|deriveP1,
p2,
data.Bytes(),
), nil
}
func NewCommandSetPinlessPath(pathStr string) (*apdu.Command, error) {
startingPoint, path, err := derivationpath.Decode(pathStr)
if err != nil {
return nil, err
}
if len(path) > 0 && startingPoint != derivationpath.StartingPointMaster {
return nil, fmt.Errorf("pinless path must be set with an absolute path")
}
data := new(bytes.Buffer)
for _, segment := range path {
if err := binary.Write(data, binary.BigEndian, segment); err != nil {
return nil, err
}
}
return apdu.NewCommand(
globalplatform.ClaGp,
InsSetPinlessPath,
0,
0,
data.Bytes(),
), nil
}
func NewCommandSign(data []byte, p1 uint8, pathStr string) (*apdu.Command, error) {
if len(data) != 32 {
return nil, fmt.Errorf("data length must be 32, got %d", len(data))
}
if p1 == P1SignDerive || p1 == P1SignDeriveAndMakeCurrent {
_, path, err := derivationpath.Decode(pathStr)
if err != nil {
return nil, err
}
pathData := new(bytes.Buffer)
for _, segment := range path {
if err := binary.Write(pathData, binary.BigEndian, segment); err != nil {
return nil, err
}
}
data = append(data, pathData.Bytes()...)
}
return apdu.NewCommand(
globalplatform.ClaGp,
InsSign,
p1,
0,
data,
), nil
}
// Internal function. Get the type of starting point for the derivation path.
// Used for both DeriveKey and ExportKey
func derivationP1FromStartingPoint(s derivationpath.StartingPoint) (uint8, error) {
switch s {
case derivationpath.StartingPointMaster:
return P1DeriveKeyFromMaster, nil
case derivationpath.StartingPointParent:
return P1DeriveKeyFromParent, nil
case derivationpath.StartingPointCurrent:
return P1DeriveKeyFromCurrent, nil
default:
return uint8(0), fmt.Errorf("invalid startingPoint %d", s)
}
}
func NewCommandExportSeed() *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsExportSeed,
0,
0,
nil,
)
}
func NewCommandExportData(page uint8) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsExportData,
uint8(page),
0,
nil,
)
}