2016-12-21 09:23:49 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2016-12-21 14:27:05 +00:00
|
|
|
#include <unistd.h>
|
2016-12-21 09:23:49 +00:00
|
|
|
|
|
|
|
#ifndef USE_OPENSSL_KERBEROS
|
|
|
|
#ifndef OPENSSL_NO_KRB5
|
|
|
|
#define OPENSSL_NO_KRB5
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
2016-12-21 14:27:05 +00:00
|
|
|
#include <openssl/bn.h>
|
2016-12-21 09:23:49 +00:00
|
|
|
#include <openssl/err.h>
|
2016-12-22 01:23:43 +00:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
#ifndef OPENSSL_NO_DH
|
|
|
|
#include <openssl/dh.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ! defined OPENSSL_NO_TLSEXT && ! defined SSL_CTRL_SET_TLSEXT_HOSTNAME
|
|
|
|
#define OPENSSL_NO_TLSEXT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
|
|
|
|
#ifndef OPENSSL_NO_ECDH
|
|
|
|
#include <openssl/ecdh.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
2016-12-21 09:23:49 +00:00
|
|
|
|
|
|
|
#include "base.h"
|
2018-09-09 05:50:33 +00:00
|
|
|
#include "http_header.h"
|
2016-12-21 09:23:49 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
typedef struct {
|
2016-12-22 01:23:43 +00:00
|
|
|
SSL_CTX *ssl_ctx; /* not patched */
|
|
|
|
/* SNI per host: with COMP_SERVER_SOCKET, COMP_HTTP_SCHEME, COMP_HTTP_HOST */
|
|
|
|
EVP_PKEY *ssl_pemfile_pkey;
|
|
|
|
X509 *ssl_pemfile_x509;
|
|
|
|
STACK_OF(X509_NAME) *ssl_ca_file_cert_names;
|
|
|
|
|
|
|
|
unsigned short ssl_verifyclient;
|
|
|
|
unsigned short ssl_verifyclient_enforce;
|
|
|
|
unsigned short ssl_verifyclient_depth;
|
|
|
|
unsigned short ssl_verifyclient_export_cert;
|
|
|
|
buffer *ssl_verifyclient_username;
|
|
|
|
|
|
|
|
unsigned short ssl_disable_client_renegotiation;
|
|
|
|
unsigned short ssl_read_ahead;
|
|
|
|
unsigned short ssl_log_noise;
|
|
|
|
|
|
|
|
/*(used only during startup; not patched)*/
|
|
|
|
unsigned short ssl_enabled; /* only interesting for setting up listening sockets. don't use at runtime */
|
|
|
|
unsigned short ssl_honor_cipher_order; /* determine SSL cipher in server-preferred order, not client-order */
|
|
|
|
unsigned short ssl_empty_fragments; /* whether to not set SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS */
|
|
|
|
unsigned short ssl_use_sslv2;
|
|
|
|
unsigned short ssl_use_sslv3;
|
|
|
|
buffer *ssl_pemfile;
|
|
|
|
buffer *ssl_ca_file;
|
2017-05-12 12:31:36 +00:00
|
|
|
buffer *ssl_ca_crl_file;
|
2017-05-16 03:12:36 +00:00
|
|
|
buffer *ssl_ca_dn_file;
|
2016-12-22 01:23:43 +00:00
|
|
|
buffer *ssl_cipher_list;
|
|
|
|
buffer *ssl_dh_file;
|
|
|
|
buffer *ssl_ec_curve;
|
2017-11-05 01:39:30 +00:00
|
|
|
array *ssl_conf_cmd;
|
2016-12-21 09:23:49 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
|
|
|
plugin_config **config_storage;
|
|
|
|
} plugin_data;
|
|
|
|
|
2016-12-22 01:23:43 +00:00
|
|
|
static int ssl_is_init;
|
|
|
|
/* need assigned p->id for deep access of module handler_ctx for connection
|
|
|
|
* i.e. handler_ctx *hctx = con->plugin_ctx[plugin_data_singleton->id]; */
|
|
|
|
static plugin_data *plugin_data_singleton;
|
2017-04-15 02:23:58 +00:00
|
|
|
#define LOCAL_SEND_BUFSIZE (64 * 1024)
|
|
|
|
static char *local_send_buffer;
|
2016-12-22 01:23:43 +00:00
|
|
|
|
2016-12-21 09:23:49 +00:00
|
|
|
typedef struct {
|
|
|
|
SSL *ssl;
|
2016-12-22 01:23:43 +00:00
|
|
|
connection *con;
|
2018-10-06 09:05:45 +00:00
|
|
|
int renegotiations; /* count of SSL_CB_HANDSHAKE_START */
|
2016-12-21 09:23:49 +00:00
|
|
|
int request_env_patched;
|
|
|
|
plugin_config conf;
|
2017-05-09 19:34:38 +00:00
|
|
|
server *srv;
|
2016-12-21 09:23:49 +00:00
|
|
|
} handler_ctx;
|
|
|
|
|
|
|
|
|
|
|
|
static handler_ctx *
|
|
|
|
handler_ctx_init (void)
|
|
|
|
{
|
|
|
|
handler_ctx *hctx = calloc(1, sizeof(*hctx));
|
|
|
|
force_assert(hctx);
|
|
|
|
return hctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
handler_ctx_free (handler_ctx *hctx)
|
|
|
|
{
|
2016-12-22 01:23:43 +00:00
|
|
|
if (hctx->ssl) SSL_free(hctx->ssl);
|
2016-12-21 09:23:49 +00:00
|
|
|
free(hctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
INIT_FUNC(mod_openssl_init)
|
|
|
|
{
|
2016-12-22 01:23:43 +00:00
|
|
|
plugin_data_singleton = (plugin_data *)calloc(1, sizeof(plugin_data));
|
|
|
|
return plugin_data_singleton;
|
2016-12-21 09:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FREE_FUNC(mod_openssl_free)
|
|
|
|
{
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
if (!p) return HANDLER_GO_ON;
|
|
|
|
|
|
|
|
if (p->config_storage) {
|
2016-12-21 14:27:05 +00:00
|
|
|
for (size_t i = 0; i < srv->config_context->used; ++i) {
|
2016-12-22 01:23:43 +00:00
|
|
|
plugin_config *s = p->config_storage[i];
|
2017-03-13 22:50:15 +00:00
|
|
|
int copy;
|
|
|
|
if (NULL == s) continue;
|
|
|
|
copy = s->ssl_enabled && buffer_string_is_empty(s->ssl_pemfile);
|
2016-12-21 14:27:05 +00:00
|
|
|
buffer_free(s->ssl_pemfile);
|
|
|
|
buffer_free(s->ssl_ca_file);
|
2017-05-12 12:31:36 +00:00
|
|
|
buffer_free(s->ssl_ca_crl_file);
|
2017-05-16 03:12:36 +00:00
|
|
|
buffer_free(s->ssl_ca_dn_file);
|
2016-12-21 14:27:05 +00:00
|
|
|
buffer_free(s->ssl_cipher_list);
|
|
|
|
buffer_free(s->ssl_dh_file);
|
|
|
|
buffer_free(s->ssl_ec_curve);
|
|
|
|
buffer_free(s->ssl_verifyclient_username);
|
2017-11-05 01:39:30 +00:00
|
|
|
array_free(s->ssl_conf_cmd);
|
|
|
|
|
2017-02-02 13:48:29 +00:00
|
|
|
if (copy) continue;
|
2016-12-21 14:27:05 +00:00
|
|
|
SSL_CTX_free(s->ssl_ctx);
|
|
|
|
EVP_PKEY_free(s->ssl_pemfile_pkey);
|
|
|
|
X509_free(s->ssl_pemfile_x509);
|
|
|
|
if (NULL != s->ssl_ca_file_cert_names)
|
|
|
|
sk_X509_NAME_pop_free(s->ssl_ca_file_cert_names,X509_NAME_free);
|
|
|
|
}
|
2016-12-21 09:23:49 +00:00
|
|
|
for (size_t i = 0; i < srv->config_context->used; ++i) {
|
|
|
|
plugin_config *s = p->config_storage[i];
|
|
|
|
if (NULL == s) continue;
|
|
|
|
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
free(p->config_storage);
|
|
|
|
}
|
|
|
|
|
2016-12-22 01:23:43 +00:00
|
|
|
if (ssl_is_init) {
|
2016-12-21 14:27:05 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L \
|
|
|
|
&& !defined(LIBRESSL_VERSION_NUMBER)
|
|
|
|
/*(OpenSSL libraries handle thread init and deinit)
|
|
|
|
* https://github.com/openssl/openssl/pull/1048 */
|
|
|
|
#else
|
|
|
|
CRYPTO_cleanup_all_ex_data();
|
|
|
|
ERR_free_strings();
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
|
|
|
|
ERR_remove_thread_state(NULL);
|
|
|
|
#else
|
|
|
|
ERR_remove_state(0);
|
|
|
|
#endif
|
|
|
|
EVP_cleanup();
|
|
|
|
#endif
|
2017-04-15 02:23:58 +00:00
|
|
|
|
|
|
|
free(local_send_buffer);
|
2016-12-21 14:27:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 01:23:43 +00:00
|
|
|
free(p);
|
|
|
|
|
2016-12-21 09:23:49 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-21 04:26:10 +00:00
|
|
|
static int
|
|
|
|
safer_X509_NAME_oneline(X509_NAME *name, char *buf, size_t sz)
|
|
|
|
{
|
|
|
|
BIO *bio = BIO_new(BIO_s_mem());
|
|
|
|
if (bio) {
|
|
|
|
int len = X509_NAME_print_ex(bio, name, 0, XN_FLAG_ONELINE);
|
|
|
|
BIO_gets(bio, buf, (int)sz); /*(may be truncated if len >= sz)*/
|
|
|
|
BIO_free(bio);
|
|
|
|
return len; /*return value has similar semantics to that of snprintf()*/
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buf[0] = '\0';
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-22 01:23:43 +00:00
|
|
|
static void
|
|
|
|
ssl_info_callback (const SSL *ssl, int where, int ret)
|
|
|
|
{
|
|
|
|
UNUSED(ret);
|
|
|
|
|
|
|
|
if (0 != (where & SSL_CB_HANDSHAKE_START)) {
|
|
|
|
handler_ctx *hctx = (handler_ctx *) SSL_get_app_data(ssl);
|
2018-10-06 09:05:45 +00:00
|
|
|
if (hctx->renegotiations >= 0) ++hctx->renegotiations;
|
2016-12-22 01:23:43 +00:00
|
|
|
}
|
2018-10-06 09:05:45 +00:00
|
|
|
#ifdef TLS1_3_VERSION
|
|
|
|
/* https://github.com/openssl/openssl/issues/5721
|
|
|
|
* "TLSv1.3 unexpected InfoCallback after handshake completed" */
|
|
|
|
if (0 != (where & SSL_CB_HANDSHAKE_DONE)) {
|
|
|
|
/* SSL_version() is valid after initial handshake completed */
|
|
|
|
if (SSL_version(ssl) >= TLS1_3_VERSION) {
|
|
|
|
/* https://wiki.openssl.org/index.php/TLS1.3
|
|
|
|
* "Renegotiation is not possible in a TLSv1.3 connection" */
|
|
|
|
handler_ctx *hctx = (handler_ctx *) SSL_get_app_data(ssl);
|
|
|
|
hctx->renegotiations = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-12-22 01:23:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 19:34:38 +00:00
|
|
|
/* https://wiki.openssl.org/index.php/Manual:SSL_CTX_set_verify(3)#EXAMPLES */
|
|
|
|
static int
|
|
|
|
verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
X509 *err_cert;
|
|
|
|
int err, depth;
|
|
|
|
SSL *ssl;
|
|
|
|
handler_ctx *hctx;
|
|
|
|
server *srv;
|
|
|
|
|
|
|
|
err = X509_STORE_CTX_get_error(ctx);
|
|
|
|
depth = X509_STORE_CTX_get_error_depth(ctx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve the pointer to the SSL of the connection currently treated
|
|
|
|
* and the application specific data stored into the SSL object.
|
|
|
|
*/
|
|
|
|
ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
|
|
|
|
hctx = (handler_ctx *) SSL_get_app_data(ssl);
|
|
|
|
srv = hctx->srv;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Catch a too long certificate chain. The depth limit set using
|
|
|
|
* SSL_CTX_set_verify_depth() is by purpose set to "limit+1" so
|
|
|
|
* that whenever the "depth>verify_depth" condition is met, we
|
|
|
|
* have violated the limit and want to log this error condition.
|
|
|
|
* We must do it here, because the CHAIN_TOO_LONG error would not
|
|
|
|
* be found explicitly; only errors introduced by cutting off the
|
|
|
|
* additional certificates would be logged.
|
|
|
|
*/
|
|
|
|
if (depth > hctx->conf.ssl_verifyclient_depth) {
|
|
|
|
preverify_ok = 0;
|
|
|
|
err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
|
|
|
|
X509_STORE_CTX_set_error(ctx, err);
|
|
|
|
}
|
|
|
|
|
2017-05-24 02:28:06 +00:00
|
|
|
if (preverify_ok && 0 == depth
|
|
|
|
&& !buffer_string_is_empty(hctx->conf.ssl_ca_dn_file)
|
|
|
|
&& !buffer_string_is_empty(hctx->conf.ssl_ca_file)) {
|
|
|
|
/* verify that client cert is issued by CA in ssl.ca-dn-file
|
|
|
|
* if both ssl.ca-dn-file and ssl.ca-file were configured */
|
|
|
|
STACK_OF(X509_NAME) * const names = hctx->conf.ssl_ca_file_cert_names;
|
|
|
|
X509_NAME *issuer;
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
err_cert = X509_STORE_CTX_get_current_cert(ctx);
|
|
|
|
#else
|
|
|
|
err_cert = ctx->current_cert;
|
|
|
|
#endif
|
|
|
|
if (NULL == err_cert) return !hctx->conf.ssl_verifyclient_enforce;
|
|
|
|
issuer = X509_get_issuer_name(err_cert);
|
|
|
|
#if 0 /*(?desirable/undesirable to have ssl_ca_file_cert_names sorted?)*/
|
|
|
|
if (-1 != sk_X509_NAME_find(names, issuer))
|
|
|
|
return preverify_ok; /* match */
|
|
|
|
#else
|
|
|
|
for (int i = 0, len = sk_X509_NAME_num(names); i < len; ++i) {
|
|
|
|
if (0 == X509_NAME_cmp(sk_X509_NAME_value(names, i), issuer))
|
|
|
|
return preverify_ok; /* match */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
preverify_ok = 0;
|
|
|
|
err = X509_V_ERR_CERT_REJECTED;
|
|
|
|
X509_STORE_CTX_set_error(ctx, err);
|
|
|
|
}
|
|
|
|
|
2017-05-09 19:34:38 +00:00
|
|
|
if (preverify_ok) {
|
2017-05-17 04:44:55 +00:00
|
|
|
return preverify_ok;
|
2017-05-09 19:34:38 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:44:55 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2017-05-09 19:34:38 +00:00
|
|
|
err_cert = X509_STORE_CTX_get_current_cert(ctx);
|
2017-05-17 04:44:55 +00:00
|
|
|
#else
|
|
|
|
err_cert = ctx->current_cert;
|
|
|
|
#endif
|
|
|
|
if (NULL == err_cert) return !hctx->conf.ssl_verifyclient_enforce;
|
2017-05-21 04:26:10 +00:00
|
|
|
safer_X509_NAME_oneline(X509_get_subject_name(err_cert),buf,sizeof(buf));
|
2017-05-09 19:34:38 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "SDSSSDSS",
|
|
|
|
"SSL: verify error:num=", err, ":",
|
|
|
|
X509_verify_cert_error_string(err), ":depth=", depth,
|
|
|
|
":subject=", buf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, err contains the last verification error. We can use
|
|
|
|
* it for something special
|
|
|
|
*/
|
|
|
|
if (!preverify_ok && (err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY ||
|
|
|
|
err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT)) {
|
2017-05-21 04:26:10 +00:00
|
|
|
safer_X509_NAME_oneline(X509_get_issuer_name(err_cert),buf,sizeof(buf));
|
2017-05-09 19:34:38 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "SS", "SSL: issuer=", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !hctx->conf.ssl_verifyclient_enforce;
|
|
|
|
}
|
2016-12-22 01:23:43 +00:00
|
|
|
|
|
|
|
#ifndef OPENSSL_NO_TLSEXT
|
|
|
|
static int mod_openssl_patch_connection (server *srv, connection *con, handler_ctx *hctx);
|
|
|
|
|
|
|
|
static int
|
|
|
|
network_ssl_servername_callback (SSL *ssl, int *al, server *srv)
|
|
|
|
{
|
|
|
|
const char *servername;
|
|
|
|
handler_ctx *hctx = (handler_ctx *) SSL_get_app_data(ssl);
|
|
|
|
connection *con = hctx->con;
|
|
|
|
UNUSED(al);
|
|
|
|
|
|
|
|
buffer_copy_string(con->uri.scheme, "https");
|
|
|
|
|
|
|
|
servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
|
|
|
|
if (NULL == servername) {
|
|
|
|
#if 0
|
|
|
|
/* this "error" just means the client didn't support it */
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
|
|
|
|
"failed to get TLS server name");
|
|
|
|
#endif
|
|
|
|
return SSL_TLSEXT_ERR_NOACK;
|
|
|
|
}
|
2017-01-03 07:49:06 +00:00
|
|
|
/* use SNI to patch mod_openssl config and then reset COMP_HTTP_HOST */
|
|
|
|
buffer_copy_string(con->uri.authority, servername);
|
|
|
|
buffer_to_lower(con->uri.authority);
|
2016-12-22 01:23:43 +00:00
|
|
|
|
|
|
|
con->conditional_is_valid[COMP_HTTP_SCHEME] = 1;
|
|
|
|
con->conditional_is_valid[COMP_HTTP_HOST] = 1;
|
|
|
|
mod_openssl_patch_connection(srv, con, hctx);
|
2017-01-03 07:49:06 +00:00
|
|
|
/* reset COMP_HTTP_HOST so that conditions re-run after request hdrs read */
|
|
|
|
/*(done in response.c:config_cond_cache_reset() after request hdrs read)*/
|
|
|
|
/*config_cond_cache_reset_item(con, COMP_HTTP_HOST);*/
|
|
|
|
/*buffer_reset(con->uri.authority);*/
|
2016-12-22 01:23:43 +00:00
|
|
|
|
|
|
|
if (NULL == hctx->conf.ssl_pemfile_x509
|
|
|
|
|| NULL == hctx->conf.ssl_pemfile_pkey) {
|
|
|
|
/* x509/pkey available <=> pemfile was set <=> pemfile got patched:
|
|
|
|
* so this should never happen, unless you nest $SERVER["socket"] */
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
|
|
|
|
"no certificate/private key for TLS server name",
|
2017-01-03 07:49:06 +00:00
|
|
|
con->uri.authority);
|
2016-12-22 01:23:43 +00:00
|
|
|
return SSL_TLSEXT_ERR_ALERT_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* first set certificate!
|
|
|
|
* setting private key checks whether certificate matches it */
|
2017-11-04 18:08:41 +00:00
|
|
|
if (1 != SSL_use_certificate(ssl, hctx->conf.ssl_pemfile_x509)) {
|
2016-12-22 01:23:43 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssb:s", "SSL:",
|
|
|
|
"failed to set certificate for TLS server name",
|
2017-01-03 07:49:06 +00:00
|
|
|
con->uri.authority,
|
2016-12-22 01:23:43 +00:00
|
|
|
ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
return SSL_TLSEXT_ERR_ALERT_FATAL;
|
|
|
|
}
|
|
|
|
|
2017-11-04 18:08:41 +00:00
|
|
|
if (1 != SSL_use_PrivateKey(ssl, hctx->conf.ssl_pemfile_pkey)) {
|
2016-12-22 01:23:43 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssb:s", "SSL:",
|
|
|
|
"failed to set private key for TLS server name",
|
2017-01-03 07:49:06 +00:00
|
|
|
con->uri.authority,
|
2016-12-22 01:23:43 +00:00
|
|
|
ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
return SSL_TLSEXT_ERR_ALERT_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hctx->conf.ssl_verifyclient) {
|
|
|
|
int mode;
|
|
|
|
if (NULL == hctx->conf.ssl_ca_file_cert_names) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssb:s", "SSL:",
|
|
|
|
"can't verify client without ssl.ca-file "
|
2017-05-24 02:28:06 +00:00
|
|
|
"or ssl.ca-dn-file for TLS server name",
|
|
|
|
con->uri.authority,
|
2016-12-22 01:23:43 +00:00
|
|
|
ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
return SSL_TLSEXT_ERR_ALERT_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_set_client_CA_list(
|
|
|
|
ssl, SSL_dup_CA_list(hctx->conf.ssl_ca_file_cert_names));
|
|
|
|
/* forcing verification here is really not that useful
|
|
|
|
* -- a client could just connect without SNI */
|
|
|
|
mode = SSL_VERIFY_PEER;
|
|
|
|
if (hctx->conf.ssl_verifyclient_enforce) {
|
|
|
|
mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
|
|
|
|
}
|
2017-05-09 19:34:38 +00:00
|
|
|
SSL_set_verify(ssl, mode, verify_callback);
|
|
|
|
SSL_set_verify_depth(ssl, hctx->conf.ssl_verifyclient_depth + 1);
|
2016-12-22 01:23:43 +00:00
|
|
|
} else {
|
|
|
|
SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static X509 *
|
|
|
|
x509_load_pem_file (server *srv, const char *file)
|
|
|
|
{
|
|
|
|
BIO *in;
|
|
|
|
X509 *x = NULL;
|
|
|
|
|
|
|
|
in = BIO_new(BIO_s_file());
|
|
|
|
if (NULL == in) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "S",
|
|
|
|
"SSL: BIO_new(BIO_s_file()) failed");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BIO_read_filename(in,file) <= 0) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "SSS",
|
|
|
|
"SSL: BIO_read_filename('", file,"') failed");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
x = PEM_read_bio_X509(in, NULL, NULL, NULL);
|
|
|
|
if (NULL == x) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "SSS",
|
|
|
|
"SSL: couldn't read X509 certificate from '", file,"'");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_free(in);
|
|
|
|
return x;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (NULL != in) BIO_free(in);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static EVP_PKEY *
|
|
|
|
evp_pkey_load_pem_file (server *srv, const char *file)
|
|
|
|
{
|
|
|
|
BIO *in;
|
|
|
|
EVP_PKEY *x = NULL;
|
|
|
|
|
|
|
|
in = BIO_new(BIO_s_file());
|
|
|
|
if (NULL == in) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s",
|
|
|
|
"SSL: BIO_new(BIO_s_file()) failed");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BIO_read_filename(in,file) <= 0) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "SSS",
|
|
|
|
"SSL: BIO_read_filename('", file,"') failed");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
x = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
|
|
|
|
if (NULL == x) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "SSS",
|
|
|
|
"SSL: couldn't read private key from '", file,"'");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_free(in);
|
|
|
|
return x;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (NULL != in) BIO_free(in);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
network_openssl_load_pemfile (server *srv, plugin_config *s, size_t ndx)
|
|
|
|
{
|
|
|
|
#ifdef OPENSSL_NO_TLSEXT
|
|
|
|
data_config *dc = (data_config *)srv->config_context->data[ndx];
|
|
|
|
if ((ndx > 0 && (COMP_SERVER_SOCKET != dc->comp
|
|
|
|
|| dc->cond != CONFIG_COND_EQ)) || !s->ssl_enabled) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
|
|
|
|
"ssl.pemfile only works in SSL socket binding context "
|
|
|
|
"as openssl version does not support TLS extensions");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
UNUSED(ndx);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
s->ssl_pemfile_x509 = x509_load_pem_file(srv, s->ssl_pemfile->ptr);
|
|
|
|
if (NULL == s->ssl_pemfile_x509) return -1;
|
|
|
|
s->ssl_pemfile_pkey = evp_pkey_load_pem_file(srv, s->ssl_pemfile->ptr);
|
|
|
|
if (NULL == s->ssl_pemfile_pkey) return -1;
|
|
|
|
|
|
|
|
if (!X509_check_private_key(s->ssl_pemfile_x509, s->ssl_pemfile_pkey)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sssb", "SSL:",
|
|
|
|
"Private key does not match the certificate public key,"
|
|
|
|
" reason:", ERR_error_string(ERR_get_error(), NULL),
|
|
|
|
s->ssl_pemfile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-05 01:39:30 +00:00
|
|
|
static int
|
|
|
|
network_openssl_ssl_conf_cmd (server *srv, plugin_config *s)
|
|
|
|
{
|
|
|
|
#ifdef SSL_CONF_FLAG_CMDLINE
|
|
|
|
|
|
|
|
int rc = 0;
|
|
|
|
data_string *ds;
|
|
|
|
SSL_CONF_CTX * const cctx = SSL_CONF_CTX_new();
|
|
|
|
SSL_CONF_CTX_set_ssl_ctx(cctx, s->ssl_ctx);
|
|
|
|
SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE
|
|
|
|
| SSL_CONF_FLAG_SERVER
|
|
|
|
| SSL_CONF_FLAG_SHOW_ERRORS
|
|
|
|
| SSL_CONF_FLAG_CERTIFICATE);
|
|
|
|
|
|
|
|
/* always disable null and export ciphers */
|
|
|
|
ds = (data_string *)
|
|
|
|
array_get_element_klen(s->ssl_conf_cmd,
|
|
|
|
CONST_STR_LEN("CipherString"));
|
|
|
|
if (NULL != ds) {
|
|
|
|
buffer_append_string_len(ds->value,
|
|
|
|
CONST_STR_LEN(":!aNULL:!eNULL:!EXP"));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < s->ssl_conf_cmd->used; ++i) {
|
|
|
|
ds = (data_string *)s->ssl_conf_cmd->data[i];
|
|
|
|
ERR_clear_error();
|
|
|
|
if (SSL_CONF_cmd(cctx, ds->key->ptr, ds->value->ptr) <= 0) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssbbss", "SSL:",
|
|
|
|
"SSL_CONF_cmd", ds->key, ds->value, ":",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == rc && 1 != SSL_CONF_CTX_finish(cctx)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sss", "SSL:",
|
|
|
|
"SSL_CONF_CTX_finish():",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_CONF_CTX_free(cctx);
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
UNUSED(s);
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
|
|
|
|
"ssl.openssl.ssl-conf-cmd not available; ignored");
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-22 01:23:43 +00:00
|
|
|
static int
|
|
|
|
network_init_ssl (server *srv, void *p_d)
|
|
|
|
{
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
|
|
|
|
#ifndef OPENSSL_NO_DH
|
|
|
|
/* 1024-bit MODP Group with 160-bit prime order subgroup (RFC5114)
|
|
|
|
* -----BEGIN DH PARAMETERS-----
|
|
|
|
* MIIBDAKBgQCxC4+WoIDgHd6S3l6uXVTsUsmfvPsGo8aaap3KUtI7YWBz4oZ1oj0Y
|
|
|
|
* mDjvHi7mUsAT7LSuqQYRIySXXDzUm4O/rMvdfZDEvXCYSI6cIZpzck7/1vrlZEc4
|
|
|
|
* +qMaT/VbzMChUa9fDci0vUW/N982XBpl5oz9p21NpwjfH7K8LkpDcQKBgQCk0cvV
|
|
|
|
* w/00EmdlpELvuZkF+BBN0lisUH/WQGz/FCZtMSZv6h5cQVZLd35pD1UE8hMWAhe0
|
|
|
|
* sBuIal6RVH+eJ0n01/vX07mpLuGQnQ0iY/gKdqaiTAh6CR9THb8KAWm2oorWYqTR
|
|
|
|
* jnOvoy13nVkY0IvIhY9Nzvl8KiSFXm7rIrOy5QICAKA=
|
|
|
|
* -----END DH PARAMETERS-----
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const unsigned char dh1024_p[]={
|
|
|
|
0xB1,0x0B,0x8F,0x96,0xA0,0x80,0xE0,0x1D,0xDE,0x92,0xDE,0x5E,
|
|
|
|
0xAE,0x5D,0x54,0xEC,0x52,0xC9,0x9F,0xBC,0xFB,0x06,0xA3,0xC6,
|
|
|
|
0x9A,0x6A,0x9D,0xCA,0x52,0xD2,0x3B,0x61,0x60,0x73,0xE2,0x86,
|
|
|
|
0x75,0xA2,0x3D,0x18,0x98,0x38,0xEF,0x1E,0x2E,0xE6,0x52,0xC0,
|
|
|
|
0x13,0xEC,0xB4,0xAE,0xA9,0x06,0x11,0x23,0x24,0x97,0x5C,0x3C,
|
|
|
|
0xD4,0x9B,0x83,0xBF,0xAC,0xCB,0xDD,0x7D,0x90,0xC4,0xBD,0x70,
|
|
|
|
0x98,0x48,0x8E,0x9C,0x21,0x9A,0x73,0x72,0x4E,0xFF,0xD6,0xFA,
|
|
|
|
0xE5,0x64,0x47,0x38,0xFA,0xA3,0x1A,0x4F,0xF5,0x5B,0xCC,0xC0,
|
|
|
|
0xA1,0x51,0xAF,0x5F,0x0D,0xC8,0xB4,0xBD,0x45,0xBF,0x37,0xDF,
|
|
|
|
0x36,0x5C,0x1A,0x65,0xE6,0x8C,0xFD,0xA7,0x6D,0x4D,0xA7,0x08,
|
|
|
|
0xDF,0x1F,0xB2,0xBC,0x2E,0x4A,0x43,0x71,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char dh1024_g[]={
|
|
|
|
0xA4,0xD1,0xCB,0xD5,0xC3,0xFD,0x34,0x12,0x67,0x65,0xA4,0x42,
|
|
|
|
0xEF,0xB9,0x99,0x05,0xF8,0x10,0x4D,0xD2,0x58,0xAC,0x50,0x7F,
|
|
|
|
0xD6,0x40,0x6C,0xFF,0x14,0x26,0x6D,0x31,0x26,0x6F,0xEA,0x1E,
|
|
|
|
0x5C,0x41,0x56,0x4B,0x77,0x7E,0x69,0x0F,0x55,0x04,0xF2,0x13,
|
|
|
|
0x16,0x02,0x17,0xB4,0xB0,0x1B,0x88,0x6A,0x5E,0x91,0x54,0x7F,
|
|
|
|
0x9E,0x27,0x49,0xF4,0xD7,0xFB,0xD7,0xD3,0xB9,0xA9,0x2E,0xE1,
|
|
|
|
0x90,0x9D,0x0D,0x22,0x63,0xF8,0x0A,0x76,0xA6,0xA2,0x4C,0x08,
|
|
|
|
0x7A,0x09,0x1F,0x53,0x1D,0xBF,0x0A,0x01,0x69,0xB6,0xA2,0x8A,
|
|
|
|
0xD6,0x62,0xA4,0xD1,0x8E,0x73,0xAF,0xA3,0x2D,0x77,0x9D,0x59,
|
|
|
|
0x18,0xD0,0x8B,0xC8,0x85,0x8F,0x4D,0xCE,0xF9,0x7C,0x2A,0x24,
|
|
|
|
0x85,0x5E,0x6E,0xEB,0x22,0xB3,0xB2,0xE5,
|
|
|
|
};
|
2018-01-07 00:04:08 +00:00
|
|
|
#endif
|
2016-12-22 01:23:43 +00:00
|
|
|
|
|
|
|
/* load SSL certificates */
|
|
|
|
for (size_t i = 0; i < srv->config_context->used; ++i) {
|
|
|
|
plugin_config *s = p->config_storage[i];
|
|
|
|
#ifndef SSL_OP_NO_COMPRESSION
|
|
|
|
#define SSL_OP_NO_COMPRESSION 0
|
|
|
|
#endif
|
|
|
|
#ifndef SSL_MODE_RELEASE_BUFFERS /* OpenSSL >= 1.0.0 */
|
|
|
|
#define SSL_MODE_RELEASE_BUFFERS 0
|
|
|
|
#endif
|
|
|
|
long ssloptions = SSL_OP_ALL
|
|
|
|
| SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
|
|
|
|
| SSL_OP_NO_COMPRESSION;
|
|
|
|
|
|
|
|
if (s->ssl_enabled) {
|
|
|
|
if (buffer_string_is_empty(s->ssl_pemfile)) {
|
2017-02-02 13:48:29 +00:00
|
|
|
/* inherit ssl settings from global scope
|
|
|
|
* (if only ssl.engine = "enable" and no other ssl.* settings)*/
|
|
|
|
if (0 != i && p->config_storage[0]->ssl_enabled) {
|
|
|
|
s->ssl_ctx = p->config_storage[0]->ssl_ctx;
|
|
|
|
continue;
|
|
|
|
}
|
2016-12-22 01:23:43 +00:00
|
|
|
/* PEM file is require */
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s",
|
2017-11-03 03:11:39 +00:00
|
|
|
"ssl.pemfile has to be set "
|
|
|
|
"when ssl.engine = \"enable\"");
|
2016-12-22 01:23:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer_string_is_empty(s->ssl_pemfile)
|
2017-05-24 02:28:06 +00:00
|
|
|
&& buffer_string_is_empty(s->ssl_ca_dn_file)
|
2016-12-22 01:23:43 +00:00
|
|
|
&& buffer_string_is_empty(s->ssl_ca_file)) continue;
|
|
|
|
|
|
|
|
if (ssl_is_init == 0) {
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L \
|
|
|
|
&& !defined(LIBRESSL_VERSION_NUMBER)
|
|
|
|
OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS
|
|
|
|
|OPENSSL_INIT_LOAD_CRYPTO_STRINGS,NULL);
|
|
|
|
OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
|
|
|
|
|OPENSSL_INIT_ADD_ALL_DIGESTS
|
|
|
|
|OPENSSL_INIT_LOAD_CONFIG, NULL);
|
|
|
|
#else
|
|
|
|
SSL_load_error_strings();
|
|
|
|
SSL_library_init();
|
|
|
|
OpenSSL_add_all_algorithms();
|
|
|
|
#endif
|
|
|
|
ssl_is_init = 1;
|
|
|
|
|
|
|
|
if (0 == RAND_status()) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
|
|
|
|
"not enough entropy in the pool");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-04-15 02:23:58 +00:00
|
|
|
|
|
|
|
local_send_buffer = malloc(LOCAL_SEND_BUFSIZE);
|
|
|
|
force_assert(NULL != local_send_buffer);
|
2016-12-22 01:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!buffer_string_is_empty(s->ssl_pemfile)) {
|
|
|
|
#ifdef OPENSSL_NO_TLSEXT
|
|
|
|
data_config *dc = (data_config *)srv->config_context->data[i];
|
|
|
|
if (COMP_HTTP_HOST == dc->comp) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
|
|
|
|
"can't use ssl.pemfile with $HTTP[\"host\"], "
|
|
|
|
"openssl version does not support TLS "
|
|
|
|
"extensions");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (network_openssl_load_pemfile(srv, s, i)) return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-16 03:12:36 +00:00
|
|
|
if (!buffer_string_is_empty(s->ssl_ca_dn_file)) {
|
|
|
|
s->ssl_ca_file_cert_names =
|
|
|
|
SSL_load_client_CA_file(s->ssl_ca_dn_file->ptr);
|
|
|
|
if (NULL == s->ssl_ca_file_cert_names) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL),
|
|
|
|
s->ssl_ca_dn_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == s->ssl_ca_file_cert_names
|
2017-05-17 04:44:55 +00:00
|
|
|
&& !buffer_string_is_empty(s->ssl_ca_file)) {
|
2016-12-22 01:23:43 +00:00
|
|
|
s->ssl_ca_file_cert_names =
|
|
|
|
SSL_load_client_CA_file(s->ssl_ca_file->ptr);
|
|
|
|
if (NULL == s->ssl_ca_file_cert_names) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL),
|
|
|
|
s->ssl_ca_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer_string_is_empty(s->ssl_pemfile) || !s->ssl_enabled) continue;
|
|
|
|
|
|
|
|
if (NULL == (s->ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* completely useless identifier;
|
|
|
|
* required for client cert verification to work with sessions */
|
|
|
|
if (0 == SSL_CTX_set_session_id_context(
|
|
|
|
s->ssl_ctx,(const unsigned char*)CONST_STR_LEN("lighttpd"))){
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss:s", "SSL:",
|
|
|
|
"failed to set session context",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->ssl_empty_fragments) {
|
|
|
|
#ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
|
|
|
|
ssloptions &= ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
|
|
|
|
#else
|
|
|
|
ssloptions &= ~0x00000800L; /* hardcode constant */
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss", "WARNING: SSL:",
|
|
|
|
"'insert empty fragments' not supported by the "
|
|
|
|
"openssl version used to compile lighttpd with");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_CTX_set_options(s->ssl_ctx, ssloptions);
|
|
|
|
SSL_CTX_set_info_callback(s->ssl_ctx, ssl_info_callback);
|
|
|
|
|
|
|
|
if (!s->ssl_use_sslv2 && 0 != SSL_OP_NO_SSLv2) {
|
|
|
|
/* disable SSLv2 */
|
|
|
|
if ((SSL_OP_NO_SSLv2
|
|
|
|
& SSL_CTX_set_options(s->ssl_ctx, SSL_OP_NO_SSLv2))
|
|
|
|
!= SSL_OP_NO_SSLv2) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s->ssl_use_sslv3 && 0 != SSL_OP_NO_SSLv3) {
|
|
|
|
/* disable SSLv3 */
|
|
|
|
if ((SSL_OP_NO_SSLv3
|
|
|
|
& SSL_CTX_set_options(s->ssl_ctx, SSL_OP_NO_SSLv3))
|
|
|
|
!= SSL_OP_NO_SSLv3) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!buffer_string_is_empty(s->ssl_cipher_list)) {
|
|
|
|
/* Disable support for low encryption ciphers */
|
|
|
|
if (SSL_CTX_set_cipher_list(s->ssl_ctx,s->ssl_cipher_list->ptr)!=1){
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->ssl_honor_cipher_order) {
|
|
|
|
SSL_CTX_set_options(s->ssl_ctx,SSL_OP_CIPHER_SERVER_PREFERENCE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef OPENSSL_NO_DH
|
2018-01-07 00:04:08 +00:00
|
|
|
{
|
|
|
|
DH *dh;
|
2016-12-22 01:23:43 +00:00
|
|
|
/* Support for Diffie-Hellman key exchange */
|
|
|
|
if (!buffer_string_is_empty(s->ssl_dh_file)) {
|
|
|
|
/* DH parameters from file */
|
2018-01-07 00:04:08 +00:00
|
|
|
BIO *bio;
|
2016-12-22 01:23:43 +00:00
|
|
|
bio = BIO_new_file((char *) s->ssl_dh_file->ptr, "r");
|
|
|
|
if (bio == NULL) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
|
|
|
"SSL: Unable to open file",
|
|
|
|
s->ssl_dh_file->ptr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
|
|
|
|
BIO_free(bio);
|
|
|
|
if (dh == NULL) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
|
|
|
"SSL: PEM_read_bio_DHparams failed",
|
|
|
|
s->ssl_dh_file->ptr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BIGNUM *dh_p, *dh_g;
|
|
|
|
/* Default DH parameters from RFC5114 */
|
|
|
|
dh = DH_new();
|
|
|
|
if (dh == NULL) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s",
|
|
|
|
"SSL: DH_new () failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dh_p = BN_bin2bn(dh1024_p,sizeof(dh1024_p), NULL);
|
|
|
|
dh_g = BN_bin2bn(dh1024_g,sizeof(dh1024_g), NULL);
|
|
|
|
if ((dh_p == NULL) || (dh_g == NULL)) {
|
|
|
|
DH_free(dh);
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s",
|
|
|
|
"SSL: BN_bin2bn () failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L \
|
|
|
|
|| defined(LIBRESSL_VERSION_NUMBER)
|
|
|
|
dh->p = dh_p;
|
|
|
|
dh->g = dh_g;
|
|
|
|
dh->length = 160;
|
|
|
|
#else
|
|
|
|
DH_set0_pqg(dh, dh_p, NULL, dh_g);
|
|
|
|
DH_set_length(dh, 160);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
SSL_CTX_set_tmp_dh(s->ssl_ctx,dh);
|
|
|
|
SSL_CTX_set_options(s->ssl_ctx,SSL_OP_SINGLE_DH_USE);
|
|
|
|
DH_free(dh);
|
2018-01-07 00:04:08 +00:00
|
|
|
}
|
2016-12-22 01:23:43 +00:00
|
|
|
#else
|
|
|
|
if (!buffer_string_is_empty(s->ssl_dh_file)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
|
|
|
"SSL: openssl compiled without DH support, "
|
|
|
|
"can't load parameters from", s->ssl_dh_file->ptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
|
|
|
|
#ifndef OPENSSL_NO_ECDH
|
2018-01-07 00:04:08 +00:00
|
|
|
{
|
|
|
|
int nid = 0;
|
2016-12-22 01:23:43 +00:00
|
|
|
/* Support for Elliptic-Curve Diffie-Hellman key exchange */
|
|
|
|
if (!buffer_string_is_empty(s->ssl_ec_curve)) {
|
|
|
|
/* OpenSSL only supports the "named curves"
|
|
|
|
* from RFC 4492, section 5.1.1. */
|
|
|
|
nid = OBJ_sn2nid((char *) s->ssl_ec_curve->ptr);
|
|
|
|
if (nid == 0) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
|
|
|
"SSL: Unknown curve name",
|
|
|
|
s->ssl_ec_curve->ptr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-07 01:05:42 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10002000
|
2016-12-22 01:23:43 +00:00
|
|
|
/* Default curve */
|
|
|
|
nid = OBJ_sn2nid("prime256v1");
|
2018-01-07 01:05:42 +00:00
|
|
|
#elif OPENSSL_VERSION_NUMBER < 0x10100000L \
|
|
|
|
|| defined(LIBRESSL_VERSION_NUMBER)
|
|
|
|
if (!SSL_CTX_set_ecdh_auto(s->ssl_ctx, 1)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s",
|
|
|
|
"SSL: SSL_CTX_set_ecdh_auto() failed");
|
|
|
|
}
|
|
|
|
#endif
|
2016-12-22 01:23:43 +00:00
|
|
|
}
|
2018-01-07 00:04:08 +00:00
|
|
|
if (nid) {
|
|
|
|
EC_KEY *ecdh;
|
|
|
|
ecdh = EC_KEY_new_by_curve_name(nid);
|
|
|
|
if (ecdh == NULL) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
|
|
|
"SSL: Unable to create curve",
|
|
|
|
s->ssl_ec_curve->ptr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
SSL_CTX_set_tmp_ecdh(s->ssl_ctx,ecdh);
|
|
|
|
SSL_CTX_set_options(s->ssl_ctx,SSL_OP_SINGLE_ECDH_USE);
|
|
|
|
EC_KEY_free(ecdh);
|
2016-12-22 01:23:43 +00:00
|
|
|
}
|
2018-01-07 00:04:08 +00:00
|
|
|
}
|
2016-12-22 01:23:43 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* load all ssl.ca-files specified in the config into each SSL_CTX
|
|
|
|
* to be prepared for SNI */
|
|
|
|
for (size_t j = 0; j < srv->config_context->used; ++j) {
|
|
|
|
plugin_config *s1 = p->config_storage[j];
|
|
|
|
|
2017-05-24 02:28:06 +00:00
|
|
|
if (!buffer_string_is_empty(s1->ssl_ca_dn_file)) {
|
|
|
|
if (1 != SSL_CTX_load_verify_locations(
|
|
|
|
s->ssl_ctx, s1->ssl_ca_dn_file->ptr, NULL)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL),
|
|
|
|
s1->ssl_ca_dn_file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2016-12-22 01:23:43 +00:00
|
|
|
if (!buffer_string_is_empty(s1->ssl_ca_file)) {
|
|
|
|
if (1 != SSL_CTX_load_verify_locations(
|
|
|
|
s->ssl_ctx, s1->ssl_ca_file->ptr, NULL)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL),
|
|
|
|
s1->ssl_ca_file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->ssl_verifyclient) {
|
|
|
|
int mode;
|
|
|
|
if (NULL == s->ssl_ca_file_cert_names) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s",
|
|
|
|
"SSL: You specified ssl.verifyclient.activate "
|
2017-05-24 02:28:06 +00:00
|
|
|
"but no ssl.ca-file or ssl.ca-dn-file");
|
2016-12-22 01:23:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
SSL_CTX_set_client_CA_list(
|
|
|
|
s->ssl_ctx, SSL_dup_CA_list(s->ssl_ca_file_cert_names));
|
|
|
|
mode = SSL_VERIFY_PEER;
|
|
|
|
if (s->ssl_verifyclient_enforce) {
|
|
|
|
mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
|
|
|
|
}
|
2017-05-09 19:34:38 +00:00
|
|
|
SSL_CTX_set_verify(s->ssl_ctx, mode, verify_callback);
|
|
|
|