56 static const char tls_state_mt[] =
"suricata:tls";
62 static int LuaTlsFlowStateGet(
lua_State *luastate)
72 struct LuaTls *s = (
struct LuaTls *)lua_newuserdata(luastate,
sizeof(*s));
77 void *
state = FlowGetAppState(
f);
81 luaL_getmetatable(luastate, tls_state_mt);
82 lua_setmetatable(luastate, -2);
86 static int GetCertNotBefore(
lua_State *luastate,
bool client,
const SSLState *ssl_state)
102 static int LuaTlsGetServerCertNotBefore(
lua_State *luastate)
104 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
105 if (s->
state == NULL) {
109 return GetCertNotBefore(luastate,
false, s->
state);
112 static int LuaTlsGetClientCertNotBefore(
lua_State *luastate)
114 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
115 if (s->
state == NULL) {
119 return GetCertNotBefore(luastate,
true, s->
state);
122 static int GetCertNotAfter(
lua_State *luastate,
bool client,
const SSLState *ssl_state)
138 static int LuaTlsGetServerCertNotAfter(
lua_State *luastate)
140 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
141 if (s->
state == NULL) {
145 return GetCertNotAfter(luastate,
false, s->
state);
147 static int LuaTlsGetClientCertNotAfter(
lua_State *luastate)
149 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
150 if (s->
state == NULL) {
154 return GetCertNotAfter(luastate,
true, s->
state);
157 static int GetCertInfo(
lua_State *luastate,
bool client,
const SSLState *ssl_state)
171 char ssl_version[SSL_VERSION_MAX_STRLEN];
181 static int LuaTlsGetServerCertInfo(
lua_State *luastate)
183 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
184 if (s->
state == NULL) {
188 return GetCertInfo(luastate,
false, s->
state);
191 static int LuaTlsGetClientCertInfo(
lua_State *luastate)
193 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
194 if (s->
state == NULL) {
198 return GetCertInfo(luastate,
true, s->
state);
210 static int LuaTlsGetSNI(
lua_State *luastate)
212 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
213 if (s->
state == NULL) {
220 return GetSNI(luastate, s->
state);
223 static int GetCertChain(
lua_State *luastate,
bool client)
225 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
226 if (s->
state == NULL) {
242 lua_newtable(luastate);
247 lua_pushinteger(luastate, u++);
249 lua_newtable(luastate);
251 lua_pushstring(luastate,
"length");
252 lua_pushinteger(luastate, cert->
cert_len);
253 lua_settable(luastate, -3);
255 lua_pushstring(luastate,
"data");
258 lua_settable(luastate, -3);
259 lua_settable(luastate, -3);
265 static int LuaTlsGetServerCertChain(
lua_State *luastate)
267 return GetCertChain(luastate,
false);
270 static int LuaTlsGetClientCertChain(
lua_State *luastate)
272 return GetCertChain(luastate,
true);
275 static int GetCertSerial(
lua_State *luastate,
bool client)
277 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
278 if (s->
state == NULL) {
295 static int LuaTlsGetServerCertSerial(
lua_State *luastate)
297 return GetCertSerial(luastate,
false);
300 static int LuaTlsGetClientCertSerial(
lua_State *luastate)
302 return GetCertSerial(luastate,
true);
305 static int GetAgreedVersion(
lua_State *luastate,
bool client)
307 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
308 if (s->
state == NULL) {
319 char ssl_version[SSL_VERSION_MAX_STRLEN];
320 SSLVersionToString(
version, ssl_version);
322 lua_pushstring(luastate, (
const char *)&ssl_version);
326 static int LuaTlsGetServerVersion(
lua_State *luastate)
328 return GetAgreedVersion(luastate,
false);
331 static int LuaTlsGetClientVersion(
lua_State *luastate)
333 return GetAgreedVersion(luastate,
true);
336 static const struct luaL_Reg tlslib_meta[] = {
338 {
"get_server_cert_not_before", LuaTlsGetServerCertNotBefore },
339 {
"get_client_cert_not_before", LuaTlsGetClientCertNotBefore },
340 {
"get_server_cert_not_after", LuaTlsGetServerCertNotAfter },
341 {
"get_client_cert_not_after", LuaTlsGetClientCertNotAfter },
342 {
"get_server_version", LuaTlsGetServerVersion },
343 {
"get_client_version", LuaTlsGetClientVersion },
344 {
"get_server_serial", LuaTlsGetServerCertSerial },
345 {
"get_client_serial", LuaTlsGetClientCertSerial },
346 {
"get_server_cert_info", LuaTlsGetServerCertInfo },
347 {
"get_client_cert_info", LuaTlsGetClientCertInfo },
348 {
"get_client_sni", LuaTlsGetSNI },
349 {
"get_client_cert_chain", LuaTlsGetClientCertChain },
350 {
"get_server_cert_chain", LuaTlsGetServerCertChain },
355 static const struct luaL_Reg tlslib[] = {
357 {
"get_tx", LuaTlsFlowStateGet },
364 luaL_newmetatable(L, tls_state_mt);
365 lua_pushvalue(L, -1);
366 lua_setfield(L, -2,
"__index");
367 luaL_setfuncs(L, tlslib_meta, 0);
369 luaL_newlib(L, tlslib);