Request minimal SSL socket example. #17066
-
About the SSL socket introduction in the document and tutorial so little, please post complete minimal SSL socket example between two ESP32 board? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 10 replies
-
You can find https server and client examples here: https://github.com/micropython/micropython/tree/master/examples/network |
Beta Was this translation helpful? Give feedback.
-
Here is an example (weather.py) that might be useful to someone: # weather.py
import socket
import tls # use tls directly
import network
import json
# import ntptime
from wifi import key # (SSID, PWD)
ssid, pwd = key # give correct cred for your wifi
# Set up WiFi
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(ssid, pwd)
while not sta_if.isconnected():
pass
print("Client IP:", sta_if.ifconfig()[0])
# do not use certificate - no need for correct time
# # ntptime.host = 'time.google.com'
# ntptime.host = '216.239.35.12'
# ntptime.timeout = 5
# ntptime.settime() # Syncs time with an NTP server
# Create SSL client context
ssl = tls.SSLContext(tls.PROTOCOL_TLS_CLIENT)
ssl.verify_mode = tls.CERT_NONE
# tls.check_hostname = False
# api.open-meteo.com (94.130.142.35)
# Create socket
server_ip = "94.130.142.35" # Replace with server IP
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
addr = socket.getaddrinfo(server_ip, 443)[0][-1] # Replace with server IP
s.connect(addr)
ssl_sock = ssl.wrap_socket(s, server_side=False)
lat,log = 60.3913,5.3221 # change this to your city
req = f"""GET /v1/forecast?latitude={lat}&longitude={log}¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m HTTP/1.1\r\nHost: api.open-meteo.com\r\nConnection: close\r\n\r\n"""
for i in range(1):
try:
print('Get weather')
msg = f"Msg-{i} Hello from client!\n".encode()
ssl_sock.write(req.encode())
data = b''
while True:
response = ssl_sock.readline()
if response:
if len(response) > 100:
data += response
else:
break
if data:
wdata = json.loads(data.decode())
print(json.dumps(wdata))
except Exception as e:
print('Error:', e)
ssl_sock.close()
s.close()
print('Done') Now, if you are interested in secure communication between two ESP32s, you need to create a server self-signed certificate using openssl. I might be able to put some examples up on the github when I find some time to do so. |
Beta Was this translation helpful? Give feedback.
-
Thanks, I need time to understand and test. |
Beta Was this translation helpful? Give feedback.
-
I have just uploaded some examples to github |
Beta Was this translation helpful? Give feedback.
-
I found some introduction about generate certificate using config file and extfile like below: configfile.conf [req]
distinguished_name = req_distinguished_name
prompt = no
[req_distinguished_name]
C = AU
ST = somestate
L = somecity
O = someorganization
OU = someunit
CN = somename v3.ext authorityKeyIdentifier=issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
IP.1 = 192.168.2.7
The What different between |
Beta Was this translation helpful? Give feedback.
-
CPython s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.2.7', 8443))
ssl_socket = context.wrap_socket(s, server_side = False, server_hostname = '192.168.2.7')
ssl_socket.getpeercert('192.168.2.7') I try use |
Beta Was this translation helpful? Give feedback.
-
I test ECC certificate verify between two ESP32 board success, but use CPython(client side) connect to ESP32 board(server side) fail(use same certificate and key file), appear:
By the way, I test RSA certificate verify, use CPython(client side) connect to ESP32 board(server side) success. |
Beta Was this translation helpful? Give feedback.
I have just uploaded some examples to github
https://github.com/shariltumin/ssl-tls-examples-micropython