diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-04-10 11:38:39 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-04-10 11:44:46 +0100 |
commit | f8072344f9fb3fbc1ac93122f6cad11204c8cbf2 (patch) | |
tree | 29a3c5b33cdaf77138d7f753a97b04d6102c84c0 | |
parent | baecba4ce93e91b553e2d5d0d8e779dcd05e1d43 (diff) | |
download | rspamd-f8072344f9fb3fbc1ac93122f6cad11204c8cbf2.tar.gz rspamd-f8072344f9fb3fbc1ac93122f6cad11204c8cbf2.zip |
[Feature] Allow multiple base32 encodings in Lua API
-rw-r--r-- | src/lua/lua_cryptobox.c | 31 | ||||
-rw-r--r-- | src/lua/lua_util.c | 30 |
2 files changed, 51 insertions, 10 deletions
diff --git a/src/lua/lua_cryptobox.c b/src/lua/lua_cryptobox.c index 71c4655f4..07b4888b9 100644 --- a/src/lua/lua_cryptobox.c +++ b/src/lua/lua_cryptobox.c @@ -827,8 +827,9 @@ lua_cryptobox_signature_hex (lua_State *L) } /*** - * @method cryptobox_signature:base32() + * @method cryptobox_signature:base32([b32type='default']) * Return base32 encoded signature string + * @param {string} b32type base32 type (default, bleach, rfc) * @return {string} raw value of signature */ static gint @@ -837,9 +838,18 @@ lua_cryptobox_signature_base32 (lua_State *L) LUA_TRACE_POINT; rspamd_fstring_t *sig = lua_check_cryptobox_sign (L, 1); gchar *encoded; + enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT; + + if (lua_type (L, 2) == LUA_TSTRING) { + btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2)); + + if (btype == RSPAMD_BASE32_INVALID) { + return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2)); + } + } if (sig) { - encoded = rspamd_encode_base32 (sig->str, sig->len, RSPAMD_BASE32_DEFAULT); + encoded = rspamd_encode_base32 (sig->str, sig->len, btype); lua_pushstring (L, encoded); g_free (encoded); } @@ -1365,8 +1375,9 @@ lua_cryptobox_hash_hex (lua_State *L) } /*** - * @method cryptobox_hash:base32() - * Finalizes hash and return it as zbase32 string + * @method cryptobox_hash:base32([b32type]) + * Finalizes hash and return it as zbase32 (by default) string + * @param {string} b32type base32 type (default, bleach, rfc) * @return {string} base32 value of hash */ static gint @@ -1379,6 +1390,16 @@ lua_cryptobox_hash_base32 (lua_State *L) guint dlen; if (h && !h->is_finished) { + enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT; + + if (lua_type (L, 2) == LUA_TSTRING) { + btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2)); + + if (btype == RSPAMD_BASE32_INVALID) { + return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2)); + } + } + memset (out_b32, 0, sizeof (out_b32)); lua_cryptobox_hash_finish (h, out, &dlen); r = out; @@ -1392,7 +1413,7 @@ lua_cryptobox_hash_base32 (lua_State *L) } } - rspamd_encode_base32_buf (r, dlen, out_b32, sizeof (out_b32), RSPAMD_BASE32_DEFAULT); + rspamd_encode_base32_buf (r, dlen, out_b32, sizeof (out_b32), btype); lua_pushstring (L, out_b32); h->is_finished = TRUE; } diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 053cce864..a378c1ace 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -94,16 +94,18 @@ LUA_FUNCTION_DEF (util, decode_qp); LUA_FUNCTION_DEF (util, decode_base64); /*** - * @function util.encode_base32(input) + * @function util.encode_base32(input, [b32type = 'default']) * Encodes data in base32 breaking lines if needed * @param {text or string} input input data + * @param {string} b32type base32 type (default, bleach, rfc) * @return {rspamd_text} encoded data chunk */ LUA_FUNCTION_DEF (util, encode_base32); /*** - * @function util.decode_base32(input) + * @function util.decode_base32(input, [b32type = 'default']) * Decodes data from base32 ignoring whitespace characters * @param {text or string} input data to decode + * @param {string} b32type base32 type (default, bleach, rfc) * @return {rspamd_text} decoded data chunk */ LUA_FUNCTION_DEF (util, decode_base32); @@ -1157,6 +1159,7 @@ lua_util_encode_base32 (lua_State *L) struct rspamd_lua_text *t; const gchar *s = NULL; gchar *out; + enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT; gsize inlen, outlen; if (lua_type (L, 1) == LUA_TSTRING) { @@ -1171,11 +1174,19 @@ lua_util_encode_base32 (lua_State *L) } } + if (lua_type (L, 2) == LUA_TSTRING) { + btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2)); + + if (btype == RSPAMD_BASE32_INVALID) { + return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2)); + } + } + if (s == NULL) { - lua_pushnil (L); + return luaL_error (L, "invalid arguments"); } else { - out = rspamd_encode_base32 (s, inlen, RSPAMD_BASE32_DEFAULT); + out = rspamd_encode_base32 (s, inlen, btype); if (out != NULL) { t = lua_newuserdata (L, sizeof (*t)); @@ -1201,6 +1212,7 @@ lua_util_decode_base32 (lua_State *L) struct rspamd_lua_text *t; const gchar *s = NULL; gsize inlen, outlen; + enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT; if (lua_type (L, 1) == LUA_TSTRING) { s = luaL_checklstring (L, 1, &inlen); @@ -1214,10 +1226,18 @@ lua_util_decode_base32 (lua_State *L) } } + if (lua_type (L, 2) == LUA_TSTRING) { + btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2)); + + if (btype == RSPAMD_BASE32_INVALID) { + return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2)); + } + } + if (s != NULL) { t = lua_newuserdata (L, sizeof (*t)); rspamd_lua_setclass (L, "rspamd{text}", -1); - t->start = rspamd_decode_base32 (s, inlen, &outlen, RSPAMD_BASE32_DEFAULT); + t->start = rspamd_decode_base32 (s, inlen, &outlen, btype); t->len = outlen; t->flags = RSPAMD_TEXT_FLAG_OWN; } |