Skip to content

Commit 165b5bb

Browse files
committed
Add support for MQTT certificates
Fixes shmuelzon#35
1 parent 9678de0 commit 165b5bb

File tree

6 files changed

+98
-32
lines changed

6 files changed

+98
-32
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ The `mqtt` section below includes the following entries:
108108
"host": "192.168.1.1",
109109
"port": 1883,
110110
"ssl": false,
111+
"client_cert": null,
112+
"client_key": null,
113+
"server_cert": null,
111114
"username": null,
112115
"password": null,
113116
"client_id": null
@@ -125,6 +128,16 @@ The `mqtt` section below includes the following entries:
125128
}
126129
```
127130
* `server` - MQTT connection parameters
131+
* `host` - Host name or IP address of the MQTT broker
132+
* `port` - TCP port of the MQTT broker. If not specificed will default to
133+
1883 or 8883, depending on SSL configuration
134+
* `client_cert`, `client_key`, `server_cert` - Full path names, including a
135+
leading slash (/), of the certificate/key file (in PEM format) stored under
136+
the data folder. For example, if a certificate file is placed at
137+
`data/certs/my_cert.pem`, the value stored in the configuration should be
138+
`/certs/my_cert.pem`
139+
* `username`, `password` - MQTT login credentials
140+
* `client_id` - The MQTT client ID
128141
* `publish` - Configuration for publishing topics
129142
* `topics`
130143
* `prefix` - Which prefix should be added to all MQTT value topics. OTA

main/ble2mqtt.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ static void wifi_on_connected(void)
163163
ESP_LOGI(TAG, "Connected to WiFi, connecting to MQTT");
164164
mqtt_connect(config_mqtt_host_get(), config_mqtt_port_get(),
165165
config_mqtt_client_id_get(), config_mqtt_username_get(),
166-
config_mqtt_password_get(), config_mqtt_ssl_get());
166+
config_mqtt_password_get(), config_mqtt_ssl_get(),
167+
config_mqtt_server_cert_get(), config_mqtt_client_cert_get(),
168+
config_mqtt_client_key_get());
167169
}
168170

169171
static void wifi_on_disconnected(void)

main/config.c

Lines changed: 72 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,36 @@ static cJSON *config;
1818
/* Internal variables */
1919
static char config_version[33];
2020

21+
/* Common utilities */
22+
static char *read_file(const char *path)
23+
{
24+
int fd, len;
25+
struct stat st;
26+
char *buf, *p;
27+
28+
if (stat(path, &st))
29+
return NULL;
30+
31+
if ((fd = open(path, O_RDONLY)) < 0)
32+
return NULL;
33+
34+
if (!(buf = p = malloc(st.st_size + 1)))
35+
return NULL;
36+
37+
while ((len = read(fd, p, 1024)) > 0)
38+
p += len;
39+
close(fd);
40+
41+
if (len < 0)
42+
{
43+
free(buf);
44+
return NULL;
45+
}
46+
47+
*p = '\0';
48+
return buf;
49+
}
50+
2151
/* BLE Configuration*/
2252
static cJSON *config_ble_get_name_by_uuid(uint8_t is_service,
2353
const char *uuid, const char *field_name)
@@ -202,6 +232,48 @@ uint8_t config_mqtt_ssl_get(void)
202232
return cJSON_IsTrue(ssl);
203233
}
204234

235+
const char *config_mqtt_file_get(const char *field)
236+
{
237+
const char *file = config_mqtt_server_get(field);
238+
char buf[128];
239+
240+
if (!file)
241+
return NULL;
242+
243+
snprintf(buf, sizeof(buf), "/spiffs%s", file);
244+
return read_file(buf);
245+
}
246+
247+
const char *config_mqtt_server_cert_get(void)
248+
{
249+
static const char *cert;
250+
251+
if (!cert)
252+
cert = config_mqtt_file_get("server_cert");
253+
254+
return cert;
255+
}
256+
257+
const char *config_mqtt_client_cert_get(void)
258+
{
259+
static const char *cert;
260+
261+
if (!cert)
262+
cert = config_mqtt_file_get("client_cert");
263+
264+
return cert;
265+
}
266+
267+
const char *config_mqtt_client_key_get(void)
268+
{
269+
static const char *key;
270+
271+
if (!key)
272+
key = config_mqtt_file_get("client_key");
273+
274+
return key;
275+
}
276+
205277
const char *config_mqtt_client_id_get(void)
206278
{
207279
return config_mqtt_server_get("client_id");
@@ -351,35 +423,6 @@ int config_update_end(config_update_handle_t handle)
351423
return 0;
352424
}
353425

354-
static char *read_file(const char *path)
355-
{
356-
int fd, len;
357-
struct stat st;
358-
char *buf, *p;
359-
360-
if (stat(path, &st))
361-
return NULL;
362-
363-
if ((fd = open(path, O_RDONLY)) < 0)
364-
return NULL;
365-
366-
if (!(buf = p = malloc(st.st_size + 1)))
367-
return NULL;
368-
369-
while ((len = read(fd, p, 1024)) > 0)
370-
p += len;
371-
close(fd);
372-
373-
if (len < 0)
374-
{
375-
free(buf);
376-
return NULL;
377-
}
378-
379-
*p = '\0';
380-
return buf;
381-
}
382-
383426
static cJSON *load_json(const char *path)
384427
{
385428
char *p, *str = read_file(path);

main/config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ uint32_t config_ble_passkey_get(const char *mac);
2020
const char *config_mqtt_host_get(void);
2121
uint16_t config_mqtt_port_get(void);
2222
uint8_t config_mqtt_ssl_get(void);
23+
const char *config_mqtt_server_cert_get(void);
24+
const char *config_mqtt_client_cert_get(void);
25+
const char *config_mqtt_client_key_get(void);
2326
const char *config_mqtt_client_id_get(void);
2427
const char *config_mqtt_username_get(void);
2528
const char *config_mqtt_password_get(void);

main/mqtt.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ static esp_err_t mqtt_event_cb(esp_mqtt_event_handle_t event)
251251
}
252252

253253
int mqtt_connect(const char *host, uint16_t port, const char *client_id,
254-
const char *username, const char *password, uint8_t ssl)
254+
const char *username, const char *password, uint8_t ssl,
255+
const char *server_cert, const char *client_cert, const char *client_key)
255256
{
256257
esp_mqtt_client_config_t config = {
257258
.event_handle = mqtt_event_cb,
@@ -261,6 +262,9 @@ int mqtt_connect(const char *host, uint16_t port, const char *client_id,
261262
.username = username,
262263
.password = password,
263264
.transport = ssl ? MQTT_TRANSPORT_OVER_SSL : MQTT_TRANSPORT_OVER_TCP,
265+
.cert_pem = server_cert,
266+
.client_cert_pem = client_cert,
267+
.client_key_pem = client_key,
264268
};
265269

266270
ESP_LOGI(TAG, "Connecting MQTT client");

main/mqtt.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ int mqtt_publish(const char *topic, uint8_t *payload, size_t len, int qos,
2323
uint8_t retained);
2424

2525
int mqtt_connect(const char *host, uint16_t port, const char *client_id,
26-
const char *username, const char *password, uint8_t ssl);
26+
const char *username, const char *password, uint8_t ssl,
27+
const char *server_cert, const char *client_cert, const char *client_key);
2728
int mqtt_disconnect(void);
2829

2930
uint8_t mqtt_is_connected(void);

0 commit comments

Comments
 (0)