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