@@ -144,9 +144,16 @@ static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
144
144
* be reflected back in the "original" key.
145
145
*/
146
146
RSA * rsa = (RSA * )EVP_PKEY_get0_RSA (ctx -> pkey );
147
+ int md_size ;
147
148
148
149
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 ) {
150
157
ERR_raise (ERR_LIB_RSA , RSA_R_INVALID_DIGEST_LENGTH );
151
158
return -1 ;
152
159
}
@@ -266,12 +273,18 @@ static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
266
273
*/
267
274
RSA * rsa = (RSA * )EVP_PKEY_get0_RSA (ctx -> pkey );
268
275
size_t rslen ;
276
+ int md_size ;
269
277
270
278
if (rctx -> md ) {
271
279
if (rctx -> pad_mode == RSA_PKCS1_PADDING )
272
280
return RSA_verify (EVP_MD_get_type (rctx -> md ), tbs , tbslen ,
273
281
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 ) {
275
288
ERR_raise (ERR_LIB_RSA , RSA_R_INVALID_DIGEST_LENGTH );
276
289
return -1 ;
277
290
}
@@ -436,6 +449,7 @@ static int check_padding_md(const EVP_MD *md, int padding)
436
449
static int pkey_rsa_ctrl (EVP_PKEY_CTX * ctx , int type , int p1 , void * p2 )
437
450
{
438
451
RSA_PKEY_CTX * rctx = ctx -> data ;
452
+ int md_size ;
439
453
440
454
switch (type ) {
441
455
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)
485
499
ERR_raise (ERR_LIB_RSA , RSA_R_INVALID_PSS_SALTLEN );
486
500
return -2 ;
487
501
}
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
+ }
488
507
if ((p1 == RSA_PSS_SALTLEN_DIGEST
489
- && rctx -> min_saltlen > EVP_MD_get_size ( rctx -> md ) )
508
+ && rctx -> min_saltlen > md_size )
490
509
|| (p1 >= 0 && p1 < rctx -> min_saltlen )) {
491
510
ERR_raise (ERR_LIB_RSA , RSA_R_PSS_SALTLEN_TOO_SMALL );
492
511
return 0 ;
@@ -850,7 +869,7 @@ static int pkey_pss_init(EVP_PKEY_CTX *ctx)
850
869
RSA_PKEY_CTX * rctx = ctx -> data ;
851
870
const EVP_MD * md ;
852
871
const EVP_MD * mgf1md ;
853
- int min_saltlen , max_saltlen ;
872
+ int min_saltlen , max_saltlen , md_size ;
854
873
855
874
/* Should never happen */
856
875
if (!pkey_ctx_is_pss (ctx ))
@@ -864,7 +883,12 @@ static int pkey_pss_init(EVP_PKEY_CTX *ctx)
864
883
return 0 ;
865
884
866
885
/* 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 ;
868
892
if ((RSA_bits (rsa ) & 0x7 ) == 1 )
869
893
max_saltlen -- ;
870
894
if (min_saltlen > max_saltlen ) {
0 commit comments