From adf15a38a148e89d73b8c19dca10d74b119d025d Mon Sep 17 00:00:00 2001 From: Ian Craggs Date: Mon, 26 Feb 2018 16:20:37 +0000 Subject: [PATCH] Check for SSLOptions structure when SSL protocol prefix is used #334 --- src/MQTTAsync.c | 12 +++++++++++- src/MQTTClient.c | 12 +++++++++++- test/test3.c | 7 ++++++- test/test5.c | 5 ++++- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/MQTTAsync.c b/src/MQTTAsync.c index fa9c5dba..938b633a 100644 --- a/src/MQTTAsync.c +++ b/src/MQTTAsync.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2017 IBM Corp. + * Copyright (c) 2009, 2018 IBM Corp. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -33,6 +33,7 @@ * Ian Craggs - SNI support * Ian Craggs - auto reconnect timing fix #218 * Ian Craggs - fix for issue #190 + * Ian Craggs - check for NULL SSL options #334 *******************************************************************************/ /** @@ -2254,6 +2255,15 @@ int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions* options) rc = MQTTASYNC_BAD_STRUCTURE; goto exit; } + +#if defined(OPENSSL) + if (m->ssl && options->ssl == NULL) + { + rc = MQTTCLIENT_NULL_PARAMETER; + goto exit; + } +#endif + if (options->will) /* check validity of will options structure */ { if (strncmp(options->will->struct_id, "MQTW", 4) != 0 || (options->will->struct_version != 0 && options->will->struct_version != 1)) diff --git a/src/MQTTClient.c b/src/MQTTClient.c index ed446fc6..a0efef81 100644 --- a/src/MQTTClient.c +++ b/src/MQTTClient.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2017 IBM Corp. + * Copyright (c) 2009, 2018 IBM Corp. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -32,6 +32,7 @@ * Ian Craggs - SNI support, message queue unpersist bug * Ian Craggs - binary will message support * Ian Craggs - waitforCompletion fix #240 + * Ian Craggs - check for NULL SSL options #334 *******************************************************************************/ /** @@ -1171,6 +1172,14 @@ int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options) goto exit; } +#if defined(OPENSSL) + if (m->ssl && options->ssl == NULL) + { + rc = MQTTCLIENT_NULL_PARAMETER; + goto exit; + } +#endif + if (options->will) /* check validity of will options structure */ { if (strncmp(options->will->struct_id, "MQTW", 4) != 0 || (options->will->struct_version != 0 && options->will->struct_version != 1)) @@ -1180,6 +1189,7 @@ int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options) } } + #if defined(OPENSSL) if (options->struct_version != 0 && options->ssl) /* check validity of SSL options structure */ { diff --git a/test/test3.c b/test/test3.c index d3956cd8..f4457449 100644 --- a/test/test3.c +++ b/test/test3.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012, 2017 IBM Corp. + * Copyright (c) 2012, 2018 IBM Corp. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -12,6 +12,7 @@ * * Contributors: * Allan Stockdill-Mander - initial API and implementation and/or initial documentation + * Ian Craggs - add SSL options NULL test *******************************************************************************/ /** @@ -584,6 +585,10 @@ int test1(struct Options options) opts.serverURIcount = options.hacount; } + /* Try with ssl opts == NULL - should get error */ + rc = MQTTClient_connect(c, &opts); + assert("Connect should fail", rc == MQTTCLIENT_NULL_PARAMETER, "rc was %d ", rc); + opts.ssl = &sslopts; if (options.server_key_file != NULL) opts.ssl->trustStore = options.server_key_file; /*file of certificates trusted by client*/ diff --git a/test/test5.c b/test/test5.c index acfbf567..4d0ba2ae 100644 --- a/test/test5.c +++ b/test/test5.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012, 2017 IBM Corp. + * Copyright (c) 2012, 2018 IBM Corp. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -637,6 +637,9 @@ int test1(struct Options options) opts.onFailure = test1OnFailure; opts.context = c; + rc = MQTTAsync_connect(c, &opts); + assert("Bad rc from connect", rc == MQTTASYNC_NULL_PARAMETER, "rc was %d ", rc); + opts.ssl = &sslopts; opts.ssl->enableServerCertAuth = 0;