2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include "internal/conf.h"
14 #include "internal/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 CONFerr(CONF_F_CONF_LOAD
, 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 CONFerr(CONF_F_CONF_LOAD_FP
, 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
,
104 CONF_set_nconf(&ctmp
, conf
);
105 return NCONF_get_section(&ctmp
, section
);
109 char *CONF_get_string(LHASH_OF(CONF_VALUE
) *conf
, const char *group
,
113 return NCONF_get_string(NULL
, group
, name
);
116 CONF_set_nconf(&ctmp
, conf
);
117 return NCONF_get_string(&ctmp
, group
, name
);
121 long CONF_get_number(LHASH_OF(CONF_VALUE
) *conf
, const char *group
,
129 status
= NCONF_get_number_e(NULL
, group
, name
, &result
);
132 CONF_set_nconf(&ctmp
, conf
);
133 status
= NCONF_get_number_e(&ctmp
, group
, name
, &result
);
136 return status
== 0 ? 0L : result
;
139 void CONF_free(LHASH_OF(CONF_VALUE
) *conf
)
142 CONF_set_nconf(&ctmp
, conf
);
143 NCONF_free_data(&ctmp
);
146 #ifndef OPENSSL_NO_STDIO
147 int CONF_dump_fp(LHASH_OF(CONF_VALUE
) *conf
, FILE *out
)
152 if ((btmp
= BIO_new_fp(out
, BIO_NOCLOSE
)) == NULL
) {
153 CONFerr(CONF_F_CONF_DUMP_FP
, ERR_R_BUF_LIB
);
156 ret
= CONF_dump_bio(conf
, btmp
);
162 int CONF_dump_bio(LHASH_OF(CONF_VALUE
) *conf
, BIO
*out
)
165 CONF_set_nconf(&ctmp
, conf
);
166 return NCONF_dump_bio(&ctmp
, out
);
170 * The following section contains the "New CONF" functions. They are
171 * completely centralised around a new CONF structure that may contain
172 * basically anything, but at least a method pointer and a table of data.
173 * These functions are also written in terms of the bridge functions used by
174 * the "CONF classic" functions, for consistency.
177 CONF
*NCONF_new(CONF_METHOD
*meth
)
182 meth
= NCONF_default();
184 ret
= meth
->create(meth
);
186 CONFerr(CONF_F_NCONF_NEW
, ERR_R_MALLOC_FAILURE
);
193 void NCONF_free(CONF
*conf
)
197 conf
->meth
->destroy(conf
);
200 void NCONF_free_data(CONF
*conf
)
204 conf
->meth
->destroy_data(conf
);
207 int NCONF_load(CONF
*conf
, const char *file
, long *eline
)
210 CONFerr(CONF_F_NCONF_LOAD
, CONF_R_NO_CONF
);
214 return conf
->meth
->load(conf
, file
, eline
);
217 #ifndef OPENSSL_NO_STDIO
218 int NCONF_load_fp(CONF
*conf
, FILE *fp
, long *eline
)
222 if ((btmp
= BIO_new_fp(fp
, BIO_NOCLOSE
)) == NULL
) {
223 CONFerr(CONF_F_NCONF_LOAD_FP
, ERR_R_BUF_LIB
);
226 ret
= NCONF_load_bio(conf
, btmp
, eline
);
232 int NCONF_load_bio(CONF
*conf
, BIO
*bp
, long *eline
)
235 CONFerr(CONF_F_NCONF_LOAD_BIO
, CONF_R_NO_CONF
);
239 return conf
->meth
->load_bio(conf
, bp
, eline
);
242 STACK_OF(CONF_VALUE
) *NCONF_get_section(const CONF
*conf
, const char *section
)
245 CONFerr(CONF_F_NCONF_GET_SECTION
, CONF_R_NO_CONF
);
249 if (section
== NULL
) {
250 CONFerr(CONF_F_NCONF_GET_SECTION
, CONF_R_NO_SECTION
);
254 return _CONF_get_section_values(conf
, section
);
257 char *NCONF_get_string(const CONF
*conf
, const char *group
, const char *name
)
259 char *s
= _CONF_get_string(conf
, group
, name
);
262 * Since we may get a value from an environment variable even if conf is
263 * NULL, let's check the value first
269 CONFerr(CONF_F_NCONF_GET_STRING
,
270 CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE
);
273 CONFerr(CONF_F_NCONF_GET_STRING
, CONF_R_NO_VALUE
);
274 ERR_add_error_data(4, "group=", group
, " name=", name
);
278 static int default_is_number(const CONF
*conf
, char c
)
280 return ossl_isdigit(c
);
283 static int default_to_int(const CONF
*conf
, char c
)
285 return (int)(c
- '0');
288 int NCONF_get_number_e(const CONF
*conf
, const char *group
, const char *name
,
293 int (*is_number
)(const CONF
*, char) = &default_is_number
;
294 int (*to_int
)(const CONF
*, char) = &default_to_int
;
296 if (result
== NULL
) {
297 CONFerr(CONF_F_NCONF_GET_NUMBER_E
, ERR_R_PASSED_NULL_PARAMETER
);
301 str
= NCONF_get_string(conf
, group
, name
);
307 if (conf
->meth
->is_number
!= NULL
)
308 is_number
= conf
->meth
->is_number
;
309 if (conf
->meth
->to_int
!= NULL
)
310 to_int
= conf
->meth
->to_int
;
312 for (res
= 0; is_number(conf
, *str
); str
++) {
313 const int d
= to_int(conf
, *str
);
315 if (res
> (LONG_MAX
- d
) / 10L) {
316 CONFerr(CONF_F_NCONF_GET_NUMBER_E
, CONF_R_NUMBER_TOO_LARGE
);
326 #ifndef OPENSSL_NO_STDIO
327 int NCONF_dump_fp(const CONF
*conf
, FILE *out
)
331 if ((btmp
= BIO_new_fp(out
, BIO_NOCLOSE
)) == NULL
) {
332 CONFerr(CONF_F_NCONF_DUMP_FP
, ERR_R_BUF_LIB
);
335 ret
= NCONF_dump_bio(conf
, btmp
);
341 int NCONF_dump_bio(const CONF
*conf
, BIO
*out
)
344 CONFerr(CONF_F_NCONF_DUMP_BIO
, CONF_R_NO_CONF
);
348 return conf
->meth
->dump(conf
, out
);
352 * These routines call the C malloc/free, to avoid intermixing with
353 * OpenSSL function pointers before the library is initialized.
355 OPENSSL_INIT_SETTINGS
*OPENSSL_INIT_new(void)
357 OPENSSL_INIT_SETTINGS
*ret
= malloc(sizeof(*ret
));
360 memset(ret
, 0, sizeof(*ret
));
365 #ifndef OPENSSL_NO_STDIO
366 int OPENSSL_INIT_set_config_appname(OPENSSL_INIT_SETTINGS
*settings
,
369 char *newappname
= NULL
;
371 if (appname
!= NULL
) {
372 newappname
= strdup(appname
);
373 if (newappname
== NULL
)
377 free(settings
->appname
);
378 settings
->appname
= newappname
;
384 void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS
*settings
)
386 free(settings
->appname
);