Store keys in WifiDevice object
This commit is contained in:
@@ -6,22 +6,28 @@
|
|||||||
WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences)
|
WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences)
|
||||||
: NetworkDevice(hostname)
|
: NetworkDevice(hostname)
|
||||||
{
|
{
|
||||||
String MQTT_CA = _preferences->getString(preference_mqtt_ca);
|
size_t caLength = _preferences->getString(preference_mqtt_ca,_ca,TLS_CA_MAX_SIZE);
|
||||||
String MQTT_CRT = _preferences->getString(preference_mqtt_crt);
|
size_t crtLength = _preferences->getString(preference_mqtt_crt,_cert,TLS_CERT_MAX_SIZE);
|
||||||
String MQTT_KEY = _preferences->getString(preference_mqtt_key);
|
size_t keyLength = _preferences->getString(preference_mqtt_key,_key,TLS_KEY_MAX_SIZE);
|
||||||
|
|
||||||
if(MQTT_CA.length() > 0)
|
if(caLength > 1) // length is 1 when empty
|
||||||
{
|
{
|
||||||
|
Serial.println(F("MQTT over TLS."));
|
||||||
|
Serial.print(_ca);
|
||||||
_wifiClientSecure = new WiFiClientSecure();
|
_wifiClientSecure = new WiFiClientSecure();
|
||||||
_wifiClientSecure->setCACert(MQTT_CA.c_str());
|
_wifiClientSecure->setCACert(_ca);
|
||||||
if(MQTT_CRT.length() > 0 && MQTT_KEY.length() > 0)
|
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
||||||
{
|
{
|
||||||
_wifiClientSecure->setCertificate(MQTT_CRT.c_str());
|
Serial.println(F("MQTT with client certificate."));
|
||||||
_wifiClientSecure->setPrivateKey(MQTT_KEY.c_str());
|
Serial.print(_cert);
|
||||||
|
Serial.print(_key);
|
||||||
|
_wifiClientSecure->setCertificate(_cert);
|
||||||
|
_wifiClientSecure->setPrivateKey(_key);
|
||||||
}
|
}
|
||||||
_mqttClient = new PubSubClient(*_wifiClientSecure);
|
_mqttClient = new PubSubClient(*_wifiClientSecure);
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
|
Serial.println(F("MQTT without TLS."));
|
||||||
_wifiClient = new WiFiClient();
|
_wifiClient = new WiFiClient();
|
||||||
_mqttClient = new PubSubClient(*_wifiClient);
|
_mqttClient = new PubSubClient(*_wifiClient);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,10 @@
|
|||||||
#include "NetworkDevice.h"
|
#include "NetworkDevice.h"
|
||||||
#include "../SpiffsCookie.h"
|
#include "../SpiffsCookie.h"
|
||||||
|
|
||||||
|
#define TLS_CA_MAX_SIZE 1800
|
||||||
|
#define TLS_CERT_MAX_SIZE 1800
|
||||||
|
#define TLS_KEY_MAX_SIZE 1800
|
||||||
|
|
||||||
class WifiDevice : public NetworkDevice
|
class WifiDevice : public NetworkDevice
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -26,4 +30,7 @@ private:
|
|||||||
WiFiClientSecure* _wifiClientSecure = nullptr;
|
WiFiClientSecure* _wifiClientSecure = nullptr;
|
||||||
PubSubClient* _mqttClient = nullptr;
|
PubSubClient* _mqttClient = nullptr;
|
||||||
SpiffsCookie _cookie;
|
SpiffsCookie _cookie;
|
||||||
|
char _ca[TLS_CA_MAX_SIZE];
|
||||||
|
char _cert[TLS_CERT_MAX_SIZE];
|
||||||
|
char _key[TLS_KEY_MAX_SIZE];
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user