|
1 | | -:mod:`ussl` -- ssl module |
2 | | -=============================== |
| 1 | +:mod:`ussl` -- SSL/TLS module |
| 2 | +============================= |
3 | 3 |
|
4 | 4 | .. module:: ussl |
5 | 5 | :synopsis: TLS/SSL wrapper for socket objects |
6 | 6 |
|
7 | | -This module provides access to Transport Layer Security (often known as |
8 | | -“Secure Sockets Layer”) encryption and peer authentication facilities for |
9 | | -network sockets, both client-side and server-side. |
| 7 | +This module provides access to Transport Layer Security (previously and |
| 8 | +widely known as “Secure Sockets Layer”) encryption and peer authentication |
| 9 | +facilities for network sockets, both client-side and server-side. |
10 | 10 |
|
11 | | -.. only:: not port_wipy |
| 11 | +Functions |
| 12 | +--------- |
12 | 13 |
|
13 | | - Functions |
14 | | - --------- |
| 14 | +.. function:: ssl.wrap_socket(sock, server_side=False, keyfile=None, certfile=None, cert_reqs=CERT_NONE, ca_certs=None) |
15 | 15 |
|
16 | | - .. function:: ssl.wrap_socket(sock, server_side=False) |
| 16 | + Takes a stream `sock` (usually usocket.socket instance of ``SOCK_STREAM`` type), |
| 17 | + and returns an instance of ssl.SSLSocket, which wraps the underlying stream in |
| 18 | + an SSL context. Returned object has the usual stream interface methods like |
| 19 | + `read()`, `write()`, etc. In MicroPython, the returned object does not expose |
| 20 | + socket interface and methods like `recv()`, `send()`. In particular, a |
| 21 | + server-side SSL socket should be created from a normal socket returned from |
| 22 | + `accept()` on a non-SSL listening server socket. |
17 | 23 |
|
18 | | - Takes a stream `sock` (usually usocket.socket instance of ``SOCK_STREAM`` type), |
19 | | - and returns an instance of ssl.SSLSocket, which wraps the underlying stream in |
20 | | - an SSL context. Returned object has the usual stream interface methods like |
21 | | - `read()`, `write()`, etc. In MicroPython, the returned object does not expose |
22 | | - socket interface and methods like `recv()`, `send()`. In particular, a |
23 | | - server-side SSL socket should be created from a normal socket returned from |
24 | | - `accept()` on a non-SSL listening server socket. |
| 24 | + Depending on the underlying module implementation for a particular board, |
| 25 | + some or all keyword arguments above may be not supported. |
25 | 26 |
|
26 | | - .. warning:: |
| 27 | +.. warning:: |
27 | 28 |
|
28 | | - Currently, this function does NOT validate server certificates, which makes |
29 | | - an SSL connection established prone to man-in-the-middle attacks. |
| 29 | + Some implementations of ``ssl`` module do NOT validate server certificates, |
| 30 | + which makes an SSL connection established prone to man-in-the-middle attacks. |
30 | 31 |
|
| 32 | +Exceptions |
| 33 | +---------- |
31 | 34 |
|
32 | | -.. only:: port_wipy |
| 35 | +.. data:: ssl.SSLError |
33 | 36 |
|
34 | | - Functions |
35 | | - --------- |
| 37 | + This exception does NOT exist. Instead its base class, OSError, is used. |
36 | 38 |
|
37 | | - .. function:: ssl.wrap_socket(sock, keyfile=None, certfile=None, server_side=False, cert_reqs=CERT_NONE, ca_certs=None) |
| 39 | +Constants |
| 40 | +--------- |
38 | 41 |
|
39 | | - Takes an instance sock of socket.socket, and returns an instance of ssl.SSLSocket, a subtype of |
40 | | - ``socket.socket``, which wraps the underlying socket in an SSL context. sock must be a ``SOCK_STREAM`` |
41 | | - socket and protocol number ``socket.IPPROTO_SEC``; other socket types are unsupported. Example:: |
| 42 | +.. data:: ssl.CERT_NONE |
| 43 | + ssl.CERT_OPTIONAL |
| 44 | + ssl.CERT_REQUIRED |
42 | 45 |
|
43 | | - import socket |
44 | | - import ssl |
45 | | - s = socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC) |
46 | | - ss = ssl.wrap_socket(s) |
47 | | - ss.connect(socket.getaddrinfo('www.google.com', 443)[0][-1]) |
48 | | - |
49 | | - Certificates must be used in order to validate the other side of the connection, and also to |
50 | | - authenticate ourselves with the other end. Such certificates must be stored as files using the |
51 | | - FTP server, and they must be placed in specific paths with specific names. |
52 | | - |
53 | | - - The certificate to validate the other side goes in: **'/flash/cert/ca.pem'** |
54 | | - - The certificate to authenticate ourselves goes in: **'/flash/cert/cert.pem'** |
55 | | - - The key for our own certificate goes in: **'/flash/cert/private.key'** |
56 | | - |
57 | | - .. note:: |
58 | | - |
59 | | - When these files are stored, they are placed inside the internal **hidden** file system |
60 | | - (just like firmware updates), and therefore they are never visible. |
61 | | - |
62 | | - For instance to connect to the Blynk servers using certificates, take the file ``ca.pem`` located |
63 | | - in the `blynk examples folder <https://github.com/wipy/wipy/tree/master/examples/blynk>`_ |
64 | | - and put it in '/flash/cert/'. Then do:: |
65 | | - |
66 | | - import socket |
67 | | - import ssl |
68 | | - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC) |
69 | | - ss = ssl.wrap_socket(s, cert_reqs=ssl.CERT_REQUIRED, ca_certs='/flash/cert/ca.pem') |
70 | | - ss.connect(socket.getaddrinfo('cloud.blynk.cc', 8441)[0][-1]) |
71 | | - |
72 | | - SSL sockets inherit all methods and from the standard sockets, see the :mod:`usocket` module. |
73 | | - |
74 | | - Exceptions |
75 | | - ---------- |
76 | | - |
77 | | - .. data:: ssl.SSLError |
78 | | - |
79 | | - Constants |
80 | | - --------- |
81 | | - |
82 | | - .. data:: ssl.CERT_NONE |
83 | | - .. data:: ssl.CERT_OPTIONAL |
84 | | - .. data:: ssl.CERT_REQUIRED |
85 | | - |
86 | | - supported values in ``cert_reqs`` |
| 46 | + Supported values for `cert_reqs` parameter. |
0 commit comments