~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.12-docs-html/library/ssl.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
 
 
4
 
 
5
<html xmlns="http://www.w3.org/1999/xhtml">
 
6
  <head>
 
7
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
8
    
 
9
    <title>17.3. ssl — TLS/SSL wrapper for socket objects &mdash; Python 2.7.12 documentation</title>
 
10
    
 
11
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
 
12
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
 
13
    
 
14
    <script type="text/javascript">
 
15
      var DOCUMENTATION_OPTIONS = {
 
16
        URL_ROOT:    '../',
 
17
        VERSION:     '2.7.12',
 
18
        COLLAPSE_INDEX: false,
 
19
        FILE_SUFFIX: '.html',
 
20
        HAS_SOURCE:  true
 
21
      };
 
22
    </script>
 
23
    <script type="text/javascript" src="../_static/jquery.js"></script>
 
24
    <script type="text/javascript" src="../_static/underscore.js"></script>
 
25
    <script type="text/javascript" src="../_static/doctools.js"></script>
 
26
    <script type="text/javascript" src="../_static/sidebar.js"></script>
 
27
    <link rel="search" type="application/opensearchdescription+xml"
 
28
          title="Search within Python 2.7.12 documentation"
 
29
          href="../_static/opensearch.xml"/>
 
30
    <link rel="author" title="About these documents" href="../about.html" />
 
31
    <link rel="copyright" title="Copyright" href="../copyright.html" />
 
32
    <link rel="top" title="Python 2.7.12 documentation" href="../contents.html" />
 
33
    <link rel="up" title="17. Interprocess Communication and Networking" href="ipc.html" />
 
34
    <link rel="next" title="17.4. signal — Set handlers for asynchronous events" href="signal.html" />
 
35
    <link rel="prev" title="17.2. socket — Low-level networking interface" href="socket.html" />
 
36
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 
37
    <script type="text/javascript" src="../_static/copybutton.js"></script>
 
38
    <script type="text/javascript" src="../_static/version_switch.js"></script>
 
39
 
 
40
    
 
41
 
 
42
  </head>
 
43
  <body role="document">  
 
44
    <div class="related" role="navigation" aria-label="related navigation">
 
45
      <h3>Navigation</h3>
 
46
      <ul>
 
47
        <li class="right" style="margin-right: 10px">
 
48
          <a href="../genindex.html" title="General Index"
 
49
             accesskey="I">index</a></li>
 
50
        <li class="right" >
 
51
          <a href="../py-modindex.html" title="Python Module Index"
 
52
             >modules</a> |</li>
 
53
        <li class="right" >
 
54
          <a href="signal.html" title="17.4. signal — Set handlers for asynchronous events"
 
55
             accesskey="N">next</a> |</li>
 
56
        <li class="right" >
 
57
          <a href="socket.html" title="17.2. socket — Low-level networking interface"
 
58
             accesskey="P">previous</a> |</li>
 
59
        <li><img src="../_static/py.png" alt=""
 
60
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
61
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
62
        <li>
 
63
          <span class="version_switcher_placeholder">2.7.12</span>
 
64
          <a href="../index.html">Documentation</a> &raquo;
 
65
        </li>
 
66
 
 
67
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
68
          <li class="nav-item nav-item-2"><a href="ipc.html" accesskey="U">17. Interprocess Communication and Networking</a> &raquo;</li> 
 
69
      </ul>
 
70
    </div>    
 
71
 
 
72
    <div class="document">
 
73
      <div class="documentwrapper">
 
74
        <div class="bodywrapper">
 
75
          <div class="body" role="main">
 
76
            
 
77
  <div class="section" id="module-ssl">
 
78
<span id="ssl-tls-ssl-wrapper-for-socket-objects"></span><h1>17.3. <a class="reference internal" href="#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal"><span class="pre">ssl</span></code></a> &#8212; TLS/SSL wrapper for socket objects<a class="headerlink" href="#module-ssl" title="Permalink to this headline">¶</a></h1>
 
79
<span class="target" id="index-0"></span><div class="versionadded" id="index-1">
 
80
<p><span class="versionmodified">New in version 2.6.</span></p>
 
81
</div>
 
82
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/2.7/Lib/ssl.py">Lib/ssl.py</a></p>
 
83
<hr class="docutils" />
 
