khoih-prog / WebSockets2_Generic

A WebSocket Server and Client library for Arduino, based on RFC6455, for writing modern Websockets applications. Now support ESP8266, ESP32 (including ESP32-S2 Saola, AI-Thinker ESP-12K, WT32_ETH01, etc.), nRF52, SAMD21, SAMD51, SAM DUE, STM32F/L/H/G/WB/MP1, Teensy, RP2040-based, etc. boards, with WiFiNINA, Teensy 4.1 NativeEthernet/QNEthernet, Ethernet W5x00 / ENC28J60 / LAN8742A / LAN8720, ESP8266 / ESP32-AT modules/shields, as well as SINRIC / Alexa / Google Home
GNU General Public License v3.0
81 stars 30 forks source link

Add setInsecure for esp32 #18

Closed LioRei closed 3 years ago

LioRei commented 3 years ago

I've added the setInsecure fonction to be able to work with self signed certificate, it's really usefull in dev ;)

Usage example in Secured ESP32 Websockets Client: replace client.setCACert(echo_org_ssl_ca_cert); by client.setInsecure(); and voila, it's working

khoih-prog commented 3 years ago

Hi

Thanks for your PR. I prefer you change the PR as follows to not using an extra variable _use_insecure (similar to ESP8266 solution) and setInsecure is only optional whenever we don't need, don't have or don't use CA Certs

  1. client.hpp: no change by deleting
bool _use_insecure;
  1. WebSockets2_Generic_Client.hpp

Don't need this:

this->_use_insecure = true;

and change to

if ( this->_optional_ssl_ca_cert || this->_optional_ssl_client_ca || this->_optional_ssl_private_key )
{
  if (this->_optional_ssl_ca_cert)
  {
    client->setCACert(this->_optional_ssl_ca_cert);
  }

  if (this->_optional_ssl_client_ca)
  {
    client->setCertificate(this->_optional_ssl_client_ca);
  }

  if (this->_optional_ssl_private_key)
  {
    client->setPrivateKey(this->_optional_ssl_private_key);
  }
}  
else
{
  client->setInsecure();
}
  1. esp32_tcp.hpp : OK with your change

Usage:

Just keep these CA Certs as nullptr, and the client->setInsecure() will be called automatically

const char* _optional_ssl_ca_cert = nullptr;
const char* _optional_ssl_client_ca = nullptr;
const char* _optional_ssl_private_key = nullptr;
khoih-prog commented 3 years ago

Thanks Lionel,

The PR has been merged and I'll add some more mods before publishing a new Release.

Regards,

khoih-prog commented 3 years ago

Hi Lionel,

Just added 2 new InSecure-mode examples. Please retest to be sure everything is OK. Thanks.

  1. InSecured-Esp32-Client
  2. InSecured-Esp8266-Client
LioRei commented 3 years ago

I have tested the example with an ESP32-PICO-KIT (esp32-pico-d4) and an ESP32-S2 SAOLA, it's working for the 2 boards ;)

I don't own an ESP8266 so I can't test it

khoih-prog commented 3 years ago

Thanks. I'll published new release soon.

khoih-prog commented 3 years ago

HI Lionel,

Just published the new WebSockets2_Generic releases v1.2.4 with a note about your contribution in Contributions and Thanks.

Hoping to get more from you.

Regards,


Release v1.2.4

  1. Add InSecure mode for ESP32. For more info, check Add setInsecure for esp32 #18
  2. Add InSecude-mode examples for ESP32/ESP8266
emanavas commented 3 years ago

I update from Arduino ide to Release v1.2.4 and produce a error D:\DC100\FW\arduino-1.8.13\portable\sketchbook\libraries\WebSockets2_Generic\src/Tiny_Websockets_Generic/network/esp32/esp32_tcp.hpp:71:24: error: 'class WiFiClientSecure' has no member named 'setInsecure' this->client.setInsecure();

LioRei commented 3 years ago

In the board manager, wich version of esp32 do you use ? Personnaly, I use 2.0.0-alpha1 because I'm using an ESP32S2 board.

khoih-prog commented 3 years ago

@emanavas

You have to install ESP32 core, at least the version v1.0.5. The setInsecure() has been defined in WiFiClientSecure.h#L66

Next time, it's better to create a new issue, not post in the closed PR like this.