Skip to content

Commit

Permalink
Use AWS IoT Core support for MQTT over port 443 (aws#141)
Browse files Browse the repository at this point in the history
* Use AWS IoT Core support for MQTT over port 443 in the five sample projects (this is done using ALPN during TLS negotiation). Also, fix a compiler warning in the three subscribe/publish projects.

* Use AWS IoT Core support for MQTT over port 443 in the tests.
  • Loading branch information
dcgaws authored and Gordon Wang committed Mar 26, 2018
1 parent d3ea2e8 commit 77f657e
Show file tree
Hide file tree
Showing 19 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/aws_iot_shadow_interface.h
Expand Up @@ -51,7 +51,7 @@ extern "C" {
*/
typedef struct {
char *pHost; ///< This will be unique to a customer and can be retrieved from the console
uint16_t port; ///< By default the port is 8883
uint16_t port; ///< Network port for TCP/IP socket
char *pRootCA; ///< Location with the Filename of the Root CA
char *pClientCRT; ///< Location of Device certs signed by AWS IoT service
char *pClientKey; ///< Location of Device private key
Expand Down
10 changes: 10 additions & 0 deletions platform/linux/mbedtls/network_mbedtls_wrapper.c
Expand Up @@ -101,6 +101,7 @@ IoT_Error_t iot_tls_connect(Network *pNetwork, TLSConnectParams *params) {
TLSDataParams *tlsDataParams = NULL;
char portBuffer[6];
char vrfy_buf[512];
const char *alpnProtocols[] = { "x-amzn-mqtt-ca", NULL };

#ifdef ENABLE_IOT_DEBUG
unsigned char buf[MBEDTLS_DEBUG_BUFFER_SIZE];
Expand Down Expand Up @@ -202,6 +203,15 @@ IoT_Error_t iot_tls_connect(Network *pNetwork, TLSConnectParams *params) {

mbedtls_ssl_conf_read_timeout(&(tlsDataParams->conf), pNetwork->tlsConnectParams.timeout_ms);

/* Use the AWS IoT ALPN extension for MQTT if port 443 is requested. */
if(443 == pNetwork->tlsConnectParams.DestinationPort) {
if((ret = mbedtls_ssl_conf_alpn_protocols(&(tlsDataParams->conf), alpnProtocols)) != 0) {
IOT_ERROR(" failed\n ! mbedtls_ssl_conf_alpn_protocols returned -0x%x\n\n", -ret);
return SSL_CONNECTION_ERROR;
}
}

/* Assign the resulting configuration to the SSL context. */
if((ret = mbedtls_ssl_setup(&(tlsDataParams->ssl), &(tlsDataParams->conf))) != 0) {
IOT_ERROR(" failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret);
return SSL_CONNECTION_ERROR;
Expand Down
2 changes: 1 addition & 1 deletion samples/linux/jobs_sample/aws_iot_config.h
Expand Up @@ -24,7 +24,7 @@
// Get from console
// =================================================
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name
Expand Down
2 changes: 1 addition & 1 deletion samples/linux/shadow_sample/aws_iot_config.h
Expand Up @@ -24,7 +24,7 @@
// Get from console
// =================================================
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name
Expand Down
2 changes: 1 addition & 1 deletion samples/linux/shadow_sample_console_echo/aws_iot_config.h
Expand Up @@ -24,7 +24,7 @@
// Get from console
// =================================================
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name
Expand Down
Expand Up @@ -24,7 +24,7 @@
// Get from console
// =================================================
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name
Expand Down
Expand Up @@ -62,7 +62,7 @@ void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, ui
IOT_UNUSED(pData);
IOT_UNUSED(pClient);
IOT_INFO("Subscribe callback");
IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, params->payload);
IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, (char *) params->payload);
}

void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data) {
Expand Down
Expand Up @@ -24,7 +24,7 @@
// Get from console
// =================================================
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name
Expand Down
Expand Up @@ -62,7 +62,7 @@ void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, ui
IOT_UNUSED(pData);
IOT_UNUSED(pClient);
IOT_INFO("Subscribe callback");
IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, params->payload);
IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, (char *) params->payload);
}

void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data) {
Expand Down
2 changes: 1 addition & 1 deletion samples/linux/subscribe_publish_sample/aws_iot_config.h
Expand Up @@ -24,7 +24,7 @@
// Get from console
// =================================================
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name
Expand Down
Expand Up @@ -62,7 +62,7 @@ void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, ui
IOT_UNUSED(pData);
IOT_UNUSED(pClient);
IOT_INFO("Subscribe callback");
IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, params->payload);
IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, (char *) params->payload);
}

