LuaSec 0.3

This commit is contained in:
Bruno Silvestre
2012-09-02 11:22:22 -03:00
parent 36e94ee40d
commit 1c95a077ee
18 changed files with 324 additions and 126 deletions

View File

@ -38,13 +38,13 @@ install: $(CMOD) $(LMOD)
$(CP) $(LMOD) $(LUAPATH)
linux:
@make $(CMOD) MYCFLAGS="$(LNX_CFLAGS)" MYLDFLAGS="$(LNX_LDFLAGS)"
@$(MAKE) $(CMOD) MYCFLAGS="$(LNX_CFLAGS)" MYLDFLAGS="$(LNX_LDFLAGS)"
bsd:
@make $(CMOD) MYCFLAGS="$(BSD_CFLAGS)" MYLDFLAGS="$(BSD_LDFLAGS)"
@$(MAKE) $(CMOD) MYCFLAGS="$(BSD_CFLAGS)" MYLDFLAGS="$(BSD_LDFLAGS)"
macosx:
@make $(CMOD) MYCFLAGS="$(MAC_CFLAGS)" MYLDFLAGS="$(MAC_LDFLAGS)" MYENV="$(MAC_ENV)"
@$(MAKE) $(CMOD) MYCFLAGS="$(MAC_CFLAGS)" MYLDFLAGS="$(MAC_LDFLAGS)" MYENV="$(MAC_ENV)"
$(CMOD): $(OBJS)

View File

