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