From 8c7d847e2e6ac6bfded210c19fd8461254bb2be3 Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Thu, 29 Sep 2022 12:10:30 +0100 Subject: [PATCH] Test usage of a custom EVP_CIPHER Test that a custom EVP_CIPHER gets used in EVP_CipherInit_ex() calls. Reviewed-by: Tomas Mraz Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/19300) --- test/evp_extra_test.c | 87 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c index 9e7a41d0ac..ad4d28ee69 100644 --- a/test/evp_extra_test.c +++ b/test/evp_extra_test.c @@ -4290,7 +4290,7 @@ static int test_custom_md_meth(void) * library context in this test. */ if (testctx != NULL) - return 1; + return TEST_skip("Non-default libctx"); custom_md_init_called = custom_md_cleanup_called = 0; @@ -4312,7 +4312,7 @@ static int test_custom_md_meth(void) /* * Initing our custom md and then initing another md should * result in the init and cleanup functions of the custom md - * from being called. + * being called. */ || !TEST_true(EVP_DigestInit_ex(mdctx, tmp, NULL)) || !TEST_true(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL)) @@ -4329,6 +4329,88 @@ static int test_custom_md_meth(void) return testresult; } +typedef struct { + int data; +} custom_ciph_ctx; + +static int custom_ciph_init_called = 0; +static int custom_ciph_cleanup_called = 0; + +static int custom_ciph_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx); + + if (p == NULL) + return 0; + + custom_ciph_init_called++; + return 1; +} + +static int custom_ciph_cleanup(EVP_CIPHER_CTX *ctx) +{ + custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx); + + if (p == NULL) + /* Nothing to do */ + return 1; + + custom_ciph_cleanup_called++; + return 1; +} + +static int test_custom_ciph_meth(void) +{ + EVP_CIPHER_CTX *ciphctx = NULL; + EVP_CIPHER *tmp = NULL; + int testresult = 0; + int nid; + + /* + * We are testing deprecated functions. We don't support a non-default + * library context in this test. + */ + if (testctx != NULL) + return TEST_skip("Non-default libctx"); + + custom_ciph_init_called = custom_ciph_cleanup_called = 0; + + nid = OBJ_create("1.3.6.1.4.1.16604.998866.2", "custom-ciph", "custom-ciph"); + if (!TEST_int_ne(nid, NID_undef)) + goto err; + tmp = EVP_CIPHER_meth_new(nid, 16, 16); + if (!TEST_ptr(tmp)) + goto err; + + if (!TEST_true(EVP_CIPHER_meth_set_init(tmp, custom_ciph_init)) + || !TEST_true(EVP_CIPHER_meth_set_flags(tmp, EVP_CIPH_ALWAYS_CALL_INIT)) + || !TEST_true(EVP_CIPHER_meth_set_cleanup(tmp, custom_ciph_cleanup)) + || !TEST_true(EVP_CIPHER_meth_set_impl_ctx_size(tmp, + sizeof(custom_ciph_ctx)))) + goto err; + + ciphctx = EVP_CIPHER_CTX_new(); + if (!TEST_ptr(ciphctx) + /* + * Initing our custom cipher and then initing another cipher + * should result in the init and cleanup functions of the custom + * cipher being called. + */ + || !TEST_true(EVP_CipherInit_ex(ciphctx, tmp, NULL, NULL, NULL, 1)) + || !TEST_true(EVP_CipherInit_ex(ciphctx, EVP_aes_128_cbc(), NULL, + NULL, NULL, 1)) + || !TEST_int_eq(custom_ciph_init_called, 1) + || !TEST_int_eq(custom_ciph_cleanup_called, 1)) + goto err; + + testresult = 1; + err: + EVP_CIPHER_CTX_free(ciphctx); + EVP_CIPHER_meth_free(tmp); + return testresult; +} + # ifndef OPENSSL_NO_DYNAMIC_ENGINE /* Test we can create a signature keys with an associated ENGINE */ static int test_signatures_with_engine(int tst) @@ -4627,6 +4709,7 @@ int setup_tests(void) ADD_ALL_TESTS(test_custom_pmeth, 12); ADD_TEST(test_evp_md_cipher_meth); ADD_TEST(test_custom_md_meth); + ADD_TEST(test_custom_ciph_meth); # ifndef OPENSSL_NO_DYNAMIC_ENGINE /* Tests only support the default libctx */ -- 2.39.2