/src/openssl/crypto/conf/conf_ssl.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2015-2025 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 <stdio.h> |
11 | | #include <string.h> |
12 | | #include <openssl/conf.h> |
13 | | #include <openssl/err.h> |
14 | | #include "conf_local.h" |
15 | | #include "internal/sslconf.h" |
16 | | #include "internal/core.h" |
17 | | #include "internal/cryptlib.h" |
18 | | |
19 | | typedef struct ssl_module_st SSL_MODULE; |
20 | | |
21 | | struct ssl_module_st { |
22 | | struct ssl_conf_name_st *names; |
23 | | size_t names_count; |
24 | | }; |
25 | | |
26 | | static void ssl_module_free(CONF_IMODULE *md) |
27 | 0 | { |
28 | 0 | SSL_MODULE *ssl = CONF_imodule_get_usr_data(md); |
29 | |
|
30 | 0 | if (ssl == NULL) |
31 | 0 | return; |
32 | | |
33 | 0 | CONF_imodule_set_usr_data(md, NULL); |
34 | 0 | ossl_lib_ctx_detach_ssl_conf_imodule(NULL, md); |
35 | |
|
36 | 0 | for (size_t i = 0; i < ssl->names_count; i++) { |
37 | 0 | struct ssl_conf_name_st *tname = ssl->names + i; |
38 | |
|
39 | 0 | OPENSSL_free(tname->name); |
40 | 0 | for (size_t j = 0; j < tname->cmd_count; j++) { |
41 | 0 | OPENSSL_free(tname->cmds[j].cmd); |
42 | 0 | OPENSSL_free(tname->cmds[j].arg); |
43 | 0 | } |
44 | 0 | OPENSSL_free(tname->cmds); |
45 | 0 | } |
46 | |
|
47 | 0 | OPENSSL_free(ssl->names); |
48 | 0 | OPENSSL_free(ssl); |
49 | 0 | } |
50 | | |
51 | | static int ssl_module_init(CONF_IMODULE *md, const CONF *cnf) |
52 | 0 | { |
53 | 0 | size_t i, j, cnt; |
54 | 0 | int rv = 0; |
55 | 0 | const char *ssl_conf_section; |
56 | 0 | STACK_OF(CONF_VALUE) *cmd_lists; |
57 | 0 | OSSL_LIB_CTX *libctx; |
58 | 0 | SSL_MODULE *ssl = NULL; |
59 | |
|
60 | 0 | ssl_conf_section = CONF_imodule_get_value(md); |
61 | 0 | cmd_lists = NCONF_get_section(cnf, ssl_conf_section); |
62 | 0 | if (sk_CONF_VALUE_num(cmd_lists) <= 0) { |
63 | 0 | int rcode = |
64 | 0 | cmd_lists == NULL |
65 | 0 | ? CONF_R_SSL_SECTION_NOT_FOUND |
66 | 0 | : CONF_R_SSL_SECTION_EMPTY; |
67 | |
|
68 | 0 | ERR_raise_data(ERR_LIB_CONF, rcode, "section=%s", ssl_conf_section); |
69 | 0 | goto err; |
70 | 0 | } |
71 | 0 | cnt = sk_CONF_VALUE_num(cmd_lists); |
72 | 0 | ssl_module_free(md); |
73 | |
|
74 | 0 | ssl = OPENSSL_zalloc(sizeof(*ssl)); |
75 | 0 | if (ssl == NULL) |
76 | 0 | goto err; |
77 | 0 | CONF_imodule_set_usr_data(md, ssl); |
78 | |
|
79 | 0 | ssl->names = OPENSSL_calloc(cnt, sizeof(*ssl->names)); |
80 | 0 | libctx = ossl_lib_ctx_get_concrete(cnf->libctx); |
81 | 0 | if (libctx == NULL || ssl->names == NULL) |
82 | 0 | goto err; |
83 | | |
84 | 0 | ossl_lib_ctx_attach_ssl_conf_imodule(libctx, md); |
85 | 0 | ssl->names_count = cnt; |
86 | 0 | for (i = 0; i < ssl->names_count; i++) { |
87 | 0 | struct ssl_conf_name_st *ssl_name = ssl->names + i; |
88 | 0 | CONF_VALUE *sect = sk_CONF_VALUE_value(cmd_lists, (int)i); |
89 | 0 | STACK_OF(CONF_VALUE) *cmds = NCONF_get_section(cnf, sect->value); |
90 | |
|
91 | 0 | if (sk_CONF_VALUE_num(cmds) <= 0) { |
92 | 0 | int rcode = |
93 | 0 | cmds == NULL |
94 | 0 | ? CONF_R_SSL_COMMAND_SECTION_NOT_FOUND |
95 | 0 | : CONF_R_SSL_COMMAND_SECTION_EMPTY; |
96 | |
|
97 | 0 | ERR_raise_data(ERR_LIB_CONF, rcode, |
98 | 0 | "name=%s, value=%s", sect->name, sect->value); |
99 | 0 | goto err; |
100 | 0 | } |
101 | 0 | ssl_name->name = OPENSSL_strdup(sect->name); |
102 | 0 | if (ssl_name->name == NULL) |
103 | 0 | goto err; |
104 | 0 | cnt = sk_CONF_VALUE_num(cmds); |
105 | 0 | ssl_name->cmds = OPENSSL_calloc(cnt, sizeof(struct ssl_conf_cmd_st)); |
106 | 0 | if (ssl_name->cmds == NULL) |
107 | 0 | goto err; |
108 | 0 | ssl_name->cmd_count = cnt; |
109 | 0 | for (j = 0; j < cnt; j++) { |
110 | 0 | const char *name; |
111 | 0 | CONF_VALUE *cmd_conf = sk_CONF_VALUE_value(cmds, (int)j); |
112 | 0 | struct ssl_conf_cmd_st *cmd = ssl_name->cmds + j; |
113 | | |
114 | | /* Skip any initial dot in name */ |
115 | 0 | name = strchr(cmd_conf->name, '.'); |
116 | 0 | if (name != NULL) |
117 | 0 | name++; |
118 | 0 | else |
119 | 0 | name = cmd_conf->name; |
120 | 0 | cmd->cmd = OPENSSL_strdup(name); |
121 | 0 | cmd->arg = OPENSSL_strdup(cmd_conf->value); |
122 | 0 | if (cmd->cmd == NULL || cmd->arg == NULL) |
123 | 0 | goto err; |
124 | 0 | } |
125 | |
|
126 | 0 | } |
127 | 0 | rv = 1; |
128 | 0 | err: |
129 | 0 | if (rv == 0) |
130 | 0 | ssl_module_free(md); |
131 | 0 | return rv; |
132 | 0 | } |
133 | | |
134 | | /* |
135 | | * Returns the set of commands with index |idx| previously searched for via |
136 | | * conf_ssl_name_find. Also stores the name of the set of commands in |*name| |
137 | | * and the number of commands in the set in |*cnt|. |
138 | | */ |
139 | | const SSL_CONF_CMD *conf_ssl_get(CONF_IMODULE *md, size_t idx, const char **name, size_t *cnt) |
140 | 0 | { |
141 | 0 | SSL_MODULE *ssl = md != NULL ? CONF_imodule_get_usr_data(md): NULL; |
142 | |
|
143 | 0 | if (ssl == NULL || ssl->names == NULL) |
144 | 0 | return NULL; |
145 | | |
146 | 0 | *name = ssl->names[idx].name; |
147 | 0 | *cnt = ssl->names[idx].cmd_count; |
148 | 0 | return ssl->names[idx].cmds; |
149 | 0 | } |
150 | | |
151 | | /* |
152 | | * Search for the named set of commands given in |name|. On success return the |
153 | | * index for the command set in |*idx|. |
154 | | * Returns 1 on success or 0 on failure. |
155 | | */ |
156 | | int conf_ssl_name_find(CONF_IMODULE *md, const char *name, size_t *idx) |
157 | 0 | { |
158 | 0 | SSL_MODULE *ssl = md != NULL ? CONF_imodule_get_usr_data(md): NULL; |
159 | 0 | const struct ssl_conf_name_st *nm; |
160 | 0 | size_t i; |
161 | |
|
162 | 0 | if (ssl == NULL || ssl->names == NULL) |
163 | 0 | return 0; |
164 | | |
165 | 0 | for (i = 0, nm = ssl->names; i < ssl->names_count; i++, nm++) { |
166 | 0 | if (strcmp(nm->name, name) == 0) { |
167 | 0 | *idx = i; |
168 | 0 | return 1; |
169 | 0 | } |
170 | 0 | } |
171 | 0 | return 0; |
172 | 0 | } |
173 | | |
174 | | /* |
175 | | * Given a command set |cmd|, return details on the command at index |idx| which |
176 | | * must be less than the number of commands in the set (as returned by |
177 | | * conf_ssl_get). The name of the command will be returned in |*cmdstr| and the |
178 | | * argument is returned in |*arg|. |
179 | | */ |
180 | | void conf_ssl_get_cmd(const SSL_CONF_CMD *cmd, size_t idx, char **cmdstr, |
181 | | char **arg) |
182 | 0 | { |
183 | 0 | *cmdstr = cmd[idx].cmd; |
184 | 0 | *arg = cmd[idx].arg; |
185 | 0 | } |
186 | | |
187 | | void ossl_config_add_ssl_module(void) |
188 | 0 | { |
189 | 0 | CONF_module_add("ssl_conf", ssl_module_init, ssl_module_free); |
190 | 0 | } |