Skip to content

Commit

Permalink
Better approach to custom client certificate
Browse files Browse the repository at this point in the history
Don't duplicate defined check. Let USE_MQTT_AWS_IOT automatically set USE_MQTT_CLIENT_CERT and use latter in all code
  • Loading branch information
GMagician committed Feb 11, 2025
1 parent d0788ef commit 10c7b30
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion BUILDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Note: the `minimal` variant is not listed as it shouldn't be used outside of the
| USE_HOME_ASSISTANT | - | - / - | - | - | - | - |
| USE_TASMOTA_DISCOVERY | x | x / x | x | x | x | x |
| USE_MQTT_TLS\* | - | - / x | - | - | - | - |
| USE_MQTT_CLIENT_CERT | - | - / - | - | - | - | - |
| USE_MQTT_AWS_IOT | - | - / - | - | - | - | - |
| USE_MQTT_CUSTOM_CERT | - | - / - | - | - | - | - |
| USE_4K_RSA | - | - / - | - | - | - | - |
| USE_TELEGRAM | - | - / - | - | - | - | - |
| USE_KNX | - | - / x | x | - | - | - |
Expand Down
2 changes: 1 addition & 1 deletion lib/lib_ssl/tls_mini/src/StackThunk_light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ uint32_t *stack_thunk_light_save = NULL; /* Saved A1 while in BearSSL */
uint32_t stack_thunk_light_refcnt = 0;

