Antoine Pitrou | e1bc898 | 2011-01-02 22:12:22 +0000 | [diff] [blame] | 1 | :mod:`ssl` --- TLS/SSL wrapper for socket objects |
| 2 | ================================================= |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: ssl |
Antoine Pitrou | e1bc898 | 2011-01-02 22:12:22 +0000 | [diff] [blame] | 5 | :synopsis: TLS/SSL wrapper for socket objects |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 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 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 15 | **Source code:** :source:`Lib/ssl.py` |
| 16 | |
| 17 | -------------- |
| 18 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 19 | This module provides access to Transport Layer Security (often known as "Secure |
| 20 | Sockets Layer") encryption and peer authentication facilities for network |
| 21 | sockets, both client-side and server-side. This module uses the OpenSSL |
| 22 | library. It is available on all modern Unix systems, Windows, Mac OS X, and |
| 23 | probably additional platforms, as long as OpenSSL is installed on that platform. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 24 | |
| 25 | .. note:: |
| 26 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 27 | Some behavior may be platform dependent, since calls are made to the |
| 28 | operating system socket APIs. The installed version of OpenSSL may also |
| 29 | cause variations in behavior. |
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 section documents the objects and functions in the ``ssl`` module; for more |
| 32 | general information about TLS, SSL, and certificates, the reader is referred to |
| 33 | the documents in the "See Also" section at the bottom. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 34 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 35 | This module provides a class, :class:`ssl.SSLSocket`, which is derived from the |
| 36 | :class:`socket.socket` type, and provides a socket-like wrapper that also |
| 37 | 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] | 38 | additional methods such as :meth:`getpeercert`, which retrieves the |
| 39 | certificate of the other side of the connection, and :meth:`cipher`,which |
| 40 | retrieves the cipher being used for the secure connection. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 41 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 42 | For more sophisticated applications, the :class:`ssl.SSLContext` class |
| 43 | helps manage settings and certificates, which can then be inherited |
| 44 | by SSL sockets created through the :meth:`SSLContext.wrap_socket` method. |
| 45 | |
| 46 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 47 | Functions, Constants, and Exceptions |
| 48 | ------------------------------------ |
| 49 | |
| 50 | .. exception:: SSLError |
| 51 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 52 | Raised to signal an error from the underlying SSL implementation |
| 53 | (currently provided by the OpenSSL library). This signifies some |
| 54 | problem in the higher-level encryption and authentication layer that's |
| 55 | superimposed on the underlying network connection. This error |
Antoine Pitrou | 5574c30 | 2011-10-12 17:53:43 +0200 | [diff] [blame] | 56 | is a subtype of :exc:`OSError`. The error code and message of |
| 57 | :exc:`SSLError` instances are provided by the OpenSSL library. |
| 58 | |
| 59 | .. versionchanged:: 3.3 |
| 60 | :exc:`SSLError` used to be a subtype of :exc:`socket.error`. |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 61 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 62 | .. attribute:: library |
| 63 | |
| 64 | A string mnemonic designating the OpenSSL submodule in which the error |
| 65 | occurred, such as ``SSL``, ``PEM`` or ``X509``. The range of possible |
| 66 | values depends on the OpenSSL version. |
| 67 | |
| 68 | .. versionadded:: 3.3 |
| 69 | |
| 70 | .. attribute:: reason |
| 71 | |
| 72 | A string mnemonic designating the reason this error occurred, for |
| 73 | example ``CERTIFICATE_VERIFY_FAILED``. The range of possible |
| 74 | values depends on the OpenSSL version. |
| 75 | |
| 76 | .. versionadded:: 3.3 |
| 77 | |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 78 | .. exception:: SSLZeroReturnError |
| 79 | |
| 80 | A subclass of :exc:`SSLError` raised when trying to read or write and |
| 81 | the SSL connection has been closed cleanly. Note that this doesn't |
| 82 | mean that the underlying transport (read TCP) has been closed. |
| 83 | |
| 84 | .. versionadded:: 3.3 |
| 85 | |
| 86 | .. exception:: SSLWantReadError |
| 87 | |
| 88 | A subclass of :exc:`SSLError` raised by a :ref:`non-blocking SSL socket |
| 89 | <ssl-nonblocking>` when trying to read or write data, but more data needs |
| 90 | to be received on the underlying TCP transport before the request can be |
| 91 | fulfilled. |
| 92 | |
| 93 | .. versionadded:: 3.3 |
| 94 | |
| 95 | .. exception:: SSLWantWriteError |
| 96 | |
| 97 | A subclass of :exc:`SSLError` raised by a :ref:`non-blocking SSL socket |
| 98 | <ssl-nonblocking>` when trying to read or write data, but more data needs |
| 99 | to be sent on the underlying TCP transport before the request can be |
| 100 | fulfilled. |
| 101 | |
| 102 | .. versionadded:: 3.3 |
| 103 | |
| 104 | .. exception:: SSLSyscallError |
| 105 | |
| 106 | A subclass of :exc:`SSLError` raised when a system error was encountered |
| 107 | while trying to fulfill an operation on a SSL socket. Unfortunately, |
| 108 | there is no easy way to inspect the original errno number. |
| 109 | |
| 110 | .. versionadded:: 3.3 |
| 111 | |
| 112 | .. exception:: SSLEOFError |
| 113 | |
| 114 | A subclass of :exc:`SSLError` raised when the SSL connection has been |
Antoine Pitrou | f3dc2d7 | 2011-10-28 00:01:03 +0200 | [diff] [blame] | 115 | terminated abruptly. Generally, you shouldn't try to reuse the underlying |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 116 | transport when this error is encountered. |
| 117 | |
| 118 | .. versionadded:: 3.3 |
| 119 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 120 | .. exception:: CertificateError |
| 121 | |
| 122 | Raised to signal an error with a certificate (such as mismatching |
| 123 | hostname). Certificate errors detected by OpenSSL, though, raise |
| 124 | an :exc:`SSLError`. |
| 125 | |
| 126 | |
| 127 | Socket creation |
| 128 | ^^^^^^^^^^^^^^^ |
| 129 | |
| 130 | The following function allows for standalone socket creation. Starting from |
| 131 | Python 3.2, it can be more flexible to use :meth:`SSLContext.wrap_socket` |
| 132 | instead. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 133 | |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 134 | .. 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] | 135 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 136 | Takes an instance ``sock`` of :class:`socket.socket`, and returns an instance |
| 137 | of :class:`ssl.SSLSocket`, a subtype of :class:`socket.socket`, which wraps |
| 138 | the underlying socket in an SSL context. For client-side sockets, the |
| 139 | context construction is lazy; if the underlying socket isn't connected yet, |
| 140 | the context construction will be performed after :meth:`connect` is called on |
| 141 | the socket. For server-side sockets, if the socket has no remote peer, it is |
| 142 | assumed to be a listening socket, and the server-side SSL wrapping is |
| 143 | automatically performed on client connections accepted via the :meth:`accept` |
| 144 | method. :func:`wrap_socket` may raise :exc:`SSLError`. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 145 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 146 | The ``keyfile`` and ``certfile`` parameters specify optional files which |
| 147 | contain a certificate to be used to identify the local side of the |
| 148 | connection. See the discussion of :ref:`ssl-certificates` for more |
| 149 | information on how the certificate is stored in the ``certfile``. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 150 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 151 | The parameter ``server_side`` is a boolean which identifies whether |
| 152 | server-side or client-side behavior is desired from this socket. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 153 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 154 | The parameter ``cert_reqs`` specifies whether a certificate is required from |
| 155 | the other side of the connection, and whether it will be validated if |
| 156 | provided. It must be one of the three values :const:`CERT_NONE` |
| 157 | (certificates ignored), :const:`CERT_OPTIONAL` (not required, but validated |
| 158 | if provided), or :const:`CERT_REQUIRED` (required and validated). If the |
| 159 | value of this parameter is not :const:`CERT_NONE`, then the ``ca_certs`` |
| 160 | parameter must point to a file of CA certificates. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 161 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 162 | The ``ca_certs`` file contains a set of concatenated "certification |
| 163 | authority" certificates, which are used to validate certificates passed from |
| 164 | the other end of the connection. See the discussion of |
| 165 | :ref:`ssl-certificates` for more information about how to arrange the |
| 166 | certificates in this file. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 167 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 168 | The parameter ``ssl_version`` specifies which version of the SSL protocol to |
| 169 | use. Typically, the server chooses a particular protocol version, and the |
| 170 | client must adapt to the server's choice. Most of the versions are not |
Antoine Pitrou | 84a2edc | 2012-01-09 21:35:11 +0100 | [diff] [blame] | 171 | interoperable with the other versions. If not specified, the default is |
| 172 | :data:`PROTOCOL_SSLv23`; it provides the most compatibility with other |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 173 | versions. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 174 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 175 | Here's a table showing which versions in a client (down the side) can connect |
| 176 | to which versions in a server (along the top): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 177 | |
| 178 | .. table:: |
| 179 | |
| 180 | ======================== ========= ========= ========== ========= |
| 181 | *client* / **server** **SSLv2** **SSLv3** **SSLv23** **TLSv1** |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 182 | ------------------------ --------- --------- ---------- --------- |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 183 | *SSLv2* yes no yes no |
Antoine Pitrou | ac8bfca | 2012-01-09 21:43:18 +0100 | [diff] [blame] | 184 | *SSLv3* no yes yes no |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 185 | *SSLv23* yes no yes no |
| 186 | *TLSv1* no no yes yes |
| 187 | ======================== ========= ========= ========== ========= |
| 188 | |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 189 | .. note:: |
| 190 | |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 191 | Which connections succeed will vary depending on the version of |
| 192 | OpenSSL. For instance, in some older versions of OpenSSL (such |
| 193 | as 0.9.7l on OS X 10.4), an SSLv2 client could not connect to an |
| 194 | SSLv23 server. Another example: beginning with OpenSSL 1.0.0, |
| 195 | an SSLv23 client will not actually attempt SSLv2 connections |
| 196 | unless you explicitly enable SSLv2 ciphers; for example, you |
| 197 | might specify ``"ALL"`` or ``"SSLv2"`` as the *ciphers* parameter |
| 198 | to enable them. |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 199 | |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 200 | The *ciphers* parameter sets the available ciphers for this SSL object. |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 201 | It should be a string in the `OpenSSL cipher list format |
| 202 | <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 203 | |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 204 | The parameter ``do_handshake_on_connect`` specifies whether to do the SSL |
| 205 | handshake automatically after doing a :meth:`socket.connect`, or whether the |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 206 | application program will call it explicitly, by invoking the |
| 207 | :meth:`SSLSocket.do_handshake` method. Calling |
| 208 | :meth:`SSLSocket.do_handshake` explicitly gives the program control over the |
| 209 | blocking behavior of the socket I/O involved in the handshake. |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 210 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 211 | The parameter ``suppress_ragged_eofs`` specifies how the |
Antoine Pitrou | dab6426 | 2010-09-19 13:31:06 +0000 | [diff] [blame] | 212 | :meth:`SSLSocket.recv` method should signal unexpected EOF from the other end |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 213 | 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] | 214 | normal EOF (an empty bytes object) in response to unexpected EOF errors |
| 215 | raised from the underlying socket; if :const:`False`, it will raise the |
| 216 | exceptions back to the caller. |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 217 | |
Ezio Melotti | 4d5195b | 2010-04-20 10:57:44 +0000 | [diff] [blame] | 218 | .. versionchanged:: 3.2 |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 219 | New optional argument *ciphers*. |
| 220 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 221 | Random generation |
| 222 | ^^^^^^^^^^^^^^^^^ |
| 223 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 224 | .. function:: RAND_bytes(num) |
| 225 | |
Victor Stinner | a675206 | 2011-05-25 11:27:40 +0200 | [diff] [blame] | 226 | Returns *num* cryptographically strong pseudo-random bytes. Raises an |
| 227 | :class:`SSLError` if the PRNG has not been seeded with enough data or if the |
| 228 | operation is not supported by the current RAND method. :func:`RAND_status` |
| 229 | can be used to check the status of the PRNG and :func:`RAND_add` can be used |
| 230 | to seed the PRNG. |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 231 | |
Victor Stinner | 19fb53c | 2011-05-24 21:32:40 +0200 | [diff] [blame] | 232 | Read the Wikipedia article, `Cryptographically secure pseudorandom number |
Victor Stinner | a675206 | 2011-05-25 11:27:40 +0200 | [diff] [blame] | 233 | generator (CSPRNG) |
Victor Stinner | 19fb53c | 2011-05-24 21:32:40 +0200 | [diff] [blame] | 234 | <http://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator>`_, |
| 235 | to get the requirements of a cryptographically generator. |
| 236 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 237 | .. versionadded:: 3.3 |
| 238 | |
| 239 | .. function:: RAND_pseudo_bytes(num) |
| 240 | |
| 241 | Returns (bytes, is_cryptographic): bytes are *num* pseudo-random bytes, |
| 242 | is_cryptographic is True if the bytes generated are cryptographically |
Victor Stinner | a675206 | 2011-05-25 11:27:40 +0200 | [diff] [blame] | 243 | strong. Raises an :class:`SSLError` if the operation is not supported by the |
| 244 | current RAND method. |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 245 | |
Victor Stinner | 19fb53c | 2011-05-24 21:32:40 +0200 | [diff] [blame] | 246 | Generated pseudo-random byte sequences will be unique if they are of |
| 247 | sufficient length, but are not necessarily unpredictable. They can be used |
| 248 | for non-cryptographic purposes and for certain purposes in cryptographic |
| 249 | protocols, but usually not for key generation etc. |
| 250 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 251 | .. versionadded:: 3.3 |
| 252 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 253 | .. function:: RAND_status() |
| 254 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 255 | Returns True if the SSL pseudo-random number generator has been seeded with |
| 256 | 'enough' randomness, and False otherwise. You can use :func:`ssl.RAND_egd` |
| 257 | and :func:`ssl.RAND_add` to increase the randomness of the pseudo-random |
| 258 | number generator. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 259 | |
| 260 | .. function:: RAND_egd(path) |
| 261 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 262 | If you are running an entropy-gathering daemon (EGD) somewhere, and *path* |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 263 | is the pathname of a socket connection open to it, this will read 256 bytes |
| 264 | of randomness from the socket, and add it to the SSL pseudo-random number |
| 265 | generator to increase the security of generated secret keys. This is |
| 266 | typically only necessary on systems without better sources of randomness. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 267 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 268 | See http://egd.sourceforge.net/ or http://prngd.sourceforge.net/ for sources |
| 269 | of entropy-gathering daemons. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 270 | |
| 271 | .. function:: RAND_add(bytes, entropy) |
| 272 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 273 | Mixes the given *bytes* into the SSL pseudo-random number generator. The |
| 274 | parameter *entropy* (a float) is a lower bound on the entropy contained in |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 275 | string (so you can always use :const:`0.0`). See :rfc:`1750` for more |
| 276 | information on sources of entropy. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 278 | Certificate handling |
| 279 | ^^^^^^^^^^^^^^^^^^^^ |
| 280 | |
| 281 | .. function:: match_hostname(cert, hostname) |
| 282 | |
| 283 | Verify that *cert* (in decoded format as returned by |
| 284 | :meth:`SSLSocket.getpeercert`) matches the given *hostname*. The rules |
| 285 | applied are those for checking the identity of HTTPS servers as outlined |
| 286 | in :rfc:`2818`, except that IP addresses are not currently supported. |
| 287 | In addition to HTTPS, this function should be suitable for checking the |
| 288 | identity of servers in various SSL-based protocols such as FTPS, IMAPS, |
| 289 | POPS and others. |
| 290 | |
| 291 | :exc:`CertificateError` is raised on failure. On success, the function |
| 292 | returns nothing:: |
| 293 | |
| 294 | >>> cert = {'subject': ((('commonName', 'example.com'),),)} |
| 295 | >>> ssl.match_hostname(cert, "example.com") |
| 296 | >>> ssl.match_hostname(cert, "example.org") |
| 297 | Traceback (most recent call last): |
| 298 | File "<stdin>", line 1, in <module> |
| 299 | File "/home/py3k/Lib/ssl.py", line 130, in match_hostname |
| 300 | ssl.CertificateError: hostname 'example.org' doesn't match 'example.com' |
| 301 | |
| 302 | .. versionadded:: 3.2 |
| 303 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 304 | .. function:: cert_time_to_seconds(timestring) |
| 305 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 306 | Returns a floating-point value containing a normal seconds-after-the-epoch |
| 307 | time value, given the time-string representing the "notBefore" or "notAfter" |
| 308 | date from a certificate. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 309 | |
| 310 | Here's an example:: |
| 311 | |
| 312 | >>> import ssl |
| 313 | >>> ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT") |
| 314 | 1178694000.0 |
| 315 | >>> import time |
| 316 | >>> time.ctime(ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT")) |
| 317 | 'Wed May 9 00:00:00 2007' |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 318 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 319 | .. function:: get_server_certificate(addr, ssl_version=PROTOCOL_SSLv3, ca_certs=None) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 320 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 321 | Given the address ``addr`` of an SSL-protected server, as a (*hostname*, |
| 322 | *port-number*) pair, fetches the server's certificate, and returns it as a |
| 323 | PEM-encoded string. If ``ssl_version`` is specified, uses that version of |
| 324 | the SSL protocol to attempt to connect to the server. If ``ca_certs`` is |
| 325 | specified, it should be a file containing a list of root certificates, the |
| 326 | same format as used for the same parameter in :func:`wrap_socket`. The call |
| 327 | will attempt to validate the server certificate against that set of root |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 328 | certificates, and will fail if the validation attempt fails. |
| 329 | |
Antoine Pitrou | 15399c3 | 2011-04-28 19:23:55 +0200 | [diff] [blame] | 330 | .. versionchanged:: 3.3 |
| 331 | This function is now IPv6-compatible. |
| 332 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 333 | .. function:: DER_cert_to_PEM_cert(DER_cert_bytes) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 334 | |
| 335 | Given a certificate as a DER-encoded blob of bytes, returns a PEM-encoded |
| 336 | string version of the same certificate. |
| 337 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 338 | .. function:: PEM_cert_to_DER_cert(PEM_cert_string) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 339 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 340 | Given a certificate as an ASCII PEM string, returns a DER-encoded sequence of |
| 341 | bytes for that same certificate. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 343 | Constants |
| 344 | ^^^^^^^^^ |
| 345 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 346 | .. data:: CERT_NONE |
| 347 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 348 | Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs`` |
| 349 | parameter to :func:`wrap_socket`. In this mode (the default), no |
| 350 | certificates will be required from the other side of the socket connection. |
| 351 | If a certificate is received from the other end, no attempt to validate it |
| 352 | is made. |
| 353 | |
| 354 | See the discussion of :ref:`ssl-security` below. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 355 | |
| 356 | .. data:: CERT_OPTIONAL |
| 357 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 358 | Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs`` |
| 359 | parameter to :func:`wrap_socket`. In this mode no certificates will be |
| 360 | required from the other side of the socket connection; but if they |
| 361 | are provided, validation will be attempted and an :class:`SSLError` |
| 362 | will be raised on failure. |
| 363 | |
| 364 | Use of this setting requires a valid set of CA certificates to |
| 365 | be passed, either to :meth:`SSLContext.load_verify_locations` or as a |
| 366 | value of the ``ca_certs`` parameter to :func:`wrap_socket`. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 367 | |
| 368 | .. data:: CERT_REQUIRED |
| 369 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 370 | Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs`` |
| 371 | parameter to :func:`wrap_socket`. In this mode, certificates are |
| 372 | required from the other side of the socket connection; an :class:`SSLError` |
| 373 | will be raised if no certificate is provided, or if its validation fails. |
| 374 | |
| 375 | Use of this setting requires a valid set of CA certificates to |
| 376 | be passed, either to :meth:`SSLContext.load_verify_locations` or as a |
| 377 | value of the ``ca_certs`` parameter to :func:`wrap_socket`. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 378 | |
| 379 | .. data:: PROTOCOL_SSLv2 |
| 380 | |
| 381 | Selects SSL version 2 as the channel encryption protocol. |
| 382 | |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 383 | This protocol is not available if OpenSSL is compiled with OPENSSL_NO_SSL2 |
| 384 | flag. |
| 385 | |
Antoine Pitrou | 8eac60d | 2010-05-16 14:19:41 +0000 | [diff] [blame] | 386 | .. warning:: |
| 387 | |
| 388 | SSL version 2 is insecure. Its use is highly discouraged. |
| 389 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 390 | .. data:: PROTOCOL_SSLv23 |
| 391 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 392 | Selects SSL version 2 or 3 as the channel encryption protocol. This is a |
| 393 | setting to use with servers for maximum compatibility with the other end of |
| 394 | an SSL connection, but it may cause the specific ciphers chosen for the |
| 395 | encryption to be of fairly low quality. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 396 | |
| 397 | .. data:: PROTOCOL_SSLv3 |
| 398 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 399 | Selects SSL version 3 as the channel encryption protocol. For clients, this |
| 400 | is the maximally compatible SSL variant. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 401 | |
| 402 | .. data:: PROTOCOL_TLSv1 |
| 403 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 404 | Selects TLS version 1 as the channel encryption protocol. This is the most |
| 405 | modern version, and probably the best choice for maximum protection, if both |
| 406 | sides can speak it. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 407 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 408 | .. data:: OP_ALL |
| 409 | |
| 410 | Enables workarounds for various bugs present in other SSL implementations. |
Antoine Pitrou | 9f6b02e | 2012-01-27 10:02:55 +0100 | [diff] [blame] | 411 | This option is set by default. It does not necessarily set the same |
| 412 | flags as OpenSSL's ``SSL_OP_ALL`` constant. |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 413 | |
| 414 | .. versionadded:: 3.2 |
| 415 | |
| 416 | .. data:: OP_NO_SSLv2 |
| 417 | |
| 418 | Prevents an SSLv2 connection. This option is only applicable in |
| 419 | conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from |
| 420 | choosing SSLv2 as the protocol version. |
| 421 | |
| 422 | .. versionadded:: 3.2 |
| 423 | |
| 424 | .. data:: OP_NO_SSLv3 |
| 425 | |
| 426 | Prevents an SSLv3 connection. This option is only applicable in |
| 427 | conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from |
| 428 | choosing SSLv3 as the protocol version. |
| 429 | |
| 430 | .. versionadded:: 3.2 |
| 431 | |
| 432 | .. data:: OP_NO_TLSv1 |
| 433 | |
| 434 | Prevents a TLSv1 connection. This option is only applicable in |
| 435 | conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from |
| 436 | choosing TLSv1 as the protocol version. |
| 437 | |
| 438 | .. versionadded:: 3.2 |
| 439 | |
Antoine Pitrou | 6db4944 | 2011-12-19 13:27:11 +0100 | [diff] [blame] | 440 | .. data:: OP_CIPHER_SERVER_PREFERENCE |
| 441 | |
| 442 | Use the server's cipher ordering preference, rather than the client's. |
| 443 | This option has no effect on client sockets and SSLv2 server sockets. |
| 444 | |
| 445 | .. versionadded:: 3.3 |
| 446 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 447 | .. data:: OP_SINGLE_DH_USE |
| 448 | |
| 449 | Prevents re-use of the same DH key for distinct SSL sessions. This |
| 450 | improves forward secrecy but requires more computational resources. |
| 451 | This option only applies to server sockets. |
| 452 | |
| 453 | .. versionadded:: 3.3 |
| 454 | |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 455 | .. data:: OP_SINGLE_ECDH_USE |
| 456 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 457 | Prevents re-use of the same ECDH key for distinct SSL sessions. This |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 458 | improves forward secrecy but requires more computational resources. |
| 459 | This option only applies to server sockets. |
| 460 | |
| 461 | .. versionadded:: 3.3 |
| 462 | |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 463 | .. data:: OP_NO_COMPRESSION |
| 464 | |
| 465 | Disable compression on the SSL channel. This is useful if the application |
| 466 | protocol supports its own compression scheme. |
| 467 | |
| 468 | This option is only available with OpenSSL 1.0.0 and later. |
| 469 | |
| 470 | .. versionadded:: 3.3 |
| 471 | |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 472 | .. data:: HAS_ECDH |
| 473 | |
| 474 | Whether the OpenSSL library has built-in support for Elliptic Curve-based |
| 475 | Diffie-Hellman key exchange. This should be true unless the feature was |
| 476 | explicitly disabled by the distributor. |
| 477 | |
| 478 | .. versionadded:: 3.3 |
| 479 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 480 | .. data:: HAS_SNI |
| 481 | |
| 482 | Whether the OpenSSL library has built-in support for the *Server Name |
| 483 | Indication* extension to the SSLv3 and TLSv1 protocols (as defined in |
| 484 | :rfc:`4366`). When true, you can use the *server_hostname* argument to |
| 485 | :meth:`SSLContext.wrap_socket`. |
| 486 | |
| 487 | .. versionadded:: 3.2 |
| 488 | |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 489 | .. data:: HAS_NPN |
| 490 | |
| 491 | Whether the OpenSSL library has built-in support for *Next Protocol |
| 492 | Negotiation* as described in the `NPN draft specification |
| 493 | <http://tools.ietf.org/html/draft-agl-tls-nextprotoneg>`_. When true, |
| 494 | you can use the :meth:`SSLContext.set_npn_protocols` method to advertise |
| 495 | which protocols you want to support. |
| 496 | |
| 497 | .. versionadded:: 3.3 |
| 498 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 499 | .. data:: CHANNEL_BINDING_TYPES |
| 500 | |
| 501 | List of supported TLS channel binding types. Strings in this list |
| 502 | can be used as arguments to :meth:`SSLSocket.get_channel_binding`. |
| 503 | |
| 504 | .. versionadded:: 3.3 |
| 505 | |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 506 | .. data:: OPENSSL_VERSION |
| 507 | |
| 508 | The version string of the OpenSSL library loaded by the interpreter:: |
| 509 | |
| 510 | >>> ssl.OPENSSL_VERSION |
| 511 | 'OpenSSL 0.9.8k 25 Mar 2009' |
| 512 | |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 513 | .. versionadded:: 3.2 |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 514 | |
| 515 | .. data:: OPENSSL_VERSION_INFO |
| 516 | |
| 517 | A tuple of five integers representing version information about the |
| 518 | OpenSSL library:: |
| 519 | |
| 520 | >>> ssl.OPENSSL_VERSION_INFO |
| 521 | (0, 9, 8, 11, 15) |
| 522 | |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 523 | .. versionadded:: 3.2 |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 524 | |
| 525 | .. data:: OPENSSL_VERSION_NUMBER |
| 526 | |
| 527 | The raw version number of the OpenSSL library, as a single integer:: |
| 528 | |
| 529 | >>> ssl.OPENSSL_VERSION_NUMBER |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 530 | 9470143 |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 531 | >>> hex(ssl.OPENSSL_VERSION_NUMBER) |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 532 | '0x9080bf' |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 533 | |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 534 | .. versionadded:: 3.2 |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 535 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 536 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 537 | SSL Sockets |
| 538 | ----------- |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 539 | |
Antoine Pitrou | e1f2f30 | 2010-09-19 13:56:11 +0000 | [diff] [blame] | 540 | SSL sockets provide the following methods of :ref:`socket-objects`: |
Antoine Pitrou | 792ff3e | 2010-09-19 13:19:21 +0000 | [diff] [blame] | 541 | |
Antoine Pitrou | e1f2f30 | 2010-09-19 13:56:11 +0000 | [diff] [blame] | 542 | - :meth:`~socket.socket.accept()` |
| 543 | - :meth:`~socket.socket.bind()` |
| 544 | - :meth:`~socket.socket.close()` |
| 545 | - :meth:`~socket.socket.connect()` |
| 546 | - :meth:`~socket.socket.detach()` |
| 547 | - :meth:`~socket.socket.fileno()` |
| 548 | - :meth:`~socket.socket.getpeername()`, :meth:`~socket.socket.getsockname()` |
| 549 | - :meth:`~socket.socket.getsockopt()`, :meth:`~socket.socket.setsockopt()` |
| 550 | - :meth:`~socket.socket.gettimeout()`, :meth:`~socket.socket.settimeout()`, |
| 551 | :meth:`~socket.socket.setblocking()` |
| 552 | - :meth:`~socket.socket.listen()` |
| 553 | - :meth:`~socket.socket.makefile()` |
| 554 | - :meth:`~socket.socket.recv()`, :meth:`~socket.socket.recv_into()` |
| 555 | (but passing a non-zero ``flags`` argument is not allowed) |
| 556 | - :meth:`~socket.socket.send()`, :meth:`~socket.socket.sendall()` (with |
| 557 | the same limitation) |
| 558 | - :meth:`~socket.socket.shutdown()` |
| 559 | |
Antoine Pitrou | 6f5dcb1 | 2011-07-11 01:35:48 +0200 | [diff] [blame] | 560 | However, since the SSL (and TLS) protocol has its own framing atop |
| 561 | of TCP, the SSL sockets abstraction can, in certain respects, diverge from |
| 562 | the specification of normal, OS-level sockets. See especially the |
| 563 | :ref:`notes on non-blocking sockets <ssl-nonblocking>`. |
| 564 | |
| 565 | SSL sockets also have the following additional methods and attributes: |
Antoine Pitrou | 792ff3e | 2010-09-19 13:19:21 +0000 | [diff] [blame] | 566 | |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 567 | .. method:: SSLSocket.do_handshake() |
| 568 | |
Antoine Pitrou | b3593ca | 2011-07-11 01:39:19 +0200 | [diff] [blame] | 569 | Perform the SSL setup handshake. |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 570 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 571 | .. method:: SSLSocket.getpeercert(binary_form=False) |
| 572 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 573 | If there is no certificate for the peer on the other end of the connection, |
| 574 | returns ``None``. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 575 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 576 | If the parameter ``binary_form`` is :const:`False`, and a certificate was |
| 577 | received from the peer, this method returns a :class:`dict` instance. If the |
| 578 | certificate was not validated, the dict is empty. If the certificate was |
Antoine Pitrou | b7c6c81 | 2012-08-16 22:14:43 +0200 | [diff] [blame] | 579 | validated, it returns a dict with several keys, amongst them ``subject`` |
| 580 | (the principal for which the certificate was issued) and ``issuer`` |
| 581 | (the principal issuing the certificate). If a certificate contains an |
| 582 | instance of the *Subject Alternative Name* extension (see :rfc:`3280`), |
| 583 | there will also be a ``subjectAltName`` key in the dictionary. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | b7c6c81 | 2012-08-16 22:14:43 +0200 | [diff] [blame] | 585 | The ``subject`` and ``issuer`` fields are tuples containing the sequence |
| 586 | of relative distinguished names (RDNs) given in the certificate's data |
| 587 | structure for the respective fields, and each RDN is a sequence of |
| 588 | name-value pairs. Here is a real-world example:: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 589 | |
Antoine Pitrou | b7c6c81 | 2012-08-16 22:14:43 +0200 | [diff] [blame] | 590 | {'issuer': ((('countryName', 'IL'),), |
| 591 | (('organizationName', 'StartCom Ltd.'),), |
| 592 | (('organizationalUnitName', |
| 593 | 'Secure Digital Certificate Signing'),), |
| 594 | (('commonName', |
| 595 | 'StartCom Class 2 Primary Intermediate Server CA'),)), |
| 596 | 'notAfter': 'Nov 22 08:15:19 2013 GMT', |
| 597 | 'notBefore': 'Nov 21 03:09:52 2011 GMT', |
| 598 | 'serialNumber': '95F0', |
| 599 | 'subject': ((('description', '571208-SLe257oHY9fVQ07Z'),), |
| 600 | (('countryName', 'US'),), |
| 601 | (('stateOrProvinceName', 'California'),), |
| 602 | (('localityName', 'San Francisco'),), |
| 603 | (('organizationName', 'Electronic Frontier Foundation, Inc.'),), |
| 604 | (('commonName', '*.eff.org'),), |
| 605 | (('emailAddress', 'hostmaster@eff.org'),)), |
| 606 | 'subjectAltName': (('DNS', '*.eff.org'), ('DNS', 'eff.org')), |
| 607 | 'version': 3} |
| 608 | |
| 609 | .. note:: |
| 610 | To validate a certificate for a particular service, you can use the |
| 611 | :func:`match_hostname` function. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 612 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 613 | If the ``binary_form`` parameter is :const:`True`, and a certificate was |
| 614 | provided, this method returns the DER-encoded form of the entire certificate |
| 615 | as a sequence of bytes, or :const:`None` if the peer did not provide a |
| 616 | certificate. This return value is independent of validation; if validation |
| 617 | was required (:const:`CERT_OPTIONAL` or :const:`CERT_REQUIRED`), it will have |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 618 | been validated, but if :const:`CERT_NONE` was used to establish the |
| 619 | connection, the certificate, if present, will not have been validated. |
| 620 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 621 | .. versionchanged:: 3.2 |
| 622 | The returned dictionary includes additional items such as ``issuer`` |
| 623 | and ``notBefore``. |
| 624 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 625 | .. method:: SSLSocket.cipher() |
| 626 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 627 | Returns a three-value tuple containing the name of the cipher being used, the |
| 628 | version of the SSL protocol that defines its use, and the number of secret |
| 629 | bits being used. If no connection has been established, returns ``None``. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 631 | .. method:: SSLSocket.compression() |
| 632 | |
| 633 | Return the compression algorithm being used as a string, or ``None`` |
| 634 | if the connection isn't compressed. |
| 635 | |
| 636 | If the higher-level protocol supports its own compression mechanism, |
| 637 | you can use :data:`OP_NO_COMPRESSION` to disable SSL-level compression. |
| 638 | |
| 639 | .. versionadded:: 3.3 |
| 640 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 641 | .. method:: SSLSocket.get_channel_binding(cb_type="tls-unique") |
| 642 | |
| 643 | Get channel binding data for current connection, as a bytes object. Returns |
| 644 | ``None`` if not connected or the handshake has not been completed. |
| 645 | |
| 646 | The *cb_type* parameter allow selection of the desired channel binding |
| 647 | type. Valid channel binding types are listed in the |
| 648 | :data:`CHANNEL_BINDING_TYPES` list. Currently only the 'tls-unique' channel |
| 649 | binding, defined by :rfc:`5929`, is supported. :exc:`ValueError` will be |
| 650 | raised if an unsupported channel binding type is requested. |
| 651 | |
| 652 | .. versionadded:: 3.3 |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 653 | |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 654 | .. method:: SSLSocket.selected_npn_protocol() |
| 655 | |
| 656 | Returns the protocol that was selected during the TLS/SSL handshake. If |
| 657 | :meth:`SSLContext.set_npn_protocols` was not called, or if the other party |
| 658 | does not support NPN, or if the handshake has not yet happened, this will |
| 659 | return ``None``. |
| 660 | |
| 661 | .. versionadded:: 3.3 |
| 662 | |
Benjamin Peterson | 4aeec04 | 2008-08-19 21:42:13 +0000 | [diff] [blame] | 663 | .. method:: SSLSocket.unwrap() |
| 664 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 665 | Performs the SSL shutdown handshake, which removes the TLS layer from the |
| 666 | underlying socket, and returns the underlying socket object. This can be |
| 667 | used to go from encrypted operation over a connection to unencrypted. The |
| 668 | returned socket should always be used for further communication with the |
| 669 | other side of the connection, rather than the original socket. |
Benjamin Peterson | 4aeec04 | 2008-08-19 21:42:13 +0000 | [diff] [blame] | 670 | |
Antoine Pitrou | ec883db | 2010-05-24 21:20:20 +0000 | [diff] [blame] | 671 | .. attribute:: SSLSocket.context |
| 672 | |
| 673 | The :class:`SSLContext` object this SSL socket is tied to. If the SSL |
| 674 | socket was created using the top-level :func:`wrap_socket` function |
| 675 | (rather than :meth:`SSLContext.wrap_socket`), this is a custom context |
| 676 | object created for this SSL socket. |
| 677 | |
| 678 | .. versionadded:: 3.2 |
| 679 | |
| 680 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 681 | SSL Contexts |
| 682 | ------------ |
| 683 | |
Antoine Pitrou | cafaad4 | 2010-05-24 15:58:43 +0000 | [diff] [blame] | 684 | .. versionadded:: 3.2 |
| 685 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 686 | An SSL context holds various data longer-lived than single SSL connections, |
| 687 | such as SSL configuration options, certificate(s) and private key(s). |
| 688 | It also manages a cache of SSL sessions for server-side sockets, in order |
| 689 | to speed up repeated connections from the same clients. |
| 690 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 691 | .. class:: SSLContext(protocol) |
| 692 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 693 | Create a new SSL context. You must pass *protocol* which must be one |
| 694 | of the ``PROTOCOL_*`` constants defined in this module. |
| 695 | :data:`PROTOCOL_SSLv23` is recommended for maximum interoperability. |
| 696 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 697 | |
| 698 | :class:`SSLContext` objects have the following methods and attributes: |
| 699 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 700 | .. method:: SSLContext.load_cert_chain(certfile, keyfile=None, password=None) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 701 | |
| 702 | Load a private key and the corresponding certificate. The *certfile* |
| 703 | string must be the path to a single file in PEM format containing the |
| 704 | certificate as well as any number of CA certificates needed to establish |
| 705 | the certificate's authenticity. The *keyfile* string, if present, must |
| 706 | point to a file containing the private key in. Otherwise the private |
| 707 | key will be taken from *certfile* as well. See the discussion of |
| 708 | :ref:`ssl-certificates` for more information on how the certificate |
| 709 | is stored in the *certfile*. |
| 710 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 711 | The *password* argument may be a function to call to get the password for |
| 712 | decrypting the private key. It will only be called if the private key is |
| 713 | encrypted and a password is necessary. It will be called with no arguments, |
| 714 | and it should return a string, bytes, or bytearray. If the return value is |
| 715 | a string it will be encoded as UTF-8 before using it to decrypt the key. |
| 716 | Alternatively a string, bytes, or bytearray value may be supplied directly |
| 717 | as the *password* argument. It will be ignored if the private key is not |
| 718 | encrypted and no password is needed. |
| 719 | |
| 720 | If the *password* argument is not specified and a password is required, |
| 721 | OpenSSL's built-in password prompting mechanism will be used to |
| 722 | interactively prompt the user for a password. |
| 723 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 724 | An :class:`SSLError` is raised if the private key doesn't |
| 725 | match with the certificate. |
| 726 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 727 | .. versionchanged:: 3.3 |
| 728 | New optional argument *password*. |
| 729 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 730 | .. method:: SSLContext.load_verify_locations(cafile=None, capath=None) |
| 731 | |
| 732 | Load a set of "certification authority" (CA) certificates used to validate |
| 733 | other peers' certificates when :data:`verify_mode` is other than |
| 734 | :data:`CERT_NONE`. At least one of *cafile* or *capath* must be specified. |
| 735 | |
| 736 | The *cafile* string, if present, is the path to a file of concatenated |
| 737 | CA certificates in PEM format. See the discussion of |
| 738 | :ref:`ssl-certificates` for more information about how to arrange the |
| 739 | certificates in this file. |
| 740 | |
| 741 | The *capath* string, if present, is |
| 742 | the path to a directory containing several CA certificates in PEM format, |
| 743 | following an `OpenSSL specific layout |
| 744 | <http://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html>`_. |
| 745 | |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 746 | .. method:: SSLContext.set_default_verify_paths() |
| 747 | |
| 748 | Load a set of default "certification authority" (CA) certificates from |
| 749 | a filesystem path defined when building the OpenSSL library. Unfortunately, |
| 750 | there's no easy way to know whether this method succeeds: no error is |
| 751 | returned if no certificates are to be found. When the OpenSSL library is |
| 752 | provided as part of the operating system, though, it is likely to be |
| 753 | configured properly. |
| 754 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 755 | .. method:: SSLContext.set_ciphers(ciphers) |
| 756 | |
| 757 | Set the available ciphers for sockets created with this context. |
| 758 | It should be a string in the `OpenSSL cipher list format |
| 759 | <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_. |
| 760 | If no cipher can be selected (because compile-time options or other |
| 761 | configuration forbids use of all the specified ciphers), an |
| 762 | :class:`SSLError` will be raised. |
| 763 | |
| 764 | .. note:: |
| 765 | when connected, the :meth:`SSLSocket.cipher` method of SSL sockets will |
| 766 | give the currently selected cipher. |
| 767 | |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 768 | .. method:: SSLContext.set_npn_protocols(protocols) |
| 769 | |
| 770 | Specify which protocols the socket should avertise during the SSL/TLS |
| 771 | handshake. It should be a list of strings, like ``['http/1.1', 'spdy/2']``, |
| 772 | ordered by preference. The selection of a protocol will happen during the |
| 773 | handshake, and will play out according to the `NPN draft specification |
| 774 | <http://tools.ietf.org/html/draft-agl-tls-nextprotoneg>`_. After a |
| 775 | successful handshake, the :meth:`SSLSocket.selected_npn_protocol` method will |
| 776 | return the agreed-upon protocol. |
| 777 | |
| 778 | This method will raise :exc:`NotImplementedError` if :data:`HAS_NPN` is |
| 779 | False. |
| 780 | |
| 781 | .. versionadded:: 3.3 |
| 782 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 783 | .. method:: SSLContext.load_dh_params(dhfile) |
| 784 | |
| 785 | Load the key generation parameters for Diffie-Helman (DH) key exchange. |
| 786 | Using DH key exchange improves forward secrecy at the expense of |
| 787 | computational resources (both on the server and on the client). |
| 788 | The *dhfile* parameter should be the path to a file containing DH |
| 789 | parameters in PEM format. |
| 790 | |
| 791 | This setting doesn't apply to client sockets. You can also use the |
| 792 | :data:`OP_SINGLE_DH_USE` option to further improve security. |
| 793 | |
| 794 | .. versionadded:: 3.3 |
| 795 | |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 796 | .. method:: SSLContext.set_ecdh_curve(curve_name) |
| 797 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 798 | Set the curve name for Elliptic Curve-based Diffie-Hellman (ECDH) key |
| 799 | exchange. ECDH is significantly faster than regular DH while arguably |
| 800 | as secure. The *curve_name* parameter should be a string describing |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 801 | a well-known elliptic curve, for example ``prime256v1`` for a widely |
| 802 | supported curve. |
| 803 | |
| 804 | This setting doesn't apply to client sockets. You can also use the |
| 805 | :data:`OP_SINGLE_ECDH_USE` option to further improve security. |
| 806 | |
Antoine Pitrou | 501da61 | 2011-12-21 09:27:41 +0100 | [diff] [blame] | 807 | This method is not available if :data:`HAS_ECDH` is False. |
| 808 | |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 809 | .. versionadded:: 3.3 |
| 810 | |
| 811 | .. seealso:: |
| 812 | `SSL/TLS & Perfect Forward Secrecy <http://vincent.bernat.im/en/blog/2011-ssl-perfect-forward-secrecy.html>`_ |
| 813 | Vincent Bernat. |
| 814 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 815 | .. method:: SSLContext.wrap_socket(sock, server_side=False, \ |
| 816 | do_handshake_on_connect=True, suppress_ragged_eofs=True, \ |
| 817 | server_hostname=None) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 818 | |
| 819 | Wrap an existing Python socket *sock* and return an :class:`SSLSocket` |
| 820 | object. The SSL socket is tied to the context, its settings and |
| 821 | certificates. The parameters *server_side*, *do_handshake_on_connect* |
| 822 | and *suppress_ragged_eofs* have the same meaning as in the top-level |
| 823 | :func:`wrap_socket` function. |
| 824 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 825 | On client connections, the optional parameter *server_hostname* specifies |
| 826 | the hostname of the service which we are connecting to. This allows a |
| 827 | single server to host multiple SSL-based services with distinct certificates, |
| 828 | quite similarly to HTTP virtual hosts. Specifying *server_hostname* |
| 829 | will raise a :exc:`ValueError` if the OpenSSL library doesn't have support |
| 830 | for it (that is, if :data:`HAS_SNI` is :const:`False`). Specifying |
| 831 | *server_hostname* will also raise a :exc:`ValueError` if *server_side* |
| 832 | is true. |
| 833 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 834 | .. method:: SSLContext.session_stats() |
| 835 | |
| 836 | Get statistics about the SSL sessions created or managed by this context. |
| 837 | A dictionary is returned which maps the names of each `piece of information |
| 838 | <http://www.openssl.org/docs/ssl/SSL_CTX_sess_number.html>`_ to their |
| 839 | numeric values. For example, here is the total number of hits and misses |
| 840 | in the session cache since the context was created:: |
| 841 | |
| 842 | >>> stats = context.session_stats() |
| 843 | >>> stats['hits'], stats['misses'] |
| 844 | (0, 0) |
| 845 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 846 | .. attribute:: SSLContext.options |
| 847 | |
| 848 | An integer representing the set of SSL options enabled on this context. |
| 849 | The default value is :data:`OP_ALL`, but you can specify other options |
| 850 | such as :data:`OP_NO_SSLv2` by ORing them together. |
| 851 | |
| 852 | .. note:: |
| 853 | With versions of OpenSSL older than 0.9.8m, it is only possible |
| 854 | to set options, not to clear them. Attempting to clear an option |
| 855 | (by resetting the corresponding bits) will raise a ``ValueError``. |
| 856 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 857 | .. attribute:: SSLContext.protocol |
| 858 | |
| 859 | The protocol version chosen when constructing the context. This attribute |
| 860 | is read-only. |
| 861 | |
| 862 | .. attribute:: SSLContext.verify_mode |
| 863 | |
| 864 | Whether to try to verify other peers' certificates and how to behave |
| 865 | if verification fails. This attribute must be one of |
| 866 | :data:`CERT_NONE`, :data:`CERT_OPTIONAL` or :data:`CERT_REQUIRED`. |
| 867 | |
| 868 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 869 | .. index:: single: certificates |
| 870 | |
| 871 | .. index:: single: X509 certificate |
| 872 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 873 | .. _ssl-certificates: |
| 874 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 875 | Certificates |
| 876 | ------------ |
| 877 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 878 | Certificates in general are part of a public-key / private-key system. In this |
| 879 | system, each *principal*, (which may be a machine, or a person, or an |
| 880 | organization) is assigned a unique two-part encryption key. One part of the key |
| 881 | is public, and is called the *public key*; the other part is kept secret, and is |
| 882 | called the *private key*. The two parts are related, in that if you encrypt a |
| 883 | message with one of the parts, you can decrypt it with the other part, and |
| 884 | **only** with the other part. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 885 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 886 | A certificate contains information about two principals. It contains the name |
| 887 | of a *subject*, and the subject's public key. It also contains a statement by a |
| 888 | second principal, the *issuer*, that the subject is who he claims to be, and |
| 889 | that this is indeed the subject's public key. The issuer's statement is signed |
| 890 | with the issuer's private key, which only the issuer knows. However, anyone can |
| 891 | verify the issuer's statement by finding the issuer's public key, decrypting the |
| 892 | statement with it, and comparing it to the other information in the certificate. |
| 893 | The certificate also contains information about the time period over which it is |
| 894 | valid. This is expressed as two fields, called "notBefore" and "notAfter". |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 895 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 896 | In the Python use of certificates, a client or server can use a certificate to |
| 897 | prove who they are. The other side of a network connection can also be required |
| 898 | to produce a certificate, and that certificate can be validated to the |
| 899 | satisfaction of the client or server that requires such validation. The |
| 900 | connection attempt can be set to raise an exception if the validation fails. |
| 901 | Validation is done automatically, by the underlying OpenSSL framework; the |
| 902 | application need not concern itself with its mechanics. But the application |
| 903 | does usually need to provide sets of certificates to allow this process to take |
| 904 | place. |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 905 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 906 | Python uses files to contain certificates. They should be formatted as "PEM" |
| 907 | (see :rfc:`1422`), which is a base-64 encoded form wrapped with a header line |
| 908 | and a footer line:: |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 909 | |
| 910 | -----BEGIN CERTIFICATE----- |
| 911 | ... (certificate in base64 PEM encoding) ... |
| 912 | -----END CERTIFICATE----- |
| 913 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 914 | Certificate chains |
| 915 | ^^^^^^^^^^^^^^^^^^ |
| 916 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 917 | The Python files which contain certificates can contain a sequence of |
| 918 | certificates, sometimes called a *certificate chain*. This chain should start |
| 919 | with the specific certificate for the principal who "is" the client or server, |
| 920 | and then the certificate for the issuer of that certificate, and then the |
| 921 | certificate for the issuer of *that* certificate, and so on up the chain till |
| 922 | you get to a certificate which is *self-signed*, that is, a certificate which |
| 923 | has the same subject and issuer, sometimes called a *root certificate*. The |
| 924 | certificates should just be concatenated together in the certificate file. For |
| 925 | example, suppose we had a three certificate chain, from our server certificate |
| 926 | to the certificate of the certification authority that signed our server |
| 927 | certificate, to the root certificate of the agency which issued the |
| 928 | certification authority's certificate:: |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 929 | |
| 930 | -----BEGIN CERTIFICATE----- |
| 931 | ... (certificate for your server)... |
| 932 | -----END CERTIFICATE----- |
| 933 | -----BEGIN CERTIFICATE----- |
| 934 | ... (the certificate for the CA)... |
| 935 | -----END CERTIFICATE----- |
| 936 | -----BEGIN CERTIFICATE----- |
| 937 | ... (the root certificate for the CA's issuer)... |
| 938 | -----END CERTIFICATE----- |
| 939 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 940 | CA certificates |
| 941 | ^^^^^^^^^^^^^^^ |
| 942 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 943 | If you are going to require validation of the other side of the connection's |
| 944 | 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] | 945 | chains for each issuer you are willing to trust. Again, this file just contains |
| 946 | these chains concatenated together. For validation, Python will use the first |
| 947 | chain it finds in the file which matches. Some "standard" root certificates are |
| 948 | available from various certification authorities: `CACert.org |
| 949 | <http://www.cacert.org/index.php?id=3>`_, `Thawte |
| 950 | <http://www.thawte.com/roots/>`_, `Verisign |
| 951 | <http://www.verisign.com/support/roots.html>`_, `Positive SSL |
| 952 | <http://www.PositiveSSL.com/ssl-certificate-support/cert_installation/UTN-USERFirst-Hardware.crt>`_ |
| 953 | (used by python.org), `Equifax and GeoTrust |
| 954 | <http://www.geotrust.com/resources/root_certificates/index.asp>`_. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 955 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 956 | In general, if you are using SSL3 or TLS1, you don't need to put the full chain |
| 957 | in your "CA certs" file; you only need the root certificates, and the remote |
| 958 | peer is supposed to furnish the other certificates necessary to chain from its |
| 959 | certificate to a root certificate. See :rfc:`4158` for more discussion of the |
| 960 | way in which certification chains can be built. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 961 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 962 | Combined key and certificate |
| 963 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 964 | |
| 965 | Often the private key is stored in the same file as the certificate; in this |
| 966 | case, only the ``certfile`` parameter to :meth:`SSLContext.load_cert_chain` |
| 967 | and :func:`wrap_socket` needs to be passed. If the private key is stored |
| 968 | with the certificate, it should come before the first certificate in |
| 969 | the certificate chain:: |
| 970 | |
| 971 | -----BEGIN RSA PRIVATE KEY----- |
| 972 | ... (private key in base64 encoding) ... |
| 973 | -----END RSA PRIVATE KEY----- |
| 974 | -----BEGIN CERTIFICATE----- |
| 975 | ... (certificate in base64 PEM encoding) ... |
| 976 | -----END CERTIFICATE----- |
| 977 | |
| 978 | Self-signed certificates |
| 979 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 980 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 981 | If you are going to create a server that provides SSL-encrypted connection |
| 982 | services, you will need to acquire a certificate for that service. There are |
| 983 | many ways of acquiring appropriate certificates, such as buying one from a |
| 984 | certification authority. Another common practice is to generate a self-signed |
| 985 | certificate. The simplest way to do this is with the OpenSSL package, using |
| 986 | something like the following:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 987 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 988 | % openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem |
| 989 | Generating a 1024 bit RSA private key |
| 990 | .......++++++ |
| 991 | .............................++++++ |
| 992 | writing new private key to 'cert.pem' |
| 993 | ----- |
| 994 | You are about to be asked to enter information that will be incorporated |
| 995 | into your certificate request. |
| 996 | What you are about to enter is what is called a Distinguished Name or a DN. |
| 997 | There are quite a few fields but you can leave some blank |
| 998 | For some fields there will be a default value, |
| 999 | If you enter '.', the field will be left blank. |
| 1000 | ----- |
| 1001 | Country Name (2 letter code) [AU]:US |
| 1002 | State or Province Name (full name) [Some-State]:MyState |
| 1003 | Locality Name (eg, city) []:Some City |
| 1004 | Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Organization, Inc. |
| 1005 | Organizational Unit Name (eg, section) []:My Group |
| 1006 | Common Name (eg, YOUR name) []:myserver.mygroup.myorganization.com |
| 1007 | Email Address []:ops@myserver.mygroup.myorganization.com |
| 1008 | % |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1009 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 1010 | The disadvantage of a self-signed certificate is that it is its own root |
| 1011 | certificate, and no one else will have it in their cache of known (and trusted) |
| 1012 | root certificates. |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1013 | |
| 1014 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1015 | Examples |
| 1016 | -------- |
| 1017 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1018 | Testing for SSL support |
| 1019 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 1020 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 1021 | To test for the presence of SSL support in a Python installation, user code |
| 1022 | should use the following idiom:: |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1023 | |
| 1024 | try: |
Georg Brandl | 8a7e5da | 2011-01-02 19:07:51 +0000 | [diff] [blame] | 1025 | import ssl |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1026 | except ImportError: |
Georg Brandl | 8a7e5da | 2011-01-02 19:07:51 +0000 | [diff] [blame] | 1027 | pass |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1028 | else: |
Georg Brandl | 8a7e5da | 2011-01-02 19:07:51 +0000 | [diff] [blame] | 1029 | ... # do something that requires SSL support |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1030 | |
| 1031 | Client-side operation |
| 1032 | ^^^^^^^^^^^^^^^^^^^^^ |
| 1033 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 1034 | 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] | 1035 | |
| 1036 | import socket, ssl, pprint |
| 1037 | |
| 1038 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1039 | # require a certificate from the server |
| 1040 | ssl_sock = ssl.wrap_socket(s, |
| 1041 | ca_certs="/etc/ca_certs_file", |
| 1042 | cert_reqs=ssl.CERT_REQUIRED) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1043 | ssl_sock.connect(('www.verisign.com', 443)) |
| 1044 | |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 1045 | pprint.pprint(ssl_sock.getpeercert()) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1046 | # note that closing the SSLSocket will also close the underlying socket |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1047 | ssl_sock.close() |
| 1048 | |
Antoine Pitrou | 441ae04 | 2012-01-06 20:06:15 +0100 | [diff] [blame] | 1049 | As of January 6, 2012, the certificate printed by this program looks like |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 1050 | this:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1051 | |
Antoine Pitrou | 441ae04 | 2012-01-06 20:06:15 +0100 | [diff] [blame] | 1052 | {'issuer': ((('countryName', 'US'),), |
| 1053 | (('organizationName', 'VeriSign, Inc.'),), |
| 1054 | (('organizationalUnitName', 'VeriSign Trust Network'),), |
| 1055 | (('organizationalUnitName', |
| 1056 | 'Terms of use at https://www.verisign.com/rpa (c)06'),), |
| 1057 | (('commonName', |
| 1058 | 'VeriSign Class 3 Extended Validation SSL SGC CA'),)), |
| 1059 | 'notAfter': 'May 25 23:59:59 2012 GMT', |
| 1060 | 'notBefore': 'May 26 00:00:00 2010 GMT', |
| 1061 | 'serialNumber': '53D2BEF924A7245E83CA01E46CAA2477', |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 1062 | 'subject': ((('1.3.6.1.4.1.311.60.2.1.3', 'US'),), |
| 1063 | (('1.3.6.1.4.1.311.60.2.1.2', 'Delaware'),), |
| 1064 | (('businessCategory', 'V1.0, Clause 5.(b)'),), |
| 1065 | (('serialNumber', '2497886'),), |
| 1066 | (('countryName', 'US'),), |
| 1067 | (('postalCode', '94043'),), |
| 1068 | (('stateOrProvinceName', 'California'),), |
| 1069 | (('localityName', 'Mountain View'),), |
| 1070 | (('streetAddress', '487 East Middlefield Road'),), |
| 1071 | (('organizationName', 'VeriSign, Inc.'),), |
| 1072 | (('organizationalUnitName', ' Production Security Services'),), |
Antoine Pitrou | 441ae04 | 2012-01-06 20:06:15 +0100 | [diff] [blame] | 1073 | (('commonName', 'www.verisign.com'),)), |
| 1074 | 'subjectAltName': (('DNS', 'www.verisign.com'), |
| 1075 | ('DNS', 'verisign.com'), |
| 1076 | ('DNS', 'www.verisign.net'), |
| 1077 | ('DNS', 'verisign.net'), |
| 1078 | ('DNS', 'www.verisign.mobi'), |
| 1079 | ('DNS', 'verisign.mobi'), |
| 1080 | ('DNS', 'www.verisign.eu'), |
| 1081 | ('DNS', 'verisign.eu')), |
| 1082 | 'version': 3} |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1083 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1084 | This other example first creates an SSL context, instructs it to verify |
| 1085 | certificates sent by peers, and feeds it a set of recognized certificate |
| 1086 | authorities (CA):: |
| 1087 | |
| 1088 | >>> context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 1089 | >>> context.verify_mode = ssl.CERT_REQUIRED |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1090 | >>> context.load_verify_locations("/etc/ssl/certs/ca-bundle.crt") |
| 1091 | |
| 1092 | (it is assumed your operating system places a bundle of all CA certificates |
| 1093 | in ``/etc/ssl/certs/ca-bundle.crt``; if not, you'll get an error and have |
| 1094 | to adjust the location) |
| 1095 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 1096 | 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] | 1097 | validates the server certificate: it ensures that the server certificate |
| 1098 | was signed with one of the CA certificates, and checks the signature for |
| 1099 | correctness:: |
| 1100 | |
| 1101 | >>> conn = context.wrap_socket(socket.socket(socket.AF_INET)) |
| 1102 | >>> conn.connect(("linuxfr.org", 443)) |
| 1103 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 1104 | You should then fetch the certificate and check its fields for conformity:: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1105 | |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 1106 | >>> cert = conn.getpeercert() |
| 1107 | >>> ssl.match_hostname(cert, "linuxfr.org") |
| 1108 | |
| 1109 | Visual inspection shows that the certificate does identify the desired service |
| 1110 | (that is, the HTTPS host ``linuxfr.org``):: |
| 1111 | |
| 1112 | >>> pprint.pprint(cert) |
Antoine Pitrou | 441ae04 | 2012-01-06 20:06:15 +0100 | [diff] [blame] | 1113 | {'issuer': ((('organizationName', 'CAcert Inc.'),), |
| 1114 | (('organizationalUnitName', 'http://www.CAcert.org'),), |
| 1115 | (('commonName', 'CAcert Class 3 Root'),)), |
| 1116 | 'notAfter': 'Jun 7 21:02:24 2013 GMT', |
| 1117 | 'notBefore': 'Jun 8 21:02:24 2011 GMT', |
| 1118 | 'serialNumber': 'D3E9', |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1119 | 'subject': ((('commonName', 'linuxfr.org'),),), |
Antoine Pitrou | 441ae04 | 2012-01-06 20:06:15 +0100 | [diff] [blame] | 1120 | 'subjectAltName': (('DNS', 'linuxfr.org'), |
| 1121 | ('othername', '<unsupported>'), |
| 1122 | ('DNS', 'linuxfr.org'), |
| 1123 | ('othername', '<unsupported>'), |
| 1124 | ('DNS', 'dev.linuxfr.org'), |
| 1125 | ('othername', '<unsupported>'), |
| 1126 | ('DNS', 'prod.linuxfr.org'), |
| 1127 | ('othername', '<unsupported>'), |
| 1128 | ('DNS', 'alpha.linuxfr.org'), |
| 1129 | ('othername', '<unsupported>'), |
| 1130 | ('DNS', '*.linuxfr.org'), |
| 1131 | ('othername', '<unsupported>')), |
| 1132 | 'version': 3} |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1133 | |
| 1134 | Now that you are assured of its authenticity, you can proceed to talk with |
| 1135 | the server:: |
| 1136 | |
Antoine Pitrou | dab6426 | 2010-09-19 13:31:06 +0000 | [diff] [blame] | 1137 | >>> conn.sendall(b"HEAD / HTTP/1.0\r\nHost: linuxfr.org\r\n\r\n") |
| 1138 | >>> pprint.pprint(conn.recv(1024).split(b"\r\n")) |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1139 | [b'HTTP/1.1 302 Found', |
| 1140 | b'Date: Sun, 16 May 2010 13:43:28 GMT', |
| 1141 | b'Server: Apache/2.2', |
| 1142 | b'Location: https://linuxfr.org/pub/', |
| 1143 | b'Vary: Accept-Encoding', |
| 1144 | b'Connection: close', |
| 1145 | b'Content-Type: text/html; charset=iso-8859-1', |
| 1146 | b'', |
| 1147 | b''] |
| 1148 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1149 | See the discussion of :ref:`ssl-security` below. |
| 1150 | |
| 1151 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1152 | Server-side operation |
| 1153 | ^^^^^^^^^^^^^^^^^^^^^ |
| 1154 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1155 | For server operation, typically you'll need to have a server certificate, and |
| 1156 | private key, each in a file. You'll first create a context holding the key |
| 1157 | and the certificate, so that clients can check your authenticity. Then |
| 1158 | you'll open a socket, bind it to a port, call :meth:`listen` on it, and start |
| 1159 | waiting for clients to connect:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1160 | |
| 1161 | import socket, ssl |
| 1162 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1163 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 1164 | context.load_cert_chain(certfile="mycertfile", keyfile="mykeyfile") |
| 1165 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1166 | bindsocket = socket.socket() |
| 1167 | bindsocket.bind(('myaddr.mydomain.com', 10023)) |
| 1168 | bindsocket.listen(5) |
| 1169 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1170 | When a client connects, you'll call :meth:`accept` on the socket to get the |
| 1171 | new socket from the other end, and use the context's :meth:`SSLContext.wrap_socket` |
| 1172 | method to create a server-side SSL socket for the connection:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1173 | |
| 1174 | while True: |
Georg Brandl | 8a7e5da | 2011-01-02 19:07:51 +0000 | [diff] [blame] | 1175 | newsocket, fromaddr = bindsocket.accept() |
| 1176 | connstream = context.wrap_socket(newsocket, server_side=True) |
| 1177 | try: |
| 1178 | deal_with_client(connstream) |
| 1179 | finally: |
Antoine Pitrou | b205d58 | 2011-01-02 22:09:27 +0000 | [diff] [blame] | 1180 | connstream.shutdown(socket.SHUT_RDWR) |
Georg Brandl | 8a7e5da | 2011-01-02 19:07:51 +0000 | [diff] [blame] | 1181 | connstream.close() |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1182 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1183 | 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] | 1184 | are finished with the client (or the client is finished with you):: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1185 | |
| 1186 | def deal_with_client(connstream): |
Georg Brandl | 8a7e5da | 2011-01-02 19:07:51 +0000 | [diff] [blame] | 1187 | data = connstream.recv(1024) |
| 1188 | # empty data means the client is finished with us |
| 1189 | while data: |
| 1190 | if not do_something(connstream, data): |
| 1191 | # we'll assume do_something returns False |
| 1192 | # when we're finished with client |
| 1193 | break |
| 1194 | data = connstream.recv(1024) |
| 1195 | # finished with client |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1196 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1197 | And go back to listening for new client connections (of course, a real server |
| 1198 | would probably handle each client connection in a separate thread, or put |
| 1199 | the sockets in non-blocking mode and use an event loop). |
| 1200 | |
| 1201 | |
Antoine Pitrou | 6f5dcb1 | 2011-07-11 01:35:48 +0200 | [diff] [blame] | 1202 | .. _ssl-nonblocking: |
| 1203 | |
| 1204 | Notes on non-blocking sockets |
| 1205 | ----------------------------- |
| 1206 | |
| 1207 | When working with non-blocking sockets, there are several things you need |
| 1208 | to be aware of: |
| 1209 | |
| 1210 | - Calling :func:`~select.select` tells you that the OS-level socket can be |
| 1211 | read from (or written to), but it does not imply that there is sufficient |
| 1212 | data at the upper SSL layer. For example, only part of an SSL frame might |
| 1213 | have arrived. Therefore, you must be ready to handle :meth:`SSLSocket.recv` |
| 1214 | and :meth:`SSLSocket.send` failures, and retry after another call to |
| 1215 | :func:`~select.select`. |
| 1216 | |
| 1217 | (of course, similar provisions apply when using other primitives such as |
| 1218 | :func:`~select.poll`) |
| 1219 | |
| 1220 | - The SSL handshake itself will be non-blocking: the |
| 1221 | :meth:`SSLSocket.do_handshake` method has to be retried until it returns |
| 1222 | successfully. Here is a synopsis using :func:`~select.select` to wait for |
| 1223 | the socket's readiness:: |
| 1224 | |
| 1225 | while True: |
| 1226 | try: |
| 1227 | sock.do_handshake() |
| 1228 | break |
Antoine Pitrou | 873bf26 | 2011-10-27 23:59:03 +0200 | [diff] [blame] | 1229 | except ssl.SSLWantReadError: |
| 1230 | select.select([sock], [], []) |
| 1231 | except ssl.SSLWantWriteError: |
| 1232 | select.select([], [sock], []) |
Antoine Pitrou | 6f5dcb1 | 2011-07-11 01:35:48 +0200 | [diff] [blame] | 1233 | |
| 1234 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1235 | .. _ssl-security: |
| 1236 | |
| 1237 | Security considerations |
| 1238 | ----------------------- |
| 1239 | |
| 1240 | Verifying certificates |
| 1241 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 1242 | |
| 1243 | :const:`CERT_NONE` is the default. Since it does not authenticate the other |
| 1244 | peer, it can be insecure, especially in client mode where most of time you |
| 1245 | would like to ensure the authenticity of the server you're talking to. |
| 1246 | Therefore, when in client mode, it is highly recommended to use |
| 1247 | :const:`CERT_REQUIRED`. However, it is in itself not sufficient; you also |
Antoine Pitrou | 59fdd67 | 2010-10-08 10:37:08 +0000 | [diff] [blame] | 1248 | have to check that the server certificate, which can be obtained by calling |
| 1249 | :meth:`SSLSocket.getpeercert`, matches the desired service. For many |
| 1250 | protocols and applications, the service can be identified by the hostname; |
| 1251 | in this case, the :func:`match_hostname` function can be used. |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1252 | |
| 1253 | In server mode, if you want to authenticate your clients using the SSL layer |
| 1254 | (rather than using a higher-level authentication mechanism), you'll also have |
| 1255 | to specify :const:`CERT_REQUIRED` and similarly check the client certificate. |
| 1256 | |
| 1257 | .. note:: |
| 1258 | |
| 1259 | In client mode, :const:`CERT_OPTIONAL` and :const:`CERT_REQUIRED` are |
| 1260 | equivalent unless anonymous ciphers are enabled (they are disabled |
| 1261 | by default). |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1262 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 1263 | Protocol versions |
| 1264 | ^^^^^^^^^^^^^^^^^ |
| 1265 | |
| 1266 | SSL version 2 is considered insecure and is therefore dangerous to use. If |
| 1267 | you want maximum compatibility between clients and servers, it is recommended |
| 1268 | to use :const:`PROTOCOL_SSLv23` as the protocol version and then disable |
| 1269 | SSLv2 explicitly using the :data:`SSLContext.options` attribute:: |
| 1270 | |
| 1271 | context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
| 1272 | context.options |= ssl.OP_NO_SSLv2 |
| 1273 | |
| 1274 | The SSL context created above will allow SSLv3 and TLSv1 connections, but |
| 1275 | not SSLv2. |
| 1276 | |
Antoine Pitrou | b7ffed8 | 2012-01-04 02:53:44 +0100 | [diff] [blame] | 1277 | Cipher selection |
| 1278 | ^^^^^^^^^^^^^^^^ |
| 1279 | |
| 1280 | If you have advanced security requirements, fine-tuning of the ciphers |
| 1281 | enabled when negotiating a SSL session is possible through the |
| 1282 | :meth:`SSLContext.set_ciphers` method. Starting from Python 3.2.3, the |
| 1283 | ssl module disables certain weak ciphers by default, but you may want |
| 1284 | to further restrict the cipher choice. For example:: |
| 1285 | |
| 1286 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 1287 | context.set_ciphers('HIGH:!aNULL:!eNULL') |
| 1288 | |
| 1289 | The ``!aNULL:!eNULL`` part of the cipher spec is necessary to disable ciphers |
| 1290 | which don't provide both encryption and authentication. Be sure to read |
| 1291 | OpenSSL's documentation about the `cipher list |
| 1292 | format <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_. |
| 1293 | If you want to check which ciphers are enabled by a given cipher list, |
| 1294 | use the ``openssl ciphers`` command on your system. |
| 1295 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1296 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1297 | .. seealso:: |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1298 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1299 | Class :class:`socket.socket` |
| 1300 | Documentation of underlying :mod:`socket` class |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1301 | |
Antoine Pitrou | f394e47 | 2011-10-07 16:58:07 +0200 | [diff] [blame] | 1302 | `TLS (Transport Layer Security) and SSL (Secure Socket Layer) <http://www3.rad.com/networks/applications/secure/tls.htm>`_ |
| 1303 | Debby Koren |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1304 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1305 | `RFC 1422: Privacy Enhancement for Internet Electronic Mail: Part II: Certificate-Based Key Management <http://www.ietf.org/rfc/rfc1422>`_ |
| 1306 | Steve Kent |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1307 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1308 | `RFC 1750: Randomness Recommendations for Security <http://www.ietf.org/rfc/rfc1750>`_ |
| 1309 | D. Eastlake et. al. |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 1310 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1311 | `RFC 3280: Internet X.509 Public Key Infrastructure Certificate and CRL Profile <http://www.ietf.org/rfc/rfc3280>`_ |
| 1312 | Housley et. al. |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 1313 | |
| 1314 | `RFC 4366: Transport Layer Security (TLS) Extensions <http://www.ietf.org/rfc/rfc4366>`_ |
| 1315 | Blake-Wilson et. al. |