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