void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data) {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/include/aws_iot_config.h
Expand Up @@ -19,7 +19,7 @@
// Get from console
// =================================================
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name
Expand Down
Expand Up @@ -226,7 +226,7 @@ int aws_iot_mqtt_tests_multi_threading_validation() {

IOT_DEBUG(" Root CA Path : %s\n clientCRT : %s\n clientKey : %s\n", root_CA, clientCRT, clientKey);
initParams.pHostURL = AWS_IOT_MQTT_HOST;
initParams.port = 8883;
initParams.port = AWS_IOT_MQTT_PORT;
initParams.pRootCALocation = root_CA;
initParams.pDeviceCertLocation = clientCRT;
initParams.pDevicePrivateKeyLocation = clientKey;
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/src/aws_iot_test_auto_reconnect.c
Expand Up @@ -101,7 +101,7 @@ int aws_iot_mqtt_tests_auto_reconnect() {
printf(" Root CA Path : %s\n clientCRT : %s\n clientKey : %s\n", root_CA, clientCRT, clientKey);
IoT_Client_Init_Params initParams;
initParams.pHostURL = AWS_IOT_MQTT_HOST;
initParams.port = 8883;
initParams.port = AWS_IOT_MQTT_PORT;
initParams.pRootCALocation = root_CA;
initParams.pDeviceCertLocation = clientCRT;
initParams.pDevicePrivateKeyLocation = clientKey;
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/src/aws_iot_test_basic_connectivity.c
Expand Up @@ -190,7 +190,7 @@ int aws_iot_mqtt_tests_basic_connectivity() {

IOT_DEBUG("Root CA Path : %s\n clientCRT : %s\n clientKey : %s\n", root_CA, clientCRT, clientKey);
initParams.pHostURL = AWS_IOT_MQTT_HOST;
initParams.port = 8883;
initParams.port = AWS_IOT_MQTT_PORT;
initParams.pRootCALocation = root_CA;
initParams.pDeviceCertLocation = clientCRT;
initParams.pDevicePrivateKeyLocation = clientKey;
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/src/aws_iot_test_jobs_api.c
Expand Up @@ -191,7 +191,7 @@ int aws_iot_jobs_basic_test() {

IOT_DEBUG("Root CA Path : %s\n clientCRT : %s\n clientKey : %s\n", root_CA, clientCRT, clientKey);
initParams.pHostURL = AWS_IOT_MQTT_HOST;
initParams.port = 8883;
initParams.port = AWS_IOT_MQTT_PORT;
initParams.pRootCALocation = root_CA;
initParams.pDeviceCertLocation = clientCRT;
initParams.pDevicePrivateKeyLocation = clientKey;
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/src/aws_iot_test_multiple_clients.c
Expand Up @@ -66,7 +66,7 @@ static IoT_Error_t aws_iot_mqtt_tests_connect_client_to_service(AWS_IoT_Client *
struct timeval start, end;

initParams.pHostURL = AWS_IOT_MQTT_HOST;
initParams.port = 8883;
initParams.port = AWS_IOT_MQTT_PORT;
initParams.pRootCALocation = rootCA;
initParams.pDeviceCertLocation = clientCRT;
initParams.pDevicePrivateKeyLocation = clientKey;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/include/aws_iot_config.h
Expand Up @@ -24,7 +24,7 @@
// Get from console
// =================================================
#define AWS_IOT_MQTT_HOST "localhost"
#define AWS_IOT_MQTT_PORT 8883
#define AWS_IOT_MQTT_PORT 443
#define AWS_IOT_MQTT_CLIENT_ID "C-SDK_UnitTestClient"
#define AWS_IOT_MY_THING_NAME "C-SDK_UnitTestThing"
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt"
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/src/aws_iot_tests_unit_disconnect_helper.c
Expand Up @@ -179,7 +179,7 @@ TEST_C(DisconnectTests, SetHandlerAndInvokedOnDisconnect) {
IOT_DEBUG("-->Running Disconnect Tests - F:7 - Disconnect, with set handler and invoked on disconnect \n");

handlerInvoked = false;
InitMQTTParamsSetup(&initParams, "localhost", 8883, false, NULL);
InitMQTTParamsSetup(&initParams, "localhost", AWS_IOT_MQTT_PORT, false, NULL);
rc = aws_iot_mqtt_init(&iotClient, &initParams);
CHECK_EQUAL_C_INT(SUCCESS, rc);

Expand Down

0 comments on commit 77f657e

Please sign in to comment.