Skip to content

Commit 882a387

Browse files
JiangJiast8m
authored andcommitted
rsa/rsa_pmeth.c: Add the checks for the EVP_MD_CTX_get_size()
Add the checks for the return value of EVP_MD_CTX_get_size() before explicitly cast them to size_t to avoid the integer overflow. Fixes: 75d44c0 ("Store digests as EVP_MD instead of a NID.") Signed-off-by: Jiasheng Jiang <[email protected]> Reviewed-by: Neil Horman <[email protected]> Reviewed-by: Tomas Mraz <[email protected]> (Merged from openssl#23953)
1 parent c45ca06 commit 882a387

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

Diff for: crypto/rsa/rsa_pmeth.c

+29-5
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,16 @@ static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
144144
* be reflected back in the "original" key.
145145
*/
146146
RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
147+
int md_size;
147148

148149
if (rctx->md) {
149-
if (tbslen != (size_t)EVP_MD_get_size(rctx->md)) {
150+
md_size = EVP_MD_get_size(rctx->md);
151+
if (md_size <= 0) {
152+
ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
153+
return -1;
154+
}
155+
156+
if (tbslen != (size_t)md_size) {
150157
ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
151158
return -1;
152159
}
@@ -266,12 +273,18 @@ static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
266273
*/
267274
RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
268275
size_t rslen;
276+
int md_size;
269277

270278
if (rctx->md) {
271279
if (rctx->pad_mode == RSA_PKCS1_PADDING)
272280
return RSA_verify(EVP_MD_get_type(rctx->md), tbs, tbslen,
273281
sig, siglen, rsa);
274-
if (tbslen != (size_t)EVP_MD_get_size(rctx->md)) {
282+
md_size = EVP_MD_get_size(rctx->md);
283+
if (md_size <= 0) {
284+
ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
285+
return -1;
286+
}
287+
if (tbslen != (size_t)md_size) {
275288
ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
276289
return -1;
277290
}
@@ -436,6 +449,7 @@ static int check_padding_md(const EVP_MD *md, int padding)
436449
static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
437450
{
438451
RSA_PKEY_CTX *rctx = ctx->data;
452+
int md_size;
439453

440454
switch (type) {
441455
case EVP_PKEY_CTRL_RSA_PADDING:
@@ -485,8 +499,13 @@ static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
485499
ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_SALTLEN);
486500
return -2;
487501
}
502+
md_size = EVP_MD_get_size(rctx->md);
503+
if (md_size <= 0) {
504+
ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
505+
return -2;
506+
}
488507
if ((p1 == RSA_PSS_SALTLEN_DIGEST
489-
&& rctx->min_saltlen > EVP_MD_get_size(rctx->md))
508+
&& rctx->min_saltlen > md_size)
490509
|| (p1 >= 0 && p1 < rctx->min_saltlen)) {
491510
ERR_raise(ERR_LIB_RSA, RSA_R_PSS_SALTLEN_TOO_SMALL);
492511
return 0;
@@ -850,7 +869,7 @@ static int pkey_pss_init(EVP_PKEY_CTX *ctx)
850869
RSA_PKEY_CTX *rctx = ctx->data;
851870
const EVP_MD *md;
852871
const EVP_MD *mgf1md;
853-
int min_saltlen, max_saltlen;
872+
int min_saltlen, max_saltlen, md_size;
854873

855874
/* Should never happen */
856875
if (!pkey_ctx_is_pss(ctx))
@@ -864,7 +883,12 @@ static int pkey_pss_init(EVP_PKEY_CTX *ctx)
864883
return 0;
865884

866885
/* See if minimum salt length exceeds maximum possible */
867-
max_saltlen = RSA_size(rsa) - EVP_MD_get_size(md);
886+
md_size = EVP_MD_get_size(md);
887+
if (md_size <= 0) {
888+
ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
889+
return 0;
890+
}
891+
max_saltlen = RSA_size(rsa) - md_size;
868892
if ((RSA_bits(rsa) & 0x7) == 1)
869893
max_saltlen--;
870894
if (min_saltlen > max_saltlen) {

0 commit comments

Comments
 (0)