src/ssl.c

Fri, 05 Nov 2010 23:11:38 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 05 Nov 2010 23:11:38 +0000
changeset 19
45b7299e4746
parent 14
1927b7b32faf
child 20
ad5eb4fd28f5
permissions
-rw-r--r--

src/ssl.c: Include x509.h to shush compiler warning

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
11
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
23 #define min(a, b) (a<b)?a:b
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
24
0
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 * Map error code into string.
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 static const char *ssl_ioerror(void *ctx, int err)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 if (err == IO_SSL) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 p_ssl ssl = (p_ssl) ctx;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 switch(ssl->error) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 case SSL_ERROR_NONE: return "No error";
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 case SSL_ERROR_ZERO_RETURN: return "closed";
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 case SSL_ERROR_WANT_READ: return "wantread";
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 case SSL_ERROR_WANT_WRITE: return "wantwrite";
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 case SSL_ERROR_WANT_CONNECT: return "'connect' not completed";
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 case SSL_ERROR_WANT_ACCEPT: return "'accept' not completed";
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 case SSL_ERROR_WANT_X509_LOOKUP: return "Waiting for callback";
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 case SSL_ERROR_SYSCALL: return "System error";
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 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
42 default: return "Unknown SSL error";
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 }
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 return socket_strerror(err);
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
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 * Close the connection before the GC collect the object.
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 static int meth_destroy(lua_State *L)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 p_ssl ssl = (p_ssl) lua_touserdata(L, 1);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 if (ssl->ssl) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 socket_setblocking(&ssl->sock);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 SSL_shutdown(ssl->ssl);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 socket_destroy(&ssl->sock);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 SSL_free(ssl->ssl);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 ssl->ssl = NULL;
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 return 0;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 /**
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 * Perform the TLS/SSL handshake
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 */
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 static int handshake(p_ssl ssl)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 int err;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 p_timeout tm = timeout_markstart(&ssl->tm);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 if (ssl->state == ST_SSL_CLOSED)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 return IO_CLOSED;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 for ( ; ; ) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 ERR_clear_error();
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 err = SSL_do_handshake(ssl->ssl);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 ssl->error = SSL_get_error(ssl->ssl, err);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 switch(ssl->error) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 case SSL_ERROR_NONE:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 ssl->state = ST_SSL_CONNECTED;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 return IO_DONE;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 case SSL_ERROR_WANT_READ:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 err = socket_waitfd(&ssl->sock, WAITFD_R, tm);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 if (err == IO_TIMEOUT) return IO_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 if (err != IO_DONE) return err;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 break;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 case SSL_ERROR_WANT_WRITE:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 err = socket_waitfd(&ssl->sock, WAITFD_W, tm);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 if (err == IO_TIMEOUT) return IO_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 if (err != IO_DONE) return err;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 break;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 case SSL_ERROR_SYSCALL:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 if (ERR_peek_error()) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 ssl->error = SSL_ERROR_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 return IO_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 if (err == 0)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 return IO_CLOSED;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 return socket_error();
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 default:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 return IO_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 return IO_UNKNOWN;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 /**
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 * Send data
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 */
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 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
110 p_timeout tm)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 int err;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 p_ssl ssl = (p_ssl) ctx;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 if (ssl->state == ST_SSL_CLOSED)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 return IO_CLOSED;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 *sent = 0;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 for ( ; ; ) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 ERR_clear_error();
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 err = SSL_write(ssl->ssl, data, (int) count);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 ssl->error = SSL_get_error(ssl->ssl, err);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 switch(ssl->error) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 case SSL_ERROR_NONE:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 *sent = err;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 return IO_DONE;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 case SSL_ERROR_WANT_READ:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 err = socket_waitfd(&ssl->sock, WAITFD_R, tm);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 if (err == IO_TIMEOUT) return IO_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 if (err != IO_DONE) return err;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 break;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 case SSL_ERROR_WANT_WRITE:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 err = socket_waitfd(&ssl->sock, WAITFD_W, tm);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 if (err == IO_TIMEOUT) return IO_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 if (err != IO_DONE) return err;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 break;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 case SSL_ERROR_SYSCALL:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 if (ERR_peek_error()) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 ssl->error = SSL_ERROR_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 return IO_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 if (err == 0)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 return IO_CLOSED;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 return socket_error();
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 default:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 return IO_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 return IO_UNKNOWN;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 /**
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 * Receive data
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 */
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 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
154 p_timeout tm)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 int err;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 p_ssl ssl = (p_ssl) ctx;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 if (ssl->state == ST_SSL_CLOSED)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 return IO_CLOSED;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 *got = 0;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 for ( ; ; ) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 ERR_clear_error();
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 err = SSL_read(ssl->ssl, data, (int) count);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 ssl->error = SSL_get_error(ssl->ssl, err);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 switch(ssl->error) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 case SSL_ERROR_NONE:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 *got = err;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 return IO_DONE;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 case SSL_ERROR_ZERO_RETURN:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 *got = err;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 return IO_CLOSED;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 case SSL_ERROR_WANT_READ:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 err = socket_waitfd(&ssl->sock, WAITFD_R, tm);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 if (err == IO_TIMEOUT) return IO_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 if (err != IO_DONE) return err;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 break;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 case SSL_ERROR_WANT_WRITE:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 err = socket_waitfd(&ssl->sock, WAITFD_W, tm);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 if (err == IO_TIMEOUT) return IO_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 if (err != IO_DONE) return err;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 break;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 case SSL_ERROR_SYSCALL:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 if (ERR_peek_error()) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 ssl->error = SSL_ERROR_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 return IO_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 if (err == 0)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 return IO_CLOSED;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 return socket_error();
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 default:
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 return IO_SSL;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 return IO_UNKNOWN;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 /**
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 * 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
199 */
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 static int meth_create(lua_State *L)
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 p_ssl ssl;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 int mode = ctx_getmode(L, 1);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 SSL_CTX *ctx = ctx_getcontext(L, 1);
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 if (mode == MD_CTX_INVALID) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 lua_pushnil(L);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 lua_pushstring(L, "invalid mode");
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 return 2;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 ssl = (p_ssl) lua_newuserdata(L, sizeof(t_ssl));
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 if (!ssl) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 lua_pushnil(L);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 lua_pushstring(L, "error creating SSL object");
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 return 2;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 ssl->ssl = SSL_new(ctx);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 if (!ssl->ssl) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 lua_pushnil(L);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 lua_pushstring(L, "error creating SSL object");
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 return 2;;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 ssl->state = ST_SSL_NEW;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 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
225 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
226 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
227
0cfca30f1ce3 ssl.c: Set SSL_MODE_RELEASE_BUFFERS mode when supported
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
228 #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
229 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
230 #endif
0cfca30f1ce3 ssl.c: Set SSL_MODE_RELEASE_BUFFERS mode when supported
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
231
0
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 if (mode == MD_CTX_SERVER)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 SSL_set_accept_state(ssl->ssl);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 else
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 SSL_set_connect_state(ssl->ssl);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 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
238 (p_error) ssl_ioerror, ssl);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 timeout_init(&ssl->tm, -1, -1);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 buffer_init(&ssl->buf, &ssl->io, &ssl->tm);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 luaL_getmetatable(L, "SSL:Connection");
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 lua_setmetatable(L, -2);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 return 1;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 /**
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 * Buffer send function
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 static int meth_send(lua_State *L) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 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
252 return buffer_meth_send(L, &ssl->buf);
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 receive 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_receive(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_receive(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 * Select support methods
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_getfd(lua_State *L)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 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
269 lua_pushnumber(L, ssl->sock);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 return 1;
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
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 * Set the TLS/SSL file descriptor.
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 * This is done *before* the handshake.
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 */
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 static int meth_setfd(lua_State *L)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 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
280 if (ssl->state != ST_SSL_NEW)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 luaL_argerror(L, 1, "invalid SSL object state");
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 ssl->sock = luaL_checkint(L, 2);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 socket_setnonblocking(&ssl->sock);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 SSL_set_fd(ssl->ssl, (int)ssl->sock);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 return 0;
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
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 /**
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 * Lua handshake function.
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 */
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 static int meth_handshake(lua_State *L)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 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
294 int err = handshake(ssl);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 if (err == IO_DONE) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 lua_pushboolean(L, 1);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 return 1;
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 lua_pushboolean(L, 0);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 lua_pushstring(L, ssl_ioerror((void*)ssl, err));
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 return 2;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 /**
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 * Close the connection.
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 static int meth_close(lua_State *L)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 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
310 meth_destroy(L);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 ssl->state = ST_SSL_CLOSED;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 return 0;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 }
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 /**
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 * Set timeout.
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 */
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 static int meth_settimeout(lua_State *L)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 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
321 return timeout_meth_settimeout(L, &ssl->tm);
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 /**
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 * Check if there is data in the buffer.
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 */
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 static int meth_dirty(lua_State *L)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 int res = 0;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 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
331 if (ssl->state != ST_SSL_CLOSED)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 res = !buffer_isempty(&ssl->buf) || SSL_pending(ssl->ssl);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 lua_pushboolean(L, res);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 return 1;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 }
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 /**
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
338 * Return the state information about the SSL object.
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339 */
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340 static int meth_want(lua_State *L)
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 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
343 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
344 switch(code) {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 case SSL_NOTHING: lua_pushstring(L, "nothing"); break;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 case SSL_READING: lua_pushstring(L, "read"); break;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 case SSL_WRITING: lua_pushstring(L, "write"); break;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 case SSL_X509_LOOKUP: lua_pushstring(L, "x509lookup"); break;
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 return 1;
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 }
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353 /**
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 * Return a pointer to SSL structure.
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355 */
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 static int meth_rawconn(lua_State *L)
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 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
359 lua_pushlightuserdata(L, (void*)ssl->ssl);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 return 1;
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
3
bd2b1836f0ba Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents: 2
diff changeset
363 /**
bd2b1836f0ba Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents: 2
diff changeset
364 * 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
365 */
bd2b1836f0ba Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents: 2
diff changeset
366 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
367 {
bd2b1836f0ba Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents: 2
diff changeset
368 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
369 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
370 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
371 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
372 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
373 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
374 } else {
bd2b1836f0ba Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents: 2
diff changeset
375 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
376 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
377 }
bd2b1836f0ba Add :compression() connection method to get the compression method in use (if any)
Tobias Markmann <tm@ayena.de>
parents: 2
diff changeset
378 }
4
718837c61318 Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents: 3
diff changeset
379
11
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
380 void luasec_push_asn1_objname(lua_State* L, ASN1_OBJECT *object, int no_name)
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
381 {
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
382 char buffer[256];
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
383 int len = OBJ_obj2txt(buffer, sizeof(buffer), object, no_name);
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
384 lua_pushlstring(L, buffer, min(sizeof(buffer),len));
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
385 }
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
386
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
387 void luasec_push_asn1_string(lua_State* L, ASN1_STRING *string)
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
388 {
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
389 if(string)
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
390 lua_pushlstring(L, (char*)ASN1_STRING_data(string), ASN1_STRING_length(string));
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
391 else
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
392 lua_pushnil(L);
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
393 }
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
394
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
395 int luasec_push_subtable(lua_State* L, int idx)
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
396 {
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
397
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
398 lua_pushvalue(L, -1);
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
399 lua_gettable(L, idx-1);
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
400
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
401 if(lua_isnil(L, -1))
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
402 {
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
403 lua_pop(L, 1);
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
404 lua_newtable(L);
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
405 lua_pushvalue(L, -2);
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
406 lua_pushvalue(L, -2);
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
407 lua_settable(L, idx-3);
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
408
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
409 lua_replace(L, -2); /* Replace key with table */
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
410 return 1;
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
411 }
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
412 lua_replace(L, -2); /* Replace key with table */
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
413 return 0;
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
414 }
8d7698d3fd26 Refactoring of :getpeercertificate(), support for subjectAltName extensions
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
415
4
718837c61318 Add :getpeercertificate() method to get peer's certificate
Tobias Markmann <tm@ayena.de>
parents: 3
diff changeset
416 /**
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
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
472 /*---------------------------------------------------------------------------*/
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
473
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
474
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
475 /**
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
476 * SSL metamethods
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
477 */
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
478 static luaL_Reg meta[] = {
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},
5
2d5a8f963181 Add :getfinished() method to get local TLS Finished message
Tobias Markmann <tm@ayena.de>
parents: 4
diff changeset
489 {"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
490 {"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
491 {NULL, NULL}
0
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
492 };
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 * SSL functions
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
496 */
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
497 static luaL_Reg funcs[] = {
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
498 {"create", meth_create},
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
499 {"setfd", meth_setfd},
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
500 {"rawconnection", meth_rawconn},
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
501 {NULL, NULL}
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
502 };
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
503
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
504 /**
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");
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
521 lua_newtable(L);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
522 luaL_register(L, NULL, meta);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
523 lua_setfield(L, -2, "__index");
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
524 lua_pushcfunction(L, meth_destroy);
f7d2d78eb424 Initial commit (LuaSec 0.4)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
525 lua_setfield(L, -2, "__gc");
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