Skip to content

Commit 8f96cea

Browse files
committed
fix: Enforcing RSA-PSS salt length during verification
1 parent 4c32a99 commit 8f96cea

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎asymmetric.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ func (v *rsaVerifier) verify(_ context.Context, payload []byte, signature []byte
277277
case RsaPkcs1v15Sha256, RsaPkcs1v15Sha384, RsaPkcs1v15Sha512:
278278
err = rsa.VerifyPKCS1v15(v.publicKey, v.hash, hashed, signature)
279279
case RsaPssSha256, RsaPssSha384, RsaPssSha512:
280-
err = rsa.VerifyPSS(v.publicKey, v.hash, hashed, signature, nil)
280+
err = rsa.VerifyPSS(v.publicKey, v.hash, hashed, signature, &rsa.PSSOptions{
281+
SaltLength: rsa.PSSSaltLengthEqualsHash,
282+
})
281283
}
282284

283285
if err != nil {

‎asymmetric_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ func TestRSAVerifierVerifyPayload(t *testing.T) {
468468
alg SignatureAlgorithm
469469
mutateMessage func(t *testing.T, message []byte) []byte
470470
mutateSignature func(t *testing.T, signature []byte) []byte
471+
pssSaltLength int
471472
shouldError bool
472473
}{
473474
"2048 key with RsaPkcs1v15Sha256": {
@@ -494,6 +495,30 @@ func TestRSAVerifierVerifyPayload(t *testing.T) {
494495
key: pk4096,
495496
alg: RsaPssSha512,
496497
},
498+
"RsaPssSha256 with short salt": {
499+
key: pk2048,
500+
alg: RsaPssSha256,
501+
pssSaltLength: 1,
502+
shouldError: true,
503+
},
504+
"RsaPssSha384 with short salt": {
505+
key: pk3072,
506+
alg: RsaPssSha384,
507+
pssSaltLength: 1,
508+
shouldError: true,
509+
},
510+
"RsaPssSha512 with short salt": {
511+
key: pk4096,
512+
alg: RsaPssSha512,
513+
pssSaltLength: 1,
514+
shouldError: true,
515+
},
516+
"RsaPssSha512 with long salt": {
517+
key: pk4096,
518+
alg: RsaPssSha512,
519+
pssSaltLength: 65,
520+
shouldError: true,
521+
},
497522
"signature is too short": {
498523
shouldError: true,
499524
alg: RsaPssSha256,
@@ -556,6 +581,16 @@ func TestRSAVerifierVerifyPayload(t *testing.T) {
556581
require.NoError(t, err)
557582
require.NotEmpty(t, res)
558583

584+
if tc.pssSaltLength > 0 {
585+
hasher := sig.hash.New()
586+
_, _ = hasher.Write(message)
587+
588+
res, err = rsa.SignPSS(rand.Reader, tc.key, sig.hash, hasher.Sum(nil), &rsa.PSSOptions{
589+
SaltLength: tc.pssSaltLength,
590+
})
591+
require.NoError(t, err)
592+
}
593+
559594
ver, err := newRSAVerifier(&tc.key.PublicKey, tc.alg)
560595
require.NoError(t, err)
561596

0 commit comments

Comments
 (0)