Debian Codegraph @

doc/api/ssl.rst

raw
1 .. _openssl-ssl:
2
3 :py:mod:`SSL` --- An interface to the SSL-specific parts of OpenSSL
4 ===================================================================
5
6 .. py:module:: OpenSSL.SSL
7 :synopsis: An interface to the SSL-specific parts of OpenSSL
8
9
10 This module handles things specific to SSL. There are two objects defined:
11 Context, Connection.
12
13 .. py:data:: TLS_METHOD
14 TLS_SERVER_METHOD
15 TLS_CLIENT_METHOD
16 SSLv2_METHOD
17 SSLv3_METHOD
18 SSLv23_METHOD
19 TLSv1_METHOD
20 TLSv1_1_METHOD
21 TLSv1_2_METHOD
22
23 These constants represent the different SSL methods to use when creating a
24 context object. New code should only use ``TLS_METHOD``, ``TLS_SERVER_METHOD``,
25 or ``TLS_CLIENT_METHOD``. If the underlying OpenSSL build is missing support
26 for any of these protocols, constructing a :py:class:`Context` using the
27 corresponding :py:const:`*_METHOD` will raise an exception.
28
29
30 .. py:data:: SSL3_VERSION
31 TLS1_VERSION
32 TLS1_1_VERSION
33 TLS1_2_VERSION
34 TLS1_3_VERSION
35
36 These constants represent the different TLS versions to use when
37 setting the minimum or maximum TLS version.
38
39 .. py:data:: VERIFY_NONE
40 VERIFY_PEER
41 VERIFY_FAIL_IF_NO_PEER_CERT
42
43 These constants represent the verification mode used by the Context
44 object's :py:meth:`set_verify` method.
45
46
47 .. py:data:: FILETYPE_PEM
48 FILETYPE_ASN1
49
50 File type constants used with the :py:meth:`use_certificate_file` and
51 :py:meth:`use_privatekey_file` methods of Context objects.
52
53
54 .. py:data:: OP_SINGLE_DH_USE
55 OP_SINGLE_ECDH_USE
56
57 Constants used with :py:meth:`set_options` of Context objects.
58
59 When these options are used, a new key will always be created when using
60 ephemeral (Elliptic curve) Diffie-Hellman.
61
62
63 .. py:data:: OP_EPHEMERAL_RSA
64
65 Constant used with :py:meth:`set_options` of Context objects.
66
67 When this option is used, ephemeral RSA keys will always be used when doing
68 RSA operations.
69
70
71 .. py:data:: OP_NO_TICKET
72
73 Constant used with :py:meth:`set_options` of Context objects.
74
75 When this option is used, the session ticket extension will not be used.
76
77
78 .. py:data:: OP_NO_COMPRESSION
79
80 Constant used with :py:meth:`set_options` of Context objects.
81
82 When this option is used, compression will not be used.
83
84
85 .. py:data:: OP_NO_SSLv2
86 OP_NO_SSLv3
87 OP_NO_TLSv1
88 OP_NO_TLSv1_1
89 OP_NO_TLSv1_2
90 OP_NO_TLSv1_3
91
92 Constants used with :py:meth:`set_options` of Context objects.
93
94 Each of these options disables one version of the SSL/TLS protocol. This
95 is interesting if you're using e.g. :py:const:`SSLv23_METHOD` to get an
96 SSLv2-compatible handshake, but don't want to use SSLv2. If the underlying
97 OpenSSL build is missing support for any of these protocols, the
98 :py:const:`OP_NO_*` constant may be undefined.
99
100
101 .. py:data:: OPENSSL_VERSION
102 OPENSSL_CFLAGS
103 OPENSSL_BUILT_ON
104 OPENSSL_PLATFORM
105 OPENSSL_DIR
106
107 .. versionchanged:: 22.1.0
108
109 Previously these were all named ``SSLEAY_*``. Those names are still
110 available for backwards compatibility, but the ``OPENSSL_*`` names are
111 preferred.
112
113 Constants used with :py:meth:`OpenSSL_version` to specify what OpenSSL version
114 information to retrieve. See the man page for the :py:func:`OpenSSL_version` C
115 API for details.
116
117
118 .. py:data:: SESS_CACHE_OFF
119 SESS_CACHE_CLIENT
120 SESS_CACHE_SERVER
121 SESS_CACHE_BOTH
122 SESS_CACHE_NO_AUTO_CLEAR
123 SESS_CACHE_NO_INTERNAL_LOOKUP
124 SESS_CACHE_NO_INTERNAL_STORE
125 SESS_CACHE_NO_INTERNAL
126
127 Constants used with :py:meth:`Context.set_session_cache_mode` to specify
128 the behavior of the session cache and potential session reuse. See the man
129 page for the :py:func:`SSL_CTX_set_session_cache_mode` C API for details.
130
131 .. versionadded:: 0.14
132
133
134 .. py:data:: OPENSSL_VERSION_NUMBER
135
136 An integer giving the version number of the OpenSSL library used to build this
137 version of pyOpenSSL. See the man page for the :py:func:`SSLeay_version` C API
138 for details.
139
140
141 .. py:data:: NO_OVERLAPPING_PROTOCOLS
142
143 A sentinel value that can be returned by the callback passed to
144 :py:meth:`Context.set_alpn_select_callback` to indicate that
145 the handshake can continue without a specific application protocol.
146
147 .. versionadded:: 19.1
148
149
150 .. autofunction:: OpenSSL_version
151
152
153 .. autoclass:: Context
154 :noindex:
155
156 .. autoclass:: Session
157
158
159 .. py:class:: Connection(context, socket)
160 :noindex:
161
162 A class representing SSL connections.
163
164 *context* should be an instance of :py:class:`Context` and *socket*
165 should be a socket [#connection-context-socket]_ object. *socket* may be
166 *None*; in this case, the Connection is created with a memory BIO: see
167 the :py:meth:`bio_read`, :py:meth:`bio_write`, and :py:meth:`bio_shutdown`
168 methods.
169
170 .. py:exception:: Error
171
172 This exception is used as a base class for the other SSL-related
173 exceptions, but may also be raised directly.
174
175 Whenever this exception is raised directly, it has a list of error messages
176 from the OpenSSL error queue, where each item is a tuple *(lib, function,
177 reason)*. Here *lib*, *function* and *reason* are all strings, describing
178 where and what the problem is. See :manpage:`err(3)` for more information.
179
180
181 .. py:exception:: ZeroReturnError
182
183 This exception matches the error return code
184 :py:data:`SSL_ERROR_ZERO_RETURN`, and is raised when the SSL Connection has
185 been closed. In SSL 3.0 and TLS 1.0, this only occurs if a closure alert has
186 occurred in the protocol, i.e. the connection has been closed cleanly. Note
187 that this does not necessarily mean that the transport layer (e.g. a socket)
188 has been closed.
189
190 It may seem a little strange that this is an exception, but it does match an
191 :py:data:`SSL_ERROR` code, and is very convenient.
192
193
194 .. py:exception:: WantReadError
195
196 The operation did not complete; the same I/O method should be called again
197 later, with the same arguments. Any I/O method can lead to this since new
198 handshakes can occur at any time.
199
200 The wanted read is for **dirty** data sent over the network, not the
201 **clean** data inside the tunnel. For a socket based SSL connection,
202 **read** means data coming at us over the network. Until that read
203 succeeds, the attempted :py:meth:`OpenSSL.SSL.Connection.recv`,
204 :py:meth:`OpenSSL.SSL.Connection.send`, or
205 :py:meth:`OpenSSL.SSL.Connection.do_handshake` is prevented or incomplete. You
206 probably want to :py:meth:`select()` on the socket before trying again.
207
208
209 .. py:exception:: WantWriteError
210
211 See :py:exc:`WantReadError`. The socket send buffer may be too full to
212 write more data.
213
214
215 .. py:exception:: WantX509LookupError
216
217 The operation did not complete because an application callback has asked to be
218 called again. The I/O method should be called again later, with the same
219 arguments.
220
221 .. note:: This won't occur in this version, as there are no such
222 callbacks in this version.
223
224
225 .. py:exception:: SysCallError
226
227 The :py:exc:`SysCallError` occurs when there's an I/O error and OpenSSL's
228 error queue does not contain any information. This can mean two things: An
229 error in the transport protocol, or an end of file that violates the protocol.
230 The parameter to the exception is always a pair *(errnum,
231 errstr)*.
232
233
234
235 .. _openssl-context:
236
237 Context objects
238 ---------------
239
240 Context objects have the following methods:
241
242 .. autoclass:: OpenSSL.SSL.Context
243 :members:
244
245 .. _openssl-session:
246
247 Session objects
248 ---------------
249
250 Session objects have no methods.
251
252
253 .. _openssl-connection:
254
255 Connection objects
256 ------------------
257
258 Connection objects have the following methods:
259
260 .. autoclass:: OpenSSL.SSL.Connection
261 :members:
262
263
264 .. Rubric:: Footnotes
265
266 .. [#connection-context-socket] Actually, all that is required is an object that
267 **behaves** like a socket, you could even use files, even though it'd be
268 tricky to get the handshakes right!