Skip to content

Commit 6438a53

Browse files
committed
Add errors for setPasswordHash
1 parent e191087 commit 6438a53

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

cores/esp32/HEXBuilder.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
#include "HEXBuilder.h"
21+
#include "WCharacter.h"
2122

2223
static uint8_t hex_char_to_byte(uint8_t c) {
2324
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa))
@@ -26,6 +27,19 @@ static uint8_t hex_char_to_byte(uint8_t c) {
2627
: 0x10; // unknown char is 16
2728
}
2829

30+
bool HEXBuilder::isHexString(const char *str, size_t len) {
31+
for (size_t i = 0; i < len; i++) {
32+
if (!isHexadecimalDigit(str[i])) {
33+
return false;
34+
}
35+
}
36+
return true;
37+
}
38+
39+
bool HEXBuilder::isHexString(String str) {
40+
return isHexString(str.c_str(), str.length());
41+
}
42+
2943
size_t HEXBuilder::hex2bytes(unsigned char *out, size_t maxlen, String &in) {
3044
return hex2bytes(out, maxlen, in.c_str());
3145
}

cores/esp32/HEXBuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@ class HEXBuilder {
3232

3333
static String bytes2hex(const unsigned char *in, size_t len);
3434
static size_t bytes2hex(char *out, size_t maxlen, const unsigned char *in, size_t len);
35+
36+
static bool isHexString(const char *str, size_t len);
37+
static bool isHexString(String str);
3538
};
3639
#endif

libraries/ArduinoOTA/src/ArduinoOTA.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "ArduinoOTA.h"
2020
#include "NetworkClient.h"
2121
#include "ESPmDNS.h"
22+
#include "HEXBuilder.h"
2223
#include "SHA2Builder.h"
2324
#include "PBKDF2_HMACBuilder.h"
2425
#include "Update.h"
@@ -86,30 +87,29 @@ ArduinoOTAClass &ArduinoOTAClass::setPassword(const char *password) {
8687

8788
ArduinoOTAClass &ArduinoOTAClass::setPasswordHash(const char *password) {
8889
if (_state == OTA_IDLE && password) {
89-
// Store the pre-hashed password directly
90-
_password.clear();
91-
_password = password;
92-
9390
size_t len = strlen(password);
91+
bool is_hex = HEXBuilder::isHexString(password, len);
9492

95-
if (len == 32) {
96-
// Check if it's a valid hex string (all chars are 0-9, a-f, A-F)
97-
bool is_hex = true;
98-
for (size_t i = 0; i < len; i++) {
99-
char c = password[i];
100-
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
101-
is_hex = false;
102-
break;
103-
}
104-
}
93+
if (!is_hex) {
94+
log_e("Invalid password hash. Expected hex string (0-9, a-f, A-F).");
95+
return *this;
96+
}
10597

98+
if (len == 32) {
10699
// Warn if MD5 hash is detected (32 hex characters)
107-
if (is_hex) {
108-
log_w("MD5 password hash detected. MD5 is deprecated and insecure.");
109-
log_w("Please use setPassword() with plain text or setPasswordHash() with SHA256 hash (64 chars).");
110-
log_w("To generate SHA256: echo -n 'yourpassword' | sha256sum");
111-
}
100+
log_w("MD5 password hash detected. MD5 is deprecated and insecure.");
101+
log_w("Please use setPassword() with plain text or setPasswordHash() with SHA256 hash (64 chars).");
102+
log_w("To generate SHA256: echo -n 'yourpassword' | sha256sum");
103+
} else if (len == 64) {
104+
log_i("Using SHA256 password hash.");
105+
} else {
106+
log_e("Invalid password hash length. Expected 32 (deprecated MD5) or 64 (SHA256) characters.");
107+
return *this;
112108
}
109+
110+
// Store the pre-hashed password directly
111+
_password.clear();
112+
_password = password;
113113
}
114114
return *this;
115115
}

0 commit comments

Comments
 (0)