2014-06-20 20:00:00 +01:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include <openssl/base.h>
|
|
|
|
|
2015-11-18 03:32:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2016-12-07 15:31:16 +00:00
|
|
|
#if !defined(OPENSSL_WINDOWS)
|
|
|
|
#include <sys/select.h>
|
|
|
|
#else
|
2017-01-10 20:20:19 +00:00
|
|
|
OPENSSL_MSVC_PRAGMA(warning(push, 3))
|
2016-12-07 15:31:16 +00:00
|
|
|
#include <winsock2.h>
|
2017-01-10 20:20:19 +00:00
|
|
|
OPENSSL_MSVC_PRAGMA(warning(pop))
|
2016-12-07 15:31:16 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/err.h>
|
2015-03-23 23:01:33 +00:00
|
|
|
#include <openssl/pem.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
#include "../crypto/internal.h"
|
2014-06-20 20:00:00 +01:00
|
|
|
#include "internal.h"
|
2014-12-11 00:09:52 +00:00
|
|
|
#include "transport_common.h"
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
static const struct argument kArguments[] = {
|
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-connect", kRequiredArgument,
|
|
|
|
"The hostname and port of the server to connect to, e.g. foo.com:443",
|
2014-06-20 20:00:00 +01:00
|
|
|
},
|
2014-10-28 22:45:39 +00:00
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-cipher", kOptionalArgument,
|
|
|
|
"An OpenSSL-style cipher suite string that configures the offered "
|
|
|
|
"ciphers",
|
2014-10-28 22:45:39 +00:00
|
|
|
},
|
2017-04-14 10:59:34 +01:00
|
|
|
{
|
|
|
|
"-curves", kOptionalArgument,
|
|
|
|
"An OpenSSL-style ECDH curves list that configures the offered curves",
|
|
|
|
},
|
2014-06-20 20:00:00 +01:00
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-max-version", kOptionalArgument,
|
|
|
|
"The maximum acceptable protocol version",
|
2015-03-23 23:01:33 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-min-version", kOptionalArgument,
|
|
|
|
"The minimum acceptable protocol version",
|
2015-03-23 23:01:33 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-server-name", kOptionalArgument, "The server name to advertise",
|
2015-03-23 23:01:33 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-select-next-proto", kOptionalArgument,
|
|
|
|
"An NPN protocol to select if the server supports NPN",
|
2015-03-23 23:01:33 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-alpn-protos", kOptionalArgument,
|
|
|
|
"A comma-separated list of ALPN protocols to advertise",
|
2015-03-23 23:01:33 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-fallback-scsv", kBooleanArgument, "Enable FALLBACK_SCSV",
|
2015-03-23 23:01:33 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-ocsp-stapling", kBooleanArgument,
|
|
|
|
"Advertise support for OCSP stabling",
|
2015-03-23 23:01:33 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-signed-certificate-timestamps", kBooleanArgument,
|
|
|
|
"Advertise support for signed certificate timestamps",
|
2015-03-23 23:01:33 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-channel-id-key", kOptionalArgument,
|
|
|
|
"The key to use for signing a channel ID",
|
2015-03-23 23:01:33 +00:00
|
|
|
},
|
2015-06-04 20:26:04 +01:00
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-false-start", kBooleanArgument, "Enable False Start",
|
2015-06-04 20:26:04 +01:00
|
|
|
},
|
2017-04-05 19:52:09 +01:00
|
|
|
{
|
|
|
|
"-session-in", kOptionalArgument,
|
|
|
|
"A file containing a session to resume.",
|
2015-08-28 20:08:34 +01:00
|
|
|
},
|
2017-04-05 19:52:09 +01:00
|
|
|
{
|
|
|
|
"-session-out", kOptionalArgument,
|
|
|
|
"A file to write the negotiated session to.",
|
2015-08-28 20:08:34 +01:00
|
|
|
},
|
2015-12-03 00:34:58 +00:00
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-key", kOptionalArgument,
|
2017-04-09 14:52:47 +01:00
|
|
|
"PEM-encoded file containing the private key.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"-cert", kOptionalArgument,
|
|
|
|
"PEM-encoded file containing the leaf certificate and optional "
|
|
|
|
"certificate chain. This is taken from the -key argument if this "
|
|
|
|
"argument is not provided.",
|
2015-12-03 00:34:58 +00:00
|
|
|
},
|
2016-07-07 22:52:02 +01:00
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-starttls", kOptionalArgument,
|
|
|
|
"A STARTTLS mini-protocol to run before the TLS handshake. Supported"
|
|
|
|
" values: 'smtp'",
|
2016-07-07 22:52:02 +01:00
|
|
|
},
|
2016-09-03 02:35:25 +01:00
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-grease", kBooleanArgument, "Enable GREASE",
|
2016-09-03 02:35:25 +01:00
|
|
|
},
|
2016-12-07 15:31:16 +00:00
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-test-resumption", kBooleanArgument,
|
|
|
|
"Connect to the server twice. The first connection is closed once a "
|
|
|
|
"session is established. The second connection offers it.",
|
2016-12-07 15:31:16 +00:00
|
|
|
},
|
2017-02-03 18:31:00 +00:00
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"-root-certs", kOptionalArgument,
|
|
|
|
"A filename containing one of more PEM root certificates. Implies that "
|
|
|
|
"verification is required.",
|
2017-02-03 18:31:00 +00:00
|
|
|
},
|
2017-01-11 16:34:52 +00:00
|
|
|
{
|
2017-03-09 19:56:07 +00:00
|
|
|
"-early-data", kOptionalArgument, "Allow early data",
|
2017-01-11 16:34:52 +00:00
|
|
|
},
|
2017-06-13 17:45:25 +01:00
|
|
|
{
|
|
|
|
"-tls13-variant", kBooleanArgument,
|
|
|
|
"Enable the experimental TLS 1.3 variant",
|
|
|
|
},
|
2017-03-28 21:38:29 +01:00
|
|
|
{
|
|
|
|
"-ed25519", kBooleanArgument, "Advertise Ed25519 support",
|
|
|
|
},
|
2017-07-07 21:47:59 +01:00
|
|
|
{
|
|
|
|
"-http-tunnel", kOptionalArgument,
|
|
|
|
"An HTTP proxy server to tunnel the TCP connection through",
|
|
|
|
},
|
2015-03-23 23:01:33 +00:00
|
|
|
{
|
2017-04-05 19:52:09 +01:00
|
|
|
"", kOptionalArgument, "",
|
2014-06-20 20:00:00 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:10:28 +01:00
|
|
|
static bssl::UniquePtr<EVP_PKEY> LoadPrivateKey(const std::string &file) {
|
|
|
|
bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file()));
|
2015-03-23 23:01:33 +00:00
|
|
|
if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<EVP_PKEY> pkey(PEM_read_bio_PrivateKey(bio.get(), nullptr,
|
|
|
|
nullptr, nullptr));
|
2015-03-23 23:01:33 +00:00
|
|
|
return pkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
|
|
|
|
const uint8_t* in, unsigned inlen, void* arg) {
|
|
|
|
*out = reinterpret_cast<uint8_t *>(arg);
|
|
|
|
*outlen = strlen(reinterpret_cast<const char *>(arg));
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-18 03:32:50 +00:00
|
|
|
static FILE *g_keylog_file = nullptr;
|
|
|
|
|
|
|
|
static void KeyLogCallback(const SSL *ssl, const char *line) {
|
|
|
|
fprintf(g_keylog_file, "%s\n", line);
|
|
|
|
fflush(g_keylog_file);
|
|
|
|
}
|
|
|
|
|
2016-08-18 04:10:28 +01:00
|
|
|
static bssl::UniquePtr<BIO> session_out;
|
2016-12-07 15:31:16 +00:00
|
|
|
static bssl::UniquePtr<SSL_SESSION> resume_session;
|
2016-08-02 20:59:26 +01:00
|
|
|
|
|
|
|
static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) {
|
|
|
|
if (session_out) {
|
|
|
|
if (!PEM_write_bio_SSL_SESSION(session_out.get(), session) ||
|
|
|
|
BIO_flush(session_out.get()) <= 0) {
|
|
|
|
fprintf(stderr, "Error while saving session:\n");
|
|
|
|
ERR_print_errors_cb(PrintErrorCallback, stderr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-12-07 15:31:16 +00:00
|
|
|
resume_session = bssl::UniquePtr<SSL_SESSION>(session);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool WaitForSession(SSL *ssl, int sock) {
|
|
|
|
fd_set read_fds;
|
|
|
|
FD_ZERO(&read_fds);
|
2016-08-02 20:59:26 +01:00
|
|
|
|
2016-12-07 15:31:16 +00:00
|
|
|
if (!SocketSetNonBlocking(sock, true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!resume_session) {
|
|
|
|
FD_SET(sock, &read_fds);
|
|
|
|
int ret = select(sock + 1, &read_fds, NULL, NULL, NULL);
|
|
|
|
if (ret <= 0) {
|
|
|
|
perror("select");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t buffer[512];
|
|
|
|
int ssl_ret = SSL_read(ssl, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
if (ssl_ret <= 0) {
|
|
|
|
int ssl_err = SSL_get_error(ssl, ssl_ret);
|
|
|
|
if (ssl_err == SSL_ERROR_WANT_READ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Error while reading: %d\n", ssl_err);
|
|
|
|
ERR_print_errors_cb(PrintErrorCallback, stderr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool DoConnection(SSL_CTX *ctx,
|
|
|
|
std::map<std::string, std::string> args_map,
|
|
|
|
bool (*cb)(SSL *ssl, int sock)) {
|
|
|
|
int sock = -1;
|
2017-07-07 21:47:59 +01:00
|
|
|
if (args_map.count("-http-tunnel") != 0) {
|
|
|
|
if (!Connect(&sock, args_map["-http-tunnel"]) ||
|
|
|
|
!DoHTTPTunnel(sock, args_map["-connect"])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (!Connect(&sock, args_map["-connect"])) {
|
2016-12-07 15:31:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args_map.count("-starttls") != 0) {
|
|
|
|
const std::string& starttls = args_map["-starttls"];
|
|
|
|
if (starttls == "smtp") {
|
|
|
|
if (!DoSMTPStartTLS(sock)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unknown value for -starttls: %s\n", starttls.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bssl::UniquePtr<BIO> bio(BIO_new_socket(sock, BIO_CLOSE));
|
|
|
|
bssl::UniquePtr<SSL> ssl(SSL_new(ctx));
|
|
|
|
|
|
|
|
if (args_map.count("-server-name") != 0) {
|
|
|
|
SSL_set_tlsext_host_name(ssl.get(), args_map["-server-name"].c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args_map.count("-session-in") != 0) {
|
|
|
|
bssl::UniquePtr<BIO> in(BIO_new_file(args_map["-session-in"].c_str(),
|
|
|
|
"rb"));
|
|
|
|
if (!in) {
|
|
|
|
fprintf(stderr, "Error reading session\n");
|
|
|
|
ERR_print_errors_cb(PrintErrorCallback, stderr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bssl::UniquePtr<SSL_SESSION> session(PEM_read_bio_SSL_SESSION(in.get(),
|
|
|
|
nullptr, nullptr, nullptr));
|
|
|
|
if (!session) {
|
|
|
|
fprintf(stderr, "Error reading session\n");
|
|
|
|
ERR_print_errors_cb(PrintErrorCallback, stderr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SSL_set_session(ssl.get(), session.get());
|
2017-04-05 19:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (resume_session) {
|
2016-12-07 15:31:16 +00:00
|
|
|
SSL_set_session(ssl.get(), resume_session.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_set_bio(ssl.get(), bio.get(), bio.get());
|
|
|
|
bio.release();
|
|
|
|
|
|
|
|
int ret = SSL_connect(ssl.get());
|
|
|
|
if (ret != 1) {
|
|
|
|
int ssl_err = SSL_get_error(ssl.get(), ret);
|
|
|
|
fprintf(stderr, "Error while connecting: %d\n", ssl_err);
|
|
|
|
ERR_print_errors_cb(PrintErrorCallback, stderr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-09 19:56:07 +00:00
|
|
|
if (args_map.count("-early-data") != 0 && SSL_in_early_data(ssl.get())) {
|
|
|
|
int ed_size = args_map["-early-data"].size();
|
|
|
|
int ssl_ret = SSL_write(ssl.get(), args_map["-early-data"].data(), ed_size);
|
|
|
|
if (ssl_ret <= 0) {
|
|
|
|
int ssl_err = SSL_get_error(ssl.get(), ssl_ret);
|
|
|
|
fprintf(stderr, "Error while writing: %d\n", ssl_err);
|
|
|
|
ERR_print_errors_cb(PrintErrorCallback, stderr);
|
|
|
|
return false;
|
|
|
|
} else if (ssl_ret != ed_size) {
|
|
|
|
fprintf(stderr, "Short write from SSL_write.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 15:31:16 +00:00
|
|
|
fprintf(stderr, "Connected.\n");
|
|
|
|
PrintConnectionInfo(ssl.get());
|
|
|
|
|
|
|
|
return cb(ssl.get(), sock);
|
2016-08-02 20:59:26 +01:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
bool Client(const std::vector<std::string> &args) {
|
2015-01-28 06:32:08 +00:00
|
|
|
if (!InitSocketLibrary()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
std::map<std::string, std::string> args_map;
|
|
|
|
|
|
|
|
if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
|
|
|
|
PrintUsage(kArguments);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(SSLv23_client_method()));
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2014-09-02 21:29:36 +01:00
|
|
|
const char *keylog_file = getenv("SSLKEYLOGFILE");
|
|
|
|
if (keylog_file) {
|
2015-11-18 03:32:50 +00:00
|
|
|
g_keylog_file = fopen(keylog_file, "a");
|
|
|
|
if (g_keylog_file == nullptr) {
|
|
|
|
perror("fopen");
|
2014-09-02 21:29:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-11-18 03:32:50 +00:00
|
|
|
SSL_CTX_set_keylog_callback(ctx.get(), KeyLogCallback);
|
2014-09-02 21:29:36 +01:00
|
|
|
}
|
|
|
|
|
2014-12-11 00:09:52 +00:00
|
|
|
if (args_map.count("-cipher") != 0 &&
|
2017-02-18 06:08:23 +00:00
|
|
|
!SSL_CTX_set_strict_cipher_list(ctx.get(), args_map["-cipher"].c_str())) {
|
2014-12-11 00:09:52 +00:00
|
|
|
fprintf(stderr, "Failed setting cipher list\n");
|
|
|
|
return false;
|
2014-10-28 22:45:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 10:59:34 +01:00
|
|
|
if (args_map.count("-curves") != 0 &&
|
|
|
|
!SSL_CTX_set1_curves_list(ctx.get(), args_map["-curves"].c_str())) {
|
|
|
|
fprintf(stderr, "Failed setting curves list\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-09 23:30:52 +00:00
|
|
|
uint16_t max_version = TLS1_3_VERSION;
|
|
|
|
if (args_map.count("-max-version") != 0 &&
|
|
|
|
!VersionFromString(&max_version, args_map["-max-version"])) {
|
|
|
|
fprintf(stderr, "Unknown protocol version: '%s'\n",
|
|
|
|
args_map["-max-version"].c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SSL_CTX_set_max_proto_version(ctx.get(), max_version)) {
|
|
|
|
return false;
|
2015-03-23 23:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (args_map.count("-min-version") != 0) {
|
|
|
|
uint16_t version;
|
|
|
|
if (!VersionFromString(&version, args_map["-min-version"])) {
|
|
|
|
fprintf(stderr, "Unknown protocol version: '%s'\n",
|
|
|
|
args_map["-min-version"].c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-20 20:12:23 +01:00
|
|
|
if (!SSL_CTX_set_min_proto_version(ctx.get(), version)) {
|
2016-09-20 00:57:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-23 23:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (args_map.count("-select-next-proto") != 0) {
|
|
|
|
const std::string &proto = args_map["-select-next-proto"];
|
|
|
|
if (proto.size() > 255) {
|
|
|
|
fprintf(stderr, "Bad NPN protocol: '%s'\n", proto.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// |SSL_CTX_set_next_proto_select_cb| is not const-correct.
|
|
|
|
SSL_CTX_set_next_proto_select_cb(ctx.get(), NextProtoSelectCallback,
|
|
|
|
const_cast<char *>(proto.c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args_map.count("-alpn-protos") != 0) {
|
|
|
|
const std::string &alpn_protos = args_map["-alpn-protos"];
|
|
|
|
std::vector<uint8_t> wire;
|
|
|
|
size_t i = 0;
|
|
|
|
while (i <= alpn_protos.size()) {
|
|
|
|
size_t j = alpn_protos.find(',', i);
|
|
|
|
if (j == std::string::npos) {
|
|
|
|
j = alpn_protos.size();
|
|
|
|
}
|
|
|
|
size_t len = j - i;
|
|
|
|
if (len > 255) {
|
|
|
|
fprintf(stderr, "Invalid ALPN protocols: '%s'\n", alpn_protos.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wire.push_back(static_cast<uint8_t>(len));
|
|
|
|
wire.resize(wire.size() + len);
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(wire.data() + wire.size() - len, alpn_protos.data() + i,
|
|
|
|
len);
|
2015-03-23 23:01:33 +00:00
|
|
|
i = j + 1;
|
|
|
|
}
|
|
|
|
if (SSL_CTX_set_alpn_protos(ctx.get(), wire.data(), wire.size()) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args_map.count("-fallback-scsv") != 0) {
|
|
|
|
SSL_CTX_set_mode(ctx.get(), SSL_MODE_SEND_FALLBACK_SCSV);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args_map.count("-ocsp-stapling") != 0) {
|
|
|
|
SSL_CTX_enable_ocsp_stapling(ctx.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args_map.count("-signed-certificate-timestamps") != 0) {
|
|
|
|
SSL_CTX_enable_signed_cert_timestamps(ctx.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args_map.count("-channel-id-key") != 0) {
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<EVP_PKEY> pkey =
|
|
|
|
LoadPrivateKey(args_map["-channel-id-key"]);
|
2015-03-23 23:01:33 +00:00
|
|
|
if (!pkey || !SSL_CTX_set1_tls_channel_id(ctx.get(), pkey.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-04 20:26:04 +01:00
|
|
|
if (args_map.count("-false-start") != 0) {
|
|
|
|
SSL_CTX_set_mode(ctx.get(), SSL_MODE_ENABLE_FALSE_START);
|
|
|
|
}
|
|
|
|
|
2015-12-03 00:34:58 +00:00
|
|
|
if (args_map.count("-key") != 0) {
|
|
|
|
const std::string &key = args_map["-key"];
|
2017-04-09 14:52:47 +01:00
|
|
|
if (!SSL_CTX_use_PrivateKey_file(ctx.get(), key.c_str(),
|
|
|
|
SSL_FILETYPE_PEM)) {
|
2015-12-03 00:34:58 +00:00
|
|
|
fprintf(stderr, "Failed to load private key: %s\n", key.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-09 14:52:47 +01:00
|
|
|
const std::string &cert =
|
|
|
|
args_map.count("-cert") != 0 ? args_map["-cert"] : key;
|
|
|
|
if (!SSL_CTX_use_certificate_chain_file(ctx.get(), cert.c_str())) {
|
|
|
|
fprintf(stderr, "Failed to load cert chain: %s\n", cert.c_str());
|
2015-12-03 00:34:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 15:31:16 +00:00
|
|
|
SSL_CTX_set_session_cache_mode(ctx.get(), SSL_SESS_CACHE_CLIENT);
|
|
|
|
SSL_CTX_sess_set_new_cb(ctx.get(), NewSessionCallback);
|
|
|
|
|
2016-08-02 20:59:26 +01:00
|
|
|
if (args_map.count("-session-out") != 0) {
|
|
|
|
session_out.reset(BIO_new_file(args_map["-session-out"].c_str(), "wb"));
|
|
|
|
if (!session_out) {
|
2016-09-06 23:18:52 +01:00
|
|
|
fprintf(stderr, "Error while opening %s:\n",
|
|
|
|
args_map["-session-out"].c_str());
|
2016-08-02 20:59:26 +01:00
|
|
|
ERR_print_errors_cb(PrintErrorCallback, stderr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-03 02:35:25 +01:00
|
|
|
if (args_map.count("-grease") != 0) {
|
|
|
|
SSL_CTX_set_grease_enabled(ctx.get(), 1);
|
|
|
|
}
|
|
|
|
|
2017-02-03 18:31:00 +00:00
|
|
|
if (args_map.count("-root-certs") != 0) {
|
|
|
|
if (!SSL_CTX_load_verify_locations(
|
|
|
|
ctx.get(), args_map["-root-certs"].c_str(), nullptr)) {
|
|
|
|
fprintf(stderr, "Failed to load root certificates.\n");
|
|
|
|
ERR_print_errors_cb(PrintErrorCallback, stderr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SSL_CTX_set_verify(ctx.get(), SSL_VERIFY_PEER, nullptr);
|
|
|
|
}
|
|
|
|
|
2017-01-11 16:34:52 +00:00
|
|
|
if (args_map.count("-early-data") != 0) {
|
|
|
|
SSL_CTX_set_early_data_enabled(ctx.get(), 1);
|
|
|
|
}
|
|
|
|
|
2017-06-13 17:45:25 +01:00
|
|
|
if (args_map.count("-tls13-variant") != 0) {
|
|
|
|
SSL_CTX_set_tls13_variant(ctx.get(), tls13_experiment);
|
|
|
|
}
|
|
|
|
|
2017-03-28 21:38:29 +01:00
|
|
|
if (args_map.count("-ed25519") != 0) {
|
|
|
|
SSL_CTX_set_ed25519_enabled(ctx.get(), 1);
|
|
|
|
}
|
|
|
|
|
2017-04-05 19:52:09 +01:00
|
|
|
if (args_map.count("-test-resumption") != 0) {
|
|
|
|
if (args_map.count("-session-in") != 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Flags -session-in and -test-resumption are incompatible.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DoConnection(ctx.get(), args_map, &WaitForSession)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2016-12-07 15:31:16 +00:00
|
|
|
return DoConnection(ctx.get(), args_map, &TransferData);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|