Skip to content

MQTT Connection with BearSSL and TLS 1.2 results in exception 28 #6232

Closed
@vishalkothari

Description

@vishalkothari

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

  • Hardware: ESP-12
  • Core Version: 2.5.2
  • Development Env: Arduino IDE
  • Operating System: Windows

Settings in IDE

  • Module: Nodemcu 1.0
  • Flash Size: 4MB
  • lwip Variant: v2 Lower Memory
  • CPU Frequency: 80Mhz
  • Upload Speed: 115200

Problem Description

I am trying to connect to MQTT over TLS 1.2 using BearSSL. I am getting exception 28 when trying to connect.
I tried using web sockets API instead MQTT and it seems to work after some retries with exception 28.

I looked at #5347, and my rootCert seems to be ok as Websocket connection works.
I also looked at #4134, but latest version of umm_malloc doesnt seem to be compatible with ESP core 2.5.2

MCVE Sketch

#include <ESP8266HTTPClient.h>
#include <time.h>
#include <sys/time.h>                   // struct timeval
#include <coredecls.h>
#include <PubSubClient.h> //from https://github.com/Imroy/pubsubclient

#define TZ              5.5       // (utc+) TZ in hours
#define DST_MN          0      // use 60mn for summer time in some countries
#define TZ_MN           ((TZ)*60)
#define TZ_SEC          ((TZ)*3600)
#define DST_SEC         ((DST_MN)*60)

const char *ssid = "<ssid>";
const char *pass = "<password>";

const char *   host = "<broker>.amazonaws.com";
const uint16_t  port = 8883;

BearSSL::WiFiClientSecure wifiClient;
PubSubClient pubsubclient(wifiClient, host, port); 

// Set time via HTTP GET service, as required for x.509 validation
time_t setClockHttp() {
  HTTPClient http;
  time_t now;
  //.....
  //Get epoch from http service
  //....

  timeval tv = { now, 0 };
  timezone tz = { TZ_MN + DST_MN, 0 };

  settimeofday(&tv, &tz);
  gettimeofday(&cbtime, NULL);
  now = cbtime.tv_sec;
  return now;
}

void fetchCertAuthority() {

static const char ca_cert[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCB
yjELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQL
ExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJp
....
4fQRbxC1lfznQgUy286dUV4otp6F01vvpX1FQHKOtw5rDgb7MzVIcbidJ4vEZV8N
hnacRHr2lVz2XTIIM6RUthg/aFzyQkqFOFSDX9HoLPKsEdao7WNq
-----END CERTIFICATE-----
)EOF";

static const char client_cert[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDWjCCAkKgAwIBAgIVAOFlUZIDMrUht5zbLrjKog1uWrUKMA0GCSqGSIb3DQEB
CwUAME0xSzBJBgNVBAsMQkFtYXpvbiBXZWIgU2VydmljZXMgTz1BbWF6b24uY29t
...
mJKLnqz8EkxQB6qd2/7XimHrmYoo/DI1KjHKfxEFxUnwkOp1wC6sh78bPXordDTL
gBWeKhwdZHZ3d6TdkY+tpmbSE13+n1+4kUit901F4NIDecdMlIN3zJqQwTPsyw==
-----END CERTIFICATE-----
)EOF";

static const char client_key[] PROGMEM = R"KEY(
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAvdXSakGZNJoGlnY7B/Q7FZNRToZzl5QUAYaOvxZRawnKWfd0
b70e7rSSphbf+GNtvKM7S2iXtMH9qz3Vmv7qVM0CxfwzFi//z3m3ncmrqOSkLSuT
....
ut8HxDMV+AmxMDPTnC+5dEhduIw8r1mMBiYr7TXcu5vnDOgzlCq+S6RyWuOVcF2m
YJ8BWgR5mp4KsuPj/eczZLnKgtDzVLoGjg5VE4dPYKypBYHzeeqX
-----END RSA PRIVATE KEY-----
)KEY";

  BearSSL::X509List cert(ca_cert);
  wifiClient.setTrustAnchors(&cert);  
  BearSSL::X509List client_crt(client_cert);
  BearSSL::PrivateKey key(client_key);
  wifiClient.setClientRSACert(&client_crt, &key);
  setClockHttp();
  Serial.printf("settings heap size2: %u\n", ESP.getFreeHeap());
  if (pubsubclient.connect("client1")) {
      Serial.println("connected");
      pubsubclient.publish("queue1","hello from esp");
  }
  Serial.printf("settings heap size3: %u\n", ESP.getFreeHeap());
  Serial.println(pubsubclient.connected());
}

