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