Paul Kehrer | 55fb341 | 2017-06-29 18:44:08 -0500 | [diff] [blame] | 1 | import os |
Maximilian Hils | 1d95dea | 2015-08-17 19:27:20 +0200 | [diff] [blame] | 2 | import socket |
Alex Gaynor | be2bd54 | 2019-02-21 21:41:22 -0500 | [diff] [blame] | 3 | import warnings |
Konstantinos Koukopoulos | 541150d | 2014-01-31 01:00:19 +0200 | [diff] [blame] | 4 | from sys import platform |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 5 | from functools import wraps, partial |
Cory Benfield | be3e7b8 | 2014-05-10 09:48:55 +0100 | [diff] [blame] | 6 | from itertools import count, chain |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 7 | from weakref import WeakValueDictionary |
| 8 | from errno import errorcode |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 9 | |
Alex Gaynor | 1257600 | 2019-11-18 00:18:50 -0500 | [diff] [blame] | 10 | from six import integer_types, int2byte, indexbytes |
Jean-Paul Calderone | 63eab69 | 2014-01-18 10:19:56 -0500 | [diff] [blame] | 11 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 12 | from OpenSSL._util import ( |
Hynek Schlawack | aa86121 | 2016-03-13 13:53:48 +0100 | [diff] [blame] | 13 | UNSPECIFIED as _UNSPECIFIED, |
| 14 | exception_from_error_queue as _exception_from_error_queue, |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 15 | ffi as _ffi, |
Daniel Holth | 079c963 | 2019-11-17 22:45:52 -0500 | [diff] [blame] | 16 | from_buffer as _from_buffer, |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 17 | lib as _lib, |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 18 | make_assert as _make_assert, |
Hynek Schlawack | aa86121 | 2016-03-13 13:53:48 +0100 | [diff] [blame] | 19 | native as _native, |
Jean-Paul Calderone | 55f9e88 | 2015-04-12 09:31:03 -0400 | [diff] [blame] | 20 | path_string as _path_string, |
Hynek Schlawack | aa86121 | 2016-03-13 13:53:48 +0100 | [diff] [blame] | 21 | text_to_bytes_and_warn as _text_to_bytes_and_warn, |
Cory Benfield | e62840e | 2016-11-28 12:17:08 +0000 | [diff] [blame] | 22 | no_zero_allocator as _no_zero_allocator, |
Jean-Paul Calderone | 55f9e88 | 2015-04-12 09:31:03 -0400 | [diff] [blame] | 23 | ) |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 24 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 25 | from OpenSSL.crypto import ( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 26 | FILETYPE_PEM, |
| 27 | _PassphraseHelper, |
| 28 | PKey, |
| 29 | X509Name, |
| 30 | X509, |
| 31 | X509Store, |
| 32 | ) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 33 | |
Nicolas Karolak | 736c621 | 2017-11-26 14:40:28 +0100 | [diff] [blame] | 34 | __all__ = [ |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 35 | "OPENSSL_VERSION_NUMBER", |
| 36 | "SSLEAY_VERSION", |
| 37 | "SSLEAY_CFLAGS", |
| 38 | "SSLEAY_PLATFORM", |
| 39 | "SSLEAY_DIR", |
| 40 | "SSLEAY_BUILT_ON", |
| 41 | "SENT_SHUTDOWN", |
| 42 | "RECEIVED_SHUTDOWN", |
| 43 | "SSLv2_METHOD", |
| 44 | "SSLv3_METHOD", |
| 45 | "SSLv23_METHOD", |
| 46 | "TLSv1_METHOD", |
| 47 | "TLSv1_1_METHOD", |
| 48 | "TLSv1_2_METHOD", |
| 49 | "OP_NO_SSLv2", |
| 50 | "OP_NO_SSLv3", |
| 51 | "OP_NO_TLSv1", |
| 52 | "OP_NO_TLSv1_1", |
| 53 | "OP_NO_TLSv1_2", |
| 54 | "OP_NO_TLSv1_3", |
| 55 | "MODE_RELEASE_BUFFERS", |
| 56 | "OP_SINGLE_DH_USE", |
| 57 | "OP_SINGLE_ECDH_USE", |
| 58 | "OP_EPHEMERAL_RSA", |
| 59 | "OP_MICROSOFT_SESS_ID_BUG", |
| 60 | "OP_NETSCAPE_CHALLENGE_BUG", |
| 61 | "OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG", |
| 62 | "OP_SSLREF2_REUSE_CERT_TYPE_BUG", |
| 63 | "OP_MICROSOFT_BIG_SSLV3_BUFFER", |
| 64 | "OP_MSIE_SSLV2_RSA_PADDING", |
| 65 | "OP_SSLEAY_080_CLIENT_DH_BUG", |
| 66 | "OP_TLS_D5_BUG", |
| 67 | "OP_TLS_BLOCK_PADDING_BUG", |
| 68 | "OP_DONT_INSERT_EMPTY_FRAGMENTS", |
| 69 | "OP_CIPHER_SERVER_PREFERENCE", |
| 70 | "OP_TLS_ROLLBACK_BUG", |
| 71 | "OP_PKCS1_CHECK_1", |
| 72 | "OP_PKCS1_CHECK_2", |
| 73 | "OP_NETSCAPE_CA_DN_BUG", |
| 74 | "OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG", |
| 75 | "OP_NO_COMPRESSION", |
| 76 | "OP_NO_QUERY_MTU", |
| 77 | "OP_COOKIE_EXCHANGE", |
| 78 | "OP_NO_TICKET", |
| 79 | "OP_ALL", |
| 80 | "VERIFY_PEER", |
| 81 | "VERIFY_FAIL_IF_NO_PEER_CERT", |
| 82 | "VERIFY_CLIENT_ONCE", |
| 83 | "VERIFY_NONE", |
| 84 | "SESS_CACHE_OFF", |
| 85 | "SESS_CACHE_CLIENT", |
| 86 | "SESS_CACHE_SERVER", |
| 87 | "SESS_CACHE_BOTH", |
| 88 | "SESS_CACHE_NO_AUTO_CLEAR", |
| 89 | "SESS_CACHE_NO_INTERNAL_LOOKUP", |
| 90 | "SESS_CACHE_NO_INTERNAL_STORE", |
| 91 | "SESS_CACHE_NO_INTERNAL", |
| 92 | "SSL_ST_CONNECT", |
| 93 | "SSL_ST_ACCEPT", |
| 94 | "SSL_ST_MASK", |
| 95 | "SSL_CB_LOOP", |
| 96 | "SSL_CB_EXIT", |
| 97 | "SSL_CB_READ", |
| 98 | "SSL_CB_WRITE", |
| 99 | "SSL_CB_ALERT", |
| 100 | "SSL_CB_READ_ALERT", |
| 101 | "SSL_CB_WRITE_ALERT", |
| 102 | "SSL_CB_ACCEPT_LOOP", |
| 103 | "SSL_CB_ACCEPT_EXIT", |
| 104 | "SSL_CB_CONNECT_LOOP", |
| 105 | "SSL_CB_CONNECT_EXIT", |
| 106 | "SSL_CB_HANDSHAKE_START", |
| 107 | "SSL_CB_HANDSHAKE_DONE", |
| 108 | "Error", |
| 109 | "WantReadError", |
| 110 | "WantWriteError", |
| 111 | "WantX509LookupError", |
| 112 | "ZeroReturnError", |
| 113 | "SysCallError", |
| 114 | "SSLeay_version", |
| 115 | "Session", |
| 116 | "Context", |
| 117 | "Connection", |
Nicolas Karolak | 736c621 | 2017-11-26 14:40:28 +0100 | [diff] [blame] | 118 | ] |
| 119 | |
Jean-Paul Calderone | 8fb5318 | 2013-12-30 08:35:49 -0500 | [diff] [blame] | 120 | try: |
Markus Unterwaditzer | 8e41d02 | 2014-04-19 12:27:11 +0200 | [diff] [blame] | 121 | _buffer = buffer |
| 122 | except NameError: |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 123 | |
Markus Unterwaditzer | 8e41d02 | 2014-04-19 12:27:11 +0200 | [diff] [blame] | 124 | class _buffer(object): |
| 125 | pass |
| 126 | |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 127 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 128 | OPENSSL_VERSION_NUMBER = _lib.OPENSSL_VERSION_NUMBER |
| 129 | SSLEAY_VERSION = _lib.SSLEAY_VERSION |
| 130 | SSLEAY_CFLAGS = _lib.SSLEAY_CFLAGS |
| 131 | SSLEAY_PLATFORM = _lib.SSLEAY_PLATFORM |
| 132 | SSLEAY_DIR = _lib.SSLEAY_DIR |
| 133 | SSLEAY_BUILT_ON = _lib.SSLEAY_BUILT_ON |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 134 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 135 | SENT_SHUTDOWN = _lib.SSL_SENT_SHUTDOWN |
| 136 | RECEIVED_SHUTDOWN = _lib.SSL_RECEIVED_SHUTDOWN |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 137 | |
| 138 | SSLv2_METHOD = 1 |
| 139 | SSLv3_METHOD = 2 |
| 140 | SSLv23_METHOD = 3 |
| 141 | TLSv1_METHOD = 4 |
Jean-Paul Calderone | 56bff94 | 2013-11-03 11:30:43 -0500 | [diff] [blame] | 142 | TLSv1_1_METHOD = 5 |
| 143 | TLSv1_2_METHOD = 6 |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 144 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 145 | OP_NO_SSLv2 = _lib.SSL_OP_NO_SSLv2 |
| 146 | OP_NO_SSLv3 = _lib.SSL_OP_NO_SSLv3 |
| 147 | OP_NO_TLSv1 = _lib.SSL_OP_NO_TLSv1 |
Alex Gaynor | 336d802 | 2017-06-29 21:46:42 -0700 | [diff] [blame] | 148 | OP_NO_TLSv1_1 = _lib.SSL_OP_NO_TLSv1_1 |
| 149 | OP_NO_TLSv1_2 = _lib.SSL_OP_NO_TLSv1_2 |
Nathaniel J. Smith | a181373 | 2019-08-01 21:32:13 -0700 | [diff] [blame] | 150 | try: |
| 151 | OP_NO_TLSv1_3 = _lib.SSL_OP_NO_TLSv1_3 |
| 152 | except AttributeError: |
| 153 | pass |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 154 | |
Alex Gaynor | bf01287 | 2016-06-04 13:18:39 -0700 | [diff] [blame] | 155 | MODE_RELEASE_BUFFERS = _lib.SSL_MODE_RELEASE_BUFFERS |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 156 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 157 | OP_SINGLE_DH_USE = _lib.SSL_OP_SINGLE_DH_USE |
Akihiro Yamazaki | e64d80c | 2015-09-06 00:16:57 +0900 | [diff] [blame] | 158 | OP_SINGLE_ECDH_USE = _lib.SSL_OP_SINGLE_ECDH_USE |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 159 | OP_EPHEMERAL_RSA = _lib.SSL_OP_EPHEMERAL_RSA |
| 160 | OP_MICROSOFT_SESS_ID_BUG = _lib.SSL_OP_MICROSOFT_SESS_ID_BUG |
| 161 | OP_NETSCAPE_CHALLENGE_BUG = _lib.SSL_OP_NETSCAPE_CHALLENGE_BUG |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 162 | OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG = ( |
| 163 | _lib.SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG |
| 164 | ) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 165 | OP_SSLREF2_REUSE_CERT_TYPE_BUG = _lib.SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG |
| 166 | OP_MICROSOFT_BIG_SSLV3_BUFFER = _lib.SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER |
Alex Gaynor | 5bb2bd1 | 2016-07-03 10:48:32 -0400 | [diff] [blame] | 167 | OP_MSIE_SSLV2_RSA_PADDING = _lib.SSL_OP_MSIE_SSLV2_RSA_PADDING |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 168 | OP_SSLEAY_080_CLIENT_DH_BUG = _lib.SSL_OP_SSLEAY_080_CLIENT_DH_BUG |
| 169 | OP_TLS_D5_BUG = _lib.SSL_OP_TLS_D5_BUG |
| 170 | OP_TLS_BLOCK_PADDING_BUG = _lib.SSL_OP_TLS_BLOCK_PADDING_BUG |
| 171 | OP_DONT_INSERT_EMPTY_FRAGMENTS = _lib.SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS |
| 172 | OP_CIPHER_SERVER_PREFERENCE = _lib.SSL_OP_CIPHER_SERVER_PREFERENCE |
| 173 | OP_TLS_ROLLBACK_BUG = _lib.SSL_OP_TLS_ROLLBACK_BUG |
| 174 | OP_PKCS1_CHECK_1 = _lib.SSL_OP_PKCS1_CHECK_1 |
| 175 | OP_PKCS1_CHECK_2 = _lib.SSL_OP_PKCS1_CHECK_2 |
| 176 | OP_NETSCAPE_CA_DN_BUG = _lib.SSL_OP_NETSCAPE_CA_DN_BUG |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 177 | OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG = ( |
| 178 | _lib.SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG |
| 179 | ) |
Alex Gaynor | bf01287 | 2016-06-04 13:18:39 -0700 | [diff] [blame] | 180 | OP_NO_COMPRESSION = _lib.SSL_OP_NO_COMPRESSION |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 181 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 182 | OP_NO_QUERY_MTU = _lib.SSL_OP_NO_QUERY_MTU |
| 183 | OP_COOKIE_EXCHANGE = _lib.SSL_OP_COOKIE_EXCHANGE |
Alex Gaynor | 5bb2bd1 | 2016-07-03 10:48:32 -0400 | [diff] [blame] | 184 | OP_NO_TICKET = _lib.SSL_OP_NO_TICKET |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 185 | |
Alex Gaynor | c488981 | 2015-09-04 08:43:17 -0400 | [diff] [blame] | 186 | OP_ALL = _lib.SSL_OP_ALL |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 187 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 188 | VERIFY_PEER = _lib.SSL_VERIFY_PEER |
| 189 | VERIFY_FAIL_IF_NO_PEER_CERT = _lib.SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
| 190 | VERIFY_CLIENT_ONCE = _lib.SSL_VERIFY_CLIENT_ONCE |
| 191 | VERIFY_NONE = _lib.SSL_VERIFY_NONE |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 192 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 193 | SESS_CACHE_OFF = _lib.SSL_SESS_CACHE_OFF |
| 194 | SESS_CACHE_CLIENT = _lib.SSL_SESS_CACHE_CLIENT |
| 195 | SESS_CACHE_SERVER = _lib.SSL_SESS_CACHE_SERVER |
| 196 | SESS_CACHE_BOTH = _lib.SSL_SESS_CACHE_BOTH |
| 197 | SESS_CACHE_NO_AUTO_CLEAR = _lib.SSL_SESS_CACHE_NO_AUTO_CLEAR |
| 198 | SESS_CACHE_NO_INTERNAL_LOOKUP = _lib.SSL_SESS_CACHE_NO_INTERNAL_LOOKUP |
| 199 | SESS_CACHE_NO_INTERNAL_STORE = _lib.SSL_SESS_CACHE_NO_INTERNAL_STORE |
| 200 | SESS_CACHE_NO_INTERNAL = _lib.SSL_SESS_CACHE_NO_INTERNAL |
Jean-Paul Calderone | d39a3f6 | 2013-03-04 12:23:51 -0800 | [diff] [blame] | 201 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 202 | SSL_ST_CONNECT = _lib.SSL_ST_CONNECT |
| 203 | SSL_ST_ACCEPT = _lib.SSL_ST_ACCEPT |
| 204 | SSL_ST_MASK = _lib.SSL_ST_MASK |
Alex Gaynor | 5af32d0 | 2016-09-24 01:52:21 -0400 | [diff] [blame] | 205 | if _lib.Cryptography_HAS_SSL_ST: |
| 206 | SSL_ST_INIT = _lib.SSL_ST_INIT |
| 207 | SSL_ST_BEFORE = _lib.SSL_ST_BEFORE |
| 208 | SSL_ST_OK = _lib.SSL_ST_OK |
| 209 | SSL_ST_RENEGOTIATE = _lib.SSL_ST_RENEGOTIATE |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 210 | __all__.extend( |
| 211 | ["SSL_ST_INIT", "SSL_ST_BEFORE", "SSL_ST_OK", "SSL_ST_RENEGOTIATE"] |
| 212 | ) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 213 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 214 | SSL_CB_LOOP = _lib.SSL_CB_LOOP |
| 215 | SSL_CB_EXIT = _lib.SSL_CB_EXIT |
| 216 | SSL_CB_READ = _lib.SSL_CB_READ |
| 217 | SSL_CB_WRITE = _lib.SSL_CB_WRITE |
| 218 | SSL_CB_ALERT = _lib.SSL_CB_ALERT |
| 219 | SSL_CB_READ_ALERT = _lib.SSL_CB_READ_ALERT |
| 220 | SSL_CB_WRITE_ALERT = _lib.SSL_CB_WRITE_ALERT |
| 221 | SSL_CB_ACCEPT_LOOP = _lib.SSL_CB_ACCEPT_LOOP |
| 222 | SSL_CB_ACCEPT_EXIT = _lib.SSL_CB_ACCEPT_EXIT |
| 223 | SSL_CB_CONNECT_LOOP = _lib.SSL_CB_CONNECT_LOOP |
| 224 | SSL_CB_CONNECT_EXIT = _lib.SSL_CB_CONNECT_EXIT |
| 225 | SSL_CB_HANDSHAKE_START = _lib.SSL_CB_HANDSHAKE_START |
| 226 | SSL_CB_HANDSHAKE_DONE = _lib.SSL_CB_HANDSHAKE_DONE |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 227 | |
Paul Kehrer | 55fb341 | 2017-06-29 18:44:08 -0500 | [diff] [blame] | 228 | # Taken from https://golang.org/src/crypto/x509/root_linux.go |
| 229 | _CERTIFICATE_FILE_LOCATIONS = [ |
| 230 | "/etc/ssl/certs/ca-certificates.crt", # Debian/Ubuntu/Gentoo etc. |
| 231 | "/etc/pki/tls/certs/ca-bundle.crt", # Fedora/RHEL 6 |
| 232 | "/etc/ssl/ca-bundle.pem", # OpenSUSE |
| 233 | "/etc/pki/tls/cacert.pem", # OpenELEC |
| 234 | "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", # CentOS/RHEL 7 |
| 235 | ] |
| 236 | |
| 237 | _CERTIFICATE_PATH_LOCATIONS = [ |
| 238 | "/etc/ssl/certs", # SLES10/SLES11 |
| 239 | ] |
| 240 | |
Paul Kehrer | a92a1a7 | 2017-07-19 15:53:23 +0200 | [diff] [blame] | 241 | # These values are compared to output from cffi's ffi.string so they must be |
| 242 | # byte strings. |
| 243 | _CRYPTOGRAPHY_MANYLINUX1_CA_DIR = b"/opt/pyca/cryptography/openssl/certs" |
| 244 | _CRYPTOGRAPHY_MANYLINUX1_CA_FILE = b"/opt/pyca/cryptography/openssl/cert.pem" |
Paul Kehrer | 55fb341 | 2017-06-29 18:44:08 -0500 | [diff] [blame] | 245 | |
Alex Gaynor | 8328495 | 2015-09-05 10:43:30 -0400 | [diff] [blame] | 246 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 247 | class Error(Exception): |
Jean-Paul Calderone | 511cde0 | 2013-12-29 10:31:13 -0500 | [diff] [blame] | 248 | """ |
| 249 | An error occurred in an `OpenSSL.SSL` API. |
| 250 | """ |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 251 | |
| 252 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 253 | _raise_current_error = partial(_exception_from_error_queue, Error) |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 254 | _openssl_assert = _make_assert(Error) |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 255 | |
| 256 | |
| 257 | class WantReadError(Error): |
| 258 | pass |
| 259 | |
| 260 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 261 | class WantWriteError(Error): |
| 262 | pass |
| 263 | |
| 264 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 265 | class WantX509LookupError(Error): |
| 266 | pass |
| 267 | |
| 268 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 269 | class ZeroReturnError(Error): |
| 270 | pass |
| 271 | |
| 272 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 273 | class SysCallError(Error): |
| 274 | pass |
| 275 | |
| 276 | |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 277 | class _CallbackExceptionHelper(object): |
| 278 | """ |
| 279 | A base class for wrapper classes that allow for intelligent exception |
| 280 | handling in OpenSSL callbacks. |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 281 | |
Jean-Paul Calderone | 1b17298 | 2015-03-22 19:37:11 -0400 | [diff] [blame] | 282 | :ivar list _problems: Any exceptions that occurred while executing in a |
| 283 | context where they could not be raised in the normal way. Typically |
| 284 | this is because OpenSSL has called into some Python code and requires a |
| 285 | return value. The exceptions are saved to be raised later when it is |
| 286 | possible to do so. |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 287 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 288 | |
Jean-Paul Calderone | 09540d7 | 2015-03-22 19:37:20 -0400 | [diff] [blame] | 289 | def __init__(self): |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 290 | self._problems = [] |
| 291 | |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 292 | def raise_if_problem(self): |
Jean-Paul Calderone | 1b17298 | 2015-03-22 19:37:11 -0400 | [diff] [blame] | 293 | """ |
| 294 | Raise an exception from the OpenSSL error queue or that was previously |
| 295 | captured whe running a callback. |
| 296 | """ |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 297 | if self._problems: |
| 298 | try: |
| 299 | _raise_current_error() |
| 300 | except Error: |
| 301 | pass |
| 302 | raise self._problems.pop(0) |
| 303 | |
| 304 | |
| 305 | class _VerifyHelper(_CallbackExceptionHelper): |
Jean-Paul Calderone | 1b17298 | 2015-03-22 19:37:11 -0400 | [diff] [blame] | 306 | """ |
| 307 | Wrap a callback such that it can be used as a certificate verification |
| 308 | callback. |
| 309 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 310 | |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 311 | def __init__(self, callback): |
Jean-Paul Calderone | 837f403 | 2015-03-22 17:38:28 -0400 | [diff] [blame] | 312 | _CallbackExceptionHelper.__init__(self) |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 313 | |
| 314 | @wraps(callback) |
| 315 | def wrapper(ok, store_ctx): |
Paul Kehrer | e738186 | 2017-11-30 20:55:25 +0800 | [diff] [blame] | 316 | x509 = _lib.X509_STORE_CTX_get_current_cert(store_ctx) |
| 317 | _lib.X509_up_ref(x509) |
| 318 | cert = X509._from_raw_x509_ptr(x509) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 319 | error_number = _lib.X509_STORE_CTX_get_error(store_ctx) |
| 320 | error_depth = _lib.X509_STORE_CTX_get_error_depth(store_ctx) |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 321 | |
Jean-Paul Calderone | 6a8cd11 | 2014-04-02 21:09:08 -0400 | [diff] [blame] | 322 | index = _lib.SSL_get_ex_data_X509_STORE_CTX_idx() |
| 323 | ssl = _lib.X509_STORE_CTX_get_ex_data(store_ctx, index) |
| 324 | connection = Connection._reverse_mapping[ssl] |
| 325 | |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 326 | try: |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 327 | result = callback( |
| 328 | connection, cert, error_number, error_depth, ok |
| 329 | ) |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 330 | except Exception as e: |
| 331 | self._problems.append(e) |
| 332 | return 0 |
| 333 | else: |
| 334 | if result: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 335 | _lib.X509_STORE_CTX_set_error(store_ctx, _lib.X509_V_OK) |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 336 | return 1 |
| 337 | else: |
| 338 | return 0 |
| 339 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 340 | self.callback = _ffi.callback( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 341 | "int (*)(int, X509_STORE_CTX *)", wrapper |
| 342 | ) |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 343 | |
| 344 | |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 345 | class _NpnAdvertiseHelper(_CallbackExceptionHelper): |
Jean-Paul Calderone | 1b17298 | 2015-03-22 19:37:11 -0400 | [diff] [blame] | 346 | """ |
| 347 | Wrap a callback such that it can be used as an NPN advertisement callback. |
| 348 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 349 | |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 350 | def __init__(self, callback): |
Jean-Paul Calderone | 837f403 | 2015-03-22 17:38:28 -0400 | [diff] [blame] | 351 | _CallbackExceptionHelper.__init__(self) |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 352 | |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 353 | @wraps(callback) |
| 354 | def wrapper(ssl, out, outlen, arg): |
| 355 | try: |
| 356 | conn = Connection._reverse_mapping[ssl] |
| 357 | protos = callback(conn) |
| 358 | |
| 359 | # Join the protocols into a Python bytestring, length-prefixing |
| 360 | # each element. |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 361 | protostr = b"".join( |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 362 | chain.from_iterable((int2byte(len(p)), p) for p in protos) |
| 363 | ) |
| 364 | |
| 365 | # Save our callback arguments on the connection object. This is |
| 366 | # done to make sure that they don't get freed before OpenSSL |
| 367 | # uses them. Then, return them appropriately in the output |
| 368 | # parameters. |
| 369 | conn._npn_advertise_callback_args = [ |
| 370 | _ffi.new("unsigned int *", len(protostr)), |
| 371 | _ffi.new("unsigned char[]", protostr), |
| 372 | ] |
| 373 | outlen[0] = conn._npn_advertise_callback_args[0][0] |
| 374 | out[0] = conn._npn_advertise_callback_args[1] |
| 375 | return 0 |
| 376 | except Exception as e: |
| 377 | self._problems.append(e) |
| 378 | return 2 # SSL_TLSEXT_ERR_ALERT_FATAL |
| 379 | |
| 380 | self.callback = _ffi.callback( |
| 381 | "int (*)(SSL *, const unsigned char **, unsigned int *, void *)", |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 382 | wrapper, |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 383 | ) |
| 384 | |
| 385 | |
| 386 | class _NpnSelectHelper(_CallbackExceptionHelper): |
Jean-Paul Calderone | 1b17298 | 2015-03-22 19:37:11 -0400 | [diff] [blame] | 387 | """ |
| 388 | Wrap a callback such that it can be used as an NPN selection callback. |
| 389 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 390 | |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 391 | def __init__(self, callback): |
Jean-Paul Calderone | 837f403 | 2015-03-22 17:38:28 -0400 | [diff] [blame] | 392 | _CallbackExceptionHelper.__init__(self) |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 393 | |
| 394 | @wraps(callback) |
| 395 | def wrapper(ssl, out, outlen, in_, inlen, arg): |
| 396 | try: |
| 397 | conn = Connection._reverse_mapping[ssl] |
| 398 | |
| 399 | # The string passed to us is actually made up of multiple |
| 400 | # length-prefixed bytestrings. We need to split that into a |
| 401 | # list. |
| 402 | instr = _ffi.buffer(in_, inlen)[:] |
| 403 | protolist = [] |
| 404 | while instr: |
Alex Gaynor | c3697ad | 2017-11-20 08:19:32 -0500 | [diff] [blame] | 405 | length = indexbytes(instr, 0) |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 406 | proto = instr[1 : length + 1] |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 407 | protolist.append(proto) |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 408 | instr = instr[length + 1 :] |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 409 | |
| 410 | # Call the callback |
| 411 | outstr = callback(conn, protolist) |
| 412 | |
| 413 | # Save our callback arguments on the connection object. This is |
| 414 | # done to make sure that they don't get freed before OpenSSL |
| 415 | # uses them. Then, return them appropriately in the output |
| 416 | # parameters. |
| 417 | conn._npn_select_callback_args = [ |
| 418 | _ffi.new("unsigned char *", len(outstr)), |
| 419 | _ffi.new("unsigned char[]", outstr), |
| 420 | ] |
| 421 | outlen[0] = conn._npn_select_callback_args[0][0] |
| 422 | out[0] = conn._npn_select_callback_args[1] |
| 423 | return 0 |
| 424 | except Exception as e: |
| 425 | self._problems.append(e) |
| 426 | return 2 # SSL_TLSEXT_ERR_ALERT_FATAL |
| 427 | |
| 428 | self.callback = _ffi.callback( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 429 | ( |
| 430 | "int (*)(SSL *, unsigned char **, unsigned char *, " |
| 431 | "const unsigned char *, unsigned int, void *)" |
| 432 | ), |
| 433 | wrapper, |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 434 | ) |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 435 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 436 | |
Mark Williams | 5d890a0 | 2019-11-17 19:56:26 -0800 | [diff] [blame] | 437 | NO_OVERLAPPING_PROTOCOLS = object() |
| 438 | |
| 439 | |
Cory Benfield | 9da5ffb | 2015-04-13 17:20:14 -0400 | [diff] [blame] | 440 | class _ALPNSelectHelper(_CallbackExceptionHelper): |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 441 | """ |
| 442 | Wrap a callback such that it can be used as an ALPN selection callback. |
| 443 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 444 | |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 445 | def __init__(self, callback): |
| 446 | _CallbackExceptionHelper.__init__(self) |
| 447 | |
| 448 | @wraps(callback) |
| 449 | def wrapper(ssl, out, outlen, in_, inlen, arg): |
| 450 | try: |
| 451 | conn = Connection._reverse_mapping[ssl] |
| 452 | |
| 453 | # The string passed to us is made up of multiple |
| 454 | # length-prefixed bytestrings. We need to split that into a |
| 455 | # list. |
| 456 | instr = _ffi.buffer(in_, inlen)[:] |
| 457 | protolist = [] |
| 458 | while instr: |
Cory Benfield | 93134db | 2015-04-13 17:22:13 -0400 | [diff] [blame] | 459 | encoded_len = indexbytes(instr, 0) |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 460 | proto = instr[1 : encoded_len + 1] |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 461 | protolist.append(proto) |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 462 | instr = instr[encoded_len + 1 :] |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 463 | |
| 464 | # Call the callback |
Mark Williams | 5d890a0 | 2019-11-17 19:56:26 -0800 | [diff] [blame] | 465 | outbytes = callback(conn, protolist) |
| 466 | any_accepted = True |
| 467 | if outbytes is NO_OVERLAPPING_PROTOCOLS: |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 468 | outbytes = b"" |
Mark Williams | 5d890a0 | 2019-11-17 19:56:26 -0800 | [diff] [blame] | 469 | any_accepted = False |
Alex Gaynor | 1257600 | 2019-11-18 00:18:50 -0500 | [diff] [blame] | 470 | elif not isinstance(outbytes, bytes): |
Mark Williams | 5d890a0 | 2019-11-17 19:56:26 -0800 | [diff] [blame] | 471 | raise TypeError( |
| 472 | "ALPN callback must return a bytestring or the " |
| 473 | "special NO_OVERLAPPING_PROTOCOLS sentinel value." |
| 474 | ) |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 475 | |
| 476 | # Save our callback arguments on the connection object to make |
| 477 | # sure that they don't get freed before OpenSSL can use them. |
| 478 | # Then, return them in the appropriate output parameters. |
| 479 | conn._alpn_select_callback_args = [ |
Mark Williams | 5d890a0 | 2019-11-17 19:56:26 -0800 | [diff] [blame] | 480 | _ffi.new("unsigned char *", len(outbytes)), |
| 481 | _ffi.new("unsigned char[]", outbytes), |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 482 | ] |
| 483 | outlen[0] = conn._alpn_select_callback_args[0][0] |
| 484 | out[0] = conn._alpn_select_callback_args[1] |
Mark Williams | 5d890a0 | 2019-11-17 19:56:26 -0800 | [diff] [blame] | 485 | if not any_accepted: |
| 486 | return _lib.SSL_TLSEXT_ERR_NOACK |
| 487 | return _lib.SSL_TLSEXT_ERR_OK |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 488 | except Exception as e: |
| 489 | self._problems.append(e) |
Mark Williams | 5d890a0 | 2019-11-17 19:56:26 -0800 | [diff] [blame] | 490 | return _lib.SSL_TLSEXT_ERR_ALERT_FATAL |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 491 | |
| 492 | self.callback = _ffi.callback( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 493 | ( |
| 494 | "int (*)(SSL *, unsigned char **, unsigned char *, " |
| 495 | "const unsigned char *, unsigned int, void *)" |
| 496 | ), |
| 497 | wrapper, |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 498 | ) |
| 499 | |
| 500 | |
Cory Benfield | 496652a | 2017-01-24 11:42:56 +0000 | [diff] [blame] | 501 | class _OCSPServerCallbackHelper(_CallbackExceptionHelper): |
| 502 | """ |
| 503 | Wrap a callback such that it can be used as an OCSP callback for the server |
| 504 | side. |
| 505 | |
| 506 | Annoyingly, OpenSSL defines one OCSP callback but uses it in two different |
| 507 | ways. For servers, that callback is expected to retrieve some OCSP data and |
| 508 | hand it to OpenSSL, and may return only SSL_TLSEXT_ERR_OK, |
| 509 | SSL_TLSEXT_ERR_FATAL, and SSL_TLSEXT_ERR_NOACK. For clients, that callback |
| 510 | is expected to check the OCSP data, and returns a negative value on error, |
| 511 | 0 if the response is not acceptable, or positive if it is. These are |
| 512 | mutually exclusive return code behaviours, and they mean that we need two |
| 513 | helpers so that we always return an appropriate error code if the user's |
| 514 | code throws an exception. |
| 515 | |
| 516 | Given that we have to have two helpers anyway, these helpers are a bit more |
| 517 | helpery than most: specifically, they hide a few more of the OpenSSL |
| 518 | functions so that the user has an easier time writing these callbacks. |
| 519 | |
| 520 | This helper implements the server side. |
| 521 | """ |
| 522 | |
| 523 | def __init__(self, callback): |
| 524 | _CallbackExceptionHelper.__init__(self) |
| 525 | |
| 526 | @wraps(callback) |
| 527 | def wrapper(ssl, cdata): |
| 528 | try: |
| 529 | conn = Connection._reverse_mapping[ssl] |
| 530 | |
| 531 | # Extract the data if any was provided. |
| 532 | if cdata != _ffi.NULL: |
| 533 | data = _ffi.from_handle(cdata) |
| 534 | else: |
| 535 | data = None |
| 536 | |
| 537 | # Call the callback. |
| 538 | ocsp_data = callback(conn, data) |
| 539 | |
Alex Gaynor | 1257600 | 2019-11-18 00:18:50 -0500 | [diff] [blame] | 540 | if not isinstance(ocsp_data, bytes): |
Cory Benfield | 496652a | 2017-01-24 11:42:56 +0000 | [diff] [blame] | 541 | raise TypeError("OCSP callback must return a bytestring.") |
| 542 | |
| 543 | # If the OCSP data was provided, we will pass it to OpenSSL. |
| 544 | # However, we have an early exit here: if no OCSP data was |
| 545 | # provided we will just exit out and tell OpenSSL that there |
| 546 | # is nothing to do. |
| 547 | if not ocsp_data: |
| 548 | return 3 # SSL_TLSEXT_ERR_NOACK |
| 549 | |
David Benjamin | 7ac5f27 | 2018-05-21 21:24:04 -0400 | [diff] [blame] | 550 | # OpenSSL takes ownership of this data and expects it to have |
| 551 | # been allocated by OPENSSL_malloc. |
Cory Benfield | 496652a | 2017-01-24 11:42:56 +0000 | [diff] [blame] | 552 | ocsp_data_length = len(ocsp_data) |
| 553 | data_ptr = _lib.OPENSSL_malloc(ocsp_data_length) |
| 554 | _ffi.buffer(data_ptr, ocsp_data_length)[:] = ocsp_data |
| 555 | |
| 556 | _lib.SSL_set_tlsext_status_ocsp_resp( |
| 557 | ssl, data_ptr, ocsp_data_length |
| 558 | ) |
| 559 | |
| 560 | return 0 |
| 561 | except Exception as e: |
| 562 | self._problems.append(e) |
| 563 | return 2 # SSL_TLSEXT_ERR_ALERT_FATAL |
| 564 | |
| 565 | self.callback = _ffi.callback("int (*)(SSL *, void *)", wrapper) |
| 566 | |
| 567 | |
| 568 | class _OCSPClientCallbackHelper(_CallbackExceptionHelper): |
| 569 | """ |
| 570 | Wrap a callback such that it can be used as an OCSP callback for the client |
| 571 | side. |
| 572 | |
| 573 | Annoyingly, OpenSSL defines one OCSP callback but uses it in two different |
| 574 | ways. For servers, that callback is expected to retrieve some OCSP data and |
| 575 | hand it to OpenSSL, and may return only SSL_TLSEXT_ERR_OK, |
| 576 | SSL_TLSEXT_ERR_FATAL, and SSL_TLSEXT_ERR_NOACK. For clients, that callback |
| 577 | is expected to check the OCSP data, and returns a negative value on error, |
| 578 | 0 if the response is not acceptable, or positive if it is. These are |
| 579 | mutually exclusive return code behaviours, and they mean that we need two |
| 580 | helpers so that we always return an appropriate error code if the user's |
| 581 | code throws an exception. |
| 582 | |
| 583 | Given that we have to have two helpers anyway, these helpers are a bit more |
| 584 | helpery than most: specifically, they hide a few more of the OpenSSL |
| 585 | functions so that the user has an easier time writing these callbacks. |
| 586 | |
| 587 | This helper implements the client side. |
| 588 | """ |
| 589 | |
| 590 | def __init__(self, callback): |
| 591 | _CallbackExceptionHelper.__init__(self) |
| 592 | |
| 593 | @wraps(callback) |
| 594 | def wrapper(ssl, cdata): |
| 595 | try: |
| 596 | conn = Connection._reverse_mapping[ssl] |
| 597 | |
| 598 | # Extract the data if any was provided. |
| 599 | if cdata != _ffi.NULL: |
| 600 | data = _ffi.from_handle(cdata) |
| 601 | else: |
| 602 | data = None |
| 603 | |
| 604 | # Get the OCSP data. |
| 605 | ocsp_ptr = _ffi.new("unsigned char **") |
| 606 | ocsp_len = _lib.SSL_get_tlsext_status_ocsp_resp(ssl, ocsp_ptr) |
| 607 | if ocsp_len < 0: |
| 608 | # No OCSP data. |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 609 | ocsp_data = b"" |
Cory Benfield | 496652a | 2017-01-24 11:42:56 +0000 | [diff] [blame] | 610 | else: |
| 611 | # Copy the OCSP data, then pass it to the callback. |
| 612 | ocsp_data = _ffi.buffer(ocsp_ptr[0], ocsp_len)[:] |
| 613 | |
| 614 | valid = callback(conn, ocsp_data, data) |
| 615 | |
| 616 | # Return 1 on success or 0 on error. |
| 617 | return int(bool(valid)) |
| 618 | |
| 619 | except Exception as e: |
| 620 | self._problems.append(e) |
| 621 | # Return negative value if an exception is hit. |
| 622 | return -1 |
| 623 | |
| 624 | self.callback = _ffi.callback("int (*)(SSL *, void *)", wrapper) |
| 625 | |
| 626 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 627 | def _asFileDescriptor(obj): |
| 628 | fd = None |
Konstantinos Koukopoulos | c8b13ea | 2014-01-28 00:21:50 -0800 | [diff] [blame] | 629 | if not isinstance(obj, integer_types): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 630 | meth = getattr(obj, "fileno", None) |
| 631 | if meth is not None: |
| 632 | obj = meth() |
| 633 | |
Konstantinos Koukopoulos | c8b13ea | 2014-01-28 00:21:50 -0800 | [diff] [blame] | 634 | if isinstance(obj, integer_types): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 635 | fd = obj |
| 636 | |
Konstantinos Koukopoulos | c8b13ea | 2014-01-28 00:21:50 -0800 | [diff] [blame] | 637 | if not isinstance(fd, integer_types): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 638 | raise TypeError("argument must be an int, or have a fileno() method.") |
| 639 | elif fd < 0: |
| 640 | raise ValueError( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 641 | "file descriptor cannot be a negative integer (%i)" % (fd,) |
| 642 | ) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 643 | |
| 644 | return fd |
| 645 | |
| 646 | |
Jean-Paul Calderone | d39a3f6 | 2013-03-04 12:23:51 -0800 | [diff] [blame] | 647 | def SSLeay_version(type): |
| 648 | """ |
| 649 | Return a string describing the version of OpenSSL in use. |
| 650 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 651 | :param type: One of the :const:`SSLEAY_` constants defined in this module. |
Jean-Paul Calderone | d39a3f6 | 2013-03-04 12:23:51 -0800 | [diff] [blame] | 652 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 653 | return _ffi.string(_lib.SSLeay_version(type)) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 654 | |
| 655 | |
Alex Gaynor | be2bd54 | 2019-02-21 21:41:22 -0500 | [diff] [blame] | 656 | def _warn_npn(): |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 657 | warnings.warn( |
| 658 | "NPN is deprecated. Protocols should switch to using ALPN.", |
| 659 | DeprecationWarning, |
| 660 | stacklevel=3, |
| 661 | ) |
Alex Gaynor | be2bd54 | 2019-02-21 21:41:22 -0500 | [diff] [blame] | 662 | |
| 663 | |
Cory Benfield | ef404df | 2016-03-29 15:32:48 +0100 | [diff] [blame] | 664 | def _make_requires(flag, error): |
Cory Benfield | a876cef | 2015-04-13 17:29:12 -0400 | [diff] [blame] | 665 | """ |
Cory Benfield | ef404df | 2016-03-29 15:32:48 +0100 | [diff] [blame] | 666 | Builds a decorator that ensures that functions that rely on OpenSSL |
| 667 | functions that are not present in this build raise NotImplementedError, |
| 668 | rather than AttributeError coming out of cryptography. |
| 669 | |
| 670 | :param flag: A cryptography flag that guards the functions, e.g. |
| 671 | ``Cryptography_HAS_NEXTPROTONEG``. |
| 672 | :param error: The string to be used in the exception if the flag is false. |
Cory Benfield | a876cef | 2015-04-13 17:29:12 -0400 | [diff] [blame] | 673 | """ |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 674 | |
Cory Benfield | ef404df | 2016-03-29 15:32:48 +0100 | [diff] [blame] | 675 | def _requires_decorator(func): |
| 676 | if not flag: |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 677 | |
Cory Benfield | ef404df | 2016-03-29 15:32:48 +0100 | [diff] [blame] | 678 | @wraps(func) |
| 679 | def explode(*args, **kwargs): |
| 680 | raise NotImplementedError(error) |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 681 | |
Cory Benfield | ef404df | 2016-03-29 15:32:48 +0100 | [diff] [blame] | 682 | return explode |
| 683 | else: |
| 684 | return func |
Cory Benfield | 10b277f | 2015-04-13 17:12:42 -0400 | [diff] [blame] | 685 | |
Cory Benfield | ef404df | 2016-03-29 15:32:48 +0100 | [diff] [blame] | 686 | return _requires_decorator |
Cory Benfield | 10b277f | 2015-04-13 17:12:42 -0400 | [diff] [blame] | 687 | |
| 688 | |
Cory Benfield | ef404df | 2016-03-29 15:32:48 +0100 | [diff] [blame] | 689 | _requires_npn = _make_requires( |
| 690 | _lib.Cryptography_HAS_NEXTPROTONEG, "NPN not available" |
| 691 | ) |
Cory Benfield | 7907e33 | 2015-04-13 17:18:25 -0400 | [diff] [blame] | 692 | |
| 693 | |
Cory Benfield | ef404df | 2016-03-29 15:32:48 +0100 | [diff] [blame] | 694 | _requires_alpn = _make_requires( |
| 695 | _lib.Cryptography_HAS_ALPN, "ALPN not available" |
| 696 | ) |
Cory Benfield | e6f3588 | 2016-03-29 11:21:04 +0100 | [diff] [blame] | 697 | |
Cory Benfield | e6f3588 | 2016-03-29 11:21:04 +0100 | [diff] [blame] | 698 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 699 | class Session(object): |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 700 | """ |
| 701 | A class representing an SSL session. A session defines certain connection |
| 702 | parameters which may be re-used to speed up the setup of subsequent |
| 703 | connections. |
| 704 | |
| 705 | .. versionadded:: 0.14 |
| 706 | """ |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 707 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 708 | pass |
| 709 | |
| 710 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 711 | class Context(object): |
| 712 | """ |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 713 | :class:`OpenSSL.SSL.Context` instances define the parameters for setting |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 714 | up new SSL connections. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 715 | |
| 716 | :param method: One of SSLv2_METHOD, SSLv3_METHOD, SSLv23_METHOD, or |
| 717 | TLSv1_METHOD. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 718 | """ |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 719 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 720 | _methods = { |
Andrew Dunham | ec84a0a | 2014-02-24 12:41:37 -0800 | [diff] [blame] | 721 | SSLv2_METHOD: "SSLv2_method", |
Jean-Paul Calderone | be2bb42 | 2013-12-29 07:34:08 -0500 | [diff] [blame] | 722 | SSLv3_METHOD: "SSLv3_method", |
| 723 | SSLv23_METHOD: "SSLv23_method", |
| 724 | TLSv1_METHOD: "TLSv1_method", |
| 725 | TLSv1_1_METHOD: "TLSv1_1_method", |
| 726 | TLSv1_2_METHOD: "TLSv1_2_method", |
Alex Gaynor | c488981 | 2015-09-04 08:43:17 -0400 | [diff] [blame] | 727 | } |
Jean-Paul Calderone | be2bb42 | 2013-12-29 07:34:08 -0500 | [diff] [blame] | 728 | _methods = dict( |
| 729 | (identifier, getattr(_lib, name)) |
| 730 | for (identifier, name) in _methods.items() |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 731 | if getattr(_lib, name, None) is not None |
| 732 | ) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 733 | |
| 734 | def __init__(self, method): |
Jean-Paul Calderone | f73a3cb | 2014-02-09 08:49:06 -0500 | [diff] [blame] | 735 | if not isinstance(method, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 736 | raise TypeError("method must be an integer") |
| 737 | |
| 738 | try: |
| 739 | method_func = self._methods[method] |
| 740 | except KeyError: |
| 741 | raise ValueError("No such protocol") |
| 742 | |
| 743 | method_obj = method_func() |
Alex Gaynor | a829e90 | 2016-06-04 18:16:01 -0700 | [diff] [blame] | 744 | _openssl_assert(method_obj != _ffi.NULL) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 745 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 746 | context = _lib.SSL_CTX_new(method_obj) |
Alex Gaynor | a829e90 | 2016-06-04 18:16:01 -0700 | [diff] [blame] | 747 | _openssl_assert(context != _ffi.NULL) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 748 | context = _ffi.gc(context, _lib.SSL_CTX_free) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 749 | |
Alex Gaynor | 77debda | 2020-04-07 13:40:59 -0400 | [diff] [blame] | 750 | # Set SSL_CTX_set_ecdh_auto so that the ECDH curve will be |
| 751 | # auto-selected. This function was added in 1.0.2 and made a noop in |
| 752 | # 1.1.0+ (where it is set automatically). |
| 753 | res = _lib.SSL_CTX_set_ecdh_auto(context, 1) |
| 754 | _openssl_assert(res == 1) |
Paul Kehrer | 6c6bf86 | 2016-12-19 06:03:48 -0600 | [diff] [blame] | 755 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 756 | self._context = context |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 757 | self._passphrase_helper = None |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 758 | self._passphrase_callback = None |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 759 | self._passphrase_userdata = None |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 760 | self._verify_helper = None |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 761 | self._verify_callback = None |
| 762 | self._info_callback = None |
| 763 | self._tlsext_servername_callback = None |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 764 | self._app_data = None |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 765 | self._npn_advertise_helper = None |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 766 | self._npn_advertise_callback = None |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 767 | self._npn_select_helper = None |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 768 | self._npn_select_callback = None |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 769 | self._alpn_select_helper = None |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 770 | self._alpn_select_callback = None |
Cory Benfield | 496652a | 2017-01-24 11:42:56 +0000 | [diff] [blame] | 771 | self._ocsp_helper = None |
| 772 | self._ocsp_callback = None |
| 773 | self._ocsp_data = None |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 774 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 775 | self.set_mode(_lib.SSL_MODE_ENABLE_PARTIAL_WRITE) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 776 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 777 | def load_verify_locations(self, cafile, capath=None): |
| 778 | """ |
| 779 | Let SSL know where we can find trusted certificates for the certificate |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 780 | chain. Note that the certificates have to be in PEM format. |
| 781 | |
| 782 | If capath is passed, it must be a directory prepared using the |
| 783 | ``c_rehash`` tool included with OpenSSL. Either, but not both, of |
| 784 | *pemfile* or *capath* may be :data:`None`. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 785 | |
Jean-Paul Calderone | 55f9e88 | 2015-04-12 09:31:03 -0400 | [diff] [blame] | 786 | :param cafile: In which file we can find the certificates (``bytes`` or |
| 787 | ``unicode``). |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 788 | :param capath: In which directory we can find the certificates |
Jean-Paul Calderone | 55f9e88 | 2015-04-12 09:31:03 -0400 | [diff] [blame] | 789 | (``bytes`` or ``unicode``). |
| 790 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 791 | :return: None |
| 792 | """ |
| 793 | if cafile is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 794 | cafile = _ffi.NULL |
Jean-Paul Calderone | 55f9e88 | 2015-04-12 09:31:03 -0400 | [diff] [blame] | 795 | else: |
| 796 | cafile = _path_string(cafile) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 797 | |
| 798 | if capath is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 799 | capath = _ffi.NULL |
Jean-Paul Calderone | 55f9e88 | 2015-04-12 09:31:03 -0400 | [diff] [blame] | 800 | else: |
| 801 | capath = _path_string(capath) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 802 | |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 803 | load_result = _lib.SSL_CTX_load_verify_locations( |
| 804 | self._context, cafile, capath |
| 805 | ) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 806 | if not load_result: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 807 | _raise_current_error() |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 808 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 809 | def _wrap_callback(self, callback): |
| 810 | @wraps(callback) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 811 | def wrapper(size, verify, userdata): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 812 | return callback(size, verify, self._passphrase_userdata) |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 813 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 814 | return _PassphraseHelper( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 815 | FILETYPE_PEM, wrapper, more_args=True, truncate=True |
| 816 | ) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 817 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 818 | def set_passwd_cb(self, callback, userdata=None): |
| 819 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 820 | Set the passphrase callback. This function will be called |
| 821 | when a private key with a passphrase is loaded. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 822 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 823 | :param callback: The Python callback to use. This must accept three |
| 824 | positional arguments. First, an integer giving the maximum length |
| 825 | of the passphrase it may return. If the returned passphrase is |
| 826 | longer than this, it will be truncated. Second, a boolean value |
| 827 | which will be true if the user should be prompted for the |
| 828 | passphrase twice and the callback should verify that the two values |
| 829 | supplied are equal. Third, the value given as the *userdata* |
| 830 | parameter to :meth:`set_passwd_cb`. The *callback* must return |
| 831 | a byte string. If an error occurs, *callback* should return a false |
| 832 | value (e.g. an empty string). |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 833 | :param userdata: (optional) A Python object which will be given as |
| 834 | argument to the callback |
| 835 | :return: None |
| 836 | """ |
| 837 | if not callable(callback): |
| 838 | raise TypeError("callback must be callable") |
| 839 | |
| 840 | self._passphrase_helper = self._wrap_callback(callback) |
| 841 | self._passphrase_callback = self._passphrase_helper.callback |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 842 | _lib.SSL_CTX_set_default_passwd_cb( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 843 | self._context, self._passphrase_callback |
| 844 | ) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 845 | self._passphrase_userdata = userdata |
| 846 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 847 | def set_default_verify_paths(self): |
| 848 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 849 | Specify that the platform provided CA certificates are to be used for |
| 850 | verification purposes. This method has some caveats related to the |
| 851 | binary wheels that cryptography (pyOpenSSL's primary dependency) ships: |
| 852 | |
| 853 | * macOS will only load certificates using this method if the user has |
| 854 | the ``openssl@1.1`` `Homebrew <https://brew.sh>`_ formula installed |
| 855 | in the default location. |
| 856 | * Windows will not work. |
| 857 | * manylinux1 cryptography wheels will work on most common Linux |
| 858 | distributions in pyOpenSSL 17.1.0 and above. pyOpenSSL detects the |
| 859 | manylinux1 wheel and attempts to load roots via a fallback path. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 860 | |
| 861 | :return: None |
| 862 | """ |
Paul Kehrer | 55fb341 | 2017-06-29 18:44:08 -0500 | [diff] [blame] | 863 | # SSL_CTX_set_default_verify_paths will attempt to load certs from |
| 864 | # both a cafile and capath that are set at compile time. However, |
| 865 | # it will first check environment variables and, if present, load |
| 866 | # those paths instead |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 867 | set_result = _lib.SSL_CTX_set_default_verify_paths(self._context) |
Alex Gaynor | 09f19f5 | 2016-07-03 09:54:09 -0400 | [diff] [blame] | 868 | _openssl_assert(set_result == 1) |
Paul Kehrer | 55fb341 | 2017-06-29 18:44:08 -0500 | [diff] [blame] | 869 | # After attempting to set default_verify_paths we need to know whether |
| 870 | # to go down the fallback path. |
| 871 | # First we'll check to see if any env vars have been set. If so, |
| 872 | # we won't try to do anything else because the user has set the path |
| 873 | # themselves. |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 874 | dir_env_var = _ffi.string(_lib.X509_get_default_cert_dir_env()).decode( |
| 875 | "ascii" |
| 876 | ) |
Paul Kehrer | 55fb341 | 2017-06-29 18:44:08 -0500 | [diff] [blame] | 877 | file_env_var = _ffi.string( |
| 878 | _lib.X509_get_default_cert_file_env() |
| 879 | ).decode("ascii") |
| 880 | if not self._check_env_vars_set(dir_env_var, file_env_var): |
| 881 | default_dir = _ffi.string(_lib.X509_get_default_cert_dir()) |
| 882 | default_file = _ffi.string(_lib.X509_get_default_cert_file()) |
| 883 | # Now we check to see if the default_dir and default_file are set |
| 884 | # to the exact values we use in our manylinux1 builds. If they are |
| 885 | # then we know to load the fallbacks |
| 886 | if ( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 887 | default_dir == _CRYPTOGRAPHY_MANYLINUX1_CA_DIR |
| 888 | and default_file == _CRYPTOGRAPHY_MANYLINUX1_CA_FILE |
Paul Kehrer | 55fb341 | 2017-06-29 18:44:08 -0500 | [diff] [blame] | 889 | ): |
| 890 | # This is manylinux1, let's load our fallback paths |
| 891 | self._fallback_default_verify_paths( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 892 | _CERTIFICATE_FILE_LOCATIONS, _CERTIFICATE_PATH_LOCATIONS |
Paul Kehrer | 55fb341 | 2017-06-29 18:44:08 -0500 | [diff] [blame] | 893 | ) |
| 894 | |
| 895 | def _check_env_vars_set(self, dir_env_var, file_env_var): |
| 896 | """ |
| 897 | Check to see if the default cert dir/file environment vars are present. |
| 898 | |
| 899 | :return: bool |
| 900 | """ |
| 901 | return ( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 902 | os.environ.get(file_env_var) is not None |
| 903 | or os.environ.get(dir_env_var) is not None |
Paul Kehrer | 55fb341 | 2017-06-29 18:44:08 -0500 | [diff] [blame] | 904 | ) |
| 905 | |
| 906 | def _fallback_default_verify_paths(self, file_path, dir_path): |
| 907 | """ |
| 908 | Default verify paths are based on the compiled version of OpenSSL. |
| 909 | However, when pyca/cryptography is compiled as a manylinux1 wheel |
| 910 | that compiled location can potentially be wrong. So, like Go, we |
| 911 | will try a predefined set of paths and attempt to load roots |
| 912 | from there. |
| 913 | |
| 914 | :return: None |
| 915 | """ |
| 916 | for cafile in file_path: |
| 917 | if os.path.isfile(cafile): |
| 918 | self.load_verify_locations(cafile) |
| 919 | break |
| 920 | |
| 921 | for capath in dir_path: |
| 922 | if os.path.isdir(capath): |
| 923 | self.load_verify_locations(None, capath) |
| 924 | break |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 925 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 926 | def use_certificate_chain_file(self, certfile): |
| 927 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 928 | Load a certificate chain from a file. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 929 | |
Jean-Paul Calderone | b6f8a79 | 2015-04-13 10:10:06 -0400 | [diff] [blame] | 930 | :param certfile: The name of the certificate chain file (``bytes`` or |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 931 | ``unicode``). Must be PEM encoded. |
Jean-Paul Calderone | b6f8a79 | 2015-04-13 10:10:06 -0400 | [diff] [blame] | 932 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 933 | :return: None |
| 934 | """ |
Jean-Paul Calderone | aac43a3 | 2015-04-12 09:51:21 -0400 | [diff] [blame] | 935 | certfile = _path_string(certfile) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 936 | |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 937 | result = _lib.SSL_CTX_use_certificate_chain_file( |
| 938 | self._context, certfile |
| 939 | ) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 940 | if not result: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 941 | _raise_current_error() |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 942 | |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 943 | def use_certificate_file(self, certfile, filetype=FILETYPE_PEM): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 944 | """ |
| 945 | Load a certificate from a file |
| 946 | |
Jean-Paul Calderone | b6f8a79 | 2015-04-13 10:10:06 -0400 | [diff] [blame] | 947 | :param certfile: The name of the certificate file (``bytes`` or |
| 948 | ``unicode``). |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 949 | :param filetype: (optional) The encoding of the file, which is either |
| 950 | :const:`FILETYPE_PEM` or :const:`FILETYPE_ASN1`. The default is |
| 951 | :const:`FILETYPE_PEM`. |
Jean-Paul Calderone | b6f8a79 | 2015-04-13 10:10:06 -0400 | [diff] [blame] | 952 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 953 | :return: None |
| 954 | """ |
Jean-Paul Calderone | d57a7b6 | 2015-04-12 09:57:36 -0400 | [diff] [blame] | 955 | certfile = _path_string(certfile) |
Jean-Paul Calderone | f73a3cb | 2014-02-09 08:49:06 -0500 | [diff] [blame] | 956 | if not isinstance(filetype, integer_types): |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 957 | raise TypeError("filetype must be an integer") |
| 958 | |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 959 | use_result = _lib.SSL_CTX_use_certificate_file( |
| 960 | self._context, certfile, filetype |
| 961 | ) |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 962 | if not use_result: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 963 | _raise_current_error() |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 964 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 965 | def use_certificate(self, cert): |
| 966 | """ |
| 967 | Load a certificate from a X509 object |
| 968 | |
| 969 | :param cert: The X509 object |
| 970 | :return: None |
| 971 | """ |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 972 | if not isinstance(cert, X509): |
| 973 | raise TypeError("cert must be an X509 instance") |
| 974 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 975 | use_result = _lib.SSL_CTX_use_certificate(self._context, cert._x509) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 976 | if not use_result: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 977 | _raise_current_error() |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 978 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 979 | def add_extra_chain_cert(self, certobj): |
| 980 | """ |
| 981 | Add certificate to chain |
| 982 | |
| 983 | :param certobj: The X509 certificate object to add to the chain |
| 984 | :return: None |
| 985 | """ |
| 986 | if not isinstance(certobj, X509): |
| 987 | raise TypeError("certobj must be an X509 instance") |
| 988 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 989 | copy = _lib.X509_dup(certobj._x509) |
| 990 | add_result = _lib.SSL_CTX_add_extra_chain_cert(self._context, copy) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 991 | if not add_result: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 992 | # TODO: This is untested. |
| 993 | _lib.X509_free(copy) |
| 994 | _raise_current_error() |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 995 | |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 996 | def _raise_passphrase_exception(self): |
Greg Bowser | 36eb2de | 2017-01-24 11:38:55 -0500 | [diff] [blame] | 997 | if self._passphrase_helper is not None: |
| 998 | self._passphrase_helper.raise_if_problem(Error) |
| 999 | |
| 1000 | _raise_current_error() |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 1001 | |
Jean-Paul Calderone | 00f84eb | 2015-04-13 12:47:21 -0400 | [diff] [blame] | 1002 | def use_privatekey_file(self, keyfile, filetype=_UNSPECIFIED): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1003 | """ |
| 1004 | Load a private key from a file |
| 1005 | |
Jean-Paul Calderone | b6f8a79 | 2015-04-13 10:10:06 -0400 | [diff] [blame] | 1006 | :param keyfile: The name of the key file (``bytes`` or ``unicode``) |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1007 | :param filetype: (optional) The encoding of the file, which is either |
| 1008 | :const:`FILETYPE_PEM` or :const:`FILETYPE_ASN1`. The default is |
| 1009 | :const:`FILETYPE_PEM`. |
Jean-Paul Calderone | b6f8a79 | 2015-04-13 10:10:06 -0400 | [diff] [blame] | 1010 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1011 | :return: None |
| 1012 | """ |
Jean-Paul Calderone | 69a4e5b | 2015-04-12 10:04:28 -0400 | [diff] [blame] | 1013 | keyfile = _path_string(keyfile) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1014 | |
Jean-Paul Calderone | 00f84eb | 2015-04-13 12:47:21 -0400 | [diff] [blame] | 1015 | if filetype is _UNSPECIFIED: |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1016 | filetype = FILETYPE_PEM |
Jean-Paul Calderone | f73a3cb | 2014-02-09 08:49:06 -0500 | [diff] [blame] | 1017 | elif not isinstance(filetype, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1018 | raise TypeError("filetype must be an integer") |
| 1019 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1020 | use_result = _lib.SSL_CTX_use_PrivateKey_file( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1021 | self._context, keyfile, filetype |
| 1022 | ) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1023 | if not use_result: |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 1024 | self._raise_passphrase_exception() |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1025 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1026 | def use_privatekey(self, pkey): |
| 1027 | """ |
| 1028 | Load a private key from a PKey object |
| 1029 | |
| 1030 | :param pkey: The PKey object |
| 1031 | :return: None |
| 1032 | """ |
| 1033 | if not isinstance(pkey, PKey): |
| 1034 | raise TypeError("pkey must be a PKey instance") |
| 1035 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1036 | use_result = _lib.SSL_CTX_use_PrivateKey(self._context, pkey._pkey) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1037 | if not use_result: |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 1038 | self._raise_passphrase_exception() |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1039 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1040 | def check_privatekey(self): |
| 1041 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1042 | Check if the private key (loaded with :meth:`use_privatekey`) matches |
| 1043 | the certificate (loaded with :meth:`use_certificate`) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1044 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1045 | :return: :data:`None` (raises :exc:`Error` if something's wrong) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1046 | """ |
Jean-Paul Calderone | a034492 | 2014-12-11 14:02:31 -0500 | [diff] [blame] | 1047 | if not _lib.SSL_CTX_check_private_key(self._context): |
| 1048 | _raise_current_error() |
| 1049 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1050 | def load_client_ca(self, cafile): |
| 1051 | """ |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1052 | Load the trusted certificates that will be sent to the client. Does |
| 1053 | not actually imply any of the certificates are trusted; that must be |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 1054 | configured separately. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1055 | |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1056 | :param bytes cafile: The path to a certificates file in PEM format. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1057 | :return: None |
| 1058 | """ |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1059 | ca_list = _lib.SSL_load_client_CA_file( |
| 1060 | _text_to_bytes_and_warn("cafile", cafile) |
| 1061 | ) |
| 1062 | _openssl_assert(ca_list != _ffi.NULL) |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1063 | _lib.SSL_CTX_set_client_CA_list(self._context, ca_list) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1064 | |
| 1065 | def set_session_id(self, buf): |
| 1066 | """ |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1067 | Set the session id to *buf* within which a session can be reused for |
| 1068 | this Context object. This is needed when doing session resumption, |
| 1069 | because there is no way for a stored session to know which Context |
| 1070 | object it is associated with. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1071 | |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1072 | :param bytes buf: The session id. |
| 1073 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1074 | :returns: None |
| 1075 | """ |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1076 | buf = _text_to_bytes_and_warn("buf", buf) |
| 1077 | _openssl_assert( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1078 | _lib.SSL_CTX_set_session_id_context(self._context, buf, len(buf),) |
| 1079 | == 1 |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1080 | ) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1081 | |
| 1082 | def set_session_cache_mode(self, mode): |
| 1083 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1084 | Set the behavior of the session cache used by all connections using |
| 1085 | this Context. The previously set mode is returned. See |
| 1086 | :const:`SESS_CACHE_*` for details about particular modes. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1087 | |
| 1088 | :param mode: One or more of the SESS_CACHE_* flags (combine using |
| 1089 | bitwise or) |
| 1090 | :returns: The previously set caching mode. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1091 | |
| 1092 | .. versionadded:: 0.14 |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1093 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1094 | if not isinstance(mode, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1095 | raise TypeError("mode must be an integer") |
| 1096 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1097 | return _lib.SSL_CTX_set_session_cache_mode(self._context, mode) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1098 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1099 | def get_session_cache_mode(self): |
| 1100 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1101 | Get the current session cache mode. |
| 1102 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1103 | :returns: The currently used cache mode. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1104 | |
| 1105 | .. versionadded:: 0.14 |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1106 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1107 | return _lib.SSL_CTX_get_session_cache_mode(self._context) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1108 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1109 | def set_verify(self, mode, callback): |
| 1110 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1111 | et the verification flags for this Context object to *mode* and specify |
| 1112 | that *callback* should be used for verification callbacks. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1113 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1114 | :param mode: The verify mode, this should be one of |
| 1115 | :const:`VERIFY_NONE` and :const:`VERIFY_PEER`. If |
| 1116 | :const:`VERIFY_PEER` is used, *mode* can be OR:ed with |
| 1117 | :const:`VERIFY_FAIL_IF_NO_PEER_CERT` and |
| 1118 | :const:`VERIFY_CLIENT_ONCE` to further control the behaviour. |
| 1119 | :param callback: The Python callback to use. This should take five |
| 1120 | arguments: A Connection object, an X509 object, and three integer |
| 1121 | variables, which are in turn potential error number, error depth |
| 1122 | and return code. *callback* should return True if verification |
| 1123 | passes and False otherwise. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1124 | :return: None |
| 1125 | |
| 1126 | See SSL_CTX_set_verify(3SSL) for further details. |
| 1127 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1128 | if not isinstance(mode, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1129 | raise TypeError("mode must be an integer") |
| 1130 | |
| 1131 | if not callable(callback): |
| 1132 | raise TypeError("callback must be callable") |
| 1133 | |
Jean-Paul Calderone | 6a8cd11 | 2014-04-02 21:09:08 -0400 | [diff] [blame] | 1134 | self._verify_helper = _VerifyHelper(callback) |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 1135 | self._verify_callback = self._verify_helper.callback |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1136 | _lib.SSL_CTX_set_verify(self._context, mode, self._verify_callback) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1137 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1138 | def set_verify_depth(self, depth): |
| 1139 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1140 | Set the maximum depth for the certificate chain verification that shall |
| 1141 | be allowed for this Context object. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1142 | |
| 1143 | :param depth: An integer specifying the verify depth |
| 1144 | :return: None |
| 1145 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1146 | if not isinstance(depth, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1147 | raise TypeError("depth must be an integer") |
| 1148 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1149 | _lib.SSL_CTX_set_verify_depth(self._context, depth) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1150 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1151 | def get_verify_mode(self): |
| 1152 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1153 | Retrieve the Context object's verify mode, as set by |
| 1154 | :meth:`set_verify`. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1155 | |
| 1156 | :return: The verify mode |
| 1157 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1158 | return _lib.SSL_CTX_get_verify_mode(self._context) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1159 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1160 | def get_verify_depth(self): |
| 1161 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1162 | Retrieve the Context object's verify depth, as set by |
| 1163 | :meth:`set_verify_depth`. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1164 | |
| 1165 | :return: The verify depth |
| 1166 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1167 | return _lib.SSL_CTX_get_verify_depth(self._context) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1168 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1169 | def load_tmp_dh(self, dhfile): |
| 1170 | """ |
| 1171 | Load parameters for Ephemeral Diffie-Hellman |
| 1172 | |
Jean-Paul Calderone | 4e0c43f | 2015-04-13 10:15:17 -0400 | [diff] [blame] | 1173 | :param dhfile: The file to load EDH parameters from (``bytes`` or |
| 1174 | ``unicode``). |
| 1175 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1176 | :return: None |
| 1177 | """ |
Jean-Paul Calderone | 9e1c1dd | 2015-04-12 10:13:13 -0400 | [diff] [blame] | 1178 | dhfile = _path_string(dhfile) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1179 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1180 | bio = _lib.BIO_new_file(dhfile, b"r") |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1181 | if bio == _ffi.NULL: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 1182 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1183 | bio = _ffi.gc(bio, _lib.BIO_free) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1184 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1185 | dh = _lib.PEM_read_bio_DHparams(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL) |
| 1186 | dh = _ffi.gc(dh, _lib.DH_free) |
| 1187 | _lib.SSL_CTX_set_tmp_dh(self._context, dh) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1188 | |
Jean-Paul Calderone | 3e4e335 | 2014-04-19 09:28:28 -0400 | [diff] [blame] | 1189 | def set_tmp_ecdh(self, curve): |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 1190 | """ |
Andy Lutomirski | 76a6133 | 2014-03-12 15:02:56 -0700 | [diff] [blame] | 1191 | Select a curve to use for ECDHE key exchange. |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 1192 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 1193 | :param curve: A curve object to use as returned by either |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1194 | :meth:`OpenSSL.crypto.get_elliptic_curve` or |
| 1195 | :meth:`OpenSSL.crypto.get_elliptic_curves`. |
Andy Lutomirski | f05a273 | 2014-03-13 17:22:25 -0700 | [diff] [blame] | 1196 | |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 1197 | :return: None |
| 1198 | """ |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 1199 | _lib.SSL_CTX_set_tmp_ecdh(self._context, curve._to_EC_KEY()) |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 1200 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1201 | def set_cipher_list(self, cipher_list): |
| 1202 | """ |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 1203 | Set the list of ciphers to be used in this context. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1204 | |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 1205 | See the OpenSSL manual for more information (e.g. |
| 1206 | :manpage:`ciphers(1)`). |
| 1207 | |
| 1208 | :param bytes cipher_list: An OpenSSL cipher string. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1209 | :return: None |
| 1210 | """ |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 1211 | cipher_list = _text_to_bytes_and_warn("cipher_list", cipher_list) |
Jean-Paul Calderone | 63eab69 | 2014-01-18 10:19:56 -0500 | [diff] [blame] | 1212 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1213 | if not isinstance(cipher_list, bytes): |
Hynek Schlawack | a7a63af | 2016-03-11 12:05:26 +0100 | [diff] [blame] | 1214 | raise TypeError("cipher_list must be a byte string.") |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1215 | |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 1216 | _openssl_assert( |
Hynek Schlawack | 22a4b66 | 2016-03-11 14:59:39 +0100 | [diff] [blame] | 1217 | _lib.SSL_CTX_set_cipher_list(self._context, cipher_list) == 1 |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 1218 | ) |
Paul Kehrer | 7d5a3bf | 2019-01-21 12:24:02 -0600 | [diff] [blame] | 1219 | # In OpenSSL 1.1.1 setting the cipher list will always return TLS 1.3 |
| 1220 | # ciphers even if you pass an invalid cipher. Applications (like |
| 1221 | # Twisted) have tests that depend on an error being raised if an |
| 1222 | # invalid cipher string is passed, but without the following check |
| 1223 | # for the TLS 1.3 specific cipher suites it would never error. |
| 1224 | tmpconn = Connection(self, None) |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1225 | if tmpconn.get_cipher_list() == [ |
| 1226 | "TLS_AES_256_GCM_SHA384", |
| 1227 | "TLS_CHACHA20_POLY1305_SHA256", |
| 1228 | "TLS_AES_128_GCM_SHA256", |
| 1229 | ]: |
Mark Williams | df2480d | 2019-02-14 19:30:07 -0800 | [diff] [blame] | 1230 | raise Error( |
| 1231 | [ |
| 1232 | ( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1233 | "SSL routines", |
| 1234 | "SSL_CTX_set_cipher_list", |
| 1235 | "no cipher match", |
Mark Williams | df2480d | 2019-02-14 19:30:07 -0800 | [diff] [blame] | 1236 | ), |
| 1237 | ], |
| 1238 | ) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1239 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1240 | def set_client_ca_list(self, certificate_authorities): |
| 1241 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 1242 | Set the list of preferred client certificate signers for this server |
| 1243 | context. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1244 | |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 1245 | This list of certificate authorities will be sent to the client when |
| 1246 | the server requests a client certificate. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1247 | |
| 1248 | :param certificate_authorities: a sequence of X509Names. |
| 1249 | :return: None |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1250 | |
| 1251 | .. versionadded:: 0.10 |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1252 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1253 | name_stack = _lib.sk_X509_NAME_new_null() |
Alex Gaynor | a829e90 | 2016-06-04 18:16:01 -0700 | [diff] [blame] | 1254 | _openssl_assert(name_stack != _ffi.NULL) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1255 | |
| 1256 | try: |
| 1257 | for ca_name in certificate_authorities: |
| 1258 | if not isinstance(ca_name, X509Name): |
| 1259 | raise TypeError( |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 1260 | "client CAs must be X509Name objects, not %s " |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1261 | "objects" % (type(ca_name).__name__,) |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 1262 | ) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1263 | copy = _lib.X509_NAME_dup(ca_name._name) |
Alex Gaynor | a829e90 | 2016-06-04 18:16:01 -0700 | [diff] [blame] | 1264 | _openssl_assert(copy != _ffi.NULL) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1265 | push_result = _lib.sk_X509_NAME_push(name_stack, copy) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1266 | if not push_result: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1267 | _lib.X509_NAME_free(copy) |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 1268 | _raise_current_error() |
Alex Gaynor | c3697ad | 2017-11-20 08:19:32 -0500 | [diff] [blame] | 1269 | except Exception: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1270 | _lib.sk_X509_NAME_free(name_stack) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1271 | raise |
| 1272 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1273 | _lib.SSL_CTX_set_client_CA_list(self._context, name_stack) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1274 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1275 | def add_client_ca(self, certificate_authority): |
| 1276 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 1277 | Add the CA certificate to the list of preferred signers for this |
| 1278 | context. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1279 | |
| 1280 | The list of certificate authorities will be sent to the client when the |
| 1281 | server requests a client certificate. |
| 1282 | |
| 1283 | :param certificate_authority: certificate authority's X509 certificate. |
| 1284 | :return: None |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1285 | |
| 1286 | .. versionadded:: 0.10 |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1287 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1288 | if not isinstance(certificate_authority, X509): |
| 1289 | raise TypeError("certificate_authority must be an X509 instance") |
| 1290 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1291 | add_result = _lib.SSL_CTX_add_client_CA( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1292 | self._context, certificate_authority._x509 |
| 1293 | ) |
Alex Gaynor | 09f19f5 | 2016-07-03 09:54:09 -0400 | [diff] [blame] | 1294 | _openssl_assert(add_result == 1) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1295 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1296 | def set_timeout(self, timeout): |
| 1297 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1298 | Set the timeout for newly created sessions for this Context object to |
| 1299 | *timeout*. The default value is 300 seconds. See the OpenSSL manual |
| 1300 | for more information (e.g. :manpage:`SSL_CTX_set_timeout(3)`). |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1301 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1302 | :param timeout: The timeout in (whole) seconds |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1303 | :return: The previous session timeout |
| 1304 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1305 | if not isinstance(timeout, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1306 | raise TypeError("timeout must be an integer") |
| 1307 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1308 | return _lib.SSL_CTX_set_timeout(self._context, timeout) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1309 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1310 | def get_timeout(self): |
| 1311 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1312 | Retrieve session timeout, as set by :meth:`set_timeout`. The default |
| 1313 | is 300 seconds. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1314 | |
| 1315 | :return: The session timeout |
| 1316 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1317 | return _lib.SSL_CTX_get_timeout(self._context) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1318 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1319 | def set_info_callback(self, callback): |
| 1320 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1321 | Set the information callback to *callback*. This function will be |
| 1322 | called from time to time during SSL handshakes. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1323 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1324 | :param callback: The Python callback to use. This should take three |
| 1325 | arguments: a Connection object and two integers. The first integer |
| 1326 | specifies where in the SSL handshake the function was called, and |
| 1327 | the other the return code from a (possibly failed) internal |
| 1328 | function call. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1329 | :return: None |
| 1330 | """ |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1331 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1332 | @wraps(callback) |
| 1333 | def wrapper(ssl, where, return_code): |
Jean-Paul Calderone | f2bbc9c | 2014-02-02 10:59:14 -0500 | [diff] [blame] | 1334 | callback(Connection._reverse_mapping[ssl], where, return_code) |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1335 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1336 | self._info_callback = _ffi.callback( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1337 | "void (*)(const SSL *, int, int)", wrapper |
| 1338 | ) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1339 | _lib.SSL_CTX_set_info_callback(self._context, self._info_callback) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1340 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1341 | def get_app_data(self): |
| 1342 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1343 | Get the application data (supplied via :meth:`set_app_data()`) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1344 | |
| 1345 | :return: The application data |
| 1346 | """ |
| 1347 | return self._app_data |
| 1348 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1349 | def set_app_data(self, data): |
| 1350 | """ |
| 1351 | Set the application data (will be returned from get_app_data()) |
| 1352 | |
| 1353 | :param data: Any Python object |
| 1354 | :return: None |
| 1355 | """ |
| 1356 | self._app_data = data |
| 1357 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1358 | def get_cert_store(self): |
| 1359 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1360 | Get the certificate store for the context. This can be used to add |
| 1361 | "trusted" certificates without using the |
| 1362 | :meth:`load_verify_locations` method. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1363 | |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1364 | :return: A X509Store object or None if it does not have one. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1365 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1366 | store = _lib.SSL_CTX_get_cert_store(self._context) |
| 1367 | if store == _ffi.NULL: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1368 | # TODO: This is untested. |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1369 | return None |
| 1370 | |
| 1371 | pystore = X509Store.__new__(X509Store) |
| 1372 | pystore._store = store |
| 1373 | return pystore |
| 1374 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1375 | def set_options(self, options): |
| 1376 | """ |
| 1377 | Add options. Options set before are not cleared! |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1378 | This method should be used with the :const:`OP_*` constants. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1379 | |
| 1380 | :param options: The options to add. |
| 1381 | :return: The new option bitmask. |
| 1382 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1383 | if not isinstance(options, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1384 | raise TypeError("options must be an integer") |
| 1385 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1386 | return _lib.SSL_CTX_set_options(self._context, options) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1387 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1388 | def set_mode(self, mode): |
| 1389 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1390 | Add modes via bitmask. Modes set before are not cleared! This method |
| 1391 | should be used with the :const:`MODE_*` constants. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1392 | |
| 1393 | :param mode: The mode to add. |
| 1394 | :return: The new mode bitmask. |
| 1395 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1396 | if not isinstance(mode, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1397 | raise TypeError("mode must be an integer") |
| 1398 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1399 | return _lib.SSL_CTX_set_mode(self._context, mode) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1400 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1401 | def set_tlsext_servername_callback(self, callback): |
| 1402 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 1403 | Specify a callback function to be called when clients specify a server |
| 1404 | name. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1405 | |
| 1406 | :param callback: The callback function. It will be invoked with one |
| 1407 | argument, the Connection instance. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1408 | |
| 1409 | .. versionadded:: 0.13 |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1410 | """ |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1411 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1412 | @wraps(callback) |
| 1413 | def wrapper(ssl, alert, arg): |
| 1414 | callback(Connection._reverse_mapping[ssl]) |
| 1415 | return 0 |
| 1416 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1417 | self._tlsext_servername_callback = _ffi.callback( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1418 | "int (*)(SSL *, int *, void *)", wrapper |
| 1419 | ) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1420 | _lib.SSL_CTX_set_tlsext_servername_callback( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1421 | self._context, self._tlsext_servername_callback |
| 1422 | ) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 1423 | |
Jeremy Lainé | 02261ad | 2018-05-16 18:33:25 +0200 | [diff] [blame] | 1424 | def set_tlsext_use_srtp(self, profiles): |
| 1425 | """ |
| 1426 | Enable support for negotiating SRTP keying material. |
| 1427 | |
| 1428 | :param bytes profiles: A colon delimited list of protection profile |
| 1429 | names, like ``b'SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32'``. |
| 1430 | :return: None |
| 1431 | """ |
| 1432 | if not isinstance(profiles, bytes): |
| 1433 | raise TypeError("profiles must be a byte string.") |
| 1434 | |
| 1435 | _openssl_assert( |
| 1436 | _lib.SSL_CTX_set_tlsext_use_srtp(self._context, profiles) == 0 |
| 1437 | ) |
| 1438 | |
Cory Benfield | 10b277f | 2015-04-13 17:12:42 -0400 | [diff] [blame] | 1439 | @_requires_npn |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1440 | def set_npn_advertise_callback(self, callback): |
| 1441 | """ |
Cory Benfield | be3e7b8 | 2014-05-10 09:48:55 +0100 | [diff] [blame] | 1442 | Specify a callback function that will be called when offering `Next |
| 1443 | Protocol Negotiation |
| 1444 | <https://technotes.googlecode.com/git/nextprotoneg.html>`_ as a server. |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1445 | |
| 1446 | :param callback: The callback function. It will be invoked with one |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1447 | argument, the :class:`Connection` instance. It should return a |
| 1448 | list of bytestrings representing the advertised protocols, like |
Cory Benfield | be3e7b8 | 2014-05-10 09:48:55 +0100 | [diff] [blame] | 1449 | ``[b'http/1.1', b'spdy/2']``. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1450 | |
| 1451 | .. versionadded:: 0.15 |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1452 | """ |
Alex Gaynor | be2bd54 | 2019-02-21 21:41:22 -0500 | [diff] [blame] | 1453 | _warn_npn() |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1454 | self._npn_advertise_helper = _NpnAdvertiseHelper(callback) |
| 1455 | self._npn_advertise_callback = self._npn_advertise_helper.callback |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1456 | _lib.SSL_CTX_set_next_protos_advertised_cb( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1457 | self._context, self._npn_advertise_callback, _ffi.NULL |
| 1458 | ) |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1459 | |
Cory Benfield | 10b277f | 2015-04-13 17:12:42 -0400 | [diff] [blame] | 1460 | @_requires_npn |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1461 | def set_npn_select_callback(self, callback): |
| 1462 | """ |
| 1463 | Specify a callback function that will be called when a server offers |
| 1464 | Next Protocol Negotiation options. |
| 1465 | |
| 1466 | :param callback: The callback function. It will be invoked with two |
| 1467 | arguments: the Connection, and a list of offered protocols as |
Cory Benfield | be3e7b8 | 2014-05-10 09:48:55 +0100 | [diff] [blame] | 1468 | bytestrings, e.g. ``[b'http/1.1', b'spdy/2']``. It should return |
| 1469 | one of those bytestrings, the chosen protocol. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1470 | |
| 1471 | .. versionadded:: 0.15 |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1472 | """ |
Alex Gaynor | be2bd54 | 2019-02-21 21:41:22 -0500 | [diff] [blame] | 1473 | _warn_npn() |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1474 | self._npn_select_helper = _NpnSelectHelper(callback) |
| 1475 | self._npn_select_callback = self._npn_select_helper.callback |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1476 | _lib.SSL_CTX_set_next_proto_select_cb( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1477 | self._context, self._npn_select_callback, _ffi.NULL |
| 1478 | ) |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1479 | |
Cory Benfield | 7907e33 | 2015-04-13 17:18:25 -0400 | [diff] [blame] | 1480 | @_requires_alpn |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1481 | def set_alpn_protos(self, protos): |
| 1482 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1483 | Specify the protocols that the client is prepared to speak after the |
| 1484 | TLS connection has been negotiated using Application Layer Protocol |
| 1485 | Negotiation. |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1486 | |
| 1487 | :param protos: A list of the protocols to be offered to the server. |
| 1488 | This list should be a Python list of bytestrings representing the |
| 1489 | protocols to offer, e.g. ``[b'http/1.1', b'spdy/2']``. |
| 1490 | """ |
| 1491 | # Take the list of protocols and join them together, prefixing them |
| 1492 | # with their lengths. |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1493 | protostr = b"".join( |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1494 | chain.from_iterable((int2byte(len(p)), p) for p in protos) |
| 1495 | ) |
| 1496 | |
| 1497 | # Build a C string from the list. We don't need to save this off |
| 1498 | # because OpenSSL immediately copies the data out. |
| 1499 | input_str = _ffi.new("unsigned char[]", protostr) |
Alex Gaynor | d61c46a | 2017-06-29 22:51:33 -0700 | [diff] [blame] | 1500 | _lib.SSL_CTX_set_alpn_protos(self._context, input_str, len(protostr)) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1501 | |
Cory Benfield | 7907e33 | 2015-04-13 17:18:25 -0400 | [diff] [blame] | 1502 | @_requires_alpn |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1503 | def set_alpn_select_callback(self, callback): |
| 1504 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1505 | Specify a callback function that will be called on the server when a |
| 1506 | client offers protocols using ALPN. |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1507 | |
| 1508 | :param callback: The callback function. It will be invoked with two |
| 1509 | arguments: the Connection, and a list of offered protocols as |
Mark Williams | 5d890a0 | 2019-11-17 19:56:26 -0800 | [diff] [blame] | 1510 | bytestrings, e.g ``[b'http/1.1', b'spdy/2']``. It can return |
| 1511 | one of those bytestrings to indicate the chosen protocol, the |
| 1512 | empty bytestring to terminate the TLS connection, or the |
| 1513 | :py:obj:`NO_OVERLAPPING_PROTOCOLS` to indicate that no offered |
| 1514 | protocol was selected, but that the connection should not be |
| 1515 | aborted. |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1516 | """ |
Cory Benfield | 9da5ffb | 2015-04-13 17:20:14 -0400 | [diff] [blame] | 1517 | self._alpn_select_helper = _ALPNSelectHelper(callback) |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 1518 | self._alpn_select_callback = self._alpn_select_helper.callback |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1519 | _lib.SSL_CTX_set_alpn_select_cb( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1520 | self._context, self._alpn_select_callback, _ffi.NULL |
| 1521 | ) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1522 | |
Cory Benfield | 496652a | 2017-01-24 11:42:56 +0000 | [diff] [blame] | 1523 | def _set_ocsp_callback(self, helper, data): |
| 1524 | """ |
| 1525 | This internal helper does the common work for |
| 1526 | ``set_ocsp_server_callback`` and ``set_ocsp_client_callback``, which is |
| 1527 | almost all of it. |
| 1528 | """ |
| 1529 | self._ocsp_helper = helper |
| 1530 | self._ocsp_callback = helper.callback |
| 1531 | if data is None: |
| 1532 | self._ocsp_data = _ffi.NULL |
| 1533 | else: |
| 1534 | self._ocsp_data = _ffi.new_handle(data) |
| 1535 | |
| 1536 | rc = _lib.SSL_CTX_set_tlsext_status_cb( |
| 1537 | self._context, self._ocsp_callback |
| 1538 | ) |
| 1539 | _openssl_assert(rc == 1) |
| 1540 | rc = _lib.SSL_CTX_set_tlsext_status_arg(self._context, self._ocsp_data) |
| 1541 | _openssl_assert(rc == 1) |
| 1542 | |
| 1543 | def set_ocsp_server_callback(self, callback, data=None): |
| 1544 | """ |
| 1545 | Set a callback to provide OCSP data to be stapled to the TLS handshake |
| 1546 | on the server side. |
| 1547 | |
| 1548 | :param callback: The callback function. It will be invoked with two |
| 1549 | arguments: the Connection, and the optional arbitrary data you have |
| 1550 | provided. The callback must return a bytestring that contains the |
| 1551 | OCSP data to staple to the handshake. If no OCSP data is available |
| 1552 | for this connection, return the empty bytestring. |
| 1553 | :param data: Some opaque data that will be passed into the callback |
| 1554 | function when called. This can be used to avoid needing to do |
| 1555 | complex data lookups or to keep track of what context is being |
| 1556 | used. This parameter is optional. |
| 1557 | """ |
| 1558 | helper = _OCSPServerCallbackHelper(callback) |
| 1559 | self._set_ocsp_callback(helper, data) |
| 1560 | |
| 1561 | def set_ocsp_client_callback(self, callback, data=None): |
| 1562 | """ |
| 1563 | Set a callback to validate OCSP data stapled to the TLS handshake on |
| 1564 | the client side. |
| 1565 | |
| 1566 | :param callback: The callback function. It will be invoked with three |
| 1567 | arguments: the Connection, a bytestring containing the stapled OCSP |
| 1568 | assertion, and the optional arbitrary data you have provided. The |
| 1569 | callback must return a boolean that indicates the result of |
| 1570 | validating the OCSP data: ``True`` if the OCSP data is valid and |
| 1571 | the certificate can be trusted, or ``False`` if either the OCSP |
| 1572 | data is invalid or the certificate has been revoked. |
| 1573 | :param data: Some opaque data that will be passed into the callback |
| 1574 | function when called. This can be used to avoid needing to do |
| 1575 | complex data lookups or to keep track of what context is being |
| 1576 | used. This parameter is optional. |
| 1577 | """ |
| 1578 | helper = _OCSPClientCallbackHelper(callback) |
| 1579 | self._set_ocsp_callback(helper, data) |
| 1580 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 1581 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1582 | class Connection(object): |
| 1583 | """ |
| 1584 | """ |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1585 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1586 | _reverse_mapping = WeakValueDictionary() |
| 1587 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1588 | def __init__(self, context, socket=None): |
| 1589 | """ |
| 1590 | Create a new Connection object, using the given OpenSSL.SSL.Context |
| 1591 | instance and socket. |
| 1592 | |
| 1593 | :param context: An SSL Context to use for this connection |
| 1594 | :param socket: The socket to use for transport layer |
| 1595 | """ |
| 1596 | if not isinstance(context, Context): |
| 1597 | raise TypeError("context must be a Context instance") |
| 1598 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1599 | ssl = _lib.SSL_new(context._context) |
| 1600 | self._ssl = _ffi.gc(ssl, _lib.SSL_free) |
Paul Kehrer | 15c2935 | 2018-05-14 13:31:27 -0400 | [diff] [blame] | 1601 | # We set SSL_MODE_AUTO_RETRY to handle situations where OpenSSL returns |
| 1602 | # an SSL_ERROR_WANT_READ when processing a non-application data packet |
| 1603 | # even though there is still data on the underlying transport. |
| 1604 | # See https://github.com/openssl/openssl/issues/6234 for more details. |
| 1605 | _lib.SSL_set_mode(self._ssl, _lib.SSL_MODE_AUTO_RETRY) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1606 | self._context = context |
Todd Chapman | 4f73e4f | 2015-08-27 11:26:43 -0400 | [diff] [blame] | 1607 | self._app_data = None |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1608 | |
Cory Benfield | be3e7b8 | 2014-05-10 09:48:55 +0100 | [diff] [blame] | 1609 | # References to strings used for Next Protocol Negotiation. OpenSSL's |
| 1610 | # header files suggest that these might get copied at some point, but |
| 1611 | # doesn't specify when, so we store them here to make sure they don't |
| 1612 | # get freed before OpenSSL uses them. |
| 1613 | self._npn_advertise_callback_args = None |
| 1614 | self._npn_select_callback_args = None |
| 1615 | |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1616 | # References to strings used for Application Layer Protocol |
| 1617 | # Negotiation. These strings get copied at some point but it's well |
| 1618 | # after the callback returns, so we have to hang them somewhere to |
| 1619 | # avoid them getting freed. |
| 1620 | self._alpn_select_callback_args = None |
| 1621 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1622 | self._reverse_mapping[self._ssl] = self |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1623 | |
| 1624 | if socket is None: |
| 1625 | self._socket = None |
Jean-Paul Calderone | 73b15c2 | 2013-03-05 18:30:39 -0800 | [diff] [blame] | 1626 | # Don't set up any gc for these, SSL_free will take care of them. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1627 | self._into_ssl = _lib.BIO_new(_lib.BIO_s_mem()) |
Alex Gaynor | a829e90 | 2016-06-04 18:16:01 -0700 | [diff] [blame] | 1628 | _openssl_assert(self._into_ssl != _ffi.NULL) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1629 | |
Alex Gaynor | a829e90 | 2016-06-04 18:16:01 -0700 | [diff] [blame] | 1630 | self._from_ssl = _lib.BIO_new(_lib.BIO_s_mem()) |
| 1631 | _openssl_assert(self._from_ssl != _ffi.NULL) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1632 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1633 | _lib.SSL_set_bio(self._ssl, self._into_ssl, self._from_ssl) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1634 | else: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1635 | self._into_ssl = None |
| 1636 | self._from_ssl = None |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1637 | self._socket = socket |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 1638 | set_result = _lib.SSL_set_fd( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1639 | self._ssl, _asFileDescriptor(self._socket) |
| 1640 | ) |
Alex Gaynor | 09f19f5 | 2016-07-03 09:54:09 -0400 | [diff] [blame] | 1641 | _openssl_assert(set_result == 1) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1642 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1643 | def __getattr__(self, name): |
| 1644 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 1645 | Look up attributes on the wrapped socket object if they are not found |
| 1646 | on the Connection object. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1647 | """ |
kjav | 0b66fa1 | 2015-09-02 11:51:26 +0100 | [diff] [blame] | 1648 | if self._socket is None: |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1649 | raise AttributeError( |
| 1650 | "'%s' object has no attribute '%s'" |
| 1651 | % (self.__class__.__name__, name) |
| 1652 | ) |
kjav | 0b66fa1 | 2015-09-02 11:51:26 +0100 | [diff] [blame] | 1653 | else: |
| 1654 | return getattr(self._socket, name) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1655 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1656 | def _raise_ssl_error(self, ssl, result): |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 1657 | if self._context._verify_helper is not None: |
| 1658 | self._context._verify_helper.raise_if_problem() |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1659 | if self._context._npn_advertise_helper is not None: |
| 1660 | self._context._npn_advertise_helper.raise_if_problem() |
| 1661 | if self._context._npn_select_helper is not None: |
| 1662 | self._context._npn_select_helper.raise_if_problem() |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 1663 | if self._context._alpn_select_helper is not None: |
| 1664 | self._context._alpn_select_helper.raise_if_problem() |
Cory Benfield | 496652a | 2017-01-24 11:42:56 +0000 | [diff] [blame] | 1665 | if self._context._ocsp_helper is not None: |
| 1666 | self._context._ocsp_helper.raise_if_problem() |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 1667 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1668 | error = _lib.SSL_get_error(ssl, result) |
| 1669 | if error == _lib.SSL_ERROR_WANT_READ: |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1670 | raise WantReadError() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1671 | elif error == _lib.SSL_ERROR_WANT_WRITE: |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 1672 | raise WantWriteError() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1673 | elif error == _lib.SSL_ERROR_ZERO_RETURN: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1674 | raise ZeroReturnError() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1675 | elif error == _lib.SSL_ERROR_WANT_X509_LOOKUP: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1676 | # TODO: This is untested. |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 1677 | raise WantX509LookupError() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1678 | elif error == _lib.SSL_ERROR_SYSCALL: |
| 1679 | if _lib.ERR_peek_error() == 0: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1680 | if result < 0: |
Konstantinos Koukopoulos | 541150d | 2014-01-31 01:00:19 +0200 | [diff] [blame] | 1681 | if platform == "win32": |
| 1682 | errno = _ffi.getwinerror()[0] |
| 1683 | else: |
| 1684 | errno = _ffi.errno |
Alex Gaynor | 5af32d0 | 2016-09-24 01:52:21 -0400 | [diff] [blame] | 1685 | |
| 1686 | if errno != 0: |
| 1687 | raise SysCallError(errno, errorcode.get(errno)) |
| 1688 | raise SysCallError(-1, "Unexpected EOF") |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1689 | else: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1690 | # TODO: This is untested. |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 1691 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1692 | elif error == _lib.SSL_ERROR_NONE: |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 1693 | pass |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1694 | else: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 1695 | _raise_current_error() |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1696 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1697 | def get_context(self): |
| 1698 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1699 | Retrieve the :class:`Context` object associated with this |
| 1700 | :class:`Connection`. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1701 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1702 | return self._context |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1703 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1704 | def set_context(self, context): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1705 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1706 | Switch this connection to a new session context. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1707 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1708 | :param context: A :class:`Context` instance giving the new session |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1709 | context to use. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1710 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1711 | if not isinstance(context, Context): |
| 1712 | raise TypeError("context must be a Context instance") |
| 1713 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1714 | _lib.SSL_set_SSL_CTX(self._ssl, context._context) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1715 | self._context = context |
| 1716 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1717 | def get_servername(self): |
| 1718 | """ |
| 1719 | Retrieve the servername extension value if provided in the client hello |
| 1720 | message, or None if there wasn't one. |
| 1721 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1722 | :return: A byte string giving the server name or :data:`None`. |
| 1723 | |
| 1724 | .. versionadded:: 0.13 |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1725 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 1726 | name = _lib.SSL_get_servername( |
| 1727 | self._ssl, _lib.TLSEXT_NAMETYPE_host_name |
| 1728 | ) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1729 | if name == _ffi.NULL: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1730 | return None |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1731 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1732 | return _ffi.string(name) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1733 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1734 | def set_tlsext_host_name(self, name): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1735 | """ |
| 1736 | Set the value of the servername extension to send in the client hello. |
| 1737 | |
| 1738 | :param name: A byte string giving the name. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1739 | |
| 1740 | .. versionadded:: 0.13 |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1741 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1742 | if not isinstance(name, bytes): |
| 1743 | raise TypeError("name must be a byte string") |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1744 | elif b"\0" in name: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1745 | raise TypeError("name must not contain NUL byte") |
| 1746 | |
| 1747 | # XXX I guess this can fail sometimes? |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1748 | _lib.SSL_set_tlsext_host_name(self._ssl, name) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1749 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1750 | def pending(self): |
| 1751 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1752 | Get the number of bytes that can be safely read from the SSL buffer |
| 1753 | (**not** the underlying transport buffer). |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1754 | |
| 1755 | :return: The number of bytes available in the receive buffer. |
| 1756 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1757 | return _lib.SSL_pending(self._ssl) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1758 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1759 | def send(self, buf, flags=0): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1760 | """ |
| 1761 | Send data on the connection. NOTE: If you get one of the WantRead, |
| 1762 | WantWrite or WantX509Lookup exceptions on this, you have to call the |
| 1763 | method again with the SAME buffer. |
| 1764 | |
Markus Unterwaditzer | 8e41d02 | 2014-04-19 12:27:11 +0200 | [diff] [blame] | 1765 | :param buf: The string, buffer or memoryview to send |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1766 | :param flags: (optional) Included for compatibility with the socket |
| 1767 | API, the value is ignored |
| 1768 | :return: The number of bytes written |
| 1769 | """ |
Abraham Martin | e82326c | 2015-02-04 10:18:10 +0000 | [diff] [blame] | 1770 | # Backward compatibility |
Jean-Paul Calderone | 39a8d59 | 2015-04-13 20:49:50 -0400 | [diff] [blame] | 1771 | buf = _text_to_bytes_and_warn("buf", buf) |
Abraham Martin | e82326c | 2015-02-04 10:18:10 +0000 | [diff] [blame] | 1772 | |
Daniel Holth | 079c963 | 2019-11-17 22:45:52 -0500 | [diff] [blame] | 1773 | with _from_buffer(buf) as data: |
| 1774 | # check len(buf) instead of len(data) for testability |
| 1775 | if len(buf) > 2147483647: |
| 1776 | raise ValueError( |
| 1777 | "Cannot send more than 2**31-1 bytes at once." |
| 1778 | ) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1779 | |
Daniel Holth | 079c963 | 2019-11-17 22:45:52 -0500 | [diff] [blame] | 1780 | result = _lib.SSL_write(self._ssl, data, len(data)) |
| 1781 | self._raise_ssl_error(self._ssl, result) |
| 1782 | |
| 1783 | return result |
| 1784 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1785 | write = send |
| 1786 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1787 | def sendall(self, buf, flags=0): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1788 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1789 | Send "all" data on the connection. This calls send() repeatedly until |
| 1790 | all data is sent. If an error occurs, it's impossible to tell how much |
| 1791 | data has been sent. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1792 | |
Markus Unterwaditzer | 8e41d02 | 2014-04-19 12:27:11 +0200 | [diff] [blame] | 1793 | :param buf: The string, buffer or memoryview to send |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1794 | :param flags: (optional) Included for compatibility with the socket |
| 1795 | API, the value is ignored |
| 1796 | :return: The number of bytes written |
| 1797 | """ |
Jean-Paul Calderone | 39a8d59 | 2015-04-13 20:49:50 -0400 | [diff] [blame] | 1798 | buf = _text_to_bytes_and_warn("buf", buf) |
Abraham Martin | e82326c | 2015-02-04 10:18:10 +0000 | [diff] [blame] | 1799 | |
Daniel Holth | 079c963 | 2019-11-17 22:45:52 -0500 | [diff] [blame] | 1800 | with _from_buffer(buf) as data: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1801 | |
Daniel Holth | 079c963 | 2019-11-17 22:45:52 -0500 | [diff] [blame] | 1802 | left_to_send = len(buf) |
| 1803 | total_sent = 0 |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1804 | |
Daniel Holth | 079c963 | 2019-11-17 22:45:52 -0500 | [diff] [blame] | 1805 | while left_to_send: |
| 1806 | # SSL_write's num arg is an int, |
| 1807 | # so we cannot send more than 2**31-1 bytes at once. |
| 1808 | result = _lib.SSL_write( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1809 | self._ssl, data + total_sent, min(left_to_send, 2147483647) |
Daniel Holth | 079c963 | 2019-11-17 22:45:52 -0500 | [diff] [blame] | 1810 | ) |
| 1811 | self._raise_ssl_error(self._ssl, result) |
| 1812 | total_sent += result |
| 1813 | left_to_send -= result |
| 1814 | |
| 1815 | return total_sent |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1816 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1817 | def recv(self, bufsiz, flags=None): |
| 1818 | """ |
Alex Gaynor | 67fc8c9 | 2016-05-27 08:27:19 -0400 | [diff] [blame] | 1819 | Receive data on the connection. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1820 | |
| 1821 | :param bufsiz: The maximum number of bytes to read |
Maximilian Hils | 1d95dea | 2015-08-17 19:27:20 +0200 | [diff] [blame] | 1822 | :param flags: (optional) The only supported flag is ``MSG_PEEK``, |
| 1823 | all other flags are ignored. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1824 | :return: The string read from the Connection |
| 1825 | """ |
Cory Benfield | e62840e | 2016-11-28 12:17:08 +0000 | [diff] [blame] | 1826 | buf = _no_zero_allocator("char[]", bufsiz) |
Maximilian Hils | 1d95dea | 2015-08-17 19:27:20 +0200 | [diff] [blame] | 1827 | if flags is not None and flags & socket.MSG_PEEK: |
| 1828 | result = _lib.SSL_peek(self._ssl, buf, bufsiz) |
| 1829 | else: |
| 1830 | result = _lib.SSL_read(self._ssl, buf, bufsiz) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1831 | self._raise_ssl_error(self._ssl, result) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1832 | return _ffi.buffer(buf, result)[:] |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 1833 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1834 | read = recv |
| 1835 | |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 1836 | def recv_into(self, buffer, nbytes=None, flags=None): |
| 1837 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1838 | Receive data on the connection and copy it directly into the provided |
| 1839 | buffer, rather than creating a new string. |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 1840 | |
| 1841 | :param buffer: The buffer to copy into. |
| 1842 | :param nbytes: (optional) The maximum number of bytes to read into the |
| 1843 | buffer. If not present, defaults to the size of the buffer. If |
| 1844 | larger than the size of the buffer, is reduced to the size of the |
| 1845 | buffer. |
Maximilian Hils | 1d95dea | 2015-08-17 19:27:20 +0200 | [diff] [blame] | 1846 | :param flags: (optional) The only supported flag is ``MSG_PEEK``, |
| 1847 | all other flags are ignored. |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 1848 | :return: The number of bytes read into the buffer. |
| 1849 | """ |
| 1850 | if nbytes is None: |
| 1851 | nbytes = len(buffer) |
| 1852 | else: |
| 1853 | nbytes = min(nbytes, len(buffer)) |
| 1854 | |
| 1855 | # We need to create a temporary buffer. This is annoying, it would be |
| 1856 | # better if we could pass memoryviews straight into the SSL_read call, |
| 1857 | # but right now we can't. Revisit this if CFFI gets that ability. |
Cory Benfield | e62840e | 2016-11-28 12:17:08 +0000 | [diff] [blame] | 1858 | buf = _no_zero_allocator("char[]", nbytes) |
Maximilian Hils | 1d95dea | 2015-08-17 19:27:20 +0200 | [diff] [blame] | 1859 | if flags is not None and flags & socket.MSG_PEEK: |
| 1860 | result = _lib.SSL_peek(self._ssl, buf, nbytes) |
| 1861 | else: |
| 1862 | result = _lib.SSL_read(self._ssl, buf, nbytes) |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 1863 | self._raise_ssl_error(self._ssl, result) |
| 1864 | |
| 1865 | # This strange line is all to avoid a memory copy. The buffer protocol |
| 1866 | # should allow us to assign a CFFI buffer to the LHS of this line, but |
| 1867 | # on CPython 3.3+ that segfaults. As a workaround, we can temporarily |
Jeremy Lainé | 1ae7cb6 | 2018-03-21 14:49:42 +0100 | [diff] [blame] | 1868 | # wrap it in a memoryview. |
| 1869 | buffer[:result] = memoryview(_ffi.buffer(buf, result)) |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 1870 | |
| 1871 | return result |
| 1872 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1873 | def _handle_bio_errors(self, bio, result): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1874 | if _lib.BIO_should_retry(bio): |
| 1875 | if _lib.BIO_should_read(bio): |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1876 | raise WantReadError() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1877 | elif _lib.BIO_should_write(bio): |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1878 | # TODO: This is untested. |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 1879 | raise WantWriteError() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1880 | elif _lib.BIO_should_io_special(bio): |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1881 | # TODO: This is untested. I think io_special means the socket |
| 1882 | # BIO has a not-yet connected socket. |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 1883 | raise ValueError("BIO_should_io_special") |
| 1884 | else: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1885 | # TODO: This is untested. |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 1886 | raise ValueError("unknown bio failure") |
| 1887 | else: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1888 | # TODO: This is untested. |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 1889 | _raise_current_error() |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1890 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1891 | def bio_read(self, bufsiz): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1892 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1893 | If the Connection was created with a memory BIO, this method can be |
| 1894 | used to read bytes from the write end of that memory BIO. Many |
| 1895 | Connection methods will add bytes which must be read in this manner or |
| 1896 | the buffer will eventually fill up and the Connection will be able to |
| 1897 | take no further actions. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1898 | |
| 1899 | :param bufsiz: The maximum number of bytes to read |
| 1900 | :return: The string read. |
| 1901 | """ |
Jean-Paul Calderone | 97e041d | 2013-03-05 21:03:12 -0800 | [diff] [blame] | 1902 | if self._from_ssl is None: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1903 | raise TypeError("Connection sock was not None") |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1904 | |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1905 | if not isinstance(bufsiz, integer_types): |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1906 | raise TypeError("bufsiz must be an integer") |
| 1907 | |
Cory Benfield | e62840e | 2016-11-28 12:17:08 +0000 | [diff] [blame] | 1908 | buf = _no_zero_allocator("char[]", bufsiz) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1909 | result = _lib.BIO_read(self._from_ssl, buf, bufsiz) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1910 | if result <= 0: |
| 1911 | self._handle_bio_errors(self._from_ssl, result) |
| 1912 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1913 | return _ffi.buffer(buf, result)[:] |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1914 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1915 | def bio_write(self, buf): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1916 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1917 | If the Connection was created with a memory BIO, this method can be |
| 1918 | used to add bytes to the read end of that memory BIO. The Connection |
| 1919 | can then read the bytes (for example, in response to a call to |
| 1920 | :meth:`recv`). |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1921 | |
| 1922 | :param buf: The string to put into the memory BIO. |
| 1923 | :return: The number of bytes written |
| 1924 | """ |
Jean-Paul Calderone | 39a8d59 | 2015-04-13 20:49:50 -0400 | [diff] [blame] | 1925 | buf = _text_to_bytes_and_warn("buf", buf) |
Abraham Martin | e82326c | 2015-02-04 10:18:10 +0000 | [diff] [blame] | 1926 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1927 | if self._into_ssl is None: |
| 1928 | raise TypeError("Connection sock was not None") |
| 1929 | |
Daniel Holth | 079c963 | 2019-11-17 22:45:52 -0500 | [diff] [blame] | 1930 | with _from_buffer(buf) as data: |
| 1931 | result = _lib.BIO_write(self._into_ssl, data, len(data)) |
| 1932 | if result <= 0: |
| 1933 | self._handle_bio_errors(self._into_ssl, result) |
| 1934 | return result |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1935 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1936 | def renegotiate(self): |
| 1937 | """ |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1938 | Renegotiate the session. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1939 | |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1940 | :return: True if the renegotiation can be started, False otherwise |
| 1941 | :rtype: bool |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1942 | """ |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1943 | if not self.renegotiate_pending(): |
| 1944 | _openssl_assert(_lib.SSL_renegotiate(self._ssl) == 1) |
| 1945 | return True |
| 1946 | return False |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1947 | |
| 1948 | def do_handshake(self): |
| 1949 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1950 | Perform an SSL handshake (usually called after :meth:`renegotiate` or |
Daniel Holth | 3efa98c | 2019-07-05 14:50:57 -0400 | [diff] [blame] | 1951 | one of :meth:`set_accept_state` or :meth:`set_connect_state`). This can |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1952 | raise the same exceptions as :meth:`send` and :meth:`recv`. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1953 | |
| 1954 | :return: None. |
| 1955 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1956 | result = _lib.SSL_do_handshake(self._ssl) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1957 | self._raise_ssl_error(self._ssl, result) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1958 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1959 | def renegotiate_pending(self): |
| 1960 | """ |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1961 | Check if there's a renegotiation in progress, it will return False once |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1962 | a renegotiation is finished. |
| 1963 | |
| 1964 | :return: Whether there's a renegotiation in progress |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1965 | :rtype: bool |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1966 | """ |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1967 | return _lib.SSL_renegotiate_pending(self._ssl) == 1 |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1968 | |
| 1969 | def total_renegotiations(self): |
| 1970 | """ |
| 1971 | Find out the total number of renegotiations. |
| 1972 | |
| 1973 | :return: The number of renegotiations. |
Hynek Schlawack | b1f3ca8 | 2016-02-13 09:10:04 +0100 | [diff] [blame] | 1974 | :rtype: int |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1975 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1976 | return _lib.SSL_total_renegotiations(self._ssl) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1977 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1978 | def connect(self, addr): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1979 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1980 | Call the :meth:`connect` method of the underlying socket and set up SSL |
| 1981 | on the socket, using the :class:`Context` object supplied to this |
| 1982 | :class:`Connection` object at creation. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1983 | |
| 1984 | :param addr: A remote address |
| 1985 | :return: What the socket's connect method returns |
| 1986 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1987 | _lib.SSL_set_connect_state(self._ssl) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1988 | return self._socket.connect(addr) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1989 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1990 | def connect_ex(self, addr): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1991 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 1992 | Call the :meth:`connect_ex` method of the underlying socket and set up |
| 1993 | SSL on the socket, using the Context object supplied to this Connection |
| 1994 | object at creation. Note that if the :meth:`connect_ex` method of the |
| 1995 | socket doesn't return 0, SSL won't be initialized. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1996 | |
| 1997 | :param addr: A remove address |
| 1998 | :return: What the socket's connect_ex method returns |
| 1999 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2000 | connect_ex = self._socket.connect_ex |
| 2001 | self.set_connect_state() |
| 2002 | return connect_ex(addr) |
| 2003 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2004 | def accept(self): |
| 2005 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2006 | Call the :meth:`accept` method of the underlying socket and set up SSL |
| 2007 | on the returned socket, using the Context object supplied to this |
| 2008 | :class:`Connection` object at creation. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2009 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2010 | :return: A *(conn, addr)* pair where *conn* is the new |
| 2011 | :class:`Connection` object created, and *address* is as returned by |
| 2012 | the socket's :meth:`accept`. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2013 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2014 | client, addr = self._socket.accept() |
| 2015 | conn = Connection(self._context, client) |
| 2016 | conn.set_accept_state() |
| 2017 | return (conn, addr) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2018 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2019 | def bio_shutdown(self): |
| 2020 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2021 | If the Connection was created with a memory BIO, this method can be |
| 2022 | used to indicate that *end of file* has been reached on the read end of |
| 2023 | that memory BIO. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2024 | |
| 2025 | :return: None |
| 2026 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2027 | if self._from_ssl is None: |
| 2028 | raise TypeError("Connection sock was not None") |
| 2029 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2030 | _lib.BIO_set_mem_eof_return(self._into_ssl, 0) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2031 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2032 | def shutdown(self): |
| 2033 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2034 | Send the shutdown message to the Connection. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2035 | |
| 2036 | :return: True if the shutdown completed successfully (i.e. both sides |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2037 | have sent closure alerts), False otherwise (in which case you |
| 2038 | call :meth:`recv` or :meth:`send` when the connection becomes |
| 2039 | readable/writeable). |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2040 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2041 | result = _lib.SSL_shutdown(self._ssl) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2042 | if result < 0: |
Paul Aurich | bff1d1a | 2015-01-08 08:36:53 -0800 | [diff] [blame] | 2043 | self._raise_ssl_error(self._ssl, result) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2044 | elif result > 0: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2045 | return True |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2046 | else: |
| 2047 | return False |
| 2048 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2049 | def get_cipher_list(self): |
| 2050 | """ |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 2051 | Retrieve the list of ciphers used by the Connection object. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2052 | |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 2053 | :return: A list of native cipher strings. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2054 | """ |
| 2055 | ciphers = [] |
| 2056 | for i in count(): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2057 | result = _lib.SSL_get_cipher_list(self._ssl, i) |
| 2058 | if result == _ffi.NULL: |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2059 | break |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2060 | ciphers.append(_native(_ffi.string(result))) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2061 | return ciphers |
| 2062 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2063 | def get_client_ca_list(self): |
| 2064 | """ |
| 2065 | Get CAs whose certificates are suggested for client authentication. |
| 2066 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2067 | :return: If this is a server connection, the list of certificate |
| 2068 | authorities that will be sent or has been sent to the client, as |
| 2069 | controlled by this :class:`Connection`'s :class:`Context`. |
| 2070 | |
| 2071 | If this is a client connection, the list will be empty until the |
| 2072 | connection with the server is established. |
| 2073 | |
| 2074 | .. versionadded:: 0.10 |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2075 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2076 | ca_names = _lib.SSL_get_client_CA_list(self._ssl) |
| 2077 | if ca_names == _ffi.NULL: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 2078 | # TODO: This is untested. |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2079 | return [] |
| 2080 | |
| 2081 | result = [] |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2082 | for i in range(_lib.sk_X509_NAME_num(ca_names)): |
| 2083 | name = _lib.sk_X509_NAME_value(ca_names, i) |
| 2084 | copy = _lib.X509_NAME_dup(name) |
Alex Gaynor | a829e90 | 2016-06-04 18:16:01 -0700 | [diff] [blame] | 2085 | _openssl_assert(copy != _ffi.NULL) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2086 | |
| 2087 | pyname = X509Name.__new__(X509Name) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2088 | pyname._name = _ffi.gc(copy, _lib.X509_NAME_free) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2089 | result.append(pyname) |
| 2090 | return result |
| 2091 | |
Ayke | e7f3345 | 2018-05-16 19:18:16 +0200 | [diff] [blame] | 2092 | def makefile(self, *args, **kwargs): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2093 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 2094 | The makefile() method is not implemented, since there is no dup |
| 2095 | semantics for SSL connections |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2096 | |
Jean-Paul Calderone | 6749ec2 | 2014-04-17 16:30:21 -0400 | [diff] [blame] | 2097 | :raise: NotImplementedError |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2098 | """ |
Alex Gaynor | 8328495 | 2015-09-05 10:43:30 -0400 | [diff] [blame] | 2099 | raise NotImplementedError( |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 2100 | "Cannot make file object of OpenSSL.SSL.Connection" |
| 2101 | ) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2102 | |
| 2103 | def get_app_data(self): |
| 2104 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2105 | Retrieve application data as set by :meth:`set_app_data`. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2106 | |
| 2107 | :return: The application data |
| 2108 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2109 | return self._app_data |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2110 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2111 | def set_app_data(self, data): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2112 | """ |
| 2113 | Set application data |
| 2114 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2115 | :param data: The application data |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2116 | :return: None |
| 2117 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2118 | self._app_data = data |
| 2119 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2120 | def get_shutdown(self): |
| 2121 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2122 | Get the shutdown state of the Connection. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2123 | |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 2124 | :return: The shutdown state, a bitvector of SENT_SHUTDOWN, |
| 2125 | RECEIVED_SHUTDOWN. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2126 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2127 | return _lib.SSL_get_shutdown(self._ssl) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2128 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2129 | def set_shutdown(self, state): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2130 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2131 | Set the shutdown state of the Connection. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2132 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2133 | :param state: bitvector of SENT_SHUTDOWN, RECEIVED_SHUTDOWN. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2134 | :return: None |
| 2135 | """ |
Jean-Paul Calderone | f73a3cb | 2014-02-09 08:49:06 -0500 | [diff] [blame] | 2136 | if not isinstance(state, integer_types): |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2137 | raise TypeError("state must be an integer") |
| 2138 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2139 | _lib.SSL_set_shutdown(self._ssl, state) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2140 | |
Hynek Schlawack | ea94f2b | 2016-03-13 16:17:53 +0100 | [diff] [blame] | 2141 | def get_state_string(self): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2142 | """ |
Hynek Schlawack | ea94f2b | 2016-03-13 16:17:53 +0100 | [diff] [blame] | 2143 | Retrieve a verbose string detailing the state of the Connection. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2144 | |
| 2145 | :return: A string representing the state |
Hynek Schlawack | ea94f2b | 2016-03-13 16:17:53 +0100 | [diff] [blame] | 2146 | :rtype: bytes |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2147 | """ |
kjav | c704a2e | 2015-09-07 12:12:27 +0100 | [diff] [blame] | 2148 | return _ffi.string(_lib.SSL_state_string_long(self._ssl)) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2149 | |
| 2150 | def server_random(self): |
| 2151 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2152 | Retrieve the random value used with the server hello message. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2153 | |
| 2154 | :return: A string representing the state |
| 2155 | """ |
Alex Gaynor | 9360306 | 2016-06-01 20:13:09 -0700 | [diff] [blame] | 2156 | session = _lib.SSL_get_session(self._ssl) |
| 2157 | if session == _ffi.NULL: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2158 | return None |
Alex Gaynor | 9360306 | 2016-06-01 20:13:09 -0700 | [diff] [blame] | 2159 | length = _lib.SSL_get_server_random(self._ssl, _ffi.NULL, 0) |
Adrián Chaves | 98c57be | 2020-03-31 16:14:50 +0200 | [diff] [blame] | 2160 | _openssl_assert(length > 0) |
Cory Benfield | e62840e | 2016-11-28 12:17:08 +0000 | [diff] [blame] | 2161 | outp = _no_zero_allocator("unsigned char[]", length) |
Alex Gaynor | 9360306 | 2016-06-01 20:13:09 -0700 | [diff] [blame] | 2162 | _lib.SSL_get_server_random(self._ssl, outp, length) |
| 2163 | return _ffi.buffer(outp, length)[:] |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2164 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2165 | def client_random(self): |
| 2166 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2167 | Retrieve the random value used with the client hello message. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2168 | |
| 2169 | :return: A string representing the state |
| 2170 | """ |
Alex Gaynor | 9360306 | 2016-06-01 20:13:09 -0700 | [diff] [blame] | 2171 | session = _lib.SSL_get_session(self._ssl) |
| 2172 | if session == _ffi.NULL: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2173 | return None |
Alex Gaynor | 9360306 | 2016-06-01 20:13:09 -0700 | [diff] [blame] | 2174 | |
| 2175 | length = _lib.SSL_get_client_random(self._ssl, _ffi.NULL, 0) |
Adrián Chaves | 98c57be | 2020-03-31 16:14:50 +0200 | [diff] [blame] | 2176 | _openssl_assert(length > 0) |
Cory Benfield | e62840e | 2016-11-28 12:17:08 +0000 | [diff] [blame] | 2177 | outp = _no_zero_allocator("unsigned char[]", length) |
Alex Gaynor | 9360306 | 2016-06-01 20:13:09 -0700 | [diff] [blame] | 2178 | _lib.SSL_get_client_random(self._ssl, outp, length) |
| 2179 | return _ffi.buffer(outp, length)[:] |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2180 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2181 | def master_key(self): |
| 2182 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2183 | Retrieve the value of the master key for this session. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2184 | |
| 2185 | :return: A string representing the state |
| 2186 | """ |
Alex Gaynor | 9360306 | 2016-06-01 20:13:09 -0700 | [diff] [blame] | 2187 | session = _lib.SSL_get_session(self._ssl) |
| 2188 | if session == _ffi.NULL: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2189 | return None |
Alex Gaynor | 9360306 | 2016-06-01 20:13:09 -0700 | [diff] [blame] | 2190 | |
| 2191 | length = _lib.SSL_SESSION_get_master_key(session, _ffi.NULL, 0) |
Adrián Chaves | 98c57be | 2020-03-31 16:14:50 +0200 | [diff] [blame] | 2192 | _openssl_assert(length > 0) |
Cory Benfield | e62840e | 2016-11-28 12:17:08 +0000 | [diff] [blame] | 2193 | outp = _no_zero_allocator("unsigned char[]", length) |
Alex Gaynor | 9360306 | 2016-06-01 20:13:09 -0700 | [diff] [blame] | 2194 | _lib.SSL_SESSION_get_master_key(session, outp, length) |
| 2195 | return _ffi.buffer(outp, length)[:] |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2196 | |
Paul Kehrer | bdb7639 | 2017-12-01 04:54:32 +0800 | [diff] [blame] | 2197 | def export_keying_material(self, label, olen, context=None): |
| 2198 | """ |
| 2199 | Obtain keying material for application use. |
| 2200 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2201 | :param: label - a disambiguating label string as described in RFC 5705 |
| 2202 | :param: olen - the length of the exported key material in bytes |
| 2203 | :param: context - a per-association context value |
| 2204 | :return: the exported key material bytes or None |
Paul Kehrer | bdb7639 | 2017-12-01 04:54:32 +0800 | [diff] [blame] | 2205 | """ |
| 2206 | outp = _no_zero_allocator("unsigned char[]", olen) |
| 2207 | context_buf = _ffi.NULL |
| 2208 | context_len = 0 |
| 2209 | use_context = 0 |
| 2210 | if context is not None: |
| 2211 | context_buf = context |
| 2212 | context_len = len(context) |
| 2213 | use_context = 1 |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 2214 | success = _lib.SSL_export_keying_material( |
| 2215 | self._ssl, |
| 2216 | outp, |
| 2217 | olen, |
| 2218 | label, |
| 2219 | len(label), |
| 2220 | context_buf, |
| 2221 | context_len, |
| 2222 | use_context, |
| 2223 | ) |
Paul Kehrer | bdb7639 | 2017-12-01 04:54:32 +0800 | [diff] [blame] | 2224 | _openssl_assert(success == 1) |
| 2225 | return _ffi.buffer(outp, olen)[:] |
| 2226 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2227 | def sock_shutdown(self, *args, **kwargs): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2228 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2229 | Call the :meth:`shutdown` method of the underlying socket. |
| 2230 | See :manpage:`shutdown(2)`. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2231 | |
| 2232 | :return: What the socket's shutdown() method returns |
| 2233 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2234 | return self._socket.shutdown(*args, **kwargs) |
| 2235 | |
Jeremy Lainé | 460a19d | 2018-05-16 19:44:19 +0200 | [diff] [blame] | 2236 | def get_certificate(self): |
| 2237 | """ |
| 2238 | Retrieve the local certificate (if any) |
| 2239 | |
| 2240 | :return: The local certificate |
| 2241 | """ |
| 2242 | cert = _lib.SSL_get_certificate(self._ssl) |
| 2243 | if cert != _ffi.NULL: |
| 2244 | _lib.X509_up_ref(cert) |
| 2245 | return X509._from_raw_x509_ptr(cert) |
| 2246 | return None |
| 2247 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2248 | def get_peer_certificate(self): |
| 2249 | """ |
| 2250 | Retrieve the other side's certificate (if any) |
| 2251 | |
| 2252 | :return: The peer's certificate |
| 2253 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2254 | cert = _lib.SSL_get_peer_certificate(self._ssl) |
| 2255 | if cert != _ffi.NULL: |
Alex Gaynor | 4aa52c3 | 2017-11-20 09:04:08 -0500 | [diff] [blame] | 2256 | return X509._from_raw_x509_ptr(cert) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2257 | return None |
| 2258 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2259 | def get_peer_cert_chain(self): |
| 2260 | """ |
| 2261 | Retrieve the other side's certificate (if any) |
| 2262 | |
| 2263 | :return: A list of X509 instances giving the peer's certificate chain, |
| 2264 | or None if it does not have one. |
| 2265 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2266 | cert_stack = _lib.SSL_get_peer_cert_chain(self._ssl) |
| 2267 | if cert_stack == _ffi.NULL: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2268 | return None |
| 2269 | |
| 2270 | result = [] |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2271 | for i in range(_lib.sk_X509_num(cert_stack)): |
Jean-Paul Calderone | 73b15c2 | 2013-03-05 18:30:39 -0800 | [diff] [blame] | 2272 | # TODO could incref instead of dup here |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2273 | cert = _lib.X509_dup(_lib.sk_X509_value(cert_stack, i)) |
Alex Gaynor | 4aa52c3 | 2017-11-20 09:04:08 -0500 | [diff] [blame] | 2274 | pycert = X509._from_raw_x509_ptr(cert) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2275 | result.append(pycert) |
| 2276 | return result |
| 2277 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2278 | def want_read(self): |
| 2279 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 2280 | Checks if more data has to be read from the transport layer to complete |
| 2281 | an operation. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2282 | |
| 2283 | :return: True iff more data has to be read |
| 2284 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2285 | return _lib.SSL_want_read(self._ssl) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2286 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2287 | def want_write(self): |
| 2288 | """ |
| 2289 | Checks if there is data to write to the transport layer to complete an |
| 2290 | operation. |
| 2291 | |
| 2292 | :return: True iff there is data to write |
| 2293 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2294 | return _lib.SSL_want_write(self._ssl) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2295 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2296 | def set_accept_state(self): |
| 2297 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 2298 | Set the connection to work in server mode. The handshake will be |
| 2299 | handled automatically by read/write. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2300 | |
| 2301 | :return: None |
| 2302 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2303 | _lib.SSL_set_accept_state(self._ssl) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2304 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2305 | def set_connect_state(self): |
| 2306 | """ |
Alex Gaynor | 62da94d | 2015-09-05 14:37:34 -0400 | [diff] [blame] | 2307 | Set the connection to work in client mode. The handshake will be |
| 2308 | handled automatically by read/write. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2309 | |
| 2310 | :return: None |
| 2311 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2312 | _lib.SSL_set_connect_state(self._ssl) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2313 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2314 | def get_session(self): |
| 2315 | """ |
| 2316 | Returns the Session currently used. |
| 2317 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2318 | :return: An instance of :class:`OpenSSL.SSL.Session` or |
| 2319 | :obj:`None` if no session exists. |
| 2320 | |
| 2321 | .. versionadded:: 0.14 |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2322 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2323 | session = _lib.SSL_get1_session(self._ssl) |
| 2324 | if session == _ffi.NULL: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2325 | return None |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2326 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2327 | pysession = Session.__new__(Session) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2328 | pysession._session = _ffi.gc(session, _lib.SSL_SESSION_free) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2329 | return pysession |
| 2330 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2331 | def set_session(self, session): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2332 | """ |
| 2333 | Set the session to be used when the TLS/SSL connection is established. |
| 2334 | |
| 2335 | :param session: A Session instance representing the session to use. |
| 2336 | :returns: None |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2337 | |
| 2338 | .. versionadded:: 0.14 |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2339 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2340 | if not isinstance(session, Session): |
| 2341 | raise TypeError("session must be a Session instance") |
| 2342 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 2343 | result = _lib.SSL_set_session(self._ssl, session._session) |
Alex Gaynor | 77debda | 2020-04-07 13:40:59 -0400 | [diff] [blame] | 2344 | _openssl_assert(result == 1) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 2345 | |
Jean-Paul Calderone | ac20956 | 2014-03-30 11:26:32 -0400 | [diff] [blame] | 2346 | def _get_finished_message(self, function): |
| 2347 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2348 | Helper to implement :meth:`get_finished` and |
| 2349 | :meth:`get_peer_finished`. |
Jean-Paul Calderone | ac20956 | 2014-03-30 11:26:32 -0400 | [diff] [blame] | 2350 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2351 | :param function: Either :data:`SSL_get_finished`: or |
| 2352 | :data:`SSL_get_peer_finished`. |
Jean-Paul Calderone | ac20956 | 2014-03-30 11:26:32 -0400 | [diff] [blame] | 2353 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2354 | :return: :data:`None` if the desired message has not yet been |
Jean-Paul Calderone | ac20956 | 2014-03-30 11:26:32 -0400 | [diff] [blame] | 2355 | received, otherwise the contents of the message. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2356 | :rtype: :class:`bytes` or :class:`NoneType` |
Jean-Paul Calderone | ac20956 | 2014-03-30 11:26:32 -0400 | [diff] [blame] | 2357 | """ |
Jean-Paul Calderone | 01af904 | 2014-03-30 11:40:42 -0400 | [diff] [blame] | 2358 | # The OpenSSL documentation says nothing about what might happen if the |
| 2359 | # count argument given is zero. Specifically, it doesn't say whether |
| 2360 | # the output buffer may be NULL in that case or not. Inspection of the |
| 2361 | # implementation reveals that it calls memcpy() unconditionally. |
| 2362 | # Section 7.1.4, paragraph 1 of the C standard suggests that |
| 2363 | # memcpy(NULL, source, 0) is not guaranteed to produce defined (let |
| 2364 | # alone desirable) behavior (though it probably does on just about |
| 2365 | # every implementation...) |
| 2366 | # |
| 2367 | # Allocate a tiny buffer to pass in (instead of just passing NULL as |
| 2368 | # one might expect) for the initial call so as to be safe against this |
| 2369 | # potentially undefined behavior. |
| 2370 | empty = _ffi.new("char[]", 0) |
| 2371 | size = function(self._ssl, empty, 0) |
Jean-Paul Calderone | ac20956 | 2014-03-30 11:26:32 -0400 | [diff] [blame] | 2372 | if size == 0: |
| 2373 | # No Finished message so far. |
| 2374 | return None |
| 2375 | |
Cory Benfield | e62840e | 2016-11-28 12:17:08 +0000 | [diff] [blame] | 2376 | buf = _no_zero_allocator("char[]", size) |
Jean-Paul Calderone | ac20956 | 2014-03-30 11:26:32 -0400 | [diff] [blame] | 2377 | function(self._ssl, buf, size) |
| 2378 | return _ffi.buffer(buf, size)[:] |
| 2379 | |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2380 | def get_finished(self): |
| 2381 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2382 | Obtain the latest TLS Finished message that we sent. |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2383 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2384 | :return: The contents of the message or :obj:`None` if the TLS |
Jean-Paul Calderone | ac20956 | 2014-03-30 11:26:32 -0400 | [diff] [blame] | 2385 | handshake has not yet completed. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2386 | :rtype: :class:`bytes` or :class:`NoneType` |
| 2387 | |
| 2388 | .. versionadded:: 0.15 |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2389 | """ |
Jean-Paul Calderone | ac20956 | 2014-03-30 11:26:32 -0400 | [diff] [blame] | 2390 | return self._get_finished_message(_lib.SSL_get_finished) |
| 2391 | |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2392 | def get_peer_finished(self): |
| 2393 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2394 | Obtain the latest TLS Finished message that we received from the peer. |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2395 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2396 | :return: The contents of the message or :obj:`None` if the TLS |
Jean-Paul Calderone | ac20956 | 2014-03-30 11:26:32 -0400 | [diff] [blame] | 2397 | handshake has not yet completed. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2398 | :rtype: :class:`bytes` or :class:`NoneType` |
| 2399 | |
| 2400 | .. versionadded:: 0.15 |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2401 | """ |
Jean-Paul Calderone | ac20956 | 2014-03-30 11:26:32 -0400 | [diff] [blame] | 2402 | return self._get_finished_message(_lib.SSL_get_peer_finished) |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2403 | |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2404 | def get_cipher_name(self): |
| 2405 | """ |
| 2406 | Obtain the name of the currently used cipher. |
Jean-Paul Calderone | 9e3ccd4 | 2014-03-29 18:13:36 -0400 | [diff] [blame] | 2407 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2408 | :returns: The name of the currently used cipher or :obj:`None` |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2409 | if no connection has been established. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2410 | :rtype: :class:`unicode` or :class:`NoneType` |
| 2411 | |
| 2412 | .. versionadded:: 0.15 |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2413 | """ |
| 2414 | cipher = _lib.SSL_get_current_cipher(self._ssl) |
| 2415 | if cipher == _ffi.NULL: |
| 2416 | return None |
| 2417 | else: |
Jean-Paul Calderone | 7f0ded4 | 2014-03-30 10:34:17 -0400 | [diff] [blame] | 2418 | name = _ffi.string(_lib.SSL_CIPHER_get_name(cipher)) |
| 2419 | return name.decode("utf-8") |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2420 | |
| 2421 | def get_cipher_bits(self): |
| 2422 | """ |
| 2423 | Obtain the number of secret bits of the currently used cipher. |
Jean-Paul Calderone | 9e3ccd4 | 2014-03-29 18:13:36 -0400 | [diff] [blame] | 2424 | |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2425 | :returns: The number of secret bits of the currently used cipher |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2426 | or :obj:`None` if no connection has been established. |
| 2427 | :rtype: :class:`int` or :class:`NoneType` |
| 2428 | |
| 2429 | .. versionadded:: 0.15 |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2430 | """ |
| 2431 | cipher = _lib.SSL_get_current_cipher(self._ssl) |
| 2432 | if cipher == _ffi.NULL: |
| 2433 | return None |
| 2434 | else: |
| 2435 | return _lib.SSL_CIPHER_get_bits(cipher, _ffi.NULL) |
| 2436 | |
| 2437 | def get_cipher_version(self): |
| 2438 | """ |
Jean-Paul Calderone | 9e3ccd4 | 2014-03-29 18:13:36 -0400 | [diff] [blame] | 2439 | Obtain the protocol version of the currently used cipher. |
| 2440 | |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2441 | :returns: The protocol name of the currently used cipher |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2442 | or :obj:`None` if no connection has been established. |
| 2443 | :rtype: :class:`unicode` or :class:`NoneType` |
| 2444 | |
| 2445 | .. versionadded:: 0.15 |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2446 | """ |
| 2447 | cipher = _lib.SSL_get_current_cipher(self._ssl) |
| 2448 | if cipher == _ffi.NULL: |
| 2449 | return None |
| 2450 | else: |
Alex Gaynor | c488981 | 2015-09-04 08:43:17 -0400 | [diff] [blame] | 2451 | version = _ffi.string(_lib.SSL_CIPHER_get_version(cipher)) |
Jean-Paul Calderone | 7f0ded4 | 2014-03-30 10:34:17 -0400 | [diff] [blame] | 2452 | return version.decode("utf-8") |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2453 | |
Jim Shaver | abff188 | 2015-05-27 09:15:55 -0400 | [diff] [blame] | 2454 | def get_protocol_version_name(self): |
Jim Shaver | ba65e66 | 2015-04-26 12:23:40 -0400 | [diff] [blame] | 2455 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2456 | Retrieve the protocol version of the current connection. |
Jim Shaver | ba65e66 | 2015-04-26 12:23:40 -0400 | [diff] [blame] | 2457 | |
| 2458 | :returns: The TLS version of the current connection, for example |
Jim Shaver | 58d2573 | 2015-05-28 11:52:32 -0400 | [diff] [blame] | 2459 | the value for TLS 1.2 would be ``TLSv1.2``or ``Unknown`` |
Jim Shaver | b5b6b0e | 2015-05-28 16:47:36 -0400 | [diff] [blame] | 2460 | for connections that were not successfully established. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2461 | :rtype: :class:`unicode` |
Jim Shaver | ba65e66 | 2015-04-26 12:23:40 -0400 | [diff] [blame] | 2462 | """ |
Jim Shaver | d1c896e | 2015-05-27 17:50:21 -0400 | [diff] [blame] | 2463 | version = _ffi.string(_lib.SSL_get_version(self._ssl)) |
Jim Shaver | 58d2573 | 2015-05-28 11:52:32 -0400 | [diff] [blame] | 2464 | return version.decode("utf-8") |
Jim Shaver | b296792 | 2015-04-26 23:58:52 -0400 | [diff] [blame] | 2465 | |
Jim Shaver | 208438c | 2015-05-28 09:52:38 -0400 | [diff] [blame] | 2466 | def get_protocol_version(self): |
| 2467 | """ |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2468 | Retrieve the SSL or TLS protocol version of the current connection. |
Jim Shaver | 208438c | 2015-05-28 09:52:38 -0400 | [diff] [blame] | 2469 | |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2470 | :returns: The TLS version of the current connection. For example, |
| 2471 | it will return ``0x769`` for connections made over TLS version 1. |
| 2472 | :rtype: :class:`int` |
Jim Shaver | 208438c | 2015-05-28 09:52:38 -0400 | [diff] [blame] | 2473 | """ |
| 2474 | version = _lib.SSL_version(self._ssl) |
| 2475 | return version |
| 2476 | |
Cory Benfield | 10b277f | 2015-04-13 17:12:42 -0400 | [diff] [blame] | 2477 | @_requires_npn |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 2478 | def get_next_proto_negotiated(self): |
| 2479 | """ |
| 2480 | Get the protocol that was negotiated by NPN. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2481 | |
| 2482 | :returns: A bytestring of the protocol name. If no protocol has been |
| 2483 | negotiated yet, returns an empty string. |
| 2484 | |
| 2485 | .. versionadded:: 0.15 |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 2486 | """ |
Alex Gaynor | be2bd54 | 2019-02-21 21:41:22 -0500 | [diff] [blame] | 2487 | _warn_npn() |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 2488 | data = _ffi.new("unsigned char **") |
| 2489 | data_len = _ffi.new("unsigned int *") |
| 2490 | |
| 2491 | _lib.SSL_get0_next_proto_negotiated(self._ssl, data, data_len) |
| 2492 | |
Cory Benfield | cd010f6 | 2014-05-15 19:00:27 +0100 | [diff] [blame] | 2493 | return _ffi.buffer(data[0], data_len[0])[:] |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2494 | |
Cory Benfield | 7907e33 | 2015-04-13 17:18:25 -0400 | [diff] [blame] | 2495 | @_requires_alpn |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 2496 | def set_alpn_protos(self, protos): |
| 2497 | """ |
Cory Benfield | e8e9c38 | 2015-04-11 17:33:48 -0400 | [diff] [blame] | 2498 | Specify the client's ALPN protocol list. |
| 2499 | |
| 2500 | These protocols are offered to the server during protocol negotiation. |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 2501 | |
| 2502 | :param protos: A list of the protocols to be offered to the server. |
| 2503 | This list should be a Python list of bytestrings representing the |
| 2504 | protocols to offer, e.g. ``[b'http/1.1', b'spdy/2']``. |
| 2505 | """ |
| 2506 | # Take the list of protocols and join them together, prefixing them |
| 2507 | # with their lengths. |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 2508 | protostr = b"".join( |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 2509 | chain.from_iterable((int2byte(len(p)), p) for p in protos) |
| 2510 | ) |
| 2511 | |
| 2512 | # Build a C string from the list. We don't need to save this off |
| 2513 | # because OpenSSL immediately copies the data out. |
| 2514 | input_str = _ffi.new("unsigned char[]", protostr) |
Alex Gaynor | d61c46a | 2017-06-29 22:51:33 -0700 | [diff] [blame] | 2515 | _lib.SSL_set_alpn_protos(self._ssl, input_str, len(protostr)) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 2516 | |
Maximilian Hils | 66ded6a | 2015-08-26 06:02:03 +0200 | [diff] [blame] | 2517 | @_requires_alpn |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 2518 | def get_alpn_proto_negotiated(self): |
Cory Benfield | 222f30e | 2015-04-13 18:10:21 -0400 | [diff] [blame] | 2519 | """ |
| 2520 | Get the protocol that was negotiated by ALPN. |
Alex Chan | d072cae | 2018-02-15 09:57:59 +0000 | [diff] [blame] | 2521 | |
| 2522 | :returns: A bytestring of the protocol name. If no protocol has been |
| 2523 | negotiated yet, returns an empty string. |
Cory Benfield | 222f30e | 2015-04-13 18:10:21 -0400 | [diff] [blame] | 2524 | """ |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 2525 | data = _ffi.new("unsigned char **") |
| 2526 | data_len = _ffi.new("unsigned int *") |
| 2527 | |
| 2528 | _lib.SSL_get0_alpn_selected(self._ssl, data, data_len) |
| 2529 | |
Cory Benfield | e8e9c38 | 2015-04-11 17:33:48 -0400 | [diff] [blame] | 2530 | if not data_len: |
Alex Gaynor | 0373718 | 2020-07-23 20:40:46 -0400 | [diff] [blame^] | 2531 | return b"" |
Cory Benfield | e8e9c38 | 2015-04-11 17:33:48 -0400 | [diff] [blame] | 2532 | |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 2533 | return _ffi.buffer(data[0], data_len[0])[:] |
| 2534 | |
Cory Benfield | 496652a | 2017-01-24 11:42:56 +0000 | [diff] [blame] | 2535 | def request_ocsp(self): |
| 2536 | """ |
| 2537 | Called to request that the server sends stapled OCSP data, if |
| 2538 | available. If this is not called on the client side then the server |
| 2539 | will not send OCSP data. Should be used in conjunction with |
| 2540 | :meth:`Context.set_ocsp_client_callback`. |
| 2541 | """ |
| 2542 | rc = _lib.SSL_set_tlsext_status_type( |
| 2543 | self._ssl, _lib.TLSEXT_STATUSTYPE_ocsp |
| 2544 | ) |
| 2545 | _openssl_assert(rc == 1) |
| 2546 | |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 2547 | |
Jean-Paul Calderone | fab157b | 2014-01-18 11:21:38 -0500 | [diff] [blame] | 2548 | # This is similar to the initialization calls at the end of OpenSSL/crypto.py |
| 2549 | # but is exercised mostly by the Context initializer. |
Jean-Paul Calderone | 11ed8e8 | 2014-01-18 10:21:50 -0500 | [diff] [blame] | 2550 | _lib.SSL_library_init() |