Project

General

Profile

Feature #11170 ยป ossl_11170.rb

normalperson (Eric Wong), 05/23/2015 01:35 AM

 
1
require 'openssl'
2
$stderr.sync = true
3
require 'socket'
4
host = '127.0.0.1'
5
TEST_KEY_DH1024 = OpenSSL::PKey::DH.new <<-_end_of_pem_
6
-----BEGIN DH PARAMETERS-----
7
MIGHAoGBAKnKQ8MNK6nYZzLrrcuTsLxuiJGXoOO5gT+tljOTbHBuiktdMTITzIY0
8
pFxIvjG05D7HoBZQfrR0c92NGWPkAiCkhQKB8JCbPVzwNLDy6DZ0pmofDKrEsYHG
9
AQjjxMXhwULlmuR/K+WwlaZPiLIBYalLAZQ7ZbOPeVkJ8ePao0eLAgEC
10
-----END DH PARAMETERS-----
11
_end_of_pem_
12

    
13
TEST_KEY_DH1024.priv_key = OpenSSL::BN.new("48561834C67E65FFD2A9B47F41" \
14
   "E5E78FDC95C387428FDB1E4B0188B64D1643C3A8D3455B945B7E8C4D166010C7C2" \
15
   "CE23BFB9BEF43D0348FE7FA5284B0225E7FE1537546D114E3D8A4411B9B9351AB4" \
16
   "51E1A358F50ED61B1F00DA29336EEBBD649980AC86D76AF8BBB065298C2052672E" \
17
   "EF3EF13AB47A15275FC2836F3AC74CEA", 16)
18

    
19
def ssl_client(host, port)
20
  ctx = OpenSSL::SSL::SSLContext.new
21
  ctx.set_params
22
  ctx.ciphers = "ADH"
23
  s = TCPSocket.new(host, port)
24
  ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
25
  ssl.connect
26
  ssl.sync_close = true
27
  ssl
28
end
29

    
30
def srv_ctx
31
  ctx = OpenSSL::SSL::SSLContext.new
32
  ctx.set_params
33
  ctx.ciphers = "ADH"
34
  ctx.tmp_dh_callback = proc { TEST_KEY_DH1024 }
35
  ctx
36
end
37

    
38
serv = TCPServer.new(host, 0)
39
port = serv.addr[1]
40
clients = []
41
accepted = []
42
pid = fork do
43
  ssl_ctx = srv_ctx
44
  buf = ""
45
  begin
46
    a = serv.accept
47
    ssl = OpenSSL::SSL::SSLSocket.new(a, ssl_ctx)
48
    ssl.accept
49
    ssl.readpartial(11, buf)
50
    ssl.write "HI".freeze
51
    ssl.flush
52
    accepted << ssl
53
  rescue => e
54
    warn "#$$: #{e.message} (#{e.class})"
55
    warn "#$$: accepted: #{accepted.size}"
56
    sleep
57
  end while true
58
end
59
serv.close
60

    
61
buf = ""
62
begin
63
  c = ssl_client(host, port)
64
  c.write "HELLO".freeze
65
  c.flush
66
  c.readpartial(11, buf)
67
  clients << c
68
rescue => e
69
  warn "#$$: #{e.message} (#{e.class})"
70
  warn "#$$: clients: #{clients.size}"
71
  sleep
72
end while true