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 
24 static 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_pushlstring(L, output, out_len);
39  SCFree(output);
40 
41  return 1;
42 }
43 
44 static int LuaBase64EncodeStandard(lua_State *L)
45 {
46  return LuaBase64Encode(L, SCBase64ModeStrict);
47 }
48 
49 static int LuaBase64EncodeStandardNoPad(lua_State *L)
50 {
51  return LuaBase64Encode(L, SCBase64ModeNoPad);
52 }
53 
54 static int LuaBase64Decode(lua_State *L, SCBase64Mode mode)
55 {
56  size_t input_len;
57  const char *input = luaL_checklstring(L, 1, &input_len);
58  if (input_len == 0) {
59  /* empty input decodes to an empty string, not an error */
60  lua_pushlstring(L, "", 0);
61  return 1;
62  }
63  char *output = SCCalloc(input_len, sizeof(char));
64  if (output == NULL) {
65  return luaL_error(L, "malloc");
66  }
67  uint32_t n = SCBase64Decode((uint8_t *)input, (uintptr_t)input_len, mode, (uint8_t *)output);
68  if (n == 0) {
69  SCFree(output);
70  return luaL_error(L, "base64 decoding failed");
71  }
72  DEBUG_VALIDATE_BUG_ON(n > input_len);
73  /* the decoded data is binary: push with length, pushstring would
74  * truncate at the first embedded NUL */
75  lua_pushlstring(L, output, n);
76  SCFree(output);
77 
78  return 1;
79 }
80 
81 static int LuaBase64DecodeStandard(lua_State *L)
82 {
83  return LuaBase64Decode(L, SCBase64ModeStrict);
84 }
85 
86 static int LuaBase64DecodeStandardNoPad(lua_State *L)
87 {
88  return LuaBase64Decode(L, SCBase64ModeNoPad);
89 }
90 
91 static int LuaBase64DecodeStandardPadOpt(lua_State *L)
92 {
93  return LuaBase64Decode(L, SCBase64ModePadOpt);
94 }
95 
96 static int LuaBase64DecodeRFC2045(lua_State *L)
97 {
98  return LuaBase64Decode(L, SCBase64ModeRFC2045);
99 }
100 
101 static int LuaBase64DecodeRFC4648(lua_State *L)
102 {
103  return LuaBase64Decode(L, SCBase64ModeRFC4648);
104 }
105 
106 static const struct luaL_Reg base64lib[] = {
107  // clang-format off
108  { "encode", LuaBase64EncodeStandard },
109  { "encode_nopad", LuaBase64EncodeStandardNoPad },
110  { "decode", LuaBase64DecodeStandard },
111  { "decode_nopad", LuaBase64DecodeStandardNoPad },
112  { "decode_padopt", LuaBase64DecodeStandardPadOpt },
113  { "decode_rfc2045", LuaBase64DecodeRFC2045 },
114  { "decode_rfc4648", LuaBase64DecodeRFC4648 },
115  { NULL, NULL },
116  // clang-format on
117 };
118 
120 {
121  luaL_newlib(L, base64lib);
122 
123  return 1;
124 }
rust.h
SCLuaLoadBase64Lib
int SCLuaLoadBase64Lib(lua_State *L)
Definition: util-lua-base64lib.c:119
lua_State
struct lua_State lua_State
Definition: suricata-common.h:527
suricata-common.h
util-validate.h
SCFree
#define SCFree(p)
Definition: util-mem.h:61
util-lua-base64lib.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109