test_ecpub: test that we can decode the DER we encoded
We should be able to round-trip through the encoded DER form of the
EC public key and get back something that compares as equal to the
original key.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14291)
diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c
index 844c8da..2195f21 100644
--- a/test/evp_extra_test.c
+++ b/test/evp_extra_test.c
@@ -2429,6 +2429,11 @@
unsigned char *p;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *ctx = NULL;
+# ifndef OPENSSL_NO_DEPRECATED_3_0
+ const unsigned char *q;
+ EVP_PKEY *pkey2 = NULL;
+ EC_KEY *ec = NULL;
+# endif
nid = ecpub_nids[idx];
@@ -2449,11 +2454,31 @@
|| !TEST_int_eq(len, savelen))
goto done;
+# ifndef OPENSSL_NO_DEPRECATED_3_0
+ /* Now try to decode the just-created DER. */
+ q = buf;
+ if (!TEST_ptr((pkey2 = EVP_PKEY_new()))
+ || !TEST_ptr((ec = EC_KEY_new_by_curve_name(nid)))
+ || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey2, ec)))
+ goto done;
+ /* EC_KEY ownership transferred */
+ ec = NULL;
+ if (!TEST_ptr(d2i_PublicKey(EVP_PKEY_EC, &pkey2, &q, savelen)))
+ goto done;
+ /* The keys should match. */
+ if (!TEST_int_eq(EVP_PKEY_cmp(pkey, pkey2), 1))
+ goto done;
+# endif
+
ret = 1;
done:
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(pkey);
+# ifndef OPENSSL_NO_DEPRECATED_3_0
+ EVP_PKEY_free(pkey2);
+ EC_KEY_free(ec);
+# endif
return ret;
}
#endif