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