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