nixio: Rework TLS support, added support for CyaSSL
[project/luci.git] / libs / nixio / src / tls-context.c
1 /*
2  * nixio - Linux I/O library for lua
3  *
4  *   Copyright (C) 2009 Steven Barth <steven@midlink.org>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18
19 #include "nixio-tls.h"
20 #include <string.h>
21
22 static SSL_CTX* nixio__checktlsctx(lua_State *L) {
23         SSL_CTX **ctx = (SSL_CTX **)luaL_checkudata(L, 1, NIXIO_TLS_CTX_META);
24         luaL_argcheck(L, *ctx, 1, "invalid context");
25         return *ctx;
26 }
27
28 static int nixio__tls_perror(lua_State *L, int code) {
29         lua_pushnil(L);
30         lua_pushinteger(L, code);
31         return 2;
32 }
33
34 static int nixio__tls_pstatus(lua_State *L, int code) {
35         if (code == 1) {
36                 lua_pushboolean(L, 1);
37                 return 1;
38         } else {
39                 return nixio__tls_perror(L, code);
40         }
41 }
42
43 static int nixio_tls_ctx(lua_State * L) {
44         const char *method = luaL_optlstring(L, 1, "client", NULL);
45
46         luaL_getmetatable(L, NIXIO_TLS_CTX_META);
47         SSL_CTX **ctx = lua_newuserdata(L, sizeof(SSL_CTX *));
48         if (!ctx) {
49                 return luaL_error(L, "out of memory");
50         }
51
52         /* create userdata */
53         lua_pushvalue(L, -2);
54         lua_setmetatable(L, -2);
55
56         if (!strcmp(method, "client")) {
57                 *ctx = SSL_CTX_new(TLSv1_client_method());
58         } else if (!strcmp(method, "server")) {
59                 *ctx = SSL_CTX_new(TLSv1_server_method());
60         } else {
61                 return luaL_argerror(L, 1, "supported values: client, server");
62         }
63
64         if (!(*ctx)) {
65                 return luaL_error(L, "unable to create TLS context");
66         }
67
68 #ifdef WITH_CYASSL
69         SSL_CTX_set_verify(*ctx, SSL_VERIFY_NONE, NULL);
70 #endif
71
72         return 1;
73 }
74
75 static int nixio_tls_ctx_create(lua_State *L) {
76         SSL_CTX *ctx = nixio__checktlsctx(L);
77         int fd = nixio__checkfd(L, 2);
78
79         lua_createtable(L, 0, 3);
80         nixio_tls_sock *sock = lua_newuserdata(L, sizeof(nixio_tls_sock));
81         if (!sock) {
82                 return luaL_error(L, "out of memory");
83         }
84         memset(sock, 0, sizeof(nixio_tls_sock));
85
86         /* create userdata */
87         luaL_getmetatable(L, NIXIO_TLS_SOCK_META);
88         lua_pushvalue(L, -1);
89         lua_setmetatable(L, -3);
90
91         sock->socket = SSL_new(ctx);
92         if (!sock->socket) {
93                 return nixio__tls_perror(L, 0);
94         }
95
96         if (SSL_set_fd(sock->socket, fd) != 1) {
97                 return nixio__tls_perror(L, 0);
98         }
99
100         /* save context and socket to prevent GC from collecting them */
101         lua_setmetatable(L, -3);
102         lua_setfield(L, -2, "connection");
103
104         lua_pushvalue(L, 1);
105         lua_setfield(L, -2, "context");
106
107         lua_pushvalue(L, 2);
108         lua_setfield(L, -2, "socket");
109
110         return 1;
111 }
112
113 static int nixio_tls_ctx_set_cert(lua_State *L) {
114         SSL_CTX *ctx = nixio__checktlsctx(L);
115         const char *cert = luaL_checkstring(L, 2);
116         return nixio__tls_pstatus(L, SSL_CTX_use_certificate_chain_file(ctx, cert));
117 }
118
119 static int nixio_tls_ctx_set_key(lua_State *L) {
120         SSL_CTX *ctx = nixio__checktlsctx(L);
121         const char *cert = luaL_checkstring(L, 2);
122         const int ktype = SSL_FILETYPE_PEM;
123         return nixio__tls_pstatus(L, SSL_CTX_use_PrivateKey_file(ctx, cert, ktype));
124 }
125
126 static int nixio_tls_ctx_set_ciphers(lua_State *L) {
127         SSL_CTX *ctx = nixio__checktlsctx(L);
128         size_t len;
129         const char *ciphers = luaL_checklstring(L, 2, &len);
130         luaL_argcheck(L, len < 255, 2, "cipher string too long");
131         return nixio__tls_pstatus(L, SSL_CTX_set_cipher_list(ctx, ciphers));
132 }
133
134 static int nixio_tls_ctx_set_verify(lua_State *L) {
135         SSL_CTX *ctx = nixio__checktlsctx(L);
136         const int j = lua_gettop(L);
137         int flags = 0;
138         for (int i=2; i<=j; i++) {
139                 const char *flag = luaL_checkstring(L, i);
140                 if (!strcmp(flag, "none")) {
141                         flags |= SSL_VERIFY_NONE;
142                 } else if (!strcmp(flag, "peer")) {
143                         flags |= SSL_VERIFY_PEER;
144                 } else if (!strcmp(flag, "verify_fail_if_no_peer_cert")) {
145                         flags |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
146                 } else if (!strcmp(flag, "client_once")) {
147                         flags |= SSL_VERIFY_CLIENT_ONCE;
148                 } else {
149                         return luaL_argerror(L, i, "supported values: none, peer, "
150                          "verify_fail_if_no_peer_cert, client_once");
151                 }
152         }
153         SSL_CTX_set_verify(ctx, flags, NULL);
154         return 0;
155 }
156
157 static int nixio_tls_ctx__gc(lua_State *L) {
158         SSL_CTX **ctx = (SSL_CTX **)luaL_checkudata(L, 1, NIXIO_TLS_CTX_META);
159         if (*ctx) {
160                 SSL_CTX_free(*ctx);
161                 *ctx = NULL;
162         }
163         return 0;
164 }
165
166 static int nixio_tls_ctx__tostring(lua_State *L) {
167         SSL_CTX *ctx = nixio__checktlsctx(L);
168         lua_pushfstring(L, "nixio TLS context: %p", ctx);
169         return 1;
170 }
171
172 /* module table */
173 static const luaL_reg R[] = {
174         {"tls",         nixio_tls_ctx},
175         {NULL,                  NULL}
176 };
177
178 /* ctx function table */
179 static const luaL_reg CTX_M[] = {
180         {"set_cert",                    nixio_tls_ctx_set_cert},
181         {"set_key",                             nixio_tls_ctx_set_key},
182         {"set_ciphers",                 nixio_tls_ctx_set_ciphers},
183         {"set_verify",                  nixio_tls_ctx_set_verify},
184         {"create",                              nixio_tls_ctx_create},
185         {"__gc",                                nixio_tls_ctx__gc},
186         {"__tostring",                  nixio_tls_ctx__tostring},
187         {NULL,                                  NULL}
188 };
189
190
191 void nixio_open_tls_context(lua_State *L) {
192         /* initialize tls library */
193     SSL_load_error_strings();
194     SSL_library_init();
195
196     /* register module functions */
197     luaL_register(L, NULL, R);
198
199 #if defined (WITH_AXTLS)
200     lua_pushliteral(L, "axtls");
201 #elif defined (WITH_CYASSL)
202     lua_pushliteral(L, "cyassl");
203 #else
204     lua_pushliteral(L, "openssl");
205 #endif
206     lua_setfield(L, -2, "tls_provider");
207
208         /* create context metatable */
209         luaL_newmetatable(L, NIXIO_TLS_CTX_META);
210         lua_pushvalue(L, -1);
211         lua_setfield(L, -2, "__index");
212         luaL_register(L, NULL, CTX_M);
213         lua_setfield(L, -2, "meta_tls_context");
214 }