2

I successfully managed to connect my ESP32 to a WebSocket server. Now I am trying to make it work with SSL. I tried this simple code to connect to www.google.com. I used this to generate the cert and key.

openssl req -newkey rsa:2048 -nodes -keyout client.key -x509 -days 365 -out client.crt

.

Then copy over the key and cert files with adafruit-ampy. Don't forget to change your serial port.

ampy -p /dev/tty.SLAB_USBtoUART put client.crt
ampy -p /dev/tty.SLAB_USBtoUART put client.key

This is the code on the ESP32

import ussl
import usocket
import networking

KEY_PATH = "client.key"
CERT_PATH = "client.crt"
HOST, PATH, PORT = "www.google.com", "/" 443

with open(KEY_PATH, 'rb') as f:
    key1 = f.read()

with open(CERT_PATH, 'rb') as f:
    cert1 = f.read()

s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
addr = usocket.getaddrinfo(HOST, PORT)[0][-1]
s.connect(addr)
sock = ussl.wrap_socket(s, key = key1, cert = cert1)
sock.write(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (PATH, HOST), 'utf8'))
print(sock.read(100))

I get this error:

mbedtls_ssl_handshake error: -7280
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 5] EIO

Has anyone successfully used ssl socket_wrap on ESP32?

EDIT (23.12.2018):

I managed to finally get something working, fetching HTML from google over HTTPS, yee. Check code above. Hope this helps. I assume the MicropPython port for ESP32 has been getting better and this is the reason this works now.

Next step is to get the SSL WebSocket working ...

EDIT (09.06.2019):

It's working now. This library works great for what I intended to do https://github.com/danni/uwebsockets

silbo
  • 31
  • 1
  • 7

2 Answers2

1

the error -7280 is translated as follows(from: include/mbedtls/ssl.h):

#define MBEDTLS_ERR_SSL_CONN_EOF -0x7280 /**< The connection indicated an EOF. */

The issue you have maybe a memory problem...Could you try adding:

import gc
gc.collect() 

after you have called getaddinfo()?

Also could also try to load the key/cert files after the s.connect() call?

Lingster
  • 977
  • 10
  • 21
  • Thanks, I will try this when I get time : ) – silbo Aug 28 '18 at 15:38
  • I am currently getting this error, I will try to investigate also >>> sock = ussl.wrap_socket(s, key = key1, cert = cert1) . mbedtls_ssl_handshake error: -80 Traceback (most recent call last): File "", line 1, in OSError: [Errno 5] EIO – silbo Dec 23 '18 at 16:07
  • It works now, I didn't need to change anything it seems, may be it was a memory error inside the ussl or usock library. I have no idea. But seems with the latest ESP32 MicroPython port everything works now. – silbo Dec 23 '18 at 19:16
0

Got it to work in the end with the following code: https://github.com/robokoding/sumorobot-firmware/blob/wifi/uwebsockets.py#L246

silbo
  • 31
  • 1
  • 7