blob: d892dbb05428068ad50dc0922428b2e86a4b35de [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
Jonathan Balletc9e066c2011-07-17 22:56:05 +090077
78 Constants used with :py:meth:`set_options` of Context objects.
79
Jean-Paul Calderone1461c492013-10-03 16:05:00 -040080 Each of these options disables one version of the SSL/TLS protocol. This
81 is interesting if you're using e.g. :py:const:`SSLv23_METHOD` to get an
82 SSLv2-compatible handshake, but don't want to use SSLv2. If the underlying
83 OpenSSL build is missing support for any of these protocols, the
84 :py:const:`OP_NO_*` constant may be undefined.
Jonathan Balletc9e066c2011-07-17 22:56:05 +090085
86
87.. py:data:: SSLEAY_VERSION
Jonathan Ballet6381da32011-07-20 16:43:38 +090088 SSLEAY_CFLAGS
89 SSLEAY_BUILT_ON
90 SSLEAY_PLATFORM
91 SSLEAY_DIR
Jonathan Balletc9e066c2011-07-17 22:56:05 +090092
93 Constants used with :py:meth:`SSLeay_version` to specify what OpenSSL version
94 information to retrieve. See the man page for the :py:func:`SSLeay_version` C
95 API for details.
96
Jean-Paul Calderone1461c492013-10-03 16:05:00 -040097
Jean-Paul Calderone8e8f90c2012-02-08 13:16:26 -050098.. py:data:: SESS_CACHE_OFF
99 SESS_CACHE_CLIENT
100 SESS_CACHE_SERVER
101 SESS_CACHE_BOTH
102 SESS_CACHE_NO_AUTO_CLEAR
103 SESS_CACHE_NO_INTERNAL_LOOKUP
104 SESS_CACHE_NO_INTERNAL_STORE
105 SESS_CACHE_NO_INTERNAL
106
107 Constants used with :py:meth:`Context.set_session_cache_mode` to specify
108 the behavior of the session cache and potential session reuse. See the man
109 page for the :py:func:`SSL_CTX_set_session_cache_mode` C API for details.
110
111 .. versionadded:: 0.14
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900112
Jean-Paul Calderone1461c492013-10-03 16:05:00 -0400113
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900114.. py:data:: OPENSSL_VERSION_NUMBER
115
116 An integer giving the version number of the OpenSSL library used to build this
117 version of pyOpenSSL. See the man page for the :py:func:`SSLeay_version` C API
118 for details.
119
120
121.. py:function:: SSLeay_version(type)
122
123 Retrieve a string describing some aspect of the underlying OpenSSL version. The
124 type passed in should be one of the :py:const:`SSLEAY_*` constants defined in
125 this module.
126
127
128.. py:data:: ContextType
129
130 See :py:class:`Context`.
131
132
133.. py:class:: Context(method)
134
135 A class representing SSL contexts. Contexts define the parameters of one or
136 more SSL connections.
137
138 *method* should be :py:const:`SSLv2_METHOD`, :py:const:`SSLv3_METHOD`,
Jean-Paul Calderone1461c492013-10-03 16:05:00 -0400139 :py:const:`SSLv23_METHOD`, :py:const:`TLSv1_METHOD`, :py:const:`TLSv1_1_METHOD`,
140 or :py:const:`TLSv1_2_METHOD`.
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900141
142
Jean-Paul Calderone6c896fe2012-02-16 08:10:04 -0500143.. py:class:: Session()
144
145 A class representing an SSL session. A session defines certain connection
146 parameters which may be re-used to speed up the setup of subsequent
147 connections.
148
149 .. versionadded:: 0.14
150
151
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900152.. py:data:: ConnectionType
153
154 See :py:class:`Connection`.
155
156
157.. py:class:: Connection(context, socket)
158
159 A class representing SSL connections.
160
161 *context* should be an instance of :py:class:`Context` and *socket*
162 should be a socket [#connection-context-socket]_ object. *socket* may be
163 *None*; in this case, the Connection is created with a memory BIO: see
164 the :py:meth:`bio_read`, :py:meth:`bio_write`, and :py:meth:`bio_shutdown`
165 methods.
166
167.. py:exception:: Error
168
169 This exception is used as a base class for the other SSL-related
170 exceptions, but may also be raised directly.
171
172 Whenever this exception is raised directly, it has a list of error messages
173 from the OpenSSL error queue, where each item is a tuple *(lib, function,
174 reason)*. Here *lib*, *function* and *reason* are all strings, describing
175 where and what the problem is. See :manpage:`err(3)` for more information.
176
177
178.. py:exception:: ZeroReturnError
179
180 This exception matches the error return code
181 :py:data:`SSL_ERROR_ZERO_RETURN`, and is raised when the SSL Connection has
182 been closed. In SSL 3.0 and TLS 1.0, this only occurs if a closure alert has
183 occurred in the protocol, i.e. the connection has been closed cleanly. Note
184 that this does not necessarily mean that the transport layer (e.g. a socket)
185 has been closed.
186
187 It may seem a little strange that this is an exception, but it does match an
188 :py:data:`SSL_ERROR` code, and is very convenient.
189
190
191.. py:exception:: WantReadError
192
193 The operation did not complete; the same I/O method should be called again
194 later, with the same arguments. Any I/O method can lead to this since new
195 handshakes can occur at any time.
196
197 The wanted read is for **dirty** data sent over the network, not the
198 **clean** data inside the tunnel. For a socket based SSL connection,
199 **read** means data coming at us over the network. Until that read
200 succeeds, the attempted :py:meth:`OpenSSL.SSL.Connection.recv`,
201 :py:meth:`OpenSSL.SSL.Connection.send`, or
202 :py:meth:`OpenSSL.SSL.Connection.do_handshake` is prevented or incomplete. You
203 probably want to :py:meth:`select()` on the socket before trying again.
204
205
206.. py:exception:: WantWriteError
207
208 See :py:exc:`WantReadError`. The socket send buffer may be too full to
209 write more data.
210
211
212.. py:exception:: WantX509LookupError
213
214 The operation did not complete because an application callback has asked to be
215 called again. The I/O method should be called again later, with the same
216 arguments.
217
218 .. note:: This won't occur in this version, as there are no such
219 callbacks in this version.
220
221
222.. py:exception:: SysCallError
223
224 The :py:exc:`SysCallError` occurs when there's an I/O error and OpenSSL's
225 error queue does not contain any information. This can mean two things: An
226 error in the transport protocol, or an end of file that violates the protocol.
227 The parameter to the exception is always a pair *(errnum,
228 errstr)*.
229
230
231
232.. _openssl-context:
233
234Context objects
235---------------
236
237Context objects have the following methods:
238
239.. :py:class:: OpenSSL.SSL.Context
240
241.. py:method:: Context.check_privatekey()
242
243 Check if the private key (loaded with :py:meth:`use_privatekey`) matches the
244 certificate (loaded with :py:meth:`use_certificate`). Returns
245 :py:data:`None` if they match, raises :py:exc:`Error` otherwise.
246
247
248.. py:method:: Context.get_app_data()
249
250 Retrieve application data as set by :py:meth:`set_app_data`.
251
252
253.. py:method:: Context.get_cert_store()
254
255 Retrieve the certificate store (a X509Store object) that the context uses.
Alex Gaynor6b5028d2014-03-31 14:23:57 -0700256 This can be used to add "trusted" certificates without using the
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900257 :py:meth:`load_verify_locations` method.
258
259
260.. py:method:: Context.get_timeout()
261
262 Retrieve session timeout, as set by :py:meth:`set_timeout`. The default is 300
263 seconds.
264
265
266.. py:method:: Context.get_verify_depth()
267
268 Retrieve the Context object's verify depth, as set by
269 :py:meth:`set_verify_depth`.
270
271
272.. py:method:: Context.get_verify_mode()
273
274 Retrieve the Context object's verify mode, as set by :py:meth:`set_verify`.
275
276
Hynek Schlawackb1f3ca82016-02-13 09:10:04 +0100277.. automethod:: Context.load_client_ca
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900278
279
280.. py:method:: Context.set_client_ca_list(certificate_authorities)
281
282 Replace the current list of preferred certificate signers that would be
283 sent to the client when requesting a client certificate with the
284 *certificate_authorities* sequence of :py:class:`OpenSSL.crypto.X509Name`'s.
285
286 .. versionadded:: 0.10
287
288
289.. py:method:: Context.add_client_ca(certificate_authority)
290
291 Extract a :py:class:`OpenSSL.crypto.X509Name` from the *certificate_authority*
292 :py:class:`OpenSSL.crypto.X509` certificate and add it to the list of preferred
293 certificate signers sent to the client when requesting a client certificate.
294
295 .. versionadded:: 0.10
296
297
298.. py:method:: Context.load_verify_locations(pemfile, capath)
299
300 Specify where CA certificates for verification purposes are located. These
301 are trusted certificates. Note that the certificates have to be in PEM
302 format. If capath is passed, it must be a directory prepared using the
Jonathan Ballet6381da32011-07-20 16:43:38 +0900303 ``c_rehash`` tool included with OpenSSL. Either, but not both, of
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900304 *pemfile* or *capath* may be :py:data:`None`.
305
306
307.. py:method:: Context.set_default_verify_paths()
308
Hynek Schlawack81021282017-07-20 10:32:37 +0200309 Specify that the platform provided CA certificates are to be used for verification purposes.
310 This method has some caveats related to the binary wheels that cryptography (pyOpenSSL's primary dependency) ships:
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900311
Hynek Schlawack81021282017-07-20 10:32:37 +0200312 * macOS will only load certificates using this method if the user has the ``openssl@1.1`` `Homebrew <https://brew.sh>`_ formula installed in the default location.
Paul Kehrerad44ccd2017-07-19 21:34:01 +0200313 * Windows will not work.
Hynek Schlawack81021282017-07-20 10:32:37 +0200314 * manylinux1 cryptography wheels will work on most common Linux distributions in pyOpenSSL 17.1.0 and above.
315 pyOpenSSL detects the manylinux1 wheel and attempts to load roots via a fallback path.
316
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900317
318.. py:method:: Context.load_tmp_dh(dhfile)
319
320 Load parameters for Ephemeral Diffie-Hellman from *dhfile*.
321
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400322
Jean-Paul Calderone3e4e3352014-04-19 09:28:28 -0400323.. py:method:: Context.set_tmp_ecdh(curve)
Alex Gaynord5419e22014-01-19 21:03:36 -0600324
Andy Lutomirskif05a2732014-03-13 17:22:25 -0700325 Select a curve to use for ECDHE key exchange.
Alex Gaynord5419e22014-01-19 21:03:36 -0600326
Jean-Paul Calderonec09fd582014-04-18 22:00:10 -0400327 The valid values of *curve* are the objects returned by
328 :py:func:`OpenSSL.crypto.get_elliptic_curves` or
329 :py:func:`OpenSSL.crypto.get_elliptic_curve`.
Alex Gaynord5419e22014-01-19 21:03:36 -0600330
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900331
332.. py:method:: Context.set_app_data(data)
333
334 Associate *data* with this Context object. *data* can be retrieved
335 later using the :py:meth:`get_app_data` method.
336
337
Hynek Schlawackf90e3682016-03-11 11:21:13 +0100338.. automethod:: Context.set_cipher_list
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900339
340.. py:method:: Context.set_info_callback(callback)
341
342 Set the information callback to *callback*. This function will be called
343 from time to time during SSL handshakes.
344
Jonathan Ballet6381da32011-07-20 16:43:38 +0900345 *callback* should take three arguments: a Connection object and two integers.
346 The first integer specifies where in the SSL handshake the function was
347 called, and the other the return code from a (possibly failed) internal
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900348 function call.
349
350
351.. py:method:: Context.set_options(options)
352
353 Add SSL options. Options you have set before are not cleared!
354 This method should be used with the :py:const:`OP_*` constants.
355
356
Jean-Paul Calderone21641542011-09-11 09:18:14 -0400357.. py:method:: Context.set_mode(mode)
358
359 Add SSL mode. Modes you have set before are not cleared! This method should
360 be used with the :py:const:`MODE_*` constants.
361
362
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900363.. py:method:: Context.set_passwd_cb(callback[, userdata])
364
365 Set the passphrase callback to *callback*. This function will be called
366 when a private key with a passphrase is loaded. *callback* must accept
367 three positional arguments. First, an integer giving the maximum length of
368 the passphrase it may return. If the returned passphrase is longer than
369 this, it will be truncated. Second, a boolean value which will be true if
370 the user should be prompted for the passphrase twice and the callback should
371 verify that the two values supplied are equal. Third, the value given as the
Diego Fernandezfe0120f2017-10-11 23:51:52 -0600372 *userdata* parameter to :py:meth:`set_passwd_cb`. The *callback* must return
373 a byte string. If an error occurs, *callback* should return a false value
374 (e.g. an empty string).
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900375
376
Jean-Paul Calderone8e8f90c2012-02-08 13:16:26 -0500377.. py:method:: Context.set_session_cache_mode(mode)
378
379 Set the behavior of the session cache used by all connections using this
380 Context. The previously set mode is returned. See :py:const:`SESS_CACHE_*`
381 for details about particular modes.
382
383 .. versionadded:: 0.14
384
385
386.. py:method:: Context.get_session_cache_mode()
387
388 Get the current session cache mode.
389
390 .. versionadded:: 0.14
391
392
Hynek Schlawackb1f3ca82016-02-13 09:10:04 +0100393.. automethod:: Context.set_session_id
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900394
395
396.. py:method:: Context.set_timeout(timeout)
397
398 Set the timeout for newly created sessions for this Context object to
399 *timeout*. *timeout* must be given in (whole) seconds. The default
400 value is 300 seconds. See the OpenSSL manual for more information (e.g.
401 :manpage:`SSL_CTX_set_timeout(3)`).
402
403
404.. py:method:: Context.set_verify(mode, callback)
405
406 Set the verification flags for this Context object to *mode* and specify
407 that *callback* should be used for verification callbacks. *mode* should be
408 one of :py:const:`VERIFY_NONE` and :py:const:`VERIFY_PEER`. If
409 :py:const:`VERIFY_PEER` is used, *mode* can be OR:ed with
410 :py:const:`VERIFY_FAIL_IF_NO_PEER_CERT` and :py:const:`VERIFY_CLIENT_ONCE`
411 to further control the behaviour.
412
413 *callback* should take five arguments: A Connection object, an X509 object,
414 and three integer variables, which are in turn potential error number, error
415 depth and return code. *callback* should return true if verification passes
416 and false otherwise.
417
418
419.. py:method:: Context.set_verify_depth(depth)
420
421 Set the maximum depth for the certificate chain verification that shall be
422 allowed for this Context object.
423
424
425.. py:method:: Context.use_certificate(cert)
426
427 Use the certificate *cert* which has to be a X509 object.
428
429
430.. py:method:: Context.add_extra_chain_cert(cert)
431
432 Adds the certificate *cert*, which has to be a X509 object, to the
433 certificate chain presented together with the certificate.
434
435
436.. py:method:: Context.use_certificate_chain_file(file)
437
438 Load a certificate chain from *file* which must be PEM encoded.
439
440
441.. py:method:: Context.use_privatekey(pkey)
442
443 Use the private key *pkey* which has to be a PKey object.
444
445
446.. py:method:: Context.use_certificate_file(file[, format])
447
448 Load the first certificate found in *file*. The certificate must be in the
449 format specified by *format*, which is either :py:const:`FILETYPE_PEM` or
450 :py:const:`FILETYPE_ASN1`. The default is :py:const:`FILETYPE_PEM`.
451
452
453.. py:method:: Context.use_privatekey_file(file[, format])
454
455 Load the first private key found in *file*. The private key must be in the
456 format specified by *format*, which is either :py:const:`FILETYPE_PEM` or
457 :py:const:`FILETYPE_ASN1`. The default is :py:const:`FILETYPE_PEM`.
458
459
460.. py:method:: Context.set_tlsext_servername_callback(callback)
461
462 Specify a one-argument callable to use as the TLS extension server name
Jonathan Ballet6381da32011-07-20 16:43:38 +0900463 callback. When a connection using the server name extension is made using
464 this context, the callback will be invoked with the :py:class:`Connection`
465 instance.
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900466
467 .. versionadded:: 0.13
468
469
Cory Benfieldbe3e7b82014-05-10 09:48:55 +0100470.. py:method:: Context.set_npn_advertise_callback(callback)
471
472 Specify a callback function that will be called when offering `Next
473 Protocol Negotiation
Hynek Schlawackc3b38e52016-10-15 14:56:14 +0200474 <https://tools.ietf.org/html/draft-agl-tls-nextprotoneg-03>`_ as a server.
Cory Benfieldbe3e7b82014-05-10 09:48:55 +0100475
476 *callback* should be the callback function. It will be invoked with one
477 argument, the :py:class:`Connection` instance. It should return a list of
478 bytestrings representing the advertised protocols, like
479 ``[b'http/1.1', b'spdy/2']``.
480
481 .. versionadded:: 0.15
482
483
484.. py:method:: Context.set_npn_select_callback(callback):
485
486 Specify a callback function that will be called when a server offers Next
487 Protocol Negotiation options.
488
489 *callback* should be the callback function. It will be invoked with two
490 arguments: the :py:class:`Connection`, and a list of offered protocols as
491 bytestrings, e.g. ``[b'http/1.1', b'spdy/2']``. It should return one of
492 those bytestrings, the chosen protocol.
493
494 .. versionadded:: 0.15
495
Cory Benfield12eae892014-06-07 15:42:56 +0100496.. py:method:: Context.set_alpn_protos(protos)
497
498 Specify the protocols that the client is prepared to speak after the TLS
Cory Benfielde58a93a2015-04-13 18:26:05 -0400499 connection has been negotiated using Application Layer Protocol
Cory Benfield12eae892014-06-07 15:42:56 +0100500 Negotiation.
501
502 *protos* should be a list of protocols that the client is offering, each
503 as a bytestring. For example, ``[b'http/1.1', b'spdy/2']``.
504
505
506.. py:method:: Context.set_alpn_select_callback(callback)
507
508 Specify a callback function that will be called on the server when a client
509 offers protocols using Application Layer Protocol Negotiation.
510
511 *callback* should be the callback function. It will be invoked with two
Cory Benfielde58a93a2015-04-13 18:26:05 -0400512 arguments: the :py:class:`Connection` and a list of offered protocols as
Cory Benfield12eae892014-06-07 15:42:56 +0100513 bytestrings, e.g. ``[b'http/1.1', b'spdy/2']``. It should return one of
514 these bytestrings, the chosen protocol.
515
Cory Benfieldbe3e7b82014-05-10 09:48:55 +0100516
Jean-Paul Calderone6c896fe2012-02-16 08:10:04 -0500517.. _openssl-session:
518
519Session objects
520---------------
521
522Session objects have no methods.
523
524
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900525.. _openssl-connection:
526
527Connection objects
528------------------
529
530Connection objects have the following methods:
531
532.. py:method:: Connection.accept()
533
534 Call the :py:meth:`accept` method of the underlying socket and set up SSL on the
535 returned socket, using the Context object supplied to this Connection object at
536 creation. Returns a pair *(conn, address)*. where *conn* is the new
537 Connection object created, and *address* is as returned by the socket's
538 :py:meth:`accept`.
539
540
541.. py:method:: Connection.bind(address)
542
543 Call the :py:meth:`bind` method of the underlying socket.
544
545
546.. py:method:: Connection.close()
547
548 Call the :py:meth:`close` method of the underlying socket. Note: If you want
549 correct SSL closure, you need to call the :py:meth:`shutdown` method first.
550
551
552.. py:method:: Connection.connect(address)
553
554 Call the :py:meth:`connect` method of the underlying socket and set up SSL on the
555 socket, using the Context object supplied to this Connection object at
556 creation.
557
558
559.. py:method:: Connection.connect_ex(address)
560
561 Call the :py:meth:`connect_ex` method of the underlying socket and set up SSL on
562 the socket, using the Context object supplied to this Connection object at
563 creation. Note that if the :py:meth:`connect_ex` method of the socket doesn't
564 return 0, SSL won't be initialized.
565
566
567.. py:method:: Connection.do_handshake()
568
569 Perform an SSL handshake (usually called after :py:meth:`renegotiate` or one of
570 :py:meth:`set_accept_state` or :py:meth:`set_accept_state`). This can raise the
571 same exceptions as :py:meth:`send` and :py:meth:`recv`.
572
573
574.. py:method:: Connection.fileno()
575
576 Retrieve the file descriptor number for the underlying socket.
577
578
579.. py:method:: Connection.listen(backlog)
580
581 Call the :py:meth:`listen` method of the underlying socket.
582
583
584.. py:method:: Connection.get_app_data()
585
586 Retrieve application data as set by :py:meth:`set_app_data`.
587
588
Hynek Schlawackf90e3682016-03-11 11:21:13 +0100589.. automethod:: Connection.get_cipher_list
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900590
591
Jim Shaver2637c3b2015-04-27 00:35:09 -0400592.. py:method:: Connection.get_protocol_version()
593
Jim Shaverabff1882015-05-27 09:15:55 -0400594 Retrieve the version of the SSL or TLS protocol used by the Connection.
Jim Shaver208438c2015-05-28 09:52:38 -0400595 For example, it will return ``0x769`` for connections made over TLS
596 version 1.
Jim Shaverabff1882015-05-27 09:15:55 -0400597
598
599.. py:method:: Connection.get_protocol_version_name()
600
Jim Shaverb5b6b0e2015-05-28 16:47:36 -0400601 Retrieve the version of the SSL or TLS protocol used by the Connection as
602 a unicode string. For example, it will return ``TLSv1`` for connections
Hynek Schlawackf90e3682016-03-11 11:21:13 +0100603 made over TLS version 1, or ``Unknown`` for connections that were not
Jim Shaverb5b6b0e2015-05-28 16:47:36 -0400604 successfully established.
Jim Shaver2637c3b2015-04-27 00:35:09 -0400605
606
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900607.. py:method:: Connection.get_client_ca_list()
608
609 Retrieve the list of preferred client certificate issuers sent by the server
610 as :py:class:`OpenSSL.crypto.X509Name` objects.
611
612 If this is a client :py:class:`Connection`, the list will be empty until the
613 connection with the server is established.
614
615 If this is a server :py:class:`Connection`, return the list of certificate
616 authorities that will be sent or has been sent to the client, as controlled
617 by this :py:class:`Connection`'s :py:class:`Context`.
618
619 .. versionadded:: 0.10
620
621
622.. py:method:: Connection.get_context()
623
624 Retrieve the Context object associated with this Connection.
625
626
627.. py:method:: Connection.set_context(context)
628
629 Specify a replacement Context object for this Connection.
630
631
632.. py:method:: Connection.get_peer_certificate()
633
634 Retrieve the other side's certificate (if any)
635
636
637.. py:method:: Connection.get_peer_cert_chain()
638
639 Retrieve the tuple of the other side's certificate chain (if any)
640
641
642.. py:method:: Connection.getpeername()
643
644 Call the :py:meth:`getpeername` method of the underlying socket.
645
646
647.. py:method:: Connection.getsockname()
648
649 Call the :py:meth:`getsockname` method of the underlying socket.
650
651
652.. py:method:: Connection.getsockopt(level, optname[, buflen])
653
654 Call the :py:meth:`getsockopt` method of the underlying socket.
655
656
657.. py:method:: Connection.pending()
658
659 Retrieve the number of bytes that can be safely read from the SSL buffer
660 (**not** the underlying transport buffer).
661
662
Maximilian Hils1d95dea2015-08-17 19:27:20 +0200663.. py:method:: Connection.recv(bufsize[, flags])
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900664
665 Receive data from the Connection. The return value is a string representing the
666 data received. The maximum amount of data to be received at once, is specified
Maximilian Hils1d95dea2015-08-17 19:27:20 +0200667 by *bufsize*. The only supported flag is ``MSG_PEEK``, all other flags are
668 ignored.
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900669
670
Jean-Paul Calderone0191a182015-03-21 07:41:35 -0400671.. py:method:: Connection.recv_into(buffer[, nbytes[, flags]])
Cory Benfield62d10332014-06-15 10:03:41 +0100672
673 Receive data from the Connection and copy it directly into the provided
674 buffer. The return value is the number of bytes read from the connection.
675 The maximum amount of data to be received at once is specified by *nbytes*.
Maximilian Hils1d95dea2015-08-17 19:27:20 +0200676 The only supported flag is ``MSG_PEEK``, all other flags are ignored.
Cory Benfield62d10332014-06-15 10:03:41 +0100677
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900678.. py:method:: Connection.bio_write(bytes)
679
680 If the Connection was created with a memory BIO, this method can be used to add
681 bytes to the read end of that memory BIO. The Connection can then read the
682 bytes (for example, in response to a call to :py:meth:`recv`).
683
684
Hynek Schlawackb1f3ca82016-02-13 09:10:04 +0100685.. automethod:: Connection.renegotiate
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900686
Hynek Schlawackb1f3ca82016-02-13 09:10:04 +0100687.. automethod:: Connection.renegotiate_pending
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900688
Hynek Schlawackb1f3ca82016-02-13 09:10:04 +0100689.. automethod:: Connection.total_renegotiations
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900690
691.. py:method:: Connection.send(string)
692
693 Send the *string* data to the Connection.
694
695
696.. py:method:: Connection.bio_read(bufsize)
697
698 If the Connection was created with a memory BIO, this method can be used to
699 read bytes from the write end of that memory BIO. Many Connection methods will
700 add bytes which must be read in this manner or the buffer will eventually fill
701 up and the Connection will be able to take no further actions.
702
703
704.. py:method:: Connection.sendall(string)
705
706 Send all of the *string* data to the Connection. This calls :py:meth:`send`
707 repeatedly until all data is sent. If an error occurs, it's impossible to tell
708 how much data has been sent.
709
710
711.. py:method:: Connection.set_accept_state()
712
713 Set the connection to work in server mode. The handshake will be handled
714 automatically by read/write.
715
716
717.. py:method:: Connection.set_app_data(data)
718
719 Associate *data* with this Connection object. *data* can be retrieved
720 later using the :py:meth:`get_app_data` method.
721
722
723.. py:method:: Connection.set_connect_state()
724
725 Set the connection to work in client mode. The handshake will be handled
726 automatically by read/write.
727
728
729.. py:method:: Connection.setblocking(flag)
730
731 Call the :py:meth:`setblocking` method of the underlying socket.
732
733
734.. py:method:: Connection.setsockopt(level, optname, value)
735
736 Call the :py:meth:`setsockopt` method of the underlying socket.
737
738
739.. py:method:: Connection.shutdown()
740
741 Send the shutdown message to the Connection. Returns true if the shutdown
742 message exchange is completed and false otherwise (in which case you call
743 :py:meth:`recv` or :py:meth:`send` when the connection becomes
744 readable/writeable.
745
746
747.. py:method:: Connection.get_shutdown()
748
749 Get the shutdown state of the Connection. Returns a bitvector of either or
750 both of *SENT_SHUTDOWN* and *RECEIVED_SHUTDOWN*.
751
752
753.. py:method:: Connection.set_shutdown(state)
754
755 Set the shutdown state of the Connection. *state* is a bitvector of
756 either or both of *SENT_SHUTDOWN* and *RECEIVED_SHUTDOWN*.
757
758
759.. py:method:: Connection.sock_shutdown(how)
760
761 Call the :py:meth:`shutdown` method of the underlying socket.
762
763
764.. py:method:: Connection.bio_shutdown()
765
766 If the Connection was created with a memory BIO, this method can be used to
767 indicate that *end of file* has been reached on the read end of that memory
768 BIO.
769
770
Hynek Schlawackea94f2b2016-03-13 16:17:53 +0100771.. automethod:: Connection.get_state_string
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900772
773.. py:method:: Connection.client_random()
774
775 Retrieve the random value used with the client hello message.
776
777
778.. py:method:: Connection.server_random()
779
780 Retrieve the random value used with the server hello message.
781
782
783.. py:method:: Connection.master_key()
784
785 Retrieve the value of the master key for this session.
786
787
788.. py:method:: Connection.want_read()
789
790 Checks if more data has to be read from the transport layer to complete an
791 operation.
792
793
794.. py:method:: Connection.want_write()
795
796 Checks if there is data to write to the transport layer to complete an
797 operation.
798
799
800.. py:method:: Connection.set_tlsext_host_name(name)
801
802 Specify the byte string to send as the server name in the client hello message.
803
804 .. versionadded:: 0.13
805
806
807.. py:method:: Connection.get_servername()
808
809 Get the value of the server name received in the client hello message.
810
811 .. versionadded:: 0.13
812
813
Jean-Paul Calderone6c896fe2012-02-16 08:10:04 -0500814.. py:method:: Connection.get_session()
815
816 Get a :py:class:`Session` instance representing the SSL session in use by
817 the connection, or :py:obj:`None` if there is no session.
818
819 .. versionadded:: 0.14
820
821
822.. py:method:: Connection.set_session(session)
823
824 Set a new SSL session (using a :py:class:`Session` instance) to be used by
825 the connection.
826
827 .. versionadded:: 0.14
828
829
Fedor Brunner416f4a12014-03-28 13:18:38 +0100830.. py:method:: Connection.get_finished()
831
832 Obtain latest TLS Finished message that we sent, or :py:obj:`None` if
833 handshake is not completed.
834
835 .. versionadded:: 0.15
836
Jean-Paul Calderone7c556ef2014-03-30 10:45:00 -0400837
Fedor Brunner416f4a12014-03-28 13:18:38 +0100838.. py:method:: Connection.get_peer_finished()
839
840 Obtain latest TLS Finished message that we expected from peer, or
841 :py:obj:`None` if handshake is not completed.
842
843 .. versionadded:: 0.15
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900844
Jean-Paul Calderone7c556ef2014-03-30 10:45:00 -0400845
Fedor Brunner2cffdbc2014-03-10 10:35:23 +0100846.. py:method:: Connection.get_cipher_name()
847
848 Obtain the name of the currently used cipher.
849
850 .. versionadded:: 0.15
851
Jean-Paul Calderone7c556ef2014-03-30 10:45:00 -0400852
Fedor Brunner2cffdbc2014-03-10 10:35:23 +0100853.. py:method:: Connection.get_cipher_bits()
854
855 Obtain the number of secret bits of the currently used cipher.
856
857 .. versionadded:: 0.15
858
Jean-Paul Calderone7c556ef2014-03-30 10:45:00 -0400859
Fedor Brunner2cffdbc2014-03-10 10:35:23 +0100860.. py:method:: Connection.get_cipher_version()
861
862 Obtain the protocol name of the currently used cipher.
863
864 .. versionadded:: 0.15
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900865
Jean-Paul Calderone7c556ef2014-03-30 10:45:00 -0400866
Cory Benfieldbe3e7b82014-05-10 09:48:55 +0100867.. py:method:: Connection.get_next_proto_negotiated():
868
Cory Benfieldcd010f62014-05-15 19:00:27 +0100869 Get the protocol that was negotiated by Next Protocol Negotiation. Returns
870 a bytestring of the protocol name. If no protocol has been negotiated yet,
871 returns an empty string.
Cory Benfieldbe3e7b82014-05-10 09:48:55 +0100872
873 .. versionadded:: 0.15
874
Cory Benfield12eae892014-06-07 15:42:56 +0100875.. py:method:: Connection.set_alpn_protos(protos)
876
877 Specify the protocols that the client is prepared to speak after the TLS
Cory Benfielde58a93a2015-04-13 18:26:05 -0400878 connection has been negotiated using Application Layer Protocol
Cory Benfield12eae892014-06-07 15:42:56 +0100879 Negotiation.
880
881 *protos* should be a list of protocols that the client is offering, each
882 as a bytestring. For example, ``[b'http/1.1', b'spdy/2']``.
883
884
885.. py:method:: Connection.get_alpn_proto_negotiated()
886
887 Get the protocol that was negotiated by Application Layer Protocol
888 Negotiation. Returns a bytestring of the protocol name. If no protocol has
889 been negotiated yet, returns an empty string.
890
Cory Benfieldbe3e7b82014-05-10 09:48:55 +0100891
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900892.. Rubric:: Footnotes
893
894.. [#connection-context-socket] Actually, all that is required is an object that
895 **behaves** like a socket, you could even use files, even though it'd be
896 tricky to get the handshakes right!