From eb8cb331601c203c55d482465e81af148c4469cc Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 5 Feb 2014 01:39:30 +0100 Subject: [PATCH 1/2] Add method for extracting public key, type and size from x509 objects --- src/x509.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/x509.c b/src/x509.c index 5e7a1dd..0ad28fe 100644 --- a/src/x509.c +++ b/src/x509.c @@ -308,6 +308,52 @@ static int meth_pem(lua_State* L) return 1; } +/** + * Extract public key in PEM format. + */ +static int meth_pubkey(lua_State* L) +{ + char* data; + long bytes; + int ret = 1; + X509* cert = lsec_checkx509(L, 1); + BIO *bio = BIO_new(BIO_s_mem()); + EVP_PKEY *pkey = X509_get_pubkey(cert); + if(PEM_write_bio_PUBKEY(bio, pkey)) { + bytes = BIO_get_mem_data(bio, &data); + if (bytes > 0) { + lua_pushlstring(L, data, bytes); + switch(EVP_PKEY_type(pkey->type)) { + case EVP_PKEY_RSA: + lua_pushstring(L, "RSA"); + break; + case EVP_PKEY_DSA: + lua_pushstring(L, "DSA"); + break; + case EVP_PKEY_DH: + lua_pushstring(L, "DH"); + break; + case EVP_PKEY_EC: + lua_pushstring(L, "EC"); + break; + default: + lua_pushstring(L, "Unknown"); + break; + } + lua_pushinteger(L, EVP_PKEY_bits(pkey)); + ret = 3; + } + else + lua_pushnil(L); + } + else + lua_pushnil(L); + /* Cleanup */ + BIO_free(bio); + EVP_PKEY_free(pkey); + return ret; +} + /** * Compute the fingerprint. */ From 11eaec652033fafd72ef0828356aa6ef320878f8 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 19 Apr 2014 23:11:32 +0200 Subject: [PATCH 2/2] Add cert:pubkey() to methods registry --- src/x509.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/x509.c b/src/x509.c index 0ad28fe..8e85653 100644 --- a/src/x509.c +++ b/src/x509.c @@ -506,6 +506,7 @@ static luaL_Reg methods[] = { {"notbefore", meth_notbefore}, {"notafter", meth_notafter}, {"pem", meth_pem}, + {"pubkey", meth_pubkey}, {"serial", meth_serial}, {"subject", meth_subject}, {"validat", meth_valid_at},