-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsslkeylogfile.c
More file actions
82 lines (63 loc) · 1.96 KB
/
Copy pathsslkeylogfile.c
File metadata and controls
82 lines (63 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* For RTLD_NEXT */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <errno.h>
#include <openssl/ssl.h>
/* Header of key log file. */
#define HEADER "# SSL key log file generated by sslkeylogfile.c\n"
/* Environment variable indicates filename of SSLKEYLOGFILE. */
#define FILENAME_ENVVAR "LIBSSL_SSLKEYLOGFILE"
static int fd_sslkeylogfile = -1;
static void open_sslkeylogfile(void);
static void write_sslkeylogfile(const SSL *ssl, const char *line);
SSL *SSL_new(SSL_CTX *ctx)
{
SSL *(*SSL_new_orig)() = dlsym(RTLD_NEXT, __func__);
if (SSL_new_orig == NULL) {
fprintf(stderr, "Could not get symbol \"%s\": %s\n", __func__, dlerror());
abort();
}
SSL_CTX_set_keylog_callback(ctx, write_sslkeylogfile);
return SSL_new_orig(ctx);
}
static void open_sslkeylogfile(void)
{
// Already opened, no need to do anything.
if (fd_sslkeylogfile != -1)
return;
const char *filename = getenv(FILENAME_ENVVAR);
// FILENAME_ENVVAR is not defined ...
if (filename == NULL) {
fprintf(stderr, "Environment variable \""FILENAME_ENVVAR"\" is not defined.");
abort();
}
// Open log file in write-only and append mode,
// create mode 0644 if not exist.
fd_sslkeylogfile = open(
filename,
O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
);
// If we could not do so ...
if (fd_sslkeylogfile == -1) {
fprintf(stderr, "Failed open SSLKEYLOGFILE (%s): %s\n", filename, strerror(errno));
abort();
}
// Move pointer to the tail of the file.
// Dump the header to file if the file is newly created (empty, or 0 is the last byte)
if (lseek(fd_sslkeylogfile, 0, SEEK_END) == 0)
write(fd_sslkeylogfile, HEADER, sizeof(HEADER) - 1);
}
static void write_sslkeylogfile(const SSL *ssl, const char *line)
{
open_sslkeylogfile();
if (fd_sslkeylogfile != -1) {
write(fd_sslkeylogfile, line, strlen(line));
write(fd_sslkeylogfile, "\n", 1); // New line
}
}