blob: 77196e10fc395db0d379363cf2202e4fc40e7d04 [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
Georg Brandl7f01a132009-09-16 15:58:14 +0000576 If the parameter ``binary_form`` is :const:`False`, and a certificate was
577 received from the peer, this method returns a :class:`dict` instance. If the
578 certificate was not validated, the dict is empty. If the certificate was
Antoine 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
616 certificate. This return value is independent of validation; if validation
617 was required (:const:`CERT_OPTIONAL` or :const:`CERT_REQUIRED`), it will have
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000618 been validated, but if :const:`CERT_NONE` was used to establish the
619 connection, the certificate, if present, will not have been validated.
620
Antoine Pitroufb046912010-11-09 20:21:19 +0000621 .. versionchanged:: 3.2
622 The returned dictionary includes additional items such as ``issuer``
623 and ``notBefore``.
624
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000625.. method:: SSLSocket.cipher()
626
Georg Brandl7f01a132009-09-16 15:58:14 +0000627 Returns a three-value tuple containing the name of the cipher being used, the
628 version of the SSL protocol that defines its use, and the number of secret
629 bits being used. If no connection has been established, returns ``None``.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000630
Antoine Pitrou8abdb8a2011-12-20 10:13:40 +0100631.. method:: SSLSocket.compression()
632
633 Return the compression algorithm being used as a string, or ``None``
634 if the connection isn't compressed.
635
636 If the higher-level protocol supports its own compression mechanism,
637 you can use :data:`OP_NO_COMPRESSION` to disable SSL-level compression.
638
639 .. versionadded:: 3.3
640
Antoine Pitroud6494802011-07-21 01:11:30 +0200641.. method:: SSLSocket.get_channel_binding(cb_type="tls-unique")
642
643 Get channel binding data for current connection, as a bytes object. Returns
644 ``None`` if not connected or the handshake has not been completed.
645
646 The *cb_type* parameter allow selection of the desired channel binding
647 type. Valid channel binding types are listed in the
648 :data:`CHANNEL_BINDING_TYPES` list. Currently only the 'tls-unique' channel
649 binding, defined by :rfc:`5929`, is supported. :exc:`ValueError` will be
650 raised if an unsupported channel binding type is requested.
651
652 .. versionadded:: 3.3
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000653
Antoine Pitroud5d17eb2012-03-22 00:23:03 +0100654.. method:: SSLSocket.selected_npn_protocol()
655
656 Returns the protocol that was selected during the TLS/SSL handshake. If
657 :meth:`SSLContext.set_npn_protocols` was not called, or if the other party
658 does not support NPN, or if the handshake has not yet happened, this will
659 return ``None``.
660
661 .. versionadded:: 3.3
662
Benjamin Peterson4aeec042008-08-19 21:42:13 +0000663.. method:: SSLSocket.unwrap()
664
Georg Brandl7f01a132009-09-16 15:58:14 +0000665 Performs the SSL shutdown handshake, which removes the TLS layer from the
666 underlying socket, and returns the underlying socket object. This can be
667 used to go from encrypted operation over a connection to unencrypted. The
668 returned socket should always be used for further communication with the
669 other side of the connection, rather than the original socket.
Benjamin Peterson4aeec042008-08-19 21:42:13 +0000670
Antoine Pitrouec883db2010-05-24 21:20:20 +0000671.. attribute:: SSLSocket.context
672
673 The :class:`SSLContext` object this SSL socket is tied to. If the SSL
674 socket was created using the top-level :func:`wrap_socket` function
675 (rather than :meth:`SSLContext.wrap_socket`), this is a custom context
676 object created for this SSL socket.
677
678 .. versionadded:: 3.2
679
680
Antoine Pitrou152efa22010-05-16 18:19:27 +0000681SSL Contexts
682------------
683
Antoine Pitroucafaad42010-05-24 15:58:43 +0000684.. versionadded:: 3.2
685
Antoine Pitroub0182c82010-10-12 20:09:02 +0000686An SSL context holds various data longer-lived than single SSL connections,
687such as SSL configuration options, certificate(s) and private key(s).
688It also manages a cache of SSL sessions for server-side sockets, in order
689to speed up repeated connections from the same clients.
690
Antoine Pitrou152efa22010-05-16 18:19:27 +0000691.. class:: SSLContext(protocol)
692
Antoine Pitroub0182c82010-10-12 20:09:02 +0000693 Create a new SSL context. You must pass *protocol* which must be one
694 of the ``PROTOCOL_*`` constants defined in this module.
695 :data:`PROTOCOL_SSLv23` is recommended for maximum interoperability.
696
Antoine Pitrou152efa22010-05-16 18:19:27 +0000697
698:class:`SSLContext` objects have the following methods and attributes:
699
Antoine Pitrou4fd1e6a2011-08-25 14:39:44 +0200700.. method:: SSLContext.load_cert_chain(certfile, keyfile=None, password=None)
Antoine Pitrou152efa22010-05-16 18:19:27 +0000701
702 Load a private key and the corresponding certificate. The *certfile*
703 string must be the path to a single file in PEM format containing the
704 certificate as well as any number of CA certificates needed to establish
705 the certificate's authenticity. The *keyfile* string, if present, must
706 point to a file containing the private key in. Otherwise the private
707 key will be taken from *certfile* as well. See the discussion of
708 :ref:`ssl-certificates` for more information on how the certificate
709 is stored in the *certfile*.
710
Antoine Pitrou4fd1e6a2011-08-25 14:39:44 +0200711 The *password* argument may be a function to call to get the password for
712 decrypting the private key. It will only be called if the private key is
713 encrypted and a password is necessary. It will be called with no arguments,
714 and it should return a string, bytes, or bytearray. If the return value is
715 a string it will be encoded as UTF-8 before using it to decrypt the key.
716 Alternatively a string, bytes, or bytearray value may be supplied directly
717 as the *password* argument. It will be ignored if the private key is not
718 encrypted and no password is needed.
719
720 If the *password* argument is not specified and a password is required,
721 OpenSSL's built-in password prompting mechanism will be used to
722 interactively prompt the user for a password.
723
Antoine Pitrou152efa22010-05-16 18:19:27 +0000724 An :class:`SSLError` is raised if the private key doesn't
725 match with the certificate.
726
Antoine Pitrou4fd1e6a2011-08-25 14:39:44 +0200727 .. versionchanged:: 3.3
728 New optional argument *password*.
729
Antoine Pitrou152efa22010-05-16 18:19:27 +0000730.. method:: SSLContext.load_verify_locations(cafile=None, capath=None)
731
732 Load a set of "certification authority" (CA) certificates used to validate
733 other peers' certificates when :data:`verify_mode` is other than
734 :data:`CERT_NONE`. At least one of *cafile* or *capath* must be specified.
735
736 The *cafile* string, if present, is the path to a file of concatenated
737 CA certificates in PEM format. See the discussion of
738 :ref:`ssl-certificates` for more information about how to arrange the
739 certificates in this file.
740
741 The *capath* string, if present, is
742 the path to a directory containing several CA certificates in PEM format,
743 following an `OpenSSL specific layout
744 <http://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html>`_.
745
Antoine Pitrou664c2d12010-11-17 20:29:42 +0000746.. method:: SSLContext.set_default_verify_paths()
747
748 Load a set of default "certification authority" (CA) certificates from
749 a filesystem path defined when building the OpenSSL library. Unfortunately,
750 there's no easy way to know whether this method succeeds: no error is
751 returned if no certificates are to be found. When the OpenSSL library is
752 provided as part of the operating system, though, it is likely to be
753 configured properly.
754
Antoine Pitrou152efa22010-05-16 18:19:27 +0000755.. method:: SSLContext.set_ciphers(ciphers)
756
757 Set the available ciphers for sockets created with this context.
758 It should be a string in the `OpenSSL cipher list format
759 <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_.
760 If no cipher can be selected (because compile-time options or other
761 configuration forbids use of all the specified ciphers), an
762 :class:`SSLError` will be raised.
763
764 .. note::
765 when connected, the :meth:`SSLSocket.cipher` method of SSL sockets will
766 give the currently selected cipher.
767
Antoine Pitroud5d17eb2012-03-22 00:23:03 +0100768.. method:: SSLContext.set_npn_protocols(protocols)
769
770 Specify which protocols the socket should avertise during the SSL/TLS
771 handshake. It should be a list of strings, like ``['http/1.1', 'spdy/2']``,
772 ordered by preference. The selection of a protocol will happen during the
773 handshake, and will play out according to the `NPN draft specification
774 <http://tools.ietf.org/html/draft-agl-tls-nextprotoneg>`_. After a
775 successful handshake, the :meth:`SSLSocket.selected_npn_protocol` method will
776 return the agreed-upon protocol.
777
778 This method will raise :exc:`NotImplementedError` if :data:`HAS_NPN` is
779 False.
780
781 .. versionadded:: 3.3
782
Antoine Pitrou0e576f12011-12-22 10:03:38 +0100783.. method:: SSLContext.load_dh_params(dhfile)
784
785 Load the key generation parameters for Diffie-Helman (DH) key exchange.
786 Using DH key exchange improves forward secrecy at the expense of
787 computational resources (both on the server and on the client).
788 The *dhfile* parameter should be the path to a file containing DH
789 parameters in PEM format.
790
791 This setting doesn't apply to client sockets. You can also use the
792 :data:`OP_SINGLE_DH_USE` option to further improve security.
793
794 .. versionadded:: 3.3
795
Antoine Pitrou923df6f2011-12-19 17:16:51 +0100796.. method:: SSLContext.set_ecdh_curve(curve_name)
797
Antoine Pitrou0e576f12011-12-22 10:03:38 +0100798 Set the curve name for Elliptic Curve-based Diffie-Hellman (ECDH) key
799 exchange. ECDH is significantly faster than regular DH while arguably
800 as secure. The *curve_name* parameter should be a string describing
Antoine Pitrou923df6f2011-12-19 17:16:51 +0100801 a well-known elliptic curve, for example ``prime256v1`` for a widely
802 supported curve.
803
804 This setting doesn't apply to client sockets. You can also use the
805 :data:`OP_SINGLE_ECDH_USE` option to further improve security.
806
Antoine Pitrou501da612011-12-21 09:27:41 +0100807 This method is not available if :data:`HAS_ECDH` is False.
808
Antoine Pitrou923df6f2011-12-19 17:16:51 +0100809 .. versionadded:: 3.3
810
811 .. seealso::
812 `SSL/TLS & Perfect Forward Secrecy <http://vincent.bernat.im/en/blog/2011-ssl-perfect-forward-secrecy.html>`_
813 Vincent Bernat.
814
Antoine Pitroud5323212010-10-22 18:19:07 +0000815.. method:: SSLContext.wrap_socket(sock, server_side=False, \
816 do_handshake_on_connect=True, suppress_ragged_eofs=True, \
817 server_hostname=None)
Antoine Pitrou152efa22010-05-16 18:19:27 +0000818
819 Wrap an existing Python socket *sock* and return an :class:`SSLSocket`
820 object. The SSL socket is tied to the context, its settings and
821 certificates. The parameters *server_side*, *do_handshake_on_connect*
822 and *suppress_ragged_eofs* have the same meaning as in the top-level
823 :func:`wrap_socket` function.
824
Antoine Pitroud5323212010-10-22 18:19:07 +0000825 On client connections, the optional parameter *server_hostname* specifies
826 the hostname of the service which we are connecting to. This allows a
827 single server to host multiple SSL-based services with distinct certificates,
828 quite similarly to HTTP virtual hosts. Specifying *server_hostname*
829 will raise a :exc:`ValueError` if the OpenSSL library doesn't have support
830 for it (that is, if :data:`HAS_SNI` is :const:`False`). Specifying
831 *server_hostname* will also raise a :exc:`ValueError` if *server_side*
832 is true.
833
Antoine Pitroub0182c82010-10-12 20:09:02 +0000834.. method:: SSLContext.session_stats()
835
836 Get statistics about the SSL sessions created or managed by this context.
837 A dictionary is returned which maps the names of each `piece of information
838 <http://www.openssl.org/docs/ssl/SSL_CTX_sess_number.html>`_ to their
839 numeric values. For example, here is the total number of hits and misses
840 in the session cache since the context was created::
841
842 >>> stats = context.session_stats()
843 >>> stats['hits'], stats['misses']
844 (0, 0)
845
Antoine Pitroub5218772010-05-21 09:56:06 +0000846.. attribute:: SSLContext.options
847
848 An integer representing the set of SSL options enabled on this context.
849 The default value is :data:`OP_ALL`, but you can specify other options
850 such as :data:`OP_NO_SSLv2` by ORing them together.
851
852 .. note::
853 With versions of OpenSSL older than 0.9.8m, it is only possible
854 to set options, not to clear them. Attempting to clear an option
855 (by resetting the corresponding bits) will raise a ``ValueError``.
856
Antoine Pitrou152efa22010-05-16 18:19:27 +0000857.. attribute:: SSLContext.protocol
858
859 The protocol version chosen when constructing the context. This attribute
860 is read-only.
861
862.. attribute:: SSLContext.verify_mode
863
864 Whether to try to verify other peers' certificates and how to behave
865 if verification fails. This attribute must be one of
866 :data:`CERT_NONE`, :data:`CERT_OPTIONAL` or :data:`CERT_REQUIRED`.
867
868
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000869.. index:: single: certificates
870
871.. index:: single: X509 certificate
872
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000873.. _ssl-certificates:
874
Thomas Woutersed03b412007-08-28 21:37:11 +0000875Certificates
876------------
877
Georg Brandl7f01a132009-09-16 15:58:14 +0000878Certificates in general are part of a public-key / private-key system. In this
879system, each *principal*, (which may be a machine, or a person, or an
880organization) is assigned a unique two-part encryption key. One part of the key
881is public, and is called the *public key*; the other part is kept secret, and is
882called the *private key*. The two parts are related, in that if you encrypt a
883message with one of the parts, you can decrypt it with the other part, and
884**only** with the other part.
Thomas Woutersed03b412007-08-28 21:37:11 +0000885
Georg Brandl7f01a132009-09-16 15:58:14 +0000886A certificate contains information about two principals. It contains the name
887of a *subject*, and the subject's public key. It also contains a statement by a
888second principal, the *issuer*, that the subject is who he claims to be, and
889that this is indeed the subject's public key. The issuer's statement is signed
890with the issuer's private key, which only the issuer knows. However, anyone can
891verify the issuer's statement by finding the issuer's public key, decrypting the
892statement with it, and comparing it to the other information in the certificate.
893The certificate also contains information about the time period over which it is
894valid. This is expressed as two fields, called "notBefore" and "notAfter".
Thomas Woutersed03b412007-08-28 21:37:11 +0000895
Georg Brandl7f01a132009-09-16 15:58:14 +0000896In the Python use of certificates, a client or server can use a certificate to
897prove who they are. The other side of a network connection can also be required
898to produce a certificate, and that certificate can be validated to the
899satisfaction of the client or server that requires such validation. The
900connection attempt can be set to raise an exception if the validation fails.
901Validation is done automatically, by the underlying OpenSSL framework; the
902application need not concern itself with its mechanics. But the application
903does usually need to provide sets of certificates to allow this process to take
904place.
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000905
Georg Brandl7f01a132009-09-16 15:58:14 +0000906Python uses files to contain certificates. They should be formatted as "PEM"
907(see :rfc:`1422`), which is a base-64 encoded form wrapped with a header line
908and a footer line::
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000909
910 -----BEGIN CERTIFICATE-----
911 ... (certificate in base64 PEM encoding) ...
912 -----END CERTIFICATE-----
913
Antoine Pitrou152efa22010-05-16 18:19:27 +0000914Certificate chains
915^^^^^^^^^^^^^^^^^^
916
Georg Brandl7f01a132009-09-16 15:58:14 +0000917The Python files which contain certificates can contain a sequence of
918certificates, sometimes called a *certificate chain*. This chain should start
919with the specific certificate for the principal who "is" the client or server,
920and then the certificate for the issuer of that certificate, and then the
921certificate for the issuer of *that* certificate, and so on up the chain till
922you get to a certificate which is *self-signed*, that is, a certificate which
923has the same subject and issuer, sometimes called a *root certificate*. The
924certificates should just be concatenated together in the certificate file. For
925example, suppose we had a three certificate chain, from our server certificate
926to the certificate of the certification authority that signed our server
927certificate, to the root certificate of the agency which issued the
928certification authority's certificate::
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000929
930 -----BEGIN CERTIFICATE-----
931 ... (certificate for your server)...
932 -----END CERTIFICATE-----
933 -----BEGIN CERTIFICATE-----
934 ... (the certificate for the CA)...
935 -----END CERTIFICATE-----
936 -----BEGIN CERTIFICATE-----
937 ... (the root certificate for the CA's issuer)...
938 -----END CERTIFICATE-----
939
Antoine Pitrou152efa22010-05-16 18:19:27 +0000940CA certificates
941^^^^^^^^^^^^^^^
942
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000943If you are going to require validation of the other side of the connection's
944certificate, you need to provide a "CA certs" file, filled with the certificate
Georg Brandl7f01a132009-09-16 15:58:14 +0000945chains for each issuer you are willing to trust. Again, this file just contains
946these chains concatenated together. For validation, Python will use the first
947chain it finds in the file which matches. Some "standard" root certificates are
948available from various certification authorities: `CACert.org
949<http://www.cacert.org/index.php?id=3>`_, `Thawte
950<http://www.thawte.com/roots/>`_, `Verisign
951<http://www.verisign.com/support/roots.html>`_, `Positive SSL
952<http://www.PositiveSSL.com/ssl-certificate-support/cert_installation/UTN-USERFirst-Hardware.crt>`_
953(used by python.org), `Equifax and GeoTrust
954<http://www.geotrust.com/resources/root_certificates/index.asp>`_.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000955
Georg Brandl7f01a132009-09-16 15:58:14 +0000956In general, if you are using SSL3 or TLS1, you don't need to put the full chain
957in your "CA certs" file; you only need the root certificates, and the remote
958peer is supposed to furnish the other certificates necessary to chain from its
959certificate to a root certificate. See :rfc:`4158` for more discussion of the
960way in which certification chains can be built.
Thomas Woutersed03b412007-08-28 21:37:11 +0000961
Antoine Pitrou152efa22010-05-16 18:19:27 +0000962Combined key and certificate
963^^^^^^^^^^^^^^^^^^^^^^^^^^^^
964
965Often the private key is stored in the same file as the certificate; in this
966case, only the ``certfile`` parameter to :meth:`SSLContext.load_cert_chain`
967and :func:`wrap_socket` needs to be passed. If the private key is stored
968with the certificate, it should come before the first certificate in
969the certificate chain::
970
971 -----BEGIN RSA PRIVATE KEY-----
972 ... (private key in base64 encoding) ...
973 -----END RSA PRIVATE KEY-----
974 -----BEGIN CERTIFICATE-----
975 ... (certificate in base64 PEM encoding) ...
976 -----END CERTIFICATE-----
977
978Self-signed certificates
979^^^^^^^^^^^^^^^^^^^^^^^^
980
Georg Brandl7f01a132009-09-16 15:58:14 +0000981If you are going to create a server that provides SSL-encrypted connection
982services, you will need to acquire a certificate for that service. There are
983many ways of acquiring appropriate certificates, such as buying one from a
984certification authority. Another common practice is to generate a self-signed
985certificate. The simplest way to do this is with the OpenSSL package, using
986something like the following::
Thomas Woutersed03b412007-08-28 21:37:11 +0000987
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000988 % openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
989 Generating a 1024 bit RSA private key
990 .......++++++
991 .............................++++++
992 writing new private key to 'cert.pem'
993 -----
994 You are about to be asked to enter information that will be incorporated
995 into your certificate request.
996 What you are about to enter is what is called a Distinguished Name or a DN.
997 There are quite a few fields but you can leave some blank
998 For some fields there will be a default value,
999 If you enter '.', the field will be left blank.
1000 -----
1001 Country Name (2 letter code) [AU]:US
1002 State or Province Name (full name) [Some-State]:MyState
1003 Locality Name (eg, city) []:Some City
1004 Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Organization, Inc.
1005 Organizational Unit Name (eg, section) []:My Group
1006 Common Name (eg, YOUR name) []:myserver.mygroup.myorganization.com
1007 Email Address []:ops@myserver.mygroup.myorganization.com
1008 %
Thomas Woutersed03b412007-08-28 21:37:11 +00001009
Georg Brandl7f01a132009-09-16 15:58:14 +00001010The disadvantage of a self-signed certificate is that it is its own root
1011certificate, and no one else will have it in their cache of known (and trusted)
1012root certificates.
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001013
1014
Thomas Woutersed03b412007-08-28 21:37:11 +00001015Examples
1016--------
1017
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001018Testing for SSL support
1019^^^^^^^^^^^^^^^^^^^^^^^
1020
Georg Brandl7f01a132009-09-16 15:58:14 +00001021To test for the presence of SSL support in a Python installation, user code
1022should use the following idiom::
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001023
1024 try:
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001025 import ssl
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001026 except ImportError:
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001027 pass
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001028 else:
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001029 ... # do something that requires SSL support
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001030
1031Client-side operation
1032^^^^^^^^^^^^^^^^^^^^^
1033
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001034This example connects to an SSL server and prints the server's certificate::
Thomas Woutersed03b412007-08-28 21:37:11 +00001035
1036 import socket, ssl, pprint
1037
1038 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001039 # require a certificate from the server
1040 ssl_sock = ssl.wrap_socket(s,
1041 ca_certs="/etc/ca_certs_file",
1042 cert_reqs=ssl.CERT_REQUIRED)
Thomas Woutersed03b412007-08-28 21:37:11 +00001043 ssl_sock.connect(('www.verisign.com', 443))
1044
Georg Brandl6911e3c2007-09-04 07:15:32 +00001045 pprint.pprint(ssl_sock.getpeercert())
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001046 # note that closing the SSLSocket will also close the underlying socket
Thomas Woutersed03b412007-08-28 21:37:11 +00001047 ssl_sock.close()
1048
Antoine Pitrou441ae042012-01-06 20:06:15 +01001049As of January 6, 2012, the certificate printed by this program looks like
Georg Brandl7f01a132009-09-16 15:58:14 +00001050this::
Thomas Woutersed03b412007-08-28 21:37:11 +00001051
Antoine Pitrou441ae042012-01-06 20:06:15 +01001052 {'issuer': ((('countryName', 'US'),),
1053 (('organizationName', 'VeriSign, Inc.'),),
1054 (('organizationalUnitName', 'VeriSign Trust Network'),),
1055 (('organizationalUnitName',
1056 'Terms of use at https://www.verisign.com/rpa (c)06'),),
1057 (('commonName',
1058 'VeriSign Class 3 Extended Validation SSL SGC CA'),)),
1059 'notAfter': 'May 25 23:59:59 2012 GMT',
1060 'notBefore': 'May 26 00:00:00 2010 GMT',
1061 'serialNumber': '53D2BEF924A7245E83CA01E46CAA2477',
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001062 'subject': ((('1.3.6.1.4.1.311.60.2.1.3', 'US'),),
1063 (('1.3.6.1.4.1.311.60.2.1.2', 'Delaware'),),
1064 (('businessCategory', 'V1.0, Clause 5.(b)'),),
1065 (('serialNumber', '2497886'),),
1066 (('countryName', 'US'),),
1067 (('postalCode', '94043'),),
1068 (('stateOrProvinceName', 'California'),),
1069 (('localityName', 'Mountain View'),),
1070 (('streetAddress', '487 East Middlefield Road'),),
1071 (('organizationName', 'VeriSign, Inc.'),),
1072 (('organizationalUnitName', ' Production Security Services'),),
Antoine Pitrou441ae042012-01-06 20:06:15 +01001073 (('commonName', 'www.verisign.com'),)),
1074 'subjectAltName': (('DNS', 'www.verisign.com'),
1075 ('DNS', 'verisign.com'),
1076 ('DNS', 'www.verisign.net'),
1077 ('DNS', 'verisign.net'),
1078 ('DNS', 'www.verisign.mobi'),
1079 ('DNS', 'verisign.mobi'),
1080 ('DNS', 'www.verisign.eu'),
1081 ('DNS', 'verisign.eu')),
1082 'version': 3}
Thomas Woutersed03b412007-08-28 21:37:11 +00001083
Antoine Pitrou152efa22010-05-16 18:19:27 +00001084This other example first creates an SSL context, instructs it to verify
1085certificates sent by peers, and feeds it a set of recognized certificate
1086authorities (CA)::
1087
1088 >>> context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001089 >>> context.verify_mode = ssl.CERT_REQUIRED
Antoine Pitrou152efa22010-05-16 18:19:27 +00001090 >>> context.load_verify_locations("/etc/ssl/certs/ca-bundle.crt")
1091
1092(it is assumed your operating system places a bundle of all CA certificates
1093in ``/etc/ssl/certs/ca-bundle.crt``; if not, you'll get an error and have
1094to adjust the location)
1095
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001096When you use the context to connect to a server, :const:`CERT_REQUIRED`
Antoine Pitrou152efa22010-05-16 18:19:27 +00001097validates the server certificate: it ensures that the server certificate
1098was signed with one of the CA certificates, and checks the signature for
1099correctness::
1100
1101 >>> conn = context.wrap_socket(socket.socket(socket.AF_INET))
1102 >>> conn.connect(("linuxfr.org", 443))
1103
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001104You should then fetch the certificate and check its fields for conformity::
Antoine Pitrou152efa22010-05-16 18:19:27 +00001105
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001106 >>> cert = conn.getpeercert()
1107 >>> ssl.match_hostname(cert, "linuxfr.org")
1108
1109Visual inspection shows that the certificate does identify the desired service
1110(that is, the HTTPS host ``linuxfr.org``)::
1111
1112 >>> pprint.pprint(cert)
Antoine Pitrou441ae042012-01-06 20:06:15 +01001113 {'issuer': ((('organizationName', 'CAcert Inc.'),),
1114 (('organizationalUnitName', 'http://www.CAcert.org'),),
1115 (('commonName', 'CAcert Class 3 Root'),)),
1116 'notAfter': 'Jun 7 21:02:24 2013 GMT',
1117 'notBefore': 'Jun 8 21:02:24 2011 GMT',
1118 'serialNumber': 'D3E9',
Antoine Pitrou152efa22010-05-16 18:19:27 +00001119 'subject': ((('commonName', 'linuxfr.org'),),),
Antoine Pitrou441ae042012-01-06 20:06:15 +01001120 'subjectAltName': (('DNS', 'linuxfr.org'),
1121 ('othername', '<unsupported>'),
1122 ('DNS', 'linuxfr.org'),
1123 ('othername', '<unsupported>'),
1124 ('DNS', 'dev.linuxfr.org'),
1125 ('othername', '<unsupported>'),
1126 ('DNS', 'prod.linuxfr.org'),
1127 ('othername', '<unsupported>'),
1128 ('DNS', 'alpha.linuxfr.org'),
1129 ('othername', '<unsupported>'),
1130 ('DNS', '*.linuxfr.org'),
1131 ('othername', '<unsupported>')),
1132 'version': 3}
Antoine Pitrou152efa22010-05-16 18:19:27 +00001133
1134Now that you are assured of its authenticity, you can proceed to talk with
1135the server::
1136
Antoine Pitroudab64262010-09-19 13:31:06 +00001137 >>> conn.sendall(b"HEAD / HTTP/1.0\r\nHost: linuxfr.org\r\n\r\n")
1138 >>> pprint.pprint(conn.recv(1024).split(b"\r\n"))
Antoine Pitrou152efa22010-05-16 18:19:27 +00001139 [b'HTTP/1.1 302 Found',
1140 b'Date: Sun, 16 May 2010 13:43:28 GMT',
1141 b'Server: Apache/2.2',
1142 b'Location: https://linuxfr.org/pub/',
1143 b'Vary: Accept-Encoding',
1144 b'Connection: close',
1145 b'Content-Type: text/html; charset=iso-8859-1',
1146 b'',
1147 b'']
1148
Antoine Pitrou152efa22010-05-16 18:19:27 +00001149See the discussion of :ref:`ssl-security` below.
1150
1151
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001152Server-side operation
1153^^^^^^^^^^^^^^^^^^^^^
1154
Antoine Pitrou152efa22010-05-16 18:19:27 +00001155For server operation, typically you'll need to have a server certificate, and
1156private key, each in a file. You'll first create a context holding the key
1157and the certificate, so that clients can check your authenticity. Then
1158you'll open a socket, bind it to a port, call :meth:`listen` on it, and start
1159waiting for clients to connect::
Thomas Woutersed03b412007-08-28 21:37:11 +00001160
1161 import socket, ssl
1162
Antoine Pitrou152efa22010-05-16 18:19:27 +00001163 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
1164 context.load_cert_chain(certfile="mycertfile", keyfile="mykeyfile")
1165
Thomas Woutersed03b412007-08-28 21:37:11 +00001166 bindsocket = socket.socket()
1167 bindsocket.bind(('myaddr.mydomain.com', 10023))
1168 bindsocket.listen(5)
1169
Antoine Pitrou152efa22010-05-16 18:19:27 +00001170When a client connects, you'll call :meth:`accept` on the socket to get the
1171new socket from the other end, and use the context's :meth:`SSLContext.wrap_socket`
1172method to create a server-side SSL socket for the connection::
Thomas Woutersed03b412007-08-28 21:37:11 +00001173
1174 while True:
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001175 newsocket, fromaddr = bindsocket.accept()
1176 connstream = context.wrap_socket(newsocket, server_side=True)
1177 try:
1178 deal_with_client(connstream)
1179 finally:
Antoine Pitroub205d582011-01-02 22:09:27 +00001180 connstream.shutdown(socket.SHUT_RDWR)
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001181 connstream.close()
Thomas Woutersed03b412007-08-28 21:37:11 +00001182
Antoine Pitrou152efa22010-05-16 18:19:27 +00001183Then you'll read data from the ``connstream`` and do something with it till you
Georg Brandl7f01a132009-09-16 15:58:14 +00001184are finished with the client (or the client is finished with you)::
Thomas Woutersed03b412007-08-28 21:37:11 +00001185
1186 def deal_with_client(connstream):
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001187 data = connstream.recv(1024)
1188 # empty data means the client is finished with us
1189 while data:
1190 if not do_something(connstream, data):
1191 # we'll assume do_something returns False
1192 # when we're finished with client
1193 break
1194 data = connstream.recv(1024)
1195 # finished with client
Thomas Woutersed03b412007-08-28 21:37:11 +00001196
Antoine Pitrou152efa22010-05-16 18:19:27 +00001197And go back to listening for new client connections (of course, a real server
1198would probably handle each client connection in a separate thread, or put
1199the sockets in non-blocking mode and use an event loop).
1200
1201
Antoine Pitrou6f5dcb12011-07-11 01:35:48 +02001202.. _ssl-nonblocking:
1203
1204Notes on non-blocking sockets
1205-----------------------------
1206
1207When working with non-blocking sockets, there are several things you need
1208to be aware of:
1209
1210- Calling :func:`~select.select` tells you that the OS-level socket can be
1211 read from (or written to), but it does not imply that there is sufficient
1212 data at the upper SSL layer. For example, only part of an SSL frame might
1213 have arrived. Therefore, you must be ready to handle :meth:`SSLSocket.recv`
1214 and :meth:`SSLSocket.send` failures, and retry after another call to
1215 :func:`~select.select`.
1216
1217 (of course, similar provisions apply when using other primitives such as
1218 :func:`~select.poll`)
1219
1220- The SSL handshake itself will be non-blocking: the
1221 :meth:`SSLSocket.do_handshake` method has to be retried until it returns
1222 successfully. Here is a synopsis using :func:`~select.select` to wait for
1223 the socket's readiness::
1224
1225 while True:
1226 try:
1227 sock.do_handshake()
1228 break
Antoine Pitrou873bf262011-10-27 23:59:03 +02001229 except ssl.SSLWantReadError:
1230 select.select([sock], [], [])
1231 except ssl.SSLWantWriteError:
1232 select.select([], [sock], [])
Antoine Pitrou6f5dcb12011-07-11 01:35:48 +02001233
1234
Antoine Pitrou152efa22010-05-16 18:19:27 +00001235.. _ssl-security:
1236
1237Security considerations
1238-----------------------
1239
1240Verifying certificates
1241^^^^^^^^^^^^^^^^^^^^^^
1242
1243:const:`CERT_NONE` is the default. Since it does not authenticate the other
1244peer, it can be insecure, especially in client mode where most of time you
1245would like to ensure the authenticity of the server you're talking to.
1246Therefore, when in client mode, it is highly recommended to use
1247:const:`CERT_REQUIRED`. However, it is in itself not sufficient; you also
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001248have to check that the server certificate, which can be obtained by calling
1249:meth:`SSLSocket.getpeercert`, matches the desired service. For many
1250protocols and applications, the service can be identified by the hostname;
1251in this case, the :func:`match_hostname` function can be used.
Antoine Pitrou152efa22010-05-16 18:19:27 +00001252
1253In server mode, if you want to authenticate your clients using the SSL layer
1254(rather than using a higher-level authentication mechanism), you'll also have
1255to specify :const:`CERT_REQUIRED` and similarly check the client certificate.
1256
1257 .. note::
1258
1259 In client mode, :const:`CERT_OPTIONAL` and :const:`CERT_REQUIRED` are
1260 equivalent unless anonymous ciphers are enabled (they are disabled
1261 by default).
Thomas Woutersed03b412007-08-28 21:37:11 +00001262
Antoine Pitroub5218772010-05-21 09:56:06 +00001263Protocol versions
1264^^^^^^^^^^^^^^^^^
1265
1266SSL version 2 is considered insecure and is therefore dangerous to use. If
1267you want maximum compatibility between clients and servers, it is recommended
1268to use :const:`PROTOCOL_SSLv23` as the protocol version and then disable
1269SSLv2 explicitly using the :data:`SSLContext.options` attribute::
1270
1271 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
1272 context.options |= ssl.OP_NO_SSLv2
1273
1274The SSL context created above will allow SSLv3 and TLSv1 connections, but
1275not SSLv2.
1276
Antoine Pitroub7ffed82012-01-04 02:53:44 +01001277Cipher selection
1278^^^^^^^^^^^^^^^^
1279
1280If you have advanced security requirements, fine-tuning of the ciphers
1281enabled when negotiating a SSL session is possible through the
1282:meth:`SSLContext.set_ciphers` method. Starting from Python 3.2.3, the
1283ssl module disables certain weak ciphers by default, but you may want
1284to further restrict the cipher choice. For example::
1285
1286 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
1287 context.set_ciphers('HIGH:!aNULL:!eNULL')
1288
1289The ``!aNULL:!eNULL`` part of the cipher spec is necessary to disable ciphers
1290which don't provide both encryption and authentication. Be sure to read
1291OpenSSL's documentation about the `cipher list
1292format <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_.
1293If you want to check which ciphers are enabled by a given cipher list,
1294use the ``openssl ciphers`` command on your system.
1295
Georg Brandl48310cd2009-01-03 21:18:54 +00001296
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001297.. seealso::
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001298
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001299 Class :class:`socket.socket`
1300 Documentation of underlying :mod:`socket` class
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001301
Antoine Pitrouf394e472011-10-07 16:58:07 +02001302 `TLS (Transport Layer Security) and SSL (Secure Socket Layer) <http://www3.rad.com/networks/applications/secure/tls.htm>`_
1303 Debby Koren
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001304
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001305 `RFC 1422: Privacy Enhancement for Internet Electronic Mail: Part II: Certificate-Based Key Management <http://www.ietf.org/rfc/rfc1422>`_
1306 Steve Kent
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001307
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001308 `RFC 1750: Randomness Recommendations for Security <http://www.ietf.org/rfc/rfc1750>`_
1309 D. Eastlake et. al.
Thomas Wouters89d996e2007-09-08 17:39:28 +00001310
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001311 `RFC 3280: Internet X.509 Public Key Infrastructure Certificate and CRL Profile <http://www.ietf.org/rfc/rfc3280>`_
1312 Housley et. al.
Antoine Pitroud5323212010-10-22 18:19:07 +00001313
1314 `RFC 4366: Transport Layer Security (TLS) Extensions <http://www.ietf.org/rfc/rfc4366>`_
1315 Blake-Wilson et. al.