From: Benjamin Kaduk Date: Fri, 19 Feb 2021 21:20:00 +0000 (-0800) Subject: test_ecpub: test that we can decode the DER we encoded X-Git-Tag: openssl-3.0.0-alpha13~148 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3d364726606424f760211b5015920410ea9c8f0d;p=thirdparty%2Fopenssl.git 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 Reviewed-by: Richard Levitte (Merged from https://github.com/openssl/openssl/pull/14291) --- diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c index 844c8da3117..2195f21a9db 100644 --- a/test/evp_extra_test.c +++ b/test/evp_extra_test.c @@ -2429,6 +2429,11 @@ static int test_ecpub(int idx) 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 @@ static int test_ecpub(int idx) || !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