Skip to content

Commit 819afba

Browse files
committed
rsa_pss_compute_saltlen(): Avoid integer overflows and check MD and RSA sizes
Fixes Coverity 1604651 Reviewed-by: Dmitry Belyavskiy <[email protected]> Reviewed-by: Tom Cosgrove <[email protected]> (Merged from openssl/openssl#25085) (cherry picked from commit 217e215)
1 parent 638e8a6 commit 819afba

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

providers/implementations/signature/rsa_sig.c

+19-3
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,29 @@ static int rsa_pss_compute_saltlen(PROV_RSA_CTX *ctx)
208208
* Provide a way to use at most the digest length, so that the default does
209209
* not violate FIPS 186-4. */
210210
if (saltlen == RSA_PSS_SALTLEN_DIGEST) {
211-
saltlen = EVP_MD_get_size(ctx->md);
211+
if ((saltlen = EVP_MD_get_size(ctx->md)) <= 0) {
212+
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
213+
return -1;
214+
}
212215
} else if (saltlen == RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
213216
saltlen = RSA_PSS_SALTLEN_MAX;
214-
saltlenMax = EVP_MD_get_size(ctx->md);
217+
if ((saltlenMax = EVP_MD_get_size(ctx->md)) <= 0) {
218+
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
219+
return -1;
220+
}
215221
}
216222
if (saltlen == RSA_PSS_SALTLEN_MAX || saltlen == RSA_PSS_SALTLEN_AUTO) {
217-
saltlen = RSA_size(ctx->rsa) - EVP_MD_get_size(ctx->md) - 2;
223+
int mdsize, rsasize;
224+
225+
if ((mdsize = EVP_MD_get_size(ctx->md)) <= 0) {
226+
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
227+
return -1;
228+
}
229+
if ((rsasize = RSA_size(ctx->rsa)) <= 2 || rsasize - 2 < mdsize) {
230+
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
231+
return -1;
232+
}
233+
saltlen = rsasize - mdsize - 2;
218234
if ((RSA_bits(ctx->rsa) & 0x7) == 1)
219235
saltlen--;
220236
if (saltlenMax >= 0 && saltlen > saltlenMax)

0 commit comments

Comments
 (0)