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