2021-08-12 04:59:29 +01:00
|
|
|
:mod:`ssl` -- SSL/TLS module
|
|
|
|
============================
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
.. module:: ssl
|
2015-10-20 14:04:55 +01:00
|
|
|
:synopsis: TLS/SSL wrapper for socket objects
|
|
|
|
|
2017-07-02 13:37:31 +01:00
|
|
|
|see_cpython_module| :mod:`python:ssl`.
|
|
|
|
|
2017-04-16 07:41:32 +01:00
|
|
|
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.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-04-16 07:41:32 +01:00
|
|
|
Functions
|
|
|
|
---------
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2022-01-28 04:42:46 +00:00
|
|
|
.. function:: ssl.wrap_socket(sock, server_side=False, keyfile=None, certfile=None, cert_reqs=CERT_NONE, cadata=None, server_hostname=None, do_handshake=True)
|
2021-05-17 04:21:26 +01:00
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
Takes a `stream` *sock* (usually socket.socket instance of ``SOCK_STREAM`` type),
|
2017-04-16 07:41:32 +01:00
|
|
|
and returns an instance of ssl.SSLSocket, which wraps the underlying stream in
|
2017-12-04 16:36:20 +00:00
|
|
|
an SSL context. Returned object has the usual `stream` interface methods like
|
2020-04-02 18:01:16 +01:00
|
|
|
``read()``, ``write()``, etc.
|
|
|
|
A server-side SSL socket should be created from a normal socket returned from
|
2021-08-12 04:59:29 +01:00
|
|
|
:meth:`~socket.socket.accept()` on a non-SSL listening server socket.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2020-04-02 18:01:16 +01:00
|
|
|
- *do_handshake* determines whether the handshake is done as part of the ``wrap_socket``
|
|
|
|
or whether it is deferred to be done as part of the initial reads or writes
|
|
|
|
(there is no ``do_handshake`` method as in CPython).
|
|
|
|
For blocking sockets doing the handshake immediately is standard. For non-blocking
|
|
|
|
sockets (i.e. when the *sock* passed into ``wrap_socket`` is in non-blocking mode)
|
|
|
|
the handshake should generally be deferred because otherwise ``wrap_socket`` blocks
|
|
|
|
until it completes. Note that in AXTLS the handshake can be deferred until the first
|
|
|
|
read or write but it then blocks until completion.
|
|
|
|
|
2022-01-28 04:42:46 +00:00
|
|
|
- *cert_reqs* determines whether the peer (server or client) must present a valid certificate.
|
|
|
|
Note that for mbedtls based ports, ``ssl.CERT_NONE`` and ``ssl.CERT_OPTIONAL`` will not
|
|
|
|
validate any certificate, only ``ssl.CERT_REQUIRED`` will.
|
|
|
|
|
|
|
|
- *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.
|
|
|
|
|
|
|
|
- *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.
|
|
|
|
|
2017-10-30 16:03:54 +00:00
|
|
|
Depending on the underlying module implementation in a particular
|
2020-06-04 02:38:45 +01:00
|
|
|
:term:`MicroPython port`, some or all keyword arguments above may be not supported.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-04-16 07:41:32 +01:00
|
|
|
.. warning::
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
Some implementations of ``ssl`` module do NOT validate server certificates,
|
2017-04-16 07:41:32 +01:00
|
|
|
which makes an SSL connection established prone to man-in-the-middle attacks.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2020-04-02 18:01:16 +01:00
|
|
|
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.
|
|
|
|
|
2017-04-16 07:41:32 +01:00
|
|
|
Exceptions
|
|
|
|
----------
|
2015-10-20 15:24:25 +01:00
|
|
|
|
2017-04-16 07:41:32 +01:00
|
|
|
.. data:: ssl.SSLError
|
2015-10-20 15:24:25 +01:00
|
|
|
|
2017-04-16 07:41:32 +01:00
|
|
|
This exception does NOT exist. Instead its base class, OSError, is used.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-04-16 07:41:32 +01:00
|
|
|
Constants
|
|
|
|
---------
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
.. data:: ssl.CERT_NONE
|
|
|
|
ssl.CERT_OPTIONAL
|
|
|
|
ssl.CERT_REQUIRED
|
2015-10-21 21:52:36 +01:00
|
|
|
|
2017-06-29 22:34:52 +01:00
|
|
|
Supported values for *cert_reqs* parameter.
|