@@ -17,9 +17,9 @@ module OpenSSL
17
17
module SSL
18
18
class SSLContext
19
19
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{
23
23
ECDHE-ECDSA-AES128-GCM-SHA256
24
24
ECDHE-RSA-AES128-GCM-SHA256
25
25
ECDHE-ECDSA-AES256-GCM-SHA384
@@ -54,32 +54,32 @@ class SSLContext
54
54
ECDHE-RSA-RC4-SHA
55
55
RC4-SHA
56
56
} . 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
74
74
75
75
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 } " }
83
83
84
84
# A callback invoked when DH parameters are required.
85
85
#
@@ -92,14 +92,14 @@ class SSLContext
92
92
93
93
attr_accessor :tmp_dh_callback
94
94
95
- # if ExtConfig::HAVE_TLSEXT_HOST_NAME
95
+ if ExtConfig ::HAVE_TLSEXT_HOST_NAME
96
96
# A callback invoked at connect time to distinguish between multiple
97
97
# server names.
98
98
#
99
99
# The callback is invoked with an SSLSocket and a server name. The
100
100
# callback must return an SSLContext for the server name or nil.
101
101
attr_accessor :servername_cb
102
- # end
102
+ end
103
103
104
104
# call-seq:
105
105
# SSLContext.new => ctx
@@ -108,9 +108,10 @@ class SSLContext
108
108
#
109
109
# You can get a list of valid methods with OpenSSL::SSL::SSLContext::METHODS
110
110
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
112
113
self . ssl_version = version if version
113
- end unless defined? JRUBY_VERSION # JRuby: handled in "native" Java
114
+ end unless defined? JRUBY_VERSION # JRuby
114
115
115
116
##
116
117
# Sets the parameters for this SSL context to the values in +params+.
@@ -129,7 +130,7 @@ def set_params(params={})
129
130
end
130
131
end
131
132
return params
132
- end unless method_defined? :set_params # JRuby: hooked up in "native" Java
133
+ end unless method_defined? :set_params # JRuby
133
134
end
134
135
135
136
module SocketForwarder
@@ -245,61 +246,64 @@ def verify_wildcard(domain_component, san_component) # :nodoc:
245
246
return false if domain_component . start_with? ( "xn--" ) && san_component != "*"
246
247
247
248
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 ] )
250
251
end
251
252
module_function :verify_wildcard
252
253
253
254
class SSLSocket
254
255
include Buffering
255
256
include SocketForwarder
256
257
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
293
294
294
295
# call-seq:
295
296
# ssl.sysclose => nil
296
297
#
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.
298
302
def sysclose
299
303
return if closed?
300
304
stop
301
305
io . close if sync_close
302
- end unless defined? JRUBY_VERSION # JRuby: handled in "native" Java
306
+ end unless method_defined? :sysclose # JRuby
303
307
304
308
##
305
309
# Perform hostname verification after an SSL connection is established
@@ -321,11 +325,11 @@ def post_connection_check(hostname)
321
325
return true
322
326
end
323
327
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
329
333
330
334
private
331
335
0 commit comments