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 | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 126 | #ifndef OPENSSL_NO_TLS1_METHOD |
Christian Heimes | a483388 | 2021-04-13 08:17:26 +0200 | [diff] [blame] | 127 | extern const SSL_METHOD *TLSv1_method(void); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 128 | #endif |
| 129 | #ifndef OPENSSL_NO_TLS1_1_METHOD |
Christian Heimes | a483388 | 2021-04-13 08:17:26 +0200 | [diff] [blame] | 130 | extern const SSL_METHOD *TLSv1_1_method(void); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 131 | #endif |
| 132 | #ifndef OPENSSL_NO_TLS1_2_METHOD |
Christian Heimes | a483388 | 2021-04-13 08:17:26 +0200 | [diff] [blame] | 133 | extern const SSL_METHOD *TLSv1_2_method(void); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 134 | #endif |
| 135 | |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 136 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 137 | #define INVALID_SOCKET (-1) |
| 138 | #endif |
| 139 | |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 140 | /* OpenSSL 1.1 does not have SSL 2.0 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 141 | #define OPENSSL_NO_SSL2 |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 142 | |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 143 | /* Default cipher suites */ |
| 144 | #ifndef PY_SSL_DEFAULT_CIPHERS |
| 145 | #define PY_SSL_DEFAULT_CIPHERS 1 |
| 146 | #endif |
| 147 | |
| 148 | #if PY_SSL_DEFAULT_CIPHERS == 0 |
| 149 | #ifndef PY_SSL_DEFAULT_CIPHER_STRING |
| 150 | #error "Py_SSL_DEFAULT_CIPHERS 0 needs Py_SSL_DEFAULT_CIPHER_STRING" |
| 151 | #endif |
| 152 | #elif PY_SSL_DEFAULT_CIPHERS == 1 |
Stéphane Wirtel | 07fbbfd | 2018-10-05 16:17:18 +0200 | [diff] [blame] | 153 | /* Python custom selection of sensible cipher suites |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 154 | * DEFAULT: OpenSSL's default cipher list. Since 1.0.2 the list is in sensible order. |
| 155 | * !aNULL:!eNULL: really no NULL ciphers |
| 156 | * !MD5:!3DES:!DES:!RC4:!IDEA:!SEED: no weak or broken algorithms on old OpenSSL versions. |
| 157 | * !aDSS: no authentication with discrete logarithm DSA algorithm |
| 158 | * !SRP:!PSK: no secure remote password or pre-shared key authentication |
| 159 | */ |
| 160 | #define PY_SSL_DEFAULT_CIPHER_STRING "DEFAULT:!aNULL:!eNULL:!MD5:!3DES:!DES:!RC4:!IDEA:!SEED:!aDSS:!SRP:!PSK" |
| 161 | #elif PY_SSL_DEFAULT_CIPHERS == 2 |
| 162 | /* Ignored in SSLContext constructor, only used to as _ssl.DEFAULT_CIPHER_STRING */ |
| 163 | #define PY_SSL_DEFAULT_CIPHER_STRING SSL_DEFAULT_CIPHER_LIST |
| 164 | #else |
| 165 | #error "Unsupported PY_SSL_DEFAULT_CIPHERS" |
| 166 | #endif |
| 167 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 168 | |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 169 | enum py_ssl_error { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 170 | /* these mirror ssl.h */ |
| 171 | PY_SSL_ERROR_NONE, |
| 172 | PY_SSL_ERROR_SSL, |
| 173 | PY_SSL_ERROR_WANT_READ, |
| 174 | PY_SSL_ERROR_WANT_WRITE, |
| 175 | PY_SSL_ERROR_WANT_X509_LOOKUP, |
| 176 | PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */ |
| 177 | PY_SSL_ERROR_ZERO_RETURN, |
| 178 | PY_SSL_ERROR_WANT_CONNECT, |
| 179 | /* start of non ssl.h errorcodes */ |
| 180 | PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */ |
| 181 | PY_SSL_ERROR_NO_SOCKET, /* socket has been GC'd */ |
| 182 | PY_SSL_ERROR_INVALID_ERROR_CODE |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 183 | }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 184 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 185 | enum py_ssl_server_or_client { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 186 | PY_SSL_CLIENT, |
| 187 | PY_SSL_SERVER |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | enum py_ssl_cert_requirements { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 191 | PY_SSL_CERT_NONE, |
| 192 | PY_SSL_CERT_OPTIONAL, |
| 193 | PY_SSL_CERT_REQUIRED |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | enum py_ssl_version { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 197 | PY_SSL_VERSION_SSL2, |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 198 | PY_SSL_VERSION_SSL3=1, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 199 | PY_SSL_VERSION_TLS, /* SSLv23 */ |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 200 | PY_SSL_VERSION_TLS1, |
| 201 | PY_SSL_VERSION_TLS1_1, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 202 | PY_SSL_VERSION_TLS1_2, |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 203 | PY_SSL_VERSION_TLS_CLIENT=0x10, |
| 204 | PY_SSL_VERSION_TLS_SERVER, |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 205 | }; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 206 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 207 | enum py_proto_version { |
| 208 | PY_PROTO_MINIMUM_SUPPORTED = -2, |
| 209 | PY_PROTO_SSLv3 = SSL3_VERSION, |
| 210 | PY_PROTO_TLSv1 = TLS1_VERSION, |
| 211 | PY_PROTO_TLSv1_1 = TLS1_1_VERSION, |
| 212 | PY_PROTO_TLSv1_2 = TLS1_2_VERSION, |
| 213 | #ifdef TLS1_3_VERSION |
| 214 | PY_PROTO_TLSv1_3 = TLS1_3_VERSION, |
| 215 | #else |
| 216 | PY_PROTO_TLSv1_3 = 0x304, |
| 217 | #endif |
| 218 | PY_PROTO_MAXIMUM_SUPPORTED = -1, |
| 219 | |
| 220 | /* OpenSSL has no dedicated API to set the minimum version to the maximum |
| 221 | * available version, and the other way around. We have to figure out the |
| 222 | * minimum and maximum available version on our own and hope for the best. |
| 223 | */ |
| 224 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 225 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_SSLv3, |
| 226 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 227 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1, |
| 228 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 229 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_1, |
| 230 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 231 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_2, |
| 232 | #elif defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 233 | PY_PROTO_MINIMUM_AVAILABLE = PY_PROTO_TLSv1_3, |
| 234 | #else |
| 235 | #error "PY_PROTO_MINIMUM_AVAILABLE not found" |
| 236 | #endif |
| 237 | |
| 238 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 239 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_3, |
| 240 | #elif defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 241 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_2, |
| 242 | #elif defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 243 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1_1, |
| 244 | #elif defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 245 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_TLSv1, |
| 246 | #elif defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 247 | PY_PROTO_MAXIMUM_AVAILABLE = PY_PROTO_SSLv3, |
| 248 | #else |
| 249 | #error "PY_PROTO_MAXIMUM_AVAILABLE not found" |
| 250 | #endif |
| 251 | }; |
| 252 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 253 | /* SSL socket object */ |
| 254 | |
| 255 | #define X509_NAME_MAXLEN 256 |
| 256 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 257 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 258 | /* In case of 'tls-unique' it will be 12 bytes for TLS, 36 bytes for |
| 259 | * older SSL, but let's be safe */ |
| 260 | #define PySSL_CB_MAXLEN 128 |
| 261 | |
Antoine Pitrou | a9bf2ac | 2012-02-17 18:47:54 +0100 | [diff] [blame] | 262 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 263 | typedef struct { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 264 | PyObject_HEAD |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 265 | SSL_CTX *ctx; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 266 | unsigned char *alpn_protocols; |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 267 | unsigned int alpn_protocols_len; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 268 | PyObject *set_sni_cb; |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 269 | int check_hostname; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 270 | /* OpenSSL has no API to get hostflags from X509_VERIFY_PARAM* struct. |
| 271 | * We have to maintain our own copy. OpenSSL's hostflags default to 0. |
| 272 | */ |
| 273 | unsigned int hostflags; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 274 | int protocol; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 275 | #ifdef TLS1_3_VERSION |
| 276 | int post_handshake_auth; |
| 277 | #endif |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 278 | PyObject *msg_cb; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 279 | PyObject *keylog_filename; |
| 280 | BIO *keylog_bio; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 281 | /* Cached module state, also used in SSLSocket and SSLSession code. */ |
| 282 | _sslmodulestate *state; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 283 | } PySSLContext; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 284 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 285 | typedef struct { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 286 | int ssl; /* last seen error from SSL */ |
| 287 | int c; /* last seen error from libc */ |
| 288 | #ifdef MS_WINDOWS |
| 289 | int ws; /* last seen error from winsock */ |
| 290 | #endif |
| 291 | } _PySSLError; |
| 292 | |
| 293 | typedef struct { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 294 | PyObject_HEAD |
| 295 | PyObject *Socket; /* weakref to socket on which we're layered */ |
| 296 | SSL *ssl; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 297 | PySSLContext *ctx; /* weakref to SSL context */ |
Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 298 | char shutdown_seen_zero; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 299 | enum py_ssl_server_or_client socket_type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 300 | PyObject *owner; /* Python level "owner" passed to servername callback */ |
| 301 | PyObject *server_hostname; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 302 | _PySSLError err; /* last seen error from various sources */ |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 303 | /* Some SSL callbacks don't have error reporting. Callback wrappers |
| 304 | * store exception information on the socket. The handshake, read, write, |
| 305 | * and shutdown methods check for chained exceptions. |
| 306 | */ |
| 307 | PyObject *exc_type; |
| 308 | PyObject *exc_value; |
| 309 | PyObject *exc_tb; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 310 | } PySSLSocket; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 311 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 312 | typedef struct { |
| 313 | PyObject_HEAD |
| 314 | BIO *bio; |
| 315 | int eof_written; |
| 316 | } PySSLMemoryBIO; |
| 317 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 318 | typedef struct { |
| 319 | PyObject_HEAD |
| 320 | SSL_SESSION *session; |
| 321 | PySSLContext *ctx; |
| 322 | } PySSLSession; |
| 323 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 324 | static inline _PySSLError _PySSL_errno(int failed, const SSL *ssl, int retcode) |
| 325 | { |
| 326 | _PySSLError err = { 0 }; |
| 327 | if (failed) { |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 328 | #ifdef MS_WINDOWS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 329 | err.ws = WSAGetLastError(); |
| 330 | _PySSL_FIX_ERRNO; |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 331 | #endif |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 332 | err.c = errno; |
| 333 | err.ssl = SSL_get_error(ssl, retcode); |
| 334 | } |
| 335 | return err; |
| 336 | } |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 337 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 338 | /*[clinic input] |
| 339 | module _ssl |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 340 | class _ssl._SSLContext "PySSLContext *" "get_state_type(type)->PySSLContext_Type" |
| 341 | class _ssl._SSLSocket "PySSLSocket *" "get_state_type(type)->PySSLSocket_Type" |
| 342 | class _ssl.MemoryBIO "PySSLMemoryBIO *" "get_state_type(type)->PySSLMemoryBIO_Type" |
| 343 | class _ssl.SSLSession "PySSLSession *" "get_state_type(type)->PySSLSession_Type" |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 344 | [clinic start generated code]*/ |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 345 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=d293bed8bae240fd]*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 346 | |
| 347 | #include "clinic/_ssl.c.h" |
| 348 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 349 | static int PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 350 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 351 | static int PySSL_set_owner(PySSLSocket *, PyObject *, void *); |
| 352 | static int PySSL_set_session(PySSLSocket *, PyObject *, void *); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 353 | |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 354 | typedef enum { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 355 | SOCKET_IS_NONBLOCKING, |
| 356 | SOCKET_IS_BLOCKING, |
| 357 | SOCKET_HAS_TIMED_OUT, |
| 358 | SOCKET_HAS_BEEN_CLOSED, |
| 359 | SOCKET_TOO_LARGE_FOR_SELECT, |
| 360 | SOCKET_OPERATION_OK |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 361 | } timeout_state; |
| 362 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 363 | /* Wrap error strings with filename and line # */ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 364 | #define ERRSTR1(x,y,z) (x ":" y ": " z) |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 365 | #define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 366 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 367 | /* Get the socket from a PySSLSocket, if it has one */ |
| 368 | #define GET_SOCKET(obj) ((obj)->Socket ? \ |
| 369 | (PySocketSockObject *) PyWeakref_GetObject((obj)->Socket) : NULL) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 370 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 371 | /* If sock is NULL, use a timeout of 0 second */ |
| 372 | #define GET_SOCKET_TIMEOUT(sock) \ |
| 373 | ((sock != NULL) ? (sock)->sock_timeout : 0) |
| 374 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 375 | #include "_ssl/debughelpers.c" |
| 376 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 377 | /* |
| 378 | * SSL errors. |
| 379 | */ |
| 380 | |
| 381 | PyDoc_STRVAR(SSLError_doc, |
| 382 | "An error occurred in the SSL implementation."); |
| 383 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 384 | PyDoc_STRVAR(SSLCertVerificationError_doc, |
| 385 | "A certificate could not be verified."); |
| 386 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 387 | PyDoc_STRVAR(SSLZeroReturnError_doc, |
| 388 | "SSL/TLS session closed cleanly."); |
| 389 | |
| 390 | PyDoc_STRVAR(SSLWantReadError_doc, |
| 391 | "Non-blocking SSL socket needs to read more data\n" |
| 392 | "before the requested operation can be completed."); |
| 393 | |
| 394 | PyDoc_STRVAR(SSLWantWriteError_doc, |
| 395 | "Non-blocking SSL socket needs to write more data\n" |
| 396 | "before the requested operation can be completed."); |
| 397 | |
| 398 | PyDoc_STRVAR(SSLSyscallError_doc, |
| 399 | "System error when attempting SSL operation."); |
| 400 | |
| 401 | PyDoc_STRVAR(SSLEOFError_doc, |
| 402 | "SSL/TLS connection terminated abruptly."); |
| 403 | |
| 404 | static PyObject * |
| 405 | SSLError_str(PyOSErrorObject *self) |
| 406 | { |
| 407 | if (self->strerror != NULL && PyUnicode_Check(self->strerror)) { |
| 408 | Py_INCREF(self->strerror); |
| 409 | return self->strerror; |
| 410 | } |
| 411 | else |
| 412 | return PyObject_Str(self->args); |
| 413 | } |
| 414 | |
| 415 | static PyType_Slot sslerror_type_slots[] = { |
Inada Naoki | 926b0cb | 2019-04-17 08:39:46 +0900 | [diff] [blame] | 416 | {Py_tp_doc, (void*)SSLError_doc}, |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 417 | {Py_tp_str, SSLError_str}, |
| 418 | {0, 0}, |
| 419 | }; |
| 420 | |
| 421 | static PyType_Spec sslerror_type_spec = { |
| 422 | "ssl.SSLError", |
| 423 | sizeof(PyOSErrorObject), |
| 424 | 0, |
| 425 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 426 | sslerror_type_slots |
| 427 | }; |
| 428 | |
| 429 | static void |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 430 | fill_and_set_sslerror(_sslmodulestate *state, |
| 431 | PySSLSocket *sslsock, PyObject *type, int ssl_errno, |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 432 | const char *errstr, int lineno, unsigned long errcode) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 433 | { |
| 434 | PyObject *err_value = NULL, *reason_obj = NULL, *lib_obj = NULL; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 435 | PyObject *verify_obj = NULL, *verify_code_obj = NULL; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 436 | PyObject *init_value, *msg, *key; |
| 437 | _Py_IDENTIFIER(reason); |
| 438 | _Py_IDENTIFIER(library); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 439 | _Py_IDENTIFIER(verify_message); |
| 440 | _Py_IDENTIFIER(verify_code); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 441 | |
| 442 | if (errcode != 0) { |
| 443 | int lib, reason; |
| 444 | |
| 445 | lib = ERR_GET_LIB(errcode); |
| 446 | reason = ERR_GET_REASON(errcode); |
| 447 | key = Py_BuildValue("ii", lib, reason); |
| 448 | if (key == NULL) |
| 449 | goto fail; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 450 | reason_obj = PyDict_GetItemWithError(state->err_codes_to_names, key); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 451 | Py_DECREF(key); |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 452 | if (reason_obj == NULL && PyErr_Occurred()) { |
| 453 | goto fail; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 454 | } |
| 455 | key = PyLong_FromLong(lib); |
| 456 | if (key == NULL) |
| 457 | goto fail; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 458 | lib_obj = PyDict_GetItemWithError(state->lib_codes_to_names, key); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 459 | Py_DECREF(key); |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 460 | if (lib_obj == NULL && PyErr_Occurred()) { |
| 461 | goto fail; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 462 | } |
| 463 | if (errstr == NULL) |
| 464 | errstr = ERR_reason_error_string(errcode); |
| 465 | } |
| 466 | if (errstr == NULL) |
| 467 | errstr = "unknown error"; |
| 468 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 469 | /* verify code for cert validation error */ |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 470 | if ((sslsock != NULL) && (type == state->PySSLCertVerificationErrorObject)) { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 471 | const char *verify_str = NULL; |
| 472 | long verify_code; |
| 473 | |
| 474 | verify_code = SSL_get_verify_result(sslsock->ssl); |
| 475 | verify_code_obj = PyLong_FromLong(verify_code); |
| 476 | if (verify_code_obj == NULL) { |
| 477 | goto fail; |
| 478 | } |
| 479 | |
| 480 | switch (verify_code) { |
| 481 | case X509_V_ERR_HOSTNAME_MISMATCH: |
| 482 | verify_obj = PyUnicode_FromFormat( |
| 483 | "Hostname mismatch, certificate is not valid for '%S'.", |
| 484 | sslsock->server_hostname |
| 485 | ); |
| 486 | break; |
| 487 | case X509_V_ERR_IP_ADDRESS_MISMATCH: |
| 488 | verify_obj = PyUnicode_FromFormat( |
| 489 | "IP address mismatch, certificate is not valid for '%S'.", |
| 490 | sslsock->server_hostname |
| 491 | ); |
| 492 | break; |
| 493 | default: |
| 494 | verify_str = X509_verify_cert_error_string(verify_code); |
| 495 | if (verify_str != NULL) { |
| 496 | verify_obj = PyUnicode_FromString(verify_str); |
| 497 | } else { |
| 498 | verify_obj = Py_None; |
| 499 | Py_INCREF(verify_obj); |
| 500 | } |
| 501 | break; |
| 502 | } |
| 503 | if (verify_obj == NULL) { |
| 504 | goto fail; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | if (verify_obj && reason_obj && lib_obj) |
| 509 | msg = PyUnicode_FromFormat("[%S: %S] %s: %S (_ssl.c:%d)", |
| 510 | lib_obj, reason_obj, errstr, verify_obj, |
| 511 | lineno); |
| 512 | else if (reason_obj && lib_obj) |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 513 | msg = PyUnicode_FromFormat("[%S: %S] %s (_ssl.c:%d)", |
| 514 | lib_obj, reason_obj, errstr, lineno); |
| 515 | else if (lib_obj) |
| 516 | msg = PyUnicode_FromFormat("[%S] %s (_ssl.c:%d)", |
| 517 | lib_obj, errstr, lineno); |
| 518 | else |
| 519 | msg = PyUnicode_FromFormat("%s (_ssl.c:%d)", errstr, lineno); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 520 | if (msg == NULL) |
| 521 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 522 | |
Paul Monson | fb7e750 | 2019-05-15 15:38:55 -0700 | [diff] [blame] | 523 | init_value = Py_BuildValue("iN", ERR_GET_REASON(ssl_errno), msg); |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 524 | if (init_value == NULL) |
| 525 | goto fail; |
| 526 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 527 | err_value = PyObject_CallObject(type, init_value); |
| 528 | Py_DECREF(init_value); |
| 529 | if (err_value == NULL) |
| 530 | goto fail; |
Victor Stinner | ba9be47 | 2013-10-31 15:00:24 +0100 | [diff] [blame] | 531 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 532 | if (reason_obj == NULL) |
| 533 | reason_obj = Py_None; |
| 534 | if (_PyObject_SetAttrId(err_value, &PyId_reason, reason_obj)) |
| 535 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 536 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 537 | if (lib_obj == NULL) |
| 538 | lib_obj = Py_None; |
| 539 | if (_PyObject_SetAttrId(err_value, &PyId_library, lib_obj)) |
| 540 | goto fail; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 541 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 542 | if ((sslsock != NULL) && (type == state->PySSLCertVerificationErrorObject)) { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 543 | /* Only set verify code / message for SSLCertVerificationError */ |
| 544 | if (_PyObject_SetAttrId(err_value, &PyId_verify_code, |
| 545 | verify_code_obj)) |
| 546 | goto fail; |
| 547 | if (_PyObject_SetAttrId(err_value, &PyId_verify_message, verify_obj)) |
| 548 | goto fail; |
| 549 | } |
| 550 | |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 551 | PyErr_SetObject(type, err_value); |
| 552 | fail: |
| 553 | Py_XDECREF(err_value); |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 554 | Py_XDECREF(verify_code_obj); |
| 555 | Py_XDECREF(verify_obj); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 556 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 557 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 558 | static int |
| 559 | PySSL_ChainExceptions(PySSLSocket *sslsock) { |
| 560 | if (sslsock->exc_type == NULL) |
| 561 | return 0; |
| 562 | |
| 563 | _PyErr_ChainExceptions(sslsock->exc_type, sslsock->exc_value, sslsock->exc_tb); |
| 564 | sslsock->exc_type = NULL; |
| 565 | sslsock->exc_value = NULL; |
| 566 | sslsock->exc_tb = NULL; |
| 567 | return -1; |
| 568 | } |
| 569 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 570 | static PyObject * |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 571 | PySSL_SetError(PySSLSocket *sslsock, int ret, const char *filename, int lineno) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 572 | { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 573 | PyObject *type; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 574 | char *errstr = NULL; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 575 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 576 | enum py_ssl_error p = PY_SSL_ERROR_NONE; |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 577 | unsigned long e = 0; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 578 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 579 | assert(sslsock != NULL); |
| 580 | |
| 581 | _sslmodulestate *state = get_state_sock(sslsock); |
| 582 | type = state->PySSLErrorObject; |
| 583 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 584 | assert(ret <= 0); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 585 | e = ERR_peek_last_error(); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 586 | |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 587 | if (sslsock->ssl != NULL) { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 588 | err = sslsock->err; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 589 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 590 | switch (err.ssl) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 591 | case SSL_ERROR_ZERO_RETURN: |
Antoine Pitrou | 41032a6 | 2011-10-27 23:56:55 +0200 | [diff] [blame] | 592 | errstr = "TLS/SSL connection has been closed (EOF)"; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 593 | type = state->PySSLZeroReturnErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 594 | p = PY_SSL_ERROR_ZERO_RETURN; |
| 595 | break; |
| 596 | case SSL_ERROR_WANT_READ: |
| 597 | errstr = "The operation did not complete (read)"; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 598 | type = state->PySSLWantReadErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 599 | p = PY_SSL_ERROR_WANT_READ; |
| 600 | break; |
| 601 | case SSL_ERROR_WANT_WRITE: |
| 602 | p = PY_SSL_ERROR_WANT_WRITE; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 603 | type = state->PySSLWantWriteErrorObject; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 604 | errstr = "The operation did not complete (write)"; |
| 605 | break; |
| 606 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 607 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 608 | errstr = "The operation did not complete (X509 lookup)"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 609 | break; |
| 610 | case SSL_ERROR_WANT_CONNECT: |
| 611 | p = PY_SSL_ERROR_WANT_CONNECT; |
| 612 | errstr = "The operation did not complete (connect)"; |
| 613 | break; |
| 614 | case SSL_ERROR_SYSCALL: |
| 615 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 616 | if (e == 0) { |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 617 | PySocketSockObject *s = GET_SOCKET(sslsock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 618 | if (ret == 0 || (((PyObject *)s) == Py_None)) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 619 | p = PY_SSL_ERROR_EOF; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 620 | type = state->PySSLEOFErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 621 | errstr = "EOF occurred in violation of protocol"; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 622 | } else if (s && ret == -1) { |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 623 | /* underlying BIO reported an I/O error */ |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 624 | ERR_clear_error(); |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 625 | #ifdef MS_WINDOWS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 626 | if (err.ws) { |
| 627 | return PyErr_SetFromWindowsErr(err.ws); |
| 628 | } |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 629 | #endif |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 630 | if (err.c) { |
| 631 | errno = err.c; |
Steve Dower | e6eb48c | 2017-09-08 15:16:15 -0700 | [diff] [blame] | 632 | return PyErr_SetFromErrno(PyExc_OSError); |
| 633 | } |
Dima Tisnek | 495bd03 | 2020-08-16 02:01:19 +0900 | [diff] [blame] | 634 | else { |
| 635 | p = PY_SSL_ERROR_EOF; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 636 | type = state->PySSLEOFErrorObject; |
Dima Tisnek | 495bd03 | 2020-08-16 02:01:19 +0900 | [diff] [blame] | 637 | errstr = "EOF occurred in violation of protocol"; |
| 638 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 639 | } else { /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 640 | p = PY_SSL_ERROR_SYSCALL; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 641 | type = state->PySSLSyscallErrorObject; |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 642 | errstr = "Some I/O error occurred"; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 643 | } |
| 644 | } else { |
| 645 | p = PY_SSL_ERROR_SYSCALL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 646 | } |
| 647 | break; |
| 648 | } |
| 649 | case SSL_ERROR_SSL: |
| 650 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 651 | p = PY_SSL_ERROR_SSL; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 652 | if (e == 0) { |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 653 | /* possible? */ |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 654 | errstr = "A failure in the SSL library occurred"; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 655 | } |
| 656 | if (ERR_GET_LIB(e) == ERR_LIB_SSL && |
| 657 | ERR_GET_REASON(e) == SSL_R_CERTIFICATE_VERIFY_FAILED) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 658 | type = state->PySSLCertVerificationErrorObject; |
Christian Heimes | b3ad0e5 | 2017-09-08 12:00:19 -0700 | [diff] [blame] | 659 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | default: |
| 663 | p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
| 664 | errstr = "Invalid error code"; |
| 665 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 666 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 667 | fill_and_set_sslerror(state, sslsock, type, p, errstr, lineno, e); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 668 | ERR_clear_error(); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 669 | PySSL_ChainExceptions(sslsock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 670 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 673 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 674 | _setSSLError (_sslmodulestate *state, const char *errstr, int errcode, const char *filename, int lineno) |
| 675 | { |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 676 | if (errstr == NULL) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 677 | errcode = ERR_peek_last_error(); |
Antoine Pitrou | 3b36fb1 | 2012-06-22 21:11:52 +0200 | [diff] [blame] | 678 | else |
| 679 | errcode = 0; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 680 | fill_and_set_sslerror(state, NULL, state->PySSLErrorObject, errcode, errstr, lineno, errcode); |
Antoine Pitrou | 9d74b42 | 2010-05-16 23:14:22 +0000 | [diff] [blame] | 681 | ERR_clear_error(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 682 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 685 | /* |
| 686 | * SSL objects |
| 687 | */ |
| 688 | |
| 689 | static int |
| 690 | _ssl_configure_hostname(PySSLSocket *self, const char* server_hostname) |
| 691 | { |
| 692 | int retval = -1; |
| 693 | ASN1_OCTET_STRING *ip; |
| 694 | PyObject *hostname; |
| 695 | size_t len; |
| 696 | |
| 697 | assert(server_hostname); |
| 698 | |
| 699 | /* Disable OpenSSL's special mode with leading dot in hostname: |
| 700 | * When name starts with a dot (e.g ".example.com"), it will be |
| 701 | * matched by a certificate valid for any sub-domain of name. |
| 702 | */ |
| 703 | len = strlen(server_hostname); |
| 704 | if (len == 0 || *server_hostname == '.') { |
| 705 | PyErr_SetString( |
| 706 | PyExc_ValueError, |
| 707 | "server_hostname cannot be an empty string or start with a " |
| 708 | "leading dot."); |
| 709 | return retval; |
| 710 | } |
| 711 | |
| 712 | /* inet_pton is not available on all platforms. */ |
| 713 | ip = a2i_IPADDRESS(server_hostname); |
| 714 | if (ip == NULL) { |
| 715 | ERR_clear_error(); |
| 716 | } |
| 717 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 718 | hostname = PyUnicode_Decode(server_hostname, len, "ascii", "strict"); |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 719 | if (hostname == NULL) { |
| 720 | goto error; |
| 721 | } |
| 722 | self->server_hostname = hostname; |
| 723 | |
| 724 | /* Only send SNI extension for non-IP hostnames */ |
| 725 | if (ip == NULL) { |
| 726 | if (!SSL_set_tlsext_host_name(self->ssl, server_hostname)) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 727 | _setSSLError(get_state_sock(self), NULL, 0, __FILE__, __LINE__); |
Zackery Spytz | c32f297 | 2020-10-25 12:02:30 -0600 | [diff] [blame] | 728 | goto error; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | if (self->ctx->check_hostname) { |
| 732 | X509_VERIFY_PARAM *param = SSL_get0_param(self->ssl); |
| 733 | if (ip == NULL) { |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 734 | if (!X509_VERIFY_PARAM_set1_host(param, server_hostname, |
| 735 | strlen(server_hostname))) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 736 | _setSSLError(get_state_sock(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 737 | goto error; |
| 738 | } |
| 739 | } else { |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 740 | if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_get0_data(ip), |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 741 | ASN1_STRING_length(ip))) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 742 | _setSSLError(get_state_sock(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 743 | goto error; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | retval = 0; |
| 748 | error: |
| 749 | if (ip != NULL) { |
| 750 | ASN1_OCTET_STRING_free(ip); |
| 751 | } |
| 752 | return retval; |
| 753 | } |
| 754 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 755 | static PySSLSocket * |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 756 | newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 757 | enum py_ssl_server_or_client socket_type, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 758 | char *server_hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 759 | PyObject *owner, PyObject *session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 760 | PySSLMemoryBIO *inbio, PySSLMemoryBIO *outbio) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 761 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 762 | PySSLSocket *self; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 763 | SSL_CTX *ctx = sslctx->ctx; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 764 | _PySSLError err = { 0 }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 765 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 766 | self = PyObject_New(PySSLSocket, get_state_ctx(sslctx)->PySSLSocket_Type); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 767 | if (self == NULL) |
| 768 | return NULL; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 769 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 770 | self->ssl = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 771 | self->Socket = NULL; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 772 | self->ctx = sslctx; |
Nathaniel J. Smith | 65ece7c | 2017-06-07 23:30:43 -0700 | [diff] [blame] | 773 | Py_INCREF(sslctx); |
Antoine Pitrou | 860aee7 | 2013-09-29 19:52:45 +0200 | [diff] [blame] | 774 | self->shutdown_seen_zero = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 775 | self->owner = NULL; |
Benjamin Peterson | 81b9ecd | 2016-08-15 21:55:37 -0700 | [diff] [blame] | 776 | self->server_hostname = NULL; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 777 | self->err = err; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 778 | self->exc_type = NULL; |
| 779 | self->exc_value = NULL; |
| 780 | self->exc_tb = NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 781 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 782 | /* Make sure the SSL error state is initialized */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 783 | ERR_clear_error(); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 784 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 785 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 786 | self->ssl = SSL_new(ctx); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 787 | PySSL_END_ALLOW_THREADS |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 788 | if (self->ssl == NULL) { |
| 789 | Py_DECREF(self); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 790 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 791 | return NULL; |
| 792 | } |
Christian Heimes | b467d9a | 2021-04-17 10:07:19 +0200 | [diff] [blame] | 793 | /* bpo43522 and OpenSSL < 1.1.1l: copy hostflags manually */ |
| 794 | #if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION < 0x101010cf |
| 795 | X509_VERIFY_PARAM *ssl_params = SSL_get0_param(self->ssl); |
| 796 | X509_VERIFY_PARAM_set_hostflags(ssl_params, sslctx->hostflags); |
| 797 | #endif |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 798 | SSL_set_app_data(self->ssl, self); |
| 799 | if (sock) { |
| 800 | SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int)); |
| 801 | } else { |
| 802 | /* BIOs are reference counted and SSL_set_bio borrows our reference. |
| 803 | * To prevent a double free in memory_bio_dealloc() we need to take an |
| 804 | * extra reference here. */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 805 | BIO_up_ref(inbio->bio); |
| 806 | BIO_up_ref(outbio->bio); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 807 | SSL_set_bio(self->ssl, inbio->bio, outbio->bio); |
| 808 | } |
Alex Gaynor | f042242 | 2018-05-14 11:51:45 -0400 | [diff] [blame] | 809 | SSL_set_mode(self->ssl, |
| 810 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_AUTO_RETRY); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 811 | |
Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 812 | #ifdef TLS1_3_VERSION |
| 813 | if (sslctx->post_handshake_auth == 1) { |
| 814 | if (socket_type == PY_SSL_SERVER) { |
| 815 | /* bpo-37428: OpenSSL does not ignore SSL_VERIFY_POST_HANDSHAKE. |
| 816 | * Set SSL_VERIFY_POST_HANDSHAKE flag only for server sockets and |
| 817 | * only in combination with SSL_VERIFY_PEER flag. */ |
| 818 | int mode = SSL_get_verify_mode(self->ssl); |
| 819 | if (mode & SSL_VERIFY_PEER) { |
| 820 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 821 | verify_cb = SSL_get_verify_callback(self->ssl); |
| 822 | mode |= SSL_VERIFY_POST_HANDSHAKE; |
| 823 | SSL_set_verify(self->ssl, mode, verify_cb); |
| 824 | } |
| 825 | } else { |
| 826 | /* client socket */ |
| 827 | SSL_set_post_handshake_auth(self->ssl, 1); |
| 828 | } |
| 829 | } |
| 830 | #endif |
| 831 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 832 | if (server_hostname != NULL) { |
| 833 | if (_ssl_configure_hostname(self, server_hostname) < 0) { |
| 834 | Py_DECREF(self); |
| 835 | return NULL; |
| 836 | } |
| 837 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 838 | /* If the socket is in non-blocking mode or timeout mode, set the BIO |
| 839 | * to non-blocking mode (blocking is the default) |
| 840 | */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 841 | if (sock && sock->sock_timeout >= 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 842 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 843 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 844 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 845 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 846 | PySSL_BEGIN_ALLOW_THREADS |
| 847 | if (socket_type == PY_SSL_CLIENT) |
| 848 | SSL_set_connect_state(self->ssl); |
| 849 | else |
| 850 | SSL_set_accept_state(self->ssl); |
| 851 | PySSL_END_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 852 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 853 | self->socket_type = socket_type; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 854 | if (sock != NULL) { |
| 855 | self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL); |
| 856 | if (self->Socket == NULL) { |
| 857 | Py_DECREF(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 858 | return NULL; |
| 859 | } |
Victor Stinner | a9eb38f | 2013-10-31 16:35:38 +0100 | [diff] [blame] | 860 | } |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 861 | if (owner && owner != Py_None) { |
| 862 | if (PySSL_set_owner(self, owner, NULL) == -1) { |
| 863 | Py_DECREF(self); |
| 864 | return NULL; |
| 865 | } |
| 866 | } |
| 867 | if (session && session != Py_None) { |
| 868 | if (PySSL_set_session(self, session, NULL) == -1) { |
| 869 | Py_DECREF(self); |
| 870 | return NULL; |
| 871 | } |
| 872 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 873 | return self; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 876 | /* SSL object methods */ |
| 877 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 878 | /*[clinic input] |
| 879 | _ssl._SSLSocket.do_handshake |
| 880 | [clinic start generated code]*/ |
| 881 | |
| 882 | static PyObject * |
| 883 | _ssl__SSLSocket_do_handshake_impl(PySSLSocket *self) |
| 884 | /*[clinic end generated code: output=6c0898a8936548f6 input=d2d737de3df018c8]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 885 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 886 | int ret; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 887 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 888 | int sockstate, nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 889 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 890 | _PyTime_t timeout, deadline = 0; |
| 891 | int has_timeout; |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 892 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 893 | if (sock) { |
| 894 | if (((PyObject*)sock) == Py_None) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 895 | _setSSLError(get_state_sock(self), |
| 896 | "Underlying socket connection gone", |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 897 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 898 | return NULL; |
| 899 | } |
| 900 | Py_INCREF(sock); |
| 901 | |
| 902 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 903 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 904 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 905 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 906 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 907 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 908 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 909 | has_timeout = (timeout > 0); |
| 910 | if (has_timeout) |
| 911 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 912 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 913 | /* Actually negotiate SSL connection */ |
| 914 | /* XXX If SSL_do_handshake() returns 0, it's also a failure. */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 915 | do { |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 916 | PySSL_BEGIN_ALLOW_THREADS |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 917 | ret = SSL_do_handshake(self->ssl); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 918 | err = _PySSL_errno(ret < 1, self->ssl, ret); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 919 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 920 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 921 | |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 922 | if (PyErr_CheckSignals()) |
| 923 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 924 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 925 | if (has_timeout) |
| 926 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 927 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 928 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 929 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 930 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 931 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 932 | } else { |
| 933 | sockstate = SOCKET_OPERATION_OK; |
| 934 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 935 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 936 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 937 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 938 | ERRSTR("The handshake operation timed out")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 939 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 940 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 941 | PyErr_SetString(get_state_sock(self)->PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 942 | ERRSTR("Underlying socket has been closed.")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 943 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 944 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 945 | PyErr_SetString(get_state_sock(self)->PySSLErrorObject, |
Antoine Pitrou | 525807b | 2010-05-12 14:05:24 +0000 | [diff] [blame] | 946 | ERRSTR("Underlying socket too large for select().")); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 947 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 948 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 949 | break; |
| 950 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 951 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 952 | err.ssl == SSL_ERROR_WANT_WRITE); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 953 | Py_XDECREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 954 | if (ret < 1) |
| 955 | return PySSL_SetError(self, ret, __FILE__, __LINE__); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 956 | if (PySSL_ChainExceptions(self) < 0) |
| 957 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 958 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 959 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 960 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 961 | PySSL_ChainExceptions(self); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 962 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 965 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 966 | _asn1obj2py(_sslmodulestate *state, const ASN1_OBJECT *name, int no_name) |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 967 | { |
| 968 | char buf[X509_NAME_MAXLEN]; |
| 969 | char *namebuf = buf; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 970 | int buflen; |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 971 | PyObject *name_obj = NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 972 | |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 973 | buflen = OBJ_obj2txt(namebuf, X509_NAME_MAXLEN, name, no_name); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 974 | if (buflen < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 975 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 976 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 977 | } |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 978 | /* initial buffer is too small for oid + terminating null byte */ |
| 979 | if (buflen > X509_NAME_MAXLEN - 1) { |
| 980 | /* make OBJ_obj2txt() calculate the required buflen */ |
| 981 | buflen = OBJ_obj2txt(NULL, 0, name, no_name); |
| 982 | /* allocate len + 1 for terminating NULL byte */ |
| 983 | namebuf = PyMem_Malloc(buflen + 1); |
| 984 | if (namebuf == NULL) { |
| 985 | PyErr_NoMemory(); |
| 986 | return NULL; |
| 987 | } |
| 988 | buflen = OBJ_obj2txt(namebuf, buflen + 1, name, no_name); |
| 989 | if (buflen < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 990 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 991 | goto done; |
| 992 | } |
| 993 | } |
| 994 | if (!buflen && no_name) { |
| 995 | Py_INCREF(Py_None); |
| 996 | name_obj = Py_None; |
| 997 | } |
| 998 | else { |
| 999 | name_obj = PyUnicode_FromStringAndSize(namebuf, buflen); |
| 1000 | } |
| 1001 | |
| 1002 | done: |
| 1003 | if (buf != namebuf) { |
| 1004 | PyMem_Free(namebuf); |
| 1005 | } |
| 1006 | return name_obj; |
| 1007 | } |
| 1008 | |
| 1009 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1010 | _create_tuple_for_attribute(_sslmodulestate *state, |
| 1011 | ASN1_OBJECT *name, ASN1_STRING *value) |
Serhiy Storchaka | e503ca5 | 2017-09-05 01:28:53 +0300 | [diff] [blame] | 1012 | { |
| 1013 | Py_ssize_t buflen; |
| 1014 | unsigned char *valuebuf = NULL; |
| 1015 | PyObject *attr; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1016 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1017 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 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 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1021 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1022 | attr = Py_BuildValue("Ns#", _asn1obj2py(state, name, 0), valuebuf, buflen); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1023 | OPENSSL_free(valuebuf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1024 | return attr; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1028 | _create_tuple_for_X509_NAME (_sslmodulestate *state, X509_NAME *xname) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1029 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1030 | PyObject *dn = NULL; /* tuple which represents the "distinguished name" */ |
| 1031 | PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */ |
| 1032 | PyObject *rdnt; |
| 1033 | PyObject *attr = NULL; /* tuple to hold an attribute */ |
| 1034 | int entry_count = X509_NAME_entry_count(xname); |
| 1035 | X509_NAME_ENTRY *entry; |
| 1036 | ASN1_OBJECT *name; |
| 1037 | ASN1_STRING *value; |
| 1038 | int index_counter; |
| 1039 | int rdn_level = -1; |
| 1040 | int retcode; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1041 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1042 | dn = PyList_New(0); |
| 1043 | if (dn == NULL) |
| 1044 | return NULL; |
| 1045 | /* now create another tuple to hold the top-level RDN */ |
| 1046 | rdn = PyList_New(0); |
| 1047 | if (rdn == NULL) |
| 1048 | goto fail0; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1049 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1050 | for (index_counter = 0; |
| 1051 | index_counter < entry_count; |
| 1052 | index_counter++) |
| 1053 | { |
| 1054 | entry = X509_NAME_get_entry(xname, index_counter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1055 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1056 | /* check to see if we've gotten to a new RDN */ |
| 1057 | if (rdn_level >= 0) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1058 | if (rdn_level != X509_NAME_ENTRY_set(entry)) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1059 | /* yes, new RDN */ |
| 1060 | /* add old RDN to DN */ |
| 1061 | rdnt = PyList_AsTuple(rdn); |
| 1062 | Py_DECREF(rdn); |
| 1063 | if (rdnt == NULL) |
| 1064 | goto fail0; |
| 1065 | retcode = PyList_Append(dn, rdnt); |
| 1066 | Py_DECREF(rdnt); |
| 1067 | if (retcode < 0) |
| 1068 | goto fail0; |
| 1069 | /* create new RDN */ |
| 1070 | rdn = PyList_New(0); |
| 1071 | if (rdn == NULL) |
| 1072 | goto fail0; |
| 1073 | } |
| 1074 | } |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1075 | rdn_level = X509_NAME_ENTRY_set(entry); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1076 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1077 | /* now add this attribute to the current RDN */ |
| 1078 | name = X509_NAME_ENTRY_get_object(entry); |
| 1079 | value = X509_NAME_ENTRY_get_data(entry); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1080 | attr = _create_tuple_for_attribute(state, name, value); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1081 | /* |
| 1082 | fprintf(stderr, "RDN level %d, attribute %s: %s\n", |
| 1083 | entry->set, |
| 1084 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)), |
| 1085 | PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1))); |
| 1086 | */ |
| 1087 | if (attr == NULL) |
| 1088 | goto fail1; |
| 1089 | retcode = PyList_Append(rdn, attr); |
| 1090 | Py_DECREF(attr); |
| 1091 | if (retcode < 0) |
| 1092 | goto fail1; |
| 1093 | } |
| 1094 | /* now, there's typically a dangling RDN */ |
Antoine Pitrou | 2f5a163 | 2012-02-15 22:25:27 +0100 | [diff] [blame] | 1095 | if (rdn != NULL) { |
| 1096 | if (PyList_GET_SIZE(rdn) > 0) { |
| 1097 | rdnt = PyList_AsTuple(rdn); |
| 1098 | Py_DECREF(rdn); |
| 1099 | if (rdnt == NULL) |
| 1100 | goto fail0; |
| 1101 | retcode = PyList_Append(dn, rdnt); |
| 1102 | Py_DECREF(rdnt); |
| 1103 | if (retcode < 0) |
| 1104 | goto fail0; |
| 1105 | } |
| 1106 | else { |
| 1107 | Py_DECREF(rdn); |
| 1108 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1109 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1110 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1111 | /* convert list to tuple */ |
| 1112 | rdnt = PyList_AsTuple(dn); |
| 1113 | Py_DECREF(dn); |
| 1114 | if (rdnt == NULL) |
| 1115 | return NULL; |
| 1116 | return rdnt; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1117 | |
| 1118 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1119 | Py_XDECREF(rdn); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1120 | |
| 1121 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1122 | Py_XDECREF(dn); |
| 1123 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1127 | _get_peer_alt_names (_sslmodulestate *state, X509 *certificate) { |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1128 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1129 | /* this code follows the procedure outlined in |
| 1130 | OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print() |
| 1131 | function to extract the STACK_OF(GENERAL_NAME), |
| 1132 | then iterates through the stack to add the |
| 1133 | names. */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1134 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1135 | int j; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1136 | PyObject *peer_alt_names = Py_None; |
Christian Heimes | 60bf2fc | 2013-09-05 16:04:35 +0200 | [diff] [blame] | 1137 | PyObject *v = NULL, *t; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1138 | GENERAL_NAMES *names = NULL; |
| 1139 | GENERAL_NAME *name; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1140 | BIO *biobuf = NULL; |
| 1141 | char buf[2048]; |
| 1142 | char *vptr; |
| 1143 | int len; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1144 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1145 | if (certificate == NULL) |
| 1146 | return peer_alt_names; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1147 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1148 | /* get a memory buffer */ |
| 1149 | biobuf = BIO_new(BIO_s_mem()); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1150 | if (biobuf == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1151 | PyErr_SetString(state->PySSLErrorObject, "failed to allocate BIO"); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1152 | return NULL; |
| 1153 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1154 | |
Alex Gaynor | b87c0df | 2017-06-06 07:53:11 -0400 | [diff] [blame] | 1155 | names = (GENERAL_NAMES *)X509_get_ext_d2i( |
| 1156 | certificate, NID_subject_alt_name, NULL, NULL); |
| 1157 | if (names != NULL) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1158 | if (peer_alt_names == Py_None) { |
| 1159 | peer_alt_names = PyList_New(0); |
| 1160 | if (peer_alt_names == NULL) |
| 1161 | goto fail; |
| 1162 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1163 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1164 | for(j = 0; j < sk_GENERAL_NAME_num(names); j++) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1165 | /* get a rendering of each name in the set of names */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1166 | int gntype; |
| 1167 | ASN1_STRING *as = NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1168 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1169 | name = sk_GENERAL_NAME_value(names, j); |
Christian Heimes | 474afdd | 2013-08-17 17:18:56 +0200 | [diff] [blame] | 1170 | gntype = name->type; |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1171 | switch (gntype) { |
| 1172 | case GEN_DIRNAME: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1173 | /* we special-case DirName as a tuple of |
| 1174 | tuples of attributes */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1175 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1176 | t = PyTuple_New(2); |
| 1177 | if (t == NULL) { |
| 1178 | goto fail; |
| 1179 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1180 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1181 | v = PyUnicode_FromString("DirName"); |
| 1182 | if (v == NULL) { |
| 1183 | Py_DECREF(t); |
| 1184 | goto fail; |
| 1185 | } |
| 1186 | PyTuple_SET_ITEM(t, 0, v); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1187 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1188 | v = _create_tuple_for_X509_NAME(state, name->d.dirn); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1189 | if (v == NULL) { |
| 1190 | Py_DECREF(t); |
| 1191 | goto fail; |
| 1192 | } |
| 1193 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1194 | break; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1195 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1196 | case GEN_EMAIL: |
| 1197 | case GEN_DNS: |
| 1198 | case GEN_URI: |
| 1199 | /* GENERAL_NAME_print() doesn't handle NULL bytes in ASN1_string |
| 1200 | correctly, CVE-2013-4238 */ |
| 1201 | t = PyTuple_New(2); |
| 1202 | if (t == NULL) |
| 1203 | goto fail; |
| 1204 | switch (gntype) { |
| 1205 | case GEN_EMAIL: |
| 1206 | v = PyUnicode_FromString("email"); |
| 1207 | as = name->d.rfc822Name; |
| 1208 | break; |
| 1209 | case GEN_DNS: |
| 1210 | v = PyUnicode_FromString("DNS"); |
| 1211 | as = name->d.dNSName; |
| 1212 | break; |
| 1213 | case GEN_URI: |
| 1214 | v = PyUnicode_FromString("URI"); |
| 1215 | as = name->d.uniformResourceIdentifier; |
| 1216 | break; |
| 1217 | } |
| 1218 | if (v == NULL) { |
| 1219 | Py_DECREF(t); |
| 1220 | goto fail; |
| 1221 | } |
| 1222 | PyTuple_SET_ITEM(t, 0, v); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1223 | v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_get0_data(as), |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1224 | ASN1_STRING_length(as)); |
| 1225 | if (v == NULL) { |
| 1226 | Py_DECREF(t); |
| 1227 | goto fail; |
| 1228 | } |
| 1229 | PyTuple_SET_ITEM(t, 1, v); |
| 1230 | break; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1231 | |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1232 | case GEN_RID: |
| 1233 | t = PyTuple_New(2); |
| 1234 | if (t == NULL) |
| 1235 | goto fail; |
| 1236 | |
| 1237 | v = PyUnicode_FromString("Registered ID"); |
| 1238 | if (v == NULL) { |
| 1239 | Py_DECREF(t); |
| 1240 | goto fail; |
| 1241 | } |
| 1242 | PyTuple_SET_ITEM(t, 0, v); |
| 1243 | |
| 1244 | len = i2t_ASN1_OBJECT(buf, sizeof(buf)-1, name->d.rid); |
| 1245 | if (len < 0) { |
| 1246 | Py_DECREF(t); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1247 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1248 | goto fail; |
| 1249 | } else if (len >= (int)sizeof(buf)) { |
| 1250 | v = PyUnicode_FromString("<INVALID>"); |
| 1251 | } else { |
| 1252 | v = PyUnicode_FromStringAndSize(buf, len); |
| 1253 | } |
| 1254 | if (v == NULL) { |
| 1255 | Py_DECREF(t); |
| 1256 | goto fail; |
| 1257 | } |
| 1258 | PyTuple_SET_ITEM(t, 1, v); |
| 1259 | break; |
| 1260 | |
Christian Heimes | 2b7de66 | 2019-12-07 17:59:36 +0100 | [diff] [blame] | 1261 | case GEN_IPADD: |
| 1262 | /* OpenSSL < 3.0.0 adds a trailing \n to IPv6. 3.0.0 removed |
| 1263 | * the trailing newline. Remove it in all versions |
| 1264 | */ |
| 1265 | t = PyTuple_New(2); |
| 1266 | if (t == NULL) |
| 1267 | goto fail; |
| 1268 | |
| 1269 | v = PyUnicode_FromString("IP Address"); |
| 1270 | if (v == NULL) { |
| 1271 | Py_DECREF(t); |
| 1272 | goto fail; |
| 1273 | } |
| 1274 | PyTuple_SET_ITEM(t, 0, v); |
| 1275 | |
| 1276 | if (name->d.ip->length == 4) { |
| 1277 | unsigned char *p = name->d.ip->data; |
| 1278 | v = PyUnicode_FromFormat( |
| 1279 | "%d.%d.%d.%d", |
| 1280 | p[0], p[1], p[2], p[3] |
| 1281 | ); |
| 1282 | } else if (name->d.ip->length == 16) { |
| 1283 | /* PyUnicode_FromFormat() does not support %X */ |
| 1284 | unsigned char *p = name->d.ip->data; |
| 1285 | len = sprintf( |
| 1286 | buf, |
| 1287 | "%X:%X:%X:%X:%X:%X:%X:%X", |
| 1288 | p[0] << 8 | p[1], |
| 1289 | p[2] << 8 | p[3], |
| 1290 | p[4] << 8 | p[5], |
| 1291 | p[6] << 8 | p[7], |
| 1292 | p[8] << 8 | p[9], |
| 1293 | p[10] << 8 | p[11], |
| 1294 | p[12] << 8 | p[13], |
| 1295 | p[14] << 8 | p[15] |
| 1296 | ); |
| 1297 | v = PyUnicode_FromStringAndSize(buf, len); |
| 1298 | } else { |
| 1299 | v = PyUnicode_FromString("<invalid>"); |
| 1300 | } |
| 1301 | |
| 1302 | if (v == NULL) { |
| 1303 | Py_DECREF(t); |
| 1304 | goto fail; |
| 1305 | } |
| 1306 | PyTuple_SET_ITEM(t, 1, v); |
| 1307 | break; |
| 1308 | |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1309 | default: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1310 | /* for everything else, we use the OpenSSL print form */ |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1311 | switch (gntype) { |
| 1312 | /* check for new general name type */ |
| 1313 | case GEN_OTHERNAME: |
| 1314 | case GEN_X400: |
| 1315 | case GEN_EDIPARTY: |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1316 | case GEN_RID: |
| 1317 | break; |
| 1318 | default: |
| 1319 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1320 | "Unknown general name type %d", |
| 1321 | gntype) == -1) { |
| 1322 | goto fail; |
| 1323 | } |
| 1324 | break; |
| 1325 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1326 | (void) BIO_reset(biobuf); |
| 1327 | GENERAL_NAME_print(biobuf, name); |
| 1328 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1329 | if (len < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1330 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1331 | goto fail; |
| 1332 | } |
| 1333 | vptr = strchr(buf, ':'); |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1334 | if (vptr == NULL) { |
| 1335 | PyErr_Format(PyExc_ValueError, |
| 1336 | "Invalid value %.200s", |
| 1337 | buf); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1338 | goto fail; |
Christian Heimes | 1c03abd | 2016-09-06 23:25:35 +0200 | [diff] [blame] | 1339 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1340 | t = PyTuple_New(2); |
| 1341 | if (t == NULL) |
| 1342 | goto fail; |
| 1343 | v = PyUnicode_FromStringAndSize(buf, (vptr - buf)); |
| 1344 | if (v == NULL) { |
| 1345 | Py_DECREF(t); |
| 1346 | goto fail; |
| 1347 | } |
| 1348 | PyTuple_SET_ITEM(t, 0, v); |
| 1349 | v = PyUnicode_FromStringAndSize((vptr + 1), |
| 1350 | (len - (vptr - buf + 1))); |
| 1351 | if (v == NULL) { |
| 1352 | Py_DECREF(t); |
| 1353 | goto fail; |
| 1354 | } |
| 1355 | PyTuple_SET_ITEM(t, 1, v); |
Christian Heimes | 824f7f3 | 2013-08-17 00:54:47 +0200 | [diff] [blame] | 1356 | break; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1357 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1358 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1359 | /* and add that rendering to the list */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1360 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1361 | if (PyList_Append(peer_alt_names, t) < 0) { |
| 1362 | Py_DECREF(t); |
| 1363 | goto fail; |
| 1364 | } |
| 1365 | Py_DECREF(t); |
| 1366 | } |
Antoine Pitrou | 116d6b9 | 2011-11-23 01:39:19 +0100 | [diff] [blame] | 1367 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1368 | } |
| 1369 | BIO_free(biobuf); |
| 1370 | if (peer_alt_names != Py_None) { |
| 1371 | v = PyList_AsTuple(peer_alt_names); |
| 1372 | Py_DECREF(peer_alt_names); |
| 1373 | return v; |
| 1374 | } else { |
| 1375 | return peer_alt_names; |
| 1376 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1377 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1378 | |
| 1379 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1380 | if (biobuf != NULL) |
| 1381 | BIO_free(biobuf); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1382 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1383 | if (peer_alt_names != Py_None) { |
| 1384 | Py_XDECREF(peer_alt_names); |
| 1385 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1386 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1387 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | static PyObject * |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1391 | _get_aia_uri(X509 *certificate, int nid) { |
| 1392 | PyObject *lst = NULL, *ostr = NULL; |
| 1393 | int i, result; |
| 1394 | AUTHORITY_INFO_ACCESS *info; |
| 1395 | |
| 1396 | info = X509_get_ext_d2i(certificate, NID_info_access, NULL, NULL); |
Benjamin Peterson | f0c9038 | 2015-11-14 15:12:18 -0800 | [diff] [blame] | 1397 | if (info == NULL) |
| 1398 | return Py_None; |
| 1399 | if (sk_ACCESS_DESCRIPTION_num(info) == 0) { |
| 1400 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1401 | return Py_None; |
| 1402 | } |
| 1403 | |
| 1404 | if ((lst = PyList_New(0)) == NULL) { |
| 1405 | goto fail; |
| 1406 | } |
| 1407 | |
| 1408 | for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { |
| 1409 | ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); |
| 1410 | ASN1_IA5STRING *uri; |
| 1411 | |
| 1412 | if ((OBJ_obj2nid(ad->method) != nid) || |
| 1413 | (ad->location->type != GEN_URI)) { |
| 1414 | continue; |
| 1415 | } |
| 1416 | uri = ad->location->d.uniformResourceIdentifier; |
| 1417 | ostr = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1418 | uri->length); |
| 1419 | if (ostr == NULL) { |
| 1420 | goto fail; |
| 1421 | } |
| 1422 | result = PyList_Append(lst, ostr); |
| 1423 | Py_DECREF(ostr); |
| 1424 | if (result < 0) { |
| 1425 | goto fail; |
| 1426 | } |
| 1427 | } |
| 1428 | AUTHORITY_INFO_ACCESS_free(info); |
| 1429 | |
| 1430 | /* convert to tuple or None */ |
| 1431 | if (PyList_Size(lst) == 0) { |
| 1432 | Py_DECREF(lst); |
| 1433 | return Py_None; |
| 1434 | } else { |
| 1435 | PyObject *tup; |
| 1436 | tup = PyList_AsTuple(lst); |
| 1437 | Py_DECREF(lst); |
| 1438 | return tup; |
| 1439 | } |
| 1440 | |
| 1441 | fail: |
| 1442 | AUTHORITY_INFO_ACCESS_free(info); |
Christian Heimes | 18fc7be | 2013-11-21 23:57:49 +0100 | [diff] [blame] | 1443 | Py_XDECREF(lst); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1444 | return NULL; |
| 1445 | } |
| 1446 | |
| 1447 | static PyObject * |
| 1448 | _get_crl_dp(X509 *certificate) { |
| 1449 | STACK_OF(DIST_POINT) *dps; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1450 | int i, j; |
| 1451 | PyObject *lst, *res = NULL; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1452 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1453 | dps = X509_get_ext_d2i(certificate, NID_crl_distribution_points, NULL, NULL); |
Christian Heimes | 949ec14 | 2013-11-21 16:26:51 +0100 | [diff] [blame] | 1454 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1455 | if (dps == NULL) |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1456 | return Py_None; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1457 | |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1458 | lst = PyList_New(0); |
| 1459 | if (lst == NULL) |
| 1460 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1461 | |
| 1462 | for (i=0; i < sk_DIST_POINT_num(dps); i++) { |
| 1463 | DIST_POINT *dp; |
| 1464 | STACK_OF(GENERAL_NAME) *gns; |
| 1465 | |
| 1466 | dp = sk_DIST_POINT_value(dps, i); |
Christian Heimes | a37f524 | 2019-01-15 23:47:42 +0100 | [diff] [blame] | 1467 | if (dp->distpoint == NULL) { |
| 1468 | /* Ignore empty DP value, CVE-2019-5010 */ |
| 1469 | continue; |
| 1470 | } |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1471 | gns = dp->distpoint->name.fullname; |
| 1472 | |
| 1473 | for (j=0; j < sk_GENERAL_NAME_num(gns); j++) { |
| 1474 | GENERAL_NAME *gn; |
| 1475 | ASN1_IA5STRING *uri; |
| 1476 | PyObject *ouri; |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1477 | int err; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1478 | |
| 1479 | gn = sk_GENERAL_NAME_value(gns, j); |
| 1480 | if (gn->type != GEN_URI) { |
| 1481 | continue; |
| 1482 | } |
| 1483 | uri = gn->d.uniformResourceIdentifier; |
| 1484 | ouri = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1485 | uri->length); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1486 | if (ouri == NULL) |
| 1487 | goto done; |
| 1488 | |
| 1489 | err = PyList_Append(lst, ouri); |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1490 | Py_DECREF(ouri); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1491 | if (err < 0) |
| 1492 | goto done; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1493 | } |
| 1494 | } |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1495 | |
| 1496 | /* Convert to tuple. */ |
| 1497 | res = (PyList_GET_SIZE(lst) > 0) ? PyList_AsTuple(lst) : Py_None; |
| 1498 | |
| 1499 | done: |
| 1500 | Py_XDECREF(lst); |
Olivier Vielpeau | 2849cc3 | 2017-04-14 21:06:07 -0400 | [diff] [blame] | 1501 | CRL_DIST_POINTS_free(dps); |
Benjamin Peterson | eda06c8 | 2015-11-11 22:07:38 -0800 | [diff] [blame] | 1502 | return res; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1506 | _decode_certificate(_sslmodulestate *state, X509 *certificate) { |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1507 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1508 | PyObject *retval = NULL; |
| 1509 | BIO *biobuf = NULL; |
| 1510 | PyObject *peer; |
| 1511 | PyObject *peer_alt_names = NULL; |
| 1512 | PyObject *issuer; |
| 1513 | PyObject *version; |
| 1514 | PyObject *sn_obj; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1515 | PyObject *obj; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1516 | ASN1_INTEGER *serialNumber; |
| 1517 | char buf[2048]; |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1518 | int len, result; |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1519 | const ASN1_TIME *notBefore, *notAfter; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1520 | PyObject *pnotBefore, *pnotAfter; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1521 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1522 | retval = PyDict_New(); |
| 1523 | if (retval == NULL) |
| 1524 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1525 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1526 | peer = _create_tuple_for_X509_NAME( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1527 | state, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1528 | X509_get_subject_name(certificate)); |
| 1529 | if (peer == NULL) |
| 1530 | goto fail0; |
| 1531 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 1532 | Py_DECREF(peer); |
| 1533 | goto fail0; |
| 1534 | } |
| 1535 | Py_DECREF(peer); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1536 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1537 | issuer = _create_tuple_for_X509_NAME( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1538 | state, |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1539 | X509_get_issuer_name(certificate)); |
| 1540 | if (issuer == NULL) |
| 1541 | goto fail0; |
| 1542 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1543 | Py_DECREF(issuer); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1544 | goto fail0; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1545 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1546 | Py_DECREF(issuer); |
| 1547 | |
| 1548 | version = PyLong_FromLong(X509_get_version(certificate) + 1); |
Christian Heimes | 5962bef | 2013-07-26 15:51:18 +0200 | [diff] [blame] | 1549 | if (version == NULL) |
| 1550 | goto fail0; |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1551 | if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 1552 | Py_DECREF(version); |
| 1553 | goto fail0; |
| 1554 | } |
| 1555 | Py_DECREF(version); |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1556 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1557 | /* get a memory buffer */ |
| 1558 | biobuf = BIO_new(BIO_s_mem()); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1559 | if (biobuf == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1560 | PyErr_SetString(state->PySSLErrorObject, "failed to allocate BIO"); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 1561 | goto fail0; |
| 1562 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1563 | |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1564 | (void) BIO_reset(biobuf); |
| 1565 | serialNumber = X509_get_serialNumber(certificate); |
| 1566 | /* should not exceed 20 octets, 160 bits, so buf is big enough */ |
| 1567 | i2a_ASN1_INTEGER(biobuf, serialNumber); |
| 1568 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1569 | if (len < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1570 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1571 | goto fail1; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1572 | } |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1573 | sn_obj = PyUnicode_FromStringAndSize(buf, len); |
| 1574 | if (sn_obj == NULL) |
| 1575 | goto fail1; |
| 1576 | if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) { |
| 1577 | Py_DECREF(sn_obj); |
| 1578 | goto fail1; |
| 1579 | } |
| 1580 | Py_DECREF(sn_obj); |
| 1581 | |
| 1582 | (void) BIO_reset(biobuf); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1583 | notBefore = X509_get0_notBefore(certificate); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1584 | ASN1_TIME_print(biobuf, notBefore); |
| 1585 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1586 | if (len < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1587 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | fb04691 | 2010-11-09 20:21:19 +0000 | [diff] [blame] | 1588 | goto fail1; |
| 1589 | } |
| 1590 | pnotBefore = PyUnicode_FromStringAndSize(buf, len); |
| 1591 | if (pnotBefore == NULL) |
| 1592 | goto fail1; |
| 1593 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 1594 | Py_DECREF(pnotBefore); |
| 1595 | goto fail1; |
| 1596 | } |
| 1597 | Py_DECREF(pnotBefore); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1598 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1599 | (void) BIO_reset(biobuf); |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 1600 | notAfter = X509_get0_notAfter(certificate); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1601 | ASN1_TIME_print(biobuf, notAfter); |
| 1602 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1603 | if (len < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1604 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1605 | goto fail1; |
| 1606 | } |
| 1607 | pnotAfter = PyUnicode_FromStringAndSize(buf, len); |
| 1608 | if (pnotAfter == NULL) |
| 1609 | goto fail1; |
| 1610 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 1611 | Py_DECREF(pnotAfter); |
| 1612 | goto fail1; |
| 1613 | } |
| 1614 | Py_DECREF(pnotAfter); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1615 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1616 | /* Now look for subjectAltName */ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1617 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1618 | peer_alt_names = _get_peer_alt_names(state, certificate); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1619 | if (peer_alt_names == NULL) |
| 1620 | goto fail1; |
| 1621 | else if (peer_alt_names != Py_None) { |
| 1622 | if (PyDict_SetItemString(retval, "subjectAltName", |
| 1623 | peer_alt_names) < 0) { |
| 1624 | Py_DECREF(peer_alt_names); |
| 1625 | goto fail1; |
| 1626 | } |
| 1627 | Py_DECREF(peer_alt_names); |
| 1628 | } |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1629 | |
Christian Heimes | bd3a7f9 | 2013-11-21 03:40:15 +0100 | [diff] [blame] | 1630 | /* Authority Information Access: OCSP URIs */ |
| 1631 | obj = _get_aia_uri(certificate, NID_ad_OCSP); |
| 1632 | if (obj == NULL) { |
| 1633 | goto fail1; |
| 1634 | } else if (obj != Py_None) { |
| 1635 | result = PyDict_SetItemString(retval, "OCSP", obj); |
| 1636 | Py_DECREF(obj); |
| 1637 | if (result < 0) { |
| 1638 | goto fail1; |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | obj = _get_aia_uri(certificate, NID_ad_ca_issuers); |
| 1643 | if (obj == NULL) { |
| 1644 | goto fail1; |
| 1645 | } else if (obj != Py_None) { |
| 1646 | result = PyDict_SetItemString(retval, "caIssuers", obj); |
| 1647 | Py_DECREF(obj); |
| 1648 | if (result < 0) { |
| 1649 | goto fail1; |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | /* CDP (CRL distribution points) */ |
| 1654 | obj = _get_crl_dp(certificate); |
| 1655 | if (obj == NULL) { |
| 1656 | goto fail1; |
| 1657 | } else if (obj != Py_None) { |
| 1658 | result = PyDict_SetItemString(retval, "crlDistributionPoints", obj); |
| 1659 | Py_DECREF(obj); |
| 1660 | if (result < 0) { |
| 1661 | goto fail1; |
| 1662 | } |
| 1663 | } |
| 1664 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1665 | BIO_free(biobuf); |
| 1666 | return retval; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1667 | |
| 1668 | fail1: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1669 | if (biobuf != NULL) |
| 1670 | BIO_free(biobuf); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1671 | fail0: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1672 | Py_XDECREF(retval); |
| 1673 | return NULL; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1674 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1675 | |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 1676 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1677 | _certificate_to_der(_sslmodulestate *state, X509 *certificate) |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 1678 | { |
| 1679 | unsigned char *bytes_buf = NULL; |
| 1680 | int len; |
| 1681 | PyObject *retval; |
| 1682 | |
| 1683 | bytes_buf = NULL; |
| 1684 | len = i2d_X509(certificate, &bytes_buf); |
| 1685 | if (len < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1686 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 1687 | return NULL; |
| 1688 | } |
| 1689 | /* this is actually an immutable bytes sequence */ |
| 1690 | retval = PyBytes_FromStringAndSize((const char *) bytes_buf, len); |
| 1691 | OPENSSL_free(bytes_buf); |
| 1692 | return retval; |
| 1693 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1694 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1695 | /*[clinic input] |
| 1696 | _ssl._test_decode_cert |
| 1697 | path: object(converter="PyUnicode_FSConverter") |
| 1698 | / |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1699 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1700 | [clinic start generated code]*/ |
| 1701 | |
| 1702 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1703 | _ssl__test_decode_cert_impl(PyObject *module, PyObject *path) |
| 1704 | /*[clinic end generated code: output=96becb9abb23c091 input=cdeaaf02d4346628]*/ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1705 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1706 | PyObject *retval = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1707 | X509 *x=NULL; |
| 1708 | BIO *cert; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1709 | _sslmodulestate *state = get_ssl_state(module); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1710 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1711 | if ((cert=BIO_new(BIO_s_file())) == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1712 | PyErr_SetString(state->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1713 | "Can't malloc memory to read file"); |
| 1714 | goto fail0; |
| 1715 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1716 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1717 | if (BIO_read_filename(cert, PyBytes_AsString(path)) <= 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1718 | PyErr_SetString(state->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1719 | "Can't open file"); |
| 1720 | goto fail0; |
| 1721 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1722 | |
Alex Gaynor | 40dad95 | 2019-08-15 08:31:28 -0400 | [diff] [blame] | 1723 | x = PEM_read_bio_X509(cert, NULL, NULL, NULL); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1724 | if (x == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1725 | PyErr_SetString(state->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1726 | "Error decoding PEM-encoded file"); |
| 1727 | goto fail0; |
| 1728 | } |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1729 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1730 | retval = _decode_certificate(state, x); |
Mark Dickinson | ee55df5 | 2010-08-03 18:31:54 +0000 | [diff] [blame] | 1731 | X509_free(x); |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1732 | |
| 1733 | fail0: |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1734 | Py_DECREF(path); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1735 | if (cert != NULL) BIO_free(cert); |
| 1736 | return retval; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1740 | /*[clinic input] |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1741 | _ssl._SSLSocket.getpeercert |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1742 | der as binary_mode: bool = False |
| 1743 | / |
| 1744 | |
| 1745 | Returns the certificate for the peer. |
| 1746 | |
| 1747 | If no certificate was provided, returns None. If a certificate was |
| 1748 | provided, but not validated, returns an empty dictionary. Otherwise |
| 1749 | returns a dict containing information about the peer certificate. |
| 1750 | |
| 1751 | If the optional argument is True, returns a DER-encoded copy of the |
| 1752 | peer certificate, or None if no certificate was provided. This will |
| 1753 | return the certificate even if it wasn't validated. |
| 1754 | [clinic start generated code]*/ |
| 1755 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1756 | static PyObject * |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 1757 | _ssl__SSLSocket_getpeercert_impl(PySSLSocket *self, int binary_mode) |
| 1758 | /*[clinic end generated code: output=1f0ab66dfb693c88 input=c0fbe802e57629b7]*/ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1759 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1760 | int verification; |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1761 | X509 *peer_cert; |
| 1762 | PyObject *result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1763 | |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1764 | if (!SSL_is_init_finished(self->ssl)) { |
Antoine Pitrou | 20b8555 | 2013-09-29 19:50:53 +0200 | [diff] [blame] | 1765 | PyErr_SetString(PyExc_ValueError, |
| 1766 | "handshake not done yet"); |
| 1767 | return NULL; |
| 1768 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1769 | peer_cert = SSL_get_peer_certificate(self->ssl); |
| 1770 | if (peer_cert == NULL) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1771 | Py_RETURN_NONE; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1772 | |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 1773 | if (binary_mode) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1774 | /* return cert in DER-encoded format */ |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1775 | result = _certificate_to_der(get_state_sock(self), peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1776 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 1777 | verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1778 | if ((verification & SSL_VERIFY_PEER) == 0) |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1779 | result = PyDict_New(); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1780 | else |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 1781 | result = _decode_certificate(get_state_sock(self), peer_cert); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1782 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 1783 | X509_free(peer_cert); |
| 1784 | return result; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1787 | static PyObject * |
| 1788 | cipher_to_tuple(const SSL_CIPHER *cipher) |
| 1789 | { |
| 1790 | const char *cipher_name, *cipher_protocol; |
| 1791 | PyObject *v, *retval = PyTuple_New(3); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1792 | if (retval == NULL) |
| 1793 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1794 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1795 | cipher_name = SSL_CIPHER_get_name(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1796 | if (cipher_name == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1797 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1798 | PyTuple_SET_ITEM(retval, 0, Py_None); |
| 1799 | } else { |
| 1800 | v = PyUnicode_FromString(cipher_name); |
| 1801 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1802 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1803 | PyTuple_SET_ITEM(retval, 0, v); |
| 1804 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1805 | |
| 1806 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1807 | if (cipher_protocol == NULL) { |
Hirokazu Yamamoto | 524f103 | 2010-12-09 10:49:00 +0000 | [diff] [blame] | 1808 | Py_INCREF(Py_None); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1809 | PyTuple_SET_ITEM(retval, 1, Py_None); |
| 1810 | } else { |
| 1811 | v = PyUnicode_FromString(cipher_protocol); |
| 1812 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1813 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1814 | PyTuple_SET_ITEM(retval, 1, v); |
| 1815 | } |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1816 | |
| 1817 | v = PyLong_FromLong(SSL_CIPHER_get_bits(cipher, NULL)); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1818 | if (v == NULL) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1819 | goto fail; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1820 | PyTuple_SET_ITEM(retval, 2, v); |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1821 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1822 | return retval; |
Guido van Rossum | f06628b | 2007-11-21 20:01:53 +0000 | [diff] [blame] | 1823 | |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1824 | fail: |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 1825 | Py_DECREF(retval); |
| 1826 | return NULL; |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1829 | static PyObject * |
| 1830 | cipher_to_dict(const SSL_CIPHER *cipher) |
| 1831 | { |
| 1832 | const char *cipher_name, *cipher_protocol; |
| 1833 | |
| 1834 | unsigned long cipher_id; |
| 1835 | int alg_bits, strength_bits, len; |
| 1836 | char buf[512] = {0}; |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1837 | int aead, nid; |
| 1838 | const char *skcipher = NULL, *digest = NULL, *kx = NULL, *auth = NULL; |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1839 | |
| 1840 | /* can be NULL */ |
| 1841 | cipher_name = SSL_CIPHER_get_name(cipher); |
| 1842 | cipher_protocol = SSL_CIPHER_get_version(cipher); |
| 1843 | cipher_id = SSL_CIPHER_get_id(cipher); |
| 1844 | SSL_CIPHER_description(cipher, buf, sizeof(buf) - 1); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 1845 | /* Downcast to avoid a warning. Safe since buf is always 512 bytes */ |
| 1846 | len = (int)strlen(buf); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1847 | if (len > 1 && buf[len-1] == '\n') |
| 1848 | buf[len-1] = '\0'; |
| 1849 | strength_bits = SSL_CIPHER_get_bits(cipher, &alg_bits); |
| 1850 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1851 | aead = SSL_CIPHER_is_aead(cipher); |
| 1852 | nid = SSL_CIPHER_get_cipher_nid(cipher); |
| 1853 | skcipher = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1854 | nid = SSL_CIPHER_get_digest_nid(cipher); |
| 1855 | digest = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1856 | nid = SSL_CIPHER_get_kx_nid(cipher); |
| 1857 | kx = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
| 1858 | nid = SSL_CIPHER_get_auth_nid(cipher); |
| 1859 | auth = nid != NID_undef ? OBJ_nid2ln(nid) : NULL; |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1860 | |
Victor Stinner | 410b988 | 2016-09-12 12:00:23 +0200 | [diff] [blame] | 1861 | return Py_BuildValue( |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1862 | "{sksssssssisi" |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1863 | "sOssssssss" |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1864 | "}", |
| 1865 | "id", cipher_id, |
| 1866 | "name", cipher_name, |
| 1867 | "protocol", cipher_protocol, |
| 1868 | "description", buf, |
| 1869 | "strength_bits", strength_bits, |
| 1870 | "alg_bits", alg_bits |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1871 | ,"aead", aead ? Py_True : Py_False, |
| 1872 | "symmetric", skcipher, |
| 1873 | "digest", digest, |
| 1874 | "kea", kx, |
| 1875 | "auth", auth |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1876 | ); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1877 | } |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 1878 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1879 | /*[clinic input] |
| 1880 | _ssl._SSLSocket.shared_ciphers |
| 1881 | [clinic start generated code]*/ |
| 1882 | |
| 1883 | static PyObject * |
| 1884 | _ssl__SSLSocket_shared_ciphers_impl(PySSLSocket *self) |
| 1885 | /*[clinic end generated code: output=3d174ead2e42c4fd input=0bfe149da8fe6306]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1886 | { |
| 1887 | STACK_OF(SSL_CIPHER) *ciphers; |
| 1888 | int i; |
| 1889 | PyObject *res; |
| 1890 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1891 | ciphers = SSL_get_ciphers(self->ssl); |
| 1892 | if (!ciphers) |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1893 | Py_RETURN_NONE; |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1894 | res = PyList_New(sk_SSL_CIPHER_num(ciphers)); |
| 1895 | if (!res) |
| 1896 | return NULL; |
| 1897 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 1898 | PyObject *tup = cipher_to_tuple(sk_SSL_CIPHER_value(ciphers, i)); |
| 1899 | if (!tup) { |
| 1900 | Py_DECREF(res); |
| 1901 | return NULL; |
| 1902 | } |
| 1903 | PyList_SET_ITEM(res, i, tup); |
| 1904 | } |
| 1905 | return res; |
| 1906 | } |
| 1907 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1908 | /*[clinic input] |
| 1909 | _ssl._SSLSocket.cipher |
| 1910 | [clinic start generated code]*/ |
| 1911 | |
| 1912 | static PyObject * |
| 1913 | _ssl__SSLSocket_cipher_impl(PySSLSocket *self) |
| 1914 | /*[clinic end generated code: output=376417c16d0e5815 input=548fb0e27243796d]*/ |
Benjamin Peterson | 4cb1781 | 2015-01-07 11:14:26 -0600 | [diff] [blame] | 1915 | { |
| 1916 | const SSL_CIPHER *current; |
| 1917 | |
| 1918 | if (self->ssl == NULL) |
| 1919 | Py_RETURN_NONE; |
| 1920 | current = SSL_get_current_cipher(self->ssl); |
| 1921 | if (current == NULL) |
| 1922 | Py_RETURN_NONE; |
| 1923 | return cipher_to_tuple(current); |
| 1924 | } |
| 1925 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1926 | /*[clinic input] |
| 1927 | _ssl._SSLSocket.version |
| 1928 | [clinic start generated code]*/ |
| 1929 | |
| 1930 | static PyObject * |
| 1931 | _ssl__SSLSocket_version_impl(PySSLSocket *self) |
| 1932 | /*[clinic end generated code: output=178aed33193b2cdb input=900186a503436fd6]*/ |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 1933 | { |
| 1934 | const char *version; |
| 1935 | |
| 1936 | if (self->ssl == NULL) |
| 1937 | Py_RETURN_NONE; |
Christian Heimes | 6877111 | 2017-09-05 21:55:40 -0700 | [diff] [blame] | 1938 | if (!SSL_is_init_finished(self->ssl)) { |
| 1939 | /* handshake not finished */ |
| 1940 | Py_RETURN_NONE; |
| 1941 | } |
Antoine Pitrou | 47e4042 | 2014-09-04 21:00:10 +0200 | [diff] [blame] | 1942 | version = SSL_get_version(self->ssl); |
| 1943 | if (!strcmp(version, "unknown")) |
| 1944 | Py_RETURN_NONE; |
| 1945 | return PyUnicode_FromString(version); |
| 1946 | } |
| 1947 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1948 | /*[clinic input] |
| 1949 | _ssl._SSLSocket.selected_alpn_protocol |
| 1950 | [clinic start generated code]*/ |
| 1951 | |
| 1952 | static PyObject * |
| 1953 | _ssl__SSLSocket_selected_alpn_protocol_impl(PySSLSocket *self) |
| 1954 | /*[clinic end generated code: output=ec33688b303d250f input=442de30e35bc2913]*/ |
| 1955 | { |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 1956 | const unsigned char *out; |
| 1957 | unsigned int outlen; |
| 1958 | |
| 1959 | SSL_get0_alpn_selected(self->ssl, &out, &outlen); |
| 1960 | |
| 1961 | if (out == NULL) |
| 1962 | Py_RETURN_NONE; |
| 1963 | return PyUnicode_FromStringAndSize((char *)out, outlen); |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 1964 | } |
Antoine Pitrou | d5d17eb | 2012-03-22 00:23:03 +0100 | [diff] [blame] | 1965 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 1966 | /*[clinic input] |
| 1967 | _ssl._SSLSocket.compression |
| 1968 | [clinic start generated code]*/ |
| 1969 | |
| 1970 | static PyObject * |
| 1971 | _ssl__SSLSocket_compression_impl(PySSLSocket *self) |
| 1972 | /*[clinic end generated code: output=bd16cb1bb4646ae7 input=5d059d0a2bbc32c8]*/ |
| 1973 | { |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 1974 | #ifdef OPENSSL_NO_COMP |
| 1975 | Py_RETURN_NONE; |
| 1976 | #else |
| 1977 | const COMP_METHOD *comp_method; |
| 1978 | const char *short_name; |
| 1979 | |
| 1980 | if (self->ssl == NULL) |
| 1981 | Py_RETURN_NONE; |
| 1982 | comp_method = SSL_get_current_compression(self->ssl); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 1983 | if (comp_method == NULL || COMP_get_type(comp_method) == NID_undef) |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 1984 | Py_RETURN_NONE; |
Christian Heimes | 281e5f8 | 2016-09-06 01:10:39 +0200 | [diff] [blame] | 1985 | short_name = OBJ_nid2sn(COMP_get_type(comp_method)); |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 1986 | if (short_name == NULL) |
| 1987 | Py_RETURN_NONE; |
| 1988 | return PyUnicode_DecodeFSDefault(short_name); |
| 1989 | #endif |
| 1990 | } |
| 1991 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 1992 | static PySSLContext *PySSL_get_context(PySSLSocket *self, void *closure) { |
| 1993 | Py_INCREF(self->ctx); |
| 1994 | return self->ctx; |
| 1995 | } |
| 1996 | |
| 1997 | static int PySSL_set_context(PySSLSocket *self, PyObject *value, |
| 1998 | void *closure) { |
| 1999 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2000 | if (PyObject_TypeCheck(value, self->ctx->state->PySSLContext_Type)) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2001 | Py_INCREF(value); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 2002 | Py_SETREF(self->ctx, (PySSLContext *)value); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2003 | SSL_set_SSL_CTX(self->ssl, self->ctx->ctx); |
Christian Heimes | 77cde50 | 2021-03-21 16:13:09 +0100 | [diff] [blame] | 2004 | /* Set SSL* internal msg_callback to state of new context's state */ |
| 2005 | SSL_set_msg_callback( |
| 2006 | self->ssl, |
| 2007 | self->ctx->msg_cb ? _PySSL_msg_callback : NULL |
| 2008 | ); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2009 | } else { |
Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2010 | PyErr_SetString(PyExc_TypeError, "The value must be a SSLContext"); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2011 | return -1; |
| 2012 | } |
| 2013 | |
| 2014 | return 0; |
| 2015 | } |
| 2016 | |
| 2017 | PyDoc_STRVAR(PySSL_set_context_doc, |
| 2018 | "_setter_context(ctx)\n\ |
| 2019 | \ |
| 2020 | This changes the context associated with the SSLSocket. This is typically\n\ |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 2021 | used from within a callback function set by the sni_callback\n\ |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2022 | on the SSLContext to change the certificate information associated with the\n\ |
| 2023 | SSLSocket before the cryptographic exchange handshake messages\n"); |
| 2024 | |
| 2025 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2026 | static PyObject * |
| 2027 | PySSL_get_server_side(PySSLSocket *self, void *c) |
| 2028 | { |
| 2029 | return PyBool_FromLong(self->socket_type == PY_SSL_SERVER); |
| 2030 | } |
| 2031 | |
| 2032 | PyDoc_STRVAR(PySSL_get_server_side_doc, |
| 2033 | "Whether this is a server-side socket."); |
| 2034 | |
| 2035 | static PyObject * |
| 2036 | PySSL_get_server_hostname(PySSLSocket *self, void *c) |
| 2037 | { |
| 2038 | if (self->server_hostname == NULL) |
| 2039 | Py_RETURN_NONE; |
| 2040 | Py_INCREF(self->server_hostname); |
| 2041 | return self->server_hostname; |
| 2042 | } |
| 2043 | |
| 2044 | PyDoc_STRVAR(PySSL_get_server_hostname_doc, |
| 2045 | "The currently set server hostname (for SNI)."); |
| 2046 | |
| 2047 | static PyObject * |
| 2048 | PySSL_get_owner(PySSLSocket *self, void *c) |
| 2049 | { |
| 2050 | PyObject *owner; |
| 2051 | |
| 2052 | if (self->owner == NULL) |
| 2053 | Py_RETURN_NONE; |
| 2054 | |
| 2055 | owner = PyWeakref_GetObject(self->owner); |
| 2056 | Py_INCREF(owner); |
| 2057 | return owner; |
| 2058 | } |
| 2059 | |
| 2060 | static int |
| 2061 | PySSL_set_owner(PySSLSocket *self, PyObject *value, void *c) |
| 2062 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2063 | Py_XSETREF(self->owner, PyWeakref_NewRef(value, NULL)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2064 | if (self->owner == NULL) |
| 2065 | return -1; |
| 2066 | return 0; |
| 2067 | } |
| 2068 | |
| 2069 | PyDoc_STRVAR(PySSL_get_owner_doc, |
| 2070 | "The Python-level owner of this object.\ |
| 2071 | Passed as \"self\" in servername callback."); |
| 2072 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2073 | static int |
| 2074 | PySSL_traverse(PySSLSocket *self, visitproc visit, void *arg) |
| 2075 | { |
| 2076 | Py_VISIT(self->exc_type); |
| 2077 | Py_VISIT(self->exc_value); |
| 2078 | Py_VISIT(self->exc_tb); |
| 2079 | return 0; |
| 2080 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2081 | |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2082 | static int |
| 2083 | PySSL_clear(PySSLSocket *self) |
| 2084 | { |
| 2085 | Py_CLEAR(self->exc_type); |
| 2086 | Py_CLEAR(self->exc_value); |
| 2087 | Py_CLEAR(self->exc_tb); |
| 2088 | return 0; |
| 2089 | } |
| 2090 | |
| 2091 | static void |
| 2092 | PySSL_dealloc(PySSLSocket *self) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2093 | { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 2094 | PyTypeObject *tp = Py_TYPE(self); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2095 | if (self->ssl) |
| 2096 | SSL_free(self->ssl); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2097 | Py_XDECREF(self->Socket); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2098 | Py_XDECREF(self->ctx); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2099 | Py_XDECREF(self->server_hostname); |
| 2100 | Py_XDECREF(self->owner); |
Victor Stinner | 32bd68c | 2020-12-01 10:37:39 +0100 | [diff] [blame] | 2101 | PyObject_Free(self); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 2102 | Py_DECREF(tp); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2105 | /* 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] | 2106 | The argument writing indicates the direction. |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2107 | Returns one of the possibilities in the timeout_state enum (above). |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2108 | */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 2109 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2110 | static int |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2111 | PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout) |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2112 | { |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2113 | int rc; |
| 2114 | #ifdef HAVE_POLL |
| 2115 | struct pollfd pollfd; |
| 2116 | _PyTime_t ms; |
| 2117 | #else |
| 2118 | int nfds; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2119 | fd_set fds; |
| 2120 | struct timeval tv; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2121 | #endif |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2122 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2123 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2124 | if ((s == NULL) || (timeout == 0)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2125 | return SOCKET_IS_NONBLOCKING; |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2126 | else if (timeout < 0) { |
| 2127 | if (s->sock_timeout > 0) |
| 2128 | return SOCKET_HAS_TIMED_OUT; |
| 2129 | else |
| 2130 | return SOCKET_IS_BLOCKING; |
| 2131 | } |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2132 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2133 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2134 | if (s->sock_fd == INVALID_SOCKET) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2135 | return SOCKET_HAS_BEEN_CLOSED; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2136 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2137 | /* Prefer poll, if available, since you can poll() any fd |
| 2138 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2139 | #ifdef HAVE_POLL |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2140 | pollfd.fd = s->sock_fd; |
| 2141 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2142 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2143 | /* timeout is in seconds, poll() uses milliseconds */ |
| 2144 | ms = (int)_PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2145 | assert(ms <= INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2146 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2147 | PySSL_BEGIN_ALLOW_THREADS |
| 2148 | rc = poll(&pollfd, 1, (int)ms); |
| 2149 | PySSL_END_ALLOW_THREADS |
| 2150 | #else |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2151 | /* Guard against socket too large for select*/ |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 2152 | if (!_PyIsSelectable_fd(s->sock_fd)) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2153 | return SOCKET_TOO_LARGE_FOR_SELECT; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2154 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2155 | _PyTime_AsTimeval_noraise(timeout, &tv, _PyTime_ROUND_CEILING); |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2156 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2157 | FD_ZERO(&fds); |
| 2158 | FD_SET(s->sock_fd, &fds); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2159 | |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2160 | /* Wait until the socket becomes ready */ |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2161 | PySSL_BEGIN_ALLOW_THREADS |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2162 | nfds = Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2163 | if (writing) |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2164 | rc = select(nfds, NULL, &fds, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2165 | else |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2166 | rc = select(nfds, &fds, NULL, NULL, &tv); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2167 | PySSL_END_ALLOW_THREADS |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2168 | #endif |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2169 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2170 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
| 2171 | (when we are able to write or when there's something to read) */ |
| 2172 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2175 | /*[clinic input] |
| 2176 | _ssl._SSLSocket.write |
| 2177 | b: Py_buffer |
| 2178 | / |
| 2179 | |
| 2180 | Writes the bytes-like object b into the SSL object. |
| 2181 | |
| 2182 | Returns the number of bytes written. |
| 2183 | [clinic start generated code]*/ |
| 2184 | |
| 2185 | static PyObject * |
| 2186 | _ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b) |
| 2187 | /*[clinic end generated code: output=aa7a6be5527358d8 input=77262d994fe5100a]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2188 | { |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame^] | 2189 | size_t count = 0; |
| 2190 | int retval; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2191 | int sockstate; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2192 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2193 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2194 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2195 | _PyTime_t timeout, deadline = 0; |
| 2196 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2197 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2198 | if (sock != NULL) { |
| 2199 | if (((PyObject*)sock) == Py_None) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2200 | _setSSLError(get_state_sock(self), |
| 2201 | "Underlying socket connection gone", |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2202 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2203 | return NULL; |
| 2204 | } |
| 2205 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2208 | if (sock != NULL) { |
| 2209 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2210 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2211 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2212 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2213 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2214 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2215 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2216 | has_timeout = (timeout > 0); |
| 2217 | if (has_timeout) |
| 2218 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2219 | |
| 2220 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2221 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2222 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2223 | "The write operation timed out"); |
| 2224 | goto error; |
| 2225 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2226 | PyErr_SetString(get_state_sock(self)->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2227 | "Underlying socket has been closed."); |
| 2228 | goto error; |
| 2229 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2230 | PyErr_SetString(get_state_sock(self)->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2231 | "Underlying socket too large for select()."); |
| 2232 | goto error; |
| 2233 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2234 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2235 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2236 | PySSL_BEGIN_ALLOW_THREADS |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame^] | 2237 | retval = SSL_write_ex(self->ssl, b->buf, (int)b->len, &count); |
| 2238 | err = _PySSL_errno(retval == 0, self->ssl, retval); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2239 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2240 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2241 | |
| 2242 | if (PyErr_CheckSignals()) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2243 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2244 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2245 | if (has_timeout) |
| 2246 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2247 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2248 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2249 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2250 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2251 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2252 | } else { |
| 2253 | sockstate = SOCKET_OPERATION_OK; |
| 2254 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2255 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2256 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2257 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2258 | "The write operation timed out"); |
| 2259 | goto error; |
| 2260 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2261 | PyErr_SetString(get_state_sock(self)->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2262 | "Underlying socket has been closed."); |
| 2263 | goto error; |
| 2264 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2265 | break; |
| 2266 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2267 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2268 | err.ssl == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2269 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2270 | Py_XDECREF(sock); |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame^] | 2271 | if (retval == 0) |
| 2272 | return PySSL_SetError(self, retval, __FILE__, __LINE__); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2273 | if (PySSL_ChainExceptions(self) < 0) |
| 2274 | return NULL; |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame^] | 2275 | return PyLong_FromSize_t(count); |
Antoine Pitrou | 7d7aede | 2009-11-25 18:55:32 +0000 | [diff] [blame] | 2276 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2277 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2278 | PySSL_ChainExceptions(self); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2279 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2282 | /*[clinic input] |
| 2283 | _ssl._SSLSocket.pending |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2284 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2285 | Returns the number of already decrypted bytes available for read, pending on the connection. |
| 2286 | [clinic start generated code]*/ |
| 2287 | |
| 2288 | static PyObject * |
| 2289 | _ssl__SSLSocket_pending_impl(PySSLSocket *self) |
| 2290 | /*[clinic end generated code: output=983d9fecdc308a83 input=2b77487d6dfd597f]*/ |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2291 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2292 | int count = 0; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2293 | _PySSLError err; |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2294 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2295 | PySSL_BEGIN_ALLOW_THREADS |
| 2296 | count = SSL_pending(self->ssl); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2297 | err = _PySSL_errno(count < 0, self->ssl, count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2298 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2299 | self->err = err; |
| 2300 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2301 | if (count < 0) |
| 2302 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
| 2303 | else |
| 2304 | return PyLong_FromLong(count); |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2307 | /*[clinic input] |
| 2308 | _ssl._SSLSocket.read |
| 2309 | size as len: int |
| 2310 | [ |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2311 | buffer: Py_buffer(accept={rwbuffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2312 | ] |
| 2313 | / |
Bill Janssen | 6e027db | 2007-11-15 22:23:56 +0000 | [diff] [blame] | 2314 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2315 | Read up to size bytes from the SSL socket. |
| 2316 | [clinic start generated code]*/ |
| 2317 | |
| 2318 | static PyObject * |
| 2319 | _ssl__SSLSocket_read_impl(PySSLSocket *self, int len, int group_right_1, |
| 2320 | Py_buffer *buffer) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 2321 | /*[clinic end generated code: output=00097776cec2a0af input=ff157eb918d0905b]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2322 | { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2323 | PyObject *dest = NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2324 | char *mem; |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame^] | 2325 | size_t count = 0; |
| 2326 | int retval; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2327 | int sockstate; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2328 | _PySSLError err; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2329 | int nonblocking; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2330 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2331 | _PyTime_t timeout, deadline = 0; |
| 2332 | int has_timeout; |
Bill Janssen | 54cc54c | 2007-12-14 22:08:56 +0000 | [diff] [blame] | 2333 | |
Martin Panter | 5503d47 | 2016-03-27 05:35:19 +0000 | [diff] [blame] | 2334 | if (!group_right_1 && len < 0) { |
| 2335 | PyErr_SetString(PyExc_ValueError, "size should not be negative"); |
| 2336 | return NULL; |
| 2337 | } |
| 2338 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2339 | if (sock != NULL) { |
| 2340 | if (((PyObject*)sock) == Py_None) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2341 | _setSSLError(get_state_sock(self), |
| 2342 | "Underlying socket connection gone", |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2343 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2344 | return NULL; |
| 2345 | } |
| 2346 | Py_INCREF(sock); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2349 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2350 | dest = PyBytes_FromStringAndSize(NULL, len); |
| 2351 | if (dest == NULL) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2352 | goto error; |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2353 | if (len == 0) { |
| 2354 | Py_XDECREF(sock); |
| 2355 | return dest; |
| 2356 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2357 | mem = PyBytes_AS_STRING(dest); |
| 2358 | } |
| 2359 | else { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2360 | mem = buffer->buf; |
| 2361 | if (len <= 0 || len > buffer->len) { |
| 2362 | len = (int) buffer->len; |
| 2363 | if (buffer->len != len) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2364 | PyErr_SetString(PyExc_OverflowError, |
| 2365 | "maximum length can't fit in a C 'int'"); |
| 2366 | goto error; |
| 2367 | } |
Martin Panter | bed7f1a | 2016-07-11 00:17:13 +0000 | [diff] [blame] | 2368 | if (len == 0) { |
| 2369 | count = 0; |
| 2370 | goto done; |
| 2371 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2372 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2375 | if (sock != NULL) { |
| 2376 | /* just in case the blocking state of the socket has been changed */ |
Victor Stinner | e245231 | 2015-03-28 03:00:46 +0100 | [diff] [blame] | 2377 | nonblocking = (sock->sock_timeout >= 0); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2378 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 2379 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 2380 | } |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2381 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2382 | timeout = GET_SOCKET_TIMEOUT(sock); |
| 2383 | has_timeout = (timeout > 0); |
| 2384 | if (has_timeout) |
| 2385 | deadline = _PyTime_GetMonotonicClock() + timeout; |
| 2386 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2387 | do { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2388 | PySSL_BEGIN_ALLOW_THREADS |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame^] | 2389 | retval = SSL_read_ex(self->ssl, mem, len, &count); |
| 2390 | err = _PySSL_errno(retval == 0, self->ssl, retval); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2391 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2392 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2393 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2394 | if (PyErr_CheckSignals()) |
| 2395 | goto error; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2396 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2397 | if (has_timeout) |
| 2398 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2399 | |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2400 | if (err.ssl == SSL_ERROR_WANT_READ) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2401 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2402 | } else if (err.ssl == SSL_ERROR_WANT_WRITE) { |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2403 | sockstate = PySSL_select(sock, 1, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2404 | } else if (err.ssl == SSL_ERROR_ZERO_RETURN && |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2405 | SSL_get_shutdown(self->ssl) == SSL_RECEIVED_SHUTDOWN) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2406 | { |
| 2407 | count = 0; |
| 2408 | goto done; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2409 | } |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2410 | else |
| 2411 | sockstate = SOCKET_OPERATION_OK; |
| 2412 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2413 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2414 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2415 | "The read operation timed out"); |
| 2416 | goto error; |
| 2417 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 2418 | break; |
| 2419 | } |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2420 | } while (err.ssl == SSL_ERROR_WANT_READ || |
| 2421 | err.ssl == SSL_ERROR_WANT_WRITE); |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2422 | |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame^] | 2423 | if (retval == 0) { |
| 2424 | PySSL_SetError(self, retval, __FILE__, __LINE__); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2425 | goto error; |
| 2426 | } |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2427 | if (self->exc_type != NULL) |
| 2428 | goto error; |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2429 | |
| 2430 | done: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2431 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2432 | if (!group_right_1) { |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2433 | _PyBytes_Resize(&dest, count); |
| 2434 | return dest; |
| 2435 | } |
| 2436 | else { |
Christian Heimes | 89d1550 | 2021-04-19 06:55:30 +0200 | [diff] [blame^] | 2437 | return PyLong_FromSize_t(count); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2438 | } |
Antoine Pitrou | 24e561a | 2010-09-03 18:38:17 +0000 | [diff] [blame] | 2439 | |
| 2440 | error: |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2441 | PySSL_ChainExceptions(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2442 | Py_XDECREF(sock); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2443 | if (!group_right_1) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2444 | Py_XDECREF(dest); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2445 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2448 | /*[clinic input] |
| 2449 | _ssl._SSLSocket.shutdown |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2450 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2451 | Does the SSL shutdown handshake with the remote end. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2452 | [clinic start generated code]*/ |
| 2453 | |
| 2454 | static PyObject * |
| 2455 | _ssl__SSLSocket_shutdown_impl(PySSLSocket *self) |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2456 | /*[clinic end generated code: output=ca1aa7ed9d25ca42 input=11d39e69b0a2bf4a]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2457 | { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2458 | _PySSLError err; |
| 2459 | int sockstate, nonblocking, ret; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2460 | int zeros = 0; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2461 | PySocketSockObject *sock = GET_SOCKET(self); |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2462 | _PyTime_t timeout, deadline = 0; |
| 2463 | int has_timeout; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2464 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2465 | if (sock != NULL) { |
| 2466 | /* Guard against closed socket */ |
Victor Stinner | 524714e | 2016-07-22 17:43:59 +0200 | [diff] [blame] | 2467 | if ((((PyObject*)sock) == Py_None) || (sock->sock_fd == INVALID_SOCKET)) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2468 | _setSSLError(get_state_sock(self), |
| 2469 | "Underlying socket connection gone", |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2470 | PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 2471 | return NULL; |
| 2472 | } |
| 2473 | Py_INCREF(sock); |
| 2474 | |
| 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); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 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 | while (1) { |
| 2487 | PySSL_BEGIN_ALLOW_THREADS |
| 2488 | /* Disable read-ahead so that unwrap can work correctly. |
| 2489 | * Otherwise OpenSSL might read in too much data, |
| 2490 | * eating clear text data that happens to be |
| 2491 | * transmitted after the SSL shutdown. |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 2492 | * Should be safe to call repeatedly every time this |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2493 | * function is used and the shutdown_seen_zero != 0 |
| 2494 | * condition is met. |
| 2495 | */ |
| 2496 | if (self->shutdown_seen_zero) |
| 2497 | SSL_set_read_ahead(self->ssl, 0); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2498 | ret = SSL_shutdown(self->ssl); |
| 2499 | err = _PySSL_errno(ret < 0, self->ssl, ret); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2500 | PySSL_END_ALLOW_THREADS |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2501 | self->err = err; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2502 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2503 | /* If err == 1, a secure shutdown with SSL_shutdown() is complete */ |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2504 | if (ret > 0) |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2505 | break; |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2506 | if (ret == 0) { |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2507 | /* Don't loop endlessly; instead preserve legacy |
| 2508 | behaviour of trying SSL_shutdown() only twice. |
| 2509 | This looks necessary for OpenSSL < 0.9.8m */ |
| 2510 | if (++zeros > 1) |
| 2511 | break; |
| 2512 | /* Shutdown was sent, now try receiving */ |
| 2513 | self->shutdown_seen_zero = 1; |
| 2514 | continue; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2517 | if (has_timeout) |
| 2518 | timeout = deadline - _PyTime_GetMonotonicClock(); |
| 2519 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2520 | /* Possibly retry shutdown until timeout or failure */ |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2521 | if (err.ssl == SSL_ERROR_WANT_READ) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2522 | sockstate = PySSL_select(sock, 0, timeout); |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2523 | else if (err.ssl == SSL_ERROR_WANT_WRITE) |
Victor Stinner | 1469070 | 2015-04-06 22:46:13 +0200 | [diff] [blame] | 2524 | sockstate = PySSL_select(sock, 1, timeout); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2525 | else |
| 2526 | break; |
Victor Stinner | 4e3cfa4 | 2015-04-02 21:28:28 +0200 | [diff] [blame] | 2527 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2528 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Steve Dower | c6fd1c1 | 2018-09-17 11:34:47 -0700 | [diff] [blame] | 2529 | if (err.ssl == SSL_ERROR_WANT_READ) |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2530 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2531 | "The read operation timed out"); |
| 2532 | else |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 2533 | PyErr_SetString(PyExc_TimeoutError, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2534 | "The write operation timed out"); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2535 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2536 | } |
| 2537 | else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2538 | PyErr_SetString(get_state_sock(self)->PySSLErrorObject, |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2539 | "Underlying socket too large for select()."); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2540 | goto error; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2541 | } |
| 2542 | else if (sockstate != SOCKET_OPERATION_OK) |
| 2543 | /* Retain the SSL error code */ |
| 2544 | break; |
| 2545 | } |
Nathaniel J. Smith | c0da582 | 2018-09-21 21:44:12 -0700 | [diff] [blame] | 2546 | if (ret < 0) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2547 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2548 | PySSL_SetError(self, ret, __FILE__, __LINE__); |
| 2549 | return NULL; |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2550 | } |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2551 | if (self->exc_type != NULL) |
| 2552 | goto error; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2553 | if (sock) |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2554 | /* It's already INCREF'ed */ |
| 2555 | return (PyObject *) sock; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2556 | else |
| 2557 | Py_RETURN_NONE; |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2558 | |
| 2559 | error: |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2560 | Py_XDECREF(sock); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2561 | PySSL_ChainExceptions(self); |
Antoine Pitrou | 8bae4ec | 2010-06-24 22:34:04 +0000 | [diff] [blame] | 2562 | return NULL; |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2565 | /*[clinic input] |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2566 | _ssl._SSLSocket.get_channel_binding |
| 2567 | cb_type: str = "tls-unique" |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2568 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2569 | Get channel binding data for current connection. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2570 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2571 | Raise ValueError if the requested `cb_type` is not supported. Return bytes |
| 2572 | of the data or None if the data is not available (e.g. before the handshake). |
| 2573 | Only 'tls-unique' channel binding data from RFC 5929 is supported. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2574 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 2575 | |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2576 | static PyObject * |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2577 | _ssl__SSLSocket_get_channel_binding_impl(PySSLSocket *self, |
| 2578 | const char *cb_type) |
| 2579 | /*[clinic end generated code: output=34bac9acb6a61d31 input=08b7e43b99c17d41]*/ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2580 | { |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2581 | char buf[PySSL_CB_MAXLEN]; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 2582 | size_t len; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2583 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2584 | if (strcmp(cb_type, "tls-unique") == 0) { |
| 2585 | if (SSL_session_reused(self->ssl) ^ !self->socket_type) { |
| 2586 | /* if session is resumed XOR we are the client */ |
| 2587 | len = SSL_get_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2588 | } |
| 2589 | else { |
| 2590 | /* if a new session XOR we are the server */ |
| 2591 | len = SSL_get_peer_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2592 | } |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2593 | } |
| 2594 | else { |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2595 | PyErr_Format( |
| 2596 | PyExc_ValueError, |
| 2597 | "'%s' channel binding type not implemented", |
| 2598 | cb_type |
| 2599 | ); |
| 2600 | return NULL; |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | /* It cannot be negative in current OpenSSL version as of July 2011 */ |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2604 | if (len == 0) |
| 2605 | Py_RETURN_NONE; |
| 2606 | |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2607 | return PyBytes_FromStringAndSize(buf, len); |
Antoine Pitrou | d649480 | 2011-07-21 01:11:30 +0200 | [diff] [blame] | 2608 | } |
| 2609 | |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2610 | /*[clinic input] |
| 2611 | _ssl._SSLSocket.verify_client_post_handshake |
| 2612 | |
| 2613 | Initiate TLS 1.3 post-handshake authentication |
| 2614 | [clinic start generated code]*/ |
| 2615 | |
| 2616 | static PyObject * |
| 2617 | _ssl__SSLSocket_verify_client_post_handshake_impl(PySSLSocket *self) |
| 2618 | /*[clinic end generated code: output=532147f3b1341425 input=6bfa874810a3d889]*/ |
| 2619 | { |
| 2620 | #ifdef TLS1_3_VERSION |
| 2621 | int err = SSL_verify_client_post_handshake(self->ssl); |
| 2622 | if (err == 0) |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2623 | return _setSSLError(get_state_sock(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2624 | else |
| 2625 | Py_RETURN_NONE; |
| 2626 | #else |
| 2627 | PyErr_SetString(PyExc_NotImplementedError, |
| 2628 | "Post-handshake auth is not supported by your " |
| 2629 | "OpenSSL version."); |
| 2630 | return NULL; |
| 2631 | #endif |
| 2632 | } |
| 2633 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2634 | static SSL_SESSION* |
| 2635 | _ssl_session_dup(SSL_SESSION *session) { |
| 2636 | SSL_SESSION *newsession = NULL; |
| 2637 | int slen; |
| 2638 | unsigned char *senc = NULL, *p; |
| 2639 | const unsigned char *const_p; |
| 2640 | |
| 2641 | if (session == NULL) { |
| 2642 | PyErr_SetString(PyExc_ValueError, "Invalid session"); |
| 2643 | goto error; |
| 2644 | } |
| 2645 | |
| 2646 | /* get length */ |
| 2647 | slen = i2d_SSL_SESSION(session, NULL); |
| 2648 | if (slen == 0 || slen > 0xFF00) { |
| 2649 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2650 | goto error; |
| 2651 | } |
| 2652 | if ((senc = PyMem_Malloc(slen)) == NULL) { |
| 2653 | PyErr_NoMemory(); |
| 2654 | goto error; |
| 2655 | } |
| 2656 | p = senc; |
| 2657 | if (!i2d_SSL_SESSION(session, &p)) { |
| 2658 | PyErr_SetString(PyExc_ValueError, "i2d() failed."); |
| 2659 | goto error; |
| 2660 | } |
| 2661 | const_p = senc; |
| 2662 | newsession = d2i_SSL_SESSION(NULL, &const_p, slen); |
| 2663 | if (session == NULL) { |
| 2664 | goto error; |
| 2665 | } |
| 2666 | PyMem_Free(senc); |
| 2667 | return newsession; |
| 2668 | error: |
| 2669 | if (senc != NULL) { |
| 2670 | PyMem_Free(senc); |
| 2671 | } |
| 2672 | return NULL; |
| 2673 | } |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2674 | |
| 2675 | static PyObject * |
| 2676 | PySSL_get_session(PySSLSocket *self, void *closure) { |
| 2677 | /* get_session can return sessions from a server-side connection, |
| 2678 | * it does not check for handshake done or client socket. */ |
| 2679 | PySSLSession *pysess; |
| 2680 | SSL_SESSION *session; |
| 2681 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2682 | /* duplicate session as workaround for session bug in OpenSSL 1.1.0, |
| 2683 | * https://github.com/openssl/openssl/issues/1550 */ |
| 2684 | session = SSL_get0_session(self->ssl); /* borrowed reference */ |
| 2685 | if (session == NULL) { |
| 2686 | Py_RETURN_NONE; |
| 2687 | } |
| 2688 | if ((session = _ssl_session_dup(session)) == NULL) { |
| 2689 | return NULL; |
| 2690 | } |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2691 | session = SSL_get1_session(self->ssl); |
| 2692 | if (session == NULL) { |
| 2693 | Py_RETURN_NONE; |
| 2694 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2695 | pysess = PyObject_GC_New(PySSLSession, self->ctx->state->PySSLSession_Type); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2696 | if (pysess == NULL) { |
| 2697 | SSL_SESSION_free(session); |
| 2698 | return NULL; |
| 2699 | } |
| 2700 | |
| 2701 | assert(self->ctx); |
| 2702 | pysess->ctx = self->ctx; |
| 2703 | Py_INCREF(pysess->ctx); |
| 2704 | pysess->session = session; |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 2705 | PyObject_GC_Track(pysess); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2706 | return (PyObject *)pysess; |
| 2707 | } |
| 2708 | |
| 2709 | static int PySSL_set_session(PySSLSocket *self, PyObject *value, |
| 2710 | void *closure) |
| 2711 | { |
| 2712 | PySSLSession *pysess; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2713 | SSL_SESSION *session; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2714 | int result; |
| 2715 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2716 | if (!Py_IS_TYPE(value, get_state_sock(self)->PySSLSession_Type)) { |
Ned Deily | 4531ec7 | 2018-06-11 20:26:28 -0400 | [diff] [blame] | 2717 | PyErr_SetString(PyExc_TypeError, "Value is not a SSLSession."); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2718 | return -1; |
| 2719 | } |
| 2720 | pysess = (PySSLSession *)value; |
| 2721 | |
| 2722 | if (self->ctx->ctx != pysess->ctx->ctx) { |
| 2723 | PyErr_SetString(PyExc_ValueError, |
| 2724 | "Session refers to a different SSLContext."); |
| 2725 | return -1; |
| 2726 | } |
| 2727 | if (self->socket_type != PY_SSL_CLIENT) { |
| 2728 | PyErr_SetString(PyExc_ValueError, |
| 2729 | "Cannot set session for server-side SSLSocket."); |
| 2730 | return -1; |
| 2731 | } |
Christian Heimes | 66dc33b | 2017-05-23 16:02:02 -0700 | [diff] [blame] | 2732 | if (SSL_is_init_finished(self->ssl)) { |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2733 | PyErr_SetString(PyExc_ValueError, |
| 2734 | "Cannot set session after handshake."); |
| 2735 | return -1; |
| 2736 | } |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2737 | /* duplicate session */ |
| 2738 | if ((session = _ssl_session_dup(pysess->session)) == NULL) { |
| 2739 | return -1; |
| 2740 | } |
| 2741 | result = SSL_set_session(self->ssl, session); |
| 2742 | /* free duplicate, SSL_set_session() bumps ref count */ |
| 2743 | SSL_SESSION_free(session); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2744 | if (result == 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2745 | _setSSLError(get_state_sock(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2746 | return -1; |
| 2747 | } |
| 2748 | return 0; |
| 2749 | } |
| 2750 | |
| 2751 | PyDoc_STRVAR(PySSL_set_session_doc, |
| 2752 | "_setter_session(session)\n\ |
| 2753 | \ |
| 2754 | Get / set SSLSession."); |
| 2755 | |
| 2756 | static PyObject * |
| 2757 | PySSL_get_session_reused(PySSLSocket *self, void *closure) { |
| 2758 | if (SSL_session_reused(self->ssl)) { |
| 2759 | Py_RETURN_TRUE; |
| 2760 | } else { |
| 2761 | Py_RETURN_FALSE; |
| 2762 | } |
| 2763 | } |
| 2764 | |
| 2765 | PyDoc_STRVAR(PySSL_get_session_reused_doc, |
| 2766 | "Was the client session reused during handshake?"); |
| 2767 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2768 | static PyGetSetDef ssl_getsetlist[] = { |
| 2769 | {"context", (getter) PySSL_get_context, |
| 2770 | (setter) PySSL_set_context, PySSL_set_context_doc}, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 2771 | {"server_side", (getter) PySSL_get_server_side, NULL, |
| 2772 | PySSL_get_server_side_doc}, |
| 2773 | {"server_hostname", (getter) PySSL_get_server_hostname, NULL, |
| 2774 | PySSL_get_server_hostname_doc}, |
| 2775 | {"owner", (getter) PySSL_get_owner, (setter) PySSL_set_owner, |
| 2776 | PySSL_get_owner_doc}, |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 2777 | {"session", (getter) PySSL_get_session, |
| 2778 | (setter) PySSL_set_session, PySSL_set_session_doc}, |
| 2779 | {"session_reused", (getter) PySSL_get_session_reused, NULL, |
| 2780 | PySSL_get_session_reused_doc}, |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 2781 | {NULL}, /* sentinel */ |
| 2782 | }; |
| 2783 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2784 | static PyMethodDef PySSLMethods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2785 | _SSL__SSLSOCKET_DO_HANDSHAKE_METHODDEF |
| 2786 | _SSL__SSLSOCKET_WRITE_METHODDEF |
| 2787 | _SSL__SSLSOCKET_READ_METHODDEF |
| 2788 | _SSL__SSLSOCKET_PENDING_METHODDEF |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 2789 | _SSL__SSLSOCKET_GETPEERCERT_METHODDEF |
| 2790 | _SSL__SSLSOCKET_GET_CHANNEL_BINDING_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2791 | _SSL__SSLSOCKET_CIPHER_METHODDEF |
| 2792 | _SSL__SSLSOCKET_SHARED_CIPHERS_METHODDEF |
| 2793 | _SSL__SSLSOCKET_VERSION_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2794 | _SSL__SSLSOCKET_SELECTED_ALPN_PROTOCOL_METHODDEF |
| 2795 | _SSL__SSLSOCKET_COMPRESSION_METHODDEF |
| 2796 | _SSL__SSLSOCKET_SHUTDOWN_METHODDEF |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2797 | _SSL__SSLSOCKET_VERIFY_CLIENT_POST_HANDSHAKE_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 2798 | {NULL, NULL} |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2799 | }; |
| 2800 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 2801 | static PyType_Slot PySSLSocket_slots[] = { |
| 2802 | {Py_tp_methods, PySSLMethods}, |
| 2803 | {Py_tp_getset, ssl_getsetlist}, |
| 2804 | {Py_tp_dealloc, PySSL_dealloc}, |
| 2805 | {Py_tp_traverse, PySSL_traverse}, |
| 2806 | {Py_tp_clear, PySSL_clear}, |
| 2807 | {0, 0}, |
| 2808 | }; |
| 2809 | |
| 2810 | static PyType_Spec PySSLSocket_spec = { |
| 2811 | "_ssl._SSLSocket", |
| 2812 | sizeof(PySSLSocket), |
| 2813 | 0, |
| 2814 | Py_TPFLAGS_DEFAULT, |
| 2815 | PySSLSocket_slots, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2816 | }; |
| 2817 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2818 | /* |
| 2819 | * _SSLContext objects |
| 2820 | */ |
| 2821 | |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2822 | static int |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2823 | _set_verify_mode(PySSLContext *self, enum py_ssl_cert_requirements n) |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2824 | { |
| 2825 | int mode; |
| 2826 | int (*verify_cb)(int, X509_STORE_CTX *) = NULL; |
| 2827 | |
| 2828 | switch(n) { |
| 2829 | case PY_SSL_CERT_NONE: |
| 2830 | mode = SSL_VERIFY_NONE; |
| 2831 | break; |
| 2832 | case PY_SSL_CERT_OPTIONAL: |
| 2833 | mode = SSL_VERIFY_PEER; |
| 2834 | break; |
| 2835 | case PY_SSL_CERT_REQUIRED: |
| 2836 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 2837 | break; |
| 2838 | default: |
| 2839 | PyErr_SetString(PyExc_ValueError, |
| 2840 | "invalid value for verify_mode"); |
| 2841 | return -1; |
| 2842 | } |
Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 2843 | |
| 2844 | /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for |
| 2845 | * server sockets and SSL_set_post_handshake_auth() for client. */ |
| 2846 | |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2847 | /* keep current verify cb */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2848 | verify_cb = SSL_CTX_get_verify_callback(self->ctx); |
| 2849 | SSL_CTX_set_verify(self->ctx, mode, verify_cb); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2850 | return 0; |
| 2851 | } |
| 2852 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2853 | /*[clinic input] |
| 2854 | @classmethod |
| 2855 | _ssl._SSLContext.__new__ |
| 2856 | protocol as proto_version: int |
| 2857 | / |
| 2858 | [clinic start generated code]*/ |
| 2859 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2860 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 2861 | _ssl__SSLContext_impl(PyTypeObject *type, int proto_version) |
| 2862 | /*[clinic end generated code: output=2cf0d7a0741b6bd1 input=8d58a805b95fc534]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2863 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2864 | PySSLContext *self; |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 2865 | long options; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2866 | SSL_CTX *ctx = NULL; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 2867 | X509_VERIFY_PARAM *params; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 2868 | int result; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2869 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2870 | /* slower approach, walk MRO and get borrowed reference to module. |
| 2871 | * _PyType_GetModuleByDef is required for SSLContext subclasses */ |
| 2872 | PyObject *module = _PyType_GetModuleByDef(type, &_sslmodule_def); |
| 2873 | if (module == NULL) { |
| 2874 | PyErr_SetString(PyExc_RuntimeError, |
| 2875 | "Cannot find internal module state"); |
| 2876 | return NULL; |
| 2877 | } |
| 2878 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2879 | PySSL_BEGIN_ALLOW_THREADS |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2880 | switch(proto_version) { |
| 2881 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 2882 | case PY_SSL_VERSION_SSL3: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2883 | ctx = SSL_CTX_new(SSLv3_method()); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2884 | break; |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 2885 | #endif |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 2886 | #if (defined(TLS1_VERSION) && \ |
| 2887 | !defined(OPENSSL_NO_TLS1) && \ |
| 2888 | !defined(OPENSSL_NO_TLS1_METHOD)) |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2889 | case PY_SSL_VERSION_TLS1: |
| 2890 | ctx = SSL_CTX_new(TLSv1_method()); |
| 2891 | break; |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 2892 | #endif |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 2893 | #if (defined(TLS1_1_VERSION) && \ |
| 2894 | !defined(OPENSSL_NO_TLS1_1) && \ |
| 2895 | !defined(OPENSSL_NO_TLS1_1_METHOD)) |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2896 | case PY_SSL_VERSION_TLS1_1: |
| 2897 | ctx = SSL_CTX_new(TLSv1_1_method()); |
| 2898 | break; |
| 2899 | #endif |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 2900 | #if (defined(TLS1_2_VERSION) && \ |
| 2901 | !defined(OPENSSL_NO_TLS1_2) && \ |
| 2902 | !defined(OPENSSL_NO_TLS1_2_METHOD)) |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2903 | case PY_SSL_VERSION_TLS1_2: |
| 2904 | ctx = SSL_CTX_new(TLSv1_2_method()); |
| 2905 | break; |
| 2906 | #endif |
| 2907 | case PY_SSL_VERSION_TLS: |
| 2908 | /* SSLv23 */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 2909 | ctx = SSL_CTX_new(TLS_method()); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2910 | break; |
| 2911 | case PY_SSL_VERSION_TLS_CLIENT: |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2912 | ctx = SSL_CTX_new(TLS_client_method()); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2913 | break; |
| 2914 | case PY_SSL_VERSION_TLS_SERVER: |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2915 | ctx = SSL_CTX_new(TLS_server_method()); |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2916 | break; |
| 2917 | default: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2918 | proto_version = -1; |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2919 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2920 | PySSL_END_ALLOW_THREADS |
| 2921 | |
| 2922 | if (proto_version == -1) { |
| 2923 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 6e8cda9 | 2020-05-16 03:33:05 +0200 | [diff] [blame] | 2924 | "invalid or unsupported protocol version"); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2925 | return NULL; |
| 2926 | } |
| 2927 | if (ctx == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2928 | _setSSLError(get_ssl_state(module), NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2929 | return NULL; |
| 2930 | } |
| 2931 | |
| 2932 | assert(type != NULL && type->tp_alloc != NULL); |
| 2933 | self = (PySSLContext *) type->tp_alloc(type, 0); |
| 2934 | if (self == NULL) { |
| 2935 | SSL_CTX_free(ctx); |
| 2936 | return NULL; |
| 2937 | } |
| 2938 | self->ctx = ctx; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 2939 | self->hostflags = X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 2940 | self->protocol = proto_version; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2941 | self->msg_cb = NULL; |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 2942 | self->keylog_filename = NULL; |
| 2943 | self->keylog_bio = NULL; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 2944 | self->alpn_protocols = NULL; |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 2945 | self->set_sni_cb = NULL; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 2946 | self->state = get_ssl_state(module); |
| 2947 | |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 2948 | /* Don't check host name by default */ |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2949 | if (proto_version == PY_SSL_VERSION_TLS_CLIENT) { |
| 2950 | self->check_hostname = 1; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2951 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2952 | Py_DECREF(self); |
| 2953 | return NULL; |
| 2954 | } |
| 2955 | } else { |
| 2956 | self->check_hostname = 0; |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 2957 | if (_set_verify_mode(self, PY_SSL_CERT_NONE) == -1) { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 2958 | Py_DECREF(self); |
| 2959 | return NULL; |
| 2960 | } |
| 2961 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2962 | /* Defaults */ |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 2963 | options = SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
| 2964 | if (proto_version != PY_SSL_VERSION_SSL2) |
| 2965 | options |= SSL_OP_NO_SSLv2; |
Benjamin Peterson | a9dcdab | 2015-11-11 22:38:41 -0800 | [diff] [blame] | 2966 | if (proto_version != PY_SSL_VERSION_SSL3) |
| 2967 | options |= SSL_OP_NO_SSLv3; |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 2968 | /* Minimal security flags for server and client side context. |
| 2969 | * Client sockets ignore server-side parameters. */ |
| 2970 | #ifdef SSL_OP_NO_COMPRESSION |
| 2971 | options |= SSL_OP_NO_COMPRESSION; |
| 2972 | #endif |
| 2973 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
| 2974 | options |= SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 2975 | #endif |
| 2976 | #ifdef SSL_OP_SINGLE_DH_USE |
| 2977 | options |= SSL_OP_SINGLE_DH_USE; |
| 2978 | #endif |
| 2979 | #ifdef SSL_OP_SINGLE_ECDH_USE |
| 2980 | options |= SSL_OP_SINGLE_ECDH_USE; |
| 2981 | #endif |
Christian Heimes | 6f37ebc | 2021-04-09 17:59:21 +0200 | [diff] [blame] | 2982 | #ifdef SSL_OP_IGNORE_UNEXPECTED_EOF |
| 2983 | /* Make OpenSSL 3.0.0 behave like 1.1.1 */ |
| 2984 | options |= SSL_OP_IGNORE_UNEXPECTED_EOF; |
| 2985 | #endif |
Antoine Pitrou | cd3d7ca | 2014-01-09 20:02:20 +0100 | [diff] [blame] | 2986 | SSL_CTX_set_options(self->ctx, options); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 2987 | |
Semen Zhydenko | 1295e11 | 2017-10-15 21:28:31 +0200 | [diff] [blame] | 2988 | /* A bare minimum cipher list without completely broken cipher suites. |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 2989 | * It's far from perfect but gives users a better head start. */ |
| 2990 | if (proto_version != PY_SSL_VERSION_SSL2) { |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 2991 | #if PY_SSL_DEFAULT_CIPHERS == 2 |
| 2992 | /* stick to OpenSSL's default settings */ |
| 2993 | result = 1; |
| 2994 | #else |
| 2995 | result = SSL_CTX_set_cipher_list(ctx, PY_SSL_DEFAULT_CIPHER_STRING); |
| 2996 | #endif |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 2997 | } else { |
| 2998 | /* SSLv2 needs MD5 */ |
| 2999 | result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL"); |
| 3000 | } |
| 3001 | if (result == 0) { |
| 3002 | Py_DECREF(self); |
| 3003 | ERR_clear_error(); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3004 | PyErr_SetString(get_state_ctx(self)->PySSLErrorObject, |
Christian Heimes | 358cfd4 | 2016-09-10 22:43:48 +0200 | [diff] [blame] | 3005 | "No cipher can be selected."); |
| 3006 | return NULL; |
| 3007 | } |
| 3008 | |
Benjamin Peterson | 3b1a8b3 | 2016-01-07 21:37:37 -0800 | [diff] [blame] | 3009 | /* Set SSL_MODE_RELEASE_BUFFERS. This potentially greatly reduces memory |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 3010 | usage for no cost at all. */ |
| 3011 | SSL_CTX_set_mode(self->ctx, SSL_MODE_RELEASE_BUFFERS); |
Antoine Pitrou | 0bebbc3 | 2014-03-22 18:13:50 +0100 | [diff] [blame] | 3012 | |
Antoine Pitrou | fc113ee | 2010-10-13 12:46:13 +0000 | [diff] [blame] | 3013 | #define SID_CTX "Python" |
| 3014 | SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX, |
| 3015 | sizeof(SID_CTX)); |
| 3016 | #undef SID_CTX |
| 3017 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3018 | params = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3019 | /* Improve trust chain building when cross-signed intermediate |
| 3020 | certificates are present. See https://bugs.python.org/issue23476. */ |
| 3021 | X509_VERIFY_PARAM_set_flags(params, X509_V_FLAG_TRUSTED_FIRST); |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3022 | X509_VERIFY_PARAM_set_hostflags(params, self->hostflags); |
Benjamin Peterson | fdb1971 | 2015-03-04 22:11:12 -0500 | [diff] [blame] | 3023 | |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3024 | #ifdef TLS1_3_VERSION |
| 3025 | self->post_handshake_auth = 0; |
| 3026 | SSL_CTX_set_post_handshake_auth(self->ctx, self->post_handshake_auth); |
| 3027 | #endif |
| 3028 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3029 | return (PyObject *)self; |
| 3030 | } |
| 3031 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3032 | static int |
| 3033 | context_traverse(PySSLContext *self, visitproc visit, void *arg) |
| 3034 | { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3035 | Py_VISIT(self->set_sni_cb); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3036 | Py_VISIT(self->msg_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3037 | return 0; |
| 3038 | } |
| 3039 | |
| 3040 | static int |
| 3041 | context_clear(PySSLContext *self) |
| 3042 | { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3043 | Py_CLEAR(self->set_sni_cb); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3044 | Py_CLEAR(self->msg_cb); |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 3045 | Py_CLEAR(self->keylog_filename); |
| 3046 | if (self->keylog_bio != NULL) { |
| 3047 | PySSL_BEGIN_ALLOW_THREADS |
| 3048 | BIO_free_all(self->keylog_bio); |
| 3049 | PySSL_END_ALLOW_THREADS |
| 3050 | self->keylog_bio = NULL; |
| 3051 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3052 | return 0; |
| 3053 | } |
| 3054 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3055 | static void |
| 3056 | context_dealloc(PySSLContext *self) |
| 3057 | { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 3058 | PyTypeObject *tp = Py_TYPE(self); |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 3059 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
| 3060 | PyObject_GC_UnTrack(self); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 3061 | context_clear(self); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3062 | SSL_CTX_free(self->ctx); |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 3063 | PyMem_FREE(self->alpn_protocols); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3064 | Py_TYPE(self)->tp_free(self); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 3065 | Py_DECREF(tp); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3068 | /*[clinic input] |
| 3069 | _ssl._SSLContext.set_ciphers |
| 3070 | cipherlist: str |
| 3071 | / |
| 3072 | [clinic start generated code]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3073 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3074 | static PyObject * |
| 3075 | _ssl__SSLContext_set_ciphers_impl(PySSLContext *self, const char *cipherlist) |
| 3076 | /*[clinic end generated code: output=3a3162f3557c0f3f input=a7ac931b9f3ca7fc]*/ |
| 3077 | { |
| 3078 | int ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3079 | if (ret == 0) { |
Antoine Pitrou | 65ec8ae | 2010-05-16 19:56:32 +0000 | [diff] [blame] | 3080 | /* Clearing the error queue is necessary on some OpenSSL versions, |
| 3081 | otherwise the error will be reported again when another SSL call |
| 3082 | is done. */ |
| 3083 | ERR_clear_error(); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3084 | PyErr_SetString(get_state_ctx(self)->PySSLErrorObject, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3085 | "No cipher can be selected."); |
| 3086 | return NULL; |
| 3087 | } |
| 3088 | Py_RETURN_NONE; |
| 3089 | } |
| 3090 | |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3091 | /*[clinic input] |
| 3092 | _ssl._SSLContext.get_ciphers |
| 3093 | [clinic start generated code]*/ |
| 3094 | |
| 3095 | static PyObject * |
| 3096 | _ssl__SSLContext_get_ciphers_impl(PySSLContext *self) |
| 3097 | /*[clinic end generated code: output=a56e4d68a406dfc4 input=a2aadc9af89b79c5]*/ |
| 3098 | { |
| 3099 | SSL *ssl = NULL; |
| 3100 | STACK_OF(SSL_CIPHER) *sk = NULL; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 3101 | const SSL_CIPHER *cipher; |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3102 | int i=0; |
| 3103 | PyObject *result = NULL, *dct; |
| 3104 | |
| 3105 | ssl = SSL_new(self->ctx); |
| 3106 | if (ssl == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3107 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3108 | goto exit; |
| 3109 | } |
| 3110 | sk = SSL_get_ciphers(ssl); |
| 3111 | |
| 3112 | result = PyList_New(sk_SSL_CIPHER_num(sk)); |
| 3113 | if (result == NULL) { |
| 3114 | goto exit; |
| 3115 | } |
| 3116 | |
| 3117 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { |
| 3118 | cipher = sk_SSL_CIPHER_value(sk, i); |
| 3119 | dct = cipher_to_dict(cipher); |
| 3120 | if (dct == NULL) { |
| 3121 | Py_CLEAR(result); |
| 3122 | goto exit; |
| 3123 | } |
| 3124 | PyList_SET_ITEM(result, i, dct); |
| 3125 | } |
| 3126 | |
| 3127 | exit: |
| 3128 | if (ssl != NULL) |
| 3129 | SSL_free(ssl); |
| 3130 | return result; |
| 3131 | |
| 3132 | } |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 3133 | |
| 3134 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3135 | static int |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3136 | do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen, |
| 3137 | const unsigned char *server_protocols, unsigned int server_protocols_len, |
| 3138 | const unsigned char *client_protocols, unsigned int client_protocols_len) |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3139 | { |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3140 | int ret; |
| 3141 | if (client_protocols == NULL) { |
| 3142 | client_protocols = (unsigned char *)""; |
| 3143 | client_protocols_len = 0; |
| 3144 | } |
| 3145 | if (server_protocols == NULL) { |
| 3146 | server_protocols = (unsigned char *)""; |
| 3147 | server_protocols_len = 0; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3148 | } |
| 3149 | |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3150 | ret = SSL_select_next_proto(out, outlen, |
| 3151 | server_protocols, server_protocols_len, |
| 3152 | client_protocols, client_protocols_len); |
| 3153 | if (alpn && ret != OPENSSL_NPN_NEGOTIATED) |
| 3154 | return SSL_TLSEXT_ERR_NOACK; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3155 | |
| 3156 | return SSL_TLSEXT_ERR_OK; |
| 3157 | } |
| 3158 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3159 | static int |
| 3160 | _selectALPN_cb(SSL *s, |
| 3161 | const unsigned char **out, unsigned char *outlen, |
| 3162 | const unsigned char *client_protocols, unsigned int client_protocols_len, |
| 3163 | void *args) |
| 3164 | { |
| 3165 | PySSLContext *ctx = (PySSLContext *)args; |
Benjamin Peterson | 8861502 | 2015-01-23 17:30:26 -0500 | [diff] [blame] | 3166 | return do_protocol_selection(1, (unsigned char **)out, outlen, |
| 3167 | ctx->alpn_protocols, ctx->alpn_protocols_len, |
| 3168 | client_protocols, client_protocols_len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3169 | } |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3170 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3171 | /*[clinic input] |
| 3172 | _ssl._SSLContext._set_alpn_protocols |
| 3173 | protos: Py_buffer |
| 3174 | / |
| 3175 | [clinic start generated code]*/ |
| 3176 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3177 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3178 | _ssl__SSLContext__set_alpn_protocols_impl(PySSLContext *self, |
| 3179 | Py_buffer *protos) |
| 3180 | /*[clinic end generated code: output=87599a7f76651a9b input=9bba964595d519be]*/ |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3181 | { |
Victor Stinner | 5a61559 | 2017-09-14 01:10:30 -0700 | [diff] [blame] | 3182 | if ((size_t)protos->len > UINT_MAX) { |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3183 | PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | d53fe5f | 2019-03-13 22:59:55 +0200 | [diff] [blame] | 3184 | "protocols longer than %u bytes", UINT_MAX); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3185 | return NULL; |
| 3186 | } |
| 3187 | |
Victor Stinner | 00d7abd | 2020-12-01 09:56:42 +0100 | [diff] [blame] | 3188 | PyMem_Free(self->alpn_protocols); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3189 | self->alpn_protocols = PyMem_Malloc(protos->len); |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3190 | if (!self->alpn_protocols) |
| 3191 | return PyErr_NoMemory(); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3192 | memcpy(self->alpn_protocols, protos->buf, protos->len); |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 3193 | self->alpn_protocols_len = (unsigned int)protos->len; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3194 | |
| 3195 | if (SSL_CTX_set_alpn_protos(self->ctx, self->alpn_protocols, self->alpn_protocols_len)) |
| 3196 | return PyErr_NoMemory(); |
| 3197 | SSL_CTX_set_alpn_select_cb(self->ctx, _selectALPN_cb, self); |
| 3198 | |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3199 | Py_RETURN_NONE; |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 3200 | } |
| 3201 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3202 | static PyObject * |
| 3203 | get_verify_mode(PySSLContext *self, void *c) |
| 3204 | { |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3205 | /* ignore SSL_VERIFY_CLIENT_ONCE and SSL_VERIFY_POST_HANDSHAKE */ |
| 3206 | int mask = (SSL_VERIFY_NONE | SSL_VERIFY_PEER | |
| 3207 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT); |
| 3208 | switch (SSL_CTX_get_verify_mode(self->ctx) & mask) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3209 | case SSL_VERIFY_NONE: |
| 3210 | return PyLong_FromLong(PY_SSL_CERT_NONE); |
| 3211 | case SSL_VERIFY_PEER: |
| 3212 | return PyLong_FromLong(PY_SSL_CERT_OPTIONAL); |
| 3213 | case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT: |
| 3214 | return PyLong_FromLong(PY_SSL_CERT_REQUIRED); |
| 3215 | } |
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 | "invalid return value from SSL_CTX_get_verify_mode"); |
| 3218 | return NULL; |
| 3219 | } |
| 3220 | |
| 3221 | static int |
| 3222 | set_verify_mode(PySSLContext *self, PyObject *arg, void *c) |
| 3223 | { |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3224 | int n; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3225 | if (!PyArg_Parse(arg, "i", &n)) |
| 3226 | return -1; |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 3227 | if (n == PY_SSL_CERT_NONE && self->check_hostname) { |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3228 | PyErr_SetString(PyExc_ValueError, |
| 3229 | "Cannot set verify_mode to CERT_NONE when " |
| 3230 | "check_hostname is enabled."); |
| 3231 | return -1; |
| 3232 | } |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3233 | return _set_verify_mode(self, n); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | static PyObject * |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3237 | get_verify_flags(PySSLContext *self, void *c) |
| 3238 | { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3239 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3240 | unsigned long flags; |
| 3241 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3242 | param = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3243 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3244 | return PyLong_FromUnsignedLong(flags); |
| 3245 | } |
| 3246 | |
| 3247 | static int |
| 3248 | set_verify_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3249 | { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3250 | X509_VERIFY_PARAM *param; |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3251 | unsigned long new_flags, flags, set, clear; |
| 3252 | |
| 3253 | if (!PyArg_Parse(arg, "k", &new_flags)) |
| 3254 | return -1; |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3255 | param = SSL_CTX_get0_param(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3256 | flags = X509_VERIFY_PARAM_get_flags(param); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3257 | clear = flags & ~new_flags; |
| 3258 | set = ~flags & new_flags; |
| 3259 | if (clear) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3260 | if (!X509_VERIFY_PARAM_clear_flags(param, clear)) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3261 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3262 | return -1; |
| 3263 | } |
| 3264 | } |
| 3265 | if (set) { |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3266 | if (!X509_VERIFY_PARAM_set_flags(param, set)) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3267 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3268 | return -1; |
| 3269 | } |
| 3270 | } |
| 3271 | return 0; |
| 3272 | } |
| 3273 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 3274 | /* Getter and setter for protocol version */ |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 3275 | static int |
| 3276 | set_min_max_proto_version(PySSLContext *self, PyObject *arg, int what) |
| 3277 | { |
| 3278 | long v; |
| 3279 | int result; |
| 3280 | |
| 3281 | if (!PyArg_Parse(arg, "l", &v)) |
| 3282 | return -1; |
| 3283 | if (v > INT_MAX) { |
| 3284 | PyErr_SetString(PyExc_OverflowError, "Option is too long"); |
| 3285 | return -1; |
| 3286 | } |
| 3287 | |
| 3288 | switch(self->protocol) { |
| 3289 | case PY_SSL_VERSION_TLS_CLIENT: /* fall through */ |
| 3290 | case PY_SSL_VERSION_TLS_SERVER: /* fall through */ |
| 3291 | case PY_SSL_VERSION_TLS: |
| 3292 | break; |
| 3293 | default: |
| 3294 | PyErr_SetString( |
| 3295 | PyExc_ValueError, |
| 3296 | "The context's protocol doesn't support modification of " |
| 3297 | "highest and lowest version." |
| 3298 | ); |
| 3299 | return -1; |
| 3300 | } |
| 3301 | |
| 3302 | if (what == 0) { |
| 3303 | switch(v) { |
| 3304 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3305 | v = 0; |
| 3306 | break; |
| 3307 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3308 | /* Emulate max for set_min_proto_version */ |
| 3309 | v = PY_PROTO_MAXIMUM_AVAILABLE; |
| 3310 | break; |
| 3311 | default: |
| 3312 | break; |
| 3313 | } |
| 3314 | result = SSL_CTX_set_min_proto_version(self->ctx, v); |
| 3315 | } |
| 3316 | else { |
| 3317 | switch(v) { |
| 3318 | case PY_PROTO_MAXIMUM_SUPPORTED: |
| 3319 | v = 0; |
| 3320 | break; |
| 3321 | case PY_PROTO_MINIMUM_SUPPORTED: |
| 3322 | /* Emulate max for set_min_proto_version */ |
| 3323 | v = PY_PROTO_MINIMUM_AVAILABLE; |
| 3324 | break; |
| 3325 | default: |
| 3326 | break; |
| 3327 | } |
| 3328 | result = SSL_CTX_set_max_proto_version(self->ctx, v); |
| 3329 | } |
| 3330 | if (result == 0) { |
| 3331 | PyErr_Format(PyExc_ValueError, |
| 3332 | "Unsupported protocol version 0x%x", v); |
| 3333 | return -1; |
| 3334 | } |
| 3335 | return 0; |
| 3336 | } |
| 3337 | |
| 3338 | static PyObject * |
| 3339 | get_minimum_version(PySSLContext *self, void *c) |
| 3340 | { |
| 3341 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MIN_PROTO_VERSION, 0, NULL); |
| 3342 | if (v == 0) { |
| 3343 | v = PY_PROTO_MINIMUM_SUPPORTED; |
| 3344 | } |
| 3345 | return PyLong_FromLong(v); |
| 3346 | } |
| 3347 | |
| 3348 | static int |
| 3349 | set_minimum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3350 | { |
| 3351 | return set_min_max_proto_version(self, arg, 0); |
| 3352 | } |
| 3353 | |
| 3354 | static PyObject * |
| 3355 | get_maximum_version(PySSLContext *self, void *c) |
| 3356 | { |
| 3357 | int v = SSL_CTX_ctrl(self->ctx, SSL_CTRL_GET_MAX_PROTO_VERSION, 0, NULL); |
| 3358 | if (v == 0) { |
| 3359 | v = PY_PROTO_MAXIMUM_SUPPORTED; |
| 3360 | } |
| 3361 | return PyLong_FromLong(v); |
| 3362 | } |
| 3363 | |
| 3364 | static int |
| 3365 | set_maximum_version(PySSLContext *self, PyObject *arg, void *c) |
| 3366 | { |
| 3367 | return set_min_max_proto_version(self, arg, 1); |
| 3368 | } |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 3369 | |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 3370 | #ifdef TLS1_3_VERSION |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3371 | static PyObject * |
| 3372 | get_num_tickets(PySSLContext *self, void *c) |
| 3373 | { |
Victor Stinner | 76611c7 | 2019-07-09 13:30:52 +0200 | [diff] [blame] | 3374 | return PyLong_FromSize_t(SSL_CTX_get_num_tickets(self->ctx)); |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3375 | } |
| 3376 | |
| 3377 | static int |
| 3378 | set_num_tickets(PySSLContext *self, PyObject *arg, void *c) |
| 3379 | { |
| 3380 | long num; |
| 3381 | if (!PyArg_Parse(arg, "l", &num)) |
| 3382 | return -1; |
| 3383 | if (num < 0) { |
| 3384 | PyErr_SetString(PyExc_ValueError, "value must be non-negative"); |
| 3385 | return -1; |
| 3386 | } |
| 3387 | if (self->protocol != PY_SSL_VERSION_TLS_SERVER) { |
| 3388 | PyErr_SetString(PyExc_ValueError, |
| 3389 | "SSLContext is not a server context."); |
| 3390 | return -1; |
| 3391 | } |
| 3392 | if (SSL_CTX_set_num_tickets(self->ctx, num) != 1) { |
| 3393 | PyErr_SetString(PyExc_ValueError, "failed to set num tickets."); |
| 3394 | return -1; |
| 3395 | } |
| 3396 | return 0; |
| 3397 | } |
| 3398 | |
| 3399 | PyDoc_STRVAR(PySSLContext_num_tickets_doc, |
| 3400 | "Control the number of TLSv1.3 session tickets"); |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 3401 | #endif /* TLS1_3_VERSION */ |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 3402 | |
matthewhughes934 | 8e836bb | 2020-07-17 09:59:15 +0100 | [diff] [blame] | 3403 | static PyObject * |
| 3404 | get_security_level(PySSLContext *self, void *c) |
| 3405 | { |
| 3406 | return PyLong_FromLong(SSL_CTX_get_security_level(self->ctx)); |
| 3407 | } |
| 3408 | PyDoc_STRVAR(PySSLContext_security_level_doc, "The current security level"); |
matthewhughes934 | 8e836bb | 2020-07-17 09:59:15 +0100 | [diff] [blame] | 3409 | |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 3410 | static PyObject * |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3411 | get_options(PySSLContext *self, void *c) |
| 3412 | { |
| 3413 | return PyLong_FromLong(SSL_CTX_get_options(self->ctx)); |
| 3414 | } |
| 3415 | |
| 3416 | static int |
| 3417 | set_options(PySSLContext *self, PyObject *arg, void *c) |
| 3418 | { |
| 3419 | long new_opts, opts, set, clear; |
| 3420 | if (!PyArg_Parse(arg, "l", &new_opts)) |
| 3421 | return -1; |
| 3422 | opts = SSL_CTX_get_options(self->ctx); |
| 3423 | clear = opts & ~new_opts; |
| 3424 | set = ~opts & new_opts; |
| 3425 | if (clear) { |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3426 | SSL_CTX_clear_options(self->ctx, clear); |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3427 | } |
| 3428 | if (set) |
| 3429 | SSL_CTX_set_options(self->ctx, set); |
| 3430 | return 0; |
| 3431 | } |
| 3432 | |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3433 | static PyObject * |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 3434 | get_host_flags(PySSLContext *self, void *c) |
| 3435 | { |
| 3436 | return PyLong_FromUnsignedLong(self->hostflags); |
| 3437 | } |
| 3438 | |
| 3439 | static int |
| 3440 | set_host_flags(PySSLContext *self, PyObject *arg, void *c) |
| 3441 | { |
| 3442 | X509_VERIFY_PARAM *param; |
| 3443 | unsigned int new_flags = 0; |
| 3444 | |
| 3445 | if (!PyArg_Parse(arg, "I", &new_flags)) |
| 3446 | return -1; |
| 3447 | |
| 3448 | param = SSL_CTX_get0_param(self->ctx); |
| 3449 | self->hostflags = new_flags; |
| 3450 | X509_VERIFY_PARAM_set_hostflags(param, new_flags); |
| 3451 | return 0; |
| 3452 | } |
| 3453 | |
| 3454 | static PyObject * |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3455 | get_check_hostname(PySSLContext *self, void *c) |
| 3456 | { |
| 3457 | return PyBool_FromLong(self->check_hostname); |
| 3458 | } |
| 3459 | |
| 3460 | static int |
| 3461 | set_check_hostname(PySSLContext *self, PyObject *arg, void *c) |
| 3462 | { |
| 3463 | int check_hostname; |
| 3464 | if (!PyArg_Parse(arg, "p", &check_hostname)) |
| 3465 | return -1; |
| 3466 | if (check_hostname && |
| 3467 | SSL_CTX_get_verify_mode(self->ctx) == SSL_VERIFY_NONE) { |
Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3468 | /* check_hostname = True sets verify_mode = CERT_REQUIRED */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3469 | if (_set_verify_mode(self, PY_SSL_CERT_REQUIRED) == -1) { |
Christian Heimes | e82c034 | 2017-09-15 20:29:57 +0200 | [diff] [blame] | 3470 | return -1; |
| 3471 | } |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3472 | } |
| 3473 | self->check_hostname = check_hostname; |
| 3474 | return 0; |
| 3475 | } |
| 3476 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3477 | static PyObject * |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3478 | get_post_handshake_auth(PySSLContext *self, void *c) { |
| 3479 | #if TLS1_3_VERSION |
| 3480 | return PyBool_FromLong(self->post_handshake_auth); |
| 3481 | #else |
| 3482 | Py_RETURN_NONE; |
| 3483 | #endif |
| 3484 | } |
| 3485 | |
| 3486 | #if TLS1_3_VERSION |
| 3487 | static int |
| 3488 | set_post_handshake_auth(PySSLContext *self, PyObject *arg, void *c) { |
Zackery Spytz | 842acaa | 2018-12-17 07:52:45 -0700 | [diff] [blame] | 3489 | if (arg == NULL) { |
| 3490 | PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); |
| 3491 | return -1; |
| 3492 | } |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3493 | int pha = PyObject_IsTrue(arg); |
| 3494 | |
| 3495 | if (pha == -1) { |
| 3496 | return -1; |
| 3497 | } |
| 3498 | self->post_handshake_auth = pha; |
| 3499 | |
Christian Heimes | f0f5930 | 2019-07-01 08:29:17 +0200 | [diff] [blame] | 3500 | /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for |
| 3501 | * server sockets and SSL_set_post_handshake_auth() for client. */ |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 3502 | |
| 3503 | return 0; |
| 3504 | } |
| 3505 | #endif |
| 3506 | |
| 3507 | static PyObject * |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 3508 | get_protocol(PySSLContext *self, void *c) { |
| 3509 | return PyLong_FromLong(self->protocol); |
| 3510 | } |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 3511 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3512 | typedef struct { |
| 3513 | PyThreadState *thread_state; |
| 3514 | PyObject *callable; |
| 3515 | char *password; |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3516 | int size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3517 | int error; |
| 3518 | } _PySSLPasswordInfo; |
| 3519 | |
| 3520 | static int |
| 3521 | _pwinfo_set(_PySSLPasswordInfo *pw_info, PyObject* password, |
| 3522 | const char *bad_type_error) |
| 3523 | { |
| 3524 | /* Set the password and size fields of a _PySSLPasswordInfo struct |
| 3525 | from a unicode, bytes, or byte array object. |
| 3526 | The password field will be dynamically allocated and must be freed |
| 3527 | by the caller */ |
| 3528 | PyObject *password_bytes = NULL; |
| 3529 | const char *data = NULL; |
| 3530 | Py_ssize_t size; |
| 3531 | |
| 3532 | if (PyUnicode_Check(password)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3533 | password_bytes = PyUnicode_AsUTF8String(password); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3534 | if (!password_bytes) { |
| 3535 | goto error; |
| 3536 | } |
| 3537 | data = PyBytes_AS_STRING(password_bytes); |
| 3538 | size = PyBytes_GET_SIZE(password_bytes); |
| 3539 | } else if (PyBytes_Check(password)) { |
| 3540 | data = PyBytes_AS_STRING(password); |
| 3541 | size = PyBytes_GET_SIZE(password); |
| 3542 | } else if (PyByteArray_Check(password)) { |
| 3543 | data = PyByteArray_AS_STRING(password); |
| 3544 | size = PyByteArray_GET_SIZE(password); |
| 3545 | } else { |
| 3546 | PyErr_SetString(PyExc_TypeError, bad_type_error); |
| 3547 | goto error; |
| 3548 | } |
| 3549 | |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3550 | if (size > (Py_ssize_t)INT_MAX) { |
| 3551 | PyErr_Format(PyExc_ValueError, |
| 3552 | "password cannot be longer than %d bytes", INT_MAX); |
| 3553 | goto error; |
| 3554 | } |
| 3555 | |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3556 | PyMem_Free(pw_info->password); |
| 3557 | pw_info->password = PyMem_Malloc(size); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3558 | if (!pw_info->password) { |
| 3559 | PyErr_SetString(PyExc_MemoryError, |
| 3560 | "unable to allocate password buffer"); |
| 3561 | goto error; |
| 3562 | } |
| 3563 | memcpy(pw_info->password, data, size); |
Victor Stinner | 9ee0203 | 2013-06-23 15:08:23 +0200 | [diff] [blame] | 3564 | pw_info->size = (int)size; |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3565 | |
| 3566 | Py_XDECREF(password_bytes); |
| 3567 | return 1; |
| 3568 | |
| 3569 | error: |
| 3570 | Py_XDECREF(password_bytes); |
| 3571 | return 0; |
| 3572 | } |
| 3573 | |
| 3574 | static int |
| 3575 | _password_callback(char *buf, int size, int rwflag, void *userdata) |
| 3576 | { |
| 3577 | _PySSLPasswordInfo *pw_info = (_PySSLPasswordInfo*) userdata; |
| 3578 | PyObject *fn_ret = NULL; |
| 3579 | |
| 3580 | PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); |
| 3581 | |
Christian Heimes | d3b73f3 | 2021-04-09 15:23:38 +0200 | [diff] [blame] | 3582 | if (pw_info->error) { |
| 3583 | /* already failed previously. OpenSSL 3.0.0-alpha14 invokes the |
| 3584 | * callback multiple times which can lead to fatal Python error in |
| 3585 | * exception check. */ |
| 3586 | goto error; |
| 3587 | } |
| 3588 | |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3589 | if (pw_info->callable) { |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3590 | fn_ret = _PyObject_CallNoArg(pw_info->callable); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3591 | if (!fn_ret) { |
| 3592 | /* TODO: It would be nice to move _ctypes_add_traceback() into the |
| 3593 | core python API, so we could use it to add a frame here */ |
| 3594 | goto error; |
| 3595 | } |
| 3596 | |
| 3597 | if (!_pwinfo_set(pw_info, fn_ret, |
| 3598 | "password callback must return a string")) { |
| 3599 | goto error; |
| 3600 | } |
| 3601 | Py_CLEAR(fn_ret); |
| 3602 | } |
| 3603 | |
| 3604 | if (pw_info->size > size) { |
| 3605 | PyErr_Format(PyExc_ValueError, |
| 3606 | "password cannot be longer than %d bytes", size); |
| 3607 | goto error; |
| 3608 | } |
| 3609 | |
| 3610 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3611 | memcpy(buf, pw_info->password, pw_info->size); |
| 3612 | return pw_info->size; |
| 3613 | |
| 3614 | error: |
| 3615 | Py_XDECREF(fn_ret); |
| 3616 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 3617 | pw_info->error = 1; |
| 3618 | return -1; |
| 3619 | } |
| 3620 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3621 | /*[clinic input] |
| 3622 | _ssl._SSLContext.load_cert_chain |
| 3623 | certfile: object |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3624 | keyfile: object = None |
| 3625 | password: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3626 | |
| 3627 | [clinic start generated code]*/ |
| 3628 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 3629 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3630 | _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile, |
| 3631 | PyObject *keyfile, PyObject *password) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3632 | /*[clinic end generated code: output=9480bc1c380e2095 input=30bc7e967ea01a58]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3633 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3634 | PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3635 | pem_password_cb *orig_passwd_cb = SSL_CTX_get_default_passwd_cb(self->ctx); |
| 3636 | 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] | 3637 | _PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 }; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3638 | int r; |
| 3639 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3640 | errno = 0; |
Antoine Pitrou | 67e8e56 | 2010-09-01 20:55:41 +0000 | [diff] [blame] | 3641 | ERR_clear_error(); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3642 | if (keyfile == Py_None) |
| 3643 | keyfile = NULL; |
| 3644 | if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3645 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3646 | PyErr_SetString(PyExc_TypeError, |
| 3647 | "certfile should be a valid filesystem path"); |
| 3648 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3649 | return NULL; |
| 3650 | } |
| 3651 | if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3652 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3653 | PyErr_SetString(PyExc_TypeError, |
| 3654 | "keyfile should be a valid filesystem path"); |
| 3655 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3656 | goto error; |
| 3657 | } |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3658 | if (password != Py_None) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3659 | if (PyCallable_Check(password)) { |
| 3660 | pw_info.callable = password; |
| 3661 | } else if (!_pwinfo_set(&pw_info, password, |
| 3662 | "password should be a string or callable")) { |
| 3663 | goto error; |
| 3664 | } |
| 3665 | SSL_CTX_set_default_passwd_cb(self->ctx, _password_callback); |
| 3666 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, &pw_info); |
| 3667 | } |
| 3668 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3669 | r = SSL_CTX_use_certificate_chain_file(self->ctx, |
| 3670 | PyBytes_AS_STRING(certfile_bytes)); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3671 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3672 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3673 | if (pw_info.error) { |
| 3674 | ERR_clear_error(); |
| 3675 | /* the password callback has already set the error information */ |
| 3676 | } |
| 3677 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 3678 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3679 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3680 | } |
| 3681 | else { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3682 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3683 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3684 | goto error; |
| 3685 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3686 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 9c25486 | 2011-04-03 18:15:34 +0200 | [diff] [blame] | 3687 | r = SSL_CTX_use_PrivateKey_file(self->ctx, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3688 | PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes), |
| 3689 | SSL_FILETYPE_PEM); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3690 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| 3691 | Py_CLEAR(keyfile_bytes); |
| 3692 | Py_CLEAR(certfile_bytes); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3693 | if (r != 1) { |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3694 | if (pw_info.error) { |
| 3695 | ERR_clear_error(); |
| 3696 | /* the password callback has already set the error information */ |
| 3697 | } |
| 3698 | else if (errno != 0) { |
Giampaolo Rodolà | e0f9863 | 2010-09-01 19:28:49 +0000 | [diff] [blame] | 3699 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3700 | PyErr_SetFromErrno(PyExc_OSError); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3701 | } |
| 3702 | else { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3703 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3704 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3705 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3706 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3707 | PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3708 | r = SSL_CTX_check_private_key(self->ctx); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3709 | PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3710 | if (r != 1) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3711 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3712 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3713 | } |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3714 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 3715 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3716 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3717 | Py_RETURN_NONE; |
| 3718 | |
| 3719 | error: |
Antoine Pitrou | 4fd1e6a | 2011-08-25 14:39:44 +0200 | [diff] [blame] | 3720 | SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 3721 | SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
Victor Stinner | 11ebff2 | 2013-07-07 17:07:52 +0200 | [diff] [blame] | 3722 | PyMem_Free(pw_info.password); |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3723 | Py_XDECREF(keyfile_bytes); |
| 3724 | Py_XDECREF(certfile_bytes); |
| 3725 | return NULL; |
| 3726 | } |
| 3727 | |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3728 | /* internal helper function, returns -1 on error |
| 3729 | */ |
| 3730 | static int |
Serhiy Storchaka | 8f87eef | 2020-04-12 14:58:27 +0300 | [diff] [blame] | 3731 | _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len, |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3732 | int filetype) |
| 3733 | { |
| 3734 | BIO *biobuf = NULL; |
| 3735 | X509_STORE *store; |
| 3736 | int retval = 0, err, loaded = 0; |
| 3737 | |
| 3738 | assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM); |
| 3739 | |
| 3740 | if (len <= 0) { |
| 3741 | PyErr_SetString(PyExc_ValueError, |
| 3742 | "Empty certificate data"); |
| 3743 | return -1; |
| 3744 | } else if (len > INT_MAX) { |
| 3745 | PyErr_SetString(PyExc_OverflowError, |
| 3746 | "Certificate data is too long."); |
| 3747 | return -1; |
| 3748 | } |
| 3749 | |
Christian Heimes | 1dbf61f | 2013-11-22 00:34:18 +0100 | [diff] [blame] | 3750 | biobuf = BIO_new_mem_buf(data, (int)len); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3751 | if (biobuf == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3752 | _setSSLError(get_state_ctx(self), "Can't allocate buffer", 0, __FILE__, __LINE__); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3753 | return -1; |
| 3754 | } |
| 3755 | |
| 3756 | store = SSL_CTX_get_cert_store(self->ctx); |
| 3757 | assert(store != NULL); |
| 3758 | |
| 3759 | while (1) { |
| 3760 | X509 *cert = NULL; |
| 3761 | int r; |
| 3762 | |
| 3763 | if (filetype == SSL_FILETYPE_ASN1) { |
| 3764 | cert = d2i_X509_bio(biobuf, NULL); |
| 3765 | } else { |
| 3766 | cert = PEM_read_bio_X509(biobuf, NULL, |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 3767 | SSL_CTX_get_default_passwd_cb(self->ctx), |
| 3768 | SSL_CTX_get_default_passwd_cb_userdata(self->ctx) |
| 3769 | ); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3770 | } |
| 3771 | if (cert == NULL) { |
| 3772 | break; |
| 3773 | } |
| 3774 | r = X509_STORE_add_cert(store, cert); |
| 3775 | X509_free(cert); |
| 3776 | if (!r) { |
| 3777 | err = ERR_peek_last_error(); |
| 3778 | if ((ERR_GET_LIB(err) == ERR_LIB_X509) && |
| 3779 | (ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) { |
| 3780 | /* cert already in hash table, not an error */ |
| 3781 | ERR_clear_error(); |
| 3782 | } else { |
| 3783 | break; |
| 3784 | } |
| 3785 | } |
| 3786 | loaded++; |
| 3787 | } |
| 3788 | |
| 3789 | err = ERR_peek_last_error(); |
| 3790 | if ((filetype == SSL_FILETYPE_ASN1) && |
| 3791 | (loaded > 0) && |
| 3792 | (ERR_GET_LIB(err) == ERR_LIB_ASN1) && |
| 3793 | (ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) { |
| 3794 | /* EOF ASN1 file, not an error */ |
| 3795 | ERR_clear_error(); |
| 3796 | retval = 0; |
| 3797 | } else if ((filetype == SSL_FILETYPE_PEM) && |
| 3798 | (loaded > 0) && |
| 3799 | (ERR_GET_LIB(err) == ERR_LIB_PEM) && |
| 3800 | (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 3801 | /* EOF PEM file, not an error */ |
| 3802 | ERR_clear_error(); |
| 3803 | retval = 0; |
| 3804 | } else { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3805 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3806 | retval = -1; |
| 3807 | } |
| 3808 | |
| 3809 | BIO_free(biobuf); |
| 3810 | return retval; |
| 3811 | } |
| 3812 | |
| 3813 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3814 | /*[clinic input] |
| 3815 | _ssl._SSLContext.load_verify_locations |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3816 | cafile: object = None |
| 3817 | capath: object = None |
| 3818 | cadata: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3819 | |
| 3820 | [clinic start generated code]*/ |
| 3821 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3822 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3823 | _ssl__SSLContext_load_verify_locations_impl(PySSLContext *self, |
| 3824 | PyObject *cafile, |
| 3825 | PyObject *capath, |
| 3826 | PyObject *cadata) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 3827 | /*[clinic end generated code: output=454c7e41230ca551 input=42ecfe258233e194]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3828 | { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3829 | PyObject *cafile_bytes = NULL, *capath_bytes = NULL; |
| 3830 | const char *cafile_buf = NULL, *capath_buf = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3831 | int r = 0, ok = 1; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3832 | |
Giampaolo Rodolà | 745ab38 | 2010-08-29 19:25:49 +0000 | [diff] [blame] | 3833 | errno = 0; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3834 | if (cafile == Py_None) |
| 3835 | cafile = NULL; |
| 3836 | if (capath == Py_None) |
| 3837 | capath = NULL; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3838 | if (cadata == Py_None) |
| 3839 | cadata = NULL; |
| 3840 | |
| 3841 | if (cafile == NULL && capath == NULL && cadata == NULL) { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3842 | PyErr_SetString(PyExc_TypeError, |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3843 | "cafile, capath and cadata cannot be all omitted"); |
| 3844 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3845 | } |
| 3846 | if (cafile && !PyUnicode_FSConverter(cafile, &cafile_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3847 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3848 | PyErr_SetString(PyExc_TypeError, |
| 3849 | "cafile should be a valid filesystem path"); |
| 3850 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3851 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3852 | } |
| 3853 | if (capath && !PyUnicode_FSConverter(capath, &capath_bytes)) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3854 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3855 | PyErr_SetString(PyExc_TypeError, |
| 3856 | "capath should be a valid filesystem path"); |
| 3857 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3858 | goto error; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3859 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3860 | |
| 3861 | /* validata cadata type and load cadata */ |
| 3862 | if (cadata) { |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3863 | if (PyUnicode_Check(cadata)) { |
| 3864 | PyObject *cadata_ascii = PyUnicode_AsASCIIString(cadata); |
| 3865 | if (cadata_ascii == NULL) { |
| 3866 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 3867 | goto invalid_cadata; |
| 3868 | } |
| 3869 | goto error; |
| 3870 | } |
| 3871 | r = _add_ca_certs(self, |
| 3872 | PyBytes_AS_STRING(cadata_ascii), |
| 3873 | PyBytes_GET_SIZE(cadata_ascii), |
| 3874 | SSL_FILETYPE_PEM); |
| 3875 | Py_DECREF(cadata_ascii); |
| 3876 | if (r == -1) { |
| 3877 | goto error; |
| 3878 | } |
| 3879 | } |
| 3880 | else if (PyObject_CheckBuffer(cadata)) { |
| 3881 | Py_buffer buf; |
| 3882 | if (PyObject_GetBuffer(cadata, &buf, PyBUF_SIMPLE)) { |
| 3883 | goto error; |
| 3884 | } |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3885 | if (!PyBuffer_IsContiguous(&buf, 'C') || buf.ndim > 1) { |
| 3886 | PyBuffer_Release(&buf); |
| 3887 | PyErr_SetString(PyExc_TypeError, |
| 3888 | "cadata should be a contiguous buffer with " |
| 3889 | "a single dimension"); |
| 3890 | goto error; |
| 3891 | } |
| 3892 | r = _add_ca_certs(self, buf.buf, buf.len, SSL_FILETYPE_ASN1); |
| 3893 | PyBuffer_Release(&buf); |
| 3894 | if (r == -1) { |
| 3895 | goto error; |
| 3896 | } |
Serhiy Storchaka | 65fb2c0 | 2019-05-31 10:39:15 +0300 | [diff] [blame] | 3897 | } |
| 3898 | else { |
| 3899 | invalid_cadata: |
| 3900 | PyErr_SetString(PyExc_TypeError, |
| 3901 | "cadata should be an ASCII string or a " |
| 3902 | "bytes-like object"); |
| 3903 | goto error; |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3904 | } |
| 3905 | } |
| 3906 | |
| 3907 | /* load cafile or capath */ |
| 3908 | if (cafile || capath) { |
| 3909 | if (cafile) |
| 3910 | cafile_buf = PyBytes_AS_STRING(cafile_bytes); |
| 3911 | if (capath) |
| 3912 | capath_buf = PyBytes_AS_STRING(capath_bytes); |
| 3913 | PySSL_BEGIN_ALLOW_THREADS |
| 3914 | r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf); |
| 3915 | PySSL_END_ALLOW_THREADS |
| 3916 | if (r != 1) { |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3917 | if (errno != 0) { |
| 3918 | ERR_clear_error(); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 3919 | PyErr_SetFromErrno(PyExc_OSError); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3920 | } |
| 3921 | else { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3922 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3923 | } |
| 3924 | goto error; |
| 3925 | } |
| 3926 | } |
| 3927 | goto end; |
| 3928 | |
| 3929 | error: |
| 3930 | ok = 0; |
| 3931 | end: |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3932 | Py_XDECREF(cafile_bytes); |
| 3933 | Py_XDECREF(capath_bytes); |
Christian Heimes | efff706 | 2013-11-21 03:35:02 +0100 | [diff] [blame] | 3934 | if (ok) { |
| 3935 | Py_RETURN_NONE; |
| 3936 | } else { |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3937 | return NULL; |
| 3938 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3941 | /*[clinic input] |
| 3942 | _ssl._SSLContext.load_dh_params |
| 3943 | path as filepath: object |
| 3944 | / |
| 3945 | |
| 3946 | [clinic start generated code]*/ |
| 3947 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3948 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3949 | _ssl__SSLContext_load_dh_params(PySSLContext *self, PyObject *filepath) |
| 3950 | /*[clinic end generated code: output=1c8e57a38e055af0 input=c8871f3c796ae1d6]*/ |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 3951 | { |
| 3952 | FILE *f; |
| 3953 | DH *dh; |
| 3954 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3955 | f = _Py_fopen_obj(filepath, "rb"); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 3956 | if (f == NULL) |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 3957 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 3958 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 3959 | errno = 0; |
| 3960 | PySSL_BEGIN_ALLOW_THREADS |
| 3961 | dh = PEM_read_DHparams(f, NULL, NULL, NULL); |
Antoine Pitrou | 457a229 | 2013-01-12 21:43:45 +0100 | [diff] [blame] | 3962 | fclose(f); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 3963 | PySSL_END_ALLOW_THREADS |
| 3964 | if (dh == NULL) { |
| 3965 | if (errno != 0) { |
| 3966 | ERR_clear_error(); |
| 3967 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); |
| 3968 | } |
| 3969 | else { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3970 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 3971 | } |
| 3972 | return NULL; |
| 3973 | } |
Zackery Spytz | aebc049 | 2020-07-07 22:21:58 -0600 | [diff] [blame] | 3974 | if (!SSL_CTX_set_tmp_dh(self->ctx, dh)) { |
| 3975 | DH_free(dh); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3976 | return _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Zackery Spytz | aebc049 | 2020-07-07 22:21:58 -0600 | [diff] [blame] | 3977 | } |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 3978 | DH_free(dh); |
| 3979 | Py_RETURN_NONE; |
| 3980 | } |
| 3981 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3982 | /*[clinic input] |
| 3983 | _ssl._SSLContext._wrap_socket |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3984 | sock: object(subclass_of="get_state_ctx(self)->Sock_Type") |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3985 | server_side: int |
| 3986 | server_hostname as hostname_obj: object = None |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 3987 | * |
| 3988 | owner: object = None |
| 3989 | session: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3990 | |
| 3991 | [clinic start generated code]*/ |
| 3992 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 3993 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 3994 | _ssl__SSLContext__wrap_socket_impl(PySSLContext *self, PyObject *sock, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 3995 | int server_side, PyObject *hostname_obj, |
| 3996 | PyObject *owner, PyObject *session) |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 3997 | /*[clinic end generated code: output=f103f238633940b4 input=f5916eadbc6eae81]*/ |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 3998 | { |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 3999 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4000 | PyObject *res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4001 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4002 | /* server_hostname is either None (or absent), or to be encoded |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4003 | as IDN A-label (ASCII str) without NULL bytes. */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4004 | if (hostname_obj != Py_None) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4005 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4006 | return NULL; |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4007 | } |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4008 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4009 | res = (PyObject *) newPySSLSocket(self, (PySocketSockObject *)sock, |
| 4010 | server_side, hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4011 | owner, session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4012 | NULL, NULL); |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 4013 | if (hostname != NULL) |
| 4014 | PyMem_Free(hostname); |
| 4015 | return res; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4018 | /*[clinic input] |
| 4019 | _ssl._SSLContext._wrap_bio |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4020 | incoming: object(subclass_of="get_state_ctx(self)->PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
| 4021 | outgoing: object(subclass_of="get_state_ctx(self)->PySSLMemoryBIO_Type", type="PySSLMemoryBIO *") |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4022 | server_side: int |
| 4023 | server_hostname as hostname_obj: object = None |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4024 | * |
| 4025 | owner: object = None |
| 4026 | session: object = None |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4027 | |
| 4028 | [clinic start generated code]*/ |
| 4029 | |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4030 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4031 | _ssl__SSLContext__wrap_bio_impl(PySSLContext *self, PySSLMemoryBIO *incoming, |
| 4032 | PySSLMemoryBIO *outgoing, int server_side, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4033 | PyObject *hostname_obj, PyObject *owner, |
| 4034 | PyObject *session) |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4035 | /*[clinic end generated code: output=5c5d6d9b41f99332 input=331edeec9c738382]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4036 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4037 | char *hostname = NULL; |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4038 | PyObject *res; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4039 | |
| 4040 | /* server_hostname is either None (or absent), or to be encoded |
Christian Heimes | d02ac25 | 2018-03-25 12:36:13 +0200 | [diff] [blame] | 4041 | as IDN A-label (ASCII str) without NULL bytes. */ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4042 | if (hostname_obj != Py_None) { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4043 | if (!PyArg_Parse(hostname_obj, "es", "ascii", &hostname)) |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4044 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | res = (PyObject *) newPySSLSocket(self, NULL, server_side, hostname, |
Christian Heimes | 141c5e8 | 2018-02-24 21:10:57 +0100 | [diff] [blame] | 4048 | owner, session, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4049 | incoming, outgoing); |
| 4050 | |
| 4051 | PyMem_Free(hostname); |
| 4052 | return res; |
| 4053 | } |
| 4054 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4055 | /*[clinic input] |
| 4056 | _ssl._SSLContext.session_stats |
| 4057 | [clinic start generated code]*/ |
| 4058 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4059 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4060 | _ssl__SSLContext_session_stats_impl(PySSLContext *self) |
| 4061 | /*[clinic end generated code: output=0d96411c42893bfb input=7e0a81fb11102c8b]*/ |
Antoine Pitrou | b0182c8 | 2010-10-12 20:09:02 +0000 | [diff] [blame] | 4062 | { |
| 4063 | int r; |
| 4064 | PyObject *value, *stats = PyDict_New(); |
| 4065 | if (!stats) |
| 4066 | return NULL; |
| 4067 | |
| 4068 | #define ADD_STATS(SSL_NAME, KEY_NAME) \ |
| 4069 | value = PyLong_FromLong(SSL_CTX_sess_ ## SSL_NAME (self->ctx)); \ |
| 4070 | if (value == NULL) \ |
| 4071 | goto error; \ |
| 4072 | r = PyDict_SetItemString(stats, KEY_NAME, value); \ |
| 4073 | Py_DECREF(value); \ |
| 4074 | if (r < 0) \ |
| 4075 | goto error; |
| 4076 | |
| 4077 | ADD_STATS(number, "number"); |
| 4078 | ADD_STATS(connect, "connect"); |
| 4079 | ADD_STATS(connect_good, "connect_good"); |
| 4080 | ADD_STATS(connect_renegotiate, "connect_renegotiate"); |
| 4081 | ADD_STATS(accept, "accept"); |
| 4082 | ADD_STATS(accept_good, "accept_good"); |
| 4083 | ADD_STATS(accept_renegotiate, "accept_renegotiate"); |
| 4084 | ADD_STATS(accept, "accept"); |
| 4085 | ADD_STATS(hits, "hits"); |
| 4086 | ADD_STATS(misses, "misses"); |
| 4087 | ADD_STATS(timeouts, "timeouts"); |
| 4088 | ADD_STATS(cache_full, "cache_full"); |
| 4089 | |
| 4090 | #undef ADD_STATS |
| 4091 | |
| 4092 | return stats; |
| 4093 | |
| 4094 | error: |
| 4095 | Py_DECREF(stats); |
| 4096 | return NULL; |
| 4097 | } |
| 4098 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4099 | /*[clinic input] |
| 4100 | _ssl._SSLContext.set_default_verify_paths |
| 4101 | [clinic start generated code]*/ |
| 4102 | |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4103 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4104 | _ssl__SSLContext_set_default_verify_paths_impl(PySSLContext *self) |
| 4105 | /*[clinic end generated code: output=0bee74e6e09deaaa input=35f3408021463d74]*/ |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4106 | { |
| 4107 | if (!SSL_CTX_set_default_verify_paths(self->ctx)) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4108 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 664c2d1 | 2010-11-17 20:29:42 +0000 | [diff] [blame] | 4109 | return NULL; |
| 4110 | } |
| 4111 | Py_RETURN_NONE; |
| 4112 | } |
| 4113 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4114 | /*[clinic input] |
| 4115 | _ssl._SSLContext.set_ecdh_curve |
| 4116 | name: object |
| 4117 | / |
| 4118 | |
| 4119 | [clinic start generated code]*/ |
| 4120 | |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4121 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4122 | _ssl__SSLContext_set_ecdh_curve(PySSLContext *self, PyObject *name) |
| 4123 | /*[clinic end generated code: output=23022c196e40d7d2 input=c2bafb6f6e34726b]*/ |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4124 | { |
| 4125 | PyObject *name_bytes; |
| 4126 | int nid; |
| 4127 | EC_KEY *key; |
| 4128 | |
| 4129 | if (!PyUnicode_FSConverter(name, &name_bytes)) |
| 4130 | return NULL; |
| 4131 | assert(PyBytes_Check(name_bytes)); |
| 4132 | nid = OBJ_sn2nid(PyBytes_AS_STRING(name_bytes)); |
| 4133 | Py_DECREF(name_bytes); |
| 4134 | if (nid == 0) { |
| 4135 | PyErr_Format(PyExc_ValueError, |
| 4136 | "unknown elliptic curve name %R", name); |
| 4137 | return NULL; |
| 4138 | } |
| 4139 | key = EC_KEY_new_by_curve_name(nid); |
| 4140 | if (key == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4141 | _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 4142 | return NULL; |
| 4143 | } |
| 4144 | SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 4145 | EC_KEY_free(key); |
| 4146 | Py_RETURN_NONE; |
| 4147 | } |
| 4148 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4149 | static int |
| 4150 | _servername_callback(SSL *s, int *al, void *args) |
| 4151 | { |
| 4152 | int ret; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4153 | PySSLContext *sslctx = (PySSLContext *) args; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4154 | PySSLSocket *ssl; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4155 | PyObject *result; |
| 4156 | /* The high-level ssl.SSLSocket object */ |
| 4157 | PyObject *ssl_socket; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4158 | const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); |
Stefan Krah | 20d6080 | 2013-01-17 17:07:17 +0100 | [diff] [blame] | 4159 | PyGILState_STATE gstate = PyGILState_Ensure(); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4160 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4161 | if (sslctx->set_sni_cb == NULL) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4162 | /* remove race condition in this the call back while if removing the |
| 4163 | * callback is in progress */ |
| 4164 | PyGILState_Release(gstate); |
Antoine Pitrou | 5dd12a5 | 2013-01-06 15:25:36 +0100 | [diff] [blame] | 4165 | return SSL_TLSEXT_ERR_OK; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4166 | } |
| 4167 | |
| 4168 | ssl = SSL_get_app_data(s); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4169 | assert(Py_IS_TYPE(ssl, get_state_ctx(sslctx)->PySSLSocket_Type)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4170 | |
Serhiy Storchaka | f51d715 | 2015-11-02 14:40:41 +0200 | [diff] [blame] | 4171 | /* The servername callback expects an argument that represents the current |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4172 | * SSL connection and that has a .context attribute that can be changed to |
| 4173 | * identify the requested hostname. Since the official API is the Python |
| 4174 | * level API we want to pass the callback a Python level object rather than |
| 4175 | * a _ssl.SSLSocket instance. If there's an "owner" (typically an |
| 4176 | * SSLObject) that will be passed. Otherwise if there's a socket then that |
| 4177 | * will be passed. If both do not exist only then the C-level object is |
| 4178 | * passed. */ |
| 4179 | if (ssl->owner) |
| 4180 | ssl_socket = PyWeakref_GetObject(ssl->owner); |
| 4181 | else if (ssl->Socket) |
| 4182 | ssl_socket = PyWeakref_GetObject(ssl->Socket); |
| 4183 | else |
| 4184 | ssl_socket = (PyObject *) ssl; |
| 4185 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4186 | Py_INCREF(ssl_socket); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4187 | if (ssl_socket == Py_None) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4188 | goto error; |
Victor Stinner | 7e00151 | 2013-06-25 00:44:31 +0200 | [diff] [blame] | 4189 | |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4190 | if (servername == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4191 | result = PyObject_CallFunctionObjArgs(sslctx->set_sni_cb, ssl_socket, |
| 4192 | Py_None, sslctx, NULL); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4193 | } |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4194 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4195 | PyObject *servername_bytes; |
| 4196 | PyObject *servername_str; |
| 4197 | |
| 4198 | servername_bytes = PyBytes_FromString(servername); |
| 4199 | if (servername_bytes == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4200 | PyErr_WriteUnraisable((PyObject *) sslctx); |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4201 | goto error; |
| 4202 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4203 | /* server_hostname was encoded to an A-label by our caller; put it |
| 4204 | * back into a str object, but still as an A-label (bpo-28414) |
| 4205 | */ |
| 4206 | servername_str = PyUnicode_FromEncodedObject(servername_bytes, "ascii", NULL); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4207 | if (servername_str == NULL) { |
| 4208 | PyErr_WriteUnraisable(servername_bytes); |
Zackery Spytz | ee96f32 | 2020-07-09 04:00:21 -0600 | [diff] [blame] | 4209 | Py_DECREF(servername_bytes); |
Antoine Pitrou | 50b24d0 | 2013-04-11 20:48:42 +0200 | [diff] [blame] | 4210 | goto error; |
| 4211 | } |
Zackery Spytz | ee96f32 | 2020-07-09 04:00:21 -0600 | [diff] [blame] | 4212 | Py_DECREF(servername_bytes); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4213 | result = PyObject_CallFunctionObjArgs( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4214 | sslctx->set_sni_cb, ssl_socket, servername_str, |
| 4215 | sslctx, NULL); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4216 | Py_DECREF(servername_str); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4217 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4218 | Py_DECREF(ssl_socket); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4219 | |
| 4220 | if (result == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4221 | PyErr_WriteUnraisable(sslctx->set_sni_cb); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4222 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 4223 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4224 | } |
| 4225 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4226 | /* Result may be None, a SSLContext or an integer |
| 4227 | * None and SSLContext are OK, integer or other values are an error. |
| 4228 | */ |
| 4229 | if (result == Py_None) { |
| 4230 | ret = SSL_TLSEXT_ERR_OK; |
| 4231 | } else { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4232 | *al = (int) PyLong_AsLong(result); |
| 4233 | if (PyErr_Occurred()) { |
| 4234 | PyErr_WriteUnraisable(result); |
| 4235 | *al = SSL_AD_INTERNAL_ERROR; |
| 4236 | } |
| 4237 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4238 | } |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4239 | Py_DECREF(result); |
| 4240 | } |
| 4241 | |
| 4242 | PyGILState_Release(gstate); |
| 4243 | return ret; |
| 4244 | |
| 4245 | error: |
| 4246 | Py_DECREF(ssl_socket); |
| 4247 | *al = SSL_AD_INTERNAL_ERROR; |
| 4248 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4249 | PyGILState_Release(gstate); |
| 4250 | return ret; |
| 4251 | } |
| 4252 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4253 | static PyObject * |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4254 | get_sni_callback(PySSLContext *self, void *c) |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4255 | { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4256 | PyObject *cb = self->set_sni_cb; |
| 4257 | if (cb == NULL) { |
| 4258 | Py_RETURN_NONE; |
| 4259 | } |
| 4260 | Py_INCREF(cb); |
| 4261 | return cb; |
| 4262 | } |
| 4263 | |
| 4264 | static int |
| 4265 | set_sni_callback(PySSLContext *self, PyObject *arg, void *c) |
| 4266 | { |
| 4267 | if (self->protocol == PY_SSL_VERSION_TLS_CLIENT) { |
| 4268 | PyErr_SetString(PyExc_ValueError, |
| 4269 | "sni_callback cannot be set on TLS_CLIENT context"); |
| 4270 | return -1; |
| 4271 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4272 | Py_CLEAR(self->set_sni_cb); |
| 4273 | if (arg == Py_None) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4274 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 4275 | } |
| 4276 | else { |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4277 | if (!PyCallable_Check(arg)) { |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4278 | SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 4279 | PyErr_SetString(PyExc_TypeError, |
| 4280 | "not a callable object"); |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4281 | return -1; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4282 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4283 | Py_INCREF(arg); |
| 4284 | self->set_sni_cb = arg; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4285 | SSL_CTX_set_tlsext_servername_callback(self->ctx, _servername_callback); |
| 4286 | SSL_CTX_set_tlsext_servername_arg(self->ctx, self); |
| 4287 | } |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4288 | return 0; |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 4289 | } |
| 4290 | |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4291 | PyDoc_STRVAR(PySSLContext_sni_callback_doc, |
| 4292 | "Set a callback that will be called when a server name is provided by the SSL/TLS client in the SNI extension.\n\ |
| 4293 | \n\ |
| 4294 | If the argument is None then the callback is disabled. The method is called\n\ |
| 4295 | with the SSLSocket, the server name as a string, and the SSLContext object.\n\ |
| 4296 | See RFC 6066 for details of the SNI extension."); |
| 4297 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4298 | /*[clinic input] |
| 4299 | _ssl._SSLContext.cert_store_stats |
| 4300 | |
| 4301 | Returns quantities of loaded X.509 certificates. |
| 4302 | |
| 4303 | X.509 certificates with a CA extension and certificate revocation lists |
| 4304 | inside the context's cert store. |
| 4305 | |
| 4306 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 4307 | been used at least once. |
| 4308 | [clinic start generated code]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4309 | |
| 4310 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4311 | _ssl__SSLContext_cert_store_stats_impl(PySSLContext *self) |
| 4312 | /*[clinic end generated code: output=5f356f4d9cca874d input=eb40dd0f6d0e40cf]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4313 | { |
| 4314 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4315 | STACK_OF(X509_OBJECT) *objs; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4316 | X509_OBJECT *obj; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4317 | int x509 = 0, crl = 0, ca = 0, i; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4318 | |
| 4319 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4320 | objs = X509_STORE_get0_objects(store); |
| 4321 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 4322 | obj = sk_X509_OBJECT_value(objs, i); |
| 4323 | switch (X509_OBJECT_get_type(obj)) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4324 | case X509_LU_X509: |
| 4325 | x509++; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4326 | if (X509_check_ca(X509_OBJECT_get0_X509(obj))) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4327 | ca++; |
| 4328 | } |
| 4329 | break; |
| 4330 | case X509_LU_CRL: |
| 4331 | crl++; |
| 4332 | break; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4333 | default: |
| 4334 | /* Ignore X509_LU_FAIL, X509_LU_RETRY, X509_LU_PKEY. |
| 4335 | * As far as I can tell they are internal states and never |
| 4336 | * stored in a cert store */ |
| 4337 | break; |
| 4338 | } |
| 4339 | } |
| 4340 | return Py_BuildValue("{sisisi}", "x509", x509, "crl", crl, |
| 4341 | "x509_ca", ca); |
| 4342 | } |
| 4343 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4344 | /*[clinic input] |
| 4345 | _ssl._SSLContext.get_ca_certs |
| 4346 | binary_form: bool = False |
| 4347 | |
| 4348 | Returns a list of dicts with information of loaded CA certs. |
| 4349 | |
| 4350 | If the optional argument is True, returns a DER-encoded copy of the CA |
| 4351 | certificate. |
| 4352 | |
| 4353 | NOTE: Certificates in a capath directory aren't loaded unless they have |
| 4354 | been used at least once. |
| 4355 | [clinic start generated code]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4356 | |
| 4357 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4358 | _ssl__SSLContext_get_ca_certs_impl(PySSLContext *self, int binary_form) |
| 4359 | /*[clinic end generated code: output=0d58f148f37e2938 input=6887b5a09b7f9076]*/ |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4360 | { |
| 4361 | X509_STORE *store; |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4362 | STACK_OF(X509_OBJECT) *objs; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4363 | PyObject *ci = NULL, *rlist = NULL; |
| 4364 | int i; |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4365 | |
| 4366 | if ((rlist = PyList_New(0)) == NULL) { |
| 4367 | return NULL; |
| 4368 | } |
| 4369 | |
| 4370 | store = SSL_CTX_get_cert_store(self->ctx); |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4371 | objs = X509_STORE_get0_objects(store); |
| 4372 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4373 | X509_OBJECT *obj; |
| 4374 | X509 *cert; |
| 4375 | |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4376 | obj = sk_X509_OBJECT_value(objs, i); |
| 4377 | if (X509_OBJECT_get_type(obj) != X509_LU_X509) { |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4378 | /* not a x509 cert */ |
| 4379 | continue; |
| 4380 | } |
| 4381 | /* CA for any purpose */ |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 4382 | cert = X509_OBJECT_get0_X509(obj); |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4383 | if (!X509_check_ca(cert)) { |
| 4384 | continue; |
| 4385 | } |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4386 | if (binary_form) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4387 | ci = _certificate_to_der(get_state_ctx(self), cert); |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4388 | } else { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4389 | ci = _decode_certificate(get_state_ctx(self), cert); |
Christian Heimes | 9a5395a | 2013-06-17 15:44:12 +0200 | [diff] [blame] | 4390 | } |
| 4391 | if (ci == NULL) { |
| 4392 | goto error; |
| 4393 | } |
| 4394 | if (PyList_Append(rlist, ci) == -1) { |
| 4395 | goto error; |
| 4396 | } |
| 4397 | Py_CLEAR(ci); |
| 4398 | } |
| 4399 | return rlist; |
| 4400 | |
| 4401 | error: |
| 4402 | Py_XDECREF(ci); |
| 4403 | Py_XDECREF(rlist); |
| 4404 | return NULL; |
| 4405 | } |
| 4406 | |
| 4407 | |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4408 | static PyGetSetDef context_getsetlist[] = { |
Christian Heimes | 1aa9a75 | 2013-12-02 02:41:19 +0100 | [diff] [blame] | 4409 | {"check_hostname", (getter) get_check_hostname, |
| 4410 | (setter) set_check_hostname, NULL}, |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 4411 | {"_host_flags", (getter) get_host_flags, |
| 4412 | (setter) set_host_flags, NULL}, |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4413 | {"minimum_version", (getter) get_minimum_version, |
| 4414 | (setter) set_minimum_version, NULL}, |
| 4415 | {"maximum_version", (getter) get_maximum_version, |
| 4416 | (setter) set_maximum_version, NULL}, |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 4417 | {"keylog_filename", (getter) _PySSLContext_get_keylog_filename, |
| 4418 | (setter) _PySSLContext_set_keylog_filename, NULL}, |
Christian Heimes | c7f7069 | 2019-05-31 11:44:05 +0200 | [diff] [blame] | 4419 | {"_msg_callback", (getter) _PySSLContext_get_msg_callback, |
| 4420 | (setter) _PySSLContext_set_msg_callback, NULL}, |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4421 | {"sni_callback", (getter) get_sni_callback, |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 4422 | (setter) set_sni_callback, PySSLContext_sni_callback_doc}, |
Christian Heimes | 39258d3 | 2021-04-17 11:36:35 +0200 | [diff] [blame] | 4423 | #ifdef TLS1_3_VERSION |
Christian Heimes | 78c7d52 | 2019-06-03 21:00:10 +0200 | [diff] [blame] | 4424 | {"num_tickets", (getter) get_num_tickets, |
| 4425 | (setter) set_num_tickets, PySSLContext_num_tickets_doc}, |
| 4426 | #endif |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 4427 | {"options", (getter) get_options, |
| 4428 | (setter) set_options, NULL}, |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 4429 | {"post_handshake_auth", (getter) get_post_handshake_auth, |
| 4430 | #ifdef TLS1_3_VERSION |
| 4431 | (setter) set_post_handshake_auth, |
| 4432 | #else |
| 4433 | NULL, |
| 4434 | #endif |
| 4435 | NULL}, |
Christian Heimes | 11a1493 | 2018-02-24 02:35:08 +0100 | [diff] [blame] | 4436 | {"protocol", (getter) get_protocol, |
| 4437 | NULL, NULL}, |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 4438 | {"verify_flags", (getter) get_verify_flags, |
| 4439 | (setter) set_verify_flags, NULL}, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4440 | {"verify_mode", (getter) get_verify_mode, |
| 4441 | (setter) set_verify_mode, NULL}, |
matthewhughes934 | 8e836bb | 2020-07-17 09:59:15 +0100 | [diff] [blame] | 4442 | {"security_level", (getter) get_security_level, |
| 4443 | NULL, PySSLContext_security_level_doc}, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4444 | {NULL}, /* sentinel */ |
| 4445 | }; |
| 4446 | |
| 4447 | static struct PyMethodDef context_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4448 | _SSL__SSLCONTEXT__WRAP_SOCKET_METHODDEF |
| 4449 | _SSL__SSLCONTEXT__WRAP_BIO_METHODDEF |
| 4450 | _SSL__SSLCONTEXT_SET_CIPHERS_METHODDEF |
| 4451 | _SSL__SSLCONTEXT__SET_ALPN_PROTOCOLS_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4452 | _SSL__SSLCONTEXT_LOAD_CERT_CHAIN_METHODDEF |
| 4453 | _SSL__SSLCONTEXT_LOAD_DH_PARAMS_METHODDEF |
| 4454 | _SSL__SSLCONTEXT_LOAD_VERIFY_LOCATIONS_METHODDEF |
| 4455 | _SSL__SSLCONTEXT_SESSION_STATS_METHODDEF |
| 4456 | _SSL__SSLCONTEXT_SET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 4457 | _SSL__SSLCONTEXT_SET_ECDH_CURVE_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4458 | _SSL__SSLCONTEXT_CERT_STORE_STATS_METHODDEF |
| 4459 | _SSL__SSLCONTEXT_GET_CA_CERTS_METHODDEF |
Christian Heimes | 25bfcd5 | 2016-09-06 00:04:45 +0200 | [diff] [blame] | 4460 | _SSL__SSLCONTEXT_GET_CIPHERS_METHODDEF |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4461 | {NULL, NULL} /* sentinel */ |
| 4462 | }; |
| 4463 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4464 | static PyType_Slot PySSLContext_slots[] = { |
| 4465 | {Py_tp_methods, context_methods}, |
| 4466 | {Py_tp_getset, context_getsetlist}, |
| 4467 | {Py_tp_new, _ssl__SSLContext}, |
| 4468 | {Py_tp_dealloc, context_dealloc}, |
| 4469 | {Py_tp_traverse, context_traverse}, |
| 4470 | {Py_tp_clear, context_clear}, |
| 4471 | {0, 0}, |
| 4472 | }; |
| 4473 | |
| 4474 | static PyType_Spec PySSLContext_spec = { |
| 4475 | "_ssl._SSLContext", |
| 4476 | sizeof(PySSLContext), |
| 4477 | 0, |
| 4478 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4479 | PySSLContext_slots, |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4480 | }; |
| 4481 | |
| 4482 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4483 | /* |
| 4484 | * MemoryBIO objects |
| 4485 | */ |
| 4486 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4487 | /*[clinic input] |
| 4488 | @classmethod |
| 4489 | _ssl.MemoryBIO.__new__ |
| 4490 | |
| 4491 | [clinic start generated code]*/ |
| 4492 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4493 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4494 | _ssl_MemoryBIO_impl(PyTypeObject *type) |
| 4495 | /*[clinic end generated code: output=8820a58db78330ac input=26d22e4909ecb1b5]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4496 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4497 | BIO *bio; |
| 4498 | PySSLMemoryBIO *self; |
| 4499 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4500 | bio = BIO_new(BIO_s_mem()); |
| 4501 | if (bio == NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4502 | PyErr_SetString(PyExc_MemoryError, "failed to allocate BIO"); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4503 | return NULL; |
| 4504 | } |
| 4505 | /* Since our BIO is non-blocking an empty read() does not indicate EOF, |
| 4506 | * just that no data is currently available. The SSL routines should retry |
| 4507 | * the read, which we can achieve by calling BIO_set_retry_read(). */ |
| 4508 | BIO_set_retry_read(bio); |
| 4509 | BIO_set_mem_eof_return(bio, -1); |
| 4510 | |
| 4511 | assert(type != NULL && type->tp_alloc != NULL); |
| 4512 | self = (PySSLMemoryBIO *) type->tp_alloc(type, 0); |
| 4513 | if (self == NULL) { |
| 4514 | BIO_free(bio); |
| 4515 | return NULL; |
| 4516 | } |
| 4517 | self->bio = bio; |
| 4518 | self->eof_written = 0; |
| 4519 | |
| 4520 | return (PyObject *) self; |
| 4521 | } |
| 4522 | |
| 4523 | static void |
| 4524 | memory_bio_dealloc(PySSLMemoryBIO *self) |
| 4525 | { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4526 | PyTypeObject *tp = Py_TYPE(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4527 | BIO_free(self->bio); |
| 4528 | Py_TYPE(self)->tp_free(self); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4529 | Py_DECREF(tp); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | static PyObject * |
| 4533 | memory_bio_get_pending(PySSLMemoryBIO *self, void *c) |
| 4534 | { |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4535 | return PyLong_FromSize_t(BIO_ctrl_pending(self->bio)); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4536 | } |
| 4537 | |
| 4538 | PyDoc_STRVAR(PySSL_memory_bio_pending_doc, |
| 4539 | "The number of bytes pending in the memory BIO."); |
| 4540 | |
| 4541 | static PyObject * |
| 4542 | memory_bio_get_eof(PySSLMemoryBIO *self, void *c) |
| 4543 | { |
| 4544 | return PyBool_FromLong((BIO_ctrl_pending(self->bio) == 0) |
| 4545 | && self->eof_written); |
| 4546 | } |
| 4547 | |
| 4548 | PyDoc_STRVAR(PySSL_memory_bio_eof_doc, |
| 4549 | "Whether the memory BIO is at EOF."); |
| 4550 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4551 | /*[clinic input] |
| 4552 | _ssl.MemoryBIO.read |
| 4553 | size as len: int = -1 |
| 4554 | / |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4555 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4556 | Read up to size bytes from the memory BIO. |
| 4557 | |
| 4558 | If size is not specified, read the entire buffer. |
| 4559 | If the return value is an empty bytes instance, this means either |
| 4560 | EOF or that no data is available. Use the "eof" property to |
| 4561 | distinguish between the two. |
| 4562 | [clinic start generated code]*/ |
| 4563 | |
| 4564 | static PyObject * |
| 4565 | _ssl_MemoryBIO_read_impl(PySSLMemoryBIO *self, int len) |
| 4566 | /*[clinic end generated code: output=a657aa1e79cd01b3 input=574d7be06a902366]*/ |
| 4567 | { |
| 4568 | int avail, nbytes; |
| 4569 | PyObject *result; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4570 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4571 | avail = (int)Py_MIN(BIO_ctrl_pending(self->bio), INT_MAX); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4572 | if ((len < 0) || (len > avail)) |
| 4573 | len = avail; |
| 4574 | |
| 4575 | result = PyBytes_FromStringAndSize(NULL, len); |
| 4576 | if ((result == NULL) || (len == 0)) |
| 4577 | return result; |
| 4578 | |
| 4579 | nbytes = BIO_read(self->bio, PyBytes_AS_STRING(result), len); |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4580 | if (nbytes < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4581 | _sslmodulestate *state = get_state_mbio(self); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4582 | Py_DECREF(result); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4583 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4584 | return NULL; |
| 4585 | } |
| 4586 | |
Zackery Spytz | 365ad2e | 2018-10-06 11:41:45 -0600 | [diff] [blame] | 4587 | /* There should never be any short reads but check anyway. */ |
| 4588 | if (nbytes < len) { |
| 4589 | _PyBytes_Resize(&result, nbytes); |
| 4590 | } |
| 4591 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4592 | return result; |
| 4593 | } |
| 4594 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4595 | /*[clinic input] |
| 4596 | _ssl.MemoryBIO.write |
| 4597 | b: Py_buffer |
| 4598 | / |
| 4599 | |
| 4600 | Writes the bytes b into the memory BIO. |
| 4601 | |
| 4602 | Returns the number of bytes written. |
| 4603 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4604 | |
| 4605 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4606 | _ssl_MemoryBIO_write_impl(PySSLMemoryBIO *self, Py_buffer *b) |
| 4607 | /*[clinic end generated code: output=156ec59110d75935 input=e45757b3e17c4808]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4608 | { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4609 | int nbytes; |
| 4610 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4611 | if (b->len > INT_MAX) { |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4612 | PyErr_Format(PyExc_OverflowError, |
| 4613 | "string longer than %d bytes", INT_MAX); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4614 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | if (self->eof_written) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4618 | PyObject *module = PyType_GetModule(Py_TYPE(self)); |
| 4619 | if (module == NULL) |
| 4620 | return NULL; |
| 4621 | PyErr_SetString(get_ssl_state(module)->PySSLErrorObject, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4622 | "cannot write() after write_eof()"); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4623 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4624 | } |
| 4625 | |
Segev Finer | 5cff637 | 2017-07-27 01:19:17 +0300 | [diff] [blame] | 4626 | nbytes = BIO_write(self->bio, b->buf, (int)b->len); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4627 | if (nbytes < 0) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4628 | _sslmodulestate *state = get_state_mbio(self); |
| 4629 | _setSSLError(state, NULL, 0, __FILE__, __LINE__); |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4630 | return NULL; |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4631 | } |
| 4632 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4633 | return PyLong_FromLong(nbytes); |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4634 | } |
| 4635 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4636 | /*[clinic input] |
| 4637 | _ssl.MemoryBIO.write_eof |
| 4638 | |
| 4639 | Write an EOF marker to the memory BIO. |
| 4640 | |
| 4641 | When all data has been read, the "eof" property will be True. |
| 4642 | [clinic start generated code]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4643 | |
| 4644 | static PyObject * |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4645 | _ssl_MemoryBIO_write_eof_impl(PySSLMemoryBIO *self) |
| 4646 | /*[clinic end generated code: output=d4106276ccd1ed34 input=56a945f1d29e8bd6]*/ |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4647 | { |
| 4648 | self->eof_written = 1; |
| 4649 | /* After an EOF is written, a zero return from read() should be a real EOF |
| 4650 | * i.e. it should not be retried. Clear the SHOULD_RETRY flag. */ |
| 4651 | BIO_clear_retry_flags(self->bio); |
| 4652 | BIO_set_mem_eof_return(self->bio, 0); |
| 4653 | |
| 4654 | Py_RETURN_NONE; |
| 4655 | } |
| 4656 | |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4657 | static PyGetSetDef memory_bio_getsetlist[] = { |
| 4658 | {"pending", (getter) memory_bio_get_pending, NULL, |
| 4659 | PySSL_memory_bio_pending_doc}, |
| 4660 | {"eof", (getter) memory_bio_get_eof, NULL, |
| 4661 | PySSL_memory_bio_eof_doc}, |
| 4662 | {NULL}, /* sentinel */ |
| 4663 | }; |
| 4664 | |
| 4665 | static struct PyMethodDef memory_bio_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4666 | _SSL_MEMORYBIO_READ_METHODDEF |
| 4667 | _SSL_MEMORYBIO_WRITE_METHODDEF |
| 4668 | _SSL_MEMORYBIO_WRITE_EOF_METHODDEF |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4669 | {NULL, NULL} /* sentinel */ |
| 4670 | }; |
| 4671 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4672 | static PyType_Slot PySSLMemoryBIO_slots[] = { |
| 4673 | {Py_tp_methods, memory_bio_methods}, |
| 4674 | {Py_tp_getset, memory_bio_getsetlist}, |
| 4675 | {Py_tp_new, _ssl_MemoryBIO}, |
| 4676 | {Py_tp_dealloc, memory_bio_dealloc}, |
| 4677 | {0, 0}, |
Antoine Pitrou | b1fdf47 | 2014-10-05 20:41:53 +0200 | [diff] [blame] | 4678 | }; |
| 4679 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4680 | static PyType_Spec PySSLMemoryBIO_spec = { |
| 4681 | "_ssl.MemoryBIO", |
| 4682 | sizeof(PySSLMemoryBIO), |
| 4683 | 0, |
| 4684 | Py_TPFLAGS_DEFAULT, |
| 4685 | PySSLMemoryBIO_slots, |
| 4686 | }; |
Antoine Pitrou | 152efa2 | 2010-05-16 18:19:27 +0000 | [diff] [blame] | 4687 | |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4688 | /* |
| 4689 | * SSL Session object |
| 4690 | */ |
| 4691 | |
| 4692 | static void |
| 4693 | PySSLSession_dealloc(PySSLSession *self) |
| 4694 | { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4695 | PyTypeObject *tp = Py_TYPE(self); |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 4696 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 4697 | PyObject_GC_UnTrack(self); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4698 | Py_XDECREF(self->ctx); |
| 4699 | if (self->session != NULL) { |
| 4700 | SSL_SESSION_free(self->session); |
| 4701 | } |
Christian Heimes | a5d0765 | 2016-09-24 10:48:05 +0200 | [diff] [blame] | 4702 | PyObject_GC_Del(self); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4703 | Py_DECREF(tp); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4704 | } |
| 4705 | |
| 4706 | static PyObject * |
| 4707 | PySSLSession_richcompare(PyObject *left, PyObject *right, int op) |
| 4708 | { |
| 4709 | int result; |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4710 | PyTypeObject *sesstype = ((PySSLSession*)left)->ctx->state->PySSLSession_Type; |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4711 | |
| 4712 | if (left == NULL || right == NULL) { |
| 4713 | PyErr_BadInternalCall(); |
| 4714 | return NULL; |
| 4715 | } |
| 4716 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4717 | if (!Py_IS_TYPE(left, sesstype) || !Py_IS_TYPE(right, sesstype)) { |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4718 | Py_RETURN_NOTIMPLEMENTED; |
| 4719 | } |
| 4720 | |
| 4721 | if (left == right) { |
| 4722 | result = 0; |
| 4723 | } else { |
| 4724 | const unsigned char *left_id, *right_id; |
| 4725 | unsigned int left_len, right_len; |
| 4726 | left_id = SSL_SESSION_get_id(((PySSLSession *)left)->session, |
| 4727 | &left_len); |
| 4728 | right_id = SSL_SESSION_get_id(((PySSLSession *)right)->session, |
| 4729 | &right_len); |
| 4730 | if (left_len == right_len) { |
| 4731 | result = memcmp(left_id, right_id, left_len); |
| 4732 | } else { |
| 4733 | result = 1; |
| 4734 | } |
| 4735 | } |
| 4736 | |
| 4737 | switch (op) { |
| 4738 | case Py_EQ: |
| 4739 | if (result == 0) { |
| 4740 | Py_RETURN_TRUE; |
| 4741 | } else { |
| 4742 | Py_RETURN_FALSE; |
| 4743 | } |
| 4744 | break; |
| 4745 | case Py_NE: |
| 4746 | if (result != 0) { |
| 4747 | Py_RETURN_TRUE; |
| 4748 | } else { |
| 4749 | Py_RETURN_FALSE; |
| 4750 | } |
| 4751 | break; |
| 4752 | case Py_LT: |
| 4753 | case Py_LE: |
| 4754 | case Py_GT: |
| 4755 | case Py_GE: |
| 4756 | Py_RETURN_NOTIMPLEMENTED; |
| 4757 | break; |
| 4758 | default: |
| 4759 | PyErr_BadArgument(); |
| 4760 | return NULL; |
| 4761 | } |
| 4762 | } |
| 4763 | |
| 4764 | static int |
| 4765 | PySSLSession_traverse(PySSLSession *self, visitproc visit, void *arg) |
| 4766 | { |
| 4767 | Py_VISIT(self->ctx); |
| 4768 | return 0; |
| 4769 | } |
| 4770 | |
| 4771 | static int |
| 4772 | PySSLSession_clear(PySSLSession *self) |
| 4773 | { |
| 4774 | Py_CLEAR(self->ctx); |
| 4775 | return 0; |
| 4776 | } |
| 4777 | |
| 4778 | |
| 4779 | static PyObject * |
| 4780 | PySSLSession_get_time(PySSLSession *self, void *closure) { |
| 4781 | return PyLong_FromLong(SSL_SESSION_get_time(self->session)); |
| 4782 | } |
| 4783 | |
| 4784 | PyDoc_STRVAR(PySSLSession_get_time_doc, |
| 4785 | "Session creation time (seconds since epoch)."); |
| 4786 | |
| 4787 | |
| 4788 | static PyObject * |
| 4789 | PySSLSession_get_timeout(PySSLSession *self, void *closure) { |
| 4790 | return PyLong_FromLong(SSL_SESSION_get_timeout(self->session)); |
| 4791 | } |
| 4792 | |
| 4793 | PyDoc_STRVAR(PySSLSession_get_timeout_doc, |
| 4794 | "Session timeout (delta in seconds)."); |
| 4795 | |
| 4796 | |
| 4797 | static PyObject * |
| 4798 | PySSLSession_get_ticket_lifetime_hint(PySSLSession *self, void *closure) { |
| 4799 | unsigned long hint = SSL_SESSION_get_ticket_lifetime_hint(self->session); |
| 4800 | return PyLong_FromUnsignedLong(hint); |
| 4801 | } |
| 4802 | |
| 4803 | PyDoc_STRVAR(PySSLSession_get_ticket_lifetime_hint_doc, |
| 4804 | "Ticket life time hint."); |
| 4805 | |
| 4806 | |
| 4807 | static PyObject * |
| 4808 | PySSLSession_get_session_id(PySSLSession *self, void *closure) { |
| 4809 | const unsigned char *id; |
| 4810 | unsigned int len; |
| 4811 | id = SSL_SESSION_get_id(self->session, &len); |
| 4812 | return PyBytes_FromStringAndSize((const char *)id, len); |
| 4813 | } |
| 4814 | |
| 4815 | PyDoc_STRVAR(PySSLSession_get_session_id_doc, |
| 4816 | "Session id"); |
| 4817 | |
| 4818 | |
| 4819 | static PyObject * |
| 4820 | PySSLSession_get_has_ticket(PySSLSession *self, void *closure) { |
| 4821 | if (SSL_SESSION_has_ticket(self->session)) { |
| 4822 | Py_RETURN_TRUE; |
| 4823 | } else { |
| 4824 | Py_RETURN_FALSE; |
| 4825 | } |
| 4826 | } |
| 4827 | |
| 4828 | PyDoc_STRVAR(PySSLSession_get_has_ticket_doc, |
| 4829 | "Does the session contain a ticket?"); |
| 4830 | |
| 4831 | |
| 4832 | static PyGetSetDef PySSLSession_getsetlist[] = { |
| 4833 | {"has_ticket", (getter) PySSLSession_get_has_ticket, NULL, |
| 4834 | PySSLSession_get_has_ticket_doc}, |
| 4835 | {"id", (getter) PySSLSession_get_session_id, NULL, |
| 4836 | PySSLSession_get_session_id_doc}, |
| 4837 | {"ticket_lifetime_hint", (getter) PySSLSession_get_ticket_lifetime_hint, |
| 4838 | NULL, PySSLSession_get_ticket_lifetime_hint_doc}, |
| 4839 | {"time", (getter) PySSLSession_get_time, NULL, |
| 4840 | PySSLSession_get_time_doc}, |
| 4841 | {"timeout", (getter) PySSLSession_get_timeout, NULL, |
| 4842 | PySSLSession_get_timeout_doc}, |
| 4843 | {NULL}, /* sentinel */ |
| 4844 | }; |
| 4845 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 4846 | static PyType_Slot PySSLSession_slots[] = { |
| 4847 | {Py_tp_getset,PySSLSession_getsetlist}, |
| 4848 | {Py_tp_richcompare, PySSLSession_richcompare}, |
| 4849 | {Py_tp_dealloc, PySSLSession_dealloc}, |
| 4850 | {Py_tp_traverse, PySSLSession_traverse}, |
| 4851 | {Py_tp_clear, PySSLSession_clear}, |
| 4852 | {0, 0}, |
| 4853 | }; |
| 4854 | |
| 4855 | static PyType_Spec PySSLSession_spec = { |
| 4856 | "_ssl.SSLSession", |
| 4857 | sizeof(PySSLSession), |
| 4858 | 0, |
| 4859 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 4860 | PySSLSession_slots, |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 4861 | }; |
| 4862 | |
| 4863 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4864 | /* helper routines for seeding the SSL PRNG */ |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4865 | /*[clinic input] |
| 4866 | _ssl.RAND_add |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 4867 | string as view: Py_buffer(accept={str, buffer}) |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4868 | entropy: double |
| 4869 | / |
| 4870 | |
| 4871 | Mix string into the OpenSSL PRNG state. |
| 4872 | |
| 4873 | entropy (a float) is a lower bound on the entropy contained in |
Chandan Kumar | 63c2c8a | 2017-06-09 15:13:58 +0530 | [diff] [blame] | 4874 | string. See RFC 4086. |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4875 | [clinic start generated code]*/ |
| 4876 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4877 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4878 | _ssl_RAND_add_impl(PyObject *module, Py_buffer *view, double entropy) |
Serhiy Storchaka | 5f31d5c | 2017-06-10 13:13:51 +0300 | [diff] [blame] | 4879 | /*[clinic end generated code: output=e6dd48df9c9024e9 input=5c33017422828f5c]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4880 | { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 4881 | const char *buf; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 4882 | Py_ssize_t len, written; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4883 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4884 | buf = (const char *)view->buf; |
| 4885 | len = view->len; |
Victor Stinner | 2e57b4e | 2014-07-01 16:37:17 +0200 | [diff] [blame] | 4886 | do { |
| 4887 | written = Py_MIN(len, INT_MAX); |
| 4888 | RAND_add(buf, (int)written, entropy); |
| 4889 | buf += written; |
| 4890 | len -= written; |
| 4891 | } while (len); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 4892 | Py_RETURN_NONE; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4893 | } |
| 4894 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4895 | static PyObject * |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4896 | PySSL_RAND(PyObject *module, int len, int pseudo) |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4897 | { |
| 4898 | int ok; |
| 4899 | PyObject *bytes; |
| 4900 | unsigned long err; |
| 4901 | const char *errstr; |
| 4902 | PyObject *v; |
| 4903 | |
Victor Stinner | 1e81a39 | 2013-12-19 16:47:04 +0100 | [diff] [blame] | 4904 | if (len < 0) { |
| 4905 | PyErr_SetString(PyExc_ValueError, "num must be positive"); |
| 4906 | return NULL; |
| 4907 | } |
| 4908 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4909 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 4910 | if (bytes == NULL) |
| 4911 | return NULL; |
| 4912 | if (pseudo) { |
Christian Heimes | a871f69 | 2020-06-01 08:58:14 +0200 | [diff] [blame] | 4913 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4914 | if (ok == 0 || ok == 1) |
| 4915 | return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False); |
| 4916 | } |
| 4917 | else { |
| 4918 | ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len); |
| 4919 | if (ok == 1) |
| 4920 | return bytes; |
| 4921 | } |
| 4922 | Py_DECREF(bytes); |
| 4923 | |
| 4924 | err = ERR_get_error(); |
| 4925 | errstr = ERR_reason_error_string(err); |
| 4926 | v = Py_BuildValue("(ks)", err, errstr); |
| 4927 | if (v != NULL) { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4928 | PyErr_SetObject(get_ssl_state(module)->PySSLErrorObject, v); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4929 | Py_DECREF(v); |
| 4930 | } |
| 4931 | return NULL; |
| 4932 | } |
| 4933 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4934 | /*[clinic input] |
| 4935 | _ssl.RAND_bytes |
| 4936 | n: int |
| 4937 | / |
| 4938 | |
| 4939 | Generate n cryptographically strong pseudo-random bytes. |
| 4940 | [clinic start generated code]*/ |
| 4941 | |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4942 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4943 | _ssl_RAND_bytes_impl(PyObject *module, int n) |
| 4944 | /*[clinic end generated code: output=977da635e4838bc7 input=678ddf2872dfebfc]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4945 | { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4946 | return PySSL_RAND(module, n, 0); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4947 | } |
| 4948 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4949 | /*[clinic input] |
| 4950 | _ssl.RAND_pseudo_bytes |
| 4951 | n: int |
| 4952 | / |
| 4953 | |
| 4954 | Generate n pseudo-random bytes. |
| 4955 | |
| 4956 | Return a pair (bytes, is_cryptographic). is_cryptographic is True |
| 4957 | if the bytes generated are cryptographically strong. |
| 4958 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4959 | |
| 4960 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4961 | _ssl_RAND_pseudo_bytes_impl(PyObject *module, int n) |
| 4962 | /*[clinic end generated code: output=b1509e937000e52d input=58312bd53f9bbdd0]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4963 | { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 4964 | return PySSL_RAND(module, n, 1); |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4965 | } |
| 4966 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4967 | /*[clinic input] |
| 4968 | _ssl.RAND_status |
| 4969 | |
| 4970 | Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not. |
| 4971 | |
| 4972 | It is necessary to seed the PRNG with RAND_add() on some platforms before |
| 4973 | using the ssl() function. |
| 4974 | [clinic start generated code]*/ |
Victor Stinner | 99c8b16 | 2011-05-24 12:05:19 +0200 | [diff] [blame] | 4975 | |
| 4976 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4977 | _ssl_RAND_status_impl(PyObject *module) |
| 4978 | /*[clinic end generated code: output=7e0aaa2d39fdc1ad input=8a774b02d1dc81f3]*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4979 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4980 | return PyLong_FromLong(RAND_status()); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 4983 | /*[clinic input] |
| 4984 | _ssl.get_default_verify_paths |
| 4985 | |
| 4986 | Return search paths and environment vars that are used by SSLContext's set_default_verify_paths() to load default CAs. |
| 4987 | |
| 4988 | The values are 'cert_file_env', 'cert_file', 'cert_dir_env', 'cert_dir'. |
| 4989 | [clinic start generated code]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 4990 | |
| 4991 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4992 | _ssl_get_default_verify_paths_impl(PyObject *module) |
| 4993 | /*[clinic end generated code: output=e5b62a466271928b input=5210c953d98c3eb5]*/ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 4994 | { |
| 4995 | PyObject *ofile_env = NULL; |
| 4996 | PyObject *ofile = NULL; |
| 4997 | PyObject *odir_env = NULL; |
| 4998 | PyObject *odir = NULL; |
| 4999 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5000 | #define CONVERT(info, target) { \ |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5001 | const char *tmp = (info); \ |
| 5002 | target = NULL; \ |
| 5003 | if (!tmp) { Py_INCREF(Py_None); target = Py_None; } \ |
| 5004 | else if ((target = PyUnicode_DecodeFSDefault(tmp)) == NULL) { \ |
| 5005 | target = PyBytes_FromString(tmp); } \ |
| 5006 | if (!target) goto error; \ |
Benjamin Peterson | 025a1fd | 2015-11-14 15:12:38 -0800 | [diff] [blame] | 5007 | } |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5008 | |
Benjamin Peterson | d113c96 | 2015-07-18 10:59:13 -0700 | [diff] [blame] | 5009 | CONVERT(X509_get_default_cert_file_env(), ofile_env); |
| 5010 | CONVERT(X509_get_default_cert_file(), ofile); |
| 5011 | CONVERT(X509_get_default_cert_dir_env(), odir_env); |
| 5012 | CONVERT(X509_get_default_cert_dir(), odir); |
| 5013 | #undef CONVERT |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5014 | |
Christian Heimes | 200bb1b | 2013-06-14 15:14:29 +0200 | [diff] [blame] | 5015 | return Py_BuildValue("NNNN", ofile_env, ofile, odir_env, odir); |
Christian Heimes | 6d7ad13 | 2013-06-09 18:02:55 +0200 | [diff] [blame] | 5016 | |
| 5017 | error: |
| 5018 | Py_XDECREF(ofile_env); |
| 5019 | Py_XDECREF(ofile); |
| 5020 | Py_XDECREF(odir_env); |
| 5021 | Py_XDECREF(odir); |
| 5022 | return NULL; |
| 5023 | } |
| 5024 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5025 | static PyObject* |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5026 | asn1obj2py(_sslmodulestate *state, ASN1_OBJECT *obj) |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5027 | { |
| 5028 | int nid; |
| 5029 | const char *ln, *sn; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5030 | |
| 5031 | nid = OBJ_obj2nid(obj); |
| 5032 | if (nid == NID_undef) { |
| 5033 | PyErr_Format(PyExc_ValueError, "Unknown object"); |
| 5034 | return NULL; |
| 5035 | } |
| 5036 | sn = OBJ_nid2sn(nid); |
| 5037 | ln = OBJ_nid2ln(nid); |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5038 | return Py_BuildValue("issN", nid, sn, ln, _asn1obj2py(state, obj, 1)); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5039 | } |
| 5040 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5041 | /*[clinic input] |
| 5042 | _ssl.txt2obj |
| 5043 | txt: str |
| 5044 | name: bool = False |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5045 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5046 | Lookup NID, short name, long name and OID of an ASN1_OBJECT. |
| 5047 | |
| 5048 | By default objects are looked up by OID. With name=True short and |
| 5049 | long name are also matched. |
| 5050 | [clinic start generated code]*/ |
| 5051 | |
| 5052 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5053 | _ssl_txt2obj_impl(PyObject *module, const char *txt, int name) |
| 5054 | /*[clinic end generated code: output=c38e3991347079c1 input=1c1e7d0aa7c48602]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5055 | { |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5056 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5057 | ASN1_OBJECT *obj; |
| 5058 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5059 | obj = OBJ_txt2obj(txt, name ? 0 : 1); |
| 5060 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5061 | PyErr_Format(PyExc_ValueError, "unknown object '%.100s'", txt); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5062 | return NULL; |
| 5063 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5064 | result = asn1obj2py(get_ssl_state(module), obj); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5065 | ASN1_OBJECT_free(obj); |
| 5066 | return result; |
| 5067 | } |
| 5068 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5069 | /*[clinic input] |
| 5070 | _ssl.nid2obj |
| 5071 | nid: int |
| 5072 | / |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5073 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5074 | Lookup NID, short name, long name and OID of an ASN1_OBJECT by NID. |
| 5075 | [clinic start generated code]*/ |
| 5076 | |
| 5077 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5078 | _ssl_nid2obj_impl(PyObject *module, int nid) |
| 5079 | /*[clinic end generated code: output=4a98ab691cd4f84a input=51787a3bee7d8f98]*/ |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5080 | { |
| 5081 | PyObject *result = NULL; |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5082 | ASN1_OBJECT *obj; |
| 5083 | |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5084 | if (nid < NID_undef) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5085 | PyErr_SetString(PyExc_ValueError, "NID must be positive."); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5086 | return NULL; |
| 5087 | } |
| 5088 | obj = OBJ_nid2obj(nid); |
| 5089 | if (obj == NULL) { |
Christian Heimes | 5398e1a | 2013-11-22 16:20:53 +0100 | [diff] [blame] | 5090 | PyErr_Format(PyExc_ValueError, "unknown NID %i", nid); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5091 | return NULL; |
| 5092 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5093 | result = asn1obj2py(get_ssl_state(module), obj); |
Christian Heimes | a6bc95a | 2013-11-17 19:59:14 +0100 | [diff] [blame] | 5094 | ASN1_OBJECT_free(obj); |
| 5095 | return result; |
| 5096 | } |
| 5097 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5098 | #ifdef _MSC_VER |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5099 | |
| 5100 | static PyObject* |
| 5101 | certEncodingType(DWORD encodingType) |
| 5102 | { |
| 5103 | static PyObject *x509_asn = NULL; |
| 5104 | static PyObject *pkcs_7_asn = NULL; |
| 5105 | |
| 5106 | if (x509_asn == NULL) { |
| 5107 | x509_asn = PyUnicode_InternFromString("x509_asn"); |
| 5108 | if (x509_asn == NULL) |
| 5109 | return NULL; |
| 5110 | } |
| 5111 | if (pkcs_7_asn == NULL) { |
| 5112 | pkcs_7_asn = PyUnicode_InternFromString("pkcs_7_asn"); |
| 5113 | if (pkcs_7_asn == NULL) |
| 5114 | return NULL; |
| 5115 | } |
| 5116 | switch(encodingType) { |
| 5117 | case X509_ASN_ENCODING: |
| 5118 | Py_INCREF(x509_asn); |
| 5119 | return x509_asn; |
| 5120 | case PKCS_7_ASN_ENCODING: |
| 5121 | Py_INCREF(pkcs_7_asn); |
| 5122 | return pkcs_7_asn; |
| 5123 | default: |
| 5124 | return PyLong_FromLong(encodingType); |
| 5125 | } |
| 5126 | } |
| 5127 | |
| 5128 | static PyObject* |
| 5129 | parseKeyUsage(PCCERT_CONTEXT pCertCtx, DWORD flags) |
| 5130 | { |
| 5131 | CERT_ENHKEY_USAGE *usage; |
| 5132 | DWORD size, error, i; |
| 5133 | PyObject *retval; |
| 5134 | |
| 5135 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, NULL, &size)) { |
| 5136 | error = GetLastError(); |
| 5137 | if (error == CRYPT_E_NOT_FOUND) { |
| 5138 | Py_RETURN_TRUE; |
| 5139 | } |
| 5140 | return PyErr_SetFromWindowsErr(error); |
| 5141 | } |
| 5142 | |
| 5143 | usage = (CERT_ENHKEY_USAGE*)PyMem_Malloc(size); |
| 5144 | if (usage == NULL) { |
| 5145 | return PyErr_NoMemory(); |
| 5146 | } |
| 5147 | |
| 5148 | /* Now get the actual enhanced usage property */ |
| 5149 | if (!CertGetEnhancedKeyUsage(pCertCtx, flags, usage, &size)) { |
| 5150 | PyMem_Free(usage); |
| 5151 | error = GetLastError(); |
| 5152 | if (error == CRYPT_E_NOT_FOUND) { |
| 5153 | Py_RETURN_TRUE; |
| 5154 | } |
| 5155 | return PyErr_SetFromWindowsErr(error); |
| 5156 | } |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5157 | retval = PyFrozenSet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5158 | if (retval == NULL) { |
| 5159 | goto error; |
| 5160 | } |
| 5161 | for (i = 0; i < usage->cUsageIdentifier; ++i) { |
| 5162 | if (usage->rgpszUsageIdentifier[i]) { |
| 5163 | PyObject *oid; |
| 5164 | int err; |
| 5165 | oid = PyUnicode_FromString(usage->rgpszUsageIdentifier[i]); |
| 5166 | if (oid == NULL) { |
| 5167 | Py_CLEAR(retval); |
| 5168 | goto error; |
| 5169 | } |
| 5170 | err = PySet_Add(retval, oid); |
| 5171 | Py_DECREF(oid); |
| 5172 | if (err == -1) { |
| 5173 | Py_CLEAR(retval); |
| 5174 | goto error; |
| 5175 | } |
| 5176 | } |
| 5177 | } |
| 5178 | error: |
| 5179 | PyMem_Free(usage); |
| 5180 | return retval; |
| 5181 | } |
| 5182 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5183 | static HCERTSTORE |
| 5184 | ssl_collect_certificates(const char *store_name) |
| 5185 | { |
| 5186 | /* this function collects the system certificate stores listed in |
| 5187 | * system_stores into a collection certificate store for being |
| 5188 | * enumerated. The store must be readable to be added to the |
| 5189 | * store collection. |
| 5190 | */ |
| 5191 | |
| 5192 | HCERTSTORE hCollectionStore = NULL, hSystemStore = NULL; |
| 5193 | static DWORD system_stores[] = { |
| 5194 | CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 5195 | CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, |
| 5196 | CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY, |
| 5197 | CERT_SYSTEM_STORE_CURRENT_USER, |
| 5198 | CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY, |
| 5199 | CERT_SYSTEM_STORE_SERVICES, |
| 5200 | CERT_SYSTEM_STORE_USERS}; |
| 5201 | size_t i, storesAdded; |
| 5202 | BOOL result; |
| 5203 | |
| 5204 | hCollectionStore = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, |
| 5205 | (HCRYPTPROV)NULL, 0, NULL); |
| 5206 | if (!hCollectionStore) { |
| 5207 | return NULL; |
| 5208 | } |
| 5209 | storesAdded = 0; |
| 5210 | for (i = 0; i < sizeof(system_stores) / sizeof(DWORD); i++) { |
| 5211 | hSystemStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, |
| 5212 | (HCRYPTPROV)NULL, |
| 5213 | CERT_STORE_READONLY_FLAG | |
| 5214 | system_stores[i], store_name); |
| 5215 | if (hSystemStore) { |
| 5216 | result = CertAddStoreToCollection(hCollectionStore, hSystemStore, |
| 5217 | CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0); |
| 5218 | if (result) { |
| 5219 | ++storesAdded; |
| 5220 | } |
neonene | ed70129 | 2019-09-09 21:33:43 +0900 | [diff] [blame] | 5221 | CertCloseStore(hSystemStore, 0); /* flag must be 0 */ |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5222 | } |
| 5223 | } |
| 5224 | if (storesAdded == 0) { |
| 5225 | CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG); |
| 5226 | return NULL; |
| 5227 | } |
| 5228 | |
| 5229 | return hCollectionStore; |
| 5230 | } |
| 5231 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5232 | /*[clinic input] |
| 5233 | _ssl.enum_certificates |
| 5234 | store_name: str |
| 5235 | |
| 5236 | Retrieve certificates from Windows' cert store. |
| 5237 | |
| 5238 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5239 | more cert storages, too. The function returns a list of (bytes, |
| 5240 | encoding_type, trust) tuples. The encoding_type flag can be interpreted |
| 5241 | with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. The trust setting is either |
| 5242 | a set of OIDs or the boolean True. |
| 5243 | [clinic start generated code]*/ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5244 | |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5245 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5246 | _ssl_enum_certificates_impl(PyObject *module, const char *store_name) |
| 5247 | /*[clinic end generated code: output=5134dc8bb3a3c893 input=915f60d70461ea4e]*/ |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5248 | { |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5249 | HCERTSTORE hCollectionStore = NULL; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5250 | PCCERT_CONTEXT pCertCtx = NULL; |
| 5251 | PyObject *keyusage = NULL, *cert = NULL, *enc = NULL, *tup = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5252 | PyObject *result = NULL; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5253 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5254 | result = PySet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5255 | if (result == NULL) { |
| 5256 | return NULL; |
| 5257 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5258 | hCollectionStore = ssl_collect_certificates(store_name); |
| 5259 | if (hCollectionStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5260 | Py_DECREF(result); |
| 5261 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5262 | } |
| 5263 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5264 | while (pCertCtx = CertEnumCertificatesInStore(hCollectionStore, pCertCtx)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5265 | cert = PyBytes_FromStringAndSize((const char*)pCertCtx->pbCertEncoded, |
| 5266 | pCertCtx->cbCertEncoded); |
| 5267 | if (!cert) { |
| 5268 | Py_CLEAR(result); |
| 5269 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5270 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5271 | if ((enc = certEncodingType(pCertCtx->dwCertEncodingType)) == NULL) { |
| 5272 | Py_CLEAR(result); |
| 5273 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5274 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5275 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG); |
| 5276 | if (keyusage == Py_True) { |
| 5277 | Py_DECREF(keyusage); |
| 5278 | keyusage = parseKeyUsage(pCertCtx, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5279 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5280 | if (keyusage == NULL) { |
| 5281 | Py_CLEAR(result); |
| 5282 | break; |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5283 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5284 | if ((tup = PyTuple_New(3)) == NULL) { |
| 5285 | Py_CLEAR(result); |
| 5286 | break; |
| 5287 | } |
| 5288 | PyTuple_SET_ITEM(tup, 0, cert); |
| 5289 | cert = NULL; |
| 5290 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5291 | enc = NULL; |
| 5292 | PyTuple_SET_ITEM(tup, 2, keyusage); |
| 5293 | keyusage = NULL; |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5294 | if (PySet_Add(result, tup) == -1) { |
| 5295 | Py_CLEAR(result); |
| 5296 | Py_CLEAR(tup); |
| 5297 | break; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5298 | } |
| 5299 | Py_CLEAR(tup); |
| 5300 | } |
| 5301 | if (pCertCtx) { |
| 5302 | /* loop ended with an error, need to clean up context manually */ |
| 5303 | CertFreeCertificateContext(pCertCtx); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5304 | } |
| 5305 | |
| 5306 | /* In error cases cert, enc and tup may not be NULL */ |
| 5307 | Py_XDECREF(cert); |
| 5308 | Py_XDECREF(enc); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5309 | Py_XDECREF(keyusage); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5310 | Py_XDECREF(tup); |
| 5311 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5312 | /* CERT_CLOSE_STORE_FORCE_FLAG forces freeing of memory for all contexts |
| 5313 | associated with the store, in this case our collection store and the |
| 5314 | associated system stores. */ |
| 5315 | if (!CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG)) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5316 | /* This error case might shadow another exception.*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5317 | Py_XDECREF(result); |
| 5318 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5319 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5320 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5321 | /* convert set to list */ |
| 5322 | if (result == NULL) { |
| 5323 | return NULL; |
| 5324 | } else { |
| 5325 | PyObject *lst = PySequence_List(result); |
| 5326 | Py_DECREF(result); |
| 5327 | return lst; |
| 5328 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5329 | } |
| 5330 | |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5331 | /*[clinic input] |
| 5332 | _ssl.enum_crls |
| 5333 | store_name: str |
| 5334 | |
| 5335 | Retrieve CRLs from Windows' cert store. |
| 5336 | |
| 5337 | store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 5338 | more cert storages, too. The function returns a list of (bytes, |
| 5339 | encoding_type) tuples. The encoding_type flag can be interpreted with |
| 5340 | X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. |
| 5341 | [clinic start generated code]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5342 | |
| 5343 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5344 | _ssl_enum_crls_impl(PyObject *module, const char *store_name) |
| 5345 | /*[clinic end generated code: output=bce467f60ccd03b6 input=a1f1d7629f1c5d3d]*/ |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5346 | { |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5347 | HCERTSTORE hCollectionStore = NULL; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5348 | PCCRL_CONTEXT pCrlCtx = NULL; |
| 5349 | PyObject *crl = NULL, *enc = NULL, *tup = NULL; |
| 5350 | PyObject *result = NULL; |
| 5351 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5352 | result = PySet_New(NULL); |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5353 | if (result == NULL) { |
| 5354 | return NULL; |
| 5355 | } |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5356 | hCollectionStore = ssl_collect_certificates(store_name); |
| 5357 | if (hCollectionStore == NULL) { |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5358 | Py_DECREF(result); |
| 5359 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5360 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5361 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5362 | while (pCrlCtx = CertEnumCRLsInStore(hCollectionStore, pCrlCtx)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5363 | crl = PyBytes_FromStringAndSize((const char*)pCrlCtx->pbCrlEncoded, |
| 5364 | pCrlCtx->cbCrlEncoded); |
| 5365 | if (!crl) { |
| 5366 | Py_CLEAR(result); |
| 5367 | break; |
| 5368 | } |
| 5369 | if ((enc = certEncodingType(pCrlCtx->dwCertEncodingType)) == NULL) { |
| 5370 | Py_CLEAR(result); |
| 5371 | break; |
| 5372 | } |
| 5373 | if ((tup = PyTuple_New(2)) == NULL) { |
| 5374 | Py_CLEAR(result); |
| 5375 | break; |
| 5376 | } |
| 5377 | PyTuple_SET_ITEM(tup, 0, crl); |
| 5378 | crl = NULL; |
| 5379 | PyTuple_SET_ITEM(tup, 1, enc); |
| 5380 | enc = NULL; |
| 5381 | |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5382 | if (PySet_Add(result, tup) == -1) { |
| 5383 | Py_CLEAR(result); |
| 5384 | Py_CLEAR(tup); |
| 5385 | break; |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5386 | } |
| 5387 | Py_CLEAR(tup); |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5388 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5389 | if (pCrlCtx) { |
| 5390 | /* loop ended with an error, need to clean up context manually */ |
| 5391 | CertFreeCRLContext(pCrlCtx); |
| 5392 | } |
| 5393 | |
| 5394 | /* In error cases cert, enc and tup may not be NULL */ |
| 5395 | Py_XDECREF(crl); |
| 5396 | Py_XDECREF(enc); |
| 5397 | Py_XDECREF(tup); |
| 5398 | |
kctherookie | d93fbbf | 2019-03-29 00:59:06 +0700 | [diff] [blame] | 5399 | /* CERT_CLOSE_STORE_FORCE_FLAG forces freeing of memory for all contexts |
| 5400 | associated with the store, in this case our collection store and the |
| 5401 | associated system stores. */ |
| 5402 | if (!CertCloseStore(hCollectionStore, CERT_CLOSE_STORE_FORCE_FLAG)) { |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5403 | /* This error case might shadow another exception.*/ |
| 5404 | Py_XDECREF(result); |
| 5405 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5406 | } |
Christian Heimes | 915cd3f | 2019-09-09 18:06:55 +0200 | [diff] [blame] | 5407 | /* convert set to list */ |
| 5408 | if (result == NULL) { |
| 5409 | return NULL; |
| 5410 | } else { |
| 5411 | PyObject *lst = PySequence_List(result); |
| 5412 | Py_DECREF(result); |
| 5413 | return lst; |
| 5414 | } |
Christian Heimes | 46bebee | 2013-06-09 19:03:31 +0200 | [diff] [blame] | 5415 | } |
Christian Heimes | 44109d7 | 2013-11-22 01:51:30 +0100 | [diff] [blame] | 5416 | |
| 5417 | #endif /* _MSC_VER */ |
Bill Janssen | 40a0f66 | 2008-08-12 16:56:25 +0000 | [diff] [blame] | 5418 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5419 | /* List of functions exported by this module. */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5420 | static PyMethodDef PySSL_methods[] = { |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5421 | _SSL__TEST_DECODE_CERT_METHODDEF |
| 5422 | _SSL_RAND_ADD_METHODDEF |
| 5423 | _SSL_RAND_BYTES_METHODDEF |
| 5424 | _SSL_RAND_PSEUDO_BYTES_METHODDEF |
Serhiy Storchaka | 4b7b82f | 2015-05-03 16:14:08 +0300 | [diff] [blame] | 5425 | _SSL_RAND_STATUS_METHODDEF |
| 5426 | _SSL_GET_DEFAULT_VERIFY_PATHS_METHODDEF |
| 5427 | _SSL_ENUM_CERTIFICATES_METHODDEF |
| 5428 | _SSL_ENUM_CRLS_METHODDEF |
| 5429 | _SSL_TXT2OBJ_METHODDEF |
| 5430 | _SSL_NID2OBJ_METHODDEF |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5431 | {NULL, NULL} /* Sentinel */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5432 | }; |
| 5433 | |
| 5434 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5435 | PyDoc_STRVAR(module_doc, |
| 5436 | "Implementation module for SSL socket operations. See the socket module\n\ |
| 5437 | for documentation."); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5438 | |
| 5439 | static int |
| 5440 | sslmodule_init_exceptions(PyObject *module) |
| 5441 | { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5442 | _sslmodulestate *state = get_ssl_state(module); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5443 | PyObject *bases = NULL; |
| 5444 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5445 | #define add_exception(exc, name, doc, base) \ |
| 5446 | do { \ |
| 5447 | (exc) = PyErr_NewExceptionWithDoc("ssl." name, (doc), (base), NULL); \ |
| 5448 | if ((state) == NULL) goto error; \ |
| 5449 | if (PyModule_AddObjectRef(module, name, exc) < 0) goto error; \ |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5450 | } while(0) |
| 5451 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5452 | state->PySSLErrorObject = PyType_FromSpecWithBases( |
| 5453 | &sslerror_type_spec, PyExc_OSError); |
| 5454 | if (state->PySSLErrorObject == NULL) { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5455 | goto error; |
| 5456 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5457 | if (PyModule_AddObjectRef(module, "SSLError", state->PySSLErrorObject) < 0) { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5458 | goto error; |
| 5459 | } |
| 5460 | |
| 5461 | /* ssl.CertificateError used to be a subclass of ValueError */ |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5462 | bases = PyTuple_Pack(2, state->PySSLErrorObject, PyExc_ValueError); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5463 | if (bases == NULL) { |
| 5464 | goto error; |
| 5465 | } |
| 5466 | add_exception( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5467 | state->PySSLCertVerificationErrorObject, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5468 | "SSLCertVerificationError", |
| 5469 | SSLCertVerificationError_doc, |
| 5470 | bases |
| 5471 | ); |
| 5472 | Py_CLEAR(bases); |
| 5473 | |
| 5474 | add_exception( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5475 | state->PySSLZeroReturnErrorObject, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5476 | "SSLZeroReturnError", |
| 5477 | SSLZeroReturnError_doc, |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5478 | state->PySSLErrorObject |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5479 | ); |
| 5480 | |
| 5481 | add_exception( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5482 | state->PySSLWantWriteErrorObject, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5483 | "SSLWantWriteError", |
| 5484 | SSLWantWriteError_doc, |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5485 | state->PySSLErrorObject |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5486 | ); |
| 5487 | |
| 5488 | add_exception( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5489 | state->PySSLWantReadErrorObject, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5490 | "SSLWantReadError", |
| 5491 | SSLWantReadError_doc, |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5492 | state->PySSLErrorObject |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5493 | ); |
| 5494 | |
| 5495 | add_exception( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5496 | state->PySSLSyscallErrorObject, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5497 | "SSLSyscallError", |
| 5498 | SSLSyscallError_doc, |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5499 | state->PySSLErrorObject |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5500 | ); |
| 5501 | |
| 5502 | add_exception( |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5503 | state->PySSLEOFErrorObject, |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5504 | "SSLEOFError", |
| 5505 | SSLEOFError_doc, |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5506 | state->PySSLErrorObject |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5507 | ); |
| 5508 | #undef add_exception |
| 5509 | |
| 5510 | return 0; |
| 5511 | error: |
| 5512 | Py_XDECREF(bases); |
| 5513 | return -1; |
| 5514 | } |
| 5515 | |
| 5516 | static int |
| 5517 | sslmodule_init_socketapi(PyObject *module) |
| 5518 | { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5519 | _sslmodulestate *state = get_ssl_state(module); |
| 5520 | PySocketModule_APIObject *sockmod = PySocketModule_ImportModuleAndAPI(); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5521 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5522 | if ((sockmod == NULL) || (sockmod->Sock_Type == NULL)) { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5523 | return -1; |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5524 | } |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5525 | state->Sock_Type = sockmod->Sock_Type; |
| 5526 | Py_INCREF(state->Sock_Type); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5527 | return 0; |
| 5528 | } |
Christian Heimes | c941e62 | 2017-09-05 15:47:11 +0200 | [diff] [blame] | 5529 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5530 | static int |
| 5531 | sslmodule_init_constants(PyObject *m) |
| 5532 | { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5533 | |
Christian Heimes | 892d66e | 2018-01-29 14:10:18 +0100 | [diff] [blame] | 5534 | PyModule_AddStringConstant(m, "_DEFAULT_CIPHERS", |
| 5535 | PY_SSL_DEFAULT_CIPHER_STRING); |
| 5536 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5537 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
| 5538 | PY_SSL_ERROR_ZERO_RETURN); |
| 5539 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
| 5540 | PY_SSL_ERROR_WANT_READ); |
| 5541 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
| 5542 | PY_SSL_ERROR_WANT_WRITE); |
| 5543 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
| 5544 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
| 5545 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
| 5546 | PY_SSL_ERROR_SYSCALL); |
| 5547 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
| 5548 | PY_SSL_ERROR_SSL); |
| 5549 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 5550 | PY_SSL_ERROR_WANT_CONNECT); |
| 5551 | /* non ssl.h errorcodes */ |
| 5552 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 5553 | PY_SSL_ERROR_EOF); |
| 5554 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 5555 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
| 5556 | /* cert requirements */ |
| 5557 | PyModule_AddIntConstant(m, "CERT_NONE", |
| 5558 | PY_SSL_CERT_NONE); |
| 5559 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 5560 | PY_SSL_CERT_OPTIONAL); |
| 5561 | PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 5562 | PY_SSL_CERT_REQUIRED); |
Christian Heimes | 2258779 | 2013-11-21 23:56:13 +0100 | [diff] [blame] | 5563 | /* CRL verification for verification_flags */ |
| 5564 | PyModule_AddIntConstant(m, "VERIFY_DEFAULT", |
| 5565 | 0); |
| 5566 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_LEAF", |
| 5567 | X509_V_FLAG_CRL_CHECK); |
| 5568 | PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_CHAIN", |
| 5569 | X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5570 | PyModule_AddIntConstant(m, "VERIFY_X509_STRICT", |
| 5571 | X509_V_FLAG_X509_STRICT); |
Chris Burr | e0b4aa0 | 2021-03-18 09:24:01 +0100 | [diff] [blame] | 5572 | PyModule_AddIntConstant(m, "VERIFY_ALLOW_PROXY_CERTS", |
| 5573 | X509_V_FLAG_ALLOW_PROXY_CERTS); |
Benjamin Peterson | 990fcaa | 2015-03-04 22:49:41 -0500 | [diff] [blame] | 5574 | PyModule_AddIntConstant(m, "VERIFY_X509_TRUSTED_FIRST", |
| 5575 | X509_V_FLAG_TRUSTED_FIRST); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 5576 | |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5577 | /* Alert Descriptions from ssl.h */ |
| 5578 | /* note RESERVED constants no longer intended for use have been removed */ |
| 5579 | /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6 */ |
| 5580 | |
| 5581 | #define ADD_AD_CONSTANT(s) \ |
| 5582 | PyModule_AddIntConstant(m, "ALERT_DESCRIPTION_"#s, \ |
| 5583 | SSL_AD_##s) |
| 5584 | |
| 5585 | ADD_AD_CONSTANT(CLOSE_NOTIFY); |
| 5586 | ADD_AD_CONSTANT(UNEXPECTED_MESSAGE); |
| 5587 | ADD_AD_CONSTANT(BAD_RECORD_MAC); |
| 5588 | ADD_AD_CONSTANT(RECORD_OVERFLOW); |
| 5589 | ADD_AD_CONSTANT(DECOMPRESSION_FAILURE); |
| 5590 | ADD_AD_CONSTANT(HANDSHAKE_FAILURE); |
| 5591 | ADD_AD_CONSTANT(BAD_CERTIFICATE); |
| 5592 | ADD_AD_CONSTANT(UNSUPPORTED_CERTIFICATE); |
| 5593 | ADD_AD_CONSTANT(CERTIFICATE_REVOKED); |
| 5594 | ADD_AD_CONSTANT(CERTIFICATE_EXPIRED); |
| 5595 | ADD_AD_CONSTANT(CERTIFICATE_UNKNOWN); |
| 5596 | ADD_AD_CONSTANT(ILLEGAL_PARAMETER); |
| 5597 | ADD_AD_CONSTANT(UNKNOWN_CA); |
| 5598 | ADD_AD_CONSTANT(ACCESS_DENIED); |
| 5599 | ADD_AD_CONSTANT(DECODE_ERROR); |
| 5600 | ADD_AD_CONSTANT(DECRYPT_ERROR); |
| 5601 | ADD_AD_CONSTANT(PROTOCOL_VERSION); |
| 5602 | ADD_AD_CONSTANT(INSUFFICIENT_SECURITY); |
| 5603 | ADD_AD_CONSTANT(INTERNAL_ERROR); |
| 5604 | ADD_AD_CONSTANT(USER_CANCELLED); |
| 5605 | ADD_AD_CONSTANT(NO_RENEGOTIATION); |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5606 | /* Not all constants are in old OpenSSL versions */ |
Antoine Pitrou | 912fbff | 2013-03-30 16:29:32 +0100 | [diff] [blame] | 5607 | #ifdef SSL_AD_UNSUPPORTED_EXTENSION |
| 5608 | ADD_AD_CONSTANT(UNSUPPORTED_EXTENSION); |
| 5609 | #endif |
| 5610 | #ifdef SSL_AD_CERTIFICATE_UNOBTAINABLE |
| 5611 | ADD_AD_CONSTANT(CERTIFICATE_UNOBTAINABLE); |
| 5612 | #endif |
| 5613 | #ifdef SSL_AD_UNRECOGNIZED_NAME |
| 5614 | ADD_AD_CONSTANT(UNRECOGNIZED_NAME); |
| 5615 | #endif |
Antoine Pitrou | 58ddc9d | 2013-01-05 21:20:29 +0100 | [diff] [blame] | 5616 | #ifdef SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE |
| 5617 | ADD_AD_CONSTANT(BAD_CERTIFICATE_STATUS_RESPONSE); |
| 5618 | #endif |
| 5619 | #ifdef SSL_AD_BAD_CERTIFICATE_HASH_VALUE |
| 5620 | ADD_AD_CONSTANT(BAD_CERTIFICATE_HASH_VALUE); |
| 5621 | #endif |
| 5622 | #ifdef SSL_AD_UNKNOWN_PSK_IDENTITY |
| 5623 | ADD_AD_CONSTANT(UNKNOWN_PSK_IDENTITY); |
| 5624 | #endif |
| 5625 | |
| 5626 | #undef ADD_AD_CONSTANT |
| 5627 | |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5628 | /* protocol versions */ |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 5629 | #ifndef OPENSSL_NO_SSL2 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5630 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 5631 | PY_SSL_VERSION_SSL2); |
Victor Stinner | 3de4919 | 2011-05-09 00:42:58 +0200 | [diff] [blame] | 5632 | #endif |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 5633 | #ifndef OPENSSL_NO_SSL3 |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5634 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 5635 | PY_SSL_VERSION_SSL3); |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame] | 5636 | #endif |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5637 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
Christian Heimes | 598894f | 2016-09-05 23:19:05 +0200 | [diff] [blame] | 5638 | PY_SSL_VERSION_TLS); |
| 5639 | PyModule_AddIntConstant(m, "PROTOCOL_TLS", |
| 5640 | PY_SSL_VERSION_TLS); |
Christian Heimes | 5fe668c | 2016-09-12 00:01:11 +0200 | [diff] [blame] | 5641 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_CLIENT", |
| 5642 | PY_SSL_VERSION_TLS_CLIENT); |
| 5643 | PyModule_AddIntConstant(m, "PROTOCOL_TLS_SERVER", |
| 5644 | PY_SSL_VERSION_TLS_SERVER); |
Antoine Pitrou | cbb82eb | 2010-05-05 15:57:33 +0000 | [diff] [blame] | 5645 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 5646 | PY_SSL_VERSION_TLS1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 5647 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_1", |
| 5648 | PY_SSL_VERSION_TLS1_1); |
| 5649 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_2", |
| 5650 | PY_SSL_VERSION_TLS1_2); |
Antoine Pitrou | 04f6a32 | 2010-04-05 21:40:07 +0000 | [diff] [blame] | 5651 | |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5652 | /* protocol options */ |
Antoine Pitrou | 3f36631 | 2012-01-27 09:50:45 +0100 | [diff] [blame] | 5653 | PyModule_AddIntConstant(m, "OP_ALL", |
| 5654 | SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5655 | PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2); |
| 5656 | PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3); |
| 5657 | PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1); |
Antoine Pitrou | 2463e5f | 2013-03-28 22:24:43 +0100 | [diff] [blame] | 5658 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1); |
| 5659 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5660 | #ifdef SSL_OP_NO_TLSv1_3 |
| 5661 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", SSL_OP_NO_TLSv1_3); |
| 5662 | #else |
| 5663 | PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", 0); |
| 5664 | #endif |
Antoine Pitrou | 6db4944 | 2011-12-19 13:27:11 +0100 | [diff] [blame] | 5665 | PyModule_AddIntConstant(m, "OP_CIPHER_SERVER_PREFERENCE", |
| 5666 | SSL_OP_CIPHER_SERVER_PREFERENCE); |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 5667 | PyModule_AddIntConstant(m, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE); |
Christian Heimes | 99a6570 | 2016-09-10 23:44:53 +0200 | [diff] [blame] | 5668 | PyModule_AddIntConstant(m, "OP_NO_TICKET", SSL_OP_NO_TICKET); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 5669 | #ifdef SSL_OP_SINGLE_ECDH_USE |
Antoine Pitrou | 923df6f | 2011-12-19 17:16:51 +0100 | [diff] [blame] | 5670 | PyModule_AddIntConstant(m, "OP_SINGLE_ECDH_USE", SSL_OP_SINGLE_ECDH_USE); |
Antoine Pitrou | e9fccb3 | 2012-02-17 11:53:10 +0100 | [diff] [blame] | 5671 | #endif |
Antoine Pitrou | 8abdb8a | 2011-12-20 10:13:40 +0100 | [diff] [blame] | 5672 | #ifdef SSL_OP_NO_COMPRESSION |
| 5673 | PyModule_AddIntConstant(m, "OP_NO_COMPRESSION", |
| 5674 | SSL_OP_NO_COMPRESSION); |
| 5675 | #endif |
Christian Heimes | 05d9fe3 | 2018-02-27 08:55:39 +0100 | [diff] [blame] | 5676 | #ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT |
| 5677 | PyModule_AddIntConstant(m, "OP_ENABLE_MIDDLEBOX_COMPAT", |
| 5678 | SSL_OP_ENABLE_MIDDLEBOX_COMPAT); |
| 5679 | #endif |
Christian Heimes | 67c4801 | 2018-05-15 16:25:40 -0400 | [diff] [blame] | 5680 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 5681 | PyModule_AddIntConstant(m, "OP_NO_RENEGOTIATION", |
| 5682 | SSL_OP_NO_RENEGOTIATION); |
| 5683 | #endif |
Christian Heimes | 6f37ebc | 2021-04-09 17:59:21 +0200 | [diff] [blame] | 5684 | #ifdef SSL_OP_IGNORE_UNEXPECTED_EOF |
| 5685 | PyModule_AddIntConstant(m, "OP_IGNORE_UNEXPECTED_EOF", |
| 5686 | SSL_OP_IGNORE_UNEXPECTED_EOF); |
| 5687 | #endif |
Antoine Pitrou | b521877 | 2010-05-21 09:56:06 +0000 | [diff] [blame] | 5688 | |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 5689 | #ifdef X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT |
| 5690 | PyModule_AddIntConstant(m, "HOSTFLAG_ALWAYS_CHECK_SUBJECT", |
| 5691 | X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT); |
| 5692 | #endif |
| 5693 | #ifdef X509_CHECK_FLAG_NEVER_CHECK_SUBJECT |
| 5694 | PyModule_AddIntConstant(m, "HOSTFLAG_NEVER_CHECK_SUBJECT", |
| 5695 | X509_CHECK_FLAG_NEVER_CHECK_SUBJECT); |
| 5696 | #endif |
| 5697 | #ifdef X509_CHECK_FLAG_NO_WILDCARDS |
| 5698 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_WILDCARDS", |
| 5699 | X509_CHECK_FLAG_NO_WILDCARDS); |
| 5700 | #endif |
| 5701 | #ifdef X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS |
| 5702 | PyModule_AddIntConstant(m, "HOSTFLAG_NO_PARTIAL_WILDCARDS", |
| 5703 | X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); |
| 5704 | #endif |
| 5705 | #ifdef X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS |
| 5706 | PyModule_AddIntConstant(m, "HOSTFLAG_MULTI_LABEL_WILDCARDS", |
| 5707 | X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS); |
| 5708 | #endif |
| 5709 | #ifdef X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS |
| 5710 | PyModule_AddIntConstant(m, "HOSTFLAG_SINGLE_LABEL_SUBDOMAINS", |
| 5711 | X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS); |
| 5712 | #endif |
| 5713 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5714 | /* protocol versions */ |
| 5715 | PyModule_AddIntConstant(m, "PROTO_MINIMUM_SUPPORTED", |
| 5716 | PY_PROTO_MINIMUM_SUPPORTED); |
| 5717 | PyModule_AddIntConstant(m, "PROTO_MAXIMUM_SUPPORTED", |
| 5718 | PY_PROTO_MAXIMUM_SUPPORTED); |
| 5719 | PyModule_AddIntConstant(m, "PROTO_SSLv3", PY_PROTO_SSLv3); |
| 5720 | PyModule_AddIntConstant(m, "PROTO_TLSv1", PY_PROTO_TLSv1); |
| 5721 | PyModule_AddIntConstant(m, "PROTO_TLSv1_1", PY_PROTO_TLSv1_1); |
| 5722 | PyModule_AddIntConstant(m, "PROTO_TLSv1_2", PY_PROTO_TLSv1_2); |
| 5723 | PyModule_AddIntConstant(m, "PROTO_TLSv1_3", PY_PROTO_TLSv1_3); |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 5724 | |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 5725 | #define addbool(m, key, value) \ |
| 5726 | do { \ |
| 5727 | PyObject *bool_obj = (value) ? Py_True : Py_False; \ |
| 5728 | Py_INCREF(bool_obj); \ |
| 5729 | PyModule_AddObject((m), (key), bool_obj); \ |
| 5730 | } while (0) |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5731 | |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5732 | addbool(m, "HAS_SNI", 1); |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5733 | addbool(m, "HAS_TLS_UNIQUE", 1); |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5734 | addbool(m, "HAS_ECDH", 1); |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5735 | addbool(m, "HAS_NPN", 0); |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5736 | addbool(m, "HAS_ALPN", 1); |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5737 | |
| 5738 | #if defined(SSL2_VERSION) && !defined(OPENSSL_NO_SSL2) |
| 5739 | addbool(m, "HAS_SSLv2", 1); |
| 5740 | #else |
| 5741 | addbool(m, "HAS_SSLv2", 0); |
| 5742 | #endif |
| 5743 | |
| 5744 | #if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3) |
| 5745 | addbool(m, "HAS_SSLv3", 1); |
| 5746 | #else |
| 5747 | addbool(m, "HAS_SSLv3", 0); |
| 5748 | #endif |
| 5749 | |
| 5750 | #if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1) |
| 5751 | addbool(m, "HAS_TLSv1", 1); |
| 5752 | #else |
| 5753 | addbool(m, "HAS_TLSv1", 0); |
| 5754 | #endif |
| 5755 | |
| 5756 | #if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1) |
| 5757 | addbool(m, "HAS_TLSv1_1", 1); |
| 5758 | #else |
| 5759 | addbool(m, "HAS_TLSv1_1", 0); |
| 5760 | #endif |
| 5761 | |
| 5762 | #if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2) |
| 5763 | addbool(m, "HAS_TLSv1_2", 1); |
| 5764 | #else |
| 5765 | addbool(m, "HAS_TLSv1_2", 0); |
| 5766 | #endif |
Benjamin Peterson | cca2732 | 2015-01-23 16:35:37 -0500 | [diff] [blame] | 5767 | |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5768 | #if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5769 | addbool(m, "HAS_TLSv1_3", 1); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5770 | #else |
Christian Heimes | 698dde1 | 2018-02-27 11:54:43 +0100 | [diff] [blame] | 5771 | addbool(m, "HAS_TLSv1_3", 0); |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5772 | #endif |
Christian Heimes | cb5b68a | 2017-09-07 18:07:00 -0700 | [diff] [blame] | 5773 | |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5774 | return 0; |
| 5775 | } |
| 5776 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5777 | static int |
| 5778 | sslmodule_init_errorcodes(PyObject *module) |
| 5779 | { |
| 5780 | _sslmodulestate *state = get_ssl_state(module); |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5781 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5782 | struct py_ssl_error_code *errcode; |
| 5783 | struct py_ssl_library_code *libcode; |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5784 | |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5785 | /* Mappings for error codes */ |
| 5786 | state->err_codes_to_names = PyDict_New(); |
| 5787 | if (state->err_codes_to_names == NULL) |
| 5788 | return -1; |
| 5789 | state->err_names_to_codes = PyDict_New(); |
| 5790 | if (state->err_names_to_codes == NULL) |
| 5791 | return -1; |
| 5792 | state->lib_codes_to_names = PyDict_New(); |
| 5793 | if (state->lib_codes_to_names == NULL) |
| 5794 | return -1; |
| 5795 | |
| 5796 | errcode = error_codes; |
| 5797 | while (errcode->mnemonic != NULL) { |
| 5798 | PyObject *mnemo, *key; |
| 5799 | mnemo = PyUnicode_FromString(errcode->mnemonic); |
| 5800 | key = Py_BuildValue("ii", errcode->library, errcode->reason); |
| 5801 | if (mnemo == NULL || key == NULL) |
| 5802 | return -1; |
| 5803 | if (PyDict_SetItem(state->err_codes_to_names, key, mnemo)) |
| 5804 | return -1; |
| 5805 | if (PyDict_SetItem(state->err_names_to_codes, mnemo, key)) |
| 5806 | return -1; |
| 5807 | Py_DECREF(key); |
| 5808 | Py_DECREF(mnemo); |
| 5809 | errcode++; |
| 5810 | } |
| 5811 | |
| 5812 | libcode = library_codes; |
| 5813 | while (libcode->library != NULL) { |
| 5814 | PyObject *mnemo, *key; |
| 5815 | key = PyLong_FromLong(libcode->code); |
| 5816 | mnemo = PyUnicode_FromString(libcode->library); |
| 5817 | if (key == NULL || mnemo == NULL) |
| 5818 | return -1; |
| 5819 | if (PyDict_SetItem(state->lib_codes_to_names, key, mnemo)) |
| 5820 | return -1; |
| 5821 | Py_DECREF(key); |
| 5822 | Py_DECREF(mnemo); |
| 5823 | libcode++; |
| 5824 | } |
| 5825 | |
| 5826 | if (PyModule_AddObjectRef(module, "err_codes_to_names", state->err_codes_to_names)) |
| 5827 | return -1; |
| 5828 | if (PyModule_AddObjectRef(module, "err_names_to_codes", state->err_names_to_codes)) |
| 5829 | return -1; |
| 5830 | if (PyModule_AddObjectRef(module, "lib_codes_to_names", state->lib_codes_to_names)) |
| 5831 | return -1; |
| 5832 | |
| 5833 | return 0; |
| 5834 | } |
| 5835 | |
| 5836 | static void |
| 5837 | parse_openssl_version(unsigned long libver, |
| 5838 | unsigned int *major, unsigned int *minor, |
| 5839 | unsigned int *fix, unsigned int *patch, |
| 5840 | unsigned int *status) |
| 5841 | { |
| 5842 | *status = libver & 0xF; |
| 5843 | libver >>= 4; |
| 5844 | *patch = libver & 0xFF; |
| 5845 | libver >>= 8; |
| 5846 | *fix = libver & 0xFF; |
| 5847 | libver >>= 8; |
| 5848 | *minor = libver & 0xFF; |
| 5849 | libver >>= 8; |
| 5850 | *major = libver & 0xFF; |
| 5851 | } |
| 5852 | |
| 5853 | static int |
| 5854 | sslmodule_init_versioninfo(PyObject *m) |
| 5855 | { |
| 5856 | PyObject *r; |
| 5857 | unsigned long libver; |
| 5858 | unsigned int major, minor, fix, patch, status; |
| 5859 | |
| 5860 | /* OpenSSL version */ |
| 5861 | /* SSLeay() gives us the version of the library linked against, |
| 5862 | which could be different from the headers version. |
| 5863 | */ |
| 5864 | libver = OpenSSL_version_num(); |
| 5865 | r = PyLong_FromUnsignedLong(libver); |
| 5866 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r)) |
| 5867 | return -1; |
| 5868 | |
| 5869 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 5870 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 5871 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r)) |
| 5872 | return -1; |
| 5873 | |
| 5874 | r = PyUnicode_FromString(OpenSSL_version(OPENSSL_VERSION)); |
| 5875 | if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r)) |
| 5876 | return -1; |
| 5877 | |
| 5878 | libver = OPENSSL_VERSION_NUMBER; |
| 5879 | parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 5880 | r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 5881 | if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r)) |
| 5882 | return -1; |
| 5883 | |
| 5884 | return 0; |
| 5885 | } |
| 5886 | |
| 5887 | static int |
| 5888 | sslmodule_init_types(PyObject *module) |
| 5889 | { |
| 5890 | _sslmodulestate *state = get_ssl_state(module); |
| 5891 | |
| 5892 | state->PySSLContext_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 5893 | module, &PySSLContext_spec, NULL |
| 5894 | ); |
| 5895 | if (state->PySSLContext_Type == NULL) |
| 5896 | return -1; |
| 5897 | |
| 5898 | state->PySSLSocket_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 5899 | module, &PySSLSocket_spec, NULL |
| 5900 | ); |
| 5901 | if (state->PySSLSocket_Type == NULL) |
| 5902 | return -1; |
| 5903 | |
| 5904 | state->PySSLMemoryBIO_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 5905 | module, &PySSLMemoryBIO_spec, NULL |
| 5906 | ); |
| 5907 | if (state->PySSLMemoryBIO_Type == NULL) |
| 5908 | return -1; |
| 5909 | |
| 5910 | state->PySSLSession_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 5911 | module, &PySSLSession_spec, NULL |
| 5912 | ); |
| 5913 | if (state->PySSLSession_Type == NULL) |
| 5914 | return -1; |
| 5915 | |
| 5916 | if (PyModule_AddType(module, state->PySSLContext_Type)) |
| 5917 | return -1; |
| 5918 | if (PyModule_AddType(module, state->PySSLSocket_Type)) |
| 5919 | return -1; |
| 5920 | if (PyModule_AddType(module, state->PySSLMemoryBIO_Type)) |
| 5921 | return -1; |
| 5922 | if (PyModule_AddType(module, state->PySSLSession_Type)) |
| 5923 | return -1; |
| 5924 | |
| 5925 | return 0; |
| 5926 | } |
| 5927 | |
| 5928 | static PyModuleDef_Slot sslmodule_slots[] = { |
| 5929 | {Py_mod_exec, sslmodule_init_types}, |
| 5930 | {Py_mod_exec, sslmodule_init_exceptions}, |
| 5931 | {Py_mod_exec, sslmodule_init_socketapi}, |
| 5932 | {Py_mod_exec, sslmodule_init_errorcodes}, |
| 5933 | {Py_mod_exec, sslmodule_init_constants}, |
| 5934 | {Py_mod_exec, sslmodule_init_versioninfo}, |
| 5935 | {0, NULL} |
| 5936 | }; |
| 5937 | |
| 5938 | static int |
| 5939 | sslmodule_traverse(PyObject *m, visitproc visit, void *arg) |
| 5940 | { |
| 5941 | _sslmodulestate *state = get_ssl_state(m); |
| 5942 | |
| 5943 | Py_VISIT(state->PySSLContext_Type); |
| 5944 | Py_VISIT(state->PySSLSocket_Type); |
| 5945 | Py_VISIT(state->PySSLMemoryBIO_Type); |
| 5946 | Py_VISIT(state->PySSLSession_Type); |
| 5947 | Py_VISIT(state->PySSLErrorObject); |
| 5948 | Py_VISIT(state->PySSLCertVerificationErrorObject); |
| 5949 | Py_VISIT(state->PySSLZeroReturnErrorObject); |
| 5950 | Py_VISIT(state->PySSLWantReadErrorObject); |
| 5951 | Py_VISIT(state->PySSLWantWriteErrorObject); |
| 5952 | Py_VISIT(state->PySSLSyscallErrorObject); |
| 5953 | Py_VISIT(state->PySSLEOFErrorObject); |
| 5954 | Py_VISIT(state->err_codes_to_names); |
| 5955 | Py_VISIT(state->err_names_to_codes); |
| 5956 | Py_VISIT(state->lib_codes_to_names); |
| 5957 | Py_VISIT(state->Sock_Type); |
| 5958 | |
| 5959 | return 0; |
| 5960 | } |
| 5961 | |
| 5962 | static int |
| 5963 | sslmodule_clear(PyObject *m) |
| 5964 | { |
| 5965 | _sslmodulestate *state = get_ssl_state(m); |
| 5966 | |
| 5967 | Py_CLEAR(state->PySSLContext_Type); |
| 5968 | Py_CLEAR(state->PySSLSocket_Type); |
| 5969 | Py_CLEAR(state->PySSLMemoryBIO_Type); |
| 5970 | Py_CLEAR(state->PySSLSession_Type); |
| 5971 | Py_CLEAR(state->PySSLErrorObject); |
| 5972 | Py_CLEAR(state->PySSLCertVerificationErrorObject); |
| 5973 | Py_CLEAR(state->PySSLZeroReturnErrorObject); |
| 5974 | Py_CLEAR(state->PySSLWantReadErrorObject); |
| 5975 | Py_CLEAR(state->PySSLWantWriteErrorObject); |
| 5976 | Py_CLEAR(state->PySSLSyscallErrorObject); |
| 5977 | Py_CLEAR(state->PySSLEOFErrorObject); |
| 5978 | Py_CLEAR(state->err_codes_to_names); |
| 5979 | Py_CLEAR(state->err_names_to_codes); |
| 5980 | Py_CLEAR(state->lib_codes_to_names); |
| 5981 | Py_CLEAR(state->Sock_Type); |
| 5982 | |
| 5983 | return 0; |
| 5984 | } |
| 5985 | |
| 5986 | static void |
| 5987 | sslmodule_free(void *m) |
| 5988 | { |
| 5989 | sslmodule_clear((PyObject *)m); |
| 5990 | } |
| 5991 | |
| 5992 | static struct PyModuleDef _sslmodule_def = { |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 5993 | PyModuleDef_HEAD_INIT, |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 5994 | .m_name = "_ssl", |
| 5995 | .m_doc = module_doc, |
| 5996 | .m_size = sizeof(_sslmodulestate), |
| 5997 | .m_methods = PySSL_methods, |
| 5998 | .m_slots = sslmodule_slots, |
| 5999 | .m_traverse = sslmodule_traverse, |
| 6000 | .m_clear = sslmodule_clear, |
| 6001 | .m_free = sslmodule_free |
Christian Heimes | 5c36da7 | 2020-11-20 09:40:12 +0100 | [diff] [blame] | 6002 | }; |
| 6003 | |
| 6004 | PyMODINIT_FUNC |
| 6005 | PyInit__ssl(void) |
| 6006 | { |
Christian Heimes | 7f1305e | 2021-04-17 20:06:38 +0200 | [diff] [blame] | 6007 | return PyModuleDef_Init(&_sslmodule_def); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 6008 | } |