-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys_test.go
593 lines (515 loc) · 17.4 KB
/
keys_test.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
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
package yubihsm_test
import (
"bytes"
"context"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"crypto/sha256"
"crypto/sha512"
"fmt"
"io"
"math/big"
"strconv"
"testing"
"github.com/nholstein/yubihsm"
internal "github.com/nholstein/yubihsm/internal"
)
// loadReplayKey creates a [yubihsm.Session] and [yubihsm.KeyPair] using replayed
// yubihsm-connector logs. The returned session is automatically
// authenticated and key loaded by label.
func loadReplayKey(t *testing.T, yubihsmConnectorLog, label string) (context.Context, testConnector, *yubihsm.Session, *yubihsm.KeyPair) {
t.Helper()
ctx, conn, session, _ := loadReplaySession(t, yubihsmConnectorLog)
private, err := session.LoadKeyPair(ctx, conn, label)
if err != nil {
t.Fatalf("session.LoadKeyPair(%q): %v", label, err)
}
return ctx, conn, session, private
}
func TestCryptoPrivateKey(t *testing.T) {
t.Parallel()
_, conn, _, privP256 := loadReplayKey(t, "sign-p256.log", "p256")
conn.flush()
_, conn, _, privEd25519 := loadReplayKey(t, "sign-ed25519.log", "test-key")
conn.flush()
_, conn, _, privRsa := loadReplayKey(t, "sign-rsa2048-pss.log", "test-rsa2048")
conn.flush()
privs := []*yubihsm.KeyPair{privP256, privEd25519, privRsa}
for _, k := range privs {
for _, x := range privs {
if k == x && !k.Equal(x) {
t.Errorf("private key must equal itself")
}
if k != x && k.Equal(x) {
t.Errorf("distinct private keys must not be equal")
}
}
}
// We have three separate private key types, and it's too easy
// to mess up the crypto interface methods on these since it's
// all untyped methods which take [any].
//
// Run a stupid large combination of [Equal] methods across a
// combination of these types to ensure everything works as
// expected. Since the [yubihsm.KeyPair.Equal] method winds up calling
// [crypto.PrivateKey.Public] the [yubihsm.KeyPair.Public] method is
// tested as well.
signerP256 := privP256.AsCryptoSigner(nil, nil, nil)
cryptoP256 := signerP256.(yubihsm.CryptoPrivateKey)
if !cryptoP256.Equal(signerP256) ||
!cryptoP256.Equal(cryptoP256) ||
!cryptoP256.Equal(privP256) ||
!privP256.Equal(signerP256) {
t.Errorf("P256 signing key must equal P256 KeyPair")
}
for _, x := range []*yubihsm.KeyPair{privEd25519, privRsa} {
if cryptoP256.Equal(x) || x.Equal(signerP256) {
t.Errorf("P256 signing key must not be equal")
}
}
decrypterRsa := privRsa.AsCryptoDecrypter(nil, nil, nil)
cryptoRsa := decrypterRsa.(yubihsm.CryptoPrivateKey)
if !cryptoRsa.Equal(decrypterRsa) ||
!cryptoRsa.Equal(cryptoRsa) ||
!cryptoRsa.Equal(privRsa) ||
!privRsa.Equal(decrypterRsa) {
t.Errorf("RSA decrypting key must equal RSA KeyPair")
}
for _, x := range []*yubihsm.KeyPair{privP256, privEd25519} {
if cryptoRsa.Equal(x) || x.Equal(decrypterRsa) {
t.Errorf("RSA decrypting key must not be equal")
}
}
}
func TestKeyECDSASign(t *testing.T) {
t.Parallel()
digest := sha256.Sum256([]byte("test ECDSA message"))
t.Run("yubihsm", func(t *testing.T) {
t.Parallel()
ctx, conn, session, private := loadReplayKey(t, "sign-p256.log", "p256")
signature, err := private.Sign(ctx, conn, session, digest[:], crypto.SHA256)
if err != nil {
t.Errorf("private.Sign(): %v", err)
}
t.Logf("signature: %x", signature)
public := private.Public().(*ecdsa.PublicKey)
if !ecdsa.VerifyASN1(public, digest[:], signature) {
t.Errorf("signature verification failed")
}
})
t.Run("crypto", func(t *testing.T) {
t.Parallel()
ctx, conn, session, private := loadReplayKey(t, "sign-p256.log", "p256")
signer := private.AsCryptoSigner(ctx, conn, session)
signature, err := signer.Sign(nil, digest[:], crypto.SHA256)
if err != nil {
t.Errorf("signer.Sign(): %v", err)
}
t.Logf("signature: %x", signature)
for _, p := range []any{private.Public(), signer.Public()} {
public := p.(*ecdsa.PublicKey)
if !ecdsa.VerifyASN1(public, digest[:], signature) {
t.Errorf("signature verification failed")
}
}
})
t.Run("can't decrypt", func(t *testing.T) {
t.Parallel()
ctx, conn, session, private := loadReplayKey(t, "sign-p256.log", "p256")
_, err := private.Decrypt(ctx, conn, session, []byte("12345"), nil)
if err == nil {
t.Errorf("decrypting using a P-256 key should fail")
}
_, _ = private.Sign(ctx, conn, session, digest[:], crypto.SHA256)
})
}
func TestKeyEd25519Sign(t *testing.T) {
t.Parallel()
message := []byte("test Ed25519 message")
t.Run("yubihsm", func(t *testing.T) {
t.Parallel()
ctx, conn, session, private := loadReplayKey(t, "sign-ed25519.log", "test-key")
t.Run("unsupport Ed25519ph", func(t *testing.T) {
digest := sha512.Sum512(message)
_, err := private.Sign(ctx, conn, session, digest[:], crypto.SHA512)
if err == nil {
t.Errorf("Ed25519ph should fail as unsupported")
}
})
signature, err := private.Sign(ctx, conn, session, message, crypto.Hash(0))
if err != nil {
t.Errorf("private.Sign(): %v", err)
}
t.Logf("signature: %x", signature)
public := private.Public().(ed25519.PublicKey)
if !ed25519.Verify(public, message, signature) {
t.Errorf("signature verification failed")
}
})
t.Run("crypto", func(t *testing.T) {
t.Parallel()
ctx, conn, session, private := loadReplayKey(t, "sign-ed25519.log", "test-key")
signer := private.AsCryptoSigner(ctx, conn, session)
signature, err := signer.Sign(nil, message, crypto.Hash(0))
if err != nil {
t.Errorf("private.Sign(): %v", err)
}
t.Logf("signature: %x", signature)
public := signer.Public().(ed25519.PublicKey)
if !ed25519.Verify(public, message, signature) {
t.Errorf("signature verification failed")
}
})
t.Run("can't decrypt", func(t *testing.T) {
t.Parallel()
ctx, conn, session, private := loadReplayKey(t, "sign-ed25519.log", "test-key")
_, err := private.Decrypt(ctx, conn, session, []byte("12345"), nil)
if err == nil {
t.Errorf("decrypting using a Ed25519 key should fail")
}
_, _ = private.Sign(ctx, conn, session, message, crypto.Hash(0))
})
}
func testKeyRSA(t *testing.T, bits int) {
message := []byte("test plaintext")
hashed := sha256.Sum256([]byte("test RSA message"))
hash := crypto.SHA256
label := fmt.Sprintf("test-rsa%d", bits)
t.Run("sign-pkcs1v15", func(t *testing.T) {
t.Parallel()
log := fmt.Sprintf("sign-rsa%d-pkcs1v15.log", bits)
ctx, conn, session, private := loadReplayKey(t, log, label)
signer := private.AsCryptoSigner(ctx, conn, session)
signature, err := signer.Sign(nil, hashed[:], hash)
if err != nil {
t.Errorf("signer.Sign(): %v", err)
}
for _, p := range []any{private.Public(), signer.Public()} {
public := p.(*rsa.PublicKey)
err = rsa.VerifyPKCS1v15(public, hash, hashed[:], signature)
if err != nil {
t.Errorf("rsa.VerifyPKCS1v15(): %v", err)
}
}
})
t.Run("sign-pss", func(t *testing.T) {
t.Parallel()
log := fmt.Sprintf("sign-rsa%d-pss.log", bits)
ctx, conn, session, private := loadReplayKey(t, log, label)
signer := private.AsCryptoSigner(ctx, conn, session)
for _, saltLength := range []int{
rsa.PSSSaltLengthAuto,
rsa.PSSSaltLengthEqualsHash,
32,
} {
opts := rsa.PSSOptions{
Hash: hash,
SaltLength: saltLength,
}
signature, err := signer.Sign(nil, hashed[:], &opts)
if err != nil {
t.Errorf("signer.Sign(): %v", err)
}
for _, p := range []any{private.Public(), signer.Public()} {
public := p.(*rsa.PublicKey)
err = rsa.VerifyPSS(public, hash, hashed[:], signature, &opts)
if err != nil {
t.Errorf("rsa.VerifyPSS(): %v", err)
}
}
}
})
loadDecryptKey := func(t *testing.T, alg, options string) (crypto.Decrypter, *rsa.PublicKey, io.Reader) {
t.Helper()
// The RSA encryption routines use crypto/randutil.MaybeReadByte
// to add non-determinism when encrypting; even when
// using a deterministic random reader. Since this skips
// randomly skips a byte the solution is to always return
// identical bytes.
rand := bytes.NewReader(make([]byte, 4096))
log := fmt.Sprintf("decrypt-rsa%d-%s-%s.log", bits, alg, options)
ctx, conn, session, private := loadReplayKey(t, log, label)
t.Cleanup(func() {
err := session.Close(ctx, conn)
if err != nil {
t.Errorf("session.Close(): %v", err)
}
})
decrypter := private.AsCryptoDecrypter(ctx, conn, session)
public := decrypter.Public().(*rsa.PublicKey)
t.Logf("decrypter: %#v", decrypter)
if !public.Equal(private.Public()) {
t.Error("mixed up the RSA public keys")
}
return decrypter, public, rand
}
checkDecryption := func(t *testing.T, plaintext []byte, err error) {
t.Helper()
if err != nil {
t.Errorf("decrypter.Decrypt(): %v", err)
}
t.Logf("plaintext: %q", plaintext)
if !bytes.Equal(plaintext, message) {
t.Logf("message: %q", message)
t.Errorf("decryption failed")
}
}
t.Run("decrypt-pkcs1v15", func(t *testing.T) {
loadDecryptKeyPKCS1v15 := func(t *testing.T, options string) (crypto.Decrypter, []byte) {
t.Helper()
decrypter, public, rand := loadDecryptKey(t, "pkcs1v15", options)
ciphertext, err := rsa.EncryptPKCS1v15(rand, public, message)
if err != nil {
t.Helper()
t.Fatalf("rsa.EncryptPKCS1v15(): %v", err)
}
return decrypter, ciphertext
}
t.Run("no PKCS1v15DecryptOptions", func(t *testing.T) {
decrypter, ciphertext := loadDecryptKeyPKCS1v15(t, "no")
plaintext, err := decrypter.Decrypt(nil, ciphertext, nil)
checkDecryption(t, plaintext, err)
})
t.Run("empty PKCS1v15DecryptOptions", func(t *testing.T) {
decrypter, ciphertext := loadDecryptKeyPKCS1v15(t, "empty")
var options rsa.PKCS1v15DecryptOptions
plaintext, err := decrypter.Decrypt(nil, ciphertext, &options)
checkDecryption(t, plaintext, err)
})
t.Run("set PKCS1v15DecryptOptions", func(t *testing.T) {
decrypter, ciphertext := loadDecryptKeyPKCS1v15(t, "set")
options := rsa.PKCS1v15DecryptOptions{
SessionKeyLen: 32,
}
plaintext, err := decrypter.Decrypt(nil, ciphertext, &options)
checkDecryption(t, plaintext, err)
})
t.Run("failed PKCS1v15DecryptOptions", func(t *testing.T) {
decrypter, ciphertext := loadDecryptKeyPKCS1v15(t, "failed")
corrupted := append([]byte{}, ciphertext...)
corrupted[0] ^= 1
options := rsa.PKCS1v15DecryptOptions{
SessionKeyLen: 32,
}
plaintext, err := decrypter.Decrypt(nil, corrupted, &options)
if err != nil {
t.Errorf("decrypter.Decrypt(): %v", err)
}
t.Logf("plaintext: %q", plaintext)
if bytes.Equal(plaintext, make([]byte, options.SessionKeyLen)) {
t.Errorf("returned zeroed plaintext")
} else if bytes.Equal(plaintext, message) {
t.Errorf("should have returned random plaintext")
}
})
//t.Run("detect error", func(t *testing.T) {
// decrypter, ciphertext := loadDecryptKeyPKCS1v15(t, "no")
// plaintext, err := decrypter.Decrypt(nil, ciphertext, nil)
// checkDecryption(t, plaintext, err)
//
// key := decrypter.(*yubihsm.CryptoDecrypter)
// session := yubihsm.Session{session: key.session.session}
// response := sessionResponse{&session, [][]byte{{0x7f, 0, 1, 9}}}
// plaintext, err = key.keyPair.Decrypt(key.ctx, &response, &session, message, nil)
// var pErr internal.Error
// if !errors.As(err, &pErr) || plaintext != nil {
// t.Errorf("should return protocol error")
// }
//})
})
t.Run("decrypt-oaep", func(t *testing.T) {
loadDecryptKeyOAEP := func(t *testing.T, options string) (crypto.Decrypter, []byte) {
t.Helper()
decrypter, public, rand := loadDecryptKey(t, "oaep", options)
ciphertext, err := rsa.EncryptOAEP(crypto.SHA256.New(), rand, public, message, []byte(t.Name()))
if err != nil {
t.Helper()
t.Fatalf("rsa.EncryptOAEP(): %v", err)
}
return decrypter, ciphertext
}
t.Run("simple", func(t *testing.T) {
decrypter, ciphertext := loadDecryptKeyOAEP(t, "simple")
t.Run("decrypt-error", func(t *testing.T) {
_, err := decrypter.Decrypt(nil, ciphertext, &rsa.PSSOptions{})
if err == nil {
t.Errorf("decryption with bad options should fail")
}
})
plaintext, err := decrypter.Decrypt(nil, ciphertext, &rsa.OAEPOptions{
Hash: crypto.SHA256,
Label: []byte(t.Name()),
})
checkDecryption(t, plaintext, err)
})
})
}
func TestKeyRSA(t *testing.T) {
t.Parallel()
for _, bits := range []int{2048, 3072, 4096} {
t.Run(strconv.Itoa(bits), func(t *testing.T) {
t.Parallel()
testKeyRSA(t, bits)
})
}
}
func TestLoadKeyPairErrors(t *testing.T) {
t.Parallel()
checkNoKey := func(t *testing.T, key *yubihsm.KeyPair, err error) {
t.Helper()
if err == nil {
t.Errorf("should have failed")
} else if key != nil {
t.Errorf("public key should be nil")
}
}
t.Run("no key found", func(t *testing.T) {
t.Parallel()
ctx, conn, session := loadSessionResponse(t, internal.CommandListObjects)
key, err := session.LoadKeyPair(ctx, conn, "not-there")
checkNoKey(t, key, err)
})
t.Run("multiple keys found", func(t *testing.T) {
t.Parallel()
ctx, conn, session := loadSessionResponse(t, internal.CommandListObjects,
0x12, 0x34, uint8(internal.TypeAsymmetricKey), 0,
0x56, 0x78, uint8(internal.TypeAsymmetricKey), 1,
)
key, err := session.LoadKeyPair(ctx, conn, "too-many")
checkNoKey(t, key, err)
})
t.Run("get key fails", func(t *testing.T) {
t.Parallel()
ctx, conn, session := loadSessionResponses(t,
makeSessionResponse(internal.CommandListObjects, 0x12, 0x34, uint8(internal.TypeAsymmetricKey), 0),
makeSessionResponse(internal.CommandEcho, 0xff),
makeSessionResponse(0x7f, 9),
)
key, err := session.LoadKeyPair(ctx, conn, "get-fails")
checkNoKey(t, key, err)
})
}
func TestKeyPairCoverage(t *testing.T) {
t.Run("panic on bad key", func(t *testing.T) {
t.Parallel()
defer func() {
p := recover()
if p == nil {
t.Error("should have recovered a panic")
} else {
t.Logf("recovered panic: %v", p)
}
}()
var (
conn yubihsm.HTTPConnector
session yubihsm.Session
private yubihsm.KeyPair
)
_, _ = private.Sign(context.Background(), &conn, &session, []byte("foobar"), nil)
t.Error("should have panicked")
})
t.Run("sign error", func(t *testing.T) {
t.Parallel()
message := []byte("test Ed25519 message")
ctx, conn, session, private := loadReplayKey(t, "sign-ed25519.log", "test-key")
signature, err := private.Sign(ctx, conn, session, message, crypto.MD5)
if signature != nil || err == nil {
t.Fatalf("private.Sign(): should reject unsupported digest")
}
signature, err = private.Sign(ctx, conn, session, message, &ed25519.Options{
Context: "Ed25519ctx is not supported",
})
if signature != nil || err == nil {
t.Fatalf("private.Sign(): should reject Ed25519ctx")
}
t.Log("need to generate a valid signature to replay sign-eddsa command")
_, err = private.Sign(ctx, conn, session, message, crypto.Hash(0))
if err != nil {
t.Fatalf("private.Sign(): %v", err)
}
signature, err = private.Sign(ctx, conn, session, message, crypto.Hash(0))
if signature != nil || err == nil {
t.Errorf("private.Sign() should have failed on an emptied message log")
}
})
t.Run("load error", func(t *testing.T) {
t.Parallel()
message := []byte("test Ed25519 message")
ctx, conn, session, private := loadReplayKey(t, "sign-ed25519.log", "test-key")
_, err := private.Sign(ctx, conn, session, message, crypto.Hash(0))
if err != nil {
t.Fatalf("private.Sign(): %v", err)
}
public, err := session.GetPublicKey(ctx, conn, 256)
if err == nil || public != nil {
t.Errorf("session.getPublicKey() should have failed on an emptied message log")
}
key, err := session.LoadKeyPair(ctx, conn, "P-256")
if err == nil || key != nil {
t.Errorf("session.LoadKeyPair() should have failed on an emptied message log")
}
})
t.Run("RSA PSS options", func(t *testing.T) {
t.Parallel()
bits := 2048
message := []byte("test plaintext")
hashed := sha256.Sum256([]byte("test RSA message"))
hash := crypto.SHA256
log := fmt.Sprintf("sign-rsa%d-pss.log", bits)
label := fmt.Sprintf("test-rsa%d", bits)
ctx, conn, session, private := loadReplayKey(t, log, label)
t.Run("negative salt length", func(t *testing.T) {
signature, err := private.Sign(ctx, conn, session, message, &rsa.PSSOptions{
Hash: crypto.SHA256,
SaltLength: -32,
})
if err == nil || signature != nil {
t.Errorf("negative salt length should cause error")
}
})
t.Run("failed auto salt length", func(t *testing.T) {
// I'm not sure a way to make this fail without cheating?
public := private.Public().(*rsa.PublicKey)
n := public.N
defer func() { public.N = n }()
for _, bad := range []*big.Int{
// Public modulus too small.
big.NewInt(0x7fffffffffffffff),
// Public modulus too large for sign-pss command.
big.NewInt(0).Lsh(big.NewInt(1), 1_000_000),
} {
public.N = bad
signature, err := private.Sign(ctx, conn, session, message, &rsa.PSSOptions{
Hash: crypto.SHA256,
})
if err == nil || signature != nil {
t.Errorf("computed salt length should fail")
}
}
})
t.Run("key still valid", func(t *testing.T) {
signer := private.AsCryptoSigner(ctx, conn, session)
for _, saltLength := range []int{
rsa.PSSSaltLengthAuto,
rsa.PSSSaltLengthEqualsHash,
32,
} {
opts := rsa.PSSOptions{
Hash: hash,
SaltLength: saltLength,
}
signature, err := signer.Sign(nil, hashed[:], &opts)
if err != nil {
t.Errorf("signer.Sign(): %v", err)
}
public := signer.Public().(*rsa.PublicKey)
err = rsa.VerifyPSS(public, hash, hashed[:], signature, &opts)
if err != nil {
t.Errorf("rsa.VerifyPSS(): %v", err)
}
}
})
})
}