Skip to content

Commit 21deb04

Browse files
Leonardo Alminanaedsiper
authored andcommitted
tls: openssl: added support for SSLKEYLOGFILE on DEV builds
Signed-off-by: Leonardo Alminana <[email protected]>
1 parent 32ba920 commit 21deb04

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

src/tls/openssl.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
* limitations under the License.
1818
*/
1919

20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
2023
#include <fluent-bit/flb_info.h>
24+
#include <fluent-bit/flb_str.h>
2125
#include <fluent-bit/flb_compat.h>
2226
#include <fluent-bit/tls/flb_tls.h>
2327
#include <fluent-bit/tls/flb_tls_info.h>
@@ -76,6 +80,7 @@ static int tls_init(void)
7680
SSL_load_error_strings();
7781
SSL_library_init();
7882
#endif
83+
7984
return 0;
8085
}
8186

@@ -134,10 +139,15 @@ static void tls_context_destroy(void *ctx_backend)
134139
struct tls_context *ctx = ctx_backend;
135140

136141
pthread_mutex_lock(&ctx->mutex);
142+
137143
SSL_CTX_free(ctx->ctx);
144+
138145
if (ctx->alpn != NULL) {
139146
flb_free(ctx->alpn);
147+
148+
ctx->alpn = NULL;
140149
}
150+
141151
pthread_mutex_unlock(&ctx->mutex);
142152

143153
flb_free(ctx);
@@ -438,7 +448,7 @@ static int macos_load_system_certificates(struct tls_context *ctx)
438448
}
439449

440450
CFRelease(certs);
441-
flb_debug("[tls] finished loading keychain certificates, total loaded: %d", loaded_cert_count);
451+
flb_debug("[tls] finished loading keychain certificates, total loaded: %lu", loaded_cert_count);
442452
return 0;
443453
}
444454
#endif
@@ -448,6 +458,9 @@ static int load_system_certificates(struct tls_context *ctx)
448458
int ret;
449459
const char *ca_file = FLB_DEFAULT_SEARCH_CA_BUNDLE;
450460

461+
(void) ret;
462+
(void) ca_file;
463+
451464
/* For Windows use specific API to read the certs store */
452465
#ifdef _MSC_VER
453466
return windows_load_system_certificates(ctx);
@@ -467,6 +480,33 @@ static int load_system_certificates(struct tls_context *ctx)
467480
#endif
468481
}
469482

483+
#ifdef FLB_HAVE_DEV
484+
/* This is not thread safe */
485+
static void ssl_key_logger(const SSL *ssl, const char *line)
486+
{
487+
char *key_log_filename;
488+
FILE *key_log_file;
489+
490+
key_log_filename = getenv("SSLKEYLOGFILE");
491+
492+
if (key_log_filename == NULL) {
493+
return;
494+
}
495+
496+
key_log_file = fopen(key_log_filename, "a");
497+
498+
if (key_log_file == NULL) {
499+
return;
500+
}
501+
502+
setvbuf(key_log_file, NULL, 0, _IOLBF);
503+
504+
fprintf(key_log_file, "%s\n", line);
505+
506+
fclose(key_log_file);
507+
}
508+
#endif
509+
470510
static void *tls_context_create(int verify,
471511
int debug,
472512
int mode,
@@ -481,6 +521,7 @@ static void *tls_context_create(int verify,
481521
SSL_CTX *ssl_ctx;
482522
struct tls_context *ctx;
483523
char err_buf[256];
524+
char *key_log_filename;
484525

485526
/*
486527
* Init library ? based in the documentation on OpenSSL >= 1.1.0 is not longer
@@ -523,6 +564,16 @@ static void *tls_context_create(int verify,
523564
flb_errno();
524565
return NULL;
525566
}
567+
568+
#ifdef FLB_HAVE_DEV
569+
key_log_filename = getenv("SSLKEYLOGFILE");
570+
571+
if (key_log_filename != NULL) {
572+
SSL_CTX_set_keylog_callback(ssl_ctx, ssl_key_logger);
573+
}
574+
#endif
575+
576+
526577
ctx->ctx = ssl_ctx;
527578
ctx->mode = mode;
528579
ctx->alpn = NULL;

0 commit comments

Comments
 (0)