Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'ciphersuites=' method to allow setting of TLSv1.3 cipher suites along with some unit tests #493

Merged
merged 8 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/openssl/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def find_openssl_library

# added in 1.1.1
have_func("EVP_PKEY_check")
have_func("SSL_CTX_set_ciphersuites")

# added in 3.0.0
have_func("SSL_set0_tmp_dh_pkey")
Expand Down
82 changes: 62 additions & 20 deletions ext/openssl/ossl_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -959,27 +959,13 @@ ossl_sslctx_get_ciphers(VALUE self)
return ary;
}

/*
* call-seq:
* ctx.ciphers = "cipher1:cipher2:..."
* ctx.ciphers = [name, ...]
* ctx.ciphers = [[name, version, bits, alg_bits], ...]
*
* Sets the list of available cipher suites for this context. Note in a server
* context some ciphers require the appropriate certificates. For example, an
* RSA cipher suite can only be chosen when an RSA certificate is available.
*/
static VALUE
ossl_sslctx_set_ciphers(VALUE self, VALUE v)
build_cipher_string(VALUE v)
{
SSL_CTX *ctx;
VALUE str, elem;
int i;

rb_check_frozen(self);
if (NIL_P(v))
return v;
else if (RB_TYPE_P(v, T_ARRAY)) {
if (RB_TYPE_P(v, T_ARRAY)) {
str = rb_str_new(0, 0);
for (i = 0; i < RARRAY_LEN(v); i++) {
elem = rb_ary_entry(v, i);
Expand All @@ -993,14 +979,67 @@ ossl_sslctx_set_ciphers(VALUE self, VALUE v)
StringValue(str);
}

return str;
}

/*
* call-seq:
* ctx.ciphers = "cipher1:cipher2:..."
* ctx.ciphers = [name, ...]
* ctx.ciphers = [[name, version, bits, alg_bits], ...]
*
* Sets the list of available cipher suites for this context. Note in a server
* context some ciphers require the appropriate certificates. For example, an
* RSA cipher suite can only be chosen when an RSA certificate is available.
*/
static VALUE
ossl_sslctx_set_ciphers(VALUE self, VALUE v)
{
SSL_CTX *ctx;
VALUE str;

rb_check_frozen(self);
if (NIL_P(v))
return v;

str = build_cipher_string(v);

GetSSLCTX(self, ctx);
if (!SSL_CTX_set_cipher_list(ctx, StringValueCStr(str))) {
if (!SSL_CTX_set_cipher_list(ctx, StringValueCStr(str)))
ossl_raise(eSSLError, "SSL_CTX_set_cipher_list");
}

return v;
}

#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
/*
* call-seq:
* ctx.tls13_ciphers = "cipher1:cipher2:..."
* ctx.tls13_ciphers = [name, ...]
* ctx.tls13_ciphers = [[name, version, bits, alg_bits], ...]
kmdz1 marked this conversation as resolved.
Show resolved Hide resolved
*
* Sets the list of available TLSv1.3 cipher suites for this context.
*/
static VALUE
ossl_sslctx_set_ciphersuites(VALUE self, VALUE v)
{
SSL_CTX *ctx;
VALUE str;

rb_check_frozen(self);
if (NIL_P(v))
return v;

str = build_cipher_string(v);

GetSSLCTX(self, ctx);
if (!SSL_CTX_set_ciphersuites(ctx, StringValueCStr(str)))
ossl_raise(eSSLError, "SSL_CTX_set_ciphersuites");

return v;
}
#endif

#ifndef OPENSSL_NO_DH
/*
* call-seq:
Expand Down Expand Up @@ -2701,8 +2740,11 @@ Init_ossl_ssl(void)
rb_define_alias(cSSLContext, "ssl_timeout=", "timeout=");
rb_define_private_method(cSSLContext, "set_minmax_proto_version",
ossl_sslctx_set_minmax_proto_version, 2);
rb_define_method(cSSLContext, "ciphers", ossl_sslctx_get_ciphers, 0);
rb_define_method(cSSLContext, "ciphers=", ossl_sslctx_set_ciphers, 1);
rb_define_method(cSSLContext, "ciphers", ossl_sslctx_get_ciphers, 0);
rb_define_method(cSSLContext, "ciphers=", ossl_sslctx_set_ciphers, 1);
kmdz1 marked this conversation as resolved.
Show resolved Hide resolved
#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
rb_define_method(cSSLContext, "ciphersuites=", ossl_sslctx_set_ciphersuites, 1);
#endif
#ifndef OPENSSL_NO_DH
rb_define_method(cSSLContext, "tmp_dh=", ossl_sslctx_set_tmp_dh, 1);
#endif
Expand Down
98 changes: 98 additions & 0 deletions test/openssl/test_ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,104 @@ def test_tmp_dh_callback
end
end

def test_ciphersuites_method_tls_connection
pend 'TLS 1.3 not supported' unless tls13_supported?
ssl_ctx = OpenSSL::SSL::SSLContext.new
tls13_ciphersuites = ssl_ctx.ciphers.select { |x| x[1] == 'TLSv1.3' }
if tls13_ciphersuites.empty? || !ssl_ctx.respond_to?(:ciphersuites=)
pend 'no support for TLSv1.3 cipher suites?'
end

tls13_csuite = tls13_ciphersuites[-1]
inputs = [tls13_csuite[0], [tls13_csuite[0]], [tls13_csuite]]
kmdz1 marked this conversation as resolved.
Show resolved Hide resolved

ctx_proc = -> ctx {
ctx.min_version = ctx.max_version = OpenSSL::SSL::TLS1_3_VERSION
ctx.ciphersuites = tls13_csuite[0]
}
kmdz1 marked this conversation as resolved.
Show resolved Hide resolved
start_server(ctx_proc: ctx_proc) do |port|
inputs.each do |input|
cli_ctx = OpenSSL::SSL::SSLContext.new
cli_ctx.min_version = cli_ctx.max_version = OpenSSL::SSL::TLS1_3_VERSION
cli_ctx.ciphersuites = input

server_connect(port, cli_ctx) do |ssl|
assert_equal(ssl.ssl_version, 'TLSv1.3')
kmdz1 marked this conversation as resolved.
Show resolved Hide resolved
assert_equal(ssl.cipher, tls13_csuite)
end
end
end
end

def test_ciphersuites_method_nil_argument
ssl_ctx = OpenSSL::SSL::SSLContext.new
pend 'ciphersuites= method is missing' unless ssl_ctx.respond_to?(:ciphersuites=)

assert_equal(ssl_ctx.ciphersuites = nil, nil)
kmdz1 marked this conversation as resolved.
Show resolved Hide resolved
end

def test_ciphersuites_method_frozen_object
ssl_ctx = OpenSSL::SSL::SSLContext.new
pend 'ciphersuites= method is missing' unless ssl_ctx.respond_to?(:ciphersuites=)

ssl_ctx.freeze
assert_raise(FrozenError) { ssl_ctx.ciphersuites = 'TLS_AES_256_GCM_SHA384' }
end

def test_ciphersuites_method_bogus_csuite
ssl_ctx = OpenSSL::SSL::SSLContext.new
pend 'ciphersuites= method is missing' unless ssl_ctx.respond_to?(:ciphersuites=)

assert_raise_with_message(
OpenSSL::SSL::SSLError,
/SSL_CTX_set_ciphersuites: no cipher match/i
) { ssl_ctx.ciphersuites = 'BOGUS' }
end

def test_ciphers_method_tls_connection
ssl_ctx = OpenSSL::SSL::SSLContext.new
csuite = ssl_ctx.ciphers.select { |x| x[0] =~ /^(EC)?DHE?-RSA/ && \
x[1] == 'TLSv1.0' }[-1]

pend "couldn't find a TLSv1.0 cipher suite for testing" if csuite.nil?
kmdz1 marked this conversation as resolved.
Show resolved Hide resolved

inputs = [csuite[0], [csuite[0]], [csuite]]

start_server do |port|
inputs.each do |input|
cli_ctx = OpenSSL::SSL::SSLContext.new
cli_ctx.min_version = cli_ctx.max_version = OpenSSL::SSL::TLS1_VERSION
kmdz1 marked this conversation as resolved.
Show resolved Hide resolved
cli_ctx.ciphers = input

server_connect(port, cli_ctx) do |ssl|
assert_equal(ssl.ssl_version, 'TLSv1')
assert_equal(ssl.cipher, csuite)
end
end
end
end

def test_ciphers_method_nil_argument
ssl_ctx = OpenSSL::SSL::SSLContext.new
assert_equal(ssl_ctx.ciphers = nil, nil)
end

def test_ciphers_method_frozen_object
ssl_ctx = OpenSSL::SSL::SSLContext.new

ssl_ctx.freeze
assert_raise(FrozenError) { ssl_ctx.ciphers = 'ECDHE-RSA-AES128-SHA' }
end

def test_ciphers_method_bogus_csuite
ssl_ctx = OpenSSL::SSL::SSLContext.new

assert_raise_with_message(
OpenSSL::SSL::SSLError,
/SSL_CTX_set_cipher_list: no cipher match/i
) { ssl_ctx.ciphers = 'BOGUS' }
end

def test_connect_works_when_setting_dh_callback_to_nil
ctx_proc = -> ctx {
ctx.max_version = :TLS1_2
Expand Down