jbuszkie / HTTPSRedirect

Clone of https://github.com/electronicsguy/ESP8266.git - just the HTTPSRedirect
7 stars 8 forks source link

SSL - An invalid SSL record was received #1

Closed Twilight-Logic closed 2 years ago

Twilight-Logic commented 2 years ago

I have been trying to interface with Google App Script using this library to read my calendar. I am following the examples I have found online. I have published my Google script as a web app and it returns the desired result in the browser on the PC. However, I cannot get the same result on the ESP32.

This is the particular project I am following and on which I have based the code in my sketch. Admittedly the author of this project is using an ESP8266 but there are a number of other very similar examples of the use of the HTTPSRedirect library online: https://github.com/SensorsIot/Reminder-with-Google-Calender/tree/master/ReminderV2

The function that is called from setup() function and makes and initial connection to the Google host works fine but shows the following warning when the log level is set to 'debug':

[I][ssl_client.cpp:127] start_ssl_client(): WARNING: Skipping SSL Verification. INSECURE!

This is to be expected since the sketch includes a call to the .setInsecure() method. However, the second function that is called from void loop() does not work and hangs at the GET request. I see the following error returned:

[E][ssl_client.cpp:36] _handle_error(): [data_to_read():287]: (-29184) SSL - An invalid SSL record was received

I have checked that the URL is being formed correctly and also tried adding delays but this has made no difference. Unfortunately I can't get past this point.

The full output in serial monitor looks like this:

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
MAC address: 30:AE:A4:FF:0B:D8
Starting WiFi client [D][WiFiGeneric.cpp:374] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 4 - STA_CONNECTED
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 7 - STA_GOT_IP
[D][WiFiGeneric.cpp:419] _eventCallback(): STA IP: 10.210.210.76, MASK: 255.255.255.192, GW: 10.210.210.125
.
IP address: 10.210.210.76
Getting time from NTP server...
Done.
Connecting to script.google.com
Attempt: 0
[I][ssl_client.cpp:127] start_ssl_client(): WARNING: Skipping SSL Verification. INSECURE!
Finished FOR
Connected to Google
Start Request...
[I][ssl_client.cpp:127] start_ssl_client(): WARNING: Skipping SSL Verification. INSECURE!
Connected.
[I][ssl_client.cpp:127] start_ssl_client(): WARNING: Skipping SSL Verification. INSECURE!
[E][ssl_client.cpp:36] _handle_error(): [data_to_read():287]: (-29184) SSL - An invalid SSL record was received

I have tried ESP32 library versions 1.0.4, 1.0.5 and 1.0.6. I had to remove the .setInsecure() line to make it compile with library version 1.0.4. I attempted to add the Google CA certificates at one point, but couldn't get this to work either. I think at least two certificates are required but am not sure how to add multiple certificates? Since none of the examples online seem to have any certificates included, I figured this ought not to be necessary so gave up on the idea and commented out the added lines.

Upload of complete sketch: Calendar-Clock.zip

Twilight-Logic commented 2 years ago

I realised after I had submitted this issue that the original project written by Andreas, and which I based mine on, was developed on the ESP8266, but I was using an ESP32. It is stated that this HTTPSDirect library works on both platforms. Looking at the source code it can be seen that it places a wrapper around many of the original WiFiSecureClient functions and with some minor differences, the syntax is essentially the same on both platforms, so no reason why it shouldn't. That led me to wonder whether the problem might be platform specific. I therefore purchased an ESP8266 to test with which arrived today. I had to make some minor adjustments to the sketch, for example to use ESP8266 versions of libraries, for example the library instead of and ESP8266mDNS.h> instead of . I also had to change the GPIO pin number for the LDR ADC sensor, but aside from that, the code is the same. It was a simple matter to wrap these dependencies within #ifdef statements so that the relevant versions of the libraries and GPIO pins can be applied depending on the detected platform:

#ifdef ESP8266
  #include <ESP8266WiFi.h>
  #include <LittleFS.h>
  #include <ESP8266mDNS.h>
  #define LDR A0
#endif

#ifdef ESP32
  #include <WiFi.h>
  #include <LITTLEFS.h>
  #include <ESPmDNS.h>
  #define LDR 35
#endif

The remainder of the code remained the same. I then compiled, uploaded and tested both Andreas project and my own project code on the ESP8266. Both ran as expected and connected to the Google App Script resource without error and I was able to read events from my calendar.

Evidently then, the problem is related to the ESP32 platform and some difference between the implementations of the ESP8266 and ESP32 versions of the WiFi library, specifically the WiFiClientSecure module. Same code. The ESP8266 version works, the ESP32 version generates the SSL error. For some reason I had assumed that it would just work identically on both.

I have raised an issue on Esspresiffs ESP32 library GitHub repository.

Twilight-Logic commented 2 years ago

The project has been on the backburner for some time now and I recently came back to it. It turns out that I was using an old version of the ESP32 SDK. It came to my attention recently that the URL to use to obtain the ESP32 board package has changed to:

https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

I don't know when that change took place, but I updated the URL and downloaded the updated board library. I no longer get the above error.

Incidentally, in order to use the library on the ESP32, I also added the following change in HTTPSRedirect.h:

ifdef ESP8266

define WIFIMCU "ESP8266"

endif

ifdef ESP32

define WIFIMCU "ESP32"

endif

I then replaced the two instances of "ESP8266" (createGetRequest & createPOSTRequest) in HTTPSredirect.cpp to "WIFIMCU", for example:

void HTTPSRedirect::createGetRequest(const String& url, const char* host){
  _Request =  String("GET ") + url + " HTTP/1.1\r\n" +
                          "Host: " + host + "\r\n" +
                          "User-Agent: " + WIFIMCU + "\r\n" +
                          (_keepAlive ? "" : "Connection: close\r\n") + 
                          "\r\n\r\n";

  return;
}

This should make no functional difference. It just sends the corresponding User-Agent string to the ESP board board used.

This issue can now be closed.