]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/namemap_internal_test.c
Make sure we use the libctx when creating an EVP_PKEY_CTX in libssl
[thirdparty/openssl.git] / test / namemap_internal_test.c
1 /*
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <openssl/evp.h>
11 #include "internal/namemap.h"
12 #include "testutil.h"
13
14 #define NAME1 "name1"
15 #define NAME2 "name2"
16 #define ALIAS1 "alias1"
17 #define ALIAS1_UC "ALIAS1"
18
19 static int test_namemap(OSSL_NAMEMAP *nm)
20 {
21 int num1 = ossl_namemap_add_name(nm, 0, NAME1);
22 int num2 = ossl_namemap_add_name(nm, 0, NAME2);
23 int num3 = ossl_namemap_add_name(nm, num1, ALIAS1);
24 int num4 = ossl_namemap_add_name(nm, 0, ALIAS1_UC);
25 int check1 = ossl_namemap_name2num(nm, NAME1);
26 int check2 = ossl_namemap_name2num(nm, NAME2);
27 int check3 = ossl_namemap_name2num(nm, ALIAS1);
28 int check4 = ossl_namemap_name2num(nm, ALIAS1_UC);
29 int false1 = ossl_namemap_name2num(nm, "cookie");
30
31 return TEST_int_ne(num1, 0)
32 && TEST_int_ne(num2, 0)
33 && TEST_int_eq(num1, num3)
34 && TEST_int_eq(num3, num4)
35 && TEST_int_eq(num1, check1)
36 && TEST_int_eq(num2, check2)
37 && TEST_int_eq(num3, check3)
38 && TEST_int_eq(num4, check4)
39 && TEST_int_eq(false1, 0);
40 }
41
42 static int test_namemap_independent(void)
43 {
44 OSSL_NAMEMAP *nm = ossl_namemap_new();
45 int ok = nm != NULL && test_namemap(nm);
46
47 ossl_namemap_free(nm);
48 return ok;
49 }
50
51 static int test_namemap_stored(void)
52 {
53 OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
54
55 return nm != NULL
56 && test_namemap(nm);
57 }
58
59 /*
60 * Test that EVP_get_digestbyname() will use the namemap when it can't find
61 * entries in the legacy method database.
62 */
63 static int test_digestbyname(void)
64 {
65 int id;
66 OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
67 const EVP_MD *sha256, *foo;
68
69 id = ossl_namemap_add_name(nm, 0, "SHA256");
70 if (!TEST_int_ne(id, 0))
71 return 0;
72 if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "foo"), id))
73 return 0;
74
75 sha256 = EVP_get_digestbyname("SHA256");
76 if (!TEST_ptr(sha256))
77 return 0;
78 foo = EVP_get_digestbyname("foo");
79 if (!TEST_ptr_eq(sha256, foo))
80 return 0;
81
82 return 1;
83 }
84
85 /*
86 * Test that EVP_get_cipherbyname() will use the namemap when it can't find
87 * entries in the legacy method database.
88 */
89 static int test_cipherbyname(void)
90 {
91 int id;
92 OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
93 const EVP_CIPHER *aes128, *bar;
94
95 id = ossl_namemap_add_name(nm, 0, "AES-128-CBC");
96 if (!TEST_int_ne(id, 0))
97 return 0;
98 if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "bar"), id))
99 return 0;
100
101 aes128 = EVP_get_cipherbyname("AES-128-CBC");
102 if (!TEST_ptr(aes128))
103 return 0;
104 bar = EVP_get_cipherbyname("bar");
105 if (!TEST_ptr_eq(aes128, bar))
106 return 0;
107
108 return 1;
109 }
110
111 /*
112 * Test that EVP_CIPHER_is_a() responds appropriately, even for ciphers that
113 * are entirely legacy.
114 */
115 static int test_cipher_is_a(void)
116 {
117 EVP_CIPHER *fetched = EVP_CIPHER_fetch(NULL, "AES-256-CCM", NULL);
118 int rv = 1;
119
120 if (!TEST_ptr_ne(fetched, NULL))
121 return 0;
122 if (!TEST_true(EVP_CIPHER_is_a(fetched, "id-aes256-CCM"))
123 || !TEST_false(EVP_CIPHER_is_a(fetched, "AES-128-GCM")))
124 rv = 0;
125 if (!TEST_true(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-256-GCM"))
126 || !TEST_false(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-128-CCM")))
127 rv = 0;
128
129 EVP_CIPHER_free(fetched);
130 return rv;
131 }
132
133 /*
134 * Test that EVP_MD_is_a() responds appropriately, even for MDs that are
135 * entirely legacy.
136 */
137 static int test_digest_is_a(void)
138 {
139 EVP_MD *fetched = EVP_MD_fetch(NULL, "SHA2-512", NULL);
140 int rv = 1;
141
142 if (!TEST_ptr_ne(fetched, NULL))
143 return 0;
144 if (!TEST_true(EVP_MD_is_a(fetched, "SHA512"))
145 || !TEST_false(EVP_MD_is_a(fetched, "SHA1")))
146 rv = 0;
147 if (!TEST_true(EVP_MD_is_a(EVP_sha256(), "SHA2-256"))
148 || !TEST_false(EVP_MD_is_a(EVP_sha256(), "SHA3-256")))
149 rv = 0;
150
151 EVP_MD_free(fetched);
152 return rv;
153 }
154
155 int setup_tests(void)
156 {
157 ADD_TEST(test_namemap_independent);
158 ADD_TEST(test_namemap_stored);
159 ADD_TEST(test_digestbyname);
160 ADD_TEST(test_cipherbyname);
161 ADD_TEST(test_digest_is_a);
162 ADD_TEST(test_cipher_is_a);
163 return 1;
164 }