Coverage Report

Created: 2026-04-22 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/ssl/ssl_mcnf.c
Line
Count
Source
1
/*
2
 * Copyright 2015-2024 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 <openssl/conf.h>
12
#include <openssl/ssl.h>
13
#include <openssl/trace.h>
14
#include "ssl_local.h"
15
#include "internal/sslconf.h"
16
#include "internal/cryptlib.h"
17
18
/* SSL library configuration module. */
19
20
void SSL_add_ssl_module(void)
21
0
{
22
    /* Do nothing. This will be added automatically by libcrypto */
23
0
}
24
25
static CONF_IMODULE *ssl_do_lookup_module(OSSL_LIB_CTX *libctx)
26
0
{
27
0
    CONF_IMODULE *m = OSSL_LIB_CTX_get_data(libctx, OSSL_LIB_CTX_SSL_CONF_IMODULE);
28
29
0
    if (m != NULL)
30
0
        return m;
31
32
0
    libctx = OSSL_LIB_CTX_get0_global_default();
33
0
    return OSSL_LIB_CTX_get_data(libctx, OSSL_LIB_CTX_SSL_CONF_IMODULE);
34
0
}
35
36
static int ssl_do_config(SSL *s, SSL_CTX *ctx, const char *name, int system)
37
0
{
38
0
    SSL_CONF_CTX *cctx = NULL;
39
0
    size_t i, idx, cmd_count;
40
0
    int err = 1;
41
0
    unsigned int flags;
42
0
    unsigned int conf_diagnostics = 0;
43
0
    const SSL_METHOD *meth;
44
0
    const SSL_CONF_CMD *cmds;
45
0
    OSSL_LIB_CTX *libctx = NULL, *prev_libctx = NULL;
46
0
    CONF_IMODULE *imod = NULL;
47
48
0
    ERR_set_mark();
49
50
0
    if (s == NULL && ctx == NULL) {
51
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
52
0
        goto err;
53
0
    }
54
0
    if (name == NULL && system)
55
0
        name = "system_default";
56
57
0
    if (name == NULL) {
58
0
        ERR_raise_data(ERR_LIB_SSL, SSL_R_INVALID_CONFIGURATION_NAME,
59
0
            "name not specified (name == NULL)");
60
0
        goto err;
61
0
    }
62
63
0
    libctx = s != NULL ? s->ctx->libctx : ctx->libctx;
64
0
    imod = ssl_do_lookup_module(libctx);
65
0
    if (!conf_ssl_name_find(imod, name, &idx)) {
66
0
        if (!system)
67
0
            ERR_raise_data(ERR_LIB_SSL, SSL_R_INVALID_CONFIGURATION_NAME,
68
0
                "name=%s", name);
69
0
        goto err;
70
0
    }
71
72
0
    cmds = conf_ssl_get(imod, idx, &name, &cmd_count);
73
0
    flags = SSL_CONF_FLAG_FILE;
74
0
    if (!system)
75
0
        flags |= SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE;
76
77
0
    cctx = SSL_CONF_CTX_new();
78
0
    if (cctx == NULL) {
79
        /* this is a fatal error, always report */
80
0
        system = 0;
81
0
        goto err;
82
0
    }
83
84
0
    if (s != NULL) {
85
0
        meth = s->method;
86
0
        SSL_CONF_CTX_set_ssl(cctx, s);
87
0
        libctx = s->ctx->libctx;
88
0
    } else {
89
0
        meth = ctx->method;
90
0
        SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
91
0
        libctx = ctx->libctx;
92
0
    }
93
94
0
    conf_diagnostics = OSSL_LIB_CTX_get_conf_diagnostics(libctx);
95
0
    if (conf_diagnostics)
96
0
        flags |= SSL_CONF_FLAG_SHOW_ERRORS;
97
0
    if (meth->ssl_accept != ssl_undefined_function)
98
0
        flags |= SSL_CONF_FLAG_SERVER;
99
0
    if (meth->ssl_connect != ssl_undefined_function)
100
0
        flags |= SSL_CONF_FLAG_CLIENT;
101
0
    SSL_CONF_CTX_set_flags(cctx, flags);
102
0
    prev_libctx = OSSL_LIB_CTX_set0_default(libctx);
103
0
    err = 0;
104
0
    for (i = 0; i < cmd_count; i++) {
105
0
        char *cmdstr, *arg;
106
0
        int rv;
107
108
0
        conf_ssl_get_cmd(cmds, i, &cmdstr, &arg);
109
0
        rv = SSL_CONF_cmd(cctx, cmdstr, arg);
110
0
        if (rv <= 0)
111
0
            ++err;
112
0
    }
113
0
    if (!SSL_CONF_CTX_finish(cctx))
114
0
        ++err;
115
0
err:
116
0
    OSSL_LIB_CTX_set0_default(prev_libctx);
117
0
    SSL_CONF_CTX_free(cctx);
118
0
    if (err == 0) {
119
0
        ERR_pop_to_mark();
120
0
        return 1;
121
0
    }
122
0
    if (system && !conf_diagnostics) {
123
        /*
124
         * Discard errors so that SSL_CTX_new does not return
125
         * success with stale errors on the error stack.
126
         */
127
0
        ERR_pop_to_mark();
128
0
        return 1;
129
0
    }
130
0
    ERR_clear_last_mark();
131
0
    return 0;
132
0
}
133
134
int SSL_config(SSL *s, const char *name)
135
0
{
136
0
    return ssl_do_config(s, NULL, name, 0);
137
0
}
138
139
int SSL_CTX_config(SSL_CTX *ctx, const char *name)
140
0
{
141
0
    return ssl_do_config(NULL, ctx, name, 0);
142
0
}
143
144
int ssl_ctx_system_config(SSL_CTX *ctx)
145
0
{
146
0
    return ssl_do_config(NULL, ctx, NULL, 1);
147
0
}