Skip to content

Commit cc5d762

Browse files
smcd253Spencer McDonough
andauthored
Add Support for Self-Signed X509 Certificates - RSA Only (#159)
Add support support for self-signed x509 certificates. Only RSA encryption supported at this time. Not tested with CA-signed certs. Co-authored-by: Spencer McDonough <[email protected]>
1 parent ae5d99c commit cc5d762

22 files changed

+704
-42
lines changed

.github/workflows/ATSAME54-XPRO.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ jobs:
3838

3939
- name: Install Ninja
4040
uses: seanmiddleditch/gha-setup-ninja@v3
41-
4241
- name: Build project
4342
run: |
4443
cmake -Bbuild -GNinja -DCMAKE_TOOLCHAIN_FILE="../../cmake/arm-gcc-cortex-m4.cmake"

MXChip/AZ3166/AZ3166.code-workspace

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"folders": [
33
{
44
"path": "."
5+
},
6+
{
7+
"path": "..\\.."
58
}
69
],
710
"settings": {

MXChip/AZ3166/app/azure_config.h

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,52 @@ typedef enum
2121

2222
// ----------------------------------------------------------------------------
2323
// Azure IoT Hub Connection Transport
24-
// Define to use the legacy MQTT connection, else Azure RTOS SDK for Azure IoT
24+
// Define to use the legacy MQTT connection, else Azure RTOS SDK for Azure IoT
2525
// ----------------------------------------------------------------------------
2626
//#define ENABLE_LEGACY_MQTT
2727

2828
// ----------------------------------------------------------------------------
2929
// Azure IoT Dynamic Provisioning Service
30-
// Define this to use the DPS service, otherwise direct IoT Hub
30+
// Define this to use the DPS service, otherwise direct IoT Hub
3131
// ----------------------------------------------------------------------------
32-
//#define ENABLE_DPS
32+
// #define ENABLE_DPS
3333

3434
// ----------------------------------------------------------------------------
35-
// Azure IoT Hub config
35+
// Azure IoT DPS Self-Signed X509Certificate
36+
// Define this to connect to DPS or Iot Hub using a self-signed X509
37+
/// certificate
38+
// ----------------------------------------------------------------------------
39+
// #define ENABLE_X509
40+
41+
// ----------------------------------------------------------------------------
42+
// Azure IoT Device ID
43+
// Make sure this is the same as the Device ID on the corresponding IoT Hub
44+
// NOTE: To be used only when ENABLE_DPS is NOT defined
3645
// ----------------------------------------------------------------------------
37-
#define IOT_HUB_HOSTNAME ""
3846
#define IOT_DEVICE_ID ""
47+
48+
// ----------------------------------------------------------------------------
49+
// Azure IoT SAS Key
50+
// The SAS key generated by configuring an IoT Hub device or DPS individual
51+
// enrollment
52+
// NOTE: To be used only when ENABLE_X509 is not defined
53+
// ----------------------------------------------------------------------------
3954
#define IOT_PRIMARY_KEY ""
4055

56+
// ----------------------------------------------------------------------------
57+
// Azure IoT Hub Hostname
58+
// The Hostname found on your IoT Hub Overview page
59+
// NOTE: To be used only when ENABLE_DPS is not defined
60+
// ----------------------------------------------------------------------------
61+
#define IOT_HUB_HOSTNAME ""
62+
4163
// ----------------------------------------------------------------------------
4264
// Azure IoT DPS config
65+
// DPS connection information
66+
// IOT_DPS_ENDPOINT is always "global.azure-devices-provisioning.net"
67+
// IOT_DPS_ID_SCOPE can be found on your DPS Overview page
68+
// IOT_DPS_REGISTRATION ID is the title of your individual enrollment
69+
// containing your SAS key or X509 certificate
4370
// ----------------------------------------------------------------------------
4471
#define IOT_DPS_ENDPOINT "global.azure-devices-provisioning.net"
4572
#define IOT_DPS_ID_SCOPE ""
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef _AZURE_DEVICE_X509_CERT_CONFIG_H
2+
#define _AZURE_DEVICE_X509_CERT_CONFIG_H
3+
4+
// ----------------------------------------------------------------------------
5+
// Azure IoT X509 Device Certificate
6+
// Replace {0x00} with your formatted output from OpenSSL and xxd here
7+
// ----------------------------------------------------------------------------
8+
const unsigned char iot_x509_device_cert[] = {0x00};
9+
unsigned int iot_x509_device_cert_len = sizeof(iot_x509_device_cert);
10+
11+
// ----------------------------------------------------------------------------
12+
// Azure IoT X509 Device Private Key
13+
// Replace {0x00} with your formatted output from OpenSSL and xxd here
14+
// ----------------------------------------------------------------------------
15+
unsigned char iot_x509_private_key[] = {0x00};
16+
const unsigned int iot_x509_private_key_len = sizeof(iot_x509_private_key);
17+
18+
#endif

MXChip/AZ3166/app/nx_client.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#include "nx_azure_iot_pnp_helpers.h"
1919

2020
#include "azure_config.h"
21+
#include "azure_device_x509_cert_config.h"
22+
23+
#define IOT_MODEL_ID "dtmi:com:example:azurertos:gsg;1"
2124
#include "azure_pnp_info.h"
2225

2326
#define IOT_MODEL_ID "dtmi:azurertos:devkit:gsgmxchip;1"
@@ -334,6 +337,22 @@ UINT azure_iot_nx_client_entry(
334337
}
335338

336339
#ifdef ENABLE_DPS
340+
# ifdef ENABLE_X509
341+
status = azure_iot_nx_client_dps_create(&azure_iot_nx_client,
342+
ip_ptr,
343+
pool_ptr,
344+
dns_ptr,
345+
unix_time_callback,
346+
IOT_DPS_ENDPOINT,
347+
IOT_DPS_ID_SCOPE,
348+
IOT_DPS_REGISTRATION_ID,
349+
"",
350+
(UCHAR*)iot_x509_device_cert,
351+
iot_x509_device_cert_len,
352+
(UCHAR*)iot_x509_private_key,
353+
iot_x509_private_key_len,
354+
IOT_MODEL_ID);
355+
# else
337356
status = azure_iot_nx_client_dps_create(&azure_iot_nx_client,
338357
ip_ptr,
339358
pool_ptr,
@@ -343,8 +362,28 @@ UINT azure_iot_nx_client_entry(
343362
IOT_DPS_ID_SCOPE,
344363
IOT_DPS_REGISTRATION_ID,
345364
IOT_PRIMARY_KEY,
365+
NULL,
366+
0,
367+
NULL,
368+
0,
346369
IOT_MODEL_ID);
370+
# endif
347371
#else
372+
# ifdef ENABLE_X509
373+
status = azure_iot_nx_client_create(&azure_iot_nx_client,
374+
ip_ptr,
375+
pool_ptr,
376+
dns_ptr,
377+
unix_time_callback,
378+
IOT_HUB_HOSTNAME,
379+
IOT_DEVICE_ID,
380+
"",
381+
(UCHAR*)iot_x509_device_cert,
382+
iot_x509_device_cert_len,
383+
(UCHAR*)iot_x509_private_key,
384+
iot_x509_private_key_len,
385+
IOT_MODEL_ID);
386+
# else
348387
status = azure_iot_nx_client_create(&azure_iot_nx_client,
349388
ip_ptr,
350389
pool_ptr,
@@ -353,7 +392,12 @@ UINT azure_iot_nx_client_entry(
353392
IOT_HUB_HOSTNAME,
354393
IOT_DEVICE_ID,
355394
IOT_PRIMARY_KEY,
395+
NULL,
396+
0,
397+
NULL,
398+
0,
356399
IOT_MODEL_ID);
400+
# endif
357401
#endif
358402
if (status != NX_SUCCESS)
359403
{

Microchip/ATSAME54-XPRO/app/azure_config.h

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,64 @@
88
// 0 - BME280 sensor is not present
99
// 1 - BME280 sensor is present
1010
// ----------------------------------------------------------------------------
11-
#define __SENSOR_BME280__ 1
11+
#define __SENSOR_BME280__ 0
1212

1313
// ----------------------------------------------------------------------------
1414
// Azure IoT Hub Connection Transport
15-
// Define to use the legacy MQTT connection, else Azure RTOS SDK for Azure IoT
15+
// Define to use the legacy MQTT connection, else Azure RTOS SDK for Azure IoT
1616
// ----------------------------------------------------------------------------
1717
//#define ENABLE_LEGACY_MQTT
1818

1919
// ----------------------------------------------------------------------------
2020
// Azure IoT Dynamic Provisioning Service
21-
// Define this to use the DPS service, otherwise direct IoT Hub
21+
// Define this to use the DPS service, otherwise direct IoT Hub
2222
// ----------------------------------------------------------------------------
23-
//#define ENABLE_DPS
23+
// #define ENABLE_DPS
2424

2525
// ----------------------------------------------------------------------------
26-
// Azure IoT Hub config
26+
// Azure IoT DPS Self-Signed X509Certificate
27+
// Define this to connect to DPS or Iot Hub using a self-signed X509
28+
/// certificate
29+
// ----------------------------------------------------------------------------
30+
// #define ENABLE_X509
31+
32+
// ----------------------------------------------------------------------------
33+
// Azure IoT Device ID
34+
// Make sure this is the same as the Device ID on the corresponding IoT Hub
35+
// NOTE: To be used only when ENABLE_DPS is NOT defined
2736
// ----------------------------------------------------------------------------
28-
#define IOT_HUB_HOSTNAME ""
2937
#define IOT_DEVICE_ID ""
38+
39+
#ifndef ENABLE_X509
40+
// ----------------------------------------------------------------------------
41+
// Azure IoT SAS Key
42+
// The SAS key generated by configuring an IoT Hub device or DPS individual
43+
// enrollment
44+
// NOTE: To be used only when ENABLE_X509 is not defined
45+
// ----------------------------------------------------------------------------
3046
#define IOT_PRIMARY_KEY ""
47+
#endif
48+
49+
#ifndef ENABLE_DPS
50+
// ----------------------------------------------------------------------------
51+
// Azure IoT Hub Hostname
52+
// The Hostname found on your IoT Hub Overview page
53+
// NOTE: To be used only when ENABLE_DPS is not defined
54+
// ----------------------------------------------------------------------------
55+
#define IOT_HUB_HOSTNAME ""
3156

57+
#else
3258
// ----------------------------------------------------------------------------
3359
// Azure IoT DPS config
60+
// DPS connection information
61+
// IOT_DPS_ENDPOINT is always "global.azure-devices-provisioning.net"
62+
// IOT_DPS_ID_SCOPE can be found on your DPS Overview page
63+
// IOT_DPS_REGISTRATION ID is the title of your individual enrollment
64+
// containing your SAS key or X509 certificate
3465
// ----------------------------------------------------------------------------
3566
#define IOT_DPS_ENDPOINT "global.azure-devices-provisioning.net"
3667
#define IOT_DPS_ID_SCOPE ""
3768
#define IOT_DPS_REGISTRATION_ID ""
69+
#endif
3870

3971
#endif // _AZURE_CONFIG_H
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef _AZURE_DEVICE_X509_CERT_CONFIG_H
2+
#define _AZURE_DEVICE_X509_CERT_CONFIG_H
3+
4+
// ----------------------------------------------------------------------------
5+
// Azure IoT X509 Device Certificate
6+
// Replace {0x00} with your formatted output from OpenSSL and xxd here
7+
// ----------------------------------------------------------------------------
8+
const unsigned char iot_x509_device_cert[] = {0x00};
9+
unsigned int iot_x509_device_cert_len = sizeof(iot_x509_device_cert);
10+
11+
// ----------------------------------------------------------------------------
12+
// Azure IoT X509 Device Private Key
13+
// Replace {0x00} with your formatted output from OpenSSL and xxd here
14+
// ----------------------------------------------------------------------------
15+
unsigned char iot_x509_private_key[] = {0x00};
16+
const unsigned int iot_x509_private_key_len = sizeof(iot_x509_private_key);
17+
18+
#endif

Microchip/ATSAME54-XPRO/app/nx_client.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "nx_azure_iot_pnp_helpers.h"
1818

1919
#include "azure_config.h"
20+
#include "azure_device_x509_cert_config.h"
2021
#include "azure_pnp_info.h"
2122

2223
#define IOT_MODEL_ID "dtmi:azurertos:devkit:gsg;1"
@@ -198,6 +199,22 @@ UINT azure_iot_nx_client_entry(
198199
}
199200

200201
#ifdef ENABLE_DPS
202+
# ifdef ENABLE_X509
203+
status = azure_iot_nx_client_dps_create(&azure_iot_nx_client,
204+
ip_ptr,
205+
pool_ptr,
206+
dns_ptr,
207+
unix_time_callback,
208+
IOT_DPS_ENDPOINT,
209+
IOT_DPS_ID_SCOPE,
210+
IOT_DPS_REGISTRATION_ID,
211+
"",
212+
(UCHAR*)iot_x509_device_cert,
213+
iot_x509_device_cert_len,
214+
(UCHAR*)iot_x509_private_key,
215+
iot_x509_private_key_len,
216+
IOT_MODEL_ID);
217+
# else
201218
status = azure_iot_nx_client_dps_create(&azure_iot_nx_client,
202219
ip_ptr,
203220
pool_ptr,
@@ -207,8 +224,28 @@ UINT azure_iot_nx_client_entry(
207224
IOT_DPS_ID_SCOPE,
208225
IOT_DPS_REGISTRATION_ID,
209226
IOT_PRIMARY_KEY,
227+
NULL,
228+
0,
229+
NULL,
230+
0,
210231
IOT_MODEL_ID);
232+
# endif
211233
#else
234+
# ifdef ENABLE_X509
235+
status = azure_iot_nx_client_create(&azure_iot_nx_client,
236+
ip_ptr,
237+
pool_ptr,
238+
dns_ptr,
239+
unix_time_callback,
240+
IOT_HUB_HOSTNAME,
241+
IOT_DEVICE_ID,
242+
"",
243+
(UCHAR*)iot_x509_device_cert,
244+
iot_x509_device_cert_len,
245+
(UCHAR*)iot_x509_private_key,
246+
iot_x509_private_key_len,
247+
IOT_MODEL_ID);
248+
# else
212249
status = azure_iot_nx_client_create(&azure_iot_nx_client,
213250
ip_ptr,
214251
pool_ptr,
@@ -217,7 +254,12 @@ UINT azure_iot_nx_client_entry(
217254
IOT_HUB_HOSTNAME,
218255
IOT_DEVICE_ID,
219256
IOT_PRIMARY_KEY,
257+
NULL,
258+
0,
259+
NULL,
260+
0,
220261
IOT_MODEL_ID);
262+
# endif
221263
#endif
222264
if (status != NX_SUCCESS)
223265
{

NXP/MIMXRT1050-EVKB/app/azure_config.h

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,55 @@
66

77
// ----------------------------------------------------------------------------
88
// Azure IoT Hub Connection Transport
9-
// Define to use the legacy MQTT connection, else Azure RTOS SDK for Azure IoT
9+
// Define to use the legacy MQTT connection, else Azure RTOS SDK for Azure IoT
1010
// ----------------------------------------------------------------------------
1111
//#define ENABLE_LEGACY_MQTT
1212

1313
// ----------------------------------------------------------------------------
14-
// Azure IoT Hub Connection Transport
15-
// Define to use the legacy MQTT connection, else Azure RTOS SDK for Azure IoT
14+
// Azure IoT Dynamic Provisioning Service
15+
// Define this to use the DPS service, otherwise direct IoT Hub
1616
// ----------------------------------------------------------------------------
17-
//#define ENABLE_LEGACY_MQTT
17+
// #define ENABLE_DPS
1818

1919
// ----------------------------------------------------------------------------
20-
// Azure IoT Hub config
20+
// Azure IoT DPS Self-Signed X509Certificate
21+
// Define this to connect to DPS or Iot Hub using a self-signed X509
22+
/// certificate
23+
// ----------------------------------------------------------------------------
24+
// #define ENABLE_X509
25+
26+
// ----------------------------------------------------------------------------
27+
// Azure IoT Device ID
28+
// Make sure this is the same as the Device ID on the corresponding IoT Hub
29+
// NOTE: To be used only when ENABLE_DPS is NOT defined
2130
// ----------------------------------------------------------------------------
22-
#define IOT_HUB_HOSTNAME ""
2331
#define IOT_DEVICE_ID ""
32+
33+
// ----------------------------------------------------------------------------
34+
// Azure IoT SAS Key
35+
// The SAS key generated by configuring an IoT Hub device or DPS individual
36+
// enrollment
37+
// NOTE: To be used only when ENABLE_X509 is not defined
38+
// ----------------------------------------------------------------------------
2439
#define IOT_PRIMARY_KEY ""
2540

41+
// ----------------------------------------------------------------------------
42+
// Azure IoT Hub Hostname
43+
// The Hostname found on your IoT Hub Overview page
44+
// NOTE: To be used only when ENABLE_DPS is not defined
45+
// ----------------------------------------------------------------------------
46+
#define IOT_HUB_HOSTNAME ""
47+
2648
// ----------------------------------------------------------------------------
2749
// Azure IoT DPS config
50+
// DPS connection information
51+
// IOT_DPS_ENDPOINT is always "global.azure-devices-provisioning.net"
52+
// IOT_DPS_ID_SCOPE can be found on your DPS Overview page
53+
// IOT_DPS_REGISTRATION ID is the title of your individual enrollment
54+
// containing your SAS key or X509 certificate
2855
// ----------------------------------------------------------------------------
2956
#define IOT_DPS_ENDPOINT "global.azure-devices-provisioning.net"
3057
#define IOT_DPS_ID_SCOPE ""
3158
#define IOT_DPS_REGISTRATION_ID ""
3259

33-
#endif // _AZURE_CONFIG_H
60+
#endif // _AZURE_CONFIG_H

0 commit comments

Comments
 (0)