//#define _stackSize (5600/4)
#if defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_AWS_IOT_LIGHT) || defined(USE_MQTT_AZURE_IOT) || defined(USE_MQTT_CUSTOM_CERT)
#if defined(USE_MQTT_CLIENT_CERT) || defined(USE_MQTT_AWS_IOT_LIGHT) || defined(USE_MQTT_AZURE_IOT)
#define _stackSize (5300/4) // using a light version of bearssl we can save 300 bytes
#else
#define _stackSize (4800/4) // no private key, we can reduce a little, max observed 4300
Expand Down
8 changes: 4 additions & 4 deletions lib/lib_ssl/tls_mini/src/WiFiClientSecureLightBearSSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,16 +994,16 @@ bool WiFiClientSecure_light::_connectSSL(const char* hostName) {
br_ssl_engine_set_buffers_bidi(_eng, _iobuf_in.get(), _iobuf_in_size, _iobuf_out.get(), _iobuf_out_size);

// ============================================================
// allocate Private key if needed, only if USE_MQTT_AWS_IOT/USE_MQTT_CUSTOM_CERT
// allocate Private key if needed, only if USE_MQTT_CLIENT_CERT
LOG_HEAP_SIZE("_connectSSL before PrivKey allocation");
#if defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_CUSTOM_CERT)
#if defined(USE_MQTT_CLIENT_CERT)
// ============================================================
// Set the EC Private Key, only USE_MQTT_AWS_IOT/USE_MQTT_CUSTOM_CERT
// Set the EC Private Key, only USE_MQTT_CLIENT_CERT
// limited to P256 curve
br_ssl_client_set_single_ec(_sc.get(), _chain_P, 1,
_sk_ec_P, _allowed_usages,
_cert_issuer_key_type, &br_ec_p256_m15, br_ecdsa_sign_asn1_get_default());
#endif // USE_MQTT_AWS_IOT/USE_MQTT_CUSTOM_CERT
#endif // USE_MQTT_CLIENT_CERT

// ============================================================
// Start TLS connection, ALL
Expand Down
11 changes: 7 additions & 4 deletions tasmota/my_user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,10 @@
//#define USE_MQTT_TLS // Use TLS for MQTT connection (+34.5k code, +7.0k mem and +4.8k additional during connection handshake)
// #define USE_MQTT_TLS_CA_CERT // [DEPRECATED] Now TLS supports dual mode using SetOption132 - this flag is now ignored
// #define USE_MQTT_AWS_IOT_LIGHT // Enable MQTT for AWS IoT in light mode, with user/password instead of private certificate
// #define USE_MQTT_AWS_IOT // [Deprecated] Enable MQTT for AWS IoT - requires a private key (+11.9k code, +0.4k mem)
// #define USE_MQTT_CLIENT_CERT // Enable MQTT with custom client certificate - requires a private key (+11.9k code, +0.4k mem)
// #define USE_MQTT_AWS_IOT // [Deprecated] Enable MQTT for AWS IoT - it includes USE_MQTT_CLIENT_CERT but it forces no user account/password
// Note: you need to generate a private key + certificate per device and update 'tasmota/tasmota_aws_iot.cpp'
// Full documentation here: https://github.com/arendst/Tasmota/wiki/AWS-IoT
// #define USE_MQTT_CUSTOM_CERT // Enable MQTT with custom client certificate - requires a private key (+11.9k code, +0.4k mem)
// for USE_4K_RSA (support for 4096 bits certificates, instead of 2048), you need to uncommend `-DUSE_4K_RSA` in `build_flags` from `platform.ini` or `platform_override.ini`

// -- MQTT - TLS - Azure IoT & IoT Central ---------
Expand Down Expand Up @@ -1336,6 +1336,9 @@
#ifdef USE_CONFIG_OVERRIDE
#include "user_config_override.h" // Configuration overrides for my_user_config.h
#endif
#if defined(USE_MQTT_AWS_IOT) && !defined(USE_MQTT_CLIENT_CERT)
#define USE_MQTT_CLIENT_CERT // USE_MQTT_AWS_IOT requires USE_MQTT_CLIENT_CERT
#endif

/*********************************************************************************************\
* Post-process obsoletes
Expand All @@ -1349,8 +1352,8 @@
* Mutual exclude options
\*********************************************************************************************/

#if defined(ESP8266) && defined(USE_DISCOVERY) && (defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_AWS_IOT_LIGHT) || defined(USE_MQTT_CUSTOM_CERT))
#error "Select either USE_DISCOVERY or USE_MQTT_AWS_IOT/USE_MQTT_CUSTOM_CERT, mDNS takes too much code space and is not needed for AWS IoT"
#if defined(ESP8266) && defined(USE_DISCOVERY) && (defined(USE_MQTT_CLIENT_CERT) || defined(USE_MQTT_AWS_IOT_LIGHT))
#error "Select either USE_DISCOVERY or USE_MQTT_CLIENT_CERT/USE_MQTT_AWS_IOT, mDNS takes too much code space and is not needed for AWS IoT"
#endif

#if defined(USE_RULES) && defined(USE_SCRIPT)
Expand Down
6 changes: 3 additions & 3 deletions tasmota/tasmota_support/settings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,7 @@ void SettingsDelta(void) {
SettingsUpdateText(SET_STAPWD1, temp41);
SettingsUpdateText(SET_STAPWD2, temp42);

#if defined(USE_MQTT_TLS) && (defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_CUSTOM_CERT))
#if defined(USE_MQTT_TLS) && defined(USE_MQTT_CLIENT_CERT)
if (!strlen(Settings->ex_mqtt_user)) {
SettingsUpdateText(SET_MQTT_HOST, temp7);
SettingsUpdateText(SET_MQTT_USER, temp9);
Expand All @@ -1567,10 +1567,10 @@ void SettingsDelta(void) {
SettingsUpdateText(SET_MQTT_HOST, aws_mqtt_host);
SettingsUpdateText(SET_MQTT_USER, "");
}
#else // No USE_MQTT_TLS and USE_MQTT_AWS_IOT/USE_MQTT_CUSTOM_CERT
#else // No USE_MQTT_TLS and USE_MQTT_CLIENT_CERT
SettingsUpdateText(SET_MQTT_HOST, temp7);
SettingsUpdateText(SET_MQTT_USER, temp9);
#endif // USE_MQTT_TLS and USE_MQTT_AWS_IOT/USE_MQTT_CUSTOM_CERT
#endif // USE_MQTT_TLS and USE_MQTT_CLIENT_CERT
SettingsUpdateText(SET_MQTT_PWD, temp10);
SettingsUpdateText(SET_MQTT_TOPIC, temp11);
}
Expand Down
22 changes: 11 additions & 11 deletions tasmota/tasmota_xdrv_driver/xdrv_02_9_mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const char kMqttCommands[] PROGMEM = "|" // No prefix
D_CMND_MQTTFINGERPRINT "|"
#endif
D_CMND_MQTTUSER "|" D_CMND_MQTTPASSWORD "|" D_CMND_MQTTKEEPALIVE "|" D_CMND_MQTTTIMEOUT "|" D_CMND_MQTTWIFITIMEOUT "|"
#if defined(USE_MQTT_TLS) && (defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_CUSTOM_CERT))
#if defined(USE_MQTT_TLS) && defined(USE_MQTT_CLIENT_CERT)
D_CMND_TLSKEY "|"
#endif
#ifdef USE_MQTT_FILE
Expand All @@ -84,7 +84,7 @@ void (* const MqttCommand[])(void) PROGMEM = {
&CmndMqttFingerprint,
#endif
&CmndMqttUser, &CmndMqttPassword, &CmndMqttKeepAlive, &CmndMqttTimeout, &CmndMqttWifiTimeout,
#if defined(USE_MQTT_TLS) && (defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_CUSTOM_CERT))
#if defined(USE_MQTT_TLS) && defined(USE_MQTT_CLIENT_CERT)
&CmndTlsKey,
#endif
#ifdef USE_MQTT_FILE
Expand All @@ -111,7 +111,7 @@ struct MQTT {
#ifdef USE_MQTT_TLS

// This part of code is necessary to store Private Key and Cert in Flash
#if defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_CUSTOM_CERT)
#if defined(USE_MQTT_CLIENT_CERT)
#include <base64.hpp>

const br_ec_private_key *AWS_IoT_Private_Key = nullptr;
Expand All @@ -134,7 +134,7 @@ public:

tls_dir_t tls_dir; // memory copy of tls_dir from flash

#endif // USE_MQTT_AWS_IOT/USE_MQTT_CUSTOM_CERT
#endif // USE_MQTT_CLIENT_CERT

// check whether the fingerprint is filled with a single value
// Filled with 0x00 = accept any fingerprint and learn it for next time
Expand Down Expand Up @@ -250,7 +250,7 @@ void MqttInit(void) {
static const char * alpn_mqtt = "mqtt"; // needs to be static
tlsClient->setALPN(&alpn_mqtt, 1); // need to set alpn to 'mqtt' for AWS IoT
}
#if defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_CUSTOM_CERT)
#if defined(USE_MQTT_CLIENT_CERT)
loadTlsDir(); // load key and certificate data from Flash
if ((nullptr != AWS_IoT_Private_Key) && (nullptr != AWS_IoT_Client_Certificate)) {
tlsClient->setClientECCert(AWS_IoT_Client_Certificate,
Expand Down Expand Up @@ -818,7 +818,7 @@ void MqttPublishPayloadPrefixTopic_P(uint32_t prefix, const char* subtopic, cons
free(romram); // Free 16k heap from 64 bytes
MqttPublishPayload(stopic, payload, binary_length, retained);

#if defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_AWS_IOT_LIGHT) || defined(USE_MQTT_CUSTOM_CERT)
#if defined(USE_MQTT_CLIENT_CERT) || defined(USE_MQTT_AWS_IOT_LIGHT)
if ((prefix > 0) && (Settings->flag4.awsiot_shadow) && (Mqtt.connected)) { // placeholder for SetOptionXX
// compute the target topic
char *topic = SettingsText(SET_MQTT_TOPIC);
Expand All @@ -844,7 +844,7 @@ void MqttPublishPayloadPrefixTopic_P(uint32_t prefix, const char* subtopic, cons
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_MQTT "Updated shadow: %s"), romram);
yield(); // #3313
}
#endif // USE_MQTT_AWS_IOT/USE_MQTT_CUSTOM_CERT
#endif // USE_MQTT_CLIENT_CERT
}

void MqttPublishPayloadPrefixTopic_P(uint32_t prefix, const char* subtopic, const char* payload, uint32_t binary_length) {
Expand Down Expand Up @@ -1120,7 +1120,7 @@ void MqttReconnect(void) {
if (!strlen(SettingsText(SET_MQTT_HOST)) || !Settings->mqtt_port) {
Mqtt.allowed = false;
}
#if defined(USE_MQTT_TLS) && (defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_CUSTOM_CERT))
#if defined(USE_MQTT_TLS) && defined(USE_MQTT_CLIENT_CERT)
// don't enable MQTT for AWS IoT if Private Key or Certificate are not set
if (Mqtt.mqtt_tls) {
if (0 == strlen(SettingsText(SET_MQTT_PWD))) { // we anticipate that an empty password does not make sense with TLS. This avoids failed connections
Expand Down Expand Up @@ -1189,7 +1189,7 @@ void MqttReconnect(void) {
MqttClient.setClient(EspClient);
MqttNonTLSWarning();
}
#if defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_CUSTOM_CERT)
#if defined(USE_MQTT_CLIENT_CERT)
// re-assign private key in case it was updated in between
if (Mqtt.mqtt_tls) {
if ((nullptr != AWS_IoT_Private_Key) && (nullptr != AWS_IoT_Client_Certificate)) {
Expand All @@ -1203,7 +1203,7 @@ void MqttReconnect(void) {
0xFFFF /* all usages, don't care */, 0);
}
}
#endif // USE_MQTT_AWS_IOT/USE_MQTT_CUSTOM_CERT
#endif // USE_MQTT_CLIENT_CERT
#ifdef USE_MQTT_AZURE_IOT
String azureMqtt_password = SettingsText(SET_MQTT_PWD);
if (azureMqtt_password.indexOf("SharedAccessSignature") == -1) {
Expand Down Expand Up @@ -1803,7 +1803,7 @@ void CmndStatusRetain(void) {
/*********************************************************************************************\
* TLS private key and certificate - store into Flash
\*********************************************************************************************/
#if defined(USE_MQTT_TLS) && (defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_CUSTOM_CERT))
#if defined(USE_MQTT_TLS) && defined(USE_MQTT_CLIENT_CERT)

#ifdef ESP32
static uint8_t * tls_spi_start = nullptr;
Expand Down

0 comments on commit 10c7b30

Please sign in to comment.