Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 1 | :mod:`ssl` --- SSL wrapper for socket objects |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 2 | ============================================= |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: ssl |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 5 | :synopsis: SSL wrapper for socket objects |
| 6 | |
| 7 | .. moduleauthor:: Bill Janssen <bill.janssen@gmail.com> |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 8 | .. sectionauthor:: Bill Janssen <bill.janssen@gmail.com> |
| 9 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 11 | .. index:: single: OpenSSL; (use in module ssl) |
| 12 | |
| 13 | .. index:: TLS, SSL, Transport Layer Security, Secure Sockets Layer |
| 14 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 15 | This module provides access to Transport Layer Security (often known as "Secure |
| 16 | Sockets Layer") encryption and peer authentication facilities for network |
| 17 | sockets, both client-side and server-side. This module uses the OpenSSL |
| 18 | library. It is available on all modern Unix systems, Windows, Mac OS X, and |
| 19 | probably additional platforms, as long as OpenSSL is installed on that platform. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 20 | |
| 21 | .. note:: |
| 22 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 23 | Some behavior may be platform dependent, since calls are made to the |
| 24 | operating system socket APIs. The installed version of OpenSSL may also |
| 25 | cause variations in behavior. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 26 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 27 | This section documents the objects and functions in the ``ssl`` module; for more |
| 28 | general information about TLS, SSL, and certificates, the reader is referred to |
| 29 | the documents in the "See Also" section at the bottom. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 30 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 31 | This module provides a class, :class:`ssl.SSLSocket`, which is derived from the |
| 32 | :class:`socket.socket` type, and provides a socket-like wrapper that also |
| 33 | encrypts and decrypts the data going over the socket with SSL. It supports |
| 34 | additional :meth:`read` and :meth:`write` methods, along with a method, |
| 35 | :meth:`getpeercert`, to retrieve the certificate of the other side of the |
| 36 | connection, and a method, :meth:`cipher`, to retrieve the cipher being used for |
| 37 | the secure connection. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 38 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 39 | Functions, Constants, and Exceptions |
| 40 | ------------------------------------ |
| 41 | |
| 42 | .. exception:: SSLError |
| 43 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 44 | Raised to signal an error from the underlying SSL implementation. This |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 45 | signifies some problem in the higher-level encryption and authentication |
| 46 | layer that's superimposed on the underlying network connection. This error |
| 47 | is a subtype of :exc:`socket.error`, which in turn is a subtype of |
| 48 | :exc:`IOError`. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 49 | |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 50 | .. 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] | 51 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 52 | Takes an instance ``sock`` of :class:`socket.socket`, and returns an instance |
| 53 | of :class:`ssl.SSLSocket`, a subtype of :class:`socket.socket`, which wraps |
| 54 | the underlying socket in an SSL context. For client-side sockets, the |
| 55 | context construction is lazy; if the underlying socket isn't connected yet, |
| 56 | the context construction will be performed after :meth:`connect` is called on |
| 57 | the socket. For server-side sockets, if the socket has no remote peer, it is |
| 58 | assumed to be a listening socket, and the server-side SSL wrapping is |
| 59 | automatically performed on client connections accepted via the :meth:`accept` |
| 60 | method. :func:`wrap_socket` may raise :exc:`SSLError`. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 61 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 62 | The ``keyfile`` and ``certfile`` parameters specify optional files which |
| 63 | contain a certificate to be used to identify the local side of the |
| 64 | connection. See the discussion of :ref:`ssl-certificates` for more |
| 65 | information on how the certificate is stored in the ``certfile``. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 66 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 67 | Often the private key is stored in the same file as the certificate; in this |
| 68 | case, only the ``certfile`` parameter need be passed. If the private key is |
| 69 | stored in a separate file, both parameters must be used. If the private key |
| 70 | is stored in the ``certfile``, it should come before the first certificate in |
| 71 | the certificate chain:: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 72 | |
| 73 | -----BEGIN RSA PRIVATE KEY----- |
| 74 | ... (private key in base64 encoding) ... |
| 75 | -----END RSA PRIVATE KEY----- |
| 76 | -----BEGIN CERTIFICATE----- |
| 77 | ... (certificate in base64 PEM encoding) ... |
| 78 | -----END CERTIFICATE----- |
| 79 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 80 | The parameter ``server_side`` is a boolean which identifies whether |
| 81 | server-side or client-side behavior is desired from this socket. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 82 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 83 | The parameter ``cert_reqs`` specifies whether a certificate is required from |
| 84 | the other side of the connection, and whether it will be validated if |
| 85 | provided. It must be one of the three values :const:`CERT_NONE` |
| 86 | (certificates ignored), :const:`CERT_OPTIONAL` (not required, but validated |
| 87 | if provided), or :const:`CERT_REQUIRED` (required and validated). If the |
| 88 | value of this parameter is not :const:`CERT_NONE`, then the ``ca_certs`` |
| 89 | parameter must point to a file of CA certificates. |
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 ``ca_certs`` file contains a set of concatenated "certification |
| 92 | authority" certificates, which are used to validate certificates passed from |
| 93 | the other end of the connection. See the discussion of |
| 94 | :ref:`ssl-certificates` for more information about how to arrange the |
| 95 | certificates in this file. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 96 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 97 | The parameter ``ssl_version`` specifies which version of the SSL protocol to |
| 98 | use. Typically, the server chooses a particular protocol version, and the |
| 99 | client must adapt to the server's choice. Most of the versions are not |
| 100 | interoperable with the other versions. If not specified, for client-side |
| 101 | operation, the default SSL version is SSLv3; for server-side operation, |
| 102 | SSLv23. These version selections provide the most compatibility with other |
| 103 | versions. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 104 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 105 | Here's a table showing which versions in a client (down the side) can connect |
| 106 | to which versions in a server (along the top): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 107 | |
| 108 | .. table:: |
| 109 | |
| 110 | ======================== ========= ========= ========== ========= |
| 111 | *client* / **server** **SSLv2** **SSLv3** **SSLv23** **TLSv1** |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 112 | ------------------------ --------- --------- ---------- --------- |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 113 | *SSLv2* yes no yes no |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 114 | *SSLv3* yes yes yes no |
| 115 | *SSLv23* yes no yes no |
| 116 | *TLSv1* no no yes yes |
| 117 | ======================== ========= ========= ========== ========= |
| 118 | |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 119 | .. note:: |
| 120 | |
| 121 | This information varies depending on the version of OpenSSL. |
| 122 | For instance, in some older versions of OpenSSL (such as 0.9.7l on |
| 123 | OS X 10.4), an SSLv2 client could not connect to an SSLv23 server. |
| 124 | Conversely, starting from 1.0.0, an SSLv23 client will actually |
| 125 | try the SSLv3 protocol unless you explicitly enable SSLv2 ciphers. |
| 126 | |
| 127 | The parameter ``ciphers`` sets the available ciphers for this SSL object. |
| 128 | It should be a string in the `OpenSSL cipher list format |
| 129 | <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 130 | |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 131 | The parameter ``do_handshake_on_connect`` specifies whether to do the SSL |
| 132 | handshake automatically after doing a :meth:`socket.connect`, or whether the |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 133 | application program will call it explicitly, by invoking the |
| 134 | :meth:`SSLSocket.do_handshake` method. Calling |
| 135 | :meth:`SSLSocket.do_handshake` explicitly gives the program control over the |
| 136 | blocking behavior of the socket I/O involved in the handshake. |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 137 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 138 | The parameter ``suppress_ragged_eofs`` specifies how the |
| 139 | :meth:`SSLSocket.read` method should signal unexpected EOF from the other end |
| 140 | of the connection. If specified as :const:`True` (the default), it returns a |
| 141 | normal EOF in response to unexpected EOF errors raised from the underlying |
| 142 | socket; if :const:`False`, it will raise the exceptions back to the caller. |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 143 | |
Ezio Melotti | 4d5195b | 2010-04-20 10:57:44 +0000 | [diff] [blame] | 144 | .. versionchanged:: 3.2 |
Antoine Pitrou | 2d9cb9c | 2010-04-17 17:40:45 +0000 | [diff] [blame] | 145 | New optional argument *ciphers*. |
| 146 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 147 | .. function:: RAND_status() |
| 148 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 149 | Returns True if the SSL pseudo-random number generator has been seeded with |
| 150 | 'enough' randomness, and False otherwise. You can use :func:`ssl.RAND_egd` |
| 151 | and :func:`ssl.RAND_add` to increase the randomness of the pseudo-random |
| 152 | number generator. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 153 | |
| 154 | .. function:: RAND_egd(path) |
| 155 | |
| 156 | If you are running an entropy-gathering daemon (EGD) somewhere, and ``path`` |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 157 | is the pathname of a socket connection open to it, this will read 256 bytes |
| 158 | of randomness from the socket, and add it to the SSL pseudo-random number |
| 159 | generator to increase the security of generated secret keys. This is |
| 160 | typically only necessary on systems without better sources of randomness. |
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 | See http://egd.sourceforge.net/ or http://prngd.sourceforge.net/ for sources |
| 163 | of entropy-gathering daemons. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 164 | |
| 165 | .. function:: RAND_add(bytes, entropy) |
| 166 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 167 | Mixes the given ``bytes`` into the SSL pseudo-random number generator. The |
| 168 | parameter ``entropy`` (a float) is a lower bound on the entropy contained in |
| 169 | string (so you can always use :const:`0.0`). See :rfc:`1750` for more |
| 170 | information on sources of entropy. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 171 | |
| 172 | .. function:: cert_time_to_seconds(timestring) |
| 173 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 174 | Returns a floating-point value containing a normal seconds-after-the-epoch |
| 175 | time value, given the time-string representing the "notBefore" or "notAfter" |
| 176 | date from a certificate. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 177 | |
| 178 | Here's an example:: |
| 179 | |
| 180 | >>> import ssl |
| 181 | >>> ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT") |
| 182 | 1178694000.0 |
| 183 | >>> import time |
| 184 | >>> time.ctime(ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT")) |
| 185 | 'Wed May 9 00:00:00 2007' |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 186 | >>> |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 187 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 188 | .. function:: get_server_certificate(addr, ssl_version=PROTOCOL_SSLv3, ca_certs=None) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 189 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 190 | Given the address ``addr`` of an SSL-protected server, as a (*hostname*, |
| 191 | *port-number*) pair, fetches the server's certificate, and returns it as a |
| 192 | PEM-encoded string. If ``ssl_version`` is specified, uses that version of |
| 193 | the SSL protocol to attempt to connect to the server. If ``ca_certs`` is |
| 194 | specified, it should be a file containing a list of root certificates, the |
| 195 | same format as used for the same parameter in :func:`wrap_socket`. The call |
| 196 | will attempt to validate the server certificate against that set of root |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 197 | certificates, and will fail if the validation attempt fails. |
| 198 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 199 | .. function:: DER_cert_to_PEM_cert(DER_cert_bytes) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 200 | |
| 201 | Given a certificate as a DER-encoded blob of bytes, returns a PEM-encoded |
| 202 | string version of the same certificate. |
| 203 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 204 | .. function:: PEM_cert_to_DER_cert(PEM_cert_string) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 205 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 206 | Given a certificate as an ASCII PEM string, returns a DER-encoded sequence of |
| 207 | bytes for that same certificate. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 208 | |
| 209 | .. data:: CERT_NONE |
| 210 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 211 | Value to pass to the ``cert_reqs`` parameter to :func:`sslobject` when no |
| 212 | certificates will be required or validated from the other side of the socket |
| 213 | connection. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 214 | |
| 215 | .. data:: CERT_OPTIONAL |
| 216 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 217 | Value to pass to the ``cert_reqs`` parameter to :func:`sslobject` when no |
| 218 | certificates will be required from the other side of the socket connection, |
| 219 | but if they are provided, will be validated. Note that use of this setting |
| 220 | requires a valid certificate validation file also be passed as a value of the |
| 221 | ``ca_certs`` parameter. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 222 | |
| 223 | .. data:: CERT_REQUIRED |
| 224 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 225 | Value to pass to the ``cert_reqs`` parameter to :func:`sslobject` when |
| 226 | certificates will be required from the other side of the socket connection. |
| 227 | Note that use of this setting requires a valid certificate validation file |
| 228 | also be passed as a value of the ``ca_certs`` parameter. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 229 | |
| 230 | .. data:: PROTOCOL_SSLv2 |
| 231 | |
| 232 | Selects SSL version 2 as the channel encryption protocol. |
| 233 | |
Antoine Pitrou | 8eac60d | 2010-05-16 14:19:41 +0000 | [diff] [blame^] | 234 | .. warning:: |
| 235 | |
| 236 | SSL version 2 is insecure. Its use is highly discouraged. |
| 237 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 238 | .. data:: PROTOCOL_SSLv23 |
| 239 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 240 | Selects SSL version 2 or 3 as the channel encryption protocol. This is a |
| 241 | setting to use with servers for maximum compatibility with the other end of |
| 242 | an SSL connection, but it may cause the specific ciphers chosen for the |
| 243 | encryption to be of fairly low quality. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 244 | |
| 245 | .. data:: PROTOCOL_SSLv3 |
| 246 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 247 | Selects SSL version 3 as the channel encryption protocol. For clients, this |
| 248 | is the maximally compatible SSL variant. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 249 | |
| 250 | .. data:: PROTOCOL_TLSv1 |
| 251 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 252 | Selects TLS version 1 as the channel encryption protocol. This is the most |
| 253 | modern version, and probably the best choice for maximum protection, if both |
| 254 | sides can speak it. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 255 | |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 256 | .. data:: OPENSSL_VERSION |
| 257 | |
| 258 | The version string of the OpenSSL library loaded by the interpreter:: |
| 259 | |
| 260 | >>> ssl.OPENSSL_VERSION |
| 261 | 'OpenSSL 0.9.8k 25 Mar 2009' |
| 262 | |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 263 | .. versionadded:: 3.2 |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 264 | |
| 265 | .. data:: OPENSSL_VERSION_INFO |
| 266 | |
| 267 | A tuple of five integers representing version information about the |
| 268 | OpenSSL library:: |
| 269 | |
| 270 | >>> ssl.OPENSSL_VERSION_INFO |
| 271 | (0, 9, 8, 11, 15) |
| 272 | |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 273 | .. versionadded:: 3.2 |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 274 | |
| 275 | .. data:: OPENSSL_VERSION_NUMBER |
| 276 | |
| 277 | The raw version number of the OpenSSL library, as a single integer:: |
| 278 | |
| 279 | >>> ssl.OPENSSL_VERSION_NUMBER |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 280 | 9470143 |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 281 | >>> hex(ssl.OPENSSL_VERSION_NUMBER) |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 282 | '0x9080bf' |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 283 | |
Antoine Pitrou | 43a94c31 | 2010-04-05 21:44:48 +0000 | [diff] [blame] | 284 | .. versionadded:: 3.2 |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 285 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 286 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 287 | SSLSocket Objects |
| 288 | ----------------- |
| 289 | |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 290 | .. method:: SSLSocket.read(nbytes=1024, buffer=None) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 291 | |
| 292 | Reads up to ``nbytes`` bytes from the SSL-encrypted channel and returns them. |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 293 | If the ``buffer`` is specified, it will attempt to read into the buffer the |
| 294 | minimum of the size of the buffer and ``nbytes``, if that is specified. If |
| 295 | no buffer is specified, an immutable buffer is allocated and returned with |
| 296 | the data read from the socket. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 297 | |
| 298 | .. method:: SSLSocket.write(data) |
| 299 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 300 | Writes the ``data`` to the other side of the connection, using the SSL |
| 301 | channel to encrypt. Returns the number of bytes written. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 302 | |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 303 | .. method:: SSLSocket.do_handshake() |
| 304 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 305 | Performs the SSL setup handshake. If the socket is non-blocking, this method |
| 306 | may raise :exc:`SSLError` with the value of the exception instance's |
| 307 | ``args[0]`` being either :const:`SSL_ERROR_WANT_READ` or |
| 308 | :const:`SSL_ERROR_WANT_WRITE`, and should be called again until it stops |
| 309 | raising those exceptions. Here's an example of how to do that:: |
Bill Janssen | 48dc27c | 2007-12-05 03:38:10 +0000 | [diff] [blame] | 310 | |
| 311 | while True: |
| 312 | try: |
| 313 | sock.do_handshake() |
| 314 | break |
| 315 | except ssl.SSLError as err: |
| 316 | if err.args[0] == ssl.SSL_ERROR_WANT_READ: |
| 317 | select.select([sock], [], []) |
| 318 | elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE: |
| 319 | select.select([], [sock], []) |
| 320 | else: |
| 321 | raise |
| 322 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 323 | .. method:: SSLSocket.getpeercert(binary_form=False) |
| 324 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 325 | If there is no certificate for the peer on the other end of the connection, |
| 326 | returns ``None``. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 327 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 328 | If the parameter ``binary_form`` is :const:`False`, and a certificate was |
| 329 | received from the peer, this method returns a :class:`dict` instance. If the |
| 330 | certificate was not validated, the dict is empty. If the certificate was |
| 331 | validated, it returns a dict with the keys ``subject`` (the principal for |
| 332 | which the certificate was issued), and ``notAfter`` (the time after which the |
| 333 | certificate should not be trusted). The certificate was already validated, |
| 334 | so the ``notBefore`` and ``issuer`` fields are not returned. If a |
| 335 | certificate contains an instance of the *Subject Alternative Name* extension |
| 336 | (see :rfc:`3280`), there will also be a ``subjectAltName`` key in the |
| 337 | dictionary. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 338 | |
| 339 | The "subject" field is a tuple containing the sequence of relative |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 340 | distinguished names (RDNs) given in the certificate's data structure for the |
| 341 | principal, and each RDN is a sequence of name-value pairs:: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 342 | |
| 343 | {'notAfter': 'Feb 16 16:54:50 2013 GMT', |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 344 | 'subject': ((('countryName', 'US'),), |
| 345 | (('stateOrProvinceName', 'Delaware'),), |
| 346 | (('localityName', 'Wilmington'),), |
| 347 | (('organizationName', 'Python Software Foundation'),), |
| 348 | (('organizationalUnitName', 'SSL'),), |
| 349 | (('commonName', 'somemachine.python.org'),))} |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 350 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 351 | If the ``binary_form`` parameter is :const:`True`, and a certificate was |
| 352 | provided, this method returns the DER-encoded form of the entire certificate |
| 353 | as a sequence of bytes, or :const:`None` if the peer did not provide a |
| 354 | certificate. This return value is independent of validation; if validation |
| 355 | was required (:const:`CERT_OPTIONAL` or :const:`CERT_REQUIRED`), it will have |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 356 | been validated, but if :const:`CERT_NONE` was used to establish the |
| 357 | connection, the certificate, if present, will not have been validated. |
| 358 | |
| 359 | .. method:: SSLSocket.cipher() |
| 360 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 361 | Returns a three-value tuple containing the name of the cipher being used, the |
| 362 | version of the SSL protocol that defines its use, and the number of secret |
| 363 | bits being used. If no connection has been established, returns ``None``. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 364 | |
| 365 | |
Benjamin Peterson | 4aeec04 | 2008-08-19 21:42:13 +0000 | [diff] [blame] | 366 | .. method:: SSLSocket.unwrap() |
| 367 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 368 | Performs the SSL shutdown handshake, which removes the TLS layer from the |
| 369 | underlying socket, and returns the underlying socket object. This can be |
| 370 | used to go from encrypted operation over a connection to unencrypted. The |
| 371 | returned socket should always be used for further communication with the |
| 372 | other side of the connection, rather than the original socket. |
Benjamin Peterson | 4aeec04 | 2008-08-19 21:42:13 +0000 | [diff] [blame] | 373 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 374 | .. index:: single: certificates |
| 375 | |
| 376 | .. index:: single: X509 certificate |
| 377 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 378 | .. _ssl-certificates: |
| 379 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 380 | Certificates |
| 381 | ------------ |
| 382 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 383 | Certificates in general are part of a public-key / private-key system. In this |
| 384 | system, each *principal*, (which may be a machine, or a person, or an |
| 385 | organization) is assigned a unique two-part encryption key. One part of the key |
| 386 | is public, and is called the *public key*; the other part is kept secret, and is |
| 387 | called the *private key*. The two parts are related, in that if you encrypt a |
| 388 | message with one of the parts, you can decrypt it with the other part, and |
| 389 | **only** with the other part. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 390 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 391 | A certificate contains information about two principals. It contains the name |
| 392 | of a *subject*, and the subject's public key. It also contains a statement by a |
| 393 | second principal, the *issuer*, that the subject is who he claims to be, and |
| 394 | that this is indeed the subject's public key. The issuer's statement is signed |
| 395 | with the issuer's private key, which only the issuer knows. However, anyone can |
| 396 | verify the issuer's statement by finding the issuer's public key, decrypting the |
| 397 | statement with it, and comparing it to the other information in the certificate. |
| 398 | The certificate also contains information about the time period over which it is |
| 399 | valid. This is expressed as two fields, called "notBefore" and "notAfter". |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 400 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 401 | In the Python use of certificates, a client or server can use a certificate to |
| 402 | prove who they are. The other side of a network connection can also be required |
| 403 | to produce a certificate, and that certificate can be validated to the |
| 404 | satisfaction of the client or server that requires such validation. The |
| 405 | connection attempt can be set to raise an exception if the validation fails. |
| 406 | Validation is done automatically, by the underlying OpenSSL framework; the |
| 407 | application need not concern itself with its mechanics. But the application |
| 408 | does usually need to provide sets of certificates to allow this process to take |
| 409 | place. |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 410 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 411 | Python uses files to contain certificates. They should be formatted as "PEM" |
| 412 | (see :rfc:`1422`), which is a base-64 encoded form wrapped with a header line |
| 413 | and a footer line:: |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 414 | |
| 415 | -----BEGIN CERTIFICATE----- |
| 416 | ... (certificate in base64 PEM encoding) ... |
| 417 | -----END CERTIFICATE----- |
| 418 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 419 | The Python files which contain certificates can contain a sequence of |
| 420 | certificates, sometimes called a *certificate chain*. This chain should start |
| 421 | with the specific certificate for the principal who "is" the client or server, |
| 422 | and then the certificate for the issuer of that certificate, and then the |
| 423 | certificate for the issuer of *that* certificate, and so on up the chain till |
| 424 | you get to a certificate which is *self-signed*, that is, a certificate which |
| 425 | has the same subject and issuer, sometimes called a *root certificate*. The |
| 426 | certificates should just be concatenated together in the certificate file. For |
| 427 | example, suppose we had a three certificate chain, from our server certificate |
| 428 | to the certificate of the certification authority that signed our server |
| 429 | certificate, to the root certificate of the agency which issued the |
| 430 | certification authority's certificate:: |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 431 | |
| 432 | -----BEGIN CERTIFICATE----- |
| 433 | ... (certificate for your server)... |
| 434 | -----END CERTIFICATE----- |
| 435 | -----BEGIN CERTIFICATE----- |
| 436 | ... (the certificate for the CA)... |
| 437 | -----END CERTIFICATE----- |
| 438 | -----BEGIN CERTIFICATE----- |
| 439 | ... (the root certificate for the CA's issuer)... |
| 440 | -----END CERTIFICATE----- |
| 441 | |
| 442 | If you are going to require validation of the other side of the connection's |
| 443 | 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] | 444 | chains for each issuer you are willing to trust. Again, this file just contains |
| 445 | these chains concatenated together. For validation, Python will use the first |
| 446 | chain it finds in the file which matches. Some "standard" root certificates are |
| 447 | available from various certification authorities: `CACert.org |
| 448 | <http://www.cacert.org/index.php?id=3>`_, `Thawte |
| 449 | <http://www.thawte.com/roots/>`_, `Verisign |
| 450 | <http://www.verisign.com/support/roots.html>`_, `Positive SSL |
| 451 | <http://www.PositiveSSL.com/ssl-certificate-support/cert_installation/UTN-USERFirst-Hardware.crt>`_ |
| 452 | (used by python.org), `Equifax and GeoTrust |
| 453 | <http://www.geotrust.com/resources/root_certificates/index.asp>`_. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 454 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 455 | In general, if you are using SSL3 or TLS1, you don't need to put the full chain |
| 456 | in your "CA certs" file; you only need the root certificates, and the remote |
| 457 | peer is supposed to furnish the other certificates necessary to chain from its |
| 458 | certificate to a root certificate. See :rfc:`4158` for more discussion of the |
| 459 | way in which certification chains can be built. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 460 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 461 | If you are going to create a server that provides SSL-encrypted connection |
| 462 | services, you will need to acquire a certificate for that service. There are |
| 463 | many ways of acquiring appropriate certificates, such as buying one from a |
| 464 | certification authority. Another common practice is to generate a self-signed |
| 465 | certificate. The simplest way to do this is with the OpenSSL package, using |
| 466 | something like the following:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 467 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 468 | % openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem |
| 469 | Generating a 1024 bit RSA private key |
| 470 | .......++++++ |
| 471 | .............................++++++ |
| 472 | writing new private key to 'cert.pem' |
| 473 | ----- |
| 474 | You are about to be asked to enter information that will be incorporated |
| 475 | into your certificate request. |
| 476 | What you are about to enter is what is called a Distinguished Name or a DN. |
| 477 | There are quite a few fields but you can leave some blank |
| 478 | For some fields there will be a default value, |
| 479 | If you enter '.', the field will be left blank. |
| 480 | ----- |
| 481 | Country Name (2 letter code) [AU]:US |
| 482 | State or Province Name (full name) [Some-State]:MyState |
| 483 | Locality Name (eg, city) []:Some City |
| 484 | Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Organization, Inc. |
| 485 | Organizational Unit Name (eg, section) []:My Group |
| 486 | Common Name (eg, YOUR name) []:myserver.mygroup.myorganization.com |
| 487 | Email Address []:ops@myserver.mygroup.myorganization.com |
| 488 | % |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 489 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 490 | The disadvantage of a self-signed certificate is that it is its own root |
| 491 | certificate, and no one else will have it in their cache of known (and trusted) |
| 492 | root certificates. |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 493 | |
| 494 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 495 | Examples |
| 496 | -------- |
| 497 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 498 | Testing for SSL support |
| 499 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 500 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 501 | To test for the presence of SSL support in a Python installation, user code |
| 502 | should use the following idiom:: |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 503 | |
| 504 | try: |
| 505 | import ssl |
| 506 | except ImportError: |
| 507 | pass |
| 508 | else: |
| 509 | [ do something that requires SSL support ] |
| 510 | |
| 511 | Client-side operation |
| 512 | ^^^^^^^^^^^^^^^^^^^^^ |
| 513 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 514 | This example connects to an SSL server, prints the server's address and |
| 515 | certificate, sends some bytes, and reads part of the response:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 516 | |
| 517 | import socket, ssl, pprint |
| 518 | |
| 519 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 520 | |
| 521 | # require a certificate from the server |
| 522 | ssl_sock = ssl.wrap_socket(s, |
| 523 | ca_certs="/etc/ca_certs_file", |
| 524 | cert_reqs=ssl.CERT_REQUIRED) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 525 | |
| 526 | ssl_sock.connect(('www.verisign.com', 443)) |
| 527 | |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 528 | print(repr(ssl_sock.getpeername())) |
| 529 | pprint.pprint(ssl_sock.getpeercert()) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 530 | print(pprint.pformat(ssl_sock.getpeercert())) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 531 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 532 | # Set a simple HTTP request -- use http.client in actual code. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 533 | ssl_sock.write("""GET / HTTP/1.0\r |
| 534 | Host: www.verisign.com\r\n\r\n""") |
| 535 | |
| 536 | # Read a chunk of data. Will not necessarily |
| 537 | # read all the data returned by the server. |
| 538 | data = ssl_sock.read() |
| 539 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 540 | # note that closing the SSLSocket will also close the underlying socket |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 541 | ssl_sock.close() |
| 542 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 543 | As of September 6, 2007, the certificate printed by this program looked like |
| 544 | this:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 545 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 546 | {'notAfter': 'May 8 23:59:59 2009 GMT', |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 547 | 'subject': ((('serialNumber', '2497886'),), |
| 548 | (('1.3.6.1.4.1.311.60.2.1.3', 'US'),), |
| 549 | (('1.3.6.1.4.1.311.60.2.1.2', 'Delaware'),), |
| 550 | (('countryName', 'US'),), |
| 551 | (('postalCode', '94043'),), |
| 552 | (('stateOrProvinceName', 'California'),), |
| 553 | (('localityName', 'Mountain View'),), |
| 554 | (('streetAddress', '487 East Middlefield Road'),), |
| 555 | (('organizationName', 'VeriSign, Inc.'),), |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 556 | (('organizationalUnitName', |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 557 | 'Production Security Services'),), |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 558 | (('organizationalUnitName', |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 559 | 'Terms of use at www.verisign.com/rpa (c)06'),), |
| 560 | (('commonName', 'www.verisign.com'),))} |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 561 | |
| 562 | which is a fairly poorly-formed ``subject`` field. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 563 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 564 | Server-side operation |
| 565 | ^^^^^^^^^^^^^^^^^^^^^ |
| 566 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 567 | For server operation, typically you'd need to have a server certificate, and |
| 568 | private key, each in a file. You'd open a socket, bind it to a port, call |
| 569 | :meth:`listen` on it, then start waiting for clients to connect:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 570 | |
| 571 | import socket, ssl |
| 572 | |
| 573 | bindsocket = socket.socket() |
| 574 | bindsocket.bind(('myaddr.mydomain.com', 10023)) |
| 575 | bindsocket.listen(5) |
| 576 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 577 | When one did, you'd call :meth:`accept` on the socket to get the new socket from |
| 578 | the other end, and use :func:`wrap_socket` to create a server-side SSL context |
| 579 | for it:: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 580 | |
| 581 | while True: |
| 582 | newsocket, fromaddr = bindsocket.accept() |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 583 | connstream = ssl.wrap_socket(newsocket, |
| 584 | server_side=True, |
| 585 | certfile="mycertfile", |
| 586 | keyfile="mykeyfile", |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 587 | ssl_version=ssl.PROTOCOL_TLSv1) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 588 | deal_with_client(connstream) |
| 589 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 590 | Then you'd read data from the ``connstream`` and do something with it till you |
| 591 | are finished with the client (or the client is finished with you):: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 592 | |
| 593 | def deal_with_client(connstream): |
| 594 | |
| 595 | data = connstream.read() |
| 596 | # null data means the client is finished with us |
| 597 | while data: |
| 598 | if not do_something(connstream, data): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 599 | # we'll assume do_something returns False |
| 600 | # when we're finished with client |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 601 | break |
| 602 | data = connstream.read() |
| 603 | # finished with client |
| 604 | connstream.close() |
| 605 | |
| 606 | And go back to listening for new client connections. |
| 607 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 608 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 609 | .. seealso:: |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 610 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 611 | Class :class:`socket.socket` |
| 612 | Documentation of underlying :mod:`socket` class |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 613 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 614 | `Introducing SSL and Certificates using OpenSSL <http://old.pseudonym.org/ssl/wwwj-index.html>`_ |
| 615 | Frederick J. Hirsch |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 616 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 617 | `RFC 1422: Privacy Enhancement for Internet Electronic Mail: Part II: Certificate-Based Key Management <http://www.ietf.org/rfc/rfc1422>`_ |
| 618 | Steve Kent |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 619 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 620 | `RFC 1750: Randomness Recommendations for Security <http://www.ietf.org/rfc/rfc1750>`_ |
| 621 | D. Eastlake et. al. |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 622 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 623 | `RFC 3280: Internet X.509 Public Key Infrastructure Certificate and CRL Profile <http://www.ietf.org/rfc/rfc3280>`_ |
| 624 | Housley et. al. |