소스 검색

Move configuration into a dedicated TestConfig struct.

This removes some duplicate code in parsing command-line flags and, more
importantly, makes configuration available when constructing the SSL_CTX and
avoids a number of globals.

Change-Id: I26e2d2285b732f855a2c82752bc8e0db480c3b30
Reviewed-on: https://boringssl-review.googlesource.com/1502
Reviewed-by: Adam Langley <agl@google.com>
kris/onging/CECPQ3_patch15
David Benjamin 10 년 전
committed by Adam Langley
부모
커밋
5a593af42a
5개의 변경된 파일284개의 추가작업 그리고 169개의 파일을 삭제
  1. +1
    -0
      ssl/test/CMakeLists.txt
  2. +105
    -156
      ssl/test/bssl_shim.cc
  3. +8
    -13
      ssl/test/runner/runner.go
  4. +121
    -0
      ssl/test/test_config.cc
  5. +49
    -0
      ssl/test/test_config.h

+ 1
- 0
ssl/test/CMakeLists.txt 파일 보기

@@ -5,6 +5,7 @@ add_executable(

async_bio.cc
bssl_shim.cc
test_config.cc
)

target_link_libraries(bssl_shim ssl crypto)

+ 105
- 156
ssl/test/bssl_shim.cc 파일 보기

@@ -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(server_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;
}


+ 8
- 13
ssl/test/runner/runner.go 파일 보기

@@ -549,20 +549,10 @@ func runTest(test *testCase, buildDir string) error {
shimEndResume, connResume := openSocketPair()

shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
flags := []string{}
if test.testType == clientTest {
flags = append(flags, "client")
} else {
flags = append(flags, "server")
}

if test.resumeSession {
flags = append(flags, "resume")
} else {
flags = append(flags, "normal")
}

var flags []string
if test.testType == serverTest {
flags = append(flags, "-server")

flags = append(flags, "-key-file")
if test.keyFile == "" {
flags = append(flags, rsaKeyFile)
@@ -577,6 +567,11 @@ func runTest(test *testCase, buildDir string) error {
flags = append(flags, test.certFile)
}
}

if test.resumeSession {
flags = append(flags, "-resume")
}

flags = append(flags, test.flags...)

var shim *exec.Cmd


+ 121
- 0
ssl/test/test_config.cc 파일 보기

@@ -0,0 +1,121 @@
/* 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 "test_config.h"

#include <string.h>


namespace {

typedef bool TestConfig::*BoolMember;
typedef std::string TestConfig::*StringMember;

struct BoolFlag {
const char *flag;
BoolMember member;
};

struct StringFlag {
const char *flag;
StringMember member;
};

const BoolFlag kBoolFlags[] = {
{ "-server", &TestConfig::is_server },
{ "-resume", &TestConfig::resume },
{ "-fallback-scsv", &TestConfig::fallback_scsv },
{ "-require-any-client-certificate",
&TestConfig::require_any_client_certificate },
{ "-false-start", &TestConfig::false_start },
{ "-async", &TestConfig::async },
{ "-write-different-record-sizes",
&TestConfig::write_different_record_sizes },
{ "-cbc-record-splitting", &TestConfig::cbc_record_splitting },
{ "-partial-write", &TestConfig::partial_write },
{ "-no-tls12", &TestConfig::no_tls12 },
{ "-no-tls11", &TestConfig::no_tls11 },
{ "-no-tls1", &TestConfig::no_tls1 },
{ "-no-ssl3", &TestConfig::no_ssl3 },
};

const size_t kNumBoolFlags = sizeof(kBoolFlags) / sizeof(kBoolFlags[0]);

// TODO(davidben): Some of these should be in a new kBase64Flags to allow NUL
// bytes.
const StringFlag kStringFlags[] = {
{ "-key-file", &TestConfig::key_file },
{ "-cert-file", &TestConfig::cert_file },
{ "-expect-server-name", &TestConfig::expected_server_name },
// Conveniently, 00 is not a certificate type.
{ "-expect-certificate-types", &TestConfig::expected_certificate_types },
{ "-advertise-npn", &TestConfig::advertise_npn },
{ "-expect-next-proto", &TestConfig::expected_next_proto },
{ "-select-next-proto", &TestConfig::select_next_proto },
};

const size_t kNumStringFlags = sizeof(kStringFlags) / sizeof(kStringFlags[0]);

} // namespace

TestConfig::TestConfig()
: is_server(false),
resume(false),
fallback_scsv(false),
require_any_client_certificate(false),
false_start(false),
async(false),
write_different_record_sizes(false),
cbc_record_splitting(false),
partial_write(false),
no_tls12(false),
no_tls11(false),
no_tls1(false),
no_ssl3(false) {
}

bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
for (int i = 0; i < argc; i++) {
size_t j;
for (j = 0; j < kNumBoolFlags; j++) {
if (strcmp(argv[i], kBoolFlags[j].flag) == 0) {
break;
}
}
if (j < kNumBoolFlags) {
out_config->*(kBoolFlags[j].member) = true;
continue;
}

for (j = 0; j < kNumStringFlags; j++) {
if (strcmp(argv[i], kStringFlags[j].flag) == 0) {
break;
}
}
if (j < kNumStringFlags) {
i++;
if (i >= argc) {
fprintf(stderr, "Missing parameter\n");
return false;
}
out_config->*(kStringFlags[j].member) = argv[i];
continue;
}

fprintf(stderr, "Unknown argument: %s\n", argv[i]);
return false;
}

return true;
}

+ 49
- 0
ssl/test/test_config.h 파일 보기

@@ -0,0 +1,49 @@
/* 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. */

#ifndef HEADER_TEST_CONFIG
#define HEADER_TEST_CONFIG

#include <string>


struct TestConfig {
TestConfig();

bool is_server;
bool resume;
bool fallback_scsv;
std::string key_file;
std::string cert_file;
std::string expected_server_name;
std::string expected_certificate_types;
bool require_any_client_certificate;
std::string advertise_npn;
std::string expected_next_proto;
bool false_start;
std::string select_next_proto;
bool async;
bool write_different_record_sizes;
bool cbc_record_splitting;
bool partial_write;
bool no_tls12;
bool no_tls11;
bool no_tls1;
bool no_ssl3;
};

bool ParseConfig(int argc, char **argv, TestConfig *out_config);


#endif // HEADER_TEST_CONFIG

불러오는 중...
취소
저장