evp: fix coverity 1484885 negative integer to size_t conversion
authorPauli <pauli@openssl.org>
Sun, 16 May 2021 23:38:29 +0000 (09:38 +1000)
committerPauli <pauli@openssl.org>
Tue, 18 May 2021 03:24:41 +0000 (13:24 +1000)
Theoretically, the IV length can come back negative which would explode.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/15300)

crypto/evp/p_seal.c

index 36900e035272a03b464e945bfc1c2ebe231e67db..9371d110e9a546b403d5a8ad365989c6b2337c58 100644 (file)
@@ -20,7 +20,7 @@ int EVP_SealInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
                  EVP_PKEY **pubk, int npubk)
 {
     unsigned char key[EVP_MAX_KEY_LENGTH];
-    int i;
+    int i, len;
     int rv = 0;
 
     if (type) {
@@ -34,15 +34,19 @@ int EVP_SealInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
     if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
         return 0;
 
-    if (EVP_CIPHER_CTX_iv_length(ctx)
-            && RAND_bytes(iv, EVP_CIPHER_CTX_iv_length(ctx)) <= 0)
+    len = EVP_CIPHER_CTX_iv_length(ctx);
+    if (len < 0 || RAND_bytes(iv, len) <= 0)
+        goto err;
+
+    len = EVP_CIPHER_CTX_key_length(ctx);
+    if (len < 0)
         goto err;
 
     if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
         goto err;
 
     for (i = 0; i < npubk; i++) {
-        size_t keylen = EVP_CIPHER_CTX_key_length(ctx);
+        size_t keylen = len;
         EVP_PKEY_CTX *pctx = NULL;
 
         if ((pctx = EVP_PKEY_CTX_new(pubk[i], NULL)) == NULL) {