test_ecpub: test that we can decode the DER we encoded
authorBenjamin Kaduk <bkaduk@akamai.com>
Fri, 19 Feb 2021 21:20:00 +0000 (13:20 -0800)
committerRichard Levitte <levitte@openssl.org>
Sat, 27 Feb 2021 15:14:09 +0000 (16:14 +0100)
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)

test/evp_extra_test.c

index 844c8da3117ef5dd0c5dd42b45af0e08a23914ce..2195f21a9db6e7c61f988d735b4b0254fcb9dd53 100644 (file)
@@ -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