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

@ -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}
};