Skip to content

Commit da704fc

Browse files
committed
ssl: rework SSLContext#ssl_version=
Reimplement SSLContext#ssl_version= as a wrapper around SSLContext#min_version= and #max_version=. SSLContext#ssl_version= used to call SSL_CTX_set_ssl_version() which replaces the SSL method used for the connections created from the SSL context. This is mainly used for forcing a specific SSL/TLS protocol version. As of OpenSSL 1.1.0, however, use of the version-specific SSL methods such as TLSv1_method() is deprecated. Follow the current recommendation -- to use the generic SSL method always and to control the supported version range by SSL_CTX_set_{min,max}_proto_version(). Actually, we have already started doing a similar thing when the extension is compiled with OpenSSL 1.1.0. OpenSSL::SSL::SSLContext::METHODS, which contained the possible names of SSL methods, is not useful anymore. It is now deprecate_constant-ed.
1 parent da668ca commit da704fc

File tree

4 files changed

+56
-100
lines changed

4 files changed

+56
-100
lines changed

ext/openssl/extconf.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ def find_openssl_library
104104

105105
Logging::message "=== Checking for OpenSSL features... ===\n"
106106
# compile options
107-
108-
# SSLv2 and SSLv3 may be removed in future versions of OpenSSL, and even macros
109-
# like OPENSSL_NO_SSL2 may not be defined.
110-
have_func("SSLv2_method")
111-
have_func("SSLv3_method")
112107
have_func("RAND_egd")
113108
engines = %w{builtin_engines openbsd_dev_crypto dynamic 4758cca aep atalla chil
114109
cswift nuron sureware ubsec padlock capi gmp gost cryptodev aesni}

ext/openssl/ossl_ssl.c

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -46,44 +46,6 @@ static ID id_i_cert_store, id_i_ca_file, id_i_ca_path, id_i_verify_mode,
4646
id_i_verify_hostname;
4747
static ID id_i_io, id_i_context, id_i_hostname;
4848

