diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-09-21 14:59:12 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-09-21 14:59:12 +0100 |
commit | 11e50a9f8219382b766c3ed5a217eb50bdafa89d (patch) | |
tree | 7954834fb7137468b919253419fb2003e74a3188 /src/lua | |
parent | 4ccd25c56d4875d8f719bd3487aec27c7f8ae31c (diff) | |
download | rspamd-11e50a9f8219382b766c3ed5a217eb50bdafa89d.tar.gz rspamd-11e50a9f8219382b766c3ed5a217eb50bdafa89d.zip |
[Rework] Allow more flexible keypair encodingvstakhov-keypair-encoding
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/lua_cryptobox.c | 31 | ||||
-rw-r--r-- | src/lua/lua_map.c | 4 |
2 files changed, 29 insertions, 6 deletions
diff --git a/src/lua/lua_cryptobox.c b/src/lua/lua_cryptobox.c index c9cac1562..3fa7d7d4f 100644 --- a/src/lua/lua_cryptobox.c +++ b/src/lua/lua_cryptobox.c @@ -503,7 +503,7 @@ lua_cryptobox_keypair_gc(lua_State *L) } /*** - * @method keypair:totable([hex=false]]) + * @method keypair:totable([encoding="zbase32"]) * Converts keypair to table (not very safe due to memory leftovers) */ static int @@ -512,16 +512,39 @@ lua_cryptobox_keypair_totable(lua_State *L) LUA_TRACE_POINT; struct rspamd_cryptobox_keypair *kp = lua_check_cryptobox_keypair(L, 1); ucl_object_t *obj; - gboolean hex = FALSE; + enum rspamd_cryptobox_keypair_encoding encoding = RSPAMD_KEYPAIR_ENCODING_DEFAULT; int ret = 1; if (kp != NULL) { if (lua_isboolean(L, 2)) { - hex = lua_toboolean(L, 2); + if (lua_toboolean(L, 2)) { + encoding = RSPAMD_KEYPAIR_ENCODING_HEX; + } + } + else if (lua_isstring(L, 2)) { + const char *enc = lua_tostring(L, 2); + + if (g_ascii_strcasecmp(enc, "hex") == 0) { + encoding = RSPAMD_KEYPAIR_ENCODING_HEX; + } + else if (g_ascii_strcasecmp(enc, "zbase32") == 0 || + g_ascii_strcasecmp(enc, "default") == 0 || + g_ascii_strcasecmp(enc, "base32") == 0) { + encoding = RSPAMD_KEYPAIR_ENCODING_ZBASE32; + } + else if (g_ascii_strcasecmp(enc, "base64") == 0) { + encoding = RSPAMD_KEYPAIR_ENCODING_BASE64; + } + else if (g_ascii_strcasecmp(enc, "binary") == 0) { + encoding = RSPAMD_KEYPAIR_ENCODING_BINARY; + } + else { + return luaL_error(L, "unknown encoding (known are: hex, zbase32/default, base64, binary: %s", enc); + } } - obj = rspamd_keypair_to_ucl(kp, hex ? RSPAMD_KEYPAIR_DUMP_HEX : RSPAMD_KEYPAIR_DUMP_DEFAULT); + obj = rspamd_keypair_to_ucl(kp, encoding, RSPAMD_KEYPAIR_DUMP_DEFAULT); ret = ucl_object_push_lua(L, obj, true); ucl_object_unref(obj); diff --git a/src/lua/lua_map.c b/src/lua/lua_map.c index 1cc2ce1bd..062613bd7 100644 --- a/src/lua/lua_map.c +++ b/src/lua/lua_map.c @@ -1256,8 +1256,8 @@ lua_map_get_sign_key(lua_State *L) bk = g_ptr_array_index(map->map->backends, i); if (bk->trusted_pubkey) { - ret = rspamd_pubkey_print(bk->trusted_pubkey, - RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); + ret = rspamd_pubkey_print(bk->trusted_pubkey, RSPAMD_KEYPAIR_ENCODING_DEFAULT, + RSPAMD_KEYPAIR_PUBKEY); } else { ret = NULL; |