Skip to content

Commit 016ca89

Browse files
Add BearSSL::setFingerprint(char* fp)
Add a method allowing a user to send in a character string for the fingerprint, like axTLS supported. Implements part of PR esp8266#4833 from @d-a-v with changes requested in discussion.
1 parent 678c7be commit 016ca89

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

libraries/ESP8266WiFi/examples/BearSSL_Validation/BearSSL_Validation.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ instead of the while certificate. This is not nearly as secure as real
102102
X.509 validation, but is better than nothing.
103103
)EOF");
104104
BearSSL::WiFiClientSecure client;
105-
const uint8_t fp[20] = {0x5F, 0xF1, 0x60, 0x31, 0x09, 0x04, 0x3E, 0xF2, 0x90, 0xD2, 0xB0, 0x8A, 0x50, 0x38, 0x04, 0xE8, 0x37, 0x9F, 0xBC, 0x76};
105+
static const char fp[] PROGMEM = "5F:F1:60:31:09:04:3E:F2:90:D2:B0:8A:50:38:04:E8:37:9F:BC:76";
106106
client.setFingerprint(fp);
107107
fetchURL(&client, host, port, path);
108108
}

libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,42 @@ bool WiFiClientSecure::_wait_for_handshake() {
536536
return _handshake_done;
537537
}
538538

539+
static uint8_t htoi (unsigned char c)
540+
{
541+
if (c>='0' && c <='9') return c - '0';
542+
else if (c>='A' && c<='F') return 10 + c - 'A';
543+
else if (c>='a' && c<='f') return 10 + c - 'a';
544+
else return 255;
545+
}
546+
547+
// Set a fingerprint by parsing an ASCII string
548+
bool WiFiClientSecure::setFingerprint(const char *fpStr) {
549+
int idx = 0;
550+
uint8_t c, d;
551+
uint8_t fp[20];
552+
553+
while (idx < 20) {
554+
c = pgm_read_byte(fpStr++);
555+
if (!c) break; // String ended, done processing
556+
d = pgm_read_byte(fpStr++);
557+
if (!d) return false; // Only half of the last hex digit, error
558+
c = htoi(c);
559+
d = htoi(d);
560+
if ((c>15) || (d>15)) {
561+
return false; // Error in one of the hex characters
562+
}
563+
fp[idx++] = (c<<4)|d;
564+
565+
// Skip 0 or more spaces or colons
566+
while ( pgm_read_byte(fpStr) && (pgm_read_byte(fpStr)==' ' || pgm_read_byte(fpStr)==':') ) {
567+
fpStr++;
568+
}
569+
}
570+
if ((idx != 20) || pgm_read_byte(_fingerprint)) {
571+
return false; // Garbage at EOL or we didn't have enough hex digits
572+
}
573+
return setFingerprint(fp);
574+
}
539575

540576
extern "C" {
541577

libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ class WiFiClientSecure : public WiFiClient {
7373
_knownkey_usages = usages;
7474
}
7575
// Only check SHA1 fingerprint of certificate
76-
void setFingerprint(const uint8_t fingerprint[20]) {
76+
bool setFingerprint(const uint8_t fingerprint[20]) {
7777
_clearAuthenticationSettings();
7878
_use_fingerprint = true;
7979
memcpy_P(_fingerprint, fingerprint, 20);
80+
return true;
8081
}
82+
bool setFingerprint(const char *fpStr);
8183
// Accept any certificate that's self-signed
8284
void allowSelfSignedCerts() {
8385
_clearAuthenticationSettings();

0 commit comments

Comments
 (0)