44 #define MODULE_NAME "LogTlsStoreLog"
46 static char tls_logfile_base_dir[PATH_MAX] =
"/tmp";
48 static char logging_dir_not_writable;
50 #define LOGGING_WRITE_ISSUE_LIMIT 6
57 static int CreateFileName(
58 const Packet *p,
SSLState *state,
char *filename,
size_t filename_size,
const bool client)
63 const char *dir = client ?
"client-" :
"";
68 if (snprintf(path,
sizeof(path),
"%s/%s%ld.%ld-%d.pem", tls_logfile_base_dir, dir,
70 file_id) ==
sizeof(path))
73 strlcpy(filename, path, filename_size);
80 #define PEMHEADER "-----BEGIN CERTIFICATE-----\n"
81 #define PEMFOOTER "-----END CERTIFICATE-----\n"
83 char filename[PATH_MAX] =
"";
87 unsigned char* pembase64ptr = NULL;
97 CreateFileName(p, state, filename,
sizeof(filename), client);
98 if (strlen(filename) == 0) {
103 fp = fopen(filename,
"w");
107 "Can't create PEM file '%s' in '%s' directory", filename, tls_logfile_base_dir);
108 logging_dir_not_writable++;
114 pemlen = Base64EncodeBufferSize(cert->
cert_len);
121 SCLogWarning(
"Can't allocate data for base64 encoding");
131 if (ret != SC_BASE64_OK) {
132 SCLogWarning(
"Invalid return of Base64Encode function");
141 size_t loffset = pemlen >= 64 ? 64 : pemlen;
142 if (fwrite(pembase64ptr, 1, loffset, fp) != loffset)
144 if (fwrite(
"\n", 1, 1, fp) != 1)
158 memcpy(filename + (strlen(filename) - 3),
"meta", 4);
159 fpmeta = fopen(filename,
"w");
160 if (fpmeta != NULL) {
161 #define PRINT_BUF_LEN 46
167 goto end_fwrite_fpmeta;
168 if (fprintf(fpmeta,
"TIME: %s\n", timebuf) < 0)
169 goto end_fwrite_fpmeta;
171 if (fprintf(fpmeta,
"PCAP PKT NUM: %"PRIu64
"\n", p->
pcap_cnt) < 0)
172 goto end_fwrite_fpmeta;
174 if (fprintf(fpmeta,
"SRC IP: %s\n", srcip) < 0)
175 goto end_fwrite_fpmeta;
176 if (fprintf(fpmeta,
"DST IP: %s\n", dstip) < 0)
177 goto end_fwrite_fpmeta;
178 if (fprintf(fpmeta,
"PROTO: %" PRIu32
"\n", p->
proto) < 0)
179 goto end_fwrite_fpmeta;
180 if (PacketIsTCP(p) || PacketIsUDP(p)) {
181 if (fprintf(fpmeta,
"SRC PORT: %" PRIu16
"\n", sp) < 0)
182 goto end_fwrite_fpmeta;
183 if (fprintf(fpmeta,
"DST PORT: %" PRIu16
"\n", dp) < 0)
184 goto end_fwrite_fpmeta;
190 "TLS FINGERPRINT: %s\n",
192 goto end_fwrite_fpmeta;
197 SCLogWarning(
"Can't create meta file '%s' in '%s' directory", filename,
198 tls_logfile_base_dir);
199 logging_dir_not_writable++;
212 logging_dir_not_writable++;
219 logging_dir_not_writable++;
232 static bool LogTlsStoreCondition(
235 if (p->
flow == NULL) {
239 if (!(PacketIsTCP(p))) {
244 if (ssl_state == NULL) {
245 SCLogDebug(
"no tls state, so no request logging");
263 static bool LogTlsStoreConditionClient(
266 if (p->
flow == NULL) {
270 if (!(PacketIsTCP(p))) {
275 if (ssl_state == NULL) {
276 SCLogDebug(
"no tls state, so no request logging");
295 void *state,
void *tx, uint64_t tx_id)
298 int ipproto = (PacketIsIPv4(p)) ? AF_INET : AF_INET6;
307 LogTlsLogPem(aft, p, ssl_state, connp, ipproto);
314 void *state,
void *tx, uint64_t tx_id)
317 int ipproto = (PacketIsIPv4(p)) ? AF_INET : AF_INET6;
326 LogTlsLogPem(aft, p, ssl_state, connp, ipproto);
332 static TmEcode LogTlsStoreLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
338 if (initdata == NULL) {
339 SCLogDebug(
"Error getting context for LogTLSStore. \"initdata\" argument NULL");
344 struct stat stat_buf;
346 if (stat(tls_logfile_base_dir, &stat_buf) != 0) {
349 ret =
SCMkDir(tls_logfile_base_dir, S_IRWXU|S_IXGRP|S_IRGRP);
353 SCLogError(
"Cannot create certs drop directory %s: %s", tls_logfile_base_dir,
358 SCLogInfo(
"Created certs drop directory %s",
359 tls_logfile_base_dir);
392 static void LogTlsStoreLogDeInitCtx(
OutputCtx *output_ctx)
408 output_ctx->
data = NULL;
409 output_ctx->
DeInit = LogTlsStoreLogDeInitCtx;
413 if (s_base_dir == NULL || strlen(s_base_dir) == 0) {
415 s_default_log_dir,
sizeof(tls_logfile_base_dir));
419 s_base_dir,
sizeof(tls_logfile_base_dir));
421 snprintf(tls_logfile_base_dir,
sizeof(tls_logfile_base_dir),
422 "%s/%s", s_default_log_dir, s_base_dir);
426 SCLogInfo(
"storing certs in %s", tls_logfile_base_dir);
431 result.
ctx = output_ctx;
439 LogTlsStoreLogInitCtx,
ALPROTO_TLS, LogTlsStoreLogger, LogTlsStoreCondition,
440 LogTlsStoreLogThreadInit, LogTlsStoreLogThreadDeinit);
443 LogTlsStoreLogInitCtx,
ALPROTO_TLS, LogTlsStoreLoggerClient, LogTlsStoreConditionClient,
444 LogTlsStoreLogThreadInit, LogTlsStoreLogThreadDeinit);