@ -1,6 +1,6 @@
/*--------------------------------------------------------------------------
* LuaSec 0.2
* Copyright (C) 2006-2007 Bruno Silvestre
* LuaSec 0.3
* Copyright (C) 2006-2008 Bruno Silvestre
*
*--------------------------------------------------------------------------*/
@ -115,6 +115,27 @@ static int set_verify_flag(const char *str, int *flag)
return 0;
}
/**
* Password callback for reading the private key.
*/
static int passwd_cb(char *buf, int size, int flag, void *udata)
{
lua_State *L = (lua_State*)udata;
switch (lua_type(L, 3)) {
case LUA_TFUNCTION:
lua_pushvalue(L, 3);
lua_call(L, 0, 1);
if (lua_type(L, -1) != LUA_TSTRING)
return 0;
/* fallback */
case LUA_TSTRING:
strncpy(buf, lua_tostring(L, -1), size);
buf[size-1] = '\0';
return (int)strlen(buf);
}
return 0;
}
/*------------------------------ Lua Functions -------------------------------*/
/**
@ -191,17 +212,32 @@ static int load_cert(lua_State *L)
*/
static int load_key(lua_State *L)
{
int ret = 1;
SSL_CTX *ctx = ctx_getcontext(L, 1);
const char *filename = luaL_checkstring(L, 2);
if (SSL_CTX_use_PrivateKey_file(ctx, filename, SSL_FILETYPE_PEM) != 1) {
lua_pushboolean(L, 0);
lua_pushfstring(L, "error loading private key (%s)",
ERR_reason_error_string(ERR_get_error()));
return 2;
switch (lua_type(L, 3)) {
case LUA_TSTRING:
case LUA_TFUNCTION:
SSL_CTX_set_default_passwd_cb(ctx, passwd_cb);
SSL_CTX_set_default_passwd_cb_userdata(ctx, L);
/* fallback */
case LUA_TNIL:
if (SSL_CTX_use_PrivateKey_file(ctx, filename, SSL_FILETYPE_PEM) == 1)
lua_pushboolean(L, 1);
else {
ret = 2;
lua_pushboolean(L, 0);
lua_pushfstring(L, "error loading private key (%s)",
ERR_reason_error_string(ERR_get_error()));
}
SSL_CTX_set_default_passwd_cb(ctx, NULL);
SSL_CTX_set_default_passwd_cb_userdata(ctx, NULL);
break;
default:
lua_pushstring(L, "invalid callback value");
lua_error(L);
}
lua_pushboolean(L, 1);
return 1;
return ret;
}
/**
@ -302,6 +338,16 @@ static int set_mode(lua_State *L)
return 1;
}
/**
* Return a pointer to SSL_CTX structure.
*/
static int raw_ctx(lua_State *L)
{
p_context ctx = checkctx(L, 1);
lua_pushlightuserdata(L, (void*)ctx->context);
return 1;
}
/**
* Package functions
*/
@ -315,6 +361,7 @@ static luaL_Reg funcs[] = {
{"setverify", set_verify},
{"setoptions", set_options},
{"setmode", set_mode},
{"rawcontext", raw_ctx},
{NULL, NULL}
};

View File

@ -2,8 +2,8 @@
#define __CONTEXT_H__
/*--------------------------------------------------------------------------
* LuaSec 0.2
* Copyright (C) 2006-2007 Bruno Silvestre
* LuaSec 0.3
* Copyright (C) 2006-2008 Bruno Silvestre
*
*--------------------------------------------------------------------------*/

View File

@ -1,6 +1,6 @@
/*--------------------------------------------------------------------------
* LuaSec 0.2
* Copyright (C) 2006-2007 Bruno Silvestre
* LuaSec 0.3
* Copyright (C) 2006-2008 Bruno Silvestre
*
*--------------------------------------------------------------------------*/
@ -334,6 +334,16 @@ static int meth_want(lua_State *L)
return 1;
}
/**
* Return a pointer to SSL structure.
*/
static int meth_rawconn(lua_State *L)
{
p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection");
lua_pushlightuserdata(L, (void*)ssl->ssl);
return 1;
}
/*---------------------------------------------------------------------------*/
@ -356,9 +366,10 @@ static luaL_Reg meta[] = {
* SSL functions
*/
static luaL_Reg funcs[] = {
{"create", meth_create},
{"setfd", meth_setfd},
{NULL, NULL}
{"create", meth_create},
{"setfd", meth_setfd},
{"rawconnection", meth_rawconn},
{NULL, NULL}
};
/**

View File

@ -2,8 +2,8 @@
#define __SSL_H__
/*--------------------------------------------------------------------------
* LuaSec 0.2
* Copyright (C) 2006-2007 Bruno Silvestre
* LuaSec 0.3
* Copyright (C) 2006-2008 Bruno Silvestre
*
*--------------------------------------------------------------------------*/

View File

@ -1,6 +1,6 @@
------------------------------------------------------------------------------
-- LuaSec 0.2
-- Copyright (C) 2006-2007 Bruno Silvestre
-- LuaSec 0.3
-- Copyright (C) 2006-2008 Bruno Silvestre
--
------------------------------------------------------------------------------
@ -9,10 +9,15 @@ module("ssl", package.seeall)
require("ssl.core")
require("ssl.context")
_COPYRIGHT = "LuaSec 0.2 - Copyright (C) 2006-2007 Bruno Silvestre\n" ..
_VERSION = "0.3"
_COPYRIGHT = "LuaSec 0.3 - Copyright (C) 2006-2008 Bruno Silvestre\n" ..
"LuaSocket 2.0.2 - Copyright (C) 2004-2007 Diego Nehab"
-- Export functions
rawconnection = core.rawconnection
rawcontext = context.rawcontext
--
--
--
@ -39,11 +44,15 @@ function newcontext(cfg)
succ, msg = context.setmode(ctx, cfg.mode)
if not succ then return nil, msg end
-- Load the key
succ, msg = context.loadkey(ctx, cfg.key)
if not succ then return nil, msg end
if cfg.key then
succ, msg = context.loadkey(ctx, cfg.key, cfg.password)
if not succ then return nil, msg end
end
-- Load the certificate
succ, msg = context.loadcert(ctx, cfg.certificate)
if not succ then return nil, msg end
if cfg.certificate then
succ, msg = context.loadcert(ctx, cfg.certificate)
if not succ then return nil, msg end
end
-- Load the CA certificates
if cfg.cafile or cfg.capath then
succ, msg = context.locations(ctx, cfg.cafile, cfg.capath)