Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 1 | .. _openssl-ssl: |
| 2 | |
| 3 | :py:mod:`SSL` --- An interface to the SSL-specific parts of OpenSSL |
| 4 | =================================================================== |
| 5 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 6 | .. py:module:: OpenSSL.SSL |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 7 | :synopsis: An interface to the SSL-specific parts of OpenSSL |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 8 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 9 | |
| 10 | This module handles things specific to SSL. There are two objects defined: |
| 11 | Context, Connection. |
| 12 | |
| 13 | .. py:data:: SSLv2_METHOD |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 14 | SSLv3_METHOD |
| 15 | SSLv23_METHOD |
| 16 | TLSv1_METHOD |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 17 | TLSv1_1_METHOD |
| 18 | TLSv1_2_METHOD |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 19 | |
| 20 | These constants represent the different SSL methods to use when creating a |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 21 | 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 Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 24 | |
| 25 | |
| 26 | .. py:data:: VERIFY_NONE |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 27 | VERIFY_PEER |
| 28 | VERIFY_FAIL_IF_NO_PEER_CERT |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 29 | |
| 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 Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 35 | FILETYPE_ASN1 |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 36 | |
| 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 Yamazaki | 7eee79d | 2015-09-05 23:50:05 +0900 | [diff] [blame] | 42 | OP_SINGLE_ECDH_USE |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 43 | |
Akihiro Yamazaki | 8509cec | 2015-09-06 02:01:21 +0900 | [diff] [blame] | 44 | Constants used with :py:meth:`set_options` of Context objects. |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 45 | |
Akihiro Yamazaki | 8509cec | 2015-09-06 02:01:21 +0900 | [diff] [blame] | 46 | When these options are used, a new key will always be created when using |
Akihiro Yamazaki | 7eee79d | 2015-09-05 23:50:05 +0900 | [diff] [blame] | 47 | ephemeral (Elliptic curve) Diffie-Hellman. |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 48 | |
| 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 Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 73 | OP_NO_SSLv3 |
| 74 | OP_NO_TLSv1 |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 75 | OP_NO_TLSv1_1 |
| 76 | OP_NO_TLSv1_2 |
Nathaniel J. Smith | a181373 | 2019-08-01 21:32:13 -0700 | [diff] [blame] | 77 | OP_NO_TLSv1_3 |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 78 | |
| 79 | Constants used with :py:meth:`set_options` of Context objects. |
| 80 | |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 81 | 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 Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 86 | |
| 87 | |
| 88 | .. py:data:: SSLEAY_VERSION |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 89 | SSLEAY_CFLAGS |
| 90 | SSLEAY_BUILT_ON |
| 91 | SSLEAY_PLATFORM |
| 92 | SSLEAY_DIR |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 93 | |
| 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 Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 98 | |
Jean-Paul Calderone | 8e8f90c | 2012-02-08 13:16:26 -0500 | [diff] [blame] | 99 | .. 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 Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 113 | |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 114 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 115 | .. 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 Williams | 5d890a0 | 2019-11-17 19:56:26 -0800 | [diff] [blame] | 122 | .. 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 Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 131 | .. autofunction:: SSLeay_version |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 132 | |
| 133 | |
| 134 | .. py:data:: ContextType |
| 135 | |
| 136 | See :py:class:`Context`. |
| 137 | |
| 138 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 139 | .. autoclass:: Context |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 140 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 141 | .. autoclass:: Session |
Jean-Paul Calderone | 6c896fe | 2012-02-16 08:10:04 -0500 | [diff] [blame] | 142 | |
| 143 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 144 | .. 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 | |
| 226 | Context objects |
| 227 | --------------- |
| 228 | |
| 229 | Context objects have the following methods: |
| 230 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 231 | .. autoclass:: OpenSSL.SSL.Context |
| 232 | :members: |
Cory Benfield | be3e7b8 | 2014-05-10 09:48:55 +0100 | [diff] [blame] | 233 | |
Jean-Paul Calderone | 6c896fe | 2012-02-16 08:10:04 -0500 | [diff] [blame] | 234 | .. _openssl-session: |
| 235 | |
| 236 | Session objects |
| 237 | --------------- |
| 238 | |
| 239 | Session objects have no methods. |
| 240 | |
| 241 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 242 | .. _openssl-connection: |
| 243 | |
| 244 | Connection objects |
| 245 | ------------------ |
| 246 | |
| 247 | Connection objects have the following methods: |
| 248 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 249 | .. autoclass:: OpenSSL.SSL.Connection |
| 250 | :members: |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 251 | |
Cory Benfield | be3e7b8 | 2014-05-10 09:48:55 +0100 | [diff] [blame] | 252 | |
Jonathan Ballet | 6381da3 | 2011-07-20 16:43:38 +0900 | [diff] [blame] | 253 | .. Rubric:: Footnotes |
| 254 | |
Jonathan Ballet | c9e066c | 2011-07-17 22:56:05 +0900 | [diff] [blame] | 255 | .. [#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! |