58 static const char tls_state_mt[] =
"suricata:tls";
64 static int LuaTlsFlowStateGet(
lua_State *luastate)
74 struct LuaTls *s = (
struct LuaTls *)lua_newuserdata(luastate,
sizeof(*s));
79 void *
state = FlowGetAppState(f);
83 luaL_getmetatable(luastate, tls_state_mt);
84 lua_setmetatable(luastate, -2);
88 static int GetCertNotBefore(
lua_State *luastate,
bool client,
const SSLState *ssl_state)
104 static int LuaTlsGetServerCertNotBefore(
lua_State *luastate)
106 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
107 if (s->
state == NULL) {
111 return GetCertNotBefore(luastate,
false, s->
state);
114 static int LuaTlsGetClientCertNotBefore(
lua_State *luastate)
116 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
117 if (s->
state == NULL) {
121 return GetCertNotBefore(luastate,
true, s->
state);
124 static int GetCertNotAfter(
lua_State *luastate,
bool client,
const SSLState *ssl_state)
140 static int LuaTlsGetServerCertNotAfter(
lua_State *luastate)
142 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
143 if (s->
state == NULL) {
147 return GetCertNotAfter(luastate,
false, s->
state);
149 static int LuaTlsGetClientCertNotAfter(
lua_State *luastate)
151 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
152 if (s->
state == NULL) {
156 return GetCertNotAfter(luastate,
true, s->
state);
159 static int GetCertInfo(
lua_State *luastate,
bool client,
const SSLState *ssl_state)
173 char ssl_version[SSL_VERSION_MAX_STRLEN];
183 static int LuaTlsGetServerCertInfo(
lua_State *luastate)
185 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
186 if (s->
state == NULL) {
190 return GetCertInfo(luastate,
false, s->
state);
193 static int LuaTlsGetClientCertInfo(
lua_State *luastate)
195 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
196 if (s->
state == NULL) {
200 return GetCertInfo(luastate,
true, s->
state);
212 static int LuaTlsGetSNI(
lua_State *luastate)
214 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
215 if (s->
state == NULL) {
222 return GetSNI(luastate, s->
state);
225 static int GetCertChain(
lua_State *luastate,
bool client)
227 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
228 if (s->
state == NULL) {
244 lua_newtable(luastate);
249 lua_pushinteger(luastate, u++);
251 lua_newtable(luastate);
253 lua_pushstring(luastate,
"length");
254 lua_pushinteger(luastate, cert->
cert_len);
255 lua_settable(luastate, -3);
257 lua_pushstring(luastate,
"data");
260 lua_settable(luastate, -3);
261 lua_settable(luastate, -3);
267 static int LuaTlsGetServerCertChain(
lua_State *luastate)
269 return GetCertChain(luastate,
false);
272 static int LuaTlsGetClientCertChain(
lua_State *luastate)
274 return GetCertChain(luastate,
true);
277 static int GetCertSerial(
lua_State *luastate,
bool client)
279 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
280 if (s->
state == NULL) {
298 static int LuaTlsGetServerCertSerial(
lua_State *luastate)
300 return GetCertSerial(luastate,
false);
303 static int LuaTlsGetClientCertSerial(
lua_State *luastate)
305 return GetCertSerial(luastate,
true);
308 static int GetAgreedVersion(
lua_State *luastate,
bool client)
310 struct LuaTls *s = (
struct LuaTls *)luaL_checkudata(luastate, 1, tls_state_mt);
311 if (s->
state == NULL) {
322 char ssl_version[SSL_VERSION_MAX_STRLEN];
323 SSLVersionToString(
version, ssl_version);
325 lua_pushstring(luastate, (
const char *)&ssl_version);
329 static int LuaTlsGetServerVersion(
lua_State *luastate)
331 return GetAgreedVersion(luastate,
false);
334 static int LuaTlsGetClientVersion(
lua_State *luastate)
336 return GetAgreedVersion(luastate,
true);
339 static const struct luaL_Reg tlslib_meta[] = {
341 {
"get_server_cert_not_before", LuaTlsGetServerCertNotBefore },
342 {
"get_client_cert_not_before", LuaTlsGetClientCertNotBefore },
343 {
"get_server_cert_not_after", LuaTlsGetServerCertNotAfter },
344 {
"get_client_cert_not_after", LuaTlsGetClientCertNotAfter },
345 {
"get_server_version", LuaTlsGetServerVersion },
346 {
"get_client_version", LuaTlsGetClientVersion },
347 {
"get_server_serial", LuaTlsGetServerCertSerial },
348 {
"get_client_serial", LuaTlsGetClientCertSerial },
349 {
"get_server_cert_info", LuaTlsGetServerCertInfo },
350 {
"get_client_cert_info", LuaTlsGetClientCertInfo },
351 {
"get_client_sni", LuaTlsGetSNI },
352 {
"get_client_cert_chain", LuaTlsGetClientCertChain },
353 {
"get_server_cert_chain", LuaTlsGetServerCertChain },
358 static const struct luaL_Reg tlslib[] = {
360 {
"get_tx", LuaTlsFlowStateGet },
367 luaL_newmetatable(L, tls_state_mt);
368 lua_pushvalue(L, -1);
369 lua_setfield(L, -2,
"__index");
370 luaL_setfuncs(L, tlslib_meta, 0);
372 luaL_newlib(L, tlslib);