Clarify |RSA_verify_raw| error handling & cleanup.

Use the common pattern of returning early instead of |goto err;| when
there's no cleanup to do yet. Also, move the error checking of
|BN_CTX_get| failure closer to the the calls to |BN_CTX_get|. Avoid
calling |OPENSSL_cleanse| on public data. Clarify when/why |buf| is not
freed.

Change-Id: I9df833db7eb7041c5af9349c461297372b988f98
Reviewed-on: https://boringssl-review.googlesource.com/7464
Reviewed-by: David Benjamin <davidben@google.com>
This commit is contained in:
Brian Smith 2016-03-04 13:42:47 -10:00 committed by David Benjamin
parent 9902262af6
commit 2a92031bb4

View File

@ -435,10 +435,7 @@ int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
const unsigned rsa_size = RSA_size(rsa); const unsigned rsa_size = RSA_size(rsa);
BIGNUM *f, *result; BIGNUM *f, *result;
int ret = 0;
int r = -1; int r = -1;
uint8_t *buf = NULL;
BN_CTX *ctx = NULL;
if (max_out < rsa_size) { if (max_out < rsa_size) {
OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL); OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
@ -454,14 +451,22 @@ int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
return 0; return 0;
} }
ctx = BN_CTX_new(); BN_CTX *ctx = BN_CTX_new();
if (ctx == NULL) { if (ctx == NULL) {
goto err; return 0;
} }
int ret = 0;
uint8_t *buf = NULL;
BN_CTX_start(ctx); BN_CTX_start(ctx);
f = BN_CTX_get(ctx); f = BN_CTX_get(ctx);
result = BN_CTX_get(ctx); result = BN_CTX_get(ctx);
if (f == NULL || result == NULL) {
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
goto err;
}
if (padding == RSA_NO_PADDING) { if (padding == RSA_NO_PADDING) {
buf = out; buf = out;
} else { } else {
@ -472,10 +477,6 @@ int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
goto err; goto err;
} }
} }
if (!f || !result) {
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
goto err;
}
if (BN_bin2bn(in, in_len, f) == NULL) { if (BN_bin2bn(in, in_len, f) == NULL) {
goto err; goto err;
@ -516,12 +517,9 @@ int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
} }
err: err:
if (ctx != NULL) {
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
} if (buf != out) {
if (padding != RSA_NO_PADDING && buf != NULL) {
OPENSSL_cleanse(buf, rsa_size);
OPENSSL_free(buf); OPENSSL_free(buf);
} }
return ret; return ret;