2012-06-05 18:26:04 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-06-05 18:26:04 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2017-04-01 08:07:32 +02:00
|
|
|
#include "shrpx_tls.h"
|
2012-06-05 18:26:04 +02:00
|
|
|
|
2015-05-13 15:30:35 +02:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <sys/socket.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_SYS_SOCKET_H
|
|
|
|
#ifdef HAVE_NETDB_H
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <netdb.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_NETDB_H
|
2012-06-07 15:39:57 +02:00
|
|
|
#include <netinet/tcp.h>
|
2012-06-08 15:41:24 +02:00
|
|
|
#include <pthread.h>
|
2015-03-30 16:20:40 +02:00
|
|
|
#include <sys/types.h>
|
2012-06-08 15:41:24 +02:00
|
|
|
|
2012-11-22 13:46:15 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2016-02-21 08:35:43 +01:00
|
|
|
#include <iomanip>
|
2012-11-22 13:46:15 +01:00
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2012-06-08 15:41:24 +02:00
|
|
|
#include <openssl/crypto.h>
|
2012-11-22 13:46:15 +01:00
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/x509v3.h>
|
2015-01-07 16:01:09 +01:00
|
|
|
#include <openssl/rand.h>
|
2015-12-11 15:14:52 +01:00
|
|
|
#include <openssl/dh.h>
|
2017-08-24 17:54:42 +02:00
|
|
|
#ifndef OPENSSL_NO_OCSP
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <openssl/ocsp.h>
|
2017-08-24 17:54:42 +02:00
|
|
|
#endif // OPENSSL_NO_OCSP
|
2012-06-05 18:26:04 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
#include <nghttp2/nghttp2.h>
|
2012-11-18 13:23:13 +01:00
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
#include "shrpx_log.h"
|
|
|
|
#include "shrpx_client_handler.h"
|
|
|
|
#include "shrpx_config.h"
|
2014-06-26 15:55:22 +02:00
|
|
|
#include "shrpx_worker.h"
|
2014-10-13 14:09:00 +02:00
|
|
|
#include "shrpx_downstream_connection_pool.h"
|
2015-07-12 15:16:20 +02:00
|
|
|
#include "shrpx_http2_session.h"
|
2015-07-25 15:22:17 +02:00
|
|
|
#include "shrpx_memcached_request.h"
|
|
|
|
#include "shrpx_memcached_dispatcher.h"
|
2016-06-24 17:28:15 +02:00
|
|
|
#include "shrpx_connection_handler.h"
|
2012-11-22 13:46:15 +01:00
|
|
|
#include "util.h"
|
2017-04-01 07:47:36 +02:00
|
|
|
#include "tls.h"
|
2015-02-02 14:47:12 +01:00
|
|
|
#include "template.h"
|
2016-05-07 09:18:58 +02:00
|
|
|
#include "ssl_compat.h"
|
2018-02-03 09:58:49 +01:00
|
|
|
#include "timegm.h"
|
2012-11-22 13:46:15 +01:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
using namespace nghttp2;
|
2012-06-05 18:26:04 +02:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
namespace tls {
|
2012-06-05 18:26:04 +02:00
|
|
|
|
2016-09-10 17:38:20 +02:00
|
|
|
#if !OPENSSL_1_1_API
|
2016-08-26 17:45:16 +02:00
|
|
|
namespace {
|
2016-08-26 16:02:51 +02:00
|
|
|
const unsigned char *ASN1_STRING_get0_data(ASN1_STRING *x) {
|
|
|
|
return ASN1_STRING_data(x);
|
|
|
|
}
|
2016-08-26 17:45:16 +02:00
|
|
|
} // namespace
|
2016-09-10 17:38:20 +02:00
|
|
|
#endif // !OPENSSL_1_1_API
|
2016-08-26 16:02:51 +02:00
|
|
|
|
2018-04-03 14:33:09 +02:00
|
|
|
#ifndef OPENSSL_NO_NEXTPROTONEG
|
2012-06-05 18:26:04 +02:00
|
|
|
namespace {
|
|
|
|
int next_proto_cb(SSL *s, const unsigned char **data, unsigned int *len,
|
2014-11-27 15:39:04 +01:00
|
|
|
void *arg) {
|
2016-01-18 06:21:09 +01:00
|
|
|
auto &prefs = get_config()->tls.alpn_prefs;
|
2014-06-10 16:15:29 +02:00
|
|
|
*data = prefs.data();
|
|
|
|
*len = prefs.size();
|
2012-06-05 18:26:04 +02:00
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
} // namespace
|
2018-04-03 14:33:09 +02:00
|
|
|
#endif // !OPENSSL_NO_NEXTPROTONEG
|
2012-06-05 18:26:04 +02:00
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) {
|
|
|
|
if (!preverify_ok) {
|
2013-11-01 17:10:18 +01:00
|
|
|
int err = X509_STORE_CTX_get_error(ctx);
|
|
|
|
int depth = X509_STORE_CTX_get_error_depth(ctx);
|
2018-02-08 08:51:23 +01:00
|
|
|
if (err == X509_V_ERR_CERT_HAS_EXPIRED && depth == 0 &&
|
|
|
|
get_config()->tls.client_verify.tolerate_expired) {
|
|
|
|
LOG(INFO) << "The client certificate has expired, but is accepted by "
|
|
|
|
"configuration";
|
|
|
|
return 1;
|
|
|
|
}
|
2013-11-01 17:10:18 +01:00
|
|
|
LOG(ERROR) << "client certificate verify error:num=" << err << ":"
|
2014-11-27 15:39:04 +01:00
|
|
|
<< X509_verify_cert_error_string(err) << ":depth=" << depth;
|
2013-11-01 17:10:18 +01:00
|
|
|
}
|
|
|
|
return preverify_ok;
|
2012-06-05 18:26:04 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-07-31 13:35:10 +02:00
|
|
|
int set_alpn_prefs(std::vector<unsigned char> &out,
|
2016-10-02 15:19:31 +02:00
|
|
|
const std::vector<StringRef> &protos) {
|
2014-11-14 15:14:39 +01:00
|
|
|
size_t len = 0;
|
2014-06-10 16:15:29 +02:00
|
|
|
|
2015-07-20 15:37:26 +02:00
|
|
|
for (const auto &proto : protos) {
|
|
|
|
if (proto.size() > 255) {
|
|
|
|
LOG(FATAL) << "Too long ALPN identifier: " << proto.size();
|
2016-07-31 13:35:10 +02:00
|
|
|
return -1;
|
2014-06-10 16:15:29 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 15:37:26 +02:00
|
|
|
len += 1 + proto.size();
|
2012-07-15 14:39:19 +02:00
|
|
|
}
|
2014-11-14 15:14:39 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (len > (1 << 16) - 1) {
|
|
|
|
LOG(FATAL) << "Too long ALPN identifier list: " << len;
|
2016-07-31 13:35:10 +02:00
|
|
|
return -1;
|
2014-11-14 15:19:16 +01:00
|
|
|
}
|
|
|
|
|
2016-07-31 13:35:10 +02:00
|
|
|
out.resize(len);
|
2014-11-14 15:14:39 +01:00
|
|
|
auto ptr = out.data();
|
|
|
|
|
2015-07-20 15:37:26 +02:00
|
|
|
for (const auto &proto : protos) {
|
|
|
|
*ptr++ = proto.size();
|
2016-10-02 15:19:31 +02:00
|
|
|
ptr = std::copy(std::begin(proto), std::end(proto), ptr);
|
2014-11-14 15:14:39 +01:00
|
|
|
}
|
|
|
|
|
2016-07-31 13:35:10 +02:00
|
|
|
return 0;
|
2012-07-15 14:39:19 +02:00
|
|
|
}
|
|
|
|
|
2012-12-07 15:42:58 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int ssl_pem_passwd_cb(char *buf, int size, int rwflag, void *user_data) {
|
|
|
|
auto config = static_cast<Config *>(user_data);
|
2016-02-14 13:20:40 +01:00
|
|
|
auto len = static_cast<int>(config->tls.private_key_passwd.size());
|
2012-12-03 07:33:04 +01:00
|
|
|
if (size < len + 1) {
|
|
|
|
LOG(ERROR) << "ssl_pem_passwd_cb: buf is too small " << size;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-07 15:42:58 +01:00
|
|
|
// Copy string including last '\0'.
|
2016-02-14 13:20:40 +01:00
|
|
|
memcpy(buf, config->tls.private_key_passwd.c_str(), len + 1);
|
2012-12-03 07:33:04 +01:00
|
|
|
return len;
|
|
|
|
}
|
2012-12-07 15:42:58 +01:00
|
|
|
} // namespace
|
2012-12-03 07:33:04 +01:00
|
|
|
|
2013-02-06 15:27:05 +01:00
|
|
|
namespace {
|
2017-12-15 16:13:27 +01:00
|
|
|
// *al is set to SSL_AD_UNRECOGNIZED_NAME by openssl, so we don't have
|
|
|
|
// to set it explicitly.
|
2014-11-27 15:39:04 +01:00
|
|
|
int servername_callback(SSL *ssl, int *al, void *arg) {
|
2015-08-12 17:12:08 +02:00
|
|
|
auto conn = static_cast<Connection *>(SSL_get_app_data(ssl));
|
|
|
|
auto handler = static_cast<ClientHandler *>(conn->data);
|
2015-02-11 11:18:41 +01:00
|
|
|
auto worker = handler->get_worker();
|
2016-06-24 17:28:15 +02:00
|
|
|
|
|
|
|
auto rawhost = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
|
|
|
|
if (rawhost == nullptr) {
|
2017-04-27 15:25:58 +02:00
|
|
|
return SSL_TLSEXT_ERR_NOACK;
|
2016-06-24 17:28:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto len = strlen(rawhost);
|
|
|
|
// NI_MAXHOST includes terminal NULL.
|
2016-09-10 15:02:46 +02:00
|
|
|
if (len == 0 || len + 1 > NI_MAXHOST) {
|
2017-04-27 15:25:58 +02:00
|
|
|
return SSL_TLSEXT_ERR_NOACK;
|
2013-02-06 15:27:05 +01:00
|
|
|
}
|
2016-06-24 17:28:15 +02:00
|
|
|
|
2016-09-10 15:02:46 +02:00
|
|
|
std::array<uint8_t, NI_MAXHOST> buf;
|
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
auto end_buf = std::copy_n(rawhost, len, std::begin(buf));
|
|
|
|
|
|
|
|
util::inp_strlower(std::begin(buf), end_buf);
|
|
|
|
|
|
|
|
auto hostname = StringRef{std::begin(buf), end_buf};
|
|
|
|
|
2016-09-10 15:02:46 +02:00
|
|
|
auto cert_tree = worker->get_cert_lookup_tree();
|
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
auto idx = cert_tree->lookup(hostname);
|
|
|
|
if (idx == -1) {
|
2017-04-27 15:25:58 +02:00
|
|
|
return SSL_TLSEXT_ERR_NOACK;
|
2016-06-24 17:28:15 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 15:25:58 +02:00
|
|
|
handler->set_tls_sni(hostname);
|
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
auto conn_handler = worker->get_connection_handler();
|
|
|
|
|
2017-02-04 10:59:06 +01:00
|
|
|
const auto &ssl_ctx_list = conn_handler->get_indexed_ssl_ctx(idx);
|
|
|
|
assert(!ssl_ctx_list.empty());
|
|
|
|
|
2018-04-14 11:31:32 +02:00
|
|
|
#if !defined(OPENSSL_IS_BORINGSSL) && !LIBRESSL_IN_USE && \
|
2017-02-04 10:59:06 +01:00
|
|
|
OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2017-05-21 03:29:27 +02:00
|
|
|
auto num_shared_curves = SSL_get_shared_curve(ssl, -1);
|
|
|
|
|
|
|
|
for (auto i = 0; i < num_shared_curves; ++i) {
|
|
|
|
auto shared_curve = SSL_get_shared_curve(ssl, i);
|
2017-02-04 10:59:06 +01:00
|
|
|
|
|
|
|
for (auto ssl_ctx : ssl_ctx_list) {
|
|
|
|
auto cert = SSL_CTX_get0_certificate(ssl_ctx);
|
2017-05-22 15:09:34 +02:00
|
|
|
|
2018-06-09 09:21:30 +02:00
|
|
|
# if OPENSSL_1_1_API
|
2017-05-21 03:29:27 +02:00
|
|
|
auto pubkey = X509_get0_pubkey(cert);
|
2018-06-09 09:21:30 +02:00
|
|
|
# else // !OPENSSL_1_1_API
|
2017-05-22 15:09:34 +02:00
|
|
|
auto pubkey = X509_get_pubkey(cert);
|
2018-06-09 09:21:30 +02:00
|
|
|
# endif // !OPENSSL_1_1_API
|
2017-05-22 15:09:34 +02:00
|
|
|
|
2017-05-21 03:29:27 +02:00
|
|
|
if (EVP_PKEY_base_id(pubkey) != EVP_PKEY_EC) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-22 15:09:34 +02:00
|
|
|
|
2018-06-09 09:21:30 +02:00
|
|
|
# if OPENSSL_1_1_API
|
2017-05-21 03:29:27 +02:00
|
|
|
auto eckey = EVP_PKEY_get0_EC_KEY(pubkey);
|
2018-06-09 09:21:30 +02:00
|
|
|
# else // !OPENSSL_1_1_API
|
2017-05-22 15:09:34 +02:00
|
|
|
auto eckey = EVP_PKEY_get1_EC_KEY(pubkey);
|
2018-06-09 09:21:30 +02:00
|
|
|
# endif // !OPENSSL_1_1_API
|
2017-05-22 15:09:34 +02:00
|
|
|
|
2017-05-21 03:29:27 +02:00
|
|
|
if (eckey == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ecgroup = EC_KEY_get0_group(eckey);
|
|
|
|
auto cert_curve = EC_GROUP_get_curve_name(ecgroup);
|
2017-02-04 10:59:06 +01:00
|
|
|
|
2018-06-09 09:21:30 +02:00
|
|
|
# if !OPENSSL_1_1_API
|
2017-05-22 15:09:34 +02:00
|
|
|
EC_KEY_free(eckey);
|
|
|
|
EVP_PKEY_free(pubkey);
|
2018-06-09 09:21:30 +02:00
|
|
|
# endif // !OPENSSL_1_1_API
|
2017-05-22 15:09:34 +02:00
|
|
|
|
2017-05-21 03:29:27 +02:00
|
|
|
if (shared_curve == cert_curve) {
|
2017-02-04 10:59:06 +01:00
|
|
|
SSL_set_SSL_CTX(ssl, ssl_ctx);
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-14 11:31:32 +02:00
|
|
|
#endif // !defined(OPENSSL_IS_BORINGSSL) && !LIBRESSL_IN_USE &&
|
2017-02-04 10:59:06 +01:00
|
|
|
// OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
|
|
|
|
SSL_set_SSL_CTX(ssl, ssl_ctx_list[0]);
|
2016-06-24 17:28:15 +02:00
|
|
|
|
2015-03-30 16:20:40 +02:00
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-09-29 16:31:50 +02:00
|
|
|
#ifndef OPENSSL_IS_BORINGSSL
|
2015-06-12 14:27:12 +02:00
|
|
|
namespace {
|
|
|
|
std::shared_ptr<std::vector<uint8_t>>
|
|
|
|
get_ocsp_data(TLSContextData *tls_ctx_data) {
|
2018-06-09 09:21:30 +02:00
|
|
|
# ifdef HAVE_ATOMIC_STD_SHARED_PTR
|
2016-09-20 15:24:12 +02:00
|
|
|
return std::atomic_load_explicit(&tls_ctx_data->ocsp_data,
|
|
|
|
std::memory_order_acquire);
|
2018-06-09 09:21:30 +02:00
|
|
|
# else // !HAVE_ATOMIC_STD_SHARED_PTR
|
2015-06-12 14:27:12 +02:00
|
|
|
std::lock_guard<std::mutex> g(tls_ctx_data->mu);
|
|
|
|
return tls_ctx_data->ocsp_data;
|
2018-06-09 09:21:30 +02:00
|
|
|
# endif // !HAVE_ATOMIC_STD_SHARED_PTR
|
2015-06-12 14:27:12 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-03-30 16:20:40 +02:00
|
|
|
namespace {
|
|
|
|
int ocsp_resp_cb(SSL *ssl, void *arg) {
|
|
|
|
auto ssl_ctx = SSL_get_SSL_CTX(ssl);
|
|
|
|
auto tls_ctx_data =
|
|
|
|
static_cast<TLSContextData *>(SSL_CTX_get_app_data(ssl_ctx));
|
|
|
|
|
2015-06-12 14:27:12 +02:00
|
|
|
auto data = get_ocsp_data(tls_ctx_data);
|
2015-03-30 16:20:40 +02:00
|
|
|
|
2015-06-12 14:27:12 +02:00
|
|
|
if (!data) {
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
2015-03-30 16:20:40 +02:00
|
|
|
|
2015-06-12 14:27:12 +02:00
|
|
|
auto buf =
|
|
|
|
static_cast<uint8_t *>(CRYPTO_malloc(data->size(), __FILE__, __LINE__));
|
2014-04-27 16:17:19 +02:00
|
|
|
|
2015-06-12 14:27:12 +02:00
|
|
|
if (!buf) {
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
2015-03-30 16:20:40 +02:00
|
|
|
}
|
2015-06-12 14:27:12 +02:00
|
|
|
|
|
|
|
std::copy(std::begin(*data), std::end(*data), buf);
|
|
|
|
|
|
|
|
SSL_set_tlsext_status_ocsp_resp(ssl, buf, data->size());
|
|
|
|
|
2014-04-27 16:17:19 +02:00
|
|
|
return SSL_TLSEXT_ERR_OK;
|
2013-02-06 15:27:05 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
2015-09-29 16:31:50 +02:00
|
|
|
#endif // OPENSSL_IS_BORINGSSL
|
2013-02-06 15:27:05 +01:00
|
|
|
|
2016-03-24 17:10:58 +01:00
|
|
|
constexpr auto MEMCACHED_SESSION_CACHE_KEY_PREFIX =
|
|
|
|
StringRef::from_lit("nghttpx:tls-session-cache:");
|
2015-07-25 15:22:17 +02:00
|
|
|
|
2017-03-28 14:06:28 +02:00
|
|
|
namespace {
|
|
|
|
int tls_session_client_new_cb(SSL *ssl, SSL_SESSION *session) {
|
|
|
|
auto conn = static_cast<Connection *>(SSL_get_app_data(ssl));
|
|
|
|
if (conn->tls.client_session_cache == nullptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
try_cache_tls_session(conn->tls.client_session_cache, session,
|
|
|
|
ev_now(conn->loop));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
namespace {
|
|
|
|
int tls_session_new_cb(SSL *ssl, SSL_SESSION *session) {
|
2015-08-12 17:12:08 +02:00
|
|
|
auto conn = static_cast<Connection *>(SSL_get_app_data(ssl));
|
|
|
|
auto handler = static_cast<ClientHandler *>(conn->data);
|
2015-07-25 15:22:17 +02:00
|
|
|
auto worker = handler->get_worker();
|
|
|
|
auto dispatcher = worker->get_session_cache_memcached_dispatcher();
|
2016-10-01 17:19:50 +02:00
|
|
|
auto &balloc = handler->get_block_allocator();
|
2015-07-25 15:22:17 +02:00
|
|
|
|
2017-03-22 13:34:13 +01:00
|
|
|
#ifdef TLS1_3_VERSION
|
|
|
|
if (SSL_version(ssl) == TLS1_3_VERSION) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif // TLS1_3_VERSION
|
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
const unsigned char *id;
|
|
|
|
unsigned int idlen;
|
|
|
|
|
|
|
|
id = SSL_SESSION_get_id(session, &idlen);
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2017-01-09 15:49:10 +01:00
|
|
|
LOG(INFO) << "Memcached: cache session, id=" << util::format_hex(id, idlen);
|
2015-07-25 15:22:17 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 16:02:44 +02:00
|
|
|
auto req = std::make_unique<MemcachedRequest>();
|
2018-10-17 07:02:57 +02:00
|
|
|
req->op = MemcachedOp::ADD;
|
2016-03-24 17:10:58 +01:00
|
|
|
req->key = MEMCACHED_SESSION_CACHE_KEY_PREFIX.str();
|
2016-10-01 17:19:50 +02:00
|
|
|
req->key +=
|
|
|
|
util::format_hex(balloc, StringRef{id, static_cast<size_t>(idlen)});
|
2015-07-25 15:22:17 +02:00
|
|
|
|
|
|
|
auto sessionlen = i2d_SSL_SESSION(session, nullptr);
|
|
|
|
req->value.resize(sessionlen);
|
|
|
|
auto buf = &req->value[0];
|
|
|
|
i2d_SSL_SESSION(session, &buf);
|
|
|
|
req->expiry = 12_h;
|
|
|
|
req->cb = [](MemcachedRequest *req, MemcachedResult res) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Memcached: session cache done. key=" << req->key
|
2018-10-17 07:11:36 +02:00
|
|
|
<< ", status_code=" << static_cast<uint16_t>(res.status_code)
|
|
|
|
<< ", value="
|
2015-07-25 15:22:17 +02:00
|
|
|
<< std::string(std::begin(res.value), std::end(res.value));
|
|
|
|
}
|
2018-10-17 07:11:36 +02:00
|
|
|
if (res.status_code != MemcachedStatusCode::NO_ERROR) {
|
2015-07-25 15:22:17 +02:00
|
|
|
LOG(WARN) << "Memcached: failed to cache session key=" << req->key
|
2018-10-17 07:11:36 +02:00
|
|
|
<< ", status_code=" << static_cast<uint16_t>(res.status_code)
|
|
|
|
<< ", value="
|
2015-07-25 15:22:17 +02:00
|
|
|
<< std::string(std::begin(res.value), std::end(res.value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
assert(!req->canceled);
|
|
|
|
|
|
|
|
dispatcher->add_request(std::move(req));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2016-05-07 09:18:58 +02:00
|
|
|
SSL_SESSION *tls_session_get_cb(SSL *ssl,
|
2016-09-10 17:38:20 +02:00
|
|
|
#if OPENSSL_1_1_API
|
2016-05-07 09:18:58 +02:00
|
|
|
const unsigned char *id,
|
2016-09-10 17:38:20 +02:00
|
|
|
#else // !OPENSSL_1_1_API
|
2016-05-07 09:18:58 +02:00
|
|
|
unsigned char *id,
|
2016-09-10 17:38:20 +02:00
|
|
|
#endif // !OPENSSL_1_1_API
|
2016-05-07 09:18:58 +02:00
|
|
|
int idlen, int *copy) {
|
2015-08-12 17:12:08 +02:00
|
|
|
auto conn = static_cast<Connection *>(SSL_get_app_data(ssl));
|
|
|
|
auto handler = static_cast<ClientHandler *>(conn->data);
|
2015-07-25 15:22:17 +02:00
|
|
|
auto worker = handler->get_worker();
|
|
|
|
auto dispatcher = worker->get_session_cache_memcached_dispatcher();
|
2016-10-01 17:19:50 +02:00
|
|
|
auto &balloc = handler->get_block_allocator();
|
2015-07-25 15:22:17 +02:00
|
|
|
|
2017-03-22 13:33:31 +01:00
|
|
|
if (idlen == 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
if (conn->tls.cached_session) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Memcached: found cached session, id="
|
|
|
|
<< util::format_hex(id, idlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is required, without this, memory leak occurs.
|
|
|
|
*copy = 0;
|
|
|
|
|
|
|
|
auto session = conn->tls.cached_session;
|
|
|
|
conn->tls.cached_session = nullptr;
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Memcached: get cached session, id="
|
|
|
|
<< util::format_hex(id, idlen);
|
|
|
|
}
|
|
|
|
|
2018-10-15 16:02:44 +02:00
|
|
|
auto req = std::make_unique<MemcachedRequest>();
|
2018-10-17 07:02:57 +02:00
|
|
|
req->op = MemcachedOp::GET;
|
2016-03-24 17:10:58 +01:00
|
|
|
req->key = MEMCACHED_SESSION_CACHE_KEY_PREFIX.str();
|
2016-10-01 17:19:50 +02:00
|
|
|
req->key +=
|
|
|
|
util::format_hex(balloc, StringRef{id, static_cast<size_t>(idlen)});
|
2015-07-25 15:22:17 +02:00
|
|
|
req->cb = [conn](MemcachedRequest *, MemcachedResult res) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2018-10-17 07:11:36 +02:00
|
|
|
LOG(INFO) << "Memcached: returned status code "
|
|
|
|
<< static_cast<uint16_t>(res.status_code);
|
2015-07-25 15:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We might stop reading, so start it again
|
|
|
|
conn->rlimit.startw();
|
|
|
|
ev_timer_again(conn->loop, &conn->rt);
|
|
|
|
|
|
|
|
conn->wlimit.startw();
|
|
|
|
ev_timer_again(conn->loop, &conn->wt);
|
|
|
|
|
|
|
|
conn->tls.cached_session_lookup_req = nullptr;
|
2018-10-17 07:11:36 +02:00
|
|
|
if (res.status_code != MemcachedStatusCode::NO_ERROR) {
|
2018-10-17 01:52:27 +02:00
|
|
|
conn->tls.handshake_state = TLSHandshakeState::CANCEL_SESSION_CACHE;
|
2015-07-25 15:22:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *p = res.value.data();
|
|
|
|
|
|
|
|
auto session = d2i_SSL_SESSION(nullptr, &p, res.value.size());
|
|
|
|
if (!session) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "cannot materialize session";
|
|
|
|
}
|
2018-10-17 01:52:27 +02:00
|
|
|
conn->tls.handshake_state = TLSHandshakeState::CANCEL_SESSION_CACHE;
|
2015-07-25 15:22:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->tls.cached_session = session;
|
2018-10-17 01:52:27 +02:00
|
|
|
conn->tls.handshake_state = TLSHandshakeState::GOT_SESSION_CACHE;
|
2015-07-25 15:22:17 +02:00
|
|
|
};
|
|
|
|
|
2018-10-17 01:52:27 +02:00
|
|
|
conn->tls.handshake_state = TLSHandshakeState::WAIT_FOR_SESSION_CACHE;
|
2015-07-25 15:22:17 +02:00
|
|
|
conn->tls.cached_session_lookup_req = req.get();
|
|
|
|
|
|
|
|
dispatcher->add_request(std::move(req));
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-01-07 16:01:09 +01:00
|
|
|
namespace {
|
|
|
|
int ticket_key_cb(SSL *ssl, unsigned char *key_name, unsigned char *iv,
|
|
|
|
EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc) {
|
2015-08-12 17:12:08 +02:00
|
|
|
auto conn = static_cast<Connection *>(SSL_get_app_data(ssl));
|
|
|
|
auto handler = static_cast<ClientHandler *>(conn->data);
|
2015-02-11 11:18:41 +01:00
|
|
|
auto worker = handler->get_worker();
|
2015-07-23 16:13:29 +02:00
|
|
|
auto ticket_keys = worker->get_ticket_keys();
|
2015-01-07 17:57:59 +01:00
|
|
|
|
2015-01-07 16:01:09 +01:00
|
|
|
if (!ticket_keys) {
|
2015-01-08 12:46:35 +01:00
|
|
|
// No ticket keys available.
|
|
|
|
return -1;
|
2015-01-07 16:01:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto &keys = ticket_keys->keys;
|
|
|
|
assert(!keys.empty());
|
|
|
|
|
|
|
|
if (enc) {
|
|
|
|
if (RAND_bytes(iv, EVP_MAX_IV_LENGTH) == 0) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
CLOG(INFO, handler) << "session ticket key: RAND_bytes failed";
|
|
|
|
}
|
2015-01-08 12:46:35 +01:00
|
|
|
return -1;
|
2015-01-07 16:01:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto &key = keys[0];
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
CLOG(INFO, handler) << "encrypt session ticket key: "
|
2015-07-17 18:49:20 +02:00
|
|
|
<< util::format_hex(key.data.name);
|
2015-01-07 16:01:09 +01:00
|
|
|
}
|
|
|
|
|
2015-07-26 19:12:07 +02:00
|
|
|
std::copy(std::begin(key.data.name), std::end(key.data.name), key_name);
|
2015-01-07 16:01:09 +01:00
|
|
|
|
2016-01-18 06:21:09 +01:00
|
|
|
EVP_EncryptInit_ex(ctx, get_config()->tls.ticket.cipher, nullptr,
|
2015-07-26 19:12:07 +02:00
|
|
|
key.data.enc_key.data(), iv);
|
|
|
|
HMAC_Init_ex(hctx, key.data.hmac_key.data(), key.hmac_keylen, key.hmac,
|
|
|
|
nullptr);
|
2015-01-07 16:01:09 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < keys.size(); ++i) {
|
2015-07-27 14:13:02 +02:00
|
|
|
auto &key = keys[i];
|
2015-07-26 19:12:07 +02:00
|
|
|
if (std::equal(std::begin(key.data.name), std::end(key.data.name),
|
|
|
|
key_name)) {
|
2015-01-07 16:01:09 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == keys.size()) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
CLOG(INFO, handler) << "session ticket key "
|
|
|
|
<< util::format_hex(key_name, 16) << " not found";
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
CLOG(INFO, handler) << "decrypt session ticket key: "
|
|
|
|
<< util::format_hex(key_name, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &key = keys[i];
|
2015-07-26 19:12:07 +02:00
|
|
|
HMAC_Init_ex(hctx, key.data.hmac_key.data(), key.hmac_keylen, key.hmac,
|
|
|
|
nullptr);
|
|
|
|
EVP_DecryptInit_ex(ctx, key.cipher, nullptr, key.data.enc_key.data(), iv);
|
2015-01-07 16:01:09 +01:00
|
|
|
|
|
|
|
return i == 0 ? 1 : 2;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-01-18 11:53:52 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void info_callback(const SSL *ssl, int where, int ret) {
|
2017-04-01 08:24:12 +02:00
|
|
|
#ifdef TLS1_3_VERSION
|
|
|
|
// TLSv1.3 has no renegotiation.
|
|
|
|
if (SSL_version(ssl) == TLS1_3_VERSION) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif // TLS1_3_VERSION
|
|
|
|
|
2014-01-18 11:53:52 +01:00
|
|
|
// To mitigate possible DOS attack using lots of renegotiations, we
|
|
|
|
// disable renegotiation. Since OpenSSL does not provide an easy way
|
|
|
|
// to disable it, we check that renegotiation is started in this
|
|
|
|
// callback.
|
2014-11-27 15:39:04 +01:00
|
|
|
if (where & SSL_CB_HANDSHAKE_START) {
|
2015-02-04 13:15:58 +01:00
|
|
|
auto conn = static_cast<Connection *>(SSL_get_app_data(ssl));
|
|
|
|
if (conn && conn->tls.initial_handshake_done) {
|
|
|
|
auto handler = static_cast<ClientHandler *>(conn->data);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2014-01-18 11:53:52 +01:00
|
|
|
CLOG(INFO, handler) << "TLS renegotiation started";
|
|
|
|
}
|
2015-02-04 13:15:58 +01:00
|
|
|
handler->start_immediate_shutdown();
|
2014-01-18 11:53:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-01-01 15:54:28 +01:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int alpn_select_proto_cb(SSL *ssl, const unsigned char **out,
|
|
|
|
unsigned char *outlen, const unsigned char *in,
|
|
|
|
unsigned int inlen, void *arg) {
|
2014-05-21 14:16:44 +02:00
|
|
|
// We assume that get_config()->npn_list contains ALPN protocol
|
|
|
|
// identifier sorted by preference order. So we just break when we
|
|
|
|
// found the first overlap.
|
2016-01-18 06:21:09 +01:00
|
|
|
for (const auto &target_proto_id : get_config()->tls.npn_list) {
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto p = in, end = in + inlen; p < end;) {
|
2014-05-21 14:16:44 +02:00
|
|
|
auto proto_id = p + 1;
|
|
|
|
auto proto_len = *p;
|
2014-04-26 15:37:48 +02:00
|
|
|
|
2015-07-20 15:37:26 +02:00
|
|
|
if (proto_id + proto_len <= end &&
|
2016-10-02 15:19:31 +02:00
|
|
|
util::streq(target_proto_id, StringRef{proto_id, proto_len})) {
|
2014-04-26 12:36:35 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
*out = reinterpret_cast<const unsigned char *>(proto_id);
|
2014-05-21 14:16:44 +02:00
|
|
|
*outlen = proto_len;
|
2014-04-26 12:36:35 +02:00
|
|
|
|
2014-05-21 14:16:44 +02:00
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
2014-04-26 12:36:35 +02:00
|
|
|
|
2014-05-21 14:16:44 +02:00
|
|
|
p += 1 + proto_len;
|
|
|
|
}
|
2014-04-26 12:36:35 +02:00
|
|
|
}
|
|
|
|
|
2014-05-21 14:16:44 +02:00
|
|
|
return SSL_TLSEXT_ERR_NOACK;
|
2014-01-01 15:54:28 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
|
2018-04-14 10:48:38 +02:00
|
|
|
#if !LIBRESSL_IN_USE && OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2017-10-23 17:17:07 +02:00
|
|
|
|
2018-06-09 09:21:30 +02:00
|
|
|
# ifndef TLSEXT_TYPE_signed_certificate_timestamp
|
|
|
|
# define TLSEXT_TYPE_signed_certificate_timestamp 18
|
|
|
|
# endif // !TLSEXT_TYPE_signed_certificate_timestamp
|
2016-10-08 12:03:21 +02:00
|
|
|
|
|
|
|
namespace {
|
2017-04-09 06:40:38 +02:00
|
|
|
int sct_add_cb(SSL *ssl, unsigned int ext_type, unsigned int context,
|
|
|
|
const unsigned char **out, size_t *outlen, X509 *x,
|
|
|
|
size_t chainidx, int *al, void *add_arg) {
|
|
|
|
assert(ext_type == TLSEXT_TYPE_signed_certificate_timestamp);
|
|
|
|
|
|
|
|
auto conn = static_cast<Connection *>(SSL_get_app_data(ssl));
|
|
|
|
if (!conn->tls.sct_requested) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "sct_add_cb is called, chainidx=" << chainidx << ", x=" << x
|
2018-08-31 14:55:33 +02:00
|
|
|
<< ", context=" << log::hex << context;
|
2017-04-09 06:40:38 +02:00
|
|
|
}
|
|
|
|
|
2017-04-09 07:38:18 +02:00
|
|
|
// We only have SCTs for leaf certificate.
|
|
|
|
if (chainidx != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-08 12:03:21 +02:00
|
|
|
auto ssl_ctx = SSL_get_SSL_CTX(ssl);
|
|
|
|
auto tls_ctx_data =
|
|
|
|
static_cast<TLSContextData *>(SSL_CTX_get_app_data(ssl_ctx));
|
|
|
|
|
|
|
|
*out = tls_ctx_data->sct_data.data();
|
|
|
|
*outlen = tls_ctx_data->sct_data.size();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2017-04-09 06:40:38 +02:00
|
|
|
void sct_free_cb(SSL *ssl, unsigned int ext_type, unsigned int context,
|
|
|
|
const unsigned char *out, void *add_arg) {
|
|
|
|
assert(ext_type == TLSEXT_TYPE_signed_certificate_timestamp);
|
2016-10-08 12:03:21 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2017-04-09 06:40:38 +02:00
|
|
|
int sct_parse_cb(SSL *ssl, unsigned int ext_type, unsigned int context,
|
|
|
|
const unsigned char *in, size_t inlen, X509 *x,
|
|
|
|
size_t chainidx, int *al, void *parse_arg) {
|
|
|
|
assert(ext_type == TLSEXT_TYPE_signed_certificate_timestamp);
|
2016-10-08 12:03:21 +02:00
|
|
|
// client SHOULD send 0 length extension_data, but it is still
|
|
|
|
// SHOULD, and not MUST.
|
|
|
|
|
2017-04-09 06:40:38 +02:00
|
|
|
// For TLSv1.3 Certificate message, sct_add_cb is called even if
|
|
|
|
// client has not sent signed_certificate_timestamp extension in its
|
|
|
|
// ClientHello. Explicitly remember that client has included it
|
|
|
|
// here.
|
|
|
|
auto conn = static_cast<Connection *>(SSL_get_app_data(ssl));
|
|
|
|
conn->tls.sct_requested = true;
|
|
|
|
|
2016-10-08 12:03:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} // namespace
|
2017-04-09 06:40:38 +02:00
|
|
|
|
2018-06-09 09:21:30 +02:00
|
|
|
# if !OPENSSL_1_1_1_API
|
2017-04-09 06:40:38 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
int legacy_sct_add_cb(SSL *ssl, unsigned int ext_type,
|
|
|
|
const unsigned char **out, size_t *outlen, int *al,
|
|
|
|
void *add_arg) {
|
|
|
|
return sct_add_cb(ssl, ext_type, 0, out, outlen, nullptr, 0, al, add_arg);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void legacy_sct_free_cb(SSL *ssl, unsigned int ext_type,
|
|
|
|
const unsigned char *out, void *add_arg) {
|
|
|
|
sct_free_cb(ssl, ext_type, 0, out, add_arg);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int legacy_sct_parse_cb(SSL *ssl, unsigned int ext_type,
|
|
|
|
const unsigned char *in, size_t inlen, int *al,
|
|
|
|
void *parse_arg) {
|
|
|
|
return sct_parse_cb(ssl, ext_type, 0, in, inlen, nullptr, 0, al, parse_arg);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2018-06-09 09:21:30 +02:00
|
|
|
# endif // !OPENSSL_1_1_1_API
|
|
|
|
#endif // !LIBRESSL_IN_USE && OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2016-10-08 12:03:21 +02:00
|
|
|
|
2018-04-14 10:48:38 +02:00
|
|
|
#ifndef OPENSSL_NO_PSK
|
2017-01-07 15:42:51 +01:00
|
|
|
namespace {
|
|
|
|
unsigned int psk_server_cb(SSL *ssl, const char *identity, unsigned char *psk,
|
|
|
|
unsigned int max_psk_len) {
|
|
|
|
auto config = get_config();
|
|
|
|
auto &tlsconf = config->tls;
|
|
|
|
|
|
|
|
auto it = tlsconf.psk_secrets.find(StringRef{identity});
|
|
|
|
if (it == std::end(tlsconf.psk_secrets)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &secret = (*it).second;
|
|
|
|
if (secret.size() > max_psk_len) {
|
|
|
|
LOG(ERROR) << "The size of PSK secret is " << secret.size()
|
|
|
|
<< ", but the acceptable maximum size is" << max_psk_len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::copy(std::begin(secret), std::end(secret), psk);
|
|
|
|
|
|
|
|
return static_cast<unsigned int>(secret.size());
|
|
|
|
}
|
|
|
|
} // namespace
|
2018-04-14 10:48:38 +02:00
|
|
|
#endif // !OPENSSL_NO_PSK
|
2017-01-07 15:42:51 +01:00
|
|
|
|
2018-04-14 10:48:38 +02:00
|
|
|
#ifndef OPENSSL_NO_PSK
|
2017-01-07 16:24:33 +01:00
|
|
|
namespace {
|
|
|
|
unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity_out,
|
|
|
|
unsigned int max_identity_len, unsigned char *psk,
|
|
|
|
unsigned int max_psk_len) {
|
|
|
|
auto config = get_config();
|
|
|
|
auto &tlsconf = config->tls;
|
|
|
|
|
2017-01-08 14:25:30 +01:00
|
|
|
auto &identity = tlsconf.client.psk.identity;
|
|
|
|
auto &secret = tlsconf.client.psk.secret;
|
2017-01-07 16:24:33 +01:00
|
|
|
|
|
|
|
if (identity.empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (identity.size() + 1 > max_identity_len) {
|
|
|
|
LOG(ERROR) << "The size of PSK identity is " << identity.size()
|
|
|
|
<< ", but the acceptable maximum size is " << max_identity_len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (secret.size() > max_psk_len) {
|
|
|
|
LOG(ERROR) << "The size of PSK secret is " << secret.size()
|
|
|
|
<< ", but the acceptable maximum size is " << max_psk_len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*std::copy(std::begin(identity), std::end(identity), identity_out) = '\0';
|
|
|
|
std::copy(std::begin(secret), std::end(secret), psk);
|
|
|
|
|
2017-02-16 15:02:19 +01:00
|
|
|
return static_cast<unsigned int>(secret.size());
|
2017-01-07 16:24:33 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
2018-04-14 10:48:38 +02:00
|
|
|
#endif // !OPENSSL_NO_PSK
|
2017-01-07 16:24:33 +01:00
|
|
|
|
2016-03-25 14:45:33 +01:00
|
|
|
struct TLSProtocol {
|
|
|
|
StringRef name;
|
|
|
|
long int mask;
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr TLSProtocol TLS_PROTOS[] = {
|
|
|
|
TLSProtocol{StringRef::from_lit("TLSv1.2"), SSL_OP_NO_TLSv1_2},
|
|
|
|
TLSProtocol{StringRef::from_lit("TLSv1.1"), SSL_OP_NO_TLSv1_1},
|
|
|
|
TLSProtocol{StringRef::from_lit("TLSv1.0"), SSL_OP_NO_TLSv1}};
|
2014-06-08 15:52:27 +02:00
|
|
|
|
2016-10-02 15:21:38 +02:00
|
|
|
long int create_tls_proto_mask(const std::vector<StringRef> &tls_proto_list) {
|
2014-01-02 03:49:38 +01:00
|
|
|
long int res = 0;
|
2014-06-08 15:52:27 +02:00
|
|
|
|
2016-03-25 14:45:33 +01:00
|
|
|
for (auto &supported : TLS_PROTOS) {
|
|
|
|
auto ok = false;
|
|
|
|
for (auto &name : tls_proto_list) {
|
|
|
|
if (util::strieq(supported.name, name)) {
|
|
|
|
ok = true;
|
2014-01-02 03:49:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-03-25 14:45:33 +01:00
|
|
|
if (!ok) {
|
|
|
|
res |= supported.mask;
|
2014-01-02 03:49:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-10-08 12:03:21 +02:00
|
|
|
SSL_CTX *create_ssl_context(const char *private_key_file, const char *cert_file,
|
|
|
|
const std::vector<uint8_t> &sct_data
|
2015-09-23 12:45:53 +02:00
|
|
|
#ifdef HAVE_NEVERBLEED
|
|
|
|
,
|
|
|
|
neverbleed_t *nb
|
|
|
|
#endif // HAVE_NEVERBLEED
|
2017-11-23 06:19:12 +01:00
|
|
|
) {
|
2015-01-12 14:47:30 +01:00
|
|
|
auto ssl_ctx = SSL_CTX_new(SSLv23_server_method());
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!ssl_ctx) {
|
2013-09-23 11:49:39 +02:00
|
|
|
LOG(FATAL) << ERR_error_string(ERR_get_error(), nullptr);
|
2012-06-05 18:26:04 +02:00
|
|
|
DIE();
|
|
|
|
}
|
2014-01-02 03:49:38 +01:00
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
constexpr auto ssl_opts =
|
|
|
|
(SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | SSL_OP_NO_SSLv2 |
|
|
|
|
SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION |
|
|
|
|
SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | SSL_OP_SINGLE_ECDH_USE |
|
2018-09-08 12:10:59 +02:00
|
|
|
SSL_OP_SINGLE_DH_USE |
|
|
|
|
SSL_OP_CIPHER_SERVER_PREFERENCE
|
|
|
|
#if OPENSSL_1_1_1_API
|
|
|
|
// The reason for disabling built-in anti-replay in OpenSSL is
|
|
|
|
// that it only works if client gets back to the same server.
|
|
|
|
// The freshness check described in
|
|
|
|
// https://tools.ietf.org/html/rfc8446#section-8.3 is still
|
|
|
|
// performed.
|
|
|
|
| SSL_OP_NO_ANTI_REPLAY
|
|
|
|
#endif // OPENSSL_1_1_1_API
|
|
|
|
;
|
2015-06-22 16:26:45 +02:00
|
|
|
|
2016-10-08 04:34:23 +02:00
|
|
|
auto config = mod_config();
|
|
|
|
auto &tlsconf = config->tls;
|
2016-01-18 06:21:09 +01:00
|
|
|
|
|
|
|
SSL_CTX_set_options(ssl_ctx, ssl_opts | tlsconf.tls_proto_mask);
|
2012-06-07 15:39:57 +02:00
|
|
|
|
2017-04-01 07:47:36 +02:00
|
|
|
if (nghttp2::tls::ssl_ctx_set_proto_versions(
|
2017-02-14 16:40:43 +01:00
|
|
|
ssl_ctx, tlsconf.min_proto_version, tlsconf.max_proto_version) != 0) {
|
|
|
|
LOG(FATAL) << "Could not set TLS protocol version";
|
2017-02-13 15:34:45 +01:00
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
|
2012-07-14 16:24:03 +02:00
|
|
|
const unsigned char sid_ctx[] = "shrpx";
|
2014-11-27 15:39:04 +01:00
|
|
|
SSL_CTX_set_session_id_context(ssl_ctx, sid_ctx, sizeof(sid_ctx) - 1);
|
2012-07-14 16:24:03 +02:00
|
|
|
SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_SERVER);
|
2015-07-25 15:22:17 +02:00
|
|
|
|
2016-02-14 12:59:10 +01:00
|
|
|
if (!tlsconf.session_cache.memcached.host.empty()) {
|
2015-07-25 15:22:17 +02:00
|
|
|
SSL_CTX_sess_set_new_cb(ssl_ctx, tls_session_new_cb);
|
|
|
|
SSL_CTX_sess_set_get_cb(ssl_ctx, tls_session_get_cb);
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:21:09 +01:00
|
|
|
SSL_CTX_set_timeout(ssl_ctx, tlsconf.session_timeout.count());
|
2012-07-14 16:24:03 +02:00
|
|
|
|
2017-01-09 06:43:13 +01:00
|
|
|
if (SSL_CTX_set_cipher_list(ssl_ctx, tlsconf.ciphers.c_str()) == 0) {
|
|
|
|
LOG(FATAL) << "SSL_CTX_set_cipher_list " << tlsconf.ciphers
|
2014-11-27 15:39:04 +01:00
|
|
|
<< " failed: " << ERR_error_string(ERR_get_error(), nullptr);
|
2014-01-13 15:01:22 +01:00
|
|
|
DIE();
|
2012-08-20 14:50:03 +02:00
|
|
|
}
|
|
|
|
|
2018-09-09 09:32:34 +02:00
|
|
|
#if OPENSSL_1_1_1_API
|
|
|
|
if (SSL_CTX_set_ciphersuites(ssl_ctx, tlsconf.tls13_ciphers.c_str()) == 0) {
|
|
|
|
LOG(FATAL) << "SSL_CTX_set_ciphersuites " << tlsconf.tls13_ciphers
|
|
|
|
<< " failed: " << ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
#endif // OPENSSL_1_1_1_API
|
|
|
|
|
2013-09-07 16:37:17 +02:00
|
|
|
#ifndef OPENSSL_NO_EC
|
2018-06-09 09:21:30 +02:00
|
|
|
# if !LIBRESSL_LEGACY_API && OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2016-10-08 03:50:28 +02:00
|
|
|
if (SSL_CTX_set1_curves_list(ssl_ctx, tlsconf.ecdh_curves.c_str()) != 1) {
|
|
|
|
LOG(FATAL) << "SSL_CTX_set1_curves_list " << tlsconf.ecdh_curves
|
|
|
|
<< " failed";
|
|
|
|
DIE();
|
|
|
|
}
|
2018-06-09 09:21:30 +02:00
|
|
|
# if !defined(OPENSSL_IS_BORINGSSL) && !OPENSSL_1_1_API
|
2016-10-08 03:50:28 +02:00
|
|
|
// It looks like we need this function call for OpenSSL 1.0.2. This
|
2016-10-25 16:20:34 +02:00
|
|
|
// function was deprecated in OpenSSL 1.1.0 and BoringSSL.
|
2016-10-08 03:50:28 +02:00
|
|
|
SSL_CTX_set_ecdh_auto(ssl_ctx, 1);
|
2018-06-09 09:21:30 +02:00
|
|
|
# endif // !defined(OPENSSL_IS_BORINGSSL) && !OPENSSL_1_1_API
|
|
|
|
# else // LIBRESSL_LEGACY_API || OPENSSL_VERSION_NUBMER < 0x10002000L
|
2013-08-30 15:07:42 +02:00
|
|
|
// Use P-256, which is sufficiently secure at the time of this
|
|
|
|
// writing.
|
|
|
|
auto ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (ecdh == nullptr) {
|
2013-08-30 15:07:42 +02:00
|
|
|
LOG(FATAL) << "EC_KEY_new_by_curv_name failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
SSL_CTX_set_tmp_ecdh(ssl_ctx, ecdh);
|
|
|
|
EC_KEY_free(ecdh);
|
2018-06-09 09:21:30 +02:00
|
|
|
# endif // LIBRESSL_LEGACY_API || OPENSSL_VERSION_NUBMER < 0x10002000L
|
|
|
|
#endif // OPENSSL_NO_EC
|
2013-08-30 15:07:42 +02:00
|
|
|
|
2016-02-14 13:22:28 +01:00
|
|
|
if (!tlsconf.dh_param_file.empty()) {
|
2013-08-30 15:07:42 +02:00
|
|
|
// Read DH parameters from file
|
2016-02-14 13:22:28 +01:00
|
|
|
auto bio = BIO_new_file(tlsconf.dh_param_file.c_str(), "r");
|
2014-11-27 15:39:04 +01:00
|
|
|
if (bio == nullptr) {
|
2013-08-30 15:07:42 +02:00
|
|
|
LOG(FATAL) << "BIO_new_file() failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
auto dh = PEM_read_bio_DHparams(bio, nullptr, nullptr, nullptr);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dh == nullptr) {
|
2013-08-30 15:07:42 +02:00
|
|
|
LOG(FATAL) << "PEM_read_bio_DHparams() failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
SSL_CTX_set_tmp_dh(ssl_ctx, dh);
|
|
|
|
DH_free(dh);
|
|
|
|
BIO_free(bio);
|
|
|
|
}
|
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
|
2017-06-13 16:00:26 +02:00
|
|
|
|
|
|
|
if (SSL_CTX_set_default_verify_paths(ssl_ctx) != 1) {
|
|
|
|
LOG(WARN) << "Could not load system trusted ca certificates: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tlsconf.cacert.empty()) {
|
|
|
|
if (SSL_CTX_load_verify_locations(ssl_ctx, tlsconf.cacert.c_str(),
|
|
|
|
nullptr) != 1) {
|
|
|
|
LOG(FATAL) << "Could not load trusted ca certificates from "
|
|
|
|
<< tlsconf.cacert << ": "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 13:20:40 +01:00
|
|
|
if (!tlsconf.private_key_passwd.empty()) {
|
2012-12-03 07:33:04 +01:00
|
|
|
SSL_CTX_set_default_passwd_cb(ssl_ctx, ssl_pem_passwd_cb);
|
2016-10-08 04:34:23 +02:00
|
|
|
SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, config);
|
2012-12-03 07:33:04 +01:00
|
|
|
}
|
2015-09-23 12:45:53 +02:00
|
|
|
|
|
|
|
#ifndef HAVE_NEVERBLEED
|
2014-11-27 15:39:04 +01:00
|
|
|
if (SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key_file,
|
|
|
|
SSL_FILETYPE_PEM) != 1) {
|
2012-11-14 13:14:11 +01:00
|
|
|
LOG(FATAL) << "SSL_CTX_use_PrivateKey_file failed: "
|
2013-09-23 11:49:39 +02:00
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
2015-09-23 12:45:53 +02:00
|
|
|
}
|
|
|
|
#else // HAVE_NEVERBLEED
|
|
|
|
std::array<char, NEVERBLEED_ERRBUF_SIZE> errbuf;
|
|
|
|
if (neverbleed_load_private_key_file(nb, ssl_ctx, private_key_file,
|
|
|
|
errbuf.data()) != 1) {
|
|
|
|
LOG(FATAL) << "neverbleed_load_private_key_file failed: " << errbuf.data();
|
2012-06-05 18:26:04 +02:00
|
|
|
DIE();
|
|
|
|
}
|
2015-09-23 12:45:53 +02:00
|
|
|
#endif // HAVE_NEVERBLEED
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (SSL_CTX_use_certificate_chain_file(ssl_ctx, cert_file) != 1) {
|
2012-11-14 13:14:11 +01:00
|
|
|
LOG(FATAL) << "SSL_CTX_use_certificate_file failed: "
|
2013-09-23 11:49:39 +02:00
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
2012-06-05 18:26:04 +02:00
|
|
|
DIE();
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (SSL_CTX_check_private_key(ssl_ctx) != 1) {
|
2012-11-14 13:14:11 +01:00
|
|
|
LOG(FATAL) << "SSL_CTX_check_private_key failed: "
|
2013-09-23 11:49:39 +02:00
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
2012-06-05 18:26:04 +02:00
|
|
|
DIE();
|
|
|
|
}
|
2016-01-18 06:21:09 +01:00
|
|
|
if (tlsconf.client_verify.enabled) {
|
2016-02-14 13:09:15 +01:00
|
|
|
if (!tlsconf.client_verify.cacert.empty()) {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (SSL_CTX_load_verify_locations(
|
2016-02-14 13:09:15 +01:00
|
|
|
ssl_ctx, tlsconf.client_verify.cacert.c_str(), nullptr) != 1) {
|
2014-06-08 14:02:40 +02:00
|
|
|
|
2013-11-01 17:10:18 +01:00
|
|
|
LOG(FATAL) << "Could not load trusted ca certificates from "
|
2016-02-14 13:09:15 +01:00
|
|
|
<< tlsconf.client_verify.cacert << ": "
|
2013-11-01 17:10:18 +01:00
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
DIE();
|
|
|
|
}
|
2014-01-08 16:07:51 +01:00
|
|
|
// It is heard that SSL_CTX_load_verify_locations() may leave
|
|
|
|
// error even though it returns success. See
|
|
|
|
// http://forum.nginx.org/read.php?29,242540
|
|
|
|
ERR_clear_error();
|
2016-02-14 13:09:15 +01:00
|
|
|
auto list = SSL_load_client_CA_file(tlsconf.client_verify.cacert.c_str());
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!list) {
|
2014-01-08 16:07:51 +01:00
|
|
|
LOG(FATAL) << "Could not load ca certificates from "
|
2016-02-14 13:09:15 +01:00
|
|
|
<< tlsconf.client_verify.cacert << ": "
|
2014-01-08 16:07:51 +01:00
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
SSL_CTX_set_client_CA_list(ssl_ctx, list);
|
2013-11-01 17:10:18 +01:00
|
|
|
}
|
2017-04-30 08:45:53 +02:00
|
|
|
SSL_CTX_set_verify(ssl_ctx,
|
|
|
|
SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE |
|
|
|
|
SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
|
2012-06-05 18:26:04 +02:00
|
|
|
verify_callback);
|
|
|
|
}
|
2013-02-06 15:27:05 +01:00
|
|
|
SSL_CTX_set_tlsext_servername_callback(ssl_ctx, servername_callback);
|
2015-01-07 16:01:09 +01:00
|
|
|
SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx, ticket_key_cb);
|
2015-09-29 16:31:50 +02:00
|
|
|
#ifndef OPENSSL_IS_BORINGSSL
|
2015-03-30 16:20:40 +02:00
|
|
|
SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
|
2015-09-29 16:31:50 +02:00
|
|
|
#endif // OPENSSL_IS_BORINGSSL
|
2014-01-18 11:53:52 +01:00
|
|
|
SSL_CTX_set_info_callback(ssl_ctx, info_callback);
|
2013-02-06 15:27:05 +01:00
|
|
|
|
2017-03-27 16:58:49 +02:00
|
|
|
#ifdef OPENSSL_IS_BORINGSSL
|
|
|
|
SSL_CTX_set_early_data_enabled(ssl_ctx, 1);
|
|
|
|
#endif // OPENSSL_IS_BORINGSSL
|
|
|
|
|
2014-01-01 15:54:28 +01:00
|
|
|
// NPN advertisement
|
2018-03-25 18:27:23 +02:00
|
|
|
#ifndef OPENSSL_NO_NEXTPROTONEG
|
2014-06-10 16:15:29 +02:00
|
|
|
SSL_CTX_set_next_protos_advertised_cb(ssl_ctx, next_proto_cb, nullptr);
|
2018-04-03 14:39:44 +02:00
|
|
|
#endif // !OPENSSL_NO_NEXTPROTONEG
|
2014-01-01 15:54:28 +01:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
// ALPN selection callback
|
|
|
|
SSL_CTX_set_alpn_select_cb(ssl_ctx, alpn_select_proto_cb, nullptr);
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2015-03-30 16:20:40 +02:00
|
|
|
|
2018-04-14 10:48:38 +02:00
|
|
|
#if !LIBRESSL_IN_USE && OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2017-04-09 06:40:38 +02:00
|
|
|
// SSL_extension_supported(TLSEXT_TYPE_signed_certificate_timestamp)
|
2017-01-06 13:29:04 +01:00
|
|
|
// returns 1, which means OpenSSL internally handles it. But
|
|
|
|
// OpenSSL handles signed_certificate_timestamp extension specially,
|
|
|
|
// and it lets custom handler to process the extension.
|
2017-04-09 06:40:38 +02:00
|
|
|
if (!sct_data.empty()) {
|
2018-06-09 09:21:30 +02:00
|
|
|
# if OPENSSL_1_1_1_API
|
2017-04-09 06:40:38 +02:00
|
|
|
// It is not entirely clear to me that SSL_EXT_CLIENT_HELLO is
|
|
|
|
// required here. sct_parse_cb is called without
|
|
|
|
// SSL_EXT_CLIENT_HELLO being set. But the passed context value
|
|
|
|
// is SSL_EXT_CLIENT_HELLO.
|
|
|
|
if (SSL_CTX_add_custom_ext(
|
|
|
|
ssl_ctx, TLSEXT_TYPE_signed_certificate_timestamp,
|
|
|
|
SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
|
|
|
|
SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_IGNORE_ON_RESUMPTION,
|
|
|
|
sct_add_cb, sct_free_cb, nullptr, sct_parse_cb, nullptr) != 1) {
|
|
|
|
LOG(FATAL) << "SSL_CTX_add_custom_ext failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
DIE();
|
|
|
|
}
|
2018-06-09 09:21:30 +02:00
|
|
|
# else // !OPENSSL_1_1_1_API
|
2017-04-09 06:40:38 +02:00
|
|
|
if (SSL_CTX_add_server_custom_ext(
|
|
|
|
ssl_ctx, TLSEXT_TYPE_signed_certificate_timestamp,
|
|
|
|
legacy_sct_add_cb, legacy_sct_free_cb, nullptr, legacy_sct_parse_cb,
|
|
|
|
nullptr) != 1) {
|
|
|
|
LOG(FATAL) << "SSL_CTX_add_server_custom_ext failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
DIE();
|
|
|
|
}
|
2018-06-09 09:21:30 +02:00
|
|
|
# endif // !OPENSSL_1_1_1_API
|
2016-10-08 12:03:21 +02:00
|
|
|
}
|
2018-04-14 10:48:38 +02:00
|
|
|
#endif // !LIBRESSL_IN_USE && OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2016-10-08 12:03:21 +02:00
|
|
|
|
2018-05-20 15:54:59 +02:00
|
|
|
#if OPENSSL_1_1_1_API
|
|
|
|
if (SSL_CTX_set_max_early_data(ssl_ctx, tlsconf.max_early_data) != 1) {
|
|
|
|
LOG(FATAL) << "SSL_CTX_set_max_early_data failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
#endif // OPENSSL_1_1_1_API
|
|
|
|
|
2018-04-14 10:48:38 +02:00
|
|
|
#ifndef OPENSSL_NO_PSK
|
2017-01-07 15:42:51 +01:00
|
|
|
SSL_CTX_set_psk_server_callback(ssl_ctx, psk_server_cb);
|
2018-04-14 10:48:38 +02:00
|
|
|
#endif // !LIBRESSL_NO_PSK
|
2017-01-07 15:42:51 +01:00
|
|
|
|
2015-03-30 16:20:40 +02:00
|
|
|
auto tls_ctx_data = new TLSContextData();
|
|
|
|
tls_ctx_data->cert_file = cert_file;
|
2016-10-08 12:03:21 +02:00
|
|
|
tls_ctx_data->sct_data = sct_data;
|
2015-03-30 16:20:40 +02:00
|
|
|
|
|
|
|
SSL_CTX_set_app_data(ssl_ctx, tls_ctx_data);
|
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
return ssl_ctx;
|
|
|
|
}
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
namespace {
|
2016-02-06 11:50:21 +01:00
|
|
|
int select_h2_next_proto_cb(SSL *ssl, unsigned char **out,
|
|
|
|
unsigned char *outlen, const unsigned char *in,
|
|
|
|
unsigned int inlen, void *arg) {
|
2014-12-15 14:51:34 +01:00
|
|
|
if (!util::select_h2(const_cast<const unsigned char **>(out), outlen, in,
|
|
|
|
inlen)) {
|
|
|
|
return SSL_TLSEXT_ERR_NOACK;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2014-12-15 14:51:34 +01:00
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-02-06 11:50:21 +01:00
|
|
|
namespace {
|
|
|
|
int select_h1_next_proto_cb(SSL *ssl, unsigned char **out,
|
|
|
|
unsigned char *outlen, const unsigned char *in,
|
|
|
|
unsigned int inlen, void *arg) {
|
|
|
|
auto end = in + inlen;
|
|
|
|
for (; in < end;) {
|
2016-03-24 17:41:06 +01:00
|
|
|
if (util::streq(NGHTTP2_H1_1_ALPN, StringRef{in, in + (in[0] + 1)})) {
|
2016-02-07 08:52:40 +01:00
|
|
|
*out = const_cast<unsigned char *>(in) + 1;
|
|
|
|
*outlen = in[0];
|
2016-02-06 11:50:21 +01:00
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
2016-02-07 08:52:40 +01:00
|
|
|
in += in[0] + 1;
|
2016-02-06 11:50:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return SSL_TLSEXT_ERR_NOACK;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-02-28 08:56:14 +01:00
|
|
|
namespace {
|
|
|
|
int select_next_proto_cb(SSL *ssl, unsigned char **out, unsigned char *outlen,
|
|
|
|
const unsigned char *in, unsigned int inlen,
|
|
|
|
void *arg) {
|
|
|
|
auto conn = static_cast<Connection *>(SSL_get_app_data(ssl));
|
|
|
|
switch (conn->proto) {
|
2018-10-16 15:17:37 +02:00
|
|
|
case Proto::HTTP1:
|
2016-02-28 08:56:14 +01:00
|
|
|
return select_h1_next_proto_cb(ssl, out, outlen, in, inlen, arg);
|
2018-10-16 15:17:37 +02:00
|
|
|
case Proto::HTTP2:
|
2016-02-28 08:56:14 +01:00
|
|
|
return select_h2_next_proto_cb(ssl, out, outlen, in, inlen, arg);
|
|
|
|
default:
|
|
|
|
return SSL_TLSEXT_ERR_NOACK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-09-26 14:28:16 +02:00
|
|
|
SSL_CTX *create_ssl_client_context(
|
|
|
|
#ifdef HAVE_NEVERBLEED
|
2016-02-11 10:34:31 +01:00
|
|
|
neverbleed_t *nb,
|
2015-09-26 14:28:16 +02:00
|
|
|
#endif // HAVE_NEVERBLEED
|
2016-02-12 16:20:38 +01:00
|
|
|
const StringRef &cacert, const StringRef &cert_file,
|
2016-02-28 08:56:14 +01:00
|
|
|
const StringRef &private_key_file,
|
2016-02-11 10:34:31 +01:00
|
|
|
int (*next_proto_select_cb)(SSL *s, unsigned char **out,
|
|
|
|
unsigned char *outlen, const unsigned char *in,
|
|
|
|
unsigned int inlen, void *arg)) {
|
2015-01-12 14:47:30 +01:00
|
|
|
auto ssl_ctx = SSL_CTX_new(SSLv23_client_method());
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!ssl_ctx) {
|
2013-09-23 11:49:39 +02:00
|
|
|
LOG(FATAL) << ERR_error_string(ERR_get_error(), nullptr);
|
2012-11-18 13:23:13 +01:00
|
|
|
DIE();
|
|
|
|
}
|
2015-06-22 16:26:45 +02:00
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
constexpr auto ssl_opts = (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
|
|
|
|
SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
|
|
|
|
SSL_OP_NO_COMPRESSION |
|
|
|
|
SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION;
|
2015-06-22 16:26:45 +02:00
|
|
|
|
2016-01-18 06:21:09 +01:00
|
|
|
auto &tlsconf = get_config()->tls;
|
|
|
|
|
|
|
|
SSL_CTX_set_options(ssl_ctx, ssl_opts | tlsconf.tls_proto_mask);
|
2012-11-18 13:23:13 +01:00
|
|
|
|
2017-11-23 06:19:12 +01:00
|
|
|
SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_CLIENT |
|
|
|
|
SSL_SESS_CACHE_NO_INTERNAL_STORE);
|
2017-03-28 14:06:28 +02:00
|
|
|
SSL_CTX_sess_set_new_cb(ssl_ctx, tls_session_client_new_cb);
|
|
|
|
|
2017-04-01 07:47:36 +02:00
|
|
|
if (nghttp2::tls::ssl_ctx_set_proto_versions(
|
2017-02-14 16:40:43 +01:00
|
|
|
ssl_ctx, tlsconf.min_proto_version, tlsconf.max_proto_version) != 0) {
|
|
|
|
LOG(FATAL) << "Could not set TLS protocol version";
|
2017-02-13 15:34:45 +01:00
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
|
2017-01-09 06:43:13 +01:00
|
|
|
if (SSL_CTX_set_cipher_list(ssl_ctx, tlsconf.client.ciphers.c_str()) == 0) {
|
|
|
|
LOG(FATAL) << "SSL_CTX_set_cipher_list " << tlsconf.client.ciphers
|
2014-11-27 15:39:04 +01:00
|
|
|
<< " failed: " << ERR_error_string(ERR_get_error(), nullptr);
|
2014-01-13 15:01:22 +01:00
|
|
|
DIE();
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
|
|
|
|
2018-09-09 09:32:34 +02:00
|
|
|
#if OPENSSL_1_1_1_API
|
|
|
|
if (SSL_CTX_set_ciphersuites(ssl_ctx, tlsconf.client.tls13_ciphers.c_str()) ==
|
|
|
|
0) {
|
|
|
|
LOG(FATAL) << "SSL_CTX_set_ciphersuites " << tlsconf.client.tls13_ciphers
|
|
|
|
<< " failed: " << ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
#endif // OPENSSL_1_1_1_API
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (SSL_CTX_set_default_verify_paths(ssl_ctx) != 1) {
|
2014-11-08 02:51:56 +01:00
|
|
|
LOG(WARN) << "Could not load system trusted ca certificates: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
2012-11-22 13:46:15 +01:00
|
|
|
}
|
|
|
|
|
2016-02-12 16:20:38 +01:00
|
|
|
if (!cacert.empty()) {
|
|
|
|
if (SSL_CTX_load_verify_locations(ssl_ctx, cacert.c_str(), nullptr) != 1) {
|
2014-06-08 14:02:40 +02:00
|
|
|
|
2016-02-11 10:34:31 +01:00
|
|
|
LOG(FATAL) << "Could not load trusted ca certificates from " << cacert
|
|
|
|
<< ": " << ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 06:41:41 +02:00
|
|
|
if (!tlsconf.insecure) {
|
|
|
|
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-02-12 16:20:38 +01:00
|
|
|
if (!cert_file.empty()) {
|
|
|
|
if (SSL_CTX_use_certificate_chain_file(ssl_ctx, cert_file.c_str()) != 1) {
|
2016-02-11 10:34:31 +01:00
|
|
|
|
|
|
|
LOG(FATAL) << "Could not load client certificate from " << cert_file
|
|
|
|
<< ": " << ERR_error_string(ERR_get_error(), nullptr);
|
2013-11-02 14:58:02 +01:00
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 16:20:38 +01:00
|
|
|
if (!private_key_file.empty()) {
|
2015-09-26 14:28:16 +02:00
|
|
|
#ifndef HAVE_NEVERBLEED
|
2016-02-12 16:20:38 +01:00
|
|
|
if (SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key_file.c_str(),
|
2014-11-27 15:39:04 +01:00
|
|
|
SSL_FILETYPE_PEM) != 1) {
|
2013-11-02 14:58:02 +01:00
|
|
|
LOG(FATAL) << "Could not load client private key from "
|
2016-02-11 10:34:31 +01:00
|
|
|
<< private_key_file << ": "
|
2013-11-02 14:58:02 +01:00
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
DIE();
|
|
|
|
}
|
2015-09-26 14:28:16 +02:00
|
|
|
#else // HAVE_NEVERBLEED
|
|
|
|
std::array<char, NEVERBLEED_ERRBUF_SIZE> errbuf;
|
2016-02-12 16:20:38 +01:00
|
|
|
if (neverbleed_load_private_key_file(nb, ssl_ctx, private_key_file.c_str(),
|
2016-01-18 06:21:09 +01:00
|
|
|
errbuf.data()) != 1) {
|
2016-02-11 10:34:31 +01:00
|
|
|
LOG(FATAL) << "neverbleed_load_private_key_file: could not load client "
|
2016-10-15 11:36:04 +02:00
|
|
|
"private key from "
|
|
|
|
<< private_key_file << ": " << errbuf.data();
|
2015-09-26 14:28:16 +02:00
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
#endif // HAVE_NEVERBLEED
|
2013-11-02 14:58:02 +01:00
|
|
|
}
|
2016-02-07 08:52:40 +01:00
|
|
|
|
2018-04-14 10:48:38 +02:00
|
|
|
#ifndef OPENSSL_NO_PSK
|
2017-01-07 16:24:33 +01:00
|
|
|
SSL_CTX_set_psk_client_callback(ssl_ctx, psk_client_cb);
|
2018-04-14 10:48:38 +02:00
|
|
|
#endif // !OPENSSL_NO_PSK
|
2017-01-07 16:24:33 +01:00
|
|
|
|
2016-02-28 08:56:14 +01:00
|
|
|
// NPN selection callback. This is required to set SSL_CTX because
|
|
|
|
// OpenSSL does not offer SSL_set_next_proto_select_cb.
|
2018-03-25 18:27:23 +02:00
|
|
|
#ifndef OPENSSL_NO_NEXTPROTONEG
|
2016-02-11 10:34:31 +01:00
|
|
|
SSL_CTX_set_next_proto_select_cb(ssl_ctx, next_proto_select_cb, nullptr);
|
2018-04-03 14:39:44 +02:00
|
|
|
#endif // !OPENSSL_NO_NEXTPROTONEG
|
2016-02-06 11:50:21 +01:00
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
return ssl_ctx;
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
SSL *create_ssl(SSL_CTX *ssl_ctx) {
|
|
|
|
auto ssl = SSL_new(ssl_ctx);
|
|
|
|
if (!ssl) {
|
2016-10-15 11:36:04 +02:00
|
|
|
LOG(ERROR) << "SSL_new() failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
2015-07-25 15:22:17 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssl;
|
|
|
|
}
|
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
ClientHandler *accept_connection(Worker *worker, int fd, sockaddr *addr,
|
2016-02-07 09:51:53 +01:00
|
|
|
int addrlen, const UpstreamAddr *faddr) {
|
2016-09-30 16:09:02 +02:00
|
|
|
std::array<char, NI_MAXHOST> host;
|
|
|
|
std::array<char, NI_MAXSERV> service;
|
2012-06-05 18:26:04 +02:00
|
|
|
int rv;
|
2012-11-19 13:40:59 +01:00
|
|
|
|
2016-01-19 15:26:04 +01:00
|
|
|
if (addr->sa_family == AF_UNIX) {
|
2016-09-30 16:09:02 +02:00
|
|
|
std::copy_n("localhost", sizeof("localhost"), std::begin(host));
|
2016-01-19 15:26:04 +01:00
|
|
|
service[0] = '\0';
|
|
|
|
} else {
|
2016-09-30 16:09:02 +02:00
|
|
|
rv = getnameinfo(addr, addrlen, host.data(), host.size(), service.data(),
|
|
|
|
service.size(), NI_NUMERICHOST | NI_NUMERICSERV);
|
2016-01-19 15:26:04 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
LOG(ERROR) << "getnameinfo() failed: " << gai_strerror(rv);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-06-12 16:39:58 +02:00
|
|
|
|
2015-09-23 12:22:34 +02:00
|
|
|
rv = util::make_socket_nodelay(fd);
|
|
|
|
if (rv == -1) {
|
|
|
|
LOG(WARN) << "Setting option TCP_NODELAY failed: errno=" << errno;
|
|
|
|
}
|
2014-06-26 15:55:22 +02:00
|
|
|
}
|
|
|
|
SSL *ssl = nullptr;
|
2016-03-23 15:56:09 +01:00
|
|
|
if (faddr->tls) {
|
|
|
|
auto ssl_ctx = worker->get_sv_ssl_ctx();
|
|
|
|
|
|
|
|
assert(ssl_ctx);
|
|
|
|
|
2015-08-09 11:33:49 +02:00
|
|
|
ssl = create_ssl(ssl_ctx);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!ssl) {
|
2014-06-26 15:55:22 +02:00
|
|
|
return nullptr;
|
2012-11-18 13:23:13 +01:00
|
|
|
}
|
2015-08-09 11:33:49 +02:00
|
|
|
// Disable TLS session ticket if we don't have working ticket
|
|
|
|
// keys.
|
|
|
|
if (!worker->get_ticket_keys()) {
|
|
|
|
SSL_set_options(ssl, SSL_OP_NO_TICKET);
|
|
|
|
}
|
2012-06-05 18:26:04 +02:00
|
|
|
}
|
2014-06-26 15:55:22 +02:00
|
|
|
|
2016-09-30 16:09:02 +02:00
|
|
|
return new ClientHandler(worker, fd, ssl, StringRef{host.data()},
|
|
|
|
StringRef{service.data()}, addr->sa_family, faddr);
|
2012-06-05 18:26:04 +02:00
|
|
|
}
|
|
|
|
|
2016-03-24 15:16:20 +01:00
|
|
|
bool tls_hostname_match(const StringRef &pattern, const StringRef &hostname) {
|
|
|
|
auto ptWildcard = std::find(std::begin(pattern), std::end(pattern), '*');
|
|
|
|
if (ptWildcard == std::end(pattern)) {
|
|
|
|
return util::strieq(pattern, hostname);
|
2012-11-22 13:46:15 +01:00
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
|
2016-03-24 15:16:20 +01:00
|
|
|
auto ptLeftLabelEnd = std::find(std::begin(pattern), std::end(pattern), '.');
|
2016-01-16 15:54:21 +01:00
|
|
|
auto wildcardEnabled = true;
|
2012-11-22 13:46:15 +01:00
|
|
|
// Do case-insensitive match. At least 2 dots are required to enable
|
|
|
|
// wildcard match. Also wildcard must be in the left-most label.
|
|
|
|
// Don't attempt to match a presented identifier where the wildcard
|
|
|
|
// character is embedded within an A-label.
|
2016-03-24 15:16:20 +01:00
|
|
|
if (ptLeftLabelEnd == std::end(pattern) ||
|
|
|
|
std::find(ptLeftLabelEnd + 1, std::end(pattern), '.') ==
|
|
|
|
std::end(pattern) ||
|
|
|
|
ptLeftLabelEnd < ptWildcard || util::istarts_with_l(pattern, "xn--")) {
|
2012-11-22 13:46:15 +01:00
|
|
|
wildcardEnabled = false;
|
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!wildcardEnabled) {
|
2016-03-24 15:16:20 +01:00
|
|
|
return util::strieq(pattern, hostname);
|
2012-11-22 13:46:15 +01:00
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
|
2016-03-24 15:16:20 +01:00
|
|
|
auto hnLeftLabelEnd =
|
|
|
|
std::find(std::begin(hostname), std::end(hostname), '.');
|
|
|
|
if (hnLeftLabelEnd == std::end(hostname) ||
|
|
|
|
!util::strieq(StringRef{ptLeftLabelEnd, std::end(pattern)},
|
|
|
|
StringRef{hnLeftLabelEnd, std::end(hostname)})) {
|
2012-11-22 13:46:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Perform wildcard match. Here '*' must match at least one
|
|
|
|
// character.
|
2016-03-24 15:16:20 +01:00
|
|
|
if (hnLeftLabelEnd - std::begin(hostname) <
|
|
|
|
ptLeftLabelEnd - std::begin(pattern)) {
|
2012-11-22 13:46:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-24 15:16:20 +01:00
|
|
|
return util::istarts_with(StringRef{std::begin(hostname), hnLeftLabelEnd},
|
|
|
|
StringRef{std::begin(pattern), ptWildcard}) &&
|
|
|
|
util::iends_with(StringRef{std::begin(hostname), hnLeftLabelEnd},
|
|
|
|
StringRef{ptWildcard + 1, ptLeftLabelEnd});
|
2012-11-22 13:46:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2016-03-24 15:16:20 +01:00
|
|
|
// if return value is not empty, StringRef.c_str() must be freed using
|
|
|
|
// OPENSSL_free().
|
|
|
|
StringRef get_common_name(X509 *cert) {
|
2016-01-16 15:54:21 +01:00
|
|
|
auto subjectname = X509_get_subject_name(cert);
|
|
|
|
if (!subjectname) {
|
|
|
|
LOG(WARN) << "Could not get X509 name object from the certificate.";
|
2016-03-24 15:16:20 +01:00
|
|
|
return StringRef{};
|
2016-01-16 15:54:21 +01:00
|
|
|
}
|
|
|
|
int lastpos = -1;
|
|
|
|
for (;;) {
|
|
|
|
lastpos = X509_NAME_get_index_by_NID(subjectname, NID_commonName, lastpos);
|
|
|
|
if (lastpos == -1) {
|
2012-11-22 13:46:15 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
auto entry = X509_NAME_get_entry(subjectname, lastpos);
|
|
|
|
|
2016-03-24 15:16:20 +01:00
|
|
|
unsigned char *p;
|
|
|
|
auto plen = ASN1_STRING_to_UTF8(&p, X509_NAME_ENTRY_get_data(entry));
|
|
|
|
if (plen < 0) {
|
2016-01-16 15:54:21 +01:00
|
|
|
continue;
|
2012-11-22 13:46:15 +01:00
|
|
|
}
|
2016-03-24 15:16:20 +01:00
|
|
|
if (std::find(p, p + plen, '\0') != p + plen) {
|
2016-01-16 15:54:21 +01:00
|
|
|
// Embedded NULL is not permitted.
|
|
|
|
continue;
|
2012-11-22 13:46:15 +01:00
|
|
|
}
|
2016-03-24 15:16:20 +01:00
|
|
|
if (plen == 0) {
|
|
|
|
LOG(WARN) << "X509 name is empty";
|
|
|
|
OPENSSL_free(p);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return StringRef{p, static_cast<size_t>(plen)};
|
2016-01-16 15:54:21 +01:00
|
|
|
}
|
2016-03-24 15:16:20 +01:00
|
|
|
return StringRef{};
|
2016-01-16 15:54:21 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2016-03-24 15:16:20 +01:00
|
|
|
int verify_numeric_hostname(X509 *cert, const StringRef &hostname,
|
2016-01-16 15:54:21 +01:00
|
|
|
const Address *addr) {
|
|
|
|
const void *saddr;
|
|
|
|
switch (addr->su.storage.ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
saddr = &addr->su.in.sin_addr;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
saddr = &addr->su.in6.sin6_addr;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto altnames = static_cast<GENERAL_NAMES *>(
|
|
|
|
X509_get_ext_d2i(cert, NID_subject_alt_name, nullptr, nullptr));
|
|
|
|
if (altnames) {
|
|
|
|
auto altnames_deleter = defer(GENERAL_NAMES_free, altnames);
|
2016-01-17 10:27:25 +01:00
|
|
|
size_t n = sk_GENERAL_NAME_num(altnames);
|
2016-04-26 15:12:51 +02:00
|
|
|
auto ip_found = false;
|
2016-01-16 15:54:21 +01:00
|
|
|
for (size_t i = 0; i < n; ++i) {
|
|
|
|
auto altname = sk_GENERAL_NAME_value(altnames, i);
|
|
|
|
if (altname->type != GEN_IPADD) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ip_addr = altname->d.iPAddress->data;
|
|
|
|
if (!ip_addr) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-17 10:27:25 +01:00
|
|
|
size_t ip_addrlen = altname->d.iPAddress->length;
|
2016-01-16 15:54:21 +01:00
|
|
|
|
2016-04-26 15:12:51 +02:00
|
|
|
ip_found = true;
|
2016-01-16 15:54:21 +01:00
|
|
|
if (addr->len == ip_addrlen && memcmp(saddr, ip_addr, ip_addrlen) == 0) {
|
2012-11-22 13:46:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 15:12:51 +02:00
|
|
|
|
|
|
|
if (ip_found) {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-22 13:46:15 +01:00
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
|
2016-03-24 15:16:20 +01:00
|
|
|
auto cn = get_common_name(cert);
|
|
|
|
if (cn.empty()) {
|
2016-01-16 15:54:21 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cn is not NULL terminated
|
2016-03-24 15:16:20 +01:00
|
|
|
auto rv = util::streq(hostname, cn);
|
|
|
|
OPENSSL_free(const_cast<char *>(cn.c_str()));
|
2016-01-16 15:54:21 +01:00
|
|
|
|
|
|
|
if (rv) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-22 13:46:15 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-01-16 15:54:21 +01:00
|
|
|
namespace {
|
2016-03-24 15:16:20 +01:00
|
|
|
int verify_hostname(X509 *cert, const StringRef &hostname,
|
2016-01-16 15:54:21 +01:00
|
|
|
const Address *addr) {
|
2016-03-24 15:16:20 +01:00
|
|
|
if (util::numeric_host(hostname.c_str())) {
|
|
|
|
return verify_numeric_hostname(cert, hostname, addr);
|
2016-01-16 15:54:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto altnames = static_cast<GENERAL_NAMES *>(
|
2014-11-27 15:39:04 +01:00
|
|
|
X509_get_ext_d2i(cert, NID_subject_alt_name, nullptr, nullptr));
|
|
|
|
if (altnames) {
|
2016-04-26 15:12:51 +02:00
|
|
|
auto dns_found = false;
|
2015-02-06 15:27:15 +01:00
|
|
|
auto altnames_deleter = defer(GENERAL_NAMES_free, altnames);
|
2016-01-17 10:27:25 +01:00
|
|
|
size_t n = sk_GENERAL_NAME_num(altnames);
|
2014-11-27 15:39:04 +01:00
|
|
|
for (size_t i = 0; i < n; ++i) {
|
2016-01-16 15:54:21 +01:00
|
|
|
auto altname = sk_GENERAL_NAME_value(altnames, i);
|
|
|
|
if (altname->type != GEN_DNS) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-26 16:02:51 +02:00
|
|
|
auto name = ASN1_STRING_get0_data(altname->d.ia5);
|
2016-01-16 15:54:21 +01:00
|
|
|
if (!name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto len = ASN1_STRING_length(altname->d.ia5);
|
2016-04-21 15:30:55 +02:00
|
|
|
if (len == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
if (std::find(name, name + len, '\0') != name + len) {
|
|
|
|
// Embedded NULL is not permitted.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-21 15:30:55 +02:00
|
|
|
if (name[len - 1] == '.') {
|
|
|
|
--len;
|
|
|
|
if (len == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 15:12:51 +02:00
|
|
|
dns_found = true;
|
|
|
|
|
2016-03-24 15:16:20 +01:00
|
|
|
if (tls_hostname_match(StringRef{name, static_cast<size_t>(len)},
|
|
|
|
hostname)) {
|
2016-01-16 15:54:21 +01:00
|
|
|
return 0;
|
2012-11-22 13:46:15 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-26 15:12:51 +02:00
|
|
|
|
|
|
|
// RFC 6125, section 6.4.4. says that client MUST not seek a match
|
|
|
|
// for CN if a dns dNSName is found.
|
|
|
|
if (dns_found) {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-22 13:46:15 +01:00
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
|
2016-03-24 15:16:20 +01:00
|
|
|
auto cn = get_common_name(cert);
|
|
|
|
if (cn.empty()) {
|
2016-01-16 15:54:21 +01:00
|
|
|
return -1;
|
2012-11-22 13:46:15 +01:00
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
|
2016-04-21 15:53:07 +02:00
|
|
|
if (cn[cn.size() - 1] == '.') {
|
|
|
|
if (cn.size() == 1) {
|
|
|
|
OPENSSL_free(const_cast<char *>(cn.c_str()));
|
2016-01-16 15:54:21 +01:00
|
|
|
|
2016-04-21 15:53:07 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cn = StringRef{cn.c_str(), cn.size() - 1};
|
2012-11-22 13:46:15 +01:00
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
|
2016-04-21 15:53:07 +02:00
|
|
|
auto rv = tls_hostname_match(cn, hostname);
|
|
|
|
OPENSSL_free(const_cast<char *>(cn.c_str()));
|
|
|
|
|
|
|
|
return rv ? 0 : -1;
|
2013-02-06 15:27:05 +01:00
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
} // namespace
|
2013-02-06 15:27:05 +01:00
|
|
|
|
2016-02-11 14:56:45 +01:00
|
|
|
int check_cert(SSL *ssl, const Address *addr, const StringRef &host) {
|
2013-09-23 11:40:17 +02:00
|
|
|
auto cert = SSL_get_peer_certificate(ssl);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!cert) {
|
2017-01-07 16:24:33 +01:00
|
|
|
// By the protocol definition, TLS server always sends certificate
|
|
|
|
// if it has. If certificate cannot be retrieved, authentication
|
|
|
|
// without certificate is used, such as PSK.
|
|
|
|
return 0;
|
2013-02-06 15:27:05 +01:00
|
|
|
}
|
2015-02-06 15:27:15 +01:00
|
|
|
auto cert_deleter = defer(X509_free, cert);
|
2016-01-18 06:21:09 +01:00
|
|
|
|
2016-03-24 15:16:20 +01:00
|
|
|
if (verify_hostname(cert, host, addr) != 0) {
|
2012-11-22 13:46:15 +01:00
|
|
|
LOG(ERROR) << "Certificate verification failed: hostname does not match";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-04 15:43:41 +01:00
|
|
|
int check_cert(SSL *ssl, const DownstreamAddr *addr, const Address *raddr) {
|
2016-04-29 07:42:18 +02:00
|
|
|
auto hostname =
|
|
|
|
addr->sni.empty() ? StringRef{addr->host} : StringRef{addr->sni};
|
2016-12-04 15:43:41 +01:00
|
|
|
return check_cert(ssl, raddr, hostname);
|
2016-02-11 14:56:45 +01:00
|
|
|
}
|
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
CertLookupTree::CertLookupTree() {}
|
2013-02-06 15:27:05 +01:00
|
|
|
|
2017-02-04 10:59:06 +01:00
|
|
|
ssize_t CertLookupTree::add_cert(const StringRef &hostname, size_t idx) {
|
2016-06-24 17:28:15 +02:00
|
|
|
std::array<uint8_t, NI_MAXHOST> buf;
|
2015-02-02 14:47:12 +01:00
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
// NI_MAXHOST includes terminal NULL byte
|
|
|
|
if (hostname.empty() || hostname.size() + 1 > buf.size()) {
|
2017-02-04 10:59:06 +01:00
|
|
|
return -1;
|
2015-02-02 14:47:12 +01:00
|
|
|
}
|
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
auto wildcard_it = std::find(std::begin(hostname), std::end(hostname), '*');
|
|
|
|
if (wildcard_it != std::end(hostname) &&
|
|
|
|
wildcard_it + 1 != std::end(hostname)) {
|
|
|
|
auto wildcard_prefix = StringRef{std::begin(hostname), wildcard_it};
|
|
|
|
auto wildcard_suffix = StringRef{wildcard_it + 1, std::end(hostname)};
|
|
|
|
|
|
|
|
auto rev_suffix = StringRef{std::begin(buf),
|
|
|
|
std::reverse_copy(std::begin(wildcard_suffix),
|
|
|
|
std::end(wildcard_suffix),
|
|
|
|
std::begin(buf))};
|
|
|
|
|
|
|
|
WildcardPattern *wpat;
|
|
|
|
|
2017-02-04 10:59:06 +01:00
|
|
|
if (wildcard_patterns_.size() !=
|
|
|
|
rev_wildcard_router_.add_route(rev_suffix, wildcard_patterns_.size())) {
|
2016-06-24 17:28:15 +02:00
|
|
|
auto wcidx = rev_wildcard_router_.match(rev_suffix);
|
|
|
|
|
|
|
|
assert(wcidx != -1);
|
|
|
|
|
|
|
|
wpat = &wildcard_patterns_[wcidx];
|
|
|
|
} else {
|
|
|
|
wildcard_patterns_.emplace_back();
|
|
|
|
wpat = &wildcard_patterns_.back();
|
2013-02-06 15:27:05 +01:00
|
|
|
}
|
2015-02-02 14:47:12 +01:00
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
auto rev_prefix = StringRef{std::begin(buf),
|
|
|
|
std::reverse_copy(std::begin(wildcard_prefix),
|
|
|
|
std::end(wildcard_prefix),
|
|
|
|
std::begin(buf))};
|
|
|
|
|
2017-02-04 10:59:06 +01:00
|
|
|
for (auto &p : wpat->rev_prefix) {
|
|
|
|
if (p.prefix == rev_prefix) {
|
|
|
|
return p.idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
wpat->rev_prefix.emplace_back(rev_prefix, idx);
|
|
|
|
|
2017-02-04 10:59:06 +01:00
|
|
|
return idx;
|
2015-02-02 14:47:12 +01:00
|
|
|
}
|
|
|
|
|
2017-02-04 10:59:06 +01:00
|
|
|
return router_.add_route(hostname, idx);
|
2016-06-24 17:28:15 +02:00
|
|
|
}
|
2015-02-02 14:47:12 +01:00
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
ssize_t CertLookupTree::lookup(const StringRef &hostname) {
|
|
|
|
std::array<uint8_t, NI_MAXHOST> buf;
|
2015-02-02 14:47:12 +01:00
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
// NI_MAXHOST includes terminal NULL byte
|
|
|
|
if (hostname.empty() || hostname.size() + 1 > buf.size()) {
|
|
|
|
return -1;
|
2013-02-06 15:27:05 +01:00
|
|
|
}
|
2015-02-02 14:47:12 +01:00
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
// Always prefer exact match
|
|
|
|
auto idx = router_.match(hostname);
|
|
|
|
if (idx != -1) {
|
|
|
|
return idx;
|
|
|
|
}
|
2013-02-06 15:27:05 +01:00
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
if (wildcard_patterns_.empty()) {
|
|
|
|
return -1;
|
2013-02-06 15:27:05 +01:00
|
|
|
}
|
2016-03-24 15:32:57 +01:00
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
ssize_t best_idx = -1;
|
|
|
|
size_t best_prefixlen = 0;
|
|
|
|
const RNode *last_node = nullptr;
|
2016-03-24 15:32:57 +01:00
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
auto rev_host = StringRef{
|
|
|
|
std::begin(buf), std::reverse_copy(std::begin(hostname),
|
|
|
|
std::end(hostname), std::begin(buf))};
|
2013-02-06 15:27:05 +01:00
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
for (;;) {
|
|
|
|
size_t nread = 0;
|
2015-02-02 14:47:12 +01:00
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
auto wcidx =
|
|
|
|
rev_wildcard_router_.match_prefix(&nread, &last_node, rev_host);
|
|
|
|
if (wcidx == -1) {
|
|
|
|
return best_idx;
|
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
// '*' must match at least one byte
|
|
|
|
if (nread == rev_host.size()) {
|
|
|
|
return best_idx;
|
2013-09-23 11:40:17 +02:00
|
|
|
}
|
2016-06-24 17:28:15 +02:00
|
|
|
|
|
|
|
rev_host = StringRef{std::begin(rev_host) + nread, std::end(rev_host)};
|
|
|
|
|
|
|
|
auto rev_prefix = StringRef{std::begin(rev_host) + 1, std::end(rev_host)};
|
|
|
|
|
|
|
|
auto &wpat = wildcard_patterns_[wcidx];
|
|
|
|
for (auto &wprefix : wpat.rev_prefix) {
|
|
|
|
if (!util::ends_with(rev_prefix, wprefix.prefix)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto prefixlen =
|
|
|
|
wprefix.prefix.size() +
|
|
|
|
(reinterpret_cast<const uint8_t *>(&rev_host[0]) - &buf[0]);
|
|
|
|
|
|
|
|
// Breaking a tie with longer suffix
|
|
|
|
if (prefixlen < best_prefixlen) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
best_idx = wprefix.idx;
|
|
|
|
best_prefixlen = prefixlen;
|
2013-02-06 15:27:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
void CertLookupTree::dump() const {
|
|
|
|
std::cerr << "exact:" << std::endl;
|
|
|
|
router_.dump();
|
|
|
|
std::cerr << "wildcard suffix (reversed):" << std::endl;
|
|
|
|
rev_wildcard_router_.dump();
|
2013-02-06 15:27:05 +01:00
|
|
|
}
|
|
|
|
|
2017-02-04 10:59:06 +01:00
|
|
|
int cert_lookup_tree_add_ssl_ctx(
|
|
|
|
CertLookupTree *lt, std::vector<std::vector<SSL_CTX *>> &indexed_ssl_ctx,
|
|
|
|
SSL_CTX *ssl_ctx) {
|
2016-06-24 17:28:15 +02:00
|
|
|
std::array<uint8_t, NI_MAXHOST> buf;
|
2016-01-16 15:54:21 +01:00
|
|
|
|
2018-04-14 11:01:50 +02:00
|
|
|
#if LIBRESSL_2_7_API || \
|
|
|
|
(!LIBRESSL_IN_USE && OPENSSL_VERSION_NUMBER >= 0x10002000L)
|
2017-02-04 10:59:06 +01:00
|
|
|
auto cert = SSL_CTX_get0_certificate(ssl_ctx);
|
2018-04-14 11:01:50 +02:00
|
|
|
#else // !LIBRESSL_2_7_API && OPENSSL_VERSION_NUMBER < 0x10002000L
|
2017-02-04 10:59:06 +01:00
|
|
|
auto tls_ctx_data =
|
|
|
|
static_cast<TLSContextData *>(SSL_CTX_get_app_data(ssl_ctx));
|
|
|
|
auto cert = load_certificate(tls_ctx_data->cert_file);
|
|
|
|
auto cert_deleter = defer(X509_free, cert);
|
2018-04-14 11:01:50 +02:00
|
|
|
#endif // !LIBRESSL_2_7_API && OPENSSL_VERSION_NUMBER < 0x10002000L
|
2017-02-04 10:59:06 +01:00
|
|
|
|
2016-01-16 15:54:21 +01:00
|
|
|
auto altnames = static_cast<GENERAL_NAMES *>(
|
|
|
|
X509_get_ext_d2i(cert, NID_subject_alt_name, nullptr, nullptr));
|
|
|
|
if (altnames) {
|
|
|
|
auto altnames_deleter = defer(GENERAL_NAMES_free, altnames);
|
2016-01-17 10:27:25 +01:00
|
|
|
size_t n = sk_GENERAL_NAME_num(altnames);
|
2016-04-26 15:12:51 +02:00
|
|
|
auto dns_found = false;
|
2016-01-16 15:54:21 +01:00
|
|
|
for (size_t i = 0; i < n; ++i) {
|
|
|
|
auto altname = sk_GENERAL_NAME_value(altnames, i);
|
|
|
|
if (altname->type != GEN_DNS) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-26 16:02:51 +02:00
|
|
|
auto name = ASN1_STRING_get0_data(altname->d.ia5);
|
2016-01-16 15:54:21 +01:00
|
|
|
if (!name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto len = ASN1_STRING_length(altname->d.ia5);
|
2016-04-21 15:30:55 +02:00
|
|
|
if (len == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
if (std::find(name, name + len, '\0') != name + len) {
|
|
|
|
// Embedded NULL is not permitted.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-21 15:30:55 +02:00
|
|
|
if (name[len - 1] == '.') {
|
|
|
|
--len;
|
|
|
|
if (len == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 15:12:51 +02:00
|
|
|
dns_found = true;
|
2016-06-24 17:28:15 +02:00
|
|
|
|
|
|
|
if (static_cast<size_t>(len) + 1 > buf.size()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto end_buf = std::copy_n(name, len, std::begin(buf));
|
|
|
|
util::inp_strlower(std::begin(buf), end_buf);
|
|
|
|
|
2017-05-20 17:19:33 +02:00
|
|
|
auto idx = lt->add_cert(StringRef{std::begin(buf), end_buf},
|
|
|
|
indexed_ssl_ctx.size());
|
|
|
|
if (idx == -1) {
|
2017-02-04 10:59:06 +01:00
|
|
|
continue;
|
|
|
|
}
|
2017-05-20 17:19:33 +02:00
|
|
|
|
2017-05-22 15:10:55 +02:00
|
|
|
if (static_cast<size_t>(idx) < indexed_ssl_ctx.size()) {
|
2017-02-04 10:59:06 +01:00
|
|
|
indexed_ssl_ctx[idx].push_back(ssl_ctx);
|
|
|
|
} else {
|
2017-05-22 15:10:55 +02:00
|
|
|
assert(static_cast<size_t>(idx) == indexed_ssl_ctx.size());
|
2017-02-04 10:59:06 +01:00
|
|
|
indexed_ssl_ctx.emplace_back(std::vector<SSL_CTX *>{ssl_ctx});
|
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
}
|
2016-04-26 15:12:51 +02:00
|
|
|
|
|
|
|
// Don't bother CN if we have dNSName.
|
|
|
|
if (dns_found) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-16 15:54:21 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 15:16:20 +01:00
|
|
|
auto cn = get_common_name(cert);
|
|
|
|
if (cn.empty()) {
|
2016-01-16 15:54:21 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-21 15:30:55 +02:00
|
|
|
if (cn[cn.size() - 1] == '.') {
|
|
|
|
if (cn.size() == 1) {
|
|
|
|
OPENSSL_free(const_cast<char *>(cn.c_str()));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cn = StringRef{cn.c_str(), cn.size() - 1};
|
|
|
|
}
|
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
auto end_buf = std::copy(std::begin(cn), std::end(cn), std::begin(buf));
|
2016-01-16 15:54:21 +01:00
|
|
|
|
2016-03-24 15:16:20 +01:00
|
|
|
OPENSSL_free(const_cast<char *>(cn.c_str()));
|
2016-01-16 15:54:21 +01:00
|
|
|
|
2016-06-24 17:28:15 +02:00
|
|
|
util::inp_strlower(std::begin(buf), end_buf);
|
|
|
|
|
2017-05-20 17:19:33 +02:00
|
|
|
auto idx =
|
|
|
|
lt->add_cert(StringRef{std::begin(buf), end_buf}, indexed_ssl_ctx.size());
|
|
|
|
if (idx == -1) {
|
2017-02-04 10:59:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2017-05-20 17:19:33 +02:00
|
|
|
|
2017-05-22 15:10:55 +02:00
|
|
|
if (static_cast<size_t>(idx) < indexed_ssl_ctx.size()) {
|
2017-02-04 10:59:06 +01:00
|
|
|
indexed_ssl_ctx[idx].push_back(ssl_ctx);
|
|
|
|
} else {
|
2017-05-22 15:10:55 +02:00
|
|
|
assert(static_cast<size_t>(idx) == indexed_ssl_ctx.size());
|
2017-02-04 10:59:06 +01:00
|
|
|
indexed_ssl_ctx.emplace_back(std::vector<SSL_CTX *>{ssl_ctx});
|
|
|
|
}
|
2016-06-24 17:28:15 +02:00
|
|
|
|
2013-02-06 15:27:05 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-02 15:19:31 +02:00
|
|
|
bool in_proto_list(const std::vector<StringRef> &protos,
|
2016-03-24 16:02:07 +01:00
|
|
|
const StringRef &needle) {
|
2015-07-20 14:37:23 +02:00
|
|
|
for (auto &proto : protos) {
|
2016-10-02 15:19:31 +02:00
|
|
|
if (util::streq(proto, needle)) {
|
2014-01-01 16:53:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-16 14:46:22 +01:00
|
|
|
bool upstream_tls_enabled(const ConnectionConfig &connconf) {
|
|
|
|
const auto &faddrs = connconf.listener.addrs;
|
2016-03-23 15:56:09 +01:00
|
|
|
return std::any_of(std::begin(faddrs), std::end(faddrs),
|
|
|
|
[](const UpstreamAddr &faddr) { return faddr.tls; });
|
|
|
|
}
|
|
|
|
|
2016-06-25 16:35:12 +02:00
|
|
|
X509 *load_certificate(const char *filename) {
|
|
|
|
auto bio = BIO_new(BIO_s_file());
|
|
|
|
if (!bio) {
|
|
|
|
fprintf(stderr, "BIO_new() failed\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto bio_deleter = defer(BIO_vfree, bio);
|
|
|
|
if (!BIO_read_filename(bio, filename)) {
|
|
|
|
fprintf(stderr, "Could not read certificate file '%s'\n", filename);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto cert = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr);
|
|
|
|
if (!cert) {
|
|
|
|
fprintf(stderr, "Could not read X509 structure from file '%s'\n", filename);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cert;
|
|
|
|
}
|
|
|
|
|
2017-02-04 10:59:06 +01:00
|
|
|
SSL_CTX *
|
|
|
|
setup_server_ssl_context(std::vector<SSL_CTX *> &all_ssl_ctx,
|
|
|
|
std::vector<std::vector<SSL_CTX *>> &indexed_ssl_ctx,
|
|
|
|
CertLookupTree *cert_tree
|
2015-09-23 12:45:53 +02:00
|
|
|
#ifdef HAVE_NEVERBLEED
|
2017-02-04 10:59:06 +01:00
|
|
|
,
|
|
|
|
neverbleed_t *nb
|
2015-09-23 12:45:53 +02:00
|
|
|
#endif // HAVE_NEVERBLEED
|
2017-11-23 06:19:12 +01:00
|
|
|
) {
|
2017-02-16 14:46:22 +01:00
|
|
|
auto config = get_config();
|
|
|
|
|
|
|
|
if (!upstream_tls_enabled(config->conn)) {
|
2015-01-12 16:18:27 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-16 14:46:22 +01:00
|
|
|
auto &tlsconf = config->tls;
|
2016-01-18 06:21:09 +01:00
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
auto ssl_ctx = create_ssl_context(tlsconf.private_key_file.c_str(),
|
|
|
|
tlsconf.cert_file.c_str(), tlsconf.sct_data
|
2015-09-23 12:45:53 +02:00
|
|
|
#ifdef HAVE_NEVERBLEED
|
2017-04-01 08:07:32 +02:00
|
|
|
,
|
|
|
|
nb
|
2015-09-23 12:45:53 +02:00
|
|
|
#endif // HAVE_NEVERBLEED
|
2017-11-23 06:19:12 +01:00
|
|
|
);
|
2015-01-12 16:18:27 +01:00
|
|
|
|
2015-03-30 16:20:40 +02:00
|
|
|
all_ssl_ctx.push_back(ssl_ctx);
|
|
|
|
|
2017-04-27 15:25:58 +02:00
|
|
|
assert(cert_tree);
|
2015-01-12 16:18:27 +01:00
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
if (cert_lookup_tree_add_ssl_ctx(cert_tree, indexed_ssl_ctx, ssl_ctx) == -1) {
|
2016-06-24 17:28:15 +02:00
|
|
|
LOG(FATAL) << "Failed to add default certificate.";
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
|
2016-10-08 12:03:21 +02:00
|
|
|
for (auto &c : tlsconf.subcerts) {
|
2017-04-01 08:07:32 +02:00
|
|
|
auto ssl_ctx = create_ssl_context(c.private_key_file.c_str(),
|
|
|
|
c.cert_file.c_str(), c.sct_data
|
2015-09-23 12:45:53 +02:00
|
|
|
#ifdef HAVE_NEVERBLEED
|
2017-04-01 08:07:32 +02:00
|
|
|
,
|
|
|
|
nb
|
2015-09-23 12:45:53 +02:00
|
|
|
#endif // HAVE_NEVERBLEED
|
2017-11-23 06:19:12 +01:00
|
|
|
);
|
2015-03-30 16:20:40 +02:00
|
|
|
all_ssl_ctx.push_back(ssl_ctx);
|
2016-06-25 16:35:12 +02:00
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
if (cert_lookup_tree_add_ssl_ctx(cert_tree, indexed_ssl_ctx, ssl_ctx) ==
|
|
|
|
-1) {
|
2015-01-12 16:18:27 +01:00
|
|
|
LOG(FATAL) << "Failed to add sub certificate.";
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssl_ctx;
|
|
|
|
}
|
|
|
|
|
2016-02-11 10:34:31 +01:00
|
|
|
SSL_CTX *setup_downstream_client_ssl_context(
|
2015-09-26 14:28:16 +02:00
|
|
|
#ifdef HAVE_NEVERBLEED
|
|
|
|
neverbleed_t *nb
|
|
|
|
#endif // HAVE_NEVERBLEED
|
2017-11-23 06:19:12 +01:00
|
|
|
) {
|
2016-02-11 10:34:31 +01:00
|
|
|
auto &tlsconf = get_config()->tls;
|
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
return create_ssl_client_context(
|
2015-09-26 14:28:16 +02:00
|
|
|
#ifdef HAVE_NEVERBLEED
|
2016-02-11 10:34:31 +01:00
|
|
|
nb,
|
2015-09-26 14:28:16 +02:00
|
|
|
#endif // HAVE_NEVERBLEED
|
2016-10-02 14:22:02 +02:00
|
|
|
tlsconf.cacert, tlsconf.client.cert_file, tlsconf.client.private_key_file,
|
|
|
|
select_next_proto_cb);
|
2016-02-28 08:56:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup_downstream_http2_alpn(SSL *ssl) {
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
// ALPN advertisement
|
2016-02-28 17:16:45 +01:00
|
|
|
auto alpn = util::get_default_alpn();
|
2016-02-28 08:56:14 +01:00
|
|
|
SSL_set_alpn_protos(ssl, alpn.data(), alpn.size());
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup_downstream_http1_alpn(SSL *ssl) {
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
// ALPN advertisement
|
2016-03-24 17:41:06 +01:00
|
|
|
SSL_set_alpn_protos(ssl, NGHTTP2_H1_1_ALPN.byte(), NGHTTP2_H1_1_ALPN.size());
|
2016-02-28 08:56:14 +01:00
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2015-01-12 16:18:27 +01:00
|
|
|
}
|
|
|
|
|
2016-06-25 16:47:22 +02:00
|
|
|
std::unique_ptr<CertLookupTree> create_cert_lookup_tree() {
|
2017-02-16 14:46:22 +01:00
|
|
|
auto config = get_config();
|
2017-04-27 15:25:58 +02:00
|
|
|
if (!upstream_tls_enabled(config->conn)) {
|
2015-02-11 11:18:41 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-15 16:02:44 +02:00
|
|
|
return std::make_unique<CertLookupTree>();
|
2015-02-11 11:18:41 +01:00
|
|
|
}
|
|
|
|
|
2016-02-21 08:35:43 +01:00
|
|
|
namespace {
|
|
|
|
std::vector<uint8_t> serialize_ssl_session(SSL_SESSION *session) {
|
|
|
|
auto len = i2d_SSL_SESSION(session, nullptr);
|
|
|
|
auto buf = std::vector<uint8_t>(len);
|
|
|
|
auto p = buf.data();
|
|
|
|
i2d_SSL_SESSION(session, &p);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2017-03-28 14:06:28 +02:00
|
|
|
void try_cache_tls_session(TLSSessionCache *cache, SSL_SESSION *session,
|
|
|
|
ev_tstamp t) {
|
|
|
|
if (cache->last_updated + 1_min > t) {
|
2016-02-21 08:35:43 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2017-03-28 14:06:28 +02:00
|
|
|
LOG(INFO) << "Client session cache entry is still fresh.";
|
2016-02-21 08:35:43 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2017-03-28 14:06:28 +02:00
|
|
|
LOG(INFO) << "Update client cache entry "
|
2018-08-31 14:55:33 +02:00
|
|
|
<< "timestamp = " << t;
|
2016-02-21 08:35:43 +01:00
|
|
|
}
|
|
|
|
|
2017-03-28 14:06:28 +02:00
|
|
|
cache->session_data = serialize_ssl_session(session);
|
|
|
|
cache->last_updated = t;
|
2016-02-21 08:35:43 +01:00
|
|
|
}
|
|
|
|
|
2016-04-28 15:25:55 +02:00
|
|
|
SSL_SESSION *reuse_tls_session(const TLSSessionCache &cache) {
|
2016-02-21 08:35:43 +01:00
|
|
|
if (cache.session_data.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto p = cache.session_data.data();
|
|
|
|
return d2i_SSL_SESSION(nullptr, &p, cache.session_data.size());
|
|
|
|
}
|
|
|
|
|
2017-02-13 15:34:45 +01:00
|
|
|
int proto_version_from_string(const StringRef &v) {
|
2017-02-14 16:40:43 +01:00
|
|
|
#ifdef TLS1_3_VERSION
|
|
|
|
if (util::strieq_l("TLSv1.3", v)) {
|
|
|
|
return TLS1_3_VERSION;
|
|
|
|
}
|
|
|
|
#endif // TLS1_3_VERSION
|
2017-02-13 15:34:45 +01:00
|
|
|
if (util::strieq_l("TLSv1.2", v)) {
|
|
|
|
return TLS1_2_VERSION;
|
|
|
|
}
|
|
|
|
if (util::strieq_l("TLSv1.1", v)) {
|
|
|
|
return TLS1_1_VERSION;
|
|
|
|
}
|
|
|
|
if (util::strieq_l("TLSv1.0", v)) {
|
|
|
|
return TLS1_VERSION;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-24 15:20:08 +02:00
|
|
|
int verify_ocsp_response(SSL_CTX *ssl_ctx, const uint8_t *ocsp_resp,
|
|
|
|
size_t ocsp_resplen) {
|
2017-08-24 17:54:42 +02:00
|
|
|
|
2018-04-14 11:31:32 +02:00
|
|
|
#if !defined(OPENSSL_NO_OCSP) && !LIBRESSL_IN_USE && \
|
2017-08-24 17:54:42 +02:00
|
|
|
OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2017-05-24 15:20:08 +02:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
STACK_OF(X509) * chain_certs;
|
|
|
|
SSL_CTX_get0_chain_certs(ssl_ctx, &chain_certs);
|
|
|
|
|
|
|
|
auto resp = d2i_OCSP_RESPONSE(nullptr, &ocsp_resp, ocsp_resplen);
|
|
|
|
if (resp == nullptr) {
|
|
|
|
LOG(ERROR) << "d2i_OCSP_RESPONSE failed";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
auto resp_deleter = defer(OCSP_RESPONSE_free, resp);
|
|
|
|
|
2018-09-16 15:19:27 +02:00
|
|
|
if (OCSP_response_status(resp) != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
|
|
|
|
LOG(ERROR) << "OCSP response status is not successful";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-24 15:20:08 +02:00
|
|
|
ERR_clear_error();
|
|
|
|
|
|
|
|
auto bs = OCSP_response_get1_basic(resp);
|
|
|
|
if (bs == nullptr) {
|
|
|
|
LOG(ERROR) << "OCSP_response_get1_basic failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
auto bs_deleter = defer(OCSP_BASICRESP_free, bs);
|
|
|
|
|
2017-06-13 16:00:26 +02:00
|
|
|
auto store = SSL_CTX_get_cert_store(ssl_ctx);
|
2017-05-30 16:52:38 +02:00
|
|
|
|
2017-05-24 15:20:08 +02:00
|
|
|
ERR_clear_error();
|
|
|
|
|
2017-06-13 16:00:26 +02:00
|
|
|
rv = OCSP_basic_verify(bs, chain_certs, store, 0);
|
2017-05-24 15:20:08 +02:00
|
|
|
|
|
|
|
if (rv != 1) {
|
|
|
|
LOG(ERROR) << "OCSP_basic_verify failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sresp = OCSP_resp_get0(bs, 0);
|
|
|
|
if (sresp == nullptr) {
|
|
|
|
LOG(ERROR) << "OCSP response verification failed: no single response";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-06-09 09:21:30 +02:00
|
|
|
# if OPENSSL_1_1_API
|
2017-05-24 15:20:08 +02:00
|
|
|
auto certid = OCSP_SINGLERESP_get0_id(sresp);
|
2018-06-09 09:21:30 +02:00
|
|
|
# else // !OPENSSL_1_1_API
|
2017-05-24 15:20:08 +02:00
|
|
|
auto certid = sresp->certId;
|
2018-06-09 09:21:30 +02:00
|
|
|
# endif // !OPENSSL_1_1_API
|
2017-05-24 15:20:08 +02:00
|
|
|
assert(certid != nullptr);
|
|
|
|
|
|
|
|
ASN1_INTEGER *serial;
|
|
|
|
rv = OCSP_id_get0_info(nullptr, nullptr, nullptr, &serial,
|
|
|
|
const_cast<OCSP_CERTID *>(certid));
|
|
|
|
if (rv != 1) {
|
|
|
|
LOG(ERROR) << "OCSP_id_get0_info failed";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (serial == nullptr) {
|
|
|
|
LOG(ERROR) << "OCSP response does not contain serial number";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto cert = SSL_CTX_get0_certificate(ssl_ctx);
|
|
|
|
auto cert_serial = X509_get_serialNumber(cert);
|
|
|
|
|
|
|
|
if (ASN1_INTEGER_cmp(cert_serial, serial)) {
|
|
|
|
LOG(ERROR) << "OCSP verification serial numbers do not match";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "OCSP verification succeeded";
|
|
|
|
}
|
2018-04-14 11:31:32 +02:00
|
|
|
#endif // !defined(OPENSSL_NO_OCSP) && !LIBRESSL_IN_USE
|
2017-08-24 17:54:42 +02:00
|
|
|
// && OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2017-05-24 15:20:08 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-31 13:28:16 +01:00
|
|
|
ssize_t get_x509_fingerprint(uint8_t *dst, size_t dstlen, const X509 *x,
|
|
|
|
const EVP_MD *md) {
|
2017-10-28 17:25:20 +02:00
|
|
|
unsigned int len = dstlen;
|
2017-10-31 13:28:16 +01:00
|
|
|
if (X509_digest(x, md, dst, &len) != 1) {
|
2017-10-28 17:25:20 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2017-11-15 15:41:47 +01:00
|
|
|
namespace {
|
|
|
|
StringRef get_x509_name(BlockAllocator &balloc, X509_NAME *nm) {
|
2017-10-28 17:25:20 +02:00
|
|
|
auto b = BIO_new(BIO_s_mem());
|
|
|
|
if (!b) {
|
|
|
|
return StringRef{};
|
|
|
|
}
|
|
|
|
|
2018-02-03 10:21:42 +01:00
|
|
|
auto b_deleter = defer(BIO_free, b);
|
|
|
|
|
2017-10-28 17:25:20 +02:00
|
|
|
// Not documented, but it seems that X509_NAME_print_ex returns the
|
|
|
|
// number of bytes written into b.
|
|
|
|
auto slen = X509_NAME_print_ex(b, nm, 0, XN_FLAG_RFC2253);
|
|
|
|
if (slen <= 0) {
|
|
|
|
return StringRef{};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto iov = make_byte_ref(balloc, slen + 1);
|
|
|
|
BIO_read(b, iov.base, slen);
|
|
|
|
iov.base[slen] = '\0';
|
|
|
|
return StringRef{iov.base, static_cast<size_t>(slen)};
|
|
|
|
}
|
2017-11-15 15:41:47 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
StringRef get_x509_subject_name(BlockAllocator &balloc, X509 *x) {
|
|
|
|
return get_x509_name(balloc, X509_get_subject_name(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef get_x509_issuer_name(BlockAllocator &balloc, X509 *x) {
|
|
|
|
return get_x509_name(balloc, X509_get_issuer_name(x));
|
|
|
|
}
|
2017-10-28 17:25:20 +02:00
|
|
|
|
2017-11-16 16:02:33 +01:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
2018-06-09 09:21:30 +02:00
|
|
|
# define bswap64(N) (N)
|
2017-11-16 16:02:33 +01:00
|
|
|
#else /* !WORDS_BIGENDIAN */
|
2018-06-09 09:21:30 +02:00
|
|
|
# define bswap64(N) \
|
|
|
|
((uint64_t)(ntohl((uint32_t)(N))) << 32 | ntohl((uint32_t)((N) >> 32)))
|
2017-11-16 16:02:33 +01:00
|
|
|
#endif /* !WORDS_BIGENDIAN */
|
|
|
|
|
2017-11-16 13:11:38 +01:00
|
|
|
StringRef get_x509_serial(BlockAllocator &balloc, X509 *x) {
|
|
|
|
#if OPENSSL_1_1_API
|
|
|
|
auto sn = X509_get0_serialNumber(x);
|
|
|
|
uint64_t r;
|
|
|
|
if (ASN1_INTEGER_get_uint64(&r, sn) != 1) {
|
|
|
|
return StringRef{};
|
|
|
|
}
|
|
|
|
|
2017-11-16 16:02:33 +01:00
|
|
|
r = bswap64(r);
|
|
|
|
return util::format_hex(
|
|
|
|
balloc, StringRef{reinterpret_cast<uint8_t *>(&r), sizeof(r)});
|
2017-11-16 13:11:38 +01:00
|
|
|
#else // !OPENSSL_1_1_API
|
|
|
|
auto sn = X509_get_serialNumber(x);
|
|
|
|
auto bn = BN_new();
|
|
|
|
auto bn_d = defer(BN_free, bn);
|
|
|
|
if (!ASN1_INTEGER_to_BN(sn, bn)) {
|
|
|
|
return StringRef{};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::array<uint8_t, 8> b;
|
|
|
|
auto n = BN_bn2bin(bn, b.data());
|
|
|
|
assert(n == b.size());
|
|
|
|
|
2017-11-16 16:02:33 +01:00
|
|
|
return util::format_hex(balloc, StringRef{std::begin(b), std::end(b)});
|
2017-11-16 13:11:38 +01:00
|
|
|
#endif // !OPENSSL_1_1_API
|
|
|
|
}
|
|
|
|
|
2018-02-03 09:58:49 +01:00
|
|
|
namespace {
|
|
|
|
// Performs conversion from |at| to time_t. The result is stored in
|
|
|
|
// |t|. This function returns 0 if it succeeds, or -1.
|
|
|
|
int time_t_from_asn1_time(time_t &t, const ASN1_TIME *at) {
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
#if OPENSSL_1_1_1_API
|
|
|
|
struct tm tm;
|
|
|
|
rv = ASN1_TIME_to_tm(at, &tm);
|
|
|
|
if (rv != 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = nghttp2_timegm(&tm);
|
|
|
|
#else // !OPENSSL_1_1_1_API
|
|
|
|
auto b = BIO_new(BIO_s_mem());
|
|
|
|
if (!b) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto bio_deleter = defer(BIO_free, b);
|
|
|
|
|
|
|
|
rv = ASN1_TIME_print(b, at);
|
|
|
|
if (rv != 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *s;
|
|
|
|
auto slen = BIO_get_mem_data(b, &s);
|
|
|
|
auto tt = util::parse_openssl_asn1_time_print(
|
|
|
|
StringRef{s, static_cast<size_t>(slen)});
|
|
|
|
if (tt == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = tt;
|
|
|
|
#endif // !OPENSSL_1_1_1_API
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int get_x509_not_before(time_t &t, X509 *x) {
|
|
|
|
#if OPENSSL_1_1_API
|
|
|
|
auto at = X509_get0_notBefore(x);
|
|
|
|
#else // !OPENSSL_1_1_API
|
|
|
|
auto at = X509_get_notBefore(x);
|
|
|
|
#endif // !OPENSSL_1_1_API
|
|
|
|
if (!at) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return time_t_from_asn1_time(t, at);
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_x509_not_after(time_t &t, X509 *x) {
|
|
|
|
#if OPENSSL_1_1_API
|
|
|
|
auto at = X509_get0_notAfter(x);
|
|
|
|
#else // !OPENSSL_1_1_API
|
|
|
|
auto at = X509_get_notAfter(x);
|
|
|
|
#endif // !OPENSSL_1_1_API
|
|
|
|
if (!at) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return time_t_from_asn1_time(t, at);
|
|
|
|
}
|
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
} // namespace tls
|
2012-06-05 18:26:04 +02:00
|
|
|
|
|
|
|
} // namespace shrpx
|