1 /* 2 * Copyright 2021-2024 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 <sys/stat.h> 11 #include <openssl/evp.h> 12 #include <openssl/conf.h> 13 #include "testutil.h" 14 15 static char *configfile = NULL; 16 static char *recurseconfigfile = NULL; 17 static char *pathedconfig = NULL; 18 19 /* 20 * Test to make sure there are no leaks or failures from loading the config 21 * file twice. 22 */ 23 static int test_double_config(void) 24 { 25 OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); 26 int testresult = 0; 27 EVP_MD *sha256 = NULL; 28 29 if (!TEST_ptr(configfile)) 30 return 0; 31 if (!TEST_ptr(ctx)) 32 return 0; 33 34 if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile))) 35 return 0; 36 if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile))) 37 return 0; 38 39 /* Check we can actually fetch something */ 40 sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL); 41 if (!TEST_ptr(sha256)) 42 goto err; 43 44 testresult = 1; 45 err: 46 EVP_MD_free(sha256); 47 OSSL_LIB_CTX_free(ctx); 48 return testresult; 49 } 50 51 static int test_recursive_config(void) 52 { 53 OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); 54 int testresult = 0; 55 unsigned long err; 56 57 if (!TEST_ptr(recurseconfigfile)) 58 goto err; 59 60 if (!TEST_ptr(ctx)) 61 goto err; 62 63 if (!TEST_false(OSSL_LIB_CTX_load_config(ctx, recurseconfigfile))) 64 goto err; 65 66 err = ERR_peek_error(); 67 /* We expect to get a recursion error here */ 68 if (ERR_GET_REASON(err) == CONF_R_RECURSIVE_SECTION_REFERENCE) 69 testresult = 1; 70 err: 71 OSSL_LIB_CTX_free(ctx); 72 return testresult; 73 } 74 75 #define P_TEST_PATH "/../test/p_test.so" 76 static int test_path_config(void) 77 { 78 OSSL_LIB_CTX *ctx = NULL; 79 OSSL_PROVIDER *prov; 80 int testresult = 0; 81 struct stat sbuf; 82 char *module_path = getenv("OPENSSL_MODULES"); 83 char *full_path = NULL; 84 int rc; 85 86 if (!TEST_ptr(module_path)) 87 return 0; 88 89 full_path = OPENSSL_zalloc(strlen(module_path) + strlen(P_TEST_PATH) + 1); 90 if (!TEST_ptr(full_path)) 91 return 0; 92 93 strcpy(full_path, module_path); 94 full_path = strcat(full_path, P_TEST_PATH); 95 TEST_info("full path is %s", full_path); 96 rc = stat(full_path, &sbuf); 97 OPENSSL_free(full_path); 98 if (rc == -1) 99 return TEST_skip("Skipping modulepath test as provider not present"); 100 101 if (!TEST_ptr(pathedconfig)) 102 return 0; 103 104 ctx = OSSL_LIB_CTX_new(); 105 if (!TEST_ptr(ctx)) 106 return 0; 107 108 if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, pathedconfig))) 109 goto err; 110 111 /* attempt to manually load the test provider */ 112 if (!TEST_ptr(prov = OSSL_PROVIDER_load(ctx, "test"))) 113 goto err; 114 115 OSSL_PROVIDER_unload(prov); 116 117 testresult = 1; 118 err: 119 OSSL_LIB_CTX_free(ctx); 120 return testresult; 121 } 122 123 OPT_TEST_DECLARE_USAGE("configfile\n") 124 125 int setup_tests(void) 126 { 127 if (!test_skip_common_options()) { 128 TEST_error("Error parsing test options\n"); 129 return 0; 130 } 131 132 if (!TEST_ptr(configfile = test_get_argument(0))) 133 return 0; 134 135 if (!TEST_ptr(recurseconfigfile = test_get_argument(1))) 136 return 0; 137 138 if (!TEST_ptr(pathedconfig = test_get_argument(2))) 139 return 0; 140 141 ADD_TEST(test_recursive_config); 142 ADD_TEST(test_double_config); 143 ADD_TEST(test_path_config); 144 return 1; 145 } 146