Skip to content

Commit db925ae

Browse files
levittehlandau
authored andcommitted
Make DH_check_pub_key() and DH_generate_key() safer yet
We already check for an excessively large P in DH_generate_key(), but not in DH_check_pub_key(), and none of them check for an excessively large Q. This change adds all the missing excessive size checks of P and Q. It's to be noted that behaviours surrounding excessively sized P and Q differ. DH_check() raises an error on the excessively sized P, but only sets a flag for the excessively sized Q. This behaviour is mimicked in DH_check_pub_key(). Reviewed-by: Tomas Mraz <tomas@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Hugo Landau <hlandau@openssl.org> (Merged from #22518) (cherry picked from commit ddeb4b6)
1 parent b37871c commit db925ae

File tree

7 files changed

+33
-6
lines changed

7 files changed

+33
-6
lines changed

crypto/dh/dh_check.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,18 @@ int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
249249
*/
250250
int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
251251
{
252+
/* Don't do any checks at all with an excessively large modulus */
253+
if (BN_num_bits(dh->params.p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) {
254+
ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
255+
*ret = DH_MODULUS_TOO_LARGE | DH_CHECK_PUBKEY_INVALID;
256+
return 0;
257+
}
258+
259+
if (dh->params.q != NULL && BN_ucmp(dh->params.p, dh->params.q) < 0) {
260+
*ret |= DH_CHECK_INVALID_Q_VALUE | DH_CHECK_PUBKEY_INVALID;
261+
return 1;
262+
}
263+
252264
return ossl_ffc_validate_public_key(&dh->params, pub_key, ret);
253265
}
254266

crypto/dh/dh_err.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Generated by util/mkerr.pl DO NOT EDIT
3-
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3+
* Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
44
*
55
* Licensed under the Apache License 2.0 (the "License"). You may not use
66
* this file except in compliance with the License. You can obtain a copy
@@ -54,6 +54,7 @@ static const ERR_STRING_DATA DH_str_reasons[] = {
5454
{ERR_PACK(ERR_LIB_DH, 0, DH_R_PARAMETER_ENCODING_ERROR),
5555
"parameter encoding error"},
5656
{ERR_PACK(ERR_LIB_DH, 0, DH_R_PEER_KEY_ERROR), "peer key error"},
57+
{ERR_PACK(ERR_LIB_DH, 0, DH_R_Q_TOO_LARGE), "q too large"},
5758
{ERR_PACK(ERR_LIB_DH, 0, DH_R_SHARED_INFO_ERROR), "shared info error"},
5859
{ERR_PACK(ERR_LIB_DH, 0, DH_R_UNABLE_TO_CHECK_GENERATOR),
5960
"unable to check generator"},

crypto/dh/dh_key.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ int ossl_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
4949
goto err;
5050
}
5151

52+
if (dh->params.q != NULL
53+
&& BN_num_bits(dh->params.q) > OPENSSL_DH_MAX_MODULUS_BITS) {
54+
ERR_raise(ERR_LIB_DH, DH_R_Q_TOO_LARGE);
55+
goto err;
56+
}
57+
5258
if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
5359
ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
5460
return 0;
@@ -267,6 +273,12 @@ static int generate_key(DH *dh)
267273
return 0;
268274
}
269275

276+
if (dh->params.q != NULL
277+
&& BN_num_bits(dh->params.q) > OPENSSL_DH_MAX_MODULUS_BITS) {
278+
ERR_raise(ERR_LIB_DH, DH_R_Q_TOO_LARGE);
279+
return 0;
280+
}
281+
270282
if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
271283
ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
272284
return 0;

crypto/err/openssl.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ DH_R_NO_PARAMETERS_SET:107:no parameters set
500500
DH_R_NO_PRIVATE_VALUE:100:no private value
501501
DH_R_PARAMETER_ENCODING_ERROR:105:parameter encoding error
502502
DH_R_PEER_KEY_ERROR:111:peer key error
503+
DH_R_Q_TOO_LARGE:130:q too large
503504
DH_R_SHARED_INFO_ERROR:113:shared info error
504505
DH_R_UNABLE_TO_CHECK_GENERATOR:121:unable to check generator
505506
DSA_R_BAD_FFC_PARAMETERS:114:bad ffc parameters

include/crypto/dherr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Generated by util/mkerr.pl DO NOT EDIT
3-
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3+
* Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
44
*
55
* Licensed under the Apache License 2.0 (the "License"). You may not use
66
* this file except in compliance with the License. You can obtain a copy

include/openssl/dh.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ DECLARE_ASN1_ITEM(DHparams)
141141
# define DH_GENERATOR_3 3
142142
# define DH_GENERATOR_5 5
143143

144-
/* DH_check error codes */
144+
/* DH_check error codes, some of them shared with DH_check_pub_key */
145145
/*
146146
* NB: These values must align with the equivalently named macros in
147147
* internal/ffc.h.
@@ -151,10 +151,10 @@ DECLARE_ASN1_ITEM(DHparams)
151151
# define DH_UNABLE_TO_CHECK_GENERATOR 0x04
152152
# define DH_NOT_SUITABLE_GENERATOR 0x08
153153
# define DH_CHECK_Q_NOT_PRIME 0x10
154-
# define DH_CHECK_INVALID_Q_VALUE 0x20
154+
# define DH_CHECK_INVALID_Q_VALUE 0x20 /* +DH_check_pub_key */
155155
# define DH_CHECK_INVALID_J_VALUE 0x40
156156
# define DH_MODULUS_TOO_SMALL 0x80
157-
# define DH_MODULUS_TOO_LARGE 0x100
157+
# define DH_MODULUS_TOO_LARGE 0x100 /* +DH_check_pub_key */
158158

159159
/* DH_check_pub_key error codes */
160160
# define DH_CHECK_PUBKEY_TOO_SMALL 0x01

include/openssl/dherr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Generated by util/mkerr.pl DO NOT EDIT
3-
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3+
* Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
44
*
55
* Licensed under the Apache License 2.0 (the "License"). You may not use
66
* this file except in compliance with the License. You can obtain a copy
@@ -50,6 +50,7 @@
5050
# define DH_R_NO_PRIVATE_VALUE 100
5151
# define DH_R_PARAMETER_ENCODING_ERROR 105
5252
# define DH_R_PEER_KEY_ERROR 111
53+
# define DH_R_Q_TOO_LARGE 130
5354
# define DH_R_SHARED_INFO_ERROR 113
5455
# define DH_R_UNABLE_TO_CHECK_GENERATOR 121
5556

0 commit comments

Comments
 (0)