Test EVP_get_[digest|cipher]byname() use the namemap
authorMatt Caswell <matt@openssl.org>
Fri, 1 Nov 2019 16:09:21 +0000 (16:09 +0000)
committerMatt Caswell <matt@openssl.org>
Wed, 6 Nov 2019 10:11:31 +0000 (10:11 +0000)
Following on from the previous commit, we test that if an algorithm has
a provider supplied alias in the namemap then EVP_get_digestbyname() and
EVP_get_cipherbyname() can still find it.

Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/10324)

test/namemap_internal_test.c

index 1aee01ed0bced6667f6e7ec81ff5d62546ff6a53..16bc571265d8925fdb2070b9b8cae2825398e42d 100644 (file)
@@ -7,6 +7,7 @@
  * https://www.openssl.org/source/license.html
  */
 
+#include <openssl/evp.h>
 #include "internal/namemap.h"
 #include "testutil.h"
 
@@ -55,9 +56,64 @@ static int test_namemap_stored(void)
         && test_namemap(nm);
 }
 
+/*
+ * Test that EVP_get_digestbyname() will use the namemap when it can't find
+ * entries in the legacy method database.
+ */
+static int test_digestbyname(void)
+{
+    int id;
+    OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
+    const EVP_MD *sha256, *foo;
+
+    id = ossl_namemap_add(nm, 0, "SHA256");
+    if (!TEST_int_ne(id, 0))
+        return 0;
+    if (!TEST_int_eq(ossl_namemap_add(nm, id, "foo"), id))
+        return 0;
+
+    sha256 = EVP_get_digestbyname("SHA256");
+    if (!TEST_ptr(sha256))
+        return 0;
+    foo = EVP_get_digestbyname("foo");
+    if (!TEST_ptr_eq(sha256, foo))
+        return 0;
+
+    return 1;
+}
+
+/*
+ * Test that EVP_get_cipherbyname() will use the namemap when it can't find
+ * entries in the legacy method database.
+ */
+static int test_cipherbyname(void)
+{
+    int id;
+    OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
+    const EVP_CIPHER *aes128, *bar;
+
+    id = ossl_namemap_add(nm, 0, "AES-128-CBC");
+    if (!TEST_int_ne(id, 0))
+        return 0;
+    if (!TEST_int_eq(ossl_namemap_add(nm, id, "bar"), id))
+        return 0;
+
+    aes128 = EVP_get_cipherbyname("AES-128-CBC");
+    if (!TEST_ptr(aes128))
+        return 0;
+    bar = EVP_get_cipherbyname("bar");
+    if (!TEST_ptr_eq(aes128, bar))
+        return 0;
+
+    return 1;
+}
+
+
 int setup_tests(void)
 {
     ADD_TEST(test_namemap_independent);
     ADD_TEST(test_namemap_stored);
+    ADD_TEST(test_digestbyname);
+    ADD_TEST(test_cipherbyname);
     return 1;
 }