@@ -29,20 +29,32 @@
#include <openssl/ssl.h>
#include "async_bio.h"
#include "test_config.h"
static int usage(const char *program) {
fprintf(stderr, "Usage: %s (client|server) (normal|resume) [flags...]\n",
fprintf(stderr, "Usage: %s [flags...]\n",
program);
return 1;
}
static const char *expected_server_name = NULL;
static int g_ex_data_index = 0;
static void SetConfigPtr(SSL *ssl, const TestConfig *config) {
SSL_set_ex_data(ssl, g_ex_data_index, (void *)config);
}
static const TestConfig *GetConfigPtr(SSL *ssl) {
return (const TestConfig *)SSL_get_ex_data(ssl, g_ex_data_index);
}
static int early_callback_called = 0;
static int select_certificate_callback(const struct ssl_early_callback_ctx *ctx) {
early_callback_called = 1;
if (!expected_server_name) {
const TestConfig *config = GetConfigPtr(ctx->ssl);
if (config->expected_server_name.empty()) {
return 1;
}
@@ -69,8 +81,9 @@ static int select_certificate_callback(const struct ssl_early_callback_ctx *ctx)
return -1;
}
if (!CBS_mem_equal(&host_name, (const uint8_t*)expected_server_name,
strlen(expected_server_name))) {
if (!CBS_mem_equal(&host_name,
(const uint8_t*)config->expected_server_name.data(),
config->expected_server_name.size())) {
fprintf(stderr, "Server name mismatch.\n");
}
@@ -81,48 +94,42 @@ static int skip_verify(int preverify_ok, X509_STORE_CTX *store_ctx) {
return 1;
}
static const char *advertise_npn = NULL;
static int next_protos_advertised_callback(SSL *ssl,
const uint8_t **out,
unsigned int *out_len,
void *arg) {
if (!advertise_npn)
const TestConfig *config = GetConfigPtr(ssl);
if (config->advertise_npn.empty())
return SSL_TLSEXT_ERR_NOACK;
// TODO(davidben): Support passing byte strings with NULs to the
// test shim.
*out = (const uint8_t*)advertise_npn;
*out_len = strlen(advertise_npn );
*out = (const uint8_t*)config-> advertise_npn.data() ;
*out_len = config->advertise_npn.size( );
return SSL_TLSEXT_ERR_OK;
}
static const char *select_next_proto = NULL;
static int next_proto_select_callback(SSL* ssl,
uint8_t** out,
uint8_t* outlen,
const uint8_t* in,
unsigned inlen,
void* arg) {
if (!select_next_proto)
const TestConfig *config = GetConfigPtr(ssl);
if (config->select_next_proto.empty())
return SSL_TLSEXT_ERR_NOACK;
*out = (uint8_t*)select_next_proto;
*outlen = strlen(select_next_proto );
*out = (uint8_t*)config-> select_next_proto.data() ;
*outlen = config->select_next_proto.size( );
return SSL_TLSEXT_ERR_OK;
}
static SSL_CTX *setup_ctx(int is_server) {
if (!SSL_library_init()) {
return NULL;
}
static SSL_CTX *setup_ctx(const TestConfig *config) {
SSL_CTX *ssl_ctx = NULL;
DH *dh = NULL;
ssl_ctx = SSL_CTX_new(
is_server ? SSLv23_server_method() : SSLv23_client_method());
config-> is_server ? SSLv23_server_method() : SSLv23_client_method());
if (ssl_ctx == NULL) {
goto err;
}
@@ -182,18 +189,11 @@ static int retry_async(SSL *ssl, int ret, BIO *bio) {
static int do_exchange(SSL_SESSION **out_session,
SSL_CTX *ssl_ctx,
int argc,
char **argv,
int is_server,
int is_resume,
const TestConfig *config,
bool is_resume,
int fd,
SSL_SESSION *session) {
bool async = false, write_different_record_sizes = false;
const char *expected_certificate_types = NULL;
const char *expected_next_proto = NULL;
expected_server_name = NULL;
early_callback_called = 0;
advertise_npn = NULL;
SSL *ssl = SSL_new(ssl_ctx);
if (ssl == NULL) {
@@ -201,101 +201,60 @@ static int do_exchange(SSL_SESSION **out_session,
return 1;
}
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-fallback-scsv") == 0) {
if (!SSL_enable_fallback_scsv(ssl)) {
BIO_print_errors_fp(stdout);
return 1;
}
} else if (strcmp(argv[i], "-key-file") == 0) {
i++;
if (i >= argc) {
fprintf(stderr, "Missing parameter\n");
return 1;
}
if (!SSL_use_PrivateKey_file(ssl, argv[i], SSL_FILETYPE_PEM)) {
BIO_print_errors_fp(stdout);
return 1;
}
} else if (strcmp(argv[i], "-cert-file") == 0) {
i++;
if (i >= argc) {
fprintf(stderr, "Missing parameter\n");
return 1;
}
if (!SSL_use_certificate_file(ssl, argv[i], SSL_FILETYPE_PEM)) {
BIO_print_errors_fp(stdout);
return 1;
}
} else if (strcmp(argv[i], "-expect-server-name") == 0) {
i++;
if (i >= argc) {
fprintf(stderr, "Missing parameter\n");
return 1;
}
expected_server_name = argv[i];
} else if (strcmp(argv[i], "-expect-certificate-types") == 0) {
i++;
if (i >= argc) {
fprintf(stderr, "Missing parameter\n");
return 1;
}
// Conveniently, 00 is not a certificate type.
expected_certificate_types = argv[i];
} else if (strcmp(argv[i], "-require-any-client-certificate") == 0) {
SSL_set_verify(ssl, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
skip_verify);
} else if (strcmp(argv[i], "-advertise-npn") == 0) {
i++;
if (i >= argc) {
fprintf(stderr, "Missing parameter\n");
return 1;
}
advertise_npn = argv[i];
} else if (strcmp(argv[i], "-expect-next-proto") == 0) {
i++;
if (i >= argc) {
fprintf(stderr, "Missing parameter\n");
return 1;
}
expected_next_proto = argv[i];
} else if (strcmp(argv[i], "-false-start") == 0) {
SSL_set_mode(ssl, SSL_MODE_HANDSHAKE_CUTTHROUGH);
} else if (strcmp(argv[i], "-select-next-proto") == 0) {
i++;
if (i >= argc) {
fprintf(stderr, "Missing parameter\n");
return 1;
}
select_next_proto = argv[i];
} else if (strcmp(argv[i], "-async") == 0) {
async = true;
} else if (strcmp(argv[i], "-write-different-record-sizes") == 0) {
write_different_record_sizes = true;
} else if (strcmp(argv[i], "-cbc-record-splitting") == 0) {
SSL_set_mode(ssl, SSL_MODE_CBC_RECORD_SPLITTING);
} else if (strcmp(argv[i], "-partial-write") == 0) {
SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);
} else if (strcmp(argv[i], "-no-tls12") == 0) {
SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
} else if (strcmp(argv[i], "-no-tls11") == 0) {
SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
} else if (strcmp(argv[i], "-no-tls1") == 0) {
SSL_set_options(ssl, SSL_OP_NO_TLSv1);
} else if (strcmp(argv[i], "-no-ssl3") == 0) {
SSL_set_options(ssl, SSL_OP_NO_SSLv3);
} else {
fprintf(stderr, "Unknown argument: %s\n", argv[i]);
SetConfigPtr(ssl, config);
if (config->fallback_scsv) {
if (!SSL_enable_fallback_scsv(ssl)) {
BIO_print_errors_fp(stdout);
return 1;
}
}
if (!config->key_file.empty()) {
if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
SSL_FILETYPE_PEM)) {
BIO_print_errors_fp(stdout);
return 1;
}
}
if (!config->cert_file.empty()) {
if (!SSL_use_certificate_file(ssl, config->cert_file.c_str(),
SSL_FILETYPE_PEM)) {
BIO_print_errors_fp(stdout);
return 1;
}
}
if (config->require_any_client_certificate) {
SSL_set_verify(ssl, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
skip_verify);
}
if (config->false_start) {
SSL_set_mode(ssl, SSL_MODE_HANDSHAKE_CUTTHROUGH);
}
if (config->cbc_record_splitting) {
SSL_set_mode(ssl, SSL_MODE_CBC_RECORD_SPLITTING);
}
if (config->partial_write) {
SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);
}
if (config->no_tls12) {
SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
}
if (config->no_tls11) {
SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
}
if (config->no_tls1) {
SSL_set_options(ssl, SSL_OP_NO_TLSv1);
}
if (config->no_ssl3) {
SSL_set_options(ssl, SSL_OP_NO_SSLv3);
}
BIO *bio = BIO_new_fd(fd, 1 /* take ownership */);
if (bio == NULL) {
BIO_print_errors_fp(stdout);
return 1;
}
if (async) {
if (config->async) {
BIO *async = async_bio_create();
BIO_push(async, bio);
bio = async;
@@ -311,12 +270,12 @@ static int do_exchange(SSL_SESSION **out_session,
int ret;
do {
if (is_server) {
if (config-> is_server) {
ret = SSL_accept(ssl);
} else {
ret = SSL_connect(ssl);
}
} while (async && retry_async(ssl, ret, bio));
} while (config-> async && retry_async(ssl, ret, bio));
if (ret != 1) {
SSL_free(ssl);
BIO_print_errors_fp(stdout);
@@ -328,12 +287,12 @@ static int do_exchange(SSL_SESSION **out_session,
return 2;
}
if (expected_server_name) {
if (!config-> expected_server_name.empty() ) {
const char *server_name =
SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
if (strcmp(s erver_name, expected_server_name) != 0 ) {
if (server_name != config-> expected_server_name) {
fprintf(stderr, "servername mismatch (got %s; want %s)\n",
server_name, expected_server_name);
server_name, config-> expected_server_name.c_str() );
return 2;
}
@@ -343,31 +302,33 @@ static int do_exchange(SSL_SESSION **out_session,
}
}
if (expected_certificate_types) {
if (!config-> expected_certificate_types.empty() ) {
uint8_t *certificate_types;
int num_certificate_types =
SSL_get0_certificate_types(ssl, &certificate_types);
if (num_certificate_types != (int)strlen(expected_certificate_types) ||
if (num_certificate_types !=
(int)config->expected_certificate_types.size() ||
memcmp(certificate_types,
expected_certificate_types,
config-> expected_certificate_types.data() ,
num_certificate_types) != 0) {
fprintf(stderr, "certificate types mismatch\n");
return 2;
}
}
if (expected_next_proto) {
if (!config-> expected_next_proto.empty() ) {
const uint8_t *next_proto;
unsigned next_proto_len;
SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
if (next_proto_len != strlen(expected_next_proto) ||
memcmp(next_proto, expected_next_proto, next_proto_len) != 0) {
if (next_proto_len != config->expected_next_proto.size() ||
memcmp(next_proto, config->expected_next_proto.data(),
next_proto_len) != 0) {
fprintf(stderr, "negotiated next proto mismatch\n");
return 2;
}
}
if (write_different_record_sizes) {
if (config-> write_different_record_sizes) {
// This mode writes a number of different record sizes in an attempt to
// trip up the CBC record splitting code.
uint8_t buf[32769];
@@ -390,7 +351,8 @@ static int do_exchange(SSL_SESSION **out_session,
if (w > 0) {
off += (size_t) w;
}
} while ((async && retry_async(ssl, w, bio)) || (w > 0 && off < len));
} while ((config->async && retry_async(ssl, w, bio)) ||
(w > 0 && off < len));
if (w < 0 || off != len) {
SSL_free(ssl);
@@ -404,7 +366,7 @@ static int do_exchange(SSL_SESSION **out_session,
int n;
do {
n = SSL_read(ssl, buf, sizeof(buf));
} while (async && retry_async(ssl, n, bio));
} while (config-> async && retry_async(ssl, n, bio));
if (n < 0) {
SSL_free(ssl);
BIO_print_errors_fp(stdout);
@@ -418,7 +380,7 @@ static int do_exchange(SSL_SESSION **out_session,
int w;
do {
w = SSL_write(ssl, buf, n);
} while (async && retry_async(ssl, w, bio));
} while (config-> async && retry_async(ssl, w, bio));
if (w != n) {
SSL_free(ssl);
BIO_print_errors_fp(stdout);
@@ -438,33 +400,21 @@ static int do_exchange(SSL_SESSION **out_session,
}
int main(int argc, char **argv) {
int is_server, resume;
#if !defined(OPENSSL_WINDOWS)
signal(SIGPIPE, SIG_IGN);
#endif
if (argc < 3) {
return usage(argv[0]);
}
if (strcmp(argv[1], "client") == 0) {
is_server = 0;
} else if (strcmp(argv[1], "server") == 0) {
is_server = 1;
} else {
return usage(argv[0]);
if (!SSL_library_init()) {
return 1;
}
g_ex_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
if (strcmp(argv[2], "normal") == 0) {
resume = 0;
} else if (strcmp(argv[2], "resume") == 0) {
resume = 1;
} else {
TestConfig config;
if (!ParseConfig(argc - 1, argv + 1, &config)) {
return usage(argv[0]);
}
SSL_CTX *ssl_ctx = setup_ctx(is_server );
SSL_CTX *ssl_ctx = setup_ctx(&config );
if (ssl_ctx == NULL) {
BIO_print_errors_fp(stdout);
return 1;
@@ -472,20 +422,19 @@ int main(int argc, char **argv) {
SSL_SESSION *session;
int ret = do_exchange(&session,
ssl_ctx,
argc - 3, argv + 3,
is_server, 0 /* is_resume */,
ssl_ctx, &config,
false /* is_resume */,
3 /* fd */, NULL /* session */);
if (ret != 0) {
return ret;
}
if (resume) {
if (config. resume) {
int ret = do_exchange(NULL,
ssl_ctx, argc - 3, argv + 3 ,
is_server, 1 /* is_resume */,
ssl_ctx, &config ,
true /* is_resume */,
4 /* fd */,
is_server ? NULL : session);
config. is_server ? NULL : session);
if (ret != 0) {
return ret;
}