suricata
util-lua-base64lib.c
Go to the documentation of this file.
1/* Copyright (C) 2025 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18#include "suricata-common.h"
19#include "util-lua-base64lib.h"
20#include "util-validate.h"
21#include "lauxlib.h"
22#include "rust.h"
23
24static int LuaBase64Encode(lua_State *L, SCBase64Mode mode)
25{
26 size_t input_len;
27 const char *input = luaL_checklstring(L, 1, &input_len);
28 size_t out_len = SCBase64EncodeBufferSize(input_len);
29 char *output = SCCalloc(out_len + 1, sizeof(char));
30 if (output == NULL) {
31 return luaL_error(L, "malloc");
32 }
33 if (SCBase64EncodeWithMode((uint8_t *)input, (unsigned long)input_len, (u_char *)output,
34 (unsigned long *)&out_len, mode) != 0) {
35 SCFree(output);
36 return luaL_error(L, "base64 encoding failed");
37 }
38 lua_pushstring(L, (const char *)output);
39 SCFree(output);
40
41 return 1;
42}
43
44static int LuaBase64EncodeStandard(lua_State *L)
45{
46 return LuaBase64Encode(L, SCBase64ModeStrict);
47}
48
49static int LuaBase64EncodeStandardNoPad(lua_State *L)
50{
51 return LuaBase64Encode(L, SCBase64ModeNoPad);
52}
53
54static int LuaBase64Decode(lua_State *L, SCBase64Mode mode)
55{
56 size_t input_len;
57 const char *input = luaL_checklstring(L, 1, &input_len);
58 char *output = SCCalloc(input_len, sizeof(char));
59 if (output == NULL) {
60 return luaL_error(L, "malloc");
61 }
62 uint32_t n = SCBase64Decode((uint8_t *)input, (uintptr_t)input_len, mode, (uint8_t *)output);
63 if (n == 0) {
64 SCFree(output);
65 return luaL_error(L, "base64 decoding failed");
66 }
67 DEBUG_VALIDATE_BUG_ON(n > input_len);
68 output[n] = '\0';
69 lua_pushstring(L, (const char *)output);
70 SCFree(output);
71
72 return 1;
73}
74
75static int LuaBase64DecodeStandard(lua_State *L)
76{
77 return LuaBase64Decode(L, SCBase64ModeStrict);
78}
79
80static int LuaBase64DecodeStandardNoPad(lua_State *L)
81{
82 return LuaBase64Decode(L, SCBase64ModeNoPad);
83}
84
85static int LuaBase64DecodeStandardPadOpt(lua_State *L)
86{
87 return LuaBase64Decode(L, SCBase64ModePadOpt);
88}
89
90static int LuaBase64DecodeRFC2045(lua_State *L)
91{
92 return LuaBase64Decode(L, SCBase64ModeRFC2045);
93}
94
95static int LuaBase64DecodeRFC4648(lua_State *L)
96{
97 return LuaBase64Decode(L, SCBase64ModeRFC4648);
98}
99
100static const struct luaL_Reg base64lib[] = {
101 // clang-format off
102 { "encode", LuaBase64EncodeStandard },
103 { "encode_nopad", LuaBase64EncodeStandardNoPad },
104 { "decode", LuaBase64DecodeStandard },
105 { "decode_nopad", LuaBase64DecodeStandardNoPad },
106 { "decode_padopt", LuaBase64DecodeStandardPadOpt },
107 { "decode_rfc2045", LuaBase64DecodeRFC2045 },
108 { "decode_rfc4648", LuaBase64DecodeRFC4648 },
109 { NULL, NULL },
110 // clang-format on
111};
112
114{
115 luaL_newlib(L, base64lib);
116
117 return 1;
118}
struct lua_State lua_State
int SCLuaLoadBase64Lib(lua_State *L)
#define SCFree(p)
Definition util-mem.h:61
#define SCCalloc(nm, sz)
Definition util-mem.h:53
#define DEBUG_VALIDATE_BUG_ON(exp)