-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
307 lines (256 loc) · 8.19 KB
/
auth.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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
package piv
import (
"bytes"
"crypto/des" //nolint:gosec
"errors"
"fmt"
"io"
iso "cunicu.li/go-iso7816"
"cunicu.li/go-iso7816/encoding/tlv"
)
var errFailedToGenerateKey = errors.New("failed to generate random key")
// authenticate attempts to authenticate against the card with the provided
// management key. The management key is required to generate new keys or add
// certificates to slots.
//
// Use DefaultManagementKey if the management key hasn't been set.
func (c *Card) authenticate(key ManagementKey) error {
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=92
// https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=918402#page=114
// Request a witness
resp, err := sendTLV(c.tx, iso.InsGeneralAuthenticate, byte(Alg3DES), keyCardManagement,
tlv.New(0x7c,
tlv.New(0x80),
),
)
if err != nil {
return fmt.Errorf("failed to execute command: %w", err)
}
cardChallenge, _, ok := resp.GetChild(0x7c, 0x80)
if !ok {
return errUnmarshal
} else if len(cardChallenge) != 8 {
return errUnexpectedLength
}
block, err := des.NewTripleDESCipher(key[:]) //nolint:gosec
if err != nil {
return fmt.Errorf("failed to create triple des block cipher: %w", err)
}
cardResponse := make([]byte, 8)
block.Decrypt(cardResponse, cardChallenge)
challenge := make([]byte, 8)
if _, err := io.ReadFull(c.Rand, challenge); err != nil {
return fmt.Errorf("failed to read random data: %w", err)
}
response := make([]byte, 8)
block.Encrypt(response, challenge)
if resp, err = sendTLV(c.tx, iso.InsGeneralAuthenticate, byte(Alg3DES), keyCardManagement,
tlv.New(0x7c,
tlv.New(0x80, cardResponse),
tlv.New(0x81, challenge),
),
); err != nil {
return fmt.Errorf("failed to execute command: %w", err)
}
if cardResponse, _, ok = resp.GetChild(0x7c, 0x82); !ok {
return errUnmarshal
} else if len(cardResponse) != 8 {
return errUnexpectedLength
} else if !bytes.Equal(cardResponse, response) {
return errChallengeFailed
}
return nil
}
// authenticateWithPIN uses a PIN protected management key to authenticate
// https://docs.yubico.com/yesdk/users-manual/application-piv/pin-only.html
// https://docs.yubico.com/yesdk/users-manual/application-piv/piv-objects.html#pinprotecteddata
//
//nolint:unused
func (c *Card) authenticateWithPIN(pin string) error {
ppd, err := c.PinProtectedData(pin)
if err != nil {
return err
}
key, err := ppd.ManagementKey()
if err != nil {
return err
}
return c.authenticate(key)
}
// SetManagementKey updates the management key to a new key. Management keys
// are triple-des keys, however padding isn't verified. To generate a new key,
// generate 24 random bytes.
//
// var newKey ManagementKey
// if _, err := io.ReadFull(rand.Reader, newKey[:]); err != nil {
// // ...
// }
// if err := c.SetManagementKey(piv.DefaultManagementKey, newKey); err != nil {
// // ...
// }
func (c *Card) SetManagementKey(oldKey, newKey ManagementKey) error {
if err := c.authenticate(oldKey); err != nil {
return fmt.Errorf("failed to authenticate with old key: %w", err)
}
p2 := byte(0xff)
touch := false // TODO
if touch {
p2 = 0xfe
}
if _, err := send(c.tx, insSetManagementKey, 0xff, p2, append([]byte{
byte(Alg3DES), keyCardManagement, 24,
}, newKey[:]...)); err != nil {
return fmt.Errorf("failed to execute command: %w", err)
}
return nil
}
// https://docs.yubico.com/yesdk/users-manual/application-piv/pin-only.html
// https://docs.yubico.com/yesdk/users-manual/application-piv/piv-objects.html#pinprotecteddata
func (c *Card) SetManagementKeyPinProtected(oldKey ManagementKey, pin string) error {
var newKey ManagementKey
if n, err := c.Rand.Read(newKey[:]); err != nil {
return fmt.Errorf("failed to generate random key: %w", err)
} else if n != len(DefaultManagementKey) {
return errFailedToGenerateKey
}
ppd, err := c.PinProtectedData(pin)
if err != nil {
return err
}
if err := ppd.SetManagementKey(newKey); err != nil {
return err
}
if err := c.SetPinProtectedData(oldKey, ppd); err != nil {
return err
}
return c.SetManagementKey(oldKey, newKey)
}
// SetPIN updates the PIN to a new value. For compatibility, PINs should be 1-8
// numeric characters.
//
// To generate a new PIN, use the crypto/rand package.
//
// // Generate a 6 character PIN.
// newPINInt, err := rand.Int(rand.Reader, bit.NewInt(1_000_000))
// if err != nil {
// // ...
// }
// // Format with leading zeros.
// newPIN := fmt.Sprintf("%06d", newPINInt)
// if err := c.SetPIN(piv.DefaultPIN, newPIN); err != nil {
// // ...
// }
func (c *Card) SetPIN(oldPIN, newPIN string) error {
oldPINData, err := encodePIN(oldPIN)
if err != nil {
return fmt.Errorf("failed to encode old PIN: %w", err)
}
newPINData, err := encodePIN(newPIN)
if err != nil {
return fmt.Errorf("failed to encode new PIN: %w", err)
}
if _, err = send(c.tx, iso.InsChangeReferenceData, 0, 0x80, append(oldPINData, newPINData...)); err != nil {
return fmt.Errorf("failed to execute command: %w", err)
}
return nil
}
// Unblock unblocks the PIN, setting it to a new value.
func (c *Card) Unblock(puk, newPIN string) error {
pukData, err := encodePIN(puk)
if err != nil {
return fmt.Errorf("failed to encode PUK: %w", err)
}
newPINData, err := encodePIN(newPIN)
if err != nil {
return fmt.Errorf("failed to encode new PIN: %w", err)
}
if _, err = send(c.tx, iso.InsResetRetryCounter, 0, 0x80, append(pukData, newPINData...)); err != nil {
return fmt.Errorf("failed to execute command: %w", err)
}
return nil
}
// SetPUK updates the PUK to a new value. For compatibility, PUKs should be 1-8
// numeric characters.
//
// To generate a new PUK, use the crypto/rand package.
//
// // Generate a 8 character PUK.
// newPUKInt, err := rand.Int(rand.Reader, big.NewInt(100_000_000))
// if err != nil {
// // ...
// }
// // Format with leading zeros.
// newPUK := fmt.Sprintf("%08d", newPUKInt)
// if err := c.SetPUK(piv.DefaultPUK, newPUK); err != nil {
// // ...
// }
func (c *Card) SetPUK(oldPUK, newPUK string) error {
oldPUKData, err := encodePIN(oldPUK)
if err != nil {
return fmt.Errorf("failed to encode old PUK: %w", err)
}
newPUKData, err := encodePIN(newPUK)
if err != nil {
return fmt.Errorf("failed to encode new PUK: %w", err)
}
if _, err = send(c.tx, iso.InsChangeReferenceData, 0, 0x81, append(oldPUKData, newPUKData...)); err != nil {
return fmt.Errorf("failed to execute command: %w", err)
}
return nil
}
func encodePIN(pin string) ([]byte, error) {
data := []byte(pin)
if len(data) == 0 {
return nil, fmt.Errorf("%w: cannot be empty", errInvalidPinLength)
}
if len(data) > 8 {
return nil, fmt.Errorf("%w: longer than 8 bytes", errInvalidPinLength)
}
// Apply padding
for i := len(data); i < 8; i++ {
data = append(data, 0xff)
}
return data, nil
}
// VerifyPIN attempts to authenticate against the card with the provided PIN.
//
// PIN authentication for other operations are handled separately, and VerifyPIN
// does not need to be called before those methods.
//
// After a specific number of authentication attempts with an invalid PIN,
// usually 3, the PIN will become block and refuse further attempts. At that
// point the PUK must be used to unblock the PIN.
//
// Use DefaultPIN if the PIN hasn't been set.
func (c *Card) VerifyPIN(pin string) error {
return login(c.tx, pin)
}
func login(tx *iso.Transaction, pin string) error {
data, err := encodePIN(pin)
if err != nil {
return err
}
// https://csrc.nist.gov/CSRC/media/Publications/sp/800-73/4/archive/2015-05-29/documents/sp800_73-4_pt2_draft.pdf#page=20
if _, err = send(tx, iso.InsVerify, 0, 0x80, data); err != nil {
return fmt.Errorf("failed to execute command: %w", err)
}
return err
}
func loginNeeded(tx *iso.Transaction) bool {
_, err := send(tx, iso.InsVerify, 0, 0x80, nil)
return err != nil
}
// Retries returns the number of attempts remaining to enter the correct PIN.
func (c *Card) Retries() (int, error) {
_, err := send(c.tx, iso.InsVerify, 0, 0x80, nil)
if err == nil {
return 0, fmt.Errorf("%w from empty pin", errExpectedError)
}
var aErr AuthError
if errors.As(err, &aErr) {
return aErr.Retries, nil
}
return 0, fmt.Errorf("invalid response: %w", err)
}