From: Vsevolod Stakhov Date: Tue, 5 Jun 2018 12:28:13 +0000 (+0100) Subject: [Minor] Add ability to get a pubkey from keypair X-Git-Tag: 1.7.6~57 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fcc57ae988f053a283bda4e2cac1c0811b6c33ba;p=thirdparty%2Frspamd.git [Minor] Add ability to get a pubkey from keypair --- diff --git a/src/lua/lua_cryptobox.c b/src/lua/lua_cryptobox.c index 99be61cdcd..0323fe0984 100644 --- a/src/lua/lua_cryptobox.c +++ b/src/lua/lua_cryptobox.c @@ -48,6 +48,7 @@ LUA_FUNCTION_DEF (cryptobox_keypair, gc); LUA_FUNCTION_DEF (cryptobox_keypair, totable); LUA_FUNCTION_DEF (cryptobox_keypair, get_type); LUA_FUNCTION_DEF (cryptobox_keypair, get_alg); +LUA_FUNCTION_DEF (cryptobox_keypair, get_pk); LUA_FUNCTION_DEF (cryptobox_signature, create); LUA_FUNCTION_DEF (cryptobox_signature, load); LUA_FUNCTION_DEF (cryptobox_signature, save); @@ -112,6 +113,8 @@ static const struct luaL_reg cryptoboxkeypairlib_m[] = { {"get_alg", lua_cryptobox_keypair_get_alg}, {"type", lua_cryptobox_keypair_get_type}, {"alg", lua_cryptobox_keypair_get_alg}, + {"pk", lua_cryptobox_keypair_get_pk}, + {"pubkey", lua_cryptobox_keypair_get_pk}, {"__gc", lua_cryptobox_keypair_gc}, {NULL, NULL} }; @@ -549,6 +552,38 @@ lua_cryptobox_keypair_get_alg (lua_State *L) return 1; } +/*** + * @method keypair:pk() + * Returns pubkey for a specific keypair + * @return {rspamd_pubkey} pubkey for a keypair + */ +static gint +lua_cryptobox_keypair_get_pk (lua_State *L) +{ + struct rspamd_cryptobox_keypair *kp = lua_check_cryptobox_keypair (L, 1); + struct rspamd_cryptobox_pubkey *pk, **ppk; + const guchar *data; + guint dlen; + + if (kp) { + data = rspamd_keypair_component (kp, RSPAMD_KEYPAIR_COMPONENT_PK, &dlen); + pk = rspamd_pubkey_from_bin (data, dlen, kp->type, kp->alg); + + if (pk == NULL) { + return luaL_error (L, "invalid keypair"); + } + + ppk = lua_newuserdata (L, sizeof (*ppk)); + *ppk = pk; + rspamd_lua_setclass (L, "rspamd{cryptobox_pubkey}", -1); + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +} + /*** * @function rspamd_cryptobox_signature.load(file) * Loads signature from raw file