Sun, 21 Nov 2010 20:14:31 -0800
verify: Flag to ignore 'invalid purpose' errors on end cert
0 | 1 | /*-------------------------------------------------------------------------- |
2 | * LuaSec 0.4 | |
3 | * Copyright (C) 2006-2009 Bruno Silvestre | |
4 | * | |
5 | *--------------------------------------------------------------------------*/ | |
6 | ||
7 | #include <string.h> | |
8 | ||
9 | #include <openssl/ssl.h> | |
11
8d7698d3fd26
Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
10 | #include <openssl/x509v3.h> |
0 | 11 | #include <openssl/err.h> |
12 | ||
13 | #include <lua.h> | |
14 | #include <lauxlib.h> | |
15 | ||
16 | #include "io.h" | |
17 | #include "buffer.h" | |
18 | #include "timeout.h" | |
19 | #include "socket.h" | |
20 | #include "ssl.h" | |
19
45b7299e4746
src/ssl.c: Include x509.h to shush compiler warning
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
21 | #include "x509.h" |
0 | 22 | |
23 | /** | |
24 | * Map error code into string. | |
25 | */ | |
26 | static const char *ssl_ioerror(void *ctx, int err) | |
27 | { | |
28 | if (err == IO_SSL) { | |
29 | p_ssl ssl = (p_ssl) ctx; | |
30 | switch(ssl->error) { | |
31 | case SSL_ERROR_NONE: return "No error"; | |
32 | case SSL_ERROR_ZERO_RETURN: return "closed"; | |
33 | case SSL_ERROR_WANT_READ: return "wantread"; | |
34 | case SSL_ERROR_WANT_WRITE: return "wantwrite"; | |
35 | case SSL_ERROR_WANT_CONNECT: return "'connect' not completed"; | |
36 | case SSL_ERROR_WANT_ACCEPT: return "'accept' not completed"; | |
37 | case SSL_ERROR_WANT_X509_LOOKUP: return "Waiting for callback"; | |
38 | case SSL_ERROR_SYSCALL: return "System error"; | |
39 | case SSL_ERROR_SSL: return ERR_reason_error_string(ERR_get_error()); | |
40 | default: return "Unknown SSL error"; | |
41 | } | |
42 | } | |
43 | return socket_strerror(err); | |
44 | } | |
45 | ||
46 | /** | |
47 | * Close the connection before the GC collect the object. | |
48 | */ | |
49 | static int meth_destroy(lua_State *L) | |
50 | { | |
51 | p_ssl ssl = (p_ssl) lua_touserdata(L, 1); | |
52 | if (ssl->ssl) { | |
53 | socket_setblocking(&ssl->sock); | |
54 | SSL_shutdown(ssl->ssl); | |
55 | socket_destroy(&ssl->sock); | |
56 | SSL_free(ssl->ssl); | |
57 | ssl->ssl = NULL; | |
58 | } | |
59 | return 0; | |
60 | } | |
61 | ||
62 | /** | |
31
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
63 | * Object information -- tostring metamethod |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
64 | */ |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
65 | static int meth_tostring(lua_State *L) |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
66 | { |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
67 | p_ssl ssl = (p_ssl)lua_touserdata(L, 1); |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
68 | lua_pushfstring(L, "SSL connection: %p", ssl); |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
69 | return 1; |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
70 | } |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
71 | |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
72 | /** |
0 | 73 | * Perform the TLS/SSL handshake |
74 | */ | |
75 | static int handshake(p_ssl ssl) | |
76 | { | |
77 | int err; | |
78 | p_timeout tm = timeout_markstart(&ssl->tm); | |
79 | if (ssl->state == ST_SSL_CLOSED) | |
80 | return IO_CLOSED; | |
81 | for ( ; ; ) { | |
82 | ERR_clear_error(); | |
83 | err = SSL_do_handshake(ssl->ssl); | |
84 | ssl->error = SSL_get_error(ssl->ssl, err); | |
85 | switch(ssl->error) { | |
86 | case SSL_ERROR_NONE: | |
87 | ssl->state = ST_SSL_CONNECTED; | |
88 | return IO_DONE; | |
89 | case SSL_ERROR_WANT_READ: | |
90 | err = socket_waitfd(&ssl->sock, WAITFD_R, tm); | |
91 | if (err == IO_TIMEOUT) return IO_SSL; | |
92 | if (err != IO_DONE) return err; | |
93 | break; | |
94 | case SSL_ERROR_WANT_WRITE: | |
95 | err = socket_waitfd(&ssl->sock, WAITFD_W, tm); | |
96 | if (err == IO_TIMEOUT) return IO_SSL; | |
97 | if (err != IO_DONE) return err; | |
98 | break; | |
99 | case SSL_ERROR_SYSCALL: | |
100 | if (ERR_peek_error()) { | |
101 | ssl->error = SSL_ERROR_SSL; | |
102 | return IO_SSL; | |
103 | } | |
104 | if (err == 0) | |
105 | return IO_CLOSED; | |
106 | return socket_error(); | |
107 | default: | |
108 | return IO_SSL; | |
109 | } | |
110 | } | |
111 | return IO_UNKNOWN; | |
112 | } | |
113 | ||
114 | /** | |
115 | * Send data | |
116 | */ | |
117 | static int ssl_send(void *ctx, const char *data, size_t count, size_t *sent, | |
118 | p_timeout tm) | |
119 | { | |
120 | int err; | |
121 | p_ssl ssl = (p_ssl) ctx; | |
122 | if (ssl->state == ST_SSL_CLOSED) | |
123 | return IO_CLOSED; | |
124 | *sent = 0; | |
125 | for ( ; ; ) { | |
126 | ERR_clear_error(); | |
127 | err = SSL_write(ssl->ssl, data, (int) count); | |
128 | ssl->error = SSL_get_error(ssl->ssl, err); | |
129 | switch(ssl->error) { | |
130 | case SSL_ERROR_NONE: | |
131 | *sent = err; | |
132 | return IO_DONE; | |
133 | case SSL_ERROR_WANT_READ: | |
134 | err = socket_waitfd(&ssl->sock, WAITFD_R, tm); | |
135 | if (err == IO_TIMEOUT) return IO_SSL; | |
136 | if (err != IO_DONE) return err; | |
137 | break; | |
138 | case SSL_ERROR_WANT_WRITE: | |
139 | err = socket_waitfd(&ssl->sock, WAITFD_W, tm); | |
140 | if (err == IO_TIMEOUT) return IO_SSL; | |
141 | if (err != IO_DONE) return err; | |
142 | break; | |
143 | case SSL_ERROR_SYSCALL: | |
144 | if (ERR_peek_error()) { | |
145 | ssl->error = SSL_ERROR_SSL; | |
146 | return IO_SSL; | |
147 | } | |
148 | if (err == 0) | |
149 | return IO_CLOSED; | |
150 | return socket_error(); | |
151 | default: | |
152 | return IO_SSL; | |
153 | } | |
154 | } | |
155 | return IO_UNKNOWN; | |
156 | } | |
157 | ||
158 | /** | |
159 | * Receive data | |
160 | */ | |
161 | static int ssl_recv(void *ctx, char *data, size_t count, size_t *got, | |
162 | p_timeout tm) | |
163 | { | |
164 | int err; | |
165 | p_ssl ssl = (p_ssl) ctx; | |
166 | if (ssl->state == ST_SSL_CLOSED) | |
167 | return IO_CLOSED; | |
168 | *got = 0; | |
169 | for ( ; ; ) { | |
170 | ERR_clear_error(); | |
171 | err = SSL_read(ssl->ssl, data, (int) count); | |
172 | ssl->error = SSL_get_error(ssl->ssl, err); | |
173 | switch(ssl->error) { | |
174 | case SSL_ERROR_NONE: | |
175 | *got = err; | |
176 | return IO_DONE; | |
177 | case SSL_ERROR_ZERO_RETURN: | |
178 | *got = err; | |
179 | return IO_CLOSED; | |
180 | case SSL_ERROR_WANT_READ: | |
181 | err = socket_waitfd(&ssl->sock, WAITFD_R, tm); | |
182 | if (err == IO_TIMEOUT) return IO_SSL; | |
183 | if (err != IO_DONE) return err; | |
184 | break; | |
185 | case SSL_ERROR_WANT_WRITE: | |
186 | err = socket_waitfd(&ssl->sock, WAITFD_W, tm); | |
187 | if (err == IO_TIMEOUT) return IO_SSL; | |
188 | if (err != IO_DONE) return err; | |
189 | break; | |
190 | case SSL_ERROR_SYSCALL: | |
191 | if (ERR_peek_error()) { | |
192 | ssl->error = SSL_ERROR_SSL; | |
193 | return IO_SSL; | |
194 | } | |
195 | if (err == 0) | |
196 | return IO_CLOSED; | |
197 | return socket_error(); | |
198 | default: | |
199 | return IO_SSL; | |
200 | } | |
201 | } | |
202 | return IO_UNKNOWN; | |
203 | } | |
204 | ||
205 | /** | |
206 | * Create a new TLS/SSL object and mark it as new. | |
207 | */ | |
208 | static int meth_create(lua_State *L) | |
209 | { | |
210 | p_ssl ssl; | |
211 | int mode = ctx_getmode(L, 1); | |
34
510432315106
verify: Flag to ignore 'invalid purpose' errors on end cert
Paul Aurich <paul@darkrain42.org>
parents:
31
diff
changeset
|
212 | p_context ctx = checkctx(L, 1); |
0 | 213 | |
214 | if (mode == MD_CTX_INVALID) { | |
215 | lua_pushnil(L); | |
216 | lua_pushstring(L, "invalid mode"); | |
217 | return 2; | |
218 | } | |
34
510432315106
verify: Flag to ignore 'invalid purpose' errors on end cert
Paul Aurich <paul@darkrain42.org>
parents:
31
diff
changeset
|
219 | if (luasec_ssl_idx == -1) { |
510432315106
verify: Flag to ignore 'invalid purpose' errors on end cert
Paul Aurich <paul@darkrain42.org>
parents:
31
diff
changeset
|
220 | luasec_ssl_idx = SSL_get_ex_new_index(0, "luasec context", NULL, NULL, NULL); |
510432315106
verify: Flag to ignore 'invalid purpose' errors on end cert
Paul Aurich <paul@darkrain42.org>
parents:
31
diff
changeset
|
221 | if (luasec_ssl_idx == -1) { |
510432315106
verify: Flag to ignore 'invalid purpose' errors on end cert
Paul Aurich <paul@darkrain42.org>
parents:
31
diff
changeset
|
222 | lua_pushnil(L); |
510432315106
verify: Flag to ignore 'invalid purpose' errors on end cert
Paul Aurich <paul@darkrain42.org>
parents:
31
diff
changeset
|
223 | lua_pushstring(L, "error creating luasec SSL index"); |
510432315106
verify: Flag to ignore 'invalid purpose' errors on end cert
Paul Aurich <paul@darkrain42.org>
parents:
31
diff
changeset
|
224 | return 2; |
510432315106
verify: Flag to ignore 'invalid purpose' errors on end cert
Paul Aurich <paul@darkrain42.org>
parents:
31
diff
changeset
|
225 | } |
510432315106
verify: Flag to ignore 'invalid purpose' errors on end cert
Paul Aurich <paul@darkrain42.org>
parents:
31
diff
changeset
|
226 | } |
0 | 227 | ssl = (p_ssl) lua_newuserdata(L, sizeof(t_ssl)); |
228 | if (!ssl) { | |
229 | lua_pushnil(L); | |
230 | lua_pushstring(L, "error creating SSL object"); | |
231 | return 2; | |
232 | } | |
34
510432315106
verify: Flag to ignore 'invalid purpose' errors on end cert
Paul Aurich <paul@darkrain42.org>
parents:
31
diff
changeset
|
233 | ssl->ssl = SSL_new(ctx->context); |
0 | 234 | if (!ssl->ssl) { |
235 | lua_pushnil(L); | |
236 | lua_pushstring(L, "error creating SSL object"); | |
237 | return 2;; | |
238 | } | |
239 | ssl->state = ST_SSL_NEW; | |
240 | SSL_set_fd(ssl->ssl, (int) SOCKET_INVALID); | |
2
0cfca30f1ce3
ssl.c: Set SSL_MODE_RELEASE_BUFFERS mode when supported
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
241 | SSL_set_mode(ssl->ssl, SSL_MODE_ENABLE_PARTIAL_WRITE | |
0 | 242 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
2
0cfca30f1ce3
ssl.c: Set SSL_MODE_RELEASE_BUFFERS mode when supported
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
243 | |
0cfca30f1ce3
ssl.c: Set SSL_MODE_RELEASE_BUFFERS mode when supported
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
244 | #ifdef SSL_MODE_RELEASE_BUFFERS |
0cfca30f1ce3
ssl.c: Set SSL_MODE_RELEASE_BUFFERS mode when supported
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
245 | SSL_set_mode(ssl->ssl, SSL_MODE_RELEASE_BUFFERS); |
0cfca30f1ce3
ssl.c: Set SSL_MODE_RELEASE_BUFFERS mode when supported
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
246 | #endif |
0cfca30f1ce3
ssl.c: Set SSL_MODE_RELEASE_BUFFERS mode when supported
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
247 | |
34
510432315106
verify: Flag to ignore 'invalid purpose' errors on end cert
Paul Aurich <paul@darkrain42.org>
parents:
31
diff
changeset
|
248 | SSL_set_ex_data(ssl->ssl, luasec_ssl_idx, ctx); |
510432315106
verify: Flag to ignore 'invalid purpose' errors on end cert
Paul Aurich <paul@darkrain42.org>
parents:
31
diff
changeset
|
249 | |
0 | 250 | if (mode == MD_CTX_SERVER) |
251 | SSL_set_accept_state(ssl->ssl); | |
252 | else | |
253 | SSL_set_connect_state(ssl->ssl); | |
254 | ||
255 | io_init(&ssl->io, (p_send) ssl_send, (p_recv) ssl_recv, | |
256 | (p_error) ssl_ioerror, ssl); | |
257 | timeout_init(&ssl->tm, -1, -1); | |
258 | buffer_init(&ssl->buf, &ssl->io, &ssl->tm); | |
259 | ||
260 | luaL_getmetatable(L, "SSL:Connection"); | |
261 | lua_setmetatable(L, -2); | |
262 | return 1; | |
263 | } | |
264 | ||
265 | /** | |
266 | * Buffer send function | |
267 | */ | |
268 | static int meth_send(lua_State *L) { | |
269 | p_ssl ssl = (p_ssl) luaL_checkudata(L, 1, "SSL:Connection"); | |
270 | return buffer_meth_send(L, &ssl->buf); | |
271 | } | |
272 | ||
273 | /** | |
274 | * Buffer receive function | |
275 | */ | |
276 | static int meth_receive(lua_State *L) { | |
277 | p_ssl ssl = (p_ssl) luaL_checkudata(L, 1, "SSL:Connection"); | |
278 | return buffer_meth_receive(L, &ssl->buf); | |
279 | } | |
280 | ||
281 | /** | |
282 | * Select support methods | |
283 | */ | |
284 | static int meth_getfd(lua_State *L) | |
285 | { | |
286 | p_ssl ssl = (p_ssl) luaL_checkudata(L, 1, "SSL:Connection"); | |
287 | lua_pushnumber(L, ssl->sock); | |
288 | return 1; | |
289 | } | |
290 | ||
291 | /** | |
292 | * Set the TLS/SSL file descriptor. | |
293 | * This is done *before* the handshake. | |
294 | */ | |
295 | static int meth_setfd(lua_State *L) | |
296 | { | |
297 | p_ssl ssl = (p_ssl) luaL_checkudata(L, 1, "SSL:Connection"); | |
298 | if (ssl->state != ST_SSL_NEW) | |
299 | luaL_argerror(L, 1, "invalid SSL object state"); | |
300 | ssl->sock = luaL_checkint(L, 2); | |
301 | socket_setnonblocking(&ssl->sock); | |
302 | SSL_set_fd(ssl->ssl, (int)ssl->sock); | |
303 | return 0; | |
304 | } | |
305 | ||
306 | /** | |
307 | * Lua handshake function. | |
308 | */ | |
309 | static int meth_handshake(lua_State *L) | |
310 | { | |
311 | p_ssl ssl = (p_ssl) luaL_checkudata(L, 1, "SSL:Connection"); | |
312 | int err = handshake(ssl); | |
313 | if (err == IO_DONE) { | |
314 | lua_pushboolean(L, 1); | |
315 | return 1; | |
316 | } | |
317 | lua_pushboolean(L, 0); | |
318 | lua_pushstring(L, ssl_ioerror((void*)ssl, err)); | |
319 | return 2; | |
320 | } | |
321 | ||
322 | /** | |
323 | * Close the connection. | |
324 | */ | |
325 | static int meth_close(lua_State *L) | |
326 | { | |
327 | p_ssl ssl = (p_ssl) luaL_checkudata(L, 1, "SSL:Connection"); | |
328 | meth_destroy(L); | |
329 | ssl->state = ST_SSL_CLOSED; | |
330 | return 0; | |
331 | } | |
332 | ||
333 | /** | |
334 | * Set timeout. | |
335 | */ | |
336 | static int meth_settimeout(lua_State *L) | |
337 | { | |
338 | p_ssl ssl = (p_ssl) luaL_checkudata(L, 1, "SSL:Connection"); | |
339 | return timeout_meth_settimeout(L, &ssl->tm); | |
340 | } | |
341 | ||
342 | /** | |
343 | * Check if there is data in the buffer. | |
344 | */ | |
345 | static int meth_dirty(lua_State *L) | |
346 | { | |
347 | int res = 0; | |
348 | p_ssl ssl = (p_ssl) luaL_checkudata(L, 1, "SSL:Connection"); | |
349 | if (ssl->state != ST_SSL_CLOSED) | |
350 | res = !buffer_isempty(&ssl->buf) || SSL_pending(ssl->ssl); | |
351 | lua_pushboolean(L, res); | |
352 | return 1; | |
353 | } | |
354 | ||
355 | /** | |
356 | * Return the state information about the SSL object. | |
357 | */ | |
358 | static int meth_want(lua_State *L) | |
359 | { | |
360 | p_ssl ssl = (p_ssl) luaL_checkudata(L, 1, "SSL:Connection"); | |
361 | int code = (ssl->state == ST_SSL_CLOSED) ? SSL_NOTHING : SSL_want(ssl->ssl); | |
362 | switch(code) { | |
363 | case SSL_NOTHING: lua_pushstring(L, "nothing"); break; | |
364 | case SSL_READING: lua_pushstring(L, "read"); break; | |
365 | case SSL_WRITING: lua_pushstring(L, "write"); break; | |
366 | case SSL_X509_LOOKUP: lua_pushstring(L, "x509lookup"); break; | |
367 | } | |
368 | return 1; | |
369 | } | |
370 | ||
371 | /** | |
372 | * Return a pointer to SSL structure. | |
373 | */ | |
374 | static int meth_rawconn(lua_State *L) | |
375 | { | |
376 | p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection"); | |
377 | lua_pushlightuserdata(L, (void*)ssl->ssl); | |
378 | return 1; | |
379 | } | |
380 | ||
3
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
381 | /** |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
382 | * Return the compression method used. |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
383 | */ |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
384 | static int meth_compression(lua_State *L) |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
385 | { |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
386 | const COMP_METHOD *comp; |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
387 | p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection"); |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
388 | comp = SSL_get_current_compression(ssl->ssl); |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
389 | if (comp) { |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
390 | lua_pushstring(L, SSL_COMP_get_name(comp)); |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
391 | return 1; |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
392 | } else { |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
393 | lua_pushboolean(L, 0); |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
394 | return 1; |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
395 | } |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
396 | } |
4
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
397 | |
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
398 | /** |
30
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
399 | * Return the validation state of the peer chain |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
400 | */ |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
401 | static int meth_getpeerchainvalid(lua_State *L) |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
402 | { |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
403 | p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection"); |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
404 | long result = SSL_get_verify_result(ssl->ssl); |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
405 | |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
406 | if (result == X509_V_OK) { |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
407 | lua_pushboolean(L, 1); |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
408 | return 1; |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
409 | } |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
410 | |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
411 | lua_pushboolean(L, 0); |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
412 | lua_pushstring(L, X509_verify_cert_error_string(result)); |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
413 | return 2; |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
414 | } |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
415 | |
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
416 | /** |
4
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
417 | * Return the peer certificate. |
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
418 | */ |
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
419 | static int meth_getpeercertificate(lua_State *L) |
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
420 | { |
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
421 | X509 *peer; |
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
422 | p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection"); |
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
423 | peer = SSL_get_peer_certificate(ssl->ssl); |
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
424 | if (peer == NULL) { |
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
425 | /* No client certificate available */ |
14
1927b7b32faf
Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
426 | lua_pushnil(L); |
7
da3cf40976f6
Modify :getpeercertificate() to return a decoded certificate (subject only at the moment)
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
427 | } |
da3cf40976f6
Modify :getpeercertificate() to return a decoded certificate (subject only at the moment)
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
428 | else |
da3cf40976f6
Modify :getpeercertificate() to return a decoded certificate (subject only at the moment)
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
429 | { |
14
1927b7b32faf
Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
430 | /* Push metatabled peer */ |
1927b7b32faf
Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
431 | luasec_push_x509(L, peer); |
4
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
432 | } |
7
da3cf40976f6
Modify :getpeercertificate() to return a decoded certificate (subject only at the moment)
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
433 | return 1; |
4
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
434 | } |
5
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
435 | |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
436 | static int meth_getfinished(lua_State *L) |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
437 | { |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
438 | p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection"); |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
439 | SSL *conn = ssl->ssl; |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
440 | char *buffer = NULL; |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
441 | size_t len = 0; |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
442 | if ((len = SSL_get_finished(conn, NULL, 0)) != 0) { |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
443 | buffer = malloc(len); |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
444 | if (buffer == NULL) return 0; |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
445 | len = SSL_get_finished(conn, buffer, len); |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
446 | lua_pushlstring(L, buffer, len); |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
447 | free(buffer); |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
448 | return 1; |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
449 | } else { |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
450 | return 0; |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
451 | } |
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
452 | } |
6
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
453 | |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
454 | static int meth_getpeerfinished(lua_State *L) |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
455 | { |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
456 | p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection"); |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
457 | SSL *conn = ssl->ssl; |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
458 | char *buffer = NULL; |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
459 | size_t len = 0; |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
460 | if ((len = SSL_get_peer_finished(conn, NULL, 0)) != 0) { |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
461 | buffer = malloc(len); |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
462 | if (buffer == NULL) return 0; |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
463 | len = SSL_get_peer_finished(conn, buffer, len); |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
464 | lua_pushlstring(L, buffer, len); |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
465 | free(buffer); |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
466 | return 1; |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
467 | } else { |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
468 | return 0; |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
469 | } |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
470 | } |
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
471 | |
0 | 472 | /*---------------------------------------------------------------------------*/ |
473 | ||
474 | ||
475 | /** | |
476 | * SSL metamethods | |
477 | */ | |
31
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
478 | static luaL_Reg methods[] = { |
3
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
479 | {"close", meth_close}, |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
480 | {"getfd", meth_getfd}, |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
481 | {"dirty", meth_dirty}, |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
482 | {"dohandshake", meth_handshake}, |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
483 | {"receive", meth_receive}, |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
484 | {"send", meth_send}, |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
485 | {"settimeout", meth_settimeout}, |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
486 | {"want", meth_want}, |
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
487 | {"compression", meth_compression}, |
4
718837c61318
Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents:
3
diff
changeset
|
488 | {"getpeercertificate",meth_getpeercertificate}, |
30
36ed99e1ce1e
ssl.core, context: Add ability to verify and continue, retrieve verification result
Paul Aurich <paul@darkrain42.org>
parents:
21
diff
changeset
|
489 | {"getpeerchainvalid", meth_getpeerchainvalid}, |
5
2d5a8f963181
Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
4
diff
changeset
|
490 | {"getfinished", meth_getfinished}, |
6
d559a15eeb40
Add :getpeerfinished() connection method to get peer's TLS Finished message
Tobias Markmann <tm@ayena.de>
parents:
5
diff
changeset
|
491 | {"getpeerfinished", meth_getpeerfinished}, |
3
bd2b1836f0ba
Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents:
2
diff
changeset
|
492 | {NULL, NULL} |
0 | 493 | }; |
494 | ||
495 | /** | |
496 | * SSL functions | |
497 | */ | |
498 | static luaL_Reg funcs[] = { | |
499 | {"create", meth_create}, | |
500 | {"setfd", meth_setfd}, | |
501 | {"rawconnection", meth_rawconn}, | |
502 | {NULL, NULL} | |
503 | }; | |
504 | ||
505 | /** | |
31
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
506 | * Context metamethods. |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
507 | */ |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
508 | static luaL_Reg meta[] = { |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
509 | {"__gc", meth_destroy}, |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
510 | {"__tostring", meth_tostring}, |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
511 | {NULL, NULL} |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
512 | }; |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
513 | |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
514 | /** |
0 | 515 | * Initialize modules |
516 | */ | |
517 | LUASEC_API int luaopen_ssl_core(lua_State *L) | |
518 | { | |
519 | /* Initialize SSL */ | |
520 | if (!SSL_library_init()) { | |
521 | lua_pushstring(L, "unable to initialize SSL library"); | |
522 | lua_error(L); | |
523 | } | |
524 | SSL_load_error_strings(); | |
525 | ||
526 | /* Initialize internal library */ | |
527 | socket_open(); | |
13
ebe0d286481c
src/ssl.c: Fix minor typo and whitespace
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
528 | |
ebe0d286481c
src/ssl.c: Fix minor typo and whitespace
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
529 | /* Register the functions and tables */ |
0 | 530 | luaL_newmetatable(L, "SSL:Connection"); |
31
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
531 | luaL_register(L, NULL, meta); |
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
532 | |
0 | 533 | lua_newtable(L); |
31
87625285de20
ssl.core: Add __tostring metamethod
Paul Aurich <paul@darkrain42.org>
parents:
30
diff
changeset
|
534 | luaL_register(L, NULL, methods); |
0 | 535 | lua_setfield(L, -2, "__index"); |
536 | ||
537 | luaL_register(L, "ssl.core", funcs); | |
538 | lua_pushnumber(L, SOCKET_INVALID); | |
539 | lua_setfield(L, -2, "invalidfd"); | |
540 | ||
541 | return 1; | |
542 | } | |
543 |