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