1e0c4386eSCy Schubert /*
2e0c4386eSCy Schubert * Copyright 2016-2021 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
10e0c4386eSCy Schubert #include <stdlib.h>
11e0c4386eSCy Schubert #include <string.h>
12e0c4386eSCy Schubert #include <openssl/conf.h>
13e0c4386eSCy Schubert #include <openssl/err.h>
14e0c4386eSCy Schubert #include "testutil.h"
15e0c4386eSCy Schubert
16e0c4386eSCy Schubert #ifdef _WIN32
17e0c4386eSCy Schubert # include <direct.h>
18e0c4386eSCy Schubert # define DIRSEP "/\\"
19e0c4386eSCy Schubert # ifndef __BORLANDC__
20e0c4386eSCy Schubert # define chdir _chdir
21e0c4386eSCy Schubert # endif
22e0c4386eSCy Schubert # define DIRSEP_PRESERVE 0
23e0c4386eSCy Schubert #elif !defined(OPENSSL_NO_POSIX_IO)
24e0c4386eSCy Schubert # include <unistd.h>
25e0c4386eSCy Schubert # ifndef OPENSSL_SYS_VMS
26e0c4386eSCy Schubert # define DIRSEP "/"
27e0c4386eSCy Schubert # define DIRSEP_PRESERVE 0
28e0c4386eSCy Schubert # else
29e0c4386eSCy Schubert # define DIRSEP "/]:"
30e0c4386eSCy Schubert # define DIRSEP_PRESERVE 1
31e0c4386eSCy Schubert # endif
32e0c4386eSCy Schubert #else
33e0c4386eSCy Schubert /* the test does not work without chdir() */
34e0c4386eSCy Schubert # define chdir(x) (-1);
35e0c4386eSCy Schubert # define DIRSEP "/"
36e0c4386eSCy Schubert # define DIRSEP_PRESERVE 0
37e0c4386eSCy Schubert #endif
38e0c4386eSCy Schubert
39e0c4386eSCy Schubert /* changes path to that of the filename */
change_path(const char * file)40e0c4386eSCy Schubert static int change_path(const char *file)
41e0c4386eSCy Schubert {
42e0c4386eSCy Schubert char *s = OPENSSL_strdup(file);
43e0c4386eSCy Schubert char *p = s;
44e0c4386eSCy Schubert char *last = NULL;
45e0c4386eSCy Schubert int ret = 0;
46e0c4386eSCy Schubert
47e0c4386eSCy Schubert if (s == NULL)
48e0c4386eSCy Schubert return -1;
49e0c4386eSCy Schubert
50e0c4386eSCy Schubert while ((p = strpbrk(p, DIRSEP)) != NULL) {
51e0c4386eSCy Schubert last = p++;
52e0c4386eSCy Schubert }
53e0c4386eSCy Schubert if (last == NULL)
54e0c4386eSCy Schubert goto err;
55e0c4386eSCy Schubert last[DIRSEP_PRESERVE] = 0;
56e0c4386eSCy Schubert
57e0c4386eSCy Schubert TEST_note("changing path to %s", s);
58e0c4386eSCy Schubert ret = chdir(s);
59e0c4386eSCy Schubert err:
60e0c4386eSCy Schubert OPENSSL_free(s);
61e0c4386eSCy Schubert return ret;
62e0c4386eSCy Schubert }
63e0c4386eSCy Schubert
64e0c4386eSCy Schubert /*
65e0c4386eSCy Schubert * This test program checks the operation of the .include directive.
66e0c4386eSCy Schubert */
67e0c4386eSCy Schubert
68e0c4386eSCy Schubert static CONF *conf;
69e0c4386eSCy Schubert static BIO *in;
70e0c4386eSCy Schubert static int expect_failure = 0;
71e0c4386eSCy Schubert
test_load_config(void)72e0c4386eSCy Schubert static int test_load_config(void)
73e0c4386eSCy Schubert {
74e0c4386eSCy Schubert long errline;
75e0c4386eSCy Schubert long val;
76e0c4386eSCy Schubert char *str;
77e0c4386eSCy Schubert long err;
78e0c4386eSCy Schubert
79e0c4386eSCy Schubert if (!TEST_int_gt(NCONF_load_bio(conf, in, &errline), 0)
80e0c4386eSCy Schubert || !TEST_int_eq(err = ERR_peek_error(), 0)) {
81e0c4386eSCy Schubert if (expect_failure)
82e0c4386eSCy Schubert return 1;
83e0c4386eSCy Schubert TEST_note("Failure loading the configuration at line %ld", errline);
84e0c4386eSCy Schubert return 0;
85e0c4386eSCy Schubert }
86e0c4386eSCy Schubert if (expect_failure) {
87e0c4386eSCy Schubert TEST_note("Failure expected but did not happen");
88e0c4386eSCy Schubert return 0;
89e0c4386eSCy Schubert }
90e0c4386eSCy Schubert
91e0c4386eSCy Schubert if (!TEST_int_gt(CONF_modules_load(conf, NULL, 0), 0)) {
92e0c4386eSCy Schubert TEST_note("Failed in CONF_modules_load");
93e0c4386eSCy Schubert return 0;
94e0c4386eSCy Schubert }
95e0c4386eSCy Schubert
96e0c4386eSCy Schubert /* verify whether CA_default/default_days is set */
97e0c4386eSCy Schubert val = 0;
98e0c4386eSCy Schubert if (!TEST_int_eq(NCONF_get_number(conf, "CA_default", "default_days", &val), 1)
99e0c4386eSCy Schubert || !TEST_int_eq(val, 365)) {
100e0c4386eSCy Schubert TEST_note("default_days incorrect");
101e0c4386eSCy Schubert return 0;
102e0c4386eSCy Schubert }
103e0c4386eSCy Schubert
104e0c4386eSCy Schubert /* verify whether req/default_bits is set */
105e0c4386eSCy Schubert val = 0;
106e0c4386eSCy Schubert if (!TEST_int_eq(NCONF_get_number(conf, "req", "default_bits", &val), 1)
107e0c4386eSCy Schubert || !TEST_int_eq(val, 2048)) {
108e0c4386eSCy Schubert TEST_note("default_bits incorrect");
109e0c4386eSCy Schubert return 0;
110e0c4386eSCy Schubert }
111e0c4386eSCy Schubert
112e0c4386eSCy Schubert /* verify whether countryName_default is set correctly */
113e0c4386eSCy Schubert str = NCONF_get_string(conf, "req_distinguished_name", "countryName_default");
114e0c4386eSCy Schubert if (!TEST_ptr(str) || !TEST_str_eq(str, "AU")) {
115e0c4386eSCy Schubert TEST_note("countryName_default incorrect");
116e0c4386eSCy Schubert return 0;
117e0c4386eSCy Schubert }
118e0c4386eSCy Schubert
119e0c4386eSCy Schubert return 1;
120e0c4386eSCy Schubert }
121e0c4386eSCy Schubert
test_check_null_numbers(void)122e0c4386eSCy Schubert static int test_check_null_numbers(void)
123e0c4386eSCy Schubert {
124e0c4386eSCy Schubert #if defined(_BSD_SOURCE) \
125e0c4386eSCy Schubert || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
126e0c4386eSCy Schubert || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
127e0c4386eSCy Schubert long val = 0;
128e0c4386eSCy Schubert
129e0c4386eSCy Schubert /* Verify that a NULL config with a present environment variable returns
130e0c4386eSCy Schubert * success and the value.
131e0c4386eSCy Schubert */
132e0c4386eSCy Schubert if (!TEST_int_eq(setenv("FNORD", "123", 1), 0)
133e0c4386eSCy Schubert || !TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
134e0c4386eSCy Schubert || !TEST_long_eq(val, 123)) {
135e0c4386eSCy Schubert TEST_note("environment variable with NULL conf failed");
136e0c4386eSCy Schubert return 0;
137e0c4386eSCy Schubert }
138e0c4386eSCy Schubert
139e0c4386eSCy Schubert /*
140e0c4386eSCy Schubert * Verify that a NULL config with a missing environment variable returns
141e0c4386eSCy Schubert * a failure code.
142e0c4386eSCy Schubert */
143e0c4386eSCy Schubert if (!TEST_int_eq(unsetenv("FNORD"), 0)
144e0c4386eSCy Schubert || !TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val))) {
145e0c4386eSCy Schubert TEST_note("missing environment variable with NULL conf failed");
146e0c4386eSCy Schubert return 0;
147e0c4386eSCy Schubert }
148e0c4386eSCy Schubert #endif
149e0c4386eSCy Schubert return 1;
150e0c4386eSCy Schubert }
151e0c4386eSCy Schubert
test_check_overflow(void)152e0c4386eSCy Schubert static int test_check_overflow(void)
153e0c4386eSCy Schubert {
154e0c4386eSCy Schubert #if defined(_BSD_SOURCE) \
155e0c4386eSCy Schubert || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \
156e0c4386eSCy Schubert || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
157e0c4386eSCy Schubert long val = 0;
158e0c4386eSCy Schubert char max[(sizeof(long) * 8) / 3 + 3];
159e0c4386eSCy Schubert char *p;
160e0c4386eSCy Schubert
161*0d0c8621SEnji Cooper p = max + BIO_snprintf(max, sizeof(max), "0%ld", LONG_MAX) - 1;
162e0c4386eSCy Schubert setenv("FNORD", max, 1);
163e0c4386eSCy Schubert if (!TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
164e0c4386eSCy Schubert || !TEST_long_eq(val, LONG_MAX))
165e0c4386eSCy Schubert return 0;
166e0c4386eSCy Schubert
167e0c4386eSCy Schubert while (++*p > '9')
168e0c4386eSCy Schubert *p-- = '0';
169e0c4386eSCy Schubert
170e0c4386eSCy Schubert setenv("FNORD", max, 1);
171e0c4386eSCy Schubert if (!TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val)))
172e0c4386eSCy Schubert return 0;
173e0c4386eSCy Schubert #endif
174e0c4386eSCy Schubert return 1;
175e0c4386eSCy Schubert }
176e0c4386eSCy Schubert
177e0c4386eSCy Schubert typedef enum OPTION_choice {
178e0c4386eSCy Schubert OPT_ERR = -1,
179e0c4386eSCy Schubert OPT_EOF = 0,
180e0c4386eSCy Schubert OPT_FAIL,
181e0c4386eSCy Schubert OPT_TEST_ENUM
182e0c4386eSCy Schubert } OPTION_CHOICE;
183e0c4386eSCy Schubert
test_get_options(void)184e0c4386eSCy Schubert const OPTIONS *test_get_options(void)
185e0c4386eSCy Schubert {
186e0c4386eSCy Schubert static const OPTIONS test_options[] = {
187e0c4386eSCy Schubert OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("conf_file\n"),
188e0c4386eSCy Schubert { "f", OPT_FAIL, '-', "A failure is expected" },
189e0c4386eSCy Schubert { NULL }
190e0c4386eSCy Schubert };
191e0c4386eSCy Schubert return test_options;
192e0c4386eSCy Schubert }
193e0c4386eSCy Schubert
setup_tests(void)194e0c4386eSCy Schubert int setup_tests(void)
195e0c4386eSCy Schubert {
196e0c4386eSCy Schubert const char *conf_file;
197e0c4386eSCy Schubert OPTION_CHOICE o;
198e0c4386eSCy Schubert
199e0c4386eSCy Schubert if (!TEST_ptr(conf = NCONF_new(NULL)))
200e0c4386eSCy Schubert return 0;
201e0c4386eSCy Schubert
202e0c4386eSCy Schubert while ((o = opt_next()) != OPT_EOF) {
203e0c4386eSCy Schubert switch (o) {
204e0c4386eSCy Schubert case OPT_FAIL:
205e0c4386eSCy Schubert expect_failure = 1;
206e0c4386eSCy Schubert break;
207e0c4386eSCy Schubert case OPT_TEST_CASES:
208e0c4386eSCy Schubert break;
209e0c4386eSCy Schubert default:
210e0c4386eSCy Schubert return 0;
211e0c4386eSCy Schubert }
212e0c4386eSCy Schubert }
213e0c4386eSCy Schubert
214e0c4386eSCy Schubert conf_file = test_get_argument(0);
215e0c4386eSCy Schubert if (!TEST_ptr(conf_file)
216e0c4386eSCy Schubert || !TEST_ptr(in = BIO_new_file(conf_file, "r"))) {
217e0c4386eSCy Schubert TEST_note("Unable to open the file argument");
218e0c4386eSCy Schubert return 0;
219e0c4386eSCy Schubert }
220e0c4386eSCy Schubert
221e0c4386eSCy Schubert /*
222e0c4386eSCy Schubert * For this test we need to chdir as we use relative
223e0c4386eSCy Schubert * path names in the config files.
224e0c4386eSCy Schubert */
225e0c4386eSCy Schubert change_path(conf_file);
226e0c4386eSCy Schubert
227e0c4386eSCy Schubert ADD_TEST(test_load_config);
228e0c4386eSCy Schubert ADD_TEST(test_check_null_numbers);
229e0c4386eSCy Schubert ADD_TEST(test_check_overflow);
230e0c4386eSCy Schubert return 1;
231e0c4386eSCy Schubert }
232e0c4386eSCy Schubert
cleanup_tests(void)233e0c4386eSCy Schubert void cleanup_tests(void)
234e0c4386eSCy Schubert {
235e0c4386eSCy Schubert BIO_vfree(in);
236e0c4386eSCy Schubert NCONF_free(conf);
237e0c4386eSCy Schubert CONF_modules_unload(1);
238e0c4386eSCy Schubert }
239