|
36 | 36 | #define MQTT_TIMEOUT (10 * TX_TIMER_TICKS_PER_SECOND) |
37 | 37 | #define MQTT_KEEP_ALIVE 240 |
38 | 38 |
|
| 39 | +CHAR* azure_iot_x509_hostname; |
| 40 | + |
| 41 | +static ULONG azure_iot_certificate_verify(NX_SECURE_TLS_SESSION* session, NX_SECURE_X509_CERT* certificate) |
| 42 | +{ |
| 43 | + UINT status; |
| 44 | + |
| 45 | + // Check certicate matches the correct address |
| 46 | + status = nx_secure_x509_common_name_dns_check( |
| 47 | + certificate, (UCHAR*)azure_iot_x509_hostname, strlen(azure_iot_x509_hostname)); |
| 48 | + if (status) |
| 49 | + { |
| 50 | + printf("Error in certificate verification: DNS name did not match CN\r\n"); |
| 51 | + } |
| 52 | + |
| 53 | + return status; |
| 54 | +} |
| 55 | + |
39 | 56 | UINT azure_iot_mqtt_register_direct_method_callback( |
40 | 57 | AZURE_IOT_MQTT* azure_iot_mqtt, func_ptr_direct_method mqtt_direct_method_callback) |
41 | 58 | { |
@@ -107,26 +124,6 @@ UINT tls_setup(NXD_MQTT_CLIENT* client, |
107 | 124 | return status; |
108 | 125 | } |
109 | 126 |
|
110 | | - status = nx_secure_tls_remote_certificate_allocate(tls_session, |
111 | | - &azure_iot_mqtt->mqtt_remote_certificate, |
112 | | - azure_iot_mqtt->mqtt_remote_cert_buffer, |
113 | | - sizeof(azure_iot_mqtt->mqtt_remote_cert_buffer)); |
114 | | - if (status != NX_SUCCESS) |
115 | | - { |
116 | | - printf("Failed to create remote certificate buffer (0x%04x)\r\n", status); |
117 | | - return status; |
118 | | - } |
119 | | - |
120 | | - status = nx_secure_tls_remote_certificate_allocate(tls_session, |
121 | | - &azure_iot_mqtt->mqtt_remote_issuer, |
122 | | - azure_iot_mqtt->mqtt_remote_issuer_buffer, |
123 | | - sizeof(azure_iot_mqtt->mqtt_remote_issuer_buffer)); |
124 | | - if (status != NX_SUCCESS) |
125 | | - { |
126 | | - printf("Failed to create remote issuer buffer (0x%04x)\r\n", status); |
127 | | - return status; |
128 | | - } |
129 | | - |
130 | 127 | // Add a CA Certificate to our trusted store for verifying incoming server certificates |
131 | 128 | status = nx_secure_x509_certificate_initialize(trusted_cert, |
132 | 129 | (UCHAR*)azure_iot_root_ca, |
@@ -157,6 +154,15 @@ UINT tls_setup(NXD_MQTT_CLIENT* client, |
157 | 154 | return status; |
158 | 155 | } |
159 | 156 |
|
| 157 | + // Setup the callback invoked when TLS has a certificate it wants to verify so we can |
| 158 | + // do additional checks not done automatically by TLS. |
| 159 | + status = nx_secure_tls_session_certificate_callback_set(tls_session, azure_iot_certificate_verify); |
| 160 | + if (status) |
| 161 | + { |
| 162 | + printf("Failed to set the session certificate callback: status: %d", status); |
| 163 | + return status; |
| 164 | + } |
| 165 | + |
160 | 166 | // Add a timestamp function for time checking and timestamps in the TLS handshake |
161 | 167 | nx_secure_tls_session_time_function_set(tls_session, azure_iot_mqtt->unix_time_get); |
162 | 168 |
|
@@ -717,6 +723,9 @@ UINT azure_iot_mqtt_connect(AZURE_IOT_MQTT* azure_iot_mqtt) |
717 | 723 | return status; |
718 | 724 | } |
719 | 725 |
|
| 726 | + // Stash the hostname in a global variable so we can verify the cert at connect |
| 727 | + azure_iot_x509_hostname = azure_iot_mqtt->mqtt_hub_hostname; |
| 728 | + |
720 | 729 | status = nxd_mqtt_client_secure_connect(&azure_iot_mqtt->nxd_mqtt_client, |
721 | 730 | &server_ip, |
722 | 731 | NXD_MQTT_TLS_PORT, |
|
0 commit comments