49-
/*
50-
* SSLContext class
51-
*/
52-
static const struct {
53-
const char *name;
54-
const SSL_METHOD *(*func)(void);
55-
int version;
56-
} ossl_ssl_method_tab[] = {
57-
#if defined(HAVE_SSL_CTX_SET_MIN_PROTO_VERSION)
58-
#define OSSL_SSL_METHOD_ENTRY(name, version) \
59-
{ #name, TLS_method, version }, \
60-
{ #name"_server", TLS_server_method, version }, \
61-
{ #name"_client", TLS_client_method, version }
62-
#else
63-
#define OSSL_SSL_METHOD_ENTRY(name, version) \
64-
{ #name, name##_method, version }, \
65-
{ #name"_server", name##_server_method, version }, \
66-
{ #name"_client", name##_client_method, version }
67-
#endif
68-
#if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL2_METHOD) && defined(HAVE_SSLV2_METHOD)
69-
OSSL_SSL_METHOD_ENTRY(SSLv2, SSL2_VERSION),
70-
#endif
71-
#if !defined(OPENSSL_NO_SSL3) && !defined(OPENSSL_NO_SSL3_METHOD) && defined(HAVE_SSLV3_METHOD)
72-
OSSL_SSL_METHOD_ENTRY(SSLv3, SSL3_VERSION),
73-
#endif
74-
#if !defined(OPENSSL_NO_TLS1) && !defined(OPENSSL_NO_TLS1_METHOD)
75-
OSSL_SSL_METHOD_ENTRY(TLSv1, TLS1_VERSION),
76-
#endif
77-
#if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_1_METHOD)
78-
OSSL_SSL_METHOD_ENTRY(TLSv1_1, TLS1_1_VERSION),
79-
#endif
80-
#if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_TLS1_2_METHOD)
81-
OSSL_SSL_METHOD_ENTRY(TLSv1_2, TLS1_2_VERSION),
82-
#endif
83-
OSSL_SSL_METHOD_ENTRY(SSLv23, 0),
84-
#undef OSSL_SSL_METHOD_ENTRY
85-
};
86-
8749
static int ossl_ssl_ex_vcb_idx;
8850
static int ossl_ssl_ex_ptr_idx;
8951
static int ossl_sslctx_ex_ptr_idx;
@@ -148,51 +110,6 @@ ossl_sslctx_s_alloc(VALUE klass)
148110
return obj;
149111
}
150112

151-
/*
152-
* call-seq:
153-
* ctx.ssl_version = :TLSv1
154-
* ctx.ssl_version = "SSLv23_client"
155-
*
156-
* Sets the SSL/TLS protocol version for the context. This forces connections to
157-
* use only the specified protocol version.
158-
*
159-
* You can get a list of valid versions with OpenSSL::SSL::SSLContext::METHODS
160-
*/
161-
static VALUE
162-
ossl_sslctx_set_ssl_version(VALUE self, VALUE ssl_method)
163-
{
164-
SSL_CTX *ctx;
165-
const char *s;
166-
VALUE m = ssl_method;
167-
int i;
168-
169-
GetSSLCTX(self, ctx);
170-
if (RB_TYPE_P(ssl_method, T_SYMBOL))
171-
m = rb_sym2str(ssl_method);
172-
s = StringValueCStr(m);
173-
for (i = 0; i < numberof(ossl_ssl_method_tab); i++) {
174-
if (strcmp(ossl_ssl_method_tab[i].name, s) == 0) {
175-
#if defined(HAVE_SSL_CTX_SET_MIN_PROTO_VERSION)
176-
int version = ossl_ssl_method_tab[i].version;
177-
#endif
178-
const SSL_METHOD *method = ossl_ssl_method_tab[i].func();
179-
180-
if (SSL_CTX_set_ssl_version(ctx, method) != 1)
181-
ossl_raise(eSSLError, "SSL_CTX_set_ssl_version");
182-
183-
#if defined(HAVE_SSL_CTX_SET_MIN_PROTO_VERSION)
184-
if (!SSL_CTX_set_min_proto_version(ctx, version))
185-
ossl_raise(eSSLError, "SSL_CTX_set_min_proto_version");
186-
if (!SSL_CTX_set_max_proto_version(ctx, version))
187-
ossl_raise(eSSLError, "SSL_CTX_set_max_proto_version");
188-
#endif
189-
return ssl_method;
190-
}
191-
}
192-
193-
ossl_raise(rb_eArgError, "unknown SSL method `%"PRIsVALUE"'.", m);
194-
}
195-
196113
static int
197114
parse_proto_version(VALUE str)
198115
{
@@ -2333,9 +2250,6 @@ ossl_ssl_tmp_key(VALUE self)
23332250
void
23342251
Init_ossl_ssl(void)
23352252
{
2336-
int i;
2337-
VALUE ary;
2338-
23392253
#if 0
23402254
mOSSL = rb_define_module("OpenSSL");
23412255
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
@@ -2632,7 +2546,6 @@ Init_ossl_ssl(void)
26322546

26332547
rb_define_alias(cSSLContext, "ssl_timeout", "timeout");
26342548
rb_define_alias(cSSLContext, "ssl_timeout=", "timeout=");
2635-
rb_define_method(cSSLContext, "ssl_version=", ossl_sslctx_set_ssl_version, 1);
26362549
rb_define_private_method(cSSLContext, "set_minmax_proto_version",
26372550
ossl_sslctx_set_minmax_proto_version, 2);
26382551
rb_define_method(cSSLContext, "ciphers", ossl_sslctx_get_ciphers, 0);
@@ -2702,14 +2615,6 @@ Init_ossl_ssl(void)
27022615
rb_define_method(cSSLContext, "options", ossl_sslctx_get_options, 0);
27032616
rb_define_method(cSSLContext, "options=", ossl_sslctx_set_options, 1);
27042617

2705-
ary = rb_ary_new2(numberof(ossl_ssl_method_tab));
2706-
for (i = 0; i < numberof(ossl_ssl_method_tab); i++) {
2707-
rb_ary_push(ary, ID2SYM(rb_intern(ossl_ssl_method_tab[i].name)));
2708-
}
2709-
rb_obj_freeze(ary);
2710-
/* The list of available SSL/TLS methods */
2711-
rb_define_const(cSSLContext, "METHODS", ary);
2712-
27132618
/*
27142619
* Document-class: OpenSSL::SSL::SSLSocket
27152620
*/

lib/openssl/ssl.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,51 @@ def max_version=(version)
181181
set_minmax_proto_version(@min_proto_version ||= nil, version)
182182
@max_proto_version = version
183183
end
184+
185+
# call-seq:
186+
# ctx.ssl_version = :TLSv1
187+
# ctx.ssl_version = "SSLv23"
188+
#
189+
# Sets the SSL/TLS protocol version for the context. This forces
190+
# connections to use only the specified protocol version. This is
191+
# deprecated and only provided for backwards compatibility. Use
192+
# #min_version= and #max_version= instead.
193+
#
194+
# === History
195+
# As the name hints, this used to call the SSL_CTX_set_ssl_version()
196+
# function which sets the SSL method used for connections created from
197+
# the context. As of Ruby/OpenSSL 2.1, this accessor method is
198+
# implemented to call #min_version= and #max_version= instead.
199+
def ssl_version=(meth)
200+
meth = meth.to_s if meth.is_a?(Symbol)
201+
if /(?<type>_client|_server)\z/ =~ meth
202+
meth = $`
203+
if $VERBOSE
204+
warn "#{caller(1)[0]}: method type #{type.inspect} is ignored"
205+
end
206+
end
207+
version = METHODS_MAP[meth.intern] or
208+
raise ArgumentError, "unknown SSL method `%s'" % meth
209+
set_minmax_proto_version(version, version)
210+
@min_proto_version = @max_proto_version = version
211+
end
212+
213+
METHODS_MAP = {
214+
SSLv23: 0,
215+
SSLv2: OpenSSL::SSL::SSL2_VERSION,
216+
SSLv3: OpenSSL::SSL::SSL3_VERSION,
217+
TLSv1: OpenSSL::SSL::TLS1_VERSION,
218+
TLSv1_1: OpenSSL::SSL::TLS1_1_VERSION,
219+
TLSv1_2: OpenSSL::SSL::TLS1_2_VERSION,
220+
}.freeze
221+
private_constant :METHODS_MAP
222+
223+
# The list of available SSL/TLS methods. This constant is only provided
224+
# for backwards compatibility.
225+
METHODS = METHODS_MAP.flat_map { |name,|
226+
[name, :"#{name}_client", :"#{name}_server"]
227+
}.freeze
228+
deprecate_constant :METHODS
184229
end
185230

186231
module SocketForwarder

test/test_ssl.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,17 @@ def test_options_disable_versions
969969
end
970970
end
971971

972+
def test_ssl_methods_constant
973+
EnvUtil.suppress_warning { # Deprecated in v2.1.0
974+
base = [:TLSv1_2, :TLSv1_1, :TLSv1, :SSLv3, :SSLv2, :SSLv23]
975+
base.each do |name|
976+
assert_include OpenSSL::SSL::SSLContext::METHODS, name
977+
assert_include OpenSSL::SSL::SSLContext::METHODS, :"#{name}_client"
978+
assert_include OpenSSL::SSL::SSLContext::METHODS, :"#{name}_server"
979+
end
980+
}
981+
end
982+
972983
def test_renegotiation_cb
973984
num_handshakes = 0
974985
renegotiation_cb = Proc.new { |ssl| num_handshakes += 1 }

0 commit comments

Comments
 (0)