diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-03-07 15:02:56 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-03-07 15:02:56 +0000 |
commit | eeeabacd11a7801168eaaeed2c2ba26137307d20 (patch) | |
tree | acf2d47c103c72a4257dc4a17836bbb92b9ed670 /src/lua/lua_map.c | |
parent | fae817031dbca39bd94e0448f3d12ecdc1914d86 (diff) | |
download | rspamd-eeeabacd11a7801168eaaeed2c2ba26137307d20.tar.gz rspamd-eeeabacd11a7801168eaaeed2c2ba26137307d20.zip |
[Feature] Add map:set_sign_key and map:get_sign_key
Diffstat (limited to 'src/lua/lua_map.c')
-rw-r--r-- | src/lua/lua_map.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/lua/lua_map.c b/src/lua/lua_map.c index 1f7ceb194..c2aac1f42 100644 --- a/src/lua/lua_map.c +++ b/src/lua/lua_map.c @@ -58,10 +58,26 @@ LUA_FUNCTION_DEF (map, is_signed); */ LUA_FUNCTION_DEF (map, get_proto); +/*** + * @method map:get_sign_key() + * Returns pubkey used for signing as base32 string or nil + * @return {string} base32 encoded string or nil + */ +LUA_FUNCTION_DEF (map, get_sign_key); + +/*** + * @method map:set_sign_key(key) + * Set trusted key for signatures for this map + * @param {string} key base32 encoded string or nil + */ +LUA_FUNCTION_DEF (map, set_sign_key); + static const struct luaL_reg maplib_m[] = { LUA_INTERFACE_DEF (map, get_key), LUA_INTERFACE_DEF (map, is_signed), LUA_INTERFACE_DEF (map, get_proto), + LUA_INTERFACE_DEF (map, get_sign_key), + LUA_INTERFACE_DEF (map, set_sign_key), {"__tostring", rspamd_lua_class_tostring}, {NULL, NULL} }; @@ -510,6 +526,69 @@ lua_map_get_proto (lua_State *L) return 1; } +static int +lua_map_get_sign_key (lua_State *L) +{ + struct rspamd_lua_map *map = lua_check_map (L); + GString *ret = NULL; + + if (map != NULL) { + if (map->map && map->map->trusted_pubkey) { + ret = rspamd_pubkey_print (map->map->trusted_pubkey, + RSPAMD_KEYPAIR_PUBKEY|RSPAMD_KEYPAIR_BASE32); + } + } + else { + return luaL_error (L, "invalid arguments"); + } + + if (ret) { + lua_pushlstring (L, ret->str, ret->len); + g_string_free (ret, TRUE); + } + else { + lua_pushnil (L); + } + + return 1; +} + +static int +lua_map_set_sign_key (lua_State *L) +{ + struct rspamd_lua_map *map = lua_check_map (L); + const gchar *pk_str; + struct rspamd_cryptobox_pubkey *pk; + gsize len; + + pk_str = lua_tolstring (L, 2, &len); + + if (map && pk_str) { + + if (!map->map) { + return luaL_error (L, "cannot set key for embedded maps"); + } + + pk = rspamd_pubkey_from_base32 (pk_str, len, RSPAMD_KEYPAIR_SIGN, + RSPAMD_CRYPTOBOX_MODE_25519); + + if (!pk) { + return luaL_error (L, "invalid pubkey string"); + } + + if (map->map->trusted_pubkey) { + /* Unref old pk */ + rspamd_pubkey_unref (map->map->trusted_pubkey); + } + + map->map->trusted_pubkey = pk; + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 0; +} void luaopen_map (lua_State * L) |