File: client.lua

package info (click to toggle)
lua-sec 1.3.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 740 kB
  • sloc: ansic: 3,807; makefile: 108; sh: 63
file content (66 lines) | stat: -rw-r--r-- 1,422 bytes parent folder | download | duplicates (6)
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
--
-- Test the conn:want() function
--
-- Public domain
--
local socket = require("socket")
local ssl    = require("ssl")

local params = {
   mode = "client",
   protocol = "tlsv1_2",
   key = "../certs/clientAkey.pem",
   certificate = "../certs/clientA.pem",
   cafile = "../certs/rootA.pem",
   verify = {"peer", "fail_if_no_peer_cert"},
   options = "all",
}

-- Wait until socket is ready (for reading or writing)
local function wait(peer)
   -- What event blocked us?
   local err
   if peer.want then  -- Is it an SSL connection?
     err = peer:want()
     print("Want? ", err)
   else
     -- No, it's a normal TCP connection...
     err = "timeout"
   end

   if err == "read" or err == "timeout" then
      socket.select({peer}, nil)
   elseif err == "write" then
      socket.select(nil, {peer})
   else
      peer:close()
      os.exit(1)
   end
end

-- Start the TCP connection
local peer = socket.tcp()
assert( peer:connect("127.0.0.1", 8888) )

-- [[ SSL wrapper
peer = assert( ssl.wrap(peer, params) )
peer:settimeout(0.3)
local succ = peer:dohandshake()
while not succ do
   wait(peer)
   succ = peer:dohandshake()
end
print("** Handshake done")
--]]

-- If the section above is commented, the timeout is not set.
-- We set it again for safetiness.
peer:settimeout(0.3)

-- Try to receive a line
local str = peer:receive("*l")
while not str do
   wait(peer)
   str = peer:receive("*l")
end
peer:close()