Fri, 05 Nov 2010 23:11:38 +0000
src/ssl.c: Include x509.h to shush compiler warning
0 | 1 | /*-------------------------------------------------------------------------- |
2 | * LuaSec 0.4 | |
3 | * Copyright (C) 2006-2009 Bruno Silvestre | |
4 | * | |
5 | *--------------------------------------------------------------------------*/ | |
6 | ||
7 | #include <string.h> | |
8 | #include <openssl/ssl.h> | |
9 | #include <openssl/err.h> | |
10 | ||
11 | #include <lua.h> | |
12 | #include <lauxlib.h> | |
13 | ||
14 | #include "context.h" | |
15 | ||
16 | struct ssl_option_s { | |
17 | const char *name; | |
18 | unsigned long code; | |
19 | }; | |
20 | typedef struct ssl_option_s ssl_option_t; | |
21 | ||
22 | ||
23 | static ssl_option_t ssl_options[] = { | |
24 | /* OpenSSL 0.9.7 and 0.9.8 */ | |
25 | {"all", SSL_OP_ALL}, | |
26 | {"cipher_server_preference", SSL_OP_CIPHER_SERVER_PREFERENCE}, | |
27 | {"dont_insert_empty_fragments", SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS}, | |
28 | {"ephemeral_rsa", SSL_OP_EPHEMERAL_RSA}, | |
29 | {"netscape_ca_dn_bug", SSL_OP_NETSCAPE_CA_DN_BUG}, | |
30 | {"netscape_challenge_bug", SSL_OP_NETSCAPE_CHALLENGE_BUG}, | |
31 | {"microsoft_big_sslv3_buffer", SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER}, | |
32 | {"microsoft_sess_id_bug", SSL_OP_MICROSOFT_SESS_ID_BUG}, | |
33 | {"msie_sslv2_rsa_padding", SSL_OP_MSIE_SSLV2_RSA_PADDING}, | |
34 | {"netscape_demo_cipher_change_bug", SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG}, | |
35 | {"netscape_reuse_cipher_change_bug", SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG}, | |
36 | {"no_session_resumption_on_renegotiation", | |
37 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION}, | |
38 | {"no_sslv2", SSL_OP_NO_SSLv2}, | |
39 | {"no_sslv3", SSL_OP_NO_SSLv3}, | |
40 | {"no_tlsv1", SSL_OP_NO_TLSv1}, | |
41 | {"pkcs1_check_1", SSL_OP_PKCS1_CHECK_1}, | |
42 | {"pkcs1_check_2", SSL_OP_PKCS1_CHECK_2}, | |
43 | {"single_dh_use", SSL_OP_SINGLE_DH_USE}, | |
44 | {"ssleay_080_client_dh_bug", SSL_OP_SSLEAY_080_CLIENT_DH_BUG}, | |
45 | {"sslref2_reuse_cert_type_bug", SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG}, | |
46 | {"tls_block_padding_bug", SSL_OP_TLS_BLOCK_PADDING_BUG}, | |
47 | {"tls_d5_bug", SSL_OP_TLS_D5_BUG}, | |
48 | {"tls_rollback_bug", SSL_OP_TLS_ROLLBACK_BUG}, | |
49 | /* OpenSSL 0.9.8 only */ | |
50 | #if OPENSSL_VERSION_NUMBER > 0x00908000L | |
51 | {"cookie_exchange", SSL_OP_COOKIE_EXCHANGE}, | |
52 | {"no_query_mtu", SSL_OP_NO_QUERY_MTU}, | |
53 | {"single_ecdh_use", SSL_OP_SINGLE_ECDH_USE}, | |
54 | #endif | |
55 | /* OpenSSL 0.9.8f and above */ | |
56 | #if defined(SSL_OP_NO_TICKET) | |
57 | {"no_ticket", SSL_OP_NO_TICKET}, | |
58 | #endif | |
1
5f89e535765a
context.c: Add no_compression option for when supported
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
59 | #if defined(SSL_OP_NO_COMPRESSION) |
5f89e535765a
context.c: Add no_compression option for when supported
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
60 | {"no_compression", SSL_OP_NO_COMPRESSION}, |
5f89e535765a
context.c: Add no_compression option for when supported
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
61 | #endif |
0 | 62 | {NULL, 0L} |
63 | }; | |
64 | ||
65 | /*--------------------------- Auxiliary Functions ----------------------------*/ | |
66 | ||
67 | /** | |
68 | * Return the context. | |
69 | */ | |
70 | static p_context checkctx(lua_State *L, int idx) | |
71 | { | |
72 | return (p_context)luaL_checkudata(L, idx, "SSL:Context"); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Prepare the SSL options flag. | |
77 | */ | |
78 | static int set_option_flag(const char *opt, unsigned long *flag) | |
79 | { | |
80 | ssl_option_t *p; | |
81 | for (p = ssl_options; p->name; p++) { | |
82 | if (!strcmp(opt, p->name)) { | |
83 | *flag |= p->code; | |
84 | return 1; | |
85 | } | |
86 | } | |
87 | return 0; | |
88 | } | |
89 | ||
90 | /** | |
91 | * Find the protocol. | |
92 | */ | |
93 | static SSL_METHOD* str2method(const char *method) | |
94 | { | |
95 | if (!strcmp(method, "sslv3")) return SSLv3_method(); | |
96 | if (!strcmp(method, "tlsv1")) return TLSv1_method(); | |
97 | if (!strcmp(method, "sslv23")) return SSLv23_method(); | |
98 | return NULL; | |
99 | } | |
100 | ||
101 | /** | |
102 | * Prepare the SSL handshake verify flag. | |
103 | */ | |
104 | static int set_verify_flag(const char *str, int *flag) | |
105 | { | |
106 | if (!strcmp(str, "none")) { | |
107 | *flag |= SSL_VERIFY_NONE; | |
108 | return 1; | |
109 | } | |
110 | if (!strcmp(str, "peer")) { | |
111 | *flag |= SSL_VERIFY_PEER; | |
112 | return 1; | |
113 | } | |
114 | if (!strcmp(str, "client_once")) { | |
115 | *flag |= SSL_VERIFY_CLIENT_ONCE; | |
116 | return 1; | |
117 | } | |
118 | if (!strcmp(str, "fail_if_no_peer_cert")) { | |
119 | *flag |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; | |
120 | return 1; | |
121 | } | |
122 | return 0; | |
123 | } | |
124 | ||
125 | /** | |
126 | * Password callback for reading the private key. | |
127 | */ | |
128 | static int passwd_cb(char *buf, int size, int flag, void *udata) | |
129 | { | |
130 | lua_State *L = (lua_State*)udata; | |
131 | switch (lua_type(L, 3)) { | |
132 | case LUA_TFUNCTION: | |
133 | lua_pushvalue(L, 3); | |
134 | lua_call(L, 0, 1); | |
135 | if (lua_type(L, -1) != LUA_TSTRING) | |
136 | return 0; | |
137 | /* fallback */ | |
138 | case LUA_TSTRING: | |
139 | strncpy(buf, lua_tostring(L, -1), size); | |
140 | buf[size-1] = '\0'; | |
141 | return (int)strlen(buf); | |
142 | } | |
143 | return 0; | |
144 | } | |
145 | ||
146 | /*------------------------------ Lua Functions -------------------------------*/ | |
147 | ||
148 | /** | |
149 | * Create a SSL context. | |
150 | */ | |
151 | static int create(lua_State *L) | |
152 | { | |
153 | p_context ctx; | |
154 | SSL_METHOD *method; | |
155 | ||
156 | method = str2method(luaL_checkstring(L, 1)); | |
157 | if (!method) { | |
158 | lua_pushnil(L); | |
159 | lua_pushstring(L, "invalid protocol"); | |
160 | return 2; | |
161 | } | |
162 | ctx = (p_context) lua_newuserdata(L, sizeof(t_context)); | |
163 | if (!ctx) { | |
164 | lua_pushnil(L); | |
165 | lua_pushstring(L, "error creating context"); | |
166 | return 2; | |
167 | } | |
168 | ctx->context = SSL_CTX_new(method); | |
169 | if (!ctx->context) { | |
170 | lua_pushnil(L); | |
171 | lua_pushstring(L, "error creating context"); | |
172 | return 2; | |
173 | } | |
174 | ctx->mode = MD_CTX_INVALID; | |
175 | /* No session support */ | |
176 | SSL_CTX_set_session_cache_mode(ctx->context, SSL_SESS_CACHE_OFF); | |
177 | luaL_getmetatable(L, "SSL:Context"); | |
178 | lua_setmetatable(L, -2); | |
179 | return 1; | |
180 | } | |
181 | ||
182 | /** | |
183 | * Load the trusting certificates. | |
184 | */ | |
185 | static int load_locations(lua_State *L) | |
186 | { | |
187 | SSL_CTX *ctx = ctx_getcontext(L, 1); | |
188 | const char *cafile = luaL_optstring(L, 2, NULL); | |
189 | const char *capath = luaL_optstring(L, 3, NULL); | |
190 | if (SSL_CTX_load_verify_locations(ctx, cafile, capath) != 1) { | |
191 | lua_pushboolean(L, 0); | |
192 | lua_pushfstring(L, "error loading CA locations (%s)", | |
193 | ERR_reason_error_string(ERR_get_error())); | |
194 | return 2; | |
195 | } | |
196 | lua_pushboolean(L, 1); | |
197 | return 1; | |
198 | } | |
199 | ||
200 | /** | |
201 | * Load the certificate file. | |
202 | */ | |
203 | static int load_cert(lua_State *L) | |
204 | { | |
205 | SSL_CTX *ctx = ctx_getcontext(L, 1); | |
206 | const char *filename = luaL_checkstring(L, 2); | |
207 | if (SSL_CTX_use_certificate_chain_file(ctx, filename) != 1) { | |
208 | lua_pushboolean(L, 0); | |
209 | lua_pushfstring(L, "error loading certificate (%s)", | |
210 | ERR_reason_error_string(ERR_get_error())); | |
211 | return 2; | |
212 | } | |
213 | lua_pushboolean(L, 1); | |
214 | return 1; | |
215 | } | |
216 | ||
217 | /** | |
218 | * Load the key file -- only in PEM format. | |
219 | */ | |
220 | static int load_key(lua_State *L) | |
221 | { | |
222 | int ret = 1; | |
223 | SSL_CTX *ctx = ctx_getcontext(L, 1); | |
224 | const char *filename = luaL_checkstring(L, 2); | |
225 | switch (lua_type(L, 3)) { | |
226 | case LUA_TSTRING: | |
227 | case LUA_TFUNCTION: | |
228 | SSL_CTX_set_default_passwd_cb(ctx, passwd_cb); | |
229 | SSL_CTX_set_default_passwd_cb_userdata(ctx, L); | |
230 | /* fallback */ | |
231 | case LUA_TNIL: | |
232 | if (SSL_CTX_use_PrivateKey_file(ctx, filename, SSL_FILETYPE_PEM) == 1) | |
233 | lua_pushboolean(L, 1); | |
234 | else { | |
235 | ret = 2; | |
236 | lua_pushboolean(L, 0); | |
237 | lua_pushfstring(L, "error loading private key (%s)", | |
238 | ERR_reason_error_string(ERR_get_error())); | |
239 | } | |
240 | SSL_CTX_set_default_passwd_cb(ctx, NULL); | |
241 | SSL_CTX_set_default_passwd_cb_userdata(ctx, NULL); | |
242 | break; | |
243 | default: | |
244 | lua_pushstring(L, "invalid callback value"); | |
245 | lua_error(L); | |
246 | } | |
247 | return ret; | |
248 | } | |
249 | ||
250 | /** | |
251 | * Set the cipher list. | |
252 | */ | |
253 | static int set_cipher(lua_State *L) | |
254 | { | |
255 | SSL_CTX *ctx = ctx_getcontext(L, 1); | |
256 | const char *list = luaL_checkstring(L, 2); | |
257 | if (SSL_CTX_set_cipher_list(ctx, list) != 1) { | |
258 | lua_pushboolean(L, 0); | |
259 | lua_pushfstring(L, "error setting cipher list (%s)", | |
260 | ERR_reason_error_string(ERR_get_error())); | |
261 | return 2; | |
262 | } | |
263 | lua_pushboolean(L, 1); | |
264 | return 1; | |
265 | } | |
266 | ||
267 | /** | |
268 | * Set the depth for certificate checking. | |
269 | */ | |
270 | static int set_depth(lua_State *L) | |
271 | { | |
272 | SSL_CTX *ctx = ctx_getcontext(L, 1); | |
273 | SSL_CTX_set_verify_depth(ctx, luaL_checkint(L, 2)); | |
274 | lua_pushboolean(L, 1); | |
275 | return 1; | |
276 | } | |
277 | ||
278 | /** | |
279 | * Set the handshake verify options. | |
280 | */ | |
281 | static int set_verify(lua_State *L) | |
282 | { | |
283 | int i; | |
284 | int flag = 0; | |
285 | SSL_CTX *ctx = ctx_getcontext(L, 1); | |
286 | int max = lua_gettop(L); | |
287 | /* any flag? */ | |
288 | if (max > 1) { | |
289 | for (i = 2; i <= max; i++) { | |
290 | if (!set_verify_flag(luaL_checkstring(L, i), &flag)) { | |
291 | lua_pushboolean(L, 0); | |
292 | lua_pushstring(L, "invalid verify option"); | |
293 | return 2; | |
294 | } | |
295 | } | |
296 | SSL_CTX_set_verify(ctx, flag, NULL); | |
297 | } | |
298 | lua_pushboolean(L, 1); | |
299 | return 1; | |
300 | } | |
301 | ||
302 | /** | |
303 | * Set the protocol options. | |
304 | */ | |
305 | static int set_options(lua_State *L) | |
306 | { | |
307 | int i; | |
308 | unsigned long flag = 0L; | |
309 | SSL_CTX *ctx = ctx_getcontext(L, 1); | |
310 | int max = lua_gettop(L); | |
311 | /* any option? */ | |
312 | if (max > 1) { | |
313 | for (i = 2; i <= max; i++) { | |
314 | if (!set_option_flag(luaL_checkstring(L, i), &flag)) { | |
315 | lua_pushboolean(L, 0); | |
316 | lua_pushstring(L, "invalid option"); | |
317 | return 2; | |
318 | } | |
319 | } | |
320 | SSL_CTX_set_options(ctx, flag); | |
321 | } | |
322 | lua_pushboolean(L, 1); | |
323 | return 1; | |
324 | } | |
325 | ||
326 | /** | |
327 | * Set the context mode. | |
328 | */ | |
329 | static int set_mode(lua_State *L) | |
330 | { | |
331 | p_context ctx = checkctx(L, 1); | |
332 | const char *str = luaL_checkstring(L, 2); | |
333 | if (!strcmp("server", str)) { | |
334 | ctx->mode = MD_CTX_SERVER; | |
335 | lua_pushboolean(L, 1); | |
336 | return 1; | |
337 | } | |
338 | if(!strcmp("client", str)) { | |
339 | ctx->mode = MD_CTX_CLIENT; | |
340 | lua_pushboolean(L, 1); | |
341 | return 1; | |
342 | } | |
343 | lua_pushboolean(L, 0); | |
344 | lua_pushstring(L, "invalid mode"); | |
345 | return 1; | |
346 | } | |
347 | ||
348 | /** | |
349 | * Return a pointer to SSL_CTX structure. | |
350 | */ | |
351 | static int raw_ctx(lua_State *L) | |
352 | { | |
353 | p_context ctx = checkctx(L, 1); | |
354 | lua_pushlightuserdata(L, (void*)ctx->context); | |
355 | return 1; | |
356 | } | |
357 | ||
358 | /** | |
359 | * Package functions | |
360 | */ | |
361 | static luaL_Reg funcs[] = { | |
362 | {"create", create}, | |
363 | {"locations", load_locations}, | |
364 | {"loadcert", load_cert}, | |
365 | {"loadkey", load_key}, | |
366 | {"setcipher", set_cipher}, | |
367 | {"setdepth", set_depth}, | |
368 | {"setverify", set_verify}, | |
369 | {"setoptions", set_options}, | |
370 | {"setmode", set_mode}, | |
371 | {"rawcontext", raw_ctx}, | |
372 | {NULL, NULL} | |
373 | }; | |
374 | ||
375 | /*-------------------------------- Metamethods -------------------------------*/ | |
376 | ||
377 | /** | |
378 | * Collect SSL context -- GC metamethod. | |
379 | */ | |
380 | static int meth_destroy(lua_State *L) | |
381 | { | |
382 | p_context ctx = checkctx(L, 1); | |
383 | if (ctx->context) { | |
384 | SSL_CTX_free(ctx->context); | |
385 | ctx->context = NULL; | |
386 | } | |
387 | return 0; | |
388 | } | |
389 | ||
390 | /** | |
391 | * Object information -- tostring metamethod. | |
392 | */ | |
393 | static int meth_tostring(lua_State *L) | |
394 | { | |
395 | p_context ctx = checkctx(L, 1); | |
396 | lua_pushfstring(L, "SSL context: %p", ctx); | |
397 | return 1; | |
398 | } | |
399 | ||
400 | /** | |
401 | * Context metamethods. | |
402 | */ | |
403 | static luaL_Reg meta[] = { | |
404 | {"__gc", meth_destroy}, | |
405 | {"__tostring", meth_tostring}, | |
406 | {NULL, NULL} | |
407 | }; | |
408 | ||
409 | ||
410 | /*----------------------------- Public Functions ---------------------------*/ | |
411 | ||
412 | /** | |
413 | * Retrieve the SSL context from the Lua stack. | |
414 | */ | |
415 | SSL_CTX* ctx_getcontext(lua_State *L, int idx) | |
416 | { | |
417 | p_context ctx = checkctx(L, idx); | |
418 | return ctx->context; | |
419 | } | |
420 | ||
421 | /** | |
422 | * Retrieve the mode from the context in the Lua stack. | |
423 | */ | |
424 | char ctx_getmode(lua_State *L, int idx) | |
425 | { | |
426 | p_context ctx = checkctx(L, idx); | |
427 | return ctx->mode; | |
428 | } | |
429 | ||
430 | /*------------------------------ Initialization ------------------------------*/ | |
431 | ||
432 | /** | |
433 | * Registre the module. | |
434 | */ | |
435 | int luaopen_ssl_context(lua_State *L) | |
436 | { | |
437 | luaL_newmetatable(L, "SSL:Context"); | |
438 | luaL_register(L, NULL, meta); | |
439 | luaL_register(L, "ssl.context", funcs); | |
440 | return 1; | |
441 | } |