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