@@ -17,9 +17,9 @@ module OpenSSL
1717 module SSL
1818 class SSLContext
1919 DEFAULT_PARAMS = {
20- :ssl_version => "SSLv23" ,
21- :verify_mode => OpenSSL ::SSL ::VERIFY_PEER ,
22- :ciphers => %w{
20+ :ssl_version => "SSLv23" ,
21+ :verify_mode => OpenSSL ::SSL ::VERIFY_PEER ,
22+ :ciphers => %w{
2323 ECDHE-ECDSA-AES128-GCM-SHA256
2424 ECDHE-RSA-AES128-GCM-SHA256
2525 ECDHE-ECDSA-AES256-GCM-SHA384
@@ -54,32 +54,32 @@ class SSLContext
5454 ECDHE-RSA-RC4-SHA
5555 RC4-SHA
5656 } . join ( ":" ) ,
57- :options => -> {
58- opts = OpenSSL ::SSL ::OP_ALL
59- opts &= ~OpenSSL ::SSL ::OP_DONT_INSERT_EMPTY_FRAGMENTS if defined? ( OpenSSL ::SSL ::OP_DONT_INSERT_EMPTY_FRAGMENTS )
60- opts |= OpenSSL ::SSL ::OP_NO_COMPRESSION if defined? ( OpenSSL ::SSL ::OP_NO_COMPRESSION )
61- opts |= OpenSSL ::SSL ::OP_NO_SSLv2 if defined? ( OpenSSL ::SSL ::OP_NO_SSLv2 )
62- opts |= OpenSSL ::SSL ::OP_NO_SSLv3 if defined? ( OpenSSL ::SSL ::OP_NO_SSLv3 )
63- opts
64- } . call
65- } unless const_defined? :DEFAULT_PARAMS # JRuby does it in Java
66-
67- unless const_defined? :DEFAULT_CERT_STORE # JRuby specific
68- DEFAULT_CERT_STORE = OpenSSL ::X509 ::Store . new
69- DEFAULT_CERT_STORE . set_default_paths
70- if defined? ( OpenSSL ::X509 ::V_FLAG_CRL_CHECK_ALL )
71- DEFAULT_CERT_STORE . flags = OpenSSL ::X509 ::V_FLAG_CRL_CHECK_ALL
72- end
73- end
57+ :options => -> {
58+ opts = OpenSSL ::SSL ::OP_ALL
59+ opts &= ~OpenSSL ::SSL ::OP_DONT_INSERT_EMPTY_FRAGMENTS if defined? ( OpenSSL ::SSL ::OP_DONT_INSERT_EMPTY_FRAGMENTS )
60+ opts |= OpenSSL ::SSL ::OP_NO_COMPRESSION if defined? ( OpenSSL ::SSL ::OP_NO_COMPRESSION )
61+ opts |= OpenSSL ::SSL ::OP_NO_SSLv2 if defined? ( OpenSSL ::SSL ::OP_NO_SSLv2 )
62+ opts |= OpenSSL ::SSL ::OP_NO_SSLv3 if defined? ( OpenSSL ::SSL ::OP_NO_SSLv3 )
63+ opts
64+ } . call
65+ } unless const_defined? :DEFAULT_PARAMS # JRuby
66+
67+ begin
68+ DEFAULT_CERT_STORE = OpenSSL ::X509 ::Store . new
69+ DEFAULT_CERT_STORE . set_default_paths
70+ if defined? ( OpenSSL ::X509 ::V_FLAG_CRL_CHECK_ALL )
71+ DEFAULT_CERT_STORE . flags = OpenSSL ::X509 ::V_FLAG_CRL_CHECK_ALL
72+ end
73+ end unless const_defined? :DEFAULT_CERT_STORE # JRuby
7474
7575 INIT_VARS = [ "cert" , "key" , "client_ca" , "ca_file" , "ca_path" ,
76- "timeout" , "verify_mode" , "verify_depth" , "renegotiation_cb" ,
77- "verify_callback" , "cert_store" , "extra_chain_cert" ,
78- "client_cert_cb" , "session_id_context" , "tmp_dh_callback" ,
79- "session_get_cb" , "session_new_cb" , "session_remove_cb" ,
80- "tmp_ecdh_callback" , "servername_cb" , "npn_protocols" ,
81- "alpn_protocols" , "alpn_select_cb" ,
82- "npn_select_cb" ] . map { |x | "@#{ x } " }
76+ "timeout" , "verify_mode" , "verify_depth" , "renegotiation_cb" ,
77+ "verify_callback" , "cert_store" , "extra_chain_cert" ,
78+ "client_cert_cb" , "session_id_context" , "tmp_dh_callback" ,
79+ "session_get_cb" , "session_new_cb" , "session_remove_cb" ,
80+ "tmp_ecdh_callback" , "servername_cb" , "npn_protocols" ,
81+ "alpn_protocols" , "alpn_select_cb" ,
82+ "npn_select_cb" ] . map { |x | "@#{ x } " }
8383
8484 # A callback invoked when DH parameters are required.
8585 #
@@ -92,14 +92,14 @@ class SSLContext
9292
9393 attr_accessor :tmp_dh_callback
9494
95- # if ExtConfig::HAVE_TLSEXT_HOST_NAME
95+ if ExtConfig ::HAVE_TLSEXT_HOST_NAME
9696 # A callback invoked at connect time to distinguish between multiple
9797 # server names.
9898 #
9999 # The callback is invoked with an SSLSocket and a server name. The
100100 # callback must return an SSLContext for the server name or nil.
101101 attr_accessor :servername_cb
102- # end
102+ end
103103
104104 # call-seq:
105105 # SSLContext.new => ctx
@@ -108,9 +108,10 @@ class SSLContext
108108 #
109109 # You can get a list of valid methods with OpenSSL::SSL::SSLContext::METHODS
110110 def initialize ( version = nil )
111- self . options |= OpenSSL ::SSL ::OP_ALL
111+ INIT_VARS . each { |v | instance_variable_set v , nil }
112+ self . options = self . options | OpenSSL ::SSL ::OP_ALL
112113 self . ssl_version = version if version
113- end unless defined? JRUBY_VERSION # JRuby: handled in "native" Java
114+ end unless defined? JRUBY_VERSION # JRuby
114115
115116 ##
116117 # Sets the parameters for this SSL context to the values in +params+.
@@ -129,7 +130,7 @@ def set_params(params={})
129130 end
130131 end
131132 return params
132- end unless method_defined? :set_params # JRuby: hooked up in "native" Java
133+ end unless method_defined? :set_params # JRuby
133134 end
134135
135136 module SocketForwarder
@@ -245,61 +246,64 @@ def verify_wildcard(domain_component, san_component) # :nodoc:
245246 return false if domain_component . start_with? ( "xn--" ) && san_component != "*"
246247
247248 parts [ 0 ] . length + parts [ 1 ] . length < domain_component . length &&
248- domain_component . start_with? ( parts [ 0 ] ) &&
249- domain_component . end_with? ( parts [ 1 ] )
249+ domain_component . start_with? ( parts [ 0 ] ) &&
250+ domain_component . end_with? ( parts [ 1 ] )
250251 end
251252 module_function :verify_wildcard
252253
253254 class SSLSocket
254255 include Buffering
255256 include SocketForwarder
256257
257- if ExtConfig ::OPENSSL_NO_SOCK
258- def initialize ( io , ctx = nil ) ; raise NotImplementedError ; end
259- else
260- if ExtConfig ::HAVE_TLSEXT_HOST_NAME
261- attr_accessor :hostname
262- end
263-
264- attr_reader :io , :context
265- attr_accessor :sync_close
266- alias :to_io :io
267-
268- # call-seq:
269- # SSLSocket.new(io) => aSSLSocket
270- # SSLSocket.new(io, ctx) => aSSLSocket
271- #
272- # Creates a new SSL socket from +io+ which must be a real ruby object (not an
273- # IO-like object that responds to read/write).
274- #
275- # If +ctx+ is provided the SSL Sockets initial params will be taken from
276- # the context.
277- #
278- # The OpenSSL::Buffering module provides additional IO methods.
279- #
280- # This method will freeze the SSLContext if one is provided;
281- # however, session management is still allowed in the frozen SSLContext.
282-
283- def initialize ( io , context = OpenSSL ::SSL ::SSLContext . new )
284- @io = io
285- @context = context
286- @sync_close = false
287- @hostname = nil
288- @io . nonblock = true if @io . respond_to? ( :nonblock= )
289- context . setup
290- super ( )
291- end
292- end unless defined? JRUBY_VERSION # JRuby: handled in "native" Java
258+ # if ExtConfig::OPENSSL_NO_SOCK
259+ # def initialize(io, ctx = nil); raise NotImplementedError; end
260+ # else
261+ # if ExtConfig::HAVE_TLSEXT_HOST_NAME
262+ # attr_accessor :hostname
263+ # end
264+ #
265+ # attr_reader :io, :context
266+ # attr_accessor :sync_close
267+ # alias :to_io :io
268+ #
269+ # # call-seq:
270+ # # SSLSocket.new(io) => aSSLSocket
271+ # # SSLSocket.new(io, ctx) => aSSLSocket
272+ # #
273+ # # Creates a new SSL socket from +io+ which must be a real ruby object (not an
274+ # # IO-like object that responds to read/write).
275+ # #
276+ # # If +ctx+ is provided the SSL Sockets initial params will be taken from
277+ # # the context.
278+ # #
279+ # # The OpenSSL::Buffering module provides additional IO methods.
280+ # #
281+ # # This method will freeze the SSLContext if one is provided;
282+ # # however, session management is still allowed in the frozen SSLContext.
283+ #
284+ # def initialize(io, context = OpenSSL::SSL::SSLContext.new)
285+ # @io = io
286+ # @context = context
287+ # @sync_close = false
288+ # @hostname = nil
289+ # @io.nonblock = true if @io.respond_to?(:nonblock=)
290+ # context.setup
291+ # super()
292+ # end
293+ # end
293294
294295 # call-seq:
295296 # ssl.sysclose => nil
296297 #
297- # Shuts down the SSL connection and prepares it for another connection.
298+ # Sends "close notify" to the peer and tries to shut down the SSL
299+ # connection gracefully.
300+ #
301+ # If sync_close is set to +true+, the underlying IO is also closed.
298302 def sysclose
299303 return if closed?
300304 stop
301305 io . close if sync_close
302- end unless defined? JRUBY_VERSION # JRuby: handled in "native" Java
306+ end unless method_defined? :sysclose # JRuby
303307
304308 ##
305309 # Perform hostname verification after an SSL connection is established
@@ -321,11 +325,11 @@ def post_connection_check(hostname)
321325 return true
322326 end
323327
324- # def session
325- # SSL::Session.new(self)
326- # rescue SSL::Session::SessionError
327- # nil
328- # end
328+ def session
329+ SSL ::Session . new ( self )
330+ rescue SSL ::Session ::SessionError
331+ nil
332+ end unless method_defined? :session # JRuby
329333
330334 private
331335
0 commit comments