blob: 8092581ece403b32b76ef635222d3bcee010efe7 [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 Pitrou41032a62011-10-27 23:56:55 +020062.. exception:: SSLZeroReturnError
63
64 A subclass of :exc:`SSLError` raised when trying to read or write and
65 the SSL connection has been closed cleanly. Note that this doesn't
66 mean that the underlying transport (read TCP) has been closed.
67
68 .. versionadded:: 3.3
69
70.. exception:: SSLWantReadError
71
72 A subclass of :exc:`SSLError` raised by a :ref:`non-blocking SSL socket
73 <ssl-nonblocking>` when trying to read or write data, but more data needs
74 to be received on the underlying TCP transport before the request can be
75 fulfilled.
76
77 .. versionadded:: 3.3
78
79.. exception:: SSLWantWriteError
80
81 A subclass of :exc:`SSLError` raised by a :ref:`non-blocking SSL socket
82 <ssl-nonblocking>` when trying to read or write data, but more data needs
83 to be sent on the underlying TCP transport before the request can be
84 fulfilled.
85
86 .. versionadded:: 3.3
87
88.. exception:: SSLSyscallError
89
90 A subclass of :exc:`SSLError` raised when a system error was encountered
91 while trying to fulfill an operation on a SSL socket. Unfortunately,
92 there is no easy way to inspect the original errno number.
93
94 .. versionadded:: 3.3
95
96.. exception:: SSLEOFError
97
98 A subclass of :exc:`SSLError` raised when the SSL connection has been
Antoine Pitrouf3dc2d72011-10-28 00:01:03 +020099 terminated abruptly. Generally, you shouldn't try to reuse the underlying
Antoine Pitrou41032a62011-10-27 23:56:55 +0200100 transport when this error is encountered.
101
102 .. versionadded:: 3.3
103
Antoine Pitrou59fdd672010-10-08 10:37:08 +0000104.. exception:: CertificateError
105
106 Raised to signal an error with a certificate (such as mismatching
107 hostname). Certificate errors detected by OpenSSL, though, raise
108 an :exc:`SSLError`.
109
110
111Socket creation
112^^^^^^^^^^^^^^^
113
114The following function allows for standalone socket creation. Starting from
115Python 3.2, it can be more flexible to use :meth:`SSLContext.wrap_socket`
116instead.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000117
Antoine Pitrou2d9cb9c2010-04-17 17:40:45 +0000118.. 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 +0000119
Georg Brandl7f01a132009-09-16 15:58:14 +0000120 Takes an instance ``sock`` of :class:`socket.socket`, and returns an instance
121 of :class:`ssl.SSLSocket`, a subtype of :class:`socket.socket`, which wraps
122 the underlying socket in an SSL context. For client-side sockets, the
123 context construction is lazy; if the underlying socket isn't connected yet,
124 the context construction will be performed after :meth:`connect` is called on
125 the socket. For server-side sockets, if the socket has no remote peer, it is
126 assumed to be a listening socket, and the server-side SSL wrapping is
127 automatically performed on client connections accepted via the :meth:`accept`
128 method. :func:`wrap_socket` may raise :exc:`SSLError`.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000129
Georg Brandl7f01a132009-09-16 15:58:14 +0000130 The ``keyfile`` and ``certfile`` parameters specify optional files which
131 contain a certificate to be used to identify the local side of the
132 connection. See the discussion of :ref:`ssl-certificates` for more
133 information on how the certificate is stored in the ``certfile``.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000134
Georg Brandl7f01a132009-09-16 15:58:14 +0000135 The parameter ``server_side`` is a boolean which identifies whether
136 server-side or client-side behavior is desired from this socket.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000137
Georg Brandl7f01a132009-09-16 15:58:14 +0000138 The parameter ``cert_reqs`` specifies whether a certificate is required from
139 the other side of the connection, and whether it will be validated if
140 provided. It must be one of the three values :const:`CERT_NONE`
141 (certificates ignored), :const:`CERT_OPTIONAL` (not required, but validated
142 if provided), or :const:`CERT_REQUIRED` (required and validated). If the
143 value of this parameter is not :const:`CERT_NONE`, then the ``ca_certs``
144 parameter must point to a file of CA certificates.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000145
Georg Brandl7f01a132009-09-16 15:58:14 +0000146 The ``ca_certs`` file contains a set of concatenated "certification
147 authority" certificates, which are used to validate certificates passed from
148 the other end of the connection. See the discussion of
149 :ref:`ssl-certificates` for more information about how to arrange the
150 certificates in this file.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000151
Georg Brandl7f01a132009-09-16 15:58:14 +0000152 The parameter ``ssl_version`` specifies which version of the SSL protocol to
153 use. Typically, the server chooses a particular protocol version, and the
154 client must adapt to the server's choice. Most of the versions are not
Antoine Pitrou84a2edc2012-01-09 21:35:11 +0100155 interoperable with the other versions. If not specified, the default is
156 :data:`PROTOCOL_SSLv23`; it provides the most compatibility with other
Georg Brandl7f01a132009-09-16 15:58:14 +0000157 versions.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000158
Georg Brandl7f01a132009-09-16 15:58:14 +0000159 Here's a table showing which versions in a client (down the side) can connect
160 to which versions in a server (along the top):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000161
162 .. table::
163
164 ======================== ========= ========= ========== =========
165 *client* / **server** **SSLv2** **SSLv3** **SSLv23** **TLSv1**
Christian Heimes255f53b2007-12-08 15:33:56 +0000166 ------------------------ --------- --------- ---------- ---------
Antoine Pitrou2d9cb9c2010-04-17 17:40:45 +0000167 *SSLv2* yes no yes no
Antoine Pitrouac8bfca2012-01-09 21:43:18 +0100168 *SSLv3* no yes yes no
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000169 *SSLv23* yes no yes no
170 *TLSv1* no no yes yes
171 ======================== ========= ========= ========== =========
172
Antoine Pitrou2d9cb9c2010-04-17 17:40:45 +0000173 .. note::
174
Benjamin Petersond7c3ed52010-06-27 22:32:30 +0000175 Which connections succeed will vary depending on the version of
176 OpenSSL. For instance, in some older versions of OpenSSL (such
177 as 0.9.7l on OS X 10.4), an SSLv2 client could not connect to an
178 SSLv23 server. Another example: beginning with OpenSSL 1.0.0,
179 an SSLv23 client will not actually attempt SSLv2 connections
180 unless you explicitly enable SSLv2 ciphers; for example, you
181 might specify ``"ALL"`` or ``"SSLv2"`` as the *ciphers* parameter
182 to enable them.
Antoine Pitrou2d9cb9c2010-04-17 17:40:45 +0000183
Benjamin Petersond7c3ed52010-06-27 22:32:30 +0000184 The *ciphers* parameter sets the available ciphers for this SSL object.
Antoine Pitrou2d9cb9c2010-04-17 17:40:45 +0000185 It should be a string in the `OpenSSL cipher list format
186 <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000187
Bill Janssen48dc27c2007-12-05 03:38:10 +0000188 The parameter ``do_handshake_on_connect`` specifies whether to do the SSL
189 handshake automatically after doing a :meth:`socket.connect`, or whether the
Georg Brandl7f01a132009-09-16 15:58:14 +0000190 application program will call it explicitly, by invoking the
191 :meth:`SSLSocket.do_handshake` method. Calling
192 :meth:`SSLSocket.do_handshake` explicitly gives the program control over the
193 blocking behavior of the socket I/O involved in the handshake.
Bill Janssen48dc27c2007-12-05 03:38:10 +0000194
Georg Brandl7f01a132009-09-16 15:58:14 +0000195 The parameter ``suppress_ragged_eofs`` specifies how the
Antoine Pitroudab64262010-09-19 13:31:06 +0000196 :meth:`SSLSocket.recv` method should signal unexpected EOF from the other end
Georg Brandl7f01a132009-09-16 15:58:14 +0000197 of the connection. If specified as :const:`True` (the default), it returns a
Antoine Pitroudab64262010-09-19 13:31:06 +0000198 normal EOF (an empty bytes object) in response to unexpected EOF errors
199 raised from the underlying socket; if :const:`False`, it will raise the
200 exceptions back to the caller.
Bill Janssen48dc27c2007-12-05 03:38:10 +0000201
Ezio Melotti4d5195b2010-04-20 10:57:44 +0000202 .. versionchanged:: 3.2
Antoine Pitrou2d9cb9c2010-04-17 17:40:45 +0000203 New optional argument *ciphers*.
204
Antoine Pitrou59fdd672010-10-08 10:37:08 +0000205Random generation
206^^^^^^^^^^^^^^^^^
207
Victor Stinner99c8b162011-05-24 12:05:19 +0200208.. function:: RAND_bytes(num)
209
Victor Stinnera6752062011-05-25 11:27:40 +0200210 Returns *num* cryptographically strong pseudo-random bytes. Raises an
211 :class:`SSLError` if the PRNG has not been seeded with enough data or if the
212 operation is not supported by the current RAND method. :func:`RAND_status`
213 can be used to check the status of the PRNG and :func:`RAND_add` can be used
214 to seed the PRNG.
Victor Stinner99c8b162011-05-24 12:05:19 +0200215
Victor Stinner19fb53c2011-05-24 21:32:40 +0200216 Read the Wikipedia article, `Cryptographically secure pseudorandom number
Victor Stinnera6752062011-05-25 11:27:40 +0200217 generator (CSPRNG)
Victor Stinner19fb53c2011-05-24 21:32:40 +0200218 <http://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator>`_,
219 to get the requirements of a cryptographically generator.
220
Victor Stinner99c8b162011-05-24 12:05:19 +0200221 .. versionadded:: 3.3
222
223.. function:: RAND_pseudo_bytes(num)
224
225 Returns (bytes, is_cryptographic): bytes are *num* pseudo-random bytes,
226 is_cryptographic is True if the bytes generated are cryptographically
Victor Stinnera6752062011-05-25 11:27:40 +0200227 strong. Raises an :class:`SSLError` if the operation is not supported by the
228 current RAND method.
Victor Stinner99c8b162011-05-24 12:05:19 +0200229
Victor Stinner19fb53c2011-05-24 21:32:40 +0200230 Generated pseudo-random byte sequences will be unique if they are of
231 sufficient length, but are not necessarily unpredictable. They can be used
232 for non-cryptographic purposes and for certain purposes in cryptographic
233 protocols, but usually not for key generation etc.
234
Victor Stinner99c8b162011-05-24 12:05:19 +0200235 .. versionadded:: 3.3
236
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000237.. function:: RAND_status()
238
Georg Brandl7f01a132009-09-16 15:58:14 +0000239 Returns True if the SSL pseudo-random number generator has been seeded with
240 'enough' randomness, and False otherwise. You can use :func:`ssl.RAND_egd`
241 and :func:`ssl.RAND_add` to increase the randomness of the pseudo-random
242 number generator.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000243
244.. function:: RAND_egd(path)
245
Victor Stinner99c8b162011-05-24 12:05:19 +0200246 If you are running an entropy-gathering daemon (EGD) somewhere, and *path*
Georg Brandl7f01a132009-09-16 15:58:14 +0000247 is the pathname of a socket connection open to it, this will read 256 bytes
248 of randomness from the socket, and add it to the SSL pseudo-random number
249 generator to increase the security of generated secret keys. This is
250 typically only necessary on systems without better sources of randomness.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000251
Georg Brandl7f01a132009-09-16 15:58:14 +0000252 See http://egd.sourceforge.net/ or http://prngd.sourceforge.net/ for sources
253 of entropy-gathering daemons.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000254
255.. function:: RAND_add(bytes, entropy)
256
Victor Stinner99c8b162011-05-24 12:05:19 +0200257 Mixes the given *bytes* into the SSL pseudo-random number generator. The
258 parameter *entropy* (a float) is a lower bound on the entropy contained in
Georg Brandl7f01a132009-09-16 15:58:14 +0000259 string (so you can always use :const:`0.0`). See :rfc:`1750` for more
260 information on sources of entropy.
Thomas Woutersed03b412007-08-28 21:37:11 +0000261
Antoine Pitrou59fdd672010-10-08 10:37:08 +0000262Certificate handling
263^^^^^^^^^^^^^^^^^^^^
264
265.. function:: match_hostname(cert, hostname)
266
267 Verify that *cert* (in decoded format as returned by
268 :meth:`SSLSocket.getpeercert`) matches the given *hostname*. The rules
269 applied are those for checking the identity of HTTPS servers as outlined
270 in :rfc:`2818`, except that IP addresses are not currently supported.
271 In addition to HTTPS, this function should be suitable for checking the
272 identity of servers in various SSL-based protocols such as FTPS, IMAPS,
273 POPS and others.
274
275 :exc:`CertificateError` is raised on failure. On success, the function
276 returns nothing::
277
278 >>> cert = {'subject': ((('commonName', 'example.com'),),)}
279 >>> ssl.match_hostname(cert, "example.com")
280 >>> ssl.match_hostname(cert, "example.org")
281 Traceback (most recent call last):
282 File "<stdin>", line 1, in <module>
283 File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
284 ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
285
286 .. versionadded:: 3.2
287
Thomas Woutersed03b412007-08-28 21:37:11 +0000288.. function:: cert_time_to_seconds(timestring)
289
Georg Brandl7f01a132009-09-16 15:58:14 +0000290 Returns a floating-point value containing a normal seconds-after-the-epoch
291 time value, given the time-string representing the "notBefore" or "notAfter"
292 date from a certificate.
Thomas Woutersed03b412007-08-28 21:37:11 +0000293
294 Here's an example::
295
296 >>> import ssl
297 >>> ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT")
298 1178694000.0
299 >>> import time
300 >>> time.ctime(ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT"))
301 'Wed May 9 00:00:00 2007'
Thomas Woutersed03b412007-08-28 21:37:11 +0000302
Georg Brandl7f01a132009-09-16 15:58:14 +0000303.. function:: get_server_certificate(addr, ssl_version=PROTOCOL_SSLv3, ca_certs=None)
Thomas Woutersed03b412007-08-28 21:37:11 +0000304
Georg Brandl7f01a132009-09-16 15:58:14 +0000305 Given the address ``addr`` of an SSL-protected server, as a (*hostname*,
306 *port-number*) pair, fetches the server's certificate, and returns it as a
307 PEM-encoded string. If ``ssl_version`` is specified, uses that version of
308 the SSL protocol to attempt to connect to the server. If ``ca_certs`` is
309 specified, it should be a file containing a list of root certificates, the
310 same format as used for the same parameter in :func:`wrap_socket`. The call
311 will attempt to validate the server certificate against that set of root
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000312 certificates, and will fail if the validation attempt fails.
313
Antoine Pitrou15399c32011-04-28 19:23:55 +0200314 .. versionchanged:: 3.3
315 This function is now IPv6-compatible.
316
Georg Brandl7f01a132009-09-16 15:58:14 +0000317.. function:: DER_cert_to_PEM_cert(DER_cert_bytes)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000318
319 Given a certificate as a DER-encoded blob of bytes, returns a PEM-encoded
320 string version of the same certificate.
321
Georg Brandl7f01a132009-09-16 15:58:14 +0000322.. function:: PEM_cert_to_DER_cert(PEM_cert_string)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000323
Georg Brandl7f01a132009-09-16 15:58:14 +0000324 Given a certificate as an ASCII PEM string, returns a DER-encoded sequence of
325 bytes for that same certificate.
Thomas Woutersed03b412007-08-28 21:37:11 +0000326
Antoine Pitrou59fdd672010-10-08 10:37:08 +0000327Constants
328^^^^^^^^^
329
Thomas Woutersed03b412007-08-28 21:37:11 +0000330.. data:: CERT_NONE
331
Antoine Pitrou152efa22010-05-16 18:19:27 +0000332 Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs``
333 parameter to :func:`wrap_socket`. In this mode (the default), no
334 certificates will be required from the other side of the socket connection.
335 If a certificate is received from the other end, no attempt to validate it
336 is made.
337
338 See the discussion of :ref:`ssl-security` below.
Thomas Woutersed03b412007-08-28 21:37:11 +0000339
340.. data:: CERT_OPTIONAL
341
Antoine Pitrou152efa22010-05-16 18:19:27 +0000342 Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs``
343 parameter to :func:`wrap_socket`. In this mode no certificates will be
344 required from the other side of the socket connection; but if they
345 are provided, validation will be attempted and an :class:`SSLError`
346 will be raised on failure.
347
348 Use of this setting requires a valid set of CA certificates to
349 be passed, either to :meth:`SSLContext.load_verify_locations` or as a
350 value of the ``ca_certs`` parameter to :func:`wrap_socket`.
Thomas Woutersed03b412007-08-28 21:37:11 +0000351
352.. data:: CERT_REQUIRED
353
Antoine Pitrou152efa22010-05-16 18:19:27 +0000354 Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs``
355 parameter to :func:`wrap_socket`. In this mode, certificates are
356 required from the other side of the socket connection; an :class:`SSLError`
357 will be raised if no certificate is provided, or if its validation fails.
358
359 Use of this setting requires a valid set of CA certificates to
360 be passed, either to :meth:`SSLContext.load_verify_locations` or as a
361 value of the ``ca_certs`` parameter to :func:`wrap_socket`.
Thomas Woutersed03b412007-08-28 21:37:11 +0000362
363.. data:: PROTOCOL_SSLv2
364
365 Selects SSL version 2 as the channel encryption protocol.
366
Victor Stinner3de49192011-05-09 00:42:58 +0200367 This protocol is not available if OpenSSL is compiled with OPENSSL_NO_SSL2
368 flag.
369
Antoine Pitrou8eac60d2010-05-16 14:19:41 +0000370 .. warning::
371
372 SSL version 2 is insecure. Its use is highly discouraged.
373
Thomas Woutersed03b412007-08-28 21:37:11 +0000374.. data:: PROTOCOL_SSLv23
375
Georg Brandl7f01a132009-09-16 15:58:14 +0000376 Selects SSL version 2 or 3 as the channel encryption protocol. This is a
377 setting to use with servers for maximum compatibility with the other end of
378 an SSL connection, but it may cause the specific ciphers chosen for the
379 encryption to be of fairly low quality.
Thomas Woutersed03b412007-08-28 21:37:11 +0000380
381.. data:: PROTOCOL_SSLv3
382
Georg Brandl7f01a132009-09-16 15:58:14 +0000383 Selects SSL version 3 as the channel encryption protocol. For clients, this
384 is the maximally compatible SSL variant.
Thomas Woutersed03b412007-08-28 21:37:11 +0000385
386.. data:: PROTOCOL_TLSv1
387
Georg Brandl7f01a132009-09-16 15:58:14 +0000388 Selects TLS version 1 as the channel encryption protocol. This is the most
389 modern version, and probably the best choice for maximum protection, if both
390 sides can speak it.
Thomas Woutersed03b412007-08-28 21:37:11 +0000391
Antoine Pitroub5218772010-05-21 09:56:06 +0000392.. data:: OP_ALL
393
394 Enables workarounds for various bugs present in other SSL implementations.
Antoine Pitrou9f6b02e2012-01-27 10:02:55 +0100395 This option is set by default. It does not necessarily set the same
396 flags as OpenSSL's ``SSL_OP_ALL`` constant.
Antoine Pitroub5218772010-05-21 09:56:06 +0000397
398 .. versionadded:: 3.2
399
400.. data:: OP_NO_SSLv2
401
402 Prevents an SSLv2 connection. This option is only applicable in
403 conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from
404 choosing SSLv2 as the protocol version.
405
406 .. versionadded:: 3.2
407
408.. data:: OP_NO_SSLv3
409
410 Prevents an SSLv3 connection. This option is only applicable in
411 conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from
412 choosing SSLv3 as the protocol version.
413
414 .. versionadded:: 3.2
415
416.. data:: OP_NO_TLSv1
417
418 Prevents a TLSv1 connection. This option is only applicable in
419 conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from
420 choosing TLSv1 as the protocol version.
421
422 .. versionadded:: 3.2
423
Antoine Pitrou6db49442011-12-19 13:27:11 +0100424.. data:: OP_CIPHER_SERVER_PREFERENCE
425
426 Use the server's cipher ordering preference, rather than the client's.
427 This option has no effect on client sockets and SSLv2 server sockets.
428
429 .. versionadded:: 3.3
430
Antoine Pitrou0e576f12011-12-22 10:03:38 +0100431.. data:: OP_SINGLE_DH_USE
432
433 Prevents re-use of the same DH key for distinct SSL sessions. This
434 improves forward secrecy but requires more computational resources.
435 This option only applies to server sockets.
436
437 .. versionadded:: 3.3
438
Antoine Pitrou923df6f2011-12-19 17:16:51 +0100439.. data:: OP_SINGLE_ECDH_USE
440
Antoine Pitrou0e576f12011-12-22 10:03:38 +0100441 Prevents re-use of the same ECDH key for distinct SSL sessions. This
Antoine Pitrou923df6f2011-12-19 17:16:51 +0100442 improves forward secrecy but requires more computational resources.
443 This option only applies to server sockets.
444
445 .. versionadded:: 3.3
446
Antoine Pitrou8abdb8a2011-12-20 10:13:40 +0100447.. data:: OP_NO_COMPRESSION
448
449 Disable compression on the SSL channel. This is useful if the application
450 protocol supports its own compression scheme.
451
452 This option is only available with OpenSSL 1.0.0 and later.
453
454 .. versionadded:: 3.3
455
Antoine Pitrou501da612011-12-21 09:27:41 +0100456.. data:: HAS_ECDH
457
458 Whether the OpenSSL library has built-in support for Elliptic Curve-based
459 Diffie-Hellman key exchange. This should be true unless the feature was
460 explicitly disabled by the distributor.
461
462 .. versionadded:: 3.3
463
Antoine Pitroud5323212010-10-22 18:19:07 +0000464.. data:: HAS_SNI
465
466 Whether the OpenSSL library has built-in support for the *Server Name
467 Indication* extension to the SSLv3 and TLSv1 protocols (as defined in
468 :rfc:`4366`). When true, you can use the *server_hostname* argument to
469 :meth:`SSLContext.wrap_socket`.
470
471 .. versionadded:: 3.2
472
Antoine Pitroud5d17eb2012-03-22 00:23:03 +0100473.. data:: HAS_NPN
474
475 Whether the OpenSSL library has built-in support for *Next Protocol
476 Negotiation* as described in the `NPN draft specification
477 <http://tools.ietf.org/html/draft-agl-tls-nextprotoneg>`_. When true,
478 you can use the :meth:`SSLContext.set_npn_protocols` method to advertise
479 which protocols you want to support.
480
481 .. versionadded:: 3.3
482
Antoine Pitroud6494802011-07-21 01:11:30 +0200483.. data:: CHANNEL_BINDING_TYPES
484
485 List of supported TLS channel binding types. Strings in this list
486 can be used as arguments to :meth:`SSLSocket.get_channel_binding`.
487
488 .. versionadded:: 3.3
489
Antoine Pitrou04f6a322010-04-05 21:40:07 +0000490.. data:: OPENSSL_VERSION
491
492 The version string of the OpenSSL library loaded by the interpreter::
493
494 >>> ssl.OPENSSL_VERSION
495 'OpenSSL 0.9.8k 25 Mar 2009'
496
Antoine Pitrou43a94c312010-04-05 21:44:48 +0000497 .. versionadded:: 3.2
Antoine Pitrou04f6a322010-04-05 21:40:07 +0000498
499.. data:: OPENSSL_VERSION_INFO
500
501 A tuple of five integers representing version information about the
502 OpenSSL library::
503
504 >>> ssl.OPENSSL_VERSION_INFO
505 (0, 9, 8, 11, 15)
506
Antoine Pitrou43a94c312010-04-05 21:44:48 +0000507 .. versionadded:: 3.2
Antoine Pitrou04f6a322010-04-05 21:40:07 +0000508
509.. data:: OPENSSL_VERSION_NUMBER
510
511 The raw version number of the OpenSSL library, as a single integer::
512
513 >>> ssl.OPENSSL_VERSION_NUMBER
Antoine Pitrou43a94c312010-04-05 21:44:48 +0000514 9470143
Antoine Pitrou04f6a322010-04-05 21:40:07 +0000515 >>> hex(ssl.OPENSSL_VERSION_NUMBER)
Antoine Pitrou43a94c312010-04-05 21:44:48 +0000516 '0x9080bf'
Antoine Pitrou04f6a322010-04-05 21:40:07 +0000517
Antoine Pitrou43a94c312010-04-05 21:44:48 +0000518 .. versionadded:: 3.2
Antoine Pitrou04f6a322010-04-05 21:40:07 +0000519
Thomas Woutersed03b412007-08-28 21:37:11 +0000520
Antoine Pitrou152efa22010-05-16 18:19:27 +0000521SSL Sockets
522-----------
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000523
Antoine Pitroue1f2f302010-09-19 13:56:11 +0000524SSL sockets provide the following methods of :ref:`socket-objects`:
Antoine Pitrou792ff3e2010-09-19 13:19:21 +0000525
Antoine Pitroue1f2f302010-09-19 13:56:11 +0000526- :meth:`~socket.socket.accept()`
527- :meth:`~socket.socket.bind()`
528- :meth:`~socket.socket.close()`
529- :meth:`~socket.socket.connect()`
530- :meth:`~socket.socket.detach()`
531- :meth:`~socket.socket.fileno()`
532- :meth:`~socket.socket.getpeername()`, :meth:`~socket.socket.getsockname()`
533- :meth:`~socket.socket.getsockopt()`, :meth:`~socket.socket.setsockopt()`
534- :meth:`~socket.socket.gettimeout()`, :meth:`~socket.socket.settimeout()`,
535 :meth:`~socket.socket.setblocking()`
536- :meth:`~socket.socket.listen()`
537- :meth:`~socket.socket.makefile()`
538- :meth:`~socket.socket.recv()`, :meth:`~socket.socket.recv_into()`
539 (but passing a non-zero ``flags`` argument is not allowed)
540- :meth:`~socket.socket.send()`, :meth:`~socket.socket.sendall()` (with
541 the same limitation)
542- :meth:`~socket.socket.shutdown()`
543
Antoine Pitrou6f5dcb12011-07-11 01:35:48 +0200544However, since the SSL (and TLS) protocol has its own framing atop
545of TCP, the SSL sockets abstraction can, in certain respects, diverge from
546the specification of normal, OS-level sockets. See especially the
547:ref:`notes on non-blocking sockets <ssl-nonblocking>`.
548
549SSL sockets also have the following additional methods and attributes:
Antoine Pitrou792ff3e2010-09-19 13:19:21 +0000550
Bill Janssen48dc27c2007-12-05 03:38:10 +0000551.. method:: SSLSocket.do_handshake()
552
Antoine Pitroub3593ca2011-07-11 01:39:19 +0200553 Perform the SSL setup handshake.
Bill Janssen48dc27c2007-12-05 03:38:10 +0000554
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000555.. method:: SSLSocket.getpeercert(binary_form=False)
556
Georg Brandl7f01a132009-09-16 15:58:14 +0000557 If there is no certificate for the peer on the other end of the connection,
558 returns ``None``.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000559
Georg Brandl7f01a132009-09-16 15:58:14 +0000560 If the parameter ``binary_form`` is :const:`False`, and a certificate was
561 received from the peer, this method returns a :class:`dict` instance. If the
562 certificate was not validated, the dict is empty. If the certificate was
563 validated, it returns a dict with the keys ``subject`` (the principal for
564 which the certificate was issued), and ``notAfter`` (the time after which the
Antoine Pitroufb046912010-11-09 20:21:19 +0000565 certificate should not be trusted). If a certificate contains an instance
566 of the *Subject Alternative Name* extension (see :rfc:`3280`), there will
567 also be a ``subjectAltName`` key in the dictionary.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000568
569 The "subject" field is a tuple containing the sequence of relative
Georg Brandl7f01a132009-09-16 15:58:14 +0000570 distinguished names (RDNs) given in the certificate's data structure for the
571 principal, and each RDN is a sequence of name-value pairs::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000572
573 {'notAfter': 'Feb 16 16:54:50 2013 GMT',
Ezio Melotti985e24d2009-09-13 07:54:02 +0000574 'subject': ((('countryName', 'US'),),
575 (('stateOrProvinceName', 'Delaware'),),
576 (('localityName', 'Wilmington'),),
577 (('organizationName', 'Python Software Foundation'),),
578 (('organizationalUnitName', 'SSL'),),
579 (('commonName', 'somemachine.python.org'),))}
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000580
Georg Brandl7f01a132009-09-16 15:58:14 +0000581 If the ``binary_form`` parameter is :const:`True`, and a certificate was
582 provided, this method returns the DER-encoded form of the entire certificate
583 as a sequence of bytes, or :const:`None` if the peer did not provide a
584 certificate. This return value is independent of validation; if validation
585 was required (:const:`CERT_OPTIONAL` or :const:`CERT_REQUIRED`), it will have
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000586 been validated, but if :const:`CERT_NONE` was used to establish the
587 connection, the certificate, if present, will not have been validated.
588
Antoine Pitroufb046912010-11-09 20:21:19 +0000589 .. versionchanged:: 3.2
590 The returned dictionary includes additional items such as ``issuer``
591 and ``notBefore``.
592
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000593.. method:: SSLSocket.cipher()
594
Georg Brandl7f01a132009-09-16 15:58:14 +0000595 Returns a three-value tuple containing the name of the cipher being used, the
596 version of the SSL protocol that defines its use, and the number of secret
597 bits being used. If no connection has been established, returns ``None``.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000598
Antoine Pitrou8abdb8a2011-12-20 10:13:40 +0100599.. method:: SSLSocket.compression()
600
601 Return the compression algorithm being used as a string, or ``None``
602 if the connection isn't compressed.
603
604 If the higher-level protocol supports its own compression mechanism,
605 you can use :data:`OP_NO_COMPRESSION` to disable SSL-level compression.
606
607 .. versionadded:: 3.3
608
Antoine Pitroud6494802011-07-21 01:11:30 +0200609.. method:: SSLSocket.get_channel_binding(cb_type="tls-unique")
610
611 Get channel binding data for current connection, as a bytes object. Returns
612 ``None`` if not connected or the handshake has not been completed.
613
614 The *cb_type* parameter allow selection of the desired channel binding
615 type. Valid channel binding types are listed in the
616 :data:`CHANNEL_BINDING_TYPES` list. Currently only the 'tls-unique' channel
617 binding, defined by :rfc:`5929`, is supported. :exc:`ValueError` will be
618 raised if an unsupported channel binding type is requested.
619
620 .. versionadded:: 3.3
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000621
Antoine Pitroud5d17eb2012-03-22 00:23:03 +0100622.. method:: SSLSocket.selected_npn_protocol()
623
624 Returns the protocol that was selected during the TLS/SSL handshake. If
625 :meth:`SSLContext.set_npn_protocols` was not called, or if the other party
626 does not support NPN, or if the handshake has not yet happened, this will
627 return ``None``.
628
629 .. versionadded:: 3.3
630
Benjamin Peterson4aeec042008-08-19 21:42:13 +0000631.. method:: SSLSocket.unwrap()
632
Georg Brandl7f01a132009-09-16 15:58:14 +0000633 Performs the SSL shutdown handshake, which removes the TLS layer from the
634 underlying socket, and returns the underlying socket object. This can be
635 used to go from encrypted operation over a connection to unencrypted. The
636 returned socket should always be used for further communication with the
637 other side of the connection, rather than the original socket.
Benjamin Peterson4aeec042008-08-19 21:42:13 +0000638
Antoine Pitrouec883db2010-05-24 21:20:20 +0000639.. attribute:: SSLSocket.context
640
641 The :class:`SSLContext` object this SSL socket is tied to. If the SSL
642 socket was created using the top-level :func:`wrap_socket` function
643 (rather than :meth:`SSLContext.wrap_socket`), this is a custom context
644 object created for this SSL socket.
645
646 .. versionadded:: 3.2
647
648
Antoine Pitrou152efa22010-05-16 18:19:27 +0000649SSL Contexts
650------------
651
Antoine Pitroucafaad42010-05-24 15:58:43 +0000652.. versionadded:: 3.2
653
Antoine Pitroub0182c82010-10-12 20:09:02 +0000654An SSL context holds various data longer-lived than single SSL connections,
655such as SSL configuration options, certificate(s) and private key(s).
656It also manages a cache of SSL sessions for server-side sockets, in order
657to speed up repeated connections from the same clients.
658
Antoine Pitrou152efa22010-05-16 18:19:27 +0000659.. class:: SSLContext(protocol)
660
Antoine Pitroub0182c82010-10-12 20:09:02 +0000661 Create a new SSL context. You must pass *protocol* which must be one
662 of the ``PROTOCOL_*`` constants defined in this module.
663 :data:`PROTOCOL_SSLv23` is recommended for maximum interoperability.
664
Antoine Pitrou152efa22010-05-16 18:19:27 +0000665
666:class:`SSLContext` objects have the following methods and attributes:
667
Antoine Pitrou4fd1e6a2011-08-25 14:39:44 +0200668.. method:: SSLContext.load_cert_chain(certfile, keyfile=None, password=None)
Antoine Pitrou152efa22010-05-16 18:19:27 +0000669
670 Load a private key and the corresponding certificate. The *certfile*
671 string must be the path to a single file in PEM format containing the
672 certificate as well as any number of CA certificates needed to establish
673 the certificate's authenticity. The *keyfile* string, if present, must
674 point to a file containing the private key in. Otherwise the private
675 key will be taken from *certfile* as well. See the discussion of
676 :ref:`ssl-certificates` for more information on how the certificate
677 is stored in the *certfile*.
678
Antoine Pitrou4fd1e6a2011-08-25 14:39:44 +0200679 The *password* argument may be a function to call to get the password for
680 decrypting the private key. It will only be called if the private key is
681 encrypted and a password is necessary. It will be called with no arguments,
682 and it should return a string, bytes, or bytearray. If the return value is
683 a string it will be encoded as UTF-8 before using it to decrypt the key.
684 Alternatively a string, bytes, or bytearray value may be supplied directly
685 as the *password* argument. It will be ignored if the private key is not
686 encrypted and no password is needed.
687
688 If the *password* argument is not specified and a password is required,
689 OpenSSL's built-in password prompting mechanism will be used to
690 interactively prompt the user for a password.
691
Antoine Pitrou152efa22010-05-16 18:19:27 +0000692 An :class:`SSLError` is raised if the private key doesn't
693 match with the certificate.
694
Antoine Pitrou4fd1e6a2011-08-25 14:39:44 +0200695 .. versionchanged:: 3.3
696 New optional argument *password*.
697
Antoine Pitrou152efa22010-05-16 18:19:27 +0000698.. method:: SSLContext.load_verify_locations(cafile=None, capath=None)
699
700 Load a set of "certification authority" (CA) certificates used to validate
701 other peers' certificates when :data:`verify_mode` is other than
702 :data:`CERT_NONE`. At least one of *cafile* or *capath* must be specified.
703
704 The *cafile* string, if present, is the path to a file of concatenated
705 CA certificates in PEM format. See the discussion of
706 :ref:`ssl-certificates` for more information about how to arrange the
707 certificates in this file.
708
709 The *capath* string, if present, is
710 the path to a directory containing several CA certificates in PEM format,
711 following an `OpenSSL specific layout
712 <http://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html>`_.
713
Antoine Pitrou664c2d12010-11-17 20:29:42 +0000714.. method:: SSLContext.set_default_verify_paths()
715
716 Load a set of default "certification authority" (CA) certificates from
717 a filesystem path defined when building the OpenSSL library. Unfortunately,
718 there's no easy way to know whether this method succeeds: no error is
719 returned if no certificates are to be found. When the OpenSSL library is
720 provided as part of the operating system, though, it is likely to be
721 configured properly.
722
Antoine Pitrou152efa22010-05-16 18:19:27 +0000723.. method:: SSLContext.set_ciphers(ciphers)
724
725 Set the available ciphers for sockets created with this context.
726 It should be a string in the `OpenSSL cipher list format
727 <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_.
728 If no cipher can be selected (because compile-time options or other
729 configuration forbids use of all the specified ciphers), an
730 :class:`SSLError` will be raised.
731
732 .. note::
733 when connected, the :meth:`SSLSocket.cipher` method of SSL sockets will
734 give the currently selected cipher.
735
Antoine Pitroud5d17eb2012-03-22 00:23:03 +0100736.. method:: SSLContext.set_npn_protocols(protocols)
737
738 Specify which protocols the socket should avertise during the SSL/TLS
739 handshake. It should be a list of strings, like ``['http/1.1', 'spdy/2']``,
740 ordered by preference. The selection of a protocol will happen during the
741 handshake, and will play out according to the `NPN draft specification
742 <http://tools.ietf.org/html/draft-agl-tls-nextprotoneg>`_. After a
743 successful handshake, the :meth:`SSLSocket.selected_npn_protocol` method will
744 return the agreed-upon protocol.
745
746 This method will raise :exc:`NotImplementedError` if :data:`HAS_NPN` is
747 False.
748
749 .. versionadded:: 3.3
750
Antoine Pitrou0e576f12011-12-22 10:03:38 +0100751.. method:: SSLContext.load_dh_params(dhfile)
752
753 Load the key generation parameters for Diffie-Helman (DH) key exchange.
754 Using DH key exchange improves forward secrecy at the expense of
755 computational resources (both on the server and on the client).
756 The *dhfile* parameter should be the path to a file containing DH
757 parameters in PEM format.
758
759 This setting doesn't apply to client sockets. You can also use the
760 :data:`OP_SINGLE_DH_USE` option to further improve security.
761
762 .. versionadded:: 3.3
763
Antoine Pitrou923df6f2011-12-19 17:16:51 +0100764.. method:: SSLContext.set_ecdh_curve(curve_name)
765
Antoine Pitrou0e576f12011-12-22 10:03:38 +0100766 Set the curve name for Elliptic Curve-based Diffie-Hellman (ECDH) key
767 exchange. ECDH is significantly faster than regular DH while arguably
768 as secure. The *curve_name* parameter should be a string describing
Antoine Pitrou923df6f2011-12-19 17:16:51 +0100769 a well-known elliptic curve, for example ``prime256v1`` for a widely
770 supported curve.
771
772 This setting doesn't apply to client sockets. You can also use the
773 :data:`OP_SINGLE_ECDH_USE` option to further improve security.
774
Antoine Pitrou501da612011-12-21 09:27:41 +0100775 This method is not available if :data:`HAS_ECDH` is False.
776
Antoine Pitrou923df6f2011-12-19 17:16:51 +0100777 .. versionadded:: 3.3
778
779 .. seealso::
780 `SSL/TLS & Perfect Forward Secrecy <http://vincent.bernat.im/en/blog/2011-ssl-perfect-forward-secrecy.html>`_
781 Vincent Bernat.
782
Antoine Pitroud5323212010-10-22 18:19:07 +0000783.. method:: SSLContext.wrap_socket(sock, server_side=False, \
784 do_handshake_on_connect=True, suppress_ragged_eofs=True, \
785 server_hostname=None)
Antoine Pitrou152efa22010-05-16 18:19:27 +0000786
787 Wrap an existing Python socket *sock* and return an :class:`SSLSocket`
788 object. The SSL socket is tied to the context, its settings and
789 certificates. The parameters *server_side*, *do_handshake_on_connect*
790 and *suppress_ragged_eofs* have the same meaning as in the top-level
791 :func:`wrap_socket` function.
792
Antoine Pitroud5323212010-10-22 18:19:07 +0000793 On client connections, the optional parameter *server_hostname* specifies
794 the hostname of the service which we are connecting to. This allows a
795 single server to host multiple SSL-based services with distinct certificates,
796 quite similarly to HTTP virtual hosts. Specifying *server_hostname*
797 will raise a :exc:`ValueError` if the OpenSSL library doesn't have support
798 for it (that is, if :data:`HAS_SNI` is :const:`False`). Specifying
799 *server_hostname* will also raise a :exc:`ValueError` if *server_side*
800 is true.
801
Antoine Pitroub0182c82010-10-12 20:09:02 +0000802.. method:: SSLContext.session_stats()
803
804 Get statistics about the SSL sessions created or managed by this context.
805 A dictionary is returned which maps the names of each `piece of information
806 <http://www.openssl.org/docs/ssl/SSL_CTX_sess_number.html>`_ to their
807 numeric values. For example, here is the total number of hits and misses
808 in the session cache since the context was created::
809
810 >>> stats = context.session_stats()
811 >>> stats['hits'], stats['misses']
812 (0, 0)
813
Antoine Pitroub5218772010-05-21 09:56:06 +0000814.. attribute:: SSLContext.options
815
816 An integer representing the set of SSL options enabled on this context.
817 The default value is :data:`OP_ALL`, but you can specify other options
818 such as :data:`OP_NO_SSLv2` by ORing them together.
819
820 .. note::
821 With versions of OpenSSL older than 0.9.8m, it is only possible
822 to set options, not to clear them. Attempting to clear an option
823 (by resetting the corresponding bits) will raise a ``ValueError``.
824
Antoine Pitrou152efa22010-05-16 18:19:27 +0000825.. attribute:: SSLContext.protocol
826
827 The protocol version chosen when constructing the context. This attribute
828 is read-only.
829
830.. attribute:: SSLContext.verify_mode
831
832 Whether to try to verify other peers' certificates and how to behave
833 if verification fails. This attribute must be one of
834 :data:`CERT_NONE`, :data:`CERT_OPTIONAL` or :data:`CERT_REQUIRED`.
835
836
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000837.. index:: single: certificates
838
839.. index:: single: X509 certificate
840
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000841.. _ssl-certificates:
842
Thomas Woutersed03b412007-08-28 21:37:11 +0000843Certificates
844------------
845
Georg Brandl7f01a132009-09-16 15:58:14 +0000846Certificates in general are part of a public-key / private-key system. In this
847system, each *principal*, (which may be a machine, or a person, or an
848organization) is assigned a unique two-part encryption key. One part of the key
849is public, and is called the *public key*; the other part is kept secret, and is
850called the *private key*. The two parts are related, in that if you encrypt a
851message with one of the parts, you can decrypt it with the other part, and
852**only** with the other part.
Thomas Woutersed03b412007-08-28 21:37:11 +0000853
Georg Brandl7f01a132009-09-16 15:58:14 +0000854A certificate contains information about two principals. It contains the name
855of a *subject*, and the subject's public key. It also contains a statement by a
856second principal, the *issuer*, that the subject is who he claims to be, and
857that this is indeed the subject's public key. The issuer's statement is signed
858with the issuer's private key, which only the issuer knows. However, anyone can
859verify the issuer's statement by finding the issuer's public key, decrypting the
860statement with it, and comparing it to the other information in the certificate.
861The certificate also contains information about the time period over which it is
862valid. This is expressed as two fields, called "notBefore" and "notAfter".
Thomas Woutersed03b412007-08-28 21:37:11 +0000863
Georg Brandl7f01a132009-09-16 15:58:14 +0000864In the Python use of certificates, a client or server can use a certificate to
865prove who they are. The other side of a network connection can also be required
866to produce a certificate, and that certificate can be validated to the
867satisfaction of the client or server that requires such validation. The
868connection attempt can be set to raise an exception if the validation fails.
869Validation is done automatically, by the underlying OpenSSL framework; the
870application need not concern itself with its mechanics. But the application
871does usually need to provide sets of certificates to allow this process to take
872place.
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000873
Georg Brandl7f01a132009-09-16 15:58:14 +0000874Python uses files to contain certificates. They should be formatted as "PEM"
875(see :rfc:`1422`), which is a base-64 encoded form wrapped with a header line
876and a footer line::
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000877
878 -----BEGIN CERTIFICATE-----
879 ... (certificate in base64 PEM encoding) ...
880 -----END CERTIFICATE-----
881
Antoine Pitrou152efa22010-05-16 18:19:27 +0000882Certificate chains
883^^^^^^^^^^^^^^^^^^
884
Georg Brandl7f01a132009-09-16 15:58:14 +0000885The Python files which contain certificates can contain a sequence of
886certificates, sometimes called a *certificate chain*. This chain should start
887with the specific certificate for the principal who "is" the client or server,
888and then the certificate for the issuer of that certificate, and then the
889certificate for the issuer of *that* certificate, and so on up the chain till
890you get to a certificate which is *self-signed*, that is, a certificate which
891has the same subject and issuer, sometimes called a *root certificate*. The
892certificates should just be concatenated together in the certificate file. For
893example, suppose we had a three certificate chain, from our server certificate
894to the certificate of the certification authority that signed our server
895certificate, to the root certificate of the agency which issued the
896certification authority's certificate::
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000897
898 -----BEGIN CERTIFICATE-----
899 ... (certificate for your server)...
900 -----END CERTIFICATE-----
901 -----BEGIN CERTIFICATE-----
902 ... (the certificate for the CA)...
903 -----END CERTIFICATE-----
904 -----BEGIN CERTIFICATE-----
905 ... (the root certificate for the CA's issuer)...
906 -----END CERTIFICATE-----
907
Antoine Pitrou152efa22010-05-16 18:19:27 +0000908CA certificates
909^^^^^^^^^^^^^^^
910
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000911If you are going to require validation of the other side of the connection's
912certificate, you need to provide a "CA certs" file, filled with the certificate
Georg Brandl7f01a132009-09-16 15:58:14 +0000913chains for each issuer you are willing to trust. Again, this file just contains
914these chains concatenated together. For validation, Python will use the first
915chain it finds in the file which matches. Some "standard" root certificates are
916available from various certification authorities: `CACert.org
917<http://www.cacert.org/index.php?id=3>`_, `Thawte
918<http://www.thawte.com/roots/>`_, `Verisign
919<http://www.verisign.com/support/roots.html>`_, `Positive SSL
920<http://www.PositiveSSL.com/ssl-certificate-support/cert_installation/UTN-USERFirst-Hardware.crt>`_
921(used by python.org), `Equifax and GeoTrust
922<http://www.geotrust.com/resources/root_certificates/index.asp>`_.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000923
Georg Brandl7f01a132009-09-16 15:58:14 +0000924In general, if you are using SSL3 or TLS1, you don't need to put the full chain
925in your "CA certs" file; you only need the root certificates, and the remote
926peer is supposed to furnish the other certificates necessary to chain from its
927certificate to a root certificate. See :rfc:`4158` for more discussion of the
928way in which certification chains can be built.
Thomas Woutersed03b412007-08-28 21:37:11 +0000929
Antoine Pitrou152efa22010-05-16 18:19:27 +0000930Combined key and certificate
931^^^^^^^^^^^^^^^^^^^^^^^^^^^^
932
933Often the private key is stored in the same file as the certificate; in this
934case, only the ``certfile`` parameter to :meth:`SSLContext.load_cert_chain`
935and :func:`wrap_socket` needs to be passed. If the private key is stored
936with the certificate, it should come before the first certificate in
937the certificate chain::
938
939 -----BEGIN RSA PRIVATE KEY-----
940 ... (private key in base64 encoding) ...
941 -----END RSA PRIVATE KEY-----
942 -----BEGIN CERTIFICATE-----
943 ... (certificate in base64 PEM encoding) ...
944 -----END CERTIFICATE-----
945
946Self-signed certificates
947^^^^^^^^^^^^^^^^^^^^^^^^
948
Georg Brandl7f01a132009-09-16 15:58:14 +0000949If you are going to create a server that provides SSL-encrypted connection
950services, you will need to acquire a certificate for that service. There are
951many ways of acquiring appropriate certificates, such as buying one from a
952certification authority. Another common practice is to generate a self-signed
953certificate. The simplest way to do this is with the OpenSSL package, using
954something like the following::
Thomas Woutersed03b412007-08-28 21:37:11 +0000955
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000956 % openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
957 Generating a 1024 bit RSA private key
958 .......++++++
959 .............................++++++
960 writing new private key to 'cert.pem'
961 -----
962 You are about to be asked to enter information that will be incorporated
963 into your certificate request.
964 What you are about to enter is what is called a Distinguished Name or a DN.
965 There are quite a few fields but you can leave some blank
966 For some fields there will be a default value,
967 If you enter '.', the field will be left blank.
968 -----
969 Country Name (2 letter code) [AU]:US
970 State or Province Name (full name) [Some-State]:MyState
971 Locality Name (eg, city) []:Some City
972 Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Organization, Inc.
973 Organizational Unit Name (eg, section) []:My Group
974 Common Name (eg, YOUR name) []:myserver.mygroup.myorganization.com
975 Email Address []:ops@myserver.mygroup.myorganization.com
976 %
Thomas Woutersed03b412007-08-28 21:37:11 +0000977
Georg Brandl7f01a132009-09-16 15:58:14 +0000978The disadvantage of a self-signed certificate is that it is its own root
979certificate, and no one else will have it in their cache of known (and trusted)
980root certificates.
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000981
982
Thomas Woutersed03b412007-08-28 21:37:11 +0000983Examples
984--------
985
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000986Testing for SSL support
987^^^^^^^^^^^^^^^^^^^^^^^
988
Georg Brandl7f01a132009-09-16 15:58:14 +0000989To test for the presence of SSL support in a Python installation, user code
990should use the following idiom::
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000991
992 try:
Georg Brandl8a7e5da2011-01-02 19:07:51 +0000993 import ssl
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000994 except ImportError:
Georg Brandl8a7e5da2011-01-02 19:07:51 +0000995 pass
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000996 else:
Georg Brandl8a7e5da2011-01-02 19:07:51 +0000997 ... # do something that requires SSL support
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000998
999Client-side operation
1000^^^^^^^^^^^^^^^^^^^^^
1001
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001002This example connects to an SSL server and prints the server's certificate::
Thomas Woutersed03b412007-08-28 21:37:11 +00001003
1004 import socket, ssl, pprint
1005
1006 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001007 # require a certificate from the server
1008 ssl_sock = ssl.wrap_socket(s,
1009 ca_certs="/etc/ca_certs_file",
1010 cert_reqs=ssl.CERT_REQUIRED)
Thomas Woutersed03b412007-08-28 21:37:11 +00001011 ssl_sock.connect(('www.verisign.com', 443))
1012
Georg Brandl6911e3c2007-09-04 07:15:32 +00001013 pprint.pprint(ssl_sock.getpeercert())
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001014 # note that closing the SSLSocket will also close the underlying socket
Thomas Woutersed03b412007-08-28 21:37:11 +00001015 ssl_sock.close()
1016
Antoine Pitrou441ae042012-01-06 20:06:15 +01001017As of January 6, 2012, the certificate printed by this program looks like
Georg Brandl7f01a132009-09-16 15:58:14 +00001018this::
Thomas Woutersed03b412007-08-28 21:37:11 +00001019
Antoine Pitrou441ae042012-01-06 20:06:15 +01001020 {'issuer': ((('countryName', 'US'),),
1021 (('organizationName', 'VeriSign, Inc.'),),
1022 (('organizationalUnitName', 'VeriSign Trust Network'),),
1023 (('organizationalUnitName',
1024 'Terms of use at https://www.verisign.com/rpa (c)06'),),
1025 (('commonName',
1026 'VeriSign Class 3 Extended Validation SSL SGC CA'),)),
1027 'notAfter': 'May 25 23:59:59 2012 GMT',
1028 'notBefore': 'May 26 00:00:00 2010 GMT',
1029 'serialNumber': '53D2BEF924A7245E83CA01E46CAA2477',
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001030 'subject': ((('1.3.6.1.4.1.311.60.2.1.3', 'US'),),
1031 (('1.3.6.1.4.1.311.60.2.1.2', 'Delaware'),),
1032 (('businessCategory', 'V1.0, Clause 5.(b)'),),
1033 (('serialNumber', '2497886'),),
1034 (('countryName', 'US'),),
1035 (('postalCode', '94043'),),
1036 (('stateOrProvinceName', 'California'),),
1037 (('localityName', 'Mountain View'),),
1038 (('streetAddress', '487 East Middlefield Road'),),
1039 (('organizationName', 'VeriSign, Inc.'),),
1040 (('organizationalUnitName', ' Production Security Services'),),
Antoine Pitrou441ae042012-01-06 20:06:15 +01001041 (('commonName', 'www.verisign.com'),)),
1042 'subjectAltName': (('DNS', 'www.verisign.com'),
1043 ('DNS', 'verisign.com'),
1044 ('DNS', 'www.verisign.net'),
1045 ('DNS', 'verisign.net'),
1046 ('DNS', 'www.verisign.mobi'),
1047 ('DNS', 'verisign.mobi'),
1048 ('DNS', 'www.verisign.eu'),
1049 ('DNS', 'verisign.eu')),
1050 'version': 3}
Thomas Woutersed03b412007-08-28 21:37:11 +00001051
Antoine Pitrou152efa22010-05-16 18:19:27 +00001052This other example first creates an SSL context, instructs it to verify
1053certificates sent by peers, and feeds it a set of recognized certificate
1054authorities (CA)::
1055
1056 >>> context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001057 >>> context.verify_mode = ssl.CERT_REQUIRED
Antoine Pitrou152efa22010-05-16 18:19:27 +00001058 >>> context.load_verify_locations("/etc/ssl/certs/ca-bundle.crt")
1059
1060(it is assumed your operating system places a bundle of all CA certificates
1061in ``/etc/ssl/certs/ca-bundle.crt``; if not, you'll get an error and have
1062to adjust the location)
1063
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001064When you use the context to connect to a server, :const:`CERT_REQUIRED`
Antoine Pitrou152efa22010-05-16 18:19:27 +00001065validates the server certificate: it ensures that the server certificate
1066was signed with one of the CA certificates, and checks the signature for
1067correctness::
1068
1069 >>> conn = context.wrap_socket(socket.socket(socket.AF_INET))
1070 >>> conn.connect(("linuxfr.org", 443))
1071
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001072You should then fetch the certificate and check its fields for conformity::
Antoine Pitrou152efa22010-05-16 18:19:27 +00001073
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001074 >>> cert = conn.getpeercert()
1075 >>> ssl.match_hostname(cert, "linuxfr.org")
1076
1077Visual inspection shows that the certificate does identify the desired service
1078(that is, the HTTPS host ``linuxfr.org``)::
1079
1080 >>> pprint.pprint(cert)
Antoine Pitrou441ae042012-01-06 20:06:15 +01001081 {'issuer': ((('organizationName', 'CAcert Inc.'),),
1082 (('organizationalUnitName', 'http://www.CAcert.org'),),
1083 (('commonName', 'CAcert Class 3 Root'),)),
1084 'notAfter': 'Jun 7 21:02:24 2013 GMT',
1085 'notBefore': 'Jun 8 21:02:24 2011 GMT',
1086 'serialNumber': 'D3E9',
Antoine Pitrou152efa22010-05-16 18:19:27 +00001087 'subject': ((('commonName', 'linuxfr.org'),),),
Antoine Pitrou441ae042012-01-06 20:06:15 +01001088 'subjectAltName': (('DNS', 'linuxfr.org'),
1089 ('othername', '<unsupported>'),
1090 ('DNS', 'linuxfr.org'),
1091 ('othername', '<unsupported>'),
1092 ('DNS', 'dev.linuxfr.org'),
1093 ('othername', '<unsupported>'),
1094 ('DNS', 'prod.linuxfr.org'),
1095 ('othername', '<unsupported>'),
1096 ('DNS', 'alpha.linuxfr.org'),
1097 ('othername', '<unsupported>'),
1098 ('DNS', '*.linuxfr.org'),
1099 ('othername', '<unsupported>')),
1100 'version': 3}
Antoine Pitrou152efa22010-05-16 18:19:27 +00001101
1102Now that you are assured of its authenticity, you can proceed to talk with
1103the server::
1104
Antoine Pitroudab64262010-09-19 13:31:06 +00001105 >>> conn.sendall(b"HEAD / HTTP/1.0\r\nHost: linuxfr.org\r\n\r\n")
1106 >>> pprint.pprint(conn.recv(1024).split(b"\r\n"))
Antoine Pitrou152efa22010-05-16 18:19:27 +00001107 [b'HTTP/1.1 302 Found',
1108 b'Date: Sun, 16 May 2010 13:43:28 GMT',
1109 b'Server: Apache/2.2',
1110 b'Location: https://linuxfr.org/pub/',
1111 b'Vary: Accept-Encoding',
1112 b'Connection: close',
1113 b'Content-Type: text/html; charset=iso-8859-1',
1114 b'',
1115 b'']
1116
Antoine Pitrou152efa22010-05-16 18:19:27 +00001117See the discussion of :ref:`ssl-security` below.
1118
1119
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001120Server-side operation
1121^^^^^^^^^^^^^^^^^^^^^
1122
Antoine Pitrou152efa22010-05-16 18:19:27 +00001123For server operation, typically you'll need to have a server certificate, and
1124private key, each in a file. You'll first create a context holding the key
1125and the certificate, so that clients can check your authenticity. Then
1126you'll open a socket, bind it to a port, call :meth:`listen` on it, and start
1127waiting for clients to connect::
Thomas Woutersed03b412007-08-28 21:37:11 +00001128
1129 import socket, ssl
1130
Antoine Pitrou152efa22010-05-16 18:19:27 +00001131 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
1132 context.load_cert_chain(certfile="mycertfile", keyfile="mykeyfile")
1133
Thomas Woutersed03b412007-08-28 21:37:11 +00001134 bindsocket = socket.socket()
1135 bindsocket.bind(('myaddr.mydomain.com', 10023))
1136 bindsocket.listen(5)
1137
Antoine Pitrou152efa22010-05-16 18:19:27 +00001138When a client connects, you'll call :meth:`accept` on the socket to get the
1139new socket from the other end, and use the context's :meth:`SSLContext.wrap_socket`
1140method to create a server-side SSL socket for the connection::
Thomas Woutersed03b412007-08-28 21:37:11 +00001141
1142 while True:
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001143 newsocket, fromaddr = bindsocket.accept()
1144 connstream = context.wrap_socket(newsocket, server_side=True)
1145 try:
1146 deal_with_client(connstream)
1147 finally:
Antoine Pitroub205d582011-01-02 22:09:27 +00001148 connstream.shutdown(socket.SHUT_RDWR)
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001149 connstream.close()
Thomas Woutersed03b412007-08-28 21:37:11 +00001150
Antoine Pitrou152efa22010-05-16 18:19:27 +00001151Then you'll read data from the ``connstream`` and do something with it till you
Georg Brandl7f01a132009-09-16 15:58:14 +00001152are finished with the client (or the client is finished with you)::
Thomas Woutersed03b412007-08-28 21:37:11 +00001153
1154 def deal_with_client(connstream):
Georg Brandl8a7e5da2011-01-02 19:07:51 +00001155 data = connstream.recv(1024)
1156 # empty data means the client is finished with us
1157 while data:
1158 if not do_something(connstream, data):
1159 # we'll assume do_something returns False
1160 # when we're finished with client
1161 break
1162 data = connstream.recv(1024)
1163 # finished with client
Thomas Woutersed03b412007-08-28 21:37:11 +00001164
Antoine Pitrou152efa22010-05-16 18:19:27 +00001165And go back to listening for new client connections (of course, a real server
1166would probably handle each client connection in a separate thread, or put
1167the sockets in non-blocking mode and use an event loop).
1168
1169
Antoine Pitrou6f5dcb12011-07-11 01:35:48 +02001170.. _ssl-nonblocking:
1171
1172Notes on non-blocking sockets
1173-----------------------------
1174
1175When working with non-blocking sockets, there are several things you need
1176to be aware of:
1177
1178- Calling :func:`~select.select` tells you that the OS-level socket can be
1179 read from (or written to), but it does not imply that there is sufficient
1180 data at the upper SSL layer. For example, only part of an SSL frame might
1181 have arrived. Therefore, you must be ready to handle :meth:`SSLSocket.recv`
1182 and :meth:`SSLSocket.send` failures, and retry after another call to
1183 :func:`~select.select`.
1184
1185 (of course, similar provisions apply when using other primitives such as
1186 :func:`~select.poll`)
1187
1188- The SSL handshake itself will be non-blocking: the
1189 :meth:`SSLSocket.do_handshake` method has to be retried until it returns
1190 successfully. Here is a synopsis using :func:`~select.select` to wait for
1191 the socket's readiness::
1192
1193 while True:
1194 try:
1195 sock.do_handshake()
1196 break
Antoine Pitrou873bf262011-10-27 23:59:03 +02001197 except ssl.SSLWantReadError:
1198 select.select([sock], [], [])
1199 except ssl.SSLWantWriteError:
1200 select.select([], [sock], [])
Antoine Pitrou6f5dcb12011-07-11 01:35:48 +02001201
1202
Antoine Pitrou152efa22010-05-16 18:19:27 +00001203.. _ssl-security:
1204
1205Security considerations
1206-----------------------
1207
1208Verifying certificates
1209^^^^^^^^^^^^^^^^^^^^^^
1210
1211:const:`CERT_NONE` is the default. Since it does not authenticate the other
1212peer, it can be insecure, especially in client mode where most of time you
1213would like to ensure the authenticity of the server you're talking to.
1214Therefore, when in client mode, it is highly recommended to use
1215:const:`CERT_REQUIRED`. However, it is in itself not sufficient; you also
Antoine Pitrou59fdd672010-10-08 10:37:08 +00001216have to check that the server certificate, which can be obtained by calling
1217:meth:`SSLSocket.getpeercert`, matches the desired service. For many
1218protocols and applications, the service can be identified by the hostname;
1219in this case, the :func:`match_hostname` function can be used.
Antoine Pitrou152efa22010-05-16 18:19:27 +00001220
1221In server mode, if you want to authenticate your clients using the SSL layer
1222(rather than using a higher-level authentication mechanism), you'll also have
1223to specify :const:`CERT_REQUIRED` and similarly check the client certificate.
1224
1225 .. note::
1226
1227 In client mode, :const:`CERT_OPTIONAL` and :const:`CERT_REQUIRED` are
1228 equivalent unless anonymous ciphers are enabled (they are disabled
1229 by default).
Thomas Woutersed03b412007-08-28 21:37:11 +00001230
Antoine Pitroub5218772010-05-21 09:56:06 +00001231Protocol versions
1232^^^^^^^^^^^^^^^^^
1233
1234SSL version 2 is considered insecure and is therefore dangerous to use. If
1235you want maximum compatibility between clients and servers, it is recommended
1236to use :const:`PROTOCOL_SSLv23` as the protocol version and then disable
1237SSLv2 explicitly using the :data:`SSLContext.options` attribute::
1238
1239 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
1240 context.options |= ssl.OP_NO_SSLv2
1241
1242The SSL context created above will allow SSLv3 and TLSv1 connections, but
1243not SSLv2.
1244
Antoine Pitroub7ffed82012-01-04 02:53:44 +01001245Cipher selection
1246^^^^^^^^^^^^^^^^
1247
1248If you have advanced security requirements, fine-tuning of the ciphers
1249enabled when negotiating a SSL session is possible through the
1250:meth:`SSLContext.set_ciphers` method. Starting from Python 3.2.3, the
1251ssl module disables certain weak ciphers by default, but you may want
1252to further restrict the cipher choice. For example::
1253
1254 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
1255 context.set_ciphers('HIGH:!aNULL:!eNULL')
1256
1257The ``!aNULL:!eNULL`` part of the cipher spec is necessary to disable ciphers
1258which don't provide both encryption and authentication. Be sure to read
1259OpenSSL's documentation about the `cipher list
1260format <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_.
1261If you want to check which ciphers are enabled by a given cipher list,
1262use the ``openssl ciphers`` command on your system.
1263
Georg Brandl48310cd2009-01-03 21:18:54 +00001264
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001265.. seealso::
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001266
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001267 Class :class:`socket.socket`
1268 Documentation of underlying :mod:`socket` class
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001269
Antoine Pitrouf394e472011-10-07 16:58:07 +02001270 `TLS (Transport Layer Security) and SSL (Secure Socket Layer) <http://www3.rad.com/networks/applications/secure/tls.htm>`_
1271 Debby Koren
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001272
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001273 `RFC 1422: Privacy Enhancement for Internet Electronic Mail: Part II: Certificate-Based Key Management <http://www.ietf.org/rfc/rfc1422>`_
1274 Steve Kent
Thomas Wouters47b49bf2007-08-30 22:15:33 +00001275
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001276 `RFC 1750: Randomness Recommendations for Security <http://www.ietf.org/rfc/rfc1750>`_
1277 D. Eastlake et. al.
Thomas Wouters89d996e2007-09-08 17:39:28 +00001278
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001279 `RFC 3280: Internet X.509 Public Key Infrastructure Certificate and CRL Profile <http://www.ietf.org/rfc/rfc3280>`_
1280 Housley et. al.
Antoine Pitroud5323212010-10-22 18:19:07 +00001281
1282 `RFC 4366: Transport Layer Security (TLS) Extensions <http://www.ietf.org/rfc/rfc4366>`_
1283 Blake-Wilson et. al.