From 1ade1542d712c95af41b4e9d93f8a115a9b52c7b Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 8 Jun 2014 12:38:52 +0200 Subject: [PATCH 01/15] Push nil if unable to encode ASN1 string as UTF-8 --- src/x509.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/x509.c b/src/x509.c index fd893db..e89b602 100644 --- a/src/x509.c +++ b/src/x509.c @@ -101,6 +101,8 @@ static void push_asn1_string(lua_State* L, ASN1_STRING *string, int encode) lua_pushlstring(L, (char*)data, len); OPENSSL_free(data); } + else + lua_pushnil(L); } } From c276e9ff608cc9bad19e599044ec238a6eb890ab Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 8 Jun 2014 12:41:20 +0200 Subject: [PATCH 02/15] Return early if ASN1 string is invalid --- src/x509.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/x509.c b/src/x509.c index e89b602..d34353c 100644 --- a/src/x509.c +++ b/src/x509.c @@ -88,8 +88,10 @@ static void push_asn1_string(lua_State* L, ASN1_STRING *string, int encode) { int len; unsigned char *data; - if (!string) + if (!string) { lua_pushnil(L); + return; + } switch (encode) { case LSEC_AI5_STRING: lua_pushlstring(L, (char*)ASN1_STRING_data(string), From b83d2c6a91680848325498d5eee5c9c4f5eff00d Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 8 Jun 2014 12:47:58 +0200 Subject: [PATCH 03/15] Don't try to encode IP addresses as UTF-8 --- src/x509.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/x509.c b/src/x509.c index d34353c..dcaca90 100644 --- a/src/x509.c +++ b/src/x509.c @@ -262,7 +262,7 @@ int meth_extensions(lua_State* L) case GEN_IPADD: lua_pushstring(L, "iPAddress"); push_subtable(L, -2); - push_asn1_string(L, general_name->d.iPAddress, px->encode); + push_asn1_string(L, general_name->d.iPAddress, LSEC_AI5_STRING); lua_rawseti(L, -2, lua_rawlen(L, -2)+1); lua_pop(L, 1); break; From f13aee5dacc263e6318652197230aa1437818760 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 8 Jun 2014 13:20:47 +0200 Subject: [PATCH 04/15] Encode iPAddress fields in human readable form --- src/x509.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/x509.c b/src/x509.c index dcaca90..6e09237 100644 --- a/src/x509.c +++ b/src/x509.c @@ -20,6 +20,8 @@ #include #include +#include + #include #include @@ -123,6 +125,31 @@ static int push_asn1_time(lua_State *L, ASN1_UTCTIME *tm) return 1; } +/** + * Return a human readable IP address. + */ +static void push_asn1_ip(lua_State *L, ASN1_STRING *string) +{ + unsigned char *ip = ASN1_STRING_data(string); + char dst[INET6_ADDRSTRLEN]; + int typ; + switch(ASN1_STRING_length(string)) { + case 4: + typ = AF_INET; + break; + case 16: + typ = AF_INET6; + break; + default: + lua_pushnil(L); + return; + } + if(inet_ntop(typ, ip, dst, INET6_ADDRSTRLEN)) + lua_pushstring(L, dst); + else + lua_pushnil(L); +} + /** * */ @@ -262,7 +289,7 @@ int meth_extensions(lua_State* L) case GEN_IPADD: lua_pushstring(L, "iPAddress"); push_subtable(L, -2); - push_asn1_string(L, general_name->d.iPAddress, LSEC_AI5_STRING); + push_asn1_ip(L, general_name->d.iPAddress); lua_rawseti(L, -2, lua_rawlen(L, -2)+1); lua_pop(L, 1); break; From 84cb83b92f2c695064aa0a72e0eee0c34fd46c15 Mon Sep 17 00:00:00 2001 From: Bruno Silvestre Date: Tue, 9 Sep 2014 21:48:26 -0300 Subject: [PATCH 05/15] - Add a parameter to server:sni(), so that we can accept an unknown name, using the initial context. - Add the method :getsniname() to retrieve the SNI hostname used. --- samples/sni/client.lua | 3 ++- samples/sni/server.lua | 6 ++++-- src/ssl.c | 49 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/samples/sni/client.lua b/samples/sni/client.lua index 1487098..79eb004 100644 --- a/samples/sni/client.lua +++ b/samples/sni/client.lua @@ -19,7 +19,8 @@ conn = ssl.wrap(conn, params) -- Comment the lines to not send a name --conn:sni("servera.br") -conn:sni("serveraa.br") +--conn:sni("serveraa.br") +conn:sni("serverb.br") assert(conn:dohandshake()) -- diff --git a/samples/sni/server.lua b/samples/sni/server.lua index 8ac4be2..101bc2f 100644 --- a/samples/sni/server.lua +++ b/samples/sni/server.lua @@ -39,10 +39,12 @@ local conn = server:accept() conn = ssl.wrap(conn, ctx01) -- Configure the name map -conn:sni({ +local sni_map = { ["servera.br"] = ctx01, ["serveraa.br"] = ctx02, -}) +} + +conn:sni(sni_map, true) assert(conn:dohandshake()) -- diff --git a/src/ssl.c b/src/ssl.c index 92d0881..b5591ac 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -660,6 +660,7 @@ static int meth_info(lua_State *L) static int sni_cb(SSL *ssl, int *ad, void *arg) { + int strict; SSL_CTX *newctx = NULL; SSL_CTX *ctx = SSL_get_SSL_CTX(ssl); lua_State *L = ((p_context)SSL_CTX_get_app_data(ctx))->L; @@ -667,41 +668,54 @@ static int sni_cb(SSL *ssl, int *ad, void *arg) /* No name, use default context */ if (!name) return SSL_TLSEXT_ERR_NOACK; - /* Search for the name in the map */ + /* Retrieve struct from registry */ luaL_getmetatable(L, "SSL:SNI:Registry"); lua_pushlightuserdata(L, (void*)ssl); lua_gettable(L, -2); + /* Strict search? */ + lua_pushstring(L, "strict"); + lua_gettable(L, -2); + strict = lua_toboolean(L, -1); + lua_pop(L, 1); + /* Search for the name in the map */ + lua_pushstring(L, "map"); + lua_gettable(L, -2); lua_pushstring(L, name); lua_gettable(L, -2); if (lua_isuserdata(L, -1)) newctx = lsec_checkcontext(L, -1); - lua_pop(L, 3); + lua_pop(L, 4); + /* Found, use this context */ if (newctx) { SSL_set_SSL_CTX(ssl, newctx); return SSL_TLSEXT_ERR_OK; } + /* Not found, but use initial context */ + if (!strict) + return SSL_TLSEXT_ERR_OK; return SSL_TLSEXT_ERR_ALERT_FATAL; } static int meth_sni(lua_State *L) { + int strict; SSL_CTX *aux; const char *name; p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection"); SSL_CTX *ctx = SSL_get_SSL_CTX(ssl->ssl); p_context pctx = (p_context)SSL_CTX_get_app_data(ctx); - switch (pctx->mode) { - case LSEC_MODE_CLIENT: + if (pctx->mode == LSEC_MODE_CLIENT) { name = luaL_checkstring(L, 2); SSL_set_tlsext_host_name(ssl->ssl, name); - break; - case LSEC_MODE_SERVER: + return 0; + } else if (pctx->mode == LSEC_MODE_SERVER) { luaL_checktype(L, 2, LUA_TTABLE); + strict = lua_toboolean(L, 3); /* Check if the table contains only (string -> context) */ lua_pushnil(L); while (lua_next(L, 2)) { - luaL_checkstring(L, 3); - aux = lsec_checkcontext(L, 4); + luaL_checkstring(L, -2); + aux = lsec_checkcontext(L, -1); /* Set callback in every context */ SSL_CTX_set_tlsext_servername_callback(aux, sni_cb); /* leave the next key on the stack */ @@ -710,15 +724,31 @@ static int meth_sni(lua_State *L) /* Save table in the register */ luaL_getmetatable(L, "SSL:SNI:Registry"); lua_pushlightuserdata(L, (void*)ssl->ssl); + lua_newtable(L); + lua_pushstring(L, "map"); lua_pushvalue(L, 2); lua_settable(L, -3); + lua_pushstring(L, "strict"); + lua_pushboolean(L, strict); + lua_settable(L, -3); + lua_settable(L, -3); /* Set callback in the default context */ SSL_CTX_set_tlsext_servername_callback(ctx, sni_cb); - break; } return 0; } +static int meth_getsniname(lua_State *L) +{ + p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection"); + const char *name = SSL_get_servername(ssl->ssl, TLSEXT_NAMETYPE_host_name); + if (name) + lua_pushstring(L, name); + else + lua_pushnil(L); + return 1; +} + static int meth_copyright(lua_State *L) { lua_pushstring(L, "LuaSec 0.5 - Copyright (C) 2006-2011 Bruno Silvestre" @@ -742,6 +772,7 @@ static luaL_Reg methods[] = { {"getpeerchain", meth_getpeerchain}, {"getpeerverification", meth_getpeerverification}, {"getpeerfinished", meth_getpeerfinished}, + {"getsniname", meth_getsniname}, {"getstats", meth_getstats}, {"setstats", meth_setstats}, {"dirty", meth_dirty}, From f514e9fb1b7b61763beb6254326bab7ec7b17fd6 Mon Sep 17 00:00:00 2001 From: Bruno Silvestre Date: Wed, 10 Sep 2014 14:41:09 -0300 Subject: [PATCH 06/15] Problem on Win64, since double does not represent SOCKET_INVALID exactly. --- src/ssl.c | 4 ---- src/ssl.lua | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/ssl.c b/src/ssl.c index b5591ac..884edc7 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -837,8 +837,6 @@ LSEC_API int luaopen_ssl_core(lua_State *L) lua_setfield(L, -2, "__index"); luaL_register(L, "ssl.core", funcs); - lua_pushnumber(L, SOCKET_INVALID); - lua_setfield(L, -2, "invalidfd"); return 1; } @@ -870,8 +868,6 @@ LSEC_API int luaopen_ssl_core(lua_State *L) lua_newtable(L); luaL_setfuncs(L, funcs, 0); - lua_pushnumber(L, SOCKET_INVALID); - lua_setfield(L, -2, "invalidfd"); return 1; } diff --git a/src/ssl.lua b/src/ssl.lua index 220b8dc..1affb40 100644 --- a/src/ssl.lua +++ b/src/ssl.lua @@ -126,7 +126,7 @@ function wrap(sock, cfg) local s, msg = core.create(ctx) if s then core.setfd(s, sock:getfd()) - sock:setfd(core.invalidfd) + sock:setfd(-1) registry[s] = ctx return s end From 4c7339cace7a4b328ad5a27c0c1dc57338df7e8b Mon Sep 17 00:00:00 2001 From: Lluixhi Scura Date: Fri, 16 Jan 2015 08:55:22 -0800 Subject: [PATCH 07/15] Fix for LibreSSL/OPENSSL_NO_COMP --- src/ssl.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ssl.c b/src/ssl.c index 884edc7..c39ae3f 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -400,24 +400,29 @@ static int meth_want(lua_State *L) } return 1; } - + /** * Return the compression method used. */ static int meth_compression(lua_State *L) { - const COMP_METHOD *comp; p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection"); if (ssl->state != LSEC_STATE_CONNECTED) { lua_pushnil(L); lua_pushstring(L, "closed"); return 2; } + +#if !defined(OPENSSL_NO_COMP) + const COMP_METHOD *comp; comp = SSL_get_current_compression(ssl->ssl); if (comp) lua_pushstring(L, SSL_COMP_get_name(comp)); else lua_pushnil(L); +#else + lua_pushnil(L); +#endif return 1; } From 5240c02f3da38be9ad13c6d1b1aa58d67fc3d245 Mon Sep 17 00:00:00 2001 From: Lluixhi Scura Date: Fri, 16 Jan 2015 09:12:14 -0800 Subject: [PATCH 08/15] Changed for strict compiles. --- src/ssl.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ssl.c b/src/ssl.c index c39ae3f..63d3a97 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -406,15 +406,16 @@ static int meth_want(lua_State *L) */ static int meth_compression(lua_State *L) { +#if !defined(OPENSSL_NO_COMP) + const COMP_METHOD *comp; +#endif p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection"); if (ssl->state != LSEC_STATE_CONNECTED) { lua_pushnil(L); lua_pushstring(L, "closed"); return 2; } - #if !defined(OPENSSL_NO_COMP) - const COMP_METHOD *comp; comp = SSL_get_current_compression(ssl->ssl); if (comp) lua_pushstring(L, SSL_COMP_get_name(comp)); From ab42d4ec864b096a85da5ee28fd6341b81d387b1 Mon Sep 17 00:00:00 2001 From: Bruno Silvestre Date: Wed, 28 Jan 2015 16:19:19 -0200 Subject: [PATCH 09/15] Stop if we don't have a string. --- src/x509.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/x509.c b/src/x509.c index fd893db..4c8ef65 100644 --- a/src/x509.c +++ b/src/x509.c @@ -88,8 +88,10 @@ static void push_asn1_string(lua_State* L, ASN1_STRING *string, int encode) { int len; unsigned char *data; - if (!string) + if (!string) { lua_pushnil(L); + return; + } switch (encode) { case LSEC_AI5_STRING: lua_pushlstring(L, (char*)ASN1_STRING_data(string), From acbf575420fb79957ffdf22c0fb8b3caf16e9780 Mon Sep 17 00:00:00 2001 From: Bruno Silvestre Date: Wed, 28 Jan 2015 16:38:00 -0200 Subject: [PATCH 10/15] BSD headers. --- src/x509.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/x509.c b/src/x509.c index 6e09237..13b2118 100644 --- a/src/x509.c +++ b/src/x509.c @@ -20,6 +20,9 @@ #include #include +#include +#include +#include #include #include From 9cb5220759aabedc0b4639c1a169b9efc5994a57 Mon Sep 17 00:00:00 2001 From: Bruno Silvestre Date: Fri, 6 Feb 2015 16:53:34 -0200 Subject: [PATCH 11/15] Remove luaL_optint() and luaL_checkint(). --- src/context.c | 2 +- src/ssl.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/context.c b/src/context.c index 4d2c838..0136b18 100644 --- a/src/context.c +++ b/src/context.c @@ -429,7 +429,7 @@ static int set_cipher(lua_State *L) static int set_depth(lua_State *L) { SSL_CTX *ctx = lsec_checkcontext(L, 1); - SSL_CTX_set_verify_depth(ctx, luaL_checkint(L, 2)); + SSL_CTX_set_verify_depth(ctx, (int)luaL_checkinteger(L, 2)); lua_pushboolean(L, 1); return 1; } diff --git a/src/ssl.c b/src/ssl.c index 63d3a97..357c15f 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -330,7 +330,7 @@ static int meth_setfd(lua_State *L) p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection"); if (ssl->state != LSEC_STATE_NEW) luaL_argerror(L, 1, "invalid SSL object state"); - ssl->sock = luaL_checkint(L, 2); + ssl->sock = (t_socket)luaL_checkinteger(L, 2); socket_setnonblocking(&ssl->sock); SSL_set_fd(ssl->ssl, (int)ssl->sock); return 0; @@ -442,7 +442,7 @@ static int meth_getpeercertificate(lua_State *L) return 2; } /* Default to the first cert */ - n = luaL_optint(L, 2, 1); + n = (int)luaL_optinteger(L, 2, 1); /* This function is 1-based, but OpenSSL is 0-based */ --n; if (n < 0) { From 97b1974039aacbcaffe62d9e86d150c36b4d9463 Mon Sep 17 00:00:00 2001 From: Bruno Silvestre Date: Fri, 6 Feb 2015 17:44:08 -0200 Subject: [PATCH 12/15] Change to luaL_newlib(). --- src/context.c | 7 +++---- src/ssl.c | 6 ++---- src/x509.c | 6 ++---- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/context.c b/src/context.c index 0136b18..5925818 100644 --- a/src/context.c +++ b/src/context.c @@ -749,13 +749,12 @@ LSEC_API int luaopen_ssl_context(lua_State *L) luaL_setfuncs(L, meta, 0); /* Create __index metamethods for context */ - lua_newtable(L); - luaL_setfuncs(L, meta_index, 0); + luaL_newlib(L, meta_index); lua_setfield(L, -2, "__index"); /* Return the module */ - lua_newtable(L); - luaL_setfuncs(L, funcs, 0); + luaL_newlib(L, funcs); + return 1; } #endif diff --git a/src/ssl.c b/src/ssl.c index 357c15f..464e65a 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -868,12 +868,10 @@ LSEC_API int luaopen_ssl_core(lua_State *L) luaL_newmetatable(L, "SSL:Connection"); luaL_setfuncs(L, meta, 0); - lua_newtable(L); - luaL_setfuncs(L, methods, 0); + luaL_newlib(L, methods); lua_setfield(L, -2, "__index"); - lua_newtable(L); - luaL_setfuncs(L, funcs, 0); + luaL_newlib(L, funcs); return 1; } diff --git a/src/x509.c b/src/x509.c index 13b2118..eb1b6f6 100644 --- a/src/x509.c +++ b/src/x509.c @@ -591,12 +591,10 @@ LSEC_API int luaopen_ssl_x509(lua_State *L) luaL_newmetatable(L, "SSL:Certificate"); luaL_setfuncs(L, meta, 0); - lua_newtable(L); - luaL_setfuncs(L, methods, 0); + luaL_newlib(L, methods); lua_setfield(L, -2, "__index"); - lua_newtable(L); - luaL_setfuncs(L, funcs, 0); + luaL_newlib(L, funcs); return 1; } From 356e03a64d7888dfa13e7447500c71ffedd5d3e0 Mon Sep 17 00:00:00 2001 From: Bruno Silvestre Date: Fri, 6 Feb 2015 18:07:29 -0200 Subject: [PATCH 13/15] Stop using module(). --- src/https.lua | 36 ++++++++++++++++++++---------------- src/ssl.lua | 25 +++++++++++++++---------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/https.lua b/src/https.lua index bdfc17d..182877e 100644 --- a/src/https.lua +++ b/src/https.lua @@ -12,22 +12,18 @@ local ltn12 = require("ltn12") local http = require("socket.http") local url = require("socket.url") -local table = require("table") -local string = require("string") +local try = socket.try -local try = socket.try -local type = type -local pairs = pairs -local getmetatable = getmetatable - -module("ssl.https") - -_VERSION = "0.5" -_COPYRIGHT = "LuaSec 0.5 - Copyright (C) 2009-2014 PUC-Rio" - --- Default settings -PORT = 443 +-- +-- Module +-- +local _M = { + _VERSION = "0.5", + _COPYRIGHT = "LuaSec 0.5 - Copyright (C) 2009-2014 PUC-Rio", + PORT = 443, +} +-- TLS configuration local cfg = { protocol = "tlsv1", options = "all", @@ -40,7 +36,7 @@ local cfg = { -- Insert default HTTPS port. local function default_https_port(u) - return url.build(url.parse(u, {port = PORT})) + return url.build(url.parse(u, {port = _M.PORT})) end -- Convert an URL to a table according to Luasocket needs. @@ -113,7 +109,7 @@ end -- @param body optional (string) -- @return (string if url == string or 1), code, headers, status -- -function request(url, body) +local function request(url, body) local result_table = {} local stringrequest = type(url) == "string" if stringrequest then @@ -136,3 +132,11 @@ function request(url, body) end return res, code, headers, status end + +-------------------------------------------------------------------------------- +-- Export module +-- + +_M.request = request + +return _M diff --git a/src/ssl.lua b/src/ssl.lua index 1affb40..99036f5 100644 --- a/src/ssl.lua +++ b/src/ssl.lua @@ -8,14 +8,6 @@ local core = require("ssl.core") local context = require("ssl.context") local x509 = require("ssl.x509") -module("ssl", package.seeall) - -_VERSION = "0.5" -_COPYRIGHT = core.copyright() - --- Export -loadcertificate = x509.load - -- We must prevent the contexts to be collected before the connections, -- otherwise the C registry will be cleared. local registry = setmetatable({}, {__mode="k"}) @@ -37,7 +29,7 @@ end -- -- -- -function newcontext(cfg) +local function newcontext(cfg) local succ, msg, ctx -- Create the context ctx, msg = context.create(cfg.protocol) @@ -115,7 +107,7 @@ end -- -- -- -function wrap(sock, cfg) +local function wrap(sock, cfg) local ctx, msg if type(cfg) == "table" then ctx, msg = newcontext(cfg) @@ -170,3 +162,16 @@ end -- core.setmethod("info", info) +-------------------------------------------------------------------------------- +-- Export module +-- + +local _M = { + _VERSION = "0.5", + _COPYRIGHT = core.copyright(), + loadcertificate = x509.load, + newcontext = newcontext, + wrap = wrap, +} + +return _M From 91d378a86ef6dfd34f80ad10bb83ec3b5fd14a72 Mon Sep 17 00:00:00 2001 From: Bruno Silvestre Date: Thu, 12 Feb 2015 16:29:02 -0200 Subject: [PATCH 14/15] Fix unpack(). --- src/ssl.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ssl.lua b/src/ssl.lua index 99036f5..afc40a5 100644 --- a/src/ssl.lua +++ b/src/ssl.lua @@ -8,6 +8,8 @@ local core = require("ssl.core") local context = require("ssl.context") local x509 = require("ssl.x509") +local unpack = table.unpack or unpack + -- We must prevent the contexts to be collected before the connections, -- otherwise the C registry will be cleared. local registry = setmetatable({}, {__mode="k"}) From 1ab6fac9197b194ea94166dff7ad81579f6113ed Mon Sep 17 00:00:00 2001 From: Bruno Silvestre Date: Thu, 12 Feb 2015 16:32:54 -0200 Subject: [PATCH 15/15] Don't set globals from C. --- src/config.h | 6 +++++- src/context.c | 21 +-------------------- src/ssl.c | 34 +--------------------------------- src/x509.c | 23 +---------------------- 4 files changed, 8 insertions(+), 76 deletions(-) diff --git a/src/config.h b/src/config.h index 43fe5e2..5b6b47d 100644 --- a/src/config.h +++ b/src/config.h @@ -14,7 +14,11 @@ #endif #if (LUA_VERSION_NUM == 501) -#define lua_rawlen(L, i) lua_objlen(L, i) +#define setfuncs(L, R) luaL_register(L, NULL, R) +#define lua_rawlen(L, i) lua_objlen(L, i) +#define luaL_newlib(L, R) do { lua_newtable(L); luaL_register(L, NULL, R); } while(0) +#else +#define setfuncs(L, R) luaL_setfuncs(L, R, 0) #endif #endif diff --git a/src/context.c b/src/context.c index 5925818..6a295e1 100644 --- a/src/context.c +++ b/src/context.c @@ -723,30 +723,12 @@ int lsec_getmode(lua_State *L, int idx) /** * Registre the module. */ -#if (LUA_VERSION_NUM == 501) LSEC_API int luaopen_ssl_context(lua_State *L) { luaL_newmetatable(L, "SSL:DH:Registry"); /* Keep all DH callbacks */ luaL_newmetatable(L, "SSL:Verify:Registry"); /* Keep all verify flags */ luaL_newmetatable(L, "SSL:Context"); - luaL_register(L, NULL, meta); - - /* Create __index metamethods for context */ - lua_newtable(L); - luaL_register(L, NULL, meta_index); - lua_setfield(L, -2, "__index"); - - /* Register the module */ - luaL_register(L, "ssl.context", funcs); - return 1; -} -#else -LSEC_API int luaopen_ssl_context(lua_State *L) -{ - luaL_newmetatable(L, "SSL:DH:Registry"); /* Keep all DH callbacks */ - luaL_newmetatable(L, "SSL:Verify:Registry"); /* Keep all verify flags */ - luaL_newmetatable(L, "SSL:Context"); - luaL_setfuncs(L, meta, 0); + setfuncs(L, meta); /* Create __index metamethods for context */ luaL_newlib(L, meta_index); @@ -757,4 +739,3 @@ LSEC_API int luaopen_ssl_context(lua_State *L) return 1; } -#endif diff --git a/src/ssl.c b/src/ssl.c index 464e65a..1780ccc 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -816,7 +816,6 @@ static luaL_Reg funcs[] = { /** * Initialize modules. */ -#if (LUA_VERSION_NUM == 501) LSEC_API int luaopen_ssl_core(lua_State *L) { /* Initialize SSL */ @@ -836,37 +835,7 @@ LSEC_API int luaopen_ssl_core(lua_State *L) /* Register the functions and tables */ luaL_newmetatable(L, "SSL:Connection"); - luaL_register(L, NULL, meta); - - lua_newtable(L); - luaL_register(L, NULL, methods); - lua_setfield(L, -2, "__index"); - - luaL_register(L, "ssl.core", funcs); - - return 1; -} -#else -LSEC_API int luaopen_ssl_core(lua_State *L) -{ - /* Initialize SSL */ - if (!SSL_library_init()) { - lua_pushstring(L, "unable to initialize SSL library"); - lua_error(L); - } - OpenSSL_add_all_algorithms(); - SSL_load_error_strings(); - -#if defined(WITH_LUASOCKET) - /* Initialize internal library */ - socket_open(); -#endif - - luaL_newmetatable(L, "SSL:SNI:Registry"); - - /* Register the functions and tables */ - luaL_newmetatable(L, "SSL:Connection"); - luaL_setfuncs(L, meta, 0); + setfuncs(L, meta); luaL_newlib(L, methods); lua_setfield(L, -2, "__index"); @@ -875,4 +844,3 @@ LSEC_API int luaopen_ssl_core(lua_State *L) return 1; } -#endif diff --git a/src/x509.c b/src/x509.c index eb1b6f6..02e858f 100644 --- a/src/x509.c +++ b/src/x509.c @@ -566,30 +566,11 @@ static luaL_Reg funcs[] = { /*--------------------------------------------------------------------------*/ -#if (LUA_VERSION_NUM == 501) - LSEC_API int luaopen_ssl_x509(lua_State *L) { /* Register the functions and tables */ luaL_newmetatable(L, "SSL:Certificate"); - luaL_register(L, NULL, meta); - - lua_newtable(L); - luaL_register(L, NULL, methods); - lua_setfield(L, -2, "__index"); - - luaL_register(L, "ssl.x509", funcs); - - return 1; -} - -#else - -LSEC_API int luaopen_ssl_x509(lua_State *L) -{ - /* Register the functions and tables */ - luaL_newmetatable(L, "SSL:Certificate"); - luaL_setfuncs(L, meta, 0); + setfuncs(L, meta); luaL_newlib(L, methods); lua_setfield(L, -2, "__index"); @@ -598,5 +579,3 @@ LSEC_API int luaopen_ssl_x509(lua_State *L) return 1; } - -#endif