blob: 1d4f9ca980d41fc4707d1faf404a77c5283cb50a [file] [log] [blame]
Antoine Pitroue1bc8982011-01-02 22:12:22 +00001:mod:`ssl` --- TLS/SSL wrapper for socket objects
2=================================================
Thomas Woutersed03b412007-08-28 21:37:11 +00003
4.. module:: ssl
Antoine Pitroue1bc8982011-01-02 22:12:22 +00005 :synopsis: TLS/SSL wrapper for socket objects
Thomas Wouters47b49bf2007-08-30 22:15:33 +00006
7.. moduleauthor:: Bill Janssen <bill.janssen@gmail.com>
Thomas Wouters47b49bf2007-08-30 22:15:33 +00008.. sectionauthor:: Bill Janssen <bill.janssen@gmail.com>
9
Thomas Woutersed03b412007-08-28 21:37:11 +000010
Thomas Wouters1b7f8912007-09-19 03:06:30 +000011.. index:: single: OpenSSL; (use in module ssl)
12
13.. index:: TLS, SSL, Transport Layer Security, Secure Sockets Layer
14
Raymond Hettinger469271d2011-01-27 20:38:46 +000015**Source code:** :source:`Lib/ssl.py`
16
17--------------
18
Georg Brandl7f01a132009-09-16 15:58:14 +000019This module provides access to Transport Layer Security (often known as "Secure
20Sockets Layer") encryption and peer authentication facilities for network
21sockets, both client-side and server-side. This module uses the OpenSSL
22library. It is available on all modern Unix systems, Windows, Mac OS X, and
23probably additional platforms, as long as OpenSSL is installed on that platform.
Thomas Woutersed03b412007-08-28 21:37:11 +000024
25.. note::
26
Georg Brandl7f01a132009-09-16 15:58:14 +000027 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 Woutersed03b412007-08-28 21:37:11 +000030
Georg Brandl7f01a132009-09-16 15:58:14 +000031This section documents the objects and functions in the ``ssl`` module; for more
32general information about TLS, SSL, and certificates, the reader is referred to
33the documents in the "See Also" section at the bottom.
Thomas Woutersed03b412007-08-28 21:37:11 +000034
Georg Brandl7f01a132009-09-16 15:58:14 +000035This 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
37encrypts and decrypts the data going over the socket with SSL. It supports
Antoine Pitroudab64262010-09-19 13:31:06 +000038additional methods such as :meth:`getpeercert`, which retrieves the
39certificate of the other side of the connection, and :meth:`cipher`,which
40retrieves the cipher being used for the secure connection.
Thomas Woutersed03b412007-08-28 21:37:11 +000041
Antoine Pitrou152efa22010-05-16 18:19:27 +000042For more sophisticated applications, the :class:`ssl.SSLContext` class
43helps manage settings and certificates, which can then be inherited
44by SSL sockets created through the :meth:`SSLContext.wrap_socket` method.
45
46
Thomas Wouters1b7f8912007-09-19 03:06:30 +000047Functions, Constants, and Exceptions
48------------------------------------
49
50.. exception:: SSLError
51
Antoine Pitrou59fdd672010-10-08 10:37:08 +000052 Raised to signal an error from the underlying SSL implementation
53 (currently provided by the OpenSSL library). This signifies some
54 problem in the higher-level encryption and authentication layer that's
55 superimposed on the underlying network connection. This error
Antoine Pitrou5574c302011-10-12 17:53:43 +020056 is a subtype of :exc:`OSError`. The error code and message of
57 :exc:`SSLError` instances are provided by the OpenSSL library.
58
59 .. versionchanged:: 3.3
60 :exc:`SSLError` used to be a subtype of :exc:`socket.error`.
Antoine Pitrou59fdd672010-10-08 10:37:08 +000061
Antoine Pitrou3b36fb12012-06-22 21:11:52 +020062 .. attribute:: library
63
64 A string mnemonic designating the OpenSSL submodule in which the error
65 occurred, such as ``SSL``, ``PEM`` or ``X509``. The range of possible
66 values depends on the OpenSSL version.
67
68 .. versionadded:: 3.3
69
70 .. attribute:: reason
71
72 A string mnemonic designating the reason this error occurred, for
73 example ``CERTIFICATE_VERIFY_FAILED``. The range of possible
74 values depends on the OpenSSL version.
75
76 .. versionadded:: 3.3
77
Antoine Pitrou41032a62011-10-27 23:56:55 +020078.. exception:: SSLZeroReturnError
79
80 A subclass of :exc:`SSLError` raised when trying to read or write and
81 the SSL connection has been closed cleanly. Note that this doesn't
82 mean that the underlying transport (read TCP) has been closed.
83
84 .. versionadded:: 3.3
85
86.. exception:: SSLWantReadError
87
88 A subclass of :exc:`SSLError` raised by a :ref:`non-blocking SSL socket
89 <ssl-nonblocking>` when trying to read or write data, but more data needs
90 to be received on the underlying TCP transport before the request can be
91 fulfilled.
92
93 .. versionadded:: 3.3
94
95.. exception:: SSLWantWriteError
96
97 A subclass of :exc:`SSLError` raised by a :ref:`non-blocking SSL socket
98 <ssl-nonblocking>` when trying to read or write data, but more data needs
99 to be sent on the underlying TCP transport before the request can be
100 fulfilled.
101
102 .. versionadded:: 3.3
103
104.. exception:: SSLSyscallError
105
106 A subclass of :exc:`SSLError` raised when a system error was encountered
107 while trying to fulfill an operation on a SSL socket. Unfortunately,
108 there is no easy way to inspect the original errno number.
109
110 .. versionadded:: 3.3
111
112.. exception:: SSLEOFError
113
114 A subclass of :exc:`SSLError` raised when the SSL connection has been
Antoine Pitrouf3dc2d72011-10-28 00:01:03 +0200115 terminated abruptly. Generally, you shouldn't try to reuse the underlying
Antoine Pitrou41032a62011-10-27 23:56:55 +0200116 transport when this error is encountered.
117
118 .. versionadded:: 3.3
119
Antoine Pitrou59fdd672010-10-08 10:37:08 +0000120.. exception:: CertificateError
121
122 Raised to signal an error with a certificate (such as mismatching
123 hostname). Certificate errors detected by OpenSSL, though, raise
124 an :exc:`SSLError`.
125
126
127Socket creation
128^^^^^^^^^^^^^^^
129
130The following function allows for standalone socket creation. Starting from
131Python 3.2, it can be more flexible to use :meth:`SSLContext.wrap_socket`
132instead.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000133
Antoine Pitrou2d9cb9c2010-04-17 17:40:45 +0000134.. 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 Wouters1b7f8912007-09-19 03:06:30 +0000135
Georg Brandl7f01a132009-09-16 15:58:14 +0000136 Takes an instance ``sock`` of :class:`socket.socket`, and returns an instance
137 of :class:`ssl.SSLSocket`, a subtype of :class:`socket.socket`, which wraps
138 the underlying socket in an SSL context. For client-side sockets, the
139 context construction is lazy; if the underlying socket isn't connected yet,
140 the context construction will be performed after :meth:`connect` is called on
141 the socket. For server-side sockets, if the socket has no remote peer, it is
142 assumed to be a listening socket, and the server-side SSL wrapping is
143 automatically performed on client connections accepted via the :meth:`accept`
144 method. :func:`wrap_socket` may raise :exc:`SSLError`.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000145
Georg Brandl7f01a132009-09-16 15:58:14 +0000146 The ``keyfile`` and ``certfile`` parameters specify optional files which
147 contain a certificate to be used to identify the local side of the
148 connection. See the discussion of :ref:`ssl-certificates` for more
149 information on how the certificate is stored in the ``certfile``.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000150
Georg Brandl7f01a132009-09-16 15:58:14 +0000151 The parameter ``server_side`` is a boolean which identifies whether
152 server-side or client-side behavior is desired from this socket.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000153
Georg Brandl7f01a132009-09-16 15:58:14 +0000154 The parameter ``cert_reqs`` specifies whether a certificate is required from
155 the other side of the connection, and whether it will be validated if
156 provided. It must be one of the three values :const:`CERT_NONE`
157 (certificates ignored), :const:`CERT_OPTIONAL` (not required, but validated
158 if provided), or :const:`CERT_REQUIRED` (required and validated). If the
159 value of this parameter is not :const:`CERT_NONE`, then the ``ca_certs``
160 parameter must point to a file of CA certificates.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000161
Georg Brandl7f01a132009-09-16 15:58:14 +0000162 The ``ca_certs`` file contains a set of concatenated "certification
163 authority" certificates, which are used to validate certificates passed from
164 the other end of the connection. See the discussion of
165 :ref:`ssl-certificates` for more information about how to arrange the
166 certificates in this file.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000167
Georg Brandl7f01a132009-09-16 15:58:14 +0000168 The parameter ``ssl_version`` specifies which version of the SSL protocol to
169 use. Typically, the server chooses a particular protocol version, and the
170 client must adapt to the server's choice. Most of the versions are not
Antoine Pitrou84a2edc2012-01-09 21:35:11 +0100171 interoperable with the other versions. If not specified, the default is
172 :data:`PROTOCOL_SSLv23`; it provides the most compatibility with other
Georg Brandl7f01a132009-09-16 15:58:14 +0000173 versions.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000174
Georg Brandl7f01a132009-09-16 15:58:14 +0000175 Here's a table showing which versions in a client (down the side) can connect
176 to which versions in a server (along the top):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000177
178 .. table::
179
180 ======================== ========= ========= ========== =========
181 *client* / **server** **SSLv2** **SSLv3** **SSLv23** **TLSv1**
Christian Heimes255f53b2007-12-08 15:33:56 +0000182 ------------------------ --------- --------- ---------- ---------
Antoine Pitrou2d9cb9c2010-04-17 17:40:45 +0000183 *SSLv2* yes no yes no
Antoine Pitrouac8bfca2012-01-09 21:43:18 +0100184 *SSLv3* no yes yes no
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000185 *SSLv23* yes no yes no
186 *TLSv1* no no yes yes
187 ======================== ========= ========= ========== =========
188
Antoine Pitrou2d9cb9c2010-04-17 17:40:45 +0000189 .. note::
190
Benjamin Petersond7c3ed52010-06-27 22:32:30 +0000191 Which connections succeed will vary depending on the version of
192 OpenSSL. For instance, in some older versions of OpenSSL (such
193 as 0.9.7l on OS X 10.4), an SSLv2 client could not connect to an
194 SSLv23 server. Another example: beginning with OpenSSL 1.0.0,
195 an SSLv23 client will not actually attempt SSLv2 connections
196 unless you explicitly enable SSLv2 ciphers; for example, you
197 might specify ``"ALL"`` or ``"SSLv2"`` as the *ciphers* parameter
198 to enable them.
Antoine Pitrou2d9cb9c2010-04-17 17:40:45 +0000199
Benjamin Petersond7c3ed52010-06-27 22:32:30 +0000200 The *ciphers* parameter sets the available ciphers for this SSL object.
Antoine Pitrou2d9cb9c2010-04-17 17:40:45 +0000201 It should be a string in the `OpenSSL cipher list format
202 <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000203
Bill Janssen48dc27c2007-12-05 03:38:10 +0000204 The parameter ``do_handshake_on_connect`` specifies whether to do the SSL
205 handshake automatically after doing a :meth:`socket.connect`, or whether the
Georg Brandl7f01a132009-09-16 15:58:14 +0000206 application program will call it explicitly, by invoking the
207 :meth:`SSLSocket.do_handshake` method. Calling
208 :meth:`SSLSocket.do_handshake` explicitly gives the program control over the
209 blocking behavior of the socket I/O involved in the handshake.
Bill Janssen48dc27c2007-12-05 03:38:10 +0000210
Georg Brandl7f01a132009-09-16 15:58:14 +0000211 The parameter ``suppress_ragged_eofs`` specifies how the
Antoine Pitroudab64262010-09-19 13:31:06 +0000212 :meth:`SSLSocket.recv` method should signal unexpected EOF from the other end
Georg Brandl7f01a132009-09-16 15:58:14 +0000213 of the connection. If specified as :const:`True` (the default), it returns a
Antoine Pitroudab64262010-09-19 13:31:06 +0000214 normal EOF (an empty bytes object) in response to unexpected EOF errors
215 raised from the underlying socket; if :const:`False`, it will raise the
216 exceptions back to the caller.
Bill Janssen48dc27c2007-12-05 03:38:10 +0000217
Ezio Melotti4d5195b2010-04-20 10:57:44 +0000218 .. versionchanged:: 3.2
Antoine Pitrou2d9cb9c2010-04-17 17:40:45 +0000219 New optional argument *ciphers*.
220
Antoine Pitrou59fdd672010-10-08 10:37:08 +0000221Random generation
222^^^^^^^^^^^^^^^^^
223
Victor Stinner99c8b162011-05-24 12:05:19 +0200224.. function:: RAND_bytes(num)
225
Victor Stinnera6752062011-05-25 11:27:40 +0200226 Returns *num* cryptographically strong pseudo-random bytes. Raises an
227 :class:`SSLError` if the PRNG has not been seeded with enough data or if the
228 operation is not supported by the current RAND method. :func:`RAND_status`
229 can be used to check the status of the PRNG and :func:`RAND_add` can be used
230 to seed the PRNG.
Victor Stinner99c8b162011-05-24 12:05:19 +0200231
Victor Stinner19fb53c2011-05-24 21:32:40 +0200232 Read the Wikipedia article, `Cryptographically secure pseudorandom number
Victor Stinnera6752062011-05-25 11:27:40 +0200233 generator (CSPRNG)
Victor Stinner19fb53c2011-05-24 21:32:40 +0200234 <http://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator>`_,
235 to get the requirements of a cryptographically generator.
236
Victor Stinner99c8b162011-05-24 12:05:19 +0200237 .. versionadded:: 3.3
238
239.. function:: RAND_pseudo_bytes(num)
240
241 Returns (bytes, is_cryptographic): bytes are *num* pseudo-random bytes,
242 is_cryptographic is True if the bytes generated are cryptographically
Victor Stinnera6752062011-05-25 11:27:40 +0200243 strong. Raises an :class:`SSLError` if the operation is not supported by the
244 current RAND method.
Victor Stinner99c8b162011-05-24 12:05:19 +0200245
Victor Stinner19fb53c2011-05-24 21:32:40 +0200246 Generated pseudo-random byte sequences will be unique if they are of
247 sufficient length, but are not necessarily unpredictable. They can be used
248 for non-cryptographic purposes and for certain purposes in cryptographic
249 protocols, but usually not for key generation etc.
250
Victor Stinner99c8b162011-05-24 12:05:19 +0200251 .. versionadded:: 3.3
252
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000253.. function:: RAND_status()
254
Georg Brandl7f01a132009-09-16 15:58:14 +0000255 Returns True if the SSL pseudo-random number generator has been seeded with
256 'enough' randomness, and False otherwise. You can use :func:`ssl.RAND_egd`
257 and :func:`ssl.RAND_add` to increase the randomness of the pseudo-random
258 number generator.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000259
260.. function:: RAND_egd(path)
261
Victor Stinner99c8b162011-05-24 12:05:19 +0200262 If you are running an entropy-gathering daemon (EGD) somewhere, and *path*
Georg Brandl7f01a132009-09-16 15:58:14 +0000263 is the pathname of a socket connection open to it, this will read 256 bytes
264 of randomness from the socket, and add it to the SSL pseudo-random number
265 generator to increase the security of generated secret keys. This is
266 typically only necessary on systems without better sources of randomness.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000267
Georg Brandl7f01a132009-09-16 15:58:14 +0000268 See http://egd.sourceforge.net/ or http://prngd.sourceforge.net/ for sources
269 of entropy-gathering daemons.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000270
271.. function:: RAND_add(bytes, entropy)
272
Victor Stinner99c8b162011-05-24 12:05:19 +0200273 Mixes the given *bytes* into the SSL pseudo-random number generator. The
274 parameter *entropy* (a float) is a lower bound on the entropy contained in
Georg Brandl7f01a132009-09-16 15:58:14 +0000275 string (so you can always use :const:`0.0`). See :rfc:`1750` for more
276 information on sources of entropy.
Thomas Woutersed03b412007-08-28 21:37:11 +0000277
Antoine Pitrou59fdd672010-10-08 10:37:08 +0000278Certificate handling
279^^^^^^^^^^^^^^^^^^^^
280
281.. function:: match_hostname(cert, hostname)
282
283 Verify that *cert* (in decoded format as returned by
284 :meth:`SSLSocket.getpeercert`) matches the given *hostname*. The rules
285 applied are those for checking the identity of HTTPS servers as outlined
286 in :rfc:`2818`, except that IP addresses are not currently supported.
287 In addition to HTTPS, this function should be suitable for checking the
288 identity of servers in various SSL-based protocols such as FTPS, IMAPS,
289 POPS and others.
290
291 :exc:`CertificateError` is raised on failure. On success, the function
292 returns nothing::
293
294 >>> cert = {'subject': ((('commonName', 'example.com'),),)}
295 >>> ssl.match_hostname(cert, "example.com")
296 >>> ssl.match_hostname(cert, "example.org")
297 Traceback (most recent call last):
298 File "<stdin>", line 1, in <module>
299 File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
300 ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
301
302 .. versionadded:: 3.2
303
Thomas Woutersed03b412007-08-28 21:37:11 +0000304.. function:: cert_time_to_seconds(timestring)
305
Georg Brandl7f01a132009-09-16 15:58:14 +0000306 Returns a floating-point value containing a normal seconds-after-the-epoch
307 time value, given the time-string representing the "notBefore" or "notAfter"
308 date from a certificate.
Thomas Woutersed03b412007-08-28 21:37:11 +0000309
310 Here's an example::
311
312 >>> import ssl
313 >>> ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT")
314 1178694000.0
315 >>> import time
316 >>> time.ctime(ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT"))
317 'Wed May 9 00:00:00 2007'
Thomas Woutersed03b412007-08-28 21:37:11 +0000318
Georg Brandl7f01a132009-09-16 15:58:14 +0000319.. function:: get_server_certificate(addr, ssl_version=PROTOCOL_SSLv3, ca_certs=None)
Thomas Woutersed03b412007-08-28 21:37:11 +0000320
Georg Brandl7f01a132009-09-16 15:58:14 +0000321 Given the address ``addr`` of an SSL-protected server, as a (*hostname*,
322 *port-number*) pair, fetches the server's certificate, and returns it as a
323 PEM-encoded string. If ``ssl_version`` is specified, uses that version of
324 the SSL protocol to attempt to connect to the server. If ``ca_certs`` is
325 specified, it should be a file containing a list of root certificates, the
326 same format as used for the same parameter in :func:`wrap_socket`. The call
327 will attempt to validate the server certificate against that set of root
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000328 certificates, and will fail if the validation attempt fails.
329
Antoine Pitrou15399c32011-04-28 19:23:55 +0200330 .. versionchanged:: 3.3
331 This function is now IPv6-compatible.
332
Georg Brandl7f01a132009-09-16 15:58:14 +0000333.. function:: DER_cert_to_PEM_cert(DER_cert_bytes)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000334
335 Given a certificate as a DER-encoded blob of bytes, returns a PEM-encoded
336 string version of the same certificate.
337
Georg Brandl7f01a132009-09-16 15:58:14 +0000338.. function:: PEM_cert_to_DER_cert(PEM_cert_string)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000339
Georg Brandl7f01a132009-09-16 15:58:14 +0000340 Given a certificate as an ASCII PEM string, returns a DER-encoded sequence of
341 bytes for that same certificate.
Thomas Woutersed03b412007-08-28 21:37:11 +0000342
Antoine Pitrou59fdd672010-10-08 10:37:08 +0000343Constants
344^^^^^^^^^
345
Thomas Woutersed03b412007-08-28 21:37:11 +0000346.. data:: CERT_NONE
347
Antoine Pitrou152efa22010-05-16 18:19:27 +0000348 Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs``
349 parameter to :func:`wrap_socket`. In this mode (the default), no
350 certificates will be required from the other side of the socket connection.
351 If a certificate is received from the other end, no attempt to validate it
352 is made.
353
354 See the discussion of :ref:`ssl-security` below.
Thomas Woutersed03b412007-08-28 21:37:11 +0000355
356.. data:: CERT_OPTIONAL
357
Antoine Pitrou152efa22010-05-16 18:19:27 +0000358 Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs``
359 parameter to :func:`wrap_socket`. In this mode no certificates will be
360 required from the other side of the socket connection; but if they
361 are provided, validation will be attempted and an :class:`SSLError`
362 will be raised on failure.
363
364 Use of this setting requires a valid set of CA certificates to
365 be passed, either to :meth:`SSLContext.load_verify_locations` or as a
366 value of the ``ca_certs`` parameter to :func:`wrap_socket`.
Thomas Woutersed03b412007-08-28 21:37:11 +0000367
368.. data:: CERT_REQUIRED
369
Antoine Pitrou152efa22010-05-16 18:19:27 +0000370 Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs``
371 parameter to :func:`wrap_socket`. In this mode, certificates are
372 required from the other side of the socket connection; an :class:`SSLError`
373 will be raised if no certificate is provided, or if its validation fails.
374
375 Use of this setting requires a valid set of CA certificates to
376 be passed, either to :meth:`SSLContext.load_verify_locations` or as a
377 value of the ``ca_certs`` parameter to :func:`wrap_socket`.
Thomas Woutersed03b412007-08-28 21:37:11 +0000378
379.. data:: PROTOCOL_SSLv2
380
381 Selects SSL version 2 as the channel encryption protocol.
382
Victor Stinner3de49192011-05-09 00:42:58 +0200383 This protocol is not available if OpenSSL is compiled with OPENSSL_NO_SSL2
384 flag.
385
Antoine Pitrou8eac60d2010-05-16 14:19:41 +0000386 .. warning::
387
388 SSL version 2 is insecure. Its use is highly discouraged.
389
Thomas Woutersed03b412007-08-28 21:37:11 +0000390.. data:: PROTOCOL_SSLv23
391
Georg Brandl7f01a132009-09-16 15:58:14 +0000392 Selects SSL version 2 or 3 as the channel encryption protocol. This is a
393 setting to use with servers for maximum compatibility with the other end of
394 an SSL connection, but it may cause the specific ciphers chosen for the
395 encryption to be of fairly low quality.
Thomas Woutersed03b412007-08-28 21:37:11 +0000396
397.. data:: PROTOCOL_SSLv3
398
Georg Brandl7f01a132009-09-16 15:58:14 +0000399 Selects SSL version 3 as the channel encryption protocol. For clients, this
400 is the maximally compatible SSL variant.
Thomas Woutersed03b412007-08-28 21:37:11 +0000401
402.. data:: PROTOCOL_TLSv1
403
Georg Brandl7f01a132009-09-16 15:58:14 +0000404 Selects TLS version 1 as the channel encryption protocol. This is the most
405 modern version, and probably the best choice for maximum protection, if both
406 sides can speak it.
Thomas Woutersed03b412007-08-28 21:37:11 +0000407
Antoine Pitroub5218772010-05-21 09:56:06 +0000408.. data:: OP_ALL
409
410 Enables workarounds for various bugs present in other SSL implementations.
Antoine Pitrou9f6b02e2012-01-27 10:02:55 +0100411 This option is set by default. It does not necessarily set the same
412 flags as OpenSSL's ``SSL_OP_ALL`` constant.
Antoine Pitroub5218772010-05-21 09:56:06 +0000413
414 .. versionadded:: 3.2
415
416.. data:: OP_NO_SSLv2
417
418 Prevents an SSLv2 connection. This option is only applicable in
419 conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from
420 choosing SSLv2 as the protocol version.
421
422 .. versionadded:: 3.2
423
424.. data:: OP_NO_SSLv3
425
426 Prevents an SSLv3 connection. This option is only applicable in
427 conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from
428 choosing SSLv3 as the protocol version.
429
430 .. versionadded:: 3.2
431
432.. data:: OP_NO_TLSv1
433
434 Prevents a TLSv1 connection. This option is only applicable in
435 conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from
436 choosing TLSv1 as the protocol version.
437
438 .. versionadded:: 3.2
439
Antoine Pitrou6db49442011-12-19 13:27:11 +0100440.. data:: OP_CIPHER_SERVER_PREFERENCE
441
442 Use the server's cipher ordering preference, rather than the client's.
443 This option has no effect on client sockets and SSLv2 server sockets.
444
445 .. versionadded:: 3.3
446
Antoine Pitrou0e576f12011-12-22 10:03:38 +0100447.. data:: OP_SINGLE_DH_USE
448
449 Prevents re-use of the same DH key for distinct SSL sessions. This
450 improves forward secrecy but requires more computational resources.
451 This option only applies to server sockets.
452
453 .. versionadded:: 3.3
454
Antoine Pitrou923df6f2011-12-19 17:16:51 +0100455.. data:: OP_SINGLE_ECDH_USE
456
Antoine Pitrou0e576f12011-12-22 10:03:38 +0100457 Prevents re-use of the same ECDH key for distinct SSL sessions. This
Antoine Pitrou923df6f2011-12-19 17:16:51 +0100458 improves forward secrecy but requires more computational resources.
459 This option only applies to server sockets.
460
461 .. versionadded:: 3.3
462
Antoine Pitrou8abdb8a2011-12-20 10:13:40 +0100463.. data:: OP_NO_COMPRESSION
464
465 Disable compression on the SSL channel. This is useful if the application
466 protocol supports its own compression scheme.
467
468 This option is only available with OpenSSL 1.0.0 and later.
469
470 .. versionadded:: 3.3
471
Antoine Pitrou501da612011-12-21 09:27:41 +0100472.. data:: HAS_ECDH
473
474 Whether the OpenSSL library has built-in support for Elliptic Curve-based
475 Diffie-Hellman key exchange. This should be true unless the feature was
476 explicitly disabled by the distributor.
477
478 .. versionadded:: 3.3
479
Antoine Pitroud5323212010-10-22 18:19:07 +0000480.. data:: HAS_SNI
481
482 Whether the OpenSSL library has built-in support for the *Server Name
483 Indication* extension to the SSLv3 and TLSv1 protocols (as defined in
484 :rfc:`4366`). When true, you can use the *server_hostname* argument to
485 :meth:`SSLContext.wrap_socket`.
486
487 .. versionadded:: 3.2
488
Antoine Pitroud5d17eb2012-03-22 00:23:03 +0100489.. data:: HAS_NPN
490
491 Whether the OpenSSL library has built-in support for *Next Protocol
492 Negotiation* as described in the `NPN draft specification
493 <http://tools.ietf.org/html/draft-agl-tls-nextprotoneg>`_. When true,
494 you can use the :meth:`SSLContext.set_npn_protocols` method to advertise
495 which protocols you want to support.
496
497 .. versionadded:: 3.3
498
Antoine Pitroud6494802011-07-21 01:11:30 +0200499.. data:: CHANNEL_BINDING_TYPES
500
501 List of supported TLS channel binding types. Strings in this list
502 can be used as arguments to :meth:`SSLSocket.get_channel_binding`.
503
504 .. versionadded:: 3.3
505
Antoine Pitrou04f6a322010-04-05 21:40:07 +0000506.. data:: OPENSSL_VERSION
507
508 The version string of the OpenSSL library loaded by the interpreter::
509
510 >>> ssl.OPENSSL_VERSION
511 'OpenSSL 0.9.8k 25 Mar 2009'
512
Antoine Pitrou43a94c312010-04-05 21:44:48 +0000513 .. versionadded:: 3.2
Antoine Pitrou04f6a322010-04-05 21:40:07 +0000514
515.. data:: OPENSSL_VERSION_INFO
516
517 A tuple of five integers representing version information about the
518 OpenSSL library::
519
520 >>> ssl.OPENSSL_VERSION_INFO
521 (0, 9, 8, 11, 15)
522
Antoine Pitrou43a94c312010-04-05 21:44:48 +0000523 .. versionadded:: 3.2
Antoine Pitrou04f6a322010-04-05 21:40:07 +0000524
525.. data:: OPENSSL_VERSION_NUMBER
526
527 The raw version number of the OpenSSL library, as a single integer::
528
529 >>> ssl.OPENSSL_VERSION_NUMBER
Antoine Pitrou43a94c312010-04-05 21:44:48 +0000530 9470143
Antoine Pitrou04f6a322010-04-05 21:40:07 +0000531 >>> hex(ssl.OPENSSL_VERSION_NUMBER)
Antoine Pitrou43a94c312010-04-05 21:44:48 +0000532 '0x9080bf'
Antoine Pitrou04f6a322010-04-05 21:40:07 +0000533
Antoine Pitrou43a94c312010-04-05 21:44:48 +0000534 .. versionadded:: 3.2
Antoine Pitrou04f6a322010-04-05 21:40:07 +0000535
Thomas Woutersed03b412007-08-28 21:37:11 +0000536
Antoine Pitrou152efa22010-05-16 18:19:27 +0000537SSL Sockets
538-----------
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000539
Antoine Pitroue1f2f302010-09-19 13:56:11 +0000540SSL sockets provide the following methods of :ref:`socket-objects`:
Antoine Pitrou792ff3e2010-09-19 13:19:21 +0000541
Antoine Pitroue1f2f302010-09-19 13:56:11 +0000542- :meth:`~socket.socket.accept()`
543- :meth:`~socket.socket.bind()`
544- :meth:`~socket.socket.close()`
545- :meth:`~socket.socket.connect()`
546- :meth:`~socket.socket.detach()`
547- :meth:`~socket.socket.fileno()`
548- :meth:`~socket.socket.getpeername()`, :meth:`~socket.socket.getsockname()`
549- :meth:`~socket.socket.getsockopt()`, :meth:`~socket.socket.setsockopt()`
550- :meth:`~socket.socket.gettimeout()`, :meth:`~socket.socket.settimeout()`,
551 :meth:`~socket.socket.setblocking()`
552- :meth:`~socket.socket.listen()`
553- :meth:`~socket.socket.makefile()`
554- :meth:`~socket.socket.recv()`, :meth:`~socket.socket.recv_into()`
555 (but passing a non-zero ``flags`` argument is not allowed)
556- :meth:`~socket.socket.send()`, :meth:`~socket.socket.sendall()` (with
557 the same limitation)
558- :meth:`~socket.socket.shutdown()`
559
Antoine Pitrou6f5dcb12011-07-11 01:35:48 +0200560However, since the SSL (and TLS) protocol has its own framing atop
561of TCP, the SSL sockets abstraction can, in certain respects, diverge from
562the specification of normal, OS-level sockets. See especially the
563:ref:`notes on non-blocking sockets <ssl-nonblocking>`.
564
565SSL sockets also have the following additional methods and attributes:
Antoine Pitrou792ff3e2010-09-19 13:19:21 +0000566
Bill Janssen48dc27c2007-12-05 03:38:10 +0000567.. method:: SSLSocket.do_handshake()
568
Antoine Pitroub3593ca2011-07-11 01:39:19 +0200569 Perform the SSL setup handshake.
Bill Janssen48dc27c2007-12-05 03:38:10 +0000570
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000571.. method:: SSLSocket.getpeercert(binary_form=False)
572
Georg Brandl7f01a132009-09-16 15:58:14 +0000573 If there is no certificate for the peer on the other end of the connection,
574 returns ``None``.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000575
Antoine Pitroud34941a2013-04-16 20:27:17 +0200576 If the ``binary_form`` parameter is :const:`False`, and a certificate was
Georg Brandl7f01a132009-09-16 15:58:14 +0000577 received from the peer, this method returns a :class:`dict` instance. If the
578 certificate was not validated, the dict is empty. If the certificate was
Antoine Pitroub7c6c812012-08-16 22:14:43 +0200579 validated, it returns a dict with several keys, amongst them ``subject``
580 (the principal for which the certificate was issued) and ``issuer``
581 (the principal issuing the certificate). If a certificate contains an
582 instance of the *Subject Alternative Name* extension (see :rfc:`3280`),
583 there will also be a ``subjectAltName`` key in the dictionary.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000584
Antoine Pitroub7c6c812012-08-16 22:14:43 +0200585 The ``subject`` and ``issuer`` fields are tuples containing the sequence
586 of relative distinguished names (RDNs) given in the certificate's data
587 structure for the respective fields, and each RDN is a sequence of
588 name-value pairs. Here is a real-world example::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000589
Antoine Pitroub7c6c812012-08-16 22:14:43 +0200590 {'issuer': ((('countryName', 'IL'),),
591 (('organizationName', 'StartCom Ltd.'),),
592 (('organizationalUnitName',
593 'Secure Digital Certificate Signing'),),
594 (('commonName',
595 'StartCom Class 2 Primary Intermediate Server CA'),)),
596 'notAfter': 'Nov 22 08:15:19 2013 GMT',
597 'notBefore': 'Nov 21 03:09:52 2011 GMT',
598 'serialNumber': '95F0',
599 'subject': ((('description', '571208-SLe257oHY9fVQ07Z'),),
600 (('countryName', 'US'),),
601 (('stateOrProvinceName', 'California'),),
602 (('localityName', 'San Francisco'),),
603 (('organizationName', 'Electronic Frontier Foundation, Inc.'),),
604 (('commonName', '*.eff.org'),),
605 (('emailAddress', 'hostmaster@eff.org'),)),
606 'subjectAltName': (('DNS', '*.eff.org'), ('DNS', 'eff.org')),
607 'version': 3}
608
609 .. note::
610 To validate a certificate for a particular service, you can use the
611 :func:`match_hostname` function.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000612
Georg Brandl7f01a132009-09-16 15:58:14 +0000613 If the ``binary_form`` parameter is :const:`True`, and a certificate was
614 provided, this method returns the DER-encoded form of the entire certificate
615 as a sequence of bytes, or :const:`None` if the peer did not provide a
Antoine Pitroud34941a2013-04-16 20:27:17 +0200616 certificate. Whether the peer provides a certificate depends on the SSL
617 socket's role:
618
619 * for a client SSL socket, the server will always provide a certificate,
620 regardless of whether validation was required;
621
622 * for a server SSL socket, the client will only provide a certificate
623 when requested by the server; therefore :meth:`getpeercert` will return
624 :const:`None` if you used :const:`CERT_NONE` (rather than
625 :const:`CERT_OPTIONAL` or :const:`CERT_REQUIRED`).
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000626
Antoine Pitroufb046912010-11-09 20:21:19 +0000627 .. versionchanged:: 3.2
628 The returned dictionary includes additional items such as ``issuer``
629 and ``notBefore``.
630
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000631.. method:: SSLSocket.cipher()
632
Georg Brandl7f01a132009-09-16 15:58:14 +0000633 Returns a three-value tuple containing the name of the cipher being used, the
634 version of the SSL protocol that defines its use, and the number of secret
635 bits being used. If no connection has been established, returns ``None``.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000636
Antoine Pitrou8abdb8a2011-12-20 10:13:40 +0100637.. method:: SSLSocket.compression()
638
639 Return the compression algorithm being used as a string, or ``None``
640 if the connection isn't compressed.
641
642 If the higher-level protocol supports its own compression mechanism,
643 you can use :data:`OP_NO_COMPRESSION` to disable SSL-level compression.
644
645 .. versionadded:: 3.3
646
Antoine Pitroud6494802011-07-21 01:11:30 +0200647.. method:: SSLSocket.get_channel_binding(cb_type="tls-unique")
648
649 Get channel binding data for current connection, as a bytes object. Returns
650 ``None`` if not connected or the handshake has not been completed.
651
652 The *cb_type* parameter allow selection of the desired channel binding
653 type. Valid channel binding types are listed in the
654 :data:`CHANNEL_BINDING_TYPES` list. Currently only the 'tls-unique' channel
655 binding, defined by :rfc:`5929`, is supported. :exc:`ValueError` will be
656 raised if an unsupported channel binding type is requested.
657
658 .. versionadded:: 3.3
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000659
Antoine Pitroud5d17eb2012-03-22 00:23:03 +0100660.. method:: SSLSocket.selected_npn_protocol()
661
662 Returns the protocol that was selected during the TLS/SSL handshake. If
663 :meth:`SSLContext.set_npn_protocols` was not called, or if the other party
664 does not support NPN, or if the handshake has not yet happened, this will
665 return ``None``.
666
667 .. versionadded:: 3.3
668
Benjamin Peterson4aeec042008-08-19 21:42:13 +0000669.. method:: SSLSocket.unwrap()
670
Georg Brandl7f01a132009-09-16 15:58:14 +0000671 Performs the SSL shutdown handshake, which removes the TLS layer from the
672 underlying socket, and returns the underlying socket object. This can be
673 used to go from encrypted operation over a connection to unencrypted. The
674 returned socket should always be used for further communication with the
675 other side of the connection, rather than the original socket.
Benjamin Peterson4aeec042008-08-19 21:42:13 +0000676
Antoine Pitrouec883db2010-05-24 21:20:20 +0000677.. attribute:: SSLSocket.context
678
679 The :class:`SSLContext` object this SSL socket is tied to. If the SSL
680 socket was created using the top-level :func:`wrap_socket` function
681 (rather than :meth:`SSLContext.wrap_socket`), this is a custom context
682 object created for this SSL socket.
683
684 .. versionadded:: 3.2
685
686
Antoine Pitrou152efa22010-05-16 18:19:27 +0000687SSL Contexts
688------------
689
Antoine Pitroucafaad42010-05-24 15:58:43 +0000690.. versionadded:: 3.2
691
Antoine Pitroub0182c82010-10-12 20:09:02 +0000692An SSL context holds various data longer-lived than single SSL connections,
693such as SSL configuration options, certificate(s) and private key(s).
694It also manages a cache of SSL sessions for server-side sockets, in order
695to speed up repeated connections from the same clients.
696
Antoine Pitrou152efa22010-05-16 18:19:27 +0000697.. class:: SSLContext(protocol)
698
Antoine Pitroub0182c82010-10-12 20:09:02 +0000699 Create a new SSL context. You must pass *protocol* which must be one
700 of the ``PROTOCOL_*`` constants defined in this module.
701 :data:`PROTOCOL_SSLv23` is recommended for maximum interoperability.
702
Antoine Pitrou152efa22010-05-16 18:19:27 +0000703
704:class:`SSLContext` objects have the following methods and attributes:
705
Antoine Pitrou4fd1e6a2011-08-25 14:39:44 +0200706.. method:: SSLContext.load_cert_chain(certfile, keyfile=None, password=None)
Antoine Pitrou152efa22010-05-16 18:19:27 +0000707
708 Load a private key and the corresponding certificate. The *certfile*
709 string must be the path to a single file in PEM format containing the
710 certificate as well as any number of CA certificates needed to establish
711 the certificate's authenticity. The *keyfile* string, if present, must
712 point to a file containing the private key in. Otherwise the private
713 key will be taken from *certfile* as well. See the discussion of
714 :ref:`ssl-certificates` for more information on how the certificate
715 is stored in the *certfile*.
716
Antoine Pitrou4fd1e6a2011-08-25 14:39:44 +0200717 The *password* argument may be a function to call to get the password for
718 decrypting the private key. It will only be called if the private key is
719 encrypted and a password is necessary. It will be called with no arguments,
720 and it should return a string, bytes, or bytearray. If the return value is
721 a string it will be encoded as UTF-8 before using it to decrypt the key.
722 Alternatively a string, bytes, or bytearray value may be supplied directly
723 as the *password* argument. It will be ignored if the private key is not
724 encrypted and no password is needed.
725
726 If the *password* argument is not specified and a password is required,
727 OpenSSL's built-in password prompting mechanism will be used to
728 interactively prompt the user for a password.
729
Antoine Pitrou152efa22010-05-16 18:19:27 +0000730 An :class:`SSLError` is raised if the private key doesn't
731 match with the certificate.
732
Antoine Pitrou4fd1e6a2011-08-25 14:39:44 +0200733 .. versionchanged:: 3.3
734 New optional argument *password*.
735
Antoine Pitrou152efa22010-05-16 18:19:27 +0000736.. method:: SSLContext.load_verify_locations(cafile=None, capath=None)
737
738 Load a set of "certification authority" (CA) certificates used to validate
739 other peers' certificates when :data:`verify_mode` is other than
740 :data:`CERT_NONE`. At least one of *cafile* or *capath* must be specified.
741
742 The *cafile* string, if present, is the path to a file of concatenated
743 CA certificates in PEM format. See the discussion of
744 :ref:`ssl-certificates` for more information about how to arrange the
745 certificates in this file.
746
747 The *capath* string, if present, is
748 the path to a directory containing several CA certificates in PEM format,
749 following an `OpenSSL specific layout
750 <http://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html>`_.
751
Antoine Pitrou664c2d12010-11-17 20:29:42 +0000752.. method:: SSLContext.set_default_verify_paths()
753
754 Load a set of default "certification authority" (CA) certificates from
755 a filesystem path defined when building the OpenSSL library. Unfortunately,
756 there's no easy way to know whether this method succeeds: no error is
757 returned if no certificates are to be found. When the OpenSSL library is
758 provided as part of the operating system, though, it is likely to be
759 configured properly.
760
Antoine Pitrou152efa22010-05-16 18:19:27 +0000761.. method:: SSLContext.set_ciphers(ciphers)
762
763 Set the available ciphers for sockets created with this context.
764 It should be a string in the `OpenSSL cipher list format
765 <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_.
766 If no cipher can be selected (because compile-time options or other
767 configuration forbids use of all the specified ciphers), an
768 :class:`SSLError` will be raised.
769
770 .. note::
771 when connected, the :meth:`SSLSocket.cipher` method of SSL sockets will
772 give the currently selected cipher.
773
Antoine Pitroud5d17eb2012-03-22 00:23:03 +0100774.. method:: SSLContext.set_npn_protocols(protocols)
775
776 Specify which protocols the socket should avertise during the SSL/TLS
777 handshake. It should be a list of strings, like ``['http/1.1', 'spdy/2']``,
778 ordered by preference. The selection of a protocol will happen during the
779 handshake, and will play out according to the `NPN draft specification
780 <http://tools.ietf.org/html/draft-agl-tls-nextprotoneg>`_. After a
781 successful handshake, the :meth:`SSLSocket.selected_npn_protocol` method will
782 return the agreed-upon protocol.
783
784 This method will raise :exc:`NotImplementedError` if :data:`HAS_NPN` is
785 False.
786
787 .. versionadded:: 3.3
788
Antoine Pitrou0e576f12011-12-22 10:03:38 +0100789.. method:: SSLContext.load_dh_params(dhfile)
790
791 Load the key generation parameters for Diffie-Helman (DH) key exchange.
792 Using DH key exchange improves forward secrecy at the expense of
793 computational resources (both on the server and on the client).
794 The *dhfile* parameter should be the path to a file containing DH
795 parameters in PEM format.
796
797 This setting doesn't apply to client sockets. You can also use the
798 :data:`OP_SINGLE_DH_USE` option to further improve security.
799
800 .. versionadded:: 3.3
801
Antoine Pitrou923df6f2011-12-19 17:16:51 +0100802.. method:: SSLContext.set_ecdh_curve(curve_name)
803
Antoine Pitrou0e576f12011-12-22 10:03:38 +0100804 Set the curve name for Elliptic Curve-based Diffie-Hellman (ECDH) key
805 exchange. ECDH is significantly faster than regular DH while arguably
806 as secure. The *curve_name* parameter should be a string describing
Antoine Pitrou923df6f2011-12-19 17:16:51 +0100807 a well-known elliptic curve, for example ``prime256v1`` for a widely
808 supported curve.
809
810 This setting doesn't apply to client sockets. You can also use the
811 :data:`OP_SINGLE_ECDH_USE` option to further improve security.
812
Antoine Pitrou501da612011-12-21 09:27:41 +0100813 This method is not available if :data:`HAS_ECDH` is False.
814
Antoine Pitrou923df6f2011-12-19 17:16:51 +0100815 .. versionadded:: 3.3
816
817 .. seealso::
818 `SSL/TLS & Perfect Forward Secrecy <http://vincent.bernat.im/en/blog/2011-ssl-perfect-forward-secrecy.html>`_
819 Vincent Bernat.
820
Antoine Pitroud5323212010-10-22 18:19:07 +0000821.. method:: SSLContext.wrap_socket(sock, server_side=False, \
822 do_handshake_on_connect=True, suppress_ragged_eofs=True, \
823 server_hostname=None)
Antoine Pitrou152efa22010-05-16 18:19:27 +0000824
825 Wrap an existing Python socket *sock* and return an :class:`SSLSocket`
826 object. The SSL socket is tied to the context, its settings and
827 certificates. The parameters *server_side*, *do_handshake_on_connect*
828 and *suppress_ragged_eofs* have the same meaning as in the top-level
829 :func:`wrap_socket` function.
830
Antoine Pitroud5323212010-10-22 18:19:07 +0000831 On client connections, the optional parameter *server_hostname* specifies
832 the hostname of the service which we are connecting to. This allows a
833 single server to host multiple SSL-based services with distinct certificates,
834 quite similarly to HTTP virtual hosts. Specifying *server_hostname*
835 will raise a :exc:`ValueError` if the OpenSSL library doesn't have support
836 for it (that is, if :data:`HAS_SNI` is :const:`False`). Specifying
837 *server_hostname* will also raise a :exc:`ValueError` if *server_side*
838 is true.
839
Antoine Pitroub0182c82010-10-12 20:09:02 +0000840.. method:: SSLContext.session_stats()
841
842 Get statistics about the SSL sessions created or managed by this context.
843 A dictionary is returned which maps the names of each `piece of information
844 <http://www.openssl.org/docs/ssl/SSL_CTX_sess_number.html>`_ to their
845 numeric values. For example, here is the total number of hits and misses
846 in the session cache since the context was created::
847
848 >>> stats = context.session_stats()
849 >>> stats['hits'], stats['misses']
850 (0, 0)
851
Antoine Pitroub5218772010-05-21 09:56:06 +0000852.. attribute:: SSLContext.options
853
854 An integer representing the set of SSL options enabled on this context.
855 The default value is :data:`OP_ALL`, but you can specify other options
856 such as :data:`OP_NO_SSLv2` by ORing them together.
857
858 .. note::
859 With versions of OpenSSL older than 0.9.8m, it is only possible
860 to set options, not to clear them. Attempting to clear an option
861 (by resetting the corresponding bits) will raise a ``ValueError``.
862
Antoine Pitrou152efa22010-05-16 18:19:27 +0000863.. attribute:: SSLContext.protocol
864
865 The protocol version chosen when constructing the context. This attribute
866 is read-only.
867
868.. attribute:: SSLContext.verify_mode
869
870 Whether to try to verify other peers' certificates and how to behave
871 if verification fails. This attribute must be one of
872 :data:`CERT_NONE`, :data:`CERT_OPTIONAL` or :data:`CERT_REQUIRED`.
873
874
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000875.. index:: single: certificates
876
877.. index:: single: X509 certificate
878
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000879.. _ssl-certificates:
880
Thomas Woutersed03b412007-08-28 21:37:11 +0000881Certificates
882------------
883
Georg Brandl7f01a132009-09-16 15:58:14 +0000884Certificates in general are part of a public-key / private-key system. In this
885system, each *principal*, (which may be a machine, or a person, or an
886organization) is assigned a unique two-part encryption key. One part of the key
887is public, and is called the *public key*; the other part is kept secret, and is
888called the *private key*. The two parts are related, in that if you encrypt a
889message with one of the parts, you can decrypt it with the other part, and
890**only** with the other part.
Thomas Woutersed03b412007-08-28 21:37:11 +0000891
Georg Brandl7f01a132009-09-16 15:58:14 +0000892A certificate contains information about two principals. It contains the name
893of a *subject*, and the subject's public key. It also contains a statement by a
894second principal, the *issuer*, that the subject is who he claims to be, and
895that this is indeed the subject's public key. The issuer's statement is signed
896with the issuer's private key, which only the issuer knows. However, anyone can
897verify the issuer's statement by finding the issuer's public key, decrypting the
898statement with it, and comparing it to the other information in the certificate.
899The certificate also contains information about the time period over which it is
900valid. This is expressed as two fields, called "notBefore" and "notAfter".
Thomas Woutersed03b412007-08-28 21:37:11 +0000901
Georg Brandl7f01a132009-09-16 15:58:14 +0000902In the Python use of certificates, a client or server can use a certificate to
903prove who they are. The other side of a network connection can also be required
904to produce a certificate, and that certificate can be validated to the
905satisfaction of the client or server that requires such validation. The
906connection attempt can be set to raise an exception if the validation fails.
907Validation is done automatically, by the underlying OpenSSL framework; the
908application need not concern itself with its mechanics. But the application
909does usually need to provide sets of certificates to allow this process to take
910place.
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000911
Georg Brandl7f01a132009-09-16 15:58:14 +0000912Python uses files to contain certificates. They should be formatted as "PEM"
913(see :rfc:`1422`), which is a base-64 encoded form wrapped with a header line
914and a footer line::
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000915
916 -----BEGIN CERTIFICATE-----
917 ... (certificate in base64 PEM encoding) ...
918 -----END CERTIFICATE-----
919
Antoine Pitrou152efa22010-05-16 18:19:27 +0000920Certificate chains
921^^^^^^^^^^^^^^^^^^
922
Georg Brandl7f01a132009-09-16 15:58:14 +0000923The Python files which contain certificates can contain a sequence of
924certificates, sometimes called a *certificate chain*. This chain should start
925with the specific certificate for the principal who "is" the client or server,
926and then the certificate for the issuer of that certificate, and then the
927certificate for the issuer of *that* certificate, and so on up the chain till
928you get to a certificate which is *self-signed*, that is, a certificate which
929has the same subject and issuer, sometimes called a *root certificate*. The
930certificates should just be concatenated together in the certificate file. For
931example, suppose we had a three certificate chain, from our server certificate
932to the certificate of the certification authority that signed our server
933certificate, to the root certificate of the agency which issued the
934certification authority's certificate::
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000935
936 -----BEGIN CERTIFICATE-----
937 ... (certificate for your server)...
938 -----END CERTIFICATE-----
939 -----BEGIN CERTIFICATE-----
940 ... (the certificate for the CA)...
941 -----END CERTIFICATE-----
942 -----BEGIN CERTIFICATE-----
943 ... (the root certificate for the CA's issuer)...
944 -----END CERTIFICATE-----
945
Antoine Pitrou152efa22010-05-16 18:19:27 +0000946CA certificates
947^^^^^^^^^^^^^^^
948
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000949If you are going to require validation of the other side of the connection's
950certificate, you need to provide a "CA certs" file, filled with the certificate
Georg Brandl7f01a132009-09-16 15:58:14 +0000951chains for each issuer you are willing to trust. Again, this file just contains
952these chains concatenated together. For validation, Python will use the first
953chain it finds in the file which matches. Some "standard" root certificates are
954available from various certification authorities: `CACert.org
955<http://www.cacert.org/index.php?id=3>`_, `Thawte
956<http://www.thawte.com/roots/>`_, `Verisign
957<http://www.verisign.com/support/roots.html>`_, `Positive SSL
958<http://www.PositiveSSL.com/ssl-certificate-support/cert_installation/UTN-USERFirst-Hardware.crt>`_
959(used by python.org), `Equifax and GeoTrust
960<http://www.geotrust.com/resources/root_certificates/index.asp>`_.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000961
Georg Brandl7f01a132009-09-16 15:58:14 +0000962In general, if you are using SSL3 or TLS1, you don't need to put the full chain
963in your "CA certs" file; you only need the root certificates, and the remote
964peer is supposed to furnish the other certificates necessary to chain from its
965certificate to a root certificate. See :rfc:`4158` for more discussion of the
966way in which certification chains can be built.
Thomas Woutersed03b412007-08-28 21:37:11 +0000967
Antoine Pitrou152efa22010-05-16 18:19:27 +0000968Combined key and certificate
969^^^^^^^^^^^^^^^^^^^^^^^^^^^^
970
971Often the private key is stored in the same file as the certificate; in this
972case, only the ``certfile`` parameter to :meth:`SSLContext.load_cert_chain`
973and :func:`wrap_socket` needs to be passed. If the private key is stored
974with the certificate, it should come before the first certificate in
975the certificate chain::
976
977 -----BEGIN RSA PRIVATE KEY-----
978 ... (private key in base64 encoding) ...
979 -----END RSA PRIVATE KEY-----
980 -----BEGIN CERTIFICATE-----
981 ... (certificate in base64 PEM encoding) ...
982 -----END CERTIFICATE-----
983
984Self-signed certificates
985^^^^^^^^^^^^^^^^^^^^^^^^
986
Georg Brandl7f01a132009-09-16 15:58:14 +0000987If you are going to create a server that provides SSL-encrypted connection
988services, you will need to acquire a certificate for that service. There are
989many ways of acquiring appropriate certificates, such as buying one from a
990certification authority. Another common practice is to generate a self-signed
991certificate. The simplest way to do this is with the OpenSSL package, using
992something like the following::
Thomas Woutersed03b412007-08-28 21:37:11 +0000993
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000994 % openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
995 Generating a 1024 bit RSA private key
996 .......++++++
997 .............................++++++
998 writing new private key to 'cert.pem'
999 -----
1000 You are about to be asked to enter information that will be incorporated
1001 into your certificate request.
1002 What you are about to enter is what is called a Distinguished Name or a DN.
1003 There are quite a few fields but you can leave some blank
1004 For some fields there will be a default value,
1005 If you enter '.', the field will be left blank.
1006 -----
1007 Country Name (2 letter code) [AU]:US
1008 State or Province Name (full name) [Some-State]:MyState
1009 Locality Name (eg, city) []:Some City
1010 Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Organization, Inc.
1011 Organizational Unit Name (eg, section) []:My Group
1012 Common Name (eg, YOUR name) []:myserver.mygroup.myorganization.com
1013 Email Address []:ops@myserver.mygroup.myorganization.com
1014 %
Thomas Woutersed03b412007-08-28 21:37:11 +00001015
Georg Brandl7f01a132009-09-16 15:58:14 +00001016The disadvantage of a self-signed certificate is that it is its own root
1017certificate, and no one else will have it in their cache of known (and trusted)
1018root certificates.
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001019
1020
Thomas Woutersed03b412007-08-28 21:37:11 +00001021Examples
1022--------
1023
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001024Testing for SSL support
1025^^^^^^^^^^^^^^^^^^^^^^^
1026
Georg Brandl7f01a132009-09-16 15:58:14 +00001027To test for the presence of SSL support in a Python installation, user code
1028should use the following idiom::
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001029
1030 try:
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001031 import ssl
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001032 except ImportError:
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001033 pass
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001034 else:
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001035 ... # do something that requires SSL support
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001036
1037Client-side operation
1038^^^^^^^^^^^^^^^^^^^^^
1039
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001040This example connects to an SSL server and prints the server's certificate::
Thomas Woutersed03b412007-08-28 21:37:11 +00001041
1042 import socket, ssl, pprint
1043
1044 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001045 # require a certificate from the server
1046 ssl_sock = ssl.wrap_socket(s,
1047 ca_certs="/etc/ca_certs_file",
1048 cert_reqs=ssl.CERT_REQUIRED)
Thomas Woutersed03b412007-08-28 21:37:11 +00001049 ssl_sock.connect(('www.verisign.com', 443))
1050
Georg Brandl6911e3c2007-09-04 07:15:32 +00001051 pprint.pprint(ssl_sock.getpeercert())
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001052 # note that closing the SSLSocket will also close the underlying socket
Thomas Woutersed03b412007-08-28 21:37:11 +00001053 ssl_sock.close()
1054
Antoine Pitrou441ae042012-01-06 20:06:15 +01001055As of January 6, 2012, the certificate printed by this program looks like
Georg Brandl7f01a132009-09-16 15:58:14 +00001056this::
Thomas Woutersed03b412007-08-28 21:37:11 +00001057
Antoine Pitrou441ae042012-01-06 20:06:15 +01001058 {'issuer': ((('countryName', 'US'),),
1059 (('organizationName', 'VeriSign, Inc.'),),
1060 (('organizationalUnitName', 'VeriSign Trust Network'),),
1061 (('organizationalUnitName',
1062 'Terms of use at https://www.verisign.com/rpa (c)06'),),
1063 (('commonName',
1064 'VeriSign Class 3 Extended Validation SSL SGC CA'),)),
1065 'notAfter': 'May 25 23:59:59 2012 GMT',
1066 'notBefore': 'May 26 00:00:00 2010 GMT',
1067 'serialNumber': '53D2BEF924A7245E83CA01E46CAA2477',
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001068 'subject': ((('1.3.6.1.4.1.311.60.2.1.3', 'US'),),
1069 (('1.3.6.1.4.1.311.60.2.1.2', 'Delaware'),),
1070 (('businessCategory', 'V1.0, Clause 5.(b)'),),
1071 (('serialNumber', '2497886'),),
1072 (('countryName', 'US'),),
1073 (('postalCode', '94043'),),
1074 (('stateOrProvinceName', 'California'),),
1075 (('localityName', 'Mountain View'),),
1076 (('streetAddress', '487 East Middlefield Road'),),
1077 (('organizationName', 'VeriSign, Inc.'),),
1078 (('organizationalUnitName', ' Production Security Services'),),
Antoine Pitrou441ae042012-01-06 20:06:15 +01001079 (('commonName', 'www.verisign.com'),)),
1080 'subjectAltName': (('DNS', 'www.verisign.com'),
1081 ('DNS', 'verisign.com'),
1082 ('DNS', 'www.verisign.net'),
1083 ('DNS', 'verisign.net'),
1084 ('DNS', 'www.verisign.mobi'),
1085 ('DNS', 'verisign.mobi'),
1086 ('DNS', 'www.verisign.eu'),
1087 ('DNS', 'verisign.eu')),
1088 'version': 3}
Thomas Woutersed03b412007-08-28 21:37:11 +00001089
Antoine Pitrou152efa22010-05-16 18:19:27 +00001090This other example first creates an SSL context, instructs it to verify
1091certificates sent by peers, and feeds it a set of recognized certificate
1092authorities (CA)::
1093
1094 >>> context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001095 >>> context.verify_mode = ssl.CERT_REQUIRED
Antoine Pitrou152efa22010-05-16 18:19:27 +00001096 >>> context.load_verify_locations("/etc/ssl/certs/ca-bundle.crt")
1097
1098(it is assumed your operating system places a bundle of all CA certificates
1099in ``/etc/ssl/certs/ca-bundle.crt``; if not, you'll get an error and have
1100to adjust the location)
1101
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001102When you use the context to connect to a server, :const:`CERT_REQUIRED`
Antoine Pitrou152efa22010-05-16 18:19:27 +00001103validates the server certificate: it ensures that the server certificate
1104was signed with one of the CA certificates, and checks the signature for
1105correctness::
1106
1107 >>> conn = context.wrap_socket(socket.socket(socket.AF_INET))
1108 >>> conn.connect(("linuxfr.org", 443))
1109
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001110You should then fetch the certificate and check its fields for conformity::
Antoine Pitrou152efa22010-05-16 18:19:27 +00001111
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001112 >>> cert = conn.getpeercert()
1113 >>> ssl.match_hostname(cert, "linuxfr.org")
1114
1115Visual inspection shows that the certificate does identify the desired service
1116(that is, the HTTPS host ``linuxfr.org``)::
1117
1118 >>> pprint.pprint(cert)
Antoine Pitrou441ae042012-01-06 20:06:15 +01001119 {'issuer': ((('organizationName', 'CAcert Inc.'),),
1120 (('organizationalUnitName', 'http://www.CAcert.org'),),
1121 (('commonName', 'CAcert Class 3 Root'),)),
1122 'notAfter': 'Jun 7 21:02:24 2013 GMT',
1123 'notBefore': 'Jun 8 21:02:24 2011 GMT',
1124 'serialNumber': 'D3E9',
Antoine Pitrou152efa22010-05-16 18:19:27 +00001125 'subject': ((('commonName', 'linuxfr.org'),),),
Antoine Pitrou441ae042012-01-06 20:06:15 +01001126 'subjectAltName': (('DNS', 'linuxfr.org'),
1127 ('othername', '<unsupported>'),
1128 ('DNS', 'linuxfr.org'),
1129 ('othername', '<unsupported>'),
1130 ('DNS', 'dev.linuxfr.org'),
1131 ('othername', '<unsupported>'),
1132 ('DNS', 'prod.linuxfr.org'),
1133 ('othername', '<unsupported>'),
1134 ('DNS', 'alpha.linuxfr.org'),
1135 ('othername', '<unsupported>'),
1136 ('DNS', '*.linuxfr.org'),
1137 ('othername', '<unsupported>')),
1138 'version': 3}
Antoine Pitrou152efa22010-05-16 18:19:27 +00001139
1140Now that you are assured of its authenticity, you can proceed to talk with
1141the server::
1142
Antoine Pitroudab64262010-09-19 13:31:06 +00001143 >>> conn.sendall(b"HEAD / HTTP/1.0\r\nHost: linuxfr.org\r\n\r\n")
1144 >>> pprint.pprint(conn.recv(1024).split(b"\r\n"))
Antoine Pitrou152efa22010-05-16 18:19:27 +00001145 [b'HTTP/1.1 302 Found',
1146 b'Date: Sun, 16 May 2010 13:43:28 GMT',
1147 b'Server: Apache/2.2',
1148 b'Location: https://linuxfr.org/pub/',
1149 b'Vary: Accept-Encoding',
1150 b'Connection: close',
1151 b'Content-Type: text/html; charset=iso-8859-1',
1152 b'',
1153 b'']
1154
Antoine Pitrou152efa22010-05-16 18:19:27 +00001155See the discussion of :ref:`ssl-security` below.
1156
1157
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001158Server-side operation
1159^^^^^^^^^^^^^^^^^^^^^
1160
Antoine Pitrou152efa22010-05-16 18:19:27 +00001161For server operation, typically you'll need to have a server certificate, and
1162private key, each in a file. You'll first create a context holding the key
1163and the certificate, so that clients can check your authenticity. Then
1164you'll open a socket, bind it to a port, call :meth:`listen` on it, and start
1165waiting for clients to connect::
Thomas Woutersed03b412007-08-28 21:37:11 +00001166
1167 import socket, ssl
1168
Antoine Pitrou152efa22010-05-16 18:19:27 +00001169 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
1170 context.load_cert_chain(certfile="mycertfile", keyfile="mykeyfile")
1171
Thomas Woutersed03b412007-08-28 21:37:11 +00001172 bindsocket = socket.socket()
1173 bindsocket.bind(('myaddr.mydomain.com', 10023))
1174 bindsocket.listen(5)
1175
Antoine Pitrou152efa22010-05-16 18:19:27 +00001176When a client connects, you'll call :meth:`accept` on the socket to get the
1177new socket from the other end, and use the context's :meth:`SSLContext.wrap_socket`
1178method to create a server-side SSL socket for the connection::
Thomas Woutersed03b412007-08-28 21:37:11 +00001179
1180 while True:
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001181 newsocket, fromaddr = bindsocket.accept()
1182 connstream = context.wrap_socket(newsocket, server_side=True)
1183 try:
1184 deal_with_client(connstream)
1185 finally:
Antoine Pitroub205d582011-01-02 22:09:27 +00001186 connstream.shutdown(socket.SHUT_RDWR)
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001187 connstream.close()
Thomas Woutersed03b412007-08-28 21:37:11 +00001188
Antoine Pitrou152efa22010-05-16 18:19:27 +00001189Then you'll read data from the ``connstream`` and do something with it till you
Georg Brandl7f01a132009-09-16 15:58:14 +00001190are finished with the client (or the client is finished with you)::
Thomas Woutersed03b412007-08-28 21:37:11 +00001191
1192 def deal_with_client(connstream):
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001193 data = connstream.recv(1024)
1194 # empty data means the client is finished with us
1195 while data:
1196 if not do_something(connstream, data):
1197 # we'll assume do_something returns False
1198 # when we're finished with client
1199 break
1200 data = connstream.recv(1024)
1201 # finished with client
Thomas Woutersed03b412007-08-28 21:37:11 +00001202
Antoine Pitrou152efa22010-05-16 18:19:27 +00001203And go back to listening for new client connections (of course, a real server
1204would probably handle each client connection in a separate thread, or put
1205the sockets in non-blocking mode and use an event loop).
1206
1207
Antoine Pitrou6f5dcb12011-07-11 01:35:48 +02001208.. _ssl-nonblocking:
1209
1210Notes on non-blocking sockets
1211-----------------------------
1212
1213When working with non-blocking sockets, there are several things you need
1214to be aware of:
1215
1216- Calling :func:`~select.select` tells you that the OS-level socket can be
1217 read from (or written to), but it does not imply that there is sufficient
1218 data at the upper SSL layer. For example, only part of an SSL frame might
1219 have arrived. Therefore, you must be ready to handle :meth:`SSLSocket.recv`
1220 and :meth:`SSLSocket.send` failures, and retry after another call to
1221 :func:`~select.select`.
1222
1223 (of course, similar provisions apply when using other primitives such as
1224 :func:`~select.poll`)
1225
1226- The SSL handshake itself will be non-blocking: the
1227 :meth:`SSLSocket.do_handshake` method has to be retried until it returns
1228 successfully. Here is a synopsis using :func:`~select.select` to wait for
1229 the socket's readiness::
1230
1231 while True:
1232 try:
1233 sock.do_handshake()
1234 break
Antoine Pitrou873bf262011-10-27 23:59:03 +02001235 except ssl.SSLWantReadError:
1236 select.select([sock], [], [])
1237 except ssl.SSLWantWriteError:
1238 select.select([], [sock], [])
Antoine Pitrou6f5dcb12011-07-11 01:35:48 +02001239
1240
Antoine Pitrou152efa22010-05-16 18:19:27 +00001241.. _ssl-security:
1242
1243Security considerations
1244-----------------------
1245
1246Verifying certificates
1247^^^^^^^^^^^^^^^^^^^^^^
1248
1249:const:`CERT_NONE` is the default. Since it does not authenticate the other
1250peer, it can be insecure, especially in client mode where most of time you
1251would like to ensure the authenticity of the server you're talking to.
1252Therefore, when in client mode, it is highly recommended to use
1253:const:`CERT_REQUIRED`. However, it is in itself not sufficient; you also
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001254have to check that the server certificate, which can be obtained by calling
1255:meth:`SSLSocket.getpeercert`, matches the desired service. For many
1256protocols and applications, the service can be identified by the hostname;
1257in this case, the :func:`match_hostname` function can be used.
Antoine Pitrou152efa22010-05-16 18:19:27 +00001258
1259In server mode, if you want to authenticate your clients using the SSL layer
1260(rather than using a higher-level authentication mechanism), you'll also have
1261to specify :const:`CERT_REQUIRED` and similarly check the client certificate.
1262
1263 .. note::
1264
1265 In client mode, :const:`CERT_OPTIONAL` and :const:`CERT_REQUIRED` are
1266 equivalent unless anonymous ciphers are enabled (they are disabled
1267 by default).
Thomas Woutersed03b412007-08-28 21:37:11 +00001268
Antoine Pitroub5218772010-05-21 09:56:06 +00001269Protocol versions
1270^^^^^^^^^^^^^^^^^
1271
1272SSL version 2 is considered insecure and is therefore dangerous to use. If
1273you want maximum compatibility between clients and servers, it is recommended
1274to use :const:`PROTOCOL_SSLv23` as the protocol version and then disable
1275SSLv2 explicitly using the :data:`SSLContext.options` attribute::
1276
1277 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
1278 context.options |= ssl.OP_NO_SSLv2
1279
1280The SSL context created above will allow SSLv3 and TLSv1 connections, but
1281not SSLv2.
1282
Antoine Pitroub7ffed82012-01-04 02:53:44 +01001283Cipher selection
1284^^^^^^^^^^^^^^^^
1285
1286If you have advanced security requirements, fine-tuning of the ciphers
1287enabled when negotiating a SSL session is possible through the
1288:meth:`SSLContext.set_ciphers` method. Starting from Python 3.2.3, the
1289ssl module disables certain weak ciphers by default, but you may want
1290to further restrict the cipher choice. For example::
1291
1292 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
1293 context.set_ciphers('HIGH:!aNULL:!eNULL')
1294
1295The ``!aNULL:!eNULL`` part of the cipher spec is necessary to disable ciphers
1296which don't provide both encryption and authentication. Be sure to read
1297OpenSSL's documentation about the `cipher list
1298format <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_.
1299If you want to check which ciphers are enabled by a given cipher list,
1300use the ``openssl ciphers`` command on your system.
1301
Georg Brandl48310cd2009-01-03 21:18:54 +00001302
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001303.. seealso::
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001304
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001305 Class :class:`socket.socket`
1306 Documentation of underlying :mod:`socket` class
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001307
Antoine Pitrouf394e472011-10-07 16:58:07 +02001308 `TLS (Transport Layer Security) and SSL (Secure Socket Layer) <http://www3.rad.com/networks/applications/secure/tls.htm>`_
1309 Debby Koren
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001310
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001311 `RFC 1422: Privacy Enhancement for Internet Electronic Mail: Part II: Certificate-Based Key Management <http://www.ietf.org/rfc/rfc1422>`_
1312 Steve Kent
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001313
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001314 `RFC 1750: Randomness Recommendations for Security <http://www.ietf.org/rfc/rfc1750>`_
1315 D. Eastlake et. al.
Thomas Wouters89d996e2007-09-08 17:39:28 +00001316
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001317 `RFC 3280: Internet X.509 Public Key Infrastructure Certificate and CRL Profile <http://www.ietf.org/rfc/rfc3280>`_
1318 Housley et. al.
Antoine Pitroud5323212010-10-22 18:19:07 +00001319
1320 `RFC 4366: Transport Layer Security (TLS) Extensions <http://www.ietf.org/rfc/rfc4366>`_
1321 Blake-Wilson et. al.