| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-09-05 15:35:49 +08:00
										 |  |  |  * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							|  |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-02 00:09:21 +08:00
										 |  |  | #include <openssl/evp.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  | #include "internal/namemap.h"
 | 
					
						
							|  |  |  | #include "testutil.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define NAME1 "name1"
 | 
					
						
							|  |  |  | #define NAME2 "name2"
 | 
					
						
							|  |  |  | #define ALIAS1 "alias1"
 | 
					
						
							| 
									
										
										
										
											2019-06-05 14:59:13 +08:00
										 |  |  | #define ALIAS1_UC "ALIAS1"
 | 
					
						
							| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-18 09:01:08 +08:00
										 |  |  | static int test_namemap_empty(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     OSSL_NAMEMAP *nm = NULL; | 
					
						
							|  |  |  |     int ok; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 04:31:56 +08:00
										 |  |  |     ok = TEST_int_eq(ossl_namemap_empty(NULL), 1) | 
					
						
							| 
									
										
										
										
											2024-05-27 22:50:05 +08:00
										 |  |  |          && TEST_ptr(nm = ossl_namemap_new(NULL)) | 
					
						
							| 
									
										
										
										
											2021-02-19 04:31:56 +08:00
										 |  |  |          && TEST_int_eq(ossl_namemap_empty(nm), 1) | 
					
						
							| 
									
										
										
										
											2020-06-18 09:01:08 +08:00
										 |  |  |          && TEST_int_ne(ossl_namemap_add_name(nm, 0, NAME1), 0) | 
					
						
							| 
									
										
										
										
											2021-02-19 04:31:56 +08:00
										 |  |  |          && TEST_int_eq(ossl_namemap_empty(nm), 0); | 
					
						
							| 
									
										
										
										
											2020-06-18 09:01:08 +08:00
										 |  |  |     ossl_namemap_free(nm); | 
					
						
							|  |  |  |     return ok; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  | static int test_namemap(OSSL_NAMEMAP *nm) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-09 07:18:05 +08:00
										 |  |  |     int num1 = ossl_namemap_add_name(nm, 0, NAME1); | 
					
						
							|  |  |  |     int num2 = ossl_namemap_add_name(nm, 0, NAME2); | 
					
						
							|  |  |  |     int num3 = ossl_namemap_add_name(nm, num1, ALIAS1); | 
					
						
							|  |  |  |     int num4 = ossl_namemap_add_name(nm, 0, ALIAS1_UC); | 
					
						
							| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  |     int check1 = ossl_namemap_name2num(nm, NAME1); | 
					
						
							|  |  |  |     int check2 = ossl_namemap_name2num(nm, NAME2); | 
					
						
							|  |  |  |     int check3 = ossl_namemap_name2num(nm, ALIAS1); | 
					
						
							| 
									
										
										
										
											2019-06-05 14:59:13 +08:00
										 |  |  |     int check4 = ossl_namemap_name2num(nm, ALIAS1_UC); | 
					
						
							| 
									
										
										
										
											2019-12-11 03:38:09 +08:00
										 |  |  |     int false1 = ossl_namemap_name2num(nm, "cookie"); | 
					
						
							| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return TEST_int_ne(num1, 0) | 
					
						
							|  |  |  |         && TEST_int_ne(num2, 0) | 
					
						
							|  |  |  |         && TEST_int_eq(num1, num3) | 
					
						
							| 
									
										
										
										
											2019-06-05 14:59:13 +08:00
										 |  |  |         && TEST_int_eq(num3, num4) | 
					
						
							| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  |         && TEST_int_eq(num1, check1) | 
					
						
							|  |  |  |         && TEST_int_eq(num2, check2) | 
					
						
							|  |  |  |         && TEST_int_eq(num3, check3) | 
					
						
							| 
									
										
										
										
											2019-06-05 14:59:13 +08:00
										 |  |  |         && TEST_int_eq(num4, check4) | 
					
						
							| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  |         && TEST_int_eq(false1, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int test_namemap_independent(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-05-27 22:50:05 +08:00
										 |  |  |     OSSL_NAMEMAP *nm = ossl_namemap_new(NULL); | 
					
						
							| 
									
										
										
										
											2020-06-18 09:01:08 +08:00
										 |  |  |     int ok = TEST_ptr(nm) && test_namemap(nm); | 
					
						
							| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ossl_namemap_free(nm); | 
					
						
							|  |  |  |     return ok; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int test_namemap_stored(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-18 09:01:08 +08:00
										 |  |  |     return TEST_ptr(nm) | 
					
						
							| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  |         && test_namemap(nm); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-02 00:09:21 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * 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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-18 09:01:08 +08:00
										 |  |  |     if (!TEST_ptr(nm)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2019-11-09 07:18:05 +08:00
										 |  |  |     id = ossl_namemap_add_name(nm, 0, "SHA256"); | 
					
						
							| 
									
										
										
										
											2019-11-02 00:09:21 +08:00
										 |  |  |     if (!TEST_int_ne(id, 0)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2019-11-09 07:18:05 +08:00
										 |  |  |     if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "foo"), id)) | 
					
						
							| 
									
										
										
										
											2019-11-02 00:09:21 +08:00
										 |  |  |         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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-18 09:01:08 +08:00
										 |  |  |     if (!TEST_ptr(nm)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2019-11-09 07:18:05 +08:00
										 |  |  |     id = ossl_namemap_add_name(nm, 0, "AES-128-CBC"); | 
					
						
							| 
									
										
										
										
											2019-11-02 00:09:21 +08:00
										 |  |  |     if (!TEST_int_ne(id, 0)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2019-11-09 07:18:05 +08:00
										 |  |  |     if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "bar"), id)) | 
					
						
							| 
									
										
										
										
											2019-11-02 00:09:21 +08:00
										 |  |  |         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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-15 08:04:37 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Test that EVP_CIPHER_is_a() responds appropriately, even for ciphers that | 
					
						
							|  |  |  |  * are entirely legacy. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int test_cipher_is_a(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     EVP_CIPHER *fetched = EVP_CIPHER_fetch(NULL, "AES-256-CCM", NULL); | 
					
						
							|  |  |  |     int rv = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-18 09:01:08 +08:00
										 |  |  |     if (!TEST_ptr(fetched)) | 
					
						
							| 
									
										
										
										
											2020-01-15 08:04:37 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     if (!TEST_true(EVP_CIPHER_is_a(fetched, "id-aes256-CCM")) | 
					
						
							|  |  |  |         || !TEST_false(EVP_CIPHER_is_a(fetched, "AES-128-GCM"))) | 
					
						
							|  |  |  |         rv = 0; | 
					
						
							|  |  |  |     if (!TEST_true(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-256-GCM")) | 
					
						
							|  |  |  |         || !TEST_false(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-128-CCM"))) | 
					
						
							|  |  |  |         rv = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EVP_CIPHER_free(fetched); | 
					
						
							|  |  |  |     return rv; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Test that EVP_MD_is_a() responds appropriately, even for MDs that are | 
					
						
							|  |  |  |  * entirely legacy. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int test_digest_is_a(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     EVP_MD *fetched = EVP_MD_fetch(NULL, "SHA2-512", NULL); | 
					
						
							|  |  |  |     int rv = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-18 09:01:08 +08:00
										 |  |  |     if (!TEST_ptr(fetched)) | 
					
						
							| 
									
										
										
										
											2020-01-15 08:04:37 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     if (!TEST_true(EVP_MD_is_a(fetched, "SHA512")) | 
					
						
							|  |  |  |         || !TEST_false(EVP_MD_is_a(fetched, "SHA1"))) | 
					
						
							|  |  |  |         rv = 0; | 
					
						
							|  |  |  |     if (!TEST_true(EVP_MD_is_a(EVP_sha256(), "SHA2-256")) | 
					
						
							|  |  |  |         || !TEST_false(EVP_MD_is_a(EVP_sha256(), "SHA3-256"))) | 
					
						
							|  |  |  |         rv = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EVP_MD_free(fetched); | 
					
						
							|  |  |  |     return rv; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-11-02 00:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  | int setup_tests(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-18 09:01:08 +08:00
										 |  |  |     ADD_TEST(test_namemap_empty); | 
					
						
							| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  |     ADD_TEST(test_namemap_independent); | 
					
						
							|  |  |  |     ADD_TEST(test_namemap_stored); | 
					
						
							| 
									
										
										
										
											2019-11-02 00:09:21 +08:00
										 |  |  |     ADD_TEST(test_digestbyname); | 
					
						
							|  |  |  |     ADD_TEST(test_cipherbyname); | 
					
						
							| 
									
										
										
										
											2020-01-15 08:04:37 +08:00
										 |  |  |     ADD_TEST(test_digest_is_a); | 
					
						
							|  |  |  |     ADD_TEST(test_cipher_is_a); | 
					
						
							| 
									
										
										
										
											2019-05-23 22:00:05 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } |