blob: ead145202f35d328f05bd3a94f0623f6b9ee0a15 [file] [log] [blame]
Jonathan Ballet6381da32011-07-20 16:43:38 +09001.. _openssl-ssl:
2
3:py:mod:`SSL` --- An interface to the SSL-specific parts of OpenSSL
4===================================================================
5
Jonathan Balletc9e066c2011-07-17 22:56:05 +09006.. py:module:: OpenSSL.SSL
Jonathan Ballet6381da32011-07-20 16:43:38 +09007 :synopsis: An interface to the SSL-specific parts of OpenSSL
Jonathan Balletc9e066c2011-07-17 22:56:05 +09008
Jonathan Balletc9e066c2011-07-17 22:56:05 +09009
10This module handles things specific to SSL. There are two objects defined:
11Context, Connection.
12
13.. py:data:: SSLv2_METHOD
Jonathan Ballet6381da32011-07-20 16:43:38 +090014 SSLv3_METHOD
15 SSLv23_METHOD
16 TLSv1_METHOD
Jean-Paul Calderone1461c492013-10-03 16:05:00 -040017 TLSv1_1_METHOD
18 TLSv1_2_METHOD
Jonathan Balletc9e066c2011-07-17 22:56:05 +090019
20 These constants represent the different SSL methods to use when creating a
Jean-Paul Calderone1461c492013-10-03 16:05:00 -040021 context object. If the underlying OpenSSL build is missing support for any
22 of these protocols, constructing a :py:class:`Context` using the
23 corresponding :py:const:`*_METHOD` will raise an exception.
Jonathan Balletc9e066c2011-07-17 22:56:05 +090024
25
26.. py:data:: VERIFY_NONE
Jonathan Ballet6381da32011-07-20 16:43:38 +090027 VERIFY_PEER
28 VERIFY_FAIL_IF_NO_PEER_CERT
Jonathan Balletc9e066c2011-07-17 22:56:05 +090029
30 These constants represent the verification mode used by the Context
31 object's :py:meth:`set_verify` method.
32
33
34.. py:data:: FILETYPE_PEM
Jonathan Ballet6381da32011-07-20 16:43:38 +090035 FILETYPE_ASN1
Jonathan Balletc9e066c2011-07-17 22:56:05 +090036
37 File type constants used with the :py:meth:`use_certificate_file` and
38 :py:meth:`use_privatekey_file` methods of Context objects.
39
40
41.. py:data:: OP_SINGLE_DH_USE
Akihiro Yamazaki7eee79d2015-09-05 23:50:05 +090042 OP_SINGLE_ECDH_USE
Jean-Paul Calderone1461c492013-10-03 16:05:00 -040043
Akihiro Yamazaki8509cec2015-09-06 02:01:21 +090044 Constants used with :py:meth:`set_options` of Context objects.
Jean-Paul Calderone1461c492013-10-03 16:05:00 -040045
Akihiro Yamazaki8509cec2015-09-06 02:01:21 +090046 When these options are used, a new key will always be created when using
Akihiro Yamazaki7eee79d2015-09-05 23:50:05 +090047 ephemeral (Elliptic curve) Diffie-Hellman.
Jean-Paul Calderone1461c492013-10-03 16:05:00 -040048
49
50.. py:data:: OP_EPHEMERAL_RSA
51
52 Constant used with :py:meth:`set_options` of Context objects.
53
54 When this option is used, ephemeral RSA keys will always be used when doing
55 RSA operations.
56
57
58.. py:data:: OP_NO_TICKET
59
60 Constant used with :py:meth:`set_options` of Context objects.
61
62 When this option is used, the session ticket extension will not be used.
63
64
65.. py:data:: OP_NO_COMPRESSION
66
67 Constant used with :py:meth:`set_options` of Context objects.
68
69 When this option is used, compression will not be used.
70
71
72.. py:data:: OP_NO_SSLv2
Jonathan Ballet6381da32011-07-20 16:43:38 +090073 OP_NO_SSLv3
74 OP_NO_TLSv1
Jean-Paul Calderone1461c492013-10-03 16:05:00 -040075 OP_NO_TLSv1_1
76 OP_NO_TLSv1_2
Nathaniel J. Smitha1813732019-08-01 21:32:13 -070077 OP_NO_TLSv1_3
Jonathan Balletc9e066c2011-07-17 22:56:05 +090078
79 Constants used with :py:meth:`set_options` of Context objects.
80
Jean-Paul Calderone1461c492013-10-03 16:05:00 -040081 Each of these options disables one version of the SSL/TLS protocol. This
82 is interesting if you're using e.g. :py:const:`SSLv23_METHOD` to get an
83 SSLv2-compatible handshake, but don't want to use SSLv2. If the underlying
84 OpenSSL build is missing support for any of these protocols, the
85 :py:const:`OP_NO_*` constant may be undefined.
Jonathan Balletc9e066c2011-07-17 22:56:05 +090086
87
88.. py:data:: SSLEAY_VERSION
Jonathan Ballet6381da32011-07-20 16:43:38 +090089 SSLEAY_CFLAGS
90 SSLEAY_BUILT_ON
91 SSLEAY_PLATFORM
92 SSLEAY_DIR
Jonathan Balletc9e066c2011-07-17 22:56:05 +090093
94 Constants used with :py:meth:`SSLeay_version` to specify what OpenSSL version
95 information to retrieve. See the man page for the :py:func:`SSLeay_version` C
96 API for details.
97
Jean-Paul Calderone1461c492013-10-03 16:05:00 -040098
Jean-Paul Calderone8e8f90c2012-02-08 13:16:26 -050099.. py:data:: SESS_CACHE_OFF
100 SESS_CACHE_CLIENT
101 SESS_CACHE_SERVER
102 SESS_CACHE_BOTH
103 SESS_CACHE_NO_AUTO_CLEAR
104 SESS_CACHE_NO_INTERNAL_LOOKUP
105 SESS_CACHE_NO_INTERNAL_STORE
106 SESS_CACHE_NO_INTERNAL
107
108 Constants used with :py:meth:`Context.set_session_cache_mode` to specify
109 the behavior of the session cache and potential session reuse. See the man
110 page for the :py:func:`SSL_CTX_set_session_cache_mode` C API for details.
111
112 .. versionadded:: 0.14
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900113
Jean-Paul Calderone1461c492013-10-03 16:05:00 -0400114
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900115.. py:data:: OPENSSL_VERSION_NUMBER
116
117 An integer giving the version number of the OpenSSL library used to build this
118 version of pyOpenSSL. See the man page for the :py:func:`SSLeay_version` C API
119 for details.
120
121
Mark Williams5d890a02019-11-17 19:56:26 -0800122.. py:data:: NO_OVERLAPPING_PROTOCOLS
123
124 A sentinel value that can be returned by the callback passed to
125 :py:meth:`Context.set_alpn_select_callback` to indicate that
126 the handshake can continue without a specific application protocol.
127
128 .. versionadded:: 19.1
129
130
Alex Chand072cae2018-02-15 09:57:59 +0000131.. autofunction:: SSLeay_version
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900132
133
134.. py:data:: ContextType
135
136 See :py:class:`Context`.
137
138
Alex Chand072cae2018-02-15 09:57:59 +0000139.. autoclass:: Context
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900140
Alex Chand072cae2018-02-15 09:57:59 +0000141.. autoclass:: Session
Jean-Paul Calderone6c896fe2012-02-16 08:10:04 -0500142
143
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900144.. py:data:: ConnectionType
145
146 See :py:class:`Connection`.
147
148
149.. py:class:: Connection(context, socket)
150
151 A class representing SSL connections.
152
153 *context* should be an instance of :py:class:`Context` and *socket*
154 should be a socket [#connection-context-socket]_ object. *socket* may be
155 *None*; in this case, the Connection is created with a memory BIO: see
156 the :py:meth:`bio_read`, :py:meth:`bio_write`, and :py:meth:`bio_shutdown`
157 methods.
158
159.. py:exception:: Error
160
161 This exception is used as a base class for the other SSL-related
162 exceptions, but may also be raised directly.
163
164 Whenever this exception is raised directly, it has a list of error messages
165 from the OpenSSL error queue, where each item is a tuple *(lib, function,
166 reason)*. Here *lib*, *function* and *reason* are all strings, describing
167 where and what the problem is. See :manpage:`err(3)` for more information.
168
169
170.. py:exception:: ZeroReturnError
171
172 This exception matches the error return code
173 :py:data:`SSL_ERROR_ZERO_RETURN`, and is raised when the SSL Connection has
174 been closed. In SSL 3.0 and TLS 1.0, this only occurs if a closure alert has
175 occurred in the protocol, i.e. the connection has been closed cleanly. Note
176 that this does not necessarily mean that the transport layer (e.g. a socket)
177 has been closed.
178
179 It may seem a little strange that this is an exception, but it does match an
180 :py:data:`SSL_ERROR` code, and is very convenient.
181
182
183.. py:exception:: WantReadError
184
185 The operation did not complete; the same I/O method should be called again
186 later, with the same arguments. Any I/O method can lead to this since new
187 handshakes can occur at any time.
188
189 The wanted read is for **dirty** data sent over the network, not the
190 **clean** data inside the tunnel. For a socket based SSL connection,
191 **read** means data coming at us over the network. Until that read
192 succeeds, the attempted :py:meth:`OpenSSL.SSL.Connection.recv`,
193 :py:meth:`OpenSSL.SSL.Connection.send`, or
194 :py:meth:`OpenSSL.SSL.Connection.do_handshake` is prevented or incomplete. You
195 probably want to :py:meth:`select()` on the socket before trying again.
196
197
198.. py:exception:: WantWriteError
199
200 See :py:exc:`WantReadError`. The socket send buffer may be too full to
201 write more data.
202
203
204.. py:exception:: WantX509LookupError
205
206 The operation did not complete because an application callback has asked to be
207 called again. The I/O method should be called again later, with the same
208 arguments.
209
210 .. note:: This won't occur in this version, as there are no such
211 callbacks in this version.
212
213
214.. py:exception:: SysCallError
215
216 The :py:exc:`SysCallError` occurs when there's an I/O error and OpenSSL's
217 error queue does not contain any information. This can mean two things: An
218 error in the transport protocol, or an end of file that violates the protocol.
219 The parameter to the exception is always a pair *(errnum,
220 errstr)*.
221
222
223
224.. _openssl-context:
225
226Context objects
227---------------
228
229Context objects have the following methods:
230
Alex Chand072cae2018-02-15 09:57:59 +0000231.. autoclass:: OpenSSL.SSL.Context
232 :members:
Cory Benfieldbe3e7b82014-05-10 09:48:55 +0100233
Jean-Paul Calderone6c896fe2012-02-16 08:10:04 -0500234.. _openssl-session:
235
236Session objects
237---------------
238
239Session objects have no methods.
240
241
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900242.. _openssl-connection:
243
244Connection objects
245------------------
246
247Connection objects have the following methods:
248
Alex Chand072cae2018-02-15 09:57:59 +0000249.. autoclass:: OpenSSL.SSL.Connection
250 :members:
Cory Benfield12eae892014-06-07 15:42:56 +0100251
Cory Benfieldbe3e7b82014-05-10 09:48:55 +0100252
Jonathan Ballet6381da32011-07-20 16:43:38 +0900253.. Rubric:: Footnotes
254
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900255.. [#connection-context-socket] Actually, all that is required is an object that
256 **behaves** like a socket, you could even use files, even though it'd be
257 tricky to get the handshakes right!