]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/conf/conf_lib.c
54ba692462f5e69169b33b3a7034a3f0a9970b53
[thirdparty/openssl.git] / crypto / conf / conf_lib.c
1 /*
2 * Copyright 2000-2020 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 "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 <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 {
27 if (default_CONF_method == NULL)
28 default_CONF_method = NCONF_default();
29
30 default_CONF_method->init(conf);
31 conf->data = hash;
32 }
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 {
41 default_CONF_method = meth;
42 return 1;
43 }
44
45 LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file,
46 long *eline)
47 {
48 LHASH_OF(CONF_VALUE) *ltmp;
49 BIO *in = NULL;
50
51 #ifdef OPENSSL_SYS_VMS
52 in = BIO_new_file(file, "r");
53 #else
54 in = BIO_new_file(file, "rb");
55 #endif
56 if (in == NULL) {
57 CONFerr(CONF_F_CONF_LOAD, ERR_R_SYS_LIB);
58 return NULL;
59 }
60
61 ltmp = CONF_load_bio(conf, in, eline);
62 BIO_free(in);
63
64 return ltmp;
65 }
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 {
71 BIO *btmp;
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);
75 return NULL;
76 }
77 ltmp = CONF_load_bio(conf, btmp, eline);
78 BIO_free(btmp);
79 return ltmp;
80 }
81 #endif
82
83 LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp,
84 long *eline)
85 {
86 CONF ctmp;
87 int ret;
88
89 CONF_set_nconf(&ctmp, conf);
90
91 ret = NCONF_load_bio(&ctmp, bp, eline);
92 if (ret)
93 return ctmp.data;
94 return NULL;
95 }
96
97 STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf,
98 const char *section)
99 {
100 if (conf == NULL) {
101 return NULL;
102 } else {
103 CONF ctmp;
104
105 CONF_set_nconf(&ctmp, conf);
106 return NCONF_get_section(&ctmp, section);
107 }
108 }
109
110 char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group,
111 const char *name)
112 {
113 if (conf == NULL) {
114 return NCONF_get_string(NULL, group, name);
115 } else {
116 CONF ctmp;
117
118 CONF_set_nconf(&ctmp, conf);
119 return NCONF_get_string(&ctmp, group, name);
120 }
121 }
122
123 long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group,
124 const char *name)
125 {
126 int status;
127 long result = 0;
128
129 ERR_set_mark();
130 if (conf == NULL) {
131 status = NCONF_get_number_e(NULL, group, name, &result);
132 } else {
133 CONF ctmp;
134
135 CONF_set_nconf(&ctmp, conf);
136 status = NCONF_get_number_e(&ctmp, group, name, &result);
137 }
138 ERR_pop_to_mark();
139 return status == 0 ? 0L : result;
140 }
141
142 void CONF_free(LHASH_OF(CONF_VALUE) *conf)
143 {
144 CONF ctmp;
145 CONF_set_nconf(&ctmp, conf);
146 NCONF_free_data(&ctmp);
147 }
148
149 #ifndef OPENSSL_NO_STDIO
150 int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out)
151 {
152 BIO *btmp;
153 int ret;
154
155 if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
156 CONFerr(CONF_F_CONF_DUMP_FP, ERR_R_BUF_LIB);
157 return 0;
158 }
159 ret = CONF_dump_bio(conf, btmp);
160 BIO_free(btmp);
161 return ret;
162 }
163 #endif
164
165 int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out)
166 {
167 CONF ctmp;
168
169 CONF_set_nconf(&ctmp, conf);
170 return NCONF_dump_bio(&ctmp, out);
171 }
172
173 /*
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.
179 */
180
181 CONF *NCONF_new_ex(OPENSSL_CTX *libctx, CONF_METHOD *meth)
182 {
183 CONF *ret;
184
185 if (meth == NULL)
186 meth = NCONF_default();
187
188 ret = meth->create(meth);
189 if (ret == NULL) {
190 CONFerr(0, ERR_R_MALLOC_FAILURE);
191 return NULL;
192 }
193 ret->libctx = libctx;
194
195 return ret;
196 }
197
198 CONF *NCONF_new(CONF_METHOD *meth)
199 {
200 return NCONF_new_ex(NULL, meth);
201 }
202
203 void NCONF_free(CONF *conf)
204 {
205 if (conf == NULL)
206 return;
207 conf->meth->destroy(conf);
208 }
209
210 void NCONF_free_data(CONF *conf)
211 {
212 if (conf == NULL)
213 return;
214 conf->meth->destroy_data(conf);
215 }
216
217 int NCONF_load(CONF *conf, const char *file, long *eline)
218 {
219 if (conf == NULL) {
220 CONFerr(CONF_F_NCONF_LOAD, CONF_R_NO_CONF);
221 return 0;
222 }
223
224 return conf->meth->load(conf, file, eline);
225 }
226
227 #ifndef OPENSSL_NO_STDIO
228 int NCONF_load_fp(CONF *conf, FILE *fp, long *eline)
229 {
230 BIO *btmp;
231 int ret;
232 if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
233 CONFerr(CONF_F_NCONF_LOAD_FP, ERR_R_BUF_LIB);
234 return 0;
235 }
236 ret = NCONF_load_bio(conf, btmp, eline);
237 BIO_free(btmp);
238 return ret;
239 }
240 #endif
241
242 int NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
243 {
244 if (conf == NULL) {
245 CONFerr(CONF_F_NCONF_LOAD_BIO, CONF_R_NO_CONF);
246 return 0;
247 }
248
249 return conf->meth->load_bio(conf, bp, eline);
250 }
251
252 STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)
253 {
254 if (conf == NULL) {
255 CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_CONF);
256 return NULL;
257 }
258
259 if (section == NULL) {
260 CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_SECTION);
261 return NULL;
262 }
263
264 return _CONF_get_section_values(conf, section);
265 }
266
267 char *NCONF_get_string(const CONF *conf, const char *group, const char *name)
268 {
269 char *s = _CONF_get_string(conf, group, name);
270
271 /*
272 * Since we may get a value from an environment variable even if conf is
273 * NULL, let's check the value first
274 */
275 if (s)
276 return s;
277
278 if (conf == NULL) {
279 CONFerr(CONF_F_NCONF_GET_STRING,
280 CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
281 return NULL;
282 }
283 CONFerr(CONF_F_NCONF_GET_STRING, CONF_R_NO_VALUE);
284 ERR_add_error_data(4, "group=", group, " name=", name);
285 return NULL;
286 }
287
288 static int default_is_number(const CONF *conf, char c)
289 {
290 return ossl_isdigit(c);
291 }
292
293 static int default_to_int(const CONF *conf, char c)
294 {
295 return (int)(c - '0');
296 }
297
298 int NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
299 long *result)
300 {
301 char *str;
302 long res;
303 int (*is_number)(const CONF *, char) = &default_is_number;
304 int (*to_int)(const CONF *, char) = &default_to_int;
305
306 if (result == NULL) {
307 CONFerr(CONF_F_NCONF_GET_NUMBER_E, ERR_R_PASSED_NULL_PARAMETER);
308 return 0;
309 }
310
311 str = NCONF_get_string(conf, group, name);
312
313 if (str == NULL)
314 return 0;
315
316 if (conf != NULL) {
317 if (conf->meth->is_number != NULL)
318 is_number = conf->meth->is_number;
319 if (conf->meth->to_int != NULL)
320 to_int = conf->meth->to_int;
321 }
322 for (res = 0; is_number(conf, *str); str++) {
323 const int d = to_int(conf, *str);
324
325 if (res > (LONG_MAX - d) / 10L) {
326 CONFerr(CONF_F_NCONF_GET_NUMBER_E, CONF_R_NUMBER_TOO_LARGE);
327 return 0;
328 }
329 res = res * 10 + d;
330 }
331
332 *result = res;
333 return 1;
334 }
335
336 long _CONF_get_number(const CONF *conf, const char *section,
337 const char *name)
338 {
339 int status;
340 long result = 0;
341
342 ERR_set_mark();
343 status = NCONF_get_number_e(conf, section, name, &result);
344 ERR_pop_to_mark();
345 return status == 0 ? 0L : result;
346 }
347
348 #ifndef OPENSSL_NO_STDIO
349 int NCONF_dump_fp(const CONF *conf, FILE *out)
350 {
351 BIO *btmp;
352 int ret;
353 if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
354 CONFerr(CONF_F_NCONF_DUMP_FP, ERR_R_BUF_LIB);
355 return 0;
356 }
357 ret = NCONF_dump_bio(conf, btmp);
358 BIO_free(btmp);
359 return ret;
360 }
361 #endif
362
363 int NCONF_dump_bio(const CONF *conf, BIO *out)
364 {
365 if (conf == NULL) {
366 CONFerr(CONF_F_NCONF_DUMP_BIO, CONF_R_NO_CONF);
367 return 0;
368 }
369
370 return conf->meth->dump(conf, out);
371 }
372
373 /*
374 * These routines call the C malloc/free, to avoid intermixing with
375 * OpenSSL function pointers before the library is initialized.
376 */
377 OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void)
378 {
379 OPENSSL_INIT_SETTINGS *ret = malloc(sizeof(*ret));
380
381 if (ret == NULL)
382 return NULL;
383
384 memset(ret, 0, sizeof(*ret));
385 ret->flags = DEFAULT_CONF_MFLAGS;
386
387 return ret;
388 }
389
390
391 #ifndef OPENSSL_NO_STDIO
392 int OPENSSL_INIT_set_config_filename(OPENSSL_INIT_SETTINGS *settings,
393 const char *filename)
394 {
395 char *newfilename = NULL;
396
397 if (filename != NULL) {
398 newfilename = strdup(filename);
399 if (newfilename == NULL)
400 return 0;
401 }
402
403 free(settings->filename);
404 settings->filename = newfilename;
405
406 return 1;
407 }
408
409 void OPENSSL_INIT_set_config_file_flags(OPENSSL_INIT_SETTINGS *settings,
410 unsigned long flags)
411 {
412 settings->flags = flags;
413 }
414
415 int OPENSSL_INIT_set_config_appname(OPENSSL_INIT_SETTINGS *settings,
416 const char *appname)
417 {
418 char *newappname = NULL;
419
420 if (appname != NULL) {
421 newappname = strdup(appname);
422 if (newappname == NULL)
423 return 0;
424 }
425
426 free(settings->appname);
427 settings->appname = newappname;
428
429 return 1;
430 }
431 #endif
432
433 void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS *settings)
434 {
435 free(settings->filename);
436 free(settings->appname);
437 free(settings);
438 }