2 * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/conf.h>
12 #include <openssl/ssl.h>
13 #include "ssl_local.h"
14 #include "internal/sslconf.h"
16 /* SSL library configuration module. */
18 void SSL_add_ssl_module(void)
20 /* Do nothing. This will be added automatically by libcrypto */
23 static int ssl_do_config(SSL *s, SSL_CTX *ctx, const char *name, int system)
25 SSL_CONF_CTX *cctx = NULL;
26 size_t i, idx, cmd_count;
29 const SSL_METHOD *meth;
30 const SSL_CONF_CMD *cmds;
31 OSSL_LIB_CTX *prev_libctx = NULL;
32 OSSL_LIB_CTX *libctx = NULL;
34 if (s == NULL && ctx == NULL) {
35 SSLerr(SSL_F_SSL_DO_CONFIG, ERR_R_PASSED_NULL_PARAMETER);
39 if (name == NULL && system)
40 name = "system_default";
41 if (!conf_ssl_name_find(name, &idx)) {
43 SSLerr(SSL_F_SSL_DO_CONFIG, SSL_R_INVALID_CONFIGURATION_NAME);
44 ERR_add_error_data(2, "name=", name);
48 cmds = conf_ssl_get(idx, &name, &cmd_count);
49 cctx = SSL_CONF_CTX_new();
52 flags = SSL_CONF_FLAG_FILE;
54 flags |= SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE;
57 SSL_CONF_CTX_set_ssl(cctx, s);
58 libctx = s->ctx->libctx;
61 SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
64 if (meth->ssl_accept != ssl_undefined_function)
65 flags |= SSL_CONF_FLAG_SERVER;
66 if (meth->ssl_connect != ssl_undefined_function)
67 flags |= SSL_CONF_FLAG_CLIENT;
68 SSL_CONF_CTX_set_flags(cctx, flags);
69 prev_libctx = OSSL_LIB_CTX_set0_default(libctx);
70 for (i = 0; i < cmd_count; i++) {
73 conf_ssl_get_cmd(cmds, i, &cmdstr, &arg);
74 rv = SSL_CONF_cmd(cctx, cmdstr, arg);
77 SSLerr(SSL_F_SSL_DO_CONFIG, SSL_R_UNKNOWN_COMMAND);
79 SSLerr(SSL_F_SSL_DO_CONFIG, SSL_R_BAD_VALUE);
80 ERR_add_error_data(6, "section=", name, ", cmd=", cmdstr,
85 rv = SSL_CONF_CTX_finish(cctx);
87 OSSL_LIB_CTX_set0_default(prev_libctx);
88 SSL_CONF_CTX_free(cctx);
89 return rv <= 0 ? 0 : 1;
92 int SSL_config(SSL *s, const char *name)
94 return ssl_do_config(s, NULL, name, 0);
97 int SSL_CTX_config(SSL_CTX *ctx, const char *name)
99 return ssl_do_config(NULL, ctx, name, 0);
102 void ssl_ctx_system_config(SSL_CTX *ctx)
104 ssl_do_config(NULL, ctx, NULL, 1);