forked from nahi/httpclient
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_ssl.rb
342 lines (314 loc) · 10.1 KB
/
test_ssl.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
require File.expand_path('helper', File.dirname(__FILE__))
require 'webrick/https'
class TestSSL < Test::Unit::TestCase
include Helper
DIR = File.dirname(File.expand_path(__FILE__))
def setup
super
@serverpid = @client = nil
@verify_callback_called = false
@verbose, $VERBOSE = $VERBOSE, nil
setup_server
setup_client
@url = "https://localhost:#{serverport}/hello"
end
def teardown
super
$VERBOSE = @verbose
end
def path(filename)
File.expand_path(filename, DIR)
end
def test_proxy_ssl
setup_proxyserver
escape_noproxy do
@client.proxy = proxyurl
@client.ssl_config.set_client_cert_file(path('client.cert'), path('client.key'))
@client.ssl_config.add_trust_ca(path('ca.cert'))
@client.ssl_config.add_trust_ca(path('subca.cert'))
@client.debug_dev = str = ""
assert_equal(200, @client.get(@url).status)
assert(/accept/ =~ @proxyio.string, 'proxy is not used')
assert(/Host: localhost:#{serverport}/ =~ str)
end
end
def test_options
cfg = @client.ssl_config
assert_nil(cfg.client_cert)
assert_nil(cfg.client_key)
assert_nil(cfg.client_ca)
assert_equal(OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT, cfg.verify_mode)
assert_nil(cfg.verify_callback)
assert_nil(cfg.timeout)
expected_options = OpenSSL::SSL::OP_ALL | OpenSSL::SSL::OP_NO_SSLv2 | OpenSSL::SSL::OP_NO_SSLv3
expected_options &= ~OpenSSL::SSL::OP_DONT_INSERT_EMPTY_FRAGMENTS if defined?(OpenSSL::SSL::OP_DONT_INSERT_EMPTY_FRAGMENTS)
expected_options |= OpenSSL::SSL::OP_NO_COMPRESSION if defined?(OpenSSL::SSL::OP_NO_COMPRESSION)
assert_equal(expected_options, cfg.options)
assert_equal("ALL:!aNULL:!eNULL:!SSLv2", cfg.ciphers)
assert_instance_of(OpenSSL::X509::Store, cfg.cert_store)
end
unless defined?(HTTPClient::JRubySSLSocket)
# JRubySSLSocket does not support sync mode.
def test_sync
cfg = @client.ssl_config
cfg.set_client_cert_file(path('client.cert'), path('client.key'))
cfg.add_trust_ca(path('ca.cert'))
cfg.add_trust_ca(path('subca.cert'))
assert_equal("hello", @client.get_content(@url))
@client.socket_sync = false
@client.reset_all
assert_equal("hello", @client.get_content(@url))
end
end
def test_debug_dev
str = @client.debug_dev = ''
cfg = @client.ssl_config
cfg.client_cert = path("client.cert")
cfg.client_key = path("client.key")
cfg.add_trust_ca(path('ca.cert'))
cfg.add_trust_ca(path('subca.cert'))
assert_equal("hello", @client.get_content(@url))
assert(str.scan(/^hello$/)[0])
end
def test_verification
cfg = @client.ssl_config
cfg.verify_callback = method(:verify_callback).to_proc
begin
@verify_callback_called = false
@client.get(@url)
assert(false)
rescue OpenSSL::SSL::SSLError => ssle
assert_match(/(certificate verify failed|unable to find valid certification path to requested target)/, ssle.message)
assert(@verify_callback_called)
end
#
cfg.client_cert = path("client.cert")
cfg.client_key = path("client.key")
@verify_callback_called = false
begin
@client.get(@url)
assert(false)
rescue OpenSSL::SSL::SSLError => ssle
assert_match(/(certificate verify failed|unable to find valid certification path to requested target)/, ssle.message)
assert(@verify_callback_called)
end
#
cfg.add_trust_ca(path('ca.cert'))
@verify_callback_called = false
begin
@client.get(@url)
assert(false)
rescue OpenSSL::SSL::SSLError => ssle
assert_match(/(certificate verify failed|unable to find valid certification path to requested target)/, ssle.message)
assert(@verify_callback_called)
end
#
cfg.add_trust_ca(path('subca.cert'))
@verify_callback_called = false
assert_equal("hello", @client.get_content(@url))
assert(@verify_callback_called)
#
if false
# JRubySSLSocket does not support depth.
# Also on travis environment, verify_depth seems to not work properly.
cfg.verify_depth = 1 # 2 required: root-sub
@verify_callback_called = false
begin
@client.get(@url)
assert(false, "verify_depth is not supported? #{OpenSSL::OPENSSL_VERSION}")
rescue OpenSSL::SSL::SSLError => ssle
assert_match(/(certificate verify failed|unable to find valid certification path to requested target)/, ssle.message)
assert(@verify_callback_called)
end
#
cfg.verify_depth = 2 # 2 required: root-sub
@verify_callback_called = false
@client.get(@url)
assert(@verify_callback_called)
#
end
cfg.verify_depth = nil
cfg.cert_store = OpenSSL::X509::Store.new
cfg.verify_mode = OpenSSL::SSL::VERIFY_PEER
begin
@client.get_content(@url)
assert(false)
rescue OpenSSL::SSL::SSLError => ssle
assert_match(/(certificate verify failed|unable to find valid certification path to requested target)/, ssle.message)
end
#
cfg.verify_mode = nil
assert_equal("hello", @client.get_content(@url))
end
if defined?(HTTPClient::JRubySSLSocket)
def test_ciphers
cfg = @client.ssl_config
cfg.set_client_cert_file(path('client.cert'), path('client-pass.key'), 'pass4key')
cfg.add_trust_ca(path('ca.cert'))
cfg.add_trust_ca(path('subca.cert'))
cfg.timeout = 123
assert_equal("hello", @client.get_content(@url))
#
cfg.ciphers = []
begin
@client.get(@url)
assert(false)
rescue OpenSSL::SSL::SSLError => ssle
assert_match(/No appropriate protocol/, ssle.message)
end
#
cfg.ciphers = %w(TLS_RSA_WITH_AES_128_CBC_SHA)
assert_equal("hello", @client.get_content(@url))
#
cfg.ciphers = HTTPClient::SSLConfig::CIPHERS_DEFAULT
assert_equal("hello", @client.get_content(@url))
end
else
def test_ciphers
cfg = @client.ssl_config
cfg.set_client_cert_file(path('client.cert'), path('client-pass.key'), 'pass4key')
cfg.add_trust_ca(path('ca.cert'))
cfg.add_trust_ca(path('subca.cert'))
cfg.timeout = 123
assert_equal("hello", @client.get_content(@url))
#
cfg.ciphers = "!ALL"
begin
@client.get(@url)
assert(false)
rescue OpenSSL::SSL::SSLError => ssle
assert_match(/no cipher match/, ssle.message)
end
#
cfg.ciphers = "ALL"
assert_equal("hello", @client.get_content(@url))
#
cfg.ciphers = "DEFAULT"
assert_equal("hello", @client.get_content(@url))
end
end
# SSL_CERT_FILE does not work with recent jruby-openssl.
# You should not depend on SSL_CERT_FILE on JRuby
if !defined?(JRUBY_VERSION)
def test_set_default_paths
assert_raise(OpenSSL::SSL::SSLError) do
@client.get(@url)
end
escape_env do
ENV['SSL_CERT_FILE'] = File.join(DIR, 'ca-chain.pem')
@client.ssl_config.set_default_paths
@client.get(@url)
end
end
end
def test_no_sslv3
teardown_server
setup_server_with_ssl_version(:SSLv3)
assert_raise(OpenSSL::SSL::SSLError) do
@client.ssl_config.verify_mode = nil
@client.get("https://localhost:#{serverport}/hello")
end
end
def test_allow_tlsv1
teardown_server
setup_server_with_ssl_version(:TLSv1)
assert_nothing_raised do
@client.ssl_config.verify_mode = nil
@client.get("https://localhost:#{serverport}/hello")
end
end
def test_use_higher_TLS
omit('TODO: it does not pass with Java 7 or old openssl ')
teardown_server
setup_server_with_ssl_version(:TLSv1_2)
assert_nothing_raised do
@client.ssl_config.verify_mode = nil
@client.get("https://localhost:#{serverport}/hello")
# TODO: should check JRubySSLSocket.ssl_socket.getSession.getProtocol
# but it's not thread safe. How can I return protocol version to the caller?
end
end
private
def cert(filename)
OpenSSL::X509::Certificate.new(File.read(File.join(DIR, filename)))
end
def key(filename)
OpenSSL::PKey::RSA.new(File.read(File.join(DIR, filename)))
end
def q(str)
%Q["#{str}"]
end
def setup_server
logger = Logger.new(STDERR)
logger.level = Logger::Severity::FATAL # avoid logging SSLError (ERROR level)
@server = WEBrick::HTTPServer.new(
:BindAddress => "localhost",
:Logger => logger,
:Port => 0,
:AccessLog => [],
:DocumentRoot => DIR,
:SSLEnable => true,
:SSLCACertificateFile => File.join(DIR, 'ca.cert'),
:SSLCertificate => cert('server.cert'),
:SSLPrivateKey => key('server.key'),
:SSLVerifyClient => nil, #OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT|OpenSSL::SSL::VERIFY_PEER,
:SSLClientCA => cert('ca.cert'),
:SSLCertName => nil
)
@serverport = @server.config[:Port]
[:hello].each do |sym|
@server.mount(
"/#{sym}",
WEBrick::HTTPServlet::ProcHandler.new(method("do_#{sym}").to_proc)
)
end
@server_thread = start_server_thread(@server)
end
def setup_server_with_ssl_version(ssl_version)
logger = Logger.new(STDERR)
logger.level = Logger::Severity::FATAL # avoid logging SSLError (ERROR level)
@server = WEBrick::HTTPServer.new(
:BindAddress => "localhost",
:Logger => logger,
:Port => 0,
:AccessLog => [],
:DocumentRoot => DIR,
:SSLEnable => true,
:SSLCACertificateFile => File.join(DIR, 'ca.cert'),
:SSLCertificate => cert('server.cert'),
:SSLPrivateKey => key('server.key')
)
@server.ssl_context.ssl_version = ssl_version
@serverport = @server.config[:Port]
[:hello].each do |sym|
@server.mount(
"/#{sym}",
WEBrick::HTTPServlet::ProcHandler.new(method("do_#{sym}").to_proc)
)
end
@server_thread = start_server_thread(@server)
end
def do_hello(req, res)
res['content-type'] = 'text/html'
res.body = "hello"
end
def start_server_thread(server)
t = Thread.new {
Thread.current.abort_on_exception = true
server.start
}
while server.status != :Running
sleep 0.1
unless t.alive?
t.join
raise
end
end
t
end
def verify_callback(ok, cert)
@verify_callback_called = true
p ["client", ok, cert] if $DEBUG
ok
end
end