You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
EVP_PKEY_derive() no longer accepts buffers larger than expected output #18517
Commit #6674 changed the behavior of EVP_PKEY_derive() to require the output buffer be the exact size of the output data, throwing KDF_R_WRONG_OUTPUT_BUFFER_SIZE if there is a mismatch. This is a regression against OpenSSL 1.1 and can cause existing software to fail.
If key is not NULL then before the call the keylen parameter should contain the length of the key buffer, if the call is successful the shared secret is written to key and the amount of data written to keylen.
However the code now says:
if (prk_len != (size_t)EVP_MD_size(evp_md)) {
KDFerr(KDF_F_HKDF_EXTRACT, KDF_R_WRONG_OUTPUT_BUFFER_SIZE);
return 0;
}
This does seem to be a bug. For clarity this looks like HKDF only and the check in question looks a little different now to what it looked like when it was first introduced. The check is now here:
Simply changing != to <= would seem sufficient, although we should also have a test for this.
Unfortunately, I do not believe this is sufficient. The new signature for HKDF_derive() has prk_len as an in-variable only, and it is not (and cannot be) updated here to the actual output length.
@mattcaswell was unfortunately looking in the wrong place. The KDF implementation "HKDF" is called with EVP_KDF_derive(), not EVP_PKEY_derive(), and the implementation is behaving correctly according to documentation:
EVP_KDF_derive() processes any parameters in Params and then derives keylen bytes of key material and places it in the key buffer. If the
algorithm produces a fixed amount of output then an error will occur
unless the keylen parameter is equal to that output size, as returned
by EVP_KDF_CTX_get_kdf_size()
EVP_PKEY_derive() calls a KEX implementation, so the spot to look would be this:
Notably, you do find a EVP_KDF_derive() call here... but it seems that the code doesn't do enough to adapt the data to fit the requirements when calling EPV_KDF_derive(), so it ends up calling it with an outlen that's too large.
Activity
mattcaswell commentedon Jun 10, 2022
This does seem to be a bug. For clarity this looks like HKDF only and the check in question looks a little different now to what it looked like when it was first introduced. The check is now here:
openssl/providers/implementations/kdfs/hkdf.c
Lines 441 to 448 in 5f4b3db
Simply changing
!=to<=would seem sufficient, although we should also have a test for this.jeredfloyd commentedon Jun 10, 2022
Unfortunately, I do not believe this is sufficient. The new signature for HKDF_derive() has prk_len as an in-variable only, and it is not (and cannot be) updated here to the actual output length.
levitte commentedon Jun 11, 2022
@mattcaswell was unfortunately looking in the wrong place. The KDF implementation "HKDF" is called with
EVP_KDF_derive(), notEVP_PKEY_derive(), and the implementation is behaving correctly according to documentation:EVP_PKEY_derive()calls a KEX implementation, so the spot to look would be this:openssl/providers/implementations/exchange/kdf_exch.c
Lines 91 to 105 in 926c698
Notably, you do find a
EVP_KDF_derive()call here... but it seems that the code doesn't do enough to adapt the data to fit the requirements when callingEPV_KDF_derive(), so it ends up calling it with anoutlenthat's too large.levitte commentedon Jun 11, 2022
@jeredfloyd, would you mind testing the modification from #18533?
jeredfloyd commentedon Jun 12, 2022
@levitte Thanks! I'll try this patch against the RHEL 9 package and report back; if it works I will also submit it internally as a proposed backport.
levitte commentedon Jun 12, 2022
@jeredfloyd, hold on... that patch isn't quite right
test/evp_test.c: Check too big output buffer sizes in PKEYKDF tests
levitte commentedon Jun 12, 2022
I've added a test in a separate PR, #18536
levitte commentedon Jun 12, 2022
I think I got it right, now
providers/implementations/exchange/kdf_exch.c: Fix kdf_derive()
test/evp_test.c: Check too big output buffer sizes in PKEYKDF tests
providers/implementations/exchange/kdf_exch.c: Fix kdf_derive()
test/evp_test.c: Check too big output buffer sizes in PKEYKDF tests
test/evp_test.c: Check too big output buffer sizes in PKEYKDF tests
providers/implementations/exchange/kdf_exch.c: Fix kdf_derive()