Konstantinos Koukopoulos | 541150d | 2014-01-31 01:00:19 +0200 | [diff] [blame] | 1 | from sys import platform |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 2 | from functools import wraps, partial |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 3 | from itertools import count |
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 |
Jean-Paul Calderone | 63eab69 | 2014-01-18 10:19:56 -0500 | [diff] [blame] | 9 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 10 | from OpenSSL._util import ( |
| 11 | ffi as _ffi, |
| 12 | lib as _lib, |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 13 | exception_from_error_queue as _exception_from_error_queue, |
| 14 | native as _native) |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 15 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 16 | from OpenSSL.crypto import ( |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 17 | FILETYPE_PEM, _PassphraseHelper, PKey, X509Name, X509, X509Store) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 18 | |
| 19 | _unspecified = object() |
| 20 | |
Jean-Paul Calderone | 8fb5318 | 2013-12-30 08:35:49 -0500 | [diff] [blame] | 21 | try: |
| 22 | _memoryview = memoryview |
| 23 | except NameError: |
| 24 | class _memoryview(object): |
| 25 | pass |
| 26 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 27 | OPENSSL_VERSION_NUMBER = _lib.OPENSSL_VERSION_NUMBER |
| 28 | SSLEAY_VERSION = _lib.SSLEAY_VERSION |
| 29 | SSLEAY_CFLAGS = _lib.SSLEAY_CFLAGS |
| 30 | SSLEAY_PLATFORM = _lib.SSLEAY_PLATFORM |
| 31 | SSLEAY_DIR = _lib.SSLEAY_DIR |
| 32 | SSLEAY_BUILT_ON = _lib.SSLEAY_BUILT_ON |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 33 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 34 | SENT_SHUTDOWN = _lib.SSL_SENT_SHUTDOWN |
| 35 | RECEIVED_SHUTDOWN = _lib.SSL_RECEIVED_SHUTDOWN |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 36 | |
| 37 | SSLv2_METHOD = 1 |
| 38 | SSLv3_METHOD = 2 |
| 39 | SSLv23_METHOD = 3 |
| 40 | TLSv1_METHOD = 4 |
Jean-Paul Calderone | 56bff94 | 2013-11-03 11:30:43 -0500 | [diff] [blame] | 41 | TLSv1_1_METHOD = 5 |
| 42 | TLSv1_2_METHOD = 6 |
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 | OP_NO_SSLv2 = _lib.SSL_OP_NO_SSLv2 |
| 45 | OP_NO_SSLv3 = _lib.SSL_OP_NO_SSLv3 |
| 46 | OP_NO_TLSv1 = _lib.SSL_OP_NO_TLSv1 |
Jean-Paul Calderone | be2bb42 | 2013-12-29 07:34:08 -0500 | [diff] [blame] | 47 | |
| 48 | OP_NO_TLSv1_1 = getattr(_lib, "SSL_OP_NO_TLSv1_1", 0) |
| 49 | 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] | 50 | |
Jean-Paul Calderone | 0d7e8a1 | 2014-01-08 16:54:13 -0500 | [diff] [blame] | 51 | try: |
| 52 | MODE_RELEASE_BUFFERS = _lib.SSL_MODE_RELEASE_BUFFERS |
| 53 | except AttributeError: |
| 54 | pass |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 55 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 56 | OP_SINGLE_DH_USE = _lib.SSL_OP_SINGLE_DH_USE |
| 57 | OP_EPHEMERAL_RSA = _lib.SSL_OP_EPHEMERAL_RSA |
| 58 | OP_MICROSOFT_SESS_ID_BUG = _lib.SSL_OP_MICROSOFT_SESS_ID_BUG |
| 59 | OP_NETSCAPE_CHALLENGE_BUG = _lib.SSL_OP_NETSCAPE_CHALLENGE_BUG |
| 60 | OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG = _lib.SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG |
| 61 | OP_SSLREF2_REUSE_CERT_TYPE_BUG = _lib.SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG |
| 62 | 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] | 63 | try: |
| 64 | OP_MSIE_SSLV2_RSA_PADDING = _lib.SSL_OP_MSIE_SSLV2_RSA_PADDING |
| 65 | except AttributeError: |
| 66 | pass |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 67 | OP_SSLEAY_080_CLIENT_DH_BUG = _lib.SSL_OP_SSLEAY_080_CLIENT_DH_BUG |
| 68 | OP_TLS_D5_BUG = _lib.SSL_OP_TLS_D5_BUG |
| 69 | OP_TLS_BLOCK_PADDING_BUG = _lib.SSL_OP_TLS_BLOCK_PADDING_BUG |
| 70 | OP_DONT_INSERT_EMPTY_FRAGMENTS = _lib.SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS |
| 71 | OP_CIPHER_SERVER_PREFERENCE = _lib.SSL_OP_CIPHER_SERVER_PREFERENCE |
| 72 | OP_TLS_ROLLBACK_BUG = _lib.SSL_OP_TLS_ROLLBACK_BUG |
| 73 | OP_PKCS1_CHECK_1 = _lib.SSL_OP_PKCS1_CHECK_1 |
| 74 | OP_PKCS1_CHECK_2 = _lib.SSL_OP_PKCS1_CHECK_2 |
| 75 | OP_NETSCAPE_CA_DN_BUG = _lib.SSL_OP_NETSCAPE_CA_DN_BUG |
| 76 | 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] | 77 | try: |
| 78 | OP_NO_COMPRESSION = _lib.SSL_OP_NO_COMPRESSION |
| 79 | except AttributeError: |
| 80 | pass |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 81 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 82 | OP_NO_QUERY_MTU = _lib.SSL_OP_NO_QUERY_MTU |
| 83 | OP_COOKIE_EXCHANGE = _lib.SSL_OP_COOKIE_EXCHANGE |
| 84 | OP_NO_TICKET = _lib.SSL_OP_NO_TICKET |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 85 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 86 | OP_ALL = _lib.SSL_OP_ALL |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 87 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 88 | VERIFY_PEER = _lib.SSL_VERIFY_PEER |
| 89 | VERIFY_FAIL_IF_NO_PEER_CERT = _lib.SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
| 90 | VERIFY_CLIENT_ONCE = _lib.SSL_VERIFY_CLIENT_ONCE |
| 91 | VERIFY_NONE = _lib.SSL_VERIFY_NONE |
Jean-Paul Calderone | 935d2da | 2013-03-04 08:11:19 -0800 | [diff] [blame] | 92 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 93 | SESS_CACHE_OFF = _lib.SSL_SESS_CACHE_OFF |
| 94 | SESS_CACHE_CLIENT = _lib.SSL_SESS_CACHE_CLIENT |
| 95 | SESS_CACHE_SERVER = _lib.SSL_SESS_CACHE_SERVER |
| 96 | SESS_CACHE_BOTH = _lib.SSL_SESS_CACHE_BOTH |
| 97 | SESS_CACHE_NO_AUTO_CLEAR = _lib.SSL_SESS_CACHE_NO_AUTO_CLEAR |
| 98 | SESS_CACHE_NO_INTERNAL_LOOKUP = _lib.SSL_SESS_CACHE_NO_INTERNAL_LOOKUP |
| 99 | SESS_CACHE_NO_INTERNAL_STORE = _lib.SSL_SESS_CACHE_NO_INTERNAL_STORE |
| 100 | SESS_CACHE_NO_INTERNAL = _lib.SSL_SESS_CACHE_NO_INTERNAL |
Jean-Paul Calderone | d39a3f6 | 2013-03-04 12:23:51 -0800 | [diff] [blame] | 101 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 102 | SSL_ST_CONNECT = _lib.SSL_ST_CONNECT |
| 103 | SSL_ST_ACCEPT = _lib.SSL_ST_ACCEPT |
| 104 | SSL_ST_MASK = _lib.SSL_ST_MASK |
| 105 | SSL_ST_INIT = _lib.SSL_ST_INIT |
| 106 | SSL_ST_BEFORE = _lib.SSL_ST_BEFORE |
| 107 | SSL_ST_OK = _lib.SSL_ST_OK |
| 108 | SSL_ST_RENEGOTIATE = _lib.SSL_ST_RENEGOTIATE |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 109 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 110 | SSL_CB_LOOP = _lib.SSL_CB_LOOP |
| 111 | SSL_CB_EXIT = _lib.SSL_CB_EXIT |
| 112 | SSL_CB_READ = _lib.SSL_CB_READ |
| 113 | SSL_CB_WRITE = _lib.SSL_CB_WRITE |
| 114 | SSL_CB_ALERT = _lib.SSL_CB_ALERT |
| 115 | SSL_CB_READ_ALERT = _lib.SSL_CB_READ_ALERT |
| 116 | SSL_CB_WRITE_ALERT = _lib.SSL_CB_WRITE_ALERT |
| 117 | SSL_CB_ACCEPT_LOOP = _lib.SSL_CB_ACCEPT_LOOP |
| 118 | SSL_CB_ACCEPT_EXIT = _lib.SSL_CB_ACCEPT_EXIT |
| 119 | SSL_CB_CONNECT_LOOP = _lib.SSL_CB_CONNECT_LOOP |
| 120 | SSL_CB_CONNECT_EXIT = _lib.SSL_CB_CONNECT_EXIT |
| 121 | SSL_CB_HANDSHAKE_START = _lib.SSL_CB_HANDSHAKE_START |
| 122 | SSL_CB_HANDSHAKE_DONE = _lib.SSL_CB_HANDSHAKE_DONE |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 123 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 124 | |
Andy Lutomirski | b4e5c8d | 2014-03-05 12:54:15 -0800 | [diff] [blame^] | 125 | NID_X9_62_c2pnb163v1 = _lib.NID_X9_62_c2pnb163v1 |
| 126 | SN_X9_62_c2pnb163v1 = _ffi.string(_lib.SN_X9_62_c2pnb163v1) |
| 127 | NID_X9_62_c2pnb163v2 = _lib.NID_X9_62_c2pnb163v2 |
| 128 | SN_X9_62_c2pnb163v2 = _ffi.string(_lib.SN_X9_62_c2pnb163v2) |
| 129 | NID_X9_62_c2pnb163v3 = _lib.NID_X9_62_c2pnb163v3 |
| 130 | SN_X9_62_c2pnb163v3 = _ffi.string(_lib.SN_X9_62_c2pnb163v3) |
| 131 | NID_X9_62_c2pnb176v1 = _lib.NID_X9_62_c2pnb176v1 |
| 132 | SN_X9_62_c2pnb176v1 = _ffi.string(_lib.SN_X9_62_c2pnb176v1) |
| 133 | NID_X9_62_c2tnb191v1 = _lib.NID_X9_62_c2tnb191v1 |
| 134 | SN_X9_62_c2tnb191v1 = _ffi.string(_lib.SN_X9_62_c2tnb191v1) |
| 135 | NID_X9_62_c2tnb191v2 = _lib.NID_X9_62_c2tnb191v2 |
| 136 | SN_X9_62_c2tnb191v2 = _ffi.string(_lib.SN_X9_62_c2tnb191v2) |
| 137 | NID_X9_62_c2tnb191v3 = _lib.NID_X9_62_c2tnb191v3 |
| 138 | SN_X9_62_c2tnb191v3 = _ffi.string(_lib.SN_X9_62_c2tnb191v3) |
| 139 | NID_X9_62_c2onb191v4 = _lib.NID_X9_62_c2onb191v4 |
| 140 | SN_X9_62_c2onb191v4 = _ffi.string(_lib.SN_X9_62_c2onb191v4) |
| 141 | NID_X9_62_c2onb191v5 = _lib.NID_X9_62_c2onb191v5 |
| 142 | SN_X9_62_c2onb191v5 = _ffi.string(_lib.SN_X9_62_c2onb191v5) |
| 143 | NID_X9_62_c2pnb208w1 = _lib.NID_X9_62_c2pnb208w1 |
| 144 | SN_X9_62_c2pnb208w1 = _ffi.string(_lib.SN_X9_62_c2pnb208w1) |
| 145 | NID_X9_62_c2tnb239v1 = _lib.NID_X9_62_c2tnb239v1 |
| 146 | SN_X9_62_c2tnb239v1 = _ffi.string(_lib.SN_X9_62_c2tnb239v1) |
| 147 | NID_X9_62_c2tnb239v2 = _lib.NID_X9_62_c2tnb239v2 |
| 148 | SN_X9_62_c2tnb239v2 = _ffi.string(_lib.SN_X9_62_c2tnb239v2) |
| 149 | NID_X9_62_c2tnb239v3 = _lib.NID_X9_62_c2tnb239v3 |
| 150 | SN_X9_62_c2tnb239v3 = _ffi.string(_lib.SN_X9_62_c2tnb239v3) |
| 151 | NID_X9_62_c2onb239v4 = _lib.NID_X9_62_c2onb239v4 |
| 152 | SN_X9_62_c2onb239v4 = _ffi.string(_lib.SN_X9_62_c2onb239v4) |
| 153 | NID_X9_62_c2onb239v5 = _lib.NID_X9_62_c2onb239v5 |
| 154 | SN_X9_62_c2onb239v5 = _ffi.string(_lib.SN_X9_62_c2onb239v5) |
| 155 | NID_X9_62_c2pnb272w1 = _lib.NID_X9_62_c2pnb272w1 |
| 156 | SN_X9_62_c2pnb272w1 = _ffi.string(_lib.SN_X9_62_c2pnb272w1) |
| 157 | NID_X9_62_c2pnb304w1 = _lib.NID_X9_62_c2pnb304w1 |
| 158 | SN_X9_62_c2pnb304w1 = _ffi.string(_lib.SN_X9_62_c2pnb304w1) |
| 159 | NID_X9_62_c2tnb359v1 = _lib.NID_X9_62_c2tnb359v1 |
| 160 | SN_X9_62_c2tnb359v1 = _ffi.string(_lib.SN_X9_62_c2tnb359v1) |
| 161 | NID_X9_62_c2pnb368w1 = _lib.NID_X9_62_c2pnb368w1 |
| 162 | SN_X9_62_c2pnb368w1 = _ffi.string(_lib.SN_X9_62_c2pnb368w1) |
| 163 | NID_X9_62_c2tnb431r1 = _lib.NID_X9_62_c2tnb431r1 |
| 164 | SN_X9_62_c2tnb431r1 = _ffi.string(_lib.SN_X9_62_c2tnb431r1) |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 165 | NID_X9_62_prime192v1 = _lib.NID_X9_62_prime192v1 |
Andy Lutomirski | b4e5c8d | 2014-03-05 12:54:15 -0800 | [diff] [blame^] | 166 | SN_X9_62_prime192v1 = _ffi.string(_lib.SN_X9_62_prime192v1) |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 167 | NID_X9_62_prime192v2 = _lib.NID_X9_62_prime192v2 |
Andy Lutomirski | b4e5c8d | 2014-03-05 12:54:15 -0800 | [diff] [blame^] | 168 | SN_X9_62_prime192v2 = _ffi.string(_lib.SN_X9_62_prime192v2) |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 169 | NID_X9_62_prime192v3 = _lib.NID_X9_62_prime192v3 |
Andy Lutomirski | b4e5c8d | 2014-03-05 12:54:15 -0800 | [diff] [blame^] | 170 | SN_X9_62_prime192v3 = _ffi.string(_lib.SN_X9_62_prime192v3) |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 171 | NID_X9_62_prime239v1 = _lib.NID_X9_62_prime239v1 |
Andy Lutomirski | b4e5c8d | 2014-03-05 12:54:15 -0800 | [diff] [blame^] | 172 | SN_X9_62_prime239v1 = _ffi.string(_lib.SN_X9_62_prime239v1) |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 173 | NID_X9_62_prime239v2 = _lib.NID_X9_62_prime239v2 |
Andy Lutomirski | b4e5c8d | 2014-03-05 12:54:15 -0800 | [diff] [blame^] | 174 | SN_X9_62_prime239v2 = _ffi.string(_lib.SN_X9_62_prime239v2) |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 175 | NID_X9_62_prime239v3 = _lib.NID_X9_62_prime239v3 |
Andy Lutomirski | b4e5c8d | 2014-03-05 12:54:15 -0800 | [diff] [blame^] | 176 | SN_X9_62_prime239v3 = _ffi.string(_lib.SN_X9_62_prime239v3) |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 177 | NID_X9_62_prime256v1 = _lib.NID_X9_62_prime256v1 |
Andy Lutomirski | b4e5c8d | 2014-03-05 12:54:15 -0800 | [diff] [blame^] | 178 | SN_X9_62_prime256v1 = _ffi.string(_lib.SN_X9_62_prime256v1) |
| 179 | NID_secp112r1 = _lib.NID_secp112r1 |
| 180 | SN_secp112r1 = _ffi.string(_lib.SN_secp112r1) |
| 181 | NID_secp112r2 = _lib.NID_secp112r2 |
| 182 | SN_secp112r2 = _ffi.string(_lib.SN_secp112r2) |
| 183 | NID_secp128r1 = _lib.NID_secp128r1 |
| 184 | SN_secp128r1 = _ffi.string(_lib.SN_secp128r1) |
| 185 | NID_secp128r2 = _lib.NID_secp128r2 |
| 186 | SN_secp128r2 = _ffi.string(_lib.SN_secp128r2) |
| 187 | NID_secp160k1 = _lib.NID_secp160k1 |
| 188 | SN_secp160k1 = _ffi.string(_lib.SN_secp160k1) |
| 189 | NID_secp160r1 = _lib.NID_secp160r1 |
| 190 | SN_secp160r1 = _ffi.string(_lib.SN_secp160r1) |
| 191 | NID_secp160r2 = _lib.NID_secp160r2 |
| 192 | SN_secp160r2 = _ffi.string(_lib.SN_secp160r2) |
| 193 | NID_sect163k1 = _lib.NID_sect163k1 |
| 194 | SN_sect163k1 = _ffi.string(_lib.SN_sect163k1) |
| 195 | NID_sect163r1 = _lib.NID_sect163r1 |
| 196 | SN_sect163r1 = _ffi.string(_lib.SN_sect163r1) |
| 197 | NID_sect163r2 = _lib.NID_sect163r2 |
| 198 | SN_sect163r2 = _ffi.string(_lib.SN_sect163r2) |
| 199 | NID_secp192k1 = _lib.NID_secp192k1 |
| 200 | SN_secp192k1 = _ffi.string(_lib.SN_secp192k1) |
| 201 | NID_secp224k1 = _lib.NID_secp224k1 |
| 202 | SN_secp224k1 = _ffi.string(_lib.SN_secp224k1) |
| 203 | NID_secp224r1 = _lib.NID_secp224r1 |
| 204 | SN_secp224r1 = _ffi.string(_lib.SN_secp224r1) |
| 205 | NID_secp256k1 = _lib.NID_secp256k1 |
| 206 | SN_secp256k1 = _ffi.string(_lib.SN_secp256k1) |
| 207 | NID_secp384r1 = _lib.NID_secp384r1 |
| 208 | SN_secp384r1 = _ffi.string(_lib.SN_secp384r1) |
| 209 | NID_secp521r1 = _lib.NID_secp521r1 |
| 210 | SN_secp521r1 = _ffi.string(_lib.SN_secp521r1) |
| 211 | NID_sect113r1 = _lib.NID_sect113r1 |
| 212 | SN_sect113r1 = _ffi.string(_lib.SN_sect113r1) |
| 213 | NID_sect113r2 = _lib.NID_sect113r2 |
| 214 | SN_sect113r2 = _ffi.string(_lib.SN_sect113r2) |
| 215 | NID_sect131r1 = _lib.NID_sect131r1 |
| 216 | SN_sect131r1 = _ffi.string(_lib.SN_sect131r1) |
| 217 | NID_sect131r2 = _lib.NID_sect131r2 |
| 218 | SN_sect131r2 = _ffi.string(_lib.SN_sect131r2) |
| 219 | NID_sect193r1 = _lib.NID_sect193r1 |
| 220 | SN_sect193r1 = _ffi.string(_lib.SN_sect193r1) |
| 221 | NID_sect193r2 = _lib.NID_sect193r2 |
| 222 | SN_sect193r2 = _ffi.string(_lib.SN_sect193r2) |
| 223 | NID_sect233k1 = _lib.NID_sect233k1 |
| 224 | SN_sect233k1 = _ffi.string(_lib.SN_sect233k1) |
| 225 | NID_sect233r1 = _lib.NID_sect233r1 |
| 226 | SN_sect233r1 = _ffi.string(_lib.SN_sect233r1) |
| 227 | NID_sect239k1 = _lib.NID_sect239k1 |
| 228 | SN_sect239k1 = _ffi.string(_lib.SN_sect239k1) |
| 229 | NID_sect283k1 = _lib.NID_sect283k1 |
| 230 | SN_sect283k1 = _ffi.string(_lib.SN_sect283k1) |
| 231 | NID_sect283r1 = _lib.NID_sect283r1 |
| 232 | SN_sect283r1 = _ffi.string(_lib.SN_sect283r1) |
| 233 | NID_sect409k1 = _lib.NID_sect409k1 |
| 234 | SN_sect409k1 = _ffi.string(_lib.SN_sect409k1) |
| 235 | NID_sect409r1 = _lib.NID_sect409r1 |
| 236 | SN_sect409r1 = _ffi.string(_lib.SN_sect409r1) |
| 237 | NID_sect571k1 = _lib.NID_sect571k1 |
| 238 | SN_sect571k1 = _ffi.string(_lib.SN_sect571k1) |
| 239 | NID_sect571r1 = _lib.NID_sect571r1 |
| 240 | SN_sect571r1 = _ffi.string(_lib.SN_sect571r1) |
| 241 | NID_wap_wsg_idm_ecid_wtls1 = _lib.NID_wap_wsg_idm_ecid_wtls1 |
| 242 | SN_wap_wsg_idm_ecid_wtls1 = _ffi.string(_lib.SN_wap_wsg_idm_ecid_wtls1) |
| 243 | NID_wap_wsg_idm_ecid_wtls3 = _lib.NID_wap_wsg_idm_ecid_wtls3 |
| 244 | SN_wap_wsg_idm_ecid_wtls3 = _ffi.string(_lib.SN_wap_wsg_idm_ecid_wtls3) |
| 245 | NID_wap_wsg_idm_ecid_wtls4 = _lib.NID_wap_wsg_idm_ecid_wtls4 |
| 246 | SN_wap_wsg_idm_ecid_wtls4 = _ffi.string(_lib.SN_wap_wsg_idm_ecid_wtls4) |
| 247 | NID_wap_wsg_idm_ecid_wtls5 = _lib.NID_wap_wsg_idm_ecid_wtls5 |
| 248 | SN_wap_wsg_idm_ecid_wtls5 = _ffi.string(_lib.SN_wap_wsg_idm_ecid_wtls5) |
| 249 | NID_wap_wsg_idm_ecid_wtls6 = _lib.NID_wap_wsg_idm_ecid_wtls6 |
| 250 | SN_wap_wsg_idm_ecid_wtls6 = _ffi.string(_lib.SN_wap_wsg_idm_ecid_wtls6) |
| 251 | NID_wap_wsg_idm_ecid_wtls7 = _lib.NID_wap_wsg_idm_ecid_wtls7 |
| 252 | SN_wap_wsg_idm_ecid_wtls7 = _ffi.string(_lib.SN_wap_wsg_idm_ecid_wtls7) |
| 253 | NID_wap_wsg_idm_ecid_wtls8 = _lib.NID_wap_wsg_idm_ecid_wtls8 |
| 254 | SN_wap_wsg_idm_ecid_wtls8 = _ffi.string(_lib.SN_wap_wsg_idm_ecid_wtls8) |
| 255 | NID_wap_wsg_idm_ecid_wtls9 = _lib.NID_wap_wsg_idm_ecid_wtls9 |
| 256 | SN_wap_wsg_idm_ecid_wtls9 = _ffi.string(_lib.SN_wap_wsg_idm_ecid_wtls9) |
| 257 | NID_wap_wsg_idm_ecid_wtls10 = _lib.NID_wap_wsg_idm_ecid_wtls10 |
| 258 | SN_wap_wsg_idm_ecid_wtls10 = _ffi.string(_lib.SN_wap_wsg_idm_ecid_wtls10) |
| 259 | NID_wap_wsg_idm_ecid_wtls11 = _lib.NID_wap_wsg_idm_ecid_wtls11 |
| 260 | SN_wap_wsg_idm_ecid_wtls11 = _ffi.string(_lib.SN_wap_wsg_idm_ecid_wtls11) |
| 261 | NID_wap_wsg_idm_ecid_wtls12 = _lib.NID_wap_wsg_idm_ecid_wtls12 |
| 262 | SN_wap_wsg_idm_ecid_wtls12 = _ffi.string(_lib.SN_wap_wsg_idm_ecid_wtls12) |
| 263 | NID_ipsec3 = _lib.NID_ipsec3 |
| 264 | SN_ipsec3 = _ffi.string(_lib.SN_ipsec3) |
| 265 | NID_ipsec4 = _lib.NID_ipsec4 |
| 266 | SN_ipsec4 = _ffi.string(_lib.SN_ipsec4) |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 267 | |
Alex Gaynor | 807853c | 2014-01-17 13:03:27 -0600 | [diff] [blame] | 268 | _Cryptography_HAS_EC = _lib.Cryptography_HAS_EC |
Alex Gaynor | 12dc084 | 2014-01-17 12:51:31 -0600 | [diff] [blame] | 269 | |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 270 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 271 | class Error(Exception): |
Jean-Paul Calderone | 511cde0 | 2013-12-29 10:31:13 -0500 | [diff] [blame] | 272 | """ |
| 273 | An error occurred in an `OpenSSL.SSL` API. |
| 274 | """ |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 275 | |
| 276 | |
| 277 | |
| 278 | _raise_current_error = partial(_exception_from_error_queue, Error) |
| 279 | |
| 280 | |
| 281 | class WantReadError(Error): |
| 282 | pass |
| 283 | |
| 284 | |
| 285 | |
| 286 | class WantWriteError(Error): |
| 287 | pass |
| 288 | |
| 289 | |
| 290 | |
| 291 | class WantX509LookupError(Error): |
| 292 | pass |
| 293 | |
| 294 | |
| 295 | |
| 296 | class ZeroReturnError(Error): |
| 297 | pass |
| 298 | |
| 299 | |
| 300 | |
| 301 | class SysCallError(Error): |
| 302 | pass |
| 303 | |
| 304 | |
| 305 | |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 306 | class _VerifyHelper(object): |
| 307 | def __init__(self, connection, callback): |
| 308 | self._problems = [] |
| 309 | |
| 310 | @wraps(callback) |
| 311 | def wrapper(ok, store_ctx): |
| 312 | cert = X509.__new__(X509) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 313 | cert._x509 = _lib.X509_STORE_CTX_get_current_cert(store_ctx) |
| 314 | error_number = _lib.X509_STORE_CTX_get_error(store_ctx) |
| 315 | error_depth = _lib.X509_STORE_CTX_get_error_depth(store_ctx) |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 316 | |
| 317 | try: |
| 318 | result = callback(connection, cert, error_number, error_depth, ok) |
| 319 | except Exception as e: |
| 320 | self._problems.append(e) |
| 321 | return 0 |
| 322 | else: |
| 323 | if result: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 324 | _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] | 325 | return 1 |
| 326 | else: |
| 327 | return 0 |
| 328 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 329 | self.callback = _ffi.callback( |
| 330 | "int (*)(int, X509_STORE_CTX *)", wrapper) |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 331 | |
| 332 | |
| 333 | def raise_if_problem(self): |
| 334 | if self._problems: |
| 335 | try: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 336 | _raise_current_error() |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 337 | except Error: |
| 338 | pass |
| 339 | raise self._problems.pop(0) |
| 340 | |
| 341 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 342 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 343 | def _asFileDescriptor(obj): |
| 344 | fd = None |
Konstantinos Koukopoulos | c8b13ea | 2014-01-28 00:21:50 -0800 | [diff] [blame] | 345 | if not isinstance(obj, integer_types): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 346 | meth = getattr(obj, "fileno", None) |
| 347 | if meth is not None: |
| 348 | obj = meth() |
| 349 | |
Konstantinos Koukopoulos | c8b13ea | 2014-01-28 00:21:50 -0800 | [diff] [blame] | 350 | if isinstance(obj, integer_types): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 351 | fd = obj |
| 352 | |
Konstantinos Koukopoulos | c8b13ea | 2014-01-28 00:21:50 -0800 | [diff] [blame] | 353 | if not isinstance(fd, integer_types): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 354 | raise TypeError("argument must be an int, or have a fileno() method.") |
| 355 | elif fd < 0: |
| 356 | raise ValueError( |
| 357 | "file descriptor cannot be a negative integer (%i)" % (fd,)) |
| 358 | |
| 359 | return fd |
| 360 | |
| 361 | |
| 362 | |
Jean-Paul Calderone | d39a3f6 | 2013-03-04 12:23:51 -0800 | [diff] [blame] | 363 | def SSLeay_version(type): |
| 364 | """ |
| 365 | Return a string describing the version of OpenSSL in use. |
| 366 | |
| 367 | :param type: One of the SSLEAY_ constants defined in this module. |
| 368 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 369 | return _ffi.string(_lib.SSLeay_version(type)) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 370 | |
| 371 | |
| 372 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 373 | class Session(object): |
| 374 | pass |
| 375 | |
| 376 | |
| 377 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 378 | class Context(object): |
| 379 | """ |
| 380 | :py:obj:`OpenSSL.SSL.Context` instances define the parameters for setting up |
| 381 | new SSL connections. |
| 382 | """ |
| 383 | _methods = { |
Jean-Paul Calderone | be2bb42 | 2013-12-29 07:34:08 -0500 | [diff] [blame] | 384 | SSLv3_METHOD: "SSLv3_method", |
| 385 | SSLv23_METHOD: "SSLv23_method", |
| 386 | TLSv1_METHOD: "TLSv1_method", |
| 387 | TLSv1_1_METHOD: "TLSv1_1_method", |
| 388 | TLSv1_2_METHOD: "TLSv1_2_method", |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 389 | } |
Jean-Paul Calderone | be2bb42 | 2013-12-29 07:34:08 -0500 | [diff] [blame] | 390 | _methods = dict( |
| 391 | (identifier, getattr(_lib, name)) |
| 392 | for (identifier, name) in _methods.items() |
| 393 | if getattr(_lib, name, None) is not None) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 394 | |
Jean-Paul Calderone | 6315787 | 2013-03-20 16:43:38 -0700 | [diff] [blame] | 395 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 396 | def __init__(self, method): |
| 397 | """ |
| 398 | :param method: One of SSLv2_METHOD, SSLv3_METHOD, SSLv23_METHOD, or |
| 399 | TLSv1_METHOD. |
| 400 | """ |
Jean-Paul Calderone | f73a3cb | 2014-02-09 08:49:06 -0500 | [diff] [blame] | 401 | if not isinstance(method, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 402 | raise TypeError("method must be an integer") |
| 403 | |
| 404 | try: |
| 405 | method_func = self._methods[method] |
| 406 | except KeyError: |
| 407 | raise ValueError("No such protocol") |
| 408 | |
| 409 | method_obj = method_func() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 410 | if method_obj == _ffi.NULL: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 411 | # TODO: This is untested. |
| 412 | _raise_current_error() |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 413 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 414 | context = _lib.SSL_CTX_new(method_obj) |
| 415 | if context == _ffi.NULL: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 416 | # TODO: This is untested. |
| 417 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 418 | context = _ffi.gc(context, _lib.SSL_CTX_free) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 419 | |
| 420 | self._context = context |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 421 | self._passphrase_helper = None |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 422 | self._passphrase_callback = None |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 423 | self._passphrase_userdata = None |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 424 | self._verify_helper = None |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 425 | self._verify_callback = None |
| 426 | self._info_callback = None |
| 427 | self._tlsext_servername_callback = None |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 428 | self._app_data = None |
| 429 | |
Jean-Paul Calderone | 1aba416 | 2013-03-05 18:50:00 -0800 | [diff] [blame] | 430 | # SSL_CTX_set_app_data(self->ctx, self); |
| 431 | # SSL_CTX_set_mode(self->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 432 | # SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 433 | # SSL_MODE_AUTO_RETRY); |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 434 | self.set_mode(_lib.SSL_MODE_ENABLE_PARTIAL_WRITE) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 435 | |
| 436 | |
| 437 | def load_verify_locations(self, cafile, capath=None): |
| 438 | """ |
| 439 | Let SSL know where we can find trusted certificates for the certificate |
| 440 | chain |
| 441 | |
| 442 | :param cafile: In which file we can find the certificates |
| 443 | :param capath: In which directory we can find the certificates |
| 444 | :return: None |
| 445 | """ |
| 446 | if cafile is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 447 | cafile = _ffi.NULL |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 448 | elif not isinstance(cafile, bytes): |
| 449 | raise TypeError("cafile must be None or a byte string") |
| 450 | |
| 451 | if capath is None: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 452 | capath = _ffi.NULL |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 453 | elif not isinstance(capath, bytes): |
| 454 | raise TypeError("capath must be None or a byte string") |
| 455 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 456 | 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] | 457 | if not load_result: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 458 | _raise_current_error() |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 459 | |
| 460 | |
| 461 | def _wrap_callback(self, callback): |
| 462 | @wraps(callback) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 463 | def wrapper(size, verify, userdata): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 464 | return callback(size, verify, self._passphrase_userdata) |
| 465 | return _PassphraseHelper( |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 466 | FILETYPE_PEM, wrapper, more_args=True, truncate=True) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 467 | |
| 468 | |
| 469 | def set_passwd_cb(self, callback, userdata=None): |
| 470 | """ |
| 471 | Set the passphrase callback |
| 472 | |
| 473 | :param callback: The Python callback to use |
| 474 | :param userdata: (optional) A Python object which will be given as |
| 475 | argument to the callback |
| 476 | :return: None |
| 477 | """ |
| 478 | if not callable(callback): |
| 479 | raise TypeError("callback must be callable") |
| 480 | |
| 481 | self._passphrase_helper = self._wrap_callback(callback) |
| 482 | self._passphrase_callback = self._passphrase_helper.callback |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 483 | _lib.SSL_CTX_set_default_passwd_cb( |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 484 | self._context, self._passphrase_callback) |
| 485 | self._passphrase_userdata = userdata |
| 486 | |
| 487 | |
| 488 | def set_default_verify_paths(self): |
| 489 | """ |
| 490 | Use the platform-specific CA certificate locations |
| 491 | |
| 492 | :return: None |
| 493 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 494 | set_result = _lib.SSL_CTX_set_default_verify_paths(self._context) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 495 | if not set_result: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 496 | # TODO: This is untested. |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 497 | _raise_current_error() |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 498 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 499 | |
| 500 | def use_certificate_chain_file(self, certfile): |
| 501 | """ |
| 502 | Load a certificate chain from a file |
| 503 | |
| 504 | :param certfile: The name of the certificate chain file |
| 505 | :return: None |
| 506 | """ |
Jean-Paul Calderone | d860798 | 2014-01-18 10:30:55 -0500 | [diff] [blame] | 507 | if isinstance(certfile, _text_type): |
| 508 | # Perhaps sys.getfilesystemencoding() could be better? |
| 509 | certfile = certfile.encode("utf-8") |
| 510 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 511 | if not isinstance(certfile, bytes): |
Jean-Paul Calderone | d860798 | 2014-01-18 10:30:55 -0500 | [diff] [blame] | 512 | raise TypeError("certfile must be bytes or unicode") |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 513 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 514 | result = _lib.SSL_CTX_use_certificate_chain_file(self._context, certfile) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 515 | if not result: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 516 | _raise_current_error() |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 517 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 518 | |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 519 | def use_certificate_file(self, certfile, filetype=FILETYPE_PEM): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 520 | """ |
| 521 | Load a certificate from a file |
| 522 | |
| 523 | :param certfile: The name of the certificate file |
| 524 | :param filetype: (optional) The encoding of the file, default is PEM |
| 525 | :return: None |
| 526 | """ |
Jean-Paul Calderone | 684baf5 | 2014-01-18 10:31:19 -0500 | [diff] [blame] | 527 | if isinstance(certfile, _text_type): |
| 528 | # Perhaps sys.getfilesystemencoding() could be better? |
| 529 | certfile = certfile.encode("utf-8") |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 530 | if not isinstance(certfile, bytes): |
Jean-Paul Calderone | 684baf5 | 2014-01-18 10:31:19 -0500 | [diff] [blame] | 531 | raise TypeError("certfile must be bytes or unicode") |
Jean-Paul Calderone | f73a3cb | 2014-02-09 08:49:06 -0500 | [diff] [blame] | 532 | if not isinstance(filetype, integer_types): |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 533 | raise TypeError("filetype must be an integer") |
| 534 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 535 | 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] | 536 | if not use_result: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 537 | _raise_current_error() |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 538 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 539 | |
| 540 | def use_certificate(self, cert): |
| 541 | """ |
| 542 | Load a certificate from a X509 object |
| 543 | |
| 544 | :param cert: The X509 object |
| 545 | :return: None |
| 546 | """ |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 547 | if not isinstance(cert, X509): |
| 548 | raise TypeError("cert must be an X509 instance") |
| 549 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 550 | use_result = _lib.SSL_CTX_use_certificate(self._context, cert._x509) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 551 | if not use_result: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 552 | _raise_current_error() |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 553 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 554 | |
| 555 | def add_extra_chain_cert(self, certobj): |
| 556 | """ |
| 557 | Add certificate to chain |
| 558 | |
| 559 | :param certobj: The X509 certificate object to add to the chain |
| 560 | :return: None |
| 561 | """ |
| 562 | if not isinstance(certobj, X509): |
| 563 | raise TypeError("certobj must be an X509 instance") |
| 564 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 565 | copy = _lib.X509_dup(certobj._x509) |
| 566 | 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] | 567 | if not add_result: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 568 | # TODO: This is untested. |
| 569 | _lib.X509_free(copy) |
| 570 | _raise_current_error() |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 571 | |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 572 | |
| 573 | def _raise_passphrase_exception(self): |
| 574 | if self._passphrase_helper is None: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 575 | _raise_current_error() |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 576 | exception = self._passphrase_helper.raise_if_problem(Error) |
| 577 | if exception is not None: |
| 578 | raise exception |
| 579 | |
| 580 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 581 | def use_privatekey_file(self, keyfile, filetype=_unspecified): |
| 582 | """ |
| 583 | Load a private key from a file |
| 584 | |
| 585 | :param keyfile: The name of the key file |
| 586 | :param filetype: (optional) The encoding of the file, default is PEM |
| 587 | :return: None |
| 588 | """ |
Jean-Paul Calderone | 87e525a | 2014-01-18 10:31:51 -0500 | [diff] [blame] | 589 | if isinstance(keyfile, _text_type): |
| 590 | # Perhaps sys.getfilesystemencoding() could be better? |
| 591 | keyfile = keyfile.encode("utf-8") |
| 592 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 593 | if not isinstance(keyfile, bytes): |
| 594 | raise TypeError("keyfile must be a byte string") |
| 595 | |
| 596 | if filetype is _unspecified: |
| 597 | filetype = FILETYPE_PEM |
Jean-Paul Calderone | f73a3cb | 2014-02-09 08:49:06 -0500 | [diff] [blame] | 598 | elif not isinstance(filetype, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 599 | raise TypeError("filetype must be an integer") |
| 600 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 601 | use_result = _lib.SSL_CTX_use_PrivateKey_file( |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 602 | self._context, keyfile, filetype) |
| 603 | if not use_result: |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 604 | self._raise_passphrase_exception() |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 605 | |
| 606 | |
| 607 | def use_privatekey(self, pkey): |
| 608 | """ |
| 609 | Load a private key from a PKey object |
| 610 | |
| 611 | :param pkey: The PKey object |
| 612 | :return: None |
| 613 | """ |
| 614 | if not isinstance(pkey, PKey): |
| 615 | raise TypeError("pkey must be a PKey instance") |
| 616 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 617 | use_result = _lib.SSL_CTX_use_PrivateKey(self._context, pkey._pkey) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 618 | if not use_result: |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 619 | self._raise_passphrase_exception() |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 620 | |
| 621 | |
| 622 | def check_privatekey(self): |
| 623 | """ |
| 624 | Check that the private key and certificate match up |
| 625 | |
| 626 | :return: None (raises an exception if something's wrong) |
| 627 | """ |
| 628 | |
| 629 | def load_client_ca(self, cafile): |
| 630 | """ |
| 631 | Load the trusted certificates that will be sent to the client (basically |
| 632 | telling the client "These are the guys I trust"). Does not actually |
| 633 | imply any of the certificates are trusted; that must be configured |
| 634 | separately. |
| 635 | |
| 636 | :param cafile: The name of the certificates file |
| 637 | :return: None |
| 638 | """ |
| 639 | |
| 640 | def set_session_id(self, buf): |
| 641 | """ |
| 642 | Set the session identifier. This is needed if you want to do session |
| 643 | resumption. |
| 644 | |
| 645 | :param buf: A Python object that can be safely converted to a string |
| 646 | :returns: None |
| 647 | """ |
| 648 | |
| 649 | def set_session_cache_mode(self, mode): |
| 650 | """ |
| 651 | Enable/disable session caching and specify the mode used. |
| 652 | |
| 653 | :param mode: One or more of the SESS_CACHE_* flags (combine using |
| 654 | bitwise or) |
| 655 | :returns: The previously set caching mode. |
| 656 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 657 | if not isinstance(mode, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 658 | raise TypeError("mode must be an integer") |
| 659 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 660 | return _lib.SSL_CTX_set_session_cache_mode(self._context, mode) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 661 | |
| 662 | |
| 663 | def get_session_cache_mode(self): |
| 664 | """ |
| 665 | :returns: The currently used cache mode. |
| 666 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 667 | return _lib.SSL_CTX_get_session_cache_mode(self._context) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 668 | |
| 669 | |
| 670 | def set_verify(self, mode, callback): |
| 671 | """ |
| 672 | Set the verify mode and verify callback |
| 673 | |
| 674 | :param mode: The verify mode, this is either VERIFY_NONE or |
| 675 | VERIFY_PEER combined with possible other flags |
| 676 | :param callback: The Python callback to use |
| 677 | :return: None |
| 678 | |
| 679 | See SSL_CTX_set_verify(3SSL) for further details. |
| 680 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 681 | if not isinstance(mode, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 682 | raise TypeError("mode must be an integer") |
| 683 | |
| 684 | if not callable(callback): |
| 685 | raise TypeError("callback must be callable") |
| 686 | |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 687 | self._verify_helper = _VerifyHelper(self, callback) |
| 688 | self._verify_callback = self._verify_helper.callback |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 689 | _lib.SSL_CTX_set_verify(self._context, mode, self._verify_callback) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 690 | |
| 691 | |
| 692 | def set_verify_depth(self, depth): |
| 693 | """ |
| 694 | Set the verify depth |
| 695 | |
| 696 | :param depth: An integer specifying the verify depth |
| 697 | :return: None |
| 698 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 699 | if not isinstance(depth, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 700 | raise TypeError("depth must be an integer") |
| 701 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 702 | _lib.SSL_CTX_set_verify_depth(self._context, depth) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 703 | |
| 704 | |
| 705 | def get_verify_mode(self): |
| 706 | """ |
| 707 | Get the verify mode |
| 708 | |
| 709 | :return: The verify mode |
| 710 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 711 | return _lib.SSL_CTX_get_verify_mode(self._context) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 712 | |
| 713 | |
| 714 | def get_verify_depth(self): |
| 715 | """ |
| 716 | Get the verify depth |
| 717 | |
| 718 | :return: The verify depth |
| 719 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 720 | return _lib.SSL_CTX_get_verify_depth(self._context) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 721 | |
| 722 | |
| 723 | def load_tmp_dh(self, dhfile): |
| 724 | """ |
| 725 | Load parameters for Ephemeral Diffie-Hellman |
| 726 | |
| 727 | :param dhfile: The file to load EDH parameters from |
| 728 | :return: None |
| 729 | """ |
| 730 | if not isinstance(dhfile, bytes): |
| 731 | raise TypeError("dhfile must be a byte string") |
| 732 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 733 | bio = _lib.BIO_new_file(dhfile, b"r") |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 734 | if bio == _ffi.NULL: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 735 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 736 | bio = _ffi.gc(bio, _lib.BIO_free) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 737 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 738 | dh = _lib.PEM_read_bio_DHparams(bio, _ffi.NULL, _ffi.NULL, _ffi.NULL) |
| 739 | dh = _ffi.gc(dh, _lib.DH_free) |
| 740 | _lib.SSL_CTX_set_tmp_dh(self._context, dh) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 741 | |
| 742 | |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 743 | def set_tmp_ecdh_by_curve_name(self, curve_name): |
| 744 | """ |
| 745 | Configure this connection to people to use Elliptical Curve |
| 746 | Diffie-Hellman key exchanges. |
| 747 | |
Alex Gaynor | a683fc0 | 2014-01-17 12:45:56 -0600 | [diff] [blame] | 748 | :param curve_name: One of the named curve constants. |
Alex Gaynor | 7b8d57a | 2014-01-17 12:08:54 -0600 | [diff] [blame] | 749 | :return: None |
| 750 | """ |
| 751 | if _lib.Cryptography_HAS_EC: |
| 752 | ecdh = _lib.EC_KEY_new_by_curve_name(curve_name) |
| 753 | if ecdh == _ffi.NULL: |
| 754 | raise ValueError( |
| 755 | "OpenSSL could not load the requested elliptic curve" |
| 756 | ) |
| 757 | _lib.SSL_CTX_set_tmp_ecdh(self._context, ecdh) |
| 758 | _lib.EC_KEY_free(ecdh) |
| 759 | else: |
| 760 | raise ValueError("OpenSSL is compiled without ECDH support") |
| 761 | |
| 762 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 763 | def set_cipher_list(self, cipher_list): |
| 764 | """ |
| 765 | Change the cipher list |
| 766 | |
| 767 | :param cipher_list: A cipher list, see ciphers(1) |
| 768 | :return: None |
| 769 | """ |
Jean-Paul Calderone | 63eab69 | 2014-01-18 10:19:56 -0500 | [diff] [blame] | 770 | if isinstance(cipher_list, _text_type): |
| 771 | cipher_list = cipher_list.encode("ascii") |
| 772 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 773 | if not isinstance(cipher_list, bytes): |
Jean-Paul Calderone | 63eab69 | 2014-01-18 10:19:56 -0500 | [diff] [blame] | 774 | raise TypeError("cipher_list must be bytes or unicode") |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 775 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 776 | result = _lib.SSL_CTX_set_cipher_list(self._context, cipher_list) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 777 | if not result: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 778 | _raise_current_error() |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 779 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 780 | |
| 781 | def set_client_ca_list(self, certificate_authorities): |
| 782 | """ |
| 783 | Set the list of preferred client certificate signers for this server context. |
| 784 | |
| 785 | This list of certificate authorities will be sent to the client when the |
| 786 | server requests a client certificate. |
| 787 | |
| 788 | :param certificate_authorities: a sequence of X509Names. |
| 789 | :return: None |
| 790 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 791 | name_stack = _lib.sk_X509_NAME_new_null() |
| 792 | if name_stack == _ffi.NULL: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 793 | # TODO: This is untested. |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 794 | _raise_current_error() |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 795 | |
| 796 | try: |
| 797 | for ca_name in certificate_authorities: |
| 798 | if not isinstance(ca_name, X509Name): |
| 799 | raise TypeError( |
| 800 | "client CAs must be X509Name objects, not %s objects" % ( |
| 801 | type(ca_name).__name__,)) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 802 | copy = _lib.X509_NAME_dup(ca_name._name) |
| 803 | if copy == _ffi.NULL: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 804 | # TODO: This is untested. |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 805 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 806 | push_result = _lib.sk_X509_NAME_push(name_stack, copy) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 807 | if not push_result: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 808 | _lib.X509_NAME_free(copy) |
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 | except: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 811 | _lib.sk_X509_NAME_free(name_stack) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 812 | raise |
| 813 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 814 | _lib.SSL_CTX_set_client_CA_list(self._context, name_stack) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 815 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 816 | |
| 817 | def add_client_ca(self, certificate_authority): |
| 818 | """ |
| 819 | Add the CA certificate to the list of preferred signers for this context. |
| 820 | |
| 821 | The list of certificate authorities will be sent to the client when the |
| 822 | server requests a client certificate. |
| 823 | |
| 824 | :param certificate_authority: certificate authority's X509 certificate. |
| 825 | :return: None |
| 826 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 827 | if not isinstance(certificate_authority, X509): |
| 828 | raise TypeError("certificate_authority must be an X509 instance") |
| 829 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 830 | add_result = _lib.SSL_CTX_add_client_CA( |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 831 | self._context, certificate_authority._x509) |
| 832 | if not add_result: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 833 | # TODO: This is untested. |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 834 | _raise_current_error() |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 835 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 836 | |
| 837 | def set_timeout(self, timeout): |
| 838 | """ |
| 839 | Set session timeout |
| 840 | |
| 841 | :param timeout: The timeout in seconds |
| 842 | :return: The previous session timeout |
| 843 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 844 | if not isinstance(timeout, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 845 | raise TypeError("timeout must be an integer") |
| 846 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 847 | return _lib.SSL_CTX_set_timeout(self._context, timeout) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 848 | |
| 849 | |
| 850 | def get_timeout(self): |
| 851 | """ |
| 852 | Get the session timeout |
| 853 | |
| 854 | :return: The session timeout |
| 855 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 856 | return _lib.SSL_CTX_get_timeout(self._context) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 857 | |
| 858 | |
| 859 | def set_info_callback(self, callback): |
| 860 | """ |
| 861 | Set the info callback |
| 862 | |
| 863 | :param callback: The Python callback to use |
| 864 | :return: None |
| 865 | """ |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 866 | @wraps(callback) |
| 867 | def wrapper(ssl, where, return_code): |
Jean-Paul Calderone | f2bbc9c | 2014-02-02 10:59:14 -0500 | [diff] [blame] | 868 | callback(Connection._reverse_mapping[ssl], where, return_code) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 869 | self._info_callback = _ffi.callback( |
| 870 | "void (*)(const SSL *, int, int)", wrapper) |
| 871 | _lib.SSL_CTX_set_info_callback(self._context, self._info_callback) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 872 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 873 | |
| 874 | def get_app_data(self): |
| 875 | """ |
| 876 | Get the application data (supplied via set_app_data()) |
| 877 | |
| 878 | :return: The application data |
| 879 | """ |
| 880 | return self._app_data |
| 881 | |
| 882 | |
| 883 | def set_app_data(self, data): |
| 884 | """ |
| 885 | Set the application data (will be returned from get_app_data()) |
| 886 | |
| 887 | :param data: Any Python object |
| 888 | :return: None |
| 889 | """ |
| 890 | self._app_data = data |
| 891 | |
| 892 | |
| 893 | def get_cert_store(self): |
| 894 | """ |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 895 | Get the certificate store for the context. |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 896 | |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 897 | :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] | 898 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 899 | store = _lib.SSL_CTX_get_cert_store(self._context) |
| 900 | if store == _ffi.NULL: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 901 | # TODO: This is untested. |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 902 | return None |
| 903 | |
| 904 | pystore = X509Store.__new__(X509Store) |
| 905 | pystore._store = store |
| 906 | return pystore |
| 907 | |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 908 | |
| 909 | def set_options(self, options): |
| 910 | """ |
| 911 | Add options. Options set before are not cleared! |
| 912 | |
| 913 | :param options: The options to add. |
| 914 | :return: The new option bitmask. |
| 915 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 916 | if not isinstance(options, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 917 | raise TypeError("options must be an integer") |
| 918 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 919 | return _lib.SSL_CTX_set_options(self._context, options) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 920 | |
| 921 | |
| 922 | def set_mode(self, mode): |
| 923 | """ |
| 924 | Add modes via bitmask. Modes set before are not cleared! |
| 925 | |
| 926 | :param mode: The mode to add. |
| 927 | :return: The new mode bitmask. |
| 928 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 929 | if not isinstance(mode, integer_types): |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 930 | raise TypeError("mode must be an integer") |
| 931 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 932 | return _lib.SSL_CTX_set_mode(self._context, mode) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 933 | |
| 934 | |
| 935 | def set_tlsext_servername_callback(self, callback): |
| 936 | """ |
| 937 | Specify a callback function to be called when clients specify a server name. |
| 938 | |
| 939 | :param callback: The callback function. It will be invoked with one |
| 940 | argument, the Connection instance. |
| 941 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 942 | @wraps(callback) |
| 943 | def wrapper(ssl, alert, arg): |
| 944 | callback(Connection._reverse_mapping[ssl]) |
| 945 | return 0 |
| 946 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 947 | self._tlsext_servername_callback = _ffi.callback( |
| 948 | "int (*)(const SSL *, int *, void *)", wrapper) |
| 949 | _lib.SSL_CTX_set_tlsext_servername_callback( |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 950 | self._context, self._tlsext_servername_callback) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 951 | |
| 952 | ContextType = Context |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 953 | |
| 954 | |
| 955 | |
| 956 | class Connection(object): |
| 957 | """ |
| 958 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 959 | _reverse_mapping = WeakValueDictionary() |
| 960 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 961 | def __init__(self, context, socket=None): |
| 962 | """ |
| 963 | Create a new Connection object, using the given OpenSSL.SSL.Context |
| 964 | instance and socket. |
| 965 | |
| 966 | :param context: An SSL Context to use for this connection |
| 967 | :param socket: The socket to use for transport layer |
| 968 | """ |
| 969 | if not isinstance(context, Context): |
| 970 | raise TypeError("context must be a Context instance") |
| 971 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 972 | ssl = _lib.SSL_new(context._context) |
| 973 | self._ssl = _ffi.gc(ssl, _lib.SSL_free) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 974 | self._context = context |
| 975 | |
| 976 | self._reverse_mapping[self._ssl] = self |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 977 | |
| 978 | if socket is None: |
| 979 | self._socket = None |
Jean-Paul Calderone | 73b15c2 | 2013-03-05 18:30:39 -0800 | [diff] [blame] | 980 | # 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] | 981 | self._into_ssl = _lib.BIO_new(_lib.BIO_s_mem()) |
| 982 | self._from_ssl = _lib.BIO_new(_lib.BIO_s_mem()) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 983 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 984 | 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] | 985 | # TODO: This is untested. |
| 986 | _raise_current_error() |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 987 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 988 | _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] | 989 | else: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 990 | self._into_ssl = None |
| 991 | self._from_ssl = None |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 992 | self._socket = socket |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 993 | set_result = _lib.SSL_set_fd(self._ssl, _asFileDescriptor(self._socket)) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 994 | if not set_result: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 995 | # TODO: This is untested. |
| 996 | _raise_current_error() |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 997 | |
| 998 | |
| 999 | def __getattr__(self, name): |
| 1000 | """ |
| 1001 | Look up attributes on the wrapped socket object if they are not found on |
| 1002 | the Connection object. |
| 1003 | """ |
| 1004 | return getattr(self._socket, name) |
| 1005 | |
| 1006 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1007 | def _raise_ssl_error(self, ssl, result): |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 1008 | if self._context._verify_helper is not None: |
| 1009 | self._context._verify_helper.raise_if_problem() |
| 1010 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1011 | error = _lib.SSL_get_error(ssl, result) |
| 1012 | if error == _lib.SSL_ERROR_WANT_READ: |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1013 | raise WantReadError() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1014 | elif error == _lib.SSL_ERROR_WANT_WRITE: |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 1015 | raise WantWriteError() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1016 | elif error == _lib.SSL_ERROR_ZERO_RETURN: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1017 | raise ZeroReturnError() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1018 | elif error == _lib.SSL_ERROR_WANT_X509_LOOKUP: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1019 | # TODO: This is untested. |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 1020 | raise WantX509LookupError() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1021 | elif error == _lib.SSL_ERROR_SYSCALL: |
| 1022 | if _lib.ERR_peek_error() == 0: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1023 | if result < 0: |
Konstantinos Koukopoulos | 541150d | 2014-01-31 01:00:19 +0200 | [diff] [blame] | 1024 | if platform == "win32": |
| 1025 | errno = _ffi.getwinerror()[0] |
| 1026 | else: |
| 1027 | errno = _ffi.errno |
| 1028 | raise SysCallError(errno, errorcode[errno]) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1029 | else: |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 1030 | raise SysCallError(-1, "Unexpected EOF") |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1031 | else: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1032 | # TODO: This is untested. |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 1033 | _raise_current_error() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1034 | elif error == _lib.SSL_ERROR_NONE: |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 1035 | pass |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1036 | else: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 1037 | _raise_current_error() |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1038 | |
| 1039 | |
| 1040 | def get_context(self): |
| 1041 | """ |
| 1042 | Get session context |
| 1043 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1044 | return self._context |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1045 | |
| 1046 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1047 | def set_context(self, context): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1048 | """ |
| 1049 | Switch this connection to a new session context |
| 1050 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1051 | :param context: A :py:class:`Context` instance giving the new session |
| 1052 | context to use. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1053 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1054 | if not isinstance(context, Context): |
| 1055 | raise TypeError("context must be a Context instance") |
| 1056 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1057 | _lib.SSL_set_SSL_CTX(self._ssl, context._context) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1058 | self._context = context |
| 1059 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1060 | |
| 1061 | def get_servername(self): |
| 1062 | """ |
| 1063 | Retrieve the servername extension value if provided in the client hello |
| 1064 | message, or None if there wasn't one. |
| 1065 | |
| 1066 | :return: A byte string giving the server name or :py:data:`None`. |
| 1067 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1068 | name = _lib.SSL_get_servername(self._ssl, _lib.TLSEXT_NAMETYPE_host_name) |
| 1069 | if name == _ffi.NULL: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1070 | return None |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1071 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1072 | return _ffi.string(name) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1073 | |
| 1074 | |
| 1075 | def set_tlsext_host_name(self, name): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1076 | """ |
| 1077 | Set the value of the servername extension to send in the client hello. |
| 1078 | |
| 1079 | :param name: A byte string giving the name. |
| 1080 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1081 | if not isinstance(name, bytes): |
| 1082 | raise TypeError("name must be a byte string") |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1083 | elif b"\0" in name: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1084 | raise TypeError("name must not contain NUL byte") |
| 1085 | |
| 1086 | # XXX I guess this can fail sometimes? |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1087 | _lib.SSL_set_tlsext_host_name(self._ssl, name) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1088 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1089 | |
| 1090 | def pending(self): |
| 1091 | """ |
| 1092 | Get the number of bytes that can be safely read from the connection |
| 1093 | |
| 1094 | :return: The number of bytes available in the receive buffer. |
| 1095 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1096 | return _lib.SSL_pending(self._ssl) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1097 | |
| 1098 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1099 | def send(self, buf, flags=0): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1100 | """ |
| 1101 | Send data on the connection. NOTE: If you get one of the WantRead, |
| 1102 | WantWrite or WantX509Lookup exceptions on this, you have to call the |
| 1103 | method again with the SAME buffer. |
| 1104 | |
| 1105 | :param buf: The string to send |
| 1106 | :param flags: (optional) Included for compatibility with the socket |
| 1107 | API, the value is ignored |
| 1108 | :return: The number of bytes written |
| 1109 | """ |
Jean-Paul Calderone | 8fb5318 | 2013-12-30 08:35:49 -0500 | [diff] [blame] | 1110 | if isinstance(buf, _memoryview): |
Jean-Paul Calderone | 1aba416 | 2013-03-05 18:50:00 -0800 | [diff] [blame] | 1111 | buf = buf.tobytes() |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1112 | if not isinstance(buf, bytes): |
| 1113 | raise TypeError("data must be a byte string") |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1114 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1115 | result = _lib.SSL_write(self._ssl, buf, len(buf)) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1116 | self._raise_ssl_error(self._ssl, result) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1117 | return result |
| 1118 | write = send |
| 1119 | |
| 1120 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1121 | def sendall(self, buf, flags=0): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1122 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1123 | Send "all" data on the connection. This calls send() repeatedly until |
| 1124 | all data is sent. If an error occurs, it's impossible to tell how much |
| 1125 | data has been sent. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1126 | |
| 1127 | :param buf: The string to send |
| 1128 | :param flags: (optional) Included for compatibility with the socket |
| 1129 | API, the value is ignored |
| 1130 | :return: The number of bytes written |
| 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() |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1134 | if not isinstance(buf, bytes): |
| 1135 | raise TypeError("buf must be a byte string") |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1136 | |
| 1137 | left_to_send = len(buf) |
| 1138 | total_sent = 0 |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1139 | data = _ffi.new("char[]", buf) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1140 | |
| 1141 | while left_to_send: |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1142 | 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] | 1143 | self._raise_ssl_error(self._ssl, result) |
| 1144 | total_sent += result |
| 1145 | left_to_send -= result |
| 1146 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1147 | |
| 1148 | def recv(self, bufsiz, flags=None): |
| 1149 | """ |
| 1150 | Receive data on the connection. NOTE: If you get one of the WantRead, |
| 1151 | WantWrite or WantX509Lookup exceptions on this, you have to call the |
| 1152 | method again with the SAME buffer. |
| 1153 | |
| 1154 | :param bufsiz: The maximum number of bytes to read |
| 1155 | :param flags: (optional) Included for compatibility with the socket |
| 1156 | API, the value is ignored |
| 1157 | :return: The string read from the Connection |
| 1158 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1159 | buf = _ffi.new("char[]", bufsiz) |
| 1160 | result = _lib.SSL_read(self._ssl, buf, bufsiz) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1161 | self._raise_ssl_error(self._ssl, result) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1162 | return _ffi.buffer(buf, result)[:] |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1163 | read = recv |
| 1164 | |
| 1165 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1166 | def _handle_bio_errors(self, bio, result): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1167 | if _lib.BIO_should_retry(bio): |
| 1168 | if _lib.BIO_should_read(bio): |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1169 | raise WantReadError() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1170 | elif _lib.BIO_should_write(bio): |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1171 | # TODO: This is untested. |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 1172 | raise WantWriteError() |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1173 | elif _lib.BIO_should_io_special(bio): |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1174 | # TODO: This is untested. I think io_special means the socket |
| 1175 | # BIO has a not-yet connected socket. |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 1176 | raise ValueError("BIO_should_io_special") |
| 1177 | else: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1178 | # TODO: This is untested. |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 1179 | raise ValueError("unknown bio failure") |
| 1180 | else: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1181 | # TODO: This is untested. |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 1182 | _raise_current_error() |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1183 | |
| 1184 | |
| 1185 | def bio_read(self, bufsiz): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1186 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1187 | When using non-socket connections this function reads the "dirty" data |
| 1188 | that would have traveled away on the network. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1189 | |
| 1190 | :param bufsiz: The maximum number of bytes to read |
| 1191 | :return: The string read. |
| 1192 | """ |
Jean-Paul Calderone | 97e041d | 2013-03-05 21:03:12 -0800 | [diff] [blame] | 1193 | if self._from_ssl is None: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1194 | raise TypeError("Connection sock was not None") |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1195 | |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1196 | if not isinstance(bufsiz, integer_types): |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1197 | raise TypeError("bufsiz must be an integer") |
| 1198 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1199 | buf = _ffi.new("char[]", bufsiz) |
| 1200 | result = _lib.BIO_read(self._from_ssl, buf, bufsiz) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1201 | if result <= 0: |
| 1202 | self._handle_bio_errors(self._from_ssl, result) |
| 1203 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1204 | return _ffi.buffer(buf, result)[:] |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1205 | |
| 1206 | |
| 1207 | def bio_write(self, buf): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1208 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1209 | When using non-socket connections this function sends "dirty" data that |
| 1210 | would have traveled in on the network. |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1211 | |
| 1212 | :param buf: The string to put into the memory BIO. |
| 1213 | :return: The number of bytes written |
| 1214 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1215 | if self._into_ssl is None: |
| 1216 | raise TypeError("Connection sock was not None") |
| 1217 | |
| 1218 | if not isinstance(buf, bytes): |
| 1219 | raise TypeError("buf must be a byte string") |
| 1220 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1221 | result = _lib.BIO_write(self._into_ssl, buf, len(buf)) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1222 | if result <= 0: |
| 1223 | self._handle_bio_errors(self._into_ssl, result) |
| 1224 | return result |
| 1225 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1226 | |
| 1227 | def renegotiate(self): |
| 1228 | """ |
| 1229 | Renegotiate the session |
| 1230 | |
| 1231 | :return: True if the renegotiation can be started, false otherwise |
| 1232 | """ |
| 1233 | |
| 1234 | def do_handshake(self): |
| 1235 | """ |
| 1236 | Perform an SSL handshake (usually called after renegotiate() or one of |
| 1237 | set_*_state()). This can raise the same exceptions as send and recv. |
| 1238 | |
| 1239 | :return: None. |
| 1240 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1241 | result = _lib.SSL_do_handshake(self._ssl) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1242 | self._raise_ssl_error(self._ssl, result) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1243 | |
| 1244 | |
| 1245 | def renegotiate_pending(self): |
| 1246 | """ |
| 1247 | Check if there's a renegotiation in progress, it will return false once |
| 1248 | a renegotiation is finished. |
| 1249 | |
| 1250 | :return: Whether there's a renegotiation in progress |
| 1251 | """ |
| 1252 | |
| 1253 | def total_renegotiations(self): |
| 1254 | """ |
| 1255 | Find out the total number of renegotiations. |
| 1256 | |
| 1257 | :return: The number of renegotiations. |
| 1258 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1259 | return _lib.SSL_total_renegotiations(self._ssl) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1260 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1261 | |
| 1262 | def connect(self, addr): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1263 | """ |
| 1264 | Connect to remote host and set up client-side SSL |
| 1265 | |
| 1266 | :param addr: A remote address |
| 1267 | :return: What the socket's connect method returns |
| 1268 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1269 | _lib.SSL_set_connect_state(self._ssl) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1270 | return self._socket.connect(addr) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1271 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1272 | |
| 1273 | def connect_ex(self, addr): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1274 | """ |
| 1275 | Connect to remote host and set up client-side SSL. Note that if the socket's |
| 1276 | connect_ex method doesn't return 0, SSL won't be initialized. |
| 1277 | |
| 1278 | :param addr: A remove address |
| 1279 | :return: What the socket's connect_ex method returns |
| 1280 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1281 | connect_ex = self._socket.connect_ex |
| 1282 | self.set_connect_state() |
| 1283 | return connect_ex(addr) |
| 1284 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1285 | |
| 1286 | def accept(self): |
| 1287 | """ |
| 1288 | Accept incoming connection and set up SSL on it |
| 1289 | |
| 1290 | :return: A (conn,addr) pair where conn is a Connection and addr is an |
| 1291 | address |
| 1292 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1293 | client, addr = self._socket.accept() |
| 1294 | conn = Connection(self._context, client) |
| 1295 | conn.set_accept_state() |
| 1296 | return (conn, addr) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1297 | |
| 1298 | |
| 1299 | def bio_shutdown(self): |
| 1300 | """ |
| 1301 | When using non-socket connections this function signals end of |
| 1302 | data on the input for this connection. |
| 1303 | |
| 1304 | :return: None |
| 1305 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1306 | if self._from_ssl is None: |
| 1307 | raise TypeError("Connection sock was not None") |
| 1308 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1309 | _lib.BIO_set_mem_eof_return(self._into_ssl, 0) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1310 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1311 | |
| 1312 | def shutdown(self): |
| 1313 | """ |
| 1314 | Send closure alert |
| 1315 | |
| 1316 | :return: True if the shutdown completed successfully (i.e. both sides |
| 1317 | have sent closure alerts), false otherwise (i.e. you have to |
| 1318 | wait for a ZeroReturnError on a recv() method call |
| 1319 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1320 | result = _lib.SSL_shutdown(self._ssl) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1321 | if result < 0: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1322 | # TODO: This is untested. |
| 1323 | _raise_current_error() |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1324 | elif result > 0: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1325 | return True |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1326 | else: |
| 1327 | return False |
| 1328 | |
| 1329 | |
| 1330 | def get_cipher_list(self): |
| 1331 | """ |
| 1332 | Get the session cipher list |
| 1333 | |
| 1334 | :return: A list of cipher strings |
| 1335 | """ |
| 1336 | ciphers = [] |
| 1337 | for i in count(): |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1338 | result = _lib.SSL_get_cipher_list(self._ssl, i) |
| 1339 | if result == _ffi.NULL: |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1340 | break |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1341 | ciphers.append(_native(_ffi.string(result))) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1342 | return ciphers |
| 1343 | |
| 1344 | |
| 1345 | def get_client_ca_list(self): |
| 1346 | """ |
| 1347 | Get CAs whose certificates are suggested for client authentication. |
| 1348 | |
| 1349 | :return: If this is a server connection, a list of X509Names representing |
| 1350 | the acceptable CAs as set by :py:meth:`OpenSSL.SSL.Context.set_client_ca_list` or |
| 1351 | :py:meth:`OpenSSL.SSL.Context.add_client_ca`. If this is a client connection, |
| 1352 | the list of such X509Names sent by the server, or an empty list if that |
| 1353 | has not yet happened. |
| 1354 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1355 | ca_names = _lib.SSL_get_client_CA_list(self._ssl) |
| 1356 | if ca_names == _ffi.NULL: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1357 | # TODO: This is untested. |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1358 | return [] |
| 1359 | |
| 1360 | result = [] |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1361 | for i in range(_lib.sk_X509_NAME_num(ca_names)): |
| 1362 | name = _lib.sk_X509_NAME_value(ca_names, i) |
| 1363 | copy = _lib.X509_NAME_dup(name) |
| 1364 | if copy == _ffi.NULL: |
Jean-Paul Calderone | a9f84ad | 2013-12-29 17:06:11 -0500 | [diff] [blame] | 1365 | # TODO: This is untested. |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 1366 | _raise_current_error() |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1367 | |
| 1368 | pyname = X509Name.__new__(X509Name) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1369 | pyname._name = _ffi.gc(copy, _lib.X509_NAME_free) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1370 | result.append(pyname) |
| 1371 | return result |
| 1372 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1373 | |
| 1374 | def makefile(self): |
| 1375 | """ |
| 1376 | The makefile() method is not implemented, since there is no dup semantics |
| 1377 | for SSL connections |
| 1378 | |
| 1379 | :raise NotImplementedError |
| 1380 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1381 | raise NotImplementedError("Cannot make file object of OpenSSL.SSL.Connection") |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1382 | |
| 1383 | |
| 1384 | def get_app_data(self): |
| 1385 | """ |
| 1386 | Get application data |
| 1387 | |
| 1388 | :return: The application data |
| 1389 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1390 | return self._app_data |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1391 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1392 | |
| 1393 | def set_app_data(self, data): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1394 | """ |
| 1395 | Set application data |
| 1396 | |
| 1397 | :param data - The application data |
| 1398 | :return: None |
| 1399 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1400 | self._app_data = data |
| 1401 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1402 | |
| 1403 | def get_shutdown(self): |
| 1404 | """ |
| 1405 | Get shutdown state |
| 1406 | |
| 1407 | :return: The shutdown state, a bitvector of SENT_SHUTDOWN, RECEIVED_SHUTDOWN. |
| 1408 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1409 | return _lib.SSL_get_shutdown(self._ssl) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1410 | |
| 1411 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1412 | def set_shutdown(self, state): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1413 | """ |
| 1414 | Set shutdown state |
| 1415 | |
| 1416 | :param state - bitvector of SENT_SHUTDOWN, RECEIVED_SHUTDOWN. |
| 1417 | :return: None |
| 1418 | """ |
Jean-Paul Calderone | f73a3cb | 2014-02-09 08:49:06 -0500 | [diff] [blame] | 1419 | if not isinstance(state, integer_types): |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1420 | raise TypeError("state must be an integer") |
| 1421 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1422 | _lib.SSL_set_shutdown(self._ssl, state) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1423 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1424 | |
| 1425 | def state_string(self): |
| 1426 | """ |
| 1427 | Get a verbose state description |
| 1428 | |
| 1429 | :return: A string representing the state |
| 1430 | """ |
| 1431 | |
| 1432 | def server_random(self): |
| 1433 | """ |
| 1434 | Get a copy of the server hello nonce. |
| 1435 | |
| 1436 | :return: A string representing the state |
| 1437 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1438 | if self._ssl.session == _ffi.NULL: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1439 | return None |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1440 | return _ffi.buffer( |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1441 | self._ssl.s3.server_random, |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1442 | _lib.SSL3_RANDOM_SIZE)[:] |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1443 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1444 | |
| 1445 | def client_random(self): |
| 1446 | """ |
| 1447 | Get a copy of the client hello nonce. |
| 1448 | |
| 1449 | :return: A string representing the state |
| 1450 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1451 | if self._ssl.session == _ffi.NULL: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1452 | return None |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1453 | return _ffi.buffer( |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1454 | self._ssl.s3.client_random, |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1455 | _lib.SSL3_RANDOM_SIZE)[:] |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1456 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1457 | |
| 1458 | def master_key(self): |
| 1459 | """ |
| 1460 | Get a copy of the master key. |
| 1461 | |
| 1462 | :return: A string representing the state |
| 1463 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1464 | if self._ssl.session == _ffi.NULL: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1465 | return None |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1466 | return _ffi.buffer( |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1467 | self._ssl.session.master_key, |
| 1468 | self._ssl.session.master_key_length)[:] |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1469 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1470 | |
| 1471 | def sock_shutdown(self, *args, **kwargs): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1472 | """ |
| 1473 | See shutdown(2) |
| 1474 | |
| 1475 | :return: What the socket's shutdown() method returns |
| 1476 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1477 | return self._socket.shutdown(*args, **kwargs) |
| 1478 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1479 | |
| 1480 | def get_peer_certificate(self): |
| 1481 | """ |
| 1482 | Retrieve the other side's certificate (if any) |
| 1483 | |
| 1484 | :return: The peer's certificate |
| 1485 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1486 | cert = _lib.SSL_get_peer_certificate(self._ssl) |
| 1487 | if cert != _ffi.NULL: |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1488 | pycert = X509.__new__(X509) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1489 | pycert._x509 = _ffi.gc(cert, _lib.X509_free) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1490 | return pycert |
| 1491 | return None |
| 1492 | |
| 1493 | |
| 1494 | def get_peer_cert_chain(self): |
| 1495 | """ |
| 1496 | Retrieve the other side's certificate (if any) |
| 1497 | |
| 1498 | :return: A list of X509 instances giving the peer's certificate chain, |
| 1499 | or None if it does not have one. |
| 1500 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1501 | cert_stack = _lib.SSL_get_peer_cert_chain(self._ssl) |
| 1502 | if cert_stack == _ffi.NULL: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1503 | return None |
| 1504 | |
| 1505 | result = [] |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1506 | for i in range(_lib.sk_X509_num(cert_stack)): |
Jean-Paul Calderone | 73b15c2 | 2013-03-05 18:30:39 -0800 | [diff] [blame] | 1507 | # TODO could incref instead of dup here |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1508 | cert = _lib.X509_dup(_lib.sk_X509_value(cert_stack, i)) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1509 | pycert = X509.__new__(X509) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1510 | pycert._x509 = _ffi.gc(cert, _lib.X509_free) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1511 | result.append(pycert) |
| 1512 | return result |
| 1513 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1514 | |
| 1515 | def want_read(self): |
| 1516 | """ |
| 1517 | Checks if more data has to be read from the transport layer to complete an |
| 1518 | operation. |
| 1519 | |
| 1520 | :return: True iff more data has to be read |
| 1521 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1522 | return _lib.SSL_want_read(self._ssl) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1523 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1524 | |
| 1525 | def want_write(self): |
| 1526 | """ |
| 1527 | Checks if there is data to write to the transport layer to complete an |
| 1528 | operation. |
| 1529 | |
| 1530 | :return: True iff there is data to write |
| 1531 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1532 | return _lib.SSL_want_write(self._ssl) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1533 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1534 | |
| 1535 | def set_accept_state(self): |
| 1536 | """ |
| 1537 | Set the connection to work in server mode. The handshake will be handled |
| 1538 | automatically by read/write. |
| 1539 | |
| 1540 | :return: None |
| 1541 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1542 | _lib.SSL_set_accept_state(self._ssl) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1543 | |
| 1544 | |
| 1545 | def set_connect_state(self): |
| 1546 | """ |
| 1547 | Set the connection to work in client mode. The handshake will be handled |
| 1548 | automatically by read/write. |
| 1549 | |
| 1550 | :return: None |
| 1551 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1552 | _lib.SSL_set_connect_state(self._ssl) |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1553 | |
| 1554 | |
| 1555 | def get_session(self): |
| 1556 | """ |
| 1557 | Returns the Session currently used. |
| 1558 | |
| 1559 | @return: An instance of :py:class:`OpenSSL.SSL.Session` or :py:obj:`None` if |
| 1560 | no session exists. |
| 1561 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1562 | session = _lib.SSL_get1_session(self._ssl) |
| 1563 | if session == _ffi.NULL: |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1564 | return None |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1565 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1566 | pysession = Session.__new__(Session) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1567 | pysession._session = _ffi.gc(session, _lib.SSL_SESSION_free) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1568 | return pysession |
| 1569 | |
| 1570 | |
| 1571 | def set_session(self, session): |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1572 | """ |
| 1573 | Set the session to be used when the TLS/SSL connection is established. |
| 1574 | |
| 1575 | :param session: A Session instance representing the session to use. |
| 1576 | :returns: None |
| 1577 | """ |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1578 | if not isinstance(session, Session): |
| 1579 | raise TypeError("session must be a Session instance") |
| 1580 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 1581 | result = _lib.SSL_set_session(self._ssl, session._session) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1582 | if not result: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 1583 | _raise_current_error() |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1584 | |
| 1585 | ConnectionType = Connection |
Jean-Paul Calderone | 11ed8e8 | 2014-01-18 10:21:50 -0500 | [diff] [blame] | 1586 | |
Jean-Paul Calderone | fab157b | 2014-01-18 11:21:38 -0500 | [diff] [blame] | 1587 | # This is similar to the initialization calls at the end of OpenSSL/crypto.py |
| 1588 | # but is exercised mostly by the Context initializer. |
Jean-Paul Calderone | 11ed8e8 | 2014-01-18 10:21:50 -0500 | [diff] [blame] | 1589 | _lib.SSL_library_init() |