Skip to content

Commit 588f414

Browse files
committed
[compat] working SSLContext#options attr
1 parent 759ceb2 commit 588f414

File tree

2 files changed

+50
-39
lines changed

2 files changed

+50
-39
lines changed

lib/openssl/ssl.rb

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,15 @@ class SSLContext
132132
# used.
133133
def set_params(params={})
134134
params = DEFAULT_PARAMS.merge(params)
135-
# TODO JRuby: need to support SSLContext#options (since Ruby 2.5)
136-
#self.options = params.delete(:options) # set before min_version/max_version
137-
params.each { |name, value| self.__send__("#{name}=", value) }
135+
self.options = params.delete(:options) # set before min_version/max_version
136+
params.each{|name, value| self.__send__("#{name}=", value) }
138137
if self.verify_mode != OpenSSL::SSL::VERIFY_NONE
139138
unless self.ca_file or self.ca_path or self.cert_store
140139
self.cert_store = DEFAULT_CERT_STORE
141140
end
142141
end
143142
return params
144-
end unless method_defined? :set_params
143+
end
145144

146145
# call-seq:
147146
# ctx.min_version = OpenSSL::SSL::TLS1_2_VERSION
@@ -194,29 +193,29 @@ def max_version=(version)
194193
# function which sets the SSL method used for connections created from
195194
# the context. As of Ruby/OpenSSL 2.1, this accessor method is
196195
# implemented to call #min_version= and #max_version= instead.
197-
def ssl_version=(meth)
198-
meth = meth.to_s if meth.is_a?(Symbol)
199-
if /(?<type>_client|_server)\z/ =~ meth
200-
meth = $`
201-
if $VERBOSE
202-
warn "#{caller(1, 1)[0]}: method type #{type.inspect} is ignored"
203-
end
204-
end
205-
version = METHODS_MAP[meth.intern] or
206-
raise ArgumentError, "unknown SSL method `%s'" % meth
207-
set_minmax_proto_version(version, version)
208-
@min_proto_version = @max_proto_version = version
209-
end unless method_defined? :ssl_version=
210-
211-
METHODS_MAP = {
212-
SSLv23: 0,
213-
SSLv2: OpenSSL::SSL::SSL2_VERSION,
214-
SSLv3: OpenSSL::SSL::SSL3_VERSION,
215-
TLSv1: OpenSSL::SSL::TLS1_VERSION,
216-
TLSv1_1: OpenSSL::SSL::TLS1_1_VERSION,
217-
TLSv1_2: OpenSSL::SSL::TLS1_2_VERSION,
218-
}.freeze
219-
private_constant :METHODS_MAP
196+
# def ssl_version=(meth)
197+
# meth = meth.to_s if meth.is_a?(Symbol)
198+
# if /(?<type>_client|_server)\z/ =~ meth
199+
# meth = $`
200+
# if $VERBOSE
201+
# warn "#{caller(1, 1)[0]}: method type #{type.inspect} is ignored"
202+
# end
203+
# end
204+
# version = METHODS_MAP[meth.intern] or
205+
# raise ArgumentError, "unknown SSL method `%s'" % meth
206+
# set_minmax_proto_version(version, version)
207+
# @min_proto_version = @max_proto_version = version
208+
# end
209+
#
210+
# METHODS_MAP = {
211+
# SSLv23: 0,
212+
# SSLv2: OpenSSL::SSL::SSL2_VERSION,
213+
# SSLv3: OpenSSL::SSL::SSL3_VERSION,
214+
# TLSv1: OpenSSL::SSL::TLS1_VERSION,
215+
# TLSv1_1: OpenSSL::SSL::TLS1_1_VERSION,
216+
# TLSv1_2: OpenSSL::SSL::TLS1_2_VERSION,
217+
# }.freeze
218+
# private_constant :METHODS_MAP
220219

221220
# METHODS setup from native (JRuby)
222221
# The list of available SSL/TLS methods. This constant is only provided

src/main/java/org/jruby/ext/openssl/SSLContext.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ public static void createSSLContext(final Ruby runtime, final RubyModule SSL) {
192192
SSLContext.addReadWriteAttribute(context, "verify_mode");
193193
SSLContext.addReadWriteAttribute(context, "verify_depth");
194194
SSLContext.addReadWriteAttribute(context, "verify_callback");
195-
SSLContext.addReadWriteAttribute(context, "options");
196195
SSLContext.addReadWriteAttribute(context, "cert_store");
197196
SSLContext.addReadWriteAttribute(context, "extra_chain_cert");
198197
SSLContext.addReadWriteAttribute(context, "client_cert_cb");
@@ -245,6 +244,8 @@ public SSLContext(Ruby runtime, RubyClass type) {
245244
super(runtime, _SSLContext(runtime));
246245
}
247246

247+
private long options = OP_ALL;
248+
248249
//private transient CipherStrings.Def cipher_list;
249250
/* same as above but sorted for lookup */
250251
//private transient CipherStrings.Def cipher_list_by_id;
@@ -269,17 +270,16 @@ public SSLContext(Ruby runtime, RubyClass type) {
269270

270271
@JRubyMethod(required = 0, optional = 1, visibility = Visibility.PRIVATE)
271272
public IRubyObject initialize(IRubyObject[] args) {
272-
if ( args.length > 0 ) set_ssl_version(args[0]);
273+
assert this.options == OP_ALL; // self.options |= OpenSSL::SSL::OP_ALL
274+
if ( args.length > 0 ) set_ssl_version(args[0]); // self.ssl_version = version if version
273275
return initializeImpl();
274276
}
275277

276-
@Override
278+
@Override // NOTE: instance variables (no internal state) on #dup
277279
public IRubyObject initialize_copy(IRubyObject original) {
278-
return super.initialize_copy(original);
279-
// NOTE: only instance variables (no internal state) on #dup
280-
// final SSLContext that = (SSLContext) original;
281-
// this.ciphers = that.ciphers;
282-
// return this;
280+
SSLContext copy = (SSLContext) super.initialize_copy(original);
281+
copy.options = ((SSLContext) original).options;
282+
return copy;
283283
}
284284

285285
final SSLContext initializeImpl() { return this; }
@@ -787,11 +787,23 @@ private String getCaPath() {
787787
}
788788

789789
private long getOptions() {
790-
IRubyObject options = getInstanceVariable("@options");
791-
if ( options != null && ! options.isNil() ) {
792-
return RubyNumeric.fix2long(options);
790+
return options;
791+
}
792+
793+
@JRubyMethod
794+
public RubyInteger options(ThreadContext context) {
795+
return context.runtime.newFixnum(getOptions());
796+
}
797+
798+
@JRubyMethod(name = "options=")
799+
public IRubyObject options_set(final IRubyObject options) {
800+
if (options.isNil()) {
801+
this.options = OP_ALL;
802+
} else {
803+
this.options = RubyNumeric.num2long(options);
793804
}
794-
return 0;
805+
806+
return this;
795807
}
796808

797809
private static List<X509AuxCertificate> convertToAuxCerts(final ThreadContext context, IRubyObject value) {

0 commit comments

Comments
 (0)