Skip to content

Commit

Permalink
rsa: Allow omission of precomputed values in NewPrivateKeyRSA
Browse files Browse the repository at this point in the history
OpenSSL 3.0 and 3.1 required precomputed values for CRT (Dp, Dq, and
Qinv), when the primes (P and Q) are included in the params array.

Since all of them can be derived from N, E, and D, this patch simply
stops passing those values if any of them are missing.

See also: openssl/openssl#22334

Signed-off-by: Daiki Ueno <dueno@redhat.com>
  • Loading branch information
ueno committed Feb 13, 2024
1 parent 98604fd commit e417598
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 20 additions & 4 deletions rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,30 @@ func newRSAKey3(isPriv bool, N, E, D, P, Q, Dp, Dq, Qinv BigInt) (C.GO_EVP_PKEY_
return nil, newOpenSSLError("OSSL_PARAM_BLD_new")
}
defer C.go_openssl_OSSL_PARAM_BLD_free(bld)
var comps = [...]struct {

type bigIntParam struct{
name *C.char
num BigInt
}{
}

comps := make([]bigIntParam, 0, 8)

required := [...]bigIntParam{
{paramRSA_N, N}, {paramRSA_E, E}, {paramRSA_D, D},
{paramRSA_P, P}, {paramRSA_Q, Q},
{paramRSA_Dp, Dp}, {paramRSA_Dq, Dq}, {paramRSA_Qinv, Qinv},
}
comps = append(comps, required[:]...)

// OpenSSL 3.0 and 3.1 required all the precomputed values if
// P and Q are present. See:
// https://github.com/openssl/openssl/pull/22334
if P != nil && Q != nil && Dp != nil && Dq != nil && Qinv != nil {
precomputed := [...]bigIntParam{
{paramRSA_P, P}, {paramRSA_Q, Q},
{paramRSA_Dp, Dp}, {paramRSA_Dq, Dq}, {paramRSA_Qinv, Qinv},
}
comps = append(comps, precomputed[:]...)
}

for _, comp := range comps {
if comp.num == nil {
continue
Expand Down
2 changes: 2 additions & 0 deletions rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ func newRSAKey(t *testing.T, size int) (*openssl.PrivateKeyRSA, *openssl.PublicK
if err != nil {
t.Fatalf("GenerateKeyRSA(%d): %v", size, err)
}
// Exercise omission of precomputed value
Dp = nil
priv, err := openssl.NewPrivateKeyRSA(N, E, D, P, Q, Dp, Dq, Qinv)
if err != nil {
t.Fatalf("NewPrivateKeyRSA(%d): %v", size, err)
Expand Down

0 comments on commit e417598

Please sign in to comment.