Skip to content

Commit 85c99a4

Browse files
committed
demos: Update all demos to use the same alpn IDs.
1 parent c30d0d2 commit 85c99a4

File tree

7 files changed

+61
-183
lines changed

7 files changed

+61
-183
lines changed

demos/defender/defender_demo_json/mqtt_operations.c

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
/* Clock for timer. */
5757
#include "clock.h"
5858

59+
/* AWS IoT Core TLS ALPN definitions for MQTT authentication */
60+
#include "aws_iot_alpn_defs.h"
61+
5962
/**
6063
* These configurations are required. Throw compilation error if the below
6164
* configs are not defined.
@@ -97,25 +100,6 @@
97100
*/
98101
#define CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( CLIENT_IDENTIFIER ) - 1 ) )
99102

100-
/**
101-
* @brief ALPN protocol name for AWS IoT MQTT.
102-
*
103-
* This will be used if the AWS_MQTT_PORT is configured as 443 for AWS IoT MQTT
104-
* broker. Please see more details about the ALPN protocol for AWS IoT MQTT
105-
* endpoint in the link below.
106-
* https://aws.amazon.com/blogs/iot/mqtt-with-tls-client-authentication-on-port-443-why-it-is-useful-and-how-it-works/
107-
*
108-
* @note OpenSSL requires that the protocol string passed to it for configuration be encoded
109-
* with the prefix of 8-bit length information of the string. Thus, the 14 byte (0x0e) length
110-
* information is prefixed to the string.
111-
*/
112-
#define ALPN_PROTOCOL_NAME "\x0ex-amzn-mqtt-ca"
113-
114-
/**
115-
* @brief Length of ALPN protocol name.
116-
*/
117-
#define ALPN_PROTOCOL_NAME_LENGTH ( ( uint16_t ) ( sizeof( ALPN_PROTOCOL_NAME ) - 1 ) )
118-
119103
/**
120104
* @brief The maximum number of retries for connecting to server.
121105
*/
@@ -431,8 +415,8 @@ static bool connectToBrokerWithBackoffRetries( NetworkContext_t * pNetworkContex
431415
* in the link below.
432416
* https://aws.amazon.com/blogs/iot/mqtt-with-tls-client-authentication-on-port-443-why-it-is-useful-and-how-it-works/
433417
*/
434-
opensslCredentials.pAlpnProtos = ALPN_PROTOCOL_NAME;
435-
opensslCredentials.alpnProtosLen = ALPN_PROTOCOL_NAME_LENGTH;
418+
opensslCredentials.pAlpnProtos = AWS_IOT_ALPN_MQTT_CA_AUTH_OPENSSL;
419+
opensslCredentials.alpnProtosLen = AWS_IOT_ALPN_MQTT_CA_AUTH_OPENSSL_LEN;
436420
}
437421

438422
/* Seed pseudo random number generator used in the demo for

demos/fleet_provisioning/fleet_provisioning_with_csr/mqtt_operations.c

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
/* Clock for timer. */
5757
#include "clock.h"
5858

59+
/* AWS IoT Core TLS ALPN definitions for MQTT authentication */
60+
#include "aws_iot_alpn_defs.h"
61+
5962
/**
6063
* These configurations are required. Throw compilation error if the below
6164
* configs are not defined.
@@ -91,25 +94,6 @@
9194
*/
9295
#define CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( CLIENT_IDENTIFIER ) - 1 ) )
9396

