ssl – SSL/TLS module¶
This module implements a subset of the corresponding CPython module,
as described below. For more information, refer to the original
CPython documentation: ssl.
This module provides access to Transport Layer Security (previously and widely known as “Secure Sockets Layer”) encryption and peer authentication facilities for network sockets, both client-side and server-side.
Functions¶
- ssl.wrap_socket(sock, server_side=False, keyfile=None, certfile=None, cert_reqs=CERT_NONE, cadata=None, server_hostname=None, do_handshake=True)¶
Wrap the given sock and return a new wrapped-socket object. The implementation of this function is to first create an
SSLContextand then call theSSLContext.wrap_socketmethod on that context object. The arguments sock, server_side and server_hostname are passed through unchanged to the method call. The argument do_handshake is passed through as do_handshake_on_connect. The remaining arguments have the following behaviour:cert_reqs determines whether the peer (server or client) must present a valid certificate. Note that for mbedtls based ports,
ssl.CERT_NONEandssl.CERT_OPTIONALwill not validate any certificate, onlyssl.CERT_REQUIREDwill.cadata is a bytes object containing the CA certificate chain (in DER format) that will validate the peer’s certificate. Currently only a single DER-encoded certificate is supported.
Depending on the underlying module implementation in a particular MicroPython port, some or all keyword arguments above may be not supported.
class SSLContext¶
- class ssl.SSLContext(protocol, /)¶
Create a new SSLContext instance. The protocol argument must be one of the
PROTOCOL_*constants.
- SSLContext.wrap_socket(sock, *, server_side=False, do_handshake_on_connect=True, server_hostname=None)¶
Takes a
streamsock (usually socket.socket instance ofSOCK_STREAMtype), and returns an instance of ssl.SSLSocket, wrapping the underlying stream. The returned object has the usualstreaminterface methods likeread(),write(), etc.server_side selects whether the wrapped socket is on the server or client side. A server-side SSL socket should be created from a normal socket returned from
accept()on a non-SSL listening server socket.do_handshake_on_connect determines whether the handshake is done as part of the
wrap_socketor whether it is deferred to be done as part of the initial reads or writes For blocking sockets doing the handshake immediately is standard. For non-blocking sockets (i.e. when the sock passed intowrap_socketis in non-blocking mode) the handshake should generally be deferred because otherwisewrap_socketblocks until it completes. Note that in AXTLS the handshake can be deferred until the first read or write but it then blocks until completion.server_hostname is for use as a client, and sets the hostname to check against the received server certificate. It also sets the name for Server Name Indication (SNI), allowing the server to present the proper certificate.
Warning
Some implementations of ssl module do NOT validate server certificates,
which makes an SSL connection established prone to man-in-the-middle attacks.
CPython’s wrap_socket returns an SSLSocket object which has methods typical
for sockets, such as send, recv, etc. MicroPython’s wrap_socket
returns an object more similar to CPython’s SSLObject which does not have
these socket methods.
- SSLContext.verify_mode¶
Set or get the behaviour for verification of peer certificates. Must be one of the
CERT_*constants.
Exceptions¶
- ssl.SSLError¶
This exception does NOT exist. Instead its base class, OSError, is used.
Constants¶
- ssl.CERT_NONE¶
- ssl.CERT_OPTIONAL¶
- ssl.CERT_REQUIRED¶
Supported values for cert_reqs parameter, and the
SSLContext.verify_modeattribute.