/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 | if (s == NULL && ctx == NULL) { |
49 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); |
50 | 0 | goto err; |
51 | 0 | } |
52 | 0 | if (name == NULL && system) |
53 | 0 | name = "system_default"; |
54 | |
|
55 | 0 | if (name == NULL) { |
56 | 0 | ERR_raise_data(ERR_LIB_SSL, SSL_R_INVALID_CONFIGURATION_NAME, |
57 | 0 | "name not specified (name == NULL)"); |
58 | 0 | goto err; |
59 | 0 | } |
60 | | |
61 | 0 | libctx = s != NULL ? s->ctx->libctx: ctx->libctx; |
62 | 0 | imod = ssl_do_lookup_module(libctx); |
63 | 0 | if (!conf_ssl_name_find(imod, name, &idx)) { |
64 | 0 | if (!system) |
65 | 0 | ERR_raise_data(ERR_LIB_SSL, SSL_R_INVALID_CONFIGURATION_NAME, |
66 | 0 | "name=%s", name); |
67 | 0 | goto err; |
68 | 0 | } |
69 | | |
70 | 0 | cmds = conf_ssl_get(imod, idx, &name, &cmd_count); |
71 | 0 | flags = SSL_CONF_FLAG_FILE; |
72 | 0 | if (!system) |
73 | 0 | flags |= SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE; |
74 | |
|
75 | 0 | cctx = SSL_CONF_CTX_new(); |
76 | 0 | if (cctx == NULL) { |
77 | | /* this is a fatal error, always report */ |
78 | 0 | system = 0; |
79 | 0 | goto err; |
80 | 0 | } |
81 | | |
82 | 0 | if (s != NULL) { |
83 | 0 | meth = s->method; |
84 | 0 | SSL_CONF_CTX_set_ssl(cctx, s); |
85 | 0 | libctx = s->ctx->libctx; |
86 | 0 | } else { |
87 | 0 | meth = ctx->method; |
88 | 0 | SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); |
89 | 0 | libctx = ctx->libctx; |
90 | 0 | } |
91 | |
|
92 | 0 | conf_diagnostics = OSSL_LIB_CTX_get_conf_diagnostics(libctx); |
93 | 0 | if (conf_diagnostics) |
94 | 0 | flags |= SSL_CONF_FLAG_SHOW_ERRORS; |
95 | 0 | if (meth->ssl_accept != ssl_undefined_function) |
96 | 0 | flags |= SSL_CONF_FLAG_SERVER; |
97 | 0 | if (meth->ssl_connect != ssl_undefined_function) |
98 | 0 | flags |= SSL_CONF_FLAG_CLIENT; |
99 | 0 | SSL_CONF_CTX_set_flags(cctx, flags); |
100 | 0 | prev_libctx = OSSL_LIB_CTX_set0_default(libctx); |
101 | 0 | err = 0; |
102 | 0 | for (i = 0; i < cmd_count; i++) { |
103 | 0 | char *cmdstr, *arg; |
104 | 0 | int rv; |
105 | |
|
106 | 0 | conf_ssl_get_cmd(cmds, i, &cmdstr, &arg); |
107 | 0 | rv = SSL_CONF_cmd(cctx, cmdstr, arg); |
108 | 0 | if (rv <= 0) |
109 | 0 | ++err; |
110 | 0 | } |
111 | 0 | if (!SSL_CONF_CTX_finish(cctx)) |
112 | 0 | ++err; |
113 | 0 | err: |
114 | 0 | OSSL_LIB_CTX_set0_default(prev_libctx); |
115 | 0 | SSL_CONF_CTX_free(cctx); |
116 | 0 | return err == 0 || (system && !conf_diagnostics); |
117 | 0 | } |
118 | | |
119 | | int SSL_config(SSL *s, const char *name) |
120 | 0 | { |
121 | 0 | return ssl_do_config(s, NULL, name, 0); |
122 | 0 | } |
123 | | |
124 | | int SSL_CTX_config(SSL_CTX *ctx, const char *name) |
125 | 0 | { |
126 | 0 | return ssl_do_config(NULL, ctx, name, 0); |
127 | 0 | } |
128 | | |
129 | | int ssl_ctx_system_config(SSL_CTX *ctx) |
130 | 0 | { |
131 | 0 | return ssl_do_config(NULL, ctx, NULL, 1); |
132 | 0 | } |