# HG changeset patch # User Matthew Wild # Date 1289057606 0 # Node ID bbff42d465128d7e91febe7c55f9f6c7b16d3d70 # Parent 4bc25168aa1c8de91da15fa1da4d2f472ca512eb x509: Add ssl.cert_from_pem() diff -r 4bc25168aa1c -r bbff42d46512 src/ssl.lua --- a/src/ssl.lua Sat Nov 06 13:04:09 2010 +0000 +++ b/src/ssl.lua Sat Nov 06 15:33:26 2010 +0000 @@ -19,6 +19,8 @@ rawconnection = core.rawconnection rawcontext = context.rawcontext +cert_from_pem = x509.cert_from_pem + -- -- -- diff -r 4bc25168aa1c -r bbff42d46512 src/x509.c --- a/src/x509.c Sat Nov 06 13:04:09 2010 +0000 +++ b/src/x509.c Sat Nov 06 15:33:26 2010 +0000 @@ -264,6 +264,22 @@ return 0; } +int cert_from_pem(lua_State* L) +{ + X509 *cert; + BIO *bio = BIO_new(BIO_s_mem()); + const char* data; size_t bytes; + data = luaL_checklstring(L, 1, &bytes); + BIO_write(bio, data, bytes); + cert = PEM_read_bio_X509(bio, NULL, NULL, NULL); + if(cert) + luasec_push_x509(L, cert); + else + lua_pushnil(L); + BIO_free(bio); + return 1; +} + /** * Certificate metamethods */ @@ -277,6 +293,14 @@ {NULL, NULL} }; +/** + * ssl.x509 functions + */ +static luaL_Reg funcs[] = { + {"cert_from_pem", cert_from_pem}, + {NULL, NULL} +}; + LUASEC_API int luaopen_ssl_x509(lua_State *L) { /* Register the functions and tables */ @@ -286,6 +310,7 @@ lua_setfield(L, -2, "__index"); lua_pushcfunction(L, meth_destroy); lua_setfield(L, -2, "__gc"); - lua_newtable(L); + + luaL_register(L, "ssl.x509", funcs); return 1; }