Skip to content

Commit

Permalink
Check the return value of ossl_bio_new_from_core_bio()
Browse files Browse the repository at this point in the history
There are missing checks of its return value in 8 different spots.

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from openssl#17154)
  • Loading branch information
x2018 authored and t8m committed Jan 3, 2022
1 parent 5bea0e2 commit 352a0bc
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 8 deletions.
6 changes: 5 additions & 1 deletion providers/implementations/encode_decode/decode_epki2pki.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ static int epki2pki_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
PKCS8_PRIV_KEY_INFO *p8inf = NULL;
const X509_ALGOR *alg = NULL;
BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
int ok = (asn1_d2i_read_bio(in, &mem) >= 0);
int ok = 0;

if (in == NULL)
return 0;

ok = (asn1_d2i_read_bio(in, &mem) >= 0);
BIO_free(in);

/* We return "empty handed". This is not an error. */
Expand Down
3 changes: 3 additions & 0 deletions providers/implementations/encode_decode/decode_msblob2key.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
void *key = NULL;
int ok = 0;

if (in == NULL)
return 0;

if (BIO_read(in, hdr_buf, 16) != 16) {
ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
goto next;
Expand Down
6 changes: 5 additions & 1 deletion providers/implementations/encode_decode/decode_pem2der.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ static int read_pem(PROV_CTX *provctx, OSSL_CORE_BIO *cin,
unsigned char **data, long *len)
{
BIO *in = ossl_bio_new_from_core_bio(provctx, cin);
int ok = (PEM_read_bio(in, pem_name, pem_header, data, len) > 0);
int ok;

if (in == NULL)
return 0;
ok = (PEM_read_bio(in, pem_name, pem_header, data, len) > 0);

BIO_free(in);
return ok;
Expand Down
3 changes: 3 additions & 0 deletions providers/implementations/encode_decode/decode_pvk2key.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
void *key = NULL;
int ok = 0;

if (in == NULL)
return 0;

ctx->selection = selection;

if ((selection == 0
Expand Down
6 changes: 5 additions & 1 deletion providers/implementations/encode_decode/encode_key2blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ static int write_blob(void *provctx, OSSL_CORE_BIO *cout,
void *data, int len)
{
BIO *out = ossl_bio_new_from_core_bio(provctx, cout);
int ret = BIO_write(out, data, len);
int ret;

if (out == NULL)
return 0;
ret = BIO_write(out, data, len);

BIO_free(out);
return ret;
Expand Down
12 changes: 8 additions & 4 deletions providers/implementations/encode_decode/encode_key2ms.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ static int write_msblob(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout,
EVP_PKEY *pkey, int ispub)
{
BIO *out = ossl_bio_new_from_core_bio(ctx->provctx, cout);
int ret =
ispub ? i2b_PublicKey_bio(out, pkey) : i2b_PrivateKey_bio(out, pkey);
int ret;

if (out == NULL)
return 0;
ret = ispub ? i2b_PublicKey_bio(out, pkey) : i2b_PrivateKey_bio(out, pkey);

BIO_free(out);
return ret;
Expand All @@ -50,14 +53,15 @@ static int write_pvk(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout,
EVP_PKEY *pkey)
{
BIO *out = NULL;
int ret = 0;
int ret;
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);

out = ossl_bio_new_from_core_bio(ctx->provctx, cout);
if (out == NULL)
return 0;
ret = i2b_PVK_bio_ex(out, pkey, ctx->pvk_encr_level,
ossl_pw_pvk_password, &ctx->pwdata, libctx, NULL);
BIO_free(out);

return ret;
}

Expand Down
5 changes: 4 additions & 1 deletion providers/implementations/encode_decode/endecoder_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ int ossl_read_der(PROV_CTX *provctx, OSSL_CORE_BIO *cin, unsigned char **data,
{
BUF_MEM *mem = NULL;
BIO *in = ossl_bio_new_from_core_bio(provctx, cin);
int ok = (asn1_d2i_read_bio(in, &mem) >= 0);
int ok;

if (in == NULL)
return 0;
ok = (asn1_d2i_read_bio(in, &mem) >= 0);
if (ok) {
*data = (unsigned char *)mem->data;
*len = (long)mem->length;
Expand Down

0 comments on commit 352a0bc

Please sign in to comment.