Skip to content

Commit

Permalink
providers/implementations/exchange/kdf_exch.c: Fix kdf_derive()
Browse files Browse the repository at this point in the history
kdf_derive() calls EVP_KDF_derive(), but didn't do enough to adapt its input
buffer length arguments to fit the requirements to call EVP_KDF_derive().

Fixes openssl#18517

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from openssl#18533)

(cherry picked from commit e906eab)
  • Loading branch information
levitte committed Jun 15, 2022
1 parent f68283c commit 0feb138
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions providers/implementations/exchange/kdf_exch.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <openssl/kdf.h>
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/err.h>
#include <openssl/proverr.h>
#include <openssl/params.h>
#include "prov/implementations.h"
#include "prov/provider_ctx.h"
Expand Down Expand Up @@ -92,16 +94,33 @@ static int kdf_derive(void *vpkdfctx, unsigned char *secret, size_t *secretlen,
size_t outlen)
{
PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
size_t kdfsize;
int ret;

if (!ossl_prov_is_running())
return 0;

kdfsize = EVP_KDF_CTX_get_kdf_size(pkdfctx->kdfctx);

if (secret == NULL) {
*secretlen = EVP_KDF_CTX_get_kdf_size(pkdfctx->kdfctx);
*secretlen = kdfsize;
return 1;
}

return EVP_KDF_derive(pkdfctx->kdfctx, secret, outlen, NULL);
if (kdfsize != SIZE_MAX) {
if (outlen < kdfsize) {
ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
return 0;
}
outlen = kdfsize;
}

ret = EVP_KDF_derive(pkdfctx->kdfctx, secret, outlen, NULL);
if (ret <= 0)
return 0;

*secretlen = outlen;
return 1;
}

static void kdf_freectx(void *vpkdfctx)
Expand Down

0 comments on commit 0feb138

Please sign in to comment.