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