Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1 | :mod:`ssl` --- SSL wrapper for socket objects |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 2 | ============================================= |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: ssl |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 5 | :synopsis: SSL wrapper for socket objects |
| 6 | |
| 7 | .. moduleauthor:: Bill Janssen <bill.janssen@gmail.com> |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 8 | .. sectionauthor:: Bill Janssen <bill.janssen@gmail.com> |
| 9 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 11 | .. index:: single: OpenSSL; (use in module ssl) |
| 12 | |
| 13 | .. index:: TLS, SSL, Transport Layer Security, Secure Sockets Layer |
| 14 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 15 | This module provides access to Transport Layer Security (often known as "Secure |
| 16 | Sockets Layer") encryption and peer authentication facilities for network |
| 17 | sockets, both client-side and server-side. This module uses the OpenSSL |
| 18 | library. It is available on all modern Unix systems, Windows, Mac OS X, and |
| 19 | probably additional platforms, as long as OpenSSL is installed on that platform. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 20 | |
| 21 | .. note:: |
| 22 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 23 | Some behavior may be platform dependent, since calls are made to the |
| 24 | operating system socket APIs. The installed version of OpenSSL may also |
| 25 | cause variations in behavior. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 26 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 27 | This section documents the objects and functions in the ``ssl`` module; for more |
| 28 | general information about TLS, SSL, and certificates, the reader is referred to |
| 29 | the documents in the "See Also" section at the bottom. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 30 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 31 | This module provides a class, :class:`ssl.SSLSocket`, which is derived from the |
| 32 | :class:`socket.socket` type, and provides a socket-like wrapper that also |
| 33 | encrypts and decrypts the data going over the socket with SSL. It supports |
Antoine Pitrou | dab6426 | 2010-09-19 13:31:06 +0000 | [diff] [blame] | 34 | additional methods such as :meth:`getpeercert`, which retrieves the |
| 35 | certificate of the other side of the connection, and :meth:`cipher`,which |
| 36 | retrieves the cipher being used for the secure connection. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 37 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 38 | For more sophisticated applications, the :class:`ssl.SSLContext` class |
| 39 | helps manage settings and certificates, which can then be inherited |
| 40 | by SSL sockets created through the :meth:`SSLContext.wrap_socket` method. |
| 41 | |
| 42 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 43 | Functions, Constants, and Exceptions |
| 44 | ------------------------------------ |
| 45 | |
| 46 | .. exception:: SSLError |
| 47 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 48 | Raised to signal an error from the underlying SSL implementation |
| 49 | (currently provided by the OpenSSL library). This signifies some |
| 50 | problem in the higher-level encryption and authentication layer that's |
| 51 | superimposed on the underlying network connection. This error |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 52 | is a subtype of :exc:`socket.error`, which in turn is a subtype of |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 53 | :exc:`IOError`. The error code and message of :exc:`SSLError` instances |
| 54 | are provided by the OpenSSL library. |
| 55 | |
| 56 | .. exception:: CertificateError |
| 57 | |
| 58 | Raised to signal an error with a certificate (such as mismatching |
| 59 | hostname). Certificate errors detected by OpenSSL, though, raise |
| 60 | an :exc:`SSLError`. |
| 61 | |
| 62 | |
| 63 | Socket creation |
| 64 | ^^^^^^^^^^^^^^^ |
| 65 | |
| 66 | The following function allows for standalone socket creation. Starting from |
| 67 | Python 3.2, it can be more flexible to use :meth:`SSLContext.wrap_socket` |
| 68 | instead. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 70 | .. function:: wrap_socket(sock, keyfile=None, certfile=None, server_side=False, cert_reqs=CERT_NONE, ssl_version={see docs}, ca_certs=None, do_handshake_on_connect=True, suppress_ragged_eofs=True, ciphers=None) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 71 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 72 | Takes an instance ``sock`` of :class:`socket.socket`, and returns an instance |
| 73 | of :class:`ssl.SSLSocket`, a subtype of :class:`socket.socket`, which wraps |
| 74 | the underlying socket in an SSL context. For client-side sockets, the |
| 75 | context construction is lazy; if the underlying socket isn't connected yet, |
| 76 | the context construction will be performed after :meth:`connect` is called on |
| 77 | the socket. For server-side sockets, if the socket has no remote peer, it is |
| 78 | assumed to be a listening socket, and the server-side SSL wrapping is |
| 79 | automatically performed on client connections accepted via the :meth:`accept` |
| 80 | method. :func:`wrap_socket` may raise :exc:`SSLError`. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 81 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 82 | The ``keyfile`` and ``certfile`` parameters specify optional files which |
| 83 | contain a certificate to be used to identify the local side of the |
| 84 | connection. See the discussion of :ref:`ssl-certificates` for more |
| 85 | information on how the certificate is stored in the ``certfile``. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 86 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 87 | The parameter ``server_side`` is a boolean which identifies whether |
| 88 | server-side or client-side behavior is desired from this socket. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 89 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 90 | The parameter ``cert_reqs`` specifies whether a certificate is required from |
| 91 | the other side of the connection, and whether it will be validated if |
| 92 | provided. It must be one of the three values :const:`CERT_NONE` |
| 93 | (certificates ignored), :const:`CERT_OPTIONAL` (not required, but validated |
| 94 | if provided), or :const:`CERT_REQUIRED` (required and validated). If the |
| 95 | value of this parameter is not :const:`CERT_NONE`, then the ``ca_certs`` |
| 96 | parameter must point to a file of CA certificates. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 97 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 98 | The ``ca_certs`` file contains a set of concatenated "certification |
| 99 | authority" certificates, which are used to validate certificates passed from |
| 100 | the other end of the connection. See the discussion of |
| 101 | :ref:`ssl-certificates` for more information about how to arrange the |
| 102 | certificates in this file. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 103 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 104 | The parameter ``ssl_version`` specifies which version of the SSL protocol to |
| 105 | use. Typically, the server chooses a particular protocol version, and the |
| 106 | client must adapt to the server's choice. Most of the versions are not |
| 107 | interoperable with the other versions. If not specified, for client-side |
| 108 | operation, the default SSL version is SSLv3; for server-side operation, |
| 109 | SSLv23. These version selections provide the most compatibility with other |
| 110 | versions. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 111 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 112 | Here's a table showing which versions in a client (down the side) can connect |
| 113 | to which versions in a server (along the top): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 114 | |
| 115 | .. table:: |
| 116 | |
| 117 | ======================== ========= ========= ========== ========= |
| 118 | *client* / **server** **SSLv2** **SSLv3** **SSLv23** **TLSv1** |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 119 | ------------------------ --------- --------- ---------- --------- |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 120 | *SSLv2* yes no yes no |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 121 | *SSLv3* yes yes yes no |
| 122 | *SSLv23* yes no yes no |
| 123 | *TLSv1* no no yes yes |
| 124 | ======================== ========= ========= ========== ========= |
| 125 | |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 126 | .. note:: |
| 127 | |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 128 | Which connections succeed will vary depending on the version of |
| 129 | OpenSSL. For instance, in some older versions of OpenSSL (such |
| 130 | as 0.9.7l on OS X 10.4), an SSLv2 client could not connect to an |
| 131 | SSLv23 server. Another example: beginning with OpenSSL 1.0.0, |
| 132 | an SSLv23 client will not actually attempt SSLv2 connections |
| 133 | unless you explicitly enable SSLv2 ciphers; for example, you |
| 134 | might specify ``"ALL"`` or ``"SSLv2"`` as the *ciphers* parameter |
| 135 | to enable them. |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 136 | |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 137 | The *ciphers* parameter sets the available ciphers for this SSL object. |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 138 | It should be a string in the `OpenSSL cipher list format |
| 139 | <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 140 | |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 141 | The parameter ``do_handshake_on_connect`` specifies whether to do the SSL |
| 142 | handshake automatically after doing a :meth:`socket.connect`, or whether the |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 143 | application program will call it explicitly, by invoking the |
| 144 | :meth:`SSLSocket.do_handshake` method. Calling |
| 145 | :meth:`SSLSocket.do_handshake` explicitly gives the program control over the |
| 146 | blocking behavior of the socket I/O involved in the handshake. |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 147 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 148 | The parameter ``suppress_ragged_eofs`` specifies how the |
Antoine Pitrou | dab6426 | 2010-09-19 13:31:06 +0000 | [diff] [blame] | 149 | :meth:`SSLSocket.recv` method should signal unexpected EOF from the other end |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 150 | of the connection. If specified as :const:`True` (the default), it returns a |
Antoine Pitrou | dab6426 | 2010-09-19 13:31:06 +0000 | [diff] [blame] | 151 | normal EOF (an empty bytes object) in response to unexpected EOF errors |
| 152 | raised from the underlying socket; if :const:`False`, it will raise the |
| 153 | exceptions back to the caller. |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 154 | |
Ezio Melotti | 4d5195b | 2010-04-20 10:57:44 +0000 | [diff] [blame] | 155 | .. versionchanged:: 3.2 |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 156 | New optional argument *ciphers*. |
| 157 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 158 | Random generation |
| 159 | ^^^^^^^^^^^^^^^^^ |
| 160 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 161 | .. function:: RAND_status() |
| 162 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 163 | Returns True if the SSL pseudo-random number generator has been seeded with |
| 164 | 'enough' randomness, and False otherwise. You can use :func:`ssl.RAND_egd` |
| 165 | and :func:`ssl.RAND_add` to increase the randomness of the pseudo-random |
| 166 | number generator. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 167 | |
| 168 | .. function:: RAND_egd(path) |
| 169 | |
| 170 | If you are running an entropy-gathering daemon (EGD) somewhere, and ``path`` |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 171 | is the pathname of a socket connection open to it, this will read 256 bytes |
| 172 | of randomness from the socket, and add it to the SSL pseudo-random number |
| 173 | generator to increase the security of generated secret keys. This is |
| 174 | typically only necessary on systems without better sources of randomness. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 175 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 176 | See http://egd.sourceforge.net/ or http://prngd.sourceforge.net/ for sources |
| 177 | of entropy-gathering daemons. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 178 | |
| 179 | .. function:: RAND_add(bytes, entropy) |
| 180 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 181 | Mixes the given ``bytes`` into the SSL pseudo-random number generator. The |
| 182 | parameter ``entropy`` (a float) is a lower bound on the entropy contained in |
| 183 | string (so you can always use :const:`0.0`). See :rfc:`1750` for more |
| 184 | information on sources of entropy. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 185 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 186 | Certificate handling |
| 187 | ^^^^^^^^^^^^^^^^^^^^ |
| 188 | |
| 189 | .. function:: match_hostname(cert, hostname) |
| 190 | |
| 191 | Verify that *cert* (in decoded format as returned by |
| 192 | :meth:`SSLSocket.getpeercert`) matches the given *hostname*. The rules |
| 193 | applied are those for checking the identity of HTTPS servers as outlined |
| 194 | in :rfc:`2818`, except that IP addresses are not currently supported. |
| 195 | In addition to HTTPS, this function should be suitable for checking the |
| 196 | identity of servers in various SSL-based protocols such as FTPS, IMAPS, |
| 197 | POPS and others. |
| 198 | |
| 199 | :exc:`CertificateError` is raised on failure. On success, the function |
| 200 | returns nothing:: |
| 201 | |
| 202 | >>> cert = {'subject': ((('commonName', 'example.com'),),)} |
| 203 | >>> ssl.match_hostname(cert, "example.com") |
| 204 | >>> ssl.match_hostname(cert, "example.org") |
| 205 | Traceback (most recent call last): |
| 206 | File "<stdin>", line 1, in <module> |
| 207 | File "/home/py3k/Lib/ssl.py", line 130, in match_hostname |
| 208 | ssl.CertificateError: hostname 'example.org' doesn't match 'example.com' |
| 209 | |
| 210 | .. versionadded:: 3.2 |
| 211 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 212 | .. function:: cert_time_to_seconds(timestring) |
| 213 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 214 | Returns a floating-point value containing a normal seconds-after-the-epoch |
| 215 | time value, given the time-string representing the "notBefore" or "notAfter" |
| 216 | date from a certificate. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 217 | |
| 218 | Here's an example:: |
| 219 | |
| 220 | >>> import ssl |
| 221 | >>> ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT") |
| 222 | 1178694000.0 |
| 223 | >>> import time |
| 224 | >>> time.ctime(ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT")) |
| 225 | 'Wed May 9 00:00:00 2007' |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 226 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 227 | .. function:: get_server_certificate(addr, ssl_version=PROTOCOL_SSLv3, ca_certs=None) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 228 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 229 | Given the address ``addr`` of an SSL-protected server, as a (*hostname*, |
| 230 | *port-number*) pair, fetches the server's certificate, and returns it as a |
| 231 | PEM-encoded string. If ``ssl_version`` is specified, uses that version of |
| 232 | the SSL protocol to attempt to connect to the server. If ``ca_certs`` is |
| 233 | specified, it should be a file containing a list of root certificates, the |
| 234 | same format as used for the same parameter in :func:`wrap_socket`. The call |
| 235 | will attempt to validate the server certificate against that set of root |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 236 | certificates, and will fail if the validation attempt fails. |
| 237 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 238 | .. function:: DER_cert_to_PEM_cert(DER_cert_bytes) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 239 | |
| 240 | Given a certificate as a DER-encoded blob of bytes, returns a PEM-encoded |
| 241 | string version of the same certificate. |
| 242 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 243 | .. function:: PEM_cert_to_DER_cert(PEM_cert_string) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 244 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 245 | Given a certificate as an ASCII PEM string, returns a DER-encoded sequence of |
| 246 | bytes for that same certificate. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 247 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 248 | Constants |
| 249 | ^^^^^^^^^ |
| 250 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 251 | .. data:: CERT_NONE |
| 252 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 253 | Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs`` |
| 254 | parameter to :func:`wrap_socket`. In this mode (the default), no |
| 255 | certificates will be required from the other side of the socket connection. |
| 256 | If a certificate is received from the other end, no attempt to validate it |
| 257 | is made. |
| 258 | |
| 259 | See the discussion of :ref:`ssl-security` below. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 260 | |
| 261 | .. data:: CERT_OPTIONAL |
| 262 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 263 | Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs`` |
| 264 | parameter to :func:`wrap_socket`. In this mode no certificates will be |
| 265 | required from the other side of the socket connection; but if they |
| 266 | are provided, validation will be attempted and an :class:`SSLError` |
| 267 | will be raised on failure. |
| 268 | |
| 269 | Use of this setting requires a valid set of CA certificates to |
| 270 | be passed, either to :meth:`SSLContext.load_verify_locations` or as a |
| 271 | value of the ``ca_certs`` parameter to :func:`wrap_socket`. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 272 | |
| 273 | .. data:: CERT_REQUIRED |
| 274 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 275 | Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs`` |
| 276 | parameter to :func:`wrap_socket`. In this mode, certificates are |
| 277 | required from the other side of the socket connection; an :class:`SSLError` |
| 278 | will be raised if no certificate is provided, or if its validation fails. |
| 279 | |
| 280 | Use of this setting requires a valid set of CA certificates to |
| 281 | be passed, either to :meth:`SSLContext.load_verify_locations` or as a |
| 282 | value of the ``ca_certs`` parameter to :func:`wrap_socket`. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 283 | |
| 284 | .. data:: PROTOCOL_SSLv2 |
| 285 | |
| 286 | Selects SSL version 2 as the channel encryption protocol. |
| 287 | |
Antoine Pitrou | 8eac60d | 2010-05-16 14:19:41 +0000 | [diff] [blame] | 288 | .. warning:: |
| 289 | |
| 290 | SSL version 2 is insecure. Its use is highly discouraged. |
| 291 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 292 | .. data:: PROTOCOL_SSLv23 |
| 293 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 294 | Selects SSL version 2 or 3 as the channel encryption protocol. This is a |
| 295 | setting to use with servers for maximum compatibility with the other end of |
| 296 | an SSL connection, but it may cause the specific ciphers chosen for the |
| 297 | encryption to be of fairly low quality. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 298 | |
| 299 | .. data:: PROTOCOL_SSLv3 |
| 300 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 301 | Selects SSL version 3 as the channel encryption protocol. For clients, this |
| 302 | is the maximally compatible SSL variant. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 303 | |
| 304 | .. data:: PROTOCOL_TLSv1 |
| 305 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 306 | Selects TLS version 1 as the channel encryption protocol. This is the most |
| 307 | modern version, and probably the best choice for maximum protection, if both |
| 308 | sides can speak it. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 310 | .. data:: OP_ALL |
| 311 | |
| 312 | Enables workarounds for various bugs present in other SSL implementations. |
| 313 | This option is set by default. |
| 314 | |
| 315 | .. versionadded:: 3.2 |
| 316 | |
| 317 | .. data:: OP_NO_SSLv2 |
| 318 | |
| 319 | Prevents an SSLv2 connection. This option is only applicable in |
| 320 | conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from |
| 321 | choosing SSLv2 as the protocol version. |
| 322 | |
| 323 | .. versionadded:: 3.2 |
| 324 | |
| 325 | .. data:: OP_NO_SSLv3 |
| 326 | |
| 327 | Prevents an SSLv3 connection. This option is only applicable in |
| 328 | conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from |
| 329 | choosing SSLv3 as the protocol version. |
| 330 | |
| 331 | .. versionadded:: 3.2 |
| 332 | |
| 333 | .. data:: OP_NO_TLSv1 |
| 334 | |
| 335 | Prevents a TLSv1 connection. This option is only applicable in |
| 336 | conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from |
| 337 | choosing TLSv1 as the protocol version. |
| 338 | |
| 339 | .. versionadded:: 3.2 |
| 340 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 341 | .. data:: HAS_SNI |
| 342 | |
| 343 | Whether the OpenSSL library has built-in support for the *Server Name |
| 344 | Indication* extension to the SSLv3 and TLSv1 protocols (as defined in |
| 345 | :rfc:`4366`). When true, you can use the *server_hostname* argument to |
| 346 | :meth:`SSLContext.wrap_socket`. |
| 347 | |
| 348 | .. versionadded:: 3.2 |
| 349 | |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 350 | .. data:: OPENSSL_VERSION |
| 351 | |
| 352 | The version string of the OpenSSL library loaded by the interpreter:: |
| 353 | |
| 354 | >>> ssl.OPENSSL_VERSION |
| 355 | 'OpenSSL 0.9.8k 25 Mar 2009' |
| 356 | |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 357 | .. versionadded:: 3.2 |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 358 | |
| 359 | .. data:: OPENSSL_VERSION_INFO |
| 360 | |
| 361 | A tuple of five integers representing version information about the |
| 362 | OpenSSL library:: |
| 363 | |
| 364 | >>> ssl.OPENSSL_VERSION_INFO |
| 365 | (0, 9, 8, 11, 15) |
| 366 | |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 367 | .. versionadded:: 3.2 |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 368 | |
| 369 | .. data:: OPENSSL_VERSION_NUMBER |
| 370 | |
| 371 | The raw version number of the OpenSSL library, as a single integer:: |
| 372 | |
| 373 | >>> ssl.OPENSSL_VERSION_NUMBER |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 374 | 9470143 |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 375 | >>> hex(ssl.OPENSSL_VERSION_NUMBER) |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 376 | '0x9080bf' |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 377 | |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 378 | .. versionadded:: 3.2 |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 379 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 380 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 381 | SSL Sockets |
| 382 | ----------- |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 383 | |
Antoine Pitrou | e1f2f30 | 2010-09-19 13:56:11 +0000 | [diff] [blame] | 384 | SSL sockets provide the following methods of :ref:`socket-objects`: |
Antoine Pitrou | 792ff3e | 2010-09-19 13:19:21 +0000 | [diff] [blame] | 385 | |
Antoine Pitrou | e1f2f30 | 2010-09-19 13:56:11 +0000 | [diff] [blame] | 386 | - :meth:`~socket.socket.accept()` |
| 387 | - :meth:`~socket.socket.bind()` |
| 388 | - :meth:`~socket.socket.close()` |
| 389 | - :meth:`~socket.socket.connect()` |
| 390 | - :meth:`~socket.socket.detach()` |
| 391 | - :meth:`~socket.socket.fileno()` |
| 392 | - :meth:`~socket.socket.getpeername()`, :meth:`~socket.socket.getsockname()` |
| 393 | - :meth:`~socket.socket.getsockopt()`, :meth:`~socket.socket.setsockopt()` |
| 394 | - :meth:`~socket.socket.gettimeout()`, :meth:`~socket.socket.settimeout()`, |
| 395 | :meth:`~socket.socket.setblocking()` |
| 396 | - :meth:`~socket.socket.listen()` |
| 397 | - :meth:`~socket.socket.makefile()` |
| 398 | - :meth:`~socket.socket.recv()`, :meth:`~socket.socket.recv_into()` |
| 399 | (but passing a non-zero ``flags`` argument is not allowed) |
| 400 | - :meth:`~socket.socket.send()`, :meth:`~socket.socket.sendall()` (with |
| 401 | the same limitation) |
| 402 | - :meth:`~socket.socket.shutdown()` |
| 403 | |
| 404 | They also have the following additional methods and attributes: |
Antoine Pitrou | 792ff3e | 2010-09-19 13:19:21 +0000 | [diff] [blame] | 405 | |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 406 | .. method:: SSLSocket.do_handshake() |
| 407 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 408 | Performs the SSL setup handshake. If the socket is non-blocking, this method |
| 409 | may raise :exc:`SSLError` with the value of the exception instance's |
| 410 | ``args[0]`` being either :const:`SSL_ERROR_WANT_READ` or |
| 411 | :const:`SSL_ERROR_WANT_WRITE`, and should be called again until it stops |
| 412 | raising those exceptions. Here's an example of how to do that:: |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 413 | |
| 414 | while True: |
| 415 | try: |
| 416 | sock.do_handshake() |
| 417 | break |
| 418 | except ssl.SSLError as err: |
| 419 | if err.args[0] == ssl.SSL_ERROR_WANT_READ: |
| 420 | select.select([sock], [], []) |
| 421 | elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE: |
| 422 | select.select([], [sock], []) |
| 423 | else: |
| 424 | raise |
| 425 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 426 | .. method:: SSLSocket.getpeercert(binary_form=False) |
| 427 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 428 | If there is no certificate for the peer on the other end of the connection, |
| 429 | returns ``None``. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 430 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 431 | If the parameter ``binary_form`` is :const:`False`, and a certificate was |
| 432 | received from the peer, this method returns a :class:`dict` instance. If the |
| 433 | certificate was not validated, the dict is empty. If the certificate was |
| 434 | validated, it returns a dict with the keys ``subject`` (the principal for |
| 435 | which the certificate was issued), and ``notAfter`` (the time after which the |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 436 | certificate should not be trusted). If a certificate contains an instance |
| 437 | of the *Subject Alternative Name* extension (see :rfc:`3280`), there will |
| 438 | also be a ``subjectAltName`` key in the dictionary. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 439 | |
| 440 | The "subject" field is a tuple containing the sequence of relative |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 441 | distinguished names (RDNs) given in the certificate's data structure for the |
| 442 | principal, and each RDN is a sequence of name-value pairs:: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 443 | |
| 444 | {'notAfter': 'Feb 16 16:54:50 2013 GMT', |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 445 | 'subject': ((('countryName', 'US'),), |
| 446 | (('stateOrProvinceName', 'Delaware'),), |
| 447 | (('localityName', 'Wilmington'),), |
| 448 | (('organizationName', 'Python Software Foundation'),), |
| 449 | (('organizationalUnitName', 'SSL'),), |
| 450 | (('commonName', 'somemachine.python.org'),))} |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 451 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 452 | If the ``binary_form`` parameter is :const:`True`, and a certificate was |
| 453 | provided, this method returns the DER-encoded form of the entire certificate |
| 454 | as a sequence of bytes, or :const:`None` if the peer did not provide a |
| 455 | certificate. This return value is independent of validation; if validation |
| 456 | was required (:const:`CERT_OPTIONAL` or :const:`CERT_REQUIRED`), it will have |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 457 | been validated, but if :const:`CERT_NONE` was used to establish the |
| 458 | connection, the certificate, if present, will not have been validated. |
| 459 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 460 | .. versionchanged:: 3.2 |
| 461 | The returned dictionary includes additional items such as ``issuer`` |
| 462 | and ``notBefore``. |
| 463 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 464 | .. method:: SSLSocket.cipher() |
| 465 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 466 | Returns a three-value tuple containing the name of the cipher being used, the |
| 467 | version of the SSL protocol that defines its use, and the number of secret |
| 468 | bits being used. If no connection has been established, returns ``None``. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 469 | |
| 470 | |
Benjamin Peterson | 4aeec04 | 2008-08-19 21:42:13 +0000 | [diff] [blame] | 471 | .. method:: SSLSocket.unwrap() |
| 472 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 473 | Performs the SSL shutdown handshake, which removes the TLS layer from the |
| 474 | underlying socket, and returns the underlying socket object. This can be |
| 475 | used to go from encrypted operation over a connection to unencrypted. The |
| 476 | returned socket should always be used for further communication with the |
| 477 | other side of the connection, rather than the original socket. |
Benjamin Peterson | 4aeec04 | 2008-08-19 21:42:13 +0000 | [diff] [blame] | 478 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 479 | |
Antoine Pitrou | ec883db | 2010-05-24 21:20:20 +0000 | [diff] [blame] | 480 | .. attribute:: SSLSocket.context |
| 481 | |
| 482 | The :class:`SSLContext` object this SSL socket is tied to. If the SSL |
| 483 | socket was created using the top-level :func:`wrap_socket` function |
| 484 | (rather than :meth:`SSLContext.wrap_socket`), this is a custom context |
| 485 | object created for this SSL socket. |
| 486 | |
| 487 | .. versionadded:: 3.2 |
| 488 | |
| 489 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 490 | SSL Contexts |
| 491 | ------------ |
| 492 | |
Antoine Pitrou | cafaad4 | 2010-05-24 15:58:43 +0000 | [diff] [blame] | 493 | .. versionadded:: 3.2 |
| 494 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 495 | An SSL context holds various data longer-lived than single SSL connections, |
| 496 | such as SSL configuration options, certificate(s) and private key(s). |
| 497 | It also manages a cache of SSL sessions for server-side sockets, in order |
| 498 | to speed up repeated connections from the same clients. |
| 499 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 500 | .. class:: SSLContext(protocol) |
| 501 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 502 | Create a new SSL context. You must pass *protocol* which must be one |
| 503 | of the ``PROTOCOL_*`` constants defined in this module. |
| 504 | :data:`PROTOCOL_SSLv23` is recommended for maximum interoperability. |
| 505 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 506 | |
| 507 | :class:`SSLContext` objects have the following methods and attributes: |
| 508 | |
| 509 | .. method:: SSLContext.load_cert_chain(certfile, keyfile=None) |
| 510 | |
| 511 | Load a private key and the corresponding certificate. The *certfile* |
| 512 | string must be the path to a single file in PEM format containing the |
| 513 | certificate as well as any number of CA certificates needed to establish |
| 514 | the certificate's authenticity. The *keyfile* string, if present, must |
| 515 | point to a file containing the private key in. Otherwise the private |
| 516 | key will be taken from *certfile* as well. See the discussion of |
| 517 | :ref:`ssl-certificates` for more information on how the certificate |
| 518 | is stored in the *certfile*. |
| 519 | |
| 520 | An :class:`SSLError` is raised if the private key doesn't |
| 521 | match with the certificate. |
| 522 | |
| 523 | .. method:: SSLContext.load_verify_locations(cafile=None, capath=None) |
| 524 | |
| 525 | Load a set of "certification authority" (CA) certificates used to validate |
| 526 | other peers' certificates when :data:`verify_mode` is other than |
| 527 | :data:`CERT_NONE`. At least one of *cafile* or *capath* must be specified. |
| 528 | |
| 529 | The *cafile* string, if present, is the path to a file of concatenated |
| 530 | CA certificates in PEM format. See the discussion of |
| 531 | :ref:`ssl-certificates` for more information about how to arrange the |
| 532 | certificates in this file. |
| 533 | |
| 534 | The *capath* string, if present, is |
| 535 | the path to a directory containing several CA certificates in PEM format, |
| 536 | following an `OpenSSL specific layout |
| 537 | <http://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html>`_. |
| 538 | |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 539 | .. method:: SSLContext.set_default_verify_paths() |
| 540 | |
| 541 | Load a set of default "certification authority" (CA) certificates from |
| 542 | a filesystem path defined when building the OpenSSL library. Unfortunately, |
| 543 | there's no easy way to know whether this method succeeds: no error is |
| 544 | returned if no certificates are to be found. When the OpenSSL library is |
| 545 | provided as part of the operating system, though, it is likely to be |
| 546 | configured properly. |
| 547 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 548 | .. method:: SSLContext.set_ciphers(ciphers) |
| 549 | |
| 550 | Set the available ciphers for sockets created with this context. |
| 551 | It should be a string in the `OpenSSL cipher list format |
| 552 | <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_. |
| 553 | If no cipher can be selected (because compile-time options or other |
| 554 | configuration forbids use of all the specified ciphers), an |
| 555 | :class:`SSLError` will be raised. |
| 556 | |
| 557 | .. note:: |
| 558 | when connected, the :meth:`SSLSocket.cipher` method of SSL sockets will |
| 559 | give the currently selected cipher. |
| 560 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 561 | .. method:: SSLContext.wrap_socket(sock, server_side=False, \ |
| 562 | do_handshake_on_connect=True, suppress_ragged_eofs=True, \ |
| 563 | server_hostname=None) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 564 | |
| 565 | Wrap an existing Python socket *sock* and return an :class:`SSLSocket` |
| 566 | object. The SSL socket is tied to the context, its settings and |
| 567 | certificates. The parameters *server_side*, *do_handshake_on_connect* |
| 568 | and *suppress_ragged_eofs* have the same meaning as in the top-level |
| 569 | :func:`wrap_socket` function. |
| 570 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 571 | On client connections, the optional parameter *server_hostname* specifies |
| 572 | the hostname of the service which we are connecting to. This allows a |
| 573 | single server to host multiple SSL-based services with distinct certificates, |
| 574 | quite similarly to HTTP virtual hosts. Specifying *server_hostname* |
| 575 | will raise a :exc:`ValueError` if the OpenSSL library doesn't have support |
| 576 | for it (that is, if :data:`HAS_SNI` is :const:`False`). Specifying |
| 577 | *server_hostname* will also raise a :exc:`ValueError` if *server_side* |
| 578 | is true. |
| 579 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 580 | .. method:: SSLContext.session_stats() |
| 581 | |
| 582 | Get statistics about the SSL sessions created or managed by this context. |
| 583 | A dictionary is returned which maps the names of each `piece of information |
| 584 | <http://www.openssl.org/docs/ssl/SSL_CTX_sess_number.html>`_ to their |
| 585 | numeric values. For example, here is the total number of hits and misses |
| 586 | in the session cache since the context was created:: |
| 587 | |
| 588 | >>> stats = context.session_stats() |
| 589 | >>> stats['hits'], stats['misses'] |
| 590 | (0, 0) |
| 591 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 592 | .. attribute:: SSLContext.options |
| 593 | |
| 594 | An integer representing the set of SSL options enabled on this context. |
| 595 | The default value is :data:`OP_ALL`, but you can specify other options |
| 596 | such as :data:`OP_NO_SSLv2` by ORing them together. |
| 597 | |
| 598 | .. note:: |
| 599 | With versions of OpenSSL older than 0.9.8m, it is only possible |
| 600 | to set options, not to clear them. Attempting to clear an option |
| 601 | (by resetting the corresponding bits) will raise a ``ValueError``. |
| 602 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 603 | .. attribute:: SSLContext.protocol |
| 604 | |
| 605 | The protocol version chosen when constructing the context. This attribute |
| 606 | is read-only. |
| 607 | |
| 608 | .. attribute:: SSLContext.verify_mode |
| 609 | |
| 610 | Whether to try to verify other peers' certificates and how to behave |
| 611 | if verification fails. This attribute must be one of |
| 612 | :data:`CERT_NONE`, :data:`CERT_OPTIONAL` or :data:`CERT_REQUIRED`. |
| 613 | |
| 614 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 615 | .. index:: single: certificates |
| 616 | |
| 617 | .. index:: single: X509 certificate |
| 618 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 619 | .. _ssl-certificates: |
| 620 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 621 | Certificates |
| 622 | ------------ |
| 623 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 624 | Certificates in general are part of a public-key / private-key system. In this |
| 625 | system, each *principal*, (which may be a machine, or a person, or an |
| 626 | organization) is assigned a unique two-part encryption key. One part of the key |
| 627 | is public, and is called the *public key*; the other part is kept secret, and is |
| 628 | called the *private key*. The two parts are related, in that if you encrypt a |
| 629 | message with one of the parts, you can decrypt it with the other part, and |
| 630 | **only** with the other part. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 631 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 632 | A certificate contains information about two principals. It contains the name |
| 633 | of a *subject*, and the subject's public key. It also contains a statement by a |
| 634 | second principal, the *issuer*, that the subject is who he claims to be, and |
| 635 | that this is indeed the subject's public key. The issuer's statement is signed |
| 636 | with the issuer's private key, which only the issuer knows. However, anyone can |
| 637 | verify the issuer's statement by finding the issuer's public key, decrypting the |
| 638 | statement with it, and comparing it to the other information in the certificate. |
| 639 | The certificate also contains information about the time period over which it is |
| 640 | valid. This is expressed as two fields, called "notBefore" and "notAfter". |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 641 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 642 | In the Python use of certificates, a client or server can use a certificate to |
| 643 | prove who they are. The other side of a network connection can also be required |
| 644 | to produce a certificate, and that certificate can be validated to the |
| 645 | satisfaction of the client or server that requires such validation. The |
| 646 | connection attempt can be set to raise an exception if the validation fails. |
| 647 | Validation is done automatically, by the underlying OpenSSL framework; the |
| 648 | application need not concern itself with its mechanics. But the application |
| 649 | does usually need to provide sets of certificates to allow this process to take |
| 650 | place. |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 651 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 652 | Python uses files to contain certificates. They should be formatted as "PEM" |
| 653 | (see :rfc:`1422`), which is a base-64 encoded form wrapped with a header line |
| 654 | and a footer line:: |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 655 | |
| 656 | -----BEGIN CERTIFICATE----- |
| 657 | ... (certificate in base64 PEM encoding) ... |
| 658 | -----END CERTIFICATE----- |
| 659 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 660 | Certificate chains |
| 661 | ^^^^^^^^^^^^^^^^^^ |
| 662 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 663 | The Python files which contain certificates can contain a sequence of |
| 664 | certificates, sometimes called a *certificate chain*. This chain should start |
| 665 | with the specific certificate for the principal who "is" the client or server, |
| 666 | and then the certificate for the issuer of that certificate, and then the |
| 667 | certificate for the issuer of *that* certificate, and so on up the chain till |
| 668 | you get to a certificate which is *self-signed*, that is, a certificate which |
| 669 | has the same subject and issuer, sometimes called a *root certificate*. The |
| 670 | certificates should just be concatenated together in the certificate file. For |
| 671 | example, suppose we had a three certificate chain, from our server certificate |
| 672 | to the certificate of the certification authority that signed our server |
| 673 | certificate, to the root certificate of the agency which issued the |
| 674 | certification authority's certificate:: |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 675 | |
| 676 | -----BEGIN CERTIFICATE----- |
| 677 | ... (certificate for your server)... |
| 678 | -----END CERTIFICATE----- |
| 679 | -----BEGIN CERTIFICATE----- |
| 680 | ... (the certificate for the CA)... |
| 681 | -----END CERTIFICATE----- |
| 682 | -----BEGIN CERTIFICATE----- |
| 683 | ... (the root certificate for the CA's issuer)... |
| 684 | -----END CERTIFICATE----- |
| 685 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 686 | CA certificates |
| 687 | ^^^^^^^^^^^^^^^ |
| 688 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 689 | If you are going to require validation of the other side of the connection's |
| 690 | certificate, you need to provide a "CA certs" file, filled with the certificate |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 691 | chains for each issuer you are willing to trust. Again, this file just contains |
| 692 | these chains concatenated together. For validation, Python will use the first |
| 693 | chain it finds in the file which matches. Some "standard" root certificates are |
| 694 | available from various certification authorities: `CACert.org |
| 695 | <http://www.cacert.org/index.php?id=3>`_, `Thawte |
| 696 | <http://www.thawte.com/roots/>`_, `Verisign |
| 697 | <http://www.verisign.com/support/roots.html>`_, `Positive SSL |
| 698 | <http://www.PositiveSSL.com/ssl-certificate-support/cert_installation/UTN-USERFirst-Hardware.crt>`_ |
| 699 | (used by python.org), `Equifax and GeoTrust |
| 700 | <http://www.geotrust.com/resources/root_certificates/index.asp>`_. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 701 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 702 | In general, if you are using SSL3 or TLS1, you don't need to put the full chain |
| 703 | in your "CA certs" file; you only need the root certificates, and the remote |
| 704 | peer is supposed to furnish the other certificates necessary to chain from its |
| 705 | certificate to a root certificate. See :rfc:`4158` for more discussion of the |
| 706 | way in which certification chains can be built. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 707 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 708 | Combined key and certificate |
| 709 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 710 | |
| 711 | Often the private key is stored in the same file as the certificate; in this |
| 712 | case, only the ``certfile`` parameter to :meth:`SSLContext.load_cert_chain` |
| 713 | and :func:`wrap_socket` needs to be passed. If the private key is stored |
| 714 | with the certificate, it should come before the first certificate in |
| 715 | the certificate chain:: |
| 716 | |
| 717 | -----BEGIN RSA PRIVATE KEY----- |
| 718 | ... (private key in base64 encoding) ... |
| 719 | -----END RSA PRIVATE KEY----- |
| 720 | -----BEGIN CERTIFICATE----- |
| 721 | ... (certificate in base64 PEM encoding) ... |
| 722 | -----END CERTIFICATE----- |
| 723 | |
| 724 | Self-signed certificates |
| 725 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 726 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 727 | If you are going to create a server that provides SSL-encrypted connection |
| 728 | services, you will need to acquire a certificate for that service. There are |
| 729 | many ways of acquiring appropriate certificates, such as buying one from a |
| 730 | certification authority. Another common practice is to generate a self-signed |
| 731 | certificate. The simplest way to do this is with the OpenSSL package, using |
| 732 | something like the following:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 733 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 734 | % openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem |
| 735 | Generating a 1024 bit RSA private key |
| 736 | .......++++++ |
| 737 | .............................++++++ |
| 738 | writing new private key to 'cert.pem' |
| 739 | ----- |
| 740 | You are about to be asked to enter information that will be incorporated |
| 741 | into your certificate request. |
| 742 | What you are about to enter is what is called a Distinguished Name or a DN. |
| 743 | There are quite a few fields but you can leave some blank |
| 744 | For some fields there will be a default value, |
| 745 | If you enter '.', the field will be left blank. |
| 746 | ----- |
| 747 | Country Name (2 letter code) [AU]:US |
| 748 | State or Province Name (full name) [Some-State]:MyState |
| 749 | Locality Name (eg, city) []:Some City |
| 750 | Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Organization, Inc. |
| 751 | Organizational Unit Name (eg, section) []:My Group |
| 752 | Common Name (eg, YOUR name) []:myserver.mygroup.myorganization.com |
| 753 | Email Address []:ops@myserver.mygroup.myorganization.com |
| 754 | % |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 755 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 756 | The disadvantage of a self-signed certificate is that it is its own root |
| 757 | certificate, and no one else will have it in their cache of known (and trusted) |
| 758 | root certificates. |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 759 | |
| 760 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 761 | Examples |
| 762 | -------- |
| 763 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 764 | Testing for SSL support |
| 765 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 766 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 767 | To test for the presence of SSL support in a Python installation, user code |
| 768 | should use the following idiom:: |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 769 | |
| 770 | try: |
Georg Brandl | 8a7e5da | 2011-01-02 19:07:51 +0000 | [diff] [blame] | 771 | import ssl |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 772 | except ImportError: |
Georg Brandl | 8a7e5da | 2011-01-02 19:07:51 +0000 | [diff] [blame] | 773 | pass |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 774 | else: |
Georg Brandl | 8a7e5da | 2011-01-02 19:07:51 +0000 | [diff] [blame] | 775 | ... # do something that requires SSL support |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 776 | |
| 777 | Client-side operation |
| 778 | ^^^^^^^^^^^^^^^^^^^^^ |
| 779 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 780 | This example connects to an SSL server and prints the server's certificate:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 781 | |
| 782 | import socket, ssl, pprint |
| 783 | |
| 784 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 785 | # require a certificate from the server |
| 786 | ssl_sock = ssl.wrap_socket(s, |
| 787 | ca_certs="/etc/ca_certs_file", |
| 788 | cert_reqs=ssl.CERT_REQUIRED) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 789 | ssl_sock.connect(('www.verisign.com', 443)) |
| 790 | |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 791 | pprint.pprint(ssl_sock.getpeercert()) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 792 | # note that closing the SSLSocket will also close the underlying socket |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 793 | ssl_sock.close() |
| 794 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 795 | As of October 6, 2010, the certificate printed by this program looks like |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 796 | this:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 797 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 798 | {'notAfter': 'May 25 23:59:59 2012 GMT', |
| 799 | 'subject': ((('1.3.6.1.4.1.311.60.2.1.3', 'US'),), |
| 800 | (('1.3.6.1.4.1.311.60.2.1.2', 'Delaware'),), |
| 801 | (('businessCategory', 'V1.0, Clause 5.(b)'),), |
| 802 | (('serialNumber', '2497886'),), |
| 803 | (('countryName', 'US'),), |
| 804 | (('postalCode', '94043'),), |
| 805 | (('stateOrProvinceName', 'California'),), |
| 806 | (('localityName', 'Mountain View'),), |
| 807 | (('streetAddress', '487 East Middlefield Road'),), |
| 808 | (('organizationName', 'VeriSign, Inc.'),), |
| 809 | (('organizationalUnitName', ' Production Security Services'),), |
| 810 | (('commonName', 'www.verisign.com'),))} |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 811 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 812 | This other example first creates an SSL context, instructs it to verify |
| 813 | certificates sent by peers, and feeds it a set of recognized certificate |
| 814 | authorities (CA):: |
| 815 | |
| 816 | >>> context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 817 | >>> context.verify_mode = ssl.CERT_REQUIRED |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 818 | >>> context.load_verify_locations("/etc/ssl/certs/ca-bundle.crt") |
| 819 | |
| 820 | (it is assumed your operating system places a bundle of all CA certificates |
| 821 | in ``/etc/ssl/certs/ca-bundle.crt``; if not, you'll get an error and have |
| 822 | to adjust the location) |
| 823 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 824 | When you use the context to connect to a server, :const:`CERT_REQUIRED` |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 825 | validates the server certificate: it ensures that the server certificate |
| 826 | was signed with one of the CA certificates, and checks the signature for |
| 827 | correctness:: |
| 828 | |
| 829 | >>> conn = context.wrap_socket(socket.socket(socket.AF_INET)) |
| 830 | >>> conn.connect(("linuxfr.org", 443)) |
| 831 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 832 | You should then fetch the certificate and check its fields for conformity:: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 833 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 834 | >>> cert = conn.getpeercert() |
| 835 | >>> ssl.match_hostname(cert, "linuxfr.org") |
| 836 | |
| 837 | Visual inspection shows that the certificate does identify the desired service |
| 838 | (that is, the HTTPS host ``linuxfr.org``):: |
| 839 | |
| 840 | >>> pprint.pprint(cert) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 841 | {'notAfter': 'Jun 26 21:41:46 2011 GMT', |
| 842 | 'subject': ((('commonName', 'linuxfr.org'),),), |
| 843 | 'subjectAltName': (('DNS', 'linuxfr.org'), ('othername', '<unsupported>'))} |
| 844 | |
| 845 | Now that you are assured of its authenticity, you can proceed to talk with |
| 846 | the server:: |
| 847 | |
Antoine Pitrou | dab6426 | 2010-09-19 13:31:06 +0000 | [diff] [blame] | 848 | >>> conn.sendall(b"HEAD / HTTP/1.0\r\nHost: linuxfr.org\r\n\r\n") |
| 849 | >>> pprint.pprint(conn.recv(1024).split(b"\r\n")) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 850 | [b'HTTP/1.1 302 Found', |
| 851 | b'Date: Sun, 16 May 2010 13:43:28 GMT', |
| 852 | b'Server: Apache/2.2', |
| 853 | b'Location: https://linuxfr.org/pub/', |
| 854 | b'Vary: Accept-Encoding', |
| 855 | b'Connection: close', |
| 856 | b'Content-Type: text/html; charset=iso-8859-1', |
| 857 | b'', |
| 858 | b''] |
| 859 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 860 | See the discussion of :ref:`ssl-security` below. |
| 861 | |
| 862 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 863 | Server-side operation |
| 864 | ^^^^^^^^^^^^^^^^^^^^^ |
| 865 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 866 | For server operation, typically you'll need to have a server certificate, and |
| 867 | private key, each in a file. You'll first create a context holding the key |
| 868 | and the certificate, so that clients can check your authenticity. Then |
| 869 | you'll open a socket, bind it to a port, call :meth:`listen` on it, and start |
| 870 | waiting for clients to connect:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 871 | |
| 872 | import socket, ssl |
| 873 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 874 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 875 | context.load_cert_chain(certfile="mycertfile", keyfile="mykeyfile") |
| 876 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 877 | bindsocket = socket.socket() |
| 878 | bindsocket.bind(('myaddr.mydomain.com', 10023)) |
| 879 | bindsocket.listen(5) |
| 880 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 881 | When a client connects, you'll call :meth:`accept` on the socket to get the |
| 882 | new socket from the other end, and use the context's :meth:`SSLContext.wrap_socket` |
| 883 | method to create a server-side SSL socket for the connection:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 884 | |
| 885 | while True: |
Georg Brandl | 8a7e5da | 2011-01-02 19:07:51 +0000 | [diff] [blame] | 886 | newsocket, fromaddr = bindsocket.accept() |
| 887 | connstream = context.wrap_socket(newsocket, server_side=True) |
| 888 | try: |
| 889 | deal_with_client(connstream) |
| 890 | finally: |
Antoine Pitrou | b205d58 | 2011-01-02 22:09:27 +0000 | [diff] [blame^] | 891 | connstream.shutdown(socket.SHUT_RDWR) |
Georg Brandl | 8a7e5da | 2011-01-02 19:07:51 +0000 | [diff] [blame] | 892 | connstream.close() |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 893 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 894 | Then you'll read data from the ``connstream`` and do something with it till you |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 895 | are finished with the client (or the client is finished with you):: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 896 | |
| 897 | def deal_with_client(connstream): |
Georg Brandl | 8a7e5da | 2011-01-02 19:07:51 +0000 | [diff] [blame] | 898 | data = connstream.recv(1024) |
| 899 | # empty data means the client is finished with us |
| 900 | while data: |
| 901 | if not do_something(connstream, data): |
| 902 | # we'll assume do_something returns False |
| 903 | # when we're finished with client |
| 904 | break |
| 905 | data = connstream.recv(1024) |
| 906 | # finished with client |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 907 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 908 | And go back to listening for new client connections (of course, a real server |
| 909 | would probably handle each client connection in a separate thread, or put |
| 910 | the sockets in non-blocking mode and use an event loop). |
| 911 | |
| 912 | |
| 913 | .. _ssl-security: |
| 914 | |
| 915 | Security considerations |
| 916 | ----------------------- |
| 917 | |
| 918 | Verifying certificates |
| 919 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 920 | |
| 921 | :const:`CERT_NONE` is the default. Since it does not authenticate the other |
| 922 | peer, it can be insecure, especially in client mode where most of time you |
| 923 | would like to ensure the authenticity of the server you're talking to. |
| 924 | Therefore, when in client mode, it is highly recommended to use |
| 925 | :const:`CERT_REQUIRED`. However, it is in itself not sufficient; you also |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 926 | have to check that the server certificate, which can be obtained by calling |
| 927 | :meth:`SSLSocket.getpeercert`, matches the desired service. For many |
| 928 | protocols and applications, the service can be identified by the hostname; |
| 929 | in this case, the :func:`match_hostname` function can be used. |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 930 | |
| 931 | In server mode, if you want to authenticate your clients using the SSL layer |
| 932 | (rather than using a higher-level authentication mechanism), you'll also have |
| 933 | to specify :const:`CERT_REQUIRED` and similarly check the client certificate. |
| 934 | |
| 935 | .. note:: |
| 936 | |
| 937 | In client mode, :const:`CERT_OPTIONAL` and :const:`CERT_REQUIRED` are |
| 938 | equivalent unless anonymous ciphers are enabled (they are disabled |
| 939 | by default). |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 941 | Protocol versions |
| 942 | ^^^^^^^^^^^^^^^^^ |
| 943 | |
| 944 | SSL version 2 is considered insecure and is therefore dangerous to use. If |
| 945 | you want maximum compatibility between clients and servers, it is recommended |
| 946 | to use :const:`PROTOCOL_SSLv23` as the protocol version and then disable |
| 947 | SSLv2 explicitly using the :data:`SSLContext.options` attribute:: |
| 948 | |
| 949 | context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
| 950 | context.options |= ssl.OP_NO_SSLv2 |
| 951 | |
| 952 | The SSL context created above will allow SSLv3 and TLSv1 connections, but |
| 953 | not SSLv2. |
| 954 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 955 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 956 | .. seealso:: |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 957 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 958 | Class :class:`socket.socket` |
| 959 | Documentation of underlying :mod:`socket` class |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 960 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 961 | `Introducing SSL and Certificates using OpenSSL <http://old.pseudonym.org/ssl/wwwj-index.html>`_ |
| 962 | Frederick J. Hirsch |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 963 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 964 | `RFC 1422: Privacy Enhancement for Internet Electronic Mail: Part II: Certificate-Based Key Management <http://www.ietf.org/rfc/rfc1422>`_ |
| 965 | Steve Kent |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 966 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 967 | `RFC 1750: Randomness Recommendations for Security <http://www.ietf.org/rfc/rfc1750>`_ |
| 968 | D. Eastlake et. al. |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 969 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 970 | `RFC 3280: Internet X.509 Public Key Infrastructure Certificate and CRL Profile <http://www.ietf.org/rfc/rfc3280>`_ |
| 971 | Housley et. al. |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 972 | |
| 973 | `RFC 4366: Transport Layer Security (TLS) Extensions <http://www.ietf.org/rfc/rfc4366>`_ |
| 974 | Blake-Wilson et. al. |