PageRenderTime 63ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.8/openssl/test_ssl.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 1032 lines | 893 code | 108 blank | 31 comment | 22 complexity | 563258e3e2b1fa14e442895f345b0811 MD5 | raw file
  1. begin
  2. require "openssl"
  3. require File.join(File.dirname(__FILE__), "utils.rb")
  4. rescue LoadError
  5. end
  6. require "rbconfig"
  7. require "socket"
  8. require "test/unit"
  9. require 'tempfile'
  10. if defined?(OpenSSL)
  11. class OpenSSL::TestSSL < Test::Unit::TestCase
  12. RUBY = ENV["RUBY"] || File.join(
  13. ::Config::CONFIG["bindir"],
  14. ::Config::CONFIG["ruby_install_name"] + ::Config::CONFIG["EXEEXT"]
  15. )
  16. SSL_SERVER = File.join(File.dirname(__FILE__), "ssl_server.rb")
  17. PORT = 20443
  18. ITERATIONS = ($0 == __FILE__) ? 100 : 10
  19. # NOT USED: Disable in-proc process launching and either run jruby with
  20. # specified args or yield args to a given block
  21. def jruby_oop(*args)
  22. prev_in_process = JRuby.runtime.instance_config.run_ruby_in_process
  23. JRuby.runtime.instance_config.run_ruby_in_process = false
  24. if block_given?
  25. yield args
  26. else
  27. `#{RUBY} #{args.join(' ')}`
  28. end
  29. ensure
  30. JRuby.runtime.instance_config.run_ruby_in_process = prev_in_process
  31. end
  32. def setup
  33. @ca_key = OpenSSL::TestUtils::TEST_KEY_RSA2048
  34. @svr_key = OpenSSL::TestUtils::TEST_KEY_RSA1024
  35. @cli_key = OpenSSL::TestUtils::TEST_KEY_DSA256
  36. @ca = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=CA")
  37. @svr = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=localhost")
  38. @cli = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=localhost")
  39. now = Time.at(Time.now.to_i)
  40. ca_exts = [
  41. ["basicConstraints","CA:TRUE",true],
  42. ["keyUsage","cRLSign,keyCertSign",true],
  43. ]
  44. ee_exts = [
  45. ["keyUsage","keyEncipherment,digitalSignature",true],
  46. ]
  47. @ca_cert = issue_cert(@ca, @ca_key, 1, now, now+3600, ca_exts,
  48. nil, nil, OpenSSL::Digest::SHA1.new)
  49. @svr_cert = issue_cert(@svr, @svr_key, 2, now, now+1800, ee_exts,
  50. @ca_cert, @ca_key, OpenSSL::Digest::SHA1.new)
  51. @cli_cert = issue_cert(@cli, @cli_key, 3, now, now+1800, ee_exts,
  52. @ca_cert, @ca_key, OpenSSL::Digest::SHA1.new)
  53. @server = nil
  54. end
  55. def teardown
  56. end
  57. def issue_cert(*arg)
  58. OpenSSL::TestUtils.issue_cert(*arg)
  59. end
  60. def issue_crl(*arg)
  61. OpenSSL::TestUtils.issue_crl(*arg)
  62. end
  63. def choose_port(port)
  64. tcps = nil
  65. 100.times{ |i|
  66. begin
  67. tcps = TCPServer.new("127.0.0.1", port+i)
  68. port = port + i
  69. break
  70. rescue Errno::EADDRINUSE
  71. next
  72. end
  73. }
  74. return tcps, port
  75. end
  76. def readwrite_loop(ctx, ssl)
  77. while line = ssl.gets
  78. if line =~ /^STARTTLS$/
  79. ssl.accept
  80. next
  81. end
  82. ssl.write(line)
  83. end
  84. rescue OpenSSL::SSL::SSLError
  85. rescue IOError
  86. ensure
  87. ssl.close rescue nil
  88. end
  89. def server_loop(ctx, ssls, server_proc)
  90. loop do
  91. ssl = nil
  92. begin
  93. ssl = ssls.accept
  94. rescue OpenSSL::SSL::SSLError
  95. retry
  96. end
  97. Thread.start do
  98. Thread.current.abort_on_exception = true
  99. server_proc.call(ctx, ssl)
  100. end
  101. end
  102. rescue Errno::EBADF, IOError, Errno::EINVAL, Errno::ECONNABORTED
  103. end
  104. def start_server(port0, verify_mode, start_immediately, args = {}, &block)
  105. ctx_proc = args[:ctx_proc]
  106. server_proc = args[:server_proc]
  107. server_proc ||= method(:readwrite_loop)
  108. store = OpenSSL::X509::Store.new
  109. store.add_cert(@ca_cert)
  110. store.purpose = OpenSSL::X509::PURPOSE_SSL_CLIENT
  111. ctx = OpenSSL::SSL::SSLContext.new
  112. ctx.cert_store = store
  113. #ctx.extra_chain_cert = [ ca_cert ]
  114. ctx.cert = @svr_cert
  115. ctx.key = @svr_key
  116. ctx.verify_mode = verify_mode
  117. ctx_proc.call(ctx) if ctx_proc
  118. Socket.do_not_reverse_lookup = true
  119. tcps, port = choose_port(port0)
  120. ssls = OpenSSL::SSL::SSLServer.new(tcps, ctx)
  121. ssls.start_immediately = start_immediately
  122. begin
  123. server = Thread.new do
  124. Thread.current.abort_on_exception = true
  125. server_loop(ctx, ssls, server_proc)
  126. end
  127. $stderr.printf("%s started: pid=%d port=%d\n", SSL_SERVER, $$, port) if $DEBUG
  128. block.call(server, port.to_i)
  129. ensure
  130. tcps.close if (tcps)
  131. if (server)
  132. server.join(5)
  133. if server.alive?
  134. server.kill
  135. server.join
  136. flunk("TCPServer was closed and SSLServer is still alive") unless $!
  137. end
  138. end
  139. end
  140. end
  141. def starttls(ssl)
  142. ssl.puts("STARTTLS")
  143. sleep 1 # When this line is eliminated, process on Cygwin blocks
  144. # forever at ssl.connect. But I don't know why it does.
  145. ssl.connect
  146. end
  147. def test_ctx_setup
  148. ctx = OpenSSL::SSL::SSLContext.new
  149. assert_equal(ctx.setup, true)
  150. assert_equal(ctx.setup, nil)
  151. end
  152. def test_connect_and_close
  153. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
  154. sock = TCPSocket.new("127.0.0.1", port)
  155. ssl = OpenSSL::SSL::SSLSocket.new(sock)
  156. assert(ssl.connect)
  157. ssl.close
  158. assert(!sock.closed?)
  159. sock.close
  160. sock = TCPSocket.new("127.0.0.1", port)
  161. ssl = OpenSSL::SSL::SSLSocket.new(sock)
  162. ssl.sync_close = true # !!
  163. assert(ssl.connect)
  164. ssl.close
  165. assert(sock.closed?)
  166. }
  167. end
  168. def test_read_and_write
  169. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
  170. sock = TCPSocket.new("127.0.0.1", port)
  171. ssl = OpenSSL::SSL::SSLSocket.new(sock)
  172. ssl.sync_close = true
  173. ssl.connect
  174. assert_raise(ArgumentError) { ssl.sysread(-1) }
  175. # puts and gets
  176. ITERATIONS.times{
  177. str = "x" * 100 + "\n"
  178. ssl.puts(str)
  179. assert_equal(str, ssl.gets)
  180. }
  181. # read and write
  182. ITERATIONS.times{|i|
  183. str = "x" * 100 + "\n"
  184. ssl.write(str)
  185. assert_equal(str, ssl.read(str.size))
  186. str = "x" * i * 100 + "\n"
  187. buf = ""
  188. ssl.write(str)
  189. assert_equal(buf.object_id, ssl.read(str.size, buf).object_id)
  190. assert_equal(str, buf)
  191. }
  192. ssl.close
  193. }
  194. end
  195. def sysread_size(ssl, size)
  196. buf = ''
  197. while buf.bytesize < size
  198. buf += ssl.sysread(size - buf.bytesize)
  199. end
  200. buf
  201. end
  202. def test_sysread_chunks
  203. args = {}
  204. args[:server_proc] = proc { |ctx, ssl|
  205. while line = ssl.gets
  206. if line =~ /^STARTTLS$/
  207. ssl.accept
  208. next
  209. end
  210. ssl.write("0" * 800)
  211. ssl.write("1" * 200)
  212. ssl.close
  213. break
  214. end
  215. }
  216. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true, args){|server, port|
  217. sock = TCPSocket.new("127.0.0.1", port)
  218. ssl = OpenSSL::SSL::SSLSocket.new(sock)
  219. ssl.sync_close = true
  220. ssl.connect
  221. ssl.syswrite("hello\n")
  222. assert_equal("0" * 200, sysread_size(ssl, 200))
  223. assert_equal("0" * 200, sysread_size(ssl, 200))
  224. assert_equal("0" * 200, sysread_size(ssl, 200))
  225. assert_equal("0" * 200, sysread_size(ssl, 200))
  226. assert_equal("1" * 200, sysread_size(ssl, 200))
  227. ssl.close
  228. }
  229. end
  230. def test_sysread_buffer
  231. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
  232. sock = TCPSocket.new("127.0.0.1", port)
  233. ssl = OpenSSL::SSL::SSLSocket.new(sock)
  234. ssl.sync_close = true
  235. ssl.connect
  236. ITERATIONS.times{|i|
  237. # the given buffer is cleared before concatenating.
  238. # NB: SSLSocket#readpartial depends sysread.
  239. str = "x" * i * 100 + "\n"
  240. ssl.syswrite(str)
  241. buf = "asdf"
  242. assert_equal(buf.object_id, ssl.sysread(0, buf).object_id)
  243. assert_equal("", buf)
  244. buf = "asdf"
  245. read = ssl.sysread(str.size, buf)
  246. assert(!read.empty?)
  247. assert_equal(buf.object_id, read.object_id)
  248. assert_equal(str[0, buf.bytesize], buf)
  249. sysread_size(ssl, str.bytesize - buf.bytesize) # drop unread bytes
  250. ssl.syswrite(str)
  251. read = ssl.sysread(str.size, nil)
  252. assert(!read.empty?)
  253. assert_equal(str[0, read.bytesize], read)
  254. sysread_size(ssl, str.bytesize - read.bytesize) # drop unread bytes
  255. }
  256. ssl.close
  257. }
  258. end
  259. def test_client_auth
  260. vflag = OpenSSL::SSL::VERIFY_PEER|OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
  261. start_server(PORT, vflag, true){|server, port|
  262. assert_raise(OpenSSL::SSL::SSLError){
  263. sock = TCPSocket.new("127.0.0.1", port)
  264. ssl = OpenSSL::SSL::SSLSocket.new(sock)
  265. ssl.connect
  266. }
  267. ctx = OpenSSL::SSL::SSLContext.new
  268. ctx.key = @cli_key
  269. ctx.cert = @cli_cert
  270. sock = TCPSocket.new("127.0.0.1", port)
  271. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  272. ssl.sync_close = true
  273. ssl.connect
  274. ssl.puts("foo")
  275. assert_equal("foo\n", ssl.gets)
  276. ssl.close
  277. called = nil
  278. ctx = OpenSSL::SSL::SSLContext.new
  279. ctx.client_cert_cb = Proc.new{ |sslconn|
  280. called = true
  281. [@cli_cert, @cli_key]
  282. }
  283. sock = TCPSocket.new("127.0.0.1", port)
  284. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  285. ssl.sync_close = true
  286. ssl.connect
  287. assert(called)
  288. ssl.puts("foo")
  289. assert_equal("foo\n", ssl.gets)
  290. ssl.close
  291. }
  292. end
  293. def test_client_auth_with_server_store
  294. vflag = OpenSSL::SSL::VERIFY_PEER
  295. localcacert_file = Tempfile.open("cafile")
  296. localcacert_file << @ca_cert.to_pem
  297. localcacert_file.close
  298. localcacert_path = localcacert_file.path
  299. ssl_store = OpenSSL::X509::Store.new
  300. ssl_store.purpose = OpenSSL::X509::PURPOSE_ANY
  301. ssl_store.add_file(localcacert_path)
  302. args = {}
  303. args[:ctx_proc] = proc { |server_ctx|
  304. server_ctx.cert = @svr_cert
  305. server_ctx.key = @svr_key
  306. server_ctx.verify_mode = vflag
  307. server_ctx.cert_store = ssl_store
  308. }
  309. start_server(PORT, vflag, true, args){|server, port|
  310. ctx = OpenSSL::SSL::SSLContext.new
  311. ctx.cert = @cli_cert
  312. ctx.key = @cli_key
  313. sock = TCPSocket.new("127.0.0.1", port)
  314. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  315. ssl.sync_close = true
  316. ssl.connect
  317. ssl.puts("foo")
  318. assert_equal("foo\n", ssl.gets)
  319. ssl.close
  320. localcacert_file.unlink
  321. }
  322. end
  323. def test_client_crl_with_server_store
  324. vflag = OpenSSL::SSL::VERIFY_PEER
  325. localcacert_file = Tempfile.open("cafile")
  326. localcacert_file << @ca_cert.to_pem
  327. localcacert_file.close
  328. localcacert_path = localcacert_file.path
  329. ssl_store = OpenSSL::X509::Store.new
  330. ssl_store.purpose = OpenSSL::X509::PURPOSE_ANY
  331. ssl_store.add_file(localcacert_path)
  332. ssl_store.flags = OpenSSL::X509::V_FLAG_CRL_CHECK_ALL|OpenSSL::X509::V_FLAG_CRL_CHECK
  333. crl = issue_crl([], 1, Time.now, Time.now+1600, [],
  334. @cli_cert, @ca_key, OpenSSL::Digest::SHA1.new)
  335. ssl_store.add_crl(OpenSSL::X509::CRL.new(crl.to_pem))
  336. args = {}
  337. args[:ctx_proc] = proc { |server_ctx|
  338. server_ctx.cert = @svr_cert
  339. server_ctx.key = @svr_key
  340. server_ctx.verify_mode = vflag
  341. server_ctx.cert_store = ssl_store
  342. }
  343. start_server(PORT, vflag, true, args){|s, p|
  344. ctx = OpenSSL::SSL::SSLContext.new
  345. ctx.cert = @cli_cert
  346. ctx.key = @cli_key
  347. assert_raise(OpenSSL::SSL::SSLError){
  348. sock = TCPSocket.new("127.0.0.1", p)
  349. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  350. ssl.sync_close = true
  351. ssl.connect
  352. ssl.close
  353. }
  354. localcacert_file.unlink
  355. }
  356. end
  357. def test_starttls
  358. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, false){|server, port|
  359. sock = TCPSocket.new("127.0.0.1", port)
  360. ssl = OpenSSL::SSL::SSLSocket.new(sock)
  361. ssl.sync_close = true
  362. str = "x" * 1000 + "\n"
  363. ITERATIONS.times{
  364. ssl.puts(str)
  365. assert_equal(str, ssl.gets)
  366. }
  367. starttls(ssl)
  368. ITERATIONS.times{
  369. ssl.puts(str)
  370. assert_equal(str, ssl.gets)
  371. }
  372. ssl.close
  373. }
  374. end
  375. def test_parallel
  376. GC.start
  377. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
  378. ssls = []
  379. 10.times{
  380. sock = TCPSocket.new("127.0.0.1", port)
  381. ssl = OpenSSL::SSL::SSLSocket.new(sock)
  382. ssl.connect
  383. ssl.sync_close = true
  384. ssls << ssl
  385. }
  386. str = "x" * 1000 + "\n"
  387. ITERATIONS.times{
  388. ssls.each{|ssl|
  389. ssl.puts(str)
  390. assert_equal(str, ssl.gets)
  391. }
  392. }
  393. ssls.each{|ssl| ssl.close }
  394. }
  395. end
  396. def test_verify_result
  397. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
  398. sock = TCPSocket.new("127.0.0.1", port)
  399. ctx = OpenSSL::SSL::SSLContext.new
  400. ctx.set_params
  401. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  402. assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
  403. assert_equal(OpenSSL::X509::V_ERR_SELF_SIGNED_CERT_IN_CHAIN, ssl.verify_result)
  404. sock = TCPSocket.new("127.0.0.1", port)
  405. ctx = OpenSSL::SSL::SSLContext.new
  406. ctx.set_params(
  407. :verify_callback => Proc.new do |preverify_ok, store_ctx|
  408. store_ctx.error = OpenSSL::X509::V_OK
  409. true
  410. end
  411. )
  412. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  413. ssl.connect
  414. assert_equal(OpenSSL::X509::V_OK, ssl.verify_result)
  415. sock = TCPSocket.new("127.0.0.1", port)
  416. ctx = OpenSSL::SSL::SSLContext.new
  417. ctx.set_params(
  418. :verify_callback => Proc.new do |preverify_ok, store_ctx|
  419. store_ctx.error = OpenSSL::X509::V_ERR_APPLICATION_VERIFICATION
  420. false
  421. end
  422. )
  423. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  424. assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
  425. assert_equal(OpenSSL::X509::V_ERR_APPLICATION_VERIFICATION, ssl.verify_result)
  426. }
  427. end
  428. def test_extra_chain_cert
  429. start_server(PORT, OpenSSL::SSL::VERIFY_PEER, true){|server, port|
  430. sock = TCPSocket.new("127.0.0.1", port)
  431. ctx = OpenSSL::SSL::SSLContext.new
  432. ctx.set_params
  433. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  434. assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
  435. assert_equal(OpenSSL::X509::V_ERR_SELF_SIGNED_CERT_IN_CHAIN, ssl.verify_result)
  436. }
  437. # server returns a chain w/o root cert so the client verification fails
  438. # with UNABLE_TO_GET_ISSUER_CERT_LOCALLY not SELF_SIGNED_CERT_IN_CHAIN.
  439. args = {}
  440. args[:ctx_proc] = proc { |server_ctx|
  441. server_ctx.cert = @svr_cert
  442. server_ctx.key = @svr_key
  443. server_ctx.extra_chain_cert = [@svr_cert]
  444. }
  445. start_server(PORT, OpenSSL::SSL::VERIFY_PEER, true, args){|server, port|
  446. sock = TCPSocket.new("127.0.0.1", port)
  447. ctx = OpenSSL::SSL::SSLContext.new
  448. ctx.set_params
  449. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  450. assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
  451. assert_equal(OpenSSL::X509::V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, ssl.verify_result)
  452. }
  453. end
  454. def test_client_ca
  455. args = {}
  456. vflag = OpenSSL::SSL::VERIFY_PEER|OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
  457. # client_ca as a cert
  458. args[:ctx_proc] = proc { |server_ctx|
  459. server_ctx.cert = @svr_cert
  460. server_ctx.key = @svr_key
  461. server_ctx.client_ca = @ca_cert
  462. }
  463. start_server(PORT, vflag, true, args){|server, port|
  464. ctx = OpenSSL::SSL::SSLContext.new
  465. ctx.key = @cli_key
  466. ctx.cert = @cli_cert
  467. sock = TCPSocket.new("127.0.0.1", port)
  468. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  469. ssl.sync_close = true
  470. ssl.connect
  471. ssl.puts("foo")
  472. assert_equal("foo\n", ssl.gets)
  473. }
  474. # client_ca as an array
  475. args[:ctx_proc] = proc { |server_ctx|
  476. server_ctx.cert = @svr_cert
  477. server_ctx.key = @svr_key
  478. server_ctx.client_ca = [@ca_cert, @svr_cert]
  479. }
  480. start_server(PORT, vflag, true, args){|server, port|
  481. ctx = OpenSSL::SSL::SSLContext.new
  482. ctx.key = @cli_key
  483. ctx.cert = @cli_cert
  484. sock = TCPSocket.new("127.0.0.1", port)
  485. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  486. ssl.sync_close = true
  487. ssl.connect
  488. ssl.puts("foo")
  489. assert_equal("foo\n", ssl.gets)
  490. }
  491. end
  492. def test_sslctx_ssl_version_client
  493. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
  494. sock = TCPSocket.new("127.0.0.1", port)
  495. ctx = OpenSSL::SSL::SSLContext.new
  496. ctx.set_params
  497. ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
  498. ctx.ssl_version = "TLSv1"
  499. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  500. assert_nothing_raised do
  501. ssl.connect
  502. end
  503. ssl.puts("hello TLSv1")
  504. ssl.close
  505. sock.close
  506. #
  507. sock = TCPSocket.new("127.0.0.1", port)
  508. ctx.ssl_version = "SSLv3"
  509. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  510. assert_nothing_raised do
  511. ssl.connect
  512. end
  513. ssl.puts("hello SSLv3")
  514. ssl.close
  515. sock.close
  516. #
  517. sock = TCPSocket.new("127.0.0.1", port)
  518. ctx.ssl_version = "SSLv3_server"
  519. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  520. assert_raise(OpenSSL::SSL::SSLError) do
  521. ssl.connect
  522. end
  523. sock.close
  524. #
  525. sock = TCPSocket.new("127.0.0.1", port)
  526. ctx.ssl_version = "TLSv1_client"
  527. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  528. assert_nothing_raised do
  529. ssl.connect
  530. end
  531. ssl.puts("hello TLSv1_client")
  532. ssl.close
  533. sock.close
  534. }
  535. end
  536. def test_sslctx_ssl_version
  537. args = {}
  538. args[:ctx_proc] = proc { |server_ctx|
  539. server_ctx.ssl_version = "TLSv1"
  540. }
  541. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true, args){|server, port|
  542. sock = TCPSocket.new("127.0.0.1", port)
  543. ctx = OpenSSL::SSL::SSLContext.new
  544. ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
  545. ctx.ssl_version = "TLSv1"
  546. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  547. assert_nothing_raised do
  548. ssl.connect
  549. end
  550. ssl.puts("hello TLSv1")
  551. ssl.close
  552. sock.close
  553. #
  554. sock = TCPSocket.new("127.0.0.1", port)
  555. ctx.ssl_version = "SSLv3"
  556. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  557. assert_raise(OpenSSL::SSL::SSLError) do
  558. ssl.connect
  559. end
  560. }
  561. end
  562. def test_verify_depth
  563. vflag = OpenSSL::SSL::VERIFY_PEER|OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
  564. args = {}
  565. # depth == 1 => OK
  566. args[:ctx_proc] = proc { |server_ctx|
  567. server_ctx.cert = @svr_cert
  568. server_ctx.key = @svr_key
  569. server_ctx.verify_mode = vflag
  570. server_ctx.verify_depth = 1
  571. }
  572. start_server(PORT, vflag, true, args){|server, port|
  573. ctx = OpenSSL::SSL::SSLContext.new
  574. ctx.key = @cli_key
  575. ctx.cert = @cli_cert
  576. sock = TCPSocket.new("127.0.0.1", port)
  577. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  578. assert_nothing_raised do
  579. ssl.connect
  580. end
  581. ssl.close
  582. }
  583. # depth == 0 => error
  584. error = nil
  585. args[:ctx_proc] = proc { |server_ctx|
  586. server_ctx.cert = @svr_cert
  587. server_ctx.key = @svr_key
  588. server_ctx.verify_mode = vflag
  589. server_ctx.verify_depth = 0
  590. server_ctx.verify_callback = proc { |preverify_ok, store_ctx|
  591. error = store_ctx.error
  592. preverify_ok
  593. }
  594. }
  595. start_server(PORT, vflag, true, args){|server, port|
  596. ctx = OpenSSL::SSL::SSLContext.new
  597. ctx.key = @cli_key
  598. ctx.cert = @cli_cert
  599. sock = TCPSocket.new("127.0.0.1", port)
  600. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  601. assert_raises(OpenSSL::SSL::SSLError) do
  602. ssl.connect
  603. end
  604. ssl.close
  605. }
  606. assert_equal OpenSSL::X509::V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, error
  607. end
  608. def test_sslctx_set_params
  609. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
  610. sock = TCPSocket.new("127.0.0.1", port)
  611. ctx = OpenSSL::SSL::SSLContext.new
  612. ctx.set_params
  613. assert_equal(OpenSSL::SSL::VERIFY_PEER, ctx.verify_mode)
  614. assert_equal(OpenSSL::SSL::OP_ALL, ctx.options)
  615. ciphers = ctx.ciphers
  616. ciphers_versions = ciphers.collect{|_, v, _, _| v }
  617. ciphers_names = ciphers.collect{|v, _, _, _| v }
  618. assert(ciphers_names.all?{|v| /ADH/ !~ v })
  619. assert(ciphers_versions.all?{|v| /SSLv2/ !~ v })
  620. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  621. assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
  622. assert_equal(OpenSSL::X509::V_ERR_SELF_SIGNED_CERT_IN_CHAIN, ssl.verify_result)
  623. }
  624. end
  625. def test_sslctx_ciphers
  626. c = OpenSSL::SSL::SSLContext.new
  627. c.ciphers = 'DEFAULT'
  628. default = c.ciphers
  629. assert(default.size > 0)
  630. c.ciphers = 'ALL'
  631. all = c.ciphers
  632. assert(all.size > 0)
  633. c.ciphers = 'LOW'
  634. low = c.ciphers
  635. assert(low.size > 0)
  636. c.ciphers = 'MEDIUM'
  637. medium = c.ciphers
  638. assert(medium.size > 0)
  639. c.ciphers = 'HIGH'
  640. high = c.ciphers
  641. assert(high.size > 0)
  642. c.ciphers = 'EXP'
  643. exp = c.ciphers
  644. assert(exp.size > 0)
  645. # -
  646. c.ciphers = 'ALL:-LOW'
  647. assert_equal(all - low, c.ciphers)
  648. c.ciphers = 'ALL:-MEDIUM'
  649. assert_equal(all - medium, c.ciphers)
  650. c.ciphers = 'ALL:-HIGH'
  651. assert_equal(all - high, c.ciphers)
  652. c.ciphers = 'ALL:-EXP'
  653. assert_equal(all - exp, c.ciphers)
  654. c.ciphers = 'ALL:-LOW:-MEDIUM'
  655. assert_equal(all - low - medium, c.ciphers)
  656. c.ciphers = 'ALL:-LOW:-MEDIUM:-HIGH'
  657. assert_equal(all - low - medium - high, c.ciphers)
  658. assert_raise(OpenSSL::SSL::SSLError) do
  659. # should be empty for OpenSSL/0.9.8l. check OpenSSL changes if this test fail.
  660. c.ciphers = 'ALL:-LOW:-MEDIUM:-HIGH:-EXP'
  661. end
  662. # !
  663. c.ciphers = 'ALL:-LOW:LOW'
  664. assert_equal(all.sort, c.ciphers.sort)
  665. c.ciphers = 'ALL:!LOW:LOW'
  666. assert_equal(all - low, c.ciphers)
  667. c.ciphers = 'ALL:!LOW:+LOW'
  668. assert_equal(all - low, c.ciphers)
  669. # +
  670. c.ciphers = 'HIGH:LOW:+LOW'
  671. assert_equal(high + low, c.ciphers)
  672. c.ciphers = 'HIGH:LOW:+HIGH'
  673. assert_equal(low + high, c.ciphers)
  674. # name+name
  675. c.ciphers = 'RC4'
  676. rc4 = c.ciphers
  677. c.ciphers = 'RSA'
  678. rsa = c.ciphers
  679. c.ciphers = 'RC4+RSA'
  680. assert_equal(rc4&rsa, c.ciphers)
  681. c.ciphers = 'RSA+RC4'
  682. assert_equal(rc4&rsa, c.ciphers)
  683. c.ciphers = 'ALL:RSA+RC4'
  684. assert_equal(all + ((rc4&rsa) - all), c.ciphers)
  685. end
  686. def test_sslctx_options
  687. args = {}
  688. args[:ctx_proc] = proc { |server_ctx|
  689. # TLSv1 only
  690. server_ctx.options = OpenSSL::SSL::OP_NO_SSLv2|OpenSSL::SSL::OP_NO_SSLv3
  691. }
  692. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true, args){|server, port|
  693. sock = TCPSocket.new("127.0.0.1", port)
  694. ctx = OpenSSL::SSL::SSLContext.new
  695. ctx.set_params
  696. ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
  697. ctx.options = OpenSSL::SSL::OP_NO_TLSv1
  698. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  699. assert_raise(OpenSSL::SSL::SSLError, Errno::ECONNRESET) do
  700. ssl.connect
  701. end
  702. ssl.close
  703. sock.close
  704. #
  705. sock = TCPSocket.new("127.0.0.1", port)
  706. ctx = OpenSSL::SSL::SSLContext.new
  707. ctx.set_params
  708. ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
  709. ctx.options = OpenSSL::SSL::OP_NO_SSLv3
  710. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  711. assert_nothing_raised do
  712. ssl.connect
  713. end
  714. ssl.close
  715. sock.close
  716. }
  717. end
  718. def test_post_connection_check
  719. sslerr = OpenSSL::SSL::SSLError
  720. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
  721. sock = TCPSocket.new("127.0.0.1", port)
  722. ssl = OpenSSL::SSL::SSLSocket.new(sock)
  723. ssl.connect
  724. assert_raise(sslerr){ssl.post_connection_check("localhost.localdomain")}
  725. assert_raise(sslerr){ssl.post_connection_check("127.0.0.1")}
  726. assert(ssl.post_connection_check("localhost"))
  727. assert_raise(sslerr){ssl.post_connection_check("foo.example.com")}
  728. cert = ssl.peer_cert
  729. assert(!OpenSSL::SSL.verify_certificate_identity(cert, "localhost.localdomain"))
  730. assert(!OpenSSL::SSL.verify_certificate_identity(cert, "127.0.0.1"))
  731. assert(OpenSSL::SSL.verify_certificate_identity(cert, "localhost"))
  732. assert(!OpenSSL::SSL.verify_certificate_identity(cert, "foo.example.com"))
  733. }
  734. now = Time.now
  735. exts = [
  736. ["keyUsage","keyEncipherment,digitalSignature",true],
  737. ["subjectAltName","DNS:localhost.localdomain",false],
  738. ["subjectAltName","IP:127.0.0.1",false],
  739. ]
  740. @svr_cert = issue_cert(@svr, @svr_key, 4, now, now+1800, exts,
  741. @ca_cert, @ca_key, OpenSSL::Digest::SHA1.new)
  742. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
  743. sock = TCPSocket.new("127.0.0.1", port)
  744. ssl = OpenSSL::SSL::SSLSocket.new(sock)
  745. ssl.connect
  746. assert(ssl.post_connection_check("localhost.localdomain"))
  747. assert(ssl.post_connection_check("127.0.0.1"))
  748. assert_raise(sslerr){ssl.post_connection_check("localhost")}
  749. assert_raise(sslerr){ssl.post_connection_check("foo.example.com")}
  750. cert = ssl.peer_cert
  751. assert(OpenSSL::SSL.verify_certificate_identity(cert, "localhost.localdomain"))
  752. assert(OpenSSL::SSL.verify_certificate_identity(cert, "127.0.0.1"))
  753. assert(!OpenSSL::SSL.verify_certificate_identity(cert, "localhost"))
  754. assert(!OpenSSL::SSL.verify_certificate_identity(cert, "foo.example.com"))
  755. }
  756. now = Time.now
  757. exts = [
  758. ["keyUsage","keyEncipherment,digitalSignature",true],
  759. ["subjectAltName","DNS:*.localdomain",false],
  760. ]
  761. @svr_cert = issue_cert(@svr, @svr_key, 5, now, now+1800, exts,
  762. @ca_cert, @ca_key, OpenSSL::Digest::SHA1.new)
  763. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true){|server, port|
  764. sock = TCPSocket.new("127.0.0.1", port)
  765. ssl = OpenSSL::SSL::SSLSocket.new(sock)
  766. ssl.connect
  767. assert(ssl.post_connection_check("localhost.localdomain"))
  768. assert_raise(sslerr){ssl.post_connection_check("127.0.0.1")}
  769. assert_raise(sslerr){ssl.post_connection_check("localhost")}
  770. assert_raise(sslerr){ssl.post_connection_check("foo.example.com")}
  771. cert = ssl.peer_cert
  772. assert(OpenSSL::SSL.verify_certificate_identity(cert, "localhost.localdomain"))
  773. assert(!OpenSSL::SSL.verify_certificate_identity(cert, "127.0.0.1"))
  774. assert(!OpenSSL::SSL.verify_certificate_identity(cert, "localhost"))
  775. assert(!OpenSSL::SSL.verify_certificate_identity(cert, "foo.example.com"))
  776. }
  777. end
  778. def TODO_implement_SSLSession_test_client_session
  779. last_session = nil
  780. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true) do |server, port|
  781. 2.times do
  782. sock = TCPSocket.new("127.0.0.1", port)
  783. # Debian's openssl 0.9.8g-13 failed at assert(ssl.session_reused?),
  784. # when use default SSLContext. [ruby-dev:36167]
  785. ctx = OpenSSL::SSL::SSLContext.new("TLSv1")
  786. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  787. ssl.sync_close = true
  788. ssl.session = last_session if last_session
  789. ssl.connect
  790. session = ssl.session
  791. if last_session
  792. assert(ssl.session_reused?)
  793. if session.respond_to?(:id)
  794. assert_equal(session.id, last_session.id)
  795. end
  796. assert_equal(session.to_pem, last_session.to_pem)
  797. assert_equal(session.to_der, last_session.to_der)
  798. # Older version of OpenSSL may not be consistent. Look up which versions later.
  799. assert_equal(session.to_text, last_session.to_text)
  800. else
  801. assert(!ssl.session_reused?)
  802. end
  803. last_session = session
  804. str = "x" * 100 + "\n"
  805. ssl.puts(str)
  806. assert_equal(str, ssl.gets)
  807. ssl.close
  808. end
  809. end
  810. end
  811. def TODO_implement_SSLSession_test_server_session
  812. connections = 0
  813. saved_session = nil
  814. ctx_proc = Proc.new do |ctx, ssl|
  815. # add test for session callbacks here
  816. end
  817. server_proc = Proc.new do |ctx, ssl|
  818. session = ssl.session
  819. stats = ctx.session_cache_stats
  820. case connections
  821. when 0
  822. assert_equal(stats[:cache_num], 1)
  823. assert_equal(stats[:cache_hits], 0)
  824. assert_equal(stats[:cache_misses], 0)
  825. assert(!ssl.session_reused?)
  826. when 1
  827. assert_equal(stats[:cache_num], 1)
  828. assert_equal(stats[:cache_hits], 1)
  829. assert_equal(stats[:cache_misses], 0)
  830. assert(ssl.session_reused?)
  831. ctx.session_remove(session)
  832. saved_session = session
  833. when 2
  834. assert_equal(stats[:cache_num], 1)
  835. assert_equal(stats[:cache_hits], 1)
  836. assert_equal(stats[:cache_misses], 1)
  837. assert(!ssl.session_reused?)
  838. ctx.session_add(saved_session)
  839. when 3
  840. assert_equal(stats[:cache_num], 2)
  841. assert_equal(stats[:cache_hits], 2)
  842. assert_equal(stats[:cache_misses], 1)
  843. assert(ssl.session_reused?)
  844. ctx.flush_sessions(Time.now + 5000)
  845. when 4
  846. assert_equal(stats[:cache_num], 1)
  847. assert_equal(stats[:cache_hits], 2)
  848. assert_equal(stats[:cache_misses], 2)
  849. assert(!ssl.session_reused?)
  850. ctx.session_add(saved_session)
  851. end
  852. connections += 1
  853. readwrite_loop(ctx, ssl)
  854. end
  855. first_session = nil
  856. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true, :ctx_proc => ctx_proc, :server_proc => server_proc) do |server, port|
  857. 10.times do |i|
  858. sock = TCPSocket.new("127.0.0.1", port)
  859. ctx = OpenSSL::SSL::SSLContext.new
  860. if defined?(OpenSSL::SSL::OP_NO_TICKET)
  861. # disable RFC4507 support
  862. ctx.options = OpenSSL::SSL::OP_NO_TICKET
  863. end
  864. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  865. ssl.sync_close = true
  866. ssl.session = first_session if first_session
  867. ssl.connect
  868. session = ssl.session
  869. if first_session
  870. case i
  871. when 1; assert(ssl.session_reused?)
  872. when 2; assert(!ssl.session_reused?)
  873. when 3; assert(ssl.session_reused?)
  874. when 4; assert(!ssl.session_reused?)
  875. when 5..10; assert(ssl.session_reused?)
  876. end
  877. end
  878. first_session ||= session
  879. str = "x" * 100 + "\n"
  880. ssl.puts(str)
  881. assert_equal(str, ssl.gets)
  882. ssl.close
  883. end
  884. end
  885. end
  886. def test_tlsext_hostname
  887. return unless OpenSSL::SSL::SSLSocket.instance_methods.include?("hostname")
  888. ctx_proc = Proc.new do |ctx, ssl|
  889. foo_ctx = ctx.dup
  890. ctx.servername_cb = Proc.new do |ssl2, hostname|
  891. case hostname
  892. when 'foo.example.com'
  893. foo_ctx
  894. when 'bar.example.com'
  895. nil
  896. else
  897. raise "unknown hostname #{hostname.inspect}"
  898. end
  899. end
  900. end
  901. server_proc = Proc.new do |ctx, ssl|
  902. readwrite_loop(ctx, ssl)
  903. end
  904. start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true, :ctx_proc => ctx_proc, :server_proc => server_proc) do |server, port|
  905. 2.times do |i|
  906. sock = TCPSocket.new("127.0.0.1", port)
  907. ctx = OpenSSL::SSL::SSLContext.new
  908. if defined?(OpenSSL::SSL::OP_NO_TICKET)
  909. # disable RFC4507 support
  910. ctx.options = OpenSSL::SSL::OP_NO_TICKET
  911. end
  912. ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  913. ssl.sync_close = true
  914. ssl.hostname = (i & 1 == 0) ? 'foo.example.com' : 'bar.example.com'
  915. ssl.connect
  916. str = "x" * 100 + "\n"
  917. ssl.puts(str)
  918. assert_equal(str, ssl.gets)
  919. ssl.close
  920. end
  921. end
  922. end
  923. end
  924. end