Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for SSL key logging #1555

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/MQTTAsync.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,12 @@ int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions* options)
m->c->sslopts->protos = (const unsigned char*)MQTTStrdup((const char*)options->ssl->protos);
m->c->sslopts->protos_len = options->ssl->protos_len;
}

if (options->ssl->ssl_keylog_cb)
{
m->c->sslopts->ssl_keylog_cb = options->ssl->ssl_keylog_cb;
m->c->sslopts->ssl_keylog_context = options->ssl->ssl_keylog_context;
}
}
#else
if (options->struct_version != 0 && options->ssl)
Expand Down
19 changes: 17 additions & 2 deletions src/MQTTAsync.h
Original file line number Diff line number Diff line change
Expand Up @@ -1072,12 +1072,13 @@ typedef struct
/** The eyecatcher for this structure. Must be MQTS */
char struct_id[4];

/** The version number of this structure. Must be 0, 1, 2, 3, 4 or 5.
/** The version number of this structure. Must be 0, 1, 2, 3, 4, 5 or 6.
* 0 means no sslVersion
* 1 means no verify, CApath
* 2 means no ssl_error_context, ssl_error_cb
* 3 means no ssl_psk_cb, ssl_psk_context, disableDefaultTrustStore
* 4 means no protos, protos_len
* 5 means no ssl_keylog_context or ssl_keylog_cb
*/
int struct_version;

Expand Down Expand Up @@ -1176,9 +1177,23 @@ typedef struct
* Exists only if struct_version >= 5
*/
unsigned int protos_len;

/**
* Callback function for handling the SSL keylogs.
* line: the SSL keylog line.
* ctx: pointer to context data set to ssl_keylog_context by the application.
* Exists only if struct_version >= 6
*/
void (*ssl_keylog_cb) (const char *line, void *ctx);

/**
* Application-specific contex for ssl_keylog_cb
* Exists only if struct_version >= 6
*/
void* ssl_keylog_context;
} MQTTAsync_SSLOptions;

#define MQTTAsync_SSLOptions_initializer { {'M', 'Q', 'T', 'S'}, 5, NULL, NULL, NULL, NULL, NULL, 1, MQTT_SSL_VERSION_DEFAULT, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0 }
#define MQTTAsync_SSLOptions_initializer { {'M', 'Q', 'T', 'S'}, 5, NULL, NULL, NULL, NULL, NULL, 1, MQTT_SSL_VERSION_DEFAULT, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL }

/** Utility structure where name/value pairs are needed */
typedef struct
Expand Down
6 changes: 6 additions & 0 deletions src/MQTTClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,12 @@ static MQTTResponse MQTTClient_connectURI(MQTTClient handle, MQTTClient_connectO
m->c->sslopts->protos = options->ssl->protos;
m->c->sslopts->protos_len = options->ssl->protos_len;
}

if (options->ssl->ssl_keylog_cb)
{
m->c->sslopts->ssl_keylog_cb = options->ssl->ssl_keylog_cb;
m->c->sslopts->ssl_keylog_context = options->ssl->ssl_keylog_context;
}
}
#endif

Expand Down
19 changes: 17 additions & 2 deletions src/MQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -677,12 +677,13 @@ typedef struct
/** The eyecatcher for this structure. Must be MQTS */
char struct_id[4];

/** The version number of this structure. Must be 0, 1, 2, 3, 4 or 5.
/** The version number of this structure. Must be 0, 1, 2, 3, 4, 5 or 6.
* 0 means no sslVersion
* 1 means no verify, CApath
* 2 means no ssl_error_context, ssl_error_cb
* 3 means no ssl_psk_cb, ssl_psk_context, disableDefaultTrustStore
* 4 means no protos, protos_len
* 5 means no ssl_keylog_context or ssl_keylog_cb
*/
int struct_version;

Expand Down Expand Up @@ -781,9 +782,23 @@ typedef struct
* Exists only if struct_version >= 5
*/
unsigned int protos_len;

/**
* Callback function for handling the SSL keylogs.
* line: the SSL keylog line.
* ctx: pointer to context data set to ssl_keylog_context by the application.
* Exists only if struct_version >= 6
*/
void (*ssl_keylog_cb) (const char *line, void *ctx);

/**
* Application-specific contex for ssl_keylog_cb
* Exists only if struct_version >= 6
*/
void* ssl_keylog_context;
} MQTTClient_SSLOptions;

#define MQTTClient_SSLOptions_initializer { {'M', 'Q', 'T', 'S'}, 5, NULL, NULL, NULL, NULL, NULL, 1, MQTT_SSL_VERSION_DEFAULT, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0 }
#define MQTTClient_SSLOptions_initializer { {'M', 'Q', 'T', 'S'}, 5, NULL, NULL, NULL, NULL, NULL, 1, MQTT_SSL_VERSION_DEFAULT, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL }

/**
* MQTTClient_libraryInfo is used to store details relating to the currently used
Expand Down
42 changes: 42 additions & 0 deletions src/SSLSocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "Heap.h"

#include <string.h>
#include <errno.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/crypto.h>
Expand Down Expand Up @@ -79,6 +80,10 @@ static ssl_mutex_type sslCoreMutex;
/* Used to store MQTTClient_SSLOptions for TLS-PSK callback */
static int tls_ex_index_ssl_opts;

/* Used to store the callback function and context for SSL key logging*/
static void (*SSL_CTX_keylog_callback) (const char *line, void *ctx) = NULL;
static void * SSL_CTX_keylog_context = NULL;

#if defined(_WIN32) || defined(_WIN64)
#define iov_len len
#define iov_base buf
Expand Down Expand Up @@ -543,6 +548,38 @@ static unsigned int call_ssl_psk_cb(SSL *ssl, const char *hint, char *identity,
return rc;
}

static void call_ssl_keylog_cb(const SSL *ssl, const char *line)
{
FUNC_ENTRY;

{
char* envval = NULL;
static int log_printed = 0;
if (SSL_CTX_keylog_callback)
{
SSL_CTX_keylog_callback(line, SSL_CTX_keylog_context);
}
else if ((envval = getenv("SSLKEYLOGFILE")) != NULL && strlen(envval) > 0)
{
FILE* fout = fopen(envval, "a");
if (fout != NULL)
{
fprintf(fout, "%s\n", line);
fclose(fout);
}
else
{
if(log_printed == 0)
Log(LOG_ERROR, 1, "SSL failed to open SSLKEYLOGFILE \"%s\" with error: %s\n", envval, strerror(errno));

log_printed = 1;
}
}
}
exit:
FUNC_EXIT;
}

int SSLSocket_createContext(networkHandles* net, MQTTClient_SSLOptions* opts)
{
int rc = 1;
Expand Down Expand Up @@ -721,6 +758,11 @@ int SSLSocket_setSocketForSSL(networkHandles* net, MQTTClient_SSLOptions* opts,

SSL_CTX_set_info_callback(net->ctx, SSL_CTX_info_callback);
SSL_CTX_set_msg_callback(net->ctx, SSL_CTX_msg_callback);

SSL_CTX_keylog_callback = opts->ssl_keylog_cb;
SSL_CTX_keylog_context = opts->ssl_keylog_context;
SSL_CTX_set_keylog_callback(net->ctx, call_ssl_keylog_cb);

if (opts->enableServerCertAuth)
SSL_CTX_set_verify(net->ctx, SSL_VERIFY_PEER, NULL);

Expand Down