84
<p>This module provides access to Transport Layer Security (often known as &#8220;Secure
 
85
Sockets Layer&#8221;) encryption and peer authentication facilities for network
 
86
sockets, both client-side and server-side.  This module uses the OpenSSL
 
87
library. It is available on all modern Unix systems, Windows, Mac OS X, and
 
88
probably additional platforms, as long as OpenSSL is installed on that platform.</p>
 
89
<div class="admonition note">
 
90
<p class="first admonition-title">Note</p>
 
91
<p class="last">Some behavior may be platform dependent, since calls are made to the
 
92
operating system socket APIs.  The installed version of OpenSSL may also
 
93
cause variations in behavior. For example, TLSv1.1 and TLSv1.2 come with
 
94
openssl version 1.0.1.</p>
 
95
</div>
 
96
<div class="admonition warning">
 
97
<p class="first admonition-title">Warning</p>
 
98
<p class="last">Don&#8217;t use this module without reading the <a class="reference internal" href="#ssl-security"><span>Security considerations</span></a>.  Doing so
 
99
may lead to a false sense of security, as the default settings of the
 
100
ssl module are not necessarily appropriate for your application.</p>
 
101
</div>
 
102
<p>This section documents the objects and functions in the <code class="docutils literal"><span class="pre">ssl</span></code> module; for more
 
103
general information about TLS, SSL, and certificates, the reader is referred to
 
104
the documents in the &#8220;See Also&#8221; section at the bottom.</p>
 
105
<p>This module provides a class, <code class="xref py py-class docutils literal"><span class="pre">ssl.SSLSocket</span></code>, which is derived from the
 
106
<a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal"><span class="pre">socket.socket</span></code></a> type, and provides a socket-like wrapper that also
 
107
encrypts and decrypts the data going over the socket with SSL.  It supports
 
108
additional methods such as <code class="xref py py-meth docutils literal"><span class="pre">getpeercert()</span></code>, which retrieves the
 
109
certificate of the other side of the connection, and <code class="xref py py-meth docutils literal"><span class="pre">cipher()</span></code>,which
 
110
retrieves the cipher being used for the secure connection.</p>
 
111
<p>For more sophisticated applications, the <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">ssl.SSLContext</span></code></a> class
 
112
helps manage settings and certificates, which can then be inherited
 
113
by SSL sockets created through the <a class="reference internal" href="#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.wrap_socket()</span></code></a> method.</p>
 
114
<div class="section" id="functions-constants-and-exceptions">
 
115
<h2>17.3.1. Functions, Constants, and Exceptions<a class="headerlink" href="#functions-constants-and-exceptions" title="Permalink to this headline">¶</a></h2>
 
116
<dl class="exception">
 
117
<dt id="ssl.SSLError">
 
118
<em class="property">exception </em><code class="descclassname">ssl.</code><code class="descname">SSLError</code><a class="headerlink" href="#ssl.SSLError" title="Permalink to this definition">¶</a></dt>
 
119
<dd><p>Raised to signal an error from the underlying SSL implementation (currently
 
120
provided by the OpenSSL library).  This signifies some problem in the
 
121
higher-level encryption and authentication layer that&#8217;s superimposed on the
 
122
underlying network connection.  This error is a subtype of
 
123
<a class="reference internal" href="socket.html#socket.error" title="socket.error"><code class="xref py py-exc docutils literal"><span class="pre">socket.error</span></code></a>, which in turn is a subtype of <a class="reference internal" href="exceptions.html#exceptions.IOError" title="exceptions.IOError"><code class="xref py py-exc docutils literal"><span class="pre">IOError</span></code></a>.  The
 
124
error code and message of <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal"><span class="pre">SSLError</span></code></a> instances are provided by the
 
125
OpenSSL library.</p>
 
126
<dl class="attribute">
 
127
<dt id="ssl.SSLError.library">
 
128
<code class="descname">library</code><a class="headerlink" href="#ssl.SSLError.library" title="Permalink to this definition">¶</a></dt>
 
129
<dd><p>A string mnemonic designating the OpenSSL submodule in which the error
 
130
occurred, such as <code class="docutils literal"><span class="pre">SSL</span></code>, <code class="docutils literal"><span class="pre">PEM</span></code> or <code class="docutils literal"><span class="pre">X509</span></code>.  The range of possible
 
131
values depends on the OpenSSL version.</p>
 
132
<div class="versionadded">
 
133
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
134
</div>
 
135
</dd></dl>
 
136
 
 
137
<dl class="attribute">
 
138
<dt id="ssl.SSLError.reason">
 
139
<code class="descname">reason</code><a class="headerlink" href="#ssl.SSLError.reason" title="Permalink to this definition">¶</a></dt>
 
140
<dd><p>A string mnemonic designating the reason this error occurred, for
 
141
example <code class="docutils literal"><span class="pre">CERTIFICATE_VERIFY_FAILED</span></code>.  The range of possible
 
142
values depends on the OpenSSL version.</p>
 
143
<div class="versionadded">
 
144
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
145
</div>
 
146
</dd></dl>
 
147
 
 
148
</dd></dl>
 
149
 
 
150
<dl class="exception">
 
151
<dt id="ssl.SSLZeroReturnError">
 
152
<em class="property">exception </em><code class="descclassname">ssl.</code><code class="descname">SSLZeroReturnError</code><a class="headerlink" href="#ssl.SSLZeroReturnError" title="Permalink to this definition">¶</a></dt>
 
153
<dd><p>A subclass of <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal"><span class="pre">SSLError</span></code></a> raised when trying to read or write and
 
154
the SSL connection has been closed cleanly.  Note that this doesn&#8217;t
 
155
mean that the underlying transport (read TCP) has been closed.</p>
 
156
<div class="versionadded">
 
157
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
158
</div>
 
159
</dd></dl>
 
160
 
 
161
<dl class="exception">
 
162
<dt id="ssl.SSLWantReadError">
 
163
<em class="property">exception </em><code class="descclassname">ssl.</code><code class="descname">SSLWantReadError</code><a class="headerlink" href="#ssl.SSLWantReadError" title="Permalink to this definition">¶</a></dt>
 
164
<dd><p>A subclass of <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal"><span class="pre">SSLError</span></code></a> raised by a <a class="reference internal" href="#ssl-nonblocking"><span>non-blocking SSL socket</span></a> when trying to read or write data, but more data needs
 
165
to be received on the underlying TCP transport before the request can be
 
166
fulfilled.</p>
 
167
<div class="versionadded">
 
168
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
169
</div>
 
170
</dd></dl>
 
171
 
 
172
<dl class="exception">
 
173
<dt id="ssl.SSLWantWriteError">
 
174
<em class="property">exception </em><code class="descclassname">ssl.</code><code class="descname">SSLWantWriteError</code><a class="headerlink" href="#ssl.SSLWantWriteError" title="Permalink to this definition">¶</a></dt>
 
175
<dd><p>A subclass of <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal"><span class="pre">SSLError</span></code></a> raised by a <a class="reference internal" href="#ssl-nonblocking"><span>non-blocking SSL socket</span></a> when trying to read or write data, but more data needs
 
176
to be sent on the underlying TCP transport before the request can be
 
177
fulfilled.</p>
 
178
<div class="versionadded">
 
179
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
180
</div>
 
181
</dd></dl>
 
182
 
 
183
<dl class="exception">
 
184
<dt id="ssl.SSLSyscallError">
 
185
<em class="property">exception </em><code class="descclassname">ssl.</code><code class="descname">SSLSyscallError</code><a class="headerlink" href="#ssl.SSLSyscallError" title="Permalink to this definition">¶</a></dt>
 
186
<dd><p>A subclass of <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal"><span class="pre">SSLError</span></code></a> raised when a system error was encountered
 
187
while trying to fulfill an operation on a SSL socket.  Unfortunately,
 
188
there is no easy way to inspect the original errno number.</p>
 
189
<div class="versionadded">
 
190
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
191
</div>
 
192
</dd></dl>
 
193
 
 
194
<dl class="exception">
 
195
<dt id="ssl.SSLEOFError">
 
196
<em class="property">exception </em><code class="descclassname">ssl.</code><code class="descname">SSLEOFError</code><a class="headerlink" href="#ssl.SSLEOFError" title="Permalink to this definition">¶</a></dt>
 
197
<dd><p>A subclass of <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal"><span class="pre">SSLError</span></code></a> raised when the SSL connection has been
 
198
terminated abruptly.  Generally, you shouldn&#8217;t try to reuse the underlying
 
199
transport when this error is encountered.</p>
 
200
<div class="versionadded">
 
201
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
202
</div>
 
203
</dd></dl>
 
204
 
 
205
<dl class="exception">
 
206
<dt id="ssl.CertificateError">
 
207
<em class="property">exception </em><code class="descclassname">ssl.</code><code class="descname">CertificateError</code><a class="headerlink" href="#ssl.CertificateError" title="Permalink to this definition">¶</a></dt>
 
208
<dd><p>Raised to signal an error with a certificate (such as mismatching
 
209
hostname).  Certificate errors detected by OpenSSL, though, raise
 
210
an <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal"><span class="pre">SSLError</span></code></a>.</p>
 
211
</dd></dl>
 
212
 
 
213
<div class="section" id="socket-creation">
 
214
<h3>17.3.1.1. Socket creation<a class="headerlink" href="#socket-creation" title="Permalink to this headline">¶</a></h3>
 
215
<p>The following function allows for standalone socket creation.  Starting from
 
216
Python 2.7.9, it can be more flexible to use <a class="reference internal" href="#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.wrap_socket()</span></code></a>
 
217
instead.</p>
 
218
<dl class="function">
 
219
<dt id="ssl.wrap_socket">
 
220
<code class="descclassname">ssl.</code><code class="descname">wrap_socket</code><span class="sig-paren">(</span><em>sock</em>, <em>keyfile=None</em>, <em>certfile=None</em>, <em>server_side=False</em>, <em>cert_reqs=CERT_NONE</em>, <em>ssl_version={see docs}</em>, <em>ca_certs=None</em>, <em>do_handshake_on_connect=True</em>, <em>suppress_ragged_eofs=True</em>, <em>ciphers=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.wrap_socket" title="Permalink to this definition">¶</a></dt>
 
221
<dd><p>Takes an instance <code class="docutils literal"><span class="pre">sock</span></code> of <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal"><span class="pre">socket.socket</span></code></a>, and returns an instance
 
222
of <code class="xref py py-class docutils literal"><span class="pre">ssl.SSLSocket</span></code>, a subtype of <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal"><span class="pre">socket.socket</span></code></a>, which wraps
 
223
the underlying socket in an SSL context.  <code class="docutils literal"><span class="pre">sock</span></code> must be a
 
224
<a class="reference internal" href="socket.html#socket.SOCK_STREAM" title="socket.SOCK_STREAM"><code class="xref py py-data docutils literal"><span class="pre">SOCK_STREAM</span></code></a> socket; other socket types are unsupported.</p>
 
225
<p>For client-side sockets, the context construction is lazy; if the
 
226
underlying socket isn&#8217;t connected yet, the context construction will be
 
227
performed after <code class="xref py py-meth docutils literal"><span class="pre">connect()</span></code> is called on the socket.  For
 
228
server-side sockets, if the socket has no remote peer, it is assumed
 
229
to be a listening socket, and the server-side SSL wrapping is
 
230
automatically performed on client connections accepted via the
 
231
<code class="xref py py-meth docutils literal"><span class="pre">accept()</span></code> method.  <a class="reference internal" href="#ssl.wrap_socket" title="ssl.wrap_socket"><code class="xref py py-func docutils literal"><span class="pre">wrap_socket()</span></code></a> may raise <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal"><span class="pre">SSLError</span></code></a>.</p>
 
232
<p>The <code class="docutils literal"><span class="pre">keyfile</span></code> and <code class="docutils literal"><span class="pre">certfile</span></code> parameters specify optional files which
 
233
contain a certificate to be used to identify the local side of the
 
234
connection.  See the discussion of <a class="reference internal" href="#ssl-certificates"><span>Certificates</span></a> for more
 
235
information on how the certificate is stored in the <code class="docutils literal"><span class="pre">certfile</span></code>.</p>
 
236
<p>The parameter <code class="docutils literal"><span class="pre">server_side</span></code> is a boolean which identifies whether
 
237
server-side or client-side behavior is desired from this socket.</p>
 
238
<p>The parameter <code class="docutils literal"><span class="pre">cert_reqs</span></code> specifies whether a certificate is required from
 
239
the other side of the connection, and whether it will be validated if
 
240
provided.  It must be one of the three values <a class="reference internal" href="#ssl.CERT_NONE" title="ssl.CERT_NONE"><code class="xref py py-const docutils literal"><span class="pre">CERT_NONE</span></code></a>
 
241
(certificates ignored), <a class="reference internal" href="#ssl.CERT_OPTIONAL" title="ssl.CERT_OPTIONAL"><code class="xref py py-const docutils literal"><span class="pre">CERT_OPTIONAL</span></code></a> (not required, but validated
 
242
if provided), or <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-const docutils literal"><span class="pre">CERT_REQUIRED</span></code></a> (required and validated).  If the
 
243
value of this parameter is not <a class="reference internal" href="#ssl.CERT_NONE" title="ssl.CERT_NONE"><code class="xref py py-const docutils literal"><span class="pre">CERT_NONE</span></code></a>, then the <code class="docutils literal"><span class="pre">ca_certs</span></code>
 
244
parameter must point to a file of CA certificates.</p>
 
245
<p>The <code class="docutils literal"><span class="pre">ca_certs</span></code> file contains a set of concatenated &#8220;certification
 
246
authority&#8221; certificates, which are used to validate certificates passed from
 
247
the other end of the connection.  See the discussion of
 
248
<a class="reference internal" href="#ssl-certificates"><span>Certificates</span></a> for more information about how to arrange the
 
249
certificates in this file.</p>
 
250
<p>The parameter <code class="docutils literal"><span class="pre">ssl_version</span></code> specifies which version of the SSL protocol to
 
251
use.  Typically, the server chooses a particular protocol version, and the
 
252
client must adapt to the server&#8217;s choice.  Most of the versions are not
 
253
interoperable with the other versions.  If not specified, the default is
 
254
<a class="reference internal" href="#ssl.PROTOCOL_SSLv23" title="ssl.PROTOCOL_SSLv23"><code class="xref py py-data docutils literal"><span class="pre">PROTOCOL_SSLv23</span></code></a>; it provides the most compatibility with other
 
255
versions.</p>
 
256
<p>Here&#8217;s a table showing which versions in a client (down the side) can connect
 
257
to which versions in a server (along the top):</p>
 
258
<blockquote>
 
259
<div><table border="1" class="docutils">
 
260
<colgroup>
 
261
<col width="29%" />
 
262
<col width="11%" />
 
263
<col width="11%" />
 
264
<col width="12%" />
 
265
<col width="11%" />
 
266
<col width="13%" />
 
267
<col width="13%" />
 
268
</colgroup>
 
269
<tbody valign="top">
 
270
<tr class="row-odd"><td><em>client</em> / <strong>server</strong></td>
 
271
<td><strong>SSLv2</strong></td>
 
272
<td><strong>SSLv3</strong></td>
 
273
<td><strong>SSLv23</strong></td>
 
274
<td><strong>TLSv1</strong></td>
 
275
<td><strong>TLSv1.1</strong></td>
 
276
<td><strong>TLSv1.2</strong></td>
 
277
</tr>
 
278
<tr class="row-even"><td><em>SSLv2</em></td>
 
279
<td>yes</td>
 
280
<td>no</td>
 
281
<td>yes</td>
 
282
<td>no</td>
 
283
<td>no</td>
 
284
<td>no</td>
 
285
</tr>
 
286
<tr class="row-odd"><td><em>SSLv3</em></td>
 
287
<td>no</td>
 
288
<td>yes</td>
 
289
<td>yes</td>
 
290
<td>no</td>
 
291
<td>no</td>
 
292
<td>no</td>
 
293
</tr>
 
294
<tr class="row-even"><td><em>SSLv23</em></td>
 
295
<td>no</td>
 
296
<td>yes</td>
 
297
<td>yes</td>
 
298
<td>yes</td>
 
299
<td>yes</td>
 
300
<td>yes</td>
 
301
</tr>
 
302
<tr class="row-odd"><td><em>TLSv1</em></td>
 
303
<td>no</td>
 
304
<td>no</td>
 
305
<td>yes</td>
 
306
<td>yes</td>
 
307
<td>no</td>
 
308
<td>no</td>
 
309
</tr>
 
310
<tr class="row-even"><td><em>TLSv1.1</em></td>
 
311
<td>no</td>
 
312
<td>no</td>
 
313
<td>yes</td>
 
314
<td>no</td>
 
315
<td>yes</td>
 
316
<td>no</td>
 
317
</tr>
 
318
<tr class="row-odd"><td><em>TLSv1.2</em></td>
 
319
<td>no</td>
 
320
<td>no</td>
 
321
<td>yes</td>
 
322
<td>no</td>
 
323
<td>no</td>
 
324
<td>yes</td>
 
325
</tr>
 
326
</tbody>
 
327
</table>
 
328
</div></blockquote>
 
329
<div class="admonition note">
 
330
<p class="first admonition-title">Note</p>
 
331
<p class="last">Which connections succeed will vary depending on the version of
 
332
OpenSSL.  For example, before OpenSSL 1.0.0, an SSLv23 client
 
333
would always attempt SSLv2 connections.</p>
 
334
</div>
 
335
<p>The <em>ciphers</em> parameter sets the available ciphers for this SSL object.
 
336
It should be a string in the <a class="reference external" href="https://www.openssl.org/docs/apps/ciphers.html#CIPHER-LIST-FORMAT">OpenSSL cipher list format</a>.</p>
 
337
<p>The parameter <code class="docutils literal"><span class="pre">do_handshake_on_connect</span></code> specifies whether to do the SSL
 
338
handshake automatically after doing a <code class="xref py py-meth docutils literal"><span class="pre">socket.connect()</span></code>, or whether the
 
339
application program will call it explicitly, by invoking the
 
340
<a class="reference internal" href="#ssl.SSLSocket.do_handshake" title="ssl.SSLSocket.do_handshake"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.do_handshake()</span></code></a> method.  Calling
 
341
<a class="reference internal" href="#ssl.SSLSocket.do_handshake" title="ssl.SSLSocket.do_handshake"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.do_handshake()</span></code></a> explicitly gives the program control over the
 
342
blocking behavior of the socket I/O involved in the handshake.</p>
 
343
<p>The parameter <code class="docutils literal"><span class="pre">suppress_ragged_eofs</span></code> specifies how the
 
344
<code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.read()</span></code> method should signal unexpected EOF from the other end
 
345
of the connection.  If specified as <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> (the default), it returns a
 
346
normal EOF (an empty bytes object) in response to unexpected EOF errors
 
347
raised from the underlying socket; if <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>, it will raise the
 
348
exceptions back to the caller.</p>
 
349
<div class="versionchanged">
 
350
<p><span class="versionmodified">Changed in version 2.7: </span>New optional argument <em>ciphers</em>.</p>
 
351
</div>
 
352
</dd></dl>
 
353
 
 
354
</div>
 
355
<div class="section" id="context-creation">
 
356
<h3>17.3.1.2. Context creation<a class="headerlink" href="#context-creation" title="Permalink to this headline">¶</a></h3>
 
357
<p>A convenience function helps create <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">SSLContext</span></code></a> objects for common
 
358
purposes.</p>
 
359
<dl class="function">
 
360
<dt id="ssl.create_default_context">
 
361
<code class="descclassname">ssl.</code><code class="descname">create_default_context</code><span class="sig-paren">(</span><em>purpose=Purpose.SERVER_AUTH</em>, <em>cafile=None</em>, <em>capath=None</em>, <em>cadata=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.create_default_context" title="Permalink to this definition">¶</a></dt>
 
362
<dd><p>Return a new <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">SSLContext</span></code></a> object with default settings for
 
363
the given <em>purpose</em>.  The settings are chosen by the <a class="reference internal" href="#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal"><span class="pre">ssl</span></code></a> module,
 
364
and usually represent a higher security level than when calling the
 
365
<a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">SSLContext</span></code></a> constructor directly.</p>
 
366
<p><em>cafile</em>, <em>capath</em>, <em>cadata</em> represent optional CA certificates to
 
367
trust for certificate verification, as in
 
368
<a class="reference internal" href="#ssl.SSLContext.load_verify_locations" title="ssl.SSLContext.load_verify_locations"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.load_verify_locations()</span></code></a>.  If all three are
 
369
<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a>, this function can choose to trust the system&#8217;s default
 
370
CA certificates instead.</p>
 
371
<p>The settings are: <a class="reference internal" href="#ssl.PROTOCOL_SSLv23" title="ssl.PROTOCOL_SSLv23"><code class="xref py py-data docutils literal"><span class="pre">PROTOCOL_SSLv23</span></code></a>, <a class="reference internal" href="#ssl.OP_NO_SSLv2" title="ssl.OP_NO_SSLv2"><code class="xref py py-data docutils literal"><span class="pre">OP_NO_SSLv2</span></code></a>, and
 
372
<a class="reference internal" href="#ssl.OP_NO_SSLv3" title="ssl.OP_NO_SSLv3"><code class="xref py py-data docutils literal"><span class="pre">OP_NO_SSLv3</span></code></a> with high encryption cipher suites without RC4 and
 
373
without unauthenticated cipher suites. Passing <a class="reference internal" href="#ssl.Purpose.SERVER_AUTH" title="ssl.Purpose.SERVER_AUTH"><code class="xref py py-data docutils literal"><span class="pre">SERVER_AUTH</span></code></a>
 
374
as <em>purpose</em> sets <a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-data docutils literal"><span class="pre">verify_mode</span></code></a> to <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-data docutils literal"><span class="pre">CERT_REQUIRED</span></code></a>
 
375
and either loads CA certificates (when at least one of <em>cafile</em>, <em>capath</em> or
 
376
<em>cadata</em> is given) or uses <a class="reference internal" href="#ssl.SSLContext.load_default_certs" title="ssl.SSLContext.load_default_certs"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.load_default_certs()</span></code></a> to load
 
377
default CA certificates.</p>
 
378
<div class="admonition note">
 
379
<p class="first admonition-title">Note</p>
 
380
<p>The protocol, options, cipher and other settings may change to more
 
381
restrictive values anytime without prior deprecation.  The values
 
382
represent a fair balance between compatibility and security.</p>
 
383
<p class="last">If your application needs specific settings, you should create a
 
384
<a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">SSLContext</span></code></a> and apply the settings yourself.</p>
 
385
</div>
 
386
<div class="admonition note">
 
387
<p class="first admonition-title">Note</p>
 
388
<p>If you find that when certain older clients or servers attempt to connect
 
389
with a <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">SSLContext</span></code></a> created by this function that they get an error
 
390
stating &#8220;Protocol or cipher suite mismatch&#8221;, it may be that they only
 
391
support SSL3.0 which this function excludes using the
 
392
<a class="reference internal" href="#ssl.OP_NO_SSLv3" title="ssl.OP_NO_SSLv3"><code class="xref py py-data docutils literal"><span class="pre">OP_NO_SSLv3</span></code></a>. SSL3.0 is widely considered to be <a class="reference external" href="https://en.wikipedia.org/wiki/POODLE">completely broken</a>. If you still wish to continue to
 
393
use this function but still allow SSL 3.0 connections you can re-enable
 
394
them using:</p>
 
395
<div class="last highlight-python"><div class="highlight"><pre><span></span><span class="n">ctx</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">create_default_context</span><span class="p">(</span><span class="n">Purpose</span><span class="o">.</span><span class="n">CLIENT_AUTH</span><span class="p">)</span>
 
396
<span class="n">ctx</span><span class="o">.</span><span class="n">options</span> <span class="o">&amp;=</span> <span class="o">~</span><span class="n">ssl</span><span class="o">.</span><span class="n">OP_NO_SSLv3</span>
 
397
</pre></div>
 
398
</div>
 
399
</div>
 
400
<div class="versionadded">
 
401
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
402
</div>
 
403
<div class="versionchanged">
 
404
<p><span class="versionmodified">Changed in version 2.7.10: </span>RC4 was dropped from the default cipher string.</p>
 
405
</div>
 
406
<div class="versionchanged">
 
407
<p><span class="versionmodified">Changed in version 2.7.13: </span>ChaCha20/Poly1305 was added to the default cipher string.</p>
 
408
<p>3DES was dropped from the default cipher string.</p>
 
409
</div>
 
410
</dd></dl>
 
411
 
 
412
<dl class="function">
 
413
<dt id="ssl._https_verify_certificates">
 
414
<code class="descclassname">ssl.</code><code class="descname">_https_verify_certificates</code><span class="sig-paren">(</span><em>enable=True</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl._https_verify_certificates" title="Permalink to this definition">¶</a></dt>
 
415
<dd><p>Specifies whether or not server certificates are verified when creating
 
416
client HTTPS connections without specifying a particular SSL context.</p>
 
417
<p>Starting with Python 2.7.9, <a class="reference internal" href="httplib.html#module-httplib" title="httplib: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">httplib</span></code></a> and modules which use it, such as
 
418
<a class="reference internal" href="urllib2.html#module-urllib2" title="urllib2: Next generation URL opening library."><code class="xref py py-mod docutils literal"><span class="pre">urllib2</span></code></a> and <a class="reference internal" href="xmlrpclib.html#module-xmlrpclib" title="xmlrpclib: XML-RPC client access."><code class="xref py py-mod docutils literal"><span class="pre">xmlrpclib</span></code></a>, default to verifying remote server
 
419
certificates received when establishing client HTTPS connections. This
 
420
default verification checks that the certificate is signed by a Certificate
 
421
Authority in the system trust store and that the Common Name (or Subject
 
422
Alternate Name) on the presented certificate matches the requested host.</p>
 
423
<p>Setting <em>enable</em> to <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> ensures this default behaviour is in
 
424
effect.</p>
 
425
<p>Setting <em>enable</em> to <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> reverts the default HTTPS certificate
 
426
handling to that of Python 2.7.8 and earlier, allowing connections to
 
427
servers using self-signed certificates, servers using certificates signed
 
428
by a Certicate Authority not present in the system trust store, and servers
 
429
where the hostname does not match the presented server certificate.</p>
 
430
<p>The leading underscore on this function denotes that it intentionally does
 
431
not exist in any implementation of Python 3 and may not be present in all
 
432
Python 2.7 implementations. The portable approach to bypassing certificate
 
433
checks or the system trust store when necessary is for tools to enable that
 
434
on a case-by-case basis by explicitly passing in a suitably configured SSL
 
435
context, rather than reverting the default behaviour of the standard library
 
436
client modules.</p>
 
437
<div class="versionadded">
 
438
<p><span class="versionmodified">New in version 2.7.12.</span></p>
 
439
</div>
 
440
<div class="admonition seealso">
 
441
<p class="first admonition-title">See also</p>
 
442
<ul class="last simple">
 
443
<li><a class="reference external" href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2014-9365">CVE-2014-9365</a>
 
444
&#8211; HTTPS man-in-the-middle attack against Python clients using default settings</li>
 
445
<li><span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0476"><strong>PEP 476</strong></a> &#8211; Enabling certificate verification by default for HTTPS</li>
 
446
<li><span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0493"><strong>PEP 493</strong></a> &#8211; HTTPS verification migration tools for Python 2.7</li>
 
447
</ul>
 
448
</div>
 
449
</dd></dl>
 
450
 
 
451
</div>
 
452
<div class="section" id="random-generation">
 
453
<h3>17.3.1.3. Random generation<a class="headerlink" href="#random-generation" title="Permalink to this headline">¶</a></h3>
 
454
<blockquote>
 
455
<div><div class="deprecated">
 
456
<p><span class="versionmodified">Deprecated since version 2.7.13: </span>OpenSSL has deprecated <code class="xref py py-func docutils literal"><span class="pre">ssl.RAND_pseudo_bytes()</span></code>, use
 
457
<code class="xref py py-func docutils literal"><span class="pre">ssl.RAND_bytes()</span></code> instead.</p>
 
458
</div>
 
459
</div></blockquote>
 
460
<dl class="function">
 
461
<dt id="ssl.RAND_status">
 
462
<code class="descclassname">ssl.</code><code class="descname">RAND_status</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.RAND_status" title="Permalink to this definition">¶</a></dt>
 
463
<dd><p>Return <code class="docutils literal"><span class="pre">True</span></code> if the SSL pseudo-random number generator has been seeded
 
464
with &#8216;enough&#8217; randomness, and <code class="docutils literal"><span class="pre">False</span></code> otherwise.  You can use
 
465
<a class="reference internal" href="#ssl.RAND_egd" title="ssl.RAND_egd"><code class="xref py py-func docutils literal"><span class="pre">ssl.RAND_egd()</span></code></a> and <a class="reference internal" href="#ssl.RAND_add" title="ssl.RAND_add"><code class="xref py py-func docutils literal"><span class="pre">ssl.RAND_add()</span></code></a> to increase the randomness of
 
466
the pseudo-random number generator.</p>
 
467
</dd></dl>
 
468
 
 
469
<dl class="function">
 
470
<dt id="ssl.RAND_egd">
 
471
<code class="descclassname">ssl.</code><code class="descname">RAND_egd</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.RAND_egd" title="Permalink to this definition">¶</a></dt>
 
472
<dd><p>If you are running an entropy-gathering daemon (EGD) somewhere, and <em>path</em>
 
473
is the pathname of a socket connection open to it, this will read 256 bytes
 
474
of randomness from the socket, and add it to the SSL pseudo-random number
 
475
generator to increase the security of generated secret keys.  This is
 
476
typically only necessary on systems without better sources of randomness.</p>
 
477
<p>See <a class="reference external" href="http://egd.sourceforge.net/">http://egd.sourceforge.net/</a> or <a class="reference external" href="http://prngd.sourceforge.net/">http://prngd.sourceforge.net/</a> for sources
 
478
of entropy-gathering daemons.</p>
 
479
<p>Availability: not available with LibreSSL and OpenSSL &gt; 1.1.0</p>
 
480
</dd></dl>
 
481
 
 
482
<dl class="function">
 
483
<dt id="ssl.RAND_add">
 
484
<code class="descclassname">ssl.</code><code class="descname">RAND_add</code><span class="sig-paren">(</span><em>bytes</em>, <em>entropy</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.RAND_add" title="Permalink to this definition">¶</a></dt>
 
485
<dd><p>Mix the given <em>bytes</em> into the SSL pseudo-random number generator.  The
 
486
parameter <em>entropy</em> (a float) is a lower bound on the entropy contained in
 
487
string (so you can always use <code class="xref py py-const docutils literal"><span class="pre">0.0</span></code>).  See <span class="target" id="index-4"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1750.html"><strong>RFC 1750</strong></a> for more
 
488
information on sources of entropy.</p>
 
489
</dd></dl>
 
490
 
 
491
</div>
 
492
<div class="section" id="certificate-handling">
 
493
<h3>17.3.1.4. Certificate handling<a class="headerlink" href="#certificate-handling" title="Permalink to this headline">¶</a></h3>
 
494
<dl class="function">
 
495
<dt id="ssl.match_hostname">
 
496
<code class="descclassname">ssl.</code><code class="descname">match_hostname</code><span class="sig-paren">(</span><em>cert</em>, <em>hostname</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.match_hostname" title="Permalink to this definition">¶</a></dt>
 
497
<dd><p>Verify that <em>cert</em> (in decoded format as returned by
 
498
<a class="reference internal" href="#ssl.SSLSocket.getpeercert" title="ssl.SSLSocket.getpeercert"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.getpeercert()</span></code></a>) matches the given <em>hostname</em>.  The rules
 
499
applied are those for checking the identity of HTTPS servers as outlined
 
500
in <span class="target" id="index-5"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2818.html"><strong>RFC 2818</strong></a> and <span class="target" id="index-6"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc6125.html"><strong>RFC 6125</strong></a>, except that IP addresses are not currently
 
501
supported. In addition to HTTPS, this function should be suitable for
 
502
checking the identity of servers in various SSL-based protocols such as
 
503
FTPS, IMAPS, POPS and others.</p>
 
504
<p><a class="reference internal" href="#ssl.CertificateError" title="ssl.CertificateError"><code class="xref py py-exc docutils literal"><span class="pre">CertificateError</span></code></a> is raised on failure. On success, the function
 
505
returns nothing:</p>
 
506
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cert</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;subject&#39;</span><span class="p">:</span> <span class="p">(((</span><span class="s1">&#39;commonName&#39;</span><span class="p">,</span> <span class="s1">&#39;example.com&#39;</span><span class="p">),),)}</span>
 
507
<span class="gp">&gt;&gt;&gt; </span><span class="n">ssl</span><span class="o">.</span><span class="n">match_hostname</span><span class="p">(</span><span class="n">cert</span><span class="p">,</span> <span class="s2">&quot;example.com&quot;</span><span class="p">)</span>
 
508
<span class="gp">&gt;&gt;&gt; </span><span class="n">ssl</span><span class="o">.</span><span class="n">match_hostname</span><span class="p">(</span><span class="n">cert</span><span class="p">,</span> <span class="s2">&quot;example.org&quot;</span><span class="p">)</span>
 
509
<span class="gt">Traceback (most recent call last):</span>
 
510
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
 
511
  File <span class="nb">&quot;/home/py3k/Lib/ssl.py&quot;</span>, line <span class="m">130</span>, in <span class="n">match_hostname</span>
 
512
<span class="gr">ssl.CertificateError</span>: <span class="n">hostname &#39;example.org&#39; doesn&#39;t match &#39;example.com&#39;</span>
 
513
</pre></div>
 
514
</div>
 
515
<div class="versionadded">
 
516
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
517
</div>
 
518
</dd></dl>
 
519
 
 
520
<dl class="function">
 
521
<dt id="ssl.cert_time_to_seconds">
 
522
<code class="descclassname">ssl.</code><code class="descname">cert_time_to_seconds</code><span class="sig-paren">(</span><em>cert_time</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.cert_time_to_seconds" title="Permalink to this definition">¶</a></dt>
 
523
<dd><p>Return the time in seconds since the Epoch, given the <code class="docutils literal"><span class="pre">cert_time</span></code>
 
524
string representing the &#8220;notBefore&#8221; or &#8220;notAfter&#8221; date from a
 
525
certificate in <code class="docutils literal"><span class="pre">&quot;%b</span> <span class="pre">%d</span> <span class="pre">%H:%M:%S</span> <span class="pre">%Y</span> <span class="pre">%Z&quot;</span></code> strptime format (C
 
526
locale).</p>
 
527
<p>Here&#8217;s an example:</p>
 
528
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">ssl</span>
 
529
<span class="gp">&gt;&gt;&gt; </span><span class="n">timestamp</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">cert_time_to_seconds</span><span class="p">(</span><span class="s2">&quot;Jan  5 09:34:43 2018 GMT&quot;</span><span class="p">)</span>
 
530
<span class="gp">&gt;&gt;&gt; </span><span class="n">timestamp</span>
 
531
<span class="go">1515144883</span>
 
532
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
 
533
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">utcfromtimestamp</span><span class="p">(</span><span class="n">timestamp</span><span class="p">))</span>
 
534
<span class="go">2018-01-05 09:34:43</span>
 
535
</pre></div>
 
536
</div>
 
537
<p>&#8220;notBefore&#8221; or &#8220;notAfter&#8221; dates must use GMT (<span class="target" id="index-7"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc5280.html"><strong>RFC 5280</strong></a>).</p>
 
538
<div class="versionchanged">
 
539
<p><span class="versionmodified">Changed in version 2.7.9: </span>Interpret the input time as a time in UTC as specified by &#8216;GMT&#8217;
 
540
timezone in the input string. Local timezone was used
 
541
previously. Return an integer (no fractions of a second in the
 
542
input format)</p>
 
543
</div>
 
544
</dd></dl>
 
545
 
 
546
<dl class="function">
 
547
<dt id="ssl.get_server_certificate">
 
548
<code class="descclassname">ssl.</code><code class="descname">get_server_certificate</code><span class="sig-paren">(</span><em>addr</em>, <em>ssl_version=PROTOCOL_SSLv23</em>, <em>ca_certs=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.get_server_certificate" title="Permalink to this definition">¶</a></dt>
 
549
<dd><p>Given the address <code class="docutils literal"><span class="pre">addr</span></code> of an SSL-protected server, as a (<em>hostname</em>,
 
550
<em>port-number</em>) pair, fetches the server&#8217;s certificate, and returns it as a
 
551
PEM-encoded string.  If <code class="docutils literal"><span class="pre">ssl_version</span></code> is specified, uses that version of
 
552
the SSL protocol to attempt to connect to the server.  If <code class="docutils literal"><span class="pre">ca_certs</span></code> is
 
553
specified, it should be a file containing a list of root certificates, the
 
554
same format as used for the same parameter in <a class="reference internal" href="#ssl.wrap_socket" title="ssl.wrap_socket"><code class="xref py py-func docutils literal"><span class="pre">wrap_socket()</span></code></a>.  The call
 
555
will attempt to validate the server certificate against that set of root
 
556
certificates, and will fail if the validation attempt fails.</p>
 
557
<div class="versionchanged">
 
558
<p><span class="versionmodified">Changed in version 2.7.9: </span>This function is now IPv6-compatible, and the default <em>ssl_version</em> is
 
559
changed from <a class="reference internal" href="#ssl.PROTOCOL_SSLv3" title="ssl.PROTOCOL_SSLv3"><code class="xref py py-data docutils literal"><span class="pre">PROTOCOL_SSLv3</span></code></a> to <a class="reference internal" href="#ssl.PROTOCOL_SSLv23" title="ssl.PROTOCOL_SSLv23"><code class="xref py py-data docutils literal"><span class="pre">PROTOCOL_SSLv23</span></code></a> for
 
560
maximum compatibility with modern servers.</p>
 
561
</div>
 
562
</dd></dl>
 
563
 
 
564
<dl class="function">
 
565
<dt id="ssl.DER_cert_to_PEM_cert">
 
566
<code class="descclassname">ssl.</code><code class="descname">DER_cert_to_PEM_cert</code><span class="sig-paren">(</span><em>DER_cert_bytes</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.DER_cert_to_PEM_cert" title="Permalink to this definition">¶</a></dt>
 
567
<dd><p>Given a certificate as a DER-encoded blob of bytes, returns a PEM-encoded
 
568
string version of the same certificate.</p>
 
569
</dd></dl>
 
570
 
 
571
<dl class="function">
 
572
<dt id="ssl.PEM_cert_to_DER_cert">
 
573
<code class="descclassname">ssl.</code><code class="descname">PEM_cert_to_DER_cert</code><span class="sig-paren">(</span><em>PEM_cert_string</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.PEM_cert_to_DER_cert" title="Permalink to this definition">¶</a></dt>
 
574
<dd><p>Given a certificate as an ASCII PEM string, returns a DER-encoded sequence of
 
575
bytes for that same certificate.</p>
 
576
</dd></dl>
 
577
 
 
578
<dl class="function">
 
579
<dt id="ssl.get_default_verify_paths">
 
580
<code class="descclassname">ssl.</code><code class="descname">get_default_verify_paths</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.get_default_verify_paths" title="Permalink to this definition">¶</a></dt>
 
581
<dd><p>Returns a named tuple with paths to OpenSSL&#8217;s default cafile and capath.
 
582
The paths are the same as used by
 
583
<a class="reference internal" href="#ssl.SSLContext.set_default_verify_paths" title="ssl.SSLContext.set_default_verify_paths"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.set_default_verify_paths()</span></code></a>. The return value is a
 
584
<a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> <code class="docutils literal"><span class="pre">DefaultVerifyPaths</span></code>:</p>
 
585
<ul class="simple">
 
586
<li><code class="xref py py-attr docutils literal"><span class="pre">cafile</span></code> - resolved path to cafile or <code class="docutils literal"><span class="pre">None</span></code> if the file doesn&#8217;t exist,</li>
 
587
<li><code class="xref py py-attr docutils literal"><span class="pre">capath</span></code> - resolved path to capath or <code class="docutils literal"><span class="pre">None</span></code> if the directory doesn&#8217;t exist,</li>
 
588
<li><code class="xref py py-attr docutils literal"><span class="pre">openssl_cafile_env</span></code> - OpenSSL&#8217;s environment key that points to a cafile,</li>
 
589
<li><code class="xref py py-attr docutils literal"><span class="pre">openssl_cafile</span></code> - hard coded path to a cafile,</li>
 
590
<li><code class="xref py py-attr docutils literal"><span class="pre">openssl_capath_env</span></code> - OpenSSL&#8217;s environment key that points to a capath,</li>
 
591
<li><code class="xref py py-attr docutils literal"><span class="pre">openssl_capath</span></code> - hard coded path to a capath directory</li>
 
592
</ul>
 
593
<p>Availability: LibreSSL ignores the environment vars
 
594
<code class="xref py py-attr docutils literal"><span class="pre">openssl_cafile_env</span></code> and <code class="xref py py-attr docutils literal"><span class="pre">openssl_capath_env</span></code></p>
 
595
<div class="versionadded">
 
596
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
597
</div>
 
598
</dd></dl>
 
599
 
 
600
<dl class="function">
 
601
<dt id="ssl.enum_certificates">
 
602
<code class="descclassname">ssl.</code><code class="descname">enum_certificates</code><span class="sig-paren">(</span><em>store_name</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.enum_certificates" title="Permalink to this definition">¶</a></dt>
 
603
<dd><p>Retrieve certificates from Windows&#8217; system cert store. <em>store_name</em> may be
 
604
one of <code class="docutils literal"><span class="pre">CA</span></code>, <code class="docutils literal"><span class="pre">ROOT</span></code> or <code class="docutils literal"><span class="pre">MY</span></code>. Windows may provide additional cert
 
605
stores, too.</p>
 
606
<p>The function returns a list of (cert_bytes, encoding_type, trust) tuples.
 
607
The encoding_type specifies the encoding of cert_bytes. It is either
 
608
<code class="xref py py-const docutils literal"><span class="pre">x509_asn</span></code> for X.509 ASN.1 data or <code class="xref py py-const docutils literal"><span class="pre">pkcs_7_asn</span></code> for
 
609
PKCS#7 ASN.1 data. Trust specifies the purpose of the certificate as a set
 
610
of OIDS or exactly <code class="docutils literal"><span class="pre">True</span></code> if the certificate is trustworthy for all
 
611
purposes.</p>
 
612
<p>Example:</p>
 
613
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ssl</span><span class="o">.</span><span class="n">enum_certificates</span><span class="p">(</span><span class="s2">&quot;CA&quot;</span><span class="p">)</span>
 
614
<span class="go">[(b&#39;data...&#39;, &#39;x509_asn&#39;, {&#39;1.3.6.1.5.5.7.3.1&#39;, &#39;1.3.6.1.5.5.7.3.2&#39;}),</span>
 
615
<span class="go"> (b&#39;data...&#39;, &#39;x509_asn&#39;, True)]</span>
 
616
</pre></div>
 
617
</div>
 
618
<p>Availability: Windows.</p>
 
619
<div class="versionadded">
 
620
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
621
</div>
 
622
</dd></dl>
 
623
 
 
624
<dl class="function">
 
625
<dt id="ssl.enum_crls">
 
626
<code class="descclassname">ssl.</code><code class="descname">enum_crls</code><span class="sig-paren">(</span><em>store_name</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.enum_crls" title="Permalink to this definition">¶</a></dt>
 
627
<dd><p>Retrieve CRLs from Windows&#8217; system cert store. <em>store_name</em> may be
 
628
one of <code class="docutils literal"><span class="pre">CA</span></code>, <code class="docutils literal"><span class="pre">ROOT</span></code> or <code class="docutils literal"><span class="pre">MY</span></code>. Windows may provide additional cert
 
629
stores, too.</p>
 
630
<p>The function returns a list of (cert_bytes, encoding_type, trust) tuples.
 
631
The encoding_type specifies the encoding of cert_bytes. It is either
 
632
<code class="xref py py-const docutils literal"><span class="pre">x509_asn</span></code> for X.509 ASN.1 data or <code class="xref py py-const docutils literal"><span class="pre">pkcs_7_asn</span></code> for
 
633
PKCS#7 ASN.1 data.</p>
 
634
<p>Availability: Windows.</p>
 
635
<div class="versionadded">
 
636
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
637
</div>
 
638
</dd></dl>
 
639
 
 
640
</div>
 
641
<div class="section" id="constants">
 
642
<h3>17.3.1.5. Constants<a class="headerlink" href="#constants" title="Permalink to this headline">¶</a></h3>
 
643
<dl class="data">
 
644
<dt id="ssl.CERT_NONE">
 
645
<code class="descclassname">ssl.</code><code class="descname">CERT_NONE</code><a class="headerlink" href="#ssl.CERT_NONE" title="Permalink to this definition">¶</a></dt>
 
646
<dd><p>Possible value for <a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-attr docutils literal"><span class="pre">SSLContext.verify_mode</span></code></a>, or the <code class="docutils literal"><span class="pre">cert_reqs</span></code>
 
647
parameter to <a class="reference internal" href="#ssl.wrap_socket" title="ssl.wrap_socket"><code class="xref py py-func docutils literal"><span class="pre">wrap_socket()</span></code></a>.  In this mode (the default), no
 
648
certificates will be required from the other side of the socket connection.
 
649
If a certificate is received from the other end, no attempt to validate it
 
650
is made.</p>
 
651
<p>See the discussion of <a class="reference internal" href="#ssl-security"><span>Security considerations</span></a> below.</p>
 
652
</dd></dl>
 
653
 
 
654
<dl class="data">
 
655
<dt id="ssl.CERT_OPTIONAL">
 
656
<code class="descclassname">ssl.</code><code class="descname">CERT_OPTIONAL</code><a class="headerlink" href="#ssl.CERT_OPTIONAL" title="Permalink to this definition">¶</a></dt>
 
657
<dd><p>Possible value for <a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-attr docutils literal"><span class="pre">SSLContext.verify_mode</span></code></a>, or the <code class="docutils literal"><span class="pre">cert_reqs</span></code>
 
658
parameter to <a class="reference internal" href="#ssl.wrap_socket" title="ssl.wrap_socket"><code class="xref py py-func docutils literal"><span class="pre">wrap_socket()</span></code></a>.  In this mode no certificates will be
 
659
required from the other side of the socket connection; but if they
 
660
are provided, validation will be attempted and an <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-class docutils literal"><span class="pre">SSLError</span></code></a>
 
661
will be raised on failure.</p>
 
662
<p>Use of this setting requires a valid set of CA certificates to
 
663
be passed, either to <a class="reference internal" href="#ssl.SSLContext.load_verify_locations" title="ssl.SSLContext.load_verify_locations"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.load_verify_locations()</span></code></a> or as a
 
664
value of the <code class="docutils literal"><span class="pre">ca_certs</span></code> parameter to <a class="reference internal" href="#ssl.wrap_socket" title="ssl.wrap_socket"><code class="xref py py-func docutils literal"><span class="pre">wrap_socket()</span></code></a>.</p>
 
665
</dd></dl>
 
666
 
 
667
<dl class="data">
 
668
<dt id="ssl.CERT_REQUIRED">
 
669
<code class="descclassname">ssl.</code><code class="descname">CERT_REQUIRED</code><a class="headerlink" href="#ssl.CERT_REQUIRED" title="Permalink to this definition">¶</a></dt>
 
670
<dd><p>Possible value for <a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-attr docutils literal"><span class="pre">SSLContext.verify_mode</span></code></a>, or the <code class="docutils literal"><span class="pre">cert_reqs</span></code>
 
671
parameter to <a class="reference internal" href="#ssl.wrap_socket" title="ssl.wrap_socket"><code class="xref py py-func docutils literal"><span class="pre">wrap_socket()</span></code></a>.  In this mode, certificates are
 
672
required from the other side of the socket connection; an <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-class docutils literal"><span class="pre">SSLError</span></code></a>
 
673
will be raised if no certificate is provided, or if its validation fails.</p>
 
674
<p>Use of this setting requires a valid set of CA certificates to
 
675
be passed, either to <a class="reference internal" href="#ssl.SSLContext.load_verify_locations" title="ssl.SSLContext.load_verify_locations"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.load_verify_locations()</span></code></a> or as a
 
676
value of the <code class="docutils literal"><span class="pre">ca_certs</span></code> parameter to <a class="reference internal" href="#ssl.wrap_socket" title="ssl.wrap_socket"><code class="xref py py-func docutils literal"><span class="pre">wrap_socket()</span></code></a>.</p>
 
677
</dd></dl>
 
678
 
 
679
<dl class="data">
 
680
<dt id="ssl.VERIFY_DEFAULT">
 
681
<code class="descclassname">ssl.</code><code class="descname">VERIFY_DEFAULT</code><a class="headerlink" href="#ssl.VERIFY_DEFAULT" title="Permalink to this definition">¶</a></dt>
 
682
<dd><p>Possible value for <a class="reference internal" href="#ssl.SSLContext.verify_flags" title="ssl.SSLContext.verify_flags"><code class="xref py py-attr docutils literal"><span class="pre">SSLContext.verify_flags</span></code></a>. In this mode, certificate
 
683
revocation lists (CRLs) are not checked. By default OpenSSL does neither
 
684
require nor verify CRLs.</p>
 
685
<div class="versionadded">
 
686
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
687
</div>
 
688
</dd></dl>
 
689
 
 
690
<dl class="data">
 
691
<dt id="ssl.VERIFY_CRL_CHECK_LEAF">
 
692
<code class="descclassname">ssl.</code><code class="descname">VERIFY_CRL_CHECK_LEAF</code><a class="headerlink" href="#ssl.VERIFY_CRL_CHECK_LEAF" title="Permalink to this definition">¶</a></dt>
 
693
<dd><p>Possible value for <a class="reference internal" href="#ssl.SSLContext.verify_flags" title="ssl.SSLContext.verify_flags"><code class="xref py py-attr docutils literal"><span class="pre">SSLContext.verify_flags</span></code></a>. In this mode, only the
 
694
peer cert is check but non of the intermediate CA certificates. The mode
 
695
requires a valid CRL that is signed by the peer cert&#8217;s issuer (its direct
 
696
ancestor CA). If no proper has been loaded
 
697
<a class="reference internal" href="#ssl.SSLContext.load_verify_locations" title="ssl.SSLContext.load_verify_locations"><code class="xref py py-attr docutils literal"><span class="pre">SSLContext.load_verify_locations</span></code></a>, validation will fail.</p>
 
698
<div class="versionadded">
 
699
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
700
</div>
 
701
</dd></dl>
 
702
 
 
703
<dl class="data">
 
704
<dt id="ssl.VERIFY_CRL_CHECK_CHAIN">
 
705
<code class="descclassname">ssl.</code><code class="descname">VERIFY_CRL_CHECK_CHAIN</code><a class="headerlink" href="#ssl.VERIFY_CRL_CHECK_CHAIN" title="Permalink to this definition">¶</a></dt>
 
706
<dd><p>Possible value for <a class="reference internal" href="#ssl.SSLContext.verify_flags" title="ssl.SSLContext.verify_flags"><code class="xref py py-attr docutils literal"><span class="pre">SSLContext.verify_flags</span></code></a>. In this mode, CRLs of
 
707
all certificates in the peer cert chain are checked.</p>
 
708
<div class="versionadded">
 
709
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
710
</div>
 
711
</dd></dl>
 
712
 
 
713
<dl class="data">
 
714
<dt id="ssl.VERIFY_X509_STRICT">
 
715
<code class="descclassname">ssl.</code><code class="descname">VERIFY_X509_STRICT</code><a class="headerlink" href="#ssl.VERIFY_X509_STRICT" title="Permalink to this definition">¶</a></dt>
 
716
<dd><p>Possible value for <a class="reference internal" href="#ssl.SSLContext.verify_flags" title="ssl.SSLContext.verify_flags"><code class="xref py py-attr docutils literal"><span class="pre">SSLContext.verify_flags</span></code></a> to disable workarounds
 
717
for broken X.509 certificates.</p>
 
718
<div class="versionadded">
 
719
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
720
</div>
 
721
</dd></dl>
 
722
 
 
723
<dl class="data">
 
724
<dt id="ssl.VERIFY_X509_TRUSTED_FIRST">
 
725
<code class="descclassname">ssl.</code><code class="descname">VERIFY_X509_TRUSTED_FIRST</code><a class="headerlink" href="#ssl.VERIFY_X509_TRUSTED_FIRST" title="Permalink to this definition">¶</a></dt>
 
726
<dd><p>Possible value for <a class="reference internal" href="#ssl.SSLContext.verify_flags" title="ssl.SSLContext.verify_flags"><code class="xref py py-attr docutils literal"><span class="pre">SSLContext.verify_flags</span></code></a>. It instructs OpenSSL to
 
727
prefer trusted certificates when building the trust chain to validate a
 
728
certificate. This flag is enabled by default.</p>
 
729
<div class="versionadded">
 
730
<p><span class="versionmodified">New in version 2.7.10.</span></p>
 
731
</div>
 
732
</dd></dl>
 
733
 
 
734
<dl class="data">
 
735
<dt id="ssl.PROTOCOL_TLS">
 
736
<code class="descclassname">ssl.</code><code class="descname">PROTOCOL_TLS</code><a class="headerlink" href="#ssl.PROTOCOL_TLS" title="Permalink to this definition">¶</a></dt>
 
737
<dd><p>Selects the highest protocol version that both the client and server support.
 
738
Despite the name, this option can select &#8220;TLS&#8221; protocols as well as &#8220;SSL&#8221;.</p>
 
739
<div class="versionadded">
 
740
<p><span class="versionmodified">New in version 2.7.13.</span></p>
 
741
</div>
 
742
</dd></dl>
 
743
 
 
744
<dl class="data">
 
745
<dt id="ssl.PROTOCOL_SSLv23">
 
746
<code class="descclassname">ssl.</code><code class="descname">PROTOCOL_SSLv23</code><a class="headerlink" href="#ssl.PROTOCOL_SSLv23" title="Permalink to this definition">¶</a></dt>
 
747
<dd><p>Alias for <code class="docutils literal"><span class="pre">PROTOCOL_TLS</span></code>.</p>
 
748
<div class="deprecated">
 
749
<p><span class="versionmodified">Deprecated since version 2.7.13: </span>Use <code class="docutils literal"><span class="pre">PROTOCOL_TLS</span></code> instead.</p>
 
750
</div>
 
751
</dd></dl>
 
752
 
 
753
<dl class="data">
 
754
<dt id="ssl.PROTOCOL_SSLv2">
 
755
<code class="descclassname">ssl.</code><code class="descname">PROTOCOL_SSLv2</code><a class="headerlink" href="#ssl.PROTOCOL_SSLv2" title="Permalink to this definition">¶</a></dt>
 
756
<dd><p>Selects SSL version 2 as the channel encryption protocol.</p>
 
757
<p>This protocol is not available if OpenSSL is compiled with the
 
758
<code class="docutils literal"><span class="pre">OPENSSL_NO_SSL2</span></code> flag.</p>
 
759
<div class="admonition warning">
 
760
<p class="first admonition-title">Warning</p>
 
761
<p class="last">SSL version 2 is insecure.  Its use is highly discouraged.</p>
 
762
</div>
 
763
<div class="deprecated">
 
764
<p><span class="versionmodified">Deprecated since version 2.7.13: </span>OpenSSL has removed support for SSLv2.</p>
 
765
</div>
 
766
</dd></dl>
 
767
 
 
768
<dl class="data">
 
769
<dt id="ssl.PROTOCOL_SSLv3">
 
770
<code class="descclassname">ssl.</code><code class="descname">PROTOCOL_SSLv3</code><a class="headerlink" href="#ssl.PROTOCOL_SSLv3" title="Permalink to this definition">¶</a></dt>
 
771
<dd><p>Selects SSL version 3 as the channel encryption protocol.</p>
 
772
<p>This protocol is not be available if OpenSSL is compiled with the
 
773
<code class="docutils literal"><span class="pre">OPENSSL_NO_SSLv3</span></code> flag.</p>
 
774
<div class="admonition warning">
 
775
<p class="first admonition-title">Warning</p>
 
776
<p class="last">SSL version 3 is insecure.  Its use is highly discouraged.</p>
 
777
</div>
 
778
<div class="deprecated">
 
779
<p><span class="versionmodified">Deprecated since version 2.7.13: </span>OpenSSL has deprecated all version specific protocols. Use the default
 
780
protocol with flags like <code class="docutils literal"><span class="pre">OP_NO_SSLv3</span></code> instead.</p>
 
781
</div>
 
782
</dd></dl>
 
783
 
 
784
<dl class="data">
 
785
<dt id="ssl.PROTOCOL_TLSv1">
 
786
<code class="descclassname">ssl.</code><code class="descname">PROTOCOL_TLSv1</code><a class="headerlink" href="#ssl.PROTOCOL_TLSv1" title="Permalink to this definition">¶</a></dt>
 
787
<dd><p>Selects TLS version 1.0 as the channel encryption protocol.</p>
 
788
<div class="deprecated">
 
789
<p><span class="versionmodified">Deprecated since version 2.7.13: </span>OpenSSL has deprecated all version specific protocols. Use the default
 
790
protocol with flags like <code class="docutils literal"><span class="pre">OP_NO_SSLv3</span></code> instead.</p>
 
791
</div>
 
792
</dd></dl>
 
793
 
 
794
<dl class="data">
 
795
<dt id="ssl.PROTOCOL_TLSv1_1">
 
796
<code class="descclassname">ssl.</code><code class="descname">PROTOCOL_TLSv1_1</code><a class="headerlink" href="#ssl.PROTOCOL_TLSv1_1" title="Permalink to this definition">¶</a></dt>
 
797
<dd><p>Selects TLS version 1.1 as the channel encryption protocol.
 
798
Available only with openssl version 1.0.1+.</p>
 
799
<div class="versionadded">
 
800
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
801
</div>
 
802
<div class="deprecated">
 
803
<p><span class="versionmodified">Deprecated since version 2.7.13: </span>OpenSSL has deprecated all version specific protocols. Use the default
 
804
protocol with flags like <code class="docutils literal"><span class="pre">OP_NO_SSLv3</span></code> instead.</p>
 
805
</div>
 
806
</dd></dl>
 
807
 
 
808
<dl class="data">
 
809
<dt id="ssl.PROTOCOL_TLSv1_2">
 
810
<code class="descclassname">ssl.</code><code class="descname">PROTOCOL_TLSv1_2</code><a class="headerlink" href="#ssl.PROTOCOL_TLSv1_2" title="Permalink to this definition">¶</a></dt>
 
811
<dd><p>Selects TLS version 1.2 as the channel encryption protocol. This is the
 
812
most modern version, and probably the best choice for maximum protection,
 
813
if both sides can speak it.  Available only with openssl version 1.0.1+.</p>
 
814
<div class="versionadded">
 
815
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
816
</div>
 
817
<div class="deprecated">
 
818
<p><span class="versionmodified">Deprecated since version 2.7.13: </span>OpenSSL has deprecated all version specific protocols. Use the default
 
819
protocol with flags like <code class="docutils literal"><span class="pre">OP_NO_SSLv3</span></code> instead.</p>
 
820
</div>
 
821
</dd></dl>
 
822
 
 
823
<dl class="data">
 
824
<dt id="ssl.OP_ALL">
 
825
<code class="descclassname">ssl.</code><code class="descname">OP_ALL</code><a class="headerlink" href="#ssl.OP_ALL" title="Permalink to this definition">¶</a></dt>
 
826
<dd><p>Enables workarounds for various bugs present in other SSL implementations.
 
827
This option is set by default.  It does not necessarily set the same
 
828
flags as OpenSSL&#8217;s <code class="docutils literal"><span class="pre">SSL_OP_ALL</span></code> constant.</p>
 
829
<div class="versionadded">
 
830
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
831
</div>
 
832
</dd></dl>
 
833
 
 
834
<dl class="data">
 
835
<dt id="ssl.OP_NO_SSLv2">
 
836
<code class="descclassname">ssl.</code><code class="descname">OP_NO_SSLv2</code><a class="headerlink" href="#ssl.OP_NO_SSLv2" title="Permalink to this definition">¶</a></dt>
 
837
<dd><p>Prevents an SSLv2 connection.  This option is only applicable in
 
838
conjunction with <a class="reference internal" href="#ssl.PROTOCOL_SSLv23" title="ssl.PROTOCOL_SSLv23"><code class="xref py py-const docutils literal"><span class="pre">PROTOCOL_SSLv23</span></code></a>.  It prevents the peers from
 
839
choosing SSLv2 as the protocol version.</p>
 
840
<div class="versionadded">
 
841
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
842
</div>
 
843
</dd></dl>
 
844
 
 
845
<dl class="data">
 
846
<dt id="ssl.OP_NO_SSLv3">
 
847
<code class="descclassname">ssl.</code><code class="descname">OP_NO_SSLv3</code><a class="headerlink" href="#ssl.OP_NO_SSLv3" title="Permalink to this definition">¶</a></dt>
 
848
<dd><p>Prevents an SSLv3 connection.  This option is only applicable in
 
849
conjunction with <a class="reference internal" href="#ssl.PROTOCOL_SSLv23" title="ssl.PROTOCOL_SSLv23"><code class="xref py py-const docutils literal"><span class="pre">PROTOCOL_SSLv23</span></code></a>.  It prevents the peers from
 
850
choosing SSLv3 as the protocol version.</p>
 
851
<div class="versionadded">
 
852
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
853
</div>
 
854
</dd></dl>
 
855
 
 
856
<dl class="data">
 
857
<dt id="ssl.OP_NO_TLSv1">
 
858
<code class="descclassname">ssl.</code><code class="descname">OP_NO_TLSv1</code><a class="headerlink" href="#ssl.OP_NO_TLSv1" title="Permalink to this definition">¶</a></dt>
 
859
<dd><p>Prevents a TLSv1 connection.  This option is only applicable in
 
860
conjunction with <a class="reference internal" href="#ssl.PROTOCOL_SSLv23" title="ssl.PROTOCOL_SSLv23"><code class="xref py py-const docutils literal"><span class="pre">PROTOCOL_SSLv23</span></code></a>.  It prevents the peers from
 
861
choosing TLSv1 as the protocol version.</p>
 
862
<div class="versionadded">
 
863
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
864
</div>
 
865
</dd></dl>
 
866
 
 
867
<dl class="data">
 
868
<dt id="ssl.OP_NO_TLSv1_1">
 
869
<code class="descclassname">ssl.</code><code class="descname">OP_NO_TLSv1_1</code><a class="headerlink" href="#ssl.OP_NO_TLSv1_1" title="Permalink to this definition">¶</a></dt>
 
870
<dd><p>Prevents a TLSv1.1 connection. This option is only applicable in conjunction
 
871
with <a class="reference internal" href="#ssl.PROTOCOL_SSLv23" title="ssl.PROTOCOL_SSLv23"><code class="xref py py-const docutils literal"><span class="pre">PROTOCOL_SSLv23</span></code></a>. It prevents the peers from choosing TLSv1.1 as
 
872
the protocol version. Available only with openssl version 1.0.1+.</p>
 
873
<div class="versionadded">
 
874
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
875
</div>
 
876
</dd></dl>
 
877
 
 
878
<dl class="data">
 
879
<dt id="ssl.OP_NO_TLSv1_2">
 
880
<code class="descclassname">ssl.</code><code class="descname">OP_NO_TLSv1_2</code><a class="headerlink" href="#ssl.OP_NO_TLSv1_2" title="Permalink to this definition">¶</a></dt>
 
881
<dd><p>Prevents a TLSv1.2 connection. This option is only applicable in conjunction
 
882
with <a class="reference internal" href="#ssl.PROTOCOL_SSLv23" title="ssl.PROTOCOL_SSLv23"><code class="xref py py-const docutils literal"><span class="pre">PROTOCOL_SSLv23</span></code></a>. It prevents the peers from choosing TLSv1.2 as
 
883
the protocol version. Available only with openssl version 1.0.1+.</p>
 
884
<div class="versionadded">
 
885
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
886
</div>
 
887
</dd></dl>
 
888
 
 
889
<dl class="data">
 
890
<dt id="ssl.OP_CIPHER_SERVER_PREFERENCE">
 
891
<code class="descclassname">ssl.</code><code class="descname">OP_CIPHER_SERVER_PREFERENCE</code><a class="headerlink" href="#ssl.OP_CIPHER_SERVER_PREFERENCE" title="Permalink to this definition">¶</a></dt>
 
892
<dd><p>Use the server&#8217;s cipher ordering preference, rather than the client&#8217;s.
 
893
This option has no effect on client sockets and SSLv2 server sockets.</p>
 
894
<div class="versionadded">
 
895
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
896
</div>
 
897
</dd></dl>
 
898
 
 
899
<dl class="data">
 
900
<dt id="ssl.OP_SINGLE_DH_USE">
 
901
<code class="descclassname">ssl.</code><code class="descname">OP_SINGLE_DH_USE</code><a class="headerlink" href="#ssl.OP_SINGLE_DH_USE" title="Permalink to this definition">¶</a></dt>
 
902
<dd><p>Prevents re-use of the same DH key for distinct SSL sessions.  This
 
903
improves forward secrecy but requires more computational resources.
 
904
This option only applies to server sockets.</p>
 
905
<div class="versionadded">
 
906
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
907
</div>
 
908
</dd></dl>
 
909
 
 
910
<dl class="data">
 
911
<dt id="ssl.OP_SINGLE_ECDH_USE">
 
912
<code class="descclassname">ssl.</code><code class="descname">OP_SINGLE_ECDH_USE</code><a class="headerlink" href="#ssl.OP_SINGLE_ECDH_USE" title="Permalink to this definition">¶</a></dt>
 
913
<dd><p>Prevents re-use of the same ECDH key for distinct SSL sessions.  This
 
914
improves forward secrecy but requires more computational resources.
 
915
This option only applies to server sockets.</p>
 
916
<div class="versionadded">
 
917
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
918
</div>
 
919
</dd></dl>
 
920
 
 
921
<dl class="data">
 
922
<dt id="ssl.OP_NO_COMPRESSION">
 
923
<code class="descclassname">ssl.</code><code class="descname">OP_NO_COMPRESSION</code><a class="headerlink" href="#ssl.OP_NO_COMPRESSION" title="Permalink to this definition">¶</a></dt>
 
924
<dd><p>Disable compression on the SSL channel.  This is useful if the application
 
925
protocol supports its own compression scheme.</p>
 
926
<p>This option is only available with OpenSSL 1.0.0 and later.</p>
 
927
<div class="versionadded">
 
928
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
929
</div>
 
930
</dd></dl>
 
931
 
 
932
<dl class="data">
 
933
<dt id="ssl.HAS_ALPN">
 
934
<code class="descclassname">ssl.</code><code class="descname">HAS_ALPN</code><a class="headerlink" href="#ssl.HAS_ALPN" title="Permalink to this definition">¶</a></dt>
 
935
<dd><p>Whether the OpenSSL library has built-in support for the <em>Application-Layer
 
936
Protocol Negotiation</em> TLS extension as described in <span class="target" id="index-8"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc7301.html"><strong>RFC 7301</strong></a>.</p>
 
937
<div class="versionadded">
 
938
<p><span class="versionmodified">New in version 2.7.10.</span></p>
 
939
</div>
 
940
</dd></dl>
 
941
 
 
942
<dl class="data">
 
943
<dt id="ssl.HAS_ECDH">
 
944
<code class="descclassname">ssl.</code><code class="descname">HAS_ECDH</code><a class="headerlink" href="#ssl.HAS_ECDH" title="Permalink to this definition">¶</a></dt>
 
945
<dd><p>Whether the OpenSSL library has built-in support for Elliptic Curve-based
 
946
Diffie-Hellman key exchange.  This should be true unless the feature was
 
947
explicitly disabled by the distributor.</p>
 
948
<div class="versionadded">
 
949
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
950
</div>
 
951
</dd></dl>
 
952
 
 
953
<dl class="data">
 
954
<dt id="ssl.HAS_SNI">
 
955
<code class="descclassname">ssl.</code><code class="descname">HAS_SNI</code><a class="headerlink" href="#ssl.HAS_SNI" title="Permalink to this definition">¶</a></dt>
 
956
<dd><p>Whether the OpenSSL library has built-in support for the <em>Server Name
 
957
Indication</em> extension (as defined in <span class="target" id="index-9"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc4366.html"><strong>RFC 4366</strong></a>).</p>
 
958
<div class="versionadded">
 
959
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
960
</div>
 
961
</dd></dl>
 
962
 
 
963
<dl class="data">
 
964
<dt id="ssl.HAS_NPN">
 
965
<code class="descclassname">ssl.</code><code class="descname">HAS_NPN</code><a class="headerlink" href="#ssl.HAS_NPN" title="Permalink to this definition">¶</a></dt>
 
966
<dd><p>Whether the OpenSSL library has built-in support for <em>Next Protocol
 
967
Negotiation</em> as described in the <a class="reference external" href="https://tools.ietf.org/html/draft-agl-tls-nextprotoneg">NPN draft specification</a>. When true,
 
968
you can use the <a class="reference internal" href="#ssl.SSLContext.set_npn_protocols" title="ssl.SSLContext.set_npn_protocols"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.set_npn_protocols()</span></code></a> method to advertise
 
969
which protocols you want to support.</p>
 
970
<div class="versionadded">
 
971
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
972
</div>
 
973
</dd></dl>
 
974
 
 
975
<dl class="data">
 
976
<dt id="ssl.CHANNEL_BINDING_TYPES">
 
977
<code class="descclassname">ssl.</code><code class="descname">CHANNEL_BINDING_TYPES</code><a class="headerlink" href="#ssl.CHANNEL_BINDING_TYPES" title="Permalink to this definition">¶</a></dt>
 
978
<dd><p>List of supported TLS channel binding types.  Strings in this list
 
979
can be used as arguments to <a class="reference internal" href="#ssl.SSLSocket.get_channel_binding" title="ssl.SSLSocket.get_channel_binding"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.get_channel_binding()</span></code></a>.</p>
 
980
<div class="versionadded">
 
981
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
982
</div>
 
983
</dd></dl>
 
984
 
 
985
<dl class="data">
 
986
<dt id="ssl.OPENSSL_VERSION">
 
987
<code class="descclassname">ssl.</code><code class="descname">OPENSSL_VERSION</code><a class="headerlink" href="#ssl.OPENSSL_VERSION" title="Permalink to this definition">¶</a></dt>
 
988
<dd><p>The version string of the OpenSSL library loaded by the interpreter:</p>
 
989
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ssl</span><span class="o">.</span><span class="n">OPENSSL_VERSION</span>
 
990
<span class="go">&#39;OpenSSL 0.9.8k 25 Mar 2009&#39;</span>
 
991
</pre></div>
 
992
</div>
 
993
<div class="versionadded">
 
994
<p><span class="versionmodified">New in version 2.7.</span></p>
 
995
</div>
 
996
</dd></dl>
 
997
 
 
998
<dl class="data">
 
999
<dt id="ssl.OPENSSL_VERSION_INFO">
 
1000
<code class="descclassname">ssl.</code><code class="descname">OPENSSL_VERSION_INFO</code><a class="headerlink" href="#ssl.OPENSSL_VERSION_INFO" title="Permalink to this definition">¶</a></dt>
 
1001
<dd><p>A tuple of five integers representing version information about the
 
1002
OpenSSL library:</p>
 
1003
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ssl</span><span class="o">.</span><span class="n">OPENSSL_VERSION_INFO</span>
 
1004
<span class="go">(0, 9, 8, 11, 15)</span>
 
1005
</pre></div>
 
1006
</div>
 
1007
<div class="versionadded">
 
1008
<p><span class="versionmodified">New in version 2.7.</span></p>
 
1009
</div>
 
1010
</dd></dl>
 
1011
 
 
1012
<dl class="data">
 
1013
<dt id="ssl.OPENSSL_VERSION_NUMBER">
 
1014
<code class="descclassname">ssl.</code><code class="descname">OPENSSL_VERSION_NUMBER</code><a class="headerlink" href="#ssl.OPENSSL_VERSION_NUMBER" title="Permalink to this definition">¶</a></dt>
 
1015
<dd><p>The raw version number of the OpenSSL library, as a single integer:</p>
 
1016
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ssl</span><span class="o">.</span><span class="n">OPENSSL_VERSION_NUMBER</span>
 
1017
<span class="go">9470143L</span>
 
1018
<span class="gp">&gt;&gt;&gt; </span><span class="nb">hex</span><span class="p">(</span><span class="n">ssl</span><span class="o">.</span><span class="n">OPENSSL_VERSION_NUMBER</span><span class="p">)</span>
 
1019
<span class="go">&#39;0x9080bfL&#39;</span>
 
1020
</pre></div>
 
1021
</div>
 
1022
<div class="versionadded">
 
1023
<p><span class="versionmodified">New in version 2.7.</span></p>
 
1024
</div>
 
1025
</dd></dl>
 
1026
 
 
1027
<dl class="data">
 
1028
<dt id="ssl.ALERT_DESCRIPTION_HANDSHAKE_FAILURE">
 
1029
<code class="descclassname">ssl.</code><code class="descname">ALERT_DESCRIPTION_HANDSHAKE_FAILURE</code><a class="headerlink" href="#ssl.ALERT_DESCRIPTION_HANDSHAKE_FAILURE" title="Permalink to this definition">¶</a></dt>
 
1030
<dt id="ssl.ALERT_DESCRIPTION_INTERNAL_ERROR">
 
1031
<code class="descclassname">ssl.</code><code class="descname">ALERT_DESCRIPTION_INTERNAL_ERROR</code><a class="headerlink" href="#ssl.ALERT_DESCRIPTION_INTERNAL_ERROR" title="Permalink to this definition">¶</a></dt>
 
1032
<dt>
 
1033
<code class="descname">ALERT_DESCRIPTION_*</code></dt>
 
1034
<dd><p>Alert Descriptions from <span class="target" id="index-10"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc5246.html"><strong>RFC 5246</strong></a> and others. The <a class="reference external" href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6">IANA TLS Alert Registry</a>
 
1035
contains this list and references to the RFCs where their meaning is defined.</p>
 
1036
<p>Used as the return value of the callback function in
 
1037
<a class="reference internal" href="#ssl.SSLContext.set_servername_callback" title="ssl.SSLContext.set_servername_callback"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.set_servername_callback()</span></code></a>.</p>
 
1038
<div class="versionadded">
 
1039
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
1040
</div>
 
1041
</dd></dl>
 
1042
 
 
1043
<dl class="data">
 
1044
<dt id="ssl.Purpose.SERVER_AUTH">
 
1045
<code class="descclassname">Purpose.</code><code class="descname">SERVER_AUTH</code><a class="headerlink" href="#ssl.Purpose.SERVER_AUTH" title="Permalink to this definition">¶</a></dt>
 
1046
<dd><p>Option for <a class="reference internal" href="#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal"><span class="pre">create_default_context()</span></code></a> and
 
1047
<a class="reference internal" href="#ssl.SSLContext.load_default_certs" title="ssl.SSLContext.load_default_certs"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.load_default_certs()</span></code></a>.  This value indicates that the
 
1048
context may be used to authenticate Web servers (therefore, it will
 
1049
be used to create client-side sockets).</p>
 
1050
<div class="versionadded">
 
1051
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
1052
</div>
 
1053
</dd></dl>
 
1054
 
 
1055
<dl class="data">
 
1056
<dt id="ssl.Purpose.CLIENT_AUTH">
 
1057
<code class="descclassname">Purpose.</code><code class="descname">CLIENT_AUTH</code><a class="headerlink" href="#ssl.Purpose.CLIENT_AUTH" title="Permalink to this definition">¶</a></dt>
 
1058
<dd><p>Option for <a class="reference internal" href="#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal"><span class="pre">create_default_context()</span></code></a> and
 
1059
<a class="reference internal" href="#ssl.SSLContext.load_default_certs" title="ssl.SSLContext.load_default_certs"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.load_default_certs()</span></code></a>.  This value indicates that the
 
1060
context may be used to authenticate Web clients (therefore, it will
 
1061
be used to create server-side sockets).</p>
 
1062
<div class="versionadded">
 
1063
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
1064
</div>
 
1065
</dd></dl>
 
1066
 
 
1067
</div>
 
1068
</div>
 
1069
<div class="section" id="ssl-sockets">
 
1070
<h2>17.3.2. SSL Sockets<a class="headerlink" href="#ssl-sockets" title="Permalink to this headline">¶</a></h2>
 
1071
<p>SSL sockets provide the following methods of <a class="reference internal" href="socket.html#socket-objects"><span>Socket Objects</span></a>:</p>
 
1072
<ul class="simple">
 
1073
<li><a class="reference internal" href="socket.html#socket.socket.accept" title="socket.socket.accept"><code class="xref py py-meth docutils literal"><span class="pre">accept()</span></code></a></li>
 
1074
<li><a class="reference internal" href="socket.html#socket.socket.bind" title="socket.socket.bind"><code class="xref py py-meth docutils literal"><span class="pre">bind()</span></code></a></li>
 
1075
<li><a class="reference internal" href="socket.html#socket.socket.close" title="socket.socket.close"><code class="xref py py-meth docutils literal"><span class="pre">close()</span></code></a></li>
 
1076
<li><a class="reference internal" href="socket.html#socket.socket.connect" title="socket.socket.connect"><code class="xref py py-meth docutils literal"><span class="pre">connect()</span></code></a></li>
 
1077
<li><a class="reference internal" href="socket.html#socket.socket.fileno" title="socket.socket.fileno"><code class="xref py py-meth docutils literal"><span class="pre">fileno()</span></code></a></li>
 
1078
<li><a class="reference internal" href="socket.html#socket.socket.getpeername" title="socket.socket.getpeername"><code class="xref py py-meth docutils literal"><span class="pre">getpeername()</span></code></a>, <a class="reference internal" href="socket.html#socket.socket.getsockname" title="socket.socket.getsockname"><code class="xref py py-meth docutils literal"><span class="pre">getsockname()</span></code></a></li>
 
1079
<li><a class="reference internal" href="socket.html#socket.socket.getsockopt" title="socket.socket.getsockopt"><code class="xref py py-meth docutils literal"><span class="pre">getsockopt()</span></code></a>, <a class="reference internal" href="socket.html#socket.socket.setsockopt" title="socket.socket.setsockopt"><code class="xref py py-meth docutils literal"><span class="pre">setsockopt()</span></code></a></li>
 
1080
<li><a class="reference internal" href="socket.html#socket.socket.gettimeout" title="socket.socket.gettimeout"><code class="xref py py-meth docutils literal"><span class="pre">gettimeout()</span></code></a>, <a class="reference internal" href="socket.html#socket.socket.settimeout" title="socket.socket.settimeout"><code class="xref py py-meth docutils literal"><span class="pre">settimeout()</span></code></a>,
 
1081
<a class="reference internal" href="socket.html#socket.socket.setblocking" title="socket.socket.setblocking"><code class="xref py py-meth docutils literal"><span class="pre">setblocking()</span></code></a></li>
 
1082
<li><a class="reference internal" href="socket.html#socket.socket.listen" title="socket.socket.listen"><code class="xref py py-meth docutils literal"><span class="pre">listen()</span></code></a></li>
 
1083
<li><a class="reference internal" href="socket.html#socket.socket.makefile" title="socket.socket.makefile"><code class="xref py py-meth docutils literal"><span class="pre">makefile()</span></code></a></li>
 
1084
<li><a class="reference internal" href="socket.html#socket.socket.recv" title="socket.socket.recv"><code class="xref py py-meth docutils literal"><span class="pre">recv()</span></code></a>, <a class="reference internal" href="socket.html#socket.socket.recv_into" title="socket.socket.recv_into"><code class="xref py py-meth docutils literal"><span class="pre">recv_into()</span></code></a>
 
1085
(but passing a non-zero <code class="docutils literal"><span class="pre">flags</span></code> argument is not allowed)</li>
 
1086
<li><a class="reference internal" href="socket.html#socket.socket.send" title="socket.socket.send"><code class="xref py py-meth docutils literal"><span class="pre">send()</span></code></a>, <a class="reference internal" href="socket.html#socket.socket.sendall" title="socket.socket.sendall"><code class="xref py py-meth docutils literal"><span class="pre">sendall()</span></code></a> (with
 
1087
the same limitation)</li>
 
1088
<li><a class="reference internal" href="socket.html#socket.socket.shutdown" title="socket.socket.shutdown"><code class="xref py py-meth docutils literal"><span class="pre">shutdown()</span></code></a></li>
 
1089
</ul>
 
1090
<p>However, since the SSL (and TLS) protocol has its own framing atop
 
1091
of TCP, the SSL sockets abstraction can, in certain respects, diverge from
 
1092
the specification of normal, OS-level sockets.  See especially the
 
1093
<a class="reference internal" href="#ssl-nonblocking"><span>notes on non-blocking sockets</span></a>.</p>
 
1094
<p>SSL sockets also have the following additional methods and attributes:</p>
 
1095
<dl class="method">
 
1096
<dt id="ssl.SSLSocket.do_handshake">
 
1097
<code class="descclassname">SSLSocket.</code><code class="descname">do_handshake</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.do_handshake" title="Permalink to this definition">¶</a></dt>
 
1098
<dd><p>Perform the SSL setup handshake.</p>
 
1099
<div class="versionchanged">
 
1100
<p><span class="versionmodified">Changed in version 2.7.9: </span>The handshake method also performs <a class="reference internal" href="#ssl.match_hostname" title="ssl.match_hostname"><code class="xref py py-func docutils literal"><span class="pre">match_hostname()</span></code></a> when the
 
1101
<a class="reference internal" href="#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal"><span class="pre">check_hostname</span></code></a> attribute of the socket&#8217;s
 
1102
<a class="reference internal" href="#ssl.SSLSocket.context" title="ssl.SSLSocket.context"><code class="xref py py-attr docutils literal"><span class="pre">context</span></code></a> is true.</p>
 
1103
</div>
 
1104
</dd></dl>
 
1105
 
 
1106
<dl class="method">
 
1107
<dt id="ssl.SSLSocket.getpeercert">
 
1108
<code class="descclassname">SSLSocket.</code><code class="descname">getpeercert</code><span class="sig-paren">(</span><em>binary_form=False</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.getpeercert" title="Permalink to this definition">¶</a></dt>
 
1109
<dd><p>If there is no certificate for the peer on the other end of the connection,
 
1110
return <code class="docutils literal"><span class="pre">None</span></code>.  If the SSL handshake hasn&#8217;t been done yet, raise
 
1111
<a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a>.</p>
 
1112
<p>If the <code class="docutils literal"><span class="pre">binary_form</span></code> parameter is <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>, and a certificate was
 
1113
received from the peer, this method returns a <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal"><span class="pre">dict</span></code></a> instance.  If the
 
1114
certificate was not validated, the dict is empty.  If the certificate was
 
1115
validated, it returns a dict with several keys, amongst them <code class="docutils literal"><span class="pre">subject</span></code>
 
1116
(the principal for which the certificate was issued) and <code class="docutils literal"><span class="pre">issuer</span></code>
 
1117
(the principal issuing the certificate).  If a certificate contains an
 
1118
instance of the <em>Subject Alternative Name</em> extension (see <span class="target" id="index-11"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3280.html"><strong>RFC 3280</strong></a>),
 
1119
there will also be a <code class="docutils literal"><span class="pre">subjectAltName</span></code> key in the dictionary.</p>
 
1120
<p>The <code class="docutils literal"><span class="pre">subject</span></code> and <code class="docutils literal"><span class="pre">issuer</span></code> fields are tuples containing the sequence
 
1121
of relative distinguished names (RDNs) given in the certificate&#8217;s data
 
1122
structure for the respective fields, and each RDN is a sequence of
 
1123
name-value pairs.  Here is a real-world example:</p>
 
1124
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="p">{</span><span class="s1">&#39;issuer&#39;</span><span class="p">:</span> <span class="p">(((</span><span class="s1">&#39;countryName&#39;</span><span class="p">,</span> <span class="s1">&#39;IL&#39;</span><span class="p">),),</span>
 
1125
            <span class="p">((</span><span class="s1">&#39;organizationName&#39;</span><span class="p">,</span> <span class="s1">&#39;StartCom Ltd.&#39;</span><span class="p">),),</span>
 
1126
            <span class="p">((</span><span class="s1">&#39;organizationalUnitName&#39;</span><span class="p">,</span>
 
1127
              <span class="s1">&#39;Secure Digital Certificate Signing&#39;</span><span class="p">),),</span>
 
1128
            <span class="p">((</span><span class="s1">&#39;commonName&#39;</span><span class="p">,</span>
 
1129
              <span class="s1">&#39;StartCom Class 2 Primary Intermediate Server CA&#39;</span><span class="p">),)),</span>
 
1130
 <span class="s1">&#39;notAfter&#39;</span><span class="p">:</span> <span class="s1">&#39;Nov 22 08:15:19 2013 GMT&#39;</span><span class="p">,</span>
 
1131
 <span class="s1">&#39;notBefore&#39;</span><span class="p">:</span> <span class="s1">&#39;Nov 21 03:09:52 2011 GMT&#39;</span><span class="p">,</span>
 
1132
 <span class="s1">&#39;serialNumber&#39;</span><span class="p">:</span> <span class="s1">&#39;95F0&#39;</span><span class="p">,</span>
 
1133
 <span class="s1">&#39;subject&#39;</span><span class="p">:</span> <span class="p">(((</span><span class="s1">&#39;description&#39;</span><span class="p">,</span> <span class="s1">&#39;571208-SLe257oHY9fVQ07Z&#39;</span><span class="p">),),</span>
 
1134
             <span class="p">((</span><span class="s1">&#39;countryName&#39;</span><span class="p">,</span> <span class="s1">&#39;US&#39;</span><span class="p">),),</span>
 
1135
             <span class="p">((</span><span class="s1">&#39;stateOrProvinceName&#39;</span><span class="p">,</span> <span class="s1">&#39;California&#39;</span><span class="p">),),</span>
 
1136
             <span class="p">((</span><span class="s1">&#39;localityName&#39;</span><span class="p">,</span> <span class="s1">&#39;San Francisco&#39;</span><span class="p">),),</span>
 
1137
             <span class="p">((</span><span class="s1">&#39;organizationName&#39;</span><span class="p">,</span> <span class="s1">&#39;Electronic Frontier Foundation, Inc.&#39;</span><span class="p">),),</span>
 
1138
             <span class="p">((</span><span class="s1">&#39;commonName&#39;</span><span class="p">,</span> <span class="s1">&#39;*.eff.org&#39;</span><span class="p">),),</span>
 
1139
             <span class="p">((</span><span class="s1">&#39;emailAddress&#39;</span><span class="p">,</span> <span class="s1">&#39;hostmaster@eff.org&#39;</span><span class="p">),)),</span>
 
1140
 <span class="s1">&#39;subjectAltName&#39;</span><span class="p">:</span> <span class="p">((</span><span class="s1">&#39;DNS&#39;</span><span class="p">,</span> <span class="s1">&#39;*.eff.org&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;DNS&#39;</span><span class="p">,</span> <span class="s1">&#39;eff.org&#39;</span><span class="p">)),</span>
 
1141
 <span class="s1">&#39;version&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">}</span>
 
1142
</pre></div>
 
1143
</div>
 
1144
<div class="admonition note">
 
1145
<p class="first admonition-title">Note</p>
 
1146
<p class="last">To validate a certificate for a particular service, you can use the
 
1147
<a class="reference internal" href="#ssl.match_hostname" title="ssl.match_hostname"><code class="xref py py-func docutils literal"><span class="pre">match_hostname()</span></code></a> function.</p>
 
1148
</div>
 
1149
<p>If the <code class="docutils literal"><span class="pre">binary_form</span></code> parameter is <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>, and a certificate was
 
1150
provided, this method returns the DER-encoded form of the entire certificate
 
1151
as a sequence of bytes, or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a> if the peer did not provide a
 
1152
certificate.  Whether the peer provides a certificate depends on the SSL
 
1153
socket&#8217;s role:</p>
 
1154
<ul class="simple">
 
1155
<li>for a client SSL socket, the server will always provide a certificate,
 
1156
regardless of whether validation was required;</li>
 
1157
<li>for a server SSL socket, the client will only provide a certificate
 
1158
when requested by the server; therefore <a class="reference internal" href="#ssl.SSLSocket.getpeercert" title="ssl.SSLSocket.getpeercert"><code class="xref py py-meth docutils literal"><span class="pre">getpeercert()</span></code></a> will return
 
1159
<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a> if you used <a class="reference internal" href="#ssl.CERT_NONE" title="ssl.CERT_NONE"><code class="xref py py-const docutils literal"><span class="pre">CERT_NONE</span></code></a> (rather than
 
1160
<a class="reference internal" href="#ssl.CERT_OPTIONAL" title="ssl.CERT_OPTIONAL"><code class="xref py py-const docutils literal"><span class="pre">CERT_OPTIONAL</span></code></a> or <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-const docutils literal"><span class="pre">CERT_REQUIRED</span></code></a>).</li>
 
1161
</ul>
 
1162
<div class="versionchanged">
 
1163
<p><span class="versionmodified">Changed in version 2.7.9: </span>The returned dictionary includes additional items such as <code class="docutils literal"><span class="pre">issuer</span></code> and
 
1164
<code class="docutils literal"><span class="pre">notBefore</span></code>. Additionall <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> is raised when the handshake
 
1165
isn&#8217;t done. The returned dictionary includes additional X509v3 extension
 
1166
items such as <code class="docutils literal"><span class="pre">crlDistributionPoints</span></code>, <code class="docutils literal"><span class="pre">caIssuers</span></code> and <code class="docutils literal"><span class="pre">OCSP</span></code> URIs.</p>
 
1167
</div>
 
1168
</dd></dl>
 
1169
 
 
1170
<dl class="method">
 
1171
<dt id="ssl.SSLSocket.cipher">
 
1172
<code class="descclassname">SSLSocket.</code><code class="descname">cipher</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.cipher" title="Permalink to this definition">¶</a></dt>
 
1173
<dd><p>Returns a three-value tuple containing the name of the cipher being used, the
 
1174
version of the SSL protocol that defines its use, and the number of secret
 
1175
bits being used.  If no connection has been established, returns <code class="docutils literal"><span class="pre">None</span></code>.</p>
 
1176
</dd></dl>
 
1177
 
 
1178
<dl class="method">
 
1179
<dt id="ssl.SSLSocket.compression">
 
1180
<code class="descclassname">SSLSocket.</code><code class="descname">compression</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.compression" title="Permalink to this definition">¶</a></dt>
 
1181
<dd><p>Return the compression algorithm being used as a string, or <code class="docutils literal"><span class="pre">None</span></code>
 
1182
if the connection isn&#8217;t compressed.</p>
 
1183
<p>If the higher-level protocol supports its own compression mechanism,
 
1184
you can use <a class="reference internal" href="#ssl.OP_NO_COMPRESSION" title="ssl.OP_NO_COMPRESSION"><code class="xref py py-data docutils literal"><span class="pre">OP_NO_COMPRESSION</span></code></a> to disable SSL-level compression.</p>
 
1185
<div class="versionadded">
 
1186
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
1187
</div>
 
1188
</dd></dl>
 
1189
 
 
1190
<dl class="method">
 
1191
<dt id="ssl.SSLSocket.get_channel_binding">
 
1192
<code class="descclassname">SSLSocket.</code><code class="descname">get_channel_binding</code><span class="sig-paren">(</span><em>cb_type=&quot;tls-unique&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.get_channel_binding" title="Permalink to this definition">¶</a></dt>
 
1193
<dd><p>Get channel binding data for current connection, as a bytes object.  Returns
 
1194
<code class="docutils literal"><span class="pre">None</span></code> if not connected or the handshake has not been completed.</p>
 
1195
<p>The <em>cb_type</em> parameter allow selection of the desired channel binding
 
1196
type. Valid channel binding types are listed in the
 
1197
<a class="reference internal" href="#ssl.CHANNEL_BINDING_TYPES" title="ssl.CHANNEL_BINDING_TYPES"><code class="xref py py-data docutils literal"><span class="pre">CHANNEL_BINDING_TYPES</span></code></a> list.  Currently only the &#8216;tls-unique&#8217; channel
 
1198
binding, defined by <span class="target" id="index-12"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc5929.html"><strong>RFC 5929</strong></a>, is supported.  <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> will be
 
1199
raised if an unsupported channel binding type is requested.</p>
 
1200
<div class="versionadded">
 
1201
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
1202
</div>
 
1203
</dd></dl>
 
1204
 
 
1205
<dl class="method">
 
1206
<dt id="ssl.SSLSocket.selected_alpn_protocol">
 
1207
<code class="descclassname">SSLSocket.</code><code class="descname">selected_alpn_protocol</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.selected_alpn_protocol" title="Permalink to this definition">¶</a></dt>
 
1208
<dd><p>Return the protocol that was selected during the TLS handshake.  If
 
1209
<a class="reference internal" href="#ssl.SSLContext.set_alpn_protocols" title="ssl.SSLContext.set_alpn_protocols"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.set_alpn_protocols()</span></code></a> was not called, if the other party does
 
1210
not support ALPN, if this socket does not support any of the client&#8217;s
 
1211
proposed protocols, or if the handshake has not happened yet, <code class="docutils literal"><span class="pre">None</span></code> is
 
1212
returned.</p>
 
1213
<div class="versionadded">
 
1214
<p><span class="versionmodified">New in version 2.7.10.</span></p>
 
1215
</div>
 
1216
</dd></dl>
 
1217
 
 
1218
<dl class="method">
 
1219
<dt id="ssl.SSLSocket.selected_npn_protocol">
 
1220
<code class="descclassname">SSLSocket.</code><code class="descname">selected_npn_protocol</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.selected_npn_protocol" title="Permalink to this definition">¶</a></dt>
 
1221
<dd><p>Return the higher-level protocol that was selected during the TLS/SSL
 
1222
handshake. If <a class="reference internal" href="#ssl.SSLContext.set_npn_protocols" title="ssl.SSLContext.set_npn_protocols"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.set_npn_protocols()</span></code></a> was not called, or
 
1223
if the other party does not support NPN, or if the handshake has not yet
 
1224
happened, this will return <code class="docutils literal"><span class="pre">None</span></code>.</p>
 
1225
<div class="versionadded">
 
1226
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
1227
</div>
 
1228
</dd></dl>
 
1229
 
 
1230
<dl class="method">
 
1231
<dt id="ssl.SSLSocket.unwrap">
 
1232
<code class="descclassname">SSLSocket.</code><code class="descname">unwrap</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.unwrap" title="Permalink to this definition">¶</a></dt>
 
1233
<dd><p>Performs the SSL shutdown handshake, which removes the TLS layer from the
 
1234
underlying socket, and returns the underlying socket object.  This can be
 
1235
used to go from encrypted operation over a connection to unencrypted.  The
 
1236
returned socket should always be used for further communication with the
 
1237
other side of the connection, rather than the original socket.</p>
 
1238
</dd></dl>
 
1239
 
 
1240
<dl class="method">
 
1241
<dt id="ssl.SSLSocket.version">
 
1242
<code class="descclassname">SSLSocket.</code><code class="descname">version</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.version" title="Permalink to this definition">¶</a></dt>
 
1243
<dd><p>Return the actual SSL protocol version negotiated by the connection
 
1244
as a string, or <code class="docutils literal"><span class="pre">None</span></code> is no secure connection is established.
 
1245
As of this writing, possible return values include <code class="docutils literal"><span class="pre">&quot;SSLv2&quot;</span></code>,
 
1246
<code class="docutils literal"><span class="pre">&quot;SSLv3&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;TLSv1&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;TLSv1.1&quot;</span></code> and <code class="docutils literal"><span class="pre">&quot;TLSv1.2&quot;</span></code>.
 
1247
Recent OpenSSL versions may define more return values.</p>
 
1248
<div class="versionadded">
 
1249
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
1250
</div>
 
1251
</dd></dl>
 
1252
 
 
1253
<dl class="attribute">
 
1254
<dt id="ssl.SSLSocket.context">
 
1255
<code class="descclassname">SSLSocket.</code><code class="descname">context</code><a class="headerlink" href="#ssl.SSLSocket.context" title="Permalink to this definition">¶</a></dt>
 
1256
<dd><p>The <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">SSLContext</span></code></a> object this SSL socket is tied to.  If the SSL
 
1257
socket was created using the top-level <a class="reference internal" href="#ssl.wrap_socket" title="ssl.wrap_socket"><code class="xref py py-func docutils literal"><span class="pre">wrap_socket()</span></code></a> function
 
1258
(rather than <a class="reference internal" href="#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.wrap_socket()</span></code></a>), this is a custom context
 
1259
object created for this SSL socket.</p>
 
1260
<div class="versionadded">
 
1261
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
1262
</div>
 
1263
</dd></dl>
 
1264
 
 
1265
</div>
 
1266
<div class="section" id="ssl-contexts">
 
1267
<h2>17.3.3. SSL Contexts<a class="headerlink" href="#ssl-contexts" title="Permalink to this headline">¶</a></h2>
 
1268
<div class="versionadded">
 
1269
<p><span class="versionmodified">New in version 2.7.9.</span></p>
 
1270
</div>
 
1271
<p>An SSL context holds various data longer-lived than single SSL connections,
 
1272
such as SSL configuration options, certificate(s) and private key(s).
 
1273
It also manages a cache of SSL sessions for server-side sockets, in order
 
1274
to speed up repeated connections from the same clients.</p>
 
1275
<dl class="class">
 
1276
<dt id="ssl.SSLContext">
 
1277
<em class="property">class </em><code class="descclassname">ssl.</code><code class="descname">SSLContext</code><span class="sig-paren">(</span><em>protocol</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext" title="Permalink to this definition">¶</a></dt>
 
1278
<dd><p>Create a new SSL context.  You must pass <em>protocol</em> which must be one
 
1279
of the <code class="docutils literal"><span class="pre">PROTOCOL_*</span></code> constants defined in this module.
 
1280
<a class="reference internal" href="#ssl.PROTOCOL_SSLv23" title="ssl.PROTOCOL_SSLv23"><code class="xref py py-data docutils literal"><span class="pre">PROTOCOL_SSLv23</span></code></a> is currently recommended for maximum
 
1281
interoperability.</p>
 
1282
<div class="admonition seealso">
 
1283
<p class="first admonition-title">See also</p>
 
1284
<p class="last"><a class="reference internal" href="#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal"><span class="pre">create_default_context()</span></code></a> lets the <a class="reference internal" href="#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal"><span class="pre">ssl</span></code></a> module choose
 
1285
security settings for a given purpose.</p>
 
1286
</div>
 
1287
</dd></dl>
 
1288
 
 
1289
<p><a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">SSLContext</span></code></a> objects have the following methods and attributes:</p>
 
1290
<dl class="method">
 
1291
<dt id="ssl.SSLContext.cert_store_stats">
 
1292
<code class="descclassname">SSLContext.</code><code class="descname">cert_store_stats</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.cert_store_stats" title="Permalink to this definition">¶</a></dt>
 
1293
<dd><p>Get statistics about quantities of loaded X.509 certificates, count of
 
1294
X.509 certificates flagged as CA certificates and certificate revocation
 
1295
lists as dictionary.</p>
 
1296
<p>Example for a context with one CA cert and one other cert:</p>
 
1297
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">context</span><span class="o">.</span><span class="n">cert_store_stats</span><span class="p">()</span>
 
1298
<span class="go">{&#39;crl&#39;: 0, &#39;x509_ca&#39;: 1, &#39;x509&#39;: 2}</span>
 
1299
</pre></div>
 
1300
</div>
 
1301
</dd></dl>
 
1302
 
 
1303
<dl class="method">
 
1304
<dt id="ssl.SSLContext.load_cert_chain">
 
1305
<code class="descclassname">SSLContext.</code><code class="descname">load_cert_chain</code><span class="sig-paren">(</span><em>certfile</em>, <em>keyfile=None</em>, <em>password=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.load_cert_chain" title="Permalink to this definition">¶</a></dt>
 
1306
<dd><p>Load a private key and the corresponding certificate.  The <em>certfile</em>
 
1307
string must be the path to a single file in PEM format containing the
 
1308
certificate as well as any number of CA certificates needed to establish
 
1309
the certificate&#8217;s authenticity.  The <em>keyfile</em> string, if present, must
 
1310
point to a file containing the private key in.  Otherwise the private
 
1311
key will be taken from <em>certfile</em> as well.  See the discussion of
 
1312
<a class="reference internal" href="#ssl-certificates"><span>Certificates</span></a> for more information on how the certificate
 
1313
is stored in the <em>certfile</em>.</p>
 
1314
<p>The <em>password</em> argument may be a function to call to get the password for
 
1315
decrypting the private key.  It will only be called if the private key is
 
1316
encrypted and a password is necessary.  It will be called with no arguments,
 
1317
and it should return a string, bytes, or bytearray.  If the return value is
 
1318
a string it will be encoded as UTF-8 before using it to decrypt the key.
 
1319
Alternatively a string, bytes, or bytearray value may be supplied directly
 
1320
as the <em>password</em> argument.  It will be ignored if the private key is not
 
1321
encrypted and no password is needed.</p>
 
1322
<p>If the <em>password</em> argument is not specified and a password is required,
 
1323
OpenSSL&#8217;s built-in password prompting mechanism will be used to
 
1324
interactively prompt the user for a password.</p>
 
1325
<p>An <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-class docutils literal"><span class="pre">SSLError</span></code></a> is raised if the private key doesn&#8217;t
 
1326
match with the certificate.</p>
 
1327
</dd></dl>
 
1328
 
 
1329
<dl class="method">
 
1330
<dt id="ssl.SSLContext.load_default_certs">
 
1331
<code class="descclassname">SSLContext.</code><code class="descname">load_default_certs</code><span class="sig-paren">(</span><em>purpose=Purpose.SERVER_AUTH</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.load_default_certs" title="Permalink to this definition">¶</a></dt>
 
1332
<dd><p>Load a set of default &#8220;certification authority&#8221; (CA) certificates from
 
1333
default locations. On Windows it loads CA certs from the <code class="docutils literal"><span class="pre">CA</span></code> and
 
1334
<code class="docutils literal"><span class="pre">ROOT</span></code> system stores. On other systems it calls
 
1335
<a class="reference internal" href="#ssl.SSLContext.set_default_verify_paths" title="ssl.SSLContext.set_default_verify_paths"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.set_default_verify_paths()</span></code></a>. In the future the method may
 
1336
load CA certificates from other locations, too.</p>
 
1337
<p>The <em>purpose</em> flag specifies what kind of CA certificates are loaded. The
 
1338
default settings <a class="reference internal" href="#ssl.Purpose.SERVER_AUTH" title="ssl.Purpose.SERVER_AUTH"><code class="xref py py-data docutils literal"><span class="pre">Purpose.SERVER_AUTH</span></code></a> loads certificates, that are
 
1339
flagged and trusted for TLS web server authentication (client side
 
1340
sockets). <a class="reference internal" href="#ssl.Purpose.CLIENT_AUTH" title="ssl.Purpose.CLIENT_AUTH"><code class="xref py py-data docutils literal"><span class="pre">Purpose.CLIENT_AUTH</span></code></a> loads CA certificates for client
 
1341
certificate verification on the server side.</p>
 
1342
</dd></dl>
 
1343
 
 
1344
<dl class="method">
 
1345
<dt id="ssl.SSLContext.load_verify_locations">
 
1346
<code class="descclassname">SSLContext.</code><code class="descname">load_verify_locations</code><span class="sig-paren">(</span><em>cafile=None</em>, <em>capath=None</em>, <em>cadata=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.load_verify_locations" title="Permalink to this definition">¶</a></dt>
 
1347
<dd><p>Load a set of &#8220;certification authority&#8221; (CA) certificates used to validate
 
1348
other peers&#8217; certificates when <a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-data docutils literal"><span class="pre">verify_mode</span></code></a> is other than
 
1349
<a class="reference internal" href="#ssl.CERT_NONE" title="ssl.CERT_NONE"><code class="xref py py-data docutils literal"><span class="pre">CERT_NONE</span></code></a>.  At least one of <em>cafile</em> or <em>capath</em> must be specified.</p>
 
1350
<p>This method can also load certification revocation lists (CRLs) in PEM or
 
1351
DER format. In order to make use of CRLs, <a class="reference internal" href="#ssl.SSLContext.verify_flags" title="ssl.SSLContext.verify_flags"><code class="xref py py-attr docutils literal"><span class="pre">SSLContext.verify_flags</span></code></a>
 
1352
must be configured properly.</p>
 
1353
<p>The <em>cafile</em> string, if present, is the path to a file of concatenated
 
1354
CA certificates in PEM format. See the discussion of
 
1355
<a class="reference internal" href="#ssl-certificates"><span>Certificates</span></a> for more information about how to arrange the
 
1356
certificates in this file.</p>
 
1357
<p>The <em>capath</em> string, if present, is
 
1358
the path to a directory containing several CA certificates in PEM format,
 
1359
following an <a class="reference external" href="https://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html">OpenSSL specific layout</a>.</p>
 
1360
<p>The <em>cadata</em> object, if present, is either an ASCII string of one or more
 
1361
PEM-encoded certificates or a bytes-like object of DER-encoded
 
1362
certificates. Like with <em>capath</em> extra lines around PEM-encoded
 
1363
certificates are ignored but at least one certificate must be present.</p>
 
1364
</dd></dl>
 
1365
 
 
1366
<dl class="method">
 
1367
<dt id="ssl.SSLContext.get_ca_certs">
 
1368
<code class="descclassname">SSLContext.</code><code class="descname">get_ca_certs</code><span class="sig-paren">(</span><em>binary_form=False</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.get_ca_certs" title="Permalink to this definition">¶</a></dt>
 
1369
<dd><p>Get a list of loaded &#8220;certification authority&#8221; (CA) certificates. If the
 
1370
<code class="docutils literal"><span class="pre">binary_form</span></code> parameter is <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> each list
 
1371
entry is a dict like the output of <a class="reference internal" href="#ssl.SSLSocket.getpeercert" title="ssl.SSLSocket.getpeercert"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.getpeercert()</span></code></a>. Otherwise
 
1372
the method returns a list of DER-encoded certificates. The returned list
 
1373
does not contain certificates from <em>capath</em> unless a certificate was
 
1374
requested and loaded by a SSL connection.</p>
 
1375
</dd></dl>
 
1376
 
 
1377
<dl class="method">
 
1378
<dt id="ssl.SSLContext.set_default_verify_paths">
 
1379
<code class="descclassname">SSLContext.</code><code class="descname">set_default_verify_paths</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.set_default_verify_paths" title="Permalink to this definition">¶</a></dt>
 
1380
<dd><p>Load a set of default &#8220;certification authority&#8221; (CA) certificates from
 
1381
a filesystem path defined when building the OpenSSL library.  Unfortunately,
 
1382
there&#8217;s no easy way to know whether this method succeeds: no error is
 
1383
returned if no certificates are to be found.  When the OpenSSL library is
 
1384
provided as part of the operating system, though, it is likely to be
 
1385
configured properly.</p>
 
1386
</dd></dl>
 
1387
 
 
1388
<dl class="method">
 
1389
<dt id="ssl.SSLContext.set_ciphers">
 
1390
<code class="descclassname">SSLContext.</code><code class="descname">set_ciphers</code><span class="sig-paren">(</span><em>ciphers</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.set_ciphers" title="Permalink to this definition">¶</a></dt>
 
1391
<dd><p>Set the available ciphers for sockets created with this context.
 
1392
It should be a string in the <a class="reference external" href="https://www.openssl.org/docs/apps/ciphers.html#CIPHER-LIST-FORMAT">OpenSSL cipher list format</a>.
 
1393
If no cipher can be selected (because compile-time options or other
 
1394
configuration forbids use of all the specified ciphers), an
 
1395
<a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-class docutils literal"><span class="pre">SSLError</span></code></a> will be raised.</p>
 
1396
<div class="admonition note">
 
1397
<p class="first admonition-title">Note</p>
 
1398
<p class="last">when connected, the <a class="reference internal" href="#ssl.SSLSocket.cipher" title="ssl.SSLSocket.cipher"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.cipher()</span></code></a> method of SSL sockets will
 
1399
give the currently selected cipher.</p>
 
1400
</div>
 
1401
</dd></dl>
 
1402
 
 
1403
<dl class="method">
 
1404
<dt id="ssl.SSLContext.set_alpn_protocols">
 
1405
<code class="descclassname">SSLContext.</code><code class="descname">set_alpn_protocols</code><span class="sig-paren">(</span><em>protocols</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.set_alpn_protocols" title="Permalink to this definition">¶</a></dt>
 
1406
<dd><p>Specify which protocols the socket should advertise during the SSL/TLS
 
1407
handshake. It should be a list of ASCII strings, like <code class="docutils literal"><span class="pre">['http/1.1',</span>
 
1408
<span class="pre">'spdy/2']</span></code>, ordered by preference. The selection of a protocol will happen
 
1409
during the handshake, and will play out according to <span class="target" id="index-13"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc7301.html"><strong>RFC 7301</strong></a>. After a
 
1410
successful handshake, the <a class="reference internal" href="#ssl.SSLSocket.selected_alpn_protocol" title="ssl.SSLSocket.selected_alpn_protocol"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.selected_alpn_protocol()</span></code></a> method will
 
1411
return the agreed-upon protocol.</p>
 
1412
<p>This method will raise <a class="reference internal" href="exceptions.html#exceptions.NotImplementedError" title="exceptions.NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a> if <a class="reference internal" href="#ssl.HAS_ALPN" title="ssl.HAS_ALPN"><code class="xref py py-data docutils literal"><span class="pre">HAS_ALPN</span></code></a> is
 
1413
False.</p>
 
1414
<p>OpenSSL 1.1.0+ will abort the handshake and raise <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal"><span class="pre">SSLError</span></code></a> when
 
1415
both sides support ALPN but cannot agree on a protocol.</p>
 
1416
<div class="versionadded">
 
1417
<p><span class="versionmodified">New in version 2.7.10.</span></p>
 
1418
</div>
 
1419
</dd></dl>
 
1420
 
 
1421
<dl class="method">
 
1422
<dt id="ssl.SSLContext.set_npn_protocols">
 
1423
<code class="descclassname">SSLContext.</code><code class="descname">set_npn_protocols</code><span class="sig-paren">(</span><em>protocols</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.set_npn_protocols" title="Permalink to this definition">¶</a></dt>
 
1424
<dd><p>Specify which protocols the socket should advertise during the SSL/TLS
 
1425
handshake. It should be a list of strings, like <code class="docutils literal"><span class="pre">['http/1.1',</span> <span class="pre">'spdy/2']</span></code>,
 
1426
ordered by preference. The selection of a protocol will happen during the
 
1427
handshake, and will play out according to the <a class="reference external" href="https://tools.ietf.org/html/draft-agl-tls-nextprotoneg">NPN draft specification</a>. After a
 
1428
successful handshake, the <a class="reference internal" href="#ssl.SSLSocket.selected_npn_protocol" title="ssl.SSLSocket.selected_npn_protocol"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.selected_npn_protocol()</span></code></a> method will
 
1429
return the agreed-upon protocol.</p>
 
1430
<p>This method will raise <a class="reference internal" href="exceptions.html#exceptions.NotImplementedError" title="exceptions.NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a> if <a class="reference internal" href="#ssl.HAS_NPN" title="ssl.HAS_NPN"><code class="xref py py-data docutils literal"><span class="pre">HAS_NPN</span></code></a> is
 
1431
False.</p>
 
1432
</dd></dl>
 
1433
 
 
1434
<dl class="method">
 
1435
<dt id="ssl.SSLContext.set_servername_callback">
 
1436
<code class="descclassname">SSLContext.</code><code class="descname">set_servername_callback</code><span class="sig-paren">(</span><em>server_name_callback</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.set_servername_callback" title="Permalink to this definition">¶</a></dt>
 
1437
<dd><p>Register a callback function that will be called after the TLS Client Hello
 
1438
handshake message has been received by the SSL/TLS server when the TLS client
 
1439
specifies a server name indication. The server name indication mechanism
 
1440
is specified in <span class="target" id="index-14"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc6066.html"><strong>RFC 6066</strong></a> section 3 - Server Name Indication.</p>
 
1441
<p>Only one callback can be set per <code class="docutils literal"><span class="pre">SSLContext</span></code>.  If <em>server_name_callback</em>
 
1442
is <code class="docutils literal"><span class="pre">None</span></code> then the callback is disabled. Calling this function a
 
1443
subsequent time will disable the previously registered callback.</p>
 
1444
<p>The callback function, <em>server_name_callback</em>, will be called with three
 
1445
arguments; the first being the <code class="xref py py-class docutils literal"><span class="pre">ssl.SSLSocket</span></code>, the second is a string
 
1446
that represents the server name that the client is intending to communicate
 
1447
(or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a> if the TLS Client Hello does not contain a server name)
 
1448
and the third argument is the original <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">SSLContext</span></code></a>. The server name
 
1449
argument is the IDNA decoded server name.</p>
 
1450
<p>A typical use of this callback is to change the <code class="xref py py-class docutils literal"><span class="pre">ssl.SSLSocket</span></code>&#8216;s
 
1451
<a class="reference internal" href="#ssl.SSLSocket.context" title="ssl.SSLSocket.context"><code class="xref py py-attr docutils literal"><span class="pre">SSLSocket.context</span></code></a> attribute to a new object of type
 
1452
<a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">SSLContext</span></code></a> representing a certificate chain that matches the server
 
1453
name.</p>
 
1454
<p>Due to the early negotiation phase of the TLS connection, only limited
 
1455
methods and attributes are usable like
 
1456
<a class="reference internal" href="#ssl.SSLSocket.selected_alpn_protocol" title="ssl.SSLSocket.selected_alpn_protocol"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.selected_alpn_protocol()</span></code></a> and <a class="reference internal" href="#ssl.SSLSocket.context" title="ssl.SSLSocket.context"><code class="xref py py-attr docutils literal"><span class="pre">SSLSocket.context</span></code></a>.
 
1457
<a class="reference internal" href="#ssl.SSLSocket.getpeercert" title="ssl.SSLSocket.getpeercert"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.getpeercert()</span></code></a>, <a class="reference internal" href="#ssl.SSLSocket.getpeercert" title="ssl.SSLSocket.getpeercert"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.getpeercert()</span></code></a>,
 
1458
<a class="reference internal" href="#ssl.SSLSocket.cipher" title="ssl.SSLSocket.cipher"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.cipher()</span></code></a> and <code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.compress()</span></code> methods require that
 
1459
the TLS connection has progressed beyond the TLS Client Hello and therefore
 
1460
will not contain return meaningful values nor can they be called safely.</p>
 
1461
<p>The <em>server_name_callback</em> function must return <code class="docutils literal"><span class="pre">None</span></code> to allow the
 
1462
TLS negotiation to continue.  If a TLS failure is required, a constant
 
1463
<a class="reference internal" href="#ssl.ALERT_DESCRIPTION_INTERNAL_ERROR" title="ssl.ALERT_DESCRIPTION_INTERNAL_ERROR"><code class="xref py py-const docutils literal"><span class="pre">ALERT_DESCRIPTION_*</span></code></a> can be
 
1464
returned.  Other return values will result in a TLS fatal error with
 
1465
<a class="reference internal" href="#ssl.ALERT_DESCRIPTION_INTERNAL_ERROR" title="ssl.ALERT_DESCRIPTION_INTERNAL_ERROR"><code class="xref py py-const docutils literal"><span class="pre">ALERT_DESCRIPTION_INTERNAL_ERROR</span></code></a>.</p>
 
1466
<p>If there is an IDNA decoding error on the server name, the TLS connection
 
1467
will terminate with an <a class="reference internal" href="#ssl.ALERT_DESCRIPTION_INTERNAL_ERROR" title="ssl.ALERT_DESCRIPTION_INTERNAL_ERROR"><code class="xref py py-const docutils literal"><span class="pre">ALERT_DESCRIPTION_INTERNAL_ERROR</span></code></a> fatal TLS
 
1468
alert message to the client.</p>
 
1469
<p>If an exception is raised from the <em>server_name_callback</em> function the TLS
 
1470
connection will terminate with a fatal TLS alert message
 
1471
<a class="reference internal" href="#ssl.ALERT_DESCRIPTION_HANDSHAKE_FAILURE" title="ssl.ALERT_DESCRIPTION_HANDSHAKE_FAILURE"><code class="xref py py-const docutils literal"><span class="pre">ALERT_DESCRIPTION_HANDSHAKE_FAILURE</span></code></a>.</p>
 
1472
<p>This method will raise <a class="reference internal" href="exceptions.html#exceptions.NotImplementedError" title="exceptions.NotImplementedError"><code class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></code></a> if the OpenSSL library
 
1473
had OPENSSL_NO_TLSEXT defined when it was built.</p>
 
1474
</dd></dl>
 
1475
 
 
1476
<dl class="method">
 
1477
<dt id="ssl.SSLContext.load_dh_params">
 
1478
<code class="descclassname">SSLContext.</code><code class="descname">load_dh_params</code><span class="sig-paren">(</span><em>dhfile</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.load_dh_params" title="Permalink to this definition">¶</a></dt>
 
1479
<dd><p>Load the key generation parameters for Diffie-Helman (DH) key exchange.
 
1480
Using DH key exchange improves forward secrecy at the expense of
 
1481
computational resources (both on the server and on the client).
 
1482
The <em>dhfile</em> parameter should be the path to a file containing DH
 
1483
parameters in PEM format.</p>
 
1484
<p>This setting doesn&#8217;t apply to client sockets.  You can also use the
 
1485
<a class="reference internal" href="#ssl.OP_SINGLE_DH_USE" title="ssl.OP_SINGLE_DH_USE"><code class="xref py py-data docutils literal"><span class="pre">OP_SINGLE_DH_USE</span></code></a> option to further improve security.</p>
 
1486
</dd></dl>
 
1487
 
 
1488
<dl class="method">
 
1489
<dt id="ssl.SSLContext.set_ecdh_curve">
 
1490
<code class="descclassname">SSLContext.</code><code class="descname">set_ecdh_curve</code><span class="sig-paren">(</span><em>curve_name</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.set_ecdh_curve" title="Permalink to this definition">¶</a></dt>
 
1491
<dd><p>Set the curve name for Elliptic Curve-based Diffie-Hellman (ECDH) key
 
1492
exchange.  ECDH is significantly faster than regular DH while arguably
 
1493
as secure.  The <em>curve_name</em> parameter should be a string describing
 
1494
a well-known elliptic curve, for example <code class="docutils literal"><span class="pre">prime256v1</span></code> for a widely
 
1495
supported curve.</p>
 
1496
<p>This setting doesn&#8217;t apply to client sockets.  You can also use the
 
1497
<a class="reference internal" href="#ssl.OP_SINGLE_ECDH_USE" title="ssl.OP_SINGLE_ECDH_USE"><code class="xref py py-data docutils literal"><span class="pre">OP_SINGLE_ECDH_USE</span></code></a> option to further improve security.</p>
 
1498
<p>This method is not available if <a class="reference internal" href="#ssl.HAS_ECDH" title="ssl.HAS_ECDH"><code class="xref py py-data docutils literal"><span class="pre">HAS_ECDH</span></code></a> is <code class="docutils literal"><span class="pre">False</span></code>.</p>
 
1499
<div class="admonition seealso">
 
1500
<p class="first admonition-title">See also</p>
 
1501
<dl class="last docutils">
 
1502
<dt><a class="reference external" href="http://vincent.bernat.im/en/blog/2011-ssl-perfect-forward-secrecy.html">SSL/TLS &amp; Perfect Forward Secrecy</a></dt>
 
1503
<dd>Vincent Bernat.</dd>
 
1504
</dl>
 
1505
</div>
 
1506
</dd></dl>
 
1507
 
 
1508
<dl class="method">
 
1509
<dt id="ssl.SSLContext.wrap_socket">
 
1510
<code class="descclassname">SSLContext.</code><code class="descname">wrap_socket</code><span class="sig-paren">(</span><em>sock</em>, <em>server_side=False</em>, <em>do_handshake_on_connect=True</em>, <em>suppress_ragged_eofs=True</em>, <em>server_hostname=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.wrap_socket" title="Permalink to this definition">¶</a></dt>
 
1511
<dd><p>Wrap an existing Python socket <em>sock</em> and return an <code class="xref py py-class docutils literal"><span class="pre">SSLSocket</span></code>
 
1512
object.  <em>sock</em> must be a <a class="reference internal" href="socket.html#socket.SOCK_STREAM" title="socket.SOCK_STREAM"><code class="xref py py-data docutils literal"><span class="pre">SOCK_STREAM</span></code></a> socket; other socket
 
1513
types are unsupported.</p>
 
1514
<p>The returned SSL socket is tied to the context, its settings and
 
1515
certificates.  The parameters <em>server_side</em>, <em>do_handshake_on_connect</em>
 
1516
and <em>suppress_ragged_eofs</em> have the same meaning as in the top-level
 
1517
<a class="reference internal" href="#ssl.wrap_socket" title="ssl.wrap_socket"><code class="xref py py-func docutils literal"><span class="pre">wrap_socket()</span></code></a> function.</p>
 
1518
<p>On client connections, the optional parameter <em>server_hostname</em> specifies
 
1519
the hostname of the service which we are connecting to.  This allows a
 
1520
single server to host multiple SSL-based services with distinct certificates,
 
1521
quite similarly to HTTP virtual hosts. Specifying <em>server_hostname</em> will
 
1522
raise a <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> if <em>server_side</em> is true.</p>
 
1523
<div class="versionchanged">
 
1524
<p><span class="versionmodified">Changed in version 2.7.9: </span>Always allow a server_hostname to be passed, even if OpenSSL does not
 
1525
have SNI.</p>
 
1526
</div>
 
1527
</dd></dl>
 
1528
 
 
1529
<dl class="method">
 
1530
<dt id="ssl.SSLContext.session_stats">
 
1531
<code class="descclassname">SSLContext.</code><code class="descname">session_stats</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.session_stats" title="Permalink to this definition">¶</a></dt>
 
1532
<dd><p>Get statistics about the SSL sessions created or managed by this context.
 
1533
A dictionary is returned which maps the names of each <a class="reference external" href="https://www.openssl.org/docs/ssl/SSL_CTX_sess_number.html">piece of information</a> to their
 
1534
numeric values.  For example, here is the total number of hits and misses
 
1535
in the session cache since the context was created:</p>
 
1536
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">stats</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">session_stats</span><span class="p">()</span>
 
1537
<span class="gp">&gt;&gt;&gt; </span><span class="n">stats</span><span class="p">[</span><span class="s1">&#39;hits&#39;</span><span class="p">],</span> <span class="n">stats</span><span class="p">[</span><span class="s1">&#39;misses&#39;</span><span class="p">]</span>
 
1538
<span class="go">(0, 0)</span>
 
1539
</pre></div>
 
1540
</div>
 
1541
</dd></dl>
 
1542
 
 
1543
<dl class="method">
 
1544
<dt>
 
1545
<code class="descclassname">SSLContext.</code><code class="descname">get_ca_certs</code><span class="sig-paren">(</span><em>binary_form=False</em><span class="sig-paren">)</span></dt>
 
1546
<dd><p>Returns a list of dicts with information of loaded CA certs. If the
 
1547
optional argument is true, returns a DER-encoded copy of the CA
 
1548
certificate.</p>
 
1549
<div class="admonition note">
 
1550
<p class="first admonition-title">Note</p>
 
1551
<p class="last">Certificates in a capath directory aren&#8217;t loaded unless they have
 
1552
been used at least once.</p>
 
1553
</div>
 
1554
</dd></dl>
 
1555
 
 
1556
<dl class="attribute">
 
1557
<dt id="ssl.SSLContext.check_hostname">
 
1558
<code class="descclassname">SSLContext.</code><code class="descname">check_hostname</code><a class="headerlink" href="#ssl.SSLContext.check_hostname" title="Permalink to this definition">¶</a></dt>
 
1559
<dd><p>Wether to match the peer cert&#8217;s hostname with <a class="reference internal" href="#ssl.match_hostname" title="ssl.match_hostname"><code class="xref py py-func docutils literal"><span class="pre">match_hostname()</span></code></a> in
 
1560
<a class="reference internal" href="#ssl.SSLSocket.do_handshake" title="ssl.SSLSocket.do_handshake"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.do_handshake()</span></code></a>. The context&#8217;s
 
1561
<a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-attr docutils literal"><span class="pre">verify_mode</span></code></a> must be set to <a class="reference internal" href="#ssl.CERT_OPTIONAL" title="ssl.CERT_OPTIONAL"><code class="xref py py-data docutils literal"><span class="pre">CERT_OPTIONAL</span></code></a> or
 
1562
<a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-data docutils literal"><span class="pre">CERT_REQUIRED</span></code></a>, and you must pass <em>server_hostname</em> to
 
1563
<a class="reference internal" href="#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal"><span class="pre">wrap_socket()</span></code></a> in order to match the hostname.</p>
 
1564
<p>Example:</p>
 
1565
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">socket</span><span class="o">,</span> <span class="nn">ssl</span>
 
1566
 
 
1567
<span class="n">context</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">SSLContext</span><span class="p">(</span><span class="n">ssl</span><span class="o">.</span><span class="n">PROTOCOL_TLSv1</span><span class="p">)</span>
 
1568
<span class="n">context</span><span class="o">.</span><span class="n">verify_mode</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">CERT_REQUIRED</span>
 
1569
<span class="n">context</span><span class="o">.</span><span class="n">check_hostname</span> <span class="o">=</span> <span class="bp">True</span>
 
1570
<span class="n">context</span><span class="o">.</span><span class="n">load_default_certs</span><span class="p">()</span>
 
1571
 
 
1572
<span class="n">s</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">socket</span><span class="p">(</span><span class="n">socket</span><span class="o">.</span><span class="n">AF_INET</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">SOCK_STREAM</span><span class="p">)</span>
 
1573
<span class="n">ssl_sock</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">wrap_socket</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">server_hostname</span><span class="o">=</span><span class="s1">&#39;www.verisign.com&#39;</span><span class="p">)</span>
 
1574
<span class="n">ssl_sock</span><span class="o">.</span><span class="n">connect</span><span class="p">((</span><span class="s1">&#39;www.verisign.com&#39;</span><span class="p">,</span> <span class="mi">443</span><span class="p">))</span>
 
1575
</pre></div>
 
1576
</div>
 
1577
<div class="admonition note">
 
1578
<p class="first admonition-title">Note</p>
 
1579
<p class="last">This features requires OpenSSL 0.9.8f or newer.</p>
 
1580
</div>
 
1581
</dd></dl>
 
1582
 
 
1583
<dl class="attribute">
 
1584
<dt id="ssl.SSLContext.options">
 
1585
<code class="descclassname">SSLContext.</code><code class="descname">options</code><a class="headerlink" href="#ssl.SSLContext.options" title="Permalink to this definition">¶</a></dt>
 
1586
<dd><p>An integer representing the set of SSL options enabled on this context.
 
1587
The default value is <a class="reference internal" href="#ssl.OP_ALL" title="ssl.OP_ALL"><code class="xref py py-data docutils literal"><span class="pre">OP_ALL</span></code></a>, but you can specify other options
 
1588
such as <a class="reference internal" href="#ssl.OP_NO_SSLv2" title="ssl.OP_NO_SSLv2"><code class="xref py py-data docutils literal"><span class="pre">OP_NO_SSLv2</span></code></a> by ORing them together.</p>
 
1589
<div class="admonition note">
 
1590
<p class="first admonition-title">Note</p>
 
1591
<p class="last">With versions of OpenSSL older than 0.9.8m, it is only possible
 
1592
to set options, not to clear them.  Attempting to clear an option
 
1593
(by resetting the corresponding bits) will raise a <code class="docutils literal"><span class="pre">ValueError</span></code>.</p>
 
1594
</div>
 
1595
</dd></dl>
 
1596
 
 
1597
<dl class="attribute">
 
1598
<dt id="ssl.SSLContext.protocol">
 
1599
<code class="descclassname">SSLContext.</code><code class="descname">protocol</code><a class="headerlink" href="#ssl.SSLContext.protocol" title="Permalink to this definition">¶</a></dt>
 
1600
<dd><p>The protocol version chosen when constructing the context.  This attribute
 
1601
is read-only.</p>
 
1602
</dd></dl>
 
1603
 
 
1604
<dl class="attribute">
 
1605
<dt id="ssl.SSLContext.verify_flags">
 
1606
<code class="descclassname">SSLContext.</code><code class="descname">verify_flags</code><a class="headerlink" href="#ssl.SSLContext.verify_flags" title="Permalink to this definition">¶</a></dt>
 
1607
<dd><p>The flags for certificate verification operations. You can set flags like
 
1608
<a class="reference internal" href="#ssl.VERIFY_CRL_CHECK_LEAF" title="ssl.VERIFY_CRL_CHECK_LEAF"><code class="xref py py-data docutils literal"><span class="pre">VERIFY_CRL_CHECK_LEAF</span></code></a> by ORing them together. By default OpenSSL
 
1609
does neither require nor verify certificate revocation lists (CRLs).
 
1610
Available only with openssl version 0.9.8+.</p>
 
1611
</dd></dl>
 
1612
 
 
1613
<dl class="attribute">
 
1614
<dt id="ssl.SSLContext.verify_mode">
 
1615
<code class="descclassname">SSLContext.</code><code class="descname">verify_mode</code><a class="headerlink" href="#ssl.SSLContext.verify_mode" title="Permalink to this definition">¶</a></dt>
 
1616
<dd><p>Whether to try to verify other peers&#8217; certificates and how to behave
 
1617
if verification fails.  This attribute must be one of
 
1618
<a class="reference internal" href="#ssl.CERT_NONE" title="ssl.CERT_NONE"><code class="xref py py-data docutils literal"><span class="pre">CERT_NONE</span></code></a>, <a class="reference internal" href="#ssl.CERT_OPTIONAL" title="ssl.CERT_OPTIONAL"><code class="xref py py-data docutils literal"><span class="pre">CERT_OPTIONAL</span></code></a> or <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-data docutils literal"><span class="pre">CERT_REQUIRED</span></code></a>.</p>
 
1619
</dd></dl>
 
1620
 
 
1621
<span class="target" id="index-15"></span></div>
 
1622
<div class="section" id="certificates">
 
1623
<span id="ssl-certificates"></span><span id="index-16"></span><h2>17.3.4. Certificates<a class="headerlink" href="#certificates" title="Permalink to this headline">¶</a></h2>
 
1624
<p>Certificates in general are part of a public-key / private-key system.  In this
 
1625
system, each <em>principal</em>, (which may be a machine, or a person, or an
 
1626
organization) is assigned a unique two-part encryption key.  One part of the key
 
1627
is public, and is called the <em>public key</em>; the other part is kept secret, and is
 
1628
called the <em>private key</em>.  The two parts are related, in that if you encrypt a
 
1629
message with one of the parts, you can decrypt it with the other part, and
 
1630
<strong>only</strong> with the other part.</p>
 
1631
<p>A certificate contains information about two principals.  It contains the name
 
1632
of a <em>subject</em>, and the subject&#8217;s public key.  It also contains a statement by a
 
1633
second principal, the <em>issuer</em>, that the subject is who he claims to be, and
 
1634
that this is indeed the subject&#8217;s public key.  The issuer&#8217;s statement is signed
 
1635
with the issuer&#8217;s private key, which only the issuer knows.  However, anyone can
 
1636
verify the issuer&#8217;s statement by finding the issuer&#8217;s public key, decrypting the
 
1637
statement with it, and comparing it to the other information in the certificate.
 
1638
The certificate also contains information about the time period over which it is
 
1639
valid.  This is expressed as two fields, called &#8220;notBefore&#8221; and &#8220;notAfter&#8221;.</p>
 
1640
<p>In the Python use of certificates, a client or server can use a certificate to
 
1641
prove who they are.  The other side of a network connection can also be required
 
1642
to produce a certificate, and that certificate can be validated to the
 
1643
satisfaction of the client or server that requires such validation.  The
 
1644
connection attempt can be set to raise an exception if the validation fails.
 
1645
Validation is done automatically, by the underlying OpenSSL framework; the
 
1646
application need not concern itself with its mechanics.  But the application
 
1647
does usually need to provide sets of certificates to allow this process to take
 
1648
place.</p>
 
1649
<p>Python uses files to contain certificates.  They should be formatted as &#8220;PEM&#8221;
 
1650
(see <span class="target" id="index-17"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1422.html"><strong>RFC 1422</strong></a>), which is a base-64 encoded form wrapped with a header line
 
1651
and a footer line:</p>
 
1652
<div class="highlight-python"><div class="highlight"><pre><span></span>-----BEGIN CERTIFICATE-----
 
1653
... (certificate in base64 PEM encoding) ...
 
1654
-----END CERTIFICATE-----
 
1655
</pre></div>
 
1656
</div>
 
1657
<div class="section" id="certificate-chains">
 
1658
<h3>17.3.4.1. Certificate chains<a class="headerlink" href="#certificate-chains" title="Permalink to this headline">¶</a></h3>
 
1659
<p>The Python files which contain certificates can contain a sequence of
 
1660
certificates, sometimes called a <em>certificate chain</em>.  This chain should start
 
1661
with the specific certificate for the principal who &#8220;is&#8221; the client or server,
 
1662
and then the certificate for the issuer of that certificate, and then the
 
1663
certificate for the issuer of <em>that</em> certificate, and so on up the chain till
 
1664
you get to a certificate which is <em>self-signed</em>, that is, a certificate which
 
1665
has the same subject and issuer, sometimes called a <em>root certificate</em>.  The
 
1666
certificates should just be concatenated together in the certificate file.  For
 
1667
example, suppose we had a three certificate chain, from our server certificate
 
1668
to the certificate of the certification authority that signed our server
 
1669
certificate, to the root certificate of the agency which issued the
 
1670
certification authority&#8217;s certificate:</p>
 
1671
<div class="highlight-python"><div class="highlight"><pre><span></span>-----BEGIN CERTIFICATE-----
 
1672
... (certificate for your server)...
 
1673
-----END CERTIFICATE-----
 
1674
-----BEGIN CERTIFICATE-----
 
1675
... (the certificate for the CA)...
 
1676
-----END CERTIFICATE-----
 
1677
-----BEGIN CERTIFICATE-----
 
1678
... (the root certificate for the CA&#39;s issuer)...
 
1679
-----END CERTIFICATE-----
 
1680
</pre></div>
 
1681
</div>
 
1682
</div>
 
1683
<div class="section" id="ca-certificates">
 
1684
<h3>17.3.4.2. CA certificates<a class="headerlink" href="#ca-certificates" title="Permalink to this headline">¶</a></h3>
 
1685
<p>If you are going to require validation of the other side of the connection&#8217;s
 
1686
certificate, you need to provide a &#8220;CA certs&#8221; file, filled with the certificate
 
1687
chains for each issuer you are willing to trust.  Again, this file just contains
 
1688
these chains concatenated together.  For validation, Python will use the first
 
1689
chain it finds in the file which matches.  The platform&#8217;s certificates file can
 
1690
be used by calling <a class="reference internal" href="#ssl.SSLContext.load_default_certs" title="ssl.SSLContext.load_default_certs"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.load_default_certs()</span></code></a>, this is done
 
1691
automatically with <a class="reference internal" href="#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal"><span class="pre">create_default_context()</span></code></a>.</p>
 
1692
</div>
 
1693
<div class="section" id="combined-key-and-certificate">
 
1694
<h3>17.3.4.3. Combined key and certificate<a class="headerlink" href="#combined-key-and-certificate" title="Permalink to this headline">¶</a></h3>
 
1695
<p>Often the private key is stored in the same file as the certificate; in this
 
1696
case, only the <code class="docutils literal"><span class="pre">certfile</span></code> parameter to <a class="reference internal" href="#ssl.SSLContext.load_cert_chain" title="ssl.SSLContext.load_cert_chain"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.load_cert_chain()</span></code></a>
 
1697
and <a class="reference internal" href="#ssl.wrap_socket" title="ssl.wrap_socket"><code class="xref py py-func docutils literal"><span class="pre">wrap_socket()</span></code></a> needs to be passed.  If the private key is stored
 
1698
with the certificate, it should come before the first certificate in
 
1699
the certificate chain:</p>
 
1700
<div class="highlight-python"><div class="highlight"><pre><span></span>-----BEGIN RSA PRIVATE KEY-----
 
1701
... (private key in base64 encoding) ...
 
1702
-----END RSA PRIVATE KEY-----
 
1703
-----BEGIN CERTIFICATE-----
 
1704
... (certificate in base64 PEM encoding) ...
 
1705
-----END CERTIFICATE-----
 
1706
</pre></div>
 
1707
</div>
 
1708
</div>
 
1709
<div class="section" id="self-signed-certificates">
 
1710
<h3>17.3.4.4. Self-signed certificates<a class="headerlink" href="#self-signed-certificates" title="Permalink to this headline">¶</a></h3>
 
1711
<p>If you are going to create a server that provides SSL-encrypted connection
 
1712
services, you will need to acquire a certificate for that service.  There are
 
1713
many ways of acquiring appropriate certificates, such as buying one from a
 
1714
certification authority.  Another common practice is to generate a self-signed
 
1715
certificate.  The simplest way to do this is with the OpenSSL package, using
 
1716
something like the following:</p>
 
1717
<div class="highlight-python"><div class="highlight"><pre><span></span>% openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
 
1718
Generating a 1024 bit RSA private key
 
1719
.......++++++
 
1720
.............................++++++
 
1721
writing new private key to &#39;cert.pem&#39;
 
1722
-----
 
1723
You are about to be asked to enter information that will be incorporated
 
1724
into your certificate request.
 
1725
What you are about to enter is what is called a Distinguished Name or a DN.
 
1726
There are quite a few fields but you can leave some blank
 
1727
For some fields there will be a default value,
 
1728
If you enter &#39;.&#39;, the field will be left blank.
 
1729
-----
 
1730
Country Name (2 letter code) [AU]:US
 
1731
State or Province Name (full name) [Some-State]:MyState
 
1732
Locality Name (eg, city) []:Some City
 
1733
Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Organization, Inc.
 
1734
Organizational Unit Name (eg, section) []:My Group
 
1735
Common Name (eg, YOUR name) []:myserver.mygroup.myorganization.com
 
1736
Email Address []:ops@myserver.mygroup.myorganization.com
 
1737
%
 
1738
</pre></div>
 
1739
</div>
 
1740
<p>The disadvantage of a self-signed certificate is that it is its own root
 
1741
certificate, and no one else will have it in their cache of known (and trusted)
 
1742
root certificates.</p>
 
1743
</div>
 
1744
</div>
 
1745
<div class="section" id="examples">
 
1746
<h2>17.3.5. Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
 
1747
<div class="section" id="testing-for-ssl-support">
 
1748
<h3>17.3.5.1. Testing for SSL support<a class="headerlink" href="#testing-for-ssl-support" title="Permalink to this headline">¶</a></h3>
 
1749
<p>To test for the presence of SSL support in a Python installation, user code
 
1750
should use the following idiom:</p>
 
1751
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
 
1752
    <span class="kn">import</span> <span class="nn">ssl</span>
 
1753
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
 
1754
    <span class="k">pass</span>
 
1755
<span class="k">else</span><span class="p">:</span>
 
1756
    <span class="o">...</span>  <span class="c1"># do something that requires SSL support</span>
 
1757
</pre></div>
 
1758
</div>
 
1759
</div>
 
1760
<div class="section" id="client-side-operation">
 
1761
<h3>17.3.5.2. Client-side operation<a class="headerlink" href="#client-side-operation" title="Permalink to this headline">¶</a></h3>
 
1762
<p>This example creates a SSL context with the recommended security settings
 
1763
for client sockets, including automatic certificate verification:</p>
 
1764
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">context</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">create_default_context</span><span class="p">()</span>
 
1765
</pre></div>
 
1766
</div>
 
1767
<p>If you prefer to tune security settings yourself, you might create
 
1768
a context from scratch (but beware that you might not get the settings
 
1769
right):</p>
 
1770
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">context</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">SSLContext</span><span class="p">(</span><span class="n">ssl</span><span class="o">.</span><span class="n">PROTOCOL_SSLv23</span><span class="p">)</span>
 
1771
<span class="gp">&gt;&gt;&gt; </span><span class="n">context</span><span class="o">.</span><span class="n">verify_mode</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">CERT_REQUIRED</span>
 
1772
<span class="gp">&gt;&gt;&gt; </span><span class="n">context</span><span class="o">.</span><span class="n">check_hostname</span> <span class="o">=</span> <span class="bp">True</span>
 
1773
<span class="gp">&gt;&gt;&gt; </span><span class="n">context</span><span class="o">.</span><span class="n">load_verify_locations</span><span class="p">(</span><span class="s2">&quot;/etc/ssl/certs/ca-bundle.crt&quot;</span><span class="p">)</span>
 
1774
</pre></div>
 
1775
</div>
 
1776
<p>(this snippet assumes your operating system places a bundle of all CA
 
1777
certificates in <code class="docutils literal"><span class="pre">/etc/ssl/certs/ca-bundle.crt</span></code>; if not, you&#8217;ll get an
 
1778
error and have to adjust the location)</p>
 
1779
<p>When you use the context to connect to a server, <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-const docutils literal"><span class="pre">CERT_REQUIRED</span></code></a>
 
1780
validates the server certificate: it ensures that the server certificate
 
1781
was signed with one of the CA certificates, and checks the signature for
 
1782
correctness:</p>
 
1783
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">wrap_socket</span><span class="p">(</span><span class="n">socket</span><span class="o">.</span><span class="n">socket</span><span class="p">(</span><span class="n">socket</span><span class="o">.</span><span class="n">AF_INET</span><span class="p">),</span>
 
1784
<span class="gp">... </span>                           <span class="n">server_hostname</span><span class="o">=</span><span class="s2">&quot;www.python.org&quot;</span><span class="p">)</span>
 
1785
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">connect</span><span class="p">((</span><span class="s2">&quot;www.python.org&quot;</span><span class="p">,</span> <span class="mi">443</span><span class="p">))</span>
 
1786
</pre></div>
 
1787
</div>
 
1788
<p>You may then fetch the certificate:</p>
 
1789
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cert</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getpeercert</span><span class="p">()</span>
 
1790
</pre></div>
 
1791
</div>
 
1792
<p>Visual inspection shows that the certificate does identify the desired service
 
1793
(that is, the HTTPS host <code class="docutils literal"><span class="pre">www.python.org</span></code>):</p>
 
1794
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">cert</span><span class="p">)</span>
 
1795
<span class="go">{&#39;OCSP&#39;: (&#39;http://ocsp.digicert.com&#39;,),</span>
 
1796
<span class="go"> &#39;caIssuers&#39;: (&#39;http://cacerts.digicert.com/DigiCertSHA2ExtendedValidationServerCA.crt&#39;,),</span>
 
1797
<span class="go"> &#39;crlDistributionPoints&#39;: (&#39;http://crl3.digicert.com/sha2-ev-server-g1.crl&#39;,</span>
 
1798
<span class="go">                           &#39;http://crl4.digicert.com/sha2-ev-server-g1.crl&#39;),</span>
 
1799
<span class="go"> &#39;issuer&#39;: (((&#39;countryName&#39;, &#39;US&#39;),),</span>
 
1800
<span class="go">            ((&#39;organizationName&#39;, &#39;DigiCert Inc&#39;),),</span>
 
1801
<span class="go">            ((&#39;organizationalUnitName&#39;, &#39;www.digicert.com&#39;),),</span>
 
1802
<span class="go">            ((&#39;commonName&#39;, &#39;DigiCert SHA2 Extended Validation Server CA&#39;),)),</span>
 
1803
<span class="go"> &#39;notAfter&#39;: &#39;Sep  9 12:00:00 2016 GMT&#39;,</span>
 
1804
<span class="go"> &#39;notBefore&#39;: &#39;Sep  5 00:00:00 2014 GMT&#39;,</span>
 
1805
<span class="go"> &#39;serialNumber&#39;: &#39;01BB6F00122B177F36CAB49CEA8B6B26&#39;,</span>
 
1806
<span class="go"> &#39;subject&#39;: (((&#39;businessCategory&#39;, &#39;Private Organization&#39;),),</span>
 
1807
<span class="go">             ((&#39;1.3.6.1.4.1.311.60.2.1.3&#39;, &#39;US&#39;),),</span>
 
1808
<span class="go">             ((&#39;1.3.6.1.4.1.311.60.2.1.2&#39;, &#39;Delaware&#39;),),</span>
 
1809
<span class="go">             ((&#39;serialNumber&#39;, &#39;3359300&#39;),),</span>
 
1810
<span class="go">             ((&#39;streetAddress&#39;, &#39;16 Allen Rd&#39;),),</span>
 
1811
<span class="go">             ((&#39;postalCode&#39;, &#39;03894-4801&#39;),),</span>
 
1812
<span class="go">             ((&#39;countryName&#39;, &#39;US&#39;),),</span>
 
1813
<span class="go">             ((&#39;stateOrProvinceName&#39;, &#39;NH&#39;),),</span>
 
1814
<span class="go">             ((&#39;localityName&#39;, &#39;Wolfeboro,&#39;),),</span>
 
1815
<span class="go">             ((&#39;organizationName&#39;, &#39;Python Software Foundation&#39;),),</span>
 
1816
<span class="go">             ((&#39;commonName&#39;, &#39;www.python.org&#39;),)),</span>
 
1817
<span class="go"> &#39;subjectAltName&#39;: ((&#39;DNS&#39;, &#39;www.python.org&#39;),</span>
 
1818
<span class="go">                    (&#39;DNS&#39;, &#39;python.org&#39;),</span>
 
1819
<span class="go">                    (&#39;DNS&#39;, &#39;pypi.python.org&#39;),</span>
 
1820
<span class="go">                    (&#39;DNS&#39;, &#39;docs.python.org&#39;),</span>
 
1821
<span class="go">                    (&#39;DNS&#39;, &#39;testpypi.python.org&#39;),</span>
 
1822
<span class="go">                    (&#39;DNS&#39;, &#39;bugs.python.org&#39;),</span>
 
1823
<span class="go">                    (&#39;DNS&#39;, &#39;wiki.python.org&#39;),</span>
 
1824
<span class="go">                    (&#39;DNS&#39;, &#39;hg.python.org&#39;),</span>
 
1825
<span class="go">                    (&#39;DNS&#39;, &#39;mail.python.org&#39;),</span>
 
1826
<span class="go">                    (&#39;DNS&#39;, &#39;packaging.python.org&#39;),</span>
 
1827
<span class="go">                    (&#39;DNS&#39;, &#39;pythonhosted.org&#39;),</span>
 
1828
<span class="go">                    (&#39;DNS&#39;, &#39;www.pythonhosted.org&#39;),</span>
 
1829
<span class="go">                    (&#39;DNS&#39;, &#39;test.pythonhosted.org&#39;),</span>
 
1830
<span class="go">                    (&#39;DNS&#39;, &#39;us.pycon.org&#39;),</span>
 
1831
<span class="go">                    (&#39;DNS&#39;, &#39;id.python.org&#39;)),</span>
 
1832
<span class="go"> &#39;version&#39;: 3}</span>
 
1833
</pre></div>
 
1834
</div>
 
1835
<p>Now the SSL channel is established and the certificate verified, you can
 
1836
proceed to talk with the server:</p>
 
1837
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">sendall</span><span class="p">(</span><span class="n">b</span><span class="s2">&quot;HEAD / HTTP/1.0</span><span class="se">\r\n</span><span class="s2">Host: linuxfr.org</span><span class="se">\r\n\r\n</span><span class="s2">&quot;</span><span class="p">)</span>
 
1838
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">conn</span><span class="o">.</span><span class="n">recv</span><span class="p">(</span><span class="mi">1024</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">b</span><span class="s2">&quot;</span><span class="se">\r\n</span><span class="s2">&quot;</span><span class="p">))</span>
 
1839
<span class="go">[b&#39;HTTP/1.1 200 OK&#39;,</span>
 
1840
<span class="go"> b&#39;Date: Sat, 18 Oct 2014 18:27:20 GMT&#39;,</span>
 
1841
<span class="go"> b&#39;Server: nginx&#39;,</span>
 
1842
<span class="go"> b&#39;Content-Type: text/html; charset=utf-8&#39;,</span>
 
1843
<span class="go"> b&#39;X-Frame-Options: SAMEORIGIN&#39;,</span>
 
1844
<span class="go"> b&#39;Content-Length: 45679&#39;,</span>
 
1845
<span class="go"> b&#39;Accept-Ranges: bytes&#39;,</span>
 
1846
<span class="go"> b&#39;Via: 1.1 varnish&#39;,</span>
 
1847
<span class="go"> b&#39;Age: 2188&#39;,</span>
 
1848
<span class="go"> b&#39;X-Served-By: cache-lcy1134-LCY&#39;,</span>
 
1849
<span class="go"> b&#39;X-Cache: HIT&#39;,</span>
 
1850
<span class="go"> b&#39;X-Cache-Hits: 11&#39;,</span>
 
1851
<span class="go"> b&#39;Vary: Cookie&#39;,</span>
 
1852
<span class="go"> b&#39;Strict-Transport-Security: max-age=63072000; includeSubDomains&#39;,</span>
 
1853
<span class="go"> b&#39;Connection: close&#39;,</span>
 
1854
<span class="go"> b&#39;&#39;,</span>
 
1855
<span class="go"> b&#39;&#39;]</span>
 
1856
</pre></div>
 
1857
</div>
 
1858
<p>See the discussion of <a class="reference internal" href="#ssl-security"><span>Security considerations</span></a> below.</p>
 
1859
</div>
 
1860
<div class="section" id="server-side-operation">
 
1861
<h3>17.3.5.3. Server-side operation<a class="headerlink" href="#server-side-operation" title="Permalink to this headline">¶</a></h3>
 
1862
<p>For server operation, typically you&#8217;ll need to have a server certificate, and
 
1863
private key, each in a file.  You&#8217;ll first create a context holding the key
 
1864
and the certificate, so that clients can check your authenticity.  Then
 
1865
you&#8217;ll open a socket, bind it to a port, call <code class="xref py py-meth docutils literal"><span class="pre">listen()</span></code> on it, and start
 
1866
waiting for clients to connect:</p>
 
1867
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">socket</span><span class="o">,</span> <span class="nn">ssl</span>
 
1868
 
 
1869
<span class="n">context</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">create_default_context</span><span class="p">(</span><span class="n">ssl</span><span class="o">.</span><span class="n">Purpose</span><span class="o">.</span><span class="n">CLIENT_AUTH</span><span class="p">)</span>
 
1870
<span class="n">context</span><span class="o">.</span><span class="n">load_cert_chain</span><span class="p">(</span><span class="n">certfile</span><span class="o">=</span><span class="s2">&quot;mycertfile&quot;</span><span class="p">,</span> <span class="n">keyfile</span><span class="o">=</span><span class="s2">&quot;mykeyfile&quot;</span><span class="p">)</span>
 
1871
 
 
1872
<span class="n">bindsocket</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">socket</span><span class="p">()</span>
 
1873
<span class="n">bindsocket</span><span class="o">.</span><span class="n">bind</span><span class="p">((</span><span class="s1">&#39;myaddr.mydomain.com&#39;</span><span class="p">,</span> <span class="mi">10023</span><span class="p">))</span>
 
1874
<span class="n">bindsocket</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
 
1875
</pre></div>
 
1876
</div>
 
1877
<p>When a client connects, you&#8217;ll call <code class="xref py py-meth docutils literal"><span class="pre">accept()</span></code> on the socket to get the
 
1878
new socket from the other end, and use the context&#8217;s <a class="reference internal" href="#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.wrap_socket()</span></code></a>
 
1879
method to create a server-side SSL socket for the connection:</p>
 
1880
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
 
1881
    <span class="n">newsocket</span><span class="p">,</span> <span class="n">fromaddr</span> <span class="o">=</span> <span class="n">bindsocket</span><span class="o">.</span><span class="n">accept</span><span class="p">()</span>
 
1882
    <span class="n">connstream</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">wrap_socket</span><span class="p">(</span><span class="n">newsocket</span><span class="p">,</span> <span class="n">server_side</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
 
1883
    <span class="k">try</span><span class="p">:</span>
 
1884
        <span class="n">deal_with_client</span><span class="p">(</span><span class="n">connstream</span><span class="p">)</span>
 
1885
    <span class="k">finally</span><span class="p">:</span>
 
1886
        <span class="n">connstream</span><span class="o">.</span><span class="n">shutdown</span><span class="p">(</span><span class="n">socket</span><span class="o">.</span><span class="n">SHUT_RDWR</span><span class="p">)</span>
 
1887
        <span class="n">connstream</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
 
1888
</pre></div>
 
1889
</div>
 
1890
<p>Then you&#8217;ll read data from the <code class="docutils literal"><span class="pre">connstream</span></code> and do something with it till you
 
1891
are finished with the client (or the client is finished with you):</p>
 
1892
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">deal_with_client</span><span class="p">(</span><span class="n">connstream</span><span class="p">):</span>
 
1893
    <span class="n">data</span> <span class="o">=</span> <span class="n">connstream</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
 
1894
    <span class="c1"># null data means the client is finished with us</span>
 
1895
    <span class="k">while</span> <span class="n">data</span><span class="p">:</span>
 
1896
        <span class="k">if</span> <span class="ow">not</span> <span class="n">do_something</span><span class="p">(</span><span class="n">connstream</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
 
1897
            <span class="c1"># we&#39;ll assume do_something returns False</span>
 
1898
            <span class="c1"># when we&#39;re finished with client</span>
 
1899
            <span class="k">break</span>
 
1900
        <span class="n">data</span> <span class="o">=</span> <span class="n">connstream</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
 
1901
    <span class="c1"># finished with client</span>
 
1902
</pre></div>
 
1903
</div>
 
1904
<p>And go back to listening for new client connections (of course, a real server
 
1905
would probably handle each client connection in a separate thread, or put
 
1906
the sockets in non-blocking mode and use an event loop).</p>
 
1907
</div>
 
1908
</div>
 
1909
<div class="section" id="notes-on-non-blocking-sockets">
 
1910
<span id="ssl-nonblocking"></span><h2>17.3.6. Notes on non-blocking sockets<a class="headerlink" href="#notes-on-non-blocking-sockets" title="Permalink to this headline">¶</a></h2>
 
1911
<p>When working with non-blocking sockets, there are several things you need
 
1912
to be aware of:</p>
 
1913
<ul>
 
1914
<li><p class="first">Calling <a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal"><span class="pre">select()</span></code></a> tells you that the OS-level socket can be
 
1915
read from (or written to), but it does not imply that there is sufficient
 
1916
data at the upper SSL layer.  For example, only part of an SSL frame might
 
1917
have arrived.  Therefore, you must be ready to handle <code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.recv()</span></code>
 
1918
and <code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.send()</span></code> failures, and retry after another call to
 
1919
<a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal"><span class="pre">select()</span></code></a>.</p>
 
1920
</li>
 
1921
<li><p class="first">Conversely, since the SSL layer has its own framing, a SSL socket may
 
1922
still have data available for reading without <a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal"><span class="pre">select()</span></code></a>
 
1923
being aware of it.  Therefore, you should first call
 
1924
<code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.recv()</span></code> to drain any potentially available data, and then
 
1925
only block on a <a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal"><span class="pre">select()</span></code></a> call if still necessary.</p>
 
1926
<p>(of course, similar provisions apply when using other primitives such as
 
1927
<a class="reference internal" href="select.html#select.poll" title="select.poll"><code class="xref py py-func docutils literal"><span class="pre">poll()</span></code></a>, or those in the <code class="xref py py-mod docutils literal"><span class="pre">selectors</span></code> module)</p>
 
1928
</li>
 
1929
<li><p class="first">The SSL handshake itself will be non-blocking: the
 
1930
<a class="reference internal" href="#ssl.SSLSocket.do_handshake" title="ssl.SSLSocket.do_handshake"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.do_handshake()</span></code></a> method has to be retried until it returns
 
1931
successfully.  Here is a synopsis using <a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal"><span class="pre">select()</span></code></a> to wait for
 
1932
the socket&#8217;s readiness:</p>
 
1933
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
 
1934
    <span class="k">try</span><span class="p">:</span>
 
1935
        <span class="n">sock</span><span class="o">.</span><span class="n">do_handshake</span><span class="p">()</span>
 
1936
        <span class="k">break</span>
 
1937
    <span class="k">except</span> <span class="n">ssl</span><span class="o">.</span><span class="n">SSLWantReadError</span><span class="p">:</span>
 
1938
        <span class="n">select</span><span class="o">.</span><span class="n">select</span><span class="p">([</span><span class="n">sock</span><span class="p">],</span> <span class="p">[],</span> <span class="p">[])</span>
 
1939
    <span class="k">except</span> <span class="n">ssl</span><span class="o">.</span><span class="n">SSLWantWriteError</span><span class="p">:</span>
 
1940
        <span class="n">select</span><span class="o">.</span><span class="n">select</span><span class="p">([],</span> <span class="p">[</span><span class="n">sock</span><span class="p">],</span> <span class="p">[])</span>
 
1941
</pre></div>
 
1942
</div>
 
1943
</li>
 
1944
</ul>
 
1945
</div>
 
1946
<div class="section" id="security-considerations">
 
1947
<span id="ssl-security"></span><h2>17.3.7. Security considerations<a class="headerlink" href="#security-considerations" title="Permalink to this headline">¶</a></h2>
 
1948
<div class="section" id="best-defaults">
 
1949
<h3>17.3.7.1. Best defaults<a class="headerlink" href="#best-defaults" title="Permalink to this headline">¶</a></h3>
 
1950
<p>For <strong>client use</strong>, if you don&#8217;t have any special requirements for your
 
1951
security policy, it is highly recommended that you use the
 
1952
<a class="reference internal" href="#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal"><span class="pre">create_default_context()</span></code></a> function to create your SSL context.
 
1953
It will load the system&#8217;s trusted CA certificates, enable certificate
 
1954
validation and hostname checking, and try to choose reasonably secure
 
1955
protocol and cipher settings.</p>
 
1956
<p>If a client certificate is needed for the connection, it can be added with
 
1957
<a class="reference internal" href="#ssl.SSLContext.load_cert_chain" title="ssl.SSLContext.load_cert_chain"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.load_cert_chain()</span></code></a>.</p>
 
1958
<p>By contrast, if you create the SSL context by calling the <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">SSLContext</span></code></a>
 
1959
constructor yourself, it will not have certificate validation nor hostname
 
1960
checking enabled by default.  If you do so, please read the paragraphs below
 
1961
to achieve a good security level.</p>
 
1962
</div>
 
1963
<div class="section" id="manual-settings">
 
1964
<h3>17.3.7.2. Manual settings<a class="headerlink" href="#manual-settings" title="Permalink to this headline">¶</a></h3>
 
1965
<div class="section" id="verifying-certificates">
 
1966
<h4>17.3.7.2.1. Verifying certificates<a class="headerlink" href="#verifying-certificates" title="Permalink to this headline">¶</a></h4>
 
1967
<p>When calling the <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">SSLContext</span></code></a> constructor directly,
 
1968
<a class="reference internal" href="#ssl.CERT_NONE" title="ssl.CERT_NONE"><code class="xref py py-const docutils literal"><span class="pre">CERT_NONE</span></code></a> is the default.  Since it does not authenticate the other
 
1969
peer, it can be insecure, especially in client mode where most of time you
 
1970
would like to ensure the authenticity of the server you&#8217;re talking to.
 
1971
Therefore, when in client mode, it is highly recommended to use
 
1972
<a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-const docutils literal"><span class="pre">CERT_REQUIRED</span></code></a>.  However, it is in itself not sufficient; you also
 
1973
have to check that the server certificate, which can be obtained by calling
 
1974
<a class="reference internal" href="#ssl.SSLSocket.getpeercert" title="ssl.SSLSocket.getpeercert"><code class="xref py py-meth docutils literal"><span class="pre">SSLSocket.getpeercert()</span></code></a>, matches the desired service.  For many
 
1975
protocols and applications, the service can be identified by the hostname;
 
1976
in this case, the <a class="reference internal" href="#ssl.match_hostname" title="ssl.match_hostname"><code class="xref py py-func docutils literal"><span class="pre">match_hostname()</span></code></a> function can be used.  This common
 
1977
check is automatically performed when <a class="reference internal" href="#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal"><span class="pre">SSLContext.check_hostname</span></code></a> is
 
1978
enabled.</p>
 
1979
<p>In server mode, if you want to authenticate your clients using the SSL layer
 
1980
(rather than using a higher-level authentication mechanism), you&#8217;ll also have
 
1981
to specify <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-const docutils literal"><span class="pre">CERT_REQUIRED</span></code></a> and similarly check the client certificate.</p>
 
1982
<blockquote>
 
1983
<div><div class="admonition note">
 
1984
<p class="first admonition-title">Note</p>
 
1985
<p class="last">In client mode, <a class="reference internal" href="#ssl.CERT_OPTIONAL" title="ssl.CERT_OPTIONAL"><code class="xref py py-const docutils literal"><span class="pre">CERT_OPTIONAL</span></code></a> and <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-const docutils literal"><span class="pre">CERT_REQUIRED</span></code></a> are
 
1986
equivalent unless anonymous ciphers are enabled (they are disabled
 
1987
by default).</p>
 
1988
</div>
 
1989
</div></blockquote>
 
1990
</div>
 
1991
<div class="section" id="protocol-versions">
 
1992
<h4>17.3.7.2.2. Protocol versions<a class="headerlink" href="#protocol-versions" title="Permalink to this headline">¶</a></h4>
 
1993
<p>SSL versions 2 and 3 are considered insecure and are therefore dangerous to
 
1994
use.  If you want maximum compatibility between clients and servers, it is
 
1995
recommended to use <a class="reference internal" href="#ssl.PROTOCOL_SSLv23" title="ssl.PROTOCOL_SSLv23"><code class="xref py py-const docutils literal"><span class="pre">PROTOCOL_SSLv23</span></code></a> as the protocol version and then
 
1996
disable SSLv2 and SSLv3 explicitly using the <a class="reference internal" href="#ssl.SSLContext.options" title="ssl.SSLContext.options"><code class="xref py py-data docutils literal"><span class="pre">SSLContext.options</span></code></a>
 
1997
attribute:</p>
 
1998
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">context</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">SSLContext</span><span class="p">(</span><span class="n">ssl</span><span class="o">.</span><span class="n">PROTOCOL_SSLv23</span><span class="p">)</span>
 
1999
<span class="n">context</span><span class="o">.</span><span class="n">options</span> <span class="o">|=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">OP_NO_SSLv2</span>
 
2000
<span class="n">context</span><span class="o">.</span><span class="n">options</span> <span class="o">|=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">OP_NO_SSLv3</span>
 
2001
</pre></div>
 
2002
</div>
 
2003
<p>The SSL context created above will only allow TLSv1 and later (if
 
2004
supported by your system) connections.</p>
 
2005
</div>
 
2006
<div class="section" id="cipher-selection">
 
2007
<h4>17.3.7.2.3. Cipher selection<a class="headerlink" href="#cipher-selection" title="Permalink to this headline">¶</a></h4>
 
2008
<p>If you have advanced security requirements, fine-tuning of the ciphers
 
2009
enabled when negotiating a SSL session is possible through the
 
2010
<a class="reference internal" href="#ssl.SSLContext.set_ciphers" title="ssl.SSLContext.set_ciphers"><code class="xref py py-meth docutils literal"><span class="pre">SSLContext.set_ciphers()</span></code></a> method.  Starting from Python 2.7.9, the
 
2011
ssl module disables certain weak ciphers by default, but you may want
 
2012
to further restrict the cipher choice. Be sure to read OpenSSL&#8217;s documentation
 
2013
about the <a class="reference external" href="https://www.openssl.org/docs/apps/ciphers.html#CIPHER-LIST-FORMAT">cipher list format</a>.
 
2014
If you want to check which ciphers are enabled by a given cipher list, use the
 
2015
<code class="docutils literal"><span class="pre">openssl</span> <span class="pre">ciphers</span></code> command on your system.</p>
 
2016
</div>
 
2017
</div>
 
2018
<div class="section" id="multi-processing">
 
2019
<h3>17.3.7.3. Multi-processing<a class="headerlink" href="#multi-processing" title="Permalink to this headline">¶</a></h3>
 
2020
<p>If using this module as part of a multi-processed application (using,
 
2021
for example the <a class="reference internal" href="multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based &quot;threading&quot; interface."><code class="xref py py-mod docutils literal"><span class="pre">multiprocessing</span></code></a> or <code class="xref py py-mod docutils literal"><span class="pre">concurrent.futures</span></code> modules),
 
2022
be aware that OpenSSL&#8217;s internal random number generator does not properly
 
2023
handle forked processes.  Applications must change the PRNG state of the
 
2024
parent process if they use any SSL feature with <a class="reference internal" href="os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal"><span class="pre">os.fork()</span></code></a>.  Any
 
2025
successful call of <a class="reference internal" href="#ssl.RAND_add" title="ssl.RAND_add"><code class="xref py py-func docutils literal"><span class="pre">RAND_add()</span></code></a>, <code class="xref py py-func docutils literal"><span class="pre">RAND_bytes()</span></code> or
 
2026
<code class="xref py py-func docutils literal"><span class="pre">RAND_pseudo_bytes()</span></code> is sufficient.</p>
 
2027
<div class="admonition seealso">
 
2028
<p class="first admonition-title">See also</p>
 
2029
<dl class="last docutils">
 
2030
<dt>Class <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal"><span class="pre">socket.socket</span></code></a></dt>
 
2031
<dd>Documentation of underlying <a class="reference internal" href="socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal"><span class="pre">socket</span></code></a> class</dd>
 
2032
<dt><a class="reference external" href="https://httpd.apache.org/docs/trunk/en/ssl/ssl_intro.html">SSL/TLS Strong Encryption: An Introduction</a></dt>
 
2033
<dd>Intro from the Apache webserver documentation</dd>
 
2034
<dt><a class="reference external" href="https://www.ietf.org/rfc/rfc1422">RFC 1422: Privacy Enhancement for Internet Electronic Mail: Part II: Certificate-Based Key Management</a></dt>
 
2035
<dd>Steve Kent</dd>
 
2036
<dt><a class="reference external" href="https://www.ietf.org/rfc/rfc1750">RFC 1750: Randomness Recommendations for Security</a></dt>
 
2037
<dd>D. Eastlake et. al.</dd>
 
2038
<dt><a class="reference external" href="https://www.ietf.org/rfc/rfc3280">RFC 3280: Internet X.509 Public Key Infrastructure Certificate and CRL Profile</a></dt>
 
2039
<dd>Housley et. al.</dd>
 
2040
<dt><a class="reference external" href="https://www.ietf.org/rfc/rfc4366">RFC 4366: Transport Layer Security (TLS) Extensions</a></dt>
 
2041
<dd>Blake-Wilson et. al.</dd>
 
2042
<dt><a class="reference external" href="https://tools.ietf.org/html/rfc5246">RFC 5246: The Transport Layer Security (TLS) Protocol Version 1.2</a></dt>
 
2043
<dd>T. Dierks et. al.</dd>
 
2044
<dt><a class="reference external" href="https://tools.ietf.org/html/rfc6066">RFC 6066: Transport Layer Security (TLS) Extensions</a></dt>
 
2045
<dd>D. Eastlake</dd>
 
2046
<dt><a class="reference external" href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xml">IANA TLS: Transport Layer Security (TLS) Parameters</a></dt>
 
2047
<dd>IANA</dd>
 
2048
</dl>
 
2049
</div>
 
2050
</div>
 
2051
</div>
 
2052
</div>
 
2053
 
 
2054
 
 
2055
          </div>
 
2056
        </div>
 
2057
      </div>
 
2058
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
 
2059
        <div class="sphinxsidebarwrapper">
 
2060
  <h3><a href="../contents.html">Table Of Contents</a></h3>
 
2061
  <ul>
 
2062
<li><a class="reference internal" href="#">17.3. <code class="docutils literal"><span class="pre">ssl</span></code> &#8212; TLS/SSL wrapper for socket objects</a><ul>
 
2063
<li><a class="reference internal" href="#functions-constants-and-exceptions">17.3.1. Functions, Constants, and Exceptions</a><ul>
 
2064
<li><a class="reference internal" href="#socket-creation">17.3.1.1. Socket creation</a></li>
 
2065
<li><a class="reference internal" href="#context-creation">17.3.1.2. Context creation</a></li>
 
2066
<li><a class="reference internal" href="#random-generation">17.3.1.3. Random generation</a></li>
 
2067
<li><a class="reference internal" href="#certificate-handling">17.3.1.4. Certificate handling</a></li>
 
2068
<li><a class="reference internal" href="#constants">17.3.1.5. Constants</a></li>
 
2069
</ul>
 
2070
</li>
 
2071
<li><a class="reference internal" href="#ssl-sockets">17.3.2. SSL Sockets</a></li>
 
2072
<li><a class="reference internal" href="#ssl-contexts">17.3.3. SSL Contexts</a></li>
 
2073
<li><a class="reference internal" href="#certificates">17.3.4. Certificates</a><ul>
 
2074
<li><a class="reference internal" href="#certificate-chains">17.3.4.1. Certificate chains</a></li>
 
2075
<li><a class="reference internal" href="#ca-certificates">17.3.4.2. CA certificates</a></li>
 
2076
<li><a class="reference internal" href="#combined-key-and-certificate">17.3.4.3. Combined key and certificate</a></li>
 
2077
<li><a class="reference internal" href="#self-signed-certificates">17.3.4.4. Self-signed certificates</a></li>
 
2078
</ul>
 
2079
</li>
 
2080
<li><a class="reference internal" href="#examples">17.3.5. Examples</a><ul>
 
2081
<li><a class="reference internal" href="#testing-for-ssl-support">17.3.5.1. Testing for SSL support</a></li>
 
2082
<li><a class="reference internal" href="#client-side-operation">17.3.5.2. Client-side operation</a></li>
 
2083
<li><a class="reference internal" href="#server-side-operation">17.3.5.3. Server-side operation</a></li>
 
2084
</ul>
 
2085
</li>
 
2086
<li><a class="reference internal" href="#notes-on-non-blocking-sockets">17.3.6. Notes on non-blocking sockets</a></li>
 
2087
<li><a class="reference internal" href="#security-considerations">17.3.7. Security considerations</a><ul>
 
2088
<li><a class="reference internal" href="#best-defaults">17.3.7.1. Best defaults</a></li>
 
2089
<li><a class="reference internal" href="#manual-settings">17.3.7.2. Manual settings</a><ul>
 
2090
<li><a class="reference internal" href="#verifying-certificates">17.3.7.2.1. Verifying certificates</a></li>
 
2091
<li><a class="reference internal" href="#protocol-versions">17.3.7.2.2. Protocol versions</a></li>
 
2092
<li><a class="reference internal" href="#cipher-selection">17.3.7.2.3. Cipher selection</a></li>
 
2093
</ul>
 
2094
</li>
 
2095
<li><a class="reference internal" href="#multi-processing">17.3.7.3. Multi-processing</a></li>
 
2096
</ul>
 
2097
</li>
 
2098
</ul>
 
2099
</li>
 
2100
</ul>
 
2101
 
 
2102
  <h4>Previous topic</h4>
 
2103
  <p class="topless"><a href="socket.html"
 
2104
                        title="previous chapter">17.2. <code class="docutils literal"><span class="pre">socket</span></code> &#8212; Low-level networking interface</a></p>
 
2105
  <h4>Next topic</h4>
 
2106
  <p class="topless"><a href="signal.html"
 
2107
                        title="next chapter">17.4. <code class="docutils literal"><span class="pre">signal</span></code> &#8212; Set handlers for asynchronous events</a></p>
 
2108
<h3>This Page</h3>
 
2109
<ul class="this-page-menu">
 
2110
  <li><a href="../bugs.html">Report a Bug</a></li>
 
2111
  <li><a href="../_sources/library/ssl.txt"
 
2112
         rel="nofollow">Show Source</a></li>
 
2113
</ul>
 
2114
 
 
2115
<div id="searchbox" style="display: none" role="search">
 
2116
  <h3>Quick search</h3>
 
2117
    <form class="search" action="../search.html" method="get">
 
2118
      <input type="text" name="q" />
 
2119
      <input type="submit" value="Go" />
 
2120
      <input type="hidden" name="check_keywords" value="yes" />
 
2121
      <input type="hidden" name="area" value="default" />
 
2122
    </form>
 
2123
    <p class="searchtip" style="font-size: 90%">
 
2124
    Enter search terms or a module, class or function name.
 
2125
    </p>
 
2126
</div>
 
2127
<script type="text/javascript">$('#searchbox').show(0);</script>
 
2128
        </div>
 
2129
      </div>
 
2130
      <div class="clearer"></div>
 
2131
    </div>  
 
2132
    <div class="related" role="navigation" aria-label="related navigation">
 
2133
      <h3>Navigation</h3>
 
2134
      <ul>
 
2135
        <li class="right" style="margin-right: 10px">
 
2136
          <a href="../genindex.html" title="General Index"
 
2137
             >index</a></li>
 
2138
        <li class="right" >
 
2139
          <a href="../py-modindex.html" title="Python Module Index"
 
2140
             >modules</a> |</li>
 
2141
        <li class="right" >
 
2142
          <a href="signal.html" title="17.4. signal — Set handlers for asynchronous events"
 
2143
             >next</a> |</li>
 
2144
        <li class="right" >
 
2145
          <a href="socket.html" title="17.2. socket — Low-level networking interface"
 
2146
             >previous</a> |</li>
 
2147
        <li><img src="../_static/py.png" alt=""
 
2148
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
2149
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
2150
        <li>
 
2151
          <span class="version_switcher_placeholder">2.7.12</span>
 
2152
          <a href="../index.html">Documentation</a> &raquo;
 
2153
        </li>
 
2154
 
 
2155
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
2156
          <li class="nav-item nav-item-2"><a href="ipc.html" >17. Interprocess Communication and Networking</a> &raquo;</li> 
 
2157
      </ul>
 
2158
    </div>  
 
2159
    <div class="footer">
 
2160
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
 
2161
    <br />
 
2162
    The Python Software Foundation is a non-profit corporation.
 
2163
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
 
2164
    <br />
 
2165
    Last updated on Oct 19, 2016.
 
2166
    <a href="../bugs.html">Found a bug</a>?
 
2167
    <br />
 
2168
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
 
2169
    </div>
 
2170
 
 
2171
  </body>
 
2172
</html>
 
 
b'\\ No newline at end of file'