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