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