94-
/**
95-
* @brief ALPN protocol name for AWS IoT MQTT.
96-
*
97-
* This will be used if the AWS_MQTT_PORT is configured as 443 for AWS IoT MQTT
98-
* broker. Please see more details about the ALPN protocol for AWS IoT MQTT
99-
* endpoint in the link below.
100-
* https://aws.amazon.com/blogs/iot/mqtt-with-tls-client-authentication-on-port-443-why-it-is-useful-and-how-it-works/
101-
*
102-
* @note OpenSSL requires that the protocol string passed to it for configuration be encoded
103-
* with the prefix of 8-bit length information of the string. Thus, the 14 byte (0x0e) length
104-
* information is prefixed to the string.
105-
*/
106-
#define ALPN_PROTOCOL_NAME "\x0ex-amzn-mqtt-ca"
107-
108-
/**
109-
* @brief Length of ALPN protocol name.
110-
*/
111-
#define ALPN_PROTOCOL_NAME_LENGTH ( ( uint16_t ) ( sizeof( ALPN_PROTOCOL_NAME ) - 1 ) )
112-
11397
/**
11498
* @brief The maximum number of retries for connecting to server.
11599
*/
@@ -409,7 +393,6 @@ static bool connectToBrokerWithBackoffRetries( NetworkContext_t * pNetworkContex
409393
BackoffAlgorithmContext_t reconnectParams;
410394
MbedtlsPkcs11Credentials_t tlsCredentials = { 0 };
411395
uint16_t nextRetryBackOff = 0U;
412-
const char * alpn[] = { ALPN_PROTOCOL_NAME, NULL };
413396

414397
/* Set the pParams member of the network context with desired transport. */
415398
pNetworkContext->pParams = &tlsContext;
@@ -430,12 +413,14 @@ static bool connectToBrokerWithBackoffRetries( NetworkContext_t * pNetworkContex
430413

431414
if( AWS_MQTT_PORT == 443 )
432415
{
416+
static const char * alpnProtoArray[] = AWS_IOT_ALPN_MQTT_CA_AUTH_MBEDTLS;
417+
433418
/* Pass the ALPN protocol name depending on the port being used.
434419
* Please see more details about the ALPN protocol for AWS IoT MQTT endpoint
435420
* in the link below.
436421
* https://aws.amazon.com/blogs/iot/mqtt-with-tls-client-authentication-on-port-443-why-it-is-useful-and-how-it-works/
437422
*/
438-
tlsCredentials.pAlpnProtos = alpn;
423+
tlsCredentials.pAlpnProtos = alpnProtoArray;
439424
}
440425

441426
/* Initialize reconnect attempts and interval */

demos/http/http_demo_mutual_auth/http_demo_mutual_auth.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
/* OpenSSL transport header. */
4141
#include "openssl_posix.h"
4242

43+
/* AWS IoT Core TLS ALPN definitions for MQTT authentication */
44+
#include "aws_iot_alpn_defs.h"
45+
4346
/* Check that AWS IoT Core endpoint is defined. */
4447
#ifndef AWS_IOT_ENDPOINT
4548
#error "AWS_IOT_ENDPOINT must be defined to your AWS IoT Core endpoint."
@@ -70,17 +73,6 @@
7073
#error "Please define a CLIENT_PRIVATE_KEY_PATH."
7174
#endif
7275

73-
/**
74-
* @brief ALPN protocol name to be sent as part of the ClientHello message.
75-
*
76-
* @note When using ALPN, port 443 must be used to connect to AWS IoT Core.
77-
*
78-
* @note OpenSSL requires that the protocol string passed to it for configuration be encoded
79-
* with the prefix of 8-bit length information of the string. Thus, the 14 byte (0x0e)
80-
* length information is prefixed to the string.
81-
*/
82-
#define IOT_CORE_ALPN_PROTOCOL_NAME "\x0ex-amzn-http-ca"
83-
8476
/* Check that transport timeout for transport send and receive is defined. */
8577
#ifndef TRANSPORT_SEND_RECV_TIMEOUT_MS
8678
#define TRANSPORT_SEND_RECV_TIMEOUT_MS ( 1000 )
@@ -185,8 +177,8 @@ static int32_t connectToServer( NetworkContext_t * pNetworkContext )
185177
/* ALPN is required when communicating to AWS IoT Core over port 443 through HTTP. */
186178
if( AWS_HTTPS_PORT == 443 )
187179
{
188-
opensslCredentials.pAlpnProtos = IOT_CORE_ALPN_PROTOCOL_NAME;
189-
opensslCredentials.alpnProtosLen = strlen( IOT_CORE_ALPN_PROTOCOL_NAME );
180+
opensslCredentials.pAlpnProtos = AWS_IOT_ALPN_HTTP_CA_AUTH_OPENSSL;
181+
opensslCredentials.alpnProtosLen = AWS_IOT_ALPN_MQTT_CA_AUTH_OPENSSL_LEN;
190182
}
191183

192184
/* Initialize server information. */

demos/mqtt/mqtt_demo_mutual_auth/mqtt_demo_mutual_auth.c

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
/* Clock for timer. */
7676
#include "clock.h"
7777

78+
/* AWS IoT Core TLS ALPN definitions for MQTT authentication */
79+
#include "aws_iot_alpn_defs.h"
80+
7881
/**
7982
* These configuration settings are required to run the mutual auth demo.
8083
* Throw compilation error if the below configs are not defined.
@@ -139,47 +142,12 @@
139142
/**
140143
* @brief Length of MQTT server host name.
141144
*/
142-
#define AWS_IOT_ENDPOINT_LENGTH ( ( uint16_t ) ( sizeof( AWS_IOT_ENDPOINT ) - 1 ) )
145+
#define AWS_IOT_ENDPOINT_LENGTH ( ( uint16_t ) ( sizeof( AWS_IOT_ENDPOINT ) - 1 ) )
143146

144147
/**
145148
* @brief Length of client identifier.
146149
*/
147-
#define CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( CLIENT_IDENTIFIER ) - 1 ) )
148-
149-
/**
150-
* @brief ALPN (Application-Layer Protocol Negotiation) protocol name for AWS IoT MQTT.
151-
*
152-
* This will be used if the AWS_MQTT_PORT is configured as 443 for AWS IoT MQTT broker.
153-
* Please see more details about the ALPN protocol for AWS IoT MQTT endpoint
154-
* in the link below.
155-
* https://aws.amazon.com/blogs/iot/mqtt-with-tls-client-authentication-on-port-443-why-it-is-useful-and-how-it-works/
156-
*
157-
* @note OpenSSL requires that the protocol string passed to it for configuration be encoded
158-
* with the prefix of 8-bit length information of the string. Thus, the 14 byte (0x0e) length
159-
* information is prefixed to the string.
160-
*/
161-
#define AWS_IOT_MQTT_ALPN "\x0ex-amzn-mqtt-ca"
162-
163-
/**
164-
* @brief Length of ALPN protocol name.
165-
*/
166-
#define AWS_IOT_MQTT_ALPN_LENGTH ( ( uint16_t ) ( sizeof( AWS_IOT_MQTT_ALPN ) - 1 ) )
167-
168-
/**
169-
* @brief This is the ALPN (Application-Layer Protocol Negotiation) string
170-
* required by AWS IoT for password-based authentication using TCP port 443.
171-
*
172-
* @note OpenSSL requires that the protocol string passed to it for configuration
173-
* be encoded with the prefix of 8-bit length information of the string. Thus, the
174-
* 4 byte (0x04) length information is prefixed to the string.
175-
*/
176-
#define AWS_IOT_PASSWORD_ALPN "\x04mqtt"
177-
178-
/**
179-
* @brief Length of password ALPN.
180-
*/
181-
#define AWS_IOT_PASSWORD_ALPN_LENGTH ( ( uint16_t ) ( sizeof( AWS_IOT_PASSWORD_ALPN ) - 1 ) )
182-
150+
#define CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( CLIENT_IDENTIFIER ) - 1 ) )
183151

184152
/**
185153
* @brief The maximum number of retries for connecting to server.
@@ -201,46 +169,45 @@
201169
*/
202170
#define CONNACK_RECV_TIMEOUT_MS ( 1000U )
203171

204-
205172
/**
206173
* @brief The topic to subscribe and publish to in the example.
207174
*
208175
* The topic name starts with the client identifier to ensure that each demo
209176
* interacts with a unique topic name.
210177
*/
211-
#define MQTT_EXAMPLE_TOPIC CLIENT_IDENTIFIER "/example/topic"
178+
#define MQTT_EXAMPLE_TOPIC CLIENT_IDENTIFIER "/example/topic"
212179

213180
/**
214181
* @brief Length of client MQTT topic.
215182
*/
216-
#define MQTT_EXAMPLE_TOPIC_LENGTH ( ( uint16_t ) ( sizeof( MQTT_EXAMPLE_TOPIC ) - 1 ) )
183+
#define MQTT_EXAMPLE_TOPIC_LENGTH ( ( uint16_t ) ( sizeof( MQTT_EXAMPLE_TOPIC ) - 1 ) )
217184

218185
/**
219186
* @brief The MQTT message published in this example.
220187
*/
221-
#define MQTT_EXAMPLE_MESSAGE "Hello World!"
188+
#define MQTT_EXAMPLE_MESSAGE "Hello World!"
222189

223190
/**
224191
* @brief The length of the MQTT message published in this example.
225192
*/
226-
#define MQTT_EXAMPLE_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_EXAMPLE_MESSAGE ) - 1 ) )
193+
#define MQTT_EXAMPLE_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_EXAMPLE_MESSAGE ) - 1 ) )
227194

228195
/**
229196
* @brief Maximum number of outgoing publishes maintained in the application
230197
* until an ack is received from the broker.
231198
*/
232-
#define MAX_OUTGOING_PUBLISHES ( 5U )
199+
#define MAX_OUTGOING_PUBLISHES ( 5U )
233200

234201
/**
235202
* @brief Invalid packet identifier for the MQTT packets. Zero is always an
236203
* invalid packet identifier as per MQTT 3.1.1 spec.
237204
*/
238-
#define MQTT_PACKET_ID_INVALID ( ( uint16_t ) 0U )
205+
#define MQTT_PACKET_ID_INVALID ( ( uint16_t ) 0U )
239206

240207
/**
241208
* @brief Timeout for MQTT_ProcessLoop function in milliseconds.
242209
*/
243-
#define MQTT_PROCESS_LOOP_TIMEOUT_MS ( 500U )
210+
#define MQTT_PROCESS_LOOP_TIMEOUT_MS ( 500U )
244211

245212
/**
246213
* @brief The maximum time interval in seconds which is allowed to elapse
@@ -251,37 +218,37 @@
251218
* absence of sending any other Control Packets, the Client MUST send a
252219
* PINGREQ Packet.
253220
*/
254-
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS ( 60U )
221+
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS ( 60U )
255222

256223
/**
257224
* @brief Delay between MQTT publishes in seconds.
258225
*/
259-
#define DELAY_BETWEEN_PUBLISHES_SECONDS ( 1U )
226+
#define DELAY_BETWEEN_PUBLISHES_SECONDS ( 1U )
260227

261228
/**
262229
* @brief Number of PUBLISH messages sent per iteration.
263230
*/
264-
#define MQTT_PUBLISH_COUNT_PER_LOOP ( 5U )
231+
#define MQTT_PUBLISH_COUNT_PER_LOOP ( 5U )
265232

266233
/**
267234
* @brief Delay in seconds between two iterations of subscribePublishLoop().
268235
*/
269-
#define MQTT_SUBPUB_LOOP_DELAY_SECONDS ( 5U )
236+
#define MQTT_SUBPUB_LOOP_DELAY_SECONDS ( 5U )
270237

271238
/**
272239
* @brief Transport timeout in milliseconds for transport send and receive.
273240
*/
274-
#define TRANSPORT_SEND_RECV_TIMEOUT_MS ( 500 )
241+
#define TRANSPORT_SEND_RECV_TIMEOUT_MS ( 500 )
275242

276243
/**
277244
* @brief The MQTT metrics string expected by AWS IoT.
278245
*/
279-
#define METRICS_STRING "?SDK=" OS_NAME "&Version=" OS_VERSION "&Platform=" HARDWARE_PLATFORM_NAME "&MQTTLib=" MQTT_LIB
246+
#define METRICS_STRING "?SDK=" OS_NAME "&Version=" OS_VERSION "&Platform=" HARDWARE_PLATFORM_NAME "&MQTTLib=" MQTT_LIB
280247

281248
/**
282249
* @brief The length of the MQTT metrics string expected by AWS IoT.
283250
*/
284-
#define METRICS_STRING_LENGTH ( ( uint16_t ) ( sizeof( METRICS_STRING ) - 1 ) )
251+
#define METRICS_STRING_LENGTH ( ( uint16_t ) ( sizeof( METRICS_STRING ) - 1 ) )
285252

286253

287254
#ifdef CLIENT_USERNAME
@@ -673,22 +640,22 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext
673640

674641
if( AWS_MQTT_PORT == 443 )
675642
{
676-
/* Pass the ALPN protocol name depending on the port being used.
643+
/* Pass the ALPN protocol name depending on the port and auth type being used.
677644
* Please see more details about the ALPN protocol for the AWS IoT MQTT
678645
* endpoint in the link below.
679646
* https://aws.amazon.com/blogs/iot/mqtt-with-tls-client-authentication-on-port-443-why-it-is-useful-and-how-it-works/
680647
*
681648
* For username and password based authentication in AWS IoT,
682-
* #AWS_IOT_PASSWORD_ALPN is used. More details can be found in the
649+
* #AWS_IOT_ALPN_MQTT_CUSTOM_AUTH is used. More details can be found in the
683650
* link below.
684651
* https://docs.aws.amazon.com/iot/latest/developerguide/custom-authentication.html
685652
*/
686653
#ifdef CLIENT_USERNAME
687-
opensslCredentials.pAlpnProtos = AWS_IOT_PASSWORD_ALPN;
688-
opensslCredentials.alpnProtosLen = AWS_IOT_PASSWORD_ALPN_LENGTH;
654+
opensslCredentials.pAlpnProtos = AWS_IOT_ALPN_MQTT_CUSTOM_AUTH_OPENSSL;
655+
opensslCredentials.alpnProtosLen = AWS_IOT_ALPN_MQTT_CUSTOM_AUTH_OPENSSL_LEN;
689656
#else
690-
opensslCredentials.pAlpnProtos = AWS_IOT_MQTT_ALPN;
691-
opensslCredentials.alpnProtosLen = AWS_IOT_MQTT_ALPN_LENGTH;
657+
opensslCredentials.pAlpnProtos = AWS_IOT_ALPN_MQTT_CA_AUTH_OPENSSL;
658+
opensslCredentials.alpnProtosLen = AWS_IOT_ALPN_MQTT_CA_AUTH_OPENSSL_LEN;
692659
#endif
693660
}
694661

demos/ota/ota_demo_core_http/ota_demo_core_http.c

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
/* Include firmware version struct definition. */
7070
#include "ota_appversion32.h"
7171

72+
/* AWS IoT Core TLS ALPN definitions for MQTT authentication */
73+
#include "aws_iot_alpn_defs.h"
74+
7275
/**
7376
* These configuration settings are required to run the OTA demo which uses mutual authentication.
7477
* Throw compilation error if the below configs are not defined.
@@ -92,25 +95,6 @@
9295
#error "Please define path to client private key(CLIENT_PRIVATE_KEY_PATH) in demo_config.h."
9396
#endif
9497

95-
/**
96-
* @brief ALPN (Application-Layer Protocol Negotiation) protocol name for AWS IoT MQTT.
97-
*
98-
* This will be used if the AWS_MQTT_PORT is configured as 443 for AWS IoT MQTT broker.
99-
* Please see more details about the ALPN protocol for AWS IoT MQTT endpoint
100-
* in the link below.
101-
* https://aws.amazon.com/blogs/iot/mqtt-with-tls-client-authentication-on-port-443-why-it-is-useful-and-how-it-works/
102-
*
103-
* @note OpenSSL requires that the protocol string passed to it for configuration be encoded
104-
* with the prefix of 8-bit length information of the string. Thus, the 14 byte (0x0e) length
105-
* information is prefixed to the string.
106-
*/
107-
#define AWS_IOT_MQTT_ALPN "\x0ex-amzn-mqtt-ca"
108-
109-
/**
110-
* @brief Length of ALPN protocol name.
111-
*/
112-
#define AWS_IOT_MQTT_ALPN_LENGTH ( ( uint16_t ) ( sizeof( AWS_IOT_MQTT_ALPN ) - 1 ) )
113-
11498
/**
11599
* @brief Length of MQTT server host name.
116100
*/
@@ -1129,11 +1113,11 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext
11291113
* https://docs.aws.amazon.com/iot/latest/developerguide/custom-authentication.html
11301114
*/
11311115
#ifdef CLIENT_USERNAME
1132-
opensslCredentials.pAlpnProtos = AWS_IOT_PASSWORD_ALPN;
1133-
opensslCredentials.alpnProtosLen = AWS_IOT_PASSWORD_ALPN_LENGTH;
1116+
opensslCredentials.pAlpnProtos = AWS_IOT_ALPN_MQTT_CUSTOM_AUTH_OPENSSL;
1117+
opensslCredentials.alpnProtosLen = AWS_IOT_ALPN_MQTT_CUSTOM_AUTH_OPENSSL_LEN;
11341118
#else
1135-
opensslCredentials.pAlpnProtos = AWS_IOT_MQTT_ALPN;
1136-
opensslCredentials.alpnProtosLen = AWS_IOT_MQTT_ALPN_LENGTH;
1119+
opensslCredentials.pAlpnProtos = AWS_IOT_ALPN_MQTT_CA_AUTH_OPENSSL;
1120+
opensslCredentials.alpnProtosLen = AWS_IOT_ALPN_MQTT_CA_AUTH_OPENSSL_LEN;
11371121
#endif
11381122
}
11391123

0 commit comments

Comments
 (0)