/src/openssl32/crypto/conf/conf_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | #include "internal/e_os.h" |
11 | | #include <stdio.h> |
12 | | #include <string.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> |
21 | | |
22 | | static CONF_METHOD *default_CONF_method = NULL; |
23 | | |
24 | | /* Init a 'CONF' structure from an old LHASH */ |
25 | | |
26 | | void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash) |
27 | 4.62k | { |
28 | 4.62k | if (default_CONF_method == NULL) |
29 | 4 | default_CONF_method = NCONF_default(); |
30 | | |
31 | 4.62k | default_CONF_method->init(conf); |
32 | 4.62k | conf->data = hash; |
33 | 4.62k | } |
34 | | |
35 | | /* |
36 | | * The following section contains the "CONF classic" functions, rewritten in |
37 | | * terms of the new CONF interface. |
38 | | */ |
39 | | |
40 | | int CONF_set_default_method(CONF_METHOD *meth) |
41 | 0 | { |
42 | 0 | default_CONF_method = meth; |
43 | 0 | return 1; |
44 | 0 | } |
45 | | |
46 | | LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file, |
47 | | long *eline) |
48 | 0 | { |
49 | 0 | LHASH_OF(CONF_VALUE) *ltmp; |
50 | 0 | BIO *in = NULL; |
51 | |
|
52 | | #ifdef OPENSSL_SYS_VMS |
53 | | in = BIO_new_file(file, "r"); |
54 | | #else |
55 | 0 | in = BIO_new_file(file, "rb"); |
56 | 0 | #endif |
57 | 0 | if (in == NULL) { |
58 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB); |
59 | 0 | return NULL; |
60 | 0 | } |
61 | | |
62 | 0 | ltmp = CONF_load_bio(conf, in, eline); |
63 | 0 | BIO_free(in); |
64 | |
|
65 | 0 | return ltmp; |
66 | 0 | } |
67 | | |
68 | | #ifndef OPENSSL_NO_STDIO |
69 | | LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp, |
70 | | long *eline) |
71 | 0 | { |
72 | 0 | BIO *btmp; |
73 | 0 | LHASH_OF(CONF_VALUE) *ltmp; |
74 | 0 | if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) { |
75 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
76 | 0 | return NULL; |
77 | 0 | } |
78 | 0 | ltmp = CONF_load_bio(conf, btmp, eline); |
79 | 0 | BIO_free(btmp); |
80 | 0 | return ltmp; |
81 | 0 | } |
82 | | #endif |
83 | | |
84 | | LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp, |
85 | | long *eline) |
86 | 0 | { |
87 | 0 | CONF ctmp; |
88 | 0 | int ret; |
89 | |
|
90 | 0 | CONF_set_nconf(&ctmp, conf); |
91 | |
|
92 | 0 | ret = NCONF_load_bio(&ctmp, bp, eline); |
93 | 0 | if (ret) |
94 | 0 | return ctmp.data; |
95 | 0 | return NULL; |
96 | 0 | } |
97 | | |
98 | | STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf, |
99 | | const char *section) |
100 | 0 | { |
101 | 0 | if (conf == NULL) { |
102 | 0 | return NULL; |
103 | 0 | } else { |
104 | 0 | CONF ctmp; |
105 | |
|
106 | 0 | CONF_set_nconf(&ctmp, conf); |
107 | 0 | return NCONF_get_section(&ctmp, section); |
108 | 0 | } |
109 | 0 | } |
110 | | |
111 | | char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group, |
112 | | const char *name) |
113 | 0 | { |
114 | 0 | if (conf == NULL) { |
115 | 0 | return NCONF_get_string(NULL, group, name); |
116 | 0 | } else { |
117 | 0 | CONF ctmp; |
118 | |
|
119 | 0 | CONF_set_nconf(&ctmp, conf); |
120 | 0 | return NCONF_get_string(&ctmp, group, name); |
121 | 0 | } |
122 | 0 | } |
123 | | |
124 | | long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group, |
125 | | const char *name) |
126 | 0 | { |
127 | 0 | int status; |
128 | 0 | long result = 0; |
129 | |
|
130 | 0 | ERR_set_mark(); |
131 | 0 | if (conf == NULL) { |
132 | 0 | status = NCONF_get_number_e(NULL, group, name, &result); |
133 | 0 | } else { |
134 | 0 | CONF ctmp; |
135 | |
|
136 | 0 | CONF_set_nconf(&ctmp, conf); |
137 | 0 | status = NCONF_get_number_e(&ctmp, group, name, &result); |
138 | 0 | } |
139 | 0 | ERR_pop_to_mark(); |
140 | 0 | return status == 0 ? 0L : result; |
141 | 0 | } |
142 | | |
143 | | void CONF_free(LHASH_OF(CONF_VALUE) *conf) |
144 | 4.62k | { |
145 | 4.62k | CONF ctmp; |
146 | 4.62k | CONF_set_nconf(&ctmp, conf); |
147 | 4.62k | NCONF_free_data(&ctmp); |
148 | 4.62k | } |
149 | | |
150 | | #ifndef OPENSSL_NO_STDIO |
151 | | int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out) |
152 | 0 | { |
153 | 0 | BIO *btmp; |
154 | 0 | int ret; |
155 | |
|
156 | 0 | if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) { |
157 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
158 | 0 | return 0; |
159 | 0 | } |
160 | 0 | ret = CONF_dump_bio(conf, btmp); |
161 | 0 | BIO_free(btmp); |
162 | 0 | return ret; |
163 | 0 | } |
164 | | #endif |
165 | | |
166 | | int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out) |
167 | 0 | { |
168 | 0 | CONF ctmp; |
169 | |
|
170 | 0 | CONF_set_nconf(&ctmp, conf); |
171 | 0 | return NCONF_dump_bio(&ctmp, out); |
172 | 0 | } |
173 | | |
174 | | /* |
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. |
180 | | */ |
181 | | |
182 | | CONF *NCONF_new_ex(OSSL_LIB_CTX *libctx, CONF_METHOD *meth) |
183 | 12.3k | { |
184 | 12.3k | CONF *ret; |
185 | | |
186 | 12.3k | if (meth == NULL) |
187 | 12.3k | meth = NCONF_default(); |
188 | | |
189 | 12.3k | ret = meth->create(meth); |
190 | 12.3k | if (ret == NULL) { |
191 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB); |
192 | 0 | return NULL; |
193 | 0 | } |
194 | 12.3k | ret->libctx = libctx; |
195 | | |
196 | 12.3k | return ret; |
197 | 12.3k | } |
198 | | |
199 | | CONF *NCONF_new(CONF_METHOD *meth) |
200 | 12.2k | { |
201 | 12.2k | return NCONF_new_ex(NULL, meth); |
202 | 12.2k | } |
203 | | |
204 | | void NCONF_free(CONF *conf) |
205 | 12.3k | { |
206 | 12.3k | if (conf == NULL) |
207 | 0 | return; |
208 | 12.3k | conf->meth->destroy(conf); |
209 | 12.3k | } |
210 | | |
211 | | void NCONF_free_data(CONF *conf) |
212 | 4.62k | { |
213 | 4.62k | if (conf == NULL) |
214 | 0 | return; |
215 | 4.62k | conf->meth->destroy_data(conf); |
216 | 4.62k | } |
217 | | |
218 | | OSSL_LIB_CTX *NCONF_get0_libctx(const CONF *conf) |
219 | 0 | { |
220 | 0 | return conf->libctx; |
221 | 0 | } |
222 | | |
223 | | typedef STACK_OF(OPENSSL_CSTRING) SECTION_NAMES; |
224 | | |
225 | | IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, SECTION_NAMES); |
226 | | |
227 | | static void collect_section_name(const CONF_VALUE *v, SECTION_NAMES *names) |
228 | 0 | { |
229 | | /* A section is a CONF_VALUE with name == NULL */ |
230 | 0 | if (v->name == NULL) |
231 | 0 | sk_OPENSSL_CSTRING_push(names, v->section); |
232 | 0 | } |
233 | | |
234 | | static int section_name_cmp(OPENSSL_CSTRING const *a, OPENSSL_CSTRING const *b) |
235 | 0 | { |
236 | 0 | return strcmp(*a, *b); |
237 | 0 | } |
238 | | |
239 | | STACK_OF(OPENSSL_CSTRING) *NCONF_get_section_names(const CONF *cnf) |
240 | 0 | { |
241 | 0 | SECTION_NAMES *names; |
242 | |
|
243 | 0 | if ((names = sk_OPENSSL_CSTRING_new(section_name_cmp)) == NULL) |
244 | 0 | return NULL; |
245 | 0 | lh_CONF_VALUE_doall_SECTION_NAMES(cnf->data, collect_section_name, names); |
246 | 0 | sk_OPENSSL_CSTRING_sort(names); |
247 | 0 | return names; |
248 | 0 | } |
249 | | |
250 | | int NCONF_load(CONF *conf, const char *file, long *eline) |
251 | 82 | { |
252 | 82 | if (conf == NULL) { |
253 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF); |
254 | 0 | return 0; |
255 | 0 | } |
256 | | |
257 | 82 | return conf->meth->load(conf, file, eline); |
258 | 82 | } |
259 | | |
260 | | #ifndef OPENSSL_NO_STDIO |
261 | | int NCONF_load_fp(CONF *conf, FILE *fp, long *eline) |
262 | 0 | { |
263 | 0 | BIO *btmp; |
264 | 0 | int ret; |
265 | 0 | if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) { |
266 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
267 | 0 | return 0; |
268 | 0 | } |
269 | 0 | ret = NCONF_load_bio(conf, btmp, eline); |
270 | 0 | BIO_free(btmp); |
271 | 0 | return ret; |
272 | 0 | } |
273 | | #endif |
274 | | |
275 | | int NCONF_load_bio(CONF *conf, BIO *bp, long *eline) |
276 | 12.2k | { |
277 | 12.2k | if (conf == NULL) { |
278 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF); |
279 | 0 | return 0; |
280 | 0 | } |
281 | | |
282 | 12.2k | return conf->meth->load_bio(conf, bp, eline); |
283 | 12.2k | } |
284 | | |
285 | | STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section) |
286 | 0 | { |
287 | 0 | if (conf == NULL) { |
288 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF); |
289 | 0 | return NULL; |
290 | 0 | } |
291 | | |
292 | 0 | if (section == NULL) { |
293 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_NO_SECTION); |
294 | 0 | return NULL; |
295 | 0 | } |
296 | | |
297 | 0 | return _CONF_get_section_values(conf, section); |
298 | 0 | } |
299 | | |
300 | | char *NCONF_get_string(const CONF *conf, const char *group, const char *name) |
301 | 0 | { |
302 | 0 | char *s = _CONF_get_string(conf, group, name); |
303 | | |
304 | | /* |
305 | | * Since we may get a value from an environment variable even if conf is |
306 | | * NULL, let's check the value first |
307 | | */ |
308 | 0 | if (s) |
309 | 0 | return s; |
310 | | |
311 | 0 | if (conf == NULL) { |
312 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE); |
313 | 0 | return NULL; |
314 | 0 | } |
315 | 0 | ERR_raise_data(ERR_LIB_CONF, CONF_R_NO_VALUE, |
316 | 0 | "group=%s name=%s", group, name); |
317 | 0 | return NULL; |
318 | 0 | } |
319 | | |
320 | | static int default_is_number(const CONF *conf, char c) |
321 | 0 | { |
322 | 0 | return ossl_isdigit(c); |
323 | 0 | } |
324 | | |
325 | | static int default_to_int(const CONF *conf, char c) |
326 | 0 | { |
327 | 0 | return (int)(c - '0'); |
328 | 0 | } |
329 | | |
330 | | int NCONF_get_number_e(const CONF *conf, const char *group, const char *name, |
331 | | long *result) |
332 | 0 | { |
333 | 0 | char *str; |
334 | 0 | long res; |
335 | 0 | int (*is_number)(const CONF *, char) = &default_is_number; |
336 | 0 | int (*to_int)(const CONF *, char) = &default_to_int; |
337 | |
|
338 | 0 | if (result == NULL) { |
339 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_PASSED_NULL_PARAMETER); |
340 | 0 | return 0; |
341 | 0 | } |
342 | | |
343 | 0 | str = NCONF_get_string(conf, group, name); |
344 | |
|
345 | 0 | if (str == NULL) |
346 | 0 | return 0; |
347 | | |
348 | 0 | if (conf != NULL) { |
349 | 0 | if (conf->meth->is_number != NULL) |
350 | 0 | is_number = conf->meth->is_number; |
351 | 0 | if (conf->meth->to_int != NULL) |
352 | 0 | to_int = conf->meth->to_int; |
353 | 0 | } |
354 | 0 | for (res = 0; is_number(conf, *str); str++) { |
355 | 0 | const int d = to_int(conf, *str); |
356 | |
|
357 | 0 | if (res > (LONG_MAX - d) / 10L) { |
358 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_NUMBER_TOO_LARGE); |
359 | 0 | return 0; |
360 | 0 | } |
361 | 0 | res = res * 10 + d; |
362 | 0 | } |
363 | | |
364 | 0 | *result = res; |
365 | 0 | return 1; |
366 | 0 | } |
367 | | |
368 | | long _CONF_get_number(const CONF *conf, const char *section, |
369 | | const char *name) |
370 | 0 | { |
371 | 0 | int status; |
372 | 0 | long result = 0; |
373 | |
|
374 | 0 | ERR_set_mark(); |
375 | 0 | status = NCONF_get_number_e(conf, section, name, &result); |
376 | 0 | ERR_pop_to_mark(); |
377 | 0 | return status == 0 ? 0L : result; |
378 | 0 | } |
379 | | |
380 | | #ifndef OPENSSL_NO_STDIO |
381 | | int NCONF_dump_fp(const CONF *conf, FILE *out) |
382 | 0 | { |
383 | 0 | BIO *btmp; |
384 | 0 | int ret; |
385 | 0 | if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) { |
386 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
387 | 0 | return 0; |
388 | 0 | } |
389 | 0 | ret = NCONF_dump_bio(conf, btmp); |
390 | 0 | BIO_free(btmp); |
391 | 0 | return ret; |
392 | 0 | } |
393 | | #endif |
394 | | |
395 | | int NCONF_dump_bio(const CONF *conf, BIO *out) |
396 | 0 | { |
397 | 0 | if (conf == NULL) { |
398 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF); |
399 | 0 | return 0; |
400 | 0 | } |
401 | | |
402 | 0 | return conf->meth->dump(conf, out); |
403 | 0 | } |
404 | | |
405 | | /* |
406 | | * These routines call the C malloc/free, to avoid intermixing with |
407 | | * OpenSSL function pointers before the library is initialized. |
408 | | */ |
409 | | OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void) |
410 | 0 | { |
411 | 0 | OPENSSL_INIT_SETTINGS *ret = malloc(sizeof(*ret)); |
412 | |
|
413 | 0 | if (ret == NULL) |
414 | 0 | return NULL; |
415 | | |
416 | 0 | memset(ret, 0, sizeof(*ret)); |
417 | 0 | ret->flags = DEFAULT_CONF_MFLAGS; |
418 | |
|
419 | 0 | return ret; |
420 | 0 | } |
421 | | |
422 | | |
423 | | #ifndef OPENSSL_NO_STDIO |
424 | | /* |
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. |
429 | | */ |
430 | | int OPENSSL_INIT_set_config_filename(OPENSSL_INIT_SETTINGS *settings, |
431 | | const char *filename) |
432 | 0 | { |
433 | 0 | char *newfilename = NULL; |
434 | |
|
435 | 0 | if (filename != NULL) { |
436 | 0 | newfilename = strdup(filename); |
437 | 0 | if (newfilename == NULL) |
438 | 0 | return 0; |
439 | 0 | } |
440 | | |
441 | 0 | free(settings->filename); |
442 | 0 | settings->filename = newfilename; |
443 | |
|
444 | 0 | return 1; |
445 | 0 | } |
446 | | |
447 | | void OPENSSL_INIT_set_config_file_flags(OPENSSL_INIT_SETTINGS *settings, |
448 | | unsigned long flags) |
449 | 0 | { |
450 | 0 | settings->flags = flags; |
451 | 0 | } |
452 | | |
453 | | /* |
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. |
458 | | */ |
459 | | int OPENSSL_INIT_set_config_appname(OPENSSL_INIT_SETTINGS *settings, |
460 | | const char *appname) |
461 | 0 | { |
462 | 0 | char *newappname = NULL; |
463 | |
|
464 | 0 | if (appname != NULL) { |
465 | 0 | newappname = strdup(appname); |
466 | 0 | if (newappname == NULL) |
467 | 0 | return 0; |
468 | 0 | } |
469 | | |
470 | 0 | free(settings->appname); |
471 | 0 | settings->appname = newappname; |
472 | |
|
473 | 0 | return 1; |
474 | 0 | } |
475 | | #endif |
476 | | |
477 | | void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS *settings) |
478 | 0 | { |
479 | 0 | if (settings == NULL) |
480 | 0 | return; |
481 | | |
482 | 0 | free(settings->filename); |
483 | 0 | free(settings->appname); |
484 | 0 | free(settings); |
485 | 0 | } |