|
|
|
/*
|
|
|
|
* mod_openssl - openssl support for lighttpd
|
|
|
|
*
|
|
|
|
* Fully-rewritten from original
|
|
|
|
* Copyright(c) 2016 Glenn Strauss gstrauss()gluelogic.com All rights reserved
|
|
|
|
* License: BSD 3-clause (same as lighttpd)
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* future possible enhancements: OCSP stapling
|
|
|
|
*
|
|
|
|
* Note: If session tickets are -not- disabled with
|
|
|
|
* ssl.openssl.ssl-conf-cmd = ("Options" => "-SessionTicket")
|
|
|
|
* mod_openssl rotates server ticket encryption key (STEK) every 24 hours.
|
|
|
|
* This is fine for use with a single lighttpd instance, but with multiple
|
|
|
|
* lighttpd workers, no coordinated STEK (server ticket encryption key)
|
|
|
|
* rotation occurs other than by (some external job) restarting lighttpd.
|
|
|
|
* Restarting lighttpd generates a new key that is shared by lighttpd workers
|
|
|
|
* for the lifetime of the new key. If the rotation period expires and
|
|
|
|
* lighttpd has not been restarted, lighttpd workers will generate new
|
|
|
|
* independent keys, making session tickets less effective for session
|
|
|
|
* resumption, since clients have a lower chance for future connections to
|
|
|
|
* reach the same lighttpd worker. However, things will still work, and a new
|
|
|
|
* session will be created if session resumption fails. Admins should plan to
|
|
|
|
* restart lighttpd at least every 24 hours if session tickets are enabled and
|
|
|
|
* multiple lighttpd workers are configured.
|
|
|
|
*/
|
|
|
|
#include "first.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/*(not needed)*/
|
|
|
|
#define OPENSSL_NO_STDIO
|
|
|
|
|
|
|
|
#ifndef USE_OPENSSL_KERBEROS
|
|
|
|
#ifndef OPENSSL_NO_KRB5
|
|
|
|
#define OPENSSL_NO_KRB5
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "sys-crypto.h"
|
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/tls1.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
|
|
|
|
|
|
|
|
#include "base.h"
|
|
|
|
#include "http_header.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
/* 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) *ssl_pemfile_chain;
|
|
|
|
const buffer *ssl_pemfile;
|
|
|
|
const buffer *ssl_privkey;
|
|
|
|
} plugin_cert;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
SSL_CTX *ssl_ctx;
|
|
|
|
} plugin_ssl_ctx;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
STACK_OF(X509_NAME) *names;
|
|
|
|
X509_STORE *certs;
|
|
|
|
} plugin_cacerts;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
SSL_CTX *ssl_ctx; /* output from network_init_ssl() */
|
|
|
|
|
|
|
|
/*(used only during startup; not patched)*/
|
|
|
|
unsigned char ssl_enabled; /* only interesting for setting up listening sockets. don't use at runtime */
|
|
|
|
unsigned char ssl_honor_cipher_order; /* determine SSL cipher in server-preferred order, not client-order */
|
|
|
|
unsigned char ssl_empty_fragments; /* whether to not set SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS */
|
|
|
|
unsigned char ssl_use_sslv2;
|
|
|
|
unsigned char ssl_use_sslv3;
|
|
|
|
const buffer *ssl_cipher_list;
|
|
|
|
const buffer *ssl_dh_file;
|
|
|
|
const buffer *ssl_ec_curve;
|
|
|
|
array *ssl_conf_cmd;
|
|
|
|
|
|
|
|
/*(copied from plugin_data for socket ssl_ctx config)*/
|
|
|
|
const plugin_cert *pc;
|
|
|
|
const plugin_cacerts *ssl_ca_file;
|
|
|
|
STACK_OF(X509_NAME) *ssl_ca_dn_file;
|
|
|
|
const buffer *ssl_ca_crl_file;
|
|
|
|
unsigned char ssl_verifyclient;
|
|
|
|
unsigned char ssl_verifyclient_enforce;
|
|
|
|
unsigned char ssl_verifyclient_depth;
|
|
|
|
unsigned char ssl_read_ahead;
|
|
|
|
unsigned char ssl_disable_client_renegotiation;
|
|
|
|
} plugin_config_socket; /*(used at startup during configuration)*/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
/* SNI per host: w/ COMP_SERVER_SOCKET, COMP_HTTP_SCHEME, COMP_HTTP_HOST */
|
|
|
|
plugin_cert *pc;
|
|
|
|
const plugin_cacerts *ssl_ca_file;
|
|
|
|
STACK_OF(X509_NAME) *ssl_ca_dn_file;
|
|
|
|
const buffer *ssl_ca_crl_file;
|
|
|
|
|
|
|
|
unsigned char ssl_verifyclient;
|
|
|
|
unsigned char ssl_verifyclient_enforce;
|
|
|
|
unsigned char ssl_verifyclient_depth;
|
|
|
|
unsigned char ssl_verifyclient_export_cert;
|
|
|
|
unsigned char ssl_read_ahead;
|
|
|
|
unsigned char ssl_log_noise;
|
|
|
|
unsigned char ssl_disable_client_renegotiation;
|
|
|
|
const buffer *ssl_verifyclient_username;
|
|
|
|
const buffer *ssl_acme_tls_1;
|
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
|
|
|
plugin_ssl_ctx *ssl_ctxs;
|
|
|
|
plugin_config defaults;
|
|
|
|
server *srv;
|
|
|
|
array *cafiles;
|
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
static int ssl_is_init;
|
|
|
|
/* need assigned p->id for deep access of module handler_ctx for connection
|
|
|
|
* i.e. handler_ctx *hctx = r->plugin_ctx[plugin_data_singleton->id]; */
|
|
|
|
static plugin_data *plugin_data_singleton;
|
|
|
|
#define LOCAL_SEND_BUFSIZE (16 * 1024)
|
|
|
|
static char *local_send_buffer;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
SSL *ssl;
|
|
|
|
request_st *r;
|
|
|
|
connection *con;
|
|
|
|
short renegotiations; /* count of SSL_CB_HANDSHAKE_START */
|
|
|
|
short close_notify;
|
|
|
|
unsigned short request_env_patched;
|
|
|
|
unsigned short alpn;
|
|
|
|
plugin_config conf;
|
|
|
|
buffer *tmp_buf;
|
|
|
|
} 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)
|
|
|
|
{
|
|
|
|
if (hctx->ssl) SSL_free(hctx->ssl);
|
|
|
|
free(hctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef TLSEXT_TYPE_session_ticket
|
|
|
|
/* ssl/ssl_local.h */
|
|
|
|
#define TLSEXT_KEYNAME_LENGTH 16
|
|
|
|
#define TLSEXT_TICK_KEY_LENGTH 32
|
|
|
|
|
|
|
|
/* openssl has a huge number of interfaces, but not the most useful;
|
|
|
|
* construct our own session ticket encryption key structure */
|
|
|
|
typedef struct tlsext_ticket_key_st {
|
|
|
|
time_t active_ts; /* tickets not issued w/ key until activation timestamp */
|
|
|
|
time_t expire_ts; /* key not valid after expiration timestamp */
|
|
|
|
unsigned char tick_key_name[TLSEXT_KEYNAME_LENGTH];
|
|
|
|
unsigned char tick_hmac_key[TLSEXT_TICK_KEY_LENGTH];
|
|
|
|
unsigned char tick_aes_key[TLSEXT_TICK_KEY_LENGTH];
|
|
|
|
} tlsext_ticket_key_t;
|
|
|
|
|
|
|
|
static tlsext_ticket_key_t session_ticket_keys[4];
|
|
|
|
static time_t stek_rotate_ts;
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
mod_openssl_session_ticket_key_generate (time_t active_ts, time_t expire_ts)
|
|
|
|
{
|
|
|
|
/* openssl RAND_*bytes() functions are called multiple times since the
|
|
|
|
* funcs might have a 32-byte limit on number of bytes returned each call
|
|
|
|
*
|
|
|
|
* (Note: session ticket encryption key generation is not expected to fail)
|
|
|
|
*
|
|
|
|
* 3 keys are stored in session_ticket_keys[]
|
|
|
|
* The 4th element of session_ticket_keys[] is used for STEK construction
|
|
|
|
*/
|
|
|
|
/*(RAND_priv_bytes() not in openssl 1.1.0; introduced in openssl 1.1.1)*/
|
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10101000L
|
|
|
|
#define RAND_priv_bytes(x,sz) RAND_bytes((x),(sz))
|
|
|
|
#endif
|
|
|
|
if (RAND_bytes(session_ticket_keys[3].tick_key_name,
|
|
|
|
TLSEXT_KEYNAME_LENGTH) <= 0
|
|
|
|
|| RAND_priv_bytes(session_ticket_keys[3].tick_hmac_key,
|
|
|
|
TLSEXT_TICK_KEY_LENGTH) <= 0
|
|
|
|
|| RAND_priv_bytes(session_ticket_keys[3].tick_aes_key,
|
|
|
|
TLSEXT_TICK_KEY_LENGTH) <= 0)
|
|
|
|
return 0;
|
|
|
|
session_ticket_keys[3].active_ts = active_ts;
|
|
|
|
session_ticket_keys[3].expire_ts = expire_ts;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
mod_openssl_session_ticket_key_rotate (void)
|
|
|
|
{
|
|
|
|
/* discard oldest key (session_ticket_keys[2]) and put newest key first
|
|
|
|
* 3 keys are stored in session_ticket_keys[0], [1], [2]
|
|
|
|
* session_ticket_keys[3] is used to construct and pass new STEK */
|
|
|
|
|
|
|
|
session_ticket_keys[2] = session_ticket_keys[1];
|
|
|
|
session_ticket_keys[1] = session_ticket_keys[0];
|
|
|
|
/*memmove(session_ticket_keys+1,
|
|
|
|
session_ticket_keys+0, sizeof(tlsext_ticket_key_t)*2);*/
|
|
|
|
session_ticket_keys[0] = session_ticket_keys[3];
|
|
|
|
|
|
|
|
/* session_ticket_keys[] is entirely wiped in mod_openssl_free_openssl() */
|
|
|
|
/*OPENSSL_cleanse(session_ticket_keys+3, sizeof(tlsext_ticket_key_t));*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static tlsext_ticket_key_t *
|
|
|
|
tlsext_ticket_key_get (void)
|
|
|
|
{
|
|
|
|
const time_t cur_ts = log_epoch_secs;
|
|
|
|
const int e = sizeof(session_ticket_keys)/sizeof(*session_ticket_keys) - 1;
|
|
|
|
for (int i = 0; i < e; ++i) {
|
|
|
|
if (session_ticket_keys[i].active_ts > cur_ts) continue;
|
|
|
|
if (session_ticket_keys[i].expire_ts < cur_ts) continue;
|
|
|
|
return &session_ticket_keys[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static tlsext_ticket_key_t *
|
|
|
|
tlsext_ticket_key_find (unsigned char key_name[16], int *refresh)
|
|
|
|
{
|
|
|
|
*refresh = 0;
|
|
|
|
const time_t cur_ts = log_epoch_secs;
|
|
|
|
const int e = sizeof(session_ticket_keys)/sizeof(*session_ticket_keys) - 1;
|
|
|
|
for (int i = 0; i < e; ++i) {
|
|
|
|
if (session_ticket_keys[i].expire_ts < cur_ts) continue;
|
|
|
|
if (0 == memcmp(session_ticket_keys[i].tick_key_name, key_name, 16))
|
|
|
|
return &session_ticket_keys[i];
|
|
|
|
if (session_ticket_keys[i].active_ts <= cur_ts)
|
|
|
|
*refresh = 1; /* newer active key is available */
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* based on reference implementation from openssl 1.1.1g man page
|
|
|
|
* man SSL_CTX_set_tlsext_ticket_key_cb
|
|
|
|
* but openssl code uses EVP_aes_256_cbc() instead of EVP_aes_128_cbc()
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ssl_tlsext_ticket_key_cb (SSL *s, unsigned char key_name[16],
|
|
|
|
unsigned char iv[EVP_MAX_IV_LENGTH],
|
|
|
|
EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc)
|
|
|
|
{
|
|
|
|
UNUSED(s);
|
|
|
|
if (enc) { /* create new session */
|
|
|
|
tlsext_ticket_key_t *k = tlsext_ticket_key_get();
|
|
|
|
if (NULL == k)
|
|
|
|
return 0; /* current key does not exist or is not valid */
|
|
|
|
memcpy(key_name, k->tick_key_name, 16);
|
|
|
|
if (RAND_bytes(iv, EVP_MAX_IV_LENGTH) <= 0)
|
|
|
|
return -1; /* insufficient random */
|
|
|
|
EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, k->tick_aes_key, iv);
|
|
|
|
HMAC_Init_ex(hctx, k->tick_hmac_key, sizeof(k->tick_hmac_key),
|
|
|
|
EVP_sha256(), NULL);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else { /* retrieve session */
|
|
|
|
int refresh;
|
|
|
|
tlsext_ticket_key_t *k = tlsext_ticket_key_find(key_name, &refresh);
|
|
|
|
if (NULL == k)
|
|
|
|
return 0;
|
|
|
|
HMAC_Init_ex(hctx, k->tick_hmac_key, sizeof(k->tick_hmac_key),
|
|
|
|
EVP_sha256(), NULL);
|
|
|
|
EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, k->tick_aes_key, iv);
|
|
|
|
return refresh ? 2 : 1;
|
|
|
|
/* 'refresh' will trigger issuing new ticket for session
|
|
|
|
* even though the current ticket is still valid */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* TLSEXT_TYPE_session_ticket */
|
|
|
|
|
|
|
|
|
|
|
|
INIT_FUNC(mod_openssl_init)
|
|
|
|
{
|
|
|
|
plugin_data_singleton = (plugin_data *)calloc(1, sizeof(plugin_data));
|
|
|
|
#ifdef DEBUG_WOLFSSL
|
|
|
|
wolfSSL_Debugging_ON();
|
|
|
|
#endif
|
|
|
|
return plugin_data_singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int mod_openssl_init_once_openssl (server *srv)
|
|
|
|
{
|
|
|
|
if (ssl_is_init) return 1;
|
|
|
|
|
|
|
|
#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(srv->errh, __FILE__, __LINE__,
|
|
|
|
"SSL: not enough entropy in the pool");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
local_send_buffer = malloc(LOCAL_SEND_BUFSIZE);
|
|
|
|
force_assert(NULL != local_send_buffer);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void mod_openssl_free_openssl (void)
|
|
|
|
{
|
|
|
|
if (!ssl_is_init) return;
|
|
|
|
|
|
|
|
#ifdef TLSEXT_TYPE_session_ticket
|
|
|
|
OPENSSL_cleanse(session_ticket_keys, sizeof(session_ticket_keys));
|
|
|
|
stek_rotate_ts = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
free(local_send_buffer);
|
|
|
|
ssl_is_init = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
mod_openssl_free_config (server *srv, plugin_data * const p)
|
|
|
|
{
|
|
|
|
array_free(p->cafiles);
|
|
|
|
|
|
|
|
if (NULL != p->ssl_ctxs) {
|
|
|
|
SSL_CTX * const ssl_ctx_global_scope = p->ssl_ctxs->ssl_ctx;
|
|
|
|
/* free ssl_ctx from $SERVER["socket"] (if not copy of global scope) */
|
|
|
|
for (uint32_t i = 1; i < srv->config_context->used; ++i) {
|
|
|
|
plugin_ssl_ctx * const s = p->ssl_ctxs + i;
|
|
|
|
if (s->ssl_ctx && s->ssl_ctx != ssl_ctx_global_scope)
|
|
|
|
SSL_CTX_free(s->ssl_ctx);
|
|
|
|
}
|
|
|
|
/* free ssl_ctx from global scope */
|
|
|
|
if (ssl_ctx_global_scope)
|
|
|
|
SSL_CTX_free(ssl_ctx_global_scope);
|
|
|
|
free(p->ssl_ctxs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == p->cvlist) return;
|
|
|
|
/* (init i to 0 if global context; to 1 to skip empty global context) */
|
|
|
|
for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
|
|
|
|
config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
|
|
|
|
for (; -1 != cpv->k_id; ++cpv) {
|
|
|
|
switch (cpv->k_id) {
|
|
|
|
case 0: /* ssl.pemfile */
|
|
|
|
if (cpv->vtype == T_CONFIG_LOCAL) {
|
|
|
|
plugin_cert *pc = cpv->v.v;
|
|
|
|
EVP_PKEY_free(pc->ssl_pemfile_pkey);
|
|
|
|
X509_free(pc->ssl_pemfile_x509);
|
|
|
|
sk_X509_pop_free(pc->ssl_pemfile_chain, X509_free);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: /* ssl.ca-file */
|
|
|
|
if (cpv->vtype == T_CONFIG_LOCAL) {
|
|
|
|
plugin_cacerts *cacerts = cpv->v.v;
|
|
|
|
sk_X509_NAME_pop_free(cacerts->names, X509_NAME_free);
|
|
|
|
X509_STORE_free(cacerts->certs);
|
|
|
|
free(cacerts);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: /* ssl.ca-dn-file */
|
|
|
|
if (cpv->vtype == T_CONFIG_LOCAL)
|
|
|
|
sk_X509_NAME_pop_free(cpv->v.v, X509_NAME_free);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* openssl BIO_s_file() employs system stdio
|
|
|
|
* system stdio buffers reads and does not guarantee to clear buffer memory
|
|
|
|
* 'man PEM_bytes_read_bio_secmem()' and see NOTES section for more info
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef OPENSSL_NO_POSIX_IO
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
static BIO *
|
|
|
|
BIO_new_rdonly_file (const char *file)
|
|
|
|
{
|
|
|
|
|
|
|
|
BIO *in = BIO_new(BIO_s_file());
|
|
|
|
if (NULL == in)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (BIO_read_filename(in, file) <= 0) {
|
|
|
|
BIO_free(in);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set I/O stream unbuffered (best-effort; not fatal)
|
|
|
|
* system stdio buffers reads and does not guarantee to clear buffer memory.
|
|
|
|
* Alternative: provide buffer (e.g. 8k) and clear after use (in caller) */
|
|
|
|
FILE *fp = NULL;
|
|
|
|
if (BIO_get_fp(in, &fp))
|
|
|
|
setvbuf(fp, NULL, _IONBF, 0);
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !OPENSSL_NO_POSIX_IO */
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "fdevent.h"
|
|
|
|
static BIO *
|
|
|
|
BIO_new_rdonly_file (const char *file)
|
|
|
|
{
|
|
|
|
/* unbuffered fd; not using system stdio */
|
|
|
|
int fd = fdevent_open_cloexec(file, 1, O_RDONLY, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BIO *in = BIO_new_fd(fd, BIO_CLOSE);
|
|
|
|
if (NULL == in) {
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !OPENSSL_NO_POSIX_IO */
|
|
|
|
|
|
|
|
|
|
|
|
/* use memory from openssl secure heap for temporary buffers, returned storage
|
|
|
|
* (pemfile might contain a private key in addition to certificate chain)
|
|
|
|
* Interfaces similar to those constructed in include/openssl/pem.h for
|
|
|
|
* PEM_read_bio_X509(), except this is named PEM_read_bio_X509_secmem().
|
|
|
|
* Similar for PEM_read_bio_X509_AUX_secmem().
|
|
|
|
*
|
|
|
|
* Supporting routine PEM_ASN1_read_bio_secmem() modified from openssl
|
|
|
|
* crypto/pem/pem_oth.c:PEM_ASN1_read_bio():
|
|
|
|
* uses PEM_bytes_read_bio_secmem() instead of PEM_bytes_read_bio()
|
|
|
|
* uses OPENSSL_secure_clear_free() instead of OPENSSL_free()
|
|
|
|
*
|
|
|
|
* PEM_bytes_read_bio_secmem() openssl 1.1.1 or later
|
|
|
|
* OPENSSL_secure_clear_free() openssl 1.1.0g or later
|
|
|
|
* As this comment is being written, only openssl 1.1.1 is actively maintained.
|
|
|
|
* Earlier vers of openssl no longer receive security patches from openssl.org.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
PEM_ASN1_read_bio_secmem(d2i_of_void *d2i, const char *name, BIO *bp, void **x,
|
|
|
|
pem_password_cb *cb, void *u)
|
|
|
|
{
|
|
|
|
const unsigned char *p = NULL;
|
|
|
|
unsigned char *data = NULL;
|
|
|
|
long len = 0;
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
|
|
|
|
if (!PEM_bytes_read_bio_secmem(&data, &len, NULL, name, bp, cb, u))
|
|
|
|
#else
|
|
|
|
if (!PEM_bytes_read_bio(&data, &len, NULL, name, bp, cb, u))
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
p = data;
|
|
|
|
ret = d2i(x, &p, len);
|
|
|
|
if (ret == NULL)
|
|
|
|
PEMerr(PEM_F_PEM_ASN1_READ_BIO, ERR_R_ASN1_LIB);
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
|
|
|
|
OPENSSL_secure_clear_free(data, len);
|
|
|
|
#else
|
|
|
|
OPENSSL_cleanse(data, len);
|
|
|
|
OPENSSL_free(data);
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static X509 *
|
|
|
|
PEM_read_bio_X509_secmem(BIO *bp, X509 **x, pem_password_cb *cb, void *u)
|
|
|
|
{
|
|
|
|
return PEM_ASN1_read_bio_secmem((d2i_of_void *)d2i_X509,
|
|
|
|
PEM_STRING_X509,
|
|
|
|
bp, (void **)x, cb, u);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static X509 *
|
|
|
|
PEM_read_bio_X509_AUX_secmem(BIO *bp, X509 **x, pem_password_cb *cb, void *u)
|
|
|
|
{
|
|
|
|
return PEM_ASN1_read_bio_secmem((d2i_of_void *)d2i_X509_AUX,
|
|
|
|
PEM_STRING_X509_TRUSTED,
|
|
|
|
bp, (void **)x, cb, u);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
mod_openssl_load_X509_sk (const char *file, log_error_st *errh, STACK_OF(X509) **chain, BIO *in)
|
|
|
|
{
|
|
|
|
STACK_OF(X509) *chain_sk = NULL;
|
|
|
|
for (X509 *ca; (ca = PEM_read_bio_X509_secmem(in,NULL,NULL,NULL)); ) {
|
|
|
|
if (NULL == chain_sk) /*(allocate only if it will not be empty)*/
|
|
|
|
chain_sk = sk_X509_new_null();
|
|
|
|
if (!chain_sk || !sk_X509_push(chain_sk, ca)) {
|
|
|
|
log_error(errh, __FILE__, __LINE__,
|
|
|
|
"SSL: couldn't read X509 certificates from '%s'", file);
|
|
|
|
if (chain_sk) sk_X509_pop_free(chain_sk, X509_free);
|
|
|
|
X509_free(ca);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*chain = chain_sk;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
mod_openssl_load_X509_STORE (const char *file, log_error_st *errh, X509_STORE **chain, BIO *in)
|
|
|
|
{
|
|
|
|
X509_STORE *chain_store = NULL;
|
|
|
|
for (X509 *ca; (ca = PEM_read_bio_X509(in,NULL,NULL,NULL)); X509_free(ca)) {
|
|
|
|
if (NULL == chain_store) /*(allocate only if it will not be empty)*/
|
|
|
|
chain_store = X509_STORE_new();
|
|
|
|
if (!chain_store || !X509_STORE_add_cert(chain_store, ca)) {
|
|
|
|
log_error(errh, __FILE__, __LINE__,
|
|
|
|
"SSL: couldn't read X509 certificates from '%s'", file);
|
|
|
|
if (chain_store) X509_STORE_free(chain_store);
|
|
|
|
X509_free(ca);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*chain = chain_store;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static plugin_cacerts *
|
|
|
|
mod_openssl_load_cacerts (const buffer *ssl_ca_file, log_error_st *errh)
|
|
|
|
{
|
|
|
|
const char *file = ssl_ca_file->ptr;
|
|
|
|
BIO *in = BIO_new(BIO_s_file());
|
|
|
|
if (NULL == in) {
|
|
|
|
log_error(errh, __FILE__, __LINE__,
|
|
|
|
"SSL: BIO_new(BIO_s_file()) failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BIO_read_filename(in, file) <= 0) {
|
|
|
|
log_error(errh, __FILE__, __LINE__,
|
|
|
|
"SSL: BIO_read_filename('%s') failed", file);
|
|
|
|
BIO_free(in);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
X509_STORE *chain_store = NULL;
|
|
|
|
if (!mod_openssl_load_X509_STORE(file, errh, &chain_store, in)) {
|
|
|
|
BIO_free(in);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_free(in);
|
|
|
|
|
|
|
|
if (NULL == chain_store) {
|
|
|
|
log_error(errh, __FILE__, __LINE__,
|
|
|
|
"SSL: ssl.ca-file is empty %s", file);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin_cacerts *cacerts = malloc(sizeof(plugin_cacerts));
|
|
|
|
force_assert(cacerts);
|
|
|
|
|
|
|
|
/* (would be more efficient to walk the X509_STORE and build the list,
|
|
|
|
* but this works for now and matches how ssl.ca-dn-file is handled) */
|
|
|
|
cacerts->names = SSL_load_client_CA_file(file);
|
|
|
|
if (NULL == cacerts->names) {
|
|
|
|
X509_STORE_free(chain_store);
|
|
|
|
free(cacerts);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cacerts->certs = chain_store;
|
|
|
|
return cacerts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
mod_openssl_load_cacrls (X509_STORE *store, const buffer *ssl_ca_crl_file, server *srv)
|
|
|
|
{
|
|
|
|
if (1 != X509_STORE_load_locations(store, ssl_ca_crl_file->ptr, NULL)) {
|
|
|
|
log_error(srv->errh, __FILE__, __LINE__,
|
|
|
|
"SSL: %s %s", ERR_error_string(ERR_get_error(), NULL),
|
|
|
|
ssl_ca_crl_file->ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10002000
|
|
|
|
static int
|
|
|
|
mod_openssl_load_verify_locn (SSL_CTX *ssl_ctx, const buffer *b, server *srv)
|
|
|
|
{
|
|
|
|
const char *fn = b->ptr;
|
|
|
|
if (1 == SSL_CTX_load_verify_locations(ssl_ctx, fn, NULL))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
log_error(srv->errh, __FILE__, __LINE__,
|
|
|
|
"SSL: %s %s", ERR_error_string(ERR_get_error(), NULL), fn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
mod_openssl_load_ca_files (SSL_CTX *ssl_ctx, plugin_data *p, server *srv)
|
|
|
|
{
|
|
|
|
/* load all ssl.ca-files specified in the config into each SSL_CTX */
|
|
|
|
|
|
|
|
for (uint32_t i = 0, used = p->cafiles->used; i < used; ++i) {
|
|
|
|
const buffer *b = &((data_string *)p->cafiles->data[i])->value;
|
|
|
|
if (!mod_openssl_load_verify_locn(ssl_ctx, b, srv))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
FREE_FUNC(mod_openssl_free)
|
|
|
|
{
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
if (NULL == p->srv) return;
|
|
|
|
mod_openssl_free_config(p->srv, p);
|
|
|
|
mod_openssl_free_openssl();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
mod_openssl_merge_config_cpv (plugin_config * const pconf, const config_plugin_value_t * const cpv)
|
|
|
|
{
|
|
|
|
switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
|
|
|
|
case 0: /* ssl.pemfile */
|
|
|
|
if (cpv->vtype == T_CONFIG_LOCAL)
|
|
|
|
pconf->pc = cpv->v.v;
|
|
|
|
break;
|
|
|
|
case 1: /* ssl.privkey */
|
|
|
|
break;
|
|
|
|
case 2: /* ssl.ca-file */
|
|
|
|
if (cpv->vtype == T_CONFIG_LOCAL)
|
|
|
|
pconf->ssl_ca_file = cpv->v.v;
|
|
|
|
break;
|
|
|
|
case 3: /* ssl.ca-dn-file */
|
|
|
|
if (cpv->vtype == T_CONFIG_LOCAL)
|
|
|
|
pconf->ssl_ca_dn_file = cpv->v.v;
|
|
|
|
break;
|
|
|
|
case 4: /* ssl.ca-crl-file */
|
|
|
|
pconf->ssl_ca_crl_file = cpv->v.b;
|
|
|
|
break;
|
|
|
|
case 5: /* ssl.read-ahead */
|
|
|
|
pconf->ssl_read_ahead = (0 != cpv->v.u);
|
|
|
|
break;
|
|
|
|
case 6: /* ssl.disable-client-renegotiation */
|
|
|
|
pconf->ssl_disable_client_renegotiation = (0 != cpv->v.u);
|
|
|
|
break;
|
|
|
|
case 7: /* ssl.verifyclient.activate */
|
|
|
|
pconf->ssl_verifyclient = (0 != cpv->v.u);
|
|
|
|
break;
|
|
|
|
case 8: /* ssl.verifyclient.enforce */
|
|
|
|
pconf->ssl_verifyclient_enforce = (0 != cpv->v.u);
|
|
|
|
break;
|
|
|
|
case 9: /* ssl.verifyclient.depth */
|
|
|
|
pconf->ssl_verifyclient_depth = (unsigned char)cpv->v.shrt;
|
|
|
|
break;
|
|
|
|
case 10:/* ssl.verifyclient.username */
|
|
|
|
pconf->ssl_verifyclient_username = cpv->v.b;
|
|
|
|
break;
|
|
|
|
case 11:/* ssl.verifyclient.exportcert */
|
|
|
|
pconf->ssl_verifyclient_export_cert = (0 != cpv->v.u);
|
|
|
|
break;
|
|
|
|
case 12:/* ssl.acme-tls-1 */
|
|
|
|
pconf->ssl_acme_tls_1 = cpv->v.b;
|
|
|
|
break;
|
|
|
|
case 13:/* debug.log-ssl-noise */
|
|
|
|
pconf->ssl_log_noise = (0 != cpv->v.u);
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
mod_openssl_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
mod_openssl_merge_config_cpv(pconf, cpv);
|
|
|
|
} while ((++cpv)->k_id != -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
mod_openssl_patch_config (request_st * const r, plugin_config * const pconf)
|
|
|
|
{
|
|
|
|
plugin_data * const p = plugin_data_singleton;
|
|
|
|
memcpy(pconf, &p->defaults, sizeof(plugin_config));
|
|
|
|
for (int i = 1, used = p->nconfig; i < used; ++i) {
|
|
|
|
if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
|
|
|
|
mod_openssl_merge_config(pconf, p->cvlist + p->cvlist[i].v.u2[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (hctx->renegotiations >= 0) ++hctx->renegotiations;
|
|
|
|
}
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preverify_ok && 0 == depth && NULL != hctx->conf.ssl_ca_dn_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 cert_names = hctx->conf.ssl_ca_dn_file;
|
|
|
|
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 cert_names sorted?)*/
|
|
|
|
if (-1 != sk_X509_NAME_find(cert_names, issuer))
|
|
|
|
return preverify_ok; /* match */
|
|
|
|
#else
|
|
|
|
for (int i = 0, len = sk_X509_NAME_num(cert_names); i < len; ++i) {
|
|
|
|
if (0 == X509_NAME_cmp(sk_X509_NAME_value(cert_names, i), issuer))
|
|
|
|
return preverify_ok; /* match */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
preverify_ok = 0;
|
|
|
|
err = X509_V_ERR_CERT_REJECTED;
|
|
|
|
X509_STORE_CTX_set_error(ctx, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preverify_ok) {
|
|
|
|
return preverify_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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;
|
|
|
|
safer_X509_NAME_oneline(X509_get_subject_name(err_cert),buf,sizeof(buf));
|
|