Skip to content

Commit

Permalink
Enable specifying the tls protocol version to use via --tls-protocols…
Browse files Browse the repository at this point in the history
… argument
  • Loading branch information
filipecosta90 committed Sep 12, 2023
1 parent 4203084 commit 8dd7eb2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions config_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ config_quantiles::config_quantiles(){

}


config_quantiles::config_quantiles(const char *str)
{
assert(str != NULL);
Expand Down
56 changes: 56 additions & 0 deletions memtier_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/rand.h>

#define REDIS_TLS_PROTO_TLSv1 (1<<0)
#define REDIS_TLS_PROTO_TLSv1_1 (1<<1)
#define REDIS_TLS_PROTO_TLSv1_2 (1<<2)
#define REDIS_TLS_PROTO_TLSv1_3 (1<<3)

/* Use safe defaults */
#ifdef TLS1_3_VERSION
#define REDIS_TLS_PROTO_DEFAULT (REDIS_TLS_PROTO_TLSv1_2|REDIS_TLS_PROTO_TLSv1_3)
#else
#define REDIS_TLS_PROTO_DEFAULT (REDIS_TLS_PROTO_TLSv1_2)
#endif

#endif

#include <stdexcept>
Expand Down Expand Up @@ -296,6 +309,8 @@ static void config_init_defaults(struct benchmark_config *cfg)
cfg->hdr_prefix = "";
if (!cfg->print_percentiles.is_defined())
cfg->print_percentiles = config_quantiles("50,99,99.9");
if (!cfg->tls_protocols)
cfg->tls_protocols = REDIS_TLS_PROTO_DEFAULT;
}

