1 /*
2 * Copyright 1995-2021 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 /* Part of the code in here was originally in conf.c, which is now removed */
11
12 #include "e_os.h"
13 #include "internal/cryptlib.h"
14 #include <stdlib.h>
15 #include <string.h>
16 #include <openssl/conf.h>
17 #include <openssl/conf_api.h>
18 #include "conf_local.h"
19
20 static void value_free_hash(const CONF_VALUE *a, LHASH_OF(CONF_VALUE) *conf);
21 static void value_free_stack_doall(CONF_VALUE *a);
22
_CONF_get_section(const CONF * conf,const char * section)23 CONF_VALUE *_CONF_get_section(const CONF *conf, const char *section)
24 {
25 CONF_VALUE vv;
26
27 if (conf == NULL || section == NULL)
28 return NULL;
29 vv.name = NULL;
30 vv.section = (char *)section;
31 return conf->data != NULL ? lh_CONF_VALUE_retrieve(conf->data, &vv) : NULL;
32 }
33
STACK_OF(CONF_VALUE)34 STACK_OF(CONF_VALUE) *_CONF_get_section_values(const CONF *conf,
35 const char *section)
36 {
37 CONF_VALUE *v;
38
39 v = _CONF_get_section(conf, section);
40 if (v == NULL)
41 return NULL;
42 return ((STACK_OF(CONF_VALUE) *)v->value);
43 }
44
_CONF_add_string(CONF * conf,CONF_VALUE * section,CONF_VALUE * value)45 int _CONF_add_string(CONF *conf, CONF_VALUE *section, CONF_VALUE *value)
46 {
47 CONF_VALUE *v = NULL;
48 STACK_OF(CONF_VALUE) *ts;
49
50 ts = (STACK_OF(CONF_VALUE) *)section->value;
51
52 value->section = section->section;
53 if (!sk_CONF_VALUE_push(ts, value))
54 return 0;
55
56 v = lh_CONF_VALUE_insert(conf->data, value);
57 if (v != NULL) {
58 (void)sk_CONF_VALUE_delete_ptr(ts, v);
59 OPENSSL_free(v->name);
60 OPENSSL_free(v->value);
61 OPENSSL_free(v);
62 }
63 return 1;
64 }
65
_CONF_get_string(const CONF * conf,const char * section,const char * name)66 char *_CONF_get_string(const CONF *conf, const char *section,
67 const char *name)
68 {
69 CONF_VALUE *v, vv;
70 char *p;
71
72 if (name == NULL)
73 return NULL;
74 if (conf == NULL)
75 return ossl_safe_getenv(name);
76 if (conf->data == NULL)
77 return NULL;
78 if (section != NULL) {
79 vv.name = (char *)name;
80 vv.section = (char *)section;
81 v = lh_CONF_VALUE_retrieve(conf->data, &vv);
82 if (v != NULL)
83 return v->value;
84 if (strcmp(section, "ENV") == 0) {
85 p = ossl_safe_getenv(name);
86 if (p != NULL)
87 return p;
88 }
89 }
90 vv.section = "default";
91 vv.name = (char *)name;
92 v = lh_CONF_VALUE_retrieve(conf->data, &vv);
93 if (v == NULL)
94 return NULL;
95 return v->value;
96 }
97
conf_value_hash(const CONF_VALUE * v)98 static unsigned long conf_value_hash(const CONF_VALUE *v)
99 {
100 return (OPENSSL_LH_strhash(v->section) << 2) ^ OPENSSL_LH_strhash(v->name);
101 }
102
conf_value_cmp(const CONF_VALUE * a,const CONF_VALUE * b)103 static int conf_value_cmp(const CONF_VALUE *a, const CONF_VALUE *b)
104 {
105 int i;
106
107 if (a->section != b->section) {
108 i = strcmp(a->section, b->section);
109 if (i != 0)
110 return i;
111 }
112
113 if (a->name != NULL && b->name != NULL)
114 return strcmp(a->name, b->name);
115 if (a->name == b->name)
116 return 0;
117 return (a->name == NULL) ? -1 : 1;
118 }
119
_CONF_new_data(CONF * conf)120 int _CONF_new_data(CONF *conf)
121 {
122 if (conf == NULL)
123 return 0;
124 if (conf->data == NULL) {
125 conf->data = lh_CONF_VALUE_new(conf_value_hash, conf_value_cmp);
126 if (conf->data == NULL)
127 return 0;
128 }
129 return 1;
130 }
131
132 typedef LHASH_OF(CONF_VALUE) LH_CONF_VALUE;
133
134 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, LH_CONF_VALUE);
135
_CONF_free_data(CONF * conf)136 void _CONF_free_data(CONF *conf)
137 {
138 if (conf == NULL)
139 return;
140
141 OPENSSL_free(conf->includedir);
142 if (conf->data == NULL)
143 return;
144
145 /* evil thing to make sure the 'OPENSSL_free()' works as expected */
146 lh_CONF_VALUE_set_down_load(conf->data, 0);
147 lh_CONF_VALUE_doall_LH_CONF_VALUE(conf->data, value_free_hash, conf->data);
148
149 /*
150 * We now have only 'section' entries in the hash table. Due to problems
151 * with
152 */
153
154 lh_CONF_VALUE_doall(conf->data, value_free_stack_doall);
155 lh_CONF_VALUE_free(conf->data);
156 }
157
value_free_hash(const CONF_VALUE * a,LHASH_OF (CONF_VALUE)* conf)158 static void value_free_hash(const CONF_VALUE *a, LHASH_OF(CONF_VALUE) *conf)
159 {
160 if (a->name != NULL)
161 (void)lh_CONF_VALUE_delete(conf, a);
162 }
163
value_free_stack_doall(CONF_VALUE * a)164 static void value_free_stack_doall(CONF_VALUE *a)
165 {
166 CONF_VALUE *vv;
167 STACK_OF(CONF_VALUE) *sk;
168 int i;
169
170 if (a->name != NULL)
171 return;
172
173 sk = (STACK_OF(CONF_VALUE) *)a->value;
174 for (i = sk_CONF_VALUE_num(sk) - 1; i >= 0; i--) {
175 vv = sk_CONF_VALUE_value(sk, i);
176 OPENSSL_free(vv->value);
177 OPENSSL_free(vv->name);
178 OPENSSL_free(vv);
179 }
180 sk_CONF_VALUE_free(sk);
181 OPENSSL_free(a->section);
182 OPENSSL_free(a);
183 }
184
_CONF_new_section(CONF * conf,const char * section)185 CONF_VALUE *_CONF_new_section(CONF *conf, const char *section)
186 {
187 STACK_OF(CONF_VALUE) *sk = NULL;
188 int i;
189 CONF_VALUE *v = NULL, *vv;
190
191 if ((sk = sk_CONF_VALUE_new_null()) == NULL)
192 goto err;
193 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL)
194 goto err;
195 i = strlen(section) + 1;
196 if ((v->section = OPENSSL_malloc(i)) == NULL)
197 goto err;
198
199 memcpy(v->section, section, i);
200 v->name = NULL;
201 v->value = (char *)sk;
202
203 vv = lh_CONF_VALUE_insert(conf->data, v);
204 if (vv != NULL || lh_CONF_VALUE_error(conf->data) > 0)
205 goto err;
206 return v;
207
208 err:
209 sk_CONF_VALUE_free(sk);
210 if (v != NULL)
211 OPENSSL_free(v->section);
212 OPENSSL_free(v);
213 return NULL;
214 }
215