-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathssl.lua
305 lines (214 loc) · 6.73 KB
/
ssl.lua
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
-- Copyright (C) Yichun Zhang (agentzh)
local ffi = require "ffi"
local base = require "resty.core.base"
local C = ffi.C
local ffi_str = ffi.string
local ffi_gc = ffi.gc
local getfenv = getfenv
local error = error
local tonumber = tonumber
local errmsg = base.get_errmsg_ptr()
local get_string_buf = base.get_string_buf
local get_size_ptr = base.get_size_ptr
local FFI_DECLINED = base.FFI_DECLINED
local FFI_OK = base.FFI_OK
ffi.cdef[[
struct ngx_ssl_conn_s;
typedef struct ngx_ssl_conn_s ngx_ssl_conn_t;
int ngx_http_lua_ffi_ssl_set_der_certificate(ngx_http_request_t *r,
const char *data, size_t len, char **err);
int ngx_http_lua_ffi_ssl_clear_certs(ngx_http_request_t *r, char **err);
int ngx_http_lua_ffi_ssl_set_der_private_key(ngx_http_request_t *r,
const char *data, size_t len, char **err);
int ngx_http_lua_ffi_ssl_raw_server_addr(ngx_http_request_t *r, char **addr,
size_t *addrlen, int *addrtype, char **err);
int ngx_http_lua_ffi_ssl_server_name(ngx_http_request_t *r, char **name,
size_t *namelen, char **err);
int ngx_http_lua_ffi_cert_pem_to_der(const unsigned char *pem, size_t pem_len,
unsigned char *der, char **err);
int ngx_http_lua_ffi_priv_key_pem_to_der(const unsigned char *pem,
size_t pem_len, unsigned char *der, char **err);
int ngx_http_lua_ffi_ssl_get_tls1_version(ngx_http_request_t *r, char **err);
void *ngx_http_lua_ffi_parse_pem_cert(const unsigned char *pem,
size_t pem_len, char **err);
void *ngx_http_lua_ffi_parse_pem_priv_key(const unsigned char *pem,
size_t pem_len, char **err);
int ngx_http_lua_ffi_set_cert(void *r, void *cdata, char **err);
int ngx_http_lua_ffi_set_priv_key(void *r, void *cdata, char **err);
void ngx_http_lua_ffi_free_cert(void *cdata);
void ngx_http_lua_ffi_free_priv_key(void *cdata);
int ngx_http_lua_ffi_ssl_set_ciphers(ngx_http_request_t *r,
const char *ciphers, char **err);
]]
local _M = { version = base.version }
local charpp = ffi.new("char*[1]")
local intp = ffi.new("int[1]")
function _M.clear_certs()
local r = getfenv(0).__ngx_req
if not r then
return error("no request found")
end
local rc = C.ngx_http_lua_ffi_ssl_clear_certs(r, errmsg)
if rc == FFI_OK then
return true
end
return nil, ffi_str(errmsg[0])
end
function _M.set_der_cert(data)
local r = getfenv(0).__ngx_req
if not r then
return error("no request found")
end
local rc = C.ngx_http_lua_ffi_ssl_set_der_certificate(r, data, #data,
errmsg)
if rc == FFI_OK then
return true
end
return nil, ffi_str(errmsg[0])
end
function _M.set_der_priv_key(data)
local r = getfenv(0).__ngx_req
if not r then
return error("no request found")
end
local rc = C.ngx_http_lua_ffi_ssl_set_der_private_key(r, data, #data,
errmsg)
if rc == FFI_OK then
return true
end
return nil, ffi_str(errmsg[0])
end
local addr_types = {
[0] = "unix",
[1] = "inet",
[2] = "inet6",
}
function _M.raw_server_addr()
local r = getfenv(0).__ngx_req
if not r then
return error("no request found")
end
local sizep = get_size_ptr()
local rc = C.ngx_http_lua_ffi_ssl_raw_server_addr(r, charpp, sizep,
intp, errmsg)
if rc == FFI_OK then
local typ = addr_types[intp[0]]
if not typ then
return nil, nil, "unknown address type: " .. intp[0]
end
return ffi_str(charpp[0], sizep[0]), typ
end
return nil, nil, ffi_str(errmsg[0])
end
function _M.server_name()
local r = getfenv(0).__ngx_req
if not r then
return error("no request found")
end
local sizep = get_size_ptr()
local rc = C.ngx_http_lua_ffi_ssl_server_name(r, charpp, sizep, errmsg)
if rc == FFI_OK then
return ffi_str(charpp[0], sizep[0])
end
if rc == FFI_DECLINED then
return nil
end
return nil, ffi_str(errmsg[0])
end
function _M.cert_pem_to_der(pem)
local outbuf = get_string_buf(#pem)
local sz = C.ngx_http_lua_ffi_cert_pem_to_der(pem, #pem, outbuf, errmsg)
if sz > 0 then
return ffi_str(outbuf, sz)
end
return nil, ffi_str(errmsg[0])
end
function _M.priv_key_pem_to_der(pem)
local outbuf = get_string_buf(#pem)
local sz = C.ngx_http_lua_ffi_priv_key_pem_to_der(pem, #pem, outbuf, errmsg)
if sz > 0 then
return ffi_str(outbuf, sz)
end
return nil, ffi_str(errmsg[0])
end
local function get_tls1_version()
local r = getfenv(0).__ngx_req
if not r then
return error("no request found")
end
local ver = C.ngx_http_lua_ffi_ssl_get_tls1_version(r, errmsg)
ver = tonumber(ver)
if ver >= 0 then
return ver
end
-- rc == FFI_ERROR
return nil, ffi_str(errmsg[0])
end
_M.get_tls1_version = get_tls1_version
function _M.parse_pem_cert(pem)
local cert = C.ngx_http_lua_ffi_parse_pem_cert(pem, #pem, errmsg)
if cert ~= nil then
return ffi_gc(cert, C.ngx_http_lua_ffi_free_cert)
end
return nil, ffi_str(errmsg[0])
end
function _M.parse_pem_priv_key(pem)
local pkey = C.ngx_http_lua_ffi_parse_pem_priv_key(pem, #pem, errmsg)
if pkey ~= nil then
return ffi_gc(pkey, C.ngx_http_lua_ffi_free_priv_key)
end
return nil, ffi_str(errmsg[0])
end
function _M.set_cert(cert)
local r = getfenv(0).__ngx_req
if not r then
return error("no request found")
end
local rc = C.ngx_http_lua_ffi_set_cert(r, cert, errmsg)
if rc == FFI_OK then
return true
end
return nil, ffi_str(errmsg[0])
end
function _M.set_priv_key(priv_key)
local r = getfenv(0).__ngx_req
if not r then
return error("no request found")
end
local rc = C.ngx_http_lua_ffi_set_priv_key(r, priv_key, errmsg)
if rc == FFI_OK then
return true
end
return nil, ffi_str(errmsg[0])
end
function _M.set_ciphers(ciphers)
local r = getfenv(0).__ngx_req
if not r then
return error("no request found")
end
local rc = C.ngx_http_lua_ffi_ssl_set_ciphers(r, ciphers, errmsg)
if rc == FFI_OK then
return true
end
return nil, ffi_str(errmsg[0])
end
do
_M.SSL3_VERSION = 0x0300
_M.TLS1_VERSION = 0x0301
_M.TLS1_1_VERSION = 0x0302
_M.TLS1_2_VERSION = 0x0303
local map = {
[_M.SSL3_VERSION] = "SSLv3",
[_M.TLS1_VERSION] = "TLSv1",
[_M.TLS1_1_VERSION] = "TLSv1.1",
[_M.TLS1_2_VERSION] = "TLSv1.2",
}
function _M.get_tls1_version_str()
local ver, err = get_tls1_version()
if not ver then
return nil, err
end
return map[ver]
end
end
return _M