Skip to content

Commit 23985ba

Browse files
slontist8m
authored andcommitted
Fix NULL deference when validating FFC public key.
Fixes CVE-2023-0217 When attempting to do a BN_Copy of params->p there was no NULL check. Since BN_copy does not check for NULL this is a NULL reference. As an aside BN_cmp() does do a NULL check, so there are other checks that fail because a NULL is passed. A more general check for NULL params has been added for both FFC public and private key validation instead. Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <pauli@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.org>
1 parent 67813d8 commit 23985ba

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

crypto/ffc/ffc_key_validate.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ int ossl_ffc_validate_public_key_partial(const FFC_PARAMS *params,
2424
BN_CTX *ctx = NULL;
2525

2626
*ret = 0;
27+
if (params == NULL || pub_key == NULL || params->p == NULL) {
28+
*ret = FFC_ERROR_PASSED_NULL_PARAM;
29+
return 0;
30+
}
31+
2732
ctx = BN_CTX_new_ex(NULL);
2833
if (ctx == NULL)
2934
goto err;
@@ -107,6 +112,10 @@ int ossl_ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv,
107112

108113
*ret = 0;
109114

115+
if (priv == NULL || upper == NULL) {
116+
*ret = FFC_ERROR_PASSED_NULL_PARAM;
117+
goto err;
118+
}
110119
if (BN_cmp(priv, BN_value_one()) < 0) {
111120
*ret |= FFC_ERROR_PRIVKEY_TOO_SMALL;
112121
goto err;

include/internal/ffc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
# define FFC_ERROR_NOT_SUITABLE_GENERATOR 0x08
7777
# define FFC_ERROR_PRIVKEY_TOO_SMALL 0x10
7878
# define FFC_ERROR_PRIVKEY_TOO_LARGE 0x20
79+
# define FFC_ERROR_PASSED_NULL_PARAM 0x40
7980

8081
/*
8182
* Finite field cryptography (FFC) domain parameters are used by DH and DSA.

test/ffc_internal_test.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,27 @@ static int ffc_public_validate_test(void)
510510
if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res)))
511511
goto err;
512512

513+
/* Fail if params is NULL */
514+
if (!TEST_false(ossl_ffc_validate_public_key(NULL, pub, &res)))
515+
goto err;
516+
if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
517+
goto err;
518+
res = -1;
519+
/* Fail if pubkey is NULL */
520+
if (!TEST_false(ossl_ffc_validate_public_key(params, NULL, &res)))
521+
goto err;
522+
if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
523+
goto err;
524+
res = -1;
525+
526+
BN_free(params->p);
527+
params->p = NULL;
528+
/* Fail if params->p is NULL */
529+
if (!TEST_false(ossl_ffc_validate_public_key(params, pub, &res)))
530+
goto err;
531+
if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
532+
goto err;
533+
513534
ret = 1;
514535
err:
515536
DH_free(dh);
@@ -567,6 +588,16 @@ static int ffc_private_validate_test(void)
567588
if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res)))
568589
goto err;
569590

591+
if (!TEST_false(ossl_ffc_validate_private_key(NULL, priv, &res)))
592+
goto err;
593+
if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
594+
goto err;
595+
res = -1;
596+
if (!TEST_false(ossl_ffc_validate_private_key(params->q, NULL, &res)))
597+
goto err;
598+
if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
599+
goto err;
600+
570601
ret = 1;
571602
err:
572603
DH_free(dh);

0 commit comments

Comments
 (0)