]>
Commit | Line | Data |
---|---|---|
734a462e | 1 | /* |
3c2bdd7d | 2 | * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. |
734a462e RL |
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 | ||
eee5f32f | 10 | #include <openssl/evp.h> |
734a462e RL |
11 | #include "internal/namemap.h" |
12 | #include "testutil.h" | |
13 | ||
14 | #define NAME1 "name1" | |
15 | #define NAME2 "name2" | |
16 | #define ALIAS1 "alias1" | |
a9550b74 | 17 | #define ALIAS1_UC "ALIAS1" |
734a462e | 18 | |
c720fc35 P |
19 | static int test_namemap_empty(void) |
20 | { | |
21 | OSSL_NAMEMAP *nm = NULL; | |
22 | int ok; | |
23 | ||
cd3f8c1b | 24 | ok = TEST_int_eq(ossl_namemap_empty(NULL), 1) |
c720fc35 | 25 | && TEST_ptr(nm = ossl_namemap_new()) |
cd3f8c1b | 26 | && TEST_int_eq(ossl_namemap_empty(nm), 1) |
c720fc35 | 27 | && TEST_int_ne(ossl_namemap_add_name(nm, 0, NAME1), 0) |
cd3f8c1b | 28 | && TEST_int_eq(ossl_namemap_empty(nm), 0); |
c720fc35 P |
29 | ossl_namemap_free(nm); |
30 | return ok; | |
31 | } | |
32 | ||
734a462e RL |
33 | static int test_namemap(OSSL_NAMEMAP *nm) |
34 | { | |
3d83c735 RL |
35 | int num1 = ossl_namemap_add_name(nm, 0, NAME1); |
36 | int num2 = ossl_namemap_add_name(nm, 0, NAME2); | |
37 | int num3 = ossl_namemap_add_name(nm, num1, ALIAS1); | |
38 | int num4 = ossl_namemap_add_name(nm, 0, ALIAS1_UC); | |
734a462e RL |
39 | int check1 = ossl_namemap_name2num(nm, NAME1); |
40 | int check2 = ossl_namemap_name2num(nm, NAME2); | |
41 | int check3 = ossl_namemap_name2num(nm, ALIAS1); | |
a9550b74 | 42 | int check4 = ossl_namemap_name2num(nm, ALIAS1_UC); |
97ba3954 | 43 | int false1 = ossl_namemap_name2num(nm, "cookie"); |
734a462e RL |
44 | |
45 | return TEST_int_ne(num1, 0) | |
46 | && TEST_int_ne(num2, 0) | |
47 | && TEST_int_eq(num1, num3) | |
a9550b74 | 48 | && TEST_int_eq(num3, num4) |
734a462e RL |
49 | && TEST_int_eq(num1, check1) |
50 | && TEST_int_eq(num2, check2) | |
51 | && TEST_int_eq(num3, check3) | |
a9550b74 | 52 | && TEST_int_eq(num4, check4) |
734a462e RL |
53 | && TEST_int_eq(false1, 0); |
54 | } | |
55 | ||
56 | static int test_namemap_independent(void) | |
57 | { | |
58 | OSSL_NAMEMAP *nm = ossl_namemap_new(); | |
c720fc35 | 59 | int ok = TEST_ptr(nm) && test_namemap(nm); |
734a462e RL |
60 | |
61 | ossl_namemap_free(nm); | |
62 | return ok; | |
63 | } | |
64 | ||
65 | static int test_namemap_stored(void) | |
66 | { | |
67 | OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL); | |
68 | ||
c720fc35 | 69 | return TEST_ptr(nm) |
734a462e RL |
70 | && test_namemap(nm); |
71 | } | |
72 | ||
eee5f32f MC |
73 | /* |
74 | * Test that EVP_get_digestbyname() will use the namemap when it can't find | |
75 | * entries in the legacy method database. | |
76 | */ | |
77 | static int test_digestbyname(void) | |
78 | { | |
79 | int id; | |
80 | OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL); | |
81 | const EVP_MD *sha256, *foo; | |
82 | ||
c720fc35 P |
83 | if (!TEST_ptr(nm)) |
84 | return 0; | |
3d83c735 | 85 | id = ossl_namemap_add_name(nm, 0, "SHA256"); |
eee5f32f MC |
86 | if (!TEST_int_ne(id, 0)) |
87 | return 0; | |
3d83c735 | 88 | if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "foo"), id)) |
eee5f32f MC |
89 | return 0; |
90 | ||
91 | sha256 = EVP_get_digestbyname("SHA256"); | |
92 | if (!TEST_ptr(sha256)) | |
93 | return 0; | |
94 | foo = EVP_get_digestbyname("foo"); | |
95 | if (!TEST_ptr_eq(sha256, foo)) | |
96 | return 0; | |
97 | ||
98 | return 1; | |
99 | } | |
100 | ||
101 | /* | |
102 | * Test that EVP_get_cipherbyname() will use the namemap when it can't find | |
103 | * entries in the legacy method database. | |
104 | */ | |
105 | static int test_cipherbyname(void) | |
106 | { | |
107 | int id; | |
108 | OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL); | |
109 | const EVP_CIPHER *aes128, *bar; | |
110 | ||
c720fc35 P |
111 | if (!TEST_ptr(nm)) |
112 | return 0; | |
3d83c735 | 113 | id = ossl_namemap_add_name(nm, 0, "AES-128-CBC"); |
eee5f32f MC |
114 | if (!TEST_int_ne(id, 0)) |
115 | return 0; | |
3d83c735 | 116 | if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "bar"), id)) |
eee5f32f MC |
117 | return 0; |
118 | ||
119 | aes128 = EVP_get_cipherbyname("AES-128-CBC"); | |
120 | if (!TEST_ptr(aes128)) | |
121 | return 0; | |
122 | bar = EVP_get_cipherbyname("bar"); | |
123 | if (!TEST_ptr_eq(aes128, bar)) | |
124 | return 0; | |
125 | ||
126 | return 1; | |
127 | } | |
128 | ||
e4a1d023 RL |
129 | /* |
130 | * Test that EVP_CIPHER_is_a() responds appropriately, even for ciphers that | |
131 | * are entirely legacy. | |
132 | */ | |
133 | static int test_cipher_is_a(void) | |
134 | { | |
135 | EVP_CIPHER *fetched = EVP_CIPHER_fetch(NULL, "AES-256-CCM", NULL); | |
136 | int rv = 1; | |
137 | ||
c720fc35 | 138 | if (!TEST_ptr(fetched)) |
e4a1d023 RL |
139 | return 0; |
140 | if (!TEST_true(EVP_CIPHER_is_a(fetched, "id-aes256-CCM")) | |
141 | || !TEST_false(EVP_CIPHER_is_a(fetched, "AES-128-GCM"))) | |
142 | rv = 0; | |
143 | if (!TEST_true(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-256-GCM")) | |
144 | || !TEST_false(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-128-CCM"))) | |
145 | rv = 0; | |
146 | ||
147 | EVP_CIPHER_free(fetched); | |
148 | return rv; | |
149 | } | |
150 | ||
151 | /* | |
152 | * Test that EVP_MD_is_a() responds appropriately, even for MDs that are | |
153 | * entirely legacy. | |
154 | */ | |
155 | static int test_digest_is_a(void) | |
156 | { | |
157 | EVP_MD *fetched = EVP_MD_fetch(NULL, "SHA2-512", NULL); | |
158 | int rv = 1; | |
159 | ||
c720fc35 | 160 | if (!TEST_ptr(fetched)) |
e4a1d023 RL |
161 | return 0; |
162 | if (!TEST_true(EVP_MD_is_a(fetched, "SHA512")) | |
163 | || !TEST_false(EVP_MD_is_a(fetched, "SHA1"))) | |
164 | rv = 0; | |
165 | if (!TEST_true(EVP_MD_is_a(EVP_sha256(), "SHA2-256")) | |
166 | || !TEST_false(EVP_MD_is_a(EVP_sha256(), "SHA3-256"))) | |
167 | rv = 0; | |
168 | ||
169 | EVP_MD_free(fetched); | |
170 | return rv; | |
171 | } | |
eee5f32f | 172 | |
734a462e RL |
173 | int setup_tests(void) |
174 | { | |
c720fc35 | 175 | ADD_TEST(test_namemap_empty); |
734a462e RL |
176 | ADD_TEST(test_namemap_independent); |
177 | ADD_TEST(test_namemap_stored); | |
eee5f32f MC |
178 | ADD_TEST(test_digestbyname); |
179 | ADD_TEST(test_cipherbyname); | |
e4a1d023 RL |
180 | ADD_TEST(test_digest_is_a); |
181 | ADD_TEST(test_cipher_is_a); | |
734a462e RL |
182 | return 1; |
183 | } |