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