Skip to content

Commit c58b251

Browse files
authored
Merge pull request #376 from padelsbach/wp-kdf-type-strcmp
Fix KDF type string compare
2 parents 6a12196 + cca72b3 commit c58b251

6 files changed

Lines changed: 104 additions & 2 deletions

File tree

src/wp_dh_exch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ static int wp_dh_set_param_kdf(wp_DhCtx* ctx, const OSSL_PARAM params[])
481481
if (kdf[0] == '\0') {
482482
ctx->kdfType = WP_KDF_NONE;
483483
}
484-
else if (XSTRNCMP(kdf, OSSL_KDF_NAME_X942KDF_ASN1, XSTRLEN(kdf)) == 0) {
484+
else if (XSTRCMP(kdf, OSSL_KDF_NAME_X942KDF_ASN1) == 0) {
485485
/* Only support the non ASN1 variant. */
486486
ctx->kdfType = WP_KDF_X963;
487487
}

src/wp_ecdh_exch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ static int wp_ecdh_set_param_kdf(wp_EcdhCtx* ctx, const OSSL_PARAM params[])
438438
if (kdf[0] == '\0') {
439439
ctx->kdfType = WP_KDF_NONE;
440440
}
441-
else if (XSTRNCMP(kdf, OSSL_KDF_NAME_X942KDF_ASN1, XSTRLEN(kdf)) == 0) {
441+
else if (XSTRCMP(kdf, OSSL_KDF_NAME_X942KDF_ASN1) == 0) {
442442
/* Only support the non ASN1 variant. */
443443
ctx->kdfType = WP_KDF_X963;
444444
}

test/test_dh.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,54 @@ int test_dh_pkey(void *data)
360360
return err;
361361
}
362362

363+
int test_dh_invalid_kdf_strings(void *data)
364+
{
365+
int err = 0;
366+
EVP_PKEY_CTX *ctx = NULL;
367+
EVP_PKEY *key = NULL;
368+
const unsigned char *p = dh_der;
369+
char *invalidKdfs[] = {
370+
(char *)"X",
371+
(char *)"X942",
372+
(char *)"X942KDF",
373+
(char *)"X942KDF-AS"
374+
};
375+
size_t i;
376+
377+
(void)data;
378+
379+
PRINT_MSG("Reject invalid DH KDF type strings");
380+
381+
key = d2i_PrivateKey_ex(EVP_PKEY_DH, NULL, &p, sizeof(dh_der), wpLibCtx,
382+
NULL);
383+
err = key == NULL;
384+
if (err == 0) {
385+
ctx = EVP_PKEY_CTX_new_from_pkey(wpLibCtx, key, NULL);
386+
err = ctx == NULL;
387+
}
388+
if (err == 0) {
389+
err = EVP_PKEY_derive_init(ctx) != 1;
390+
}
391+
for (i = 0; (err == 0) && (i < (sizeof(invalidKdfs) / sizeof(*invalidKdfs)));
392+
i++) {
393+
OSSL_PARAM params[2];
394+
395+
params[0] = OSSL_PARAM_construct_utf8_string(
396+
OSSL_EXCHANGE_PARAM_KDF_TYPE, invalidKdfs[i], 0);
397+
params[1] = OSSL_PARAM_construct_end();
398+
399+
err = EVP_PKEY_CTX_set_params(ctx, params) > 0;
400+
if (err != 0) {
401+
PRINT_ERR_MSG("Accepted invalid DH KDF type: %s", invalidKdfs[i]);
402+
}
403+
}
404+
405+
EVP_PKEY_CTX_free(ctx);
406+
EVP_PKEY_free(key);
407+
408+
return err;
409+
}
410+
363411
int test_dh_decode(void *data)
364412
{
365413
int err = 0;

test/test_ecc.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,56 @@ static int test_ecdh(const unsigned char *privKey, size_t len,
851851
return err;
852852
}
853853

854+
#ifdef WP_HAVE_EC_P256
855+
int test_ecdh_invalid_kdf_strings(void *data)
856+
{
857+
int err = 0;
858+
EVP_PKEY_CTX *ctx = NULL;
859+
EVP_PKEY *key = NULL;
860+
const unsigned char *p = ecc_key_der_256;
861+
char *invalidKdfs[] = {
862+
(char *)"X",
863+
(char *)"X942",
864+
(char *)"X942KDF",
865+
(char *)"X942KDF-AS"
866+
};
867+
size_t i;
868+
869+
(void)data;
870+
871+
PRINT_MSG("Reject invalid ECDH KDF type strings");
872+
873+
key = d2i_PrivateKey_ex(EVP_PKEY_EC, NULL, &p, sizeof(ecc_key_der_256),
874+
wpLibCtx, NULL);
875+
err = key == NULL;
876+
if (err == 0) {
877+
ctx = EVP_PKEY_CTX_new_from_pkey(wpLibCtx, key, NULL);
878+
err = ctx == NULL;
879+
}
880+
if (err == 0) {
881+
err = EVP_PKEY_derive_init(ctx) != 1;
882+
}
883+
for (i = 0; (err == 0) && (i < (sizeof(invalidKdfs) / sizeof(*invalidKdfs)));
884+
i++) {
885+
OSSL_PARAM params[2];
886+
887+
params[0] = OSSL_PARAM_construct_utf8_string(
888+
OSSL_EXCHANGE_PARAM_KDF_TYPE, invalidKdfs[i], 0);
889+
params[1] = OSSL_PARAM_construct_end();
890+
891+
err = EVP_PKEY_CTX_set_params(ctx, params) > 0;
892+
if (err != 0) {
893+
PRINT_ERR_MSG("Accepted invalid ECDH KDF type: %s", invalidKdfs[i]);
894+
}
895+
}
896+
897+
EVP_PKEY_CTX_free(ctx);
898+
EVP_PKEY_free(key);
899+
900+
return err;
901+
}
902+
#endif /* WP_HAVE_EC_P256 */
903+
854904
#ifdef WP_HAVE_EC_P192
855905
int test_ecdh_p192(void *data)
856906
{

test/unit.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ TEST_CASE test_case[] = {
285285
#ifdef WP_HAVE_DH
286286
TEST_DECL(test_dh_pgen_pkey, NULL),
287287
TEST_DECL(test_dh_pkey, NULL),
288+
TEST_DECL(test_dh_invalid_kdf_strings, NULL),
288289
TEST_DECL(test_dh_decode, NULL),
289290
TEST_DECL(test_dh_krb5_keygen, NULL),
290291
#ifndef WOLFPROV_QUICKTEST
@@ -352,6 +353,7 @@ TEST_CASE test_case[] = {
352353
#ifdef WP_HAVE_ECKEYGEN
353354
TEST_DECL(test_ecdh_p256_keygen, NULL),
354355
#endif
356+
TEST_DECL(test_ecdh_invalid_kdf_strings, NULL),
355357
TEST_DECL(test_ecdh_p256, NULL),
356358
#endif
357359
#ifdef WP_HAVE_ECDSA

test/unit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ int test_rsa_null_init(void* data);
285285
#ifdef WP_HAVE_DH
286286
int test_dh_pgen_pkey(void *data);
287287
int test_dh_pkey(void *data);
288+
int test_dh_invalid_kdf_strings(void *data);
288289
int test_dh_decode(void *data);
289290
int test_dh_get_params(void *data);
290291
int test_dh_krb5_keygen(void *data);
@@ -361,6 +362,7 @@ int test_ecdh_p192(void *data);
361362
int test_ecdh_p224(void *data);
362363
#endif /* WP_HAVE_EC_P224 */
363364
#ifdef WP_HAVE_EC_P256
365+
int test_ecdh_invalid_kdf_strings(void *data);
364366
int test_ecdh_p256(void *data);
365367
#endif /* WP_HAVE_EC_P256 */
366368
#ifdef WP_HAVE_EC_P384

0 commit comments

Comments
 (0)