From fb7d71873c46f9c89706375adb335cdc537532e5 Mon Sep 17 00:00:00 2001 From: bricewge Date: Wed, 27 Jun 2018 17:04:49 +0200 Subject: [PATCH] fix #833 Signed-off-by: Brice Waegeneire --- src/handle_connect.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/handle_connect.c b/src/handle_connect.c index 4246af167..dc1291495 100644 --- a/src/handle_connect.c +++ b/src/handle_connect.c @@ -132,6 +132,7 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) X509 *client_cert = NULL; X509_NAME *name; X509_NAME_ENTRY *name_entry; + ASN1_STRING *name_asn1 = NULL; #endif G_CONNECTION_COUNT_INC(); @@ -447,7 +448,19 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) } name_entry = X509_NAME_get_entry(name, i); if(name_entry){ - context->username = mosquitto__strdup((char *)X509_NAME_ENTRY_get_data(name_entry)); + name_asn1 = X509_NAME_ENTRY_get_data(name_entry); + if (name_asn1 == NULL) { + send__connack(context, 0, CONNACK_REFUSED_BAD_USERNAME_PASSWORD); + rc = 1; + goto handle_connect_error; + } + context->username = mosquitto__strdup((char *) ASN1_STRING_data(name_asn1)); + /* Make sure there isn't an embedded NUL character in the CN */ + if ((size_t)ASN1_STRING_length(name_asn1) != strlen(context->username)) { + send__connack(context, 0, CONNACK_REFUSED_BAD_USERNAME_PASSWORD); + rc = 1; + goto handle_connect_error; + } } } else { // use_subject_as_username BIO *subject_bio = BIO_new(BIO_s_mem());