src/https.lua

Tue, 05 Jul 2011 18:12:17 -0700

author
Paul Aurich <paul@darkrain42.org>
date
Tue, 05 Jul 2011 18:12:17 -0700
changeset 44
b3a0d23e5b20
parent 0
f7d2d78eb424
permissions
-rw-r--r--

ssl: Add a missing call to setciphers()

0
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 ----------------------------------------------------------------------------
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- LuaSec 0.4
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2009 PUC-Rio
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- Author: Pablo Musa
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- Author: Tomas Guisasola
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 ---------------------------------------------------------------------------
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local socket = require("socket")
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local ssl = require("ssl")
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local ltn12 = require("ltn12")
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local http = require("socket.http")
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local url = require("socket.url")
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local table = require("table")
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local string = require("string")
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local try = socket.try
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local type = type
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local pairs = pairs
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local getmetatable = getmetatable
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 module("ssl.https")
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 _VERSION = "0.4"
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 _COPYRIGHT = "LuaSec 0.4 - Copyright (C) 2009 PUC-Rio"
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 -- Default settings
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 PORT = 443
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local cfg = {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 protocol = "tlsv1",
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 options = "all",
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 verify = "none",
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 --------------------------------------------------------------------
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 -- Auxiliar Functions
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 --------------------------------------------------------------------
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 -- Insert default HTTPS port.
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local function default_https_port(u)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 return url.build(url.parse(u, {port = PORT}))
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 -- Convert an URL to a table according to Luasocket needs.
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local function urlstring_totable(url, body, result_table)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 url = {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 url = default_https_port(url),
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 method = body and "POST" or "GET",
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 sink = ltn12.sink.table(result_table)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if body then
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 url.source = ltn12.source.string(body)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 url.headers = {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 ["content-length"] = #body,
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 ["content-type"] = "application/x-www-form-urlencoded",
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 return url
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 -- Forward calls to the real connection object.
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local function reg(conn)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local mt = getmetatable(conn.sock).__index
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 for name, method in pairs(mt) do
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 if type(method) == "function" then
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 conn[name] = function (self, ...)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 return method(self.sock, ...)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 -- Return a function which performs the SSL/TLS connection.
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 local function tcp(params)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 params = params or {}
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 -- Default settings
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 for k, v in pairs(cfg) do
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 params[k] = params[k] or v
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 -- Force client mode
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 params.mode = "client"
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 -- 'create' function for LuaSocket
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 return function ()
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local conn = {}
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 conn.sock = try(socket.tcp())
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 local st = getmetatable(conn.sock).__index.settimeout
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 function conn:settimeout(...)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 return st(self.sock, ...)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 -- Replace TCP's connection function
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 function conn:connect(host, port)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 try(self.sock:connect(host, port))
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 self.sock = try(ssl.wrap(self.sock, params))
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 try(self.sock:dohandshake())
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 reg(self, getmetatable(self.sock))
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 return 1
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 return conn
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 --------------------------------------------------------------------
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 -- Main Function
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 --------------------------------------------------------------------
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 -- Make a HTTP request over secure connection. This function receives
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 -- the same parameters of LuaSocket's HTTP module (except 'proxy' and
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 -- 'redirect') plus LuaSec parameters.
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 --
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 -- @param url mandatory (string or table)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 -- @param body optional (string)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 -- @return (string if url == string or 1), code, headers, status
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 --
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 function request(url, body)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 local result_table = {}
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 local stringrequest = type(url) == "string"
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 if stringrequest then
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 url = urlstring_totable(url, body, result_table)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 else
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 url.url = default_https_port(url.url)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 if http.PROXY or url.proxy then
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 return nil, "proxy not supported"
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 elseif url.redirect then
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 return nil, "redirect not supported"
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 elseif url.create then
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 return nil, "create function not permitted"
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 -- New 'create' function to establish a secure connection
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 url.create = tcp(url)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 local res, code, headers, status = http.request(url)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 if res and stringrequest then
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 return table.concat(result_table), code, headers, status
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 end
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 return res, code, headers, status
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 end

mercurial