From bfa89349d869e27dbe3467236ba269bca0a6f148 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Tue, 2 May 2023 21:52:50 +0200 Subject: [PATCH 1/1] openssl: Assume that openssl allocation functions functions succeed. We do the same thing with malloc() and friends, so replace error checking by assertions whenever OOM is the only possible error reason. Also add assertions for EVP_MD_CTX_new(), whose return value was not checked at all. --- openssl.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/openssl.c b/openssl.c index 6dba1b27..495d83c2 100644 --- a/openssl.c +++ b/openssl.c @@ -111,9 +111,7 @@ static int read_public_key(const unsigned char *blob, int blen, BIGNUM *n, *e; const unsigned char *p = blob, *end = blob + blen; - rsa = RSA_new(); - if (!rsa) - return -E_BIGNUM; + assert((rsa = RSA_new())); ret = read_bignum(p, end - p, &e); if (ret < 0) goto free_rsa; @@ -139,11 +137,10 @@ free_rsa: static int read_pem_private_key(const char *path, struct asymmetric_key *priv) { EVP_PKEY *pkey; - BIO *bio = BIO_new(BIO_s_file()); + BIO *bio; + assert((bio = BIO_new(BIO_s_file()))); priv->rsa = NULL; - if (!bio) - return -E_PRIVATE_KEY; if (BIO_read_filename(bio, path) <= 0) goto bio_free; pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); @@ -164,9 +161,7 @@ static int read_openssh_private_key(const unsigned char *blob, BIGNUM *n, *e, *d, *iqmp, *p, *q; /* stored in the key file */ const unsigned char *cp = blob; - rsa = RSA_new(); - if (!rsa) - return -E_BIGNUM; + assert((rsa = RSA_new())); ret = read_bignum(cp, end - cp, &n); if (ret < 0) goto free_rsa; @@ -335,7 +330,7 @@ struct stream_cipher *sc_new(const unsigned char *data, int len) struct stream_cipher *sc = alloc(sizeof(*sc)); assert(len >= 2 * AES_CRT128_BLOCK_SIZE); - sc->aes = EVP_CIPHER_CTX_new(); + assert((sc->aes = EVP_CIPHER_CTX_new())); EVP_EncryptInit_ex(sc->aes, EVP_aes_128_ctr(), NULL, data, data + AES_CRT128_BLOCK_SIZE); return sc; @@ -375,8 +370,11 @@ void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst) void hash_function(const char *data, unsigned long len, unsigned char *hash) { - EVP_MD_CTX *c = EVP_MD_CTX_new(); - int ret = EVP_DigestInit_ex(c, EVP_sha1(), NULL); + int ret; + EVP_MD_CTX *c; + + assert((c = EVP_MD_CTX_new())); + ret = EVP_DigestInit_ex(c, EVP_sha1(), NULL); assert(ret != 0); ret = EVP_DigestUpdate(c, data, len); assert(ret != 0); @@ -387,8 +385,11 @@ void hash_function(const char *data, unsigned long len, unsigned char *hash) void hash2_function(const char *data, unsigned long len, unsigned char *hash) { - EVP_MD_CTX *c = EVP_MD_CTX_new(); - int ret = EVP_DigestInit_ex(c, EVP_sha256(), NULL); + int ret; + EVP_MD_CTX *c; + + assert((c = EVP_MD_CTX_new())); + ret = EVP_DigestInit_ex(c, EVP_sha256(), NULL); assert(ret != 0); ret = EVP_DigestUpdate(c, data, len); assert(ret != 0); -- 2.39.2