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