static int generate_random_seed()
Expand Down Expand Up @@ -404,6 +419,7 @@ static int config_parse_args(int argc, char *argv[], struct benchmark_config *cf
o_tls_cacert,
o_tls_skip_verify,
o_tls_sni,
o_tls_protocols,
o_hdr_file_prefix,
o_help
};
Expand All @@ -423,6 +439,7 @@ static int config_parse_args(int argc, char *argv[], struct benchmark_config *cf
{ "cacert", 1, 0, o_tls_cacert },
{ "tls-skip-verify", 0, 0, o_tls_skip_verify },
{ "sni", 1, 0, o_tls_sni },
{ "tls-protocols", 1, 0, o_tls_protocols },
#endif
{ "out-file", 1, 0, 'o' },
{ "hdr-file-prefix", 1, 0, o_hdr_file_prefix },
Expand Down Expand Up @@ -863,6 +880,35 @@ static int config_parse_args(int argc, char *argv[], struct benchmark_config *cf
case o_tls_sni:
cfg->tls_sni = optarg;
break;
case o_tls_protocols:

Check warning on line 883 in memtier_benchmark.cpp

View check run for this annotation

Codecov / codecov/patch

memtier_benchmark.cpp#L883

Added line #L883 was not covered by tests
{
const char tls_delimiter = ',';
char* tls_token = strtok(optarg, &tls_delimiter);

Check warning on line 886 in memtier_benchmark.cpp

View check run for this annotation

Codecov / codecov/patch

memtier_benchmark.cpp#L885-L886

Added lines #L885 - L886 were not covered by tests
// Loop through the tokens and print them
while (tls_token != nullptr) {
if (!strcasecmp(tls_token, "tlsv1"))
cfg->tls_protocols |= REDIS_TLS_PROTO_TLSv1;
else if (!strcasecmp(tls_token, "tlsv1.1"))
cfg->tls_protocols |= REDIS_TLS_PROTO_TLSv1_1;
else if (!strcasecmp(tls_token, "tlsv1.2"))
cfg->tls_protocols |= REDIS_TLS_PROTO_TLSv1_2;
else if (!strcasecmp(tls_token, "tlsv1.3")) {

Check warning on line 895 in memtier_benchmark.cpp

View check run for this annotation

Codecov / codecov/patch

memtier_benchmark.cpp#L888-L895

Added lines #L888 - L895 were not covered by tests
#ifdef TLS1_3_VERSION
cfg->tls_protocols |= REDIS_TLS_PROTO_TLSv1_3;

Check warning on line 897 in memtier_benchmark.cpp

View check run for this annotation

Codecov / codecov/patch

memtier_benchmark.cpp#L897

Added line #L897 was not covered by tests
#else
fprintf(stderr, "TLSv1.3 is specified in tls-protocols but not supported by OpenSSL.");
return -1;
#endif
} else {
fprintf(stderr, "Invalid tls-protocols specified. "

Check warning on line 903 in memtier_benchmark.cpp

View check run for this annotation

Codecov / codecov/patch

memtier_benchmark.cpp#L903

Added line #L903 was not covered by tests
"Use a combination of 'TLSv1', 'TLSv1.1', 'TLSv1.2' and 'TLSv1.3'.");
return -1;
break;

Check warning on line 906 in memtier_benchmark.cpp

View check run for this annotation

Codecov / codecov/patch

memtier_benchmark.cpp#L905-L906

Added lines #L905 - L906 were not covered by tests
}
tls_token = strtok(nullptr, &tls_delimiter);

Check warning on line 908 in memtier_benchmark.cpp

View check run for this annotation

Codecov / codecov/patch

memtier_benchmark.cpp#L908

Added line #L908 was not covered by tests
}
break;

Check warning on line 910 in memtier_benchmark.cpp

View check run for this annotation

Codecov / codecov/patch

memtier_benchmark.cpp#L910

Added line #L910 was not covered by tests
}
#endif
default:
return -1;
Expand Down Expand Up @@ -903,6 +949,7 @@ void usage() {
" --key=FILE Use specified private key for TLS\n"
" --cacert=FILE Use specified CA certs bundle for TLS\n"
" --tls-skip-verify Skip verification of server certificate\n"
" --tls-protocols Specify the tls protocol version to use, comma delemited. Use a combination of 'TLSv1', 'TLSv1.1', 'TLSv1.2' and 'TLSv1.3'"
" --sni=STRING Add an SNI header\n"
#endif
" -x, --run-count=NUMBER Number of full-test iterations to perform\n"
Expand Down Expand Up @@ -1311,6 +1358,15 @@ int main(int argc, char *argv[])
cfg.openssl_ctx = SSL_CTX_new(SSLv23_client_method());
SSL_CTX_set_options(cfg.openssl_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);

if (!(cfg.tls_protocols & REDIS_TLS_PROTO_TLSv1))
SSL_CTX_set_options(cfg.openssl_ctx, SSL_OP_NO_TLSv1);
if (!(cfg.tls_protocols & REDIS_TLS_PROTO_TLSv1_1))
SSL_CTX_set_options(cfg.openssl_ctx, SSL_OP_NO_TLSv1_1);
if (!(cfg.tls_protocols & REDIS_TLS_PROTO_TLSv1_2))
SSL_CTX_set_options(cfg.openssl_ctx, SSL_OP_NO_TLSv1_2);

Check warning on line 1366 in memtier_benchmark.cpp

View check run for this annotation

Codecov / codecov/patch

memtier_benchmark.cpp#L1366

Added line #L1366 was not covered by tests
if (!(cfg.tls_protocols & REDIS_TLS_PROTO_TLSv1_3))
SSL_CTX_set_options(cfg.openssl_ctx, SSL_OP_NO_TLSv1_3);

Check warning on line 1368 in memtier_benchmark.cpp

View check run for this annotation

Codecov / codecov/patch

memtier_benchmark.cpp#L1368

Added line #L1368 was not covered by tests

if (cfg.tls_cert) {
if (!SSL_CTX_use_certificate_chain_file(cfg.openssl_ctx, cfg.tls_cert)) {
ERR_print_errors_fp(stderr);
Expand Down
1 change: 1 addition & 0 deletions memtier_benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ struct benchmark_config {
const char *tls_cacert;
bool tls_skip_verify;
const char *tls_sni;
int tls_protocols;
SSL_CTX *openssl_ctx;
#endif
};
Expand Down

0 comments on commit 8dd7eb2

Please sign in to comment.