]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Extend the tests we have for low level RSA/DSA methods to cover EC
authorMatt Caswell <matt@openssl.org>
Tue, 3 Feb 2026 15:26:19 +0000 (15:26 +0000)
committerTomas Mraz <tomas@openssl.org>
Fri, 13 Feb 2026 07:57:58 +0000 (08:57 +0100)
We want to test that if we use a custom EC_KEY_METHOD, then it still works
even when we use a provider.

Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
MergeDate: Fri Feb 13 07:58:17 2026
(Merged from https://github.com/openssl/openssl/pull/29960)

test/evp_extra_test.c

index 0082da1006d5b35e6d8bc8d3dd09b9a90955ed17..d51d46d4c79b4025a6d0660fb0715bd7670ea5bb 100644 (file)
@@ -6440,6 +6440,72 @@ err:
 }
 #endif
 
+#ifndef OPENSSL_NO_EC
+static int (*orig_ec_sign)(int type, const unsigned char *dgst,
+    int dlen, unsigned char *sig,
+    unsigned int *siglen,
+    const BIGNUM *kinv, const BIGNUM *r,
+    EC_KEY *eckey);
+static int (*orig_ec_sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
+    BIGNUM **kinvp, BIGNUM **rp);
+static ECDSA_SIG *(*orig_ec_sign_sig)(const unsigned char *dgst,
+    int dgst_len, const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey);
+static int tst_ec_sign(int type, const unsigned char *dgst,
+    int dlen, unsigned char *sig,
+    unsigned int *siglen,
+    const BIGNUM *kinv, const BIGNUM *r,
+    EC_KEY *eckey)
+{
+    sign_hits++;
+    return orig_ec_sign(type, dgst, dlen, sig, siglen, kinv, r, eckey);
+}
+
+/* Test that a low level EC_KEY method still gets used even with a provider */
+static int test_low_level_ec_method(void)
+{
+    EC_KEY *ec = NULL;
+    const EC_KEY_METHOD *def = EC_KEY_get_default_method();
+    EC_KEY_METHOD *method = EC_KEY_METHOD_new(def);
+    EVP_PKEY *pkey = NULL;
+    int testresult = 0;
+
+    if (nullprov != NULL) {
+        testresult = TEST_skip("Test does not support a non-default library context");
+        goto err;
+    }
+
+    if (!TEST_ptr(method))
+        goto err;
+
+    if (!TEST_ptr(ec = EC_KEY_new_by_curve_name_ex(NULL, NULL, NID_X9_62_prime256v1)))
+        goto err;
+    if (!TEST_true(EC_KEY_generate_key(ec)))
+        goto err;
+
+    EC_KEY_METHOD_get_sign(def, &orig_ec_sign, &orig_ec_sign_setup, &orig_ec_sign_sig);
+    EC_KEY_METHOD_set_sign(method, tst_ec_sign, orig_ec_sign_setup, orig_ec_sign_sig);
+    if (!TEST_true(EC_KEY_set_method(ec, method)))
+        goto err;
+
+    pkey = EVP_PKEY_new();
+    if (!TEST_ptr(pkey))
+        goto err;
+    if (!TEST_int_gt(EVP_PKEY_assign_EC_KEY(pkey, ec), 0))
+        goto err;
+    ec = NULL;
+
+    if (!do_sign_with_method(pkey))
+        goto err;
+
+    testresult = 1;
+err:
+    EC_KEY_free(ec);
+    EVP_PKEY_free(pkey);
+    EC_KEY_METHOD_free(method);
+    return testresult;
+}
+#endif
+
 int setup_tests(void)
 {
     char *config_file = NULL;
@@ -6616,6 +6682,9 @@ int setup_tests(void)
 #ifndef OPENSSL_NO_DSA
     ADD_TEST(test_low_level_dsa_method);
 #endif
+#ifndef OPENSSL_NO_EC
+    ADD_TEST(test_low_level_ec_method);
+#endif
 
     return 1;
 }