1 /* MQTT Mutual Authentication Example
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <stddef.h>
12 #include <string.h>
13 #include "esp_wifi.h"
14 #include "esp_system.h"
15 #include "nvs_flash.h"
16 #include "esp_event.h"
17 #include "esp_netif.h"
18 #include "protocol_examples_common.h"
19
20 #include "freertos/FreeRTOS.h"
21 #include "freertos/task.h"
22 #include "freertos/semphr.h"
23 #include "freertos/queue.h"
24
25 #include "lwip/sockets.h"
26 #include "lwip/dns.h"
27 #include "lwip/netdb.h"
28
29 #include "esp_log.h"
30 #include "mqtt_client.h"
31 #include "rsa_sign_alt.h"
32
33 /* pre_prov - name of partition containing encrypted prv key parameters ( It is set as such to synchronize it with the pre provisioning service */
34 #define NVS_PARTITION_NAME "pre_prov"
35 /* esp_ds_ns - namespace used for defining values in esp_ds_nvs */
36 #define NVS_NAMESPACE "esp_ds_ns"
37 /* esp_ds_key_id - efuse key block id where 256 bit key is stored, which will be read by
38 * DS module to perform DS operation */
39 #define NVS_EFUSE_KEY_ID "esp_ds_key_id"
40 /* esp_ds_rsa_len - length of RSA private key (in bits) which is encrypted */
41 #define NVS_RSA_LEN "esp_ds_rsa_len"
42 /* following entries denote key(ASCII string) for particular value in key-value pair of esp_ds_nvs (which are defined in esp_ds_ns) */
43 /* ciphertext_c - encrypted RSA private key, see ESP32-S2 Techincal Reference Manual for more details */
44 #define NVS_CIPHER_C "esp_ds_c"
45 /* initialization vector (iv) - 256 bit value used to encrypt RSA private key (to generate ciphertext_c) */
46 #define NVS_IV "esp_ds_iv"
47 static const char *TAG = "MQTTS_EXAMPLE";
48
49 extern const uint8_t client_cert_pem_start[] asm("_binary_client_crt_start");
50 extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end");
51 extern const uint8_t server_cert_pem_start[] asm("_binary_mosquitto_org_crt_start");
52 extern const uint8_t server_cert_pem_end[] asm("_binary_mosquitto_org_crt_end");
53
mqtt_event_handler(esp_mqtt_event_handle_t event)54 static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
55 {
56 esp_mqtt_client_handle_t client = event->client;
57 int msg_id;
58 // your_context_t *context = event->context;
59 switch (event->event_id) {
60 case MQTT_EVENT_CONNECTED:
61 ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
62 msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
63 ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
64
65 msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
66 ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
67
68 msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1");
69 ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id);
70 break;
71 case MQTT_EVENT_DISCONNECTED:
72 ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
73 break;
74
75 case MQTT_EVENT_SUBSCRIBED:
76 ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
77 msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0);
78 ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
79 break;
80 case MQTT_EVENT_UNSUBSCRIBED:
81 ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
82 break;
83 case MQTT_EVENT_PUBLISHED:
84 ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
85 break;
86 case MQTT_EVENT_DATA:
87 ESP_LOGI(TAG, "MQTT_EVENT_DATA");
88 printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
89 printf("DATA=%.*s\r\n", event->data_len, event->data);
90 break;
91 case MQTT_EVENT_ERROR:
92 ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
93 break;
94 default:
95 ESP_LOGI(TAG, "Other event id:%d", event->event_id);
96 break;
97 }
98 return ESP_OK;
99 }
100
esp_read_ds_data_from_nvs(void)101 void *esp_read_ds_data_from_nvs(void)
102 {
103 esp_ds_data_ctx_t *ds_data_ctx;
104 ds_data_ctx = (esp_ds_data_ctx_t *)malloc(sizeof(esp_ds_data_ctx_t));
105 if (ds_data_ctx == NULL) {
106 ESP_LOGE(TAG, "Error in allocating memory for esp_ds_data_context");
107 goto exit;
108 }
109
110 ds_data_ctx->esp_ds_data = (esp_ds_data_t *)calloc(1, sizeof(esp_ds_data_t));
111 if (ds_data_ctx->esp_ds_data == NULL) {
112 ESP_LOGE(TAG, "Could not allocate memory for DS data handle ");
113 goto exit;
114 }
115
116 nvs_handle_t esp_ds_nvs_handle;
117 esp_err_t esp_ret;
118 esp_ret = nvs_flash_init_partition(NVS_PARTITION_NAME);
119 if (esp_ret != ESP_OK) {
120 ESP_LOGE(TAG, "Error in esp_ds_nvs partition init,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret));
121 goto exit;
122 }
123
124 esp_ret = nvs_open_from_partition(NVS_PARTITION_NAME, NVS_NAMESPACE,
125 NVS_READONLY, &esp_ds_nvs_handle);
126 if (esp_ret != ESP_OK) {
127 ESP_LOGE(TAG, "Error in esp_ds_nvs partition open,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret));
128 goto exit;
129 }
130
131 esp_ret = nvs_get_u8(esp_ds_nvs_handle, NVS_EFUSE_KEY_ID, &ds_data_ctx->efuse_key_id);
132 if (esp_ret != ESP_OK) {
133 ESP_LOGE(TAG, "Error in efuse_key_id value from nvs,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret));
134 goto exit;
135 }
136
137 esp_ret = nvs_get_u16(esp_ds_nvs_handle, NVS_RSA_LEN, &ds_data_ctx->rsa_length_bits);
138 if (esp_ret != ESP_OK) {
139 ESP_LOGE(TAG, "Error in reading rsa key length value from nvs,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret));
140 goto exit;
141 }
142
143 size_t blob_length = ESP_DS_C_LEN;
144 esp_ret = nvs_get_blob(esp_ds_nvs_handle, NVS_CIPHER_C, (void *)(ds_data_ctx->esp_ds_data->c), &blob_length);
145 if ((esp_ret != ESP_OK) || (blob_length != ESP_DS_C_LEN)) {
146 ESP_LOGE(TAG, "Error in reading ciphertext_c value from nvs,bytes_read = %d,\nreturned %02x (%s)", blob_length, esp_ret, esp_err_to_name(esp_ret));
147 goto exit;
148 }
149
150 blob_length = ESP_DS_IV_LEN;
151 esp_ret = nvs_get_blob(esp_ds_nvs_handle, NVS_IV, (void *)(ds_data_ctx->esp_ds_data->iv), &blob_length);
152 if ((esp_ret != ESP_OK) || (blob_length != ESP_DS_IV_LEN)) {
153 ESP_LOGE(TAG, "Error in reading initialization vector value from nvs,bytes_read = %d,\nreturned %02x (%s)", blob_length, esp_ret, esp_err_to_name(esp_ret));
154 goto exit;
155 }
156
157 return (void *)ds_data_ctx;
158 exit:
159 if (ds_data_ctx != NULL) {
160 free(ds_data_ctx->esp_ds_data);
161 }
162 free(ds_data_ctx);
163 return NULL;
164 }
165
mqtt_app_start(void)166 static void mqtt_app_start(void)
167 {
168
169 /* The context is used by the DS peripheral, should not be freed */
170 void *ds_data = esp_read_ds_data_from_nvs();
171 if (ds_data == NULL) {
172 ESP_LOGE(TAG, "Error in reading DS data from NVS");
173 vTaskDelete(NULL);
174 }
175 const esp_mqtt_client_config_t mqtt_cfg = {
176 .uri = "mqtts://test.mosquitto.org:8884",
177 .event_handle = mqtt_event_handler,
178 .cert_pem = (const char *)server_cert_pem_start,
179 .client_cert_pem = (const char *)client_cert_pem_start,
180 .client_key_pem = NULL,
181 .ds_data = ds_data,
182 };
183
184 ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
185 esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
186 esp_mqtt_client_start(client);
187 }
188
app_main(void)189 void app_main(void)
190 {
191 ESP_LOGI(TAG, "[APP] Startup..");
192 ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
193 ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
194
195 esp_log_level_set("*", ESP_LOG_INFO);
196 esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
197 esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE);
198 esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);
199 esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE);
200
201 ESP_ERROR_CHECK(nvs_flash_init());
202 ESP_ERROR_CHECK(esp_netif_init());
203 ESP_ERROR_CHECK(esp_event_loop_create_default());
204
205 /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
206 * Read "Establishing Wi-Fi or Ethernet Connection" section in
207 * examples/protocols/README.md for more information about this function.
208 */
209 ESP_ERROR_CHECK(example_connect());
210
211 mqtt_app_start();
212 }
213