void setup(){
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
  Serial.printf("settings heap size1: %u\n", ESP.getFreeHeap());
  fetchCertAuthority();
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.begin(ssid, pass);
    if (WiFi.waitForConnectResult() != WL_CONNECTED)
      return;
    Serial.println("WiFi connected");
  }

  if (WiFi.status() == WL_CONNECTED) {
    if (!pubsubclient.connected()) {
      if (pubsubclient.connect("client1")) {
        pubsubclient.publish("queue1","hello from esp");
      }
    }
    else 
      pubsubclient.loop();
  }
}

Debug Messages


WiFi connected
IP address: 
192.168.0.104
settings heap size1: 44256
settings heap size2: 40064
settings heap size3: 38272
0
Connecting to Vishal kothari...
WiFi connected

Exception (28):
epc1=0x4020ab40 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00fe8524 depc=0x00000000
>>>stack>>>
ctx: cont
sp: 3ffffca0 end: 3fffffc0 offset: 01a0
3ffffe40:  00000000 3ffffea0 40208b6c 3fffefa0  
3ffffe50:  00000000 00000000 00000000 4020914b  
3ffffe60:  00000000 3fff189c 3ffeeb78 402033a1  
3ffffe70:  000022b3 00000d50 3ffeeb78 3ffeeb40  
3ffffe80:  000022b3 3ffeeb78 3fff0fbc 3ffeeb40  
3ffffe90:  000022b3 3ffeeb78 3fff0fbc 4020485d  
3ffffea0:  4020b580 3e367803 4020b580 3e367803  
3ffffeb0:  3fffff00 3ffeeb78 3ffeeb38 40206ed4  
3ffffec0:  3ffe87c1 00000000 3fffff54 40207cbc  
3ffffed0:  3ffe87c1 00000000 3fffff00 40207e70  
3ffffee0:  68736956 3ffeec44 3ffeeb38 3ffeeda8  
3ffffef0:  3ffe8524 3ffeec44 3ffeeb38 4020710d  
3fffff00:  4020b458 00000001 0000eb00 40203e00  
3fffff10:  00000000 00000000 3ffe85e1 00000000  
3fffff20:  3ffe8524 3ffeeb01 30303030 00000031  
3fffff30:  00fe85e3 00000000 00000000 00207c1e  
3fffff40:  00000000 00000000 00000000 00000000  
3fffff50:  00fe8524 00000000 00000000 00207cbc  
3fffff60:  3ffe000f 00000000 00000000 ff20af95  
3fffff70:  3ffe8524 3ffeec44 3ffeeb38 402010bc  
3fffff80:  30303030 00000031 00feec7c 40201486  
3fffff90:  4020b580 6800a8c0 feefeffe feefeffe  
3fffffa0:  3fffdad0 00000000 3ffeed78 40208c1c  
3fffffb0:  feefeffe feefeffe 3ffe8554 401004f5  
<<<stack<<<
 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v8b899c12
~ld

OR

WiFi connected
IP address: 
192.168.0.104
settings heap size1: 44256
settings heap size2: 40064

Exception (28):
epc1=0x4023304a epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000014 depc=0x00000000

>>>stack>>>

ctx: sys
sp: 3fffed50 end: 3fffffb0 offset: 01a0
3fffeef0:  3ffef8e4 40239df7 3ffe9b50 3ffe9b5c  
3fffef00:  3ffe9b5c 00000276 00000000 00000013  
3fffef10:  00000002 0000001a 40243763 3ffecce8  
3fffef20:  3ffe9b50 3fffdcc0 3ffe92e8 3ffe92e8  
3fffef30:  00000080 3ffecce8 3fffdab0 00000000  
3fffef40:  40243023 3fffdab0 00000000 00000001  
3fffef50:  3ffe92e8 40000f49 3fffdab0 40000f49  
3fffef60:  40000e19 40001878 00000002 3fffffc0  
3fffef70:  3fffff10 aa55aa55 000000cb 40104278  
3fffef80:  4010427e 00000002 3fffffc0 7fff7fff  
3fffef90:  4010000d 7fff7fff 7fff7fff 07ff7fff  
3fffefa0:  40100530 3fffef3c 401004dd 3ffffd48  
3fffefb0:  3fffffc0 00000000 00000000 feefeffe  
3fffefc0:  feefeffe feefeffe feefeffe feefeffe  
3fffefd0:  feefeffe feefeffe feefeffe feefeffe  
3fffefe0:  feefeffe feefeffe feefeffe feefeffe  
3fffeff0:  feefeffe feefeffe feefeffe feefeffe  
3ffff000:  feefeffe feefeffe feefeffe feefeffe  
3ffff010:  feefeffe feefeffe feefeffe feefeffe  
....
3fffff90:  4020b580 6800a8c0 feefeffe feefeffe  
3fffffa0:  3fffdad0 00000000 3ffeed78 40208c14  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v8b899c12
~ld

