Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1 | /* SSL socket module |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2 | |
| 3 | SSL support based on patches by Brian E Gallew and Laszlo Kovacs. |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 4 | Re-worked a bit by Bill Janssen to add server-side support and |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 5 | certificate decoding. Chris Stawarz contributed some non-blocking |
| 6 | patches. |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 7 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 8 | This module is imported by ssl.py. It should *not* be used |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 9 | directly. |
| 10 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 11 | XXX should partial writes be enabled, SSL_MODE_ENABLE_PARTIAL_WRITE? |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 12 | |
| 13 | XXX integrate several "shutdown modes" as suggested in |
| 14 | http://bugs.python.org/issue8108#msg102867 ? |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Christian Heimes | a483388 | 2021-04-13 08:17:26 +0200 | [diff] [blame] | 17 | /* Don't warn about deprecated functions, */ |
| 18 | #ifndef OPENSSL_API_COMPAT |
| 19 | // 0x10101000L == 1.1.1, 30000 == 3.0.0 |
| 20 | #define OPENSSL_API_COMPAT 0x10101000L |
| 21 | #endif |
| 22 | #define OPENSSL_NO_DEPRECATED 1 |
| 23 | |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 24 | #define PY_SSIZE_T_CLEAN |
| 25 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 26 | #include "Python.h" |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 27 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 28 | /* Include symbols from _socket module */ |
| 29 | #include "socketmodule.h" |
| 30 | |
| 31 | #include "_ssl.h" |
| 32 | |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 33 | /* Redefined below for Windows debug builds after important #includes */ |
| 34 | #define _PySSL_FIX_ERRNO |
Christian Heimes | f77b4b2 | 2013-08-21 13:26:05 +0200 | [diff] [blame] | 35 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 36 | #define PySSL_BEGIN_ALLOW_THREADS_S(save) \ |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 37 | do { (save) = PyEval_SaveThread(); } while(0) |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 38 | #define PySSL_END_ALLOW_THREADS_S(save) \ |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 39 | do { PyEval_RestoreThread(save); _PySSL_FIX_ERRNO; } while(0) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 40 | #define PySSL_BEGIN_ALLOW_THREADS { \ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 41 | PyThreadState *_save = NULL; \ |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 42 | PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 43 | #define PySSL_BLOCK_THREADS PySSL_END_ALLOW_THREADS_S(_save); |
| 44 | #define PySSL_UNBLOCK_THREADS PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 45 | #define PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS_S(_save); } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 46 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 47 | |
| 48 | #if defined(HAVE_POLL_H) |
| 49 | #include <poll.h> |
| 50 | #elif defined(HAVE_SYS_POLL_H) |
| 51 | #include <sys/poll.h> |
| 52 | #endif |
| 53 | |
| 54 | /* Include OpenSSL header files */ |
| 55 | #include "openssl/rsa.h" |
| 56 | #include "openssl/crypto.h" |
| 57 | #include "openssl/x509.h" |
| 58 | #include "openssl/x509v3.h" |
| 59 | #include "openssl/pem.h" |
| 60 | #include "openssl/ssl.h" |
| 61 | #include "openssl/err.h" |
| 62 | #include "openssl/rand.h" |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 63 | #include "openssl/bio.h" |
Alexandru Ardelean | b3a271f | 2018-09-17 14:53:31 +0300 | [diff] [blame] | 64 | #include "openssl/dh.h" |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 65 | |
Christian Heimes | c087a26 | 2020-05-15 20:55:25 +0200 | [diff] [blame] | 66 | #ifndef OPENSSL_THREADS |
| 67 | # error "OPENSSL_THREADS is not defined, Python requires thread-safe OpenSSL" |
| 68 | #endif |
| 69 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 70 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 71 | |
| 72 | struct py_ssl_error_code { |
| 73 | const char *mnemonic; |
| 74 | int library, reason; |
| 75 | }; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 76 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 77 | struct py_ssl_library_code { |
| 78 | const char *library; |
| 79 | int code; |
| 80 | }; |
| 81 | |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 82 | #if defined(MS_WINDOWS) && defined(Py_DEBUG) |
| 83 | /* Debug builds on Windows rely on getting errno directly from OpenSSL. |
| 84 | * However, because it uses a different CRT, we need to transfer the |
| 85 | * value of errno from OpenSSL into our debug CRT. |
| 86 | * |
| 87 | * Don't be fooled - this is horribly ugly code. The only reasonable |
| 88 | * alternative is to do both debug and release builds of OpenSSL, which |
| 89 | * requires much uglier code to transform their automatically generated |
| 90 | * makefile. This is the lesser of all the evils. |
| 91 | */ |
| 92 | |
| 93 | static void _PySSLFixErrno(void) { |
| 94 | HMODULE ucrtbase = GetModuleHandleW(L"ucrtbase.dll"); |
| 95 | if (!ucrtbase) { |
| 96 | /* If ucrtbase.dll is not loaded but the SSL DLLs are, we likely |
| 97 | * have a catastrophic failure, but this function is not the |
| 98 | * place to raise it. */ |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | typedef int *(__stdcall *errno_func)(void); |
| 103 | errno_func ssl_errno = (errno_func)GetProcAddress(ucrtbase, "_errno"); |
| 104 | if (ssl_errno) { |
| 105 | errno = *ssl_errno(); |
| 106 | *ssl_errno() = 0; |
| 107 | } else { |
| 108 | errno = ENOTRECOVERABLE; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | #undef _PySSL_FIX_ERRNO |
| 113 | #define _PySSL_FIX_ERRNO _PySSLFixErrno() |
| 114 | #endif |
| 115 | |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 116 | /* Include generated data (error codes) */ |
Christian Heimes | 150af75 | 2021-04-09 17:02:00 +0200 | [diff] [blame] | 117 | #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) |
| 118 | #include "_ssl_data_300.h" |
| 119 | #elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 120 | #include "_ssl_data_111.h" |
| 121 | #else |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 122 | #include "_ssl_data.h" |
Christian Heimes | 150af75 | 2021-04-09 17:02:00 +0200 | [diff] [blame] | 123 | #endif |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 124 | |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 125 | /* OpenSSL API 1.1.0+ does not include version methods */ |
Christian Heimes | 3309113 | 2021-04-20 18:10:10 +0200 | [diff] [blame] | 126 | #ifndef OPENSSL_NO_SSL3_METHOD |
| 127 | extern const SSL_METHOD *SSLv3_method(void); |
| 128 | #endif |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 129 | #ifndef OPENSSL_NO_TLS1_METHOD |
Christian Heimes | a483388 | 2021-04-13 08:17:26 +0200 | [diff] [blame] | 130 | extern const SSL_METHOD *TLSv1_method(void); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 131 | #endif |
| 132 | #ifndef OPENSSL_NO_TLS1_1_METHOD |
Christian Heimes | a483388 | 2021-04-13 08:17:26 +0200 | [diff] [blame] | 133 | extern const SSL_METHOD *TLSv1_1_method(void); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 134 | #endif |
| 135 | #ifndef OPENSSL_NO_TLS1_2_METHOD |
Christian Heimes | a483388 | 2021-04-13 08:17:26 +0200 | [diff] [blame] | 136 | extern const SSL_METHOD *TLSv1_2_method(void); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 137 | #endif |
| 138 | |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 139 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 140 | #define INVALID_SOCKET (-1) |
| 141 | #endif |
| 142 | |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 143 | /* OpenSSL 1.1 does not have SSL 2.0 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 144 | #define OPENSSL_NO_SSL2 |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 145 | |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 146 | /* Default cipher suites */ |
| 147 | #ifndef PY_SSL_DEFAULT_CIPHERS |
| 148 | #define PY_SSL_DEFAULT_CIPHERS 1 |
| 149 | #endif |
| 150 | |
| 151 | #if PY_SSL_DEFAULT_CIPHERS == 0 |
| 152 | #ifndef PY_SSL_DEFAULT_CIPHER_STRING |
| 153 | #error "Py_SSL_DEFAULT_CIPHERS 0 needs Py_SSL_DEFAULT_CIPHER_STRING" |
| 154 | #endif |
Christian Heimes | e983252 | 2021-05-01 20:53:10 +0200 | [diff] [blame] | 155 | #ifndef PY_SSL_MIN_PROTOCOL |
| 156 | #define PY_SSL_MIN_PROTOCOL TLS1_2_VERSION |
| 157 | #endif |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 158 | #elif PY_SSL_DEFAULT_CIPHERS == 1 |
Stéphane Wirtel | 07fbbfd | 2018-10-05 16:17:18 +0200 | [diff] [blame] | 159 | /* Python custom selection of sensible cipher suites |
Christian Heimes | e983252 | 2021-05-01 20:53:10 +0200 | [diff] [blame] | 160 | * @SECLEVEL=2: security level 2 with 112 bits minimum security (e.g. 2048 bits RSA key) |
| 161 | * ECDH+*: enable ephemeral elliptic curve Diffie-Hellman |
| 162 | * DHE+*: fallback to ephemeral finite field Diffie-Hellman |
| 163 | * encryption order: AES AEAD (GCM), ChaCha AEAD, AES CBC |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 164 | * !aNULL:!eNULL: really no NULL ciphers |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 165 | * !aDSS: no authentication with discrete logarithm DSA algorithm |
Christian Heimes | e983252 | 2021-05-01 20:53:10 +0200 | [diff] [blame] | 166 | * !SHA1: no weak SHA1 MAC |
| 167 | * !AESCCM: no CCM mode, it's uncommon and slow |
| 168 | * |
| 169 | * Based on Hynek's excellent blog post (update 2021-02-11) |
| 170 | * https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/ |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 171 | */ |
Christian Heimes | e983252 | 2021-05-01 20:53:10 +0200 | [diff] [blame] | 172 | #define PY_SSL_DEFAULT_CIPHER_STRING "@SECLEVEL=2:ECDH+AESGCM:ECDH+CHACHA20:ECDH+AES:DHE+AES:!aNULL:!eNULL:!aDSS:!SHA1:!AESCCM" |
| 173 | #ifndef PY_SSL_MIN_PROTOCOL |
| 174 | #define PY_SSL_MIN_PROTOCOL TLS1_2_VERSION |
| 175 | #endif |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 176 | #elif PY_SSL_DEFAULT_CIPHERS == 2 |
| 177 | /* Ignored in SSLContext constructor, only used to as _ssl.DEFAULT_CIPHER_STRING */ |
| 178 | #define PY_SSL_DEFAULT_CIPHER_STRING SSL_DEFAULT_CIPHER_LIST |
| 179 | #else |
| 180 | #error "Unsupported PY_SSL_DEFAULT_CIPHERS" |
| 181 | #endif |
| 182 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 183 | |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 184 | enum py_ssl_error { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 185 | /* these mirror ssl.h */ |
| 186 | PY_SSL_ERROR_NONE, |
| 187 | PY_SSL_ERROR_SSL, |
| 188 | PY_SSL_ERROR_WANT_READ, |
| 189 | PY_SSL_ERROR_WANT_WRITE, |
| 190 | PY_SSL_ERROR_WANT_X509_LOOKUP, |
| 191 | PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */ |
| 192 | PY_SSL_ERROR_ZERO_RETURN, |
| 193 | PY_SSL_ERROR_WANT_CONNECT, |
| 194 | /* start of non ssl.h errorcodes */ |
| 195 | PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */ |
| 196 | PY_SSL_ERROR_NO_SOCKET, /* socket has been GC'd */ |
| 197 | PY_SSL_ERROR_INVALID_ERROR_CODE |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 198 | }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 199 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 200 | enum py_ssl_server_or_client { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 201 | PY_SSL_CLIENT, |
| 202 | PY_SSL_SERVER |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | enum py_ssl_cert_requirements { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 206 | PY_SSL_CERT_NONE, |
| 207 | PY_SSL_CERT_OPTIONAL, |
| 208 | PY_SSL_CERT_REQUIRED |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | enum py_ssl_version { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 212 | PY_SSL_VERSION_SSL2, |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 213 | PY_SSL_VERSION_SSL3=1, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 214 | PY_SSL_VERSION_TLS, /* SSLv23 */ |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 215 | PY_SSL_VERSION_TLS1, |
| 216 | PY_SSL_VERSION_TLS1_1, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 217 | PY_SSL_VERSION_TLS1_2, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 218 | PY_SSL_VERSION_TLS_CLIENT=0x10, |
| 219 | PY_SSL_VERSION_TLS_SERVER, |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 220 | }; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 221 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 222 | enum py_proto_version { |
| 223 | PY_PROTO_MINIMUM_SUPPORTED = -2, |
| 224 | PY_PROTO_SSLv3 = SSL3_VERSION, |
| 225 | PY_PROTO_TLSv1 = TLS1_VERSION, |
| 226 | PY_PROTO_TLSv1_1 = TLS1_1_VERSION, |
| 227 | PY_PROTO_TLSv1_2 = TLS1_2_VERSION, |
| 228 | #ifdef TLS1_3_VERSION |
| 229 | PY_PROTO_TLSv1_3 = TLS1_3_VERSION, |
| 230 | #else |
| 231 | PY_PROTO_TLSv1_3 = 0x304, |
| 232 | #endif |
| 233 | PY_PROTO_MAXIMUM_SUPPORTED = -1, |
| 234 | |
| 235 | /* OpenSSL has no dedicated API to set the minimum version to the maximum |
| 236 | * available version, and the other way around. We have to figure out the |
| 237 | * minimum and maximum available version on our own and hope for the best. |
| 238 | */ |
| 239 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 240 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_SSLv3, |
| 241 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 242 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1, |
| 243 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 244 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_1, |
| 245 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 246 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_2, |
| 247 | #elif defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 248 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_3, |
| 249 | #else |
| 250 | #error "PY_PROTO_MINIMUM_AVAILABLE not found" |
| 251 | #endif |
| 252 | |
| 253 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 254 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_3, |
| 255 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 256 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_2, |
| 257 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 258 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_1, |
| 259 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 260 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1, |
| 261 | #elif defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 262 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_SSLv3, |
| 263 | #else |
| 264 | #error "PY_PROTO_MAXIMUM_AVAILABLE not found" |
| 265 | #endif |
| 266 | }; |
| 267 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 268 | /* SSL socket object */ |
| 269 | |
| 270 | #define X509_NAME_MAXLEN 256 |
| 271 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 272 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 273 | /* In case of 'tls-unique' it will be 12 bytes for TLS, 36 bytes for |
| 274 | * older SSL, but let's be safe */ |
| 275 | #define PySSL_CB_MAXLEN 128 |
| 276 | |
Antoine Pitrou | a9bf2ac | 2012-02-17 18:47:54 +0100 | [diff] [blame] | 277 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 278 | typedef struct { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 279 | PyObject_HEAD |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 280 | SSL_CTX *ctx; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 281 | unsigned char *alpn_protocols; |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 282 | unsigned int alpn_protocols_len; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 283 | PyObject *set_sni_cb; |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 284 | int check_hostname; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 285 | /* OpenSSL has no API to get hostflags from X509_VERIFY_PARAM* struct. |
| 286 | * We have to maintain our own copy. OpenSSL's hostflags default to 0. |
| 287 | */ |
| 288 | unsigned int hostflags; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 289 | int protocol; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 290 | #ifdef TLS1_3_VERSION |
| 291 | int post_handshake_auth; |
| 292 | #endif |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 293 | PyObject *msg_cb; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 294 | PyObject *keylog_filename; |
| 295 | BIO *keylog_bio; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 296 | /* Cached module state, also used in SSLSocket and SSLSession code. */ |
| 297 | _sslmodulestate *state; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 298 | } PySSLContext; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 299 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 300 | typedef struct { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 301 | int ssl; /* last seen error from SSL */ |
| 302 | int c; /* last seen error from libc */ |
| 303 | #ifdef MS_WINDOWS |
| 304 | int ws; /* last seen error from winsock */ |
| 305 | #endif |
| 306 | } _PySSLError; |
| 307 | |
| 308 | typedef struct { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 309 | PyObject_HEAD |
| 310 | PyObject *Socket; /* weakref to socket on which we're layered */ |
| 311 | SSL *ssl; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 312 | PySSLContext *ctx; /* weakref to SSL context */ |
Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 313 | char shutdown_seen_zero; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 314 | enum py_ssl_server_or_client socket_type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 315 | PyObject *owner; /* Python level "owner" passed to servername callback */ |
| 316 | PyObject *server_hostname; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 317 | _PySSLError err; /* last seen error from various sources */ |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 318 | /* Some SSL callbacks don't have error reporting. Callback wrappers |
| 319 | * store exception information on the socket. The handshake, read, write, |
| 320 | * and shutdown methods check for chained exceptions. |
| 321 | */ |
| 322 | PyObject *exc_type; |
| 323 | PyObject *exc_value; |
| 324 | PyObject *exc_tb; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 325 | } PySSLSocket; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 326 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 327 | typedef struct { |
| 328 | PyObject_HEAD |
| 329 | BIO *bio; |
| 330 | int eof_written; |
| 331 | } PySSLMemoryBIO; |
| 332 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 333 | typedef struct { |
| 334 | PyObject_HEAD |
| 335 | SSL_SESSION *session; |
| 336 | PySSLContext *ctx; |
| 337 | } PySSLSession; |
| 338 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 339 | static inline _PySSLError _PySSL_errno(int failed, const SSL *ssl, int retcode) |
| 340 | { |
| 341 | _PySSLError err = { 0 }; |
| 342 | if (failed) { |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 343 | #ifdef MS_WINDOWS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 344 | err.ws = WSAGetLastError(); |
| 345 | _PySSL_FIX_ERRNO; |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 346 | #endif |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 347 | err.c = errno; |
| 348 | err.ssl = SSL_get_error(ssl, retcode); |
| 349 | } |
| 350 | return err; |
| 351 | } |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 352 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 353 | /*[clinic input] |
| 354 | module _ssl |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 355 | class _ssl._SSLContext "PySSLContext *" "get_state_type(type)->PySSLContext_Type" |
| 356 | class _ssl._SSLSocket "PySSLSocket *" "get_state_type(type)->PySSLSocket_Type" |
| 357 | class _ssl.MemoryBIO "PySSLMemoryBIO *" "get_state_type(type)->PySSLMemoryBIO_Type" |
| 358 | class _ssl.SSLSession "PySSLSession *" "get_state_type(type)->PySSLSession_Type" |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 359 | [clinic start generated code]*/ |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 360 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=d293bed8bae240fd]*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 361 | |
| 362 | #include "clinic/_ssl.c.h" |
| 363 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 364 | static int PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 365 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 366 | static int PySSL_set_owner(PySSLSocket *, PyObject *, void *); |
| 367 | static int PySSL_set_session(PySSLSocket *, PyObject *, void *); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 368 | |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 369 | typedef enum { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 370 | SOCKET_IS_NONBLOCKING, |
| 371 | SOCKET_IS_BLOCKING, |
| 372 | SOCKET_HAS_TIMED_OUT, |
| 373 | SOCKET_HAS_BEEN_CLOSED, |
| 374 | SOCKET_TOO_LARGE_FOR_SELECT, |
| 375 | SOCKET_OPERATION_OK |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 376 | } timeout_state; |
| 377 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 378 | /* Wrap error strings with filename and line # */ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 379 | #define ERRSTR1(x,y,z) (x ":" y ": " z) |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 380 | #define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 381 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 382 | /* Get the socket from a PySSLSocket, if it has one */ |
| 383 | #define GET_SOCKET(obj) ((obj)->Socket ? \ |
| 384 | (PySocketSockObject *) PyWeakref_GetObject((obj)->Socket) : NULL) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 385 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 386 | /* If sock is NULL, use a timeout of 0 second */ |
| 387 | #define GET_SOCKET_TIMEOUT(sock) \ |
| 388 | ((sock != NULL) ? (sock)->sock_timeout : 0) |
| 389 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 390 | #include "_ssl/debughelpers.c" |
| 391 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 392 | /* |
| 393 | * SSL errors. |
| 394 | */ |
| 395 | |
| 396 | PyDoc_STRVAR(SSLError_doc, |
| 397 | "An error occurred in the SSL implementation."); |
| 398 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 399 | PyDoc_STRVAR(SSLCertVerificationError_doc, |
| 400 | "A certificate could not be verified."); |
| 401 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 402 | PyDoc_STRVAR(SSLZeroReturnError_doc, |
| 403 | "SSL/TLS session closed cleanly."); |
| 404 | |
| 405 | PyDoc_STRVAR(SSLWantReadError_doc, |
| 406 | "Non-blocking SSL socket needs to read more data\n" |
| 407 | "before the requested operation can be completed."); |
| 408 | |
| 409 | PyDoc_STRVAR(SSLWantWriteError_doc, |
| 410 | "Non-blocking SSL socket needs to write more data\n" |
| 411 | "before the requested operation can be completed."); |
| 412 | |
| 413 | PyDoc_STRVAR(SSLSyscallError_doc, |
| 414 | "System error when attempting SSL operation."); |
| 415 | |
| 416 | PyDoc_STRVAR(SSLEOFError_doc, |
| 417 | "SSL/TLS connection terminated abruptly."); |
| 418 | |
| 419 | static PyObject * |
| 420 | SSLError_str(PyOSErrorObject *self) |
| 421 | { |
| 422 | if (self->strerror != NULL && PyUnicode_Check(self->strerror)) { |
| 423 | Py_INCREF(self->strerror); |
| 424 | return self->strerror; |
| 425 | } |
| 426 | else |
| 427 | return PyObject_Str(self->args); |
| 428 | } |
| 429 | |
| 430 | static PyType_Slot sslerror_type_slots[] = { |
Inada Naoki | 926b0cb | 2019-04-17 08:39:46 +0900 | [diff] [blame] | 431 | {Py_tp_doc, (void*)SSLError_doc}, |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 432 | {Py_tp_str, SSLError_str}, |
| 433 | {0, 0}, |
| 434 | }; |
| 435 | |
| 436 | static PyType_Spec sslerror_type_spec = { |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 437 | .name = "ssl.SSLError", |
| 438 | .basicsize = sizeof(PyOSErrorObject), |
| 439 | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 440 | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC), |
| 441 | .slots = sslerror_type_slots |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 442 | }; |
| 443 | |
| 444 | static void |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 445 | fill_and_set_sslerror(_sslmodulestate *state, |
| 446 | PySSLSocket *sslsock, PyObject *type, int ssl_errno, |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 447 | const char *errstr, int lineno, unsigned long errcode) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 448 | { |
| 449 | PyObject *err_value = NULL, *reason_obj = NULL, *lib_obj = NULL; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 450 | PyObject *verify_obj = NULL, *verify_code_obj = NULL; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 451 | PyObject *init_value, *msg, *key; |
| 452 | _Py_IDENTIFIER(reason); |
| 453 | _Py_IDENTIFIER(library); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 454 | _Py_IDENTIFIER(verify_message); |
| 455 | _Py_IDENTIFIER(verify_code); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 456 | |
| 457 | if (errcode != 0) { |
| 458 | int lib, reason; |
| 459 | |
| 460 | lib = ERR_GET_LIB(errcode); |
| 461 | reason = ERR_GET_REASON(errcode); |
| 462 | key = Py_BuildValue("ii", lib, reason); |
| 463 | if (key == NULL) |
| 464 | goto fail; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 465 | reason_obj = PyDict_GetItemWithError(state->err_codes_to_names, key); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 466 | Py_DECREF(key); |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 467 | if (reason_obj == NULL && PyErr_Occurred()) { |
| 468 | goto fail; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 469 | } |
| 470 | key = PyLong_FromLong(lib); |
| 471 | if (key == NULL) |
| 472 | goto fail; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 473 | lib_obj = PyDict_GetItemWithError(state->lib_codes_to_names, key); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 474 | Py_DECREF(key); |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 475 | if (lib_obj == NULL && PyErr_Occurred()) { |
| 476 | goto fail; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 477 | } |
| 478 | if (errstr == NULL) |
| 479 | errstr = ERR_reason_error_string(errcode); |
| 480 | } |
| 481 | if (errstr == NULL) |
| 482 | errstr = "unknown error"; |
| 483 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 484 | /* verify code for cert validation error */ |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 485 | if ((sslsock != NULL) && (type == state->PySSLCertVerificationErrorObject)) { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 486 | const char *verify_str = NULL; |
| 487 | long verify_code; |
| 488 | |
| 489 | verify_code = SSL_get_verify_result(sslsock->ssl); |
| 490 | verify_code_obj = PyLong_FromLong(verify_code); |
| 491 | if (verify_code_obj == NULL) { |
| 492 | goto fail; |
| 493 | } |
| 494 | |
| 495 | switch (verify_code) { |
| 496 | case X509_V_ERR_HOSTNAME_MISMATCH: |
| 497 | verify_obj = PyUnicode_FromFormat( |
| 498 | "Hostname mismatch, certificate is not valid for '%S'.", |
| 499 | sslsock->server_hostname |
| 500 | ); |
| 501 | break; |
| 502 | case X509_V_ERR_IP_ADDRESS_MISMATCH: |
| 503 | verify_obj = PyUnicode_FromFormat( |
| 504 | "IP address mismatch, certificate is not valid for '%S'.", |
| 505 | sslsock->server_hostname |
| 506 | ); |
| 507 | break; |
| 508 | default: |
| 509 | verify_str = X509_verify_cert_error_string(verify_code); |
| 510 | if (verify_str != NULL) { |
| 511 | verify_obj = PyUnicode_FromString(verify_str); |
| 512 | } else { |
| 513 | verify_obj = Py_None; |
| 514 | Py_INCREF(verify_obj); |
| 515 | } |
| 516 | break; |
| 517 | } |
| 518 | if (verify_obj == NULL) { |
| 519 | goto fail; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | if (verify_obj && reason_obj && lib_obj) |
| 524 | msg = PyUnicode_FromFormat("[%S: %S] %s: %S (_ssl.c:%d)", |
| 525 | lib_obj, reason_obj, errstr, verify_obj, |
| 526 | lineno); |
| 527 | else if (reason_obj && lib_obj) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 528 | msg = PyUnicode_FromFormat("[%S: %S] %s (_ssl.c:%d)", |
| 529 | lib_obj, reason_obj, errstr, lineno); |
| 530 | else if (lib_obj) |
| 531 | msg = PyUnicode_FromFormat("[%S] %s (_ssl.c:%d)", |
| 532 | lib_obj, errstr, lineno); |
| 533 | else |
| 534 | msg = PyUnicode_FromFormat("%s (_ssl.c:%d)", errstr, lineno); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 535 | if (msg == NULL) |
| 536 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 537 | |
Paul Monson | fb7e750 | 2019-05-15 15:38:55 -0700 | [diff] [blame] | 538 | init_value = Py_BuildValue("iN", ERR_GET_REASON(ssl_errno), msg); |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 539 | if (init_value == NULL) |
| 540 | goto fail; |
| 541 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 542 | err_value = PyObject_CallObject(type, init_value); |
| 543 | Py_DECREF(init_value); |
| 544 | if (err_value == NULL) |
| 545 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 546 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 547 | if (reason_obj == NULL) |
| 548 | reason_obj = Py_None; |
| 549 | if (_PyObject_SetAttrId(err_value, &PyId_reason, reason_obj)) |
| 550 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 551 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 552 | if (lib_obj == NULL) |
| 553 | lib_obj = Py_None; |
| 554 | if (_PyObject_SetAttrId(err_value, &PyId_library, lib_obj)) |
| 555 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 556 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 557 | if ((sslsock != NULL) && (type == state->PySSLCertVerificationErrorObject)) { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 558 | /* Only set verify code / message for SSLCertVerificationError */ |
| 559 | if (_PyObject_SetAttrId(err_value, &PyId_verify_code, |
| 560 | verify_code_obj)) |
| 561 | goto fail; |
| 562 | if (_PyObject_SetAttrId(err_value, &PyId_verify_message, verify_obj)) |
| 563 | goto fail; |
| 564 | } |
| 565 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 566 | PyErr_SetObject(type, err_value); |
| 567 | fail: |
| 568 | Py_XDECREF(err_value); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 569 | Py_XDECREF(verify_code_obj); |
| 570 | Py_XDECREF(verify_obj); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 571 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 572 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 573 | static int |
| 574 | PySSL_ChainExceptions(PySSLSocket *sslsock) { |
| 575 | if (sslsock->exc_type == NULL) |
| 576 | return 0; |
| 577 | |
| 578 | _PyErr_ChainExceptions(sslsock->exc_type, sslsock->exc_value, sslsock->exc_tb); |
| 579 | sslsock->exc_type = NULL; |
| 580 | sslsock->exc_value = NULL; |
| 581 | sslsock->exc_tb = NULL; |
| 582 | return -1; |
| 583 | } |
| 584 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 585 | static PyObject * |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 586 | PySSL_SetError(PySSLSocket *sslsock, int ret, const char *filename, int lineno) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 587 | { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 588 | PyObject *type; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 589 | char *errstr = NULL; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 590 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 591 | enum py_ssl_error p = PY_SSL_ERROR_NONE; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 592 | unsigned long e = 0; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 593 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 594 | assert(sslsock != NULL); |
| 595 | |
| 596 | _sslmodulestate *state = get_state_sock(sslsock); |
| 597 | type = state->PySSLErrorObject; |
| 598 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 599 | assert(ret <= 0); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 600 | e = ERR_peek_last_error(); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 601 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 602 | if (sslsock->ssl != NULL) { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 603 | err = sslsock->err; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 604 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 605 | switch (err.ssl) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 606 | case SSL_ERROR_ZERO_RETURN: |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 607 | errstr = "TLS/SSL connection has been closed (EOF)"; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 608 | type = state->PySSLZeroReturnErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 609 | p = PY_SSL_ERROR_ZERO_RETURN; |
| 610 | break; |
| 611 | case SSL_ERROR_WANT_READ: |
| 612 | errstr = "The operation did not complete (read)"; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 613 | type = state->PySSLWantReadErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 614 | p = PY_SSL_ERROR_WANT_READ; |
| 615 | break; |
| 616 | case SSL_ERROR_WANT_WRITE: |
| 617 | p = PY_SSL_ERROR_WANT_WRITE; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 618 | type = state->PySSLWantWriteErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 619 | errstr = "The operation did not complete (write)"; |
| 620 | break; |
| 621 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 622 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 623 | errstr = "The operation did not complete (X509 lookup)"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 624 | break; |
| 625 | case SSL_ERROR_WANT_CONNECT: |
| 626 | p = PY_SSL_ERROR_WANT_CONNECT; |
| 627 | errstr = "The operation did not complete (connect)"; |
| 628 | break; |
| 629 | case SSL_ERROR_SYSCALL: |
| 630 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 631 | if (e == 0) { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 632 | PySocketSockObject *s = GET_SOCKET(sslsock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 633 | if (ret == 0 || (((PyObject *)s) == Py_None)) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 634 | p = PY_SSL_ERROR_EOF; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 635 | type = state->PySSLEOFErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 636 | errstr = "EOF occurred in violation of protocol"; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 637 | } else if (s && ret == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 638 | /* underlying BIO reported an I/O error */ |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 639 | ERR_clear_error(); |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 640 | #ifdef MS_WINDOWS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 641 | if (err.ws) { |
| 642 | return PyErr_SetFromWindowsErr(err.ws); |
| 643 | } |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 644 | #endif |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 645 | if (err.c) { |
| 646 | errno = err.c; |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 647 | return PyErr_SetFromErrno(PyExc_OSError); |
| 648 | } |
Dima Tisnek | 495bd03 | 2020-08-16 02:01:19 +0900 | [diff] [blame] | 649 | else { |
| 650 | p = PY_SSL_ERROR_EOF; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 651 | type = state->PySSLEOFErrorObject; |
Dima Tisnek | 495bd03 | 2020-08-16 02:01:19 +0900 | [diff] [blame] | 652 | errstr = "EOF occurred in violation of protocol"; |
| 653 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 654 | } else { /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 655 | p = PY_SSL_ERROR_SYSCALL; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 656 | type = state->PySSLSyscallErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 657 | errstr = "Some I/O error occurred"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 658 | } |
| 659 | } else { |
| 660 | p = PY_SSL_ERROR_SYSCALL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 661 | } |
| 662 | break; |
| 663 | } |
| 664 | case SSL_ERROR_SSL: |
| 665 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 666 | p = PY_SSL_ERROR_SSL; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 667 | if (e == 0) { |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 668 | /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 669 | errstr = "A failure in the SSL library occurred"; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 670 | } |
| 671 | if (ERR_GET_LIB(e) == ERR_LIB_SSL && |
| 672 | ERR_GET_REASON(e) == SSL_R_CERTIFICATE_VERIFY_FAILED) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 673 | type = state->PySSLCertVerificationErrorObject; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 674 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 675 | break; |
| 676 | } |
| 677 | default: |
| 678 | p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
| 679 | errstr = "Invalid error code"; |
| 680 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 681 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 682 | fill_and_set_sslerror(state, sslsock, type, p, errstr, lineno, e); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 683 | ERR_clear_error(); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 684 | PySSL_ChainExceptions(sslsock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 685 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 688 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 689 | _setSSLError (_sslmodulestate *state, const char *errstr, int errcode, const char *filename, int lineno) |
| 690 | { |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 691 | if (errstr == NULL) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 692 | errcode = ERR_peek_last_error(); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 693 | else |
| 694 | errcode = 0; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 695 | fill_and_set_sslerror(state, NULL, state->PySSLErrorObject, errcode, errstr, lineno, errcode); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 696 | ERR_clear_error(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 697 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 700 | static int |
| 701 | _ssl_deprecated(const char* name, int stacklevel) { |
| 702 | return PyErr_WarnFormat( |
| 703 | PyExc_DeprecationWarning, stacklevel, |
| 704 | "ssl module: %s is deprecated", name |
| 705 | ); |
| 706 | } |
| 707 | |
| 708 | #define PY_SSL_DEPRECATED(name, stacklevel, ret) \ |
| 709 | if (_ssl_deprecated((name), (stacklevel)) == -1) return (ret) |
| 710 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 711 | /* |
| 712 | * SSL objects |
| 713 | */ |
| 714 | |
| 715 | static int |
| 716 | _ssl_configure_hostname(PySSLSocket *self, const char* server_hostname) |
| 717 | { |
| 718 | int retval = -1; |
| 719 | ASN1_OCTET_STRING *ip; |
| 720 | PyObject *hostname; |
| 721 | size_t len; |
| 722 | |
| 723 | assert(server_hostname); |
| 724 | |
| 725 | /* Disable OpenSSL's special mode with leading dot in hostname: |
| 726 | * When name starts with a dot (e.g ".example.com"), it will be |
| 727 | * matched by a certificate valid for any sub-domain of name. |
| 728 | */ |
| 729 | len = strlen(server_hostname); |
| 730 | if (len == 0 || *server_hostname == '.') { |
| 731 | PyErr_SetString( |
| 732 | PyExc_ValueError, |
| 733 | "server_hostname cannot be an empty string or start with a " |
| 734 | "leading dot."); |
| 735 | return retval; |
| 736 | } |
| 737 | |
| 738 | /* inet_pton is not available on all platforms. */ |
| 739 | ip = a2i_IPADDRESS(server_hostname); |
| 740 | if (ip == NULL) { |
| 741 | ERR_clear_error(); |
| 742 | } |
| 743 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 744 | hostname = PyUnicode_Decode(server_hostname, len, "ascii", "strict"); |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 745 | if (hostname == NULL) { |
| 746 | goto error; |
| 747 | } |
| 748 | self->server_hostname = hostname; |
| 749 | |
| 750 | /* Only send SNI extension for non-IP hostnames */ |
| 751 | if (ip == NULL) { |
| 752 | if (!SSL_set_tlsext_host_name(self->ssl, server_hostname)) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 753 | _setSSLError(get_state_sock(self), NULL, 0, __FILE__, __LINE__); |
Zackery Spytz | c32f297 | 2020-10-25 12:02:30 -0600 | [diff] [blame] | 754 | goto error; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | if (self->ctx->check_hostname) { |
| 758 | X509_VERIFY_PARAM *param = SSL_get0_param(self->ssl); |
| 759 | if (ip == NULL) { |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 760 | if (!X509_VERIFY_PARAM_set1_host(param, server_hostname, |
| 761 | strlen(server_hostname))) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 762 | _setSSLError(get_state_sock(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 763 | goto error; |
| 764 | } |
| 765 | } else { |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 766 | if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_get0_data(ip), |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 767 | ASN1_STRING_length(ip))) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 768 | _setSSLError(get_state_sock(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 769 | goto error; |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | retval = 0; |
| 774 | error: |
| 775 | if (ip != NULL) { |
| 776 | ASN1_OCTET_STRING_free(ip); |
| 777 | } |
| 778 | return retval; |
| 779 | } |
| 780 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 781 | static PySSLSocket * |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 782 | newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 783 | enum py_ssl_server_or_client socket_type, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 784 | char *server_hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 785 | PyObject *owner, PyObject *session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 786 | PySSLMemoryBIO *inbio, PySSLMemoryBIO *outbio) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 787 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 788 | PySSLSocket *self; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 789 | SSL_CTX *ctx = sslctx->ctx; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 790 | _PySSLError err = { 0 }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 791 | |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 792 | self = PyObject_GC_New(PySSLSocket, |
| 793 | get_state_ctx(sslctx)->PySSLSocket_Type); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 794 | if (self == NULL) |
| 795 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 796 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 797 | self->ssl = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 798 | self->Socket = NULL; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 799 | self->ctx = sslctx; |
Nathaniel J. Smith | 65ece7c | 2017-06-07 23:30:43 -0700 | [diff] [blame] | 800 | Py_INCREF(sslctx); |
Antoine Pitrou | 860aee7 | 2013-09-29 19:52:45 +0200 | [diff] [blame] | 801 | self->shutdown_seen_zero = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 802 | self->owner = NULL; |
Benjamin Peterson | 81b9ecd | 2016-08-15 21:55:37 -0700 | [diff] [blame] | 803 | self->server_hostname = NULL; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 804 | self->err = err; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 805 | self->exc_type = NULL; |
| 806 | self->exc_value = NULL; |
| 807 | self->exc_tb = NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 808 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 809 | /* Make sure the SSL error state is initialized */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 810 | ERR_clear_error(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 811 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 812 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 813 | self->ssl = SSL_new(ctx); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 814 | PySSL_END_ALLOW_THREADS |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 815 | if (self->ssl == NULL) { |
| 816 | Py_DECREF(self); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 817 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 818 | return NULL; |
| 819 | } |
Christian Heimes | b467d9a | 2021-04-17 10:07:19 +0200 | [diff] [blame] | 820 | /* bpo43522 and OpenSSL < 1.1.1l: copy hostflags manually */ |
| 821 | #if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION < 0x101010cf |
| 822 | X509_VERIFY_PARAM *ssl_params = SSL_get0_param(self->ssl); |
| 823 | X509_VERIFY_PARAM_set_hostflags(ssl_params, sslctx->hostflags); |
| 824 | #endif |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 825 | SSL_set_app_data(self->ssl, self); |
| 826 | if (sock) { |
| 827 | SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int)); |
| 828 | } else { |
| 829 | /* BIOs are reference counted and SSL_set_bio borrows our reference. |
| 830 | * To prevent a double free in memory_bio_dealloc() we need to take an |
| 831 | * extra reference here. */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 832 | BIO_up_ref(inbio->bio); |
| 833 | BIO_up_ref(outbio->bio); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 834 | SSL_set_bio(self->ssl, inbio->bio, outbio->bio); |
| 835 | } |
Alex Gaynor | f042242 | 2018-05-14 11:51:45 -0400 | [diff] [blame] | 836 | SSL_set_mode(self->ssl, |
| 837 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_AUTO_RETRY); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 838 | |
Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 839 | #ifdef TLS1_3_VERSION |
| 840 | if (sslctx->post_handshake_auth == 1) { |
| 841 | if (socket_type == PY_SSL_SERVER) { |
| 842 | /* bpo-37428: OpenSSL does not ignore SSL_VERIFY_POST_HANDSHAKE. |
| 843 | * Set SSL_VERIFY_POST_HANDSHAKE flag only for server sockets and |
| 844 | * only in combination with SSL_VERIFY_PEER flag. */ |
| 845 | int mode = SSL_get_verify_mode(self->ssl); |
| 846 | if (mode & SSL_VERIFY_PEER) { |
| 847 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 848 | verify_cb = SSL_get_verify_callback(self->ssl); |
| 849 | mode |= SSL_VERIFY_POST_HANDSHAKE; |
| 850 | SSL_set_verify(self->ssl, mode, verify_cb); |
| 851 | } |
| 852 | } else { |
| 853 | /* client socket */ |
| 854 | SSL_set_post_handshake_auth(self->ssl, 1); |
| 855 | } |
| 856 | } |
| 857 | #endif |
| 858 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 859 | if (server_hostname != NULL) { |
| 860 | if (_ssl_configure_hostname(self, server_hostname) < 0) { |
| 861 | Py_DECREF(self); |
| 862 | return NULL; |
| 863 | } |
| 864 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 865 | /* If the socket is in non-blocking mode or timeout mode, set the BIO |
| 866 | * to non-blocking mode (blocking is the default) |
| 867 | */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 868 | if (sock && sock->sock_timeout >= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 869 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 870 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 871 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 872 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 873 | PySSL_BEGIN_ALLOW_THREADS |
| 874 | if (socket_type == PY_SSL_CLIENT) |
| 875 | SSL_set_connect_state(self->ssl); |
| 876 | else |
| 877 | SSL_set_accept_state(self->ssl); |
| 878 | PySSL_END_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 880 | self->socket_type = socket_type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 881 | if (sock != NULL) { |
| 882 | self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL); |
| 883 | if (self->Socket == NULL) { |
| 884 | Py_DECREF(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 885 | return NULL; |
| 886 | } |
Victor Stinner | a9eb38f | 2013-10-31 16:35:38 +0100 | [diff] [blame] | 887 | } |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 888 | if (owner && owner != Py_None) { |
| 889 | if (PySSL_set_owner(self, owner, NULL) == -1) { |
| 890 | Py_DECREF(self); |
| 891 | return NULL; |
| 892 | } |
| 893 | } |
| 894 | if (session && session != Py_None) { |
| 895 | if (PySSL_set_session(self, session, NULL) == -1) { |
| 896 | Py_DECREF(self); |
| 897 | return NULL; |
| 898 | } |
| 899 | } |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 900 | |
| 901 | PyObject_GC_Track(self); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 902 | return self; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 905 | /* SSL object methods */ |
| 906 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 907 | /*[clinic input] |
| 908 | _ssl._SSLSocket.do_handshake |
| 909 | [clinic start generated code]*/ |
| 910 | |
| 911 | static PyObject * |
| 912 | _ssl__SSLSocket_do_handshake_impl(PySSLSocket *self) |
| 913 | /*[clinic end generated code: output=6c0898a8936548f6 input=d2d737de3df018c8]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 914 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 915 | int ret; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 916 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 917 | int sockstate, nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 918 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 919 | _PyTime_t timeout, deadline = 0; |
| 920 | int has_timeout; |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 921 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 922 | if (sock) { |
| 923 | if (((PyObject*)sock) == Py_None) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 924 | _setSSLError(get_state_sock(self), |
| 925 | "Underlying socket connection gone", |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 926 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 927 | return NULL; |
| 928 | } |
| 929 | Py_INCREF(sock); |
| 930 | |
| 931 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 932 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 933 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 934 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 935 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 936 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 937 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 938 | has_timeout = (timeout > 0); |
| 939 | if (has_timeout) |
| 940 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 941 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 942 | /* Actually negotiate SSL connection */ |
| 943 | /* XXX If SSL_do_handshake() returns 0, it's also a failure. */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 944 | do { |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 945 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 946 | ret = SSL_do_handshake(self->ssl); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 947 | err = _PySSL_errno(ret < 1, self->ssl, ret); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 948 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 949 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 950 | |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 951 | if (PyErr_CheckSignals()) |
| 952 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 953 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 954 | if (has_timeout) |
| 955 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 956 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 957 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 958 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 959 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 960 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 961 | } else { |
| 962 | sockstate = SOCKET_OPERATION_OK; |
| 963 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 964 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 965 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 966 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 967 | ERRSTR("The handshake operation timed out")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 968 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 969 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 970 | PyErr_SetString(get_state_sock(self)->PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 971 | ERRSTR("Underlying socket has been closed.")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 972 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 973 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 974 | PyErr_SetString(get_state_sock(self)->PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 975 | ERRSTR("Underlying socket too large for select().")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 976 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 977 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 978 | break; |
| 979 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 980 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 981 | err.ssl == SSL_ERROR_WANT_WRITE); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 982 | Py_XDECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 983 | if (ret < 1) |
| 984 | return PySSL_SetError(self, ret, __FILE__, __LINE__); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 985 | if (PySSL_ChainExceptions(self) < 0) |
| 986 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 987 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 988 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 989 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 990 | PySSL_ChainExceptions(self); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 991 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 994 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 995 | _asn1obj2py(_sslmodulestate *state, const ASN1_OBJECT *name, int no_name) |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 996 | { |
| 997 | char buf[X509_NAME_MAXLEN]; |
| 998 | char *namebuf = buf; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 999 | int buflen; |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1000 | PyObject *name_obj = NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1001 | |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1002 | buflen = OBJ_obj2txt(namebuf, X509_NAME_MAXLEN, name, no_name); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1003 | if (buflen < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1004 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1005 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1006 | } |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1007 | /* initial buffer is too small for oid + terminating null byte */ |
| 1008 | if (buflen > X509_NAME_MAXLEN - 1) { |
| 1009 | /* make OBJ_obj2txt() calculate the required buflen */ |
| 1010 | buflen = OBJ_obj2txt(NULL, 0, name, no_name); |
| 1011 | /* allocate len + 1 for terminating NULL byte */ |
| 1012 | namebuf = PyMem_Malloc(buflen + 1); |
| 1013 | if (namebuf == NULL) { |
| 1014 | PyErr_NoMemory(); |
| 1015 | return NULL; |
| 1016 | } |
| 1017 | buflen = OBJ_obj2txt(namebuf, buflen + 1, name, no_name); |
| 1018 | if (buflen < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1019 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1020 | goto done; |
| 1021 | } |
| 1022 | } |
| 1023 | if (!buflen && no_name) { |
| 1024 | Py_INCREF(Py_None); |
| 1025 | name_obj = Py_None; |
| 1026 | } |
| 1027 | else { |
| 1028 | name_obj = PyUnicode_FromStringAndSize(namebuf, buflen); |
| 1029 | } |
| 1030 | |
| 1031 | done: |
| 1032 | if (buf != namebuf) { |
| 1033 | PyMem_Free(namebuf); |
| 1034 | } |
| 1035 | return name_obj; |
| 1036 | } |
| 1037 | |
| 1038 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1039 | _create_tuple_for_attribute(_sslmodulestate *state, |
| 1040 | ASN1_OBJECT *name, ASN1_STRING *value) |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1041 | { |
| 1042 | Py_ssize_t buflen; |
| 1043 | unsigned char *valuebuf = NULL; |
| 1044 | PyObject *attr; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1045 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1046 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 1047 | if (buflen < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1048 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1049 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1050 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1051 | attr = Py_BuildValue("Ns#", _asn1obj2py(state, name, 0), valuebuf, buflen); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1052 | OPENSSL_free(valuebuf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1053 | return attr; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1057 | _create_tuple_for_X509_NAME (_sslmodulestate *state, X509_NAME *xname) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1058 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1059 | PyObject *dn = NULL; /* tuple which represents the "distinguished name" */ |
| 1060 | PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */ |
| 1061 | PyObject *rdnt; |
| 1062 | PyObject *attr = NULL; /* tuple to hold an attribute */ |
| 1063 | int entry_count = X509_NAME_entry_count(xname); |
| 1064 | X509_NAME_ENTRY *entry; |
| 1065 | ASN1_OBJECT *name; |
| 1066 | ASN1_STRING *value; |
| 1067 | int index_counter; |
| 1068 | int rdn_level = -1; |
| 1069 | int retcode; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1070 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1071 | dn = PyList_New(0); |
| 1072 | if (dn == NULL) |
| 1073 | return NULL; |
| 1074 | /* now create another tuple to hold the top-level RDN */ |
| 1075 | rdn = PyList_New(0); |
| 1076 | if (rdn == NULL) |
| 1077 | goto fail0; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1078 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1079 | for (index_counter = 0; |
| 1080 | index_counter < entry_count; |
| 1081 | index_counter++) |
| 1082 | { |
| 1083 | entry = X509_NAME_get_entry(xname, index_counter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1084 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1085 | /* check to see if we've gotten to a new RDN */ |
| 1086 | if (rdn_level >= 0) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1087 | if (rdn_level != X509_NAME_ENTRY_set(entry)) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1088 | /* yes, new RDN */ |
| 1089 | /* add old RDN to DN */ |
| 1090 | rdnt = PyList_AsTuple(rdn); |
| 1091 | Py_DECREF(rdn); |
| 1092 | if (rdnt == NULL) |
| 1093 | goto fail0; |
| 1094 | retcode = PyList_Append(dn, rdnt); |
| 1095 | Py_DECREF(rdnt); |
| 1096 | if (retcode < 0) |
| 1097 | goto fail0; |
| 1098 | /* create new RDN */ |
| 1099 | rdn = PyList_New(0); |
| 1100 | if (rdn == NULL) |
| 1101 | goto fail0; |
| 1102 | } |
| 1103 | } |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1104 | rdn_level = X509_NAME_ENTRY_set(entry); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1105 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1106 | /* now add this attribute to the current RDN */ |
| 1107 | name = X509_NAME_ENTRY_get_object(entry); |
| 1108 | value = X509_NAME_ENTRY_get_data(entry); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1109 | attr = _create_tuple_for_attribute(state, name, value); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1110 | /* |
| 1111 | fprintf(stderr, "RDN level %d, attribute %s: %s\n", |
| 1112 | entry->set, |
| 1113 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)), |
| 1114 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1))); |
| 1115 | */ |
| 1116 | if (attr == NULL) |
| 1117 | goto fail1; |
| 1118 | retcode = PyList_Append(rdn, attr); |
| 1119 | Py_DECREF(attr); |
| 1120 | if (retcode < 0) |
| 1121 | goto fail1; |
| 1122 | } |
| 1123 | /* now, there's typically a dangling RDN */ |
Antoine Pitrou | 2f5a163 | 2012-02-15 22:25:27 +0100 | [diff] [blame] | 1124 | if (rdn != NULL) { |
| 1125 | if (PyList_GET_SIZE(rdn) > 0) { |
| 1126 | rdnt = PyList_AsTuple(rdn); |
| 1127 | Py_DECREF(rdn); |
| 1128 | if (rdnt == NULL) |
| 1129 | goto fail0; |
| 1130 | retcode = PyList_Append(dn, rdnt); |
| 1131 | Py_DECREF(rdnt); |
| 1132 | if (retcode < 0) |
| 1133 | goto fail0; |
| 1134 | } |
| 1135 | else { |
| 1136 | Py_DECREF(rdn); |
| 1137 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1138 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1139 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1140 | /* convert list to tuple */ |
| 1141 | rdnt = PyList_AsTuple(dn); |
| 1142 | Py_DECREF(dn); |
| 1143 | if (rdnt == NULL) |
| 1144 | return NULL; |
| 1145 | return rdnt; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1146 | |
| 1147 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1148 | Py_XDECREF(rdn); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1149 | |
| 1150 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1151 | Py_XDECREF(dn); |
| 1152 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1156 | _get_peer_alt_names (_sslmodulestate *state, X509 *certificate) { |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1157 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1158 | /* this code follows the procedure outlined in |
| 1159 | OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print() |
| 1160 | function to extract the STACK_OF(GENERAL_NAME), |
| 1161 | then iterates through the stack to add the |
| 1162 | names. */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1163 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1164 | int j; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1165 | PyObject *peer_alt_names = Py_None; |
Christian Heimes | 60bf2fc | 2013-09-05 16:04:35 +0200 | [diff] [blame] | 1166 | PyObject *v = NULL, *t; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1167 | GENERAL_NAMES *names = NULL; |
| 1168 | GENERAL_NAME *name; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1169 | BIO *biobuf = NULL; |
| 1170 | char buf[2048]; |
| 1171 | char *vptr; |
| 1172 | int len; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1173 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1174 | if (certificate == NULL) |
| 1175 | return peer_alt_names; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1176 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1177 | /* get a memory buffer */ |
| 1178 | biobuf = BIO_new(BIO_s_mem()); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1179 | if (biobuf == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1180 | PyErr_SetString(state->PySSLErrorObject, "failed to allocate BIO"); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1181 | return NULL; |
| 1182 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1183 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1184 | names = (GENERAL_NAMES *)X509_get_ext_d2i( |
| 1185 | certificate, NID_subject_alt_name, NULL, NULL); |
| 1186 | if (names != NULL) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1187 | if (peer_alt_names == Py_None) { |
| 1188 | peer_alt_names = PyList_New(0); |
| 1189 | if (peer_alt_names == NULL) |
| 1190 | goto fail; |
| 1191 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1192 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1193 | for(j = 0; j < sk_GENERAL_NAME_num(names); j++) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1194 | /* get a rendering of each name in the set of names */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1195 | int gntype; |
| 1196 | ASN1_STRING *as = NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1197 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1198 | name = sk_GENERAL_NAME_value(names, j); |
Christian Heimes | 474afdd | 2013-08-17 17:18:56 +0200 | [diff] [blame] | 1199 | gntype = name->type; |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1200 | switch (gntype) { |
| 1201 | case GEN_DIRNAME: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1202 | /* we special-case DirName as a tuple of |
| 1203 | tuples of attributes */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1204 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1205 | t = PyTuple_New(2); |
| 1206 | if (t == NULL) { |
| 1207 | goto fail; |
| 1208 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1209 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1210 | v = PyUnicode_FromString("DirName"); |
| 1211 | if (v == NULL) { |
| 1212 | Py_DECREF(t); |
| 1213 | goto fail; |
| 1214 | } |
| 1215 | PyTuple_SET_ITEM(t, 0, v); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1216 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1217 | v = _create_tuple_for_X509_NAME(state, name->d.dirn); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1218 | if (v == NULL) { |
| 1219 | Py_DECREF(t); |
| 1220 | goto fail; |
| 1221 | } |
| 1222 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1223 | break; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1224 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1225 | case GEN_EMAIL: |
| 1226 | case GEN_DNS: |
| 1227 | case GEN_URI: |
| 1228 | /* GENERAL_NAME_print() doesn't handle NULL bytes in ASN1_string |
| 1229 | correctly, CVE-2013-4238 */ |
| 1230 | t = PyTuple_New(2); |
| 1231 | if (t == NULL) |
| 1232 | goto fail; |
| 1233 | switch (gntype) { |
| 1234 | case GEN_EMAIL: |
| 1235 | v = PyUnicode_FromString("email"); |
| 1236 | as = name->d.rfc822Name; |
| 1237 | break; |
| 1238 | case GEN_DNS: |
| 1239 | v = PyUnicode_FromString("DNS"); |
| 1240 | as = name->d.dNSName; |
| 1241 | break; |
| 1242 | case GEN_URI: |
| 1243 | v = PyUnicode_FromString("URI"); |
| 1244 | as = name->d.uniformResourceIdentifier; |
| 1245 | break; |
| 1246 | } |
| 1247 | if (v == NULL) { |
| 1248 | Py_DECREF(t); |
| 1249 | goto fail; |
| 1250 | } |
| 1251 | PyTuple_SET_ITEM(t, 0, v); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1252 | v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_get0_data(as), |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1253 | ASN1_STRING_length(as)); |
| 1254 | if (v == NULL) { |
| 1255 | Py_DECREF(t); |
| 1256 | goto fail; |
| 1257 | } |
| 1258 | PyTuple_SET_ITEM(t, 1, v); |
| 1259 | break; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1260 | |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1261 | case GEN_RID: |
| 1262 | t = PyTuple_New(2); |
| 1263 | if (t == NULL) |
| 1264 | goto fail; |
| 1265 | |
| 1266 | v = PyUnicode_FromString("Registered ID"); |
| 1267 | if (v == NULL) { |
| 1268 | Py_DECREF(t); |
| 1269 | goto fail; |
| 1270 | } |
| 1271 | PyTuple_SET_ITEM(t, 0, v); |
| 1272 | |
| 1273 | len = i2t_ASN1_OBJECT(buf, sizeof(buf)-1, name->d.rid); |
| 1274 | if (len < 0) { |
| 1275 | Py_DECREF(t); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1276 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1277 | goto fail; |
| 1278 | } else if (len >= (int)sizeof(buf)) { |
| 1279 | v = PyUnicode_FromString("<INVALID>"); |
| 1280 | } else { |
| 1281 | v = PyUnicode_FromStringAndSize(buf, len); |
| 1282 | } |
| 1283 | if (v == NULL) { |
| 1284 | Py_DECREF(t); |
| 1285 | goto fail; |
| 1286 | } |
| 1287 | PyTuple_SET_ITEM(t, 1, v); |
| 1288 | break; |
| 1289 | |
Christian Heimes | 2b7de66 | 2019-12-07 17:59:36 +0100 | [diff] [blame] | 1290 | case GEN_IPADD: |
| 1291 | /* OpenSSL < 3.0.0 adds a trailing \n to IPv6. 3.0.0 removed |
| 1292 | * the trailing newline. Remove it in all versions |
| 1293 | */ |
| 1294 | t = PyTuple_New(2); |
| 1295 | if (t == NULL) |
| 1296 | goto fail; |
| 1297 | |
| 1298 | v = PyUnicode_FromString("IP Address"); |
| 1299 | if (v == NULL) { |
| 1300 | Py_DECREF(t); |
| 1301 | goto fail; |
| 1302 | } |
| 1303 | PyTuple_SET_ITEM(t, 0, v); |
| 1304 | |
| 1305 | if (name->d.ip->length == 4) { |
| 1306 | unsigned char *p = name->d.ip->data; |
| 1307 | v = PyUnicode_FromFormat( |
| 1308 | "%d.%d.%d.%d", |
| 1309 | p[0], p[1], p[2], p[3] |
| 1310 | ); |
| 1311 | } else if (name->d.ip->length == 16) { |
| 1312 | /* PyUnicode_FromFormat() does not support %X */ |
| 1313 | unsigned char *p = name->d.ip->data; |
| 1314 | len = sprintf( |
| 1315 | buf, |
| 1316 | "%X:%X:%X:%X:%X:%X:%X:%X", |
| 1317 | p[0] << 8 | p[1], |
| 1318 | p[2] << 8 | p[3], |
| 1319 | p[4] << 8 | p[5], |
| 1320 | p[6] << 8 | p[7], |
| 1321 | p[8] << 8 | p[9], |
| 1322 | p[10] << 8 | p[11], |
| 1323 | p[12] << 8 | p[13], |
| 1324 | p[14] << 8 | p[15] |
| 1325 | ); |
| 1326 | v = PyUnicode_FromStringAndSize(buf, len); |
| 1327 | } else { |
| 1328 | v = PyUnicode_FromString("<invalid>"); |
| 1329 | } |
| 1330 | |
| 1331 | if (v == NULL) { |
| 1332 | Py_DECREF(t); |
| 1333 | goto fail; |
| 1334 | } |
| 1335 | PyTuple_SET_ITEM(t, 1, v); |
| 1336 | break; |
| 1337 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1338 | default: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1339 | /* for everything else, we use the OpenSSL print form */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1340 | switch (gntype) { |
| 1341 | /* check for new general name type */ |
| 1342 | case GEN_OTHERNAME: |
| 1343 | case GEN_X400: |
| 1344 | case GEN_EDIPARTY: |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1345 | case GEN_RID: |
| 1346 | break; |
| 1347 | default: |
| 1348 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1349 | "Unknown general name type %d", |
| 1350 | gntype) == -1) { |
| 1351 | goto fail; |
| 1352 | } |
| 1353 | break; |
| 1354 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1355 | (void) BIO_reset(biobuf); |
| 1356 | GENERAL_NAME_print(biobuf, name); |
| 1357 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1358 | if (len < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1359 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1360 | goto fail; |
| 1361 | } |
| 1362 | vptr = strchr(buf, ':'); |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1363 | if (vptr == NULL) { |
| 1364 | PyErr_Format(PyExc_ValueError, |
| 1365 | "Invalid value %.200s", |
| 1366 | buf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1367 | goto fail; |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1368 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1369 | t = PyTuple_New(2); |
| 1370 | if (t == NULL) |
| 1371 | goto fail; |
| 1372 | v = PyUnicode_FromStringAndSize(buf, (vptr - buf)); |
| 1373 | if (v == NULL) { |
| 1374 | Py_DECREF(t); |
| 1375 | goto fail; |
| 1376 | } |
| 1377 | PyTuple_SET_ITEM(t, 0, v); |
| 1378 | v = PyUnicode_FromStringAndSize((vptr + 1), |
| 1379 | (len - (vptr - buf + 1))); |
| 1380 | if (v == NULL) { |
| 1381 | Py_DECREF(t); |
| 1382 | goto fail; |
| 1383 | } |
| 1384 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1385 | break; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1386 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1387 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1388 | /* and add that rendering to the list */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1389 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1390 | if (PyList_Append(peer_alt_names, t) < 0) { |
| 1391 | Py_DECREF(t); |
| 1392 | goto fail; |
| 1393 | } |
| 1394 | Py_DECREF(t); |
| 1395 | } |
Antoine Pitrou | 116d6b9 | 2011-11-23 01:39:19 +0100 | [diff] [blame] | 1396 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1397 | } |
| 1398 | BIO_free(biobuf); |
| 1399 | if (peer_alt_names != Py_None) { |
| 1400 | v = PyList_AsTuple(peer_alt_names); |
| 1401 | Py_DECREF(peer_alt_names); |
| 1402 | return v; |
| 1403 | } else { |
| 1404 | return peer_alt_names; |
| 1405 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1406 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1407 | |
| 1408 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1409 | if (biobuf != NULL) |
| 1410 | BIO_free(biobuf); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1411 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1412 | if (peer_alt_names != Py_None) { |
| 1413 | Py_XDECREF(peer_alt_names); |
| 1414 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1415 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1416 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | static PyObject * |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1420 | _get_aia_uri(X509 *certificate, int nid) { |
| 1421 | PyObject *lst = NULL, *ostr = NULL; |
| 1422 | int i, result; |
| 1423 | AUTHORITY_INFO_ACCESS *info; |
| 1424 | |
| 1425 | info = X509_get_ext_d2i(certificate, NID_info_access, NULL, NULL); |
Benjamin Peterson | f0c9038 | 2015-11-14 15:12:18 -0800 | [diff] [blame] | 1426 | if (info == NULL) |
| 1427 | return Py_None; |
| 1428 | if (sk_ACCESS_DESCRIPTION_num(info) == 0) { |
| 1429 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1430 | return Py_None; |
| 1431 | } |
| 1432 | |
| 1433 | if ((lst = PyList_New(0)) == NULL) { |
| 1434 | goto fail; |
| 1435 | } |
| 1436 | |
| 1437 | for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { |
| 1438 | ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); |
| 1439 | ASN1_IA5STRING *uri; |
| 1440 | |
| 1441 | if ((OBJ_obj2nid(ad->method) != nid) || |
| 1442 | (ad->location->type != GEN_URI)) { |
| 1443 | continue; |
| 1444 | } |
| 1445 | uri = ad->location->d.uniformResourceIdentifier; |
| 1446 | ostr = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1447 | uri->length); |
| 1448 | if (ostr == NULL) { |
| 1449 | goto fail; |
| 1450 | } |
| 1451 | result = PyList_Append(lst, ostr); |
| 1452 | Py_DECREF(ostr); |
| 1453 | if (result < 0) { |
| 1454 | goto fail; |
| 1455 | } |
| 1456 | } |
| 1457 | AUTHORITY_INFO_ACCESS_free(info); |
| 1458 | |
| 1459 | /* convert to tuple or None */ |
| 1460 | if (PyList_Size(lst) == 0) { |
| 1461 | Py_DECREF(lst); |
| 1462 | return Py_None; |
| 1463 | } else { |
| 1464 | PyObject *tup; |
| 1465 | tup = PyList_AsTuple(lst); |
| 1466 | Py_DECREF(lst); |
| 1467 | return tup; |
| 1468 | } |
| 1469 | |
| 1470 | fail: |
| 1471 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | 18fc7be | 2013-11-21 23:57:49 +0100 | [diff] [blame] | 1472 | Py_XDECREF(lst); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1473 | return NULL; |
| 1474 | } |
| 1475 | |
| 1476 | static PyObject * |
| 1477 | _get_crl_dp(X509 *certificate) { |
| 1478 | STACK_OF(DIST_POINT) *dps; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1479 | int i, j; |
| 1480 | PyObject *lst, *res = NULL; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1481 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1482 | dps = X509_get_ext_d2i(certificate, NID_crl_distribution_points, NULL, NULL); |
Christian Heimes | 949ec14 | 2013-11-21 16:26:51 +0100 | [diff] [blame] | 1483 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1484 | if (dps == NULL) |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1485 | return Py_None; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1486 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1487 | lst = PyList_New(0); |
| 1488 | if (lst == NULL) |
| 1489 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1490 | |
| 1491 | for (i=0; i < sk_DIST_POINT_num(dps); i++) { |
| 1492 | DIST_POINT *dp; |
| 1493 | STACK_OF(GENERAL_NAME) *gns; |
| 1494 | |
| 1495 | dp = sk_DIST_POINT_value(dps, i); |
Christian Heimes | a37f524 | 2019-01-15 23:47:42 +0100 | [diff] [blame] | 1496 | if (dp->distpoint == NULL) { |
| 1497 | /* Ignore empty DP value, CVE-2019-5010 */ |
| 1498 | continue; |
| 1499 | } |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1500 | gns = dp->distpoint->name.fullname; |
| 1501 | |
| 1502 | for (j=0; j < sk_GENERAL_NAME_num(gns); j++) { |
| 1503 | GENERAL_NAME *gn; |
| 1504 | ASN1_IA5STRING *uri; |
| 1505 | PyObject *ouri; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1506 | int err; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1507 | |
| 1508 | gn = sk_GENERAL_NAME_value(gns, j); |
| 1509 | if (gn->type != GEN_URI) { |
| 1510 | continue; |
| 1511 | } |
| 1512 | uri = gn->d.uniformResourceIdentifier; |
| 1513 | ouri = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1514 | uri->length); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1515 | if (ouri == NULL) |
| 1516 | goto done; |
| 1517 | |
| 1518 | err = PyList_Append(lst, ouri); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1519 | Py_DECREF(ouri); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1520 | if (err < 0) |
| 1521 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1522 | } |
| 1523 | } |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1524 | |
| 1525 | /* Convert to tuple. */ |
| 1526 | res = (PyList_GET_SIZE(lst) > 0) ? PyList_AsTuple(lst) : Py_None; |
| 1527 | |
| 1528 | done: |
| 1529 | Py_XDECREF(lst); |
Olivier Vielpeau | 2849cc3 | 2017-04-14 21:06:07 -0400 | [diff] [blame] | 1530 | CRL_DIST_POINTS_free(dps); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1531 | return res; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1535 | _decode_certificate(_sslmodulestate *state, X509 *certificate) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1536 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1537 | PyObject *retval = NULL; |
| 1538 | BIO *biobuf = NULL; |
| 1539 | PyObject *peer; |
| 1540 | PyObject *peer_alt_names = NULL; |
| 1541 | PyObject *issuer; |
| 1542 | PyObject *version; |
| 1543 | PyObject *sn_obj; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1544 | PyObject *obj; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1545 | ASN1_INTEGER *serialNumber; |
| 1546 | char buf[2048]; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1547 | int len, result; |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1548 | const ASN1_TIME *notBefore, *notAfter; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1549 | PyObject *pnotBefore, *pnotAfter; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1550 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1551 | retval = PyDict_New(); |
| 1552 | if (retval == NULL) |
| 1553 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1554 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1555 | peer = _create_tuple_for_X509_NAME( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1556 | state, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1557 | X509_get_subject_name(certificate)); |
| 1558 | if (peer == NULL) |
| 1559 | goto fail0; |
| 1560 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 1561 | Py_DECREF(peer); |
| 1562 | goto fail0; |
| 1563 | } |
| 1564 | Py_DECREF(peer); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1565 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1566 | issuer = _create_tuple_for_X509_NAME( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1567 | state, |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1568 | X509_get_issuer_name(certificate)); |
| 1569 | if (issuer == NULL) |
| 1570 | goto fail0; |
| 1571 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1572 | Py_DECREF(issuer); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1573 | goto fail0; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1574 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1575 | Py_DECREF(issuer); |
| 1576 | |
| 1577 | version = PyLong_FromLong(X509_get_version(certificate) + 1); |
Christian Heimes | 5962bef | 2013-07-26 15:51:18 +0200 | [diff] [blame] | 1578 | if (version == NULL) |
| 1579 | goto fail0; |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1580 | if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 1581 | Py_DECREF(version); |
| 1582 | goto fail0; |
| 1583 | } |
| 1584 | Py_DECREF(version); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1585 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1586 | /* get a memory buffer */ |
| 1587 | biobuf = BIO_new(BIO_s_mem()); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1588 | if (biobuf == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1589 | PyErr_SetString(state->PySSLErrorObject, "failed to allocate BIO"); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1590 | goto fail0; |
| 1591 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1592 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1593 | (void) BIO_reset(biobuf); |
| 1594 | serialNumber = X509_get_serialNumber(certificate); |
| 1595 | /* should not exceed 20 octets, 160 bits, so buf is big enough */ |
| 1596 | i2a_ASN1_INTEGER(biobuf, serialNumber); |
| 1597 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1598 | if (len < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1599 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1600 | goto fail1; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1601 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1602 | sn_obj = PyUnicode_FromStringAndSize(buf, len); |
| 1603 | if (sn_obj == NULL) |
| 1604 | goto fail1; |
| 1605 | if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) { |
| 1606 | Py_DECREF(sn_obj); |
| 1607 | goto fail1; |
| 1608 | } |
| 1609 | Py_DECREF(sn_obj); |
| 1610 | |
| 1611 | (void) BIO_reset(biobuf); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1612 | notBefore = X509_get0_notBefore(certificate); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1613 | ASN1_TIME_print(biobuf, notBefore); |
| 1614 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1615 | if (len < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1616 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1617 | goto fail1; |
| 1618 | } |
| 1619 | pnotBefore = PyUnicode_FromStringAndSize(buf, len); |
| 1620 | if (pnotBefore == NULL) |
| 1621 | goto fail1; |
| 1622 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 1623 | Py_DECREF(pnotBefore); |
| 1624 | goto fail1; |
| 1625 | } |
| 1626 | Py_DECREF(pnotBefore); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1627 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1628 | (void) BIO_reset(biobuf); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1629 | notAfter = X509_get0_notAfter(certificate); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1630 | ASN1_TIME_print(biobuf, notAfter); |
| 1631 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1632 | if (len < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1633 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1634 | goto fail1; |
| 1635 | } |
| 1636 | pnotAfter = PyUnicode_FromStringAndSize(buf, len); |
| 1637 | if (pnotAfter == NULL) |
| 1638 | goto fail1; |
| 1639 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 1640 | Py_DECREF(pnotAfter); |
| 1641 | goto fail1; |
| 1642 | } |
| 1643 | Py_DECREF(pnotAfter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1644 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1645 | /* Now look for subjectAltName */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1646 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1647 | peer_alt_names = _get_peer_alt_names(state, certificate); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1648 | if (peer_alt_names == NULL) |
| 1649 | goto fail1; |
| 1650 | else if (peer_alt_names != Py_None) { |
| 1651 | if (PyDict_SetItemString(retval, "subjectAltName", |
| 1652 | peer_alt_names) < 0) { |
| 1653 | Py_DECREF(peer_alt_names); |
| 1654 | goto fail1; |
| 1655 | } |
| 1656 | Py_DECREF(peer_alt_names); |
| 1657 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1658 | |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1659 | /* Authority Information Access: OCSP URIs */ |
| 1660 | obj = _get_aia_uri(certificate, NID_ad_OCSP); |
| 1661 | if (obj == NULL) { |
| 1662 | goto fail1; |
| 1663 | } else if (obj != Py_None) { |
| 1664 | result = PyDict_SetItemString(retval, "OCSP", obj); |
| 1665 | Py_DECREF(obj); |
| 1666 | if (result < 0) { |
| 1667 | goto fail1; |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | obj = _get_aia_uri(certificate, NID_ad_ca_issuers); |
| 1672 | if (obj == NULL) { |
| 1673 | goto fail1; |
| 1674 | } else if (obj != Py_None) { |
| 1675 | result = PyDict_SetItemString(retval, "caIssuers", obj); |
| 1676 | Py_DECREF(obj); |
| 1677 | if (result < 0) { |
| 1678 | goto fail1; |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | /* CDP (CRL distribution points) */ |
| 1683 | obj = _get_crl_dp(certificate); |
| 1684 | if (obj == NULL) { |
| 1685 | goto fail1; |
| 1686 | } else if (obj != Py_None) { |
| 1687 | result = PyDict_SetItemString(retval, "crlDistributionPoints", obj); |
| 1688 | Py_DECREF(obj); |
| 1689 | if (result < 0) { |
| 1690 | goto fail1; |
| 1691 | } |
| 1692 | } |
| 1693 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1694 | BIO_free(biobuf); |
| 1695 | return retval; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1696 | |
| 1697 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1698 | if (biobuf != NULL) |
| 1699 | BIO_free(biobuf); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1700 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1701 | Py_XDECREF(retval); |
| 1702 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1703 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1704 | |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 1705 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1706 | _certificate_to_der(_sslmodulestate *state, X509 *certificate) |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 1707 | { |
| 1708 | unsigned char *bytes_buf = NULL; |
| 1709 | int len; |
| 1710 | PyObject *retval; |
| 1711 | |
| 1712 | bytes_buf = NULL; |
| 1713 | len = i2d_X509(certificate, &bytes_buf); |
| 1714 | if (len < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1715 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 1716 | return NULL; |
| 1717 | } |
| 1718 | /* this is actually an immutable bytes sequence */ |
| 1719 | retval = PyBytes_FromStringAndSize((const char *) bytes_buf, len); |
| 1720 | OPENSSL_free(bytes_buf); |
| 1721 | return retval; |
| 1722 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1723 | |
Christian Heimes | 666991f | 2021-04-26 15:01:40 +0200 | [diff] [blame] | 1724 | #include "_ssl/misc.c" |
| 1725 | #include "_ssl/cert.c" |
| 1726 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1727 | /*[clinic input] |
| 1728 | _ssl._test_decode_cert |
| 1729 | path: object(converter="PyUnicode_FSConverter") |
| 1730 | / |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1731 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1732 | [clinic start generated code]*/ |
| 1733 | |
| 1734 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1735 | _ssl__test_decode_cert_impl(PyObject *module, PyObject *path) |
| 1736 | /*[clinic end generated code: output=96becb9abb23c091 input=cdeaaf02d4346628]*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1737 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1738 | PyObject *retval = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1739 | X509 *x=NULL; |
| 1740 | BIO *cert; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1741 | _sslmodulestate *state = get_ssl_state(module); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1742 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1743 | if ((cert=BIO_new(BIO_s_file())) == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1744 | PyErr_SetString(state->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1745 | "Can't malloc memory to read file"); |
| 1746 | goto fail0; |
| 1747 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1748 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1749 | if (BIO_read_filename(cert, PyBytes_AsString(path)) <= 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1750 | PyErr_SetString(state->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1751 | "Can't open file"); |
| 1752 | goto fail0; |
| 1753 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1754 | |
Alex Gaynor | 40dad95 | 2019-08-15 08:31:28 -0400 | [diff] [blame] | 1755 | x = PEM_read_bio_X509(cert, NULL, NULL, NULL); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1756 | if (x == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1757 | PyErr_SetString(state->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1758 | "Error decoding PEM-encoded file"); |
| 1759 | goto fail0; |
| 1760 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1761 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1762 | retval = _decode_certificate(state, x); |
Mark Dickinson | ee55df5 | 2010-08-03 18:31:54 +0000 | [diff] [blame] | 1763 | X509_free(x); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1764 | |
| 1765 | fail0: |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1766 | Py_DECREF(path); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1767 | if (cert != NULL) BIO_free(cert); |
| 1768 | return retval; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1772 | /*[clinic input] |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1773 | _ssl._SSLSocket.getpeercert |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1774 | der as binary_mode: bool = False |
| 1775 | / |
| 1776 | |
| 1777 | Returns the certificate for the peer. |
| 1778 | |
| 1779 | If no certificate was provided, returns None. If a certificate was |
| 1780 | provided, but not validated, returns an empty dictionary. Otherwise |
| 1781 | returns a dict containing information about the peer certificate. |
| 1782 | |
| 1783 | If the optional argument is True, returns a DER-encoded copy of the |
| 1784 | peer certificate, or None if no certificate was provided. This will |
| 1785 | return the certificate even if it wasn't validated. |
| 1786 | [clinic start generated code]*/ |
| 1787 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1788 | static PyObject * |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1789 | _ssl__SSLSocket_getpeercert_impl(PySSLSocket *self, int binary_mode) |
| 1790 | /*[clinic end generated code: output=1f0ab66dfb693c88 input=c0fbe802e57629b7]*/ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1791 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1792 | int verification; |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1793 | X509 *peer_cert; |
| 1794 | PyObject *result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1795 | |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1796 | if (!SSL_is_init_finished(self->ssl)) { |
Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 1797 | PyErr_SetString(PyExc_ValueError, |
| 1798 | "handshake not done yet"); |
| 1799 | return NULL; |
| 1800 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1801 | peer_cert = SSL_get_peer_certificate(self->ssl); |
| 1802 | if (peer_cert == NULL) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1803 | Py_RETURN_NONE; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1804 | |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 1805 | if (binary_mode) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1806 | /* return cert in DER-encoded format */ |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1807 | result = _certificate_to_der(get_state_sock(self), peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1808 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1809 | verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1810 | if ((verification & SSL_VERIFY_PEER) == 0) |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1811 | result = PyDict_New(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1812 | else |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1813 | result = _decode_certificate(get_state_sock(self), peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1814 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1815 | X509_free(peer_cert); |
| 1816 | return result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Christian Heimes | 666991f | 2021-04-26 15:01:40 +0200 | [diff] [blame] | 1819 | /*[clinic input] |
| 1820 | _ssl._SSLSocket.get_verified_chain |
| 1821 | |
| 1822 | [clinic start generated code]*/ |
| 1823 | |
| 1824 | static PyObject * |
| 1825 | _ssl__SSLSocket_get_verified_chain_impl(PySSLSocket *self) |
| 1826 | /*[clinic end generated code: output=802421163cdc3110 input=5fb0714f77e2bd51]*/ |
| 1827 | { |
| 1828 | /* borrowed reference */ |
| 1829 | STACK_OF(X509) *chain = SSL_get0_verified_chain(self->ssl); |
| 1830 | if (chain == NULL) { |
| 1831 | Py_RETURN_NONE; |
| 1832 | } |
| 1833 | return _PySSL_CertificateFromX509Stack(self->ctx->state, chain, 1); |
| 1834 | } |
| 1835 | |
| 1836 | /*[clinic input] |
| 1837 | _ssl._SSLSocket.get_unverified_chain |
| 1838 | |
| 1839 | [clinic start generated code]*/ |
| 1840 | |
| 1841 | static PyObject * |
| 1842 | _ssl__SSLSocket_get_unverified_chain_impl(PySSLSocket *self) |
| 1843 | /*[clinic end generated code: output=5acdae414e13f913 input=78c33c360c635cb5]*/ |
| 1844 | { |
| 1845 | PyObject *retval; |
| 1846 | /* borrowed reference */ |
| 1847 | /* TODO: include SSL_get_peer_certificate() for server-side sockets */ |
| 1848 | STACK_OF(X509) *chain = SSL_get_peer_cert_chain(self->ssl); |
| 1849 | if (chain == NULL) { |
| 1850 | Py_RETURN_NONE; |
| 1851 | } |
| 1852 | retval = _PySSL_CertificateFromX509Stack(self->ctx->state, chain, 1); |
| 1853 | if (retval == NULL) { |
| 1854 | return NULL; |
| 1855 | } |
| 1856 | /* OpenSSL does not include peer cert for server side connections */ |
| 1857 | if (self->socket_type == PY_SSL_SERVER) { |
| 1858 | PyObject *peerobj = NULL; |
| 1859 | X509 *peer = SSL_get_peer_certificate(self->ssl); |
| 1860 | |
| 1861 | if (peer == NULL) { |
| 1862 | peerobj = Py_None; |
| 1863 | Py_INCREF(peerobj); |
| 1864 | } else { |
| 1865 | /* consume X509 reference on success */ |
| 1866 | peerobj = _PySSL_CertificateFromX509(self->ctx->state, peer, 0); |
| 1867 | if (peerobj == NULL) { |
| 1868 | X509_free(peer); |
| 1869 | Py_DECREF(retval); |
| 1870 | return NULL; |
| 1871 | } |
| 1872 | } |
| 1873 | int res = PyList_Insert(retval, 0, peerobj); |
| 1874 | Py_DECREF(peerobj); |
| 1875 | if (res < 0) { |
| 1876 | Py_DECREF(retval); |
| 1877 | return NULL; |
| 1878 | } |
| 1879 | } |
| 1880 | return retval; |
| 1881 | } |
| 1882 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1883 | static PyObject * |
| 1884 | cipher_to_tuple(const SSL_CIPHER *cipher) |
| 1885 | { |
| 1886 | const char *cipher_name, *cipher_protocol; |
| 1887 | PyObject *v, *retval = PyTuple_New(3); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1888 | if (retval == NULL) |
| 1889 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1890 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1891 | cipher_name = SSL_CIPHER_get_name(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1892 | if (cipher_name == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1893 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1894 | PyTuple_SET_ITEM(retval, 0, Py_None); |
| 1895 | } else { |
| 1896 | v = PyUnicode_FromString(cipher_name); |
| 1897 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1898 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1899 | PyTuple_SET_ITEM(retval, 0, v); |
| 1900 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1901 | |
| 1902 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1903 | if (cipher_protocol == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1904 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1905 | PyTuple_SET_ITEM(retval, 1, Py_None); |
| 1906 | } else { |
| 1907 | v = PyUnicode_FromString(cipher_protocol); |
| 1908 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1909 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1910 | PyTuple_SET_ITEM(retval, 1, v); |
| 1911 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1912 | |
| 1913 | v = PyLong_FromLong(SSL_CIPHER_get_bits(cipher, NULL)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1914 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1915 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1916 | PyTuple_SET_ITEM(retval, 2, v); |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1917 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1918 | return retval; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1919 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1920 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1921 | Py_DECREF(retval); |
| 1922 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1925 | static PyObject * |
| 1926 | cipher_to_dict(const SSL_CIPHER *cipher) |
| 1927 | { |
| 1928 | const char *cipher_name, *cipher_protocol; |
| 1929 | |
| 1930 | unsigned long cipher_id; |
| 1931 | int alg_bits, strength_bits, len; |
| 1932 | char buf[512] = {0}; |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1933 | int aead, nid; |
| 1934 | const char *skcipher = NULL, *digest = NULL, *kx = NULL, *auth = NULL; |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1935 | |
| 1936 | /* can be NULL */ |
| 1937 | cipher_name = SSL_CIPHER_get_name(cipher); |
| 1938 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
| 1939 | cipher_id = SSL_CIPHER_get_id(cipher); |
| 1940 | SSL_CIPHER_description(cipher, buf, sizeof(buf) - 1); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 1941 | /* Downcast to avoid a warning. Safe since buf is always 512 bytes */ |
| 1942 | len = (int)strlen(buf); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1943 | if (len > 1 && buf[len-1] == '\n') |
| 1944 | buf[len-1] = '\0'; |
| 1945 | strength_bits = SSL_CIPHER_get_bits(cipher, &alg_bits); |
| 1946 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1947 | aead = SSL_CIPHER_is_aead(cipher); |
| 1948 | nid = SSL_CIPHER_get_cipher_nid(cipher); |
| 1949 | skcipher = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1950 | nid = SSL_CIPHER_get_digest_nid(cipher); |
| 1951 | digest = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1952 | nid = SSL_CIPHER_get_kx_nid(cipher); |
| 1953 | kx = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1954 | nid = SSL_CIPHER_get_auth_nid(cipher); |
| 1955 | auth = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1956 | |
Victor Stinner | 410b988 | 2016-09-12 12:00:23 +0200 | [diff] [blame] | 1957 | return Py_BuildValue( |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1958 | "{sksssssssisi" |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1959 | "sOssssssss" |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1960 | "}", |
| 1961 | "id", cipher_id, |
| 1962 | "name", cipher_name, |
| 1963 | "protocol", cipher_protocol, |
| 1964 | "description", buf, |
| 1965 | "strength_bits", strength_bits, |
| 1966 | "alg_bits", alg_bits |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1967 | ,"aead", aead ? Py_True : Py_False, |
| 1968 | "symmetric", skcipher, |
| 1969 | "digest", digest, |
| 1970 | "kea", kx, |
| 1971 | "auth", auth |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1972 | ); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1973 | } |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1974 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1975 | /*[clinic input] |
| 1976 | _ssl._SSLSocket.shared_ciphers |
| 1977 | [clinic start generated code]*/ |
| 1978 | |
| 1979 | static PyObject * |
| 1980 | _ssl__SSLSocket_shared_ciphers_impl(PySSLSocket *self) |
| 1981 | /*[clinic end generated code: output=3d174ead2e42c4fd input=0bfe149da8fe6306]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1982 | { |
| 1983 | STACK_OF(SSL_CIPHER) *ciphers; |
| 1984 | int i; |
| 1985 | PyObject *res; |
| 1986 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1987 | ciphers = SSL_get_ciphers(self->ssl); |
| 1988 | if (!ciphers) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1989 | Py_RETURN_NONE; |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1990 | res = PyList_New(sk_SSL_CIPHER_num(ciphers)); |
| 1991 | if (!res) |
| 1992 | return NULL; |
| 1993 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 1994 | PyObject *tup = cipher_to_tuple(sk_SSL_CIPHER_value(ciphers, i)); |
| 1995 | if (!tup) { |
| 1996 | Py_DECREF(res); |
| 1997 | return NULL; |
| 1998 | } |
| 1999 | PyList_SET_ITEM(res, i, tup); |
| 2000 | } |
| 2001 | return res; |
| 2002 | } |
| 2003 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2004 | /*[clinic input] |
| 2005 | _ssl._SSLSocket.cipher |
| 2006 | [clinic start generated code]*/ |
| 2007 | |
| 2008 | static PyObject * |
| 2009 | _ssl__SSLSocket_cipher_impl(PySSLSocket *self) |
| 2010 | /*[clinic end generated code: output=376417c16d0e5815 input=548fb0e27243796d]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 2011 | { |
| 2012 | const SSL_CIPHER *current; |
| 2013 | |
| 2014 | if (self->ssl == NULL) |
| 2015 | Py_RETURN_NONE; |
| 2016 | current = SSL_get_current_cipher(self->ssl); |
| 2017 | if (current == NULL) |
| 2018 | Py_RETURN_NONE; |
| 2019 | return cipher_to_tuple(current); |
| 2020 | } |
| 2021 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2022 | /*[clinic input] |
| 2023 | _ssl._SSLSocket.version |
| 2024 | [clinic start generated code]*/ |
| 2025 | |
| 2026 | static PyObject * |
| 2027 | _ssl__SSLSocket_version_impl(PySSLSocket *self) |
| 2028 | /*[clinic end generated code: output=178aed33193b2cdb input=900186a503436fd6]*/ |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 2029 | { |
| 2030 | const char *version; |
| 2031 | |
| 2032 | if (self->ssl == NULL) |
| 2033 | Py_RETURN_NONE; |
Christian Heimes | 6877111 | 2017-09-05 21:55:40 -0700 | [diff] [blame] | 2034 | if (!SSL_is_init_finished(self->ssl)) { |
| 2035 | /* handshake not finished */ |
| 2036 | Py_RETURN_NONE; |
| 2037 | } |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 2038 | version = SSL_get_version(self->ssl); |
| 2039 | if (!strcmp(version, "unknown")) |
| 2040 | Py_RETURN_NONE; |
| 2041 | return PyUnicode_FromString(version); |
| 2042 | } |
| 2043 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2044 | /*[clinic input] |
| 2045 | _ssl._SSLSocket.selected_alpn_protocol |
| 2046 | [clinic start generated code]*/ |
| 2047 | |
| 2048 | static PyObject * |
| 2049 | _ssl__SSLSocket_selected_alpn_protocol_impl(PySSLSocket *self) |
| 2050 | /*[clinic end generated code: output=ec33688b303d250f input=442de30e35bc2913]*/ |
| 2051 | { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2052 | const unsigned char *out; |
| 2053 | unsigned int outlen; |
| 2054 | |
| 2055 | SSL_get0_alpn_selected(self->ssl, &out, &outlen); |
| 2056 | |
| 2057 | if (out == NULL) |
| 2058 | Py_RETURN_NONE; |
| 2059 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2060 | } |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 2061 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2062 | /*[clinic input] |
| 2063 | _ssl._SSLSocket.compression |
| 2064 | [clinic start generated code]*/ |
| 2065 | |
| 2066 | static PyObject * |
| 2067 | _ssl__SSLSocket_compression_impl(PySSLSocket *self) |
| 2068 | /*[clinic end generated code: output=bd16cb1bb4646ae7 input=5d059d0a2bbc32c8]*/ |
| 2069 | { |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2070 | #ifdef OPENSSL_NO_COMP |
| 2071 | Py_RETURN_NONE; |
| 2072 | #else |
| 2073 | const COMP_METHOD *comp_method; |
| 2074 | const char *short_name; |
| 2075 | |
| 2076 | if (self->ssl == NULL) |
| 2077 | Py_RETURN_NONE; |
| 2078 | comp_method = SSL_get_current_compression(self->ssl); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2079 | if (comp_method == NULL || COMP_get_type(comp_method) == NID_undef) |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2080 | Py_RETURN_NONE; |
Christian Heimes | 281e5f8 | 2016-09-06 01:10:39 +0200 | [diff] [blame] | 2081 | short_name = OBJ_nid2sn(COMP_get_type(comp_method)); |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 2082 | if (short_name == NULL) |
| 2083 | Py_RETURN_NONE; |
| 2084 | return PyUnicode_DecodeFSDefault(short_name); |
| 2085 | #endif |
| 2086 | } |
| 2087 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2088 | static PySSLContext *PySSL_get_context(PySSLSocket *self, void *closure) { |
| 2089 | Py_INCREF(self->ctx); |
| 2090 | return self->ctx; |
| 2091 | } |
| 2092 | |
| 2093 | static int PySSL_set_context(PySSLSocket *self, PyObject *value, |
| 2094 | void *closure) { |
| 2095 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2096 | if (PyObject_TypeCheck(value, self->ctx->state->PySSLContext_Type)) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2097 | Py_INCREF(value); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 2098 | Py_SETREF(self->ctx, (PySSLContext *)value); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2099 | SSL_set_SSL_CTX(self->ssl, self->ctx->ctx); |
Christian Heimes | 77cde50 | 2021-03-21 16:13:09 +0100 | [diff] [blame] | 2100 | /* Set SSL* internal msg_callback to state of new context's state */ |
| 2101 | SSL_set_msg_callback( |
| 2102 | self->ssl, |
| 2103 | self->ctx->msg_cb ? _PySSL_msg_callback : NULL |
| 2104 | ); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2105 | } else { |
Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2106 | PyErr_SetString(PyExc_TypeError, "The value must be a SSLContext"); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2107 | return -1; |
| 2108 | } |
| 2109 | |
| 2110 | return 0; |
| 2111 | } |
| 2112 | |
| 2113 | PyDoc_STRVAR(PySSL_set_context_doc, |
| 2114 | "_setter_context(ctx)\n\ |
| 2115 | \ |
| 2116 | This changes the context associated with the SSLSocket. This is typically\n\ |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 2117 | used from within a callback function set by the sni_callback\n\ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2118 | on the SSLContext to change the certificate information associated with the\n\ |
| 2119 | SSLSocket before the cryptographic exchange handshake messages\n"); |
| 2120 | |
| 2121 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2122 | static PyObject * |
| 2123 | PySSL_get_server_side(PySSLSocket *self, void *c) |
| 2124 | { |
| 2125 | return PyBool_FromLong(self->socket_type == PY_SSL_SERVER); |
| 2126 | } |
| 2127 | |
| 2128 | PyDoc_STRVAR(PySSL_get_server_side_doc, |
| 2129 | "Whether this is a server-side socket."); |
| 2130 | |
| 2131 | static PyObject * |
| 2132 | PySSL_get_server_hostname(PySSLSocket *self, void *c) |
| 2133 | { |
| 2134 | if (self->server_hostname == NULL) |
| 2135 | Py_RETURN_NONE; |
| 2136 | Py_INCREF(self->server_hostname); |
| 2137 | return self->server_hostname; |
| 2138 | } |
| 2139 | |
| 2140 | PyDoc_STRVAR(PySSL_get_server_hostname_doc, |
| 2141 | "The currently set server hostname (for SNI)."); |
| 2142 | |
| 2143 | static PyObject * |
| 2144 | PySSL_get_owner(PySSLSocket *self, void *c) |
| 2145 | { |
| 2146 | PyObject *owner; |
| 2147 | |
| 2148 | if (self->owner == NULL) |
| 2149 | Py_RETURN_NONE; |
| 2150 | |
| 2151 | owner = PyWeakref_GetObject(self->owner); |
| 2152 | Py_INCREF(owner); |
| 2153 | return owner; |
| 2154 | } |
| 2155 | |
| 2156 | static int |
| 2157 | PySSL_set_owner(PySSLSocket *self, PyObject *value, void *c) |
| 2158 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2159 | Py_XSETREF(self->owner, PyWeakref_NewRef(value, NULL)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2160 | if (self->owner == NULL) |
| 2161 | return -1; |
| 2162 | return 0; |
| 2163 | } |
| 2164 | |
| 2165 | PyDoc_STRVAR(PySSL_get_owner_doc, |
| 2166 | "The Python-level owner of this object.\ |
| 2167 | Passed as \"self\" in servername callback."); |
| 2168 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2169 | static int |
| 2170 | PySSL_traverse(PySSLSocket *self, visitproc visit, void *arg) |
| 2171 | { |
| 2172 | Py_VISIT(self->exc_type); |
| 2173 | Py_VISIT(self->exc_value); |
| 2174 | Py_VISIT(self->exc_tb); |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 2175 | Py_VISIT(Py_TYPE(self)); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2176 | return 0; |
| 2177 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2178 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2179 | static int |
| 2180 | PySSL_clear(PySSLSocket *self) |
| 2181 | { |
| 2182 | Py_CLEAR(self->exc_type); |
| 2183 | Py_CLEAR(self->exc_value); |
| 2184 | Py_CLEAR(self->exc_tb); |
| 2185 | return 0; |
| 2186 | } |
| 2187 | |
| 2188 | static void |
| 2189 | PySSL_dealloc(PySSLSocket *self) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2190 | { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 2191 | PyTypeObject *tp = Py_TYPE(self); |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 2192 | PyObject_GC_UnTrack(self); |
| 2193 | if (self->ssl) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2194 | SSL_free(self->ssl); |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 2195 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2196 | Py_XDECREF(self->Socket); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2197 | Py_XDECREF(self->ctx); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2198 | Py_XDECREF(self->server_hostname); |
| 2199 | Py_XDECREF(self->owner); |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 2200 | PyObject_GC_Del(self); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 2201 | Py_DECREF(tp); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2204 | /* If the socket has a timeout, do a select()/poll() on the socket. |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2205 | The argument writing indicates the direction. |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2206 | Returns one of the possibilities in the timeout_state enum (above). |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2207 | */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2208 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2209 | static int |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2210 | PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout) |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2211 | { |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2212 | int rc; |
| 2213 | #ifdef HAVE_POLL |
| 2214 | struct pollfd pollfd; |
| 2215 | _PyTime_t ms; |
| 2216 | #else |
| 2217 | int nfds; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2218 | fd_set fds; |
| 2219 | struct timeval tv; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2220 | #endif |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2221 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2222 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2223 | if ((s == NULL) || (timeout == 0)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2224 | return SOCKET_IS_NONBLOCKING; |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2225 | else if (timeout < 0) { |
| 2226 | if (s->sock_timeout > 0) |
| 2227 | return SOCKET_HAS_TIMED_OUT; |
| 2228 | else |
| 2229 | return SOCKET_IS_BLOCKING; |
| 2230 | } |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2231 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2232 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2233 | if (s->sock_fd == INVALID_SOCKET) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2234 | return SOCKET_HAS_BEEN_CLOSED; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2235 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2236 | /* Prefer poll, if available, since you can poll() any fd |
| 2237 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2238 | #ifdef HAVE_POLL |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2239 | pollfd.fd = s->sock_fd; |
| 2240 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2241 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2242 | /* timeout is in seconds, poll() uses milliseconds */ |
| 2243 | ms = (int)_PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2244 | assert(ms <= INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2245 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2246 | PySSL_BEGIN_ALLOW_THREADS |
| 2247 | rc = poll(&pollfd, 1, (int)ms); |
| 2248 | PySSL_END_ALLOW_THREADS |
| 2249 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2250 | /* Guard against socket too large for select*/ |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 2251 | if (!_PyIsSelectable_fd(s->sock_fd)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2252 | return SOCKET_TOO_LARGE_FOR_SELECT; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2253 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2254 | _PyTime_AsTimeval_noraise(timeout, &tv, _PyTime_ROUND_CEILING); |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2255 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2256 | FD_ZERO(&fds); |
| 2257 | FD_SET(s->sock_fd, &fds); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2258 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2259 | /* Wait until the socket becomes ready */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2260 | PySSL_BEGIN_ALLOW_THREADS |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2261 | nfds = Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2262 | if (writing) |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2263 | rc = select(nfds, NULL, &fds, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2264 | else |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2265 | rc = select(nfds, &fds, NULL, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2266 | PySSL_END_ALLOW_THREADS |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2267 | #endif |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2268 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2269 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
| 2270 | (when we are able to write or when there's something to read) */ |
| 2271 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2274 | /*[clinic input] |
| 2275 | _ssl._SSLSocket.write |
| 2276 | b: Py_buffer |
| 2277 | / |
| 2278 | |
| 2279 | Writes the bytes-like object b into the SSL object. |
| 2280 | |
| 2281 | Returns the number of bytes written. |
| 2282 | [clinic start generated code]*/ |
| 2283 | |
| 2284 | static PyObject * |
| 2285 | _ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b) |
| 2286 | /*[clinic end generated code: output=aa7a6be5527358d8 input=77262d994fe5100a]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2287 | { |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame] | 2288 | size_t count = 0; |
| 2289 | int retval; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2290 | int sockstate; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2291 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2292 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2293 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2294 | _PyTime_t timeout, deadline = 0; |
| 2295 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2296 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2297 | if (sock != NULL) { |
| 2298 | if (((PyObject*)sock) == Py_None) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2299 | _setSSLError(get_state_sock(self), |
| 2300 | "Underlying socket connection gone", |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2301 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2302 | return NULL; |
| 2303 | } |
| 2304 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2307 | if (sock != NULL) { |
| 2308 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2309 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2310 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2311 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2312 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2313 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2314 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2315 | has_timeout = (timeout > 0); |
| 2316 | if (has_timeout) |
| 2317 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2318 | |
| 2319 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2320 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2321 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2322 | "The write operation timed out"); |
| 2323 | goto error; |
| 2324 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2325 | PyErr_SetString(get_state_sock(self)->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2326 | "Underlying socket has been closed."); |
| 2327 | goto error; |
| 2328 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2329 | PyErr_SetString(get_state_sock(self)->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2330 | "Underlying socket too large for select()."); |
| 2331 | goto error; |
| 2332 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2333 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2334 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2335 | PySSL_BEGIN_ALLOW_THREADS |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame] | 2336 | retval = SSL_write_ex(self->ssl, b->buf, (int)b->len, &count); |
| 2337 | err = _PySSL_errno(retval == 0, self->ssl, retval); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2338 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2339 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2340 | |
| 2341 | if (PyErr_CheckSignals()) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2342 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2343 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2344 | if (has_timeout) |
| 2345 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2346 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2347 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2348 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2349 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2350 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2351 | } else { |
| 2352 | sockstate = SOCKET_OPERATION_OK; |
| 2353 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2354 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2355 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2356 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2357 | "The write operation timed out"); |
| 2358 | goto error; |
| 2359 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2360 | PyErr_SetString(get_state_sock(self)->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2361 | "Underlying socket has been closed."); |
| 2362 | goto error; |
| 2363 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2364 | break; |
| 2365 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2366 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2367 | err.ssl == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2368 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2369 | Py_XDECREF(sock); |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame] | 2370 | if (retval == 0) |
| 2371 | return PySSL_SetError(self, retval, __FILE__, __LINE__); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2372 | if (PySSL_ChainExceptions(self) < 0) |
| 2373 | return NULL; |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame] | 2374 | return PyLong_FromSize_t(count); |
Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 2375 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2376 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2377 | PySSL_ChainExceptions(self); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2378 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2381 | /*[clinic input] |
| 2382 | _ssl._SSLSocket.pending |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2383 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2384 | Returns the number of already decrypted bytes available for read, pending on the connection. |
| 2385 | [clinic start generated code]*/ |
| 2386 | |
| 2387 | static PyObject * |
| 2388 | _ssl__SSLSocket_pending_impl(PySSLSocket *self) |
| 2389 | /*[clinic end generated code: output=983d9fecdc308a83 input=2b77487d6dfd597f]*/ |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2390 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2391 | int count = 0; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2392 | _PySSLError err; |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2393 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2394 | PySSL_BEGIN_ALLOW_THREADS |
| 2395 | count = SSL_pending(self->ssl); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2396 | err = _PySSL_errno(count < 0, self->ssl, count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2397 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2398 | self->err = err; |
| 2399 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2400 | if (count < 0) |
| 2401 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2402 | else |
| 2403 | return PyLong_FromLong(count); |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2406 | /*[clinic input] |
| 2407 | _ssl._SSLSocket.read |
| 2408 | size as len: int |
| 2409 | [ |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2410 | buffer: Py_buffer(accept={rwbuffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2411 | ] |
| 2412 | / |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2413 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2414 | Read up to size bytes from the SSL socket. |
| 2415 | [clinic start generated code]*/ |
| 2416 | |
| 2417 | static PyObject * |
| 2418 | _ssl__SSLSocket_read_impl(PySSLSocket *self, int len, int group_right_1, |
| 2419 | Py_buffer *buffer) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2420 | /*[clinic end generated code: output=00097776cec2a0af input=ff157eb918d0905b]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2421 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2422 | PyObject *dest = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2423 | char *mem; |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame] | 2424 | size_t count = 0; |
| 2425 | int retval; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2426 | int sockstate; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2427 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2428 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2429 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2430 | _PyTime_t timeout, deadline = 0; |
| 2431 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2432 | |
Martin Panter | 5503d47 | 2016-03-27 05:35:19 +0000 | [diff] [blame] | 2433 | if (!group_right_1 && len < 0) { |
| 2434 | PyErr_SetString(PyExc_ValueError, "size should not be negative"); |
| 2435 | return NULL; |
| 2436 | } |
| 2437 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2438 | if (sock != NULL) { |
| 2439 | if (((PyObject*)sock) == Py_None) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2440 | _setSSLError(get_state_sock(self), |
| 2441 | "Underlying socket connection gone", |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2442 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2443 | return NULL; |
| 2444 | } |
| 2445 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2448 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2449 | dest = PyBytes_FromStringAndSize(NULL, len); |
| 2450 | if (dest == NULL) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2451 | goto error; |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2452 | if (len == 0) { |
| 2453 | Py_XDECREF(sock); |
| 2454 | return dest; |
| 2455 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2456 | mem = PyBytes_AS_STRING(dest); |
| 2457 | } |
| 2458 | else { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2459 | mem = buffer->buf; |
| 2460 | if (len <= 0 || len > buffer->len) { |
| 2461 | len = (int) buffer->len; |
| 2462 | if (buffer->len != len) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2463 | PyErr_SetString(PyExc_OverflowError, |
| 2464 | "maximum length can't fit in a C 'int'"); |
| 2465 | goto error; |
| 2466 | } |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2467 | if (len == 0) { |
| 2468 | count = 0; |
| 2469 | goto done; |
| 2470 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2471 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2474 | if (sock != NULL) { |
| 2475 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2476 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2477 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2478 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2479 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2480 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2481 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2482 | has_timeout = (timeout > 0); |
| 2483 | if (has_timeout) |
| 2484 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2485 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2486 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2487 | PySSL_BEGIN_ALLOW_THREADS |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame] | 2488 | retval = SSL_read_ex(self->ssl, mem, len, &count); |
| 2489 | err = _PySSL_errno(retval == 0, self->ssl, retval); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2490 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2491 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2492 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2493 | if (PyErr_CheckSignals()) |
| 2494 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2495 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2496 | if (has_timeout) |
| 2497 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2498 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2499 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2500 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2501 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2502 | sockstate = PySSL_select(sock, 1, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2503 | } else if (err.ssl == SSL_ERROR_ZERO_RETURN && |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2504 | SSL_get_shutdown(self->ssl) == SSL_RECEIVED_SHUTDOWN) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2505 | { |
| 2506 | count = 0; |
| 2507 | goto done; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2508 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2509 | else |
| 2510 | sockstate = SOCKET_OPERATION_OK; |
| 2511 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2512 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2513 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2514 | "The read operation timed out"); |
| 2515 | goto error; |
| 2516 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2517 | break; |
| 2518 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2519 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2520 | err.ssl == SSL_ERROR_WANT_WRITE); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2521 | |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame] | 2522 | if (retval == 0) { |
| 2523 | PySSL_SetError(self, retval, __FILE__, __LINE__); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2524 | goto error; |
| 2525 | } |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2526 | if (self->exc_type != NULL) |
| 2527 | goto error; |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2528 | |
| 2529 | done: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2530 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2531 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2532 | _PyBytes_Resize(&dest, count); |
| 2533 | return dest; |
| 2534 | } |
| 2535 | else { |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame] | 2536 | return PyLong_FromSize_t(count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2537 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2538 | |
| 2539 | error: |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2540 | PySSL_ChainExceptions(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2541 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2542 | if (!group_right_1) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2543 | Py_XDECREF(dest); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2544 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2547 | /*[clinic input] |
| 2548 | _ssl._SSLSocket.shutdown |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2549 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2550 | Does the SSL shutdown handshake with the remote end. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2551 | [clinic start generated code]*/ |
| 2552 | |
| 2553 | static PyObject * |
| 2554 | _ssl__SSLSocket_shutdown_impl(PySSLSocket *self) |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2555 | /*[clinic end generated code: output=ca1aa7ed9d25ca42 input=11d39e69b0a2bf4a]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2556 | { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2557 | _PySSLError err; |
| 2558 | int sockstate, nonblocking, ret; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2559 | int zeros = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2560 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2561 | _PyTime_t timeout, deadline = 0; |
| 2562 | int has_timeout; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2563 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2564 | if (sock != NULL) { |
| 2565 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2566 | if ((((PyObject*)sock) == Py_None) || (sock->sock_fd == INVALID_SOCKET)) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2567 | _setSSLError(get_state_sock(self), |
| 2568 | "Underlying socket connection gone", |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2569 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2570 | return NULL; |
| 2571 | } |
| 2572 | Py_INCREF(sock); |
| 2573 | |
| 2574 | /* Just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2575 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2576 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2577 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2578 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2579 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2580 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2581 | has_timeout = (timeout > 0); |
| 2582 | if (has_timeout) |
| 2583 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2584 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2585 | while (1) { |
| 2586 | PySSL_BEGIN_ALLOW_THREADS |
| 2587 | /* Disable read-ahead so that unwrap can work correctly. |
| 2588 | * Otherwise OpenSSL might read in too much data, |
| 2589 | * eating clear text data that happens to be |
| 2590 | * transmitted after the SSL shutdown. |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 2591 | * Should be safe to call repeatedly every time this |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2592 | * function is used and the shutdown_seen_zero != 0 |
| 2593 | * condition is met. |
| 2594 | */ |
| 2595 | if (self->shutdown_seen_zero) |
| 2596 | SSL_set_read_ahead(self->ssl, 0); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2597 | ret = SSL_shutdown(self->ssl); |
| 2598 | err = _PySSL_errno(ret < 0, self->ssl, ret); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2599 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2600 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2601 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2602 | /* If err == 1, a secure shutdown with SSL_shutdown() is complete */ |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2603 | if (ret > 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2604 | break; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2605 | if (ret == 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2606 | /* Don't loop endlessly; instead preserve legacy |
| 2607 | behaviour of trying SSL_shutdown() only twice. |
| 2608 | This looks necessary for OpenSSL < 0.9.8m */ |
| 2609 | if (++zeros > 1) |
| 2610 | break; |
| 2611 | /* Shutdown was sent, now try receiving */ |
| 2612 | self->shutdown_seen_zero = 1; |
| 2613 | continue; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2616 | if (has_timeout) |
| 2617 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2618 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2619 | /* Possibly retry shutdown until timeout or failure */ |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2620 | if (err.ssl == SSL_ERROR_WANT_READ) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2621 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2622 | else if (err.ssl == SSL_ERROR_WANT_WRITE) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2623 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2624 | else |
| 2625 | break; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2626 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2627 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2628 | if (err.ssl == SSL_ERROR_WANT_READ) |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2629 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2630 | "The read operation timed out"); |
| 2631 | else |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2632 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2633 | "The write operation timed out"); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2634 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2635 | } |
| 2636 | else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2637 | PyErr_SetString(get_state_sock(self)->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2638 | "Underlying socket too large for select()."); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2639 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2640 | } |
| 2641 | else if (sockstate != SOCKET_OPERATION_OK) |
| 2642 | /* Retain the SSL error code */ |
| 2643 | break; |
| 2644 | } |
Nathaniel J. Smith | c0da582 | 2018-09-21 21:44:12 -0700 | [diff] [blame] | 2645 | if (ret < 0) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2646 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2647 | PySSL_SetError(self, ret, __FILE__, __LINE__); |
| 2648 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2649 | } |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2650 | if (self->exc_type != NULL) |
| 2651 | goto error; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2652 | if (sock) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2653 | /* It's already INCREF'ed */ |
| 2654 | return (PyObject *) sock; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2655 | else |
| 2656 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2657 | |
| 2658 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2659 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2660 | PySSL_ChainExceptions(self); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2661 | return NULL; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2664 | /*[clinic input] |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2665 | _ssl._SSLSocket.get_channel_binding |
| 2666 | cb_type: str = "tls-unique" |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2667 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2668 | Get channel binding data for current connection. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2669 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2670 | Raise ValueError if the requested `cb_type` is not supported. Return bytes |
| 2671 | of the data or None if the data is not available (e.g. before the handshake). |
| 2672 | Only 'tls-unique' channel binding data from RFC 5929 is supported. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2673 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2674 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2675 | static PyObject * |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2676 | _ssl__SSLSocket_get_channel_binding_impl(PySSLSocket *self, |
| 2677 | const char *cb_type) |
| 2678 | /*[clinic end generated code: output=34bac9acb6a61d31 input=08b7e43b99c17d41]*/ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2679 | { |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2680 | char buf[PySSL_CB_MAXLEN]; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 2681 | size_t len; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2682 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2683 | if (strcmp(cb_type, "tls-unique") == 0) { |
| 2684 | if (SSL_session_reused(self->ssl) ^ !self->socket_type) { |
| 2685 | /* if session is resumed XOR we are the client */ |
| 2686 | len = SSL_get_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2687 | } |
| 2688 | else { |
| 2689 | /* if a new session XOR we are the server */ |
| 2690 | len = SSL_get_peer_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2691 | } |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2692 | } |
| 2693 | else { |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2694 | PyErr_Format( |
| 2695 | PyExc_ValueError, |
| 2696 | "'%s' channel binding type not implemented", |
| 2697 | cb_type |
| 2698 | ); |
| 2699 | return NULL; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | /* It cannot be negative in current OpenSSL version as of July 2011 */ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2703 | if (len == 0) |
| 2704 | Py_RETURN_NONE; |
| 2705 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2706 | return PyBytes_FromStringAndSize(buf, len); |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2707 | } |
| 2708 | |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2709 | /*[clinic input] |
| 2710 | _ssl._SSLSocket.verify_client_post_handshake |
| 2711 | |
| 2712 | Initiate TLS 1.3 post-handshake authentication |
| 2713 | [clinic start generated code]*/ |
| 2714 | |
| 2715 | static PyObject * |
| 2716 | _ssl__SSLSocket_verify_client_post_handshake_impl(PySSLSocket *self) |
| 2717 | /*[clinic end generated code: output=532147f3b1341425 input=6bfa874810a3d889]*/ |
| 2718 | { |
| 2719 | #ifdef TLS1_3_VERSION |
| 2720 | int err = SSL_verify_client_post_handshake(self->ssl); |
| 2721 | if (err == 0) |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2722 | return _setSSLError(get_state_sock(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2723 | else |
| 2724 | Py_RETURN_NONE; |
| 2725 | #else |
| 2726 | PyErr_SetString(PyExc_NotImplementedError, |
| 2727 | "Post-handshake auth is not supported by your " |
| 2728 | "OpenSSL version."); |
| 2729 | return NULL; |
| 2730 | #endif |
| 2731 | } |
| 2732 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2733 | static SSL_SESSION* |
| 2734 | _ssl_session_dup(SSL_SESSION *session) { |
| 2735 | SSL_SESSION *newsession = NULL; |
| 2736 | int slen; |
| 2737 | unsigned char *senc = NULL, *p; |
| 2738 | const unsigned char *const_p; |
| 2739 | |
| 2740 | if (session == NULL) { |
| 2741 | PyErr_SetString(PyExc_ValueError, "Invalid session"); |
| 2742 | goto error; |
| 2743 | } |
| 2744 | |
| 2745 | /* get length */ |
| 2746 | slen = i2d_SSL_SESSION(session, NULL); |
| 2747 | if (slen == 0 || slen > 0xFF00) { |
| 2748 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2749 | goto error; |
| 2750 | } |
| 2751 | if ((senc = PyMem_Malloc(slen)) == NULL) { |
| 2752 | PyErr_NoMemory(); |
| 2753 | goto error; |
| 2754 | } |
| 2755 | p = senc; |
| 2756 | if (!i2d_SSL_SESSION(session, &p)) { |
| 2757 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2758 | goto error; |
| 2759 | } |
| 2760 | const_p = senc; |
| 2761 | newsession = d2i_SSL_SESSION(NULL, &const_p, slen); |
| 2762 | if (session == NULL) { |
| 2763 | goto error; |
| 2764 | } |
| 2765 | PyMem_Free(senc); |
| 2766 | return newsession; |
| 2767 | error: |
| 2768 | if (senc != NULL) { |
| 2769 | PyMem_Free(senc); |
| 2770 | } |
| 2771 | return NULL; |
| 2772 | } |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2773 | |
| 2774 | static PyObject * |
| 2775 | PySSL_get_session(PySSLSocket *self, void *closure) { |
| 2776 | /* get_session can return sessions from a server-side connection, |
| 2777 | * it does not check for handshake done or client socket. */ |
| 2778 | PySSLSession *pysess; |
| 2779 | SSL_SESSION *session; |
| 2780 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2781 | /* duplicate session as workaround for session bug in OpenSSL 1.1.0, |
| 2782 | * https://github.com/openssl/openssl/issues/1550 */ |
| 2783 | session = SSL_get0_session(self->ssl); /* borrowed reference */ |
| 2784 | if (session == NULL) { |
| 2785 | Py_RETURN_NONE; |
| 2786 | } |
| 2787 | if ((session = _ssl_session_dup(session)) == NULL) { |
| 2788 | return NULL; |
| 2789 | } |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2790 | session = SSL_get1_session(self->ssl); |
| 2791 | if (session == NULL) { |
| 2792 | Py_RETURN_NONE; |
| 2793 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2794 | pysess = PyObject_GC_New(PySSLSession, self->ctx->state->PySSLSession_Type); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2795 | if (pysess == NULL) { |
| 2796 | SSL_SESSION_free(session); |
| 2797 | return NULL; |
| 2798 | } |
| 2799 | |
| 2800 | assert(self->ctx); |
| 2801 | pysess->ctx = self->ctx; |
| 2802 | Py_INCREF(pysess->ctx); |
| 2803 | pysess->session = session; |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2804 | PyObject_GC_Track(pysess); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2805 | return (PyObject *)pysess; |
| 2806 | } |
| 2807 | |
| 2808 | static int PySSL_set_session(PySSLSocket *self, PyObject *value, |
| 2809 | void *closure) |
| 2810 | { |
| 2811 | PySSLSession *pysess; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2812 | SSL_SESSION *session; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2813 | int result; |
| 2814 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2815 | if (!Py_IS_TYPE(value, get_state_sock(self)->PySSLSession_Type)) { |
Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2816 | PyErr_SetString(PyExc_TypeError, "Value is not a SSLSession."); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2817 | return -1; |
| 2818 | } |
| 2819 | pysess = (PySSLSession *)value; |
| 2820 | |
| 2821 | if (self->ctx->ctx != pysess->ctx->ctx) { |
| 2822 | PyErr_SetString(PyExc_ValueError, |
| 2823 | "Session refers to a different SSLContext."); |
| 2824 | return -1; |
| 2825 | } |
| 2826 | if (self->socket_type != PY_SSL_CLIENT) { |
| 2827 | PyErr_SetString(PyExc_ValueError, |
| 2828 | "Cannot set session for server-side SSLSocket."); |
| 2829 | return -1; |
| 2830 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 2831 | if (SSL_is_init_finished(self->ssl)) { |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2832 | PyErr_SetString(PyExc_ValueError, |
| 2833 | "Cannot set session after handshake."); |
| 2834 | return -1; |
| 2835 | } |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2836 | /* duplicate session */ |
| 2837 | if ((session = _ssl_session_dup(pysess->session)) == NULL) { |
| 2838 | return -1; |
| 2839 | } |
| 2840 | result = SSL_set_session(self->ssl, session); |
| 2841 | /* free duplicate, SSL_set_session() bumps ref count */ |
| 2842 | SSL_SESSION_free(session); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2843 | if (result == 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2844 | _setSSLError(get_state_sock(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2845 | return -1; |
| 2846 | } |
| 2847 | return 0; |
| 2848 | } |
| 2849 | |
| 2850 | PyDoc_STRVAR(PySSL_set_session_doc, |
| 2851 | "_setter_session(session)\n\ |
| 2852 | \ |
| 2853 | Get / set SSLSession."); |
| 2854 | |
| 2855 | static PyObject * |
| 2856 | PySSL_get_session_reused(PySSLSocket *self, void *closure) { |
| 2857 | if (SSL_session_reused(self->ssl)) { |
| 2858 | Py_RETURN_TRUE; |
| 2859 | } else { |
| 2860 | Py_RETURN_FALSE; |
| 2861 | } |
| 2862 | } |
| 2863 | |
| 2864 | PyDoc_STRVAR(PySSL_get_session_reused_doc, |
| 2865 | "Was the client session reused during handshake?"); |
| 2866 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2867 | static PyGetSetDef ssl_getsetlist[] = { |
| 2868 | {"context", (getter) PySSL_get_context, |
| 2869 | (setter) PySSL_set_context, PySSL_set_context_doc}, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2870 | {"server_side", (getter) PySSL_get_server_side, NULL, |
| 2871 | PySSL_get_server_side_doc}, |
| 2872 | {"server_hostname", (getter) PySSL_get_server_hostname, NULL, |
| 2873 | PySSL_get_server_hostname_doc}, |
| 2874 | {"owner", (getter) PySSL_get_owner, (setter) PySSL_set_owner, |
| 2875 | PySSL_get_owner_doc}, |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2876 | {"session", (getter) PySSL_get_session, |
| 2877 | (setter) PySSL_set_session, PySSL_set_session_doc}, |
| 2878 | {"session_reused", (getter) PySSL_get_session_reused, NULL, |
| 2879 | PySSL_get_session_reused_doc}, |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2880 | {NULL}, /* sentinel */ |
| 2881 | }; |
| 2882 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2883 | static PyMethodDef PySSLMethods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2884 | _SSL__SSLSOCKET_DO_HANDSHAKE_METHODDEF |
| 2885 | _SSL__SSLSOCKET_WRITE_METHODDEF |
| 2886 | _SSL__SSLSOCKET_READ_METHODDEF |
| 2887 | _SSL__SSLSOCKET_PENDING_METHODDEF |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2888 | _SSL__SSLSOCKET_GETPEERCERT_METHODDEF |
| 2889 | _SSL__SSLSOCKET_GET_CHANNEL_BINDING_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2890 | _SSL__SSLSOCKET_CIPHER_METHODDEF |
| 2891 | _SSL__SSLSOCKET_SHARED_CIPHERS_METHODDEF |
| 2892 | _SSL__SSLSOCKET_VERSION_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2893 | _SSL__SSLSOCKET_SELECTED_ALPN_PROTOCOL_METHODDEF |
| 2894 | _SSL__SSLSOCKET_COMPRESSION_METHODDEF |
| 2895 | _SSL__SSLSOCKET_SHUTDOWN_METHODDEF |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2896 | _SSL__SSLSOCKET_VERIFY_CLIENT_POST_HANDSHAKE_METHODDEF |
Christian Heimes | 666991f | 2021-04-26 15:01:40 +0200 | [diff] [blame] | 2897 | _SSL__SSLSOCKET_GET_UNVERIFIED_CHAIN_METHODDEF |
| 2898 | _SSL__SSLSOCKET_GET_VERIFIED_CHAIN_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2899 | {NULL, NULL} |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2900 | }; |
| 2901 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 2902 | static PyType_Slot PySSLSocket_slots[] = { |
| 2903 | {Py_tp_methods, PySSLMethods}, |
| 2904 | {Py_tp_getset, ssl_getsetlist}, |
| 2905 | {Py_tp_dealloc, PySSL_dealloc}, |
| 2906 | {Py_tp_traverse, PySSL_traverse}, |
| 2907 | {Py_tp_clear, PySSL_clear}, |
| 2908 | {0, 0}, |
| 2909 | }; |
| 2910 | |
| 2911 | static PyType_Spec PySSLSocket_spec = { |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 2912 | .name = "_ssl._SSLSocket", |
| 2913 | .basicsize = sizeof(PySSLSocket), |
| 2914 | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | |
| 2915 | Py_TPFLAGS_HAVE_GC), |
| 2916 | .slots = PySSLSocket_slots, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2917 | }; |
| 2918 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2919 | /* |
| 2920 | * _SSLContext objects |
| 2921 | */ |
| 2922 | |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2923 | static int |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2924 | _set_verify_mode(PySSLContext *self, enum py_ssl_cert_requirements n) |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2925 | { |
| 2926 | int mode; |
| 2927 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 2928 | |
| 2929 | switch(n) { |
| 2930 | case PY_SSL_CERT_NONE: |
| 2931 | mode = SSL_VERIFY_NONE; |
| 2932 | break; |
| 2933 | case PY_SSL_CERT_OPTIONAL: |
| 2934 | mode = SSL_VERIFY_PEER; |
| 2935 | break; |
| 2936 | case PY_SSL_CERT_REQUIRED: |
| 2937 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 2938 | break; |
| 2939 | default: |
| 2940 | PyErr_SetString(PyExc_ValueError, |
| 2941 | "invalid value for verify_mode"); |
| 2942 | return -1; |
| 2943 | } |
Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 2944 | |
| 2945 | /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for |
| 2946 | * server sockets and SSL_set_post_handshake_auth() for client. */ |
| 2947 | |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2948 | /* keep current verify cb */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2949 | verify_cb = SSL_CTX_get_verify_callback(self->ctx); |
| 2950 | SSL_CTX_set_verify(self->ctx, mode, verify_cb); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2951 | return 0; |
| 2952 | } |
| 2953 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2954 | /*[clinic input] |
| 2955 | @classmethod |
| 2956 | _ssl._SSLContext.__new__ |
| 2957 | protocol as proto_version: int |
| 2958 | / |
| 2959 | [clinic start generated code]*/ |
| 2960 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2961 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2962 | _ssl__SSLContext_impl(PyTypeObject *type, int proto_version) |
| 2963 | /*[clinic end generated code: output=2cf0d7a0741b6bd1 input=8d58a805b95fc534]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2964 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2965 | PySSLContext *self; |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 2966 | long options; |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 2967 | const SSL_METHOD *method = NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2968 | SSL_CTX *ctx = NULL; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 2969 | X509_VERIFY_PARAM *params; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 2970 | int result; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2971 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2972 | /* slower approach, walk MRO and get borrowed reference to module. |
| 2973 | * _PyType_GetModuleByDef is required for SSLContext subclasses */ |
| 2974 | PyObject *module = _PyType_GetModuleByDef(type, &_sslmodule_def); |
| 2975 | if (module == NULL) { |
| 2976 | PyErr_SetString(PyExc_RuntimeError, |
| 2977 | "Cannot find internal module state"); |
| 2978 | return NULL; |
| 2979 | } |
| 2980 | |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2981 | switch(proto_version) { |
| 2982 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 2983 | case PY_SSL_VERSION_SSL3: |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 2984 | PY_SSL_DEPRECATED("PROTOCOL_SSLv3", 2, NULL); |
| 2985 | method = SSLv3_method(); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2986 | break; |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 2987 | #endif |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 2988 | #if (defined(TLS1_VERSION) && \ |
| 2989 | !defined(OPENSSL_NO_TLS1) && \ |
| 2990 | !defined(OPENSSL_NO_TLS1_METHOD)) |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2991 | case PY_SSL_VERSION_TLS1: |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 2992 | PY_SSL_DEPRECATED("PROTOCOL_TLSv1", 2, NULL); |
| 2993 | method = TLSv1_method(); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2994 | break; |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 2995 | #endif |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 2996 | #if (defined(TLS1_1_VERSION) && \ |
| 2997 | !defined(OPENSSL_NO_TLS1_1) && \ |
| 2998 | !defined(OPENSSL_NO_TLS1_1_METHOD)) |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2999 | case PY_SSL_VERSION_TLS1_1: |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 3000 | PY_SSL_DEPRECATED("PROTOCOL_TLSv1_1", 2, NULL); |
| 3001 | method = TLSv1_1_method(); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3002 | break; |
| 3003 | #endif |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 3004 | #if (defined(TLS1_2_VERSION) && \ |
| 3005 | !defined(OPENSSL_NO_TLS1_2) && \ |
| 3006 | !defined(OPENSSL_NO_TLS1_2_METHOD)) |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3007 | case PY_SSL_VERSION_TLS1_2: |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 3008 | PY_SSL_DEPRECATED("PROTOCOL_TLSv1_2", 2, NULL); |
| 3009 | method = TLSv1_2_method(); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3010 | break; |
| 3011 | #endif |
| 3012 | case PY_SSL_VERSION_TLS: |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 3013 | PY_SSL_DEPRECATED("PROTOCOL_TLS", 2, NULL); |
| 3014 | method = TLS_method(); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3015 | break; |
| 3016 | case PY_SSL_VERSION_TLS_CLIENT: |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 3017 | method = TLS_client_method(); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3018 | break; |
| 3019 | case PY_SSL_VERSION_TLS_SERVER: |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 3020 | method = TLS_server_method(); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3021 | break; |
| 3022 | default: |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 3023 | method = NULL; |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 3024 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3025 | |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 3026 | if (method == NULL) { |
| 3027 | PyErr_Format(PyExc_ValueError, |
| 3028 | "invalid or unsupported protocol version %i", |
| 3029 | proto_version); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3030 | return NULL; |
| 3031 | } |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 3032 | |
| 3033 | PySSL_BEGIN_ALLOW_THREADS |
| 3034 | ctx = SSL_CTX_new(method); |
| 3035 | PySSL_END_ALLOW_THREADS |
| 3036 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3037 | if (ctx == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3038 | _setSSLError(get_ssl_state(module), NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3039 | return NULL; |
| 3040 | } |
| 3041 | |
| 3042 | assert(type != NULL && type->tp_alloc != NULL); |
| 3043 | self = (PySSLContext *) type->tp_alloc(type, 0); |
| 3044 | if (self == NULL) { |
| 3045 | SSL_CTX_free(ctx); |
| 3046 | return NULL; |
| 3047 | } |
| 3048 | self->ctx = ctx; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3049 | self->hostflags = X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3050 | self->protocol = proto_version; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3051 | self->msg_cb = NULL; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3052 | self->keylog_filename = NULL; |
| 3053 | self->keylog_bio = NULL; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3054 | self->alpn_protocols = NULL; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3055 | self->set_sni_cb = NULL; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3056 | self->state = get_ssl_state(module); |
| 3057 | |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3058 | /* Don't check host name by default */ |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3059 | if (proto_version == PY_SSL_VERSION_TLS_CLIENT) { |
| 3060 | self->check_hostname = 1; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3061 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3062 | Py_DECREF(self); |
| 3063 | return NULL; |
| 3064 | } |
| 3065 | } else { |
| 3066 | self->check_hostname = 0; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3067 | if (_set_verify_mode(self, PY_SSL_CERT_NONE) == -1) { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3068 | Py_DECREF(self); |
| 3069 | return NULL; |
| 3070 | } |
| 3071 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3072 | /* Defaults */ |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3073 | options = SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
| 3074 | if (proto_version != PY_SSL_VERSION_SSL2) |
| 3075 | options |= SSL_OP_NO_SSLv2; |
Benjamin Peterson | a9dcdab | 2015-11-11 22:38:41 -0800 | [diff] [blame] | 3076 | if (proto_version != PY_SSL_VERSION_SSL3) |
| 3077 | options |= SSL_OP_NO_SSLv3; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3078 | /* Minimal security flags for server and client side context. |
| 3079 | * Client sockets ignore server-side parameters. */ |
| 3080 | #ifdef SSL_OP_NO_COMPRESSION |
| 3081 | options |= SSL_OP_NO_COMPRESSION; |
| 3082 | #endif |
| 3083 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
| 3084 | options |= SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 3085 | #endif |
| 3086 | #ifdef SSL_OP_SINGLE_DH_USE |
| 3087 | options |= SSL_OP_SINGLE_DH_USE; |
| 3088 | #endif |
| 3089 | #ifdef SSL_OP_SINGLE_ECDH_USE |
| 3090 | options |= SSL_OP_SINGLE_ECDH_USE; |
| 3091 | #endif |
Christian Heimes | 6f37ebc | 2021-04-09 17:59:21 +0200 | [diff] [blame] | 3092 | #ifdef SSL_OP_IGNORE_UNEXPECTED_EOF |
| 3093 | /* Make OpenSSL 3.0.0 behave like 1.1.1 */ |
| 3094 | options |= SSL_OP_IGNORE_UNEXPECTED_EOF; |
| 3095 | #endif |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 3096 | SSL_CTX_set_options(self->ctx, options); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3097 | |
Semen Zhydenko | 1295e11 | 2017-10-15 21:28:31 +0200 | [diff] [blame] | 3098 | /* A bare minimum cipher list without completely broken cipher suites. |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3099 | * It's far from perfect but gives users a better head start. */ |
| 3100 | if (proto_version != PY_SSL_VERSION_SSL2) { |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 3101 | #if PY_SSL_DEFAULT_CIPHERS == 2 |
| 3102 | /* stick to OpenSSL's default settings */ |
| 3103 | result = 1; |
| 3104 | #else |
| 3105 | result = SSL_CTX_set_cipher_list(ctx, PY_SSL_DEFAULT_CIPHER_STRING); |
| 3106 | #endif |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3107 | } else { |
| 3108 | /* SSLv2 needs MD5 */ |
| 3109 | result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL"); |
| 3110 | } |
| 3111 | if (result == 0) { |
| 3112 | Py_DECREF(self); |
| 3113 | ERR_clear_error(); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3114 | PyErr_SetString(get_state_ctx(self)->PySSLErrorObject, |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3115 | "No cipher can be selected."); |
Christian Heimes | e983252 | 2021-05-01 20:53:10 +0200 | [diff] [blame] | 3116 | goto error; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3117 | } |
Christian Heimes | e983252 | 2021-05-01 20:53:10 +0200 | [diff] [blame] | 3118 | #ifdef PY_SSL_MIN_PROTOCOL |
| 3119 | switch(proto_version) { |
| 3120 | case PY_SSL_VERSION_TLS: |
| 3121 | case PY_SSL_VERSION_TLS_CLIENT: |
| 3122 | case PY_SSL_VERSION_TLS_SERVER: |
| 3123 | result = SSL_CTX_set_min_proto_version(ctx, PY_SSL_MIN_PROTOCOL); |
| 3124 | if (result == 0) { |
| 3125 | PyErr_Format(PyExc_ValueError, |
| 3126 | "Failed to set minimum protocol 0x%x", |
| 3127 | PY_SSL_MIN_PROTOCOL); |
| 3128 | goto error; |
| 3129 | } |
| 3130 | break; |
| 3131 | default: |
| 3132 | break; |
| 3133 | } |
| 3134 | #endif |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3135 | |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3136 | /* Set SSL_MODE_RELEASE_BUFFERS. This potentially greatly reduces memory |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 3137 | usage for no cost at all. */ |
| 3138 | SSL_CTX_set_mode(self->ctx, SSL_MODE_RELEASE_BUFFERS); |
Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3139 | |
Antoine Pitrou | fc113ee | 2010-10-13 12:46:13 +0000 | [diff] [blame] | 3140 | #define SID_CTX "Python" |
| 3141 | SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX, |
| 3142 | sizeof(SID_CTX)); |
| 3143 | #undef SID_CTX |
| 3144 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3145 | params = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3146 | /* Improve trust chain building when cross-signed intermediate |
| 3147 | certificates are present. See https://bugs.python.org/issue23476. */ |
| 3148 | X509_VERIFY_PARAM_set_flags(params, X509_V_FLAG_TRUSTED_FIRST); |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3149 | X509_VERIFY_PARAM_set_hostflags(params, self->hostflags); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3150 | |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3151 | #ifdef TLS1_3_VERSION |
| 3152 | self->post_handshake_auth = 0; |
| 3153 | SSL_CTX_set_post_handshake_auth(self->ctx, self->post_handshake_auth); |
| 3154 | #endif |
| 3155 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3156 | return (PyObject *)self; |
Christian Heimes | e983252 | 2021-05-01 20:53:10 +0200 | [diff] [blame] | 3157 | error: |
| 3158 | Py_XDECREF(self); |
| 3159 | ERR_clear_error(); |
| 3160 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3163 | static int |
| 3164 | context_traverse(PySSLContext *self, visitproc visit, void *arg) |
| 3165 | { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3166 | Py_VISIT(self->set_sni_cb); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3167 | Py_VISIT(self->msg_cb); |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 3168 | Py_VISIT(Py_TYPE(self)); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3169 | return 0; |
| 3170 | } |
| 3171 | |
| 3172 | static int |
| 3173 | context_clear(PySSLContext *self) |
| 3174 | { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3175 | Py_CLEAR(self->set_sni_cb); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3176 | Py_CLEAR(self->msg_cb); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3177 | Py_CLEAR(self->keylog_filename); |
| 3178 | if (self->keylog_bio != NULL) { |
| 3179 | PySSL_BEGIN_ALLOW_THREADS |
| 3180 | BIO_free_all(self->keylog_bio); |
| 3181 | PySSL_END_ALLOW_THREADS |
| 3182 | self->keylog_bio = NULL; |
| 3183 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3184 | return 0; |
| 3185 | } |
| 3186 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3187 | static void |
| 3188 | context_dealloc(PySSLContext *self) |
| 3189 | { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 3190 | PyTypeObject *tp = Py_TYPE(self); |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 3191 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
| 3192 | PyObject_GC_UnTrack(self); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3193 | context_clear(self); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3194 | SSL_CTX_free(self->ctx); |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 3195 | PyMem_FREE(self->alpn_protocols); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3196 | Py_TYPE(self)->tp_free(self); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 3197 | Py_DECREF(tp); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3200 | /*[clinic input] |
| 3201 | _ssl._SSLContext.set_ciphers |
| 3202 | cipherlist: str |
| 3203 | / |
| 3204 | [clinic start generated code]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3205 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3206 | static PyObject * |
| 3207 | _ssl__SSLContext_set_ciphers_impl(PySSLContext *self, const char *cipherlist) |
| 3208 | /*[clinic end generated code: output=3a3162f3557c0f3f input=a7ac931b9f3ca7fc]*/ |
| 3209 | { |
| 3210 | int ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3211 | if (ret == 0) { |
Antoine Pitrou | 65ec8ae | 2010-05-16 19:56:32 +0000 | [diff] [blame] | 3212 | /* Clearing the error queue is necessary on some OpenSSL versions, |
| 3213 | otherwise the error will be reported again when another SSL call |
| 3214 | is done. */ |
| 3215 | ERR_clear_error(); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3216 | PyErr_SetString(get_state_ctx(self)->PySSLErrorObject, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3217 | "No cipher can be selected."); |
| 3218 | return NULL; |
| 3219 | } |
| 3220 | Py_RETURN_NONE; |
| 3221 | } |
| 3222 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3223 | /*[clinic input] |
| 3224 | _ssl._SSLContext.get_ciphers |
| 3225 | [clinic start generated code]*/ |
| 3226 | |
| 3227 | static PyObject * |
| 3228 | _ssl__SSLContext_get_ciphers_impl(PySSLContext *self) |
| 3229 | /*[clinic end generated code: output=a56e4d68a406dfc4 input=a2aadc9af89b79c5]*/ |
| 3230 | { |
| 3231 | SSL *ssl = NULL; |
| 3232 | STACK_OF(SSL_CIPHER) *sk = NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 3233 | const SSL_CIPHER *cipher; |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3234 | int i=0; |
| 3235 | PyObject *result = NULL, *dct; |
| 3236 | |
| 3237 | ssl = SSL_new(self->ctx); |
| 3238 | if (ssl == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3239 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3240 | goto exit; |
| 3241 | } |
| 3242 | sk = SSL_get_ciphers(ssl); |
| 3243 | |
| 3244 | result = PyList_New(sk_SSL_CIPHER_num(sk)); |
| 3245 | if (result == NULL) { |
| 3246 | goto exit; |
| 3247 | } |
| 3248 | |
| 3249 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { |
| 3250 | cipher = sk_SSL_CIPHER_value(sk, i); |
| 3251 | dct = cipher_to_dict(cipher); |
| 3252 | if (dct == NULL) { |
| 3253 | Py_CLEAR(result); |
| 3254 | goto exit; |
| 3255 | } |
| 3256 | PyList_SET_ITEM(result, i, dct); |
| 3257 | } |
| 3258 | |
| 3259 | exit: |
| 3260 | if (ssl != NULL) |
| 3261 | SSL_free(ssl); |
| 3262 | return result; |
| 3263 | |
| 3264 | } |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3265 | |
| 3266 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3267 | static int |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3268 | do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen, |
| 3269 | const unsigned char *server_protocols, unsigned int server_protocols_len, |
| 3270 | const unsigned char *client_protocols, unsigned int client_protocols_len) |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3271 | { |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3272 | int ret; |
| 3273 | if (client_protocols == NULL) { |
| 3274 | client_protocols = (unsigned char *)""; |
| 3275 | client_protocols_len = 0; |
| 3276 | } |
| 3277 | if (server_protocols == NULL) { |
| 3278 | server_protocols = (unsigned char *)""; |
| 3279 | server_protocols_len = 0; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3280 | } |
| 3281 | |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3282 | ret = SSL_select_next_proto(out, outlen, |
| 3283 | server_protocols, server_protocols_len, |
| 3284 | client_protocols, client_protocols_len); |
| 3285 | if (alpn && ret != OPENSSL_NPN_NEGOTIATED) |
| 3286 | return SSL_TLSEXT_ERR_NOACK; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3287 | |
| 3288 | return SSL_TLSEXT_ERR_OK; |
| 3289 | } |
| 3290 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3291 | static int |
| 3292 | _selectALPN_cb(SSL *s, |
| 3293 | const unsigned char **out, unsigned char *outlen, |
| 3294 | const unsigned char *client_protocols, unsigned int client_protocols_len, |
| 3295 | void *args) |
| 3296 | { |
| 3297 | PySSLContext *ctx = (PySSLContext *)args; |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3298 | return do_protocol_selection(1, (unsigned char **)out, outlen, |
| 3299 | ctx->alpn_protocols, ctx->alpn_protocols_len, |
| 3300 | client_protocols, client_protocols_len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3301 | } |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3302 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3303 | /*[clinic input] |
| 3304 | _ssl._SSLContext._set_alpn_protocols |
| 3305 | protos: Py_buffer |
| 3306 | / |
| 3307 | [clinic start generated code]*/ |
| 3308 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3309 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3310 | _ssl__SSLContext__set_alpn_protocols_impl(PySSLContext *self, |
| 3311 | Py_buffer *protos) |
| 3312 | /*[clinic end generated code: output=87599a7f76651a9b input=9bba964595d519be]*/ |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3313 | { |
Victor Stinner | 5a61559 | 2017-09-14 01:10:30 -0700 | [diff] [blame] | 3314 | if ((size_t)protos->len > UINT_MAX) { |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3315 | PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | d53fe5f | 2019-03-13 22:59:55 +0200 | [diff] [blame] | 3316 | "protocols longer than %u bytes", UINT_MAX); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3317 | return NULL; |
| 3318 | } |
| 3319 | |
Victor Stinner | 00d7abd | 2020-12-01 09:56:42 +0100 | [diff] [blame] | 3320 | PyMem_Free(self->alpn_protocols); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3321 | self->alpn_protocols = PyMem_Malloc(protos->len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3322 | if (!self->alpn_protocols) |
| 3323 | return PyErr_NoMemory(); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3324 | memcpy(self->alpn_protocols, protos->buf, protos->len); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3325 | self->alpn_protocols_len = (unsigned int)protos->len; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3326 | |
| 3327 | if (SSL_CTX_set_alpn_protos(self->ctx, self->alpn_protocols, self->alpn_protocols_len)) |
| 3328 | return PyErr_NoMemory(); |
| 3329 | SSL_CTX_set_alpn_select_cb(self->ctx, _selectALPN_cb, self); |
| 3330 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3331 | Py_RETURN_NONE; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3332 | } |
| 3333 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3334 | static PyObject * |
| 3335 | get_verify_mode(PySSLContext *self, void *c) |
| 3336 | { |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3337 | /* ignore SSL_VERIFY_CLIENT_ONCE and SSL_VERIFY_POST_HANDSHAKE */ |
| 3338 | int mask = (SSL_VERIFY_NONE | SSL_VERIFY_PEER | |
| 3339 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT); |
| 3340 | switch (SSL_CTX_get_verify_mode(self->ctx) & mask) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3341 | case SSL_VERIFY_NONE: |
| 3342 | return PyLong_FromLong(PY_SSL_CERT_NONE); |
| 3343 | case SSL_VERIFY_PEER: |
| 3344 | return PyLong_FromLong(PY_SSL_CERT_OPTIONAL); |
| 3345 | case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT: |
| 3346 | return PyLong_FromLong(PY_SSL_CERT_REQUIRED); |
| 3347 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3348 | PyErr_SetString(get_state_ctx(self)->PySSLErrorObject, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3349 | "invalid return value from SSL_CTX_get_verify_mode"); |
| 3350 | return NULL; |
| 3351 | } |
| 3352 | |
| 3353 | static int |
| 3354 | set_verify_mode(PySSLContext *self, PyObject *arg, void *c) |
| 3355 | { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3356 | int n; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3357 | if (!PyArg_Parse(arg, "i", &n)) |
| 3358 | return -1; |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3359 | if (n == PY_SSL_CERT_NONE && self->check_hostname) { |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3360 | PyErr_SetString(PyExc_ValueError, |
| 3361 | "Cannot set verify_mode to CERT_NONE when " |
| 3362 | "check_hostname is enabled."); |
| 3363 | return -1; |
| 3364 | } |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3365 | return _set_verify_mode(self, n); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
| 3368 | static PyObject * |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3369 | get_verify_flags(PySSLContext *self, void *c) |
| 3370 | { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3371 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3372 | unsigned long flags; |
| 3373 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3374 | param = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3375 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3376 | return PyLong_FromUnsignedLong(flags); |
| 3377 | } |
| 3378 | |
| 3379 | static int |
| 3380 | set_verify_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3381 | { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3382 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3383 | unsigned long new_flags, flags, set, clear; |
| 3384 | |
| 3385 | if (!PyArg_Parse(arg, "k", &new_flags)) |
| 3386 | return -1; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3387 | param = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3388 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3389 | clear = flags & ~new_flags; |
| 3390 | set = ~flags & new_flags; |
| 3391 | if (clear) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3392 | if (!X509_VERIFY_PARAM_clear_flags(param, clear)) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3393 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3394 | return -1; |
| 3395 | } |
| 3396 | } |
| 3397 | if (set) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3398 | if (!X509_VERIFY_PARAM_set_flags(param, set)) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3399 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3400 | return -1; |
| 3401 | } |
| 3402 | } |
| 3403 | return 0; |
| 3404 | } |
| 3405 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 3406 | /* Getter and setter for protocol version */ |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 3407 | static int |
| 3408 | set_min_max_proto_version(PySSLContext *self, PyObject *arg, int what) |
| 3409 | { |
| 3410 | long v; |
| 3411 | int result; |
| 3412 | |
| 3413 | if (!PyArg_Parse(arg, "l", &v)) |
| 3414 | return -1; |
| 3415 | if (v > INT_MAX) { |
| 3416 | PyErr_SetString(PyExc_OverflowError, "Option is too long"); |
| 3417 | return -1; |
| 3418 | } |
| 3419 | |
| 3420 | switch(self->protocol) { |
| 3421 | case PY_SSL_VERSION_TLS_CLIENT: /* fall through */ |
| 3422 | case PY_SSL_VERSION_TLS_SERVER: /* fall through */ |
| 3423 | case PY_SSL_VERSION_TLS: |
| 3424 | break; |
| 3425 | default: |
| 3426 | PyErr_SetString( |
| 3427 | PyExc_ValueError, |
| 3428 | "The context's protocol doesn't support modification of " |
| 3429 | "highest and lowest version." |
| 3430 | ); |
| 3431 | return -1; |
| 3432 | } |
| 3433 | |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 3434 | /* check for deprecations and supported values */ |
| 3435 | switch(v) { |
| 3436 | case PY_PROTO_SSLv3: |
| 3437 | PY_SSL_DEPRECATED("TLSVersion.SSLv3", 2, -1); |
| 3438 | break; |
| 3439 | case PY_PROTO_TLSv1: |
| 3440 | PY_SSL_DEPRECATED("TLSVersion.TLSv1", 2, -1); |
| 3441 | break; |
| 3442 | case PY_PROTO_TLSv1_1: |
| 3443 | PY_SSL_DEPRECATED("TLSVersion.TLSv1_1", 2, -1); |
| 3444 | break; |
| 3445 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3446 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3447 | case PY_PROTO_TLSv1_2: |
| 3448 | case PY_PROTO_TLSv1_3: |
| 3449 | /* ok */ |
| 3450 | break; |
| 3451 | default: |
| 3452 | PyErr_Format(PyExc_ValueError, |
| 3453 | "Unsupported TLS/SSL version 0x%x", v); |
| 3454 | return -1; |
| 3455 | } |
| 3456 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 3457 | if (what == 0) { |
| 3458 | switch(v) { |
| 3459 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3460 | v = 0; |
| 3461 | break; |
| 3462 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3463 | /* Emulate max for set_min_proto_version */ |
| 3464 | v = PY_PROTO_MAXIMUM_AVAILABLE; |
| 3465 | break; |
| 3466 | default: |
| 3467 | break; |
| 3468 | } |
| 3469 | result = SSL_CTX_set_min_proto_version(self->ctx, v); |
| 3470 | } |
| 3471 | else { |
| 3472 | switch(v) { |
| 3473 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3474 | v = 0; |
| 3475 | break; |
| 3476 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3477 | /* Emulate max for set_min_proto_version */ |
| 3478 | v = PY_PROTO_MINIMUM_AVAILABLE; |
| 3479 | break; |
| 3480 | default: |
| 3481 | break; |
| 3482 | } |
| 3483 | result = SSL_CTX_set_max_proto_version(self->ctx, v); |
| 3484 | } |
| 3485 | if (result == 0) { |
| 3486 | PyErr_Format(PyExc_ValueError, |
| 3487 | "Unsupported protocol version 0x%x", v); |
| 3488 | return -1; |
| 3489 | } |
| 3490 | return 0; |
| 3491 | } |
| 3492 | |
| 3493 | static PyObject * |
| 3494 | get_minimum_version(PySSLContext *self, void *c) |
| 3495 | { |
| 3496 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MIN_PROTO_VERSION, 0, NULL); |
| 3497 | if (v == 0) { |
| 3498 | v = PY_PROTO_MINIMUM_SUPPORTED; |
| 3499 | } |
| 3500 | return PyLong_FromLong(v); |
| 3501 | } |
| 3502 | |
| 3503 | static int |
| 3504 | set_minimum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3505 | { |
| 3506 | return set_min_max_proto_version(self, arg, 0); |
| 3507 | } |
| 3508 | |
| 3509 | static PyObject * |
| 3510 | get_maximum_version(PySSLContext *self, void *c) |
| 3511 | { |
| 3512 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MAX_PROTO_VERSION, 0, NULL); |
| 3513 | if (v == 0) { |
| 3514 | v = PY_PROTO_MAXIMUM_SUPPORTED; |
| 3515 | } |
| 3516 | return PyLong_FromLong(v); |
| 3517 | } |
| 3518 | |
| 3519 | static int |
| 3520 | set_maximum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3521 | { |
| 3522 | return set_min_max_proto_version(self, arg, 1); |
| 3523 | } |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 3524 | |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 3525 | #ifdef TLS1_3_VERSION |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3526 | static PyObject * |
| 3527 | get_num_tickets(PySSLContext *self, void *c) |
| 3528 | { |
Victor Stinner | 76611c7 | 2019-07-09 13:30:52 +0200 | [diff] [blame] | 3529 | return PyLong_FromSize_t(SSL_CTX_get_num_tickets(self->ctx)); |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | static int |
| 3533 | set_num_tickets(PySSLContext *self, PyObject *arg, void *c) |
| 3534 | { |
| 3535 | long num; |
| 3536 | if (!PyArg_Parse(arg, "l", &num)) |
| 3537 | return -1; |
| 3538 | if (num < 0) { |
| 3539 | PyErr_SetString(PyExc_ValueError, "value must be non-negative"); |
| 3540 | return -1; |
| 3541 | } |
| 3542 | if (self->protocol != PY_SSL_VERSION_TLS_SERVER) { |
| 3543 | PyErr_SetString(PyExc_ValueError, |
| 3544 | "SSLContext is not a server context."); |
| 3545 | return -1; |
| 3546 | } |
| 3547 | if (SSL_CTX_set_num_tickets(self->ctx, num) != 1) { |
| 3548 | PyErr_SetString(PyExc_ValueError, "failed to set num tickets."); |
| 3549 | return -1; |
| 3550 | } |
| 3551 | return 0; |
| 3552 | } |
| 3553 | |
| 3554 | PyDoc_STRVAR(PySSLContext_num_tickets_doc, |
| 3555 | "Control the number of TLSv1.3 session tickets"); |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 3556 | #endif /* TLS1_3_VERSION */ |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3557 | |
matthewhughes934 | 8e836bb | 2020-07-17 09:59:15 +0100 | [diff] [blame] | 3558 | static PyObject * |
| 3559 | get_security_level(PySSLContext *self, void *c) |
| 3560 | { |
| 3561 | return PyLong_FromLong(SSL_CTX_get_security_level(self->ctx)); |
| 3562 | } |
| 3563 | PyDoc_STRVAR(PySSLContext_security_level_doc, "The current security level"); |
matthewhughes934 | 8e836bb | 2020-07-17 09:59:15 +0100 | [diff] [blame] | 3564 | |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3565 | static PyObject * |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3566 | get_options(PySSLContext *self, void *c) |
| 3567 | { |
| 3568 | return PyLong_FromLong(SSL_CTX_get_options(self->ctx)); |
| 3569 | } |
| 3570 | |
| 3571 | static int |
| 3572 | set_options(PySSLContext *self, PyObject *arg, void *c) |
| 3573 | { |
| 3574 | long new_opts, opts, set, clear; |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 3575 | long opt_no = ( |
| 3576 | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | |
| 3577 | SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1_2 | SSL_OP_NO_TLSv1_2 |
| 3578 | ); |
| 3579 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3580 | if (!PyArg_Parse(arg, "l", &new_opts)) |
| 3581 | return -1; |
| 3582 | opts = SSL_CTX_get_options(self->ctx); |
| 3583 | clear = opts & ~new_opts; |
| 3584 | set = ~opts & new_opts; |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 3585 | |
| 3586 | if ((set & opt_no) != 0) { |
| 3587 | if (_ssl_deprecated("Setting OP_NO_SSL* or SSL_NO_TLS* options is " |
| 3588 | "deprecated", 2) < 0) { |
| 3589 | return -1; |
| 3590 | } |
| 3591 | } |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3592 | if (clear) { |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3593 | SSL_CTX_clear_options(self->ctx, clear); |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3594 | } |
| 3595 | if (set) |
| 3596 | SSL_CTX_set_options(self->ctx, set); |
| 3597 | return 0; |
| 3598 | } |
| 3599 | |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3600 | static PyObject * |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3601 | get_host_flags(PySSLContext *self, void *c) |
| 3602 | { |
| 3603 | return PyLong_FromUnsignedLong(self->hostflags); |
| 3604 | } |
| 3605 | |
| 3606 | static int |
| 3607 | set_host_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3608 | { |
| 3609 | X509_VERIFY_PARAM *param; |
| 3610 | unsigned int new_flags = 0; |
| 3611 | |
| 3612 | if (!PyArg_Parse(arg, "I", &new_flags)) |
| 3613 | return -1; |
| 3614 | |
| 3615 | param = SSL_CTX_get0_param(self->ctx); |
| 3616 | self->hostflags = new_flags; |
| 3617 | X509_VERIFY_PARAM_set_hostflags(param, new_flags); |
| 3618 | return 0; |
| 3619 | } |
| 3620 | |
| 3621 | static PyObject * |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3622 | get_check_hostname(PySSLContext *self, void *c) |
| 3623 | { |
| 3624 | return PyBool_FromLong(self->check_hostname); |
| 3625 | } |
| 3626 | |
| 3627 | static int |
| 3628 | set_check_hostname(PySSLContext *self, PyObject *arg, void *c) |
| 3629 | { |
| 3630 | int check_hostname; |
| 3631 | if (!PyArg_Parse(arg, "p", &check_hostname)) |
| 3632 | return -1; |
| 3633 | if (check_hostname && |
| 3634 | SSL_CTX_get_verify_mode(self->ctx) == SSL_VERIFY_NONE) { |
Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3635 | /* check_hostname = True sets verify_mode = CERT_REQUIRED */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3636 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3637 | return -1; |
| 3638 | } |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3639 | } |
| 3640 | self->check_hostname = check_hostname; |
| 3641 | return 0; |
| 3642 | } |
| 3643 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3644 | static PyObject * |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3645 | get_post_handshake_auth(PySSLContext *self, void *c) { |
| 3646 | #if TLS1_3_VERSION |
| 3647 | return PyBool_FromLong(self->post_handshake_auth); |
| 3648 | #else |
| 3649 | Py_RETURN_NONE; |
| 3650 | #endif |
| 3651 | } |
| 3652 | |
| 3653 | #if TLS1_3_VERSION |
| 3654 | static int |
| 3655 | set_post_handshake_auth(PySSLContext *self, PyObject *arg, void *c) { |
Zackery Spytz | 842acaa | 2018-12-17 07:52:45 -0700 | [diff] [blame] | 3656 | if (arg == NULL) { |
| 3657 | PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); |
| 3658 | return -1; |
| 3659 | } |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3660 | int pha = PyObject_IsTrue(arg); |
| 3661 | |
| 3662 | if (pha == -1) { |
| 3663 | return -1; |
| 3664 | } |
| 3665 | self->post_handshake_auth = pha; |
| 3666 | |
Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 3667 | /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for |
| 3668 | * server sockets and SSL_set_post_handshake_auth() for client. */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3669 | |
| 3670 | return 0; |
| 3671 | } |
| 3672 | #endif |
| 3673 | |
| 3674 | static PyObject * |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3675 | get_protocol(PySSLContext *self, void *c) { |
| 3676 | return PyLong_FromLong(self->protocol); |
| 3677 | } |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3678 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3679 | typedef struct { |
| 3680 | PyThreadState *thread_state; |
| 3681 | PyObject *callable; |
| 3682 | char *password; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3683 | int size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3684 | int error; |
| 3685 | } _PySSLPasswordInfo; |
| 3686 | |
| 3687 | static int |
| 3688 | _pwinfo_set(_PySSLPasswordInfo *pw_info, PyObject* password, |
| 3689 | const char *bad_type_error) |
| 3690 | { |
| 3691 | /* Set the password and size fields of a _PySSLPasswordInfo struct |
| 3692 | from a unicode, bytes, or byte array object. |
| 3693 | The password field will be dynamically allocated and must be freed |
| 3694 | by the caller */ |
| 3695 | PyObject *password_bytes = NULL; |
| 3696 | const char *data = NULL; |
| 3697 | Py_ssize_t size; |
| 3698 | |
| 3699 | if (PyUnicode_Check(password)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3700 | password_bytes = PyUnicode_AsUTF8String(password); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3701 | if (!password_bytes) { |
| 3702 | goto error; |
| 3703 | } |
| 3704 | data = PyBytes_AS_STRING(password_bytes); |
| 3705 | size = PyBytes_GET_SIZE(password_bytes); |
| 3706 | } else if (PyBytes_Check(password)) { |
| 3707 | data = PyBytes_AS_STRING(password); |
| 3708 | size = PyBytes_GET_SIZE(password); |
| 3709 | } else if (PyByteArray_Check(password)) { |
| 3710 | data = PyByteArray_AS_STRING(password); |
| 3711 | size = PyByteArray_GET_SIZE(password); |
| 3712 | } else { |
| 3713 | PyErr_SetString(PyExc_TypeError, bad_type_error); |
| 3714 | goto error; |
| 3715 | } |
| 3716 | |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3717 | if (size > (Py_ssize_t)INT_MAX) { |
| 3718 | PyErr_Format(PyExc_ValueError, |
| 3719 | "password cannot be longer than %d bytes", INT_MAX); |
| 3720 | goto error; |
| 3721 | } |
| 3722 | |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3723 | PyMem_Free(pw_info->password); |
| 3724 | pw_info->password = PyMem_Malloc(size); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3725 | if (!pw_info->password) { |
| 3726 | PyErr_SetString(PyExc_MemoryError, |
| 3727 | "unable to allocate password buffer"); |
| 3728 | goto error; |
| 3729 | } |
| 3730 | memcpy(pw_info->password, data, size); |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3731 | pw_info->size = (int)size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3732 | |
| 3733 | Py_XDECREF(password_bytes); |
| 3734 | return 1; |
| 3735 | |
| 3736 | error: |
| 3737 | Py_XDECREF(password_bytes); |
| 3738 | return 0; |
| 3739 | } |
| 3740 | |
| 3741 | static int |
| 3742 | _password_callback(char *buf, int size, int rwflag, void *userdata) |
| 3743 | { |
| 3744 | _PySSLPasswordInfo *pw_info = (_PySSLPasswordInfo*) userdata; |
| 3745 | PyObject *fn_ret = NULL; |
| 3746 | |
| 3747 | PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); |
| 3748 | |
Christian Heimes | d3b73f3 | 2021-04-09 15:23:38 +0200 | [diff] [blame] | 3749 | if (pw_info->error) { |
| 3750 | /* already failed previously. OpenSSL 3.0.0-alpha14 invokes the |
| 3751 | * callback multiple times which can lead to fatal Python error in |
| 3752 | * exception check. */ |
| 3753 | goto error; |
| 3754 | } |
| 3755 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3756 | if (pw_info->callable) { |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3757 | fn_ret = _PyObject_CallNoArg(pw_info->callable); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3758 | if (!fn_ret) { |
| 3759 | /* TODO: It would be nice to move _ctypes_add_traceback() into the |
| 3760 | core python API, so we could use it to add a frame here */ |
| 3761 | goto error; |
| 3762 | } |
| 3763 | |
| 3764 | if (!_pwinfo_set(pw_info, fn_ret, |
| 3765 | "password callback must return a string")) { |
| 3766 | goto error; |
| 3767 | } |
| 3768 | Py_CLEAR(fn_ret); |
| 3769 | } |
| 3770 | |
| 3771 | if (pw_info->size > size) { |
| 3772 | PyErr_Format(PyExc_ValueError, |
| 3773 | "password cannot be longer than %d bytes", size); |
| 3774 | goto error; |
| 3775 | } |
| 3776 | |
| 3777 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3778 | memcpy(buf, pw_info->password, pw_info->size); |
| 3779 | return pw_info->size; |
| 3780 | |
| 3781 | error: |
| 3782 | Py_XDECREF(fn_ret); |
| 3783 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3784 | pw_info->error = 1; |
| 3785 | return -1; |
| 3786 | } |
| 3787 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3788 | /*[clinic input] |
| 3789 | _ssl._SSLContext.load_cert_chain |
| 3790 | certfile: object |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3791 | keyfile: object = None |
| 3792 | password: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3793 | |
| 3794 | [clinic start generated code]*/ |
| 3795 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3796 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3797 | _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, |
| 3798 | PyObject *keyfile, PyObject *password) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3799 | /*[clinic end generated code: output=9480bc1c380e2095 input=30bc7e967ea01a58]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3800 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3801 | PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3802 | pem_password_cb *orig_passwd_cb = SSL_CTX_get_default_passwd_cb(self->ctx); |
| 3803 | void *orig_passwd_userdata = SSL_CTX_get_default_passwd_cb_userdata(self->ctx); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3804 | _PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 }; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3805 | int r; |
| 3806 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3807 | errno = 0; |
Antoine Pitrou | 67e8e56 | 2010-09-01 20:55:41 +0000 | [diff] [blame] | 3808 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3809 | if (keyfile == Py_None) |
| 3810 | keyfile = NULL; |
| 3811 | if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3812 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3813 | PyErr_SetString(PyExc_TypeError, |
| 3814 | "certfile should be a valid filesystem path"); |
| 3815 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3816 | return NULL; |
| 3817 | } |
| 3818 | if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3819 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3820 | PyErr_SetString(PyExc_TypeError, |
| 3821 | "keyfile should be a valid filesystem path"); |
| 3822 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3823 | goto error; |
| 3824 | } |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3825 | if (password != Py_None) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3826 | if (PyCallable_Check(password)) { |
| 3827 | pw_info.callable = password; |
| 3828 | } else if (!_pwinfo_set(&pw_info, password, |
| 3829 | "password should be a string or callable")) { |
| 3830 | goto error; |
| 3831 | } |
| 3832 | SSL_CTX_set_default_passwd_cb(self->ctx, _password_callback); |
| 3833 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, &pw_info); |
| 3834 | } |
| 3835 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3836 | r = SSL_CTX_use_certificate_chain_file(self->ctx, |
| 3837 | PyBytes_AS_STRING(certfile_bytes)); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3838 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3839 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3840 | if (pw_info.error) { |
| 3841 | ERR_clear_error(); |
| 3842 | /* the password callback has already set the error information */ |
| 3843 | } |
| 3844 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 3845 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3846 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3847 | } |
| 3848 | else { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3849 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3850 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3851 | goto error; |
| 3852 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3853 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 9c25486 | 2011-04-03 18:15:34 +0200 | [diff] [blame] | 3854 | r = SSL_CTX_use_PrivateKey_file(self->ctx, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3855 | PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes), |
| 3856 | SSL_FILETYPE_PEM); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3857 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| 3858 | Py_CLEAR(keyfile_bytes); |
| 3859 | Py_CLEAR(certfile_bytes); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3860 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3861 | if (pw_info.error) { |
| 3862 | ERR_clear_error(); |
| 3863 | /* the password callback has already set the error information */ |
| 3864 | } |
| 3865 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 3866 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3867 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3868 | } |
| 3869 | else { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3870 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3871 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3872 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3873 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3874 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3875 | r = SSL_CTX_check_private_key(self->ctx); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3876 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3877 | if (r != 1) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3878 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3879 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3880 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3881 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 3882 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3883 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3884 | Py_RETURN_NONE; |
| 3885 | |
| 3886 | error: |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3887 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 3888 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3889 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3890 | Py_XDECREF(keyfile_bytes); |
| 3891 | Py_XDECREF(certfile_bytes); |
| 3892 | return NULL; |
| 3893 | } |
| 3894 | |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3895 | /* internal helper function, returns -1 on error |
| 3896 | */ |
| 3897 | static int |
Serhiy Storchaka | 8f87eef | 2020-04-12 14:58:27 +0300 | [diff] [blame] | 3898 | _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len, |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3899 | int filetype) |
| 3900 | { |
| 3901 | BIO *biobuf = NULL; |
| 3902 | X509_STORE *store; |
Christian Heimes | b9ad88b | 2021-04-23 13:51:40 +0200 | [diff] [blame] | 3903 | int retval = -1, err, loaded = 0; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3904 | |
| 3905 | assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM); |
| 3906 | |
| 3907 | if (len <= 0) { |
| 3908 | PyErr_SetString(PyExc_ValueError, |
| 3909 | "Empty certificate data"); |
| 3910 | return -1; |
| 3911 | } else if (len > INT_MAX) { |
| 3912 | PyErr_SetString(PyExc_OverflowError, |
| 3913 | "Certificate data is too long."); |
| 3914 | return -1; |
| 3915 | } |
| 3916 | |
Christian Heimes | 1dbf61f | 2013-11-22 00:34:18 +0100 | [diff] [blame] | 3917 | biobuf = BIO_new_mem_buf(data, (int)len); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3918 | if (biobuf == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3919 | _setSSLError(get_state_ctx(self), "Can't allocate buffer", 0, __FILE__, __LINE__); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3920 | return -1; |
| 3921 | } |
| 3922 | |
| 3923 | store = SSL_CTX_get_cert_store(self->ctx); |
| 3924 | assert(store != NULL); |
| 3925 | |
| 3926 | while (1) { |
| 3927 | X509 *cert = NULL; |
| 3928 | int r; |
| 3929 | |
| 3930 | if (filetype == SSL_FILETYPE_ASN1) { |
| 3931 | cert = d2i_X509_bio(biobuf, NULL); |
| 3932 | } else { |
| 3933 | cert = PEM_read_bio_X509(biobuf, NULL, |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3934 | SSL_CTX_get_default_passwd_cb(self->ctx), |
| 3935 | SSL_CTX_get_default_passwd_cb_userdata(self->ctx) |
| 3936 | ); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3937 | } |
| 3938 | if (cert == NULL) { |
| 3939 | break; |
| 3940 | } |
| 3941 | r = X509_STORE_add_cert(store, cert); |
| 3942 | X509_free(cert); |
| 3943 | if (!r) { |
| 3944 | err = ERR_peek_last_error(); |
| 3945 | if ((ERR_GET_LIB(err) == ERR_LIB_X509) && |
| 3946 | (ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) { |
| 3947 | /* cert already in hash table, not an error */ |
| 3948 | ERR_clear_error(); |
| 3949 | } else { |
| 3950 | break; |
| 3951 | } |
| 3952 | } |
| 3953 | loaded++; |
| 3954 | } |
| 3955 | |
| 3956 | err = ERR_peek_last_error(); |
Christian Heimes | b9ad88b | 2021-04-23 13:51:40 +0200 | [diff] [blame] | 3957 | if (loaded == 0) { |
| 3958 | const char *msg = NULL; |
| 3959 | if (filetype == SSL_FILETYPE_PEM) { |
| 3960 | msg = "no start line: cadata does not contain a certificate"; |
| 3961 | } else { |
| 3962 | msg = "not enough data: cadata does not contain a certificate"; |
| 3963 | } |
| 3964 | _setSSLError(get_state_ctx(self), msg, 0, __FILE__, __LINE__); |
| 3965 | retval = -1; |
| 3966 | } else if ((filetype == SSL_FILETYPE_ASN1) && |
| 3967 | (ERR_GET_LIB(err) == ERR_LIB_ASN1) && |
| 3968 | (ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) { |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3969 | /* EOF ASN1 file, not an error */ |
| 3970 | ERR_clear_error(); |
| 3971 | retval = 0; |
| 3972 | } else if ((filetype == SSL_FILETYPE_PEM) && |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3973 | (ERR_GET_LIB(err) == ERR_LIB_PEM) && |
| 3974 | (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 3975 | /* EOF PEM file, not an error */ |
| 3976 | ERR_clear_error(); |
| 3977 | retval = 0; |
Christian Heimes | b9ad88b | 2021-04-23 13:51:40 +0200 | [diff] [blame] | 3978 | } else if (err != 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3979 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3980 | retval = -1; |
Christian Heimes | b9ad88b | 2021-04-23 13:51:40 +0200 | [diff] [blame] | 3981 | } else { |
| 3982 | retval = 0; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3983 | } |
| 3984 | |
| 3985 | BIO_free(biobuf); |
| 3986 | return retval; |
| 3987 | } |
| 3988 | |
| 3989 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3990 | /*[clinic input] |
| 3991 | _ssl._SSLContext.load_verify_locations |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3992 | cafile: object = None |
| 3993 | capath: object = None |
| 3994 | cadata: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3995 | |
| 3996 | [clinic start generated code]*/ |
| 3997 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3998 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3999 | _ssl__SSLContext_load_verify_locations_impl(PySSLContext *self, |
| 4000 | PyObject *cafile, |
| 4001 | PyObject *capath, |
| 4002 | PyObject *cadata) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4003 | /*[clinic end generated code: output=454c7e41230ca551 input=42ecfe258233e194]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4004 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4005 | PyObject *cafile_bytes = NULL, *capath_bytes = NULL; |
| 4006 | const char *cafile_buf = NULL, *capath_buf = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4007 | int r = 0, ok = 1; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4008 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 4009 | errno = 0; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4010 | if (cafile == Py_None) |
| 4011 | cafile = NULL; |
| 4012 | if (capath == Py_None) |
| 4013 | capath = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4014 | if (cadata == Py_None) |
| 4015 | cadata = NULL; |
| 4016 | |
| 4017 | if (cafile == NULL && capath == NULL && cadata == NULL) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4018 | PyErr_SetString(PyExc_TypeError, |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4019 | "cafile, capath and cadata cannot be all omitted"); |
| 4020 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4021 | } |
| 4022 | if (cafile && !PyUnicode_FSConverter(cafile, &cafile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4023 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4024 | PyErr_SetString(PyExc_TypeError, |
| 4025 | "cafile should be a valid filesystem path"); |
| 4026 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4027 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4028 | } |
| 4029 | if (capath && !PyUnicode_FSConverter(capath, &capath_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4030 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4031 | PyErr_SetString(PyExc_TypeError, |
| 4032 | "capath should be a valid filesystem path"); |
| 4033 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4034 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4035 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4036 | |
| 4037 | /* validata cadata type and load cadata */ |
| 4038 | if (cadata) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4039 | if (PyUnicode_Check(cadata)) { |
| 4040 | PyObject *cadata_ascii = PyUnicode_AsASCIIString(cadata); |
| 4041 | if (cadata_ascii == NULL) { |
| 4042 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 4043 | goto invalid_cadata; |
| 4044 | } |
| 4045 | goto error; |
| 4046 | } |
| 4047 | r = _add_ca_certs(self, |
| 4048 | PyBytes_AS_STRING(cadata_ascii), |
| 4049 | PyBytes_GET_SIZE(cadata_ascii), |
| 4050 | SSL_FILETYPE_PEM); |
| 4051 | Py_DECREF(cadata_ascii); |
| 4052 | if (r == -1) { |
| 4053 | goto error; |
| 4054 | } |
| 4055 | } |
| 4056 | else if (PyObject_CheckBuffer(cadata)) { |
| 4057 | Py_buffer buf; |
| 4058 | if (PyObject_GetBuffer(cadata, &buf, PyBUF_SIMPLE)) { |
| 4059 | goto error; |
| 4060 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4061 | if (!PyBuffer_IsContiguous(&buf, 'C') || buf.ndim > 1) { |
| 4062 | PyBuffer_Release(&buf); |
| 4063 | PyErr_SetString(PyExc_TypeError, |
| 4064 | "cadata should be a contiguous buffer with " |
| 4065 | "a single dimension"); |
| 4066 | goto error; |
| 4067 | } |
| 4068 | r = _add_ca_certs(self, buf.buf, buf.len, SSL_FILETYPE_ASN1); |
| 4069 | PyBuffer_Release(&buf); |
| 4070 | if (r == -1) { |
| 4071 | goto error; |
| 4072 | } |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 4073 | } |
| 4074 | else { |
| 4075 | invalid_cadata: |
| 4076 | PyErr_SetString(PyExc_TypeError, |
| 4077 | "cadata should be an ASCII string or a " |
| 4078 | "bytes-like object"); |
| 4079 | goto error; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4080 | } |
| 4081 | } |
| 4082 | |
| 4083 | /* load cafile or capath */ |
| 4084 | if (cafile || capath) { |
| 4085 | if (cafile) |
| 4086 | cafile_buf = PyBytes_AS_STRING(cafile_bytes); |
| 4087 | if (capath) |
| 4088 | capath_buf = PyBytes_AS_STRING(capath_bytes); |
| 4089 | PySSL_BEGIN_ALLOW_THREADS |
| 4090 | r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf); |
| 4091 | PySSL_END_ALLOW_THREADS |
| 4092 | if (r != 1) { |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4093 | if (errno != 0) { |
| 4094 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 4095 | PyErr_SetFromErrno(PyExc_OSError); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4096 | } |
| 4097 | else { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4098 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4099 | } |
| 4100 | goto error; |
| 4101 | } |
| 4102 | } |
| 4103 | goto end; |
| 4104 | |
| 4105 | error: |
| 4106 | ok = 0; |
| 4107 | end: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4108 | Py_XDECREF(cafile_bytes); |
| 4109 | Py_XDECREF(capath_bytes); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 4110 | if (ok) { |
| 4111 | Py_RETURN_NONE; |
| 4112 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4113 | return NULL; |
| 4114 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4117 | /*[clinic input] |
| 4118 | _ssl._SSLContext.load_dh_params |
| 4119 | path as filepath: object |
| 4120 | / |
| 4121 | |
| 4122 | [clinic start generated code]*/ |
| 4123 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4124 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4125 | _ssl__SSLContext_load_dh_params(PySSLContext *self, PyObject *filepath) |
| 4126 | /*[clinic end generated code: output=1c8e57a38e055af0 input=c8871f3c796ae1d6]*/ |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4127 | { |
| 4128 | FILE *f; |
| 4129 | DH *dh; |
| 4130 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4131 | f = _Py_fopen_obj(filepath, "rb"); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4132 | if (f == NULL) |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4133 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 4134 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4135 | errno = 0; |
| 4136 | PySSL_BEGIN_ALLOW_THREADS |
| 4137 | dh = PEM_read_DHparams(f, NULL, NULL, NULL); |
Antoine Pitrou | 457a229 | 2013-01-12 21:43:45 +0100 | [diff] [blame] | 4138 | fclose(f); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4139 | PySSL_END_ALLOW_THREADS |
| 4140 | if (dh == NULL) { |
| 4141 | if (errno != 0) { |
| 4142 | ERR_clear_error(); |
| 4143 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); |
| 4144 | } |
| 4145 | else { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4146 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4147 | } |
| 4148 | return NULL; |
| 4149 | } |
Zackery Spytz | aebc049 | 2020-07-07 22:21:58 -0600 | [diff] [blame] | 4150 | if (!SSL_CTX_set_tmp_dh(self->ctx, dh)) { |
| 4151 | DH_free(dh); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4152 | return _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Zackery Spytz | aebc049 | 2020-07-07 22:21:58 -0600 | [diff] [blame] | 4153 | } |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4154 | DH_free(dh); |
| 4155 | Py_RETURN_NONE; |
| 4156 | } |
| 4157 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4158 | /*[clinic input] |
| 4159 | _ssl._SSLContext._wrap_socket |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4160 | sock: object(subclass_of="get_state_ctx(self)->Sock_Type") |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4161 | server_side: int |
| 4162 | server_hostname as hostname_obj: object = None |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4163 | * |
| 4164 | owner: object = None |
| 4165 | session: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4166 | |
| 4167 | [clinic start generated code]*/ |
| 4168 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 4169 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4170 | _ssl__SSLContext__wrap_socket_impl(PySSLContext *self, PyObject *sock, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4171 | int server_side, PyObject *hostname_obj, |
| 4172 | PyObject *owner, PyObject *session) |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4173 | /*[clinic end generated code: output=f103f238633940b4 input=f5916eadbc6eae81]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4174 | { |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4175 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4176 | PyObject *res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4177 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4178 | /* server_hostname is either None (or absent), or to be encoded |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4179 | as IDN A-label (ASCII str) without NULL bytes. */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4180 | if (hostname_obj != Py_None) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4181 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4182 | return NULL; |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4183 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4184 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4185 | res = (PyObject *) newPySSLSocket(self, (PySocketSockObject *)sock, |
| 4186 | server_side, hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4187 | owner, session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4188 | NULL, NULL); |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4189 | if (hostname != NULL) |
| 4190 | PyMem_Free(hostname); |
| 4191 | return res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4194 | /*[clinic input] |
| 4195 | _ssl._SSLContext._wrap_bio |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4196 | incoming: object(subclass_of="get_state_ctx(self)->PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
| 4197 | outgoing: object(subclass_of="get_state_ctx(self)->PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4198 | server_side: int |
| 4199 | server_hostname as hostname_obj: object = None |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4200 | * |
| 4201 | owner: object = None |
| 4202 | session: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4203 | |
| 4204 | [clinic start generated code]*/ |
| 4205 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4206 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4207 | _ssl__SSLContext__wrap_bio_impl(PySSLContext *self, PySSLMemoryBIO *incoming, |
| 4208 | PySSLMemoryBIO *outgoing, int server_side, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4209 | PyObject *hostname_obj, PyObject *owner, |
| 4210 | PyObject *session) |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4211 | /*[clinic end generated code: output=5c5d6d9b41f99332 input=331edeec9c738382]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4212 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4213 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4214 | PyObject *res; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4215 | |
| 4216 | /* server_hostname is either None (or absent), or to be encoded |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4217 | as IDN A-label (ASCII str) without NULL bytes. */ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4218 | if (hostname_obj != Py_None) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4219 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4220 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4221 | } |
| 4222 | |
| 4223 | res = (PyObject *) newPySSLSocket(self, NULL, server_side, hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4224 | owner, session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4225 | incoming, outgoing); |
| 4226 | |
| 4227 | PyMem_Free(hostname); |
| 4228 | return res; |
| 4229 | } |
| 4230 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4231 | /*[clinic input] |
| 4232 | _ssl._SSLContext.session_stats |
| 4233 | [clinic start generated code]*/ |
| 4234 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4235 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4236 | _ssl__SSLContext_session_stats_impl(PySSLContext *self) |
| 4237 | /*[clinic end generated code: output=0d96411c42893bfb input=7e0a81fb11102c8b]*/ |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4238 | { |
| 4239 | int r; |
| 4240 | PyObject *value, *stats = PyDict_New(); |
| 4241 | if (!stats) |
| 4242 | return NULL; |
| 4243 | |
| 4244 | #define ADD_STATS(SSL_NAME, KEY_NAME) \ |
| 4245 | value = PyLong_FromLong(SSL_CTX_sess_ ## SSL_NAME (self->ctx)); \ |
| 4246 | if (value == NULL) \ |
| 4247 | goto error; \ |
| 4248 | r = PyDict_SetItemString(stats, KEY_NAME, value); \ |
| 4249 | Py_DECREF(value); \ |
| 4250 | if (r < 0) \ |
| 4251 | goto error; |
| 4252 | |
| 4253 | ADD_STATS(number, "number"); |
| 4254 | ADD_STATS(connect, "connect"); |
| 4255 | ADD_STATS(connect_good, "connect_good"); |
| 4256 | ADD_STATS(connect_renegotiate, "connect_renegotiate"); |
| 4257 | ADD_STATS(accept, "accept"); |
| 4258 | ADD_STATS(accept_good, "accept_good"); |
| 4259 | ADD_STATS(accept_renegotiate, "accept_renegotiate"); |
| 4260 | ADD_STATS(accept, "accept"); |
| 4261 | ADD_STATS(hits, "hits"); |
| 4262 | ADD_STATS(misses, "misses"); |
| 4263 | ADD_STATS(timeouts, "timeouts"); |
| 4264 | ADD_STATS(cache_full, "cache_full"); |
| 4265 | |
| 4266 | #undef ADD_STATS |
| 4267 | |
| 4268 | return stats; |
| 4269 | |
| 4270 | error: |
| 4271 | Py_DECREF(stats); |
| 4272 | return NULL; |
| 4273 | } |
| 4274 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4275 | /*[clinic input] |
| 4276 | _ssl._SSLContext.set_default_verify_paths |
| 4277 | [clinic start generated code]*/ |
| 4278 | |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4279 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4280 | _ssl__SSLContext_set_default_verify_paths_impl(PySSLContext *self) |
| 4281 | /*[clinic end generated code: output=0bee74e6e09deaaa input=35f3408021463d74]*/ |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4282 | { |
| 4283 | if (!SSL_CTX_set_default_verify_paths(self->ctx)) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4284 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4285 | return NULL; |
| 4286 | } |
| 4287 | Py_RETURN_NONE; |
| 4288 | } |
| 4289 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4290 | /*[clinic input] |
| 4291 | _ssl._SSLContext.set_ecdh_curve |
| 4292 | name: object |
| 4293 | / |
| 4294 | |
| 4295 | [clinic start generated code]*/ |
| 4296 | |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4297 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4298 | _ssl__SSLContext_set_ecdh_curve(PySSLContext *self, PyObject *name) |
| 4299 | /*[clinic end generated code: output=23022c196e40d7d2 input=c2bafb6f6e34726b]*/ |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4300 | { |
| 4301 | PyObject *name_bytes; |
| 4302 | int nid; |
| 4303 | EC_KEY *key; |
| 4304 | |
| 4305 | if (!PyUnicode_FSConverter(name, &name_bytes)) |
| 4306 | return NULL; |
| 4307 | assert(PyBytes_Check(name_bytes)); |
| 4308 | nid = OBJ_sn2nid(PyBytes_AS_STRING(name_bytes)); |
| 4309 | Py_DECREF(name_bytes); |
| 4310 | if (nid == 0) { |
| 4311 | PyErr_Format(PyExc_ValueError, |
| 4312 | "unknown elliptic curve name %R", name); |
| 4313 | return NULL; |
| 4314 | } |
| 4315 | key = EC_KEY_new_by_curve_name(nid); |
| 4316 | if (key == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4317 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4318 | return NULL; |
| 4319 | } |
| 4320 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 4321 | EC_KEY_free(key); |
| 4322 | Py_RETURN_NONE; |
| 4323 | } |
| 4324 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4325 | static int |
| 4326 | _servername_callback(SSL *s, int *al, void *args) |
| 4327 | { |
| 4328 | int ret; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4329 | PySSLContext *sslctx = (PySSLContext *) args; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4330 | PySSLSocket *ssl; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4331 | PyObject *result; |
| 4332 | /* The high-level ssl.SSLSocket object */ |
| 4333 | PyObject *ssl_socket; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4334 | const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); |
Stefan Krah | 20d6080 | 2013-01-17 17:07:17 +0100 | [diff] [blame] | 4335 | PyGILState_STATE gstate = PyGILState_Ensure(); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4336 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4337 | if (sslctx->set_sni_cb == NULL) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4338 | /* remove race condition in this the call back while if removing the |
| 4339 | * callback is in progress */ |
| 4340 | PyGILState_Release(gstate); |
Antoine Pitrou | 5dd12a5 | 2013-01-06 15:25:36 +0100 | [diff] [blame] | 4341 | return SSL_TLSEXT_ERR_OK; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4342 | } |
| 4343 | |
| 4344 | ssl = SSL_get_app_data(s); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4345 | assert(Py_IS_TYPE(ssl, get_state_ctx(sslctx)->PySSLSocket_Type)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4346 | |
Serhiy Storchaka | f51d715 | 2015-11-02 14:40:41 +0200 | [diff] [blame] | 4347 | /* The servername callback expects an argument that represents the current |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4348 | * SSL connection and that has a .context attribute that can be changed to |
| 4349 | * identify the requested hostname. Since the official API is the Python |
| 4350 | * level API we want to pass the callback a Python level object rather than |
| 4351 | * a _ssl.SSLSocket instance. If there's an "owner" (typically an |
| 4352 | * SSLObject) that will be passed. Otherwise if there's a socket then that |
| 4353 | * will be passed. If both do not exist only then the C-level object is |
| 4354 | * passed. */ |
| 4355 | if (ssl->owner) |
| 4356 | ssl_socket = PyWeakref_GetObject(ssl->owner); |
| 4357 | else if (ssl->Socket) |
| 4358 | ssl_socket = PyWeakref_GetObject(ssl->Socket); |
| 4359 | else |
| 4360 | ssl_socket = (PyObject *) ssl; |
| 4361 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4362 | Py_INCREF(ssl_socket); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4363 | if (ssl_socket == Py_None) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4364 | goto error; |
Victor Stinner | 7e00151 | 2013-06-25 00:44:31 +0200 | [diff] [blame] | 4365 | |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4366 | if (servername == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4367 | result = PyObject_CallFunctionObjArgs(sslctx->set_sni_cb, ssl_socket, |
| 4368 | Py_None, sslctx, NULL); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4369 | } |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4370 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4371 | PyObject *servername_bytes; |
| 4372 | PyObject *servername_str; |
| 4373 | |
| 4374 | servername_bytes = PyBytes_FromString(servername); |
| 4375 | if (servername_bytes == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4376 | PyErr_WriteUnraisable((PyObject *) sslctx); |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4377 | goto error; |
| 4378 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4379 | /* server_hostname was encoded to an A-label by our caller; put it |
| 4380 | * back into a str object, but still as an A-label (bpo-28414) |
| 4381 | */ |
| 4382 | servername_str = PyUnicode_FromEncodedObject(servername_bytes, "ascii", NULL); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4383 | if (servername_str == NULL) { |
| 4384 | PyErr_WriteUnraisable(servername_bytes); |
Zackery Spytz | ee96f32 | 2020-07-09 04:00:21 -0600 | [diff] [blame] | 4385 | Py_DECREF(servername_bytes); |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4386 | goto error; |
| 4387 | } |
Zackery Spytz | ee96f32 | 2020-07-09 04:00:21 -0600 | [diff] [blame] | 4388 | Py_DECREF(servername_bytes); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4389 | result = PyObject_CallFunctionObjArgs( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4390 | sslctx->set_sni_cb, ssl_socket, servername_str, |
| 4391 | sslctx, NULL); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4392 | Py_DECREF(servername_str); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4393 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4394 | Py_DECREF(ssl_socket); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4395 | |
| 4396 | if (result == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4397 | PyErr_WriteUnraisable(sslctx->set_sni_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4398 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 4399 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4400 | } |
| 4401 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4402 | /* Result may be None, a SSLContext or an integer |
| 4403 | * None and SSLContext are OK, integer or other values are an error. |
| 4404 | */ |
| 4405 | if (result == Py_None) { |
| 4406 | ret = SSL_TLSEXT_ERR_OK; |
| 4407 | } else { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4408 | *al = (int) PyLong_AsLong(result); |
| 4409 | if (PyErr_Occurred()) { |
| 4410 | PyErr_WriteUnraisable(result); |
| 4411 | *al = SSL_AD_INTERNAL_ERROR; |
| 4412 | } |
| 4413 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4414 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4415 | Py_DECREF(result); |
| 4416 | } |
| 4417 | |
| 4418 | PyGILState_Release(gstate); |
| 4419 | return ret; |
| 4420 | |
| 4421 | error: |
| 4422 | Py_DECREF(ssl_socket); |
| 4423 | *al = SSL_AD_INTERNAL_ERROR; |
| 4424 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4425 | PyGILState_Release(gstate); |
| 4426 | return ret; |
| 4427 | } |
| 4428 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4429 | static PyObject * |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4430 | get_sni_callback(PySSLContext *self, void *c) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4431 | { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4432 | PyObject *cb = self->set_sni_cb; |
| 4433 | if (cb == NULL) { |
| 4434 | Py_RETURN_NONE; |
| 4435 | } |
| 4436 | Py_INCREF(cb); |
| 4437 | return cb; |
| 4438 | } |
| 4439 | |
| 4440 | static int |
| 4441 | set_sni_callback(PySSLContext *self, PyObject *arg, void *c) |
| 4442 | { |
| 4443 | if (self->protocol == PY_SSL_VERSION_TLS_CLIENT) { |
| 4444 | PyErr_SetString(PyExc_ValueError, |
| 4445 | "sni_callback cannot be set on TLS_CLIENT context"); |
| 4446 | return -1; |
| 4447 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4448 | Py_CLEAR(self->set_sni_cb); |
| 4449 | if (arg == Py_None) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4450 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 4451 | } |
| 4452 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4453 | if (!PyCallable_Check(arg)) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4454 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 4455 | PyErr_SetString(PyExc_TypeError, |
| 4456 | "not a callable object"); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4457 | return -1; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4458 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4459 | Py_INCREF(arg); |
| 4460 | self->set_sni_cb = arg; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4461 | SSL_CTX_set_tlsext_servername_callback(self->ctx, _servername_callback); |
| 4462 | SSL_CTX_set_tlsext_servername_arg(self->ctx, self); |
| 4463 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4464 | return 0; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4465 | } |
| 4466 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4467 | PyDoc_STRVAR(PySSLContext_sni_callback_doc, |
| 4468 | "Set a callback that will be called when a server name is provided by the SSL/TLS client in the SNI extension.\n\ |
| 4469 | \n\ |
| 4470 | If the argument is None then the callback is disabled. The method is called\n\ |
| 4471 | with the SSLSocket, the server name as a string, and the SSLContext object.\n\ |
| 4472 | See RFC 6066 for details of the SNI extension."); |
| 4473 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4474 | /*[clinic input] |
| 4475 | _ssl._SSLContext.cert_store_stats |
| 4476 | |
| 4477 | Returns quantities of loaded X.509 certificates. |
| 4478 | |
| 4479 | X.509 certificates with a CA extension and certificate revocation lists |
| 4480 | inside the context's cert store. |
| 4481 | |
| 4482 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 4483 | been used at least once. |
| 4484 | [clinic start generated code]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4485 | |
| 4486 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4487 | _ssl__SSLContext_cert_store_stats_impl(PySSLContext *self) |
| 4488 | /*[clinic end generated code: output=5f356f4d9cca874d input=eb40dd0f6d0e40cf]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4489 | { |
| 4490 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4491 | STACK_OF(X509_OBJECT) *objs; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4492 | X509_OBJECT *obj; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4493 | int x509 = 0, crl = 0, ca = 0, i; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4494 | |
| 4495 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4496 | objs = X509_STORE_get0_objects(store); |
| 4497 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 4498 | obj = sk_X509_OBJECT_value(objs, i); |
| 4499 | switch (X509_OBJECT_get_type(obj)) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4500 | case X509_LU_X509: |
| 4501 | x509++; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4502 | if (X509_check_ca(X509_OBJECT_get0_X509(obj))) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4503 | ca++; |
| 4504 | } |
| 4505 | break; |
| 4506 | case X509_LU_CRL: |
| 4507 | crl++; |
| 4508 | break; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4509 | default: |
| 4510 | /* Ignore X509_LU_FAIL, X509_LU_RETRY, X509_LU_PKEY. |
| 4511 | * As far as I can tell they are internal states and never |
| 4512 | * stored in a cert store */ |
| 4513 | break; |
| 4514 | } |
| 4515 | } |
| 4516 | return Py_BuildValue("{sisisi}", "x509", x509, "crl", crl, |
| 4517 | "x509_ca", ca); |
| 4518 | } |
| 4519 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4520 | /*[clinic input] |
| 4521 | _ssl._SSLContext.get_ca_certs |
| 4522 | binary_form: bool = False |
| 4523 | |
| 4524 | Returns a list of dicts with information of loaded CA certs. |
| 4525 | |
| 4526 | If the optional argument is True, returns a DER-encoded copy of the CA |
| 4527 | certificate. |
| 4528 | |
| 4529 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 4530 | been used at least once. |
| 4531 | [clinic start generated code]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4532 | |
| 4533 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4534 | _ssl__SSLContext_get_ca_certs_impl(PySSLContext *self, int binary_form) |
| 4535 | /*[clinic end generated code: output=0d58f148f37e2938 input=6887b5a09b7f9076]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4536 | { |
| 4537 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4538 | STACK_OF(X509_OBJECT) *objs; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4539 | PyObject *ci = NULL, *rlist = NULL; |
| 4540 | int i; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4541 | |
| 4542 | if ((rlist = PyList_New(0)) == NULL) { |
| 4543 | return NULL; |
| 4544 | } |
| 4545 | |
| 4546 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4547 | objs = X509_STORE_get0_objects(store); |
| 4548 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4549 | X509_OBJECT *obj; |
| 4550 | X509 *cert; |
| 4551 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4552 | obj = sk_X509_OBJECT_value(objs, i); |
| 4553 | if (X509_OBJECT_get_type(obj) != X509_LU_X509) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4554 | /* not a x509 cert */ |
| 4555 | continue; |
| 4556 | } |
| 4557 | /* CA for any purpose */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4558 | cert = X509_OBJECT_get0_X509(obj); |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4559 | if (!X509_check_ca(cert)) { |
| 4560 | continue; |
| 4561 | } |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4562 | if (binary_form) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4563 | ci = _certificate_to_der(get_state_ctx(self), cert); |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4564 | } else { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4565 | ci = _decode_certificate(get_state_ctx(self), cert); |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4566 | } |
| 4567 | if (ci == NULL) { |
| 4568 | goto error; |
| 4569 | } |
| 4570 | if (PyList_Append(rlist, ci) == -1) { |
| 4571 | goto error; |
| 4572 | } |
| 4573 | Py_CLEAR(ci); |
| 4574 | } |
| 4575 | return rlist; |
| 4576 | |
| 4577 | error: |
| 4578 | Py_XDECREF(ci); |
| 4579 | Py_XDECREF(rlist); |
| 4580 | return NULL; |
| 4581 | } |
| 4582 | |
| 4583 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4584 | static PyGetSetDef context_getsetlist[] = { |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 4585 | {"check_hostname", (getter) get_check_hostname, |
| 4586 | (setter) set_check_hostname, NULL}, |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 4587 | {"_host_flags", (getter) get_host_flags, |
| 4588 | (setter) set_host_flags, NULL}, |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4589 | {"minimum_version", (getter) get_minimum_version, |
| 4590 | (setter) set_minimum_version, NULL}, |
| 4591 | {"maximum_version", (getter) get_maximum_version, |
| 4592 | (setter) set_maximum_version, NULL}, |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 4593 | {"keylog_filename", (getter) _PySSLContext_get_keylog_filename, |
| 4594 | (setter) _PySSLContext_set_keylog_filename, NULL}, |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 4595 | {"_msg_callback", (getter) _PySSLContext_get_msg_callback, |
| 4596 | (setter) _PySSLContext_set_msg_callback, NULL}, |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4597 | {"sni_callback", (getter) get_sni_callback, |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4598 | (setter) set_sni_callback, PySSLContext_sni_callback_doc}, |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 4599 | #ifdef TLS1_3_VERSION |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 4600 | {"num_tickets", (getter) get_num_tickets, |
| 4601 | (setter) set_num_tickets, PySSLContext_num_tickets_doc}, |
| 4602 | #endif |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 4603 | {"options", (getter) get_options, |
| 4604 | (setter) set_options, NULL}, |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 4605 | {"post_handshake_auth", (getter) get_post_handshake_auth, |
| 4606 | #ifdef TLS1_3_VERSION |
| 4607 | (setter) set_post_handshake_auth, |
| 4608 | #else |
| 4609 | NULL, |
| 4610 | #endif |
| 4611 | NULL}, |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4612 | {"protocol", (getter) get_protocol, |
| 4613 | NULL, NULL}, |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 4614 | {"verify_flags", (getter) get_verify_flags, |
| 4615 | (setter) set_verify_flags, NULL}, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4616 | {"verify_mode", (getter) get_verify_mode, |
| 4617 | (setter) set_verify_mode, NULL}, |
matthewhughes934 | 8e836bb | 2020-07-17 09:59:15 +0100 | [diff] [blame] | 4618 | {"security_level", (getter) get_security_level, |
| 4619 | NULL, PySSLContext_security_level_doc}, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4620 | {NULL}, /* sentinel */ |
| 4621 | }; |
| 4622 | |
| 4623 | static struct PyMethodDef context_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4624 | _SSL__SSLCONTEXT__WRAP_SOCKET_METHODDEF |
| 4625 | _SSL__SSLCONTEXT__WRAP_BIO_METHODDEF |
| 4626 | _SSL__SSLCONTEXT_SET_CIPHERS_METHODDEF |
| 4627 | _SSL__SSLCONTEXT__SET_ALPN_PROTOCOLS_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4628 | _SSL__SSLCONTEXT_LOAD_CERT_CHAIN_METHODDEF |
| 4629 | _SSL__SSLCONTEXT_LOAD_DH_PARAMS_METHODDEF |
| 4630 | _SSL__SSLCONTEXT_LOAD_VERIFY_LOCATIONS_METHODDEF |
| 4631 | _SSL__SSLCONTEXT_SESSION_STATS_METHODDEF |
| 4632 | _SSL__SSLCONTEXT_SET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 4633 | _SSL__SSLCONTEXT_SET_ECDH_CURVE_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4634 | _SSL__SSLCONTEXT_CERT_STORE_STATS_METHODDEF |
| 4635 | _SSL__SSLCONTEXT_GET_CA_CERTS_METHODDEF |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 4636 | _SSL__SSLCONTEXT_GET_CIPHERS_METHODDEF |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4637 | {NULL, NULL} /* sentinel */ |
| 4638 | }; |
| 4639 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4640 | static PyType_Slot PySSLContext_slots[] = { |
| 4641 | {Py_tp_methods, context_methods}, |
| 4642 | {Py_tp_getset, context_getsetlist}, |
| 4643 | {Py_tp_new, _ssl__SSLContext}, |
| 4644 | {Py_tp_dealloc, context_dealloc}, |
| 4645 | {Py_tp_traverse, context_traverse}, |
| 4646 | {Py_tp_clear, context_clear}, |
| 4647 | {0, 0}, |
| 4648 | }; |
| 4649 | |
| 4650 | static PyType_Spec PySSLContext_spec = { |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 4651 | .name = "_ssl._SSLContext", |
| 4652 | .basicsize = sizeof(PySSLContext), |
| 4653 | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
| 4654 | Py_TPFLAGS_IMMUTABLETYPE), |
| 4655 | .slots = PySSLContext_slots, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4656 | }; |
| 4657 | |
| 4658 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4659 | /* |
| 4660 | * MemoryBIO objects |
| 4661 | */ |
| 4662 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4663 | /*[clinic input] |
| 4664 | @classmethod |
| 4665 | _ssl.MemoryBIO.__new__ |
| 4666 | |
| 4667 | [clinic start generated code]*/ |
| 4668 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4669 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4670 | _ssl_MemoryBIO_impl(PyTypeObject *type) |
| 4671 | /*[clinic end generated code: output=8820a58db78330ac input=26d22e4909ecb1b5]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4672 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4673 | BIO *bio; |
| 4674 | PySSLMemoryBIO *self; |
| 4675 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4676 | bio = BIO_new(BIO_s_mem()); |
| 4677 | if (bio == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4678 | PyErr_SetString(PyExc_MemoryError, "failed to allocate BIO"); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4679 | return NULL; |
| 4680 | } |
| 4681 | /* Since our BIO is non-blocking an empty read() does not indicate EOF, |
| 4682 | * just that no data is currently available. The SSL routines should retry |
| 4683 | * the read, which we can achieve by calling BIO_set_retry_read(). */ |
| 4684 | BIO_set_retry_read(bio); |
| 4685 | BIO_set_mem_eof_return(bio, -1); |
| 4686 | |
| 4687 | assert(type != NULL && type->tp_alloc != NULL); |
| 4688 | self = (PySSLMemoryBIO *) type->tp_alloc(type, 0); |
| 4689 | if (self == NULL) { |
| 4690 | BIO_free(bio); |
| 4691 | return NULL; |
| 4692 | } |
| 4693 | self->bio = bio; |
| 4694 | self->eof_written = 0; |
| 4695 | |
| 4696 | return (PyObject *) self; |
| 4697 | } |
| 4698 | |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 4699 | static int |
| 4700 | memory_bio_traverse(PySSLMemoryBIO *self, visitproc visit, void *arg) |
| 4701 | { |
| 4702 | Py_VISIT(Py_TYPE(self)); |
| 4703 | return 0; |
| 4704 | } |
| 4705 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4706 | static void |
| 4707 | memory_bio_dealloc(PySSLMemoryBIO *self) |
| 4708 | { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4709 | PyTypeObject *tp = Py_TYPE(self); |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 4710 | PyObject_GC_UnTrack(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4711 | BIO_free(self->bio); |
| 4712 | Py_TYPE(self)->tp_free(self); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4713 | Py_DECREF(tp); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4714 | } |
| 4715 | |
| 4716 | static PyObject * |
| 4717 | memory_bio_get_pending(PySSLMemoryBIO *self, void *c) |
| 4718 | { |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4719 | return PyLong_FromSize_t(BIO_ctrl_pending(self->bio)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | PyDoc_STRVAR(PySSL_memory_bio_pending_doc, |
| 4723 | "The number of bytes pending in the memory BIO."); |
| 4724 | |
| 4725 | static PyObject * |
| 4726 | memory_bio_get_eof(PySSLMemoryBIO *self, void *c) |
| 4727 | { |
| 4728 | return PyBool_FromLong((BIO_ctrl_pending(self->bio) == 0) |
| 4729 | && self->eof_written); |
| 4730 | } |
| 4731 | |
| 4732 | PyDoc_STRVAR(PySSL_memory_bio_eof_doc, |
| 4733 | "Whether the memory BIO is at EOF."); |
| 4734 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4735 | /*[clinic input] |
| 4736 | _ssl.MemoryBIO.read |
| 4737 | size as len: int = -1 |
| 4738 | / |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4739 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4740 | Read up to size bytes from the memory BIO. |
| 4741 | |
| 4742 | If size is not specified, read the entire buffer. |
| 4743 | If the return value is an empty bytes instance, this means either |
| 4744 | EOF or that no data is available. Use the "eof" property to |
| 4745 | distinguish between the two. |
| 4746 | [clinic start generated code]*/ |
| 4747 | |
| 4748 | static PyObject * |
| 4749 | _ssl_MemoryBIO_read_impl(PySSLMemoryBIO *self, int len) |
| 4750 | /*[clinic end generated code: output=a657aa1e79cd01b3 input=574d7be06a902366]*/ |
| 4751 | { |
| 4752 | int avail, nbytes; |
| 4753 | PyObject *result; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4754 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4755 | avail = (int)Py_MIN(BIO_ctrl_pending(self->bio), INT_MAX); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4756 | if ((len < 0) || (len > avail)) |
| 4757 | len = avail; |
| 4758 | |
| 4759 | result = PyBytes_FromStringAndSize(NULL, len); |
| 4760 | if ((result == NULL) || (len == 0)) |
| 4761 | return result; |
| 4762 | |
| 4763 | nbytes = BIO_read(self->bio, PyBytes_AS_STRING(result), len); |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4764 | if (nbytes < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4765 | _sslmodulestate *state = get_state_mbio(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4766 | Py_DECREF(result); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4767 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4768 | return NULL; |
| 4769 | } |
| 4770 | |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4771 | /* There should never be any short reads but check anyway. */ |
| 4772 | if (nbytes < len) { |
| 4773 | _PyBytes_Resize(&result, nbytes); |
| 4774 | } |
| 4775 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4776 | return result; |
| 4777 | } |
| 4778 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4779 | /*[clinic input] |
| 4780 | _ssl.MemoryBIO.write |
| 4781 | b: Py_buffer |
| 4782 | / |
| 4783 | |
| 4784 | Writes the bytes b into the memory BIO. |
| 4785 | |
| 4786 | Returns the number of bytes written. |
| 4787 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4788 | |
| 4789 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4790 | _ssl_MemoryBIO_write_impl(PySSLMemoryBIO *self, Py_buffer *b) |
| 4791 | /*[clinic end generated code: output=156ec59110d75935 input=e45757b3e17c4808]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4792 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4793 | int nbytes; |
| 4794 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4795 | if (b->len > INT_MAX) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4796 | PyErr_Format(PyExc_OverflowError, |
| 4797 | "string longer than %d bytes", INT_MAX); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4798 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | if (self->eof_written) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4802 | PyObject *module = PyType_GetModule(Py_TYPE(self)); |
| 4803 | if (module == NULL) |
| 4804 | return NULL; |
| 4805 | PyErr_SetString(get_ssl_state(module)->PySSLErrorObject, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4806 | "cannot write() after write_eof()"); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4807 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4808 | } |
| 4809 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4810 | nbytes = BIO_write(self->bio, b->buf, (int)b->len); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4811 | if (nbytes < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4812 | _sslmodulestate *state = get_state_mbio(self); |
| 4813 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4814 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4815 | } |
| 4816 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4817 | return PyLong_FromLong(nbytes); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4818 | } |
| 4819 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4820 | /*[clinic input] |
| 4821 | _ssl.MemoryBIO.write_eof |
| 4822 | |
| 4823 | Write an EOF marker to the memory BIO. |
| 4824 | |
| 4825 | When all data has been read, the "eof" property will be True. |
| 4826 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4827 | |
| 4828 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4829 | _ssl_MemoryBIO_write_eof_impl(PySSLMemoryBIO *self) |
| 4830 | /*[clinic end generated code: output=d4106276ccd1ed34 input=56a945f1d29e8bd6]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4831 | { |
| 4832 | self->eof_written = 1; |
| 4833 | /* After an EOF is written, a zero return from read() should be a real EOF |
| 4834 | * i.e. it should not be retried. Clear the SHOULD_RETRY flag. */ |
| 4835 | BIO_clear_retry_flags(self->bio); |
| 4836 | BIO_set_mem_eof_return(self->bio, 0); |
| 4837 | |
| 4838 | Py_RETURN_NONE; |
| 4839 | } |
| 4840 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4841 | static PyGetSetDef memory_bio_getsetlist[] = { |
| 4842 | {"pending", (getter) memory_bio_get_pending, NULL, |
| 4843 | PySSL_memory_bio_pending_doc}, |
| 4844 | {"eof", (getter) memory_bio_get_eof, NULL, |
| 4845 | PySSL_memory_bio_eof_doc}, |
| 4846 | {NULL}, /* sentinel */ |
| 4847 | }; |
| 4848 | |
| 4849 | static struct PyMethodDef memory_bio_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4850 | _SSL_MEMORYBIO_READ_METHODDEF |
| 4851 | _SSL_MEMORYBIO_WRITE_METHODDEF |
| 4852 | _SSL_MEMORYBIO_WRITE_EOF_METHODDEF |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4853 | {NULL, NULL} /* sentinel */ |
| 4854 | }; |
| 4855 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4856 | static PyType_Slot PySSLMemoryBIO_slots[] = { |
| 4857 | {Py_tp_methods, memory_bio_methods}, |
| 4858 | {Py_tp_getset, memory_bio_getsetlist}, |
| 4859 | {Py_tp_new, _ssl_MemoryBIO}, |
| 4860 | {Py_tp_dealloc, memory_bio_dealloc}, |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 4861 | {Py_tp_traverse, memory_bio_traverse}, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4862 | {0, 0}, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4863 | }; |
| 4864 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4865 | static PyType_Spec PySSLMemoryBIO_spec = { |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 4866 | .name = "_ssl.MemoryBIO", |
| 4867 | .basicsize = sizeof(PySSLMemoryBIO), |
| 4868 | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | |
| 4869 | Py_TPFLAGS_HAVE_GC), |
| 4870 | .slots = PySSLMemoryBIO_slots, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4871 | }; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4872 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4873 | /* |
| 4874 | * SSL Session object |
| 4875 | */ |
| 4876 | |
| 4877 | static void |
| 4878 | PySSLSession_dealloc(PySSLSession *self) |
| 4879 | { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4880 | PyTypeObject *tp = Py_TYPE(self); |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 4881 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 4882 | PyObject_GC_UnTrack(self); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4883 | Py_XDECREF(self->ctx); |
| 4884 | if (self->session != NULL) { |
| 4885 | SSL_SESSION_free(self->session); |
| 4886 | } |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 4887 | PyObject_GC_Del(self); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4888 | Py_DECREF(tp); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4889 | } |
| 4890 | |
| 4891 | static PyObject * |
| 4892 | PySSLSession_richcompare(PyObject *left, PyObject *right, int op) |
| 4893 | { |
| 4894 | int result; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4895 | PyTypeObject *sesstype = ((PySSLSession*)left)->ctx->state->PySSLSession_Type; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4896 | |
| 4897 | if (left == NULL || right == NULL) { |
| 4898 | PyErr_BadInternalCall(); |
| 4899 | return NULL; |
| 4900 | } |
| 4901 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4902 | if (!Py_IS_TYPE(left, sesstype) || !Py_IS_TYPE(right, sesstype)) { |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4903 | Py_RETURN_NOTIMPLEMENTED; |
| 4904 | } |
| 4905 | |
| 4906 | if (left == right) { |
| 4907 | result = 0; |
| 4908 | } else { |
| 4909 | const unsigned char *left_id, *right_id; |
| 4910 | unsigned int left_len, right_len; |
| 4911 | left_id = SSL_SESSION_get_id(((PySSLSession *)left)->session, |
| 4912 | &left_len); |
| 4913 | right_id = SSL_SESSION_get_id(((PySSLSession *)right)->session, |
| 4914 | &right_len); |
| 4915 | if (left_len == right_len) { |
| 4916 | result = memcmp(left_id, right_id, left_len); |
| 4917 | } else { |
| 4918 | result = 1; |
| 4919 | } |
| 4920 | } |
| 4921 | |
| 4922 | switch (op) { |
| 4923 | case Py_EQ: |
| 4924 | if (result == 0) { |
| 4925 | Py_RETURN_TRUE; |
| 4926 | } else { |
| 4927 | Py_RETURN_FALSE; |
| 4928 | } |
| 4929 | break; |
| 4930 | case Py_NE: |
| 4931 | if (result != 0) { |
| 4932 | Py_RETURN_TRUE; |
| 4933 | } else { |
| 4934 | Py_RETURN_FALSE; |
| 4935 | } |
| 4936 | break; |
| 4937 | case Py_LT: |
| 4938 | case Py_LE: |
| 4939 | case Py_GT: |
| 4940 | case Py_GE: |
| 4941 | Py_RETURN_NOTIMPLEMENTED; |
| 4942 | break; |
| 4943 | default: |
| 4944 | PyErr_BadArgument(); |
| 4945 | return NULL; |
| 4946 | } |
| 4947 | } |
| 4948 | |
| 4949 | static int |
| 4950 | PySSLSession_traverse(PySSLSession *self, visitproc visit, void *arg) |
| 4951 | { |
| 4952 | Py_VISIT(self->ctx); |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 4953 | Py_VISIT(Py_TYPE(self)); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4954 | return 0; |
| 4955 | } |
| 4956 | |
| 4957 | static int |
| 4958 | PySSLSession_clear(PySSLSession *self) |
| 4959 | { |
| 4960 | Py_CLEAR(self->ctx); |
| 4961 | return 0; |
| 4962 | } |
| 4963 | |
| 4964 | |
| 4965 | static PyObject * |
| 4966 | PySSLSession_get_time(PySSLSession *self, void *closure) { |
| 4967 | return PyLong_FromLong(SSL_SESSION_get_time(self->session)); |
| 4968 | } |
| 4969 | |
| 4970 | PyDoc_STRVAR(PySSLSession_get_time_doc, |
| 4971 | "Session creation time (seconds since epoch)."); |
| 4972 | |
| 4973 | |
| 4974 | static PyObject * |
| 4975 | PySSLSession_get_timeout(PySSLSession *self, void *closure) { |
| 4976 | return PyLong_FromLong(SSL_SESSION_get_timeout(self->session)); |
| 4977 | } |
| 4978 | |
| 4979 | PyDoc_STRVAR(PySSLSession_get_timeout_doc, |
| 4980 | "Session timeout (delta in seconds)."); |
| 4981 | |
| 4982 | |
| 4983 | static PyObject * |
| 4984 | PySSLSession_get_ticket_lifetime_hint(PySSLSession *self, void *closure) { |
| 4985 | unsigned long hint = SSL_SESSION_get_ticket_lifetime_hint(self->session); |
| 4986 | return PyLong_FromUnsignedLong(hint); |
| 4987 | } |
| 4988 | |
| 4989 | PyDoc_STRVAR(PySSLSession_get_ticket_lifetime_hint_doc, |
| 4990 | "Ticket life time hint."); |
| 4991 | |
| 4992 | |
| 4993 | static PyObject * |
| 4994 | PySSLSession_get_session_id(PySSLSession *self, void *closure) { |
| 4995 | const unsigned char *id; |
| 4996 | unsigned int len; |
| 4997 | id = SSL_SESSION_get_id(self->session, &len); |
| 4998 | return PyBytes_FromStringAndSize((const char *)id, len); |
| 4999 | } |
| 5000 | |
| 5001 | PyDoc_STRVAR(PySSLSession_get_session_id_doc, |
| 5002 | "Session id"); |
| 5003 | |
| 5004 | |
| 5005 | static PyObject * |
| 5006 | PySSLSession_get_has_ticket(PySSLSession *self, void *closure) { |
| 5007 | if (SSL_SESSION_has_ticket(self->session)) { |
| 5008 | Py_RETURN_TRUE; |
| 5009 | } else { |
| 5010 | Py_RETURN_FALSE; |
| 5011 | } |
| 5012 | } |
| 5013 | |
| 5014 | PyDoc_STRVAR(PySSLSession_get_has_ticket_doc, |
| 5015 | "Does the session contain a ticket?"); |
| 5016 | |
| 5017 | |
| 5018 | static PyGetSetDef PySSLSession_getsetlist[] = { |
| 5019 | {"has_ticket", (getter) PySSLSession_get_has_ticket, NULL, |
| 5020 | PySSLSession_get_has_ticket_doc}, |
| 5021 | {"id", (getter) PySSLSession_get_session_id, NULL, |
| 5022 | PySSLSession_get_session_id_doc}, |
| 5023 | {"ticket_lifetime_hint", (getter) PySSLSession_get_ticket_lifetime_hint, |
| 5024 | NULL, PySSLSession_get_ticket_lifetime_hint_doc}, |
| 5025 | {"time", (getter) PySSLSession_get_time, NULL, |
| 5026 | PySSLSession_get_time_doc}, |
| 5027 | {"timeout", (getter) PySSLSession_get_timeout, NULL, |
| 5028 | PySSLSession_get_timeout_doc}, |
| 5029 | {NULL}, /* sentinel */ |
| 5030 | }; |
| 5031 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5032 | static PyType_Slot PySSLSession_slots[] = { |
| 5033 | {Py_tp_getset,PySSLSession_getsetlist}, |
| 5034 | {Py_tp_richcompare, PySSLSession_richcompare}, |
| 5035 | {Py_tp_dealloc, PySSLSession_dealloc}, |
| 5036 | {Py_tp_traverse, PySSLSession_traverse}, |
| 5037 | {Py_tp_clear, PySSLSession_clear}, |
| 5038 | {0, 0}, |
| 5039 | }; |
| 5040 | |
| 5041 | static PyType_Spec PySSLSession_spec = { |
Miss Islington (bot) | ea47a8a | 2021-05-27 09:25:22 -0700 | [diff] [blame^] | 5042 | .name = "_ssl.SSLSession", |
| 5043 | .basicsize = sizeof(PySSLSession), |
| 5044 | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 5045 | Py_TPFLAGS_IMMUTABLETYPE), |
| 5046 | .slots = PySSLSession_slots, |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5047 | }; |
| 5048 | |
| 5049 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5050 | /* helper routines for seeding the SSL PRNG */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5051 | /*[clinic input] |
| 5052 | _ssl.RAND_add |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 5053 | string as view: Py_buffer(accept={str, buffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5054 | entropy: double |
| 5055 | / |
| 5056 | |
| 5057 | Mix string into the OpenSSL PRNG state. |
| 5058 | |
| 5059 | entropy (a float) is a lower bound on the entropy contained in |
Chandan Kumar | 63c2c8a | 2017-06-09 15:13:58 +0530 | [diff] [blame] | 5060 | string. See RFC 4086. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5061 | [clinic start generated code]*/ |
| 5062 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5063 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5064 | _ssl_RAND_add_impl(PyObject *module, Py_buffer *view, double entropy) |
Serhiy Storchaka | 5f31d5c | 2017-06-10 13:13:51 +0300 | [diff] [blame] | 5065 | /*[clinic end generated code: output=e6dd48df9c9024e9 input=5c33017422828f5c]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5066 | { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 5067 | const char *buf; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5068 | Py_ssize_t len, written; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5069 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5070 | buf = (const char *)view->buf; |
| 5071 | len = view->len; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 5072 | do { |
| 5073 | written = Py_MIN(len, INT_MAX); |
| 5074 | RAND_add(buf, (int)written, entropy); |
| 5075 | buf += written; |
| 5076 | len -= written; |
| 5077 | } while (len); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5078 | Py_RETURN_NONE; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5079 | } |
| 5080 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5081 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5082 | PySSL_RAND(PyObject *module, int len, int pseudo) |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5083 | { |
| 5084 | int ok; |
| 5085 | PyObject *bytes; |
| 5086 | unsigned long err; |
| 5087 | const char *errstr; |
| 5088 | PyObject *v; |
| 5089 | |
Victor Stinner | 1e81a39 | 2013-12-19 16:47:04 +0100 | [diff] [blame] | 5090 | if (len < 0) { |
| 5091 | PyErr_SetString(PyExc_ValueError, "num must be positive"); |
| 5092 | return NULL; |
| 5093 | } |
| 5094 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5095 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 5096 | if (bytes == NULL) |
| 5097 | return NULL; |
| 5098 | if (pseudo) { |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 5099 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5100 | if (ok == 0 || ok == 1) |
| 5101 | return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False); |
| 5102 | } |
| 5103 | else { |
| 5104 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 5105 | if (ok == 1) |
| 5106 | return bytes; |
| 5107 | } |
| 5108 | Py_DECREF(bytes); |
| 5109 | |
| 5110 | err = ERR_get_error(); |
| 5111 | errstr = ERR_reason_error_string(err); |
| 5112 | v = Py_BuildValue("(ks)", err, errstr); |
| 5113 | if (v != NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5114 | PyErr_SetObject(get_ssl_state(module)->PySSLErrorObject, v); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5115 | Py_DECREF(v); |
| 5116 | } |
| 5117 | return NULL; |
| 5118 | } |
| 5119 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5120 | /*[clinic input] |
| 5121 | _ssl.RAND_bytes |
| 5122 | n: int |
| 5123 | / |
| 5124 | |
| 5125 | Generate n cryptographically strong pseudo-random bytes. |
| 5126 | [clinic start generated code]*/ |
| 5127 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5128 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5129 | _ssl_RAND_bytes_impl(PyObject *module, int n) |
| 5130 | /*[clinic end generated code: output=977da635e4838bc7 input=678ddf2872dfebfc]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5131 | { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5132 | return PySSL_RAND(module, n, 0); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5133 | } |
| 5134 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5135 | /*[clinic input] |
| 5136 | _ssl.RAND_pseudo_bytes |
| 5137 | n: int |
| 5138 | / |
| 5139 | |
| 5140 | Generate n pseudo-random bytes. |
| 5141 | |
| 5142 | Return a pair (bytes, is_cryptographic). is_cryptographic is True |
| 5143 | if the bytes generated are cryptographically strong. |
| 5144 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5145 | |
| 5146 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5147 | _ssl_RAND_pseudo_bytes_impl(PyObject *module, int n) |
| 5148 | /*[clinic end generated code: output=b1509e937000e52d input=58312bd53f9bbdd0]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5149 | { |
Christian Heimes | 2875c60 | 2021-04-19 07:27:10 +0200 | [diff] [blame] | 5150 | PY_SSL_DEPRECATED("RAND_pseudo_bytes", 1, NULL); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5151 | return PySSL_RAND(module, n, 1); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5152 | } |
| 5153 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5154 | /*[clinic input] |
| 5155 | _ssl.RAND_status |
| 5156 | |
Zackery Spytz | 7d37b86 | 2021-04-23 10:07:37 -0600 | [diff] [blame] | 5157 | Returns True if the OpenSSL PRNG has been seeded with enough data and False if not. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5158 | |
| 5159 | It is necessary to seed the PRNG with RAND_add() on some platforms before |
| 5160 | using the ssl() function. |
| 5161 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 5162 | |
| 5163 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5164 | _ssl_RAND_status_impl(PyObject *module) |
Zackery Spytz | 7d37b86 | 2021-04-23 10:07:37 -0600 | [diff] [blame] | 5165 | /*[clinic end generated code: output=7e0aaa2d39fdc1ad input=d5ae5aea52f36e01]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5166 | { |
Zackery Spytz | 7d37b86 | 2021-04-23 10:07:37 -0600 | [diff] [blame] | 5167 | return PyBool_FromLong(RAND_status()); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5168 | } |
| 5169 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5170 | /*[clinic input] |
| 5171 | _ssl.get_default_verify_paths |
| 5172 | |
| 5173 | Return search paths and environment vars that are used by SSLContext's set_default_verify_paths() to load default CAs. |
| 5174 | |
| 5175 | The values are 'cert_file_env', 'cert_file', 'cert_dir_env', 'cert_dir'. |
| 5176 | [clinic start generated code]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5177 | |
| 5178 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5179 | _ssl_get_default_verify_paths_impl(PyObject *module) |
| 5180 | /*[clinic end generated code: output=e5b62a466271928b input=5210c953d98c3eb5]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5181 | { |
| 5182 | PyObject *ofile_env = NULL; |
| 5183 | PyObject *ofile = NULL; |
| 5184 | PyObject *odir_env = NULL; |
| 5185 | PyObject *odir = NULL; |
| 5186 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5187 | #define CONVERT(info, target) { \ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5188 | const char *tmp = (info); \ |
| 5189 | target = NULL; \ |
| 5190 | if (!tmp) { Py_INCREF(Py_None); target = Py_None; } \ |
| 5191 | else if ((target = PyUnicode_DecodeFSDefault(tmp)) == NULL) { \ |
| 5192 | target = PyBytes_FromString(tmp); } \ |
| 5193 | if (!target) goto error; \ |
Benjamin Peterson | 025a1fd | 2015-11-14 15:12:38 -0800 | [diff] [blame] | 5194 | } |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5195 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5196 | CONVERT(X509_get_default_cert_file_env(), ofile_env); |
| 5197 | CONVERT(X509_get_default_cert_file(), ofile); |
| 5198 | CONVERT(X509_get_default_cert_dir_env(), odir_env); |
| 5199 | CONVERT(X509_get_default_cert_dir(), odir); |
| 5200 | #undef CONVERT |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5201 | |
Christian Heimes | 200bb1b | 2013-06-14 15:14:29 +0200 | [diff] [blame] | 5202 | return Py_BuildValue("NNNN", ofile_env, ofile, odir_env, odir); |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5203 | |
| 5204 | error: |
| 5205 | Py_XDECREF(ofile_env); |
| 5206 | Py_XDECREF(ofile); |
| 5207 | Py_XDECREF(odir_env); |
| 5208 | Py_XDECREF(odir); |
| 5209 | return NULL; |
| 5210 | } |
| 5211 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5212 | static PyObject* |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5213 | asn1obj2py(_sslmodulestate *state, ASN1_OBJECT *obj) |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5214 | { |
| 5215 | int nid; |
| 5216 | const char *ln, *sn; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5217 | |
| 5218 | nid = OBJ_obj2nid(obj); |
| 5219 | if (nid == NID_undef) { |
| 5220 | PyErr_Format(PyExc_ValueError, "Unknown object"); |
| 5221 | return NULL; |
| 5222 | } |
| 5223 | sn = OBJ_nid2sn(nid); |
| 5224 | ln = OBJ_nid2ln(nid); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5225 | return Py_BuildValue("issN", nid, sn, ln, _asn1obj2py(state, obj, 1)); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5226 | } |
| 5227 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5228 | /*[clinic input] |
| 5229 | _ssl.txt2obj |
| 5230 | txt: str |
| 5231 | name: bool = False |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5232 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5233 | Lookup NID, short name, long name and OID of an ASN1_OBJECT. |
| 5234 | |
| 5235 | By default objects are looked up by OID. With name=True short and |
| 5236 | long name are also matched. |
| 5237 | [clinic start generated code]*/ |
| 5238 | |
| 5239 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5240 | _ssl_txt2obj_impl(PyObject *module, const char *txt, int name) |
| 5241 | /*[clinic end generated code: output=c38e3991347079c1 input=1c1e7d0aa7c48602]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5242 | { |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5243 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5244 | ASN1_OBJECT *obj; |
| 5245 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5246 | obj = OBJ_txt2obj(txt, name ? 0 : 1); |
| 5247 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5248 | PyErr_Format(PyExc_ValueError, "unknown object '%.100s'", txt); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5249 | return NULL; |
| 5250 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5251 | result = asn1obj2py(get_ssl_state(module), obj); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5252 | ASN1_OBJECT_free(obj); |
| 5253 | return result; |
| 5254 | } |
| 5255 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5256 | /*[clinic input] |
| 5257 | _ssl.nid2obj |
| 5258 | nid: int |
| 5259 | / |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5260 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5261 | Lookup NID, short name, long name and OID of an ASN1_OBJECT by NID. |
| 5262 | [clinic start generated code]*/ |
| 5263 | |
| 5264 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5265 | _ssl_nid2obj_impl(PyObject *module, int nid) |
| 5266 | /*[clinic end generated code: output=4a98ab691cd4f84a input=51787a3bee7d8f98]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5267 | { |
| 5268 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5269 | ASN1_OBJECT *obj; |
| 5270 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5271 | if (nid < NID_undef) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5272 | PyErr_SetString(PyExc_ValueError, "NID must be positive."); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5273 | return NULL; |
| 5274 | } |
| 5275 | obj = OBJ_nid2obj(nid); |
| 5276 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5277 | PyErr_Format(PyExc_ValueError, "unknown NID %i", nid); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5278 | return NULL; |
| 5279 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5280 | result = asn1obj2py(get_ssl_state(module), obj); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5281 | ASN1_OBJECT_free(obj); |
| 5282 | return result; |
| 5283 | } |
| 5284 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5285 | #ifdef _MSC_VER |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5286 | |
| 5287 | static PyObject* |
| 5288 | certEncodingType(DWORD encodingType) |
| 5289 | { |
| 5290 | static PyObject *x509_asn = NULL; |
| 5291 | static PyObject *pkcs_7_asn = NULL; |
| 5292 | |
| 5293 | if (x509_asn == NULL) { |
| 5294 | x509_asn = PyUnicode_InternFromString("x509_asn"); |
| 5295 | if (x509_asn == NULL) |
| 5296 | return NULL; |
| 5297 | } |
| 5298 | if (pkcs_7_asn == NULL) { |
| 5299 | pkcs_7_asn = PyUnicode_InternFromString("pkcs_7_asn"); |
| 5300 | if (pkcs_7_asn == NULL) |
| 5301 | return NULL; |
| 5302 | } |
| 5303 | switch(encodingType) { |
| 5304 | case X509_ASN_ENCODING: |
| 5305 | Py_INCREF(x509_asn); |
| 5306 | return x509_asn; |
| 5307 | case PKCS_7_ASN_ENCODING: |
| 5308 | Py_INCREF(pkcs_7_asn); |
| 5309 | return pkcs_7_asn; |
| 5310 | default: |
| 5311 | return PyLong_FromLong(encodingType); |
| 5312 | } |
| 5313 | } |
| 5314 | |
| 5315 | static PyObject* |
| 5316 | parseKeyUsage(PCCERT_CONTEXT pCertCtx, DWORD flags) |
| 5317 | { |
| 5318 | CERT_ENHKEY_USAGE *usage; |
| 5319 | DWORD size, error, i; |
| 5320 | PyObject *retval; |
| 5321 | |
| 5322 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, NULL, &size)) { |
| 5323 | error = GetLastError(); |
| 5324 | if (error == CRYPT_E_NOT_FOUND) { |
| 5325 | Py_RETURN_TRUE; |
| 5326 | } |
| 5327 | return PyErr_SetFromWindowsErr(error); |
| 5328 | } |
| 5329 | |
| 5330 | usage = (CERT_ENHKEY_USAGE*)PyMem_Malloc(size); |
| 5331 | if (usage == NULL) { |
| 5332 | return PyErr_NoMemory(); |
| 5333 | } |
| 5334 | |
| 5335 | /* Now get the actual enhanced usage property */ |
| 5336 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, usage, &size)) { |
| 5337 | PyMem_Free(usage); |
| 5338 | error = GetLastError(); |
| 5339 | if (error == CRYPT_E_NOT_FOUND) { |
| 5340 | Py_RETURN_TRUE; |
| 5341 | } |
| 5342 | return PyErr_SetFromWindowsErr(error); |
| 5343 | } |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5344 | retval = PyFrozenSet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5345 | if (retval == NULL) { |
| 5346 | goto error; |
| 5347 | } |
| 5348 | for (i = 0; i < usage->cUsageIdentifier; ++i) { |
| 5349 | if (usage->rgpszUsageIdentifier[i]) { |
| 5350 | PyObject *oid; |
| 5351 | int err; |
| 5352 | oid = PyUnicode_FromString(usage->rgpszUsageIdentifier[i]); |
| 5353 | if (oid == NULL) { |
| 5354 | Py_CLEAR(retval); |
| 5355 | goto error; |
| 5356 | } |
| 5357 | err = PySet_Add(retval, oid); |
| 5358 | Py_DECREF(oid); |
| 5359 | if (err == -1) { |
| 5360 | Py_CLEAR(retval); |
| 5361 | goto error; |
| 5362 | } |
| 5363 | } |
| 5364 | } |
| 5365 | error: |
| 5366 | PyMem_Free(usage); |
| 5367 | return retval; |
| 5368 | } |
| 5369 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5370 | static HCERTSTORE |
| 5371 | ssl_collect_certificates(const char *store_name) |
| 5372 | { |
| 5373 | /* this function collects the system certificate stores listed in |
| 5374 | * system_stores into a collection certificate store for being |
| 5375 | * enumerated. The store must be readable to be added to the |
| 5376 | * store collection. |
| 5377 | */ |
| 5378 | |
| 5379 | HCERTSTORE hCollectionStore = NULL, hSystemStore = NULL; |
| 5380 | static DWORD system_stores[] = { |
| 5381 | CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 5382 | CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, |
| 5383 | CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY, |
| 5384 | CERT_SYSTEM_STORE_CURRENT_USER, |
| 5385 | CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY, |
| 5386 | CERT_SYSTEM_STORE_SERVICES, |
| 5387 | CERT_SYSTEM_STORE_USERS}; |
| 5388 | size_t i, storesAdded; |
| 5389 | BOOL result; |
| 5390 | |
| 5391 | hCollectionStore = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, |
| 5392 | (HCRYPTPROV)NULL, 0, NULL); |
| 5393 | if (!hCollectionStore) { |
| 5394 | return NULL; |
| 5395 | } |
| 5396 | storesAdded = 0; |
| 5397 | for (i = 0; i < sizeof(system_stores) / sizeof(DWORD); i++) { |
| 5398 | hSystemStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, |
| 5399 | (HCRYPTPROV)NULL, |
| 5400 | CERT_STORE_READONLY_FLAG | |
| 5401 | system_stores[i], store_name); |
| 5402 | if (hSystemStore) { |
| 5403 | result = CertAddStoreToCollection(hCollectionStore, hSystemStore, |
| 5404 | CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0); |
| 5405 | if (result) { |
| 5406 | ++storesAdded; |
| 5407 | } |
neonene | ed70129 | 2019-09-09 21:33:43 +0900 | [diff] [blame] | 5408 | CertCloseStore(hSystemStore, 0); /* flag must be 0 */ |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5409 | } |
| 5410 | } |
| 5411 | if (storesAdded == 0) { |
| 5412 | CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG); |
| 5413 | return NULL; |
| 5414 | } |
| 5415 | |
| 5416 | return hCollectionStore; |
| 5417 | } |
| 5418 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5419 | /*[clinic input] |
| 5420 | _ssl.enum_certificates |
| 5421 | store_name: str |
| 5422 | |
| 5423 | Retrieve certificates from Windows' cert store. |
| 5424 | |
| 5425 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5426 | more cert storages, too. The function returns a list of (bytes, |
| 5427 | encoding_type, trust) tuples. The encoding_type flag can be interpreted |
| 5428 | with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. The trust setting is either |
| 5429 | a set of OIDs or the boolean True. |
| 5430 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5431 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5432 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5433 | _ssl_enum_certificates_impl(PyObject *module, const char *store_name) |
| 5434 | /*[clinic end generated code: output=5134dc8bb3a3c893 input=915f60d70461ea4e]*/ |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5435 | { |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5436 | HCERTSTORE hCollectionStore = NULL; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5437 | PCCERT_CONTEXT pCertCtx = NULL; |
| 5438 | PyObject *keyusage = NULL, *cert = NULL, *enc = NULL, *tup = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5439 | PyObject *result = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5440 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5441 | result = PySet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5442 | if (result == NULL) { |
| 5443 | return NULL; |
| 5444 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5445 | hCollectionStore = ssl_collect_certificates(store_name); |
| 5446 | if (hCollectionStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5447 | Py_DECREF(result); |
| 5448 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5449 | } |
| 5450 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5451 | while (pCertCtx = CertEnumCertificatesInStore(hCollectionStore, pCertCtx)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5452 | cert = PyBytes_FromStringAndSize((const char*)pCertCtx->pbCertEncoded, |
| 5453 | pCertCtx->cbCertEncoded); |
| 5454 | if (!cert) { |
| 5455 | Py_CLEAR(result); |
| 5456 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5457 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5458 | if ((enc = certEncodingType(pCertCtx->dwCertEncodingType)) == NULL) { |
| 5459 | Py_CLEAR(result); |
| 5460 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5461 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5462 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG); |
| 5463 | if (keyusage == Py_True) { |
| 5464 | Py_DECREF(keyusage); |
| 5465 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5466 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5467 | if (keyusage == NULL) { |
| 5468 | Py_CLEAR(result); |
| 5469 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5470 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5471 | if ((tup = PyTuple_New(3)) == NULL) { |
| 5472 | Py_CLEAR(result); |
| 5473 | break; |
| 5474 | } |
| 5475 | PyTuple_SET_ITEM(tup, 0, cert); |
| 5476 | cert = NULL; |
| 5477 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5478 | enc = NULL; |
| 5479 | PyTuple_SET_ITEM(tup, 2, keyusage); |
| 5480 | keyusage = NULL; |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5481 | if (PySet_Add(result, tup) == -1) { |
| 5482 | Py_CLEAR(result); |
| 5483 | Py_CLEAR(tup); |
| 5484 | break; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5485 | } |
| 5486 | Py_CLEAR(tup); |
| 5487 | } |
| 5488 | if (pCertCtx) { |
| 5489 | /* loop ended with an error, need to clean up context manually */ |
| 5490 | CertFreeCertificateContext(pCertCtx); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | /* In error cases cert, enc and tup may not be NULL */ |
| 5494 | Py_XDECREF(cert); |
| 5495 | Py_XDECREF(enc); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5496 | Py_XDECREF(keyusage); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5497 | Py_XDECREF(tup); |
| 5498 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5499 | /* CERT_CLOSE_STORE_FORCE_FLAG forces freeing of memory for all contexts |
| 5500 | associated with the store, in this case our collection store and the |
| 5501 | associated system stores. */ |
| 5502 | if (!CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG)) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5503 | /* This error case might shadow another exception.*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5504 | Py_XDECREF(result); |
| 5505 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5506 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5507 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5508 | /* convert set to list */ |
| 5509 | if (result == NULL) { |
| 5510 | return NULL; |
| 5511 | } else { |
| 5512 | PyObject *lst = PySequence_List(result); |
| 5513 | Py_DECREF(result); |
| 5514 | return lst; |
| 5515 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5516 | } |
| 5517 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5518 | /*[clinic input] |
| 5519 | _ssl.enum_crls |
| 5520 | store_name: str |
| 5521 | |
| 5522 | Retrieve CRLs from Windows' cert store. |
| 5523 | |
| 5524 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5525 | more cert storages, too. The function returns a list of (bytes, |
| 5526 | encoding_type) tuples. The encoding_type flag can be interpreted with |
| 5527 | X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. |
| 5528 | [clinic start generated code]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5529 | |
| 5530 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5531 | _ssl_enum_crls_impl(PyObject *module, const char *store_name) |
| 5532 | /*[clinic end generated code: output=bce467f60ccd03b6 input=a1f1d7629f1c5d3d]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5533 | { |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5534 | HCERTSTORE hCollectionStore = NULL; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5535 | PCCRL_CONTEXT pCrlCtx = NULL; |
| 5536 | PyObject *crl = NULL, *enc = NULL, *tup = NULL; |
| 5537 | PyObject *result = NULL; |
| 5538 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5539 | result = PySet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5540 | if (result == NULL) { |
| 5541 | return NULL; |
| 5542 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5543 | hCollectionStore = ssl_collect_certificates(store_name); |
| 5544 | if (hCollectionStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5545 | Py_DECREF(result); |
| 5546 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5547 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5548 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5549 | while (pCrlCtx = CertEnumCRLsInStore(hCollectionStore, pCrlCtx)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5550 | crl = PyBytes_FromStringAndSize((const char*)pCrlCtx->pbCrlEncoded, |
| 5551 | pCrlCtx->cbCrlEncoded); |
| 5552 | if (!crl) { |
| 5553 | Py_CLEAR(result); |
| 5554 | break; |
| 5555 | } |
| 5556 | if ((enc = certEncodingType(pCrlCtx->dwCertEncodingType)) == NULL) { |
| 5557 | Py_CLEAR(result); |
| 5558 | break; |
| 5559 | } |
| 5560 | if ((tup = PyTuple_New(2)) == NULL) { |
| 5561 | Py_CLEAR(result); |
| 5562 | break; |
| 5563 | } |
| 5564 | PyTuple_SET_ITEM(tup, 0, crl); |
| 5565 | crl = NULL; |
| 5566 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5567 | enc = NULL; |
| 5568 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5569 | if (PySet_Add(result, tup) == -1) { |
| 5570 | Py_CLEAR(result); |
| 5571 | Py_CLEAR(tup); |
| 5572 | break; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5573 | } |
| 5574 | Py_CLEAR(tup); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5575 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5576 | if (pCrlCtx) { |
| 5577 | /* loop ended with an error, need to clean up context manually */ |
| 5578 | CertFreeCRLContext(pCrlCtx); |
| 5579 | } |
| 5580 | |
| 5581 | /* In error cases cert, enc and tup may not be NULL */ |
| 5582 | Py_XDECREF(crl); |
| 5583 | Py_XDECREF(enc); |
| 5584 | Py_XDECREF(tup); |
| 5585 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5586 | /* CERT_CLOSE_STORE_FORCE_FLAG forces freeing of memory for all contexts |
| 5587 | associated with the store, in this case our collection store and the |
| 5588 | associated system stores. */ |
| 5589 | if (!CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5590 | /* This error case might shadow another exception.*/ |
| 5591 | Py_XDECREF(result); |
| 5592 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5593 | } |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5594 | /* convert set to list */ |
| 5595 | if (result == NULL) { |
| 5596 | return NULL; |
| 5597 | } else { |
| 5598 | PyObject *lst = PySequence_List(result); |
| 5599 | Py_DECREF(result); |
| 5600 | return lst; |
| 5601 | } |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5602 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5603 | |
| 5604 | #endif /* _MSC_VER */ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5605 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5606 | /* List of functions exported by this module. */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5607 | static PyMethodDef PySSL_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5608 | _SSL__TEST_DECODE_CERT_METHODDEF |
| 5609 | _SSL_RAND_ADD_METHODDEF |
| 5610 | _SSL_RAND_BYTES_METHODDEF |
| 5611 | _SSL_RAND_PSEUDO_BYTES_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5612 | _SSL_RAND_STATUS_METHODDEF |
| 5613 | _SSL_GET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 5614 | _SSL_ENUM_CERTIFICATES_METHODDEF |
| 5615 | _SSL_ENUM_CRLS_METHODDEF |
| 5616 | _SSL_TXT2OBJ_METHODDEF |
| 5617 | _SSL_NID2OBJ_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5618 | {NULL, NULL} /* Sentinel */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5619 | }; |
| 5620 | |
| 5621 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5622 | PyDoc_STRVAR(module_doc, |
| 5623 | "Implementation module for SSL socket operations. See the socket module\n\ |
| 5624 | for documentation."); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5625 | |
| 5626 | static int |
| 5627 | sslmodule_init_exceptions(PyObject *module) |
| 5628 | { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5629 | _sslmodulestate *state = get_ssl_state(module); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5630 | PyObject *bases = NULL; |
| 5631 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5632 | #define add_exception(exc, name, doc, base) \ |
| 5633 | do { \ |
| 5634 | (exc) = PyErr_NewExceptionWithDoc("ssl." name, (doc), (base), NULL); \ |
| 5635 | if ((state) == NULL) goto error; \ |
| 5636 | if (PyModule_AddObjectRef(module, name, exc) < 0) goto error; \ |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5637 | } while(0) |
| 5638 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5639 | state->PySSLErrorObject = PyType_FromSpecWithBases( |
| 5640 | &sslerror_type_spec, PyExc_OSError); |
| 5641 | if (state->PySSLErrorObject == NULL) { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5642 | goto error; |
| 5643 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5644 | if (PyModule_AddObjectRef(module, "SSLError", state->PySSLErrorObject) < 0) { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5645 | goto error; |
| 5646 | } |
| 5647 | |
| 5648 | /* ssl.CertificateError used to be a subclass of ValueError */ |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5649 | bases = PyTuple_Pack(2, state->PySSLErrorObject, PyExc_ValueError); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5650 | if (bases == NULL) { |
| 5651 | goto error; |
| 5652 | } |
| 5653 | add_exception( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5654 | state->PySSLCertVerificationErrorObject, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5655 | "SSLCertVerificationError", |
| 5656 | SSLCertVerificationError_doc, |
| 5657 | bases |
| 5658 | ); |
| 5659 | Py_CLEAR(bases); |
| 5660 | |
| 5661 | add_exception( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5662 | state->PySSLZeroReturnErrorObject, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5663 | "SSLZeroReturnError", |
| 5664 | SSLZeroReturnError_doc, |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5665 | state->PySSLErrorObject |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5666 | ); |
| 5667 | |
| 5668 | add_exception( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5669 | state->PySSLWantWriteErrorObject, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5670 | "SSLWantWriteError", |
| 5671 | SSLWantWriteError_doc, |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5672 | state->PySSLErrorObject |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5673 | ); |
| 5674 | |
| 5675 | add_exception( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5676 | state->PySSLWantReadErrorObject, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5677 | "SSLWantReadError", |
| 5678 | SSLWantReadError_doc, |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5679 | state->PySSLErrorObject |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5680 | ); |
| 5681 | |
| 5682 | add_exception( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5683 | state->PySSLSyscallErrorObject, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5684 | "SSLSyscallError", |
| 5685 | SSLSyscallError_doc, |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5686 | state->PySSLErrorObject |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5687 | ); |
| 5688 | |
| 5689 | add_exception( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5690 | state->PySSLEOFErrorObject, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5691 | "SSLEOFError", |
| 5692 | SSLEOFError_doc, |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5693 | state->PySSLErrorObject |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5694 | ); |
| 5695 | #undef add_exception |
| 5696 | |
| 5697 | return 0; |
| 5698 | error: |
| 5699 | Py_XDECREF(bases); |
| 5700 | return -1; |
| 5701 | } |
| 5702 | |
| 5703 | static int |
| 5704 | sslmodule_init_socketapi(PyObject *module) |
| 5705 | { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5706 | _sslmodulestate *state = get_ssl_state(module); |
| 5707 | PySocketModule_APIObject *sockmod = PySocketModule_ImportModuleAndAPI(); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5708 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5709 | if ((sockmod == NULL) || (sockmod->Sock_Type == NULL)) { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5710 | return -1; |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5711 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5712 | state->Sock_Type = sockmod->Sock_Type; |
| 5713 | Py_INCREF(state->Sock_Type); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5714 | return 0; |
| 5715 | } |
Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 5716 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5717 | static int |
| 5718 | sslmodule_init_constants(PyObject *m) |
| 5719 | { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5720 | |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 5721 | PyModule_AddStringConstant(m, "_DEFAULT_CIPHERS", |
| 5722 | PY_SSL_DEFAULT_CIPHER_STRING); |
| 5723 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5724 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
| 5725 | PY_SSL_ERROR_ZERO_RETURN); |
| 5726 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
| 5727 | PY_SSL_ERROR_WANT_READ); |
| 5728 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
| 5729 | PY_SSL_ERROR_WANT_WRITE); |
| 5730 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
| 5731 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
| 5732 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
| 5733 | PY_SSL_ERROR_SYSCALL); |
| 5734 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
| 5735 | PY_SSL_ERROR_SSL); |
| 5736 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 5737 | PY_SSL_ERROR_WANT_CONNECT); |
| 5738 | /* non ssl.h errorcodes */ |
| 5739 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 5740 | PY_SSL_ERROR_EOF); |
| 5741 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 5742 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
| 5743 | /* cert requirements */ |
| 5744 | PyModule_AddIntConstant(m, "CERT_NONE", |
| 5745 | PY_SSL_CERT_NONE); |
| 5746 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 5747 | PY_SSL_CERT_OPTIONAL); |
| 5748 | PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 5749 | PY_SSL_CERT_REQUIRED); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 5750 | /* CRL verification for verification_flags */ |
| 5751 | PyModule_AddIntConstant(m, "VERIFY_DEFAULT", |
| 5752 | 0); |
| 5753 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_LEAF", |
| 5754 | X509_V_FLAG_CRL_CHECK); |
| 5755 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_CHAIN", |
| 5756 | X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5757 | PyModule_AddIntConstant(m, "VERIFY_X509_STRICT", |
| 5758 | X509_V_FLAG_X509_STRICT); |
Chris Burr | e0b4aa0 | 2021-03-18 09:24:01 +0100 | [diff] [blame] | 5759 | PyModule_AddIntConstant(m, "VERIFY_ALLOW_PROXY_CERTS", |
| 5760 | X509_V_FLAG_ALLOW_PROXY_CERTS); |
Benjamin Peterson | 990fcaa | 2015-03-04 22:49:41 -0500 | [diff] [blame] | 5761 | PyModule_AddIntConstant(m, "VERIFY_X509_TRUSTED_FIRST", |
| 5762 | X509_V_FLAG_TRUSTED_FIRST); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 5763 | |
l0x | 64d9752 | 2021-04-19 13:51:18 +0200 | [diff] [blame] | 5764 | #ifdef X509_V_FLAG_PARTIAL_CHAIN |
| 5765 | PyModule_AddIntConstant(m, "VERIFY_X509_PARTIAL_CHAIN", |
| 5766 | X509_V_FLAG_PARTIAL_CHAIN); |
| 5767 | #endif |
| 5768 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5769 | /* Alert Descriptions from ssl.h */ |
| 5770 | /* note RESERVED constants no longer intended for use have been removed */ |
| 5771 | /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6 */ |
| 5772 | |
| 5773 | #define ADD_AD_CONSTANT(s) \ |
| 5774 | PyModule_AddIntConstant(m, "ALERT_DESCRIPTION_"#s, \ |
| 5775 | SSL_AD_##s) |
| 5776 | |
| 5777 | ADD_AD_CONSTANT(CLOSE_NOTIFY); |
| 5778 | ADD_AD_CONSTANT(UNEXPECTED_MESSAGE); |
| 5779 | ADD_AD_CONSTANT(BAD_RECORD_MAC); |
| 5780 | ADD_AD_CONSTANT(RECORD_OVERFLOW); |
| 5781 | ADD_AD_CONSTANT(DECOMPRESSION_FAILURE); |
| 5782 | ADD_AD_CONSTANT(HANDSHAKE_FAILURE); |
| 5783 | ADD_AD_CONSTANT(BAD_CERTIFICATE); |
| 5784 | ADD_AD_CONSTANT(UNSUPPORTED_CERTIFICATE); |
| 5785 | ADD_AD_CONSTANT(CERTIFICATE_REVOKED); |
| 5786 | ADD_AD_CONSTANT(CERTIFICATE_EXPIRED); |
| 5787 | ADD_AD_CONSTANT(CERTIFICATE_UNKNOWN); |
| 5788 | ADD_AD_CONSTANT(ILLEGAL_PARAMETER); |
| 5789 | ADD_AD_CONSTANT(UNKNOWN_CA); |
| 5790 | ADD_AD_CONSTANT(ACCESS_DENIED); |
| 5791 | ADD_AD_CONSTANT(DECODE_ERROR); |
| 5792 | ADD_AD_CONSTANT(DECRYPT_ERROR); |
| 5793 | ADD_AD_CONSTANT(PROTOCOL_VERSION); |
| 5794 | ADD_AD_CONSTANT(INSUFFICIENT_SECURITY); |
| 5795 | ADD_AD_CONSTANT(INTERNAL_ERROR); |
| 5796 | ADD_AD_CONSTANT(USER_CANCELLED); |
| 5797 | ADD_AD_CONSTANT(NO_RENEGOTIATION); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5798 | /* Not all constants are in old OpenSSL versions */ |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 5799 | #ifdef SSL_AD_UNSUPPORTED_EXTENSION |
| 5800 | ADD_AD_CONSTANT(UNSUPPORTED_EXTENSION); |
| 5801 | #endif |
| 5802 | #ifdef SSL_AD_CERTIFICATE_UNOBTAINABLE |
| 5803 | ADD_AD_CONSTANT(CERTIFICATE_UNOBTAINABLE); |
| 5804 | #endif |
| 5805 | #ifdef SSL_AD_UNRECOGNIZED_NAME |
| 5806 | ADD_AD_CONSTANT(UNRECOGNIZED_NAME); |
| 5807 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5808 | #ifdef SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE |
| 5809 | ADD_AD_CONSTANT(BAD_CERTIFICATE_STATUS_RESPONSE); |
| 5810 | #endif |
| 5811 | #ifdef SSL_AD_BAD_CERTIFICATE_HASH_VALUE |
| 5812 | ADD_AD_CONSTANT(BAD_CERTIFICATE_HASH_VALUE); |
| 5813 | #endif |
| 5814 | #ifdef SSL_AD_UNKNOWN_PSK_IDENTITY |
| 5815 | ADD_AD_CONSTANT(UNKNOWN_PSK_IDENTITY); |
| 5816 | #endif |
| 5817 | |
| 5818 | #undef ADD_AD_CONSTANT |
| 5819 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5820 | /* protocol versions */ |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 5821 | #ifndef OPENSSL_NO_SSL2 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5822 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 5823 | PY_SSL_VERSION_SSL2); |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 5824 | #endif |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 5825 | #ifndef OPENSSL_NO_SSL3 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5826 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 5827 | PY_SSL_VERSION_SSL3); |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 5828 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5829 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5830 | PY_SSL_VERSION_TLS); |
| 5831 | PyModule_AddIntConstant(m, "PROTOCOL_TLS", |
| 5832 | PY_SSL_VERSION_TLS); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 5833 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_CLIENT", |
| 5834 | PY_SSL_VERSION_TLS_CLIENT); |
| 5835 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_SERVER", |
| 5836 | PY_SSL_VERSION_TLS_SERVER); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5837 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 5838 | PY_SSL_VERSION_TLS1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 5839 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_1", |
| 5840 | PY_SSL_VERSION_TLS1_1); |
| 5841 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_2", |
| 5842 | PY_SSL_VERSION_TLS1_2); |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 5843 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5844 | /* protocol options */ |
Antoine Pitrou | 3f36631 | 2012-01-27 09:50:45 +0100 | [diff] [blame] | 5845 | PyModule_AddIntConstant(m, "OP_ALL", |
| 5846 | SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5847 | PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2); |
| 5848 | PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3); |
| 5849 | PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 5850 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1); |
| 5851 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5852 | #ifdef SSL_OP_NO_TLSv1_3 |
| 5853 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", SSL_OP_NO_TLSv1_3); |
| 5854 | #else |
| 5855 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", 0); |
| 5856 | #endif |
Antoine Pitrou | 6db4944 | 2011-12-19 13:27:11 +0100 | [diff] [blame] | 5857 | PyModule_AddIntConstant(m, "OP_CIPHER_SERVER_PREFERENCE", |
| 5858 | SSL_OP_CIPHER_SERVER_PREFERENCE); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 5859 | PyModule_AddIntConstant(m, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5860 | PyModule_AddIntConstant(m, "OP_NO_TICKET", SSL_OP_NO_TICKET); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 5861 | #ifdef SSL_OP_SINGLE_ECDH_USE |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 5862 | PyModule_AddIntConstant(m, "OP_SINGLE_ECDH_USE", SSL_OP_SINGLE_ECDH_USE); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 5863 | #endif |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 5864 | #ifdef SSL_OP_NO_COMPRESSION |
| 5865 | PyModule_AddIntConstant(m, "OP_NO_COMPRESSION", |
| 5866 | SSL_OP_NO_COMPRESSION); |
| 5867 | #endif |
Christian Heimes | 05d9fe3 | 2018-02-27 08:55:39 +0100 | [diff] [blame] | 5868 | #ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT |
| 5869 | PyModule_AddIntConstant(m, "OP_ENABLE_MIDDLEBOX_COMPAT", |
| 5870 | SSL_OP_ENABLE_MIDDLEBOX_COMPAT); |
| 5871 | #endif |
Christian Heimes | 67c4801 | 2018-05-15 16:25:40 -0400 | [diff] [blame] | 5872 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 5873 | PyModule_AddIntConstant(m, "OP_NO_RENEGOTIATION", |
| 5874 | SSL_OP_NO_RENEGOTIATION); |
| 5875 | #endif |
Christian Heimes | 6f37ebc | 2021-04-09 17:59:21 +0200 | [diff] [blame] | 5876 | #ifdef SSL_OP_IGNORE_UNEXPECTED_EOF |
| 5877 | PyModule_AddIntConstant(m, "OP_IGNORE_UNEXPECTED_EOF", |
| 5878 | SSL_OP_IGNORE_UNEXPECTED_EOF); |
| 5879 | #endif |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5880 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 5881 | #ifdef X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT |
| 5882 | PyModule_AddIntConstant(m, "HOSTFLAG_ALWAYS_CHECK_SUBJECT", |
| 5883 | X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT); |
| 5884 | #endif |
| 5885 | #ifdef X509_CHECK_FLAG_NEVER_CHECK_SUBJECT |
| 5886 | PyModule_AddIntConstant(m, "HOSTFLAG_NEVER_CHECK_SUBJECT", |
| 5887 | X509_CHECK_FLAG_NEVER_CHECK_SUBJECT); |
| 5888 | #endif |
| 5889 | #ifdef X509_CHECK_FLAG_NO_WILDCARDS |
| 5890 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_WILDCARDS", |
| 5891 | X509_CHECK_FLAG_NO_WILDCARDS); |
| 5892 | #endif |
| 5893 | #ifdef X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS |
| 5894 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_PARTIAL_WILDCARDS", |
| 5895 | X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); |
| 5896 | #endif |
| 5897 | #ifdef X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS |
| 5898 | PyModule_AddIntConstant(m, "HOSTFLAG_MULTI_LABEL_WILDCARDS", |
| 5899 | X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS); |
| 5900 | #endif |
| 5901 | #ifdef X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS |
| 5902 | PyModule_AddIntConstant(m, "HOSTFLAG_SINGLE_LABEL_SUBDOMAINS", |
| 5903 | X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS); |
| 5904 | #endif |
| 5905 | |
Christian Heimes | 666991f | 2021-04-26 15:01:40 +0200 | [diff] [blame] | 5906 | /* file types */ |
| 5907 | PyModule_AddIntConstant(m, "ENCODING_PEM", PY_SSL_ENCODING_PEM); |
| 5908 | PyModule_AddIntConstant(m, "ENCODING_DER", PY_SSL_ENCODING_DER); |
| 5909 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5910 | /* protocol versions */ |
| 5911 | PyModule_AddIntConstant(m, "PROTO_MINIMUM_SUPPORTED", |
| 5912 | PY_PROTO_MINIMUM_SUPPORTED); |
| 5913 | PyModule_AddIntConstant(m, "PROTO_MAXIMUM_SUPPORTED", |
| 5914 | PY_PROTO_MAXIMUM_SUPPORTED); |
| 5915 | PyModule_AddIntConstant(m, "PROTO_SSLv3", PY_PROTO_SSLv3); |
| 5916 | PyModule_AddIntConstant(m, "PROTO_TLSv1", PY_PROTO_TLSv1); |
| 5917 | PyModule_AddIntConstant(m, "PROTO_TLSv1_1", PY_PROTO_TLSv1_1); |
| 5918 | PyModule_AddIntConstant(m, "PROTO_TLSv1_2", PY_PROTO_TLSv1_2); |
| 5919 | PyModule_AddIntConstant(m, "PROTO_TLSv1_3", PY_PROTO_TLSv1_3); |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 5920 | |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 5921 | #define addbool(m, key, value) \ |
| 5922 | do { \ |
| 5923 | PyObject *bool_obj = (value) ? Py_True : Py_False; \ |
| 5924 | Py_INCREF(bool_obj); \ |
| 5925 | PyModule_AddObject((m), (key), bool_obj); \ |
| 5926 | } while (0) |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5927 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5928 | addbool(m, "HAS_SNI", 1); |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5929 | addbool(m, "HAS_TLS_UNIQUE", 1); |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5930 | addbool(m, "HAS_ECDH", 1); |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5931 | addbool(m, "HAS_NPN", 0); |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5932 | addbool(m, "HAS_ALPN", 1); |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5933 | |
| 5934 | #if defined(SSL2_VERSION) && !defined(OPENSSL_NO_SSL2) |
| 5935 | addbool(m, "HAS_SSLv2", 1); |
| 5936 | #else |
| 5937 | addbool(m, "HAS_SSLv2", 0); |
| 5938 | #endif |
| 5939 | |
| 5940 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 5941 | addbool(m, "HAS_SSLv3", 1); |
| 5942 | #else |
| 5943 | addbool(m, "HAS_SSLv3", 0); |
| 5944 | #endif |
| 5945 | |
| 5946 | #if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 5947 | addbool(m, "HAS_TLSv1", 1); |
| 5948 | #else |
| 5949 | addbool(m, "HAS_TLSv1", 0); |
| 5950 | #endif |
| 5951 | |
| 5952 | #if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 5953 | addbool(m, "HAS_TLSv1_1", 1); |
| 5954 | #else |
| 5955 | addbool(m, "HAS_TLSv1_1", 0); |
| 5956 | #endif |
| 5957 | |
| 5958 | #if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 5959 | addbool(m, "HAS_TLSv1_2", 1); |
| 5960 | #else |
| 5961 | addbool(m, "HAS_TLSv1_2", 0); |
| 5962 | #endif |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 5963 | |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5964 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5965 | addbool(m, "HAS_TLSv1_3", 1); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5966 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5967 | addbool(m, "HAS_TLSv1_3", 0); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5968 | #endif |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5969 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5970 | return 0; |
| 5971 | } |
| 5972 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5973 | static int |
| 5974 | sslmodule_init_errorcodes(PyObject *module) |
| 5975 | { |
| 5976 | _sslmodulestate *state = get_ssl_state(module); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5977 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5978 | struct py_ssl_error_code *errcode; |
| 5979 | struct py_ssl_library_code *libcode; |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5980 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5981 | /* Mappings for error codes */ |
| 5982 | state->err_codes_to_names = PyDict_New(); |
| 5983 | if (state->err_codes_to_names == NULL) |
| 5984 | return -1; |
| 5985 | state->err_names_to_codes = PyDict_New(); |
| 5986 | if (state->err_names_to_codes == NULL) |
| 5987 | return -1; |
| 5988 | state->lib_codes_to_names = PyDict_New(); |
| 5989 | if (state->lib_codes_to_names == NULL) |
| 5990 | return -1; |
| 5991 | |
| 5992 | errcode = error_codes; |
| 5993 | while (errcode->mnemonic != NULL) { |
| 5994 | PyObject *mnemo, *key; |
| 5995 | mnemo = PyUnicode_FromString(errcode->mnemonic); |
| 5996 | key = Py_BuildValue("ii", errcode->library, errcode->reason); |
| 5997 | if (mnemo == NULL || key == NULL) |
| 5998 | return -1; |
| 5999 | if (PyDict_SetItem(state->err_codes_to_names, key, mnemo)) |
| 6000 | return -1; |
| 6001 | if (PyDict_SetItem(state->err_names_to_codes, mnemo, key)) |
| 6002 | return -1; |
| 6003 | Py_DECREF(key); |
| 6004 | Py_DECREF(mnemo); |
| 6005 | errcode++; |
| 6006 | } |
| 6007 | |
| 6008 | libcode = library_codes; |
| 6009 | while (libcode->library != NULL) { |
| 6010 | PyObject *mnemo, *key; |
| 6011 | key = PyLong_FromLong(libcode->code); |
| 6012 | mnemo = PyUnicode_FromString(libcode->library); |
| 6013 | if (key == NULL || mnemo == NULL) |
| 6014 | return -1; |
| 6015 | if (PyDict_SetItem(state->lib_codes_to_names, key, mnemo)) |
| 6016 | return -1; |
| 6017 | Py_DECREF(key); |
| 6018 | Py_DECREF(mnemo); |
| 6019 | libcode++; |
| 6020 | } |
| 6021 | |
| 6022 | if (PyModule_AddObjectRef(module, "err_codes_to_names", state->err_codes_to_names)) |
| 6023 | return -1; |
| 6024 | if (PyModule_AddObjectRef(module, "err_names_to_codes", state->err_names_to_codes)) |
| 6025 | return -1; |
| 6026 | if (PyModule_AddObjectRef(module, "lib_codes_to_names", state->lib_codes_to_names)) |
| 6027 | return -1; |
| 6028 | |
| 6029 | return 0; |
| 6030 | } |
| 6031 | |
| 6032 | static void |
| 6033 | parse_openssl_version(unsigned long libver, |
| 6034 | unsigned int *major, unsigned int *minor, |
| 6035 | unsigned int *fix, unsigned int *patch, |
| 6036 | unsigned int *status) |
| 6037 | { |
| 6038 | *status = libver & 0xF; |
| 6039 | libver >>= 4; |
| 6040 | *patch = libver & 0xFF; |
| 6041 | libver >>= 8; |
| 6042 | *fix = libver & 0xFF; |
| 6043 | libver >>= 8; |
| 6044 | *minor = libver & 0xFF; |
| 6045 | libver >>= 8; |
| 6046 | *major = libver & 0xFF; |
| 6047 | } |
| 6048 | |
| 6049 | static int |
| 6050 | sslmodule_init_versioninfo(PyObject *m) |
| 6051 | { |
| 6052 | PyObject *r; |
| 6053 | unsigned long libver; |
| 6054 | unsigned int major, minor, fix, patch, status; |
| 6055 | |
| 6056 | /* OpenSSL version */ |
| 6057 | /* SSLeay() gives us the version of the library linked against, |
| 6058 | which could be different from the headers version. |
| 6059 | */ |
| 6060 | libver = OpenSSL_version_num(); |
| 6061 | r = PyLong_FromUnsignedLong(libver); |
| 6062 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r)) |
| 6063 | return -1; |
| 6064 | |
| 6065 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 6066 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 6067 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r)) |
| 6068 | return -1; |
| 6069 | |
| 6070 | r = PyUnicode_FromString(OpenSSL_version(OPENSSL_VERSION)); |
| 6071 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r)) |
| 6072 | return -1; |
| 6073 | |
| 6074 | libver = OPENSSL_VERSION_NUMBER; |
| 6075 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 6076 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 6077 | if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r)) |
| 6078 | return -1; |
| 6079 | |
| 6080 | return 0; |
| 6081 | } |
| 6082 | |
| 6083 | static int |
| 6084 | sslmodule_init_types(PyObject *module) |
| 6085 | { |
| 6086 | _sslmodulestate *state = get_ssl_state(module); |
| 6087 | |
| 6088 | state->PySSLContext_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 6089 | module, &PySSLContext_spec, NULL |
| 6090 | ); |
| 6091 | if (state->PySSLContext_Type == NULL) |
| 6092 | return -1; |
| 6093 | |
| 6094 | state->PySSLSocket_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 6095 | module, &PySSLSocket_spec, NULL |
| 6096 | ); |
| 6097 | if (state->PySSLSocket_Type == NULL) |
| 6098 | return -1; |
| 6099 | |
| 6100 | state->PySSLMemoryBIO_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 6101 | module, &PySSLMemoryBIO_spec, NULL |
| 6102 | ); |
| 6103 | if (state->PySSLMemoryBIO_Type == NULL) |
| 6104 | return -1; |
| 6105 | |
| 6106 | state->PySSLSession_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 6107 | module, &PySSLSession_spec, NULL |
| 6108 | ); |
| 6109 | if (state->PySSLSession_Type == NULL) |
| 6110 | return -1; |
| 6111 | |
Christian Heimes | 666991f | 2021-04-26 15:01:40 +0200 | [diff] [blame] | 6112 | state->PySSLCertificate_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 6113 | module, &PySSLCertificate_spec, NULL |
| 6114 | ); |
| 6115 | if (state->PySSLCertificate_Type == NULL) |
| 6116 | return -1; |
| 6117 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 6118 | if (PyModule_AddType(module, state->PySSLContext_Type)) |
| 6119 | return -1; |
| 6120 | if (PyModule_AddType(module, state->PySSLSocket_Type)) |
| 6121 | return -1; |
| 6122 | if (PyModule_AddType(module, state->PySSLMemoryBIO_Type)) |
| 6123 | return -1; |
| 6124 | if (PyModule_AddType(module, state->PySSLSession_Type)) |
| 6125 | return -1; |
Christian Heimes | 666991f | 2021-04-26 15:01:40 +0200 | [diff] [blame] | 6126 | if (PyModule_AddType(module, state->PySSLCertificate_Type)) |
| 6127 | return -1; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 6128 | return 0; |
| 6129 | } |
| 6130 | |
| 6131 | static PyModuleDef_Slot sslmodule_slots[] = { |
| 6132 | {Py_mod_exec, sslmodule_init_types}, |
| 6133 | {Py_mod_exec, sslmodule_init_exceptions}, |
| 6134 | {Py_mod_exec, sslmodule_init_socketapi}, |
| 6135 | {Py_mod_exec, sslmodule_init_errorcodes}, |
| 6136 | {Py_mod_exec, sslmodule_init_constants}, |
| 6137 | {Py_mod_exec, sslmodule_init_versioninfo}, |
| 6138 | {0, NULL} |
| 6139 | }; |
| 6140 | |
| 6141 | static int |
| 6142 | sslmodule_traverse(PyObject *m, visitproc visit, void *arg) |
| 6143 | { |
| 6144 | _sslmodulestate *state = get_ssl_state(m); |
| 6145 | |
| 6146 | Py_VISIT(state->PySSLContext_Type); |
| 6147 | Py_VISIT(state->PySSLSocket_Type); |
| 6148 | Py_VISIT(state->PySSLMemoryBIO_Type); |
| 6149 | Py_VISIT(state->PySSLSession_Type); |
Christian Heimes | 666991f | 2021-04-26 15:01:40 +0200 | [diff] [blame] | 6150 | Py_VISIT(state->PySSLCertificate_Type); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 6151 | Py_VISIT(state->PySSLErrorObject); |
| 6152 | Py_VISIT(state->PySSLCertVerificationErrorObject); |
| 6153 | Py_VISIT(state->PySSLZeroReturnErrorObject); |
| 6154 | Py_VISIT(state->PySSLWantReadErrorObject); |
| 6155 | Py_VISIT(state->PySSLWantWriteErrorObject); |
| 6156 | Py_VISIT(state->PySSLSyscallErrorObject); |
| 6157 | Py_VISIT(state->PySSLEOFErrorObject); |
| 6158 | Py_VISIT(state->err_codes_to_names); |
| 6159 | Py_VISIT(state->err_names_to_codes); |
| 6160 | Py_VISIT(state->lib_codes_to_names); |
| 6161 | Py_VISIT(state->Sock_Type); |
| 6162 | |
| 6163 | return 0; |
| 6164 | } |
| 6165 | |
| 6166 | static int |
| 6167 | sslmodule_clear(PyObject *m) |
| 6168 | { |
| 6169 | _sslmodulestate *state = get_ssl_state(m); |
| 6170 | |
| 6171 | Py_CLEAR(state->PySSLContext_Type); |
| 6172 | Py_CLEAR(state->PySSLSocket_Type); |
| 6173 | Py_CLEAR(state->PySSLMemoryBIO_Type); |
| 6174 | Py_CLEAR(state->PySSLSession_Type); |
Christian Heimes | 666991f | 2021-04-26 15:01:40 +0200 | [diff] [blame] | 6175 | Py_CLEAR(state->PySSLCertificate_Type); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 6176 | Py_CLEAR(state->PySSLErrorObject); |
| 6177 | Py_CLEAR(state->PySSLCertVerificationErrorObject); |
| 6178 | Py_CLEAR(state->PySSLZeroReturnErrorObject); |
| 6179 | Py_CLEAR(state->PySSLWantReadErrorObject); |
| 6180 | Py_CLEAR(state->PySSLWantWriteErrorObject); |
| 6181 | Py_CLEAR(state->PySSLSyscallErrorObject); |
| 6182 | Py_CLEAR(state->PySSLEOFErrorObject); |
| 6183 | Py_CLEAR(state->err_codes_to_names); |
| 6184 | Py_CLEAR(state->err_names_to_codes); |
| 6185 | Py_CLEAR(state->lib_codes_to_names); |
| 6186 | Py_CLEAR(state->Sock_Type); |
| 6187 | |
| 6188 | return 0; |
| 6189 | } |
| 6190 | |
| 6191 | static void |
| 6192 | sslmodule_free(void *m) |
| 6193 | { |
| 6194 | sslmodule_clear((PyObject *)m); |
| 6195 | } |
| 6196 | |
| 6197 | static struct PyModuleDef _sslmodule_def = { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6198 | PyModuleDef_HEAD_INIT, |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 6199 | .m_name = "_ssl", |
| 6200 | .m_doc = module_doc, |
| 6201 | .m_size = sizeof(_sslmodulestate), |
| 6202 | .m_methods = PySSL_methods, |
| 6203 | .m_slots = sslmodule_slots, |
| 6204 | .m_traverse = sslmodule_traverse, |
| 6205 | .m_clear = sslmodule_clear, |
| 6206 | .m_free = sslmodule_free |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6207 | }; |
| 6208 | |
| 6209 | PyMODINIT_FUNC |
| 6210 | PyInit__ssl(void) |
| 6211 | { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 6212 | return PyModuleDef_Init(&_sslmodule_def); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6213 | } |