xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/prov_config_test.c (revision 97e3c58506797315d86c0608cba9d3f55de0c735)
1b0d17251Schristos /*
20e2e28bcSchristos  * Copyright 2021-2024 The OpenSSL Project Authors. All Rights Reserved.
3b0d17251Schristos  *
4b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5b0d17251Schristos  * this file except in compliance with the License.  You can obtain a copy
6b0d17251Schristos  * in the file LICENSE in the source distribution or at
7b0d17251Schristos  * https://www.openssl.org/source/license.html
8b0d17251Schristos  */
9b0d17251Schristos 
100e2e28bcSchristos #include <sys/stat.h>
11b0d17251Schristos #include <openssl/evp.h>
120e2e28bcSchristos #include <openssl/conf.h>
13b0d17251Schristos #include "testutil.h"
14b0d17251Schristos 
15b0d17251Schristos static char *configfile = NULL;
160e2e28bcSchristos static char *recurseconfigfile = NULL;
170e2e28bcSchristos static char *pathedconfig = NULL;
18b0d17251Schristos 
19b0d17251Schristos /*
20b0d17251Schristos  * Test to make sure there are no leaks or failures from loading the config
21b0d17251Schristos  * file twice.
22b0d17251Schristos  */
23b0d17251Schristos static int test_double_config(void)
24b0d17251Schristos {
25b0d17251Schristos     OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
26b0d17251Schristos     int testresult = 0;
27b0d17251Schristos     EVP_MD *sha256 = NULL;
28b0d17251Schristos 
29b0d17251Schristos     if (!TEST_ptr(ctx))
30b0d17251Schristos         return 0;
31b0d17251Schristos 
32b0d17251Schristos     if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
33*97e3c585Schristos         goto err;
34b0d17251Schristos     if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
35*97e3c585Schristos         goto err;
36b0d17251Schristos 
37b0d17251Schristos     /* Check we can actually fetch something */
38b0d17251Schristos     sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL);
39b0d17251Schristos     if (!TEST_ptr(sha256))
40b0d17251Schristos         goto err;
41b0d17251Schristos 
42b0d17251Schristos     testresult = 1;
43b0d17251Schristos  err:
44b0d17251Schristos     EVP_MD_free(sha256);
45b0d17251Schristos     OSSL_LIB_CTX_free(ctx);
46b0d17251Schristos     return testresult;
47b0d17251Schristos }
48b0d17251Schristos 
490e2e28bcSchristos static int test_recursive_config(void)
500e2e28bcSchristos {
510e2e28bcSchristos     OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
520e2e28bcSchristos     int testresult = 0;
530e2e28bcSchristos     unsigned long err;
540e2e28bcSchristos 
550e2e28bcSchristos     if (!TEST_ptr(ctx))
560e2e28bcSchristos         goto err;
570e2e28bcSchristos 
580e2e28bcSchristos     if (!TEST_false(OSSL_LIB_CTX_load_config(ctx, recurseconfigfile)))
590e2e28bcSchristos         goto err;
600e2e28bcSchristos 
610e2e28bcSchristos     err = ERR_peek_error();
620e2e28bcSchristos     /* We expect to get a recursion error here */
630e2e28bcSchristos     if (ERR_GET_REASON(err) == CONF_R_RECURSIVE_SECTION_REFERENCE)
640e2e28bcSchristos         testresult = 1;
650e2e28bcSchristos  err:
660e2e28bcSchristos     OSSL_LIB_CTX_free(ctx);
670e2e28bcSchristos     return testresult;
680e2e28bcSchristos }
690e2e28bcSchristos 
700e2e28bcSchristos #define P_TEST_PATH "/../test/p_test.so"
710e2e28bcSchristos static int test_path_config(void)
720e2e28bcSchristos {
730e2e28bcSchristos     OSSL_LIB_CTX *ctx = NULL;
740e2e28bcSchristos     OSSL_PROVIDER *prov;
750e2e28bcSchristos     int testresult = 0;
760e2e28bcSchristos     struct stat sbuf;
770e2e28bcSchristos     char *module_path = getenv("OPENSSL_MODULES");
780e2e28bcSchristos     char *full_path = NULL;
790e2e28bcSchristos     int rc;
800e2e28bcSchristos 
810e2e28bcSchristos     if (!TEST_ptr(module_path))
820e2e28bcSchristos         return 0;
830e2e28bcSchristos 
840e2e28bcSchristos     full_path = OPENSSL_zalloc(strlen(module_path) + strlen(P_TEST_PATH) + 1);
850e2e28bcSchristos     if (!TEST_ptr(full_path))
860e2e28bcSchristos         return 0;
870e2e28bcSchristos 
880e2e28bcSchristos     strcpy(full_path, module_path);
890e2e28bcSchristos     full_path = strcat(full_path, P_TEST_PATH);
900e2e28bcSchristos     TEST_info("full path is %s", full_path);
910e2e28bcSchristos     rc = stat(full_path, &sbuf);
920e2e28bcSchristos     OPENSSL_free(full_path);
930e2e28bcSchristos     if (rc == -1)
940e2e28bcSchristos         return TEST_skip("Skipping modulepath test as provider not present");
950e2e28bcSchristos 
960e2e28bcSchristos     if (!TEST_ptr(pathedconfig))
970e2e28bcSchristos         return 0;
980e2e28bcSchristos 
990e2e28bcSchristos     ctx = OSSL_LIB_CTX_new();
1000e2e28bcSchristos     if (!TEST_ptr(ctx))
1010e2e28bcSchristos         return 0;
1020e2e28bcSchristos 
1030e2e28bcSchristos     if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, pathedconfig)))
1040e2e28bcSchristos         goto err;
1050e2e28bcSchristos 
1060e2e28bcSchristos     /* attempt to manually load the test provider */
1070e2e28bcSchristos     if (!TEST_ptr(prov = OSSL_PROVIDER_load(ctx, "test")))
1080e2e28bcSchristos         goto err;
1090e2e28bcSchristos 
1100e2e28bcSchristos     OSSL_PROVIDER_unload(prov);
1110e2e28bcSchristos 
1120e2e28bcSchristos     testresult = 1;
1130e2e28bcSchristos  err:
1140e2e28bcSchristos     OSSL_LIB_CTX_free(ctx);
1150e2e28bcSchristos     return testresult;
1160e2e28bcSchristos }
1170e2e28bcSchristos 
118b0d17251Schristos OPT_TEST_DECLARE_USAGE("configfile\n")
119b0d17251Schristos 
120b0d17251Schristos int setup_tests(void)
121b0d17251Schristos {
122b0d17251Schristos     if (!test_skip_common_options()) {
123b0d17251Schristos         TEST_error("Error parsing test options\n");
124b0d17251Schristos         return 0;
125b0d17251Schristos     }
126b0d17251Schristos 
127b0d17251Schristos     if (!TEST_ptr(configfile = test_get_argument(0)))
128b0d17251Schristos         return 0;
129b0d17251Schristos 
1300e2e28bcSchristos     if (!TEST_ptr(recurseconfigfile = test_get_argument(1)))
1310e2e28bcSchristos         return 0;
1320e2e28bcSchristos 
1330e2e28bcSchristos     if (!TEST_ptr(pathedconfig = test_get_argument(2)))
1340e2e28bcSchristos         return 0;
1350e2e28bcSchristos 
1360e2e28bcSchristos     ADD_TEST(test_recursive_config);
137b0d17251Schristos     ADD_TEST(test_double_config);
1380e2e28bcSchristos     ADD_TEST(test_path_config);
139b0d17251Schristos     return 1;
140b0d17251Schristos }
141