2
0

conf_lib.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "e_os.h"
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "internal/conf.h"
  13. #include "crypto/ctype.h"
  14. #include <openssl/crypto.h>
  15. #include <openssl/err.h>
  16. #include <openssl/conf.h>
  17. #include <openssl/conf_api.h>
  18. #include <openssl/lhash.h>
  19. static CONF_METHOD *default_CONF_method = NULL;
  20. /* Init a 'CONF' structure from an old LHASH */
  21. void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash)
  22. {
  23. if (default_CONF_method == NULL)
  24. default_CONF_method = NCONF_default();
  25. default_CONF_method->init(conf);
  26. conf->data = hash;
  27. }
  28. /*
  29. * The following section contains the "CONF classic" functions, rewritten in
  30. * terms of the new CONF interface.
  31. */
  32. int CONF_set_default_method(CONF_METHOD *meth)
  33. {
  34. default_CONF_method = meth;
  35. return 1;
  36. }
  37. LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file,
  38. long *eline)
  39. {
  40. LHASH_OF(CONF_VALUE) *ltmp;
  41. BIO *in = NULL;
  42. #ifdef OPENSSL_SYS_VMS
  43. in = BIO_new_file(file, "r");
  44. #else
  45. in = BIO_new_file(file, "rb");
  46. #endif
  47. if (in == NULL) {
  48. CONFerr(CONF_F_CONF_LOAD, ERR_R_SYS_LIB);
  49. return NULL;
  50. }
  51. ltmp = CONF_load_bio(conf, in, eline);
  52. BIO_free(in);
  53. return ltmp;
  54. }
  55. #ifndef OPENSSL_NO_STDIO
  56. LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp,
  57. long *eline)
  58. {
  59. BIO *btmp;
  60. LHASH_OF(CONF_VALUE) *ltmp;
  61. if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  62. CONFerr(CONF_F_CONF_LOAD_FP, ERR_R_BUF_LIB);
  63. return NULL;
  64. }
  65. ltmp = CONF_load_bio(conf, btmp, eline);
  66. BIO_free(btmp);
  67. return ltmp;
  68. }
  69. #endif
  70. LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp,
  71. long *eline)
  72. {
  73. CONF ctmp;
  74. int ret;
  75. CONF_set_nconf(&ctmp, conf);
  76. ret = NCONF_load_bio(&ctmp, bp, eline);
  77. if (ret)
  78. return ctmp.data;
  79. return NULL;
  80. }
  81. STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf,
  82. const char *section)
  83. {
  84. if (conf == NULL) {
  85. return NULL;
  86. } else {
  87. CONF ctmp;
  88. CONF_set_nconf(&ctmp, conf);
  89. return NCONF_get_section(&ctmp, section);
  90. }
  91. }
  92. char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group,
  93. const char *name)
  94. {
  95. if (conf == NULL) {
  96. return NCONF_get_string(NULL, group, name);
  97. } else {
  98. CONF ctmp;
  99. CONF_set_nconf(&ctmp, conf);
  100. return NCONF_get_string(&ctmp, group, name);
  101. }
  102. }
  103. long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group,
  104. const char *name)
  105. {
  106. int status;
  107. long result = 0;
  108. ERR_set_mark();
  109. if (conf == NULL) {
  110. status = NCONF_get_number_e(NULL, group, name, &result);
  111. } else {
  112. CONF ctmp;
  113. CONF_set_nconf(&ctmp, conf);
  114. status = NCONF_get_number_e(&ctmp, group, name, &result);
  115. }
  116. ERR_pop_to_mark();
  117. return status == 0 ? 0L : result;
  118. }
  119. void CONF_free(LHASH_OF(CONF_VALUE) *conf)
  120. {
  121. CONF ctmp;
  122. CONF_set_nconf(&ctmp, conf);
  123. NCONF_free_data(&ctmp);
  124. }
  125. #ifndef OPENSSL_NO_STDIO
  126. int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out)
  127. {
  128. BIO *btmp;
  129. int ret;
  130. if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
  131. CONFerr(CONF_F_CONF_DUMP_FP, ERR_R_BUF_LIB);
  132. return 0;
  133. }
  134. ret = CONF_dump_bio(conf, btmp);
  135. BIO_free(btmp);
  136. return ret;
  137. }
  138. #endif
  139. int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out)
  140. {
  141. CONF ctmp;
  142. CONF_set_nconf(&ctmp, conf);
  143. return NCONF_dump_bio(&ctmp, out);
  144. }
  145. /*
  146. * The following section contains the "New CONF" functions. They are
  147. * completely centralised around a new CONF structure that may contain
  148. * basically anything, but at least a method pointer and a table of data.
  149. * These functions are also written in terms of the bridge functions used by
  150. * the "CONF classic" functions, for consistency.
  151. */
  152. CONF *NCONF_new(CONF_METHOD *meth)
  153. {
  154. CONF *ret;
  155. if (meth == NULL)
  156. meth = NCONF_default();
  157. ret = meth->create(meth);
  158. if (ret == NULL) {
  159. CONFerr(CONF_F_NCONF_NEW, ERR_R_MALLOC_FAILURE);
  160. return NULL;
  161. }
  162. return ret;
  163. }
  164. void NCONF_free(CONF *conf)
  165. {
  166. if (conf == NULL)
  167. return;
  168. conf->meth->destroy(conf);
  169. }
  170. void NCONF_free_data(CONF *conf)
  171. {
  172. if (conf == NULL)
  173. return;
  174. conf->meth->destroy_data(conf);
  175. }
  176. int NCONF_load(CONF *conf, const char *file, long *eline)
  177. {
  178. if (conf == NULL) {
  179. CONFerr(CONF_F_NCONF_LOAD, CONF_R_NO_CONF);
  180. return 0;
  181. }
  182. return conf->meth->load(conf, file, eline);
  183. }
  184. #ifndef OPENSSL_NO_STDIO
  185. int NCONF_load_fp(CONF *conf, FILE *fp, long *eline)
  186. {
  187. BIO *btmp;
  188. int ret;
  189. if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  190. CONFerr(CONF_F_NCONF_LOAD_FP, ERR_R_BUF_LIB);
  191. return 0;
  192. }
  193. ret = NCONF_load_bio(conf, btmp, eline);
  194. BIO_free(btmp);
  195. return ret;
  196. }
  197. #endif
  198. int NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
  199. {
  200. if (conf == NULL) {
  201. CONFerr(CONF_F_NCONF_LOAD_BIO, CONF_R_NO_CONF);
  202. return 0;
  203. }
  204. return conf->meth->load_bio(conf, bp, eline);
  205. }
  206. STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)
  207. {
  208. if (conf == NULL) {
  209. CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_CONF);
  210. return NULL;
  211. }
  212. if (section == NULL) {
  213. CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_SECTION);
  214. return NULL;
  215. }
  216. return _CONF_get_section_values(conf, section);
  217. }
  218. char *NCONF_get_string(const CONF *conf, const char *group, const char *name)
  219. {
  220. char *s = _CONF_get_string(conf, group, name);
  221. /*
  222. * Since we may get a value from an environment variable even if conf is
  223. * NULL, let's check the value first
  224. */
  225. if (s)
  226. return s;
  227. if (conf == NULL) {
  228. CONFerr(CONF_F_NCONF_GET_STRING,
  229. CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
  230. return NULL;
  231. }
  232. CONFerr(CONF_F_NCONF_GET_STRING, CONF_R_NO_VALUE);
  233. ERR_add_error_data(4, "group=", group, " name=", name);
  234. return NULL;
  235. }
  236. static int default_is_number(const CONF *conf, char c)
  237. {
  238. return ossl_isdigit(c);
  239. }
  240. static int default_to_int(const CONF *conf, char c)
  241. {
  242. return (int)(c - '0');
  243. }
  244. int NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
  245. long *result)
  246. {
  247. char *str;
  248. long res;
  249. int (*is_number)(const CONF *, char) = &default_is_number;
  250. int (*to_int)(const CONF *, char) = &default_to_int;
  251. if (result == NULL) {
  252. CONFerr(CONF_F_NCONF_GET_NUMBER_E, ERR_R_PASSED_NULL_PARAMETER);
  253. return 0;
  254. }
  255. str = NCONF_get_string(conf, group, name);
  256. if (str == NULL)
  257. return 0;
  258. if (conf != NULL) {
  259. if (conf->meth->is_number != NULL)
  260. is_number = conf->meth->is_number;
  261. if (conf->meth->to_int != NULL)
  262. to_int = conf->meth->to_int;
  263. }
  264. for (res = 0; is_number(conf, *str); str++) {
  265. const int d = to_int(conf, *str);
  266. if (res > (LONG_MAX - d) / 10L) {
  267. CONFerr(CONF_F_NCONF_GET_NUMBER_E, CONF_R_NUMBER_TOO_LARGE);
  268. return 0;
  269. }
  270. res = res * 10 + d;
  271. }
  272. *result = res;
  273. return 1;
  274. }
  275. #ifndef OPENSSL_NO_STDIO
  276. int NCONF_dump_fp(const CONF *conf, FILE *out)
  277. {
  278. BIO *btmp;
  279. int ret;
  280. if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
  281. CONFerr(CONF_F_NCONF_DUMP_FP, ERR_R_BUF_LIB);
  282. return 0;
  283. }
  284. ret = NCONF_dump_bio(conf, btmp);
  285. BIO_free(btmp);
  286. return ret;
  287. }
  288. #endif
  289. int NCONF_dump_bio(const CONF *conf, BIO *out)
  290. {
  291. if (conf == NULL) {
  292. CONFerr(CONF_F_NCONF_DUMP_BIO, CONF_R_NO_CONF);
  293. return 0;
  294. }
  295. return conf->meth->dump(conf, out);
  296. }
  297. /*
  298. * These routines call the C malloc/free, to avoid intermixing with
  299. * OpenSSL function pointers before the library is initialized.
  300. */
  301. OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void)
  302. {
  303. OPENSSL_INIT_SETTINGS *ret = malloc(sizeof(*ret));
  304. if (ret == NULL)
  305. return NULL;
  306. memset(ret, 0, sizeof(*ret));
  307. ret->flags = DEFAULT_CONF_MFLAGS;
  308. return ret;
  309. }
  310. #ifndef OPENSSL_NO_STDIO
  311. int OPENSSL_INIT_set_config_filename(OPENSSL_INIT_SETTINGS *settings,
  312. const char *filename)
  313. {
  314. char *newfilename = NULL;
  315. if (filename != NULL) {
  316. newfilename = strdup(filename);
  317. if (newfilename == NULL)
  318. return 0;
  319. }
  320. free(settings->filename);
  321. settings->filename = newfilename;
  322. return 1;
  323. }
  324. void OPENSSL_INIT_set_config_file_flags(OPENSSL_INIT_SETTINGS *settings,
  325. unsigned long flags)
  326. {
  327. settings->flags = flags;
  328. }
  329. int OPENSSL_INIT_set_config_appname(OPENSSL_INIT_SETTINGS *settings,
  330. const char *appname)
  331. {
  332. char *newappname = NULL;
  333. if (appname != NULL) {
  334. newappname = strdup(appname);
  335. if (newappname == NULL)
  336. return 0;
  337. }
  338. free(settings->appname);
  339. settings->appname = newappname;
  340. return 1;
  341. }
  342. #endif
  343. void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS *settings)
  344. {
  345. free(settings->filename);
  346. free(settings->appname);
  347. free(settings);
  348. }