Fix bug in EVP_CIPHER_CTX_get_iv_length()
authorPauli <pauli@openssl.org>
Tue, 26 Jul 2022 01:46:45 +0000 (11:46 +1000)
committerPauli <pauli@openssl.org>
Fri, 19 Aug 2022 10:12:24 +0000 (20:12 +1000)
Out of range values could possibly be returned due to a lack of range checking.
Very unlikely to be exploitable for our provider because sensible values are
returned for all ciphers.

Also fixed the defaulting code so that the cipher's IV length is returned if
the cipher ctx doesn't support getting.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/18995)

crypto/evp/evp_lib.c

index e0ad6cf93e91f6a80a5288efdc1330a783bcf6c3..d88066d0a2b61f41fe8d1e95db109f6eb8b7488f 100644 (file)
@@ -509,12 +509,17 @@ int EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX *ctx)
         size_t v = len;
         OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
 
-        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &v);
-        rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
-        if (rv != EVP_CTRL_RET_UNSUPPORTED) {
-            if (rv <= 0)
+        if (ctx->cipher->get_ctx_params != NULL) {
+            params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN,
+                                                    &v);
+            rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
+            if (rv > 0) {
+                if (OSSL_PARAM_modified(params)
+                        && !OSSL_PARAM_get_int(params, &len))
+                    return -1;
+            } else if (rv != EVP_CTRL_RET_UNSUPPORTED) {
                 return -1;
-            len = (int)v;
+            }
         }
         /* Code below to be removed when legacy support is dropped. */
         else if ((EVP_CIPHER_get_flags(ctx->cipher)