x509: Add ssl.cert_from_pem()

Sat, 06 Nov 2010 15:33:26 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 06 Nov 2010 15:33:26 +0000
changeset 26
bbff42d46512
parent 25
4bc25168aa1c
child 27
3e0325d39a61

x509: Add ssl.cert_from_pem()

src/ssl.lua file | annotate | diff | comparison | revisions
src/x509.c file | annotate | diff | comparison | revisions
--- 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
+
 --
 --
 --
--- 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;
 }

mercurial