2 * Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/e_os.h"
13 #include "internal/conf.h"
14 #include "crypto/ctype.h"
15 #include <openssl/crypto.h>
16 #include <openssl/err.h>
17 #include <openssl/conf.h>
18 #include <openssl/conf_api.h>
19 #include "conf_local.h"
20 #include <openssl/lhash.h>
22 static CONF_METHOD *default_CONF_method = NULL;
24 /* Init a 'CONF' structure from an old LHASH */
26 void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash)
28 if (default_CONF_method == NULL)
29 default_CONF_method = NCONF_default();
31 default_CONF_method->init(conf);
36 * The following section contains the "CONF classic" functions, rewritten in
37 * terms of the new CONF interface.
40 int CONF_set_default_method(CONF_METHOD *meth)
42 default_CONF_method = meth;
46 LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file,
49 LHASH_OF(CONF_VALUE) *ltmp;
52 #ifdef OPENSSL_SYS_VMS
53 in = BIO_new_file(file, "r");
55 in = BIO_new_file(file, "rb");
58 ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB);
62 ltmp = CONF_load_bio(conf, in, eline);
68 #ifndef OPENSSL_NO_STDIO
69 LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp,
73 LHASH_OF(CONF_VALUE) *ltmp;
74 if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
75 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
78 ltmp = CONF_load_bio(conf, btmp, eline);
84 LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp,
90 CONF_set_nconf(&ctmp, conf);
92 ret = NCONF_load_bio(&ctmp, bp, eline);
98 STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf,
106 CONF_set_nconf(&ctmp, conf);
107 return NCONF_get_section(&ctmp, section);
111 char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group,
115 return NCONF_get_string(NULL, group, name);
119 CONF_set_nconf(&ctmp, conf);
120 return NCONF_get_string(&ctmp, group, name);
124 long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group,
132 status = NCONF_get_number_e(NULL, group, name, &result);
136 CONF_set_nconf(&ctmp, conf);
137 status = NCONF_get_number_e(&ctmp, group, name, &result);
140 return status == 0 ? 0L : result;
143 void CONF_free(LHASH_OF(CONF_VALUE) *conf)
146 CONF_set_nconf(&ctmp, conf);
147 NCONF_free_data(&ctmp);
150 #ifndef OPENSSL_NO_STDIO
151 int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out)
156 if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
157 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
160 ret = CONF_dump_bio(conf, btmp);
166 int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out)
170 CONF_set_nconf(&ctmp, conf);
171 return NCONF_dump_bio(&ctmp, out);
175 * The following section contains the "New CONF" functions. They are
176 * completely centralised around a new CONF structure that may contain
177 * basically anything, but at least a method pointer and a table of data.
178 * These functions are also written in terms of the bridge functions used by
179 * the "CONF classic" functions, for consistency.
182 CONF *NCONF_new_ex(OSSL_LIB_CTX *libctx, CONF_METHOD *meth)
187 meth = NCONF_default();
189 ret = meth->create(meth);
191 ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB);
194 ret->libctx = libctx;
199 CONF *NCONF_new(CONF_METHOD *meth)
201 return NCONF_new_ex(NULL, meth);
204 void NCONF_free(CONF *conf)
208 conf->meth->destroy(conf);
211 void NCONF_free_data(CONF *conf)
215 conf->meth->destroy_data(conf);
218 OSSL_LIB_CTX *NCONF_get0_libctx(const CONF *conf)
223 typedef STACK_OF(OPENSSL_CSTRING) SECTION_NAMES;
225 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, SECTION_NAMES);
227 static void collect_section_name(const CONF_VALUE *v, SECTION_NAMES *names)
229 /* A section is a CONF_VALUE with name == NULL */
231 sk_OPENSSL_CSTRING_push(names, v->section);
234 static int section_name_cmp(OPENSSL_CSTRING const *a, OPENSSL_CSTRING const *b)
236 return strcmp(*a, *b);
239 STACK_OF(OPENSSL_CSTRING) *NCONF_get_section_names(const CONF *cnf)
241 SECTION_NAMES *names;
243 if ((names = sk_OPENSSL_CSTRING_new(section_name_cmp)) == NULL)
245 lh_CONF_VALUE_doall_SECTION_NAMES(cnf->data, collect_section_name, names);
246 sk_OPENSSL_CSTRING_sort(names);
250 int NCONF_load(CONF *conf, const char *file, long *eline)
253 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
257 return conf->meth->load(conf, file, eline);
260 #ifndef OPENSSL_NO_STDIO
261 int NCONF_load_fp(CONF *conf, FILE *fp, long *eline)
265 if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
266 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
269 ret = NCONF_load_bio(conf, btmp, eline);
275 int NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
278 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
282 return conf->meth->load_bio(conf, bp, eline);
285 STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)
288 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
292 if (section == NULL) {
293 ERR_raise(ERR_LIB_CONF, CONF_R_NO_SECTION);
297 return _CONF_get_section_values(conf, section);
300 char *NCONF_get_string(const CONF *conf, const char *group, const char *name)
302 char *s = _CONF_get_string(conf, group, name);
305 * Since we may get a value from an environment variable even if conf is
306 * NULL, let's check the value first
312 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
315 ERR_raise_data(ERR_LIB_CONF, CONF_R_NO_VALUE,
316 "group=%s name=%s", group, name);
320 static int default_is_number(const CONF *conf, char c)
322 return ossl_isdigit(c);
325 static int default_to_int(const CONF *conf, char c)
327 return (int)(c - '0');
330 int NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
335 int (*is_number)(const CONF *, char) = &default_is_number;
336 int (*to_int)(const CONF *, char) = &default_to_int;
338 if (result == NULL) {
339 ERR_raise(ERR_LIB_CONF, ERR_R_PASSED_NULL_PARAMETER);
343 str = NCONF_get_string(conf, group, name);
349 if (conf->meth->is_number != NULL)
350 is_number = conf->meth->is_number;
351 if (conf->meth->to_int != NULL)
352 to_int = conf->meth->to_int;
354 for (res = 0; is_number(conf, *str); str++) {
355 const int d = to_int(conf, *str);
357 if (res > (LONG_MAX - d) / 10L) {
358 ERR_raise(ERR_LIB_CONF, CONF_R_NUMBER_TOO_LARGE);
368 long _CONF_get_number(const CONF *conf, const char *section,
375 status = NCONF_get_number_e(conf, section, name, &result);
377 return status == 0 ? 0L : result;
380 #ifndef OPENSSL_NO_STDIO
381 int NCONF_dump_fp(const CONF *conf, FILE *out)
385 if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
386 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
389 ret = NCONF_dump_bio(conf, btmp);
395 int NCONF_dump_bio(const CONF *conf, BIO *out)
398 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
402 return conf->meth->dump(conf, out);
406 * These routines call the C malloc/free, to avoid intermixing with
407 * OpenSSL function pointers before the library is initialized.
409 OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void)
411 OPENSSL_INIT_SETTINGS *ret = malloc(sizeof(*ret));
416 memset(ret, 0, sizeof(*ret));
417 ret->flags = DEFAULT_CONF_MFLAGS;
423 #ifndef OPENSSL_NO_STDIO
425 * If CRYPTO_set_mem_functions is called after this, then
426 * memory allocation and deallocation in this function can
427 * become disjointed. Avoid this by always using standard
428 * strdup & free instead of OPENSSL_strdup & OPENSSL_free.
430 int OPENSSL_INIT_set_config_filename(OPENSSL_INIT_SETTINGS *settings,
431 const char *filename)
433 char *newfilename = NULL;
435 if (filename != NULL) {
436 newfilename = strdup(filename);
437 if (newfilename == NULL)
441 free(settings->filename);
442 settings->filename = newfilename;
447 void OPENSSL_INIT_set_config_file_flags(OPENSSL_INIT_SETTINGS *settings,
450 settings->flags = flags;
454 * If CRYPTO_set_mem_functions is called after this, then
455 * memory allocation and deallocation in this function can
456 * become disjointed. Avoid this by always using standard
457 * strdup & free instead of OPENSSL_strdup & OPENSSL_free.
459 int OPENSSL_INIT_set_config_appname(OPENSSL_INIT_SETTINGS *settings,
462 char *newappname = NULL;
464 if (appname != NULL) {
465 newappname = strdup(appname);
466 if (newappname == NULL)
470 free(settings->appname);
471 settings->appname = newappname;
477 void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS *settings)
479 free(settings->filename);
480 free(settings->appname);