Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: Ability to use a custom CA certificate for MQTT broker #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Useful Methods:

* void OTA_setHostname(char* hostname); //give a hostname to the device for OTA identification

* void useCustomCaCert(const char* cert); //path to a DER certificate stored on the SPIFFS filesystem

ToDo:
-----

Expand Down
25 changes: 24 additions & 1 deletion src/ESPHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,18 @@ bool ESPHelper::begin(){
//as long as an mqtt ip has been set create an instance of PubSub for client
if(_mqttSet){
//make mqtt client use either the secure or non-secure wifi client depending on the setting
if(_useSecureClient){client = PubSubClient(_currentNet.mqttHost, _currentNet.mqttPort, wifiClientSecure);}
if(_useSecureClient) {
client = PubSubClient(_currentNet.mqttHost, _currentNet.mqttPort, wifiClientSecure);
if (_caFile) {
if(wifiClientSecure.loadCACert(_caFile)) {
debugPrintln("Loaded Cert into client");
} else {
debugPrintln("Unable to load Cert into client");
}
} else {
debugPrintln("No CA file");
}
}
else{client = PubSubClient(_currentNet.mqttHost, _currentNet.mqttPort, wifiClient);}

//set the mqtt message callback if needed
Expand Down Expand Up @@ -344,6 +355,18 @@ void ESPHelper::useSecureClient(const char* fingerprint){
_useSecureClient = true;
}

void ESPHelper::useCustomCaCert(const char* cert){
SPIFFS.begin();
_caFile = SPIFFS.open(cert, "r");
if(!_caFile) {
debugPrintln("Couldn't load Cert");
return;
} else {
debugPrint("Loaded Cert");
debugPrintln(cert);
}
}

//enables and sets up broadcast mode rather than station mode. This allows users to create a network from the ESP
//and upload using OTA even if there is no network already present. This disables all MQTT connections
void ESPHelper::broadcastMode(const char* ssid, const char* password, const IPAddress ip){
Expand Down
4 changes: 4 additions & 0 deletions src/ESPHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
#include "FS.h"



Expand Down Expand Up @@ -285,6 +286,8 @@ class ESPHelper{

void useSecureClient(const char* fingerprint);

void useCustomCaCert(const char* cert);

void broadcastMode(const char* ssid, const char* password, const IPAddress ip);
void disableBroadcast();

Expand Down Expand Up @@ -374,6 +377,7 @@ class ESPHelper{
WiFiClientSecure wifiClientSecure;
const char* _fingerprint;
bool _useSecureClient = false;
File _caFile;


String _clientName;
Expand Down