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