# HG changeset patch # User Matthew Wild # Date 1288992330 0 # Node ID 0cefcdd5b635f40f6bba0b6697e2f6fec468829c # Parent f1de983ff6596b963d3dbe6b5173c5455c7c80d3 Add :pem() method to certificates diff -r f1de983ff659 -r 0cefcdd5b635 src/x509.c --- a/src/x509.c Fri Nov 05 21:25:13 2010 +0000 +++ b/src/x509.c Fri Nov 05 21:25:30 2010 +0000 @@ -151,11 +151,31 @@ return 1; } +int meth_pem(lua_State* L) +{ + X509* cert = luasec_to_x509(L, 1); + BIO *bio = BIO_new(BIO_s_mem()); + char* data; long bytes; + if(!PEM_write_bio_X509(bio, cert)) + { + lua_pushnil(L); + return 1; + } + bytes = BIO_get_mem_data(bio, &data); + if(bytes > 0) + lua_pushlstring(L, data, bytes); + else + lua_pushnil(L); + BIO_free(bio); + return 1; +} + /** * Certificate metamethods */ static luaL_Reg meta[] = { {"decode", meth_decode}, + {"pem", meth_pem}, {NULL, NULL} };