* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / sample / openssl / echo_cli.rb
blob069a21ec9444ebf29ed3d0e6a53ac47812065ab7
1 #!/usr/bin/env ruby
3 require 'socket'
4 require 'openssl'
5 require 'optparse'
7 options = ARGV.getopts("p:c:k:C:")
9 host      = ARGV[0] || "localhost"
10 port      = options["p"] || "2000"
11 cert_file = options["c"]
12 key_file  = options["k"]
13 ca_path   = options["C"]
15 ctx = OpenSSL::SSL::SSLContext.new()
16 if cert_file && key_file
17   ctx.cert = OpenSSL::X509::Certificate.new(File::read(cert_file))
18   ctx.key  = OpenSSL::PKey::RSA.new(File::read(key_file))
19 end
20 if ca_path
21   ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
22   ctx.ca_path = ca_path
23 else
24   $stderr.puts "!!! WARNING: PEER CERTIFICATE WON'T BE VERIFIED !!!"
25 end
27 s = TCPSocket.new(host, port)
28 ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
29 ssl.connect # start SSL session
30 p ssl.peer_cert
31 errors = Hash.new
32 OpenSSL::X509.constants.grep(/^V_(ERR_|OK)/).each do |name|
33   errors[OpenSSL::X509.const_get(name)] = name
34 end
35 p errors[ssl.verify_result]
37 ssl.sync_close = true  # if true the underlying socket will be
38                        # closed in SSLSocket#close. (default: false)
39 while line = $stdin.gets
40   ssl.write line
41   puts ssl.gets.inspect
42 end
44 ssl.close