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