1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <stdio.h> 11e1051a39Sopenharmony_ci#include <openssl/conf.h> 12e1051a39Sopenharmony_ci#include <openssl/ssl.h> 13e1051a39Sopenharmony_ci#include "ssl_local.h" 14e1051a39Sopenharmony_ci#include "internal/sslconf.h" 15e1051a39Sopenharmony_ci 16e1051a39Sopenharmony_ci/* SSL library configuration module. */ 17e1051a39Sopenharmony_ci 18e1051a39Sopenharmony_civoid SSL_add_ssl_module(void) 19e1051a39Sopenharmony_ci{ 20e1051a39Sopenharmony_ci /* Do nothing. This will be added automatically by libcrypto */ 21e1051a39Sopenharmony_ci} 22e1051a39Sopenharmony_ci 23e1051a39Sopenharmony_cistatic int ssl_do_config(SSL *s, SSL_CTX *ctx, const char *name, int system) 24e1051a39Sopenharmony_ci{ 25e1051a39Sopenharmony_ci SSL_CONF_CTX *cctx = NULL; 26e1051a39Sopenharmony_ci size_t i, idx, cmd_count; 27e1051a39Sopenharmony_ci int rv = 0; 28e1051a39Sopenharmony_ci unsigned int flags; 29e1051a39Sopenharmony_ci const SSL_METHOD *meth; 30e1051a39Sopenharmony_ci const SSL_CONF_CMD *cmds; 31e1051a39Sopenharmony_ci OSSL_LIB_CTX *prev_libctx = NULL; 32e1051a39Sopenharmony_ci OSSL_LIB_CTX *libctx = NULL; 33e1051a39Sopenharmony_ci 34e1051a39Sopenharmony_ci if (s == NULL && ctx == NULL) { 35e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); 36e1051a39Sopenharmony_ci goto err; 37e1051a39Sopenharmony_ci } 38e1051a39Sopenharmony_ci 39e1051a39Sopenharmony_ci if (name == NULL && system) 40e1051a39Sopenharmony_ci name = "system_default"; 41e1051a39Sopenharmony_ci if (!conf_ssl_name_find(name, &idx)) { 42e1051a39Sopenharmony_ci if (!system) 43e1051a39Sopenharmony_ci ERR_raise_data(ERR_LIB_SSL, SSL_R_INVALID_CONFIGURATION_NAME, 44e1051a39Sopenharmony_ci "name=%s", name); 45e1051a39Sopenharmony_ci goto err; 46e1051a39Sopenharmony_ci } 47e1051a39Sopenharmony_ci cmds = conf_ssl_get(idx, &name, &cmd_count); 48e1051a39Sopenharmony_ci cctx = SSL_CONF_CTX_new(); 49e1051a39Sopenharmony_ci if (cctx == NULL) 50e1051a39Sopenharmony_ci goto err; 51e1051a39Sopenharmony_ci flags = SSL_CONF_FLAG_FILE; 52e1051a39Sopenharmony_ci if (!system) 53e1051a39Sopenharmony_ci flags |= SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE; 54e1051a39Sopenharmony_ci if (s != NULL) { 55e1051a39Sopenharmony_ci meth = s->method; 56e1051a39Sopenharmony_ci SSL_CONF_CTX_set_ssl(cctx, s); 57e1051a39Sopenharmony_ci libctx = s->ctx->libctx; 58e1051a39Sopenharmony_ci } else { 59e1051a39Sopenharmony_ci meth = ctx->method; 60e1051a39Sopenharmony_ci SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); 61e1051a39Sopenharmony_ci libctx = ctx->libctx; 62e1051a39Sopenharmony_ci } 63e1051a39Sopenharmony_ci if (meth->ssl_accept != ssl_undefined_function) 64e1051a39Sopenharmony_ci flags |= SSL_CONF_FLAG_SERVER; 65e1051a39Sopenharmony_ci if (meth->ssl_connect != ssl_undefined_function) 66e1051a39Sopenharmony_ci flags |= SSL_CONF_FLAG_CLIENT; 67e1051a39Sopenharmony_ci SSL_CONF_CTX_set_flags(cctx, flags); 68e1051a39Sopenharmony_ci prev_libctx = OSSL_LIB_CTX_set0_default(libctx); 69e1051a39Sopenharmony_ci for (i = 0; i < cmd_count; i++) { 70e1051a39Sopenharmony_ci char *cmdstr, *arg; 71e1051a39Sopenharmony_ci 72e1051a39Sopenharmony_ci conf_ssl_get_cmd(cmds, i, &cmdstr, &arg); 73e1051a39Sopenharmony_ci rv = SSL_CONF_cmd(cctx, cmdstr, arg); 74e1051a39Sopenharmony_ci if (rv <= 0) { 75e1051a39Sopenharmony_ci int errcode = rv == -2 ? SSL_R_UNKNOWN_COMMAND : SSL_R_BAD_VALUE; 76e1051a39Sopenharmony_ci 77e1051a39Sopenharmony_ci ERR_raise_data(ERR_LIB_SSL, errcode, 78e1051a39Sopenharmony_ci "section=%s, cmd=%s, arg=%s", name, cmdstr, arg); 79e1051a39Sopenharmony_ci goto err; 80e1051a39Sopenharmony_ci } 81e1051a39Sopenharmony_ci } 82e1051a39Sopenharmony_ci rv = SSL_CONF_CTX_finish(cctx); 83e1051a39Sopenharmony_ci err: 84e1051a39Sopenharmony_ci OSSL_LIB_CTX_set0_default(prev_libctx); 85e1051a39Sopenharmony_ci SSL_CONF_CTX_free(cctx); 86e1051a39Sopenharmony_ci return rv <= 0 ? 0 : 1; 87e1051a39Sopenharmony_ci} 88e1051a39Sopenharmony_ci 89e1051a39Sopenharmony_ciint SSL_config(SSL *s, const char *name) 90e1051a39Sopenharmony_ci{ 91e1051a39Sopenharmony_ci return ssl_do_config(s, NULL, name, 0); 92e1051a39Sopenharmony_ci} 93e1051a39Sopenharmony_ci 94e1051a39Sopenharmony_ciint SSL_CTX_config(SSL_CTX *ctx, const char *name) 95e1051a39Sopenharmony_ci{ 96e1051a39Sopenharmony_ci return ssl_do_config(NULL, ctx, name, 0); 97e1051a39Sopenharmony_ci} 98e1051a39Sopenharmony_ci 99e1051a39Sopenharmony_civoid ssl_ctx_system_config(SSL_CTX *ctx) 100e1051a39Sopenharmony_ci{ 101e1051a39Sopenharmony_ci ssl_do_config(NULL, ctx, NULL, 1); 102e1051a39Sopenharmony_ci} 103