src/ssl.c

Sat, 20 Nov 2010 20:04:11 -0800

author
Paul Aurich <paul@darkrain42.org>
date
Sat, 20 Nov 2010 20:04:11 -0800
changeset 31
87625285de20
parent 30
36ed99e1ce1e
child 34
510432315106
permissions
-rw-r--r--

ssl.core: Add __tostring metamethod

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

mercurial