Exception 28: LoadProhibited: A load referenced a page mapped with an attribute that does not permit loads
Decoding 84 results
0x4023304a: ieee80211_crypto_decap at ?? line ?
0x40239df7: sta_input at ?? line ?
0x40243763: pp_tx_idle_timeout at ?? line ?
0x40243023: ppPeocessRxPktHdr at ?? line ?
0x40104278: call_user_start_local at ?? line ?
0x4010427e: call_user_start_local at ?? line ?
0x4010000d: call_user_start at ?? line ?
0x40100530: cont_ret at C:\Users\vikothar\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266/cont.S line 142
0x401004dd: cont_continue at C:\Users\vikothar\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266/cont.S line 51
0x4025b790: node_remove_from_list at ?? line ?
0x401030bd: lmacProcessTXStartData at ?? line ?
0x401030ba: lmacProcessTXStartData at ?? line ?
0x4021fa30: sha2big_update at /home/earle/Arduino/hardware/esp8266com/esp8266/tools/sdk/ssl/bearssl/src/hash/sha2big.c line 165
:  (inlined by) br_sha384_update at /home/earle/Arduino/hardware/esp8266com/esp8266/tools/sdk/ssl/bearssl/src/hash/sha2big.c line 207
0x40101fe6: wDev_ProcessFiq at ?? line ?
0x4022029b: sha2small_out at /home/earle/Arduino/hardware/esp8266com/esp8266/tools/sdk/ssl/bearssl/src/hash/sha2small.c line 249
0x40101e3c: wDev_ProcessFiq at ?? line ?
0x40236596: ieee80211_output_pbuf at ?? line ?
0x40244193: pp_attach at ?? line ?
0x402441e2: pp_attach at ?? line ?
0x402442ee: pp_attach at ?? line ?
0x40100d72: pp_post at ?? line ?
0x4024328b: ppTxPkt at ?? line ?
0x40236643: ieee80211_output_pbuf at ?? line ?

OR

Exception 28: LoadProhibited: A load referenced a page mapped with an attribute that does not permit loads
Decoding 19 results
0x4020ab40: BearSSL::PrivateKey::isRSA() const at C:\Users\vikothar\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\libraries\ESP8266WiFi\src/BearSSLHelpers.cpp line 728 (discriminator 1)
0x40208b6c: esp_yield at C:\Users\vikothar\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266/core_esp8266_main.cpp line 91
0x4020914b: delay at C:\Users\vikothar\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266/core_esp8266_wiring.cpp line 54
0x402033a1: WiFiClient::connect(IPAddress, unsigned short) at C:\Users\vikothar\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\libraries\ESP8266WiFi\src/include/ClientContext.h line 136
:  (inlined by) WiFiClient::connect(IPAddress, unsigned short) at C:\Users\vikothar\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\libraries\ESP8266WiFi\src/WiFiClient.cpp line 170
0x4020485d: BearSSL::WiFiClientSecure::connect(char const*, unsigned short) at C:\Users\vikothar\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\libraries\ESP8266WiFi\src/WiFiClientSecureBearSSL.cpp line 231
0x4020b580: std::function ::swap(std::function &) at ?? line ?
0x4020b580: std::function ::swap(std::function &) at ?? line ?
0x40206ed4: PubSubClient::connect(MQTT::Connect&) at C:\vishal data\arduino-1.8.3\libraries\pubsubclient-master\src/PubSubClient.cpp line 185
0x40207cbc: String::String(char const*) at C:\Users\vikothar\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266/WString.cpp line 36
0x40207e70: String::String(String const&) at C:\Users\vikothar\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266/WString.cpp line 41
0x4020710d: PubSubClient::connect(String) at C:\vishal data\arduino-1.8.3\libraries\pubsubclient-master\src/PubSubClient.cpp line 168
0x4020b458: std::function ::swap(std::function &) at ?? line ?
0x40203e00: BearSSL::WiFiClientSecure::_installClientX509Validator() at C:\Users\vikothar\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\libraries\ESP8266WiFi\src/WiFiClientSecureBearSSL.cpp line 927
0x402010bc: loop at C:\vishal data\iot\esp code\esp-awsiot-bearssl/esp-awsiot-bearssl.ino line 235

Any inputs on this will be very helpful. Thanks.

Metadata

Metadata

Labels

waiting for feedbackWaiting on additional info. If it's not received, the issue may be closed.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions