Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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 = cmd_lists == NULL
64
0
            ? CONF_R_SSL_SECTION_NOT_FOUND
65
0
            : CONF_R_SSL_SECTION_EMPTY;
66
67
0
        ERR_raise_data(ERR_LIB_CONF, rcode, "section=%s", ssl_conf_section);
68
0
        goto err;
69
0
    }
70
0
    cnt = sk_CONF_VALUE_num(cmd_lists);
71
0
    ssl_module_free(md);
72
73
0
    ssl = OPENSSL_zalloc(sizeof(*ssl));
74
0
    if (ssl == NULL)
75
0
        goto err;
76
0
    CONF_imodule_set_usr_data(md, ssl);
77
78
0
    ssl->names = OPENSSL_calloc(cnt, sizeof(*ssl->names));
79
0
    libctx = ossl_lib_ctx_get_concrete(cnf->libctx);
80
0
    if (libctx == NULL || ssl->names == NULL)
81
0
        goto err;
82
83
0
    ossl_lib_ctx_attach_ssl_conf_imodule(libctx, md);
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 = cmds == NULL
92
0
                ? CONF_R_SSL_COMMAND_SECTION_NOT_FOUND
93
0
                : CONF_R_SSL_COMMAND_SECTION_EMPTY;
94
95
0
            ERR_raise_data(ERR_LIB_CONF, rcode,
96
0
                "name=%s, value=%s", sect->name, sect->value);
97
0
            goto err;
98
0
        }
99
0
        ssl_name->name = OPENSSL_strdup(sect->name);
100
0
        if (ssl_name->name == NULL)
101
0
            goto err;
102
0
        cnt = sk_CONF_VALUE_num(cmds);
103
0
        ssl_name->cmds = OPENSSL_calloc(cnt, sizeof(struct ssl_conf_cmd_st));
104
0
        if (ssl_name->cmds == NULL)
105
0
            goto err;
106
0
        ssl_name->cmd_count = cnt;
107
0
        for (j = 0; j < cnt; j++) {
108
0
            const char *name;
109
0
            CONF_VALUE *cmd_conf = sk_CONF_VALUE_value(cmds, (int)j);
110
0
            struct ssl_conf_cmd_st *cmd = ssl_name->cmds + j;
111
112
            /* Skip any initial dot in name */
113
0
            name = strchr(cmd_conf->name, '.');
114
0
            if (name != NULL)
115
0
                name++;
116
0
            else
117
0
                name = cmd_conf->name;
118
0
            cmd->cmd = OPENSSL_strdup(name);
119
0
            cmd->arg = OPENSSL_strdup(cmd_conf->value);
120
0
            if (cmd->cmd == NULL || cmd->arg == NULL)
121
0
                goto err;
122
0
        }
123
0
    }
124
0
    rv = 1;
125
0
err:
126
0
    if (rv == 0)
127
0
        ssl_module_free(md);
128
0
    return rv;
129
0
}
130
131
/*
132
 * Returns the set of commands with index |idx| previously searched for via
133
 * conf_ssl_name_find. Also stores the name of the set of commands in |*name|
134
 * and the number of commands in the set in |*cnt|.
135
 */
136
const SSL_CONF_CMD *conf_ssl_get(CONF_IMODULE *md, size_t idx, const char **name, size_t *cnt)
137
0
{
138
0
    SSL_MODULE *ssl = md != NULL ? CONF_imodule_get_usr_data(md) : NULL;
139
140
0
    if (ssl == NULL || ssl->names == NULL)
141
0
        return NULL;
142
143
0
    *name = ssl->names[idx].name;
144
0
    *cnt = ssl->names[idx].cmd_count;
145
0
    return ssl->names[idx].cmds;
146
0
}
147
148
/*
149
 * Search for the named set of commands given in |name|. On success return the
150
 * index for the command set in |*idx|.
151
 * Returns 1 on success or 0 on failure.
152
 */
153
int conf_ssl_name_find(CONF_IMODULE *md, const char *name, size_t *idx)
154
0
{
155
0
    SSL_MODULE *ssl = md != NULL ? CONF_imodule_get_usr_data(md) : NULL;
156
0
    const struct ssl_conf_name_st *nm;
157
0
    size_t i;
158
159
0
    if (ssl == NULL || ssl->names == NULL)
160
0
        return 0;
161
162
0
    for (i = 0, nm = ssl->names; i < ssl->names_count; i++, nm++) {
163
0
        if (strcmp(nm->name, name) == 0) {
164
0
            *idx = i;
165
0
            return 1;
166
0
        }
167
0
    }
168
0
    return 0;
169
0
}
170
171
/*
172
 * Given a command set |cmd|, return details on the command at index |idx| which
173
 * must be less than the number of commands in the set (as returned by
174
 * conf_ssl_get). The name of the command will be returned in |*cmdstr| and the
175
 * argument is returned in |*arg|.
176
 */
177
void conf_ssl_get_cmd(const SSL_CONF_CMD *cmd, size_t idx, char **cmdstr,
178
    char **arg)
179
0
{
180
0
    *cmdstr = cmd[idx].cmd;
181
0
    *arg = cmd[idx].arg;
182
0
}
183
184
void ossl_config_add_ssl_module(void)
185
0
{
186
0
    CONF_module_add("ssl_conf", ssl_module_init, ssl_module_free);
187
0
}