2
0

conf_include_test.c 7.7 KB

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