/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 | 27.3k | { |
27 | 27.3k | CONF_IMODULE *m = OSSL_LIB_CTX_get_data(libctx, OSSL_LIB_CTX_SSL_CONF_IMODULE); |
28 | | |
29 | 27.3k | if (m != NULL) |
30 | 0 | return m; |
31 | | |
32 | 27.3k | libctx = OSSL_LIB_CTX_get0_global_default(); |
33 | 27.3k | return OSSL_LIB_CTX_get_data(libctx, OSSL_LIB_CTX_SSL_CONF_IMODULE); |
34 | 27.3k | } |
35 | | |
36 | | static int ssl_do_config(SSL *s, SSL_CTX *ctx, const char *name, int system) |
37 | 27.3k | { |
38 | 27.3k | SSL_CONF_CTX *cctx = NULL; |
39 | 27.3k | size_t i, idx, cmd_count; |
40 | 27.3k | int err = 1; |
41 | 27.3k | unsigned int flags; |
42 | 27.3k | unsigned int conf_diagnostics = 0; |
43 | 27.3k | const SSL_METHOD *meth; |
44 | 27.3k | const SSL_CONF_CMD *cmds; |
45 | 27.3k | OSSL_LIB_CTX *libctx = NULL, *prev_libctx = NULL; |
46 | 27.3k | CONF_IMODULE *imod = NULL; |
47 | | |
48 | 27.3k | if (s == NULL && ctx == NULL) { |
49 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); |
50 | 0 | goto err; |
51 | 0 | } |
52 | 27.3k | if (name == NULL && system) |
53 | 27.3k | name = "system_default"; |
54 | | |
55 | 27.3k | libctx = s != NULL ? s->ctx->libctx: ctx->libctx; |
56 | 27.3k | imod = ssl_do_lookup_module(libctx); |
57 | 27.3k | if (!conf_ssl_name_find(imod, name, &idx)) { |
58 | 27.3k | if (!system) |
59 | 0 | ERR_raise_data(ERR_LIB_SSL, SSL_R_INVALID_CONFIGURATION_NAME, |
60 | 0 | "name=%s", name); |
61 | 27.3k | goto err; |
62 | 27.3k | } |
63 | | |
64 | 0 | cmds = conf_ssl_get(imod, idx, &name, &cmd_count); |
65 | 0 | flags = SSL_CONF_FLAG_FILE; |
66 | 0 | if (!system) |
67 | 0 | flags |= SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE; |
68 | |
|
69 | 0 | cctx = SSL_CONF_CTX_new(); |
70 | 0 | if (cctx == NULL) { |
71 | | /* this is a fatal error, always report */ |
72 | 0 | system = 0; |
73 | 0 | goto err; |
74 | 0 | } |
75 | | |
76 | 0 | if (s != NULL) { |
77 | 0 | meth = s->method; |
78 | 0 | SSL_CONF_CTX_set_ssl(cctx, s); |
79 | 0 | libctx = s->ctx->libctx; |
80 | 0 | } else { |
81 | 0 | meth = ctx->method; |
82 | 0 | SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); |
83 | 0 | libctx = ctx->libctx; |
84 | 0 | } |
85 | |
|
86 | 0 | conf_diagnostics = OSSL_LIB_CTX_get_conf_diagnostics(libctx); |
87 | 0 | if (conf_diagnostics) |
88 | 0 | flags |= SSL_CONF_FLAG_SHOW_ERRORS; |
89 | 0 | if (meth->ssl_accept != ssl_undefined_function) |
90 | 0 | flags |= SSL_CONF_FLAG_SERVER; |
91 | 0 | if (meth->ssl_connect != ssl_undefined_function) |
92 | 0 | flags |= SSL_CONF_FLAG_CLIENT; |
93 | 0 | SSL_CONF_CTX_set_flags(cctx, flags); |
94 | 0 | prev_libctx = OSSL_LIB_CTX_set0_default(libctx); |
95 | 0 | err = 0; |
96 | 0 | for (i = 0; i < cmd_count; i++) { |
97 | 0 | char *cmdstr, *arg; |
98 | 0 | int rv; |
99 | |
|
100 | 0 | conf_ssl_get_cmd(cmds, i, &cmdstr, &arg); |
101 | 0 | rv = SSL_CONF_cmd(cctx, cmdstr, arg); |
102 | 0 | if (rv <= 0) |
103 | 0 | ++err; |
104 | 0 | } |
105 | 0 | if (!SSL_CONF_CTX_finish(cctx)) |
106 | 0 | ++err; |
107 | 27.3k | err: |
108 | 27.3k | OSSL_LIB_CTX_set0_default(prev_libctx); |
109 | 27.3k | SSL_CONF_CTX_free(cctx); |
110 | 27.3k | return err == 0 || (system && !conf_diagnostics); |
111 | 0 | } |
112 | | |
113 | | int SSL_config(SSL *s, const char *name) |
114 | 0 | { |
115 | 0 | return ssl_do_config(s, NULL, name, 0); |
116 | 0 | } |
117 | | |
118 | | int SSL_CTX_config(SSL_CTX *ctx, const char *name) |
119 | 0 | { |
120 | 0 | return ssl_do_config(NULL, ctx, name, 0); |
121 | 0 | } |
122 | | |
123 | | int ssl_ctx_system_config(SSL_CTX *ctx) |
124 | 103k | { |
125 | 103k | return ssl_do_config(NULL, ctx, NULL, 1); |
126 | 103k | } |