]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/conf/conf_api.c
Move e_os.h to include/internal
[thirdparty/openssl.git] / crypto / conf / conf_api.c
CommitLineData
62867571 1/*
4333b89f 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
d86b6915 3 *
2044d382 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
d86b6915
RL
8 */
9
10/* Part of the code in here was originally in conf.c, which is now removed */
11
d5f9166b 12#include "internal/e_os.h"
5c39a55d 13#include "internal/cryptlib.h"
2c1f5ce4 14#include <stdlib.h>
0baed24c 15#include <string.h>
d86b6915
RL
16#include <openssl/conf.h>
17#include <openssl/conf_api.h>
ff234c68 18#include "conf_local.h"
d86b6915 19
2a056de8 20static void value_free_hash(const CONF_VALUE *a, LHASH_OF(CONF_VALUE) *conf);
5ce278a7 21static void value_free_stack_doall(CONF_VALUE *a);
97b17195 22
9dd5ae65 23CONF_VALUE *_CONF_get_section(const CONF *conf, const char *section)
0f113f3e 24{
2b9bafe6 25 CONF_VALUE vv;
d86b6915 26
2b9bafe6 27 if (conf == NULL || section == NULL)
26a7d938 28 return NULL;
0f113f3e
MC
29 vv.name = NULL;
30 vv.section = (char *)section;
15795943 31 return conf->data != NULL ? lh_CONF_VALUE_retrieve(conf->data, &vv) : NULL;
0f113f3e 32}
d86b6915 33
9dd5ae65 34STACK_OF(CONF_VALUE) *_CONF_get_section_values(const CONF *conf,
0f113f3e
MC
35 const char *section)
36{
37 CONF_VALUE *v;
d86b6915 38
0f113f3e 39 v = _CONF_get_section(conf, section);
2b9bafe6 40 if (v == NULL)
26a7d938 41 return NULL;
2b9bafe6 42 return ((STACK_OF(CONF_VALUE) *)v->value);
0f113f3e 43}
d86b6915
RL
44
45int _CONF_add_string(CONF *conf, CONF_VALUE *section, CONF_VALUE *value)
0f113f3e
MC
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;
2b9bafe6 53 if (!sk_CONF_VALUE_push(ts, value))
0f113f3e 54 return 0;
0f113f3e
MC
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
66char *_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)
26a7d938 73 return NULL;
2b9bafe6
RS
74 if (conf == NULL)
75 return ossl_safe_getenv(name);
15795943
DDO
76 if (conf->data == NULL)
77 return NULL;
2b9bafe6 78 if (section != NULL) {
0f113f3e 79 vv.name = (char *)name;
2b9bafe6 80 vv.section = (char *)section;
0f113f3e
MC
81 v = lh_CONF_VALUE_retrieve(conf->data, &vv);
82 if (v != NULL)
26a7d938 83 return v->value;
2b9bafe6
RS
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;
0f113f3e
MC
96}
97
3c1d6bbc 98static unsigned long conf_value_hash(const CONF_VALUE *v)
0f113f3e 99{
739a1eb1 100 return (OPENSSL_LH_strhash(v->section) << 2) ^ OPENSSL_LH_strhash(v->name);
0f113f3e
MC
101}
102
3c1d6bbc 103static int conf_value_cmp(const CONF_VALUE *a, const CONF_VALUE *b)
0f113f3e
MC
104{
105 int i;
106
107 if (a->section != b->section) {
108 i = strcmp(a->section, b->section);
2b9bafe6 109 if (i != 0)
26a7d938 110 return i;
0f113f3e
MC
111 }
112
2b9bafe6
RS
113 if (a->name != NULL && b->name != NULL)
114 return strcmp(a->name, b->name);
115 if (a->name == b->name)
26a7d938 116 return 0;
2b9bafe6 117 return (a->name == NULL) ? -1 : 1;
0f113f3e
MC
118}
119
d86b6915 120int _CONF_new_data(CONF *conf)
0f113f3e 121{
2b9bafe6 122 if (conf == NULL)
0f113f3e 123 return 0;
62d0577e
DSH
124 if (conf->data == NULL) {
125 conf->data = lh_CONF_VALUE_new(conf_value_hash, conf_value_cmp);
126 if (conf->data == NULL)
0f113f3e 127 return 0;
62d0577e 128 }
0f113f3e
MC
129 return 1;
130}
d86b6915 131
2a056de8
DSH
132typedef LHASH_OF(CONF_VALUE) LH_CONF_VALUE;
133
134IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, LH_CONF_VALUE);
135
d86b6915 136void _CONF_free_data(CONF *conf)
0f113f3e 137{
19b30f1c
BE
138 if (conf == NULL)
139 return;
140
141 OPENSSL_free(conf->includedir);
142 if (conf->data == NULL)
0f113f3e
MC
143 return;
144
e6b5c341
DSH
145 /* evil thing to make sure the 'OPENSSL_free()' works as expected */
146 lh_CONF_VALUE_set_down_load(conf->data, 0);
2a056de8 147 lh_CONF_VALUE_doall_LH_CONF_VALUE(conf->data, value_free_hash, conf->data);
0f113f3e
MC
148
149 /*
150 * We now have only 'section' entries in the hash table. Due to problems
151 * with
152 */
153
63c75cd6 154 lh_CONF_VALUE_doall(conf->data, value_free_stack_doall);
0f113f3e
MC
155 lh_CONF_VALUE_free(conf->data);
156}
d86b6915 157
2a056de8 158static void value_free_hash(const CONF_VALUE *a, LHASH_OF(CONF_VALUE) *conf)
0f113f3e
MC
159{
160 if (a->name != NULL)
161 (void)lh_CONF_VALUE_delete(conf, a);
162}
d86b6915 163
5ce278a7 164static void value_free_stack_doall(CONF_VALUE *a)
0f113f3e
MC
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 }
efa7dd64 180 sk_CONF_VALUE_free(sk);
0f113f3e
MC
181 OPENSSL_free(a->section);
182 OPENSSL_free(a);
183}
d86b6915 184
9dd5ae65 185CONF_VALUE *_CONF_new_section(CONF *conf, const char *section)
0f113f3e
MC
186{
187 STACK_OF(CONF_VALUE) *sk = NULL;
efa7dd64 188 int i;
0f113f3e
MC
189 CONF_VALUE *v = NULL, *vv;
190
191 if ((sk = sk_CONF_VALUE_new_null()) == NULL)
192 goto err;
b4faea50 193 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL)
0f113f3e
MC
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);
aebd0e5c 204 if (vv != NULL || lh_CONF_VALUE_error(conf->data) > 0)
64d9844a 205 goto err;
efa7dd64
RS
206 return v;
207
0f113f3e 208 err:
efa7dd64 209 sk_CONF_VALUE_free(sk);
aebd0e5c
PK
210 if (v != NULL)
211 OPENSSL_free(v->section);
efa7dd64
RS
212 OPENSSL_free(v);
213 return NULL;
0f113f3e 214}