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