gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

3394 lines
315KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="ssl — TLS/SSL wrapper for socket objects" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/ssl.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/ssl.py This module provides access to Transport Layer Security (often known as “Secure Sockets Layer”) encryption and peer authentication facilities for network sockets, both clien..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/ssl.py This module provides access to Transport Layer Security (often known as “Secure Sockets Layer”) encryption and peer authentication facilities for network sockets, both clien..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>ssl — TLS/SSL wrapper for socket objects &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="select — Waiting for I/O completion" href="select.html" />
  33. <link rel="prev" title="socket — Low-level networking interface" href="socket.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/ssl.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code> — TLS/SSL wrapper for socket objects</a><ul>
  86. <li><a class="reference internal" href="#functions-constants-and-exceptions">Functions, Constants, and Exceptions</a><ul>
  87. <li><a class="reference internal" href="#socket-creation">Socket creation</a></li>
  88. <li><a class="reference internal" href="#context-creation">Context creation</a></li>
  89. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  90. <li><a class="reference internal" href="#random-generation">Random generation</a></li>
  91. <li><a class="reference internal" href="#certificate-handling">Certificate handling</a></li>
  92. <li><a class="reference internal" href="#constants">Constants</a></li>
  93. </ul>
  94. </li>
  95. <li><a class="reference internal" href="#ssl-sockets">SSL Sockets</a></li>
  96. <li><a class="reference internal" href="#ssl-contexts">SSL Contexts</a></li>
  97. <li><a class="reference internal" href="#certificates">Certificates</a><ul>
  98. <li><a class="reference internal" href="#certificate-chains">Certificate chains</a></li>
  99. <li><a class="reference internal" href="#ca-certificates">CA certificates</a></li>
  100. <li><a class="reference internal" href="#combined-key-and-certificate">Combined key and certificate</a></li>
  101. <li><a class="reference internal" href="#self-signed-certificates">Self-signed certificates</a></li>
  102. </ul>
  103. </li>
  104. <li><a class="reference internal" href="#examples">Examples</a><ul>
  105. <li><a class="reference internal" href="#testing-for-ssl-support">Testing for SSL support</a></li>
  106. <li><a class="reference internal" href="#client-side-operation">Client-side operation</a></li>
  107. <li><a class="reference internal" href="#server-side-operation">Server-side operation</a></li>
  108. </ul>
  109. </li>
  110. <li><a class="reference internal" href="#notes-on-non-blocking-sockets">Notes on non-blocking sockets</a></li>
  111. <li><a class="reference internal" href="#memory-bio-support">Memory BIO Support</a></li>
  112. <li><a class="reference internal" href="#ssl-session">SSL session</a></li>
  113. <li><a class="reference internal" href="#security-considerations">Security considerations</a><ul>
  114. <li><a class="reference internal" href="#best-defaults">Best defaults</a></li>
  115. <li><a class="reference internal" href="#manual-settings">Manual settings</a><ul>
  116. <li><a class="reference internal" href="#verifying-certificates">Verifying certificates</a></li>
  117. <li><a class="reference internal" href="#protocol-versions">Protocol versions</a></li>
  118. <li><a class="reference internal" href="#cipher-selection">Cipher selection</a></li>
  119. </ul>
  120. </li>
  121. <li><a class="reference internal" href="#multi-processing">Multi-processing</a></li>
  122. </ul>
  123. </li>
  124. <li><a class="reference internal" href="#tls-1-3">TLS 1.3</a></li>
  125. </ul>
  126. </li>
  127. </ul>
  128. </div>
  129. <div>
  130. <h4>Previous topic</h4>
  131. <p class="topless"><a href="socket.html"
  132. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code> — Low-level networking interface</a></p>
  133. </div>
  134. <div>
  135. <h4>Next topic</h4>
  136. <p class="topless"><a href="select.html"
  137. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">select</span></code> — Waiting for I/O completion</a></p>
  138. </div>
  139. <div role="note" aria-label="source link">
  140. <h3>This Page</h3>
  141. <ul class="this-page-menu">
  142. <li><a href="../bugs.html">Report a Bug</a></li>
  143. <li>
  144. <a href="https://github.com/python/cpython/blob/main/Doc/library/ssl.rst"
  145. rel="nofollow">Show Source
  146. </a>
  147. </li>
  148. </ul>
  149. </div>
  150. </nav>
  151. </div>
  152. </div>
  153. <div class="related" role="navigation" aria-label="related navigation">
  154. <h3>Navigation</h3>
  155. <ul>
  156. <li class="right" style="margin-right: 10px">
  157. <a href="../genindex.html" title="General Index"
  158. accesskey="I">index</a></li>
  159. <li class="right" >
  160. <a href="../py-modindex.html" title="Python Module Index"
  161. >modules</a> |</li>
  162. <li class="right" >
  163. <a href="select.html" title="select — Waiting for I/O completion"
  164. accesskey="N">next</a> |</li>
  165. <li class="right" >
  166. <a href="socket.html" title="socket — Low-level networking interface"
  167. accesskey="P">previous</a> |</li>
  168. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  169. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  170. <li class="switchers">
  171. <div class="language_switcher_placeholder"></div>
  172. <div class="version_switcher_placeholder"></div>
  173. </li>
  174. <li>
  175. </li>
  176. <li id="cpython-language-and-version">
  177. <a href="../index.html">3.12.3 Documentation</a> &#187;
  178. </li>
  179. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  180. <li class="nav-item nav-item-2"><a href="ipc.html" accesskey="U">Networking and Interprocess Communication</a> &#187;</li>
  181. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code> — TLS/SSL wrapper for socket objects</a></li>
  182. <li class="right">
  183. <div class="inline-search" role="search">
  184. <form class="inline-search" action="../search.html" method="get">
  185. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  186. <input type="submit" value="Go" />
  187. </form>
  188. </div>
  189. |
  190. </li>
  191. <li class="right">
  192. <label class="theme-selector-label">
  193. Theme
  194. <select class="theme-selector" oninput="activateTheme(this.value)">
  195. <option value="auto" selected>Auto</option>
  196. <option value="light">Light</option>
  197. <option value="dark">Dark</option>
  198. </select>
  199. </label> |</li>
  200. </ul>
  201. </div>
  202. <div class="document">
  203. <div class="documentwrapper">
  204. <div class="bodywrapper">
  205. <div class="body" role="main">
  206. <section id="module-ssl">
  207. <span id="ssl-tls-ssl-wrapper-for-socket-objects"></span><h1><a class="reference internal" href="#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code></a> — TLS/SSL wrapper for socket objects<a class="headerlink" href="#module-ssl" title="Link to this heading">¶</a></h1>
  208. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/ssl.py">Lib/ssl.py</a></p>
  209. <span id="index-0"></span><hr class="docutils" id="index-1" />
  210. <p>This module provides access to Transport Layer Security (often known as “Secure
  211. Sockets Layer”) encryption and peer authentication facilities for network
  212. sockets, both client-side and server-side. This module uses the OpenSSL
  213. library. It is available on all modern Unix systems, Windows, macOS, and
  214. probably additional platforms, as long as OpenSSL is installed on that platform.</p>
  215. <div class="admonition note">
  216. <p class="admonition-title">Note</p>
  217. <p>Some behavior may be platform dependent, since calls are made to the
  218. operating system socket APIs. The installed version of OpenSSL may also
  219. cause variations in behavior. For example, TLSv1.3 comes with OpenSSL version
  220. 1.1.1.</p>
  221. </div>
  222. <div class="admonition warning">
  223. <p class="admonition-title">Warning</p>
  224. <p>Don’t use this module without reading the <a class="reference internal" href="#ssl-security"><span class="std std-ref">Security considerations</span></a>. Doing so
  225. may lead to a false sense of security, as the default settings of the
  226. ssl module are not necessarily appropriate for your application.</p>
  227. </div>
  228. <div class="availability docutils container">
  229. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p>
  230. <p>This module does not work or is not available on WebAssembly platforms
  231. <code class="docutils literal notranslate"><span class="pre">wasm32-emscripten</span></code> and <code class="docutils literal notranslate"><span class="pre">wasm32-wasi</span></code>. See
  232. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  233. </div>
  234. <p>This section documents the objects and functions in the <code class="docutils literal notranslate"><span class="pre">ssl</span></code> module; for more
  235. general information about TLS, SSL, and certificates, the reader is referred to
  236. the documents in the “See Also” section at the bottom.</p>
  237. <p>This module provides a class, <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLSocket</span></code></a>, which is derived from the
  238. <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal notranslate"><span class="pre">socket.socket</span></code></a> type, and provides a socket-like wrapper that also
  239. encrypts and decrypts the data going over the socket with SSL. It supports
  240. additional methods such as <code class="xref py py-meth docutils literal notranslate"><span class="pre">getpeercert()</span></code>, which retrieves the
  241. certificate of the other side of the connection, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">cipher()</span></code>, which
  242. retrieves the cipher being used for the secure connection.</p>
  243. <p>For more sophisticated applications, the <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> class
  244. helps manage settings and certificates, which can then be inherited
  245. 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 notranslate"><span class="pre">SSLContext.wrap_socket()</span></code></a> method.</p>
  246. <div class="versionchanged">
  247. <p><span class="versionmodified changed">Changed in version 3.5.3: </span>Updated to support linking with OpenSSL 1.1.0</p>
  248. </div>
  249. <div class="versionchanged">
  250. <p><span class="versionmodified changed">Changed in version 3.6: </span>OpenSSL 0.9.8, 1.0.0 and 1.0.1 are deprecated and no longer supported.
  251. In the future the ssl module will require at least OpenSSL 1.0.2 or
  252. 1.1.0.</p>
  253. </div>
  254. <div class="versionchanged">
  255. <p><span class="versionmodified changed">Changed in version 3.10: </span><span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0644/"><strong>PEP 644</strong></a> has been implemented. The ssl module requires OpenSSL 1.1.1
  256. or newer.</p>
  257. <p>Use of deprecated constants and functions result in deprecation warnings.</p>
  258. </div>
  259. <section id="functions-constants-and-exceptions">
  260. <h2>Functions, Constants, and Exceptions<a class="headerlink" href="#functions-constants-and-exceptions" title="Link to this heading">¶</a></h2>
  261. <section id="socket-creation">
  262. <h3>Socket creation<a class="headerlink" href="#socket-creation" title="Link to this heading">¶</a></h3>
  263. <p>Instances of <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> must be created using the
  264. <a class="reference internal" href="#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.wrap_socket()</span></code></a> method. The helper function
  265. <a class="reference internal" href="#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_default_context()</span></code></a> returns a new context with secure default
  266. settings.</p>
  267. <p>Client socket example with default context and IPv4/IPv6 dual stack:</p>
  268. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">socket</span>
  269. <span class="kn">import</span> <span class="nn">ssl</span>
  270. <span class="n">hostname</span> <span class="o">=</span> <span class="s1">&#39;www.python.org&#39;</span>
  271. <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>
  272. <span class="k">with</span> <span class="n">socket</span><span class="o">.</span><span class="n">create_connection</span><span class="p">((</span><span class="n">hostname</span><span class="p">,</span> <span class="mi">443</span><span class="p">))</span> <span class="k">as</span> <span class="n">sock</span><span class="p">:</span>
  273. <span class="k">with</span> <span class="n">context</span><span class="o">.</span><span class="n">wrap_socket</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">server_hostname</span><span class="o">=</span><span class="n">hostname</span><span class="p">)</span> <span class="k">as</span> <span class="n">ssock</span><span class="p">:</span>
  274. <span class="nb">print</span><span class="p">(</span><span class="n">ssock</span><span class="o">.</span><span class="n">version</span><span class="p">())</span>
  275. </pre></div>
  276. </div>
  277. <p>Client socket example with custom context and IPv4:</p>
  278. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">hostname</span> <span class="o">=</span> <span class="s1">&#39;www.python.org&#39;</span>
  279. <span class="c1"># PROTOCOL_TLS_CLIENT requires valid cert chain and hostname</span>
  280. <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_TLS_CLIENT</span><span class="p">)</span>
  281. <span class="n">context</span><span class="o">.</span><span class="n">load_verify_locations</span><span class="p">(</span><span class="s1">&#39;path/to/cabundle.pem&#39;</span><span class="p">)</span>
  282. <span class="k">with</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> <span class="mi">0</span><span class="p">)</span> <span class="k">as</span> <span class="n">sock</span><span class="p">:</span>
  283. <span class="k">with</span> <span class="n">context</span><span class="o">.</span><span class="n">wrap_socket</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">server_hostname</span><span class="o">=</span><span class="n">hostname</span><span class="p">)</span> <span class="k">as</span> <span class="n">ssock</span><span class="p">:</span>
  284. <span class="nb">print</span><span class="p">(</span><span class="n">ssock</span><span class="o">.</span><span class="n">version</span><span class="p">())</span>
  285. </pre></div>
  286. </div>
  287. <p>Server socket example listening on localhost IPv4:</p>
  288. <div class="highlight-python3 notranslate"><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_TLS_SERVER</span><span class="p">)</span>
  289. <span class="n">context</span><span class="o">.</span><span class="n">load_cert_chain</span><span class="p">(</span><span class="s1">&#39;/path/to/certchain.pem&#39;</span><span class="p">,</span> <span class="s1">&#39;/path/to/private.key&#39;</span><span class="p">)</span>
  290. <span class="k">with</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> <span class="mi">0</span><span class="p">)</span> <span class="k">as</span> <span class="n">sock</span><span class="p">:</span>
  291. <span class="n">sock</span><span class="o">.</span><span class="n">bind</span><span class="p">((</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">8443</span><span class="p">))</span>
  292. <span class="n">sock</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
  293. <span class="k">with</span> <span class="n">context</span><span class="o">.</span><span class="n">wrap_socket</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">server_side</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="k">as</span> <span class="n">ssock</span><span class="p">:</span>
  294. <span class="n">conn</span><span class="p">,</span> <span class="n">addr</span> <span class="o">=</span> <span class="n">ssock</span><span class="o">.</span><span class="n">accept</span><span class="p">()</span>
  295. <span class="o">...</span>
  296. </pre></div>
  297. </div>
  298. </section>
  299. <section id="context-creation">
  300. <h3>Context creation<a class="headerlink" href="#context-creation" title="Link to this heading">¶</a></h3>
  301. <p>A convenience function helps create <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> objects for common
  302. purposes.</p>
  303. <dl class="py function">
  304. <dt class="sig sig-object py" id="ssl.create_default_context">
  305. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">create_default_context</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">purpose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">Purpose.SERVER_AUTH</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cafile</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">capath</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cadata</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.create_default_context" title="Link to this definition">¶</a></dt>
  306. <dd><p>Return a new <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> object with default settings for
  307. 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 notranslate"><span class="pre">ssl</span></code></a> module,
  308. and usually represent a higher security level than when calling the
  309. <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> constructor directly.</p>
  310. <p><em>cafile</em>, <em>capath</em>, <em>cadata</em> represent optional CA certificates to
  311. trust for certificate verification, as in
  312. <a class="reference internal" href="#ssl.SSLContext.load_verify_locations" title="ssl.SSLContext.load_verify_locations"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.load_verify_locations()</span></code></a>. If all three are
  313. <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>, this function can choose to trust the system’s default
  314. CA certificates instead.</p>
  315. <p>The settings are: <a class="reference internal" href="#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a> or
  316. <a class="reference internal" href="#ssl.PROTOCOL_TLS_SERVER" title="ssl.PROTOCOL_TLS_SERVER"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_SERVER</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 notranslate"><span class="pre">OP_NO_SSLv2</span></code></a>, and <a class="reference internal" href="#ssl.OP_NO_SSLv3" title="ssl.OP_NO_SSLv3"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_SSLv3</span></code></a>
  317. with high encryption cipher suites without RC4 and
  318. without unauthenticated cipher suites. Passing <a class="reference internal" href="#ssl.Purpose.SERVER_AUTH" title="ssl.Purpose.SERVER_AUTH"><code class="xref py py-const docutils literal notranslate"><span class="pre">SERVER_AUTH</span></code></a>
  319. 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 notranslate"><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 notranslate"><span class="pre">CERT_REQUIRED</span></code></a>
  320. and either loads CA certificates (when at least one of <em>cafile</em>, <em>capath</em> or
  321. <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 notranslate"><span class="pre">SSLContext.load_default_certs()</span></code></a> to load
  322. default CA certificates.</p>
  323. <p>When <a class="reference internal" href="#ssl.SSLContext.keylog_filename" title="ssl.SSLContext.keylog_filename"><code class="xref py py-attr docutils literal notranslate"><span class="pre">keylog_filename</span></code></a> is supported and the environment
  324. variable <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">SSLKEYLOGFILE</span></code> is set, <a class="reference internal" href="#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_default_context()</span></code></a>
  325. enables key logging.</p>
  326. <div class="admonition note">
  327. <p class="admonition-title">Note</p>
  328. <p>The protocol, options, cipher and other settings may change to more
  329. restrictive values anytime without prior deprecation. The values
  330. represent a fair balance between compatibility and security.</p>
  331. <p>If your application needs specific settings, you should create a
  332. <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> and apply the settings yourself.</p>
  333. </div>
  334. <div class="admonition note">
  335. <p class="admonition-title">Note</p>
  336. <p>If you find that when certain older clients or servers attempt to connect
  337. with a <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> created by this function that they get an error
  338. stating “Protocol or cipher suite mismatch”, it may be that they only
  339. support SSL3.0 which this function excludes using the
  340. <a class="reference internal" href="#ssl.OP_NO_SSLv3" title="ssl.OP_NO_SSLv3"><code class="xref py py-data docutils literal notranslate"><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
  341. use this function but still allow SSL 3.0 connections you can re-enable
  342. them using:</p>
  343. <div class="highlight-python3 notranslate"><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>
  344. <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>
  345. </pre></div>
  346. </div>
  347. </div>
  348. <div class="versionadded">
  349. <p><span class="versionmodified added">New in version 3.4.</span></p>
  350. </div>
  351. <div class="versionchanged">
  352. <p><span class="versionmodified changed">Changed in version 3.4.4: </span>RC4 was dropped from the default cipher string.</p>
  353. </div>
  354. <div class="versionchanged">
  355. <p><span class="versionmodified changed">Changed in version 3.6: </span>ChaCha20/Poly1305 was added to the default cipher string.</p>
  356. <p>3DES was dropped from the default cipher string.</p>
  357. </div>
  358. <div class="versionchanged">
  359. <p><span class="versionmodified changed">Changed in version 3.8: </span>Support for key logging to <span class="target" id="index-4"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">SSLKEYLOGFILE</span></code> was added.</p>
  360. </div>
  361. <div class="versionchanged">
  362. <p><span class="versionmodified changed">Changed in version 3.10: </span>The context now uses <a class="reference internal" href="#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a> or
  363. <a class="reference internal" href="#ssl.PROTOCOL_TLS_SERVER" title="ssl.PROTOCOL_TLS_SERVER"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_SERVER</span></code></a> protocol instead of generic
  364. <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a>.</p>
  365. </div>
  366. </dd></dl>
  367. </section>
  368. <section id="exceptions">
  369. <h3>Exceptions<a class="headerlink" href="#exceptions" title="Link to this heading">¶</a></h3>
  370. <dl class="py exception">
  371. <dt class="sig sig-object py" id="ssl.SSLError">
  372. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">SSLError</span></span><a class="headerlink" href="#ssl.SSLError" title="Link to this definition">¶</a></dt>
  373. <dd><p>Raised to signal an error from the underlying SSL implementation
  374. (currently provided by the OpenSSL library). This signifies some
  375. problem in the higher-level encryption and authentication layer that’s
  376. superimposed on the underlying network connection. This error
  377. is a subtype of <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>. The error code and message of
  378. <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLError</span></code></a> instances are provided by the OpenSSL library.</p>
  379. <div class="versionchanged">
  380. <p><span class="versionmodified changed">Changed in version 3.3: </span><a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLError</span></code></a> used to be a subtype of <a class="reference internal" href="socket.html#socket.error" title="socket.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">socket.error</span></code></a>.</p>
  381. </div>
  382. <dl class="py attribute">
  383. <dt class="sig sig-object py" id="ssl.SSLError.library">
  384. <span class="sig-name descname"><span class="pre">library</span></span><a class="headerlink" href="#ssl.SSLError.library" title="Link to this definition">¶</a></dt>
  385. <dd><p>A string mnemonic designating the OpenSSL submodule in which the error
  386. occurred, such as <code class="docutils literal notranslate"><span class="pre">SSL</span></code>, <code class="docutils literal notranslate"><span class="pre">PEM</span></code> or <code class="docutils literal notranslate"><span class="pre">X509</span></code>. The range of possible
  387. values depends on the OpenSSL version.</p>
  388. <div class="versionadded">
  389. <p><span class="versionmodified added">New in version 3.3.</span></p>
  390. </div>
  391. </dd></dl>
  392. <dl class="py attribute">
  393. <dt class="sig sig-object py" id="ssl.SSLError.reason">
  394. <span class="sig-name descname"><span class="pre">reason</span></span><a class="headerlink" href="#ssl.SSLError.reason" title="Link to this definition">¶</a></dt>
  395. <dd><p>A string mnemonic designating the reason this error occurred, for
  396. example <code class="docutils literal notranslate"><span class="pre">CERTIFICATE_VERIFY_FAILED</span></code>. The range of possible
  397. values depends on the OpenSSL version.</p>
  398. <div class="versionadded">
  399. <p><span class="versionmodified added">New in version 3.3.</span></p>
  400. </div>
  401. </dd></dl>
  402. </dd></dl>
  403. <dl class="py exception">
  404. <dt class="sig sig-object py" id="ssl.SSLZeroReturnError">
  405. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">SSLZeroReturnError</span></span><a class="headerlink" href="#ssl.SSLZeroReturnError" title="Link to this definition">¶</a></dt>
  406. <dd><p>A subclass of <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLError</span></code></a> raised when trying to read or write and
  407. the SSL connection has been closed cleanly. Note that this doesn’t
  408. mean that the underlying transport (read TCP) has been closed.</p>
  409. <div class="versionadded">
  410. <p><span class="versionmodified added">New in version 3.3.</span></p>
  411. </div>
  412. </dd></dl>
  413. <dl class="py exception">
  414. <dt class="sig sig-object py" id="ssl.SSLWantReadError">
  415. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">SSLWantReadError</span></span><a class="headerlink" href="#ssl.SSLWantReadError" title="Link to this definition">¶</a></dt>
  416. <dd><p>A subclass of <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLError</span></code></a> raised by a <a class="reference internal" href="#ssl-nonblocking"><span class="std std-ref">non-blocking SSL socket</span></a> when trying to read or write data, but more data needs
  417. to be received on the underlying TCP transport before the request can be
  418. fulfilled.</p>
  419. <div class="versionadded">
  420. <p><span class="versionmodified added">New in version 3.3.</span></p>
  421. </div>
  422. </dd></dl>
  423. <dl class="py exception">
  424. <dt class="sig sig-object py" id="ssl.SSLWantWriteError">
  425. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">SSLWantWriteError</span></span><a class="headerlink" href="#ssl.SSLWantWriteError" title="Link to this definition">¶</a></dt>
  426. <dd><p>A subclass of <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLError</span></code></a> raised by a <a class="reference internal" href="#ssl-nonblocking"><span class="std std-ref">non-blocking SSL socket</span></a> when trying to read or write data, but more data needs
  427. to be sent on the underlying TCP transport before the request can be
  428. fulfilled.</p>
  429. <div class="versionadded">
  430. <p><span class="versionmodified added">New in version 3.3.</span></p>
  431. </div>
  432. </dd></dl>
  433. <dl class="py exception">
  434. <dt class="sig sig-object py" id="ssl.SSLSyscallError">
  435. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">SSLSyscallError</span></span><a class="headerlink" href="#ssl.SSLSyscallError" title="Link to this definition">¶</a></dt>
  436. <dd><p>A subclass of <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLError</span></code></a> raised when a system error was encountered
  437. while trying to fulfill an operation on a SSL socket. Unfortunately,
  438. there is no easy way to inspect the original errno number.</p>
  439. <div class="versionadded">
  440. <p><span class="versionmodified added">New in version 3.3.</span></p>
  441. </div>
  442. </dd></dl>
  443. <dl class="py exception">
  444. <dt class="sig sig-object py" id="ssl.SSLEOFError">
  445. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">SSLEOFError</span></span><a class="headerlink" href="#ssl.SSLEOFError" title="Link to this definition">¶</a></dt>
  446. <dd><p>A subclass of <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLError</span></code></a> raised when the SSL connection has been
  447. terminated abruptly. Generally, you shouldn’t try to reuse the underlying
  448. transport when this error is encountered.</p>
  449. <div class="versionadded">
  450. <p><span class="versionmodified added">New in version 3.3.</span></p>
  451. </div>
  452. </dd></dl>
  453. <dl class="py exception">
  454. <dt class="sig sig-object py" id="ssl.SSLCertVerificationError">
  455. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">SSLCertVerificationError</span></span><a class="headerlink" href="#ssl.SSLCertVerificationError" title="Link to this definition">¶</a></dt>
  456. <dd><p>A subclass of <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLError</span></code></a> raised when certificate validation has
  457. failed.</p>
  458. <div class="versionadded">
  459. <p><span class="versionmodified added">New in version 3.7.</span></p>
  460. </div>
  461. <dl class="py attribute">
  462. <dt class="sig sig-object py" id="ssl.SSLCertVerificationError.verify_code">
  463. <span class="sig-name descname"><span class="pre">verify_code</span></span><a class="headerlink" href="#ssl.SSLCertVerificationError.verify_code" title="Link to this definition">¶</a></dt>
  464. <dd><p>A numeric error number that denotes the verification error.</p>
  465. </dd></dl>
  466. <dl class="py attribute">
  467. <dt class="sig sig-object py" id="ssl.SSLCertVerificationError.verify_message">
  468. <span class="sig-name descname"><span class="pre">verify_message</span></span><a class="headerlink" href="#ssl.SSLCertVerificationError.verify_message" title="Link to this definition">¶</a></dt>
  469. <dd><p>A human readable string of the verification error.</p>
  470. </dd></dl>
  471. </dd></dl>
  472. <dl class="py exception">
  473. <dt class="sig sig-object py" id="ssl.CertificateError">
  474. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">CertificateError</span></span><a class="headerlink" href="#ssl.CertificateError" title="Link to this definition">¶</a></dt>
  475. <dd><p>An alias for <a class="reference internal" href="#ssl.SSLCertVerificationError" title="ssl.SSLCertVerificationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLCertVerificationError</span></code></a>.</p>
  476. <div class="versionchanged">
  477. <p><span class="versionmodified changed">Changed in version 3.7: </span>The exception is now an alias for <a class="reference internal" href="#ssl.SSLCertVerificationError" title="ssl.SSLCertVerificationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLCertVerificationError</span></code></a>.</p>
  478. </div>
  479. </dd></dl>
  480. </section>
  481. <section id="random-generation">
  482. <h3>Random generation<a class="headerlink" href="#random-generation" title="Link to this heading">¶</a></h3>
  483. <dl class="py function">
  484. <dt class="sig sig-object py" id="ssl.RAND_bytes">
  485. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">RAND_bytes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">num</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.RAND_bytes" title="Link to this definition">¶</a></dt>
  486. <dd><p>Return <em>num</em> cryptographically strong pseudo-random bytes. Raises an
  487. <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLError</span></code></a> if the PRNG has not been seeded with enough data or if the
  488. operation is not supported by the current RAND method. <a class="reference internal" href="#ssl.RAND_status" title="ssl.RAND_status"><code class="xref py py-func docutils literal notranslate"><span class="pre">RAND_status()</span></code></a>
  489. can be used to check the status of the PRNG and <a class="reference internal" href="#ssl.RAND_add" title="ssl.RAND_add"><code class="xref py py-func docutils literal notranslate"><span class="pre">RAND_add()</span></code></a> can be used
  490. to seed the PRNG.</p>
  491. <p>For almost all applications <a class="reference internal" href="os.html#os.urandom" title="os.urandom"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.urandom()</span></code></a> is preferable.</p>
  492. <p>Read the Wikipedia article, <a class="reference external" href="https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator">Cryptographically secure pseudorandom number
  493. generator (CSPRNG)</a>,
  494. to get the requirements of a cryptographically strong generator.</p>
  495. <div class="versionadded">
  496. <p><span class="versionmodified added">New in version 3.3.</span></p>
  497. </div>
  498. </dd></dl>
  499. <dl class="py function">
  500. <dt class="sig sig-object py" id="ssl.RAND_status">
  501. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">RAND_status</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.RAND_status" title="Link to this definition">¶</a></dt>
  502. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the SSL pseudo-random number generator has been seeded
  503. with ‘enough’ randomness, and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. You can use
  504. <code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.RAND_egd()</span></code> and <a class="reference internal" href="#ssl.RAND_add" title="ssl.RAND_add"><code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.RAND_add()</span></code></a> to increase the randomness of
  505. the pseudo-random number generator.</p>
  506. </dd></dl>
  507. <dl class="py function">
  508. <dt class="sig sig-object py" id="ssl.RAND_add">
  509. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">RAND_add</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">bytes</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">entropy</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.RAND_add" title="Link to this definition">¶</a></dt>
  510. <dd><p>Mix the given <em>bytes</em> into the SSL pseudo-random number generator. The
  511. parameter <em>entropy</em> (a float) is a lower bound on the entropy contained in
  512. string (so you can always use <code class="docutils literal notranslate"><span class="pre">0.0</span></code>). See <span class="target" id="index-5"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc1750.html"><strong>RFC 1750</strong></a> for more
  513. information on sources of entropy.</p>
  514. <div class="versionchanged">
  515. <p><span class="versionmodified changed">Changed in version 3.5: </span>Writable <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> is now accepted.</p>
  516. </div>
  517. </dd></dl>
  518. </section>
  519. <section id="certificate-handling">
  520. <h3>Certificate handling<a class="headerlink" href="#certificate-handling" title="Link to this heading">¶</a></h3>
  521. <dl class="py function">
  522. <dt class="sig sig-object py" id="ssl.cert_time_to_seconds">
  523. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">cert_time_to_seconds</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cert_time</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.cert_time_to_seconds" title="Link to this definition">¶</a></dt>
  524. <dd><p>Return the time in seconds since the Epoch, given the <code class="docutils literal notranslate"><span class="pre">cert_time</span></code>
  525. string representing the “notBefore” or “notAfter” date from a
  526. certificate in <code class="docutils literal notranslate"><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
  527. locale).</p>
  528. <p>Here’s an example:</p>
  529. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">ssl</span>
  530. <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>
  531. <span class="gp">&gt;&gt;&gt; </span><span class="n">timestamp</span>
  532. <span class="go">1515144883</span>
  533. <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>
  534. <span class="gp">&gt;&gt;&gt; </span><span class="nb">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>
  535. <span class="go">2018-01-05 09:34:43</span>
  536. </pre></div>
  537. </div>
  538. <p>“notBefore” or “notAfter” dates must use GMT (<span class="target" id="index-6"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5280.html"><strong>RFC 5280</strong></a>).</p>
  539. <div class="versionchanged">
  540. <p><span class="versionmodified changed">Changed in version 3.5: </span>Interpret the input time as a time in UTC as specified by ‘GMT’
  541. timezone in the input string. Local timezone was used
  542. previously. Return an integer (no fractions of a second in the
  543. input format)</p>
  544. </div>
  545. </dd></dl>
  546. <dl class="py function">
  547. <dt class="sig sig-object py" id="ssl.get_server_certificate">
  548. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">get_server_certificate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">addr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ssl_version=PROTOCOL_TLS_CLIENT</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ca_certs=None</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.get_server_certificate" title="Link to this definition">¶</a></dt>
  549. <dd><p>Given the address <code class="docutils literal notranslate"><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’s certificate, and returns it as a
  551. PEM-encoded string. If <code class="docutils literal notranslate"><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 <em>ca_certs</em> is
  553. specified, it should be a file containing a list of root certificates, the
  554. same format as used for the <em>cafile</em> parameter in
  555. <a class="reference internal" href="#ssl.SSLContext.load_verify_locations" title="ssl.SSLContext.load_verify_locations"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.load_verify_locations()</span></code></a>. The call will attempt to validate the
  556. server certificate against that set of root certificates, and will fail
  557. if the validation attempt fails. A timeout can be specified with the
  558. <code class="docutils literal notranslate"><span class="pre">timeout</span></code> parameter.</p>
  559. <div class="versionchanged">
  560. <p><span class="versionmodified changed">Changed in version 3.3: </span>This function is now IPv6-compatible.</p>
  561. </div>
  562. <div class="versionchanged">
  563. <p><span class="versionmodified changed">Changed in version 3.5: </span>The default <em>ssl_version</em> is changed from <a class="reference internal" href="#ssl.PROTOCOL_SSLv3" title="ssl.PROTOCOL_SSLv3"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_SSLv3</span></code></a> to
  564. <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a> for maximum compatibility with modern servers.</p>
  565. </div>
  566. <div class="versionchanged">
  567. <p><span class="versionmodified changed">Changed in version 3.10: </span>The <em>timeout</em> parameter was added.</p>
  568. </div>
  569. </dd></dl>
  570. <dl class="py function">
  571. <dt class="sig sig-object py" id="ssl.DER_cert_to_PEM_cert">
  572. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">DER_cert_to_PEM_cert</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">DER_cert_bytes</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.DER_cert_to_PEM_cert" title="Link to this definition">¶</a></dt>
  573. <dd><p>Given a certificate as a DER-encoded blob of bytes, returns a PEM-encoded
  574. string version of the same certificate.</p>
  575. </dd></dl>
  576. <dl class="py function">
  577. <dt class="sig sig-object py" id="ssl.PEM_cert_to_DER_cert">
  578. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">PEM_cert_to_DER_cert</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">PEM_cert_string</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.PEM_cert_to_DER_cert" title="Link to this definition">¶</a></dt>
  579. <dd><p>Given a certificate as an ASCII PEM string, returns a DER-encoded sequence of
  580. bytes for that same certificate.</p>
  581. </dd></dl>
  582. <dl class="py function">
  583. <dt class="sig sig-object py" id="ssl.get_default_verify_paths">
  584. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">get_default_verify_paths</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.get_default_verify_paths" title="Link to this definition">¶</a></dt>
  585. <dd><p>Returns a named tuple with paths to OpenSSL’s default cafile and capath.
  586. The paths are the same as used by
  587. <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 notranslate"><span class="pre">SSLContext.set_default_verify_paths()</span></code></a>. The return value is a
  588. <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> <code class="docutils literal notranslate"><span class="pre">DefaultVerifyPaths</span></code>:</p>
  589. <ul class="simple">
  590. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">cafile</span></code> - resolved path to cafile or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the file doesn’t exist,</p></li>
  591. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">capath</span></code> - resolved path to capath or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the directory doesn’t exist,</p></li>
  592. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">openssl_cafile_env</span></code> - OpenSSL’s environment key that points to a cafile,</p></li>
  593. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">openssl_cafile</span></code> - hard coded path to a cafile,</p></li>
  594. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">openssl_capath_env</span></code> - OpenSSL’s environment key that points to a capath,</p></li>
  595. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">openssl_capath</span></code> - hard coded path to a capath directory</p></li>
  596. </ul>
  597. <div class="versionadded">
  598. <p><span class="versionmodified added">New in version 3.4.</span></p>
  599. </div>
  600. </dd></dl>
  601. <dl class="py function">
  602. <dt class="sig sig-object py" id="ssl.enum_certificates">
  603. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">enum_certificates</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">store_name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.enum_certificates" title="Link to this definition">¶</a></dt>
  604. <dd><p>Retrieve certificates from Windows’ system cert store. <em>store_name</em> may be
  605. one of <code class="docutils literal notranslate"><span class="pre">CA</span></code>, <code class="docutils literal notranslate"><span class="pre">ROOT</span></code> or <code class="docutils literal notranslate"><span class="pre">MY</span></code>. Windows may provide additional cert
  606. stores, too.</p>
  607. <p>The function returns a list of (cert_bytes, encoding_type, trust) tuples.
  608. The encoding_type specifies the encoding of cert_bytes. It is either
  609. <code class="xref py py-const docutils literal notranslate"><span class="pre">x509_asn</span></code> for X.509 ASN.1 data or <code class="xref py py-const docutils literal notranslate"><span class="pre">pkcs_7_asn</span></code> for
  610. PKCS#7 ASN.1 data. Trust specifies the purpose of the certificate as a set
  611. of OIDS or exactly <code class="docutils literal notranslate"><span class="pre">True</span></code> if the certificate is trustworthy for all
  612. purposes.</p>
  613. <p>Example:</p>
  614. <div class="highlight-python3 notranslate"><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>
  615. <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>
  616. <span class="go"> (b&#39;data...&#39;, &#39;x509_asn&#39;, True)]</span>
  617. </pre></div>
  618. </div>
  619. <div class="availability docutils container">
  620. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
  621. </div>
  622. <div class="versionadded">
  623. <p><span class="versionmodified added">New in version 3.4.</span></p>
  624. </div>
  625. </dd></dl>
  626. <dl class="py function">
  627. <dt class="sig sig-object py" id="ssl.enum_crls">
  628. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">enum_crls</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">store_name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.enum_crls" title="Link to this definition">¶</a></dt>
  629. <dd><p>Retrieve CRLs from Windows’ system cert store. <em>store_name</em> may be
  630. one of <code class="docutils literal notranslate"><span class="pre">CA</span></code>, <code class="docutils literal notranslate"><span class="pre">ROOT</span></code> or <code class="docutils literal notranslate"><span class="pre">MY</span></code>. Windows may provide additional cert
  631. stores, too.</p>
  632. <p>The function returns a list of (cert_bytes, encoding_type, trust) tuples.
  633. The encoding_type specifies the encoding of cert_bytes. It is either
  634. <code class="xref py py-const docutils literal notranslate"><span class="pre">x509_asn</span></code> for X.509 ASN.1 data or <code class="xref py py-const docutils literal notranslate"><span class="pre">pkcs_7_asn</span></code> for
  635. PKCS#7 ASN.1 data.</p>
  636. <div class="availability docutils container">
  637. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
  638. </div>
  639. <div class="versionadded">
  640. <p><span class="versionmodified added">New in version 3.4.</span></p>
  641. </div>
  642. </dd></dl>
  643. </section>
  644. <section id="constants">
  645. <h3>Constants<a class="headerlink" href="#constants" title="Link to this heading">¶</a></h3>
  646. <blockquote>
  647. <div><p>All constants are now <a class="reference internal" href="enum.html#enum.IntEnum" title="enum.IntEnum"><code class="xref py py-class docutils literal notranslate"><span class="pre">enum.IntEnum</span></code></a> or <a class="reference internal" href="enum.html#enum.IntFlag" title="enum.IntFlag"><code class="xref py py-class docutils literal notranslate"><span class="pre">enum.IntFlag</span></code></a> collections.</p>
  648. <div class="versionadded">
  649. <p><span class="versionmodified added">New in version 3.6.</span></p>
  650. </div>
  651. </div></blockquote>
  652. <dl class="py data">
  653. <dt class="sig sig-object py" id="ssl.CERT_NONE">
  654. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">CERT_NONE</span></span><a class="headerlink" href="#ssl.CERT_NONE" title="Link to this definition">¶</a></dt>
  655. <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 notranslate"><span class="pre">SSLContext.verify_mode</span></code></a>.
  656. Except for <a class="reference internal" href="#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a>,
  657. it is the default mode. With client-side sockets, just about any
  658. cert is accepted. Validation errors, such as untrusted or expired cert,
  659. are ignored and do not abort the TLS/SSL handshake.</p>
  660. <p>In server mode, no certificate is requested from the client, so the client
  661. does not send any for client cert authentication.</p>
  662. <p>See the discussion of <a class="reference internal" href="#ssl-security"><span class="std std-ref">Security considerations</span></a> below.</p>
  663. </dd></dl>
  664. <dl class="py data">
  665. <dt class="sig sig-object py" id="ssl.CERT_OPTIONAL">
  666. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">CERT_OPTIONAL</span></span><a class="headerlink" href="#ssl.CERT_OPTIONAL" title="Link to this definition">¶</a></dt>
  667. <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 notranslate"><span class="pre">SSLContext.verify_mode</span></code></a>.
  668. In client mode, <a class="reference internal" href="#ssl.CERT_OPTIONAL" title="ssl.CERT_OPTIONAL"><code class="xref py py-const docutils literal notranslate"><span class="pre">CERT_OPTIONAL</span></code></a>
  669. has the same meaning as <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-const docutils literal notranslate"><span class="pre">CERT_REQUIRED</span></code></a>. It is recommended to
  670. use <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-const docutils literal notranslate"><span class="pre">CERT_REQUIRED</span></code></a> for client-side sockets instead.</p>
  671. <p>In server mode, a client certificate request is sent to the client. The
  672. client may either ignore the request or send a certificate in order
  673. perform TLS client cert authentication. If the client chooses to send
  674. a certificate, it is verified. Any verification error immediately aborts
  675. the TLS handshake.</p>
  676. <p>Use of this setting requires a valid set of CA certificates to
  677. be passed 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 notranslate"><span class="pre">SSLContext.load_verify_locations()</span></code></a>.</p>
  678. </dd></dl>
  679. <dl class="py data">
  680. <dt class="sig sig-object py" id="ssl.CERT_REQUIRED">
  681. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">CERT_REQUIRED</span></span><a class="headerlink" href="#ssl.CERT_REQUIRED" title="Link to this definition">¶</a></dt>
  682. <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 notranslate"><span class="pre">SSLContext.verify_mode</span></code></a>.
  683. In this mode, certificates are
  684. 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 notranslate"><span class="pre">SSLError</span></code></a>
  685. will be raised if no certificate is provided, or if its validation fails.
  686. This mode is <strong>not</strong> sufficient to verify a certificate in client mode as
  687. it does not match hostnames. <a class="reference internal" href="#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">check_hostname</span></code></a> must be
  688. enabled as well to verify the authenticity of a cert.
  689. <a class="reference internal" href="#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a> uses <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-const docutils literal notranslate"><span class="pre">CERT_REQUIRED</span></code></a> and
  690. enables <a class="reference internal" href="#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">check_hostname</span></code></a> by default.</p>
  691. <p>With server socket, this mode provides mandatory TLS client cert
  692. authentication. A client certificate request is sent to the client and
  693. the client must provide a valid and trusted certificate.</p>
  694. <p>Use of this setting requires a valid set of CA certificates to
  695. be passed 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 notranslate"><span class="pre">SSLContext.load_verify_locations()</span></code></a>.</p>
  696. </dd></dl>
  697. <dl class="py class">
  698. <dt class="sig sig-object py" id="ssl.VerifyMode">
  699. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">VerifyMode</span></span><a class="headerlink" href="#ssl.VerifyMode" title="Link to this definition">¶</a></dt>
  700. <dd><p><a class="reference internal" href="enum.html#enum.IntEnum" title="enum.IntEnum"><code class="xref py py-class docutils literal notranslate"><span class="pre">enum.IntEnum</span></code></a> collection of CERT_* constants.</p>
  701. <div class="versionadded">
  702. <p><span class="versionmodified added">New in version 3.6.</span></p>
  703. </div>
  704. </dd></dl>
  705. <dl class="py data">
  706. <dt class="sig sig-object py" id="ssl.VERIFY_DEFAULT">
  707. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">VERIFY_DEFAULT</span></span><a class="headerlink" href="#ssl.VERIFY_DEFAULT" title="Link to this definition">¶</a></dt>
  708. <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 notranslate"><span class="pre">SSLContext.verify_flags</span></code></a>. In this mode, certificate
  709. revocation lists (CRLs) are not checked. By default OpenSSL does neither
  710. require nor verify CRLs.</p>
  711. <div class="versionadded">
  712. <p><span class="versionmodified added">New in version 3.4.</span></p>
  713. </div>
  714. </dd></dl>
  715. <dl class="py data">
  716. <dt class="sig sig-object py" id="ssl.VERIFY_CRL_CHECK_LEAF">
  717. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">VERIFY_CRL_CHECK_LEAF</span></span><a class="headerlink" href="#ssl.VERIFY_CRL_CHECK_LEAF" title="Link to this definition">¶</a></dt>
  718. <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 notranslate"><span class="pre">SSLContext.verify_flags</span></code></a>. In this mode, only the
  719. peer cert is checked but none of the intermediate CA certificates. The mode
  720. requires a valid CRL that is signed by the peer cert’s issuer (its direct
  721. ancestor CA). If no proper CRL has been loaded with
  722. <a class="reference internal" href="#ssl.SSLContext.load_verify_locations" title="ssl.SSLContext.load_verify_locations"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.load_verify_locations</span></code></a>, validation will fail.</p>
  723. <div class="versionadded">
  724. <p><span class="versionmodified added">New in version 3.4.</span></p>
  725. </div>
  726. </dd></dl>
  727. <dl class="py data">
  728. <dt class="sig sig-object py" id="ssl.VERIFY_CRL_CHECK_CHAIN">
  729. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">VERIFY_CRL_CHECK_CHAIN</span></span><a class="headerlink" href="#ssl.VERIFY_CRL_CHECK_CHAIN" title="Link to this definition">¶</a></dt>
  730. <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 notranslate"><span class="pre">SSLContext.verify_flags</span></code></a>. In this mode, CRLs of
  731. all certificates in the peer cert chain are checked.</p>
  732. <div class="versionadded">
  733. <p><span class="versionmodified added">New in version 3.4.</span></p>
  734. </div>
  735. </dd></dl>
  736. <dl class="py data">
  737. <dt class="sig sig-object py" id="ssl.VERIFY_X509_STRICT">
  738. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">VERIFY_X509_STRICT</span></span><a class="headerlink" href="#ssl.VERIFY_X509_STRICT" title="Link to this definition">¶</a></dt>
  739. <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 notranslate"><span class="pre">SSLContext.verify_flags</span></code></a> to disable workarounds
  740. for broken X.509 certificates.</p>
  741. <div class="versionadded">
  742. <p><span class="versionmodified added">New in version 3.4.</span></p>
  743. </div>
  744. </dd></dl>
  745. <dl class="py data">
  746. <dt class="sig sig-object py" id="ssl.VERIFY_ALLOW_PROXY_CERTS">
  747. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">VERIFY_ALLOW_PROXY_CERTS</span></span><a class="headerlink" href="#ssl.VERIFY_ALLOW_PROXY_CERTS" title="Link to this definition">¶</a></dt>
  748. <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 notranslate"><span class="pre">SSLContext.verify_flags</span></code></a> to enables proxy
  749. certificate verification.</p>
  750. <div class="versionadded">
  751. <p><span class="versionmodified added">New in version 3.10.</span></p>
  752. </div>
  753. </dd></dl>
  754. <dl class="py data">
  755. <dt class="sig sig-object py" id="ssl.VERIFY_X509_TRUSTED_FIRST">
  756. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">VERIFY_X509_TRUSTED_FIRST</span></span><a class="headerlink" href="#ssl.VERIFY_X509_TRUSTED_FIRST" title="Link to this definition">¶</a></dt>
  757. <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 notranslate"><span class="pre">SSLContext.verify_flags</span></code></a>. It instructs OpenSSL to
  758. prefer trusted certificates when building the trust chain to validate a
  759. certificate. This flag is enabled by default.</p>
  760. <div class="versionadded">
  761. <p><span class="versionmodified added">New in version 3.4.4.</span></p>
  762. </div>
  763. </dd></dl>
  764. <dl class="py data">
  765. <dt class="sig sig-object py" id="ssl.VERIFY_X509_PARTIAL_CHAIN">
  766. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">VERIFY_X509_PARTIAL_CHAIN</span></span><a class="headerlink" href="#ssl.VERIFY_X509_PARTIAL_CHAIN" title="Link to this definition">¶</a></dt>
  767. <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 notranslate"><span class="pre">SSLContext.verify_flags</span></code></a>. It instructs OpenSSL to
  768. accept intermediate CAs in the trust store to be treated as trust-anchors,
  769. in the same way as the self-signed root CA certificates. This makes it
  770. possible to trust certificates issued by an intermediate CA without having
  771. to trust its ancestor root CA.</p>
  772. <div class="versionadded">
  773. <p><span class="versionmodified added">New in version 3.10.</span></p>
  774. </div>
  775. </dd></dl>
  776. <dl class="py class">
  777. <dt class="sig sig-object py" id="ssl.VerifyFlags">
  778. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">VerifyFlags</span></span><a class="headerlink" href="#ssl.VerifyFlags" title="Link to this definition">¶</a></dt>
  779. <dd><p><a class="reference internal" href="enum.html#enum.IntFlag" title="enum.IntFlag"><code class="xref py py-class docutils literal notranslate"><span class="pre">enum.IntFlag</span></code></a> collection of VERIFY_* constants.</p>
  780. <div class="versionadded">
  781. <p><span class="versionmodified added">New in version 3.6.</span></p>
  782. </div>
  783. </dd></dl>
  784. <dl class="py data">
  785. <dt class="sig sig-object py" id="ssl.PROTOCOL_TLS">
  786. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">PROTOCOL_TLS</span></span><a class="headerlink" href="#ssl.PROTOCOL_TLS" title="Link to this definition">¶</a></dt>
  787. <dd><p>Selects the highest protocol version that both the client and server support.
  788. Despite the name, this option can select both “SSL” and “TLS” protocols.</p>
  789. <div class="versionadded">
  790. <p><span class="versionmodified added">New in version 3.6.</span></p>
  791. </div>
  792. <div class="deprecated">
  793. <p><span class="versionmodified deprecated">Deprecated since version 3.10: </span>TLS clients and servers require different default settings for secure
  794. communication. The generic TLS protocol constant is deprecated in
  795. favor of <a class="reference internal" href="#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a> and <a class="reference internal" href="#ssl.PROTOCOL_TLS_SERVER" title="ssl.PROTOCOL_TLS_SERVER"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_SERVER</span></code></a>.</p>
  796. </div>
  797. </dd></dl>
  798. <dl class="py data">
  799. <dt class="sig sig-object py" id="ssl.PROTOCOL_TLS_CLIENT">
  800. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">PROTOCOL_TLS_CLIENT</span></span><a class="headerlink" href="#ssl.PROTOCOL_TLS_CLIENT" title="Link to this definition">¶</a></dt>
  801. <dd><p>Auto-negotiate the highest protocol version that both the client and
  802. server support, and configure the context client-side connections. The
  803. protocol enables <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-data docutils literal notranslate"><span class="pre">CERT_REQUIRED</span></code></a> and
  804. <a class="reference internal" href="#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">check_hostname</span></code></a> by default.</p>
  805. <div class="versionadded">
  806. <p><span class="versionmodified added">New in version 3.6.</span></p>
  807. </div>
  808. </dd></dl>
  809. <dl class="py data">
  810. <dt class="sig sig-object py" id="ssl.PROTOCOL_TLS_SERVER">
  811. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">PROTOCOL_TLS_SERVER</span></span><a class="headerlink" href="#ssl.PROTOCOL_TLS_SERVER" title="Link to this definition">¶</a></dt>
  812. <dd><p>Auto-negotiate the highest protocol version that both the client and
  813. server support, and configure the context server-side connections.</p>
  814. <div class="versionadded">
  815. <p><span class="versionmodified added">New in version 3.6.</span></p>
  816. </div>
  817. </dd></dl>
  818. <dl class="py data">
  819. <dt class="sig sig-object py" id="ssl.PROTOCOL_SSLv23">
  820. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">PROTOCOL_SSLv23</span></span><a class="headerlink" href="#ssl.PROTOCOL_SSLv23" title="Link to this definition">¶</a></dt>
  821. <dd><p>Alias for <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a>.</p>
  822. <div class="deprecated">
  823. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span>Use <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a> instead.</p>
  824. </div>
  825. </dd></dl>
  826. <dl class="py data">
  827. <dt class="sig sig-object py" id="ssl.PROTOCOL_SSLv3">
  828. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">PROTOCOL_SSLv3</span></span><a class="headerlink" href="#ssl.PROTOCOL_SSLv3" title="Link to this definition">¶</a></dt>
  829. <dd><p>Selects SSL version 3 as the channel encryption protocol.</p>
  830. <p>This protocol is not available if OpenSSL is compiled with the
  831. <code class="docutils literal notranslate"><span class="pre">no-ssl3</span></code> option.</p>
  832. <div class="admonition warning">
  833. <p class="admonition-title">Warning</p>
  834. <p>SSL version 3 is insecure. Its use is highly discouraged.</p>
  835. </div>
  836. <div class="deprecated">
  837. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span>OpenSSL has deprecated all version specific protocols. Use the default
  838. protocol <a class="reference internal" href="#ssl.PROTOCOL_TLS_SERVER" title="ssl.PROTOCOL_TLS_SERVER"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_SERVER</span></code></a> or <a class="reference internal" href="#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a>
  839. with <a class="reference internal" href="#ssl.SSLContext.minimum_version" title="ssl.SSLContext.minimum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.minimum_version</span></code></a> and
  840. <a class="reference internal" href="#ssl.SSLContext.maximum_version" title="ssl.SSLContext.maximum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.maximum_version</span></code></a> instead.</p>
  841. </div>
  842. </dd></dl>
  843. <dl class="py data">
  844. <dt class="sig sig-object py" id="ssl.PROTOCOL_TLSv1">
  845. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">PROTOCOL_TLSv1</span></span><a class="headerlink" href="#ssl.PROTOCOL_TLSv1" title="Link to this definition">¶</a></dt>
  846. <dd><p>Selects TLS version 1.0 as the channel encryption protocol.</p>
  847. <div class="deprecated">
  848. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span>OpenSSL has deprecated all version specific protocols.</p>
  849. </div>
  850. </dd></dl>
  851. <dl class="py data">
  852. <dt class="sig sig-object py" id="ssl.PROTOCOL_TLSv1_1">
  853. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">PROTOCOL_TLSv1_1</span></span><a class="headerlink" href="#ssl.PROTOCOL_TLSv1_1" title="Link to this definition">¶</a></dt>
  854. <dd><p>Selects TLS version 1.1 as the channel encryption protocol.
  855. Available only with openssl version 1.0.1+.</p>
  856. <div class="versionadded">
  857. <p><span class="versionmodified added">New in version 3.4.</span></p>
  858. </div>
  859. <div class="deprecated">
  860. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span>OpenSSL has deprecated all version specific protocols.</p>
  861. </div>
  862. </dd></dl>
  863. <dl class="py data">
  864. <dt class="sig sig-object py" id="ssl.PROTOCOL_TLSv1_2">
  865. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">PROTOCOL_TLSv1_2</span></span><a class="headerlink" href="#ssl.PROTOCOL_TLSv1_2" title="Link to this definition">¶</a></dt>
  866. <dd><p>Selects TLS version 1.2 as the channel encryption protocol.
  867. Available only with openssl version 1.0.1+.</p>
  868. <div class="versionadded">
  869. <p><span class="versionmodified added">New in version 3.4.</span></p>
  870. </div>
  871. <div class="deprecated">
  872. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span>OpenSSL has deprecated all version specific protocols.</p>
  873. </div>
  874. </dd></dl>
  875. <dl class="py data">
  876. <dt class="sig sig-object py" id="ssl.OP_ALL">
  877. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_ALL</span></span><a class="headerlink" href="#ssl.OP_ALL" title="Link to this definition">¶</a></dt>
  878. <dd><p>Enables workarounds for various bugs present in other SSL implementations.
  879. This option is set by default. It does not necessarily set the same
  880. flags as OpenSSL’s <code class="docutils literal notranslate"><span class="pre">SSL_OP_ALL</span></code> constant.</p>
  881. <div class="versionadded">
  882. <p><span class="versionmodified added">New in version 3.2.</span></p>
  883. </div>
  884. </dd></dl>
  885. <dl class="py data">
  886. <dt class="sig sig-object py" id="ssl.OP_NO_SSLv2">
  887. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_NO_SSLv2</span></span><a class="headerlink" href="#ssl.OP_NO_SSLv2" title="Link to this definition">¶</a></dt>
  888. <dd><p>Prevents an SSLv2 connection. This option is only applicable in
  889. conjunction with <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a>. It prevents the peers from
  890. choosing SSLv2 as the protocol version.</p>
  891. <div class="versionadded">
  892. <p><span class="versionmodified added">New in version 3.2.</span></p>
  893. </div>
  894. <div class="deprecated">
  895. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span>SSLv2 is deprecated</p>
  896. </div>
  897. </dd></dl>
  898. <dl class="py data">
  899. <dt class="sig sig-object py" id="ssl.OP_NO_SSLv3">
  900. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_NO_SSLv3</span></span><a class="headerlink" href="#ssl.OP_NO_SSLv3" title="Link to this definition">¶</a></dt>
  901. <dd><p>Prevents an SSLv3 connection. This option is only applicable in
  902. conjunction with <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a>. It prevents the peers from
  903. choosing SSLv3 as the protocol version.</p>
  904. <div class="versionadded">
  905. <p><span class="versionmodified added">New in version 3.2.</span></p>
  906. </div>
  907. <div class="deprecated">
  908. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span>SSLv3 is deprecated</p>
  909. </div>
  910. </dd></dl>
  911. <dl class="py data">
  912. <dt class="sig sig-object py" id="ssl.OP_NO_TLSv1">
  913. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_NO_TLSv1</span></span><a class="headerlink" href="#ssl.OP_NO_TLSv1" title="Link to this definition">¶</a></dt>
  914. <dd><p>Prevents a TLSv1 connection. This option is only applicable in
  915. conjunction with <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a>. It prevents the peers from
  916. choosing TLSv1 as the protocol version.</p>
  917. <div class="versionadded">
  918. <p><span class="versionmodified added">New in version 3.2.</span></p>
  919. </div>
  920. <div class="deprecated">
  921. <p><span class="versionmodified deprecated">Deprecated since version 3.7: </span>The option is deprecated since OpenSSL 1.1.0, use the new
  922. <a class="reference internal" href="#ssl.SSLContext.minimum_version" title="ssl.SSLContext.minimum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.minimum_version</span></code></a> and
  923. <a class="reference internal" href="#ssl.SSLContext.maximum_version" title="ssl.SSLContext.maximum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.maximum_version</span></code></a> instead.</p>
  924. </div>
  925. </dd></dl>
  926. <dl class="py data">
  927. <dt class="sig sig-object py" id="ssl.OP_NO_TLSv1_1">
  928. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_NO_TLSv1_1</span></span><a class="headerlink" href="#ssl.OP_NO_TLSv1_1" title="Link to this definition">¶</a></dt>
  929. <dd><p>Prevents a TLSv1.1 connection. This option is only applicable in conjunction
  930. with <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a>. It prevents the peers from choosing TLSv1.1 as
  931. the protocol version. Available only with openssl version 1.0.1+.</p>
  932. <div class="versionadded">
  933. <p><span class="versionmodified added">New in version 3.4.</span></p>
  934. </div>
  935. <div class="deprecated">
  936. <p><span class="versionmodified deprecated">Deprecated since version 3.7: </span>The option is deprecated since OpenSSL 1.1.0.</p>
  937. </div>
  938. </dd></dl>
  939. <dl class="py data">
  940. <dt class="sig sig-object py" id="ssl.OP_NO_TLSv1_2">
  941. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_NO_TLSv1_2</span></span><a class="headerlink" href="#ssl.OP_NO_TLSv1_2" title="Link to this definition">¶</a></dt>
  942. <dd><p>Prevents a TLSv1.2 connection. This option is only applicable in conjunction
  943. with <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a>. It prevents the peers from choosing TLSv1.2 as
  944. the protocol version. Available only with openssl version 1.0.1+.</p>
  945. <div class="versionadded">
  946. <p><span class="versionmodified added">New in version 3.4.</span></p>
  947. </div>
  948. <div class="deprecated">
  949. <p><span class="versionmodified deprecated">Deprecated since version 3.7: </span>The option is deprecated since OpenSSL 1.1.0.</p>
  950. </div>
  951. </dd></dl>
  952. <dl class="py data">
  953. <dt class="sig sig-object py" id="ssl.OP_NO_TLSv1_3">
  954. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_NO_TLSv1_3</span></span><a class="headerlink" href="#ssl.OP_NO_TLSv1_3" title="Link to this definition">¶</a></dt>
  955. <dd><p>Prevents a TLSv1.3 connection. This option is only applicable in conjunction
  956. with <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a>. It prevents the peers from choosing TLSv1.3 as
  957. the protocol version. TLS 1.3 is available with OpenSSL 1.1.1 or later.
  958. When Python has been compiled against an older version of OpenSSL, the
  959. flag defaults to <em>0</em>.</p>
  960. <div class="versionadded">
  961. <p><span class="versionmodified added">New in version 3.6.3.</span></p>
  962. </div>
  963. <div class="deprecated">
  964. <p><span class="versionmodified deprecated">Deprecated since version 3.7: </span>The option is deprecated since OpenSSL 1.1.0. It was added to 2.7.15 and
  965. 3.6.3 for backwards compatibility with OpenSSL 1.0.2.</p>
  966. </div>
  967. </dd></dl>
  968. <dl class="py data">
  969. <dt class="sig sig-object py" id="ssl.OP_NO_RENEGOTIATION">
  970. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_NO_RENEGOTIATION</span></span><a class="headerlink" href="#ssl.OP_NO_RENEGOTIATION" title="Link to this definition">¶</a></dt>
  971. <dd><p>Disable all renegotiation in TLSv1.2 and earlier. Do not send
  972. HelloRequest messages, and ignore renegotiation requests via ClientHello.</p>
  973. <p>This option is only available with OpenSSL 1.1.0h and later.</p>
  974. <div class="versionadded">
  975. <p><span class="versionmodified added">New in version 3.7.</span></p>
  976. </div>
  977. </dd></dl>
  978. <dl class="py data">
  979. <dt class="sig sig-object py" id="ssl.OP_CIPHER_SERVER_PREFERENCE">
  980. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_CIPHER_SERVER_PREFERENCE</span></span><a class="headerlink" href="#ssl.OP_CIPHER_SERVER_PREFERENCE" title="Link to this definition">¶</a></dt>
  981. <dd><p>Use the server’s cipher ordering preference, rather than the client’s.
  982. This option has no effect on client sockets and SSLv2 server sockets.</p>
  983. <div class="versionadded">
  984. <p><span class="versionmodified added">New in version 3.3.</span></p>
  985. </div>
  986. </dd></dl>
  987. <dl class="py data">
  988. <dt class="sig sig-object py" id="ssl.OP_SINGLE_DH_USE">
  989. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_SINGLE_DH_USE</span></span><a class="headerlink" href="#ssl.OP_SINGLE_DH_USE" title="Link to this definition">¶</a></dt>
  990. <dd><p>Prevents re-use of the same DH key for distinct SSL sessions. This
  991. improves forward secrecy but requires more computational resources.
  992. This option only applies to server sockets.</p>
  993. <div class="versionadded">
  994. <p><span class="versionmodified added">New in version 3.3.</span></p>
  995. </div>
  996. </dd></dl>
  997. <dl class="py data">
  998. <dt class="sig sig-object py" id="ssl.OP_SINGLE_ECDH_USE">
  999. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_SINGLE_ECDH_USE</span></span><a class="headerlink" href="#ssl.OP_SINGLE_ECDH_USE" title="Link to this definition">¶</a></dt>
  1000. <dd><p>Prevents re-use of the same ECDH key for distinct SSL sessions. This
  1001. improves forward secrecy but requires more computational resources.
  1002. This option only applies to server sockets.</p>
  1003. <div class="versionadded">
  1004. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1005. </div>
  1006. </dd></dl>
  1007. <dl class="py data">
  1008. <dt class="sig sig-object py" id="ssl.OP_ENABLE_MIDDLEBOX_COMPAT">
  1009. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_ENABLE_MIDDLEBOX_COMPAT</span></span><a class="headerlink" href="#ssl.OP_ENABLE_MIDDLEBOX_COMPAT" title="Link to this definition">¶</a></dt>
  1010. <dd><p>Send dummy Change Cipher Spec (CCS) messages in TLS 1.3 handshake to make
  1011. a TLS 1.3 connection look more like a TLS 1.2 connection.</p>
  1012. <p>This option is only available with OpenSSL 1.1.1 and later.</p>
  1013. <div class="versionadded">
  1014. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1015. </div>
  1016. </dd></dl>
  1017. <dl class="py data">
  1018. <dt class="sig sig-object py" id="ssl.OP_NO_COMPRESSION">
  1019. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_NO_COMPRESSION</span></span><a class="headerlink" href="#ssl.OP_NO_COMPRESSION" title="Link to this definition">¶</a></dt>
  1020. <dd><p>Disable compression on the SSL channel. This is useful if the application
  1021. protocol supports its own compression scheme.</p>
  1022. <div class="versionadded">
  1023. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1024. </div>
  1025. </dd></dl>
  1026. <dl class="py class">
  1027. <dt class="sig sig-object py" id="ssl.Options">
  1028. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">Options</span></span><a class="headerlink" href="#ssl.Options" title="Link to this definition">¶</a></dt>
  1029. <dd><p><a class="reference internal" href="enum.html#enum.IntFlag" title="enum.IntFlag"><code class="xref py py-class docutils literal notranslate"><span class="pre">enum.IntFlag</span></code></a> collection of OP_* constants.</p>
  1030. </dd></dl>
  1031. <dl class="py data">
  1032. <dt class="sig sig-object py" id="ssl.OP_NO_TICKET">
  1033. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_NO_TICKET</span></span><a class="headerlink" href="#ssl.OP_NO_TICKET" title="Link to this definition">¶</a></dt>
  1034. <dd><p>Prevent client side from requesting a session ticket.</p>
  1035. <div class="versionadded">
  1036. <p><span class="versionmodified added">New in version 3.6.</span></p>
  1037. </div>
  1038. </dd></dl>
  1039. <dl class="py data">
  1040. <dt class="sig sig-object py" id="ssl.OP_IGNORE_UNEXPECTED_EOF">
  1041. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_IGNORE_UNEXPECTED_EOF</span></span><a class="headerlink" href="#ssl.OP_IGNORE_UNEXPECTED_EOF" title="Link to this definition">¶</a></dt>
  1042. <dd><p>Ignore unexpected shutdown of TLS connections.</p>
  1043. <p>This option is only available with OpenSSL 3.0.0 and later.</p>
  1044. <div class="versionadded">
  1045. <p><span class="versionmodified added">New in version 3.10.</span></p>
  1046. </div>
  1047. </dd></dl>
  1048. <dl class="py data">
  1049. <dt class="sig sig-object py" id="ssl.OP_ENABLE_KTLS">
  1050. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_ENABLE_KTLS</span></span><a class="headerlink" href="#ssl.OP_ENABLE_KTLS" title="Link to this definition">¶</a></dt>
  1051. <dd><p>Enable the use of the kernel TLS. To benefit from the feature, OpenSSL must
  1052. have been compiled with support for it, and the negotiated cipher suites and
  1053. extensions must be supported by it (a list of supported ones may vary by
  1054. platform and kernel version).</p>
  1055. <p>Note that with enabled kernel TLS some cryptographic operations are
  1056. performed by the kernel directly and not via any available OpenSSL
  1057. Providers. This might be undesirable if, for example, the application
  1058. requires all cryptographic operations to be performed by the FIPS provider.</p>
  1059. <p>This option is only available with OpenSSL 3.0.0 and later.</p>
  1060. <div class="versionadded">
  1061. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1062. </div>
  1063. </dd></dl>
  1064. <dl class="py data">
  1065. <dt class="sig sig-object py" id="ssl.OP_LEGACY_SERVER_CONNECT">
  1066. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OP_LEGACY_SERVER_CONNECT</span></span><a class="headerlink" href="#ssl.OP_LEGACY_SERVER_CONNECT" title="Link to this definition">¶</a></dt>
  1067. <dd><p>Allow legacy insecure renegotiation between OpenSSL and unpatched servers
  1068. only.</p>
  1069. <div class="versionadded">
  1070. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1071. </div>
  1072. </dd></dl>
  1073. <dl class="py data">
  1074. <dt class="sig sig-object py" id="ssl.HAS_ALPN">
  1075. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">HAS_ALPN</span></span><a class="headerlink" href="#ssl.HAS_ALPN" title="Link to this definition">¶</a></dt>
  1076. <dd><p>Whether the OpenSSL library has built-in support for the <em>Application-Layer
  1077. Protocol Negotiation</em> TLS extension as described in <span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc7301.html"><strong>RFC 7301</strong></a>.</p>
  1078. <div class="versionadded">
  1079. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1080. </div>
  1081. </dd></dl>
  1082. <dl class="py data">
  1083. <dt class="sig sig-object py" id="ssl.HAS_NEVER_CHECK_COMMON_NAME">
  1084. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">HAS_NEVER_CHECK_COMMON_NAME</span></span><a class="headerlink" href="#ssl.HAS_NEVER_CHECK_COMMON_NAME" title="Link to this definition">¶</a></dt>
  1085. <dd><p>Whether the OpenSSL library has built-in support not checking subject
  1086. common name and <a class="reference internal" href="#ssl.SSLContext.hostname_checks_common_name" title="ssl.SSLContext.hostname_checks_common_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.hostname_checks_common_name</span></code></a> is
  1087. writeable.</p>
  1088. <div class="versionadded">
  1089. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1090. </div>
  1091. </dd></dl>
  1092. <dl class="py data">
  1093. <dt class="sig sig-object py" id="ssl.HAS_ECDH">
  1094. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">HAS_ECDH</span></span><a class="headerlink" href="#ssl.HAS_ECDH" title="Link to this definition">¶</a></dt>
  1095. <dd><p>Whether the OpenSSL library has built-in support for the Elliptic Curve-based
  1096. Diffie-Hellman key exchange. This should be true unless the feature was
  1097. explicitly disabled by the distributor.</p>
  1098. <div class="versionadded">
  1099. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1100. </div>
  1101. </dd></dl>
  1102. <dl class="py data">
  1103. <dt class="sig sig-object py" id="ssl.HAS_SNI">
  1104. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">HAS_SNI</span></span><a class="headerlink" href="#ssl.HAS_SNI" title="Link to this definition">¶</a></dt>
  1105. <dd><p>Whether the OpenSSL library has built-in support for the <em>Server Name
  1106. Indication</em> extension (as defined in <span class="target" id="index-8"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc6066.html"><strong>RFC 6066</strong></a>).</p>
  1107. <div class="versionadded">
  1108. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1109. </div>
  1110. </dd></dl>
  1111. <dl class="py data">
  1112. <dt class="sig sig-object py" id="ssl.HAS_NPN">
  1113. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">HAS_NPN</span></span><a class="headerlink" href="#ssl.HAS_NPN" title="Link to this definition">¶</a></dt>
  1114. <dd><p>Whether the OpenSSL library has built-in support for the <em>Next Protocol
  1115. Negotiation</em> as described in the <a class="reference external" href="https://en.wikipedia.org/wiki/Application-Layer_Protocol_Negotiation">Application Layer Protocol
  1116. Negotiation</a>.
  1117. When true, 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 notranslate"><span class="pre">SSLContext.set_npn_protocols()</span></code></a> method to advertise
  1118. which protocols you want to support.</p>
  1119. <div class="versionadded">
  1120. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1121. </div>
  1122. </dd></dl>
  1123. <dl class="py data">
  1124. <dt class="sig sig-object py" id="ssl.HAS_SSLv2">
  1125. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">HAS_SSLv2</span></span><a class="headerlink" href="#ssl.HAS_SSLv2" title="Link to this definition">¶</a></dt>
  1126. <dd><p>Whether the OpenSSL library has built-in support for the SSL 2.0 protocol.</p>
  1127. <div class="versionadded">
  1128. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1129. </div>
  1130. </dd></dl>
  1131. <dl class="py data">
  1132. <dt class="sig sig-object py" id="ssl.HAS_SSLv3">
  1133. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">HAS_SSLv3</span></span><a class="headerlink" href="#ssl.HAS_SSLv3" title="Link to this definition">¶</a></dt>
  1134. <dd><p>Whether the OpenSSL library has built-in support for the SSL 3.0 protocol.</p>
  1135. <div class="versionadded">
  1136. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1137. </div>
  1138. </dd></dl>
  1139. <dl class="py data">
  1140. <dt class="sig sig-object py" id="ssl.HAS_TLSv1">
  1141. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">HAS_TLSv1</span></span><a class="headerlink" href="#ssl.HAS_TLSv1" title="Link to this definition">¶</a></dt>
  1142. <dd><p>Whether the OpenSSL library has built-in support for the TLS 1.0 protocol.</p>
  1143. <div class="versionadded">
  1144. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1145. </div>
  1146. </dd></dl>
  1147. <dl class="py data">
  1148. <dt class="sig sig-object py" id="ssl.HAS_TLSv1_1">
  1149. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">HAS_TLSv1_1</span></span><a class="headerlink" href="#ssl.HAS_TLSv1_1" title="Link to this definition">¶</a></dt>
  1150. <dd><p>Whether the OpenSSL library has built-in support for the TLS 1.1 protocol.</p>
  1151. <div class="versionadded">
  1152. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1153. </div>
  1154. </dd></dl>
  1155. <dl class="py data">
  1156. <dt class="sig sig-object py" id="ssl.HAS_TLSv1_2">
  1157. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">HAS_TLSv1_2</span></span><a class="headerlink" href="#ssl.HAS_TLSv1_2" title="Link to this definition">¶</a></dt>
  1158. <dd><p>Whether the OpenSSL library has built-in support for the TLS 1.2 protocol.</p>
  1159. <div class="versionadded">
  1160. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1161. </div>
  1162. </dd></dl>
  1163. <dl class="py data">
  1164. <dt class="sig sig-object py" id="ssl.HAS_TLSv1_3">
  1165. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">HAS_TLSv1_3</span></span><a class="headerlink" href="#ssl.HAS_TLSv1_3" title="Link to this definition">¶</a></dt>
  1166. <dd><p>Whether the OpenSSL library has built-in support for the TLS 1.3 protocol.</p>
  1167. <div class="versionadded">
  1168. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1169. </div>
  1170. </dd></dl>
  1171. <dl class="py data">
  1172. <dt class="sig sig-object py" id="ssl.CHANNEL_BINDING_TYPES">
  1173. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">CHANNEL_BINDING_TYPES</span></span><a class="headerlink" href="#ssl.CHANNEL_BINDING_TYPES" title="Link to this definition">¶</a></dt>
  1174. <dd><p>List of supported TLS channel binding types. Strings in this list
  1175. 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 notranslate"><span class="pre">SSLSocket.get_channel_binding()</span></code></a>.</p>
  1176. <div class="versionadded">
  1177. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1178. </div>
  1179. </dd></dl>
  1180. <dl class="py data">
  1181. <dt class="sig sig-object py" id="ssl.OPENSSL_VERSION">
  1182. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OPENSSL_VERSION</span></span><a class="headerlink" href="#ssl.OPENSSL_VERSION" title="Link to this definition">¶</a></dt>
  1183. <dd><p>The version string of the OpenSSL library loaded by the interpreter:</p>
  1184. <div class="highlight-python3 notranslate"><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>
  1185. <span class="go">&#39;OpenSSL 1.0.2k 26 Jan 2017&#39;</span>
  1186. </pre></div>
  1187. </div>
  1188. <div class="versionadded">
  1189. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1190. </div>
  1191. </dd></dl>
  1192. <dl class="py data">
  1193. <dt class="sig sig-object py" id="ssl.OPENSSL_VERSION_INFO">
  1194. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OPENSSL_VERSION_INFO</span></span><a class="headerlink" href="#ssl.OPENSSL_VERSION_INFO" title="Link to this definition">¶</a></dt>
  1195. <dd><p>A tuple of five integers representing version information about the
  1196. OpenSSL library:</p>
  1197. <div class="highlight-python3 notranslate"><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>
  1198. <span class="go">(1, 0, 2, 11, 15)</span>
  1199. </pre></div>
  1200. </div>
  1201. <div class="versionadded">
  1202. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1203. </div>
  1204. </dd></dl>
  1205. <dl class="py data">
  1206. <dt class="sig sig-object py" id="ssl.OPENSSL_VERSION_NUMBER">
  1207. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">OPENSSL_VERSION_NUMBER</span></span><a class="headerlink" href="#ssl.OPENSSL_VERSION_NUMBER" title="Link to this definition">¶</a></dt>
  1208. <dd><p>The raw version number of the OpenSSL library, as a single integer:</p>
  1209. <div class="highlight-python3 notranslate"><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>
  1210. <span class="go">268443839</span>
  1211. <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>
  1212. <span class="go">&#39;0x100020bf&#39;</span>
  1213. </pre></div>
  1214. </div>
  1215. <div class="versionadded">
  1216. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1217. </div>
  1218. </dd></dl>
  1219. <dl class="py data">
  1220. <dt class="sig sig-object py" id="ssl.ALERT_DESCRIPTION_HANDSHAKE_FAILURE">
  1221. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">ALERT_DESCRIPTION_HANDSHAKE_FAILURE</span></span><a class="headerlink" href="#ssl.ALERT_DESCRIPTION_HANDSHAKE_FAILURE" title="Link to this definition">¶</a></dt>
  1222. <dt class="sig sig-object py" id="ssl.ALERT_DESCRIPTION_INTERNAL_ERROR">
  1223. <span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">ALERT_DESCRIPTION_INTERNAL_ERROR</span></span><a class="headerlink" href="#ssl.ALERT_DESCRIPTION_INTERNAL_ERROR" title="Link to this definition">¶</a></dt>
  1224. <dt class="sig sig-object py">
  1225. <span class="sig-name descname"><span class="pre">ALERT_DESCRIPTION_*</span></span></dt>
  1226. <dd><p>Alert Descriptions from <span class="target" id="index-9"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/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>
  1227. contains this list and references to the RFCs where their meaning is defined.</p>
  1228. <p>Used as the return value of the callback function in
  1229. <a class="reference internal" href="#ssl.SSLContext.set_servername_callback" title="ssl.SSLContext.set_servername_callback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.set_servername_callback()</span></code></a>.</p>
  1230. <div class="versionadded">
  1231. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1232. </div>
  1233. </dd></dl>
  1234. <dl class="py class">
  1235. <dt class="sig sig-object py" id="ssl.AlertDescription">
  1236. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">AlertDescription</span></span><a class="headerlink" href="#ssl.AlertDescription" title="Link to this definition">¶</a></dt>
  1237. <dd><p><a class="reference internal" href="enum.html#enum.IntEnum" title="enum.IntEnum"><code class="xref py py-class docutils literal notranslate"><span class="pre">enum.IntEnum</span></code></a> collection of ALERT_DESCRIPTION_* constants.</p>
  1238. <div class="versionadded">
  1239. <p><span class="versionmodified added">New in version 3.6.</span></p>
  1240. </div>
  1241. </dd></dl>
  1242. <dl class="py data">
  1243. <dt class="sig sig-object py" id="ssl.Purpose.SERVER_AUTH">
  1244. <span class="sig-prename descclassname"><span class="pre">Purpose.</span></span><span class="sig-name descname"><span class="pre">SERVER_AUTH</span></span><a class="headerlink" href="#ssl.Purpose.SERVER_AUTH" title="Link to this definition">¶</a></dt>
  1245. <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 notranslate"><span class="pre">create_default_context()</span></code></a> and
  1246. <a class="reference internal" href="#ssl.SSLContext.load_default_certs" title="ssl.SSLContext.load_default_certs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.load_default_certs()</span></code></a>. This value indicates that the
  1247. context may be used to authenticate web servers (therefore, it will
  1248. be used to create client-side sockets).</p>
  1249. <div class="versionadded">
  1250. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1251. </div>
  1252. </dd></dl>
  1253. <dl class="py data">
  1254. <dt class="sig sig-object py" id="ssl.Purpose.CLIENT_AUTH">
  1255. <span class="sig-prename descclassname"><span class="pre">Purpose.</span></span><span class="sig-name descname"><span class="pre">CLIENT_AUTH</span></span><a class="headerlink" href="#ssl.Purpose.CLIENT_AUTH" title="Link to this definition">¶</a></dt>
  1256. <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 notranslate"><span class="pre">create_default_context()</span></code></a> and
  1257. <a class="reference internal" href="#ssl.SSLContext.load_default_certs" title="ssl.SSLContext.load_default_certs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.load_default_certs()</span></code></a>. This value indicates that the
  1258. context may be used to authenticate web clients (therefore, it will
  1259. be used to create server-side sockets).</p>
  1260. <div class="versionadded">
  1261. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1262. </div>
  1263. </dd></dl>
  1264. <dl class="py class">
  1265. <dt class="sig sig-object py" id="ssl.SSLErrorNumber">
  1266. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">SSLErrorNumber</span></span><a class="headerlink" href="#ssl.SSLErrorNumber" title="Link to this definition">¶</a></dt>
  1267. <dd><p><a class="reference internal" href="enum.html#enum.IntEnum" title="enum.IntEnum"><code class="xref py py-class docutils literal notranslate"><span class="pre">enum.IntEnum</span></code></a> collection of SSL_ERROR_* constants.</p>
  1268. <div class="versionadded">
  1269. <p><span class="versionmodified added">New in version 3.6.</span></p>
  1270. </div>
  1271. </dd></dl>
  1272. <dl class="py class">
  1273. <dt class="sig sig-object py" id="ssl.TLSVersion">
  1274. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">TLSVersion</span></span><a class="headerlink" href="#ssl.TLSVersion" title="Link to this definition">¶</a></dt>
  1275. <dd><p><a class="reference internal" href="enum.html#enum.IntEnum" title="enum.IntEnum"><code class="xref py py-class docutils literal notranslate"><span class="pre">enum.IntEnum</span></code></a> collection of SSL and TLS versions for
  1276. <a class="reference internal" href="#ssl.SSLContext.maximum_version" title="ssl.SSLContext.maximum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.maximum_version</span></code></a> and <a class="reference internal" href="#ssl.SSLContext.minimum_version" title="ssl.SSLContext.minimum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.minimum_version</span></code></a>.</p>
  1277. <div class="versionadded">
  1278. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1279. </div>
  1280. </dd></dl>
  1281. <dl class="py attribute">
  1282. <dt class="sig sig-object py" id="ssl.TLSVersion.MINIMUM_SUPPORTED">
  1283. <span class="sig-prename descclassname"><span class="pre">TLSVersion.</span></span><span class="sig-name descname"><span class="pre">MINIMUM_SUPPORTED</span></span><a class="headerlink" href="#ssl.TLSVersion.MINIMUM_SUPPORTED" title="Link to this definition">¶</a></dt>
  1284. <dd></dd></dl>
  1285. <dl class="py attribute">
  1286. <dt class="sig sig-object py" id="ssl.TLSVersion.MAXIMUM_SUPPORTED">
  1287. <span class="sig-prename descclassname"><span class="pre">TLSVersion.</span></span><span class="sig-name descname"><span class="pre">MAXIMUM_SUPPORTED</span></span><a class="headerlink" href="#ssl.TLSVersion.MAXIMUM_SUPPORTED" title="Link to this definition">¶</a></dt>
  1288. <dd><p>The minimum or maximum supported SSL or TLS version. These are magic
  1289. constants. Their values don’t reflect the lowest and highest available
  1290. TLS/SSL versions.</p>
  1291. </dd></dl>
  1292. <dl class="py attribute">
  1293. <dt class="sig sig-object py" id="ssl.TLSVersion.SSLv3">
  1294. <span class="sig-prename descclassname"><span class="pre">TLSVersion.</span></span><span class="sig-name descname"><span class="pre">SSLv3</span></span><a class="headerlink" href="#ssl.TLSVersion.SSLv3" title="Link to this definition">¶</a></dt>
  1295. <dd></dd></dl>
  1296. <dl class="py attribute">
  1297. <dt class="sig sig-object py" id="ssl.TLSVersion.TLSv1">
  1298. <span class="sig-prename descclassname"><span class="pre">TLSVersion.</span></span><span class="sig-name descname"><span class="pre">TLSv1</span></span><a class="headerlink" href="#ssl.TLSVersion.TLSv1" title="Link to this definition">¶</a></dt>
  1299. <dd></dd></dl>
  1300. <dl class="py attribute">
  1301. <dt class="sig sig-object py" id="ssl.TLSVersion.TLSv1_1">
  1302. <span class="sig-prename descclassname"><span class="pre">TLSVersion.</span></span><span class="sig-name descname"><span class="pre">TLSv1_1</span></span><a class="headerlink" href="#ssl.TLSVersion.TLSv1_1" title="Link to this definition">¶</a></dt>
  1303. <dd></dd></dl>
  1304. <dl class="py attribute">
  1305. <dt class="sig sig-object py" id="ssl.TLSVersion.TLSv1_2">
  1306. <span class="sig-prename descclassname"><span class="pre">TLSVersion.</span></span><span class="sig-name descname"><span class="pre">TLSv1_2</span></span><a class="headerlink" href="#ssl.TLSVersion.TLSv1_2" title="Link to this definition">¶</a></dt>
  1307. <dd></dd></dl>
  1308. <dl class="py attribute">
  1309. <dt class="sig sig-object py" id="ssl.TLSVersion.TLSv1_3">
  1310. <span class="sig-prename descclassname"><span class="pre">TLSVersion.</span></span><span class="sig-name descname"><span class="pre">TLSv1_3</span></span><a class="headerlink" href="#ssl.TLSVersion.TLSv1_3" title="Link to this definition">¶</a></dt>
  1311. <dd><p>SSL 3.0 to TLS 1.3.</p>
  1312. <div class="deprecated">
  1313. <p><span class="versionmodified deprecated">Deprecated since version 3.10: </span>All <a class="reference internal" href="#ssl.TLSVersion" title="ssl.TLSVersion"><code class="xref py py-class docutils literal notranslate"><span class="pre">TLSVersion</span></code></a> members except <a class="reference internal" href="#ssl.TLSVersion.TLSv1_2" title="ssl.TLSVersion.TLSv1_2"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TLSVersion.TLSv1_2</span></code></a> and
  1314. <a class="reference internal" href="#ssl.TLSVersion.TLSv1_3" title="ssl.TLSVersion.TLSv1_3"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TLSVersion.TLSv1_3</span></code></a> are deprecated.</p>
  1315. </div>
  1316. </dd></dl>
  1317. </section>
  1318. </section>
  1319. <section id="ssl-sockets">
  1320. <h2>SSL Sockets<a class="headerlink" href="#ssl-sockets" title="Link to this heading">¶</a></h2>
  1321. <dl class="py class">
  1322. <dt class="sig sig-object py" id="ssl.SSLSocket">
  1323. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">SSLSocket</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">socket.socket</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket" title="Link to this definition">¶</a></dt>
  1324. <dd><p>SSL sockets provide the following methods of <a class="reference internal" href="socket.html#socket-objects"><span class="std std-ref">Socket Objects</span></a>:</p>
  1325. <ul class="simple">
  1326. <li><p><a class="reference internal" href="socket.html#socket.socket.accept" title="socket.socket.accept"><code class="xref py py-meth docutils literal notranslate"><span class="pre">accept()</span></code></a></p></li>
  1327. <li><p><a class="reference internal" href="socket.html#socket.socket.bind" title="socket.socket.bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bind()</span></code></a></p></li>
  1328. <li><p><a class="reference internal" href="socket.html#socket.socket.close" title="socket.socket.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a></p></li>
  1329. <li><p><a class="reference internal" href="socket.html#socket.socket.connect" title="socket.socket.connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">connect()</span></code></a></p></li>
  1330. <li><p><a class="reference internal" href="socket.html#socket.socket.detach" title="socket.socket.detach"><code class="xref py py-meth docutils literal notranslate"><span class="pre">detach()</span></code></a></p></li>
  1331. <li><p><a class="reference internal" href="socket.html#socket.socket.fileno" title="socket.socket.fileno"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fileno()</span></code></a></p></li>
  1332. <li><p><a class="reference internal" href="socket.html#socket.socket.getpeername" title="socket.socket.getpeername"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">getsockname()</span></code></a></p></li>
  1333. <li><p><a class="reference internal" href="socket.html#socket.socket.getsockopt" title="socket.socket.getsockopt"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">setsockopt()</span></code></a></p></li>
  1334. <li><p><a class="reference internal" href="socket.html#socket.socket.gettimeout" title="socket.socket.gettimeout"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">settimeout()</span></code></a>,
  1335. <a class="reference internal" href="socket.html#socket.socket.setblocking" title="socket.socket.setblocking"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setblocking()</span></code></a></p></li>
  1336. <li><p><a class="reference internal" href="socket.html#socket.socket.listen" title="socket.socket.listen"><code class="xref py py-meth docutils literal notranslate"><span class="pre">listen()</span></code></a></p></li>
  1337. <li><p><a class="reference internal" href="socket.html#socket.socket.makefile" title="socket.socket.makefile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">makefile()</span></code></a></p></li>
  1338. <li><p><a class="reference internal" href="socket.html#socket.socket.recv" title="socket.socket.recv"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">recv_into()</span></code></a>
  1339. (but passing a non-zero <code class="docutils literal notranslate"><span class="pre">flags</span></code> argument is not allowed)</p></li>
  1340. <li><p><a class="reference internal" href="socket.html#socket.socket.send" title="socket.socket.send"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">sendall()</span></code></a> (with
  1341. the same limitation)</p></li>
  1342. <li><p><a class="reference internal" href="socket.html#socket.socket.sendfile" title="socket.socket.sendfile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sendfile()</span></code></a> (but <a class="reference internal" href="os.html#os.sendfile" title="os.sendfile"><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.sendfile</span></code></a> will be used
  1343. for plain-text sockets only, else <a class="reference internal" href="socket.html#socket.socket.send" title="socket.socket.send"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send()</span></code></a> will be used)</p></li>
  1344. <li><p><a class="reference internal" href="socket.html#socket.socket.shutdown" title="socket.socket.shutdown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">shutdown()</span></code></a></p></li>
  1345. </ul>
  1346. <p>However, since the SSL (and TLS) protocol has its own framing atop
  1347. of TCP, the SSL sockets abstraction can, in certain respects, diverge from
  1348. the specification of normal, OS-level sockets. See especially the
  1349. <a class="reference internal" href="#ssl-nonblocking"><span class="std std-ref">notes on non-blocking sockets</span></a>.</p>
  1350. <p>Instances of <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> must be created using the
  1351. <a class="reference internal" href="#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.wrap_socket()</span></code></a> method.</p>
  1352. <div class="versionchanged">
  1353. <p><span class="versionmodified changed">Changed in version 3.5: </span>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">sendfile()</span></code> method was added.</p>
  1354. </div>
  1355. <div class="versionchanged">
  1356. <p><span class="versionmodified changed">Changed in version 3.5: </span>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">shutdown()</span></code> does not reset the socket timeout each time bytes
  1357. are received or sent. The socket timeout is now the maximum total duration
  1358. of the shutdown.</p>
  1359. </div>
  1360. <div class="deprecated">
  1361. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span>It is deprecated to create a <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> instance directly, use
  1362. <a class="reference internal" href="#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.wrap_socket()</span></code></a> to wrap a socket.</p>
  1363. </div>
  1364. <div class="versionchanged">
  1365. <p><span class="versionmodified changed">Changed in version 3.7: </span><a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> instances must to created with
  1366. <a class="reference internal" href="#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wrap_socket()</span></code></a>. In earlier versions, it was possible
  1367. to create instances directly. This was never documented or officially
  1368. supported.</p>
  1369. </div>
  1370. <div class="versionchanged">
  1371. <p><span class="versionmodified changed">Changed in version 3.10: </span>Python now uses <code class="docutils literal notranslate"><span class="pre">SSL_read_ex</span></code> and <code class="docutils literal notranslate"><span class="pre">SSL_write_ex</span></code> internally. The
  1372. functions support reading and writing of data larger than 2 GB. Writing
  1373. zero-length data no longer fails with a protocol violation error.</p>
  1374. </div>
  1375. </dd></dl>
  1376. <p>SSL sockets also have the following additional methods and attributes:</p>
  1377. <dl class="py method">
  1378. <dt class="sig sig-object py" id="ssl.SSLSocket.read">
  1379. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">read</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">len</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1024</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffer</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.read" title="Link to this definition">¶</a></dt>
  1380. <dd><p>Read up to <em>len</em> bytes of data from the SSL socket and return the result as
  1381. a <code class="docutils literal notranslate"><span class="pre">bytes</span></code> instance. If <em>buffer</em> is specified, then read into the buffer
  1382. instead, and return the number of bytes read.</p>
  1383. <p>Raise <a class="reference internal" href="#ssl.SSLWantReadError" title="ssl.SSLWantReadError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLWantReadError</span></code></a> or <a class="reference internal" href="#ssl.SSLWantWriteError" title="ssl.SSLWantWriteError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLWantWriteError</span></code></a> if the socket is
  1384. <a class="reference internal" href="#ssl-nonblocking"><span class="std std-ref">non-blocking</span></a> and the read would block.</p>
  1385. <p>As at any time a re-negotiation is possible, a call to <a class="reference internal" href="#ssl.SSLSocket.read" title="ssl.SSLSocket.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> can also
  1386. cause write operations.</p>
  1387. <div class="versionchanged">
  1388. <p><span class="versionmodified changed">Changed in version 3.5: </span>The socket timeout is no longer reset each time bytes are received or sent.
  1389. The socket timeout is now the maximum total duration to read up to <em>len</em>
  1390. bytes.</p>
  1391. </div>
  1392. <div class="deprecated">
  1393. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span>Use <code class="xref py py-meth docutils literal notranslate"><span class="pre">recv()</span></code> instead of <a class="reference internal" href="#ssl.SSLSocket.read" title="ssl.SSLSocket.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a>.</p>
  1394. </div>
  1395. </dd></dl>
  1396. <dl class="py method">
  1397. <dt class="sig sig-object py" id="ssl.SSLSocket.write">
  1398. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">buf</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.write" title="Link to this definition">¶</a></dt>
  1399. <dd><p>Write <em>buf</em> to the SSL socket and return the number of bytes written. The
  1400. <em>buf</em> argument must be an object supporting the buffer interface.</p>
  1401. <p>Raise <a class="reference internal" href="#ssl.SSLWantReadError" title="ssl.SSLWantReadError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLWantReadError</span></code></a> or <a class="reference internal" href="#ssl.SSLWantWriteError" title="ssl.SSLWantWriteError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLWantWriteError</span></code></a> if the socket is
  1402. <a class="reference internal" href="#ssl-nonblocking"><span class="std std-ref">non-blocking</span></a> and the write would block.</p>
  1403. <p>As at any time a re-negotiation is possible, a call to <a class="reference internal" href="#ssl.SSLSocket.write" title="ssl.SSLSocket.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> can
  1404. also cause read operations.</p>
  1405. <div class="versionchanged">
  1406. <p><span class="versionmodified changed">Changed in version 3.5: </span>The socket timeout is no longer reset each time bytes are received or sent.
  1407. The socket timeout is now the maximum total duration to write <em>buf</em>.</p>
  1408. </div>
  1409. <div class="deprecated">
  1410. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span>Use <code class="xref py py-meth docutils literal notranslate"><span class="pre">send()</span></code> instead of <a class="reference internal" href="#ssl.SSLSocket.write" title="ssl.SSLSocket.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a>.</p>
  1411. </div>
  1412. </dd></dl>
  1413. <div class="admonition note">
  1414. <p class="admonition-title">Note</p>
  1415. <p>The <a class="reference internal" href="#ssl.SSLSocket.read" title="ssl.SSLSocket.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> and <a class="reference internal" href="#ssl.SSLSocket.write" title="ssl.SSLSocket.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> methods are the
  1416. low-level methods that read and write unencrypted, application-level data
  1417. and decrypt/encrypt it to encrypted, wire-level data. These methods
  1418. require an active SSL connection, i.e. the handshake was completed and
  1419. <a class="reference internal" href="#ssl.SSLSocket.unwrap" title="ssl.SSLSocket.unwrap"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.unwrap()</span></code></a> was not called.</p>
  1420. <p>Normally you should use the socket API methods like
  1421. <a class="reference internal" href="socket.html#socket.socket.recv" title="socket.socket.recv"><code class="xref py py-meth docutils literal notranslate"><span class="pre">recv()</span></code></a> and <a class="reference internal" href="socket.html#socket.socket.send" title="socket.socket.send"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send()</span></code></a> instead of these
  1422. methods.</p>
  1423. </div>
  1424. <dl class="py method">
  1425. <dt class="sig sig-object py" id="ssl.SSLSocket.do_handshake">
  1426. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">do_handshake</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.do_handshake" title="Link to this definition">¶</a></dt>
  1427. <dd><p>Perform the SSL setup handshake.</p>
  1428. <div class="versionchanged">
  1429. <p><span class="versionmodified changed">Changed in version 3.4: </span>The handshake method also performs <code class="xref py py-func docutils literal notranslate"><span class="pre">match_hostname()</span></code> when the
  1430. <a class="reference internal" href="#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">check_hostname</span></code></a> attribute of the socket’s
  1431. <a class="reference internal" href="#ssl.SSLSocket.context" title="ssl.SSLSocket.context"><code class="xref py py-attr docutils literal notranslate"><span class="pre">context</span></code></a> is true.</p>
  1432. </div>
  1433. <div class="versionchanged">
  1434. <p><span class="versionmodified changed">Changed in version 3.5: </span>The socket timeout is no longer reset each time bytes are received or sent.
  1435. The socket timeout is now the maximum total duration of the handshake.</p>
  1436. </div>
  1437. <div class="versionchanged">
  1438. <p><span class="versionmodified changed">Changed in version 3.7: </span>Hostname or IP address is matched by OpenSSL during handshake. The
  1439. function <code class="xref py py-func docutils literal notranslate"><span class="pre">match_hostname()</span></code> is no longer used. In case OpenSSL
  1440. refuses a hostname or IP address, the handshake is aborted early and
  1441. a TLS alert message is sent to the peer.</p>
  1442. </div>
  1443. </dd></dl>
  1444. <dl class="py method">
  1445. <dt class="sig sig-object py" id="ssl.SSLSocket.getpeercert">
  1446. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">getpeercert</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">binary_form</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.getpeercert" title="Link to this definition">¶</a></dt>
  1447. <dd><p>If there is no certificate for the peer on the other end of the connection,
  1448. return <code class="docutils literal notranslate"><span class="pre">None</span></code>. If the SSL handshake hasn’t been done yet, raise
  1449. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
  1450. <p>If the <code class="docutils literal notranslate"><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 notranslate"><span class="pre">False</span></code></a>, and a certificate was
  1451. 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 notranslate"><span class="pre">dict</span></code></a> instance. If the
  1452. certificate was not validated, the dict is empty. If the certificate was
  1453. validated, it returns a dict with several keys, amongst them <code class="docutils literal notranslate"><span class="pre">subject</span></code>
  1454. (the principal for which the certificate was issued) and <code class="docutils literal notranslate"><span class="pre">issuer</span></code>
  1455. (the principal issuing the certificate). If a certificate contains an
  1456. instance of the <em>Subject Alternative Name</em> extension (see <span class="target" id="index-10"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3280.html"><strong>RFC 3280</strong></a>),
  1457. there will also be a <code class="docutils literal notranslate"><span class="pre">subjectAltName</span></code> key in the dictionary.</p>
  1458. <p>The <code class="docutils literal notranslate"><span class="pre">subject</span></code> and <code class="docutils literal notranslate"><span class="pre">issuer</span></code> fields are tuples containing the sequence
  1459. of relative distinguished names (RDNs) given in the certificate’s data
  1460. structure for the respective fields, and each RDN is a sequence of
  1461. name-value pairs. Here is a real-world example:</p>
  1462. <div class="highlight-python3 notranslate"><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>
  1463. <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>
  1464. <span class="p">((</span><span class="s1">&#39;organizationalUnitName&#39;</span><span class="p">,</span>
  1465. <span class="s1">&#39;Secure Digital Certificate Signing&#39;</span><span class="p">),),</span>
  1466. <span class="p">((</span><span class="s1">&#39;commonName&#39;</span><span class="p">,</span>
  1467. <span class="s1">&#39;StartCom Class 2 Primary Intermediate Server CA&#39;</span><span class="p">),)),</span>
  1468. <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>
  1469. <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>
  1470. <span class="s1">&#39;serialNumber&#39;</span><span class="p">:</span> <span class="s1">&#39;95F0&#39;</span><span class="p">,</span>
  1471. <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>
  1472. <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>
  1473. <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>
  1474. <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>
  1475. <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>
  1476. <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>
  1477. <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>
  1478. <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>
  1479. <span class="s1">&#39;version&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">}</span>
  1480. </pre></div>
  1481. </div>
  1482. <p>If the <code class="docutils literal notranslate"><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 notranslate"><span class="pre">True</span></code></a>, and a certificate was
  1483. provided, this method returns the DER-encoded form of the entire certificate
  1484. as a sequence of bytes, or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> if the peer did not provide a
  1485. certificate. Whether the peer provides a certificate depends on the SSL
  1486. socket’s role:</p>
  1487. <ul class="simple">
  1488. <li><p>for a client SSL socket, the server will always provide a certificate,
  1489. regardless of whether validation was required;</p></li>
  1490. <li><p>for a server SSL socket, the client will only provide a certificate
  1491. 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 notranslate"><span class="pre">getpeercert()</span></code></a> will return
  1492. <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><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 notranslate"><span class="pre">CERT_NONE</span></code></a> (rather than
  1493. <a class="reference internal" href="#ssl.CERT_OPTIONAL" title="ssl.CERT_OPTIONAL"><code class="xref py py-const docutils literal notranslate"><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 notranslate"><span class="pre">CERT_REQUIRED</span></code></a>).</p></li>
  1494. </ul>
  1495. <p>See also <a class="reference internal" href="#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.check_hostname</span></code></a>.</p>
  1496. <div class="versionchanged">
  1497. <p><span class="versionmodified changed">Changed in version 3.2: </span>The returned dictionary includes additional items such as <code class="docutils literal notranslate"><span class="pre">issuer</span></code>
  1498. and <code class="docutils literal notranslate"><span class="pre">notBefore</span></code>.</p>
  1499. </div>
  1500. <div class="versionchanged">
  1501. <p><span class="versionmodified changed">Changed in version 3.4: </span><a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised when the handshake isn’t done.
  1502. The returned dictionary includes additional X509v3 extension items
  1503. such as <code class="docutils literal notranslate"><span class="pre">crlDistributionPoints</span></code>, <code class="docutils literal notranslate"><span class="pre">caIssuers</span></code> and <code class="docutils literal notranslate"><span class="pre">OCSP</span></code> URIs.</p>
  1504. </div>
  1505. <div class="versionchanged">
  1506. <p><span class="versionmodified changed">Changed in version 3.9: </span>IPv6 address strings no longer have a trailing new line.</p>
  1507. </div>
  1508. </dd></dl>
  1509. <dl class="py method">
  1510. <dt class="sig sig-object py" id="ssl.SSLSocket.cipher">
  1511. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">cipher</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.cipher" title="Link to this definition">¶</a></dt>
  1512. <dd><p>Returns a three-value tuple containing the name of the cipher being used, the
  1513. version of the SSL protocol that defines its use, and the number of secret
  1514. bits being used. If no connection has been established, returns <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1515. </dd></dl>
  1516. <dl class="py method">
  1517. <dt class="sig sig-object py" id="ssl.SSLSocket.shared_ciphers">
  1518. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">shared_ciphers</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.shared_ciphers" title="Link to this definition">¶</a></dt>
  1519. <dd><p>Return the list of ciphers available in both the client and server. Each
  1520. entry of the returned list is a three-value tuple containing the name of the
  1521. cipher, the version of the SSL protocol that defines its use, and the number
  1522. of secret bits the cipher uses. <a class="reference internal" href="#ssl.SSLSocket.shared_ciphers" title="ssl.SSLSocket.shared_ciphers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">shared_ciphers()</span></code></a> returns
  1523. <code class="docutils literal notranslate"><span class="pre">None</span></code> if no connection has been established or the socket is a client
  1524. socket.</p>
  1525. <div class="versionadded">
  1526. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1527. </div>
  1528. </dd></dl>
  1529. <dl class="py method">
  1530. <dt class="sig sig-object py" id="ssl.SSLSocket.compression">
  1531. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">compression</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.compression" title="Link to this definition">¶</a></dt>
  1532. <dd><p>Return the compression algorithm being used as a string, or <code class="docutils literal notranslate"><span class="pre">None</span></code>
  1533. if the connection isn’t compressed.</p>
  1534. <p>If the higher-level protocol supports its own compression mechanism,
  1535. 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 notranslate"><span class="pre">OP_NO_COMPRESSION</span></code></a> to disable SSL-level compression.</p>
  1536. <div class="versionadded">
  1537. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1538. </div>
  1539. </dd></dl>
  1540. <dl class="py method">
  1541. <dt class="sig sig-object py" id="ssl.SSLSocket.get_channel_binding">
  1542. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">get_channel_binding</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cb_type</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'tls-unique'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.get_channel_binding" title="Link to this definition">¶</a></dt>
  1543. <dd><p>Get channel binding data for current connection, as a bytes object. Returns
  1544. <code class="docutils literal notranslate"><span class="pre">None</span></code> if not connected or the handshake has not been completed.</p>
  1545. <p>The <em>cb_type</em> parameter allow selection of the desired channel binding
  1546. type. Valid channel binding types are listed in the
  1547. <a class="reference internal" href="#ssl.CHANNEL_BINDING_TYPES" title="ssl.CHANNEL_BINDING_TYPES"><code class="xref py py-data docutils literal notranslate"><span class="pre">CHANNEL_BINDING_TYPES</span></code></a> list. Currently only the ‘tls-unique’ channel
  1548. binding, defined by <span class="target" id="index-11"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5929.html"><strong>RFC 5929</strong></a>, is supported. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> will be
  1549. raised if an unsupported channel binding type is requested.</p>
  1550. <div class="versionadded">
  1551. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1552. </div>
  1553. </dd></dl>
  1554. <dl class="py method">
  1555. <dt class="sig sig-object py" id="ssl.SSLSocket.selected_alpn_protocol">
  1556. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">selected_alpn_protocol</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.selected_alpn_protocol" title="Link to this definition">¶</a></dt>
  1557. <dd><p>Return the protocol that was selected during the TLS handshake. If
  1558. <a class="reference internal" href="#ssl.SSLContext.set_alpn_protocols" title="ssl.SSLContext.set_alpn_protocols"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.set_alpn_protocols()</span></code></a> was not called, if the other party does
  1559. not support ALPN, if this socket does not support any of the client’s
  1560. proposed protocols, or if the handshake has not happened yet, <code class="docutils literal notranslate"><span class="pre">None</span></code> is
  1561. returned.</p>
  1562. <div class="versionadded">
  1563. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1564. </div>
  1565. </dd></dl>
  1566. <dl class="py method">
  1567. <dt class="sig sig-object py" id="ssl.SSLSocket.selected_npn_protocol">
  1568. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">selected_npn_protocol</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.selected_npn_protocol" title="Link to this definition">¶</a></dt>
  1569. <dd><p>Return the higher-level protocol that was selected during the TLS/SSL
  1570. 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 notranslate"><span class="pre">SSLContext.set_npn_protocols()</span></code></a> was not called, or
  1571. if the other party does not support NPN, or if the handshake has not yet
  1572. happened, this will return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1573. <div class="versionadded">
  1574. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1575. </div>
  1576. <div class="deprecated">
  1577. <p><span class="versionmodified deprecated">Deprecated since version 3.10: </span>NPN has been superseded by ALPN</p>
  1578. </div>
  1579. </dd></dl>
  1580. <dl class="py method">
  1581. <dt class="sig sig-object py" id="ssl.SSLSocket.unwrap">
  1582. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">unwrap</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.unwrap" title="Link to this definition">¶</a></dt>
  1583. <dd><p>Performs the SSL shutdown handshake, which removes the TLS layer from the
  1584. underlying socket, and returns the underlying socket object. This can be
  1585. used to go from encrypted operation over a connection to unencrypted. The
  1586. returned socket should always be used for further communication with the
  1587. other side of the connection, rather than the original socket.</p>
  1588. </dd></dl>
  1589. <dl class="py method">
  1590. <dt class="sig sig-object py" id="ssl.SSLSocket.verify_client_post_handshake">
  1591. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">verify_client_post_handshake</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.verify_client_post_handshake" title="Link to this definition">¶</a></dt>
  1592. <dd><p>Requests post-handshake authentication (PHA) from a TLS 1.3 client. PHA
  1593. can only be initiated for a TLS 1.3 connection from a server-side socket,
  1594. after the initial TLS handshake and with PHA enabled on both sides, see
  1595. <a class="reference internal" href="#ssl.SSLContext.post_handshake_auth" title="ssl.SSLContext.post_handshake_auth"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.post_handshake_auth</span></code></a>.</p>
  1596. <p>The method does not perform a cert exchange immediately. The server-side
  1597. sends a CertificateRequest during the next write event and expects the
  1598. client to respond with a certificate on the next read event.</p>
  1599. <p>If any precondition isn’t met (e.g. not TLS 1.3, PHA not enabled), an
  1600. <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLError</span></code></a> is raised.</p>
  1601. <div class="admonition note">
  1602. <p class="admonition-title">Note</p>
  1603. <p>Only available with OpenSSL 1.1.1 and TLS 1.3 enabled. Without TLS 1.3
  1604. support, the method raises <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
  1605. </div>
  1606. <div class="versionadded">
  1607. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1608. </div>
  1609. </dd></dl>
  1610. <dl class="py method">
  1611. <dt class="sig sig-object py" id="ssl.SSLSocket.version">
  1612. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">version</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.version" title="Link to this definition">¶</a></dt>
  1613. <dd><p>Return the actual SSL protocol version negotiated by the connection
  1614. as a string, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no secure connection is established.
  1615. As of this writing, possible return values include <code class="docutils literal notranslate"><span class="pre">&quot;SSLv2&quot;</span></code>,
  1616. <code class="docutils literal notranslate"><span class="pre">&quot;SSLv3&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;TLSv1&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;TLSv1.1&quot;</span></code> and <code class="docutils literal notranslate"><span class="pre">&quot;TLSv1.2&quot;</span></code>.
  1617. Recent OpenSSL versions may define more return values.</p>
  1618. <div class="versionadded">
  1619. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1620. </div>
  1621. </dd></dl>
  1622. <dl class="py method">
  1623. <dt class="sig sig-object py" id="ssl.SSLSocket.pending">
  1624. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">pending</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLSocket.pending" title="Link to this definition">¶</a></dt>
  1625. <dd><p>Returns the number of already decrypted bytes available for read, pending on
  1626. the connection.</p>
  1627. </dd></dl>
  1628. <dl class="py attribute">
  1629. <dt class="sig sig-object py" id="ssl.SSLSocket.context">
  1630. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">context</span></span><a class="headerlink" href="#ssl.SSLSocket.context" title="Link to this definition">¶</a></dt>
  1631. <dd><p>The <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> object this SSL socket is tied to.</p>
  1632. <div class="versionadded">
  1633. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1634. </div>
  1635. </dd></dl>
  1636. <dl class="py attribute">
  1637. <dt class="sig sig-object py" id="ssl.SSLSocket.server_side">
  1638. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">server_side</span></span><a class="headerlink" href="#ssl.SSLSocket.server_side" title="Link to this definition">¶</a></dt>
  1639. <dd><p>A boolean which is <code class="docutils literal notranslate"><span class="pre">True</span></code> for server-side sockets and <code class="docutils literal notranslate"><span class="pre">False</span></code> for
  1640. client-side sockets.</p>
  1641. <div class="versionadded">
  1642. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1643. </div>
  1644. </dd></dl>
  1645. <dl class="py attribute">
  1646. <dt class="sig sig-object py" id="ssl.SSLSocket.server_hostname">
  1647. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">server_hostname</span></span><a class="headerlink" href="#ssl.SSLSocket.server_hostname" title="Link to this definition">¶</a></dt>
  1648. <dd><p>Hostname of the server: <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> type, or <code class="docutils literal notranslate"><span class="pre">None</span></code> for server-side
  1649. socket or if the hostname was not specified in the constructor.</p>
  1650. <div class="versionadded">
  1651. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1652. </div>
  1653. <div class="versionchanged">
  1654. <p><span class="versionmodified changed">Changed in version 3.7: </span>The attribute is now always ASCII text. When <code class="docutils literal notranslate"><span class="pre">server_hostname</span></code> is
  1655. an internationalized domain name (IDN), this attribute now stores the
  1656. A-label form (<code class="docutils literal notranslate"><span class="pre">&quot;xn--pythn-mua.org&quot;</span></code>), rather than the U-label form
  1657. (<code class="docutils literal notranslate"><span class="pre">&quot;pythön.org&quot;</span></code>).</p>
  1658. </div>
  1659. </dd></dl>
  1660. <dl class="py attribute">
  1661. <dt class="sig sig-object py" id="ssl.SSLSocket.session">
  1662. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">session</span></span><a class="headerlink" href="#ssl.SSLSocket.session" title="Link to this definition">¶</a></dt>
  1663. <dd><p>The <a class="reference internal" href="#ssl.SSLSession" title="ssl.SSLSession"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSession</span></code></a> for this SSL connection. The session is available
  1664. for client and server side sockets after the TLS handshake has been
  1665. performed. For client sockets the session can be set before
  1666. <a class="reference internal" href="#ssl.SSLSocket.do_handshake" title="ssl.SSLSocket.do_handshake"><code class="xref py py-meth docutils literal notranslate"><span class="pre">do_handshake()</span></code></a> has been called to reuse a session.</p>
  1667. <div class="versionadded">
  1668. <p><span class="versionmodified added">New in version 3.6.</span></p>
  1669. </div>
  1670. </dd></dl>
  1671. <dl class="py attribute">
  1672. <dt class="sig sig-object py" id="ssl.SSLSocket.session_reused">
  1673. <span class="sig-prename descclassname"><span class="pre">SSLSocket.</span></span><span class="sig-name descname"><span class="pre">session_reused</span></span><a class="headerlink" href="#ssl.SSLSocket.session_reused" title="Link to this definition">¶</a></dt>
  1674. <dd><div class="versionadded">
  1675. <p><span class="versionmodified added">New in version 3.6.</span></p>
  1676. </div>
  1677. </dd></dl>
  1678. </section>
  1679. <section id="ssl-contexts">
  1680. <h2>SSL Contexts<a class="headerlink" href="#ssl-contexts" title="Link to this heading">¶</a></h2>
  1681. <div class="versionadded">
  1682. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1683. </div>
  1684. <p>An SSL context holds various data longer-lived than single SSL connections,
  1685. such as SSL configuration options, certificate(s) and private key(s).
  1686. It also manages a cache of SSL sessions for server-side sockets, in order
  1687. to speed up repeated connections from the same clients.</p>
  1688. <dl class="py class">
  1689. <dt class="sig sig-object py" id="ssl.SSLContext">
  1690. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">SSLContext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext" title="Link to this definition">¶</a></dt>
  1691. <dd><p>Create a new SSL context. You may pass <em>protocol</em> which must be one
  1692. of the <code class="docutils literal notranslate"><span class="pre">PROTOCOL_*</span></code> constants defined in this module. The parameter
  1693. specifies which version of the SSL protocol to use. Typically, the
  1694. server chooses a particular protocol version, and the client must adapt
  1695. to the server’s choice. Most of the versions are not interoperable
  1696. with the other versions. If not specified, the default is
  1697. <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a>; it provides the most compatibility with other
  1698. versions.</p>
  1699. <p>Here’s a table showing which versions in a client (down the side) can connect
  1700. to which versions in a server (along the top):</p>
  1701. <table class="docutils align-default">
  1702. <tbody>
  1703. <tr class="row-odd"><td><p><em>client</em> / <strong>server</strong></p></td>
  1704. <td><p><strong>SSLv2</strong></p></td>
  1705. <td><p><strong>SSLv3</strong></p></td>
  1706. <td><p><strong>TLS</strong> <a class="footnote-reference brackets" href="#id9" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>3<span class="fn-bracket">]</span></a></p></td>
  1707. <td><p><strong>TLSv1</strong></p></td>
  1708. <td><p><strong>TLSv1.1</strong></p></td>
  1709. <td><p><strong>TLSv1.2</strong></p></td>
  1710. </tr>
  1711. <tr class="row-even"><td><p><em>SSLv2</em></p></td>
  1712. <td><p>yes</p></td>
  1713. <td><p>no</p></td>
  1714. <td><p>no <a class="footnote-reference brackets" href="#id7" id="id2" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  1715. <td><p>no</p></td>
  1716. <td><p>no</p></td>
  1717. <td><p>no</p></td>
  1718. </tr>
  1719. <tr class="row-odd"><td><p><em>SSLv3</em></p></td>
  1720. <td><p>no</p></td>
  1721. <td><p>yes</p></td>
  1722. <td><p>no <a class="footnote-reference brackets" href="#id8" id="id3" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a></p></td>
  1723. <td><p>no</p></td>
  1724. <td><p>no</p></td>
  1725. <td><p>no</p></td>
  1726. </tr>
  1727. <tr class="row-even"><td><p><em>TLS</em> (<em>SSLv23</em>) <a class="footnote-reference brackets" href="#id9" id="id4" role="doc-noteref"><span class="fn-bracket">[</span>3<span class="fn-bracket">]</span></a></p></td>
  1728. <td><p>no <a class="footnote-reference brackets" href="#id7" id="id5" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  1729. <td><p>no <a class="footnote-reference brackets" href="#id8" id="id6" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a></p></td>
  1730. <td><p>yes</p></td>
  1731. <td><p>yes</p></td>
  1732. <td><p>yes</p></td>
  1733. <td><p>yes</p></td>
  1734. </tr>
  1735. <tr class="row-odd"><td><p><em>TLSv1</em></p></td>
  1736. <td><p>no</p></td>
  1737. <td><p>no</p></td>
  1738. <td><p>yes</p></td>
  1739. <td><p>yes</p></td>
  1740. <td><p>no</p></td>
  1741. <td><p>no</p></td>
  1742. </tr>
  1743. <tr class="row-even"><td><p><em>TLSv1.1</em></p></td>
  1744. <td><p>no</p></td>
  1745. <td><p>no</p></td>
  1746. <td><p>yes</p></td>
  1747. <td><p>no</p></td>
  1748. <td><p>yes</p></td>
  1749. <td><p>no</p></td>
  1750. </tr>
  1751. <tr class="row-odd"><td><p><em>TLSv1.2</em></p></td>
  1752. <td><p>no</p></td>
  1753. <td><p>no</p></td>
  1754. <td><p>yes</p></td>
  1755. <td><p>no</p></td>
  1756. <td><p>no</p></td>
  1757. <td><p>yes</p></td>
  1758. </tr>
  1759. </tbody>
  1760. </table>
  1761. <p class="rubric">Footnotes</p>
  1762. <aside class="footnote-list brackets">
  1763. <aside class="footnote brackets" id="id7" role="doc-footnote">
  1764. <span class="label"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></span>
  1765. <span class="backrefs">(<a role="doc-backlink" href="#id2">1</a>,<a role="doc-backlink" href="#id5">2</a>)</span>
  1766. <p><a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> disables SSLv2 with <a class="reference internal" href="#ssl.OP_NO_SSLv2" title="ssl.OP_NO_SSLv2"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_SSLv2</span></code></a> by default.</p>
  1767. </aside>
  1768. <aside class="footnote brackets" id="id8" role="doc-footnote">
  1769. <span class="label"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></span>
  1770. <span class="backrefs">(<a role="doc-backlink" href="#id3">1</a>,<a role="doc-backlink" href="#id6">2</a>)</span>
  1771. <p><a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> disables SSLv3 with <a class="reference internal" href="#ssl.OP_NO_SSLv3" title="ssl.OP_NO_SSLv3"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_SSLv3</span></code></a> by default.</p>
  1772. </aside>
  1773. <aside class="footnote brackets" id="id9" role="doc-footnote">
  1774. <span class="label"><span class="fn-bracket">[</span>3<span class="fn-bracket">]</span></span>
  1775. <span class="backrefs">(<a role="doc-backlink" href="#id1">1</a>,<a role="doc-backlink" href="#id4">2</a>)</span>
  1776. <p>TLS 1.3 protocol will be available with <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a> in
  1777. OpenSSL &gt;= 1.1.1. There is no dedicated PROTOCOL constant for just
  1778. TLS 1.3.</p>
  1779. </aside>
  1780. </aside>
  1781. <div class="admonition seealso">
  1782. <p class="admonition-title">See also</p>
  1783. <p><a class="reference internal" href="#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal notranslate"><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 notranslate"><span class="pre">ssl</span></code></a> module choose
  1784. security settings for a given purpose.</p>
  1785. </div>
  1786. <div class="versionchanged">
  1787. <p><span class="versionmodified changed">Changed in version 3.6: </span>The context is created with secure default values. The options
  1788. <a class="reference internal" href="#ssl.OP_NO_COMPRESSION" title="ssl.OP_NO_COMPRESSION"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_COMPRESSION</span></code></a>, <a class="reference internal" href="#ssl.OP_CIPHER_SERVER_PREFERENCE" title="ssl.OP_CIPHER_SERVER_PREFERENCE"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_CIPHER_SERVER_PREFERENCE</span></code></a>,
  1789. <a class="reference internal" href="#ssl.OP_SINGLE_DH_USE" title="ssl.OP_SINGLE_DH_USE"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_SINGLE_DH_USE</span></code></a>, <a class="reference internal" href="#ssl.OP_SINGLE_ECDH_USE" title="ssl.OP_SINGLE_ECDH_USE"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_SINGLE_ECDH_USE</span></code></a>,
  1790. <a class="reference internal" href="#ssl.OP_NO_SSLv2" title="ssl.OP_NO_SSLv2"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_SSLv2</span></code></a>,
  1791. and <a class="reference internal" href="#ssl.OP_NO_SSLv3" title="ssl.OP_NO_SSLv3"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_SSLv3</span></code></a> (except for <a class="reference internal" href="#ssl.PROTOCOL_SSLv3" title="ssl.PROTOCOL_SSLv3"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_SSLv3</span></code></a>) are
  1792. set by default. The initial cipher suite list contains only <code class="docutils literal notranslate"><span class="pre">HIGH</span></code>
  1793. ciphers, no <code class="docutils literal notranslate"><span class="pre">NULL</span></code> ciphers and no <code class="docutils literal notranslate"><span class="pre">MD5</span></code> ciphers.</p>
  1794. </div>
  1795. <div class="deprecated">
  1796. <p><span class="versionmodified deprecated">Deprecated since version 3.10: </span><a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> without protocol argument is deprecated. The
  1797. context class will either require <a class="reference internal" href="#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a> or
  1798. <a class="reference internal" href="#ssl.PROTOCOL_TLS_SERVER" title="ssl.PROTOCOL_TLS_SERVER"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_SERVER</span></code></a> protocol in the future.</p>
  1799. </div>
  1800. <div class="versionchanged">
  1801. <p><span class="versionmodified changed">Changed in version 3.10: </span>The default cipher suites now include only secure AES and ChaCha20
  1802. ciphers with forward secrecy and security level 2. RSA and DH keys with
  1803. less than 2048 bits and ECC keys with less than 224 bits are prohibited.
  1804. <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a>, <a class="reference internal" href="#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a>, and
  1805. <a class="reference internal" href="#ssl.PROTOCOL_TLS_SERVER" title="ssl.PROTOCOL_TLS_SERVER"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_SERVER</span></code></a> use TLS 1.2 as minimum TLS version.</p>
  1806. </div>
  1807. </dd></dl>
  1808. <p><a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> objects have the following methods and attributes:</p>
  1809. <dl class="py method">
  1810. <dt class="sig sig-object py" id="ssl.SSLContext.cert_store_stats">
  1811. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">cert_store_stats</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.cert_store_stats" title="Link to this definition">¶</a></dt>
  1812. <dd><p>Get statistics about quantities of loaded X.509 certificates, count of
  1813. X.509 certificates flagged as CA certificates and certificate revocation
  1814. lists as dictionary.</p>
  1815. <p>Example for a context with one CA cert and one other cert:</p>
  1816. <div class="highlight-python3 notranslate"><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>
  1817. <span class="go">{&#39;crl&#39;: 0, &#39;x509_ca&#39;: 1, &#39;x509&#39;: 2}</span>
  1818. </pre></div>
  1819. </div>
  1820. <div class="versionadded">
  1821. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1822. </div>
  1823. </dd></dl>
  1824. <dl class="py method">
  1825. <dt class="sig sig-object py" id="ssl.SSLContext.load_cert_chain">
  1826. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">load_cert_chain</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">certfile</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">keyfile</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">password</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.load_cert_chain" title="Link to this definition">¶</a></dt>
  1827. <dd><p>Load a private key and the corresponding certificate. The <em>certfile</em>
  1828. string must be the path to a single file in PEM format containing the
  1829. certificate as well as any number of CA certificates needed to establish
  1830. the certificate’s authenticity. The <em>keyfile</em> string, if present, must
  1831. point to a file containing the private key. Otherwise the private
  1832. key will be taken from <em>certfile</em> as well. See the discussion of
  1833. <a class="reference internal" href="#ssl-certificates"><span class="std std-ref">Certificates</span></a> for more information on how the certificate
  1834. is stored in the <em>certfile</em>.</p>
  1835. <p>The <em>password</em> argument may be a function to call to get the password for
  1836. decrypting the private key. It will only be called if the private key is
  1837. encrypted and a password is necessary. It will be called with no arguments,
  1838. and it should return a string, bytes, or bytearray. If the return value is
  1839. a string it will be encoded as UTF-8 before using it to decrypt the key.
  1840. Alternatively a string, bytes, or bytearray value may be supplied directly
  1841. as the <em>password</em> argument. It will be ignored if the private key is not
  1842. encrypted and no password is needed.</p>
  1843. <p>If the <em>password</em> argument is not specified and a password is required,
  1844. OpenSSL’s built-in password prompting mechanism will be used to
  1845. interactively prompt the user for a password.</p>
  1846. <p>An <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLError</span></code></a> is raised if the private key doesn’t
  1847. match with the certificate.</p>
  1848. <div class="versionchanged">
  1849. <p><span class="versionmodified changed">Changed in version 3.3: </span>New optional argument <em>password</em>.</p>
  1850. </div>
  1851. </dd></dl>
  1852. <dl class="py method">
  1853. <dt class="sig sig-object py" id="ssl.SSLContext.load_default_certs">
  1854. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">load_default_certs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">purpose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">Purpose.SERVER_AUTH</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.load_default_certs" title="Link to this definition">¶</a></dt>
  1855. <dd><p>Load a set of default “certification authority” (CA) certificates from
  1856. default locations. On Windows it loads CA certs from the <code class="docutils literal notranslate"><span class="pre">CA</span></code> and
  1857. <code class="docutils literal notranslate"><span class="pre">ROOT</span></code> system stores. On all systems it calls
  1858. <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 notranslate"><span class="pre">SSLContext.set_default_verify_paths()</span></code></a>. In the future the method may
  1859. load CA certificates from other locations, too.</p>
  1860. <p>The <em>purpose</em> flag specifies what kind of CA certificates are loaded. The
  1861. default settings <a class="reference internal" href="#ssl.Purpose.SERVER_AUTH" title="ssl.Purpose.SERVER_AUTH"><code class="xref py py-const docutils literal notranslate"><span class="pre">Purpose.SERVER_AUTH</span></code></a> loads certificates, that are
  1862. flagged and trusted for TLS web server authentication (client side
  1863. sockets). <a class="reference internal" href="#ssl.Purpose.CLIENT_AUTH" title="ssl.Purpose.CLIENT_AUTH"><code class="xref py py-const docutils literal notranslate"><span class="pre">Purpose.CLIENT_AUTH</span></code></a> loads CA certificates for client
  1864. certificate verification on the server side.</p>
  1865. <div class="versionadded">
  1866. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1867. </div>
  1868. </dd></dl>
  1869. <dl class="py method">
  1870. <dt class="sig sig-object py" id="ssl.SSLContext.load_verify_locations">
  1871. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">load_verify_locations</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cafile</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">capath</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cadata</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.load_verify_locations" title="Link to this definition">¶</a></dt>
  1872. <dd><p>Load a set of “certification authority” (CA) certificates used to validate
  1873. other peers’ certificates when <a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-data docutils literal notranslate"><span class="pre">verify_mode</span></code></a> is other than
  1874. <a class="reference internal" href="#ssl.CERT_NONE" title="ssl.CERT_NONE"><code class="xref py py-data docutils literal notranslate"><span class="pre">CERT_NONE</span></code></a>. At least one of <em>cafile</em> or <em>capath</em> must be specified.</p>
  1875. <p>This method can also load certification revocation lists (CRLs) in PEM or
  1876. 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 notranslate"><span class="pre">SSLContext.verify_flags</span></code></a>
  1877. must be configured properly.</p>
  1878. <p>The <em>cafile</em> string, if present, is the path to a file of concatenated
  1879. CA certificates in PEM format. See the discussion of
  1880. <a class="reference internal" href="#ssl-certificates"><span class="std std-ref">Certificates</span></a> for more information about how to arrange the
  1881. certificates in this file.</p>
  1882. <p>The <em>capath</em> string, if present, is
  1883. the path to a directory containing several CA certificates in PEM format,
  1884. following an <a class="reference external" href="https://www.openssl.org/docs/manmaster/man3/SSL_CTX_load_verify_locations.html">OpenSSL specific layout</a>.</p>
  1885. <p>The <em>cadata</em> object, if present, is either an ASCII string of one or more
  1886. PEM-encoded certificates or a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> of DER-encoded
  1887. certificates. Like with <em>capath</em> extra lines around PEM-encoded
  1888. certificates are ignored but at least one certificate must be present.</p>
  1889. <div class="versionchanged">
  1890. <p><span class="versionmodified changed">Changed in version 3.4: </span>New optional argument <em>cadata</em></p>
  1891. </div>
  1892. </dd></dl>
  1893. <dl class="py method">
  1894. <dt class="sig sig-object py" id="ssl.SSLContext.get_ca_certs">
  1895. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">get_ca_certs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">binary_form</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.get_ca_certs" title="Link to this definition">¶</a></dt>
  1896. <dd><p>Get a list of loaded “certification authority” (CA) certificates. If the
  1897. <code class="docutils literal notranslate"><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 notranslate"><span class="pre">False</span></code></a> each list
  1898. 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 notranslate"><span class="pre">SSLSocket.getpeercert()</span></code></a>. Otherwise
  1899. the method returns a list of DER-encoded certificates. The returned list
  1900. does not contain certificates from <em>capath</em> unless a certificate was
  1901. requested and loaded by a SSL connection.</p>
  1902. <div class="admonition note">
  1903. <p class="admonition-title">Note</p>
  1904. <p>Certificates in a capath directory aren’t loaded unless they have
  1905. been used at least once.</p>
  1906. </div>
  1907. <div class="versionadded">
  1908. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1909. </div>
  1910. </dd></dl>
  1911. <dl class="py method">
  1912. <dt class="sig sig-object py" id="ssl.SSLContext.get_ciphers">
  1913. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">get_ciphers</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.get_ciphers" title="Link to this definition">¶</a></dt>
  1914. <dd><p>Get a list of enabled ciphers. The list is in order of cipher priority.
  1915. See <a class="reference internal" href="#ssl.SSLContext.set_ciphers" title="ssl.SSLContext.set_ciphers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.set_ciphers()</span></code></a>.</p>
  1916. <p>Example:</p>
  1917. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ctx</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>
  1918. <span class="gp">&gt;&gt;&gt; </span><span class="n">ctx</span><span class="o">.</span><span class="n">set_ciphers</span><span class="p">(</span><span class="s1">&#39;ECDHE+AESGCM:!ECDSA&#39;</span><span class="p">)</span>
  1919. <span class="gp">&gt;&gt;&gt; </span><span class="n">ctx</span><span class="o">.</span><span class="n">get_ciphers</span><span class="p">()</span>
  1920. <span class="go">[{&#39;aead&#39;: True,</span>
  1921. <span class="go"> &#39;alg_bits&#39;: 256,</span>
  1922. <span class="go"> &#39;auth&#39;: &#39;auth-rsa&#39;,</span>
  1923. <span class="go"> &#39;description&#39;: &#39;ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2 Kx=ECDH Au=RSA &#39;</span>
  1924. <span class="go"> &#39;Enc=AESGCM(256) Mac=AEAD&#39;,</span>
  1925. <span class="go"> &#39;digest&#39;: None,</span>
  1926. <span class="go"> &#39;id&#39;: 50380848,</span>
  1927. <span class="go"> &#39;kea&#39;: &#39;kx-ecdhe&#39;,</span>
  1928. <span class="go"> &#39;name&#39;: &#39;ECDHE-RSA-AES256-GCM-SHA384&#39;,</span>
  1929. <span class="go"> &#39;protocol&#39;: &#39;TLSv1.2&#39;,</span>
  1930. <span class="go"> &#39;strength_bits&#39;: 256,</span>
  1931. <span class="go"> &#39;symmetric&#39;: &#39;aes-256-gcm&#39;},</span>
  1932. <span class="go"> {&#39;aead&#39;: True,</span>
  1933. <span class="go"> &#39;alg_bits&#39;: 128,</span>
  1934. <span class="go"> &#39;auth&#39;: &#39;auth-rsa&#39;,</span>
  1935. <span class="go"> &#39;description&#39;: &#39;ECDHE-RSA-AES128-GCM-SHA256 TLSv1.2 Kx=ECDH Au=RSA &#39;</span>
  1936. <span class="go"> &#39;Enc=AESGCM(128) Mac=AEAD&#39;,</span>
  1937. <span class="go"> &#39;digest&#39;: None,</span>
  1938. <span class="go"> &#39;id&#39;: 50380847,</span>
  1939. <span class="go"> &#39;kea&#39;: &#39;kx-ecdhe&#39;,</span>
  1940. <span class="go"> &#39;name&#39;: &#39;ECDHE-RSA-AES128-GCM-SHA256&#39;,</span>
  1941. <span class="go"> &#39;protocol&#39;: &#39;TLSv1.2&#39;,</span>
  1942. <span class="go"> &#39;strength_bits&#39;: 128,</span>
  1943. <span class="go"> &#39;symmetric&#39;: &#39;aes-128-gcm&#39;}]</span>
  1944. </pre></div>
  1945. </div>
  1946. <div class="versionadded">
  1947. <p><span class="versionmodified added">New in version 3.6.</span></p>
  1948. </div>
  1949. </dd></dl>
  1950. <dl class="py method">
  1951. <dt class="sig sig-object py" id="ssl.SSLContext.set_default_verify_paths">
  1952. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">set_default_verify_paths</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.set_default_verify_paths" title="Link to this definition">¶</a></dt>
  1953. <dd><p>Load a set of default “certification authority” (CA) certificates from
  1954. a filesystem path defined when building the OpenSSL library. Unfortunately,
  1955. there’s no easy way to know whether this method succeeds: no error is
  1956. returned if no certificates are to be found. When the OpenSSL library is
  1957. provided as part of the operating system, though, it is likely to be
  1958. configured properly.</p>
  1959. </dd></dl>
  1960. <dl class="py method">
  1961. <dt class="sig sig-object py" id="ssl.SSLContext.set_ciphers">
  1962. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">set_ciphers</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">ciphers</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.set_ciphers" title="Link to this definition">¶</a></dt>
  1963. <dd><p>Set the available ciphers for sockets created with this context.
  1964. It should be a string in the <a class="reference external" href="https://www.openssl.org/docs/manmaster/man1/ciphers.html">OpenSSL cipher list format</a>.
  1965. If no cipher can be selected (because compile-time options or other
  1966. configuration forbids use of all the specified ciphers), an
  1967. <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLError</span></code></a> will be raised.</p>
  1968. <div class="admonition note">
  1969. <p class="admonition-title">Note</p>
  1970. <p>when connected, the <a class="reference internal" href="#ssl.SSLSocket.cipher" title="ssl.SSLSocket.cipher"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.cipher()</span></code></a> method of SSL sockets will
  1971. give the currently selected cipher.</p>
  1972. <p>TLS 1.3 cipher suites cannot be disabled with
  1973. <a class="reference internal" href="#ssl.SSLContext.set_ciphers" title="ssl.SSLContext.set_ciphers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_ciphers()</span></code></a>.</p>
  1974. </div>
  1975. </dd></dl>
  1976. <dl class="py method">
  1977. <dt class="sig sig-object py" id="ssl.SSLContext.set_alpn_protocols">
  1978. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">set_alpn_protocols</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocols</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.set_alpn_protocols" title="Link to this definition">¶</a></dt>
  1979. <dd><p>Specify which protocols the socket should advertise during the SSL/TLS
  1980. handshake. It should be a list of ASCII strings, like <code class="docutils literal notranslate"><span class="pre">['http/1.1',</span>
  1981. <span class="pre">'spdy/2']</span></code>, ordered by preference. The selection of a protocol will happen
  1982. during the handshake, and will play out according to <span class="target" id="index-12"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc7301.html"><strong>RFC 7301</strong></a>. After a
  1983. 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 notranslate"><span class="pre">SSLSocket.selected_alpn_protocol()</span></code></a> method will
  1984. return the agreed-upon protocol.</p>
  1985. <p>This method will raise <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><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 notranslate"><span class="pre">HAS_ALPN</span></code></a> is
  1986. <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  1987. <div class="versionadded">
  1988. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1989. </div>
  1990. </dd></dl>
  1991. <dl class="py method">
  1992. <dt class="sig sig-object py" id="ssl.SSLContext.set_npn_protocols">
  1993. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">set_npn_protocols</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">protocols</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.set_npn_protocols" title="Link to this definition">¶</a></dt>
  1994. <dd><p>Specify which protocols the socket should advertise during the SSL/TLS
  1995. handshake. It should be a list of strings, like <code class="docutils literal notranslate"><span class="pre">['http/1.1',</span> <span class="pre">'spdy/2']</span></code>,
  1996. ordered by preference. The selection of a protocol will happen during the
  1997. handshake, and will play out according to the <a class="reference external" href="https://en.wikipedia.org/wiki/Application-Layer_Protocol_Negotiation">Application Layer Protocol Negotiation</a>. After a
  1998. 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 notranslate"><span class="pre">SSLSocket.selected_npn_protocol()</span></code></a> method will
  1999. return the agreed-upon protocol.</p>
  2000. <p>This method will raise <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><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 notranslate"><span class="pre">HAS_NPN</span></code></a> is
  2001. <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  2002. <div class="versionadded">
  2003. <p><span class="versionmodified added">New in version 3.3.</span></p>
  2004. </div>
  2005. <div class="deprecated">
  2006. <p><span class="versionmodified deprecated">Deprecated since version 3.10: </span>NPN has been superseded by ALPN</p>
  2007. </div>
  2008. </dd></dl>
  2009. <dl class="py attribute">
  2010. <dt class="sig sig-object py" id="ssl.SSLContext.sni_callback">
  2011. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">sni_callback</span></span><a class="headerlink" href="#ssl.SSLContext.sni_callback" title="Link to this definition">¶</a></dt>
  2012. <dd><p>Register a callback function that will be called after the TLS Client Hello
  2013. handshake message has been received by the SSL/TLS server when the TLS client
  2014. specifies a server name indication. The server name indication mechanism
  2015. is specified in <span class="target" id="index-13"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc6066.html"><strong>RFC 6066</strong></a> section 3 - Server Name Indication.</p>
  2016. <p>Only one callback can be set per <code class="docutils literal notranslate"><span class="pre">SSLContext</span></code>. If <em>sni_callback</em>
  2017. is set to <code class="docutils literal notranslate"><span class="pre">None</span></code> then the callback is disabled. Calling this function a
  2018. subsequent time will disable the previously registered callback.</p>
  2019. <p>The callback function will be called with three
  2020. arguments; the first being the <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLSocket</span></code></a>, the second is a string
  2021. that represents the server name that the client is intending to communicate
  2022. (or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> if the TLS Client Hello does not contain a server name)
  2023. 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 notranslate"><span class="pre">SSLContext</span></code></a>. The server name
  2024. argument is text. For internationalized domain name, the server
  2025. name is an IDN A-label (<code class="docutils literal notranslate"><span class="pre">&quot;xn--pythn-mua.org&quot;</span></code>).</p>
  2026. <p>A typical use of this callback is to change the <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLSocket</span></code></a>’s
  2027. <a class="reference internal" href="#ssl.SSLSocket.context" title="ssl.SSLSocket.context"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLSocket.context</span></code></a> attribute to a new object of type
  2028. <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> representing a certificate chain that matches the server
  2029. name.</p>
  2030. <p>Due to the early negotiation phase of the TLS connection, only limited
  2031. methods and attributes are usable like
  2032. <a class="reference internal" href="#ssl.SSLSocket.selected_alpn_protocol" title="ssl.SSLSocket.selected_alpn_protocol"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">SSLSocket.context</span></code></a>.
  2033. The <a class="reference internal" href="#ssl.SSLSocket.getpeercert" title="ssl.SSLSocket.getpeercert"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.getpeercert()</span></code></a>,
  2034. <a class="reference internal" href="#ssl.SSLSocket.cipher" title="ssl.SSLSocket.cipher"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.cipher()</span></code></a> and <a class="reference internal" href="#ssl.SSLSocket.compression" title="ssl.SSLSocket.compression"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.compression()</span></code></a> methods require that
  2035. the TLS connection has progressed beyond the TLS Client Hello and therefore
  2036. will not return meaningful values nor can they be called safely.</p>
  2037. <p>The <em>sni_callback</em> function must return <code class="docutils literal notranslate"><span class="pre">None</span></code> to allow the
  2038. TLS negotiation to continue. If a TLS failure is required, a constant
  2039. <a class="reference internal" href="#ssl.ALERT_DESCRIPTION_INTERNAL_ERROR" title="ssl.ALERT_DESCRIPTION_INTERNAL_ERROR"><code class="xref py py-const docutils literal notranslate"><span class="pre">ALERT_DESCRIPTION_*</span></code></a> can be
  2040. returned. Other return values will result in a TLS fatal error with
  2041. <a class="reference internal" href="#ssl.ALERT_DESCRIPTION_INTERNAL_ERROR" title="ssl.ALERT_DESCRIPTION_INTERNAL_ERROR"><code class="xref py py-const docutils literal notranslate"><span class="pre">ALERT_DESCRIPTION_INTERNAL_ERROR</span></code></a>.</p>
  2042. <p>If an exception is raised from the <em>sni_callback</em> function the TLS
  2043. connection will terminate with a fatal TLS alert message
  2044. <a class="reference internal" href="#ssl.ALERT_DESCRIPTION_HANDSHAKE_FAILURE" title="ssl.ALERT_DESCRIPTION_HANDSHAKE_FAILURE"><code class="xref py py-const docutils literal notranslate"><span class="pre">ALERT_DESCRIPTION_HANDSHAKE_FAILURE</span></code></a>.</p>
  2045. <p>This method will raise <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> if the OpenSSL library
  2046. had OPENSSL_NO_TLSEXT defined when it was built.</p>
  2047. <div class="versionadded">
  2048. <p><span class="versionmodified added">New in version 3.7.</span></p>
  2049. </div>
  2050. </dd></dl>
  2051. <dl class="py attribute">
  2052. <dt class="sig sig-object py" id="ssl.SSLContext.set_servername_callback">
  2053. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">set_servername_callback</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">server_name_callback</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.set_servername_callback" title="Link to this definition">¶</a></dt>
  2054. <dd><p>This is a legacy API retained for backwards compatibility. When possible,
  2055. you should use <a class="reference internal" href="#ssl.SSLContext.sni_callback" title="ssl.SSLContext.sni_callback"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sni_callback</span></code></a> instead. The given <em>server_name_callback</em>
  2056. is similar to <em>sni_callback</em>, except that when the server hostname is an
  2057. IDN-encoded internationalized domain name, the <em>server_name_callback</em>
  2058. receives a decoded U-label (<code class="docutils literal notranslate"><span class="pre">&quot;pythön.org&quot;</span></code>).</p>
  2059. <p>If there is an decoding error on the server name, the TLS connection will
  2060. 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 notranslate"><span class="pre">ALERT_DESCRIPTION_INTERNAL_ERROR</span></code></a> fatal TLS
  2061. alert message to the client.</p>
  2062. <div class="versionadded">
  2063. <p><span class="versionmodified added">New in version 3.4.</span></p>
  2064. </div>
  2065. </dd></dl>
  2066. <dl class="py method">
  2067. <dt class="sig sig-object py" id="ssl.SSLContext.load_dh_params">
  2068. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">load_dh_params</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dhfile</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.load_dh_params" title="Link to this definition">¶</a></dt>
  2069. <dd><p>Load the key generation parameters for Diffie-Hellman (DH) key exchange.
  2070. Using DH key exchange improves forward secrecy at the expense of
  2071. computational resources (both on the server and on the client).
  2072. The <em>dhfile</em> parameter should be the path to a file containing DH
  2073. parameters in PEM format.</p>
  2074. <p>This setting doesn’t apply to client sockets. You can also use the
  2075. <a class="reference internal" href="#ssl.OP_SINGLE_DH_USE" title="ssl.OP_SINGLE_DH_USE"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_SINGLE_DH_USE</span></code></a> option to further improve security.</p>
  2076. <div class="versionadded">
  2077. <p><span class="versionmodified added">New in version 3.3.</span></p>
  2078. </div>
  2079. </dd></dl>
  2080. <dl class="py method">
  2081. <dt class="sig sig-object py" id="ssl.SSLContext.set_ecdh_curve">
  2082. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">set_ecdh_curve</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">curve_name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.set_ecdh_curve" title="Link to this definition">¶</a></dt>
  2083. <dd><p>Set the curve name for Elliptic Curve-based Diffie-Hellman (ECDH) key
  2084. exchange. ECDH is significantly faster than regular DH while arguably
  2085. as secure. The <em>curve_name</em> parameter should be a string describing
  2086. a well-known elliptic curve, for example <code class="docutils literal notranslate"><span class="pre">prime256v1</span></code> for a widely
  2087. supported curve.</p>
  2088. <p>This setting doesn’t apply to client sockets. You can also use the
  2089. <a class="reference internal" href="#ssl.OP_SINGLE_ECDH_USE" title="ssl.OP_SINGLE_ECDH_USE"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_SINGLE_ECDH_USE</span></code></a> option to further improve security.</p>
  2090. <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 notranslate"><span class="pre">HAS_ECDH</span></code></a> is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  2091. <div class="versionadded">
  2092. <p><span class="versionmodified added">New in version 3.3.</span></p>
  2093. </div>
  2094. <div class="admonition seealso">
  2095. <p class="admonition-title">See also</p>
  2096. <dl class="simple">
  2097. <dt><a class="reference external" href="https://vincent.bernat.ch/en/blog/2011-ssl-perfect-forward-secrecy">SSL/TLS &amp; Perfect Forward Secrecy</a></dt><dd><p>Vincent Bernat.</p>
  2098. </dd>
  2099. </dl>
  2100. </div>
  2101. </dd></dl>
  2102. <dl class="py method">
  2103. <dt class="sig sig-object py" id="ssl.SSLContext.wrap_socket">
  2104. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">wrap_socket</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sock</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">server_side</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">do_handshake_on_connect</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">suppress_ragged_eofs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">server_hostname</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">session</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.wrap_socket" title="Link to this definition">¶</a></dt>
  2105. <dd><p>Wrap an existing Python socket <em>sock</em> and return an instance of
  2106. <a class="reference internal" href="#ssl.SSLContext.sslsocket_class" title="ssl.SSLContext.sslsocket_class"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.sslsocket_class</span></code></a> (default <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a>). The
  2107. returned SSL socket is tied to the context, its settings and certificates.
  2108. <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-const docutils literal notranslate"><span class="pre">SOCK_STREAM</span></code></a> socket; other
  2109. socket types are unsupported.</p>
  2110. <p>The parameter <code class="docutils literal notranslate"><span class="pre">server_side</span></code> is a boolean which identifies whether
  2111. server-side or client-side behavior is desired from this socket.</p>
  2112. <p>For client-side sockets, the context construction is lazy; if the
  2113. underlying socket isn’t connected yet, the context construction will be
  2114. performed after <code class="xref py py-meth docutils literal notranslate"><span class="pre">connect()</span></code> is called on the socket. For
  2115. server-side sockets, if the socket has no remote peer, it is assumed
  2116. to be a listening socket, and the server-side SSL wrapping is
  2117. automatically performed on client connections accepted via the
  2118. <code class="xref py py-meth docutils literal notranslate"><span class="pre">accept()</span></code> method. The method may raise <a class="reference internal" href="#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLError</span></code></a>.</p>
  2119. <p>On client connections, the optional parameter <em>server_hostname</em> specifies
  2120. the hostname of the service which we are connecting to. This allows a
  2121. single server to host multiple SSL-based services with distinct certificates,
  2122. quite similarly to HTTP virtual hosts. Specifying <em>server_hostname</em> will
  2123. raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if <em>server_side</em> is true.</p>
  2124. <p>The parameter <code class="docutils literal notranslate"><span class="pre">do_handshake_on_connect</span></code> specifies whether to do the SSL
  2125. handshake automatically after doing a <code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.connect()</span></code>, or whether the
  2126. application program will call it explicitly, by invoking the
  2127. <a class="reference internal" href="#ssl.SSLSocket.do_handshake" title="ssl.SSLSocket.do_handshake"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.do_handshake()</span></code></a> method. Calling
  2128. <a class="reference internal" href="#ssl.SSLSocket.do_handshake" title="ssl.SSLSocket.do_handshake"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.do_handshake()</span></code></a> explicitly gives the program control over the
  2129. blocking behavior of the socket I/O involved in the handshake.</p>
  2130. <p>The parameter <code class="docutils literal notranslate"><span class="pre">suppress_ragged_eofs</span></code> specifies how the
  2131. <code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.recv()</span></code> method should signal unexpected EOF from the other end
  2132. of the connection. If specified as <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> (the default), it returns a
  2133. normal EOF (an empty bytes object) in response to unexpected EOF errors
  2134. raised from the underlying socket; if <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a>, it will raise the
  2135. exceptions back to the caller.</p>
  2136. <p><em>session</em>, see <a class="reference internal" href="#ssl.SSLSocket.session" title="ssl.SSLSocket.session"><code class="xref py py-attr docutils literal notranslate"><span class="pre">session</span></code></a>.</p>
  2137. <p>To wrap an <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> in another <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a>, use
  2138. <a class="reference internal" href="#ssl.SSLContext.wrap_bio" title="ssl.SSLContext.wrap_bio"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.wrap_bio()</span></code></a>.</p>
  2139. <div class="versionchanged">
  2140. <p><span class="versionmodified changed">Changed in version 3.5: </span>Always allow a server_hostname to be passed, even if OpenSSL does not
  2141. have SNI.</p>
  2142. </div>
  2143. <div class="versionchanged">
  2144. <p><span class="versionmodified changed">Changed in version 3.6: </span><em>session</em> argument was added.</p>
  2145. <div class="versionchanged">
  2146. <p><span class="versionmodified changed">Changed in version 3.7: </span>The method returns an instance of <a class="reference internal" href="#ssl.SSLContext.sslsocket_class" title="ssl.SSLContext.sslsocket_class"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.sslsocket_class</span></code></a>
  2147. instead of hard-coded <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a>.</p>
  2148. </div>
  2149. </div>
  2150. </dd></dl>
  2151. <dl class="py attribute">
  2152. <dt class="sig sig-object py" id="ssl.SSLContext.sslsocket_class">
  2153. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">sslsocket_class</span></span><a class="headerlink" href="#ssl.SSLContext.sslsocket_class" title="Link to this definition">¶</a></dt>
  2154. <dd><p>The return type of <a class="reference internal" href="#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.wrap_socket()</span></code></a>, defaults to
  2155. <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a>. The attribute can be overridden on instance of class
  2156. in order to return a custom subclass of <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a>.</p>
  2157. <div class="versionadded">
  2158. <p><span class="versionmodified added">New in version 3.7.</span></p>
  2159. </div>
  2160. </dd></dl>
  2161. <dl class="py method">
  2162. <dt class="sig sig-object py" id="ssl.SSLContext.wrap_bio">
  2163. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">wrap_bio</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">incoming</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">outgoing</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">server_side</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">server_hostname</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">session</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.wrap_bio" title="Link to this definition">¶</a></dt>
  2164. <dd><p>Wrap the BIO objects <em>incoming</em> and <em>outgoing</em> and return an instance of
  2165. <a class="reference internal" href="#ssl.SSLContext.sslobject_class" title="ssl.SSLContext.sslobject_class"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.sslobject_class</span></code></a> (default <a class="reference internal" href="#ssl.SSLObject" title="ssl.SSLObject"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLObject</span></code></a>). The SSL
  2166. routines will read input data from the incoming BIO and write data to the
  2167. outgoing BIO.</p>
  2168. <p>The <em>server_side</em>, <em>server_hostname</em> and <em>session</em> parameters have the
  2169. same meaning as in <a class="reference internal" href="#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.wrap_socket()</span></code></a>.</p>
  2170. <div class="versionchanged">
  2171. <p><span class="versionmodified changed">Changed in version 3.6: </span><em>session</em> argument was added.</p>
  2172. </div>
  2173. <div class="versionchanged">
  2174. <p><span class="versionmodified changed">Changed in version 3.7: </span>The method returns an instance of <a class="reference internal" href="#ssl.SSLContext.sslobject_class" title="ssl.SSLContext.sslobject_class"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.sslobject_class</span></code></a>
  2175. instead of hard-coded <a class="reference internal" href="#ssl.SSLObject" title="ssl.SSLObject"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLObject</span></code></a>.</p>
  2176. </div>
  2177. </dd></dl>
  2178. <dl class="py attribute">
  2179. <dt class="sig sig-object py" id="ssl.SSLContext.sslobject_class">
  2180. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">sslobject_class</span></span><a class="headerlink" href="#ssl.SSLContext.sslobject_class" title="Link to this definition">¶</a></dt>
  2181. <dd><p>The return type of <a class="reference internal" href="#ssl.SSLContext.wrap_bio" title="ssl.SSLContext.wrap_bio"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.wrap_bio()</span></code></a>, defaults to
  2182. <a class="reference internal" href="#ssl.SSLObject" title="ssl.SSLObject"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLObject</span></code></a>. The attribute can be overridden on instance of class
  2183. in order to return a custom subclass of <a class="reference internal" href="#ssl.SSLObject" title="ssl.SSLObject"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLObject</span></code></a>.</p>
  2184. <div class="versionadded">
  2185. <p><span class="versionmodified added">New in version 3.7.</span></p>
  2186. </div>
  2187. </dd></dl>
  2188. <dl class="py method">
  2189. <dt class="sig sig-object py" id="ssl.SSLContext.session_stats">
  2190. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">session_stats</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.SSLContext.session_stats" title="Link to this definition">¶</a></dt>
  2191. <dd><p>Get statistics about the SSL sessions created or managed by this context.
  2192. A dictionary is returned which maps the names of each <a class="reference external" href="https://www.openssl.org/docs/man1.1.1/man3/SSL_CTX_sess_number.html">piece of information</a> to their
  2193. numeric values. For example, here is the total number of hits and misses
  2194. in the session cache since the context was created:</p>
  2195. <div class="highlight-python3 notranslate"><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>
  2196. <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>
  2197. <span class="go">(0, 0)</span>
  2198. </pre></div>
  2199. </div>
  2200. </dd></dl>
  2201. <dl class="py attribute">
  2202. <dt class="sig sig-object py" id="ssl.SSLContext.check_hostname">
  2203. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">check_hostname</span></span><a class="headerlink" href="#ssl.SSLContext.check_hostname" title="Link to this definition">¶</a></dt>
  2204. <dd><p>Whether to match the peer cert’s hostname in
  2205. <a class="reference internal" href="#ssl.SSLSocket.do_handshake" title="ssl.SSLSocket.do_handshake"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.do_handshake()</span></code></a>. The context’s
  2206. <a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-attr docutils literal notranslate"><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 notranslate"><span class="pre">CERT_OPTIONAL</span></code></a> or
  2207. <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-data docutils literal notranslate"><span class="pre">CERT_REQUIRED</span></code></a>, and you must pass <em>server_hostname</em> to
  2208. <a class="reference internal" href="#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wrap_socket()</span></code></a> in order to match the hostname. Enabling
  2209. hostname checking automatically sets <a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">verify_mode</span></code></a> from
  2210. <a class="reference internal" href="#ssl.CERT_NONE" title="ssl.CERT_NONE"><code class="xref py py-data docutils literal notranslate"><span class="pre">CERT_NONE</span></code></a> to <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-data docutils literal notranslate"><span class="pre">CERT_REQUIRED</span></code></a>. It cannot be set back to
  2211. <a class="reference internal" href="#ssl.CERT_NONE" title="ssl.CERT_NONE"><code class="xref py py-data docutils literal notranslate"><span class="pre">CERT_NONE</span></code></a> as long as hostname checking is enabled. The
  2212. <a class="reference internal" href="#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a> protocol enables hostname checking by default.
  2213. With other protocols, hostname checking must be enabled explicitly.</p>
  2214. <p>Example:</p>
  2215. <div class="highlight-python3 notranslate"><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>
  2216. <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_2</span><span class="p">)</span>
  2217. <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>
  2218. <span class="n">context</span><span class="o">.</span><span class="n">check_hostname</span> <span class="o">=</span> <span class="kc">True</span>
  2219. <span class="n">context</span><span class="o">.</span><span class="n">load_default_certs</span><span class="p">()</span>
  2220. <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>
  2221. <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>
  2222. <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>
  2223. </pre></div>
  2224. </div>
  2225. <div class="versionadded">
  2226. <p><span class="versionmodified added">New in version 3.4.</span></p>
  2227. </div>
  2228. <div class="versionchanged">
  2229. <p><span class="versionmodified changed">Changed in version 3.7: </span><a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">verify_mode</span></code></a> is now automatically changed
  2230. to <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-data docutils literal notranslate"><span class="pre">CERT_REQUIRED</span></code></a> when hostname checking is enabled and
  2231. <a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">verify_mode</span></code></a> is <a class="reference internal" href="#ssl.CERT_NONE" title="ssl.CERT_NONE"><code class="xref py py-data docutils literal notranslate"><span class="pre">CERT_NONE</span></code></a>. Previously
  2232. the same operation would have failed with a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
  2233. </div>
  2234. </dd></dl>
  2235. <dl class="py attribute">
  2236. <dt class="sig sig-object py" id="ssl.SSLContext.keylog_filename">
  2237. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">keylog_filename</span></span><a class="headerlink" href="#ssl.SSLContext.keylog_filename" title="Link to this definition">¶</a></dt>
  2238. <dd><p>Write TLS keys to a keylog file, whenever key material is generated or
  2239. received. The keylog file is designed for debugging purposes only. The
  2240. file format is specified by NSS and used by many traffic analyzers such
  2241. as Wireshark. The log file is opened in append-only mode. Writes are
  2242. synchronized between threads, but not between processes.</p>
  2243. <div class="versionadded">
  2244. <p><span class="versionmodified added">New in version 3.8.</span></p>
  2245. </div>
  2246. </dd></dl>
  2247. <dl class="py attribute">
  2248. <dt class="sig sig-object py" id="ssl.SSLContext.maximum_version">
  2249. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">maximum_version</span></span><a class="headerlink" href="#ssl.SSLContext.maximum_version" title="Link to this definition">¶</a></dt>
  2250. <dd><p>A <a class="reference internal" href="#ssl.TLSVersion" title="ssl.TLSVersion"><code class="xref py py-class docutils literal notranslate"><span class="pre">TLSVersion</span></code></a> enum member representing the highest supported
  2251. TLS version. The value defaults to <a class="reference internal" href="#ssl.TLSVersion.MAXIMUM_SUPPORTED" title="ssl.TLSVersion.MAXIMUM_SUPPORTED"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TLSVersion.MAXIMUM_SUPPORTED</span></code></a>.
  2252. The attribute is read-only for protocols other than <a class="reference internal" href="#ssl.PROTOCOL_TLS" title="ssl.PROTOCOL_TLS"><code class="xref py py-attr docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code></a>,
  2253. <a class="reference internal" href="#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-attr docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a>, and <a class="reference internal" href="#ssl.PROTOCOL_TLS_SERVER" title="ssl.PROTOCOL_TLS_SERVER"><code class="xref py py-attr docutils literal notranslate"><span class="pre">PROTOCOL_TLS_SERVER</span></code></a>.</p>
  2254. <p>The attributes <a class="reference internal" href="#ssl.SSLContext.maximum_version" title="ssl.SSLContext.maximum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">maximum_version</span></code></a>,
  2255. <a class="reference internal" href="#ssl.SSLContext.minimum_version" title="ssl.SSLContext.minimum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">minimum_version</span></code></a> and
  2256. <a class="reference internal" href="#ssl.SSLContext.options" title="ssl.SSLContext.options"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.options</span></code></a> all affect the supported SSL
  2257. and TLS versions of the context. The implementation does not prevent
  2258. invalid combination. For example a context with
  2259. <a class="reference internal" href="#ssl.OP_NO_TLSv1_2" title="ssl.OP_NO_TLSv1_2"><code class="xref py py-attr docutils literal notranslate"><span class="pre">OP_NO_TLSv1_2</span></code></a> in <a class="reference internal" href="#ssl.SSLContext.options" title="ssl.SSLContext.options"><code class="xref py py-attr docutils literal notranslate"><span class="pre">options</span></code></a> and
  2260. <a class="reference internal" href="#ssl.SSLContext.maximum_version" title="ssl.SSLContext.maximum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">maximum_version</span></code></a> set to <a class="reference internal" href="#ssl.TLSVersion.TLSv1_2" title="ssl.TLSVersion.TLSv1_2"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TLSVersion.TLSv1_2</span></code></a>
  2261. will not be able to establish a TLS 1.2 connection.</p>
  2262. <div class="versionadded">
  2263. <p><span class="versionmodified added">New in version 3.7.</span></p>
  2264. </div>
  2265. </dd></dl>
  2266. <dl class="py attribute">
  2267. <dt class="sig sig-object py" id="ssl.SSLContext.minimum_version">
  2268. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">minimum_version</span></span><a class="headerlink" href="#ssl.SSLContext.minimum_version" title="Link to this definition">¶</a></dt>
  2269. <dd><p>Like <a class="reference internal" href="#ssl.SSLContext.maximum_version" title="ssl.SSLContext.maximum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.maximum_version</span></code></a> except it is the lowest
  2270. supported version or <a class="reference internal" href="#ssl.TLSVersion.MINIMUM_SUPPORTED" title="ssl.TLSVersion.MINIMUM_SUPPORTED"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TLSVersion.MINIMUM_SUPPORTED</span></code></a>.</p>
  2271. <div class="versionadded">
  2272. <p><span class="versionmodified added">New in version 3.7.</span></p>
  2273. </div>
  2274. </dd></dl>
  2275. <dl class="py attribute">
  2276. <dt class="sig sig-object py" id="ssl.SSLContext.num_tickets">
  2277. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">num_tickets</span></span><a class="headerlink" href="#ssl.SSLContext.num_tickets" title="Link to this definition">¶</a></dt>
  2278. <dd><p>Control the number of TLS 1.3 session tickets of a
  2279. <a class="reference internal" href="#ssl.PROTOCOL_TLS_SERVER" title="ssl.PROTOCOL_TLS_SERVER"><code class="xref py py-attr docutils literal notranslate"><span class="pre">PROTOCOL_TLS_SERVER</span></code></a> context. The setting has no impact on TLS
  2280. 1.0 to 1.2 connections.</p>
  2281. <div class="versionadded">
  2282. <p><span class="versionmodified added">New in version 3.8.</span></p>
  2283. </div>
  2284. </dd></dl>
  2285. <dl class="py attribute">
  2286. <dt class="sig sig-object py" id="ssl.SSLContext.options">
  2287. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">options</span></span><a class="headerlink" href="#ssl.SSLContext.options" title="Link to this definition">¶</a></dt>
  2288. <dd><p>An integer representing the set of SSL options enabled on this context.
  2289. The default value is <a class="reference internal" href="#ssl.OP_ALL" title="ssl.OP_ALL"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_ALL</span></code></a>, but you can specify other options
  2290. such as <a class="reference internal" href="#ssl.OP_NO_SSLv2" title="ssl.OP_NO_SSLv2"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_SSLv2</span></code></a> by ORing them together.</p>
  2291. <div class="versionchanged">
  2292. <p><span class="versionmodified changed">Changed in version 3.6: </span><a class="reference internal" href="#ssl.SSLContext.options" title="ssl.SSLContext.options"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.options</span></code></a> returns <a class="reference internal" href="#ssl.Options" title="ssl.Options"><code class="xref py py-class docutils literal notranslate"><span class="pre">Options</span></code></a> flags:</p>
  2293. <div class="doctest highlight-default notranslate"><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">create_default_context</span><span class="p">()</span><span class="o">.</span><span class="n">options</span>
  2294. <span class="go">&lt;Options.OP_ALL|OP_NO_SSLv3|OP_NO_SSLv2|OP_NO_COMPRESSION: 2197947391&gt;</span>
  2295. </pre></div>
  2296. </div>
  2297. </div>
  2298. <div class="deprecated">
  2299. <p><span class="versionmodified deprecated">Deprecated since version 3.7: </span>All <code class="docutils literal notranslate"><span class="pre">OP_NO_SSL*</span></code> and <code class="docutils literal notranslate"><span class="pre">OP_NO_TLS*</span></code> options have been deprecated since
  2300. Python 3.7. Use <a class="reference internal" href="#ssl.SSLContext.minimum_version" title="ssl.SSLContext.minimum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.minimum_version</span></code></a> and
  2301. <a class="reference internal" href="#ssl.SSLContext.maximum_version" title="ssl.SSLContext.maximum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.maximum_version</span></code></a> instead.</p>
  2302. </div>
  2303. </dd></dl>
  2304. <dl class="py attribute">
  2305. <dt class="sig sig-object py" id="ssl.SSLContext.post_handshake_auth">
  2306. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">post_handshake_auth</span></span><a class="headerlink" href="#ssl.SSLContext.post_handshake_auth" title="Link to this definition">¶</a></dt>
  2307. <dd><p>Enable TLS 1.3 post-handshake client authentication. Post-handshake auth
  2308. is disabled by default and a server can only request a TLS client
  2309. certificate during the initial handshake. When enabled, a server may
  2310. request a TLS client certificate at any time after the handshake.</p>
  2311. <p>When enabled on client-side sockets, the client signals the server that
  2312. it supports post-handshake authentication.</p>
  2313. <p>When enabled on server-side sockets, <a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.verify_mode</span></code></a> must
  2314. be set to <a class="reference internal" href="#ssl.CERT_OPTIONAL" title="ssl.CERT_OPTIONAL"><code class="xref py py-data docutils literal notranslate"><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 notranslate"><span class="pre">CERT_REQUIRED</span></code></a>, too. The
  2315. actual client cert exchange is delayed until
  2316. <a class="reference internal" href="#ssl.SSLSocket.verify_client_post_handshake" title="ssl.SSLSocket.verify_client_post_handshake"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.verify_client_post_handshake()</span></code></a> is called and some I/O is
  2317. performed.</p>
  2318. <div class="versionadded">
  2319. <p><span class="versionmodified added">New in version 3.8.</span></p>
  2320. </div>
  2321. </dd></dl>
  2322. <dl class="py attribute">
  2323. <dt class="sig sig-object py" id="ssl.SSLContext.protocol">
  2324. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">protocol</span></span><a class="headerlink" href="#ssl.SSLContext.protocol" title="Link to this definition">¶</a></dt>
  2325. <dd><p>The protocol version chosen when constructing the context. This attribute
  2326. is read-only.</p>
  2327. </dd></dl>
  2328. <dl class="py attribute">
  2329. <dt class="sig sig-object py" id="ssl.SSLContext.hostname_checks_common_name">
  2330. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">hostname_checks_common_name</span></span><a class="headerlink" href="#ssl.SSLContext.hostname_checks_common_name" title="Link to this definition">¶</a></dt>
  2331. <dd><p>Whether <a class="reference internal" href="#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">check_hostname</span></code></a> falls back to verify the cert’s
  2332. subject common name in the absence of a subject alternative name
  2333. extension (default: true).</p>
  2334. <div class="versionadded">
  2335. <p><span class="versionmodified added">New in version 3.7.</span></p>
  2336. </div>
  2337. <div class="versionchanged">
  2338. <p><span class="versionmodified changed">Changed in version 3.10: </span>The flag had no effect with OpenSSL before version 1.1.1l. Python 3.8.9,
  2339. 3.9.3, and 3.10 include workarounds for previous versions.</p>
  2340. </div>
  2341. </dd></dl>
  2342. <dl class="py attribute">
  2343. <dt class="sig sig-object py" id="ssl.SSLContext.security_level">
  2344. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">security_level</span></span><a class="headerlink" href="#ssl.SSLContext.security_level" title="Link to this definition">¶</a></dt>
  2345. <dd><p>An integer representing the <a class="reference external" href="https://www.openssl.org/docs/manmaster/man3/SSL_CTX_get_security_level.html">security level</a>
  2346. for the context. This attribute is read-only.</p>
  2347. <div class="versionadded">
  2348. <p><span class="versionmodified added">New in version 3.10.</span></p>
  2349. </div>
  2350. </dd></dl>
  2351. <dl class="py attribute">
  2352. <dt class="sig sig-object py" id="ssl.SSLContext.verify_flags">
  2353. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">verify_flags</span></span><a class="headerlink" href="#ssl.SSLContext.verify_flags" title="Link to this definition">¶</a></dt>
  2354. <dd><p>The flags for certificate verification operations. You can set flags like
  2355. <a class="reference internal" href="#ssl.VERIFY_CRL_CHECK_LEAF" title="ssl.VERIFY_CRL_CHECK_LEAF"><code class="xref py py-data docutils literal notranslate"><span class="pre">VERIFY_CRL_CHECK_LEAF</span></code></a> by ORing them together. By default OpenSSL
  2356. does neither require nor verify certificate revocation lists (CRLs).</p>
  2357. <div class="versionadded">
  2358. <p><span class="versionmodified added">New in version 3.4.</span></p>
  2359. </div>
  2360. <div class="versionchanged">
  2361. <p><span class="versionmodified changed">Changed in version 3.6: </span><a class="reference internal" href="#ssl.SSLContext.verify_flags" title="ssl.SSLContext.verify_flags"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.verify_flags</span></code></a> returns <a class="reference internal" href="#ssl.VerifyFlags" title="ssl.VerifyFlags"><code class="xref py py-class docutils literal notranslate"><span class="pre">VerifyFlags</span></code></a> flags:</p>
  2362. <div class="doctest highlight-default notranslate"><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">create_default_context</span><span class="p">()</span><span class="o">.</span><span class="n">verify_flags</span>
  2363. <span class="go">&lt;VerifyFlags.VERIFY_X509_TRUSTED_FIRST: 32768&gt;</span>
  2364. </pre></div>
  2365. </div>
  2366. </div>
  2367. </dd></dl>
  2368. <dl class="py attribute">
  2369. <dt class="sig sig-object py" id="ssl.SSLContext.verify_mode">
  2370. <span class="sig-prename descclassname"><span class="pre">SSLContext.</span></span><span class="sig-name descname"><span class="pre">verify_mode</span></span><a class="headerlink" href="#ssl.SSLContext.verify_mode" title="Link to this definition">¶</a></dt>
  2371. <dd><p>Whether to try to verify other peers’ certificates and how to behave
  2372. if verification fails. This attribute must be one of
  2373. <a class="reference internal" href="#ssl.CERT_NONE" title="ssl.CERT_NONE"><code class="xref py py-data docutils literal notranslate"><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 notranslate"><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 notranslate"><span class="pre">CERT_REQUIRED</span></code></a>.</p>
  2374. <div class="versionchanged">
  2375. <p><span class="versionmodified changed">Changed in version 3.6: </span><a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.verify_mode</span></code></a> returns <a class="reference internal" href="#ssl.VerifyMode" title="ssl.VerifyMode"><code class="xref py py-class docutils literal notranslate"><span class="pre">VerifyMode</span></code></a> enum:</p>
  2376. <div class="doctest highlight-default notranslate"><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">create_default_context</span><span class="p">()</span><span class="o">.</span><span class="n">verify_mode</span>
  2377. <span class="go">&lt;VerifyMode.CERT_REQUIRED: 2&gt;</span>
  2378. </pre></div>
  2379. </div>
  2380. </div>
  2381. </dd></dl>
  2382. </section>
  2383. <section id="certificates">
  2384. <span id="ssl-certificates"></span><span id="index-15"></span><span id="index-14"></span><h2>Certificates<a class="headerlink" href="#certificates" title="Link to this heading">¶</a></h2>
  2385. <p>Certificates in general are part of a public-key / private-key system. In this
  2386. system, each <em>principal</em>, (which may be a machine, or a person, or an
  2387. organization) is assigned a unique two-part encryption key. One part of the key
  2388. is public, and is called the <em>public key</em>; the other part is kept secret, and is
  2389. called the <em>private key</em>. The two parts are related, in that if you encrypt a
  2390. message with one of the parts, you can decrypt it with the other part, and
  2391. <strong>only</strong> with the other part.</p>
  2392. <p>A certificate contains information about two principals. It contains the name
  2393. of a <em>subject</em>, and the subject’s public key. It also contains a statement by a
  2394. second principal, the <em>issuer</em>, that the subject is who they claim to be, and
  2395. that this is indeed the subject’s public key. The issuer’s statement is signed
  2396. with the issuer’s private key, which only the issuer knows. However, anyone can
  2397. verify the issuer’s statement by finding the issuer’s public key, decrypting the
  2398. statement with it, and comparing it to the other information in the certificate.
  2399. The certificate also contains information about the time period over which it is
  2400. valid. This is expressed as two fields, called “notBefore” and “notAfter”.</p>
  2401. <p>In the Python use of certificates, a client or server can use a certificate to
  2402. prove who they are. The other side of a network connection can also be required
  2403. to produce a certificate, and that certificate can be validated to the
  2404. satisfaction of the client or server that requires such validation. The
  2405. connection attempt can be set to raise an exception if the validation fails.
  2406. Validation is done automatically, by the underlying OpenSSL framework; the
  2407. application need not concern itself with its mechanics. But the application
  2408. does usually need to provide sets of certificates to allow this process to take
  2409. place.</p>
  2410. <p>Python uses files to contain certificates. They should be formatted as “PEM”
  2411. (see <span class="target" id="index-16"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc1422.html"><strong>RFC 1422</strong></a>), which is a base-64 encoded form wrapped with a header line
  2412. and a footer line:</p>
  2413. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">-----</span><span class="n">BEGIN</span> <span class="n">CERTIFICATE</span><span class="o">-----</span>
  2414. <span class="o">...</span> <span class="p">(</span><span class="n">certificate</span> <span class="ow">in</span> <span class="n">base64</span> <span class="n">PEM</span> <span class="n">encoding</span><span class="p">)</span> <span class="o">...</span>
  2415. <span class="o">-----</span><span class="n">END</span> <span class="n">CERTIFICATE</span><span class="o">-----</span>
  2416. </pre></div>
  2417. </div>
  2418. <section id="certificate-chains">
  2419. <h3>Certificate chains<a class="headerlink" href="#certificate-chains" title="Link to this heading">¶</a></h3>
  2420. <p>The Python files which contain certificates can contain a sequence of
  2421. certificates, sometimes called a <em>certificate chain</em>. This chain should start
  2422. with the specific certificate for the principal who “is” the client or server,
  2423. and then the certificate for the issuer of that certificate, and then the
  2424. certificate for the issuer of <em>that</em> certificate, and so on up the chain till
  2425. you get to a certificate which is <em>self-signed</em>, that is, a certificate which
  2426. has the same subject and issuer, sometimes called a <em>root certificate</em>. The
  2427. certificates should just be concatenated together in the certificate file. For
  2428. example, suppose we had a three certificate chain, from our server certificate
  2429. to the certificate of the certification authority that signed our server
  2430. certificate, to the root certificate of the agency which issued the
  2431. certification authority’s certificate:</p>
  2432. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">-----</span><span class="n">BEGIN</span> <span class="n">CERTIFICATE</span><span class="o">-----</span>
  2433. <span class="o">...</span> <span class="p">(</span><span class="n">certificate</span> <span class="k">for</span> <span class="n">your</span> <span class="n">server</span><span class="p">)</span><span class="o">...</span>
  2434. <span class="o">-----</span><span class="n">END</span> <span class="n">CERTIFICATE</span><span class="o">-----</span>
  2435. <span class="o">-----</span><span class="n">BEGIN</span> <span class="n">CERTIFICATE</span><span class="o">-----</span>
  2436. <span class="o">...</span> <span class="p">(</span><span class="n">the</span> <span class="n">certificate</span> <span class="k">for</span> <span class="n">the</span> <span class="n">CA</span><span class="p">)</span><span class="o">...</span>
  2437. <span class="o">-----</span><span class="n">END</span> <span class="n">CERTIFICATE</span><span class="o">-----</span>
  2438. <span class="o">-----</span><span class="n">BEGIN</span> <span class="n">CERTIFICATE</span><span class="o">-----</span>
  2439. <span class="o">...</span> <span class="p">(</span><span class="n">the</span> <span class="n">root</span> <span class="n">certificate</span> <span class="k">for</span> <span class="n">the</span> <span class="n">CA</span><span class="s1">&#39;s issuer)...</span>
  2440. <span class="o">-----</span><span class="n">END</span> <span class="n">CERTIFICATE</span><span class="o">-----</span>
  2441. </pre></div>
  2442. </div>
  2443. </section>
  2444. <section id="ca-certificates">
  2445. <h3>CA certificates<a class="headerlink" href="#ca-certificates" title="Link to this heading">¶</a></h3>
  2446. <p>If you are going to require validation of the other side of the connection’s
  2447. certificate, you need to provide a “CA certs” file, filled with the certificate
  2448. chains for each issuer you are willing to trust. Again, this file just contains
  2449. these chains concatenated together. For validation, Python will use the first
  2450. chain it finds in the file which matches. The platform’s certificates file can
  2451. 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 notranslate"><span class="pre">SSLContext.load_default_certs()</span></code></a>, this is done
  2452. automatically with <a class="reference internal" href="#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_default_context()</span></code></a>.</p>
  2453. </section>
  2454. <section id="combined-key-and-certificate">
  2455. <h3>Combined key and certificate<a class="headerlink" href="#combined-key-and-certificate" title="Link to this heading">¶</a></h3>
  2456. <p>Often the private key is stored in the same file as the certificate; in this
  2457. case, only the <code class="docutils literal notranslate"><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 notranslate"><span class="pre">SSLContext.load_cert_chain()</span></code></a>
  2458. needs to be passed. If the private key is stored
  2459. with the certificate, it should come before the first certificate in
  2460. the certificate chain:</p>
  2461. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">-----</span><span class="n">BEGIN</span> <span class="n">RSA</span> <span class="n">PRIVATE</span> <span class="n">KEY</span><span class="o">-----</span>
  2462. <span class="o">...</span> <span class="p">(</span><span class="n">private</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">base64</span> <span class="n">encoding</span><span class="p">)</span> <span class="o">...</span>
  2463. <span class="o">-----</span><span class="n">END</span> <span class="n">RSA</span> <span class="n">PRIVATE</span> <span class="n">KEY</span><span class="o">-----</span>
  2464. <span class="o">-----</span><span class="n">BEGIN</span> <span class="n">CERTIFICATE</span><span class="o">-----</span>
  2465. <span class="o">...</span> <span class="p">(</span><span class="n">certificate</span> <span class="ow">in</span> <span class="n">base64</span> <span class="n">PEM</span> <span class="n">encoding</span><span class="p">)</span> <span class="o">...</span>
  2466. <span class="o">-----</span><span class="n">END</span> <span class="n">CERTIFICATE</span><span class="o">-----</span>
  2467. </pre></div>
  2468. </div>
  2469. </section>
  2470. <section id="self-signed-certificates">
  2471. <h3>Self-signed certificates<a class="headerlink" href="#self-signed-certificates" title="Link to this heading">¶</a></h3>
  2472. <p>If you are going to create a server that provides SSL-encrypted connection
  2473. services, you will need to acquire a certificate for that service. There are
  2474. many ways of acquiring appropriate certificates, such as buying one from a
  2475. certification authority. Another common practice is to generate a self-signed
  2476. certificate. The simplest way to do this is with the OpenSSL package, using
  2477. something like the following:</p>
  2478. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">%</span> <span class="n">openssl</span> <span class="n">req</span> <span class="o">-</span><span class="n">new</span> <span class="o">-</span><span class="n">x509</span> <span class="o">-</span><span class="n">days</span> <span class="mi">365</span> <span class="o">-</span><span class="n">nodes</span> <span class="o">-</span><span class="n">out</span> <span class="n">cert</span><span class="o">.</span><span class="n">pem</span> <span class="o">-</span><span class="n">keyout</span> <span class="n">cert</span><span class="o">.</span><span class="n">pem</span>
  2479. <span class="n">Generating</span> <span class="n">a</span> <span class="mi">1024</span> <span class="n">bit</span> <span class="n">RSA</span> <span class="n">private</span> <span class="n">key</span>
  2480. <span class="o">.......++++++</span>
  2481. <span class="o">.............................++++++</span>
  2482. <span class="n">writing</span> <span class="n">new</span> <span class="n">private</span> <span class="n">key</span> <span class="n">to</span> <span class="s1">&#39;cert.pem&#39;</span>
  2483. <span class="o">-----</span>
  2484. <span class="n">You</span> <span class="n">are</span> <span class="n">about</span> <span class="n">to</span> <span class="n">be</span> <span class="n">asked</span> <span class="n">to</span> <span class="n">enter</span> <span class="n">information</span> <span class="n">that</span> <span class="n">will</span> <span class="n">be</span> <span class="n">incorporated</span>
  2485. <span class="n">into</span> <span class="n">your</span> <span class="n">certificate</span> <span class="n">request</span><span class="o">.</span>
  2486. <span class="n">What</span> <span class="n">you</span> <span class="n">are</span> <span class="n">about</span> <span class="n">to</span> <span class="n">enter</span> <span class="ow">is</span> <span class="n">what</span> <span class="ow">is</span> <span class="n">called</span> <span class="n">a</span> <span class="n">Distinguished</span> <span class="n">Name</span> <span class="ow">or</span> <span class="n">a</span> <span class="n">DN</span><span class="o">.</span>
  2487. <span class="n">There</span> <span class="n">are</span> <span class="n">quite</span> <span class="n">a</span> <span class="n">few</span> <span class="n">fields</span> <span class="n">but</span> <span class="n">you</span> <span class="n">can</span> <span class="n">leave</span> <span class="n">some</span> <span class="n">blank</span>
  2488. <span class="n">For</span> <span class="n">some</span> <span class="n">fields</span> <span class="n">there</span> <span class="n">will</span> <span class="n">be</span> <span class="n">a</span> <span class="n">default</span> <span class="n">value</span><span class="p">,</span>
  2489. <span class="n">If</span> <span class="n">you</span> <span class="n">enter</span> <span class="s1">&#39;.&#39;</span><span class="p">,</span> <span class="n">the</span> <span class="n">field</span> <span class="n">will</span> <span class="n">be</span> <span class="n">left</span> <span class="n">blank</span><span class="o">.</span>
  2490. <span class="o">-----</span>
  2491. <span class="n">Country</span> <span class="n">Name</span> <span class="p">(</span><span class="mi">2</span> <span class="n">letter</span> <span class="n">code</span><span class="p">)</span> <span class="p">[</span><span class="n">AU</span><span class="p">]:</span><span class="n">US</span>
  2492. <span class="n">State</span> <span class="ow">or</span> <span class="n">Province</span> <span class="n">Name</span> <span class="p">(</span><span class="n">full</span> <span class="n">name</span><span class="p">)</span> <span class="p">[</span><span class="n">Some</span><span class="o">-</span><span class="n">State</span><span class="p">]:</span><span class="n">MyState</span>
  2493. <span class="n">Locality</span> <span class="n">Name</span> <span class="p">(</span><span class="n">eg</span><span class="p">,</span> <span class="n">city</span><span class="p">)</span> <span class="p">[]:</span><span class="n">Some</span> <span class="n">City</span>
  2494. <span class="n">Organization</span> <span class="n">Name</span> <span class="p">(</span><span class="n">eg</span><span class="p">,</span> <span class="n">company</span><span class="p">)</span> <span class="p">[</span><span class="n">Internet</span> <span class="n">Widgits</span> <span class="n">Pty</span> <span class="n">Ltd</span><span class="p">]:</span><span class="n">My</span> <span class="n">Organization</span><span class="p">,</span> <span class="n">Inc</span><span class="o">.</span>
  2495. <span class="n">Organizational</span> <span class="n">Unit</span> <span class="n">Name</span> <span class="p">(</span><span class="n">eg</span><span class="p">,</span> <span class="n">section</span><span class="p">)</span> <span class="p">[]:</span><span class="n">My</span> <span class="n">Group</span>
  2496. <span class="n">Common</span> <span class="n">Name</span> <span class="p">(</span><span class="n">eg</span><span class="p">,</span> <span class="n">YOUR</span> <span class="n">name</span><span class="p">)</span> <span class="p">[]:</span><span class="n">myserver</span><span class="o">.</span><span class="n">mygroup</span><span class="o">.</span><span class="n">myorganization</span><span class="o">.</span><span class="n">com</span>
  2497. <span class="n">Email</span> <span class="n">Address</span> <span class="p">[]:</span><span class="n">ops</span><span class="nd">@myserver</span><span class="o">.</span><span class="n">mygroup</span><span class="o">.</span><span class="n">myorganization</span><span class="o">.</span><span class="n">com</span>
  2498. <span class="o">%</span>
  2499. </pre></div>
  2500. </div>
  2501. <p>The disadvantage of a self-signed certificate is that it is its own root
  2502. certificate, and no one else will have it in their cache of known (and trusted)
  2503. root certificates.</p>
  2504. </section>
  2505. </section>
  2506. <section id="examples">
  2507. <h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  2508. <section id="testing-for-ssl-support">
  2509. <h3>Testing for SSL support<a class="headerlink" href="#testing-for-ssl-support" title="Link to this heading">¶</a></h3>
  2510. <p>To test for the presence of SSL support in a Python installation, user code
  2511. should use the following idiom:</p>
  2512. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
  2513. <span class="kn">import</span> <span class="nn">ssl</span>
  2514. <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
  2515. <span class="k">pass</span>
  2516. <span class="k">else</span><span class="p">:</span>
  2517. <span class="o">...</span> <span class="c1"># do something that requires SSL support</span>
  2518. </pre></div>
  2519. </div>
  2520. </section>
  2521. <section id="client-side-operation">
  2522. <h3>Client-side operation<a class="headerlink" href="#client-side-operation" title="Link to this heading">¶</a></h3>
  2523. <p>This example creates a SSL context with the recommended security settings
  2524. for client sockets, including automatic certificate verification:</p>
  2525. <div class="highlight-python3 notranslate"><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>
  2526. </pre></div>
  2527. </div>
  2528. <p>If you prefer to tune security settings yourself, you might create
  2529. a context from scratch (but beware that you might not get the settings
  2530. right):</p>
  2531. <div class="highlight-python3 notranslate"><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_TLS_CLIENT</span><span class="p">)</span>
  2532. <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>
  2533. </pre></div>
  2534. </div>
  2535. <p>(this snippet assumes your operating system places a bundle of all CA
  2536. certificates in <code class="docutils literal notranslate"><span class="pre">/etc/ssl/certs/ca-bundle.crt</span></code>; if not, you’ll get an
  2537. error and have to adjust the location)</p>
  2538. <p>The <a class="reference internal" href="#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a> protocol configures the context for cert
  2539. validation and hostname verification. <a class="reference internal" href="#ssl.SSLContext.verify_mode" title="ssl.SSLContext.verify_mode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">verify_mode</span></code></a> is
  2540. set to <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-data docutils literal notranslate"><span class="pre">CERT_REQUIRED</span></code></a> and <a class="reference internal" href="#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">check_hostname</span></code></a> is set
  2541. to <code class="docutils literal notranslate"><span class="pre">True</span></code>. All other protocols create SSL contexts with insecure defaults.</p>
  2542. <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 notranslate"><span class="pre">CERT_REQUIRED</span></code></a>
  2543. and <a class="reference internal" href="#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">check_hostname</span></code></a> validate the server certificate: it
  2544. ensures that the server certificate was signed with one of the CA
  2545. certificates, checks the signature for correctness, and verifies other
  2546. properties like validity and identity of the hostname:</p>
  2547. <div class="highlight-python3 notranslate"><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>
  2548. <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>
  2549. <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>
  2550. </pre></div>
  2551. </div>
  2552. <p>You may then fetch the certificate:</p>
  2553. <div class="highlight-python3 notranslate"><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>
  2554. </pre></div>
  2555. </div>
  2556. <p>Visual inspection shows that the certificate does identify the desired service
  2557. (that is, the HTTPS host <code class="docutils literal notranslate"><span class="pre">www.python.org</span></code>):</p>
  2558. <div class="highlight-python3 notranslate"><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>
  2559. <span class="go">{&#39;OCSP&#39;: (&#39;http://ocsp.digicert.com&#39;,),</span>
  2560. <span class="go"> &#39;caIssuers&#39;: (&#39;http://cacerts.digicert.com/DigiCertSHA2ExtendedValidationServerCA.crt&#39;,),</span>
  2561. <span class="go"> &#39;crlDistributionPoints&#39;: (&#39;http://crl3.digicert.com/sha2-ev-server-g1.crl&#39;,</span>
  2562. <span class="go"> &#39;http://crl4.digicert.com/sha2-ev-server-g1.crl&#39;),</span>
  2563. <span class="go"> &#39;issuer&#39;: (((&#39;countryName&#39;, &#39;US&#39;),),</span>
  2564. <span class="go"> ((&#39;organizationName&#39;, &#39;DigiCert Inc&#39;),),</span>
  2565. <span class="go"> ((&#39;organizationalUnitName&#39;, &#39;www.digicert.com&#39;),),</span>
  2566. <span class="go"> ((&#39;commonName&#39;, &#39;DigiCert SHA2 Extended Validation Server CA&#39;),)),</span>
  2567. <span class="go"> &#39;notAfter&#39;: &#39;Sep 9 12:00:00 2016 GMT&#39;,</span>
  2568. <span class="go"> &#39;notBefore&#39;: &#39;Sep 5 00:00:00 2014 GMT&#39;,</span>
  2569. <span class="go"> &#39;serialNumber&#39;: &#39;01BB6F00122B177F36CAB49CEA8B6B26&#39;,</span>
  2570. <span class="go"> &#39;subject&#39;: (((&#39;businessCategory&#39;, &#39;Private Organization&#39;),),</span>
  2571. <span class="go"> ((&#39;1.3.6.1.4.1.311.60.2.1.3&#39;, &#39;US&#39;),),</span>
  2572. <span class="go"> ((&#39;1.3.6.1.4.1.311.60.2.1.2&#39;, &#39;Delaware&#39;),),</span>
  2573. <span class="go"> ((&#39;serialNumber&#39;, &#39;3359300&#39;),),</span>
  2574. <span class="go"> ((&#39;streetAddress&#39;, &#39;16 Allen Rd&#39;),),</span>
  2575. <span class="go"> ((&#39;postalCode&#39;, &#39;03894-4801&#39;),),</span>
  2576. <span class="go"> ((&#39;countryName&#39;, &#39;US&#39;),),</span>
  2577. <span class="go"> ((&#39;stateOrProvinceName&#39;, &#39;NH&#39;),),</span>
  2578. <span class="go"> ((&#39;localityName&#39;, &#39;Wolfeboro&#39;),),</span>
  2579. <span class="go"> ((&#39;organizationName&#39;, &#39;Python Software Foundation&#39;),),</span>
  2580. <span class="go"> ((&#39;commonName&#39;, &#39;www.python.org&#39;),)),</span>
  2581. <span class="go"> &#39;subjectAltName&#39;: ((&#39;DNS&#39;, &#39;www.python.org&#39;),</span>
  2582. <span class="go"> (&#39;DNS&#39;, &#39;python.org&#39;),</span>
  2583. <span class="go"> (&#39;DNS&#39;, &#39;pypi.org&#39;),</span>
  2584. <span class="go"> (&#39;DNS&#39;, &#39;docs.python.org&#39;),</span>
  2585. <span class="go"> (&#39;DNS&#39;, &#39;testpypi.org&#39;),</span>
  2586. <span class="go"> (&#39;DNS&#39;, &#39;bugs.python.org&#39;),</span>
  2587. <span class="go"> (&#39;DNS&#39;, &#39;wiki.python.org&#39;),</span>
  2588. <span class="go"> (&#39;DNS&#39;, &#39;hg.python.org&#39;),</span>
  2589. <span class="go"> (&#39;DNS&#39;, &#39;mail.python.org&#39;),</span>
  2590. <span class="go"> (&#39;DNS&#39;, &#39;packaging.python.org&#39;),</span>
  2591. <span class="go"> (&#39;DNS&#39;, &#39;pythonhosted.org&#39;),</span>
  2592. <span class="go"> (&#39;DNS&#39;, &#39;www.pythonhosted.org&#39;),</span>
  2593. <span class="go"> (&#39;DNS&#39;, &#39;test.pythonhosted.org&#39;),</span>
  2594. <span class="go"> (&#39;DNS&#39;, &#39;us.pycon.org&#39;),</span>
  2595. <span class="go"> (&#39;DNS&#39;, &#39;id.python.org&#39;)),</span>
  2596. <span class="go"> &#39;version&#39;: 3}</span>
  2597. </pre></div>
  2598. </div>
  2599. <p>Now the SSL channel is established and the certificate verified, you can
  2600. proceed to talk with the server:</p>
  2601. <div class="highlight-python3 notranslate"><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="sa">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>
  2602. <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="sa">b</span><span class="s2">&quot;</span><span class="se">\r\n</span><span class="s2">&quot;</span><span class="p">))</span>
  2603. <span class="go">[b&#39;HTTP/1.1 200 OK&#39;,</span>
  2604. <span class="go"> b&#39;Date: Sat, 18 Oct 2014 18:27:20 GMT&#39;,</span>
  2605. <span class="go"> b&#39;Server: nginx&#39;,</span>
  2606. <span class="go"> b&#39;Content-Type: text/html; charset=utf-8&#39;,</span>
  2607. <span class="go"> b&#39;X-Frame-Options: SAMEORIGIN&#39;,</span>
  2608. <span class="go"> b&#39;Content-Length: 45679&#39;,</span>
  2609. <span class="go"> b&#39;Accept-Ranges: bytes&#39;,</span>
  2610. <span class="go"> b&#39;Via: 1.1 varnish&#39;,</span>
  2611. <span class="go"> b&#39;Age: 2188&#39;,</span>
  2612. <span class="go"> b&#39;X-Served-By: cache-lcy1134-LCY&#39;,</span>
  2613. <span class="go"> b&#39;X-Cache: HIT&#39;,</span>
  2614. <span class="go"> b&#39;X-Cache-Hits: 11&#39;,</span>
  2615. <span class="go"> b&#39;Vary: Cookie&#39;,</span>
  2616. <span class="go"> b&#39;Strict-Transport-Security: max-age=63072000; includeSubDomains&#39;,</span>
  2617. <span class="go"> b&#39;Connection: close&#39;,</span>
  2618. <span class="go"> b&#39;&#39;,</span>
  2619. <span class="go"> b&#39;&#39;]</span>
  2620. </pre></div>
  2621. </div>
  2622. <p>See the discussion of <a class="reference internal" href="#ssl-security"><span class="std std-ref">Security considerations</span></a> below.</p>
  2623. </section>
  2624. <section id="server-side-operation">
  2625. <h3>Server-side operation<a class="headerlink" href="#server-side-operation" title="Link to this heading">¶</a></h3>
  2626. <p>For server operation, typically you’ll need to have a server certificate, and
  2627. private key, each in a file. You’ll first create a context holding the key
  2628. and the certificate, so that clients can check your authenticity. Then
  2629. you’ll open a socket, bind it to a port, call <code class="xref py py-meth docutils literal notranslate"><span class="pre">listen()</span></code> on it, and start
  2630. waiting for clients to connect:</p>
  2631. <div class="highlight-python3 notranslate"><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>
  2632. <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>
  2633. <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>
  2634. <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>
  2635. <span class="n">bindsocket</span><span class="o">.</span><span class="n">bind</span><span class="p">((</span><span class="s1">&#39;myaddr.example.com&#39;</span><span class="p">,</span> <span class="mi">10023</span><span class="p">))</span>
  2636. <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>
  2637. </pre></div>
  2638. </div>
  2639. <p>When a client connects, you’ll call <code class="xref py py-meth docutils literal notranslate"><span class="pre">accept()</span></code> on the socket to get the
  2640. new socket from the other end, and use the context’s <a class="reference internal" href="#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.wrap_socket()</span></code></a>
  2641. method to create a server-side SSL socket for the connection:</p>
  2642. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
  2643. <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>
  2644. <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="kc">True</span><span class="p">)</span>
  2645. <span class="k">try</span><span class="p">:</span>
  2646. <span class="n">deal_with_client</span><span class="p">(</span><span class="n">connstream</span><span class="p">)</span>
  2647. <span class="k">finally</span><span class="p">:</span>
  2648. <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>
  2649. <span class="n">connstream</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2650. </pre></div>
  2651. </div>
  2652. <p>Then you’ll read data from the <code class="docutils literal notranslate"><span class="pre">connstream</span></code> and do something with it till you
  2653. are finished with the client (or the client is finished with you):</p>
  2654. <div class="highlight-python3 notranslate"><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>
  2655. <span class="n">data</span> <span class="o">=</span> <span class="n">connstream</span><span class="o">.</span><span class="n">recv</span><span class="p">(</span><span class="mi">1024</span><span class="p">)</span>
  2656. <span class="c1"># empty data means the client is finished with us</span>
  2657. <span class="k">while</span> <span class="n">data</span><span class="p">:</span>
  2658. <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>
  2659. <span class="c1"># we&#39;ll assume do_something returns False</span>
  2660. <span class="c1"># when we&#39;re finished with client</span>
  2661. <span class="k">break</span>
  2662. <span class="n">data</span> <span class="o">=</span> <span class="n">connstream</span><span class="o">.</span><span class="n">recv</span><span class="p">(</span><span class="mi">1024</span><span class="p">)</span>
  2663. <span class="c1"># finished with client</span>
  2664. </pre></div>
  2665. </div>
  2666. <p>And go back to listening for new client connections (of course, a real server
  2667. would probably handle each client connection in a separate thread, or put
  2668. the sockets in <a class="reference internal" href="#ssl-nonblocking"><span class="std std-ref">non-blocking mode</span></a> and use an event loop).</p>
  2669. </section>
  2670. </section>
  2671. <section id="notes-on-non-blocking-sockets">
  2672. <span id="ssl-nonblocking"></span><h2>Notes on non-blocking sockets<a class="headerlink" href="#notes-on-non-blocking-sockets" title="Link to this heading">¶</a></h2>
  2673. <p>SSL sockets behave slightly different than regular sockets in
  2674. non-blocking mode. When working with non-blocking sockets, there are
  2675. thus several things you need to be aware of:</p>
  2676. <ul>
  2677. <li><p>Most <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> methods will raise either
  2678. <a class="reference internal" href="#ssl.SSLWantWriteError" title="ssl.SSLWantWriteError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLWantWriteError</span></code></a> or <a class="reference internal" href="#ssl.SSLWantReadError" title="ssl.SSLWantReadError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLWantReadError</span></code></a> instead of
  2679. <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BlockingIOError</span></code></a> if an I/O operation would
  2680. block. <a class="reference internal" href="#ssl.SSLWantReadError" title="ssl.SSLWantReadError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLWantReadError</span></code></a> will be raised if a read operation on
  2681. the underlying socket is necessary, and <a class="reference internal" href="#ssl.SSLWantWriteError" title="ssl.SSLWantWriteError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLWantWriteError</span></code></a> for
  2682. a write operation on the underlying socket. Note that attempts to
  2683. <em>write</em> to an SSL socket may require <em>reading</em> from the underlying
  2684. socket first, and attempts to <em>read</em> from the SSL socket may require
  2685. a prior <em>write</em> to the underlying socket.</p>
  2686. <div class="versionchanged">
  2687. <p><span class="versionmodified changed">Changed in version 3.5: </span>In earlier Python versions, the <code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.send()</span></code> method
  2688. returned zero instead of raising <a class="reference internal" href="#ssl.SSLWantWriteError" title="ssl.SSLWantWriteError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLWantWriteError</span></code></a> or
  2689. <a class="reference internal" href="#ssl.SSLWantReadError" title="ssl.SSLWantReadError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLWantReadError</span></code></a>.</p>
  2690. </div>
  2691. </li>
  2692. <li><p>Calling <a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select()</span></code></a> tells you that the OS-level socket can be
  2693. read from (or written to), but it does not imply that there is sufficient
  2694. data at the upper SSL layer. For example, only part of an SSL frame might
  2695. have arrived. Therefore, you must be ready to handle <code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.recv()</span></code>
  2696. and <code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.send()</span></code> failures, and retry after another call to
  2697. <a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select()</span></code></a>.</p></li>
  2698. <li><p>Conversely, since the SSL layer has its own framing, a SSL socket may
  2699. 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 notranslate"><span class="pre">select()</span></code></a>
  2700. being aware of it. Therefore, you should first call
  2701. <code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.recv()</span></code> to drain any potentially available data, and then
  2702. only block on a <a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select()</span></code></a> call if still necessary.</p>
  2703. <p>(of course, similar provisions apply when using other primitives such as
  2704. <a class="reference internal" href="select.html#select.poll" title="select.poll"><code class="xref py py-func docutils literal notranslate"><span class="pre">poll()</span></code></a>, or those in the <a class="reference internal" href="selectors.html#module-selectors" title="selectors: High-level I/O multiplexing."><code class="xref py py-mod docutils literal notranslate"><span class="pre">selectors</span></code></a> module)</p>
  2705. </li>
  2706. <li><p>The SSL handshake itself will be non-blocking: the
  2707. <a class="reference internal" href="#ssl.SSLSocket.do_handshake" title="ssl.SSLSocket.do_handshake"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.do_handshake()</span></code></a> method has to be retried until it returns
  2708. 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 notranslate"><span class="pre">select()</span></code></a> to wait for
  2709. the socket’s readiness:</p>
  2710. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
  2711. <span class="k">try</span><span class="p">:</span>
  2712. <span class="n">sock</span><span class="o">.</span><span class="n">do_handshake</span><span class="p">()</span>
  2713. <span class="k">break</span>
  2714. <span class="k">except</span> <span class="n">ssl</span><span class="o">.</span><span class="n">SSLWantReadError</span><span class="p">:</span>
  2715. <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>
  2716. <span class="k">except</span> <span class="n">ssl</span><span class="o">.</span><span class="n">SSLWantWriteError</span><span class="p">:</span>
  2717. <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>
  2718. </pre></div>
  2719. </div>
  2720. </li>
  2721. </ul>
  2722. <div class="admonition seealso">
  2723. <p class="admonition-title">See also</p>
  2724. <p>The <a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code></a> module supports <a class="reference internal" href="#ssl-nonblocking"><span class="std std-ref">non-blocking SSL sockets</span></a> and provides a
  2725. higher level API. It polls for events using the <a class="reference internal" href="selectors.html#module-selectors" title="selectors: High-level I/O multiplexing."><code class="xref py py-mod docutils literal notranslate"><span class="pre">selectors</span></code></a> module and
  2726. handles <a class="reference internal" href="#ssl.SSLWantWriteError" title="ssl.SSLWantWriteError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLWantWriteError</span></code></a>, <a class="reference internal" href="#ssl.SSLWantReadError" title="ssl.SSLWantReadError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLWantReadError</span></code></a> and
  2727. <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BlockingIOError</span></code></a> exceptions. It runs the SSL handshake asynchronously
  2728. as well.</p>
  2729. </div>
  2730. </section>
  2731. <section id="memory-bio-support">
  2732. <h2>Memory BIO Support<a class="headerlink" href="#memory-bio-support" title="Link to this heading">¶</a></h2>
  2733. <div class="versionadded">
  2734. <p><span class="versionmodified added">New in version 3.5.</span></p>
  2735. </div>
  2736. <p>Ever since the SSL module was introduced in Python 2.6, the <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a>
  2737. class has provided two related but distinct areas of functionality:</p>
  2738. <ul class="simple">
  2739. <li><p>SSL protocol handling</p></li>
  2740. <li><p>Network IO</p></li>
  2741. </ul>
  2742. <p>The network IO API is identical to that provided by <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal notranslate"><span class="pre">socket.socket</span></code></a>,
  2743. from which <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> also inherits. This allows an SSL socket to be
  2744. used as a drop-in replacement for a regular socket, making it very easy to add
  2745. SSL support to an existing application.</p>
  2746. <p>Combining SSL protocol handling and network IO usually works well, but there
  2747. are some cases where it doesn’t. An example is async IO frameworks that want to
  2748. use a different IO multiplexing model than the “select/poll on a file
  2749. descriptor” (readiness based) model that is assumed by <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal notranslate"><span class="pre">socket.socket</span></code></a>
  2750. and by the internal OpenSSL socket IO routines. This is mostly relevant for
  2751. platforms like Windows where this model is not efficient. For this purpose, a
  2752. reduced scope variant of <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> called <a class="reference internal" href="#ssl.SSLObject" title="ssl.SSLObject"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLObject</span></code></a> is
  2753. provided.</p>
  2754. <dl class="py class">
  2755. <dt class="sig sig-object py" id="ssl.SSLObject">
  2756. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">SSLObject</span></span><a class="headerlink" href="#ssl.SSLObject" title="Link to this definition">¶</a></dt>
  2757. <dd><p>A reduced-scope variant of <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> representing an SSL protocol
  2758. instance that does not contain any network IO methods. This class is
  2759. typically used by framework authors that want to implement asynchronous IO
  2760. for SSL through memory buffers.</p>
  2761. <p>This class implements an interface on top of a low-level SSL object as
  2762. implemented by OpenSSL. This object captures the state of an SSL connection
  2763. but does not provide any network IO itself. IO needs to be performed through
  2764. separate “BIO” objects which are OpenSSL’s IO abstraction layer.</p>
  2765. <p>This class has no public constructor. An <a class="reference internal" href="#ssl.SSLObject" title="ssl.SSLObject"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLObject</span></code></a> instance
  2766. must be created using the <a class="reference internal" href="#ssl.SSLContext.wrap_bio" title="ssl.SSLContext.wrap_bio"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wrap_bio()</span></code></a> method. This
  2767. method will create the <a class="reference internal" href="#ssl.SSLObject" title="ssl.SSLObject"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLObject</span></code></a> instance and bind it to a
  2768. pair of BIOs. The <em>incoming</em> BIO is used to pass data from Python to the
  2769. SSL protocol instance, while the <em>outgoing</em> BIO is used to pass data the
  2770. other way around.</p>
  2771. <p>The following methods are available:</p>
  2772. <ul class="simple">
  2773. <li><p><a class="reference internal" href="#ssl.SSLSocket.context" title="ssl.SSLSocket.context"><code class="xref py py-attr docutils literal notranslate"><span class="pre">context</span></code></a></p></li>
  2774. <li><p><a class="reference internal" href="#ssl.SSLSocket.server_side" title="ssl.SSLSocket.server_side"><code class="xref py py-attr docutils literal notranslate"><span class="pre">server_side</span></code></a></p></li>
  2775. <li><p><a class="reference internal" href="#ssl.SSLSocket.server_hostname" title="ssl.SSLSocket.server_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">server_hostname</span></code></a></p></li>
  2776. <li><p><a class="reference internal" href="#ssl.SSLSocket.session" title="ssl.SSLSocket.session"><code class="xref py py-attr docutils literal notranslate"><span class="pre">session</span></code></a></p></li>
  2777. <li><p><a class="reference internal" href="#ssl.SSLSocket.session_reused" title="ssl.SSLSocket.session_reused"><code class="xref py py-attr docutils literal notranslate"><span class="pre">session_reused</span></code></a></p></li>
  2778. <li><p><a class="reference internal" href="#ssl.SSLSocket.read" title="ssl.SSLSocket.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a></p></li>
  2779. <li><p><a class="reference internal" href="#ssl.SSLSocket.write" title="ssl.SSLSocket.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a></p></li>
  2780. <li><p><a class="reference internal" href="#ssl.SSLSocket.getpeercert" title="ssl.SSLSocket.getpeercert"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getpeercert()</span></code></a></p></li>
  2781. <li><p><a class="reference internal" href="#ssl.SSLSocket.selected_alpn_protocol" title="ssl.SSLSocket.selected_alpn_protocol"><code class="xref py py-meth docutils literal notranslate"><span class="pre">selected_alpn_protocol()</span></code></a></p></li>
  2782. <li><p><a class="reference internal" href="#ssl.SSLSocket.selected_npn_protocol" title="ssl.SSLSocket.selected_npn_protocol"><code class="xref py py-meth docutils literal notranslate"><span class="pre">selected_npn_protocol()</span></code></a></p></li>
  2783. <li><p><a class="reference internal" href="#ssl.SSLSocket.cipher" title="ssl.SSLSocket.cipher"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cipher()</span></code></a></p></li>
  2784. <li><p><a class="reference internal" href="#ssl.SSLSocket.shared_ciphers" title="ssl.SSLSocket.shared_ciphers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">shared_ciphers()</span></code></a></p></li>
  2785. <li><p><a class="reference internal" href="#ssl.SSLSocket.compression" title="ssl.SSLSocket.compression"><code class="xref py py-meth docutils literal notranslate"><span class="pre">compression()</span></code></a></p></li>
  2786. <li><p><a class="reference internal" href="#ssl.SSLSocket.pending" title="ssl.SSLSocket.pending"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pending()</span></code></a></p></li>
  2787. <li><p><a class="reference internal" href="#ssl.SSLSocket.do_handshake" title="ssl.SSLSocket.do_handshake"><code class="xref py py-meth docutils literal notranslate"><span class="pre">do_handshake()</span></code></a></p></li>
  2788. <li><p><a class="reference internal" href="#ssl.SSLSocket.verify_client_post_handshake" title="ssl.SSLSocket.verify_client_post_handshake"><code class="xref py py-meth docutils literal notranslate"><span class="pre">verify_client_post_handshake()</span></code></a></p></li>
  2789. <li><p><a class="reference internal" href="#ssl.SSLSocket.unwrap" title="ssl.SSLSocket.unwrap"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unwrap()</span></code></a></p></li>
  2790. <li><p><a class="reference internal" href="#ssl.SSLSocket.get_channel_binding" title="ssl.SSLSocket.get_channel_binding"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_channel_binding()</span></code></a></p></li>
  2791. <li><p><a class="reference internal" href="#ssl.SSLSocket.version" title="ssl.SSLSocket.version"><code class="xref py py-meth docutils literal notranslate"><span class="pre">version()</span></code></a></p></li>
  2792. </ul>
  2793. <p>When compared to <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a>, this object lacks the following
  2794. features:</p>
  2795. <ul class="simple">
  2796. <li><p>Any form of network IO; <code class="docutils literal notranslate"><span class="pre">recv()</span></code> and <code class="docutils literal notranslate"><span class="pre">send()</span></code> read and write only to
  2797. the underlying <a class="reference internal" href="#ssl.MemoryBIO" title="ssl.MemoryBIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">MemoryBIO</span></code></a> buffers.</p></li>
  2798. <li><p>There is no <em>do_handshake_on_connect</em> machinery. You must always manually
  2799. call <a class="reference internal" href="#ssl.SSLSocket.do_handshake" title="ssl.SSLSocket.do_handshake"><code class="xref py py-meth docutils literal notranslate"><span class="pre">do_handshake()</span></code></a> to start the handshake.</p></li>
  2800. <li><p>There is no handling of <em>suppress_ragged_eofs</em>. All end-of-file conditions
  2801. that are in violation of the protocol are reported via the
  2802. <a class="reference internal" href="#ssl.SSLEOFError" title="ssl.SSLEOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLEOFError</span></code></a> exception.</p></li>
  2803. <li><p>The method <a class="reference internal" href="#ssl.SSLSocket.unwrap" title="ssl.SSLSocket.unwrap"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unwrap()</span></code></a> call does not return anything,
  2804. unlike for an SSL socket where it returns the underlying socket.</p></li>
  2805. <li><p>The <em>server_name_callback</em> callback passed to
  2806. <a class="reference internal" href="#ssl.SSLContext.set_servername_callback" title="ssl.SSLContext.set_servername_callback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.set_servername_callback()</span></code></a> will get an <a class="reference internal" href="#ssl.SSLObject" title="ssl.SSLObject"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLObject</span></code></a>
  2807. instance instead of a <a class="reference internal" href="#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> instance as its first parameter.</p></li>
  2808. </ul>
  2809. <p>Some notes related to the use of <a class="reference internal" href="#ssl.SSLObject" title="ssl.SSLObject"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLObject</span></code></a>:</p>
  2810. <ul class="simple">
  2811. <li><p>All IO on an <a class="reference internal" href="#ssl.SSLObject" title="ssl.SSLObject"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLObject</span></code></a> is <a class="reference internal" href="#ssl-nonblocking"><span class="std std-ref">non-blocking</span></a>.
  2812. This means that for example <a class="reference internal" href="#ssl.SSLSocket.read" title="ssl.SSLSocket.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> will raise an
  2813. <a class="reference internal" href="#ssl.SSLWantReadError" title="ssl.SSLWantReadError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLWantReadError</span></code></a> if it needs more data than the incoming BIO has
  2814. available.</p></li>
  2815. </ul>
  2816. <div class="versionchanged">
  2817. <p><span class="versionmodified changed">Changed in version 3.7: </span><a class="reference internal" href="#ssl.SSLObject" title="ssl.SSLObject"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLObject</span></code></a> instances must be created with
  2818. <a class="reference internal" href="#ssl.SSLContext.wrap_bio" title="ssl.SSLContext.wrap_bio"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wrap_bio()</span></code></a>. In earlier versions, it was possible to
  2819. create instances directly. This was never documented or officially
  2820. supported.</p>
  2821. </div>
  2822. </dd></dl>
  2823. <p>An SSLObject communicates with the outside world using memory buffers. The
  2824. class <a class="reference internal" href="#ssl.MemoryBIO" title="ssl.MemoryBIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">MemoryBIO</span></code></a> provides a memory buffer that can be used for this
  2825. purpose. It wraps an OpenSSL memory BIO (Basic IO) object:</p>
  2826. <dl class="py class">
  2827. <dt class="sig sig-object py" id="ssl.MemoryBIO">
  2828. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">MemoryBIO</span></span><a class="headerlink" href="#ssl.MemoryBIO" title="Link to this definition">¶</a></dt>
  2829. <dd><p>A memory buffer that can be used to pass data between Python and an SSL
  2830. protocol instance.</p>
  2831. <dl class="py attribute">
  2832. <dt class="sig sig-object py" id="ssl.MemoryBIO.pending">
  2833. <span class="sig-name descname"><span class="pre">pending</span></span><a class="headerlink" href="#ssl.MemoryBIO.pending" title="Link to this definition">¶</a></dt>
  2834. <dd><p>Return the number of bytes currently in the memory buffer.</p>
  2835. </dd></dl>
  2836. <dl class="py attribute">
  2837. <dt class="sig sig-object py" id="ssl.MemoryBIO.eof">
  2838. <span class="sig-name descname"><span class="pre">eof</span></span><a class="headerlink" href="#ssl.MemoryBIO.eof" title="Link to this definition">¶</a></dt>
  2839. <dd><p>A boolean indicating whether the memory BIO is current at the end-of-file
  2840. position.</p>
  2841. </dd></dl>
  2842. <dl class="py method">
  2843. <dt class="sig sig-object py" id="ssl.MemoryBIO.read">
  2844. <span class="sig-name descname"><span class="pre">read</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.MemoryBIO.read" title="Link to this definition">¶</a></dt>
  2845. <dd><p>Read up to <em>n</em> bytes from the memory buffer. If <em>n</em> is not specified or
  2846. negative, all bytes are returned.</p>
  2847. </dd></dl>
  2848. <dl class="py method">
  2849. <dt class="sig sig-object py" id="ssl.MemoryBIO.write">
  2850. <span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">buf</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ssl.MemoryBIO.write" title="Link to this definition">¶</a></dt>
  2851. <dd><p>Write the bytes from <em>buf</em> to the memory BIO. The <em>buf</em> argument must be an
  2852. object supporting the buffer protocol.</p>
  2853. <p>The return value is the number of bytes written, which is always equal to
  2854. the length of <em>buf</em>.</p>
  2855. </dd></dl>
  2856. <dl class="py method">
  2857. <dt class="sig sig-object py" id="ssl.MemoryBIO.write_eof">
  2858. <span class="sig-name descname"><span class="pre">write_eof</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ssl.MemoryBIO.write_eof" title="Link to this definition">¶</a></dt>
  2859. <dd><p>Write an EOF marker to the memory BIO. After this method has been called, it
  2860. is illegal to call <a class="reference internal" href="#ssl.MemoryBIO.write" title="ssl.MemoryBIO.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a>. The attribute <a class="reference internal" href="#ssl.MemoryBIO.eof" title="ssl.MemoryBIO.eof"><code class="xref py py-attr docutils literal notranslate"><span class="pre">eof</span></code></a> will
  2861. become true after all data currently in the buffer has been read.</p>
  2862. </dd></dl>
  2863. </dd></dl>
  2864. </section>
  2865. <section id="ssl-session">
  2866. <h2>SSL session<a class="headerlink" href="#ssl-session" title="Link to this heading">¶</a></h2>
  2867. <div class="versionadded">
  2868. <p><span class="versionmodified added">New in version 3.6.</span></p>
  2869. </div>
  2870. <dl class="py class">
  2871. <dt class="sig sig-object py" id="ssl.SSLSession">
  2872. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ssl.</span></span><span class="sig-name descname"><span class="pre">SSLSession</span></span><a class="headerlink" href="#ssl.SSLSession" title="Link to this definition">¶</a></dt>
  2873. <dd><p>Session object used by <a class="reference internal" href="#ssl.SSLSocket.session" title="ssl.SSLSocket.session"><code class="xref py py-attr docutils literal notranslate"><span class="pre">session</span></code></a>.</p>
  2874. <dl class="py attribute">
  2875. <dt class="sig sig-object py" id="ssl.SSLSession.id">
  2876. <span class="sig-name descname"><span class="pre">id</span></span><a class="headerlink" href="#ssl.SSLSession.id" title="Link to this definition">¶</a></dt>
  2877. <dd></dd></dl>
  2878. <dl class="py attribute">
  2879. <dt class="sig sig-object py" id="ssl.SSLSession.time">
  2880. <span class="sig-name descname"><span class="pre">time</span></span><a class="headerlink" href="#ssl.SSLSession.time" title="Link to this definition">¶</a></dt>
  2881. <dd></dd></dl>
  2882. <dl class="py attribute">
  2883. <dt class="sig sig-object py" id="ssl.SSLSession.timeout">
  2884. <span class="sig-name descname"><span class="pre">timeout</span></span><a class="headerlink" href="#ssl.SSLSession.timeout" title="Link to this definition">¶</a></dt>
  2885. <dd></dd></dl>
  2886. <dl class="py attribute">
  2887. <dt class="sig sig-object py" id="ssl.SSLSession.ticket_lifetime_hint">
  2888. <span class="sig-name descname"><span class="pre">ticket_lifetime_hint</span></span><a class="headerlink" href="#ssl.SSLSession.ticket_lifetime_hint" title="Link to this definition">¶</a></dt>
  2889. <dd></dd></dl>
  2890. <dl class="py attribute">
  2891. <dt class="sig sig-object py" id="ssl.SSLSession.has_ticket">
  2892. <span class="sig-name descname"><span class="pre">has_ticket</span></span><a class="headerlink" href="#ssl.SSLSession.has_ticket" title="Link to this definition">¶</a></dt>
  2893. <dd></dd></dl>
  2894. </dd></dl>
  2895. </section>
  2896. <section id="security-considerations">
  2897. <span id="ssl-security"></span><h2>Security considerations<a class="headerlink" href="#security-considerations" title="Link to this heading">¶</a></h2>
  2898. <section id="best-defaults">
  2899. <h3>Best defaults<a class="headerlink" href="#best-defaults" title="Link to this heading">¶</a></h3>
  2900. <p>For <strong>client use</strong>, if you don’t have any special requirements for your
  2901. security policy, it is highly recommended that you use the
  2902. <a class="reference internal" href="#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_default_context()</span></code></a> function to create your SSL context.
  2903. It will load the system’s trusted CA certificates, enable certificate
  2904. validation and hostname checking, and try to choose reasonably secure
  2905. protocol and cipher settings.</p>
  2906. <p>For example, here is how you would use the <a class="reference internal" href="smtplib.html#smtplib.SMTP" title="smtplib.SMTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">smtplib.SMTP</span></code></a> class to
  2907. create a trusted, secure connection to a SMTP server:</p>
  2908. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">ssl</span><span class="o">,</span> <span class="nn">smtplib</span>
  2909. <span class="gp">&gt;&gt;&gt; </span><span class="n">smtp</span> <span class="o">=</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="s2">&quot;mail.python.org&quot;</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mi">587</span><span class="p">)</span>
  2910. <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>
  2911. <span class="gp">&gt;&gt;&gt; </span><span class="n">smtp</span><span class="o">.</span><span class="n">starttls</span><span class="p">(</span><span class="n">context</span><span class="o">=</span><span class="n">context</span><span class="p">)</span>
  2912. <span class="go">(220, b&#39;2.0.0 Ready to start TLS&#39;)</span>
  2913. </pre></div>
  2914. </div>
  2915. <p>If a client certificate is needed for the connection, it can be added with
  2916. <a class="reference internal" href="#ssl.SSLContext.load_cert_chain" title="ssl.SSLContext.load_cert_chain"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.load_cert_chain()</span></code></a>.</p>
  2917. <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 notranslate"><span class="pre">SSLContext</span></code></a>
  2918. constructor yourself, it will not have certificate validation nor hostname
  2919. checking enabled by default. If you do so, please read the paragraphs below
  2920. to achieve a good security level.</p>
  2921. </section>
  2922. <section id="manual-settings">
  2923. <h3>Manual settings<a class="headerlink" href="#manual-settings" title="Link to this heading">¶</a></h3>
  2924. <section id="verifying-certificates">
  2925. <h4>Verifying certificates<a class="headerlink" href="#verifying-certificates" title="Link to this heading">¶</a></h4>
  2926. <p>When calling the <a class="reference internal" href="#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> constructor directly,
  2927. <a class="reference internal" href="#ssl.CERT_NONE" title="ssl.CERT_NONE"><code class="xref py py-const docutils literal notranslate"><span class="pre">CERT_NONE</span></code></a> is the default. Since it does not authenticate the other
  2928. peer, it can be insecure, especially in client mode where most of time you
  2929. would like to ensure the authenticity of the server you’re talking to.
  2930. Therefore, when in client mode, it is highly recommended to use
  2931. <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-const docutils literal notranslate"><span class="pre">CERT_REQUIRED</span></code></a>. However, it is in itself not sufficient; you also
  2932. have to check that the server certificate, which can be obtained by calling
  2933. <a class="reference internal" href="#ssl.SSLSocket.getpeercert" title="ssl.SSLSocket.getpeercert"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.getpeercert()</span></code></a>, matches the desired service. For many
  2934. protocols and applications, the service can be identified by the hostname.
  2935. This common check is automatically performed when
  2936. <a class="reference internal" href="#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.check_hostname</span></code></a> is enabled.</p>
  2937. <div class="versionchanged">
  2938. <p><span class="versionmodified changed">Changed in version 3.7: </span>Hostname matchings is now performed by OpenSSL. Python no longer uses
  2939. <code class="xref py py-func docutils literal notranslate"><span class="pre">match_hostname()</span></code>.</p>
  2940. </div>
  2941. <p>In server mode, if you want to authenticate your clients using the SSL layer
  2942. (rather than using a higher-level authentication mechanism), you’ll also have
  2943. to specify <a class="reference internal" href="#ssl.CERT_REQUIRED" title="ssl.CERT_REQUIRED"><code class="xref py py-const docutils literal notranslate"><span class="pre">CERT_REQUIRED</span></code></a> and similarly check the client certificate.</p>
  2944. </section>
  2945. <section id="protocol-versions">
  2946. <h4>Protocol versions<a class="headerlink" href="#protocol-versions" title="Link to this heading">¶</a></h4>
  2947. <p>SSL versions 2 and 3 are considered insecure and are therefore dangerous to
  2948. use. If you want maximum compatibility between clients and servers, it is
  2949. recommended to use <a class="reference internal" href="#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a> or
  2950. <a class="reference internal" href="#ssl.PROTOCOL_TLS_SERVER" title="ssl.PROTOCOL_TLS_SERVER"><code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS_SERVER</span></code></a> as the protocol version. SSLv2 and SSLv3 are
  2951. disabled by default.</p>
  2952. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">client_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_TLS_CLIENT</span><span class="p">)</span>
  2953. <span class="gp">&gt;&gt;&gt; </span><span class="n">client_context</span><span class="o">.</span><span class="n">minimum_version</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">TLSVersion</span><span class="o">.</span><span class="n">TLSv1_3</span>
  2954. <span class="gp">&gt;&gt;&gt; </span><span class="n">client_context</span><span class="o">.</span><span class="n">maximum_version</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">TLSVersion</span><span class="o">.</span><span class="n">TLSv1_3</span>
  2955. </pre></div>
  2956. </div>
  2957. <p>The SSL context created above will only allow TLSv1.3 and later (if
  2958. supported by your system) connections to a server. <a class="reference internal" href="#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a>
  2959. implies certificate validation and hostname checks by default. You have to
  2960. load certificates into the context.</p>
  2961. </section>
  2962. <section id="cipher-selection">
  2963. <h4>Cipher selection<a class="headerlink" href="#cipher-selection" title="Link to this heading">¶</a></h4>
  2964. <p>If you have advanced security requirements, fine-tuning of the ciphers
  2965. enabled when negotiating a SSL session is possible through the
  2966. <a class="reference internal" href="#ssl.SSLContext.set_ciphers" title="ssl.SSLContext.set_ciphers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.set_ciphers()</span></code></a> method. Starting from Python 3.2.3, the
  2967. ssl module disables certain weak ciphers by default, but you may want
  2968. to further restrict the cipher choice. Be sure to read OpenSSL’s documentation
  2969. about the <a class="reference external" href="https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT">cipher list format</a>.
  2970. If you want to check which ciphers are enabled by a given cipher list, use
  2971. <a class="reference internal" href="#ssl.SSLContext.get_ciphers" title="ssl.SSLContext.get_ciphers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.get_ciphers()</span></code></a> or the <code class="docutils literal notranslate"><span class="pre">openssl</span> <span class="pre">ciphers</span></code> command on your
  2972. system.</p>
  2973. </section>
  2974. </section>
  2975. <section id="multi-processing">
  2976. <h3>Multi-processing<a class="headerlink" href="#multi-processing" title="Link to this heading">¶</a></h3>
  2977. <p>If using this module as part of a multi-processed application (using,
  2978. for example the <a class="reference internal" href="multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> or <a class="reference internal" href="concurrent.futures.html#module-concurrent.futures" title="concurrent.futures: Execute computations concurrently using threads or processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> modules),
  2979. be aware that OpenSSL’s internal random number generator does not properly
  2980. handle forked processes. Applications must change the PRNG state of the
  2981. 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 notranslate"><span class="pre">os.fork()</span></code></a>. Any
  2982. successful call of <a class="reference internal" href="#ssl.RAND_add" title="ssl.RAND_add"><code class="xref py py-func docutils literal notranslate"><span class="pre">RAND_add()</span></code></a> or <a class="reference internal" href="#ssl.RAND_bytes" title="ssl.RAND_bytes"><code class="xref py py-func docutils literal notranslate"><span class="pre">RAND_bytes()</span></code></a> is
  2983. sufficient.</p>
  2984. </section>
  2985. </section>
  2986. <section id="tls-1-3">
  2987. <span id="ssl-tlsv1-3"></span><h2>TLS 1.3<a class="headerlink" href="#tls-1-3" title="Link to this heading">¶</a></h2>
  2988. <div class="versionadded">
  2989. <p><span class="versionmodified added">New in version 3.7.</span></p>
  2990. </div>
  2991. <p>The TLS 1.3 protocol behaves slightly differently than previous version
  2992. of TLS/SSL. Some new TLS 1.3 features are not yet available.</p>
  2993. <ul class="simple">
  2994. <li><p>TLS 1.3 uses a disjunct set of cipher suites. All AES-GCM and
  2995. ChaCha20 cipher suites are enabled by default. The method
  2996. <a class="reference internal" href="#ssl.SSLContext.set_ciphers" title="ssl.SSLContext.set_ciphers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.set_ciphers()</span></code></a> cannot enable or disable any TLS 1.3
  2997. ciphers yet, but <a class="reference internal" href="#ssl.SSLContext.get_ciphers" title="ssl.SSLContext.get_ciphers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.get_ciphers()</span></code></a> returns them.</p></li>
  2998. <li><p>Session tickets are no longer sent as part of the initial handshake and
  2999. are handled differently. <a class="reference internal" href="#ssl.SSLSocket.session" title="ssl.SSLSocket.session"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLSocket.session</span></code></a> and <a class="reference internal" href="#ssl.SSLSession" title="ssl.SSLSession"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSession</span></code></a>
  3000. are not compatible with TLS 1.3.</p></li>
  3001. <li><p>Client-side certificates are also no longer verified during the initial
  3002. handshake. A server can request a certificate at any time. Clients
  3003. process certificate requests while they send or receive application data
  3004. from the server.</p></li>
  3005. <li><p>TLS 1.3 features like early data, deferred TLS client cert request,
  3006. signature algorithm configuration, and rekeying are not supported yet.</p></li>
  3007. </ul>
  3008. <div class="admonition seealso">
  3009. <p class="admonition-title">See also</p>
  3010. <dl class="simple">
  3011. <dt>Class <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal notranslate"><span class="pre">socket.socket</span></code></a></dt><dd><p>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 notranslate"><span class="pre">socket</span></code></a> class</p>
  3012. </dd>
  3013. <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><dd><p>Intro from the Apache HTTP Server documentation</p>
  3014. </dd>
  3015. <dt><span class="target" id="index-17"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc1422.html"><strong>RFC 1422: Privacy Enhancement for Internet Electronic Mail: Part II: Certificate-Based Key Management</strong></a></dt><dd><p>Steve Kent</p>
  3016. </dd>
  3017. <dt><span class="target" id="index-18"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4086.html"><strong>RFC 4086: Randomness Requirements for Security</strong></a></dt><dd><p>Donald E., Jeffrey I. Schiller</p>
  3018. </dd>
  3019. <dt><span class="target" id="index-19"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5280.html"><strong>RFC 5280: Internet X.509 Public Key Infrastructure Certificate and Certificate Revocation List (CRL) Profile</strong></a></dt><dd><p>D. Cooper</p>
  3020. </dd>
  3021. <dt><span class="target" id="index-20"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5246.html"><strong>RFC 5246: The Transport Layer Security (TLS) Protocol Version 1.2</strong></a></dt><dd><p>T. Dierks et. al.</p>
  3022. </dd>
  3023. <dt><span class="target" id="index-21"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc6066.html"><strong>RFC 6066: Transport Layer Security (TLS) Extensions</strong></a></dt><dd><p>D. Eastlake</p>
  3024. </dd>
  3025. <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><dd><p>IANA</p>
  3026. </dd>
  3027. <dt><span class="target" id="index-22"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc7525.html"><strong>RFC 7525: Recommendations for Secure Use of Transport Layer Security (TLS) and Datagram Transport Layer Security (DTLS)</strong></a></dt><dd><p>IETF</p>
  3028. </dd>
  3029. <dt><a class="reference external" href="https://wiki.mozilla.org/Security/Server_Side_TLS">Mozilla’s Server Side TLS recommendations</a></dt><dd><p>Mozilla</p>
  3030. </dd>
  3031. </dl>
  3032. </div>
  3033. </section>
  3034. </section>
  3035. <div class="clearer"></div>
  3036. </div>
  3037. </div>
  3038. </div>
  3039. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  3040. <div class="sphinxsidebarwrapper">
  3041. <div>
  3042. <h3><a href="../contents.html">Table of Contents</a></h3>
  3043. <ul>
  3044. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code> — TLS/SSL wrapper for socket objects</a><ul>
  3045. <li><a class="reference internal" href="#functions-constants-and-exceptions">Functions, Constants, and Exceptions</a><ul>
  3046. <li><a class="reference internal" href="#socket-creation">Socket creation</a></li>
  3047. <li><a class="reference internal" href="#context-creation">Context creation</a></li>
  3048. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  3049. <li><a class="reference internal" href="#random-generation">Random generation</a></li>
  3050. <li><a class="reference internal" href="#certificate-handling">Certificate handling</a></li>
  3051. <li><a class="reference internal" href="#constants">Constants</a></li>
  3052. </ul>
  3053. </li>
  3054. <li><a class="reference internal" href="#ssl-sockets">SSL Sockets</a></li>
  3055. <li><a class="reference internal" href="#ssl-contexts">SSL Contexts</a></li>
  3056. <li><a class="reference internal" href="#certificates">Certificates</a><ul>
  3057. <li><a class="reference internal" href="#certificate-chains">Certificate chains</a></li>
  3058. <li><a class="reference internal" href="#ca-certificates">CA certificates</a></li>
  3059. <li><a class="reference internal" href="#combined-key-and-certificate">Combined key and certificate</a></li>
  3060. <li><a class="reference internal" href="#self-signed-certificates">Self-signed certificates</a></li>
  3061. </ul>
  3062. </li>
  3063. <li><a class="reference internal" href="#examples">Examples</a><ul>
  3064. <li><a class="reference internal" href="#testing-for-ssl-support">Testing for SSL support</a></li>
  3065. <li><a class="reference internal" href="#client-side-operation">Client-side operation</a></li>
  3066. <li><a class="reference internal" href="#server-side-operation">Server-side operation</a></li>
  3067. </ul>
  3068. </li>
  3069. <li><a class="reference internal" href="#notes-on-non-blocking-sockets">Notes on non-blocking sockets</a></li>
  3070. <li><a class="reference internal" href="#memory-bio-support">Memory BIO Support</a></li>
  3071. <li><a class="reference internal" href="#ssl-session">SSL session</a></li>
  3072. <li><a class="reference internal" href="#security-considerations">Security considerations</a><ul>
  3073. <li><a class="reference internal" href="#best-defaults">Best defaults</a></li>
  3074. <li><a class="reference internal" href="#manual-settings">Manual settings</a><ul>
  3075. <li><a class="reference internal" href="#verifying-certificates">Verifying certificates</a></li>
  3076. <li><a class="reference internal" href="#protocol-versions">Protocol versions</a></li>
  3077. <li><a class="reference internal" href="#cipher-selection">Cipher selection</a></li>
  3078. </ul>
  3079. </li>
  3080. <li><a class="reference internal" href="#multi-processing">Multi-processing</a></li>
  3081. </ul>
  3082. </li>
  3083. <li><a class="reference internal" href="#tls-1-3">TLS 1.3</a></li>
  3084. </ul>
  3085. </li>
  3086. </ul>
  3087. </div>
  3088. <div>
  3089. <h4>Previous topic</h4>
  3090. <p class="topless"><a href="socket.html"
  3091. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code> — Low-level networking interface</a></p>
  3092. </div>
  3093. <div>
  3094. <h4>Next topic</h4>
  3095. <p class="topless"><a href="select.html"
  3096. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">select</span></code> — Waiting for I/O completion</a></p>
  3097. </div>
  3098. <div role="note" aria-label="source link">
  3099. <h3>This Page</h3>
  3100. <ul class="this-page-menu">
  3101. <li><a href="../bugs.html">Report a Bug</a></li>
  3102. <li>
  3103. <a href="https://github.com/python/cpython/blob/main/Doc/library/ssl.rst"
  3104. rel="nofollow">Show Source
  3105. </a>
  3106. </li>
  3107. </ul>
  3108. </div>
  3109. </div>
  3110. <div id="sidebarbutton" title="Collapse sidebar">
  3111. <span>«</span>
  3112. </div>
  3113. </div>
  3114. <div class="clearer"></div>
  3115. </div>
  3116. <div class="related" role="navigation" aria-label="related navigation">
  3117. <h3>Navigation</h3>
  3118. <ul>
  3119. <li class="right" style="margin-right: 10px">
  3120. <a href="../genindex.html" title="General Index"
  3121. >index</a></li>
  3122. <li class="right" >
  3123. <a href="../py-modindex.html" title="Python Module Index"
  3124. >modules</a> |</li>
  3125. <li class="right" >
  3126. <a href="select.html" title="select — Waiting for I/O completion"
  3127. >next</a> |</li>
  3128. <li class="right" >
  3129. <a href="socket.html" title="socket — Low-level networking interface"
  3130. >previous</a> |</li>
  3131. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  3132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  3133. <li class="switchers">
  3134. <div class="language_switcher_placeholder"></div>
  3135. <div class="version_switcher_placeholder"></div>
  3136. </li>
  3137. <li>
  3138. </li>
  3139. <li id="cpython-language-and-version">
  3140. <a href="../index.html">3.12.3 Documentation</a> &#187;
  3141. </li>
  3142. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  3143. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  3144. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code> — TLS/SSL wrapper for socket objects</a></li>
  3145. <li class="right">
  3146. <div class="inline-search" role="search">
  3147. <form class="inline-search" action="../search.html" method="get">
  3148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  3149. <input type="submit" value="Go" />
  3150. </form>
  3151. </div>
  3152. |
  3153. </li>
  3154. <li class="right">
  3155. <label class="theme-selector-label">
  3156. Theme
  3157. <select class="theme-selector" oninput="activateTheme(this.value)">
  3158. <option value="auto" selected>Auto</option>
  3159. <option value="light">Light</option>
  3160. <option value="dark">Dark</option>
  3161. </select>
  3162. </label> |</li>
  3163. </ul>
  3164. </div>
  3165. <div class="footer">
  3166. &copy;
  3167. <a href="../copyright.html">
  3168. Copyright
  3169. </a>
  3170. 2001-2024, Python Software Foundation.
  3171. <br />
  3172. This page is licensed under the Python Software Foundation License Version 2.
  3173. <br />
  3174. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  3175. <br />
  3176. See <a href="/license.html">History and License</a> for more information.<br />
  3177. <br />
  3178. The Python Software Foundation is a non-profit corporation.
  3179. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  3180. <br />
  3181. <br />
  3182. Last updated on Apr 09, 2024 (13:47 UTC).
  3183. <a href="/bugs.html">Found a bug</a>?
  3184. <br />
  3185. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  3186. </div>
  3187. </body>
  3188. </html>
上海开阖软件有限公司 沪ICP备12045867号-1