2 * Copyright 2000-2020 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
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 <openssl/lhash.h>
21 static CONF_METHOD *default_CONF_method = NULL;
23 /* Init a 'CONF' structure from an old LHASH */
25 void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash)
27 if (default_CONF_method == NULL)
28 default_CONF_method = NCONF_default();
30 default_CONF_method->init(conf);
35 * The following section contains the "CONF classic" functions, rewritten in
36 * terms of the new CONF interface.
39 int CONF_set_default_method(CONF_METHOD *meth)
41 default_CONF_method = meth;
45 LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file,
48 LHASH_OF(CONF_VALUE) *ltmp;
51 #ifdef OPENSSL_SYS_VMS
52 in = BIO_new_file(file, "r");
54 in = BIO_new_file(file, "rb");
57 ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB);
61 ltmp = CONF_load_bio(conf, in, eline);
67 #ifndef OPENSSL_NO_STDIO
68 LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp,
72 LHASH_OF(CONF_VALUE) *ltmp;
73 if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
74 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
77 ltmp = CONF_load_bio(conf, btmp, eline);
83 LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp,
89 CONF_set_nconf(&ctmp, conf);
91 ret = NCONF_load_bio(&ctmp, bp, eline);
97 STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf,
105 CONF_set_nconf(&ctmp, conf);
106 return NCONF_get_section(&ctmp, section);
110 char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group,
114 return NCONF_get_string(NULL, group, name);
118 CONF_set_nconf(&ctmp, conf);
119 return NCONF_get_string(&ctmp, group, name);
123 long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group,
131 status = NCONF_get_number_e(NULL, group, name, &result);
135 CONF_set_nconf(&ctmp, conf);
136 status = NCONF_get_number_e(&ctmp, group, name, &result);
139 return status == 0 ? 0L : result;
142 void CONF_free(LHASH_OF(CONF_VALUE) *conf)
145 CONF_set_nconf(&ctmp, conf);
146 NCONF_free_data(&ctmp);
149 #ifndef OPENSSL_NO_STDIO
150 int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out)
155 if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
156 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
159 ret = CONF_dump_bio(conf, btmp);
165 int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out)
169 CONF_set_nconf(&ctmp, conf);
170 return NCONF_dump_bio(&ctmp, out);
174 * The following section contains the "New CONF" functions. They are
175 * completely centralised around a new CONF structure that may contain
176 * basically anything, but at least a method pointer and a table of data.
177 * These functions are also written in terms of the bridge functions used by
178 * the "CONF classic" functions, for consistency.
181 CONF *NCONF_new_ex(OSSL_LIB_CTX *libctx, CONF_METHOD *meth)
186 meth = NCONF_default();
188 ret = meth->create(meth);
190 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
193 ret->libctx = libctx;
198 CONF *NCONF_new(CONF_METHOD *meth)
200 return NCONF_new_ex(NULL, meth);
203 void NCONF_free(CONF *conf)
207 conf->meth->destroy(conf);
210 void NCONF_free_data(CONF *conf)
214 conf->meth->destroy_data(conf);
217 int NCONF_load(CONF *conf, const char *file, long *eline)
220 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
224 return conf->meth->load(conf, file, eline);
227 #ifndef OPENSSL_NO_STDIO
228 int NCONF_load_fp(CONF *conf, FILE *fp, long *eline)
232 if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
233 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
236 ret = NCONF_load_bio(conf, btmp, eline);
242 int NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
245 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
249 return conf->meth->load_bio(conf, bp, eline);
252 STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)
255 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
259 if (section == NULL) {
260 ERR_raise(ERR_LIB_CONF, CONF_R_NO_SECTION);
264 return _CONF_get_section_values(conf, section);
267 char *NCONF_get_string(const CONF *conf, const char *group, const char *name)
269 char *s = _CONF_get_string(conf, group, name);
272 * Since we may get a value from an environment variable even if conf is
273 * NULL, let's check the value first
279 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
282 ERR_raise_data(ERR_LIB_CONF, CONF_R_NO_VALUE,
283 "group=%s name=%s", group, name);
287 static int default_is_number(const CONF *conf, char c)
289 return ossl_isdigit(c);
292 static int default_to_int(const CONF *conf, char c)
294 return (int)(c - '0');
297 int NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
302 int (*is_number)(const CONF *, char) = &default_is_number;
303 int (*to_int)(const CONF *, char) = &default_to_int;
305 if (result == NULL) {
306 ERR_raise(ERR_LIB_CONF, ERR_R_PASSED_NULL_PARAMETER);
310 str = NCONF_get_string(conf, group, name);
316 if (conf->meth->is_number != NULL)
317 is_number = conf->meth->is_number;
318 if (conf->meth->to_int != NULL)
319 to_int = conf->meth->to_int;
321 for (res = 0; is_number(conf, *str); str++) {
322 const int d = to_int(conf, *str);
324 if (res > (LONG_MAX - d) / 10L) {
325 ERR_raise(ERR_LIB_CONF, CONF_R_NUMBER_TOO_LARGE);
335 long _CONF_get_number(const CONF *conf, const char *section,
342 status = NCONF_get_number_e(conf, section, name, &result);
344 return status == 0 ? 0L : result;
347 #ifndef OPENSSL_NO_STDIO
348 int NCONF_dump_fp(const CONF *conf, FILE *out)
352 if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
353 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
356 ret = NCONF_dump_bio(conf, btmp);
362 int NCONF_dump_bio(const CONF *conf, BIO *out)
365 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
369 return conf->meth->dump(conf, out);
373 * These routines call the C malloc/free, to avoid intermixing with
374 * OpenSSL function pointers before the library is initialized.
376 OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void)
378 OPENSSL_INIT_SETTINGS *ret = malloc(sizeof(*ret));
383 memset(ret, 0, sizeof(*ret));
384 ret->flags = DEFAULT_CONF_MFLAGS;
390 #ifndef OPENSSL_NO_STDIO
391 int OPENSSL_INIT_set_config_filename(OPENSSL_INIT_SETTINGS *settings,
392 const char *filename)
394 char *newfilename = NULL;
396 if (filename != NULL) {
397 newfilename = strdup(filename);
398 if (newfilename == NULL)
402 free(settings->filename);
403 settings->filename = newfilename;
408 void OPENSSL_INIT_set_config_file_flags(OPENSSL_INIT_SETTINGS *settings,
411 settings->flags = flags;
414 int OPENSSL_INIT_set_config_appname(OPENSSL_INIT_SETTINGS *settings,
417 char *newappname = NULL;
419 if (appname != NULL) {
420 newappname = strdup(appname);
421 if (newappname == NULL)
425 free(settings->appname);
426 settings->appname = newappname;
432 void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS *settings)
434 free(settings->filename);
435 free(settings->appname);