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