Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1 | /* SSL socket module |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2 | |
| 3 | SSL support based on patches by Brian E Gallew and Laszlo Kovacs. |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 4 | Re-worked a bit by Bill Janssen to add server-side support and |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 5 | certificate decoding. Chris Stawarz contributed some non-blocking |
| 6 | patches. |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 7 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 8 | This module is imported by ssl.py. It should *not* be used |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 9 | directly. |
| 10 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 11 | XXX should partial writes be enabled, SSL_MODE_ENABLE_PARTIAL_WRITE? |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include "Python.h" |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 15 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 16 | #ifdef WITH_THREAD |
| 17 | #include "pythread.h" |
| 18 | #define PySSL_BEGIN_ALLOW_THREADS { \ |
Neal Norwitz | e9057ff | 2008-01-27 17:10:35 +0000 | [diff] [blame] | 19 | PyThreadState *_save = NULL; \ |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 20 | if (_ssl_locks_count>0) {_save = PyEval_SaveThread();} |
| 21 | #define PySSL_BLOCK_THREADS if (_ssl_locks_count>0){PyEval_RestoreThread(_save)}; |
| 22 | #define PySSL_UNBLOCK_THREADS if (_ssl_locks_count>0){_save = PyEval_SaveThread()}; |
| 23 | #define PySSL_END_ALLOW_THREADS if (_ssl_locks_count>0){PyEval_RestoreThread(_save);} \ |
| 24 | } |
| 25 | |
| 26 | #else /* no WITH_THREAD */ |
| 27 | |
| 28 | #define PySSL_BEGIN_ALLOW_THREADS |
| 29 | #define PySSL_BLOCK_THREADS |
| 30 | #define PySSL_UNBLOCK_THREADS |
| 31 | #define PySSL_END_ALLOW_THREADS |
| 32 | |
| 33 | #endif |
| 34 | |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 35 | enum py_ssl_error { |
| 36 | /* these mirror ssl.h */ |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 37 | PY_SSL_ERROR_NONE, |
| 38 | PY_SSL_ERROR_SSL, |
| 39 | PY_SSL_ERROR_WANT_READ, |
| 40 | PY_SSL_ERROR_WANT_WRITE, |
| 41 | PY_SSL_ERROR_WANT_X509_LOOKUP, |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 42 | PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */ |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 43 | PY_SSL_ERROR_ZERO_RETURN, |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 44 | PY_SSL_ERROR_WANT_CONNECT, |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 45 | /* start of non ssl.h errorcodes */ |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 46 | PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */ |
| 47 | PY_SSL_ERROR_INVALID_ERROR_CODE |
| 48 | }; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 50 | enum py_ssl_server_or_client { |
| 51 | PY_SSL_CLIENT, |
| 52 | PY_SSL_SERVER |
| 53 | }; |
| 54 | |
| 55 | enum py_ssl_cert_requirements { |
| 56 | PY_SSL_CERT_NONE, |
| 57 | PY_SSL_CERT_OPTIONAL, |
| 58 | PY_SSL_CERT_REQUIRED |
| 59 | }; |
| 60 | |
| 61 | enum py_ssl_version { |
| 62 | PY_SSL_VERSION_SSL2, |
| 63 | PY_SSL_VERSION_SSL3, |
| 64 | PY_SSL_VERSION_SSL23, |
| 65 | PY_SSL_VERSION_TLS1, |
| 66 | }; |
| 67 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 68 | /* Include symbols from _socket module */ |
| 69 | #include "socketmodule.h" |
| 70 | |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 71 | #if defined(HAVE_POLL_H) |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 72 | #include <poll.h> |
| 73 | #elif defined(HAVE_SYS_POLL_H) |
| 74 | #include <sys/poll.h> |
| 75 | #endif |
| 76 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 77 | /* Include OpenSSL header files */ |
| 78 | #include "openssl/rsa.h" |
| 79 | #include "openssl/crypto.h" |
| 80 | #include "openssl/x509.h" |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 81 | #include "openssl/x509v3.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 82 | #include "openssl/pem.h" |
| 83 | #include "openssl/ssl.h" |
| 84 | #include "openssl/err.h" |
| 85 | #include "openssl/rand.h" |
| 86 | |
| 87 | /* SSL error object */ |
| 88 | static PyObject *PySSLErrorObject; |
| 89 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 90 | #ifdef WITH_THREAD |
| 91 | |
| 92 | /* serves as a flag to see whether we've initialized the SSL thread support. */ |
| 93 | /* 0 means no, greater than 0 means yes */ |
| 94 | |
| 95 | static unsigned int _ssl_locks_count = 0; |
| 96 | |
| 97 | #endif /* def WITH_THREAD */ |
| 98 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 99 | /* SSL socket object */ |
| 100 | |
| 101 | #define X509_NAME_MAXLEN 256 |
| 102 | |
| 103 | /* RAND_* APIs got added to OpenSSL in 0.9.5 */ |
| 104 | #if OPENSSL_VERSION_NUMBER >= 0x0090500fL |
| 105 | # define HAVE_OPENSSL_RAND 1 |
| 106 | #else |
| 107 | # undef HAVE_OPENSSL_RAND |
| 108 | #endif |
| 109 | |
| 110 | typedef struct { |
| 111 | PyObject_HEAD |
| 112 | PySocketSockObject *Socket; /* Socket on which we're layered */ |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 113 | SSL_CTX* ctx; |
| 114 | SSL* ssl; |
| 115 | X509* peer_cert; |
| 116 | char server[X509_NAME_MAXLEN]; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 117 | char issuer[X509_NAME_MAXLEN]; |
| 118 | |
| 119 | } PySSLObject; |
| 120 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 121 | static PyTypeObject PySSL_Type; |
| 122 | static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args); |
| 123 | static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 124 | static int check_socket_and_wait_for_timeout(PySocketSockObject *s, |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 125 | int writing); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 126 | static PyObject *PySSL_peercert(PySSLObject *self, PyObject *args); |
| 127 | static PyObject *PySSL_cipher(PySSLObject *self); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 128 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 129 | #define PySSLObject_Check(v) (Py_TYPE(v) == &PySSL_Type) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 130 | |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 131 | typedef enum { |
| 132 | SOCKET_IS_NONBLOCKING, |
| 133 | SOCKET_IS_BLOCKING, |
| 134 | SOCKET_HAS_TIMED_OUT, |
| 135 | SOCKET_HAS_BEEN_CLOSED, |
Neal Norwitz | 389cea8 | 2006-02-13 00:35:21 +0000 | [diff] [blame] | 136 | SOCKET_TOO_LARGE_FOR_SELECT, |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 137 | SOCKET_OPERATION_OK |
| 138 | } timeout_state; |
| 139 | |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 140 | /* Wrap error strings with filename and line # */ |
| 141 | #define STRINGIFY1(x) #x |
| 142 | #define STRINGIFY2(x) STRINGIFY1(x) |
| 143 | #define ERRSTR1(x,y,z) (x ":" y ": " z) |
| 144 | #define ERRSTR(x) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x) |
| 145 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 146 | /* XXX It might be helpful to augment the error message generated |
| 147 | below with the name of the SSL function that generated the error. |
| 148 | I expect it's obvious most of the time. |
| 149 | */ |
| 150 | |
| 151 | static PyObject * |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 152 | PySSL_SetError(PySSLObject *obj, int ret, char *filename, int lineno) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 153 | { |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 154 | PyObject *v; |
Neal Norwitz | 049da9e | 2007-08-25 16:41:36 +0000 | [diff] [blame] | 155 | char buf[2048]; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 156 | char *errstr; |
| 157 | int err; |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 158 | enum py_ssl_error p = PY_SSL_ERROR_NONE; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 159 | |
| 160 | assert(ret <= 0); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 161 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 162 | if (obj->ssl != NULL) { |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 163 | err = SSL_get_error(obj->ssl, ret); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 165 | switch (err) { |
| 166 | case SSL_ERROR_ZERO_RETURN: |
| 167 | errstr = "TLS/SSL connection has been closed"; |
| 168 | p = PY_SSL_ERROR_ZERO_RETURN; |
| 169 | break; |
| 170 | case SSL_ERROR_WANT_READ: |
| 171 | errstr = "The operation did not complete (read)"; |
| 172 | p = PY_SSL_ERROR_WANT_READ; |
| 173 | break; |
| 174 | case SSL_ERROR_WANT_WRITE: |
| 175 | p = PY_SSL_ERROR_WANT_WRITE; |
| 176 | errstr = "The operation did not complete (write)"; |
| 177 | break; |
| 178 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 179 | p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
| 180 | errstr = |
| 181 | "The operation did not complete (X509 lookup)"; |
| 182 | break; |
| 183 | case SSL_ERROR_WANT_CONNECT: |
| 184 | p = PY_SSL_ERROR_WANT_CONNECT; |
| 185 | errstr = "The operation did not complete (connect)"; |
| 186 | break; |
| 187 | case SSL_ERROR_SYSCALL: |
| 188 | { |
| 189 | unsigned long e = ERR_get_error(); |
| 190 | if (e == 0) { |
| 191 | if (ret == 0 || !obj->Socket) { |
| 192 | p = PY_SSL_ERROR_EOF; |
| 193 | errstr = |
| 194 | "EOF occurred in violation of protocol"; |
| 195 | } else if (ret == -1) { |
| 196 | /* underlying BIO reported an I/O error */ |
| 197 | return obj->Socket->errorhandler(); |
| 198 | } else { /* possible? */ |
| 199 | p = PY_SSL_ERROR_SYSCALL; |
| 200 | errstr = "Some I/O error occurred"; |
| 201 | } |
| 202 | } else { |
Jeremy Hylton | 4e54730 | 2002-07-02 18:25:00 +0000 | [diff] [blame] | 203 | p = PY_SSL_ERROR_SYSCALL; |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 204 | /* XXX Protected by global interpreter lock */ |
| 205 | errstr = ERR_error_string(e, NULL); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 206 | } |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 207 | break; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 208 | } |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 209 | case SSL_ERROR_SSL: |
| 210 | { |
| 211 | unsigned long e = ERR_get_error(); |
| 212 | p = PY_SSL_ERROR_SSL; |
| 213 | if (e != 0) |
| 214 | /* XXX Protected by global interpreter lock */ |
| 215 | errstr = ERR_error_string(e, NULL); |
| 216 | else { /* possible? */ |
| 217 | errstr = |
| 218 | "A failure in the SSL library occurred"; |
| 219 | } |
| 220 | break; |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 221 | } |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 222 | default: |
| 223 | p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
| 224 | errstr = "Invalid error code"; |
| 225 | } |
| 226 | } else { |
| 227 | errstr = ERR_error_string(ERR_peek_last_error(), NULL); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 228 | } |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 229 | PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr); |
| 230 | v = Py_BuildValue("(is)", p, buf); |
| 231 | if (v != NULL) { |
| 232 | PyErr_SetObject(PySSLErrorObject, v); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 233 | Py_DECREF(v); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 234 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 235 | return NULL; |
| 236 | } |
| 237 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 238 | static PyObject * |
| 239 | _setSSLError (char *errstr, int errcode, char *filename, int lineno) { |
| 240 | |
| 241 | char buf[2048]; |
| 242 | PyObject *v; |
| 243 | |
| 244 | if (errstr == NULL) { |
| 245 | errcode = ERR_peek_last_error(); |
| 246 | errstr = ERR_error_string(errcode, NULL); |
| 247 | } |
| 248 | PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr); |
| 249 | v = Py_BuildValue("(is)", errcode, buf); |
| 250 | if (v != NULL) { |
| 251 | PyErr_SetObject(PySSLErrorObject, v); |
| 252 | Py_DECREF(v); |
| 253 | } |
| 254 | return NULL; |
| 255 | } |
| 256 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 257 | static PySSLObject * |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 258 | newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file, |
| 259 | enum py_ssl_server_or_client socket_type, |
| 260 | enum py_ssl_cert_requirements certreq, |
| 261 | enum py_ssl_version proto_version, |
| 262 | char *cacerts_file) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 263 | { |
| 264 | PySSLObject *self; |
| 265 | char *errstr = NULL; |
| 266 | int ret; |
Neal Norwitz | 049da9e | 2007-08-25 16:41:36 +0000 | [diff] [blame] | 267 | int verification_mode; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 268 | |
| 269 | self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */ |
Neal Norwitz | 38e3b7d | 2006-05-11 07:51:59 +0000 | [diff] [blame] | 270 | if (self == NULL) |
Neal Norwitz | c6a989a | 2006-05-10 06:57:58 +0000 | [diff] [blame] | 271 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 272 | memset(self->server, '\0', sizeof(char) * X509_NAME_MAXLEN); |
| 273 | memset(self->issuer, '\0', sizeof(char) * X509_NAME_MAXLEN); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 274 | self->peer_cert = NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 275 | self->ssl = NULL; |
| 276 | self->ctx = NULL; |
| 277 | self->Socket = NULL; |
| 278 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 279 | /* Make sure the SSL error state is initialized */ |
| 280 | (void) ERR_get_state(); |
| 281 | ERR_clear_error(); |
| 282 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 283 | if ((key_file && !cert_file) || (!key_file && cert_file)) { |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 284 | errstr = ERRSTR("Both the key & certificate files " |
| 285 | "must be specified"); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 286 | goto fail; |
| 287 | } |
| 288 | |
| 289 | if ((socket_type == PY_SSL_SERVER) && |
| 290 | ((key_file == NULL) || (cert_file == NULL))) { |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 291 | errstr = ERRSTR("Both the key & certificate files " |
| 292 | "must be specified for server-side operation"); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 293 | goto fail; |
| 294 | } |
| 295 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 296 | PySSL_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 297 | if (proto_version == PY_SSL_VERSION_TLS1) |
| 298 | self->ctx = SSL_CTX_new(TLSv1_method()); /* Set up context */ |
| 299 | else if (proto_version == PY_SSL_VERSION_SSL3) |
| 300 | self->ctx = SSL_CTX_new(SSLv3_method()); /* Set up context */ |
| 301 | else if (proto_version == PY_SSL_VERSION_SSL2) |
| 302 | self->ctx = SSL_CTX_new(SSLv2_method()); /* Set up context */ |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 303 | else if (proto_version == PY_SSL_VERSION_SSL23) |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 304 | self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */ |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 305 | PySSL_END_ALLOW_THREADS |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 306 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 307 | if (self->ctx == NULL) { |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 308 | errstr = ERRSTR("Invalid SSL protocol variant specified."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 309 | goto fail; |
| 310 | } |
| 311 | |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 312 | if (certreq != PY_SSL_CERT_NONE) { |
| 313 | if (cacerts_file == NULL) { |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 314 | errstr = ERRSTR("No root certificates specified for " |
| 315 | "verification of other-side certificates."); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 316 | goto fail; |
| 317 | } else { |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 318 | PySSL_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 319 | ret = SSL_CTX_load_verify_locations(self->ctx, |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 320 | cacerts_file, |
| 321 | NULL); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 322 | PySSL_END_ALLOW_THREADS |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 323 | if (ret != 1) { |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 324 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 325 | goto fail; |
| 326 | } |
| 327 | } |
| 328 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 329 | if (key_file) { |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 330 | PySSL_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 331 | ret = SSL_CTX_use_PrivateKey_file(self->ctx, key_file, |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 332 | SSL_FILETYPE_PEM); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 333 | PySSL_END_ALLOW_THREADS |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 334 | if (ret != 1) { |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 335 | _setSSLError(NULL, ret, __FILE__, __LINE__); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 336 | goto fail; |
| 337 | } |
| 338 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 339 | PySSL_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 340 | ret = SSL_CTX_use_certificate_chain_file(self->ctx, |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 341 | cert_file); |
| 342 | PySSL_END_ALLOW_THREADS |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 343 | if (ret != 1) { |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 344 | /* |
| 345 | fprintf(stderr, "ret is %d, errcode is %lu, %lu, with file \"%s\"\n", |
| 346 | ret, ERR_peek_error(), ERR_peek_last_error(), cert_file); |
| 347 | */ |
| 348 | if (ERR_peek_last_error() != 0) { |
| 349 | _setSSLError(NULL, ret, __FILE__, __LINE__); |
| 350 | goto fail; |
| 351 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 355 | /* ssl compatibility */ |
| 356 | SSL_CTX_set_options(self->ctx, SSL_OP_ALL); |
| 357 | |
Neal Norwitz | 049da9e | 2007-08-25 16:41:36 +0000 | [diff] [blame] | 358 | verification_mode = SSL_VERIFY_NONE; |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 359 | if (certreq == PY_SSL_CERT_OPTIONAL) |
| 360 | verification_mode = SSL_VERIFY_PEER; |
| 361 | else if (certreq == PY_SSL_CERT_REQUIRED) |
| 362 | verification_mode = (SSL_VERIFY_PEER | |
| 363 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT); |
| 364 | SSL_CTX_set_verify(self->ctx, verification_mode, |
| 365 | NULL); /* set verify lvl */ |
| 366 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 367 | PySSL_BEGIN_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 368 | self->ssl = SSL_new(self->ctx); /* New ssl struct */ |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 369 | PySSL_END_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 370 | SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */ |
Antoine Pitrou | 206685b | 2010-04-09 20:44:09 +0000 | [diff] [blame] | 371 | #ifdef SSL_MODE_AUTO_RETRY |
Antoine Pitrou | cfd6200 | 2010-03-26 19:33:53 +0000 | [diff] [blame] | 372 | SSL_set_mode(self->ssl, SSL_MODE_AUTO_RETRY); |
Antoine Pitrou | 206685b | 2010-04-09 20:44:09 +0000 | [diff] [blame] | 373 | #endif |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 374 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 375 | /* If the socket is in non-blocking mode or timeout mode, set the BIO |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 376 | * to non-blocking mode (blocking is the default) |
| 377 | */ |
| 378 | if (Sock->sock_timeout >= 0.0) { |
| 379 | /* Set both the read and write BIO's to non-blocking mode */ |
| 380 | BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 381 | BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 382 | } |
| 383 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 384 | PySSL_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 385 | if (socket_type == PY_SSL_CLIENT) |
| 386 | SSL_set_connect_state(self->ssl); |
| 387 | else |
| 388 | SSL_set_accept_state(self->ssl); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 389 | PySSL_END_ALLOW_THREADS |
Martin v. Löwis | 09c35f7 | 2002-07-28 09:57:45 +0000 | [diff] [blame] | 390 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 391 | self->Socket = Sock; |
| 392 | Py_INCREF(self->Socket); |
| 393 | return self; |
| 394 | fail: |
| 395 | if (errstr) |
| 396 | PyErr_SetString(PySSLErrorObject, errstr); |
| 397 | Py_DECREF(self); |
| 398 | return NULL; |
| 399 | } |
| 400 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 401 | static PyObject * |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 402 | PySSL_sslwrap(PyObject *self, PyObject *args) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 403 | { |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 404 | PySocketSockObject *Sock; |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 405 | int server_side = 0; |
| 406 | int verification_mode = PY_SSL_CERT_NONE; |
| 407 | int protocol = PY_SSL_VERSION_SSL23; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 408 | char *key_file = NULL; |
| 409 | char *cert_file = NULL; |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 410 | char *cacerts_file = NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 411 | |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 412 | if (!PyArg_ParseTuple(args, "O!i|zziiz:sslwrap", |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 413 | PySocketModule.Sock_Type, |
Martin v. Löwis | a811c38 | 2006-10-19 11:00:37 +0000 | [diff] [blame] | 414 | &Sock, |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 415 | &server_side, |
| 416 | &key_file, &cert_file, |
| 417 | &verification_mode, &protocol, |
| 418 | &cacerts_file)) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 419 | return NULL; |
| 420 | |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 421 | /* |
| 422 | fprintf(stderr, |
| 423 | "server_side is %d, keyfile %p, certfile %p, verify_mode %d, " |
| 424 | "protocol %d, certs %p\n", |
| 425 | server_side, key_file, cert_file, verification_mode, |
| 426 | protocol, cacerts_file); |
| 427 | */ |
| 428 | |
| 429 | return (PyObject *) newPySSLObject(Sock, key_file, cert_file, |
| 430 | server_side, verification_mode, |
| 431 | protocol, cacerts_file); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 434 | PyDoc_STRVAR(ssl_doc, |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 435 | "sslwrap(socket, server_side, [keyfile, certfile, certs_mode, protocol,\n" |
| 436 | " cacertsfile]) -> sslobject"); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 437 | |
| 438 | /* SSL object methods */ |
| 439 | |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 440 | static PyObject *PySSL_SSLdo_handshake(PySSLObject *self) |
| 441 | { |
| 442 | int ret; |
| 443 | int err; |
| 444 | int sockstate; |
| 445 | |
| 446 | /* Actually negotiate SSL connection */ |
| 447 | /* XXX If SSL_do_handshake() returns 0, it's also a failure. */ |
| 448 | sockstate = 0; |
| 449 | do { |
| 450 | PySSL_BEGIN_ALLOW_THREADS |
| 451 | ret = SSL_do_handshake(self->ssl); |
| 452 | err = SSL_get_error(self->ssl, ret); |
| 453 | PySSL_END_ALLOW_THREADS |
| 454 | if(PyErr_CheckSignals()) { |
| 455 | return NULL; |
| 456 | } |
| 457 | if (err == SSL_ERROR_WANT_READ) { |
| 458 | sockstate = check_socket_and_wait_for_timeout(self->Socket, 0); |
| 459 | } else if (err == SSL_ERROR_WANT_WRITE) { |
| 460 | sockstate = check_socket_and_wait_for_timeout(self->Socket, 1); |
| 461 | } else { |
| 462 | sockstate = SOCKET_OPERATION_OK; |
| 463 | } |
| 464 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 465 | PyErr_SetString(PySSLErrorObject, |
| 466 | ERRSTR("The handshake operation timed out")); |
| 467 | return NULL; |
| 468 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 469 | PyErr_SetString(PySSLErrorObject, |
| 470 | ERRSTR("Underlying socket has been closed.")); |
| 471 | return NULL; |
| 472 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 473 | PyErr_SetString(PySSLErrorObject, |
| 474 | ERRSTR("Underlying socket too large for select().")); |
| 475 | return NULL; |
| 476 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 477 | break; |
| 478 | } |
| 479 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
| 480 | if (ret < 1) |
| 481 | return PySSL_SetError(self, ret, __FILE__, __LINE__); |
| 482 | self->ssl->debug = 1; |
| 483 | |
| 484 | if (self->peer_cert) |
| 485 | X509_free (self->peer_cert); |
| 486 | PySSL_BEGIN_ALLOW_THREADS |
| 487 | if ((self->peer_cert = SSL_get_peer_certificate(self->ssl))) { |
| 488 | X509_NAME_oneline(X509_get_subject_name(self->peer_cert), |
| 489 | self->server, X509_NAME_MAXLEN); |
| 490 | X509_NAME_oneline(X509_get_issuer_name(self->peer_cert), |
| 491 | self->issuer, X509_NAME_MAXLEN); |
| 492 | } |
| 493 | PySSL_END_ALLOW_THREADS |
| 494 | |
| 495 | Py_INCREF(Py_None); |
| 496 | return Py_None; |
| 497 | } |
| 498 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 499 | static PyObject * |
| 500 | PySSL_server(PySSLObject *self) |
| 501 | { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 502 | return PyString_FromString(self->server); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | static PyObject * |
| 506 | PySSL_issuer(PySSLObject *self) |
| 507 | { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 508 | return PyString_FromString(self->issuer); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 511 | static PyObject * |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 512 | _create_tuple_for_attribute (ASN1_OBJECT *name, ASN1_STRING *value) { |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 513 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 514 | char namebuf[X509_NAME_MAXLEN]; |
| 515 | int buflen; |
| 516 | PyObject *name_obj; |
| 517 | PyObject *value_obj; |
| 518 | PyObject *attr; |
| 519 | unsigned char *valuebuf = NULL; |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 520 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 521 | buflen = OBJ_obj2txt(namebuf, sizeof(namebuf), name, 0); |
| 522 | if (buflen < 0) { |
| 523 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 524 | goto fail; |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 525 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 526 | name_obj = PyString_FromStringAndSize(namebuf, buflen); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 527 | if (name_obj == NULL) |
| 528 | goto fail; |
| 529 | |
| 530 | buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 531 | if (buflen < 0) { |
| 532 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 533 | Py_DECREF(name_obj); |
| 534 | goto fail; |
| 535 | } |
| 536 | value_obj = PyUnicode_DecodeUTF8((char *) valuebuf, |
| 537 | buflen, "strict"); |
| 538 | OPENSSL_free(valuebuf); |
| 539 | if (value_obj == NULL) { |
| 540 | Py_DECREF(name_obj); |
| 541 | goto fail; |
| 542 | } |
| 543 | attr = PyTuple_New(2); |
| 544 | if (attr == NULL) { |
| 545 | Py_DECREF(name_obj); |
| 546 | Py_DECREF(value_obj); |
| 547 | goto fail; |
| 548 | } |
| 549 | PyTuple_SET_ITEM(attr, 0, name_obj); |
| 550 | PyTuple_SET_ITEM(attr, 1, value_obj); |
| 551 | return attr; |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 552 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 553 | fail: |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 554 | return NULL; |
| 555 | } |
| 556 | |
| 557 | static PyObject * |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 558 | _create_tuple_for_X509_NAME (X509_NAME *xname) |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 559 | { |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 560 | PyObject *dn = NULL; /* tuple which represents the "distinguished name" */ |
| 561 | PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */ |
| 562 | PyObject *rdnt; |
| 563 | PyObject *attr = NULL; /* tuple to hold an attribute */ |
| 564 | int entry_count = X509_NAME_entry_count(xname); |
| 565 | X509_NAME_ENTRY *entry; |
| 566 | ASN1_OBJECT *name; |
| 567 | ASN1_STRING *value; |
| 568 | int index_counter; |
| 569 | int rdn_level = -1; |
| 570 | int retcode; |
| 571 | |
| 572 | dn = PyList_New(0); |
| 573 | if (dn == NULL) |
| 574 | return NULL; |
| 575 | /* now create another tuple to hold the top-level RDN */ |
| 576 | rdn = PyList_New(0); |
| 577 | if (rdn == NULL) |
| 578 | goto fail0; |
| 579 | |
| 580 | for (index_counter = 0; |
| 581 | index_counter < entry_count; |
| 582 | index_counter++) |
| 583 | { |
| 584 | entry = X509_NAME_get_entry(xname, index_counter); |
| 585 | |
| 586 | /* check to see if we've gotten to a new RDN */ |
| 587 | if (rdn_level >= 0) { |
| 588 | if (rdn_level != entry->set) { |
| 589 | /* yes, new RDN */ |
| 590 | /* add old RDN to DN */ |
| 591 | rdnt = PyList_AsTuple(rdn); |
| 592 | Py_DECREF(rdn); |
| 593 | if (rdnt == NULL) |
| 594 | goto fail0; |
| 595 | retcode = PyList_Append(dn, rdnt); |
| 596 | Py_DECREF(rdnt); |
| 597 | if (retcode < 0) |
| 598 | goto fail0; |
| 599 | /* create new RDN */ |
| 600 | rdn = PyList_New(0); |
| 601 | if (rdn == NULL) |
| 602 | goto fail0; |
| 603 | } |
| 604 | } |
| 605 | rdn_level = entry->set; |
| 606 | |
| 607 | /* now add this attribute to the current RDN */ |
| 608 | name = X509_NAME_ENTRY_get_object(entry); |
| 609 | value = X509_NAME_ENTRY_get_data(entry); |
| 610 | attr = _create_tuple_for_attribute(name, value); |
| 611 | /* |
| 612 | fprintf(stderr, "RDN level %d, attribute %s: %s\n", |
| 613 | entry->set, |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 614 | PyString_AS_STRING(PyTuple_GET_ITEM(attr, 0)), |
| 615 | PyString_AS_STRING(PyTuple_GET_ITEM(attr, 1))); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 616 | */ |
| 617 | if (attr == NULL) |
| 618 | goto fail1; |
| 619 | retcode = PyList_Append(rdn, attr); |
| 620 | Py_DECREF(attr); |
| 621 | if (retcode < 0) |
| 622 | goto fail1; |
| 623 | } |
| 624 | /* now, there's typically a dangling RDN */ |
| 625 | if ((rdn != NULL) && (PyList_Size(rdn) > 0)) { |
| 626 | rdnt = PyList_AsTuple(rdn); |
| 627 | Py_DECREF(rdn); |
| 628 | if (rdnt == NULL) |
| 629 | goto fail0; |
| 630 | retcode = PyList_Append(dn, rdnt); |
| 631 | Py_DECREF(rdnt); |
| 632 | if (retcode < 0) |
| 633 | goto fail0; |
| 634 | } |
| 635 | |
| 636 | /* convert list to tuple */ |
| 637 | rdnt = PyList_AsTuple(dn); |
| 638 | Py_DECREF(dn); |
| 639 | if (rdnt == NULL) |
| 640 | return NULL; |
| 641 | return rdnt; |
| 642 | |
| 643 | fail1: |
| 644 | Py_XDECREF(rdn); |
| 645 | |
| 646 | fail0: |
| 647 | Py_XDECREF(dn); |
| 648 | return NULL; |
| 649 | } |
| 650 | |
| 651 | static PyObject * |
| 652 | _get_peer_alt_names (X509 *certificate) { |
| 653 | |
| 654 | /* this code follows the procedure outlined in |
| 655 | OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print() |
| 656 | function to extract the STACK_OF(GENERAL_NAME), |
| 657 | then iterates through the stack to add the |
| 658 | names. */ |
| 659 | |
| 660 | int i, j; |
| 661 | PyObject *peer_alt_names = Py_None; |
| 662 | PyObject *v, *t; |
| 663 | X509_EXTENSION *ext = NULL; |
| 664 | GENERAL_NAMES *names = NULL; |
| 665 | GENERAL_NAME *name; |
| 666 | X509V3_EXT_METHOD *method; |
| 667 | BIO *biobuf = NULL; |
| 668 | char buf[2048]; |
| 669 | char *vptr; |
| 670 | int len; |
Christian Heimes | a63f268 | 2007-12-14 04:38:13 +0000 | [diff] [blame] | 671 | const unsigned char *p; |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 672 | |
| 673 | if (certificate == NULL) |
| 674 | return peer_alt_names; |
| 675 | |
| 676 | /* get a memory buffer */ |
| 677 | biobuf = BIO_new(BIO_s_mem()); |
| 678 | |
| 679 | i = 0; |
| 680 | while ((i = X509_get_ext_by_NID( |
| 681 | certificate, NID_subject_alt_name, i)) >= 0) { |
| 682 | |
| 683 | if (peer_alt_names == Py_None) { |
| 684 | peer_alt_names = PyList_New(0); |
| 685 | if (peer_alt_names == NULL) |
| 686 | goto fail; |
| 687 | } |
| 688 | |
| 689 | /* now decode the altName */ |
| 690 | ext = X509_get_ext(certificate, i); |
| 691 | if(!(method = X509V3_EXT_get(ext))) { |
| 692 | PyErr_SetString(PySSLErrorObject, |
| 693 | ERRSTR("No method for internalizing subjectAltName!")); |
| 694 | goto fail; |
| 695 | } |
| 696 | |
| 697 | p = ext->value->data; |
Neal Norwitz | e9057ff | 2008-01-27 17:10:35 +0000 | [diff] [blame] | 698 | if (method->it) |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 699 | names = (GENERAL_NAMES*) (ASN1_item_d2i(NULL, |
| 700 | &p, |
| 701 | ext->value->length, |
| 702 | ASN1_ITEM_ptr(method->it))); |
| 703 | else |
| 704 | names = (GENERAL_NAMES*) (method->d2i(NULL, |
| 705 | &p, |
| 706 | ext->value->length)); |
| 707 | |
| 708 | for(j = 0; j < sk_GENERAL_NAME_num(names); j++) { |
| 709 | |
| 710 | /* get a rendering of each name in the set of names */ |
| 711 | |
| 712 | name = sk_GENERAL_NAME_value(names, j); |
| 713 | if (name->type == GEN_DIRNAME) { |
| 714 | |
| 715 | /* we special-case DirName as a tuple of tuples of attributes */ |
| 716 | |
| 717 | t = PyTuple_New(2); |
| 718 | if (t == NULL) { |
| 719 | goto fail; |
| 720 | } |
| 721 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 722 | v = PyString_FromString("DirName"); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 723 | if (v == NULL) { |
| 724 | Py_DECREF(t); |
| 725 | goto fail; |
| 726 | } |
| 727 | PyTuple_SET_ITEM(t, 0, v); |
| 728 | |
| 729 | v = _create_tuple_for_X509_NAME (name->d.dirn); |
| 730 | if (v == NULL) { |
| 731 | Py_DECREF(t); |
| 732 | goto fail; |
| 733 | } |
| 734 | PyTuple_SET_ITEM(t, 1, v); |
| 735 | |
| 736 | } else { |
| 737 | |
| 738 | /* for everything else, we use the OpenSSL print form */ |
| 739 | |
| 740 | (void) BIO_reset(biobuf); |
| 741 | GENERAL_NAME_print(biobuf, name); |
| 742 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 743 | if (len < 0) { |
| 744 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 745 | goto fail; |
| 746 | } |
| 747 | vptr = strchr(buf, ':'); |
| 748 | if (vptr == NULL) |
| 749 | goto fail; |
| 750 | t = PyTuple_New(2); |
| 751 | if (t == NULL) |
| 752 | goto fail; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 753 | v = PyString_FromStringAndSize(buf, (vptr - buf)); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 754 | if (v == NULL) { |
| 755 | Py_DECREF(t); |
| 756 | goto fail; |
| 757 | } |
| 758 | PyTuple_SET_ITEM(t, 0, v); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 759 | v = PyString_FromStringAndSize((vptr + 1), (len - (vptr - buf + 1))); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 760 | if (v == NULL) { |
| 761 | Py_DECREF(t); |
| 762 | goto fail; |
| 763 | } |
| 764 | PyTuple_SET_ITEM(t, 1, v); |
| 765 | } |
| 766 | |
| 767 | /* and add that rendering to the list */ |
| 768 | |
| 769 | if (PyList_Append(peer_alt_names, t) < 0) { |
| 770 | Py_DECREF(t); |
| 771 | goto fail; |
| 772 | } |
| 773 | Py_DECREF(t); |
| 774 | } |
| 775 | } |
| 776 | BIO_free(biobuf); |
| 777 | if (peer_alt_names != Py_None) { |
| 778 | v = PyList_AsTuple(peer_alt_names); |
| 779 | Py_DECREF(peer_alt_names); |
| 780 | return v; |
| 781 | } else { |
| 782 | return peer_alt_names; |
| 783 | } |
| 784 | |
| 785 | |
| 786 | fail: |
| 787 | if (biobuf != NULL) |
| 788 | BIO_free(biobuf); |
| 789 | |
| 790 | if (peer_alt_names != Py_None) { |
| 791 | Py_XDECREF(peer_alt_names); |
| 792 | } |
| 793 | |
| 794 | return NULL; |
| 795 | } |
| 796 | |
| 797 | static PyObject * |
| 798 | _decode_certificate (X509 *certificate, int verbose) { |
| 799 | |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 800 | PyObject *retval = NULL; |
| 801 | BIO *biobuf = NULL; |
Neal Norwitz | 049da9e | 2007-08-25 16:41:36 +0000 | [diff] [blame] | 802 | PyObject *peer; |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 803 | PyObject *peer_alt_names = NULL; |
Neal Norwitz | 049da9e | 2007-08-25 16:41:36 +0000 | [diff] [blame] | 804 | PyObject *issuer; |
| 805 | PyObject *version; |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 806 | PyObject *sn_obj; |
| 807 | ASN1_INTEGER *serialNumber; |
Neal Norwitz | 049da9e | 2007-08-25 16:41:36 +0000 | [diff] [blame] | 808 | char buf[2048]; |
| 809 | int len; |
| 810 | ASN1_TIME *notBefore, *notAfter; |
| 811 | PyObject *pnotBefore, *pnotAfter; |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 812 | |
| 813 | retval = PyDict_New(); |
| 814 | if (retval == NULL) |
| 815 | return NULL; |
| 816 | |
Bill Janssen | ffe576d | 2007-09-05 00:46:27 +0000 | [diff] [blame] | 817 | peer = _create_tuple_for_X509_NAME( |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 818 | X509_get_subject_name(certificate)); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 819 | if (peer == NULL) |
| 820 | goto fail0; |
| 821 | if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 822 | Py_DECREF(peer); |
| 823 | goto fail0; |
| 824 | } |
| 825 | Py_DECREF(peer); |
| 826 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 827 | if (verbose) { |
| 828 | issuer = _create_tuple_for_X509_NAME( |
| 829 | X509_get_issuer_name(certificate)); |
| 830 | if (issuer == NULL) |
| 831 | goto fail0; |
| 832 | if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
| 833 | Py_DECREF(issuer); |
| 834 | goto fail0; |
| 835 | } |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 836 | Py_DECREF(issuer); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 837 | |
| 838 | version = PyInt_FromLong(X509_get_version(certificate) + 1); |
| 839 | if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 840 | Py_DECREF(version); |
| 841 | goto fail0; |
| 842 | } |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 843 | Py_DECREF(version); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 844 | } |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 845 | |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 846 | /* get a memory buffer */ |
| 847 | biobuf = BIO_new(BIO_s_mem()); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 848 | |
| 849 | if (verbose) { |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 850 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 851 | (void) BIO_reset(biobuf); |
| 852 | serialNumber = X509_get_serialNumber(certificate); |
| 853 | /* should not exceed 20 octets, 160 bits, so buf is big enough */ |
| 854 | i2a_ASN1_INTEGER(biobuf, serialNumber); |
| 855 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 856 | if (len < 0) { |
| 857 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 858 | goto fail1; |
| 859 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 860 | sn_obj = PyString_FromStringAndSize(buf, len); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 861 | if (sn_obj == NULL) |
| 862 | goto fail1; |
| 863 | if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) { |
| 864 | Py_DECREF(sn_obj); |
| 865 | goto fail1; |
| 866 | } |
| 867 | Py_DECREF(sn_obj); |
| 868 | |
| 869 | (void) BIO_reset(biobuf); |
| 870 | notBefore = X509_get_notBefore(certificate); |
| 871 | ASN1_TIME_print(biobuf, notBefore); |
| 872 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 873 | if (len < 0) { |
| 874 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 875 | goto fail1; |
| 876 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 877 | pnotBefore = PyString_FromStringAndSize(buf, len); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 878 | if (pnotBefore == NULL) |
| 879 | goto fail1; |
| 880 | if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 881 | Py_DECREF(pnotBefore); |
| 882 | goto fail1; |
| 883 | } |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 884 | Py_DECREF(pnotBefore); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 885 | } |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 886 | |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 887 | (void) BIO_reset(biobuf); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 888 | notAfter = X509_get_notAfter(certificate); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 889 | ASN1_TIME_print(biobuf, notAfter); |
| 890 | len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 891 | if (len < 0) { |
| 892 | _setSSLError(NULL, 0, __FILE__, __LINE__); |
| 893 | goto fail1; |
| 894 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 895 | pnotAfter = PyString_FromStringAndSize(buf, len); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 896 | if (pnotAfter == NULL) |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 897 | goto fail1; |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 898 | if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 899 | Py_DECREF(pnotAfter); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 900 | goto fail1; |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 901 | } |
| 902 | Py_DECREF(pnotAfter); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 903 | |
| 904 | /* Now look for subjectAltName */ |
| 905 | |
| 906 | peer_alt_names = _get_peer_alt_names(certificate); |
| 907 | if (peer_alt_names == NULL) |
| 908 | goto fail1; |
| 909 | else if (peer_alt_names != Py_None) { |
| 910 | if (PyDict_SetItemString(retval, "subjectAltName", |
| 911 | peer_alt_names) < 0) { |
| 912 | Py_DECREF(peer_alt_names); |
| 913 | goto fail1; |
| 914 | } |
| 915 | Py_DECREF(peer_alt_names); |
| 916 | } |
| 917 | |
| 918 | BIO_free(biobuf); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 919 | return retval; |
| 920 | |
| 921 | fail1: |
| 922 | if (biobuf != NULL) |
| 923 | BIO_free(biobuf); |
| 924 | fail0: |
| 925 | Py_XDECREF(retval); |
| 926 | return NULL; |
| 927 | } |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 928 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 929 | |
| 930 | static PyObject * |
| 931 | PySSL_test_decode_certificate (PyObject *mod, PyObject *args) { |
| 932 | |
| 933 | PyObject *retval = NULL; |
| 934 | char *filename = NULL; |
| 935 | X509 *x=NULL; |
| 936 | BIO *cert; |
| 937 | int verbose = 1; |
| 938 | |
| 939 | if (!PyArg_ParseTuple(args, "s|i:test_decode_certificate", &filename, &verbose)) |
| 940 | return NULL; |
| 941 | |
| 942 | if ((cert=BIO_new(BIO_s_file())) == NULL) { |
| 943 | PyErr_SetString(PySSLErrorObject, "Can't malloc memory to read file"); |
| 944 | goto fail0; |
| 945 | } |
| 946 | |
| 947 | if (BIO_read_filename(cert,filename) <= 0) { |
| 948 | PyErr_SetString(PySSLErrorObject, "Can't open file"); |
| 949 | goto fail0; |
| 950 | } |
| 951 | |
| 952 | x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL); |
| 953 | if (x == NULL) { |
| 954 | PyErr_SetString(PySSLErrorObject, "Error decoding PEM-encoded file"); |
| 955 | goto fail0; |
| 956 | } |
| 957 | |
| 958 | retval = _decode_certificate(x, verbose); |
| 959 | |
| 960 | fail0: |
| 961 | |
| 962 | if (cert != NULL) BIO_free(cert); |
| 963 | return retval; |
| 964 | } |
| 965 | |
| 966 | |
| 967 | static PyObject * |
| 968 | PySSL_peercert(PySSLObject *self, PyObject *args) |
| 969 | { |
| 970 | PyObject *retval = NULL; |
| 971 | int len; |
| 972 | int verification; |
| 973 | PyObject *binary_mode = Py_None; |
| 974 | |
| 975 | if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode)) |
| 976 | return NULL; |
| 977 | |
| 978 | if (!self->peer_cert) |
| 979 | Py_RETURN_NONE; |
| 980 | |
| 981 | if (PyObject_IsTrue(binary_mode)) { |
| 982 | /* return cert in DER-encoded format */ |
| 983 | |
| 984 | unsigned char *bytes_buf = NULL; |
| 985 | |
| 986 | bytes_buf = NULL; |
| 987 | len = i2d_X509(self->peer_cert, &bytes_buf); |
| 988 | if (len < 0) { |
| 989 | PySSL_SetError(self, len, __FILE__, __LINE__); |
| 990 | return NULL; |
| 991 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 992 | retval = PyString_FromStringAndSize((const char *) bytes_buf, len); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 993 | OPENSSL_free(bytes_buf); |
| 994 | return retval; |
| 995 | |
| 996 | } else { |
| 997 | |
| 998 | verification = SSL_CTX_get_verify_mode(self->ctx); |
| 999 | if ((verification & SSL_VERIFY_PEER) == 0) |
| 1000 | return PyDict_New(); |
| 1001 | else |
| 1002 | return _decode_certificate (self->peer_cert, 0); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | PyDoc_STRVAR(PySSL_peercert_doc, |
| 1007 | "peer_certificate([der=False]) -> certificate\n\ |
| 1008 | \n\ |
| 1009 | Returns the certificate for the peer. If no certificate was provided,\n\ |
| 1010 | returns None. If a certificate was provided, but not validated, returns\n\ |
| 1011 | an empty dictionary. Otherwise returns a dict containing information\n\ |
| 1012 | about the peer certificate.\n\ |
| 1013 | \n\ |
| 1014 | If the optional argument is True, returns a DER-encoded copy of the\n\ |
| 1015 | peer certificate, or None if no certificate was provided. This will\n\ |
| 1016 | return the certificate even if it wasn't validated."); |
| 1017 | |
| 1018 | static PyObject *PySSL_cipher (PySSLObject *self) { |
| 1019 | |
| 1020 | PyObject *retval, *v; |
| 1021 | SSL_CIPHER *current; |
| 1022 | char *cipher_name; |
| 1023 | char *cipher_protocol; |
| 1024 | |
| 1025 | if (self->ssl == NULL) |
| 1026 | return Py_None; |
| 1027 | current = SSL_get_current_cipher(self->ssl); |
| 1028 | if (current == NULL) |
| 1029 | return Py_None; |
| 1030 | |
| 1031 | retval = PyTuple_New(3); |
| 1032 | if (retval == NULL) |
| 1033 | return NULL; |
| 1034 | |
| 1035 | cipher_name = (char *) SSL_CIPHER_get_name(current); |
| 1036 | if (cipher_name == NULL) { |
| 1037 | PyTuple_SET_ITEM(retval, 0, Py_None); |
| 1038 | } else { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1039 | v = PyString_FromString(cipher_name); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1040 | if (v == NULL) |
| 1041 | goto fail0; |
| 1042 | PyTuple_SET_ITEM(retval, 0, v); |
| 1043 | } |
| 1044 | cipher_protocol = SSL_CIPHER_get_version(current); |
| 1045 | if (cipher_protocol == NULL) { |
| 1046 | PyTuple_SET_ITEM(retval, 1, Py_None); |
| 1047 | } else { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1048 | v = PyString_FromString(cipher_protocol); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1049 | if (v == NULL) |
| 1050 | goto fail0; |
| 1051 | PyTuple_SET_ITEM(retval, 1, v); |
| 1052 | } |
| 1053 | v = PyInt_FromLong(SSL_CIPHER_get_bits(current, NULL)); |
| 1054 | if (v == NULL) |
| 1055 | goto fail0; |
| 1056 | PyTuple_SET_ITEM(retval, 2, v); |
| 1057 | return retval; |
| 1058 | |
| 1059 | fail0: |
| 1060 | Py_DECREF(retval); |
| 1061 | return NULL; |
| 1062 | } |
| 1063 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1064 | static void PySSL_dealloc(PySSLObject *self) |
| 1065 | { |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1066 | if (self->peer_cert) /* Possible not to have one? */ |
| 1067 | X509_free (self->peer_cert); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1068 | if (self->ssl) |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1069 | SSL_free(self->ssl); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1070 | if (self->ctx) |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1071 | SSL_CTX_free(self->ctx); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1072 | Py_XDECREF(self->Socket); |
| 1073 | PyObject_Del(self); |
| 1074 | } |
| 1075 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 1076 | /* If the socket has a timeout, do a select()/poll() on the socket. |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1077 | The argument writing indicates the direction. |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1078 | Returns one of the possibilities in the timeout_state enum (above). |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1079 | */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1080 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1081 | static int |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1082 | check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing) |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1083 | { |
| 1084 | fd_set fds; |
| 1085 | struct timeval tv; |
| 1086 | int rc; |
| 1087 | |
| 1088 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1089 | if (s->sock_timeout < 0.0) |
| 1090 | return SOCKET_IS_BLOCKING; |
| 1091 | else if (s->sock_timeout == 0.0) |
| 1092 | return SOCKET_IS_NONBLOCKING; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1093 | |
| 1094 | /* Guard against closed socket */ |
| 1095 | if (s->sock_fd < 0) |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1096 | return SOCKET_HAS_BEEN_CLOSED; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1097 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 1098 | /* Prefer poll, if available, since you can poll() any fd |
| 1099 | * which can't be done with select(). */ |
| 1100 | #ifdef HAVE_POLL |
| 1101 | { |
| 1102 | struct pollfd pollfd; |
| 1103 | int timeout; |
| 1104 | |
| 1105 | pollfd.fd = s->sock_fd; |
| 1106 | pollfd.events = writing ? POLLOUT : POLLIN; |
| 1107 | |
| 1108 | /* s->sock_timeout is in seconds, timeout in ms */ |
| 1109 | timeout = (int)(s->sock_timeout * 1000 + 0.5); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1110 | PySSL_BEGIN_ALLOW_THREADS |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 1111 | rc = poll(&pollfd, 1, timeout); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1112 | PySSL_END_ALLOW_THREADS |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 1113 | |
| 1114 | goto normal_return; |
| 1115 | } |
| 1116 | #endif |
| 1117 | |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1118 | /* Guard against socket too large for select*/ |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 1119 | #ifndef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1120 | if (s->sock_fd >= FD_SETSIZE) |
Neal Norwitz | 389cea8 | 2006-02-13 00:35:21 +0000 | [diff] [blame] | 1121 | return SOCKET_TOO_LARGE_FOR_SELECT; |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 1122 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1123 | |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1124 | /* Construct the arguments to select */ |
| 1125 | tv.tv_sec = (int)s->sock_timeout; |
| 1126 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 1127 | FD_ZERO(&fds); |
| 1128 | FD_SET(s->sock_fd, &fds); |
| 1129 | |
| 1130 | /* See if the socket is ready */ |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1131 | PySSL_BEGIN_ALLOW_THREADS |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1132 | if (writing) |
| 1133 | rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 1134 | else |
| 1135 | rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1136 | PySSL_END_ALLOW_THREADS |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1137 | |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 1138 | #ifdef HAVE_POLL |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 1139 | normal_return: |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 1140 | #endif |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1141 | /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
| 1142 | (when we are able to write or when there's something to read) */ |
| 1143 | return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1146 | static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args) |
| 1147 | { |
| 1148 | char *data; |
| 1149 | int len; |
Martin v. Löwis | 405a795 | 2003-10-27 14:24:37 +0000 | [diff] [blame] | 1150 | int count; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1151 | int sockstate; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1152 | int err; |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 1153 | int nonblocking; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1154 | |
Martin v. Löwis | 405a795 | 2003-10-27 14:24:37 +0000 | [diff] [blame] | 1155 | if (!PyArg_ParseTuple(args, "s#:write", &data, &count)) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1156 | return NULL; |
| 1157 | |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 1158 | /* just in case the blocking state of the socket has been changed */ |
| 1159 | nonblocking = (self->Socket->sock_timeout >= 0.0); |
| 1160 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1161 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 1162 | |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1163 | sockstate = check_socket_and_wait_for_timeout(self->Socket, 1); |
| 1164 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 1165 | PyErr_SetString(PySSLErrorObject, |
| 1166 | "The write operation timed out"); |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1167 | return NULL; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1168 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 1169 | PyErr_SetString(PySSLErrorObject, |
| 1170 | "Underlying socket has been closed."); |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1171 | return NULL; |
Neal Norwitz | 389cea8 | 2006-02-13 00:35:21 +0000 | [diff] [blame] | 1172 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 1173 | PyErr_SetString(PySSLErrorObject, |
| 1174 | "Underlying socket too large for select()."); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1175 | return NULL; |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1176 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1177 | do { |
| 1178 | err = 0; |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1179 | PySSL_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 405a795 | 2003-10-27 14:24:37 +0000 | [diff] [blame] | 1180 | len = SSL_write(self->ssl, data, count); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1181 | err = SSL_get_error(self->ssl, len); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1182 | PySSL_END_ALLOW_THREADS |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 1183 | if(PyErr_CheckSignals()) { |
| 1184 | return NULL; |
| 1185 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1186 | if (err == SSL_ERROR_WANT_READ) { |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 1187 | sockstate = |
| 1188 | check_socket_and_wait_for_timeout(self->Socket, 0); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1189 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 1190 | sockstate = |
| 1191 | check_socket_and_wait_for_timeout(self->Socket, 1); |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1192 | } else { |
| 1193 | sockstate = SOCKET_OPERATION_OK; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1194 | } |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1195 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 1196 | PyErr_SetString(PySSLErrorObject, |
| 1197 | "The write operation timed out"); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1198 | return NULL; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1199 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 1200 | PyErr_SetString(PySSLErrorObject, |
| 1201 | "Underlying socket has been closed."); |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1202 | return NULL; |
| 1203 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 1204 | break; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1205 | } |
| 1206 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1207 | if (len > 0) |
| 1208 | return PyInt_FromLong(len); |
| 1209 | else |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1210 | return PySSL_SetError(self, len, __FILE__, __LINE__); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1213 | PyDoc_STRVAR(PySSL_SSLwrite_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1214 | "write(s) -> len\n\ |
| 1215 | \n\ |
| 1216 | Writes the string s into the SSL object. Returns the number\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1217 | of bytes written."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1218 | |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 1219 | static PyObject *PySSL_SSLpending(PySSLObject *self) |
| 1220 | { |
| 1221 | int count = 0; |
| 1222 | |
| 1223 | PySSL_BEGIN_ALLOW_THREADS |
| 1224 | count = SSL_pending(self->ssl); |
| 1225 | PySSL_END_ALLOW_THREADS |
| 1226 | if (count < 0) |
| 1227 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
| 1228 | else |
| 1229 | return PyInt_FromLong(count); |
| 1230 | } |
| 1231 | |
| 1232 | PyDoc_STRVAR(PySSL_SSLpending_doc, |
| 1233 | "pending() -> count\n\ |
| 1234 | \n\ |
| 1235 | Returns the number of already decrypted bytes available for read,\n\ |
| 1236 | pending on the connection.\n"); |
| 1237 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1238 | static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args) |
| 1239 | { |
| 1240 | PyObject *buf; |
| 1241 | int count = 0; |
| 1242 | int len = 1024; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1243 | int sockstate; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1244 | int err; |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 1245 | int nonblocking; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1246 | |
| 1247 | if (!PyArg_ParseTuple(args, "|i:read", &len)) |
| 1248 | return NULL; |
| 1249 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1250 | if (!(buf = PyString_FromStringAndSize((char *) 0, len))) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1251 | return NULL; |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1252 | |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 1253 | /* just in case the blocking state of the socket has been changed */ |
| 1254 | nonblocking = (self->Socket->sock_timeout >= 0.0); |
| 1255 | BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1256 | BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 1257 | |
Georg Brandl | 43f08a8 | 2006-03-31 18:01:16 +0000 | [diff] [blame] | 1258 | /* first check if there are bytes ready to be read */ |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1259 | PySSL_BEGIN_ALLOW_THREADS |
Georg Brandl | 43f08a8 | 2006-03-31 18:01:16 +0000 | [diff] [blame] | 1260 | count = SSL_pending(self->ssl); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1261 | PySSL_END_ALLOW_THREADS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1262 | |
Georg Brandl | 43f08a8 | 2006-03-31 18:01:16 +0000 | [diff] [blame] | 1263 | if (!count) { |
| 1264 | sockstate = check_socket_and_wait_for_timeout(self->Socket, 0); |
| 1265 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1266 | PyErr_SetString(PySSLErrorObject, |
| 1267 | "The read operation timed out"); |
Georg Brandl | 43f08a8 | 2006-03-31 18:01:16 +0000 | [diff] [blame] | 1268 | Py_DECREF(buf); |
| 1269 | return NULL; |
| 1270 | } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1271 | PyErr_SetString(PySSLErrorObject, |
| 1272 | "Underlying socket too large for select()."); |
| 1273 | Py_DECREF(buf); |
Georg Brandl | 43f08a8 | 2006-03-31 18:01:16 +0000 | [diff] [blame] | 1274 | return NULL; |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1275 | } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 1276 | if (SSL_get_shutdown(self->ssl) != |
| 1277 | SSL_RECEIVED_SHUTDOWN) |
| 1278 | { |
| 1279 | Py_DECREF(buf); |
| 1280 | PyErr_SetString(PySSLErrorObject, |
| 1281 | "Socket closed without SSL shutdown handshake"); |
| 1282 | return NULL; |
| 1283 | } else { |
| 1284 | /* should contain a zero-length string */ |
| 1285 | _PyString_Resize(&buf, 0); |
| 1286 | return buf; |
| 1287 | } |
Georg Brandl | 43f08a8 | 2006-03-31 18:01:16 +0000 | [diff] [blame] | 1288 | } |
Guido van Rossum | 99d4abf | 2003-01-27 22:22:50 +0000 | [diff] [blame] | 1289 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1290 | do { |
| 1291 | err = 0; |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1292 | PySSL_BEGIN_ALLOW_THREADS |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1293 | count = SSL_read(self->ssl, PyString_AsString(buf), len); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1294 | err = SSL_get_error(self->ssl, count); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1295 | PySSL_END_ALLOW_THREADS |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 1296 | if(PyErr_CheckSignals()) { |
| 1297 | Py_DECREF(buf); |
| 1298 | return NULL; |
| 1299 | } |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1300 | if (err == SSL_ERROR_WANT_READ) { |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 1301 | sockstate = |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1302 | check_socket_and_wait_for_timeout(self->Socket, 0); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1303 | } else if (err == SSL_ERROR_WANT_WRITE) { |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1304 | sockstate = |
| 1305 | check_socket_and_wait_for_timeout(self->Socket, 1); |
| 1306 | } else if ((err == SSL_ERROR_ZERO_RETURN) && |
| 1307 | (SSL_get_shutdown(self->ssl) == |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 1308 | SSL_RECEIVED_SHUTDOWN)) |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1309 | { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1310 | _PyString_Resize(&buf, 0); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1311 | return buf; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1312 | } else { |
| 1313 | sockstate = SOCKET_OPERATION_OK; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1314 | } |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1315 | if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 1316 | PyErr_SetString(PySSLErrorObject, |
| 1317 | "The read operation timed out"); |
Martin v. Löwis | afec8e3 | 2003-06-28 07:40:23 +0000 | [diff] [blame] | 1318 | Py_DECREF(buf); |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1319 | return NULL; |
Andrew M. Kuchling | 9c3efe3 | 2004-07-10 21:15:17 +0000 | [diff] [blame] | 1320 | } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 1321 | break; |
Guido van Rossum | 4f707ac | 2003-01-31 18:13:18 +0000 | [diff] [blame] | 1322 | } |
| 1323 | } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1324 | if (count <= 0) { |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1325 | Py_DECREF(buf); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1326 | return PySSL_SetError(self, count, __FILE__, __LINE__); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1327 | } |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1328 | if (count != len) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1329 | _PyString_Resize(&buf, count); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1330 | return buf; |
| 1331 | } |
| 1332 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1333 | PyDoc_STRVAR(PySSL_SSLread_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1334 | "read([len]) -> string\n\ |
| 1335 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1336 | Read up to len bytes from the SSL socket."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1337 | |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 1338 | static PyObject *PySSL_SSLshutdown(PySSLObject *self) |
| 1339 | { |
| 1340 | int err; |
| 1341 | |
| 1342 | /* Guard against closed socket */ |
| 1343 | if (self->Socket->sock_fd < 0) { |
| 1344 | PyErr_SetString(PySSLErrorObject, |
| 1345 | "Underlying socket has been closed."); |
| 1346 | return NULL; |
| 1347 | } |
| 1348 | |
| 1349 | PySSL_BEGIN_ALLOW_THREADS |
| 1350 | err = SSL_shutdown(self->ssl); |
| 1351 | if (err == 0) { |
| 1352 | /* we need to call it again to finish the shutdown */ |
| 1353 | err = SSL_shutdown(self->ssl); |
| 1354 | } |
| 1355 | PySSL_END_ALLOW_THREADS |
| 1356 | |
| 1357 | if (err < 0) |
| 1358 | return PySSL_SetError(self, err, __FILE__, __LINE__); |
| 1359 | else { |
| 1360 | Py_INCREF(self->Socket); |
| 1361 | return (PyObject *) (self->Socket); |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | PyDoc_STRVAR(PySSL_SSLshutdown_doc, |
| 1366 | "shutdown(s) -> socket\n\ |
| 1367 | \n\ |
| 1368 | Does the SSL shutdown handshake with the remote end, and returns\n\ |
| 1369 | the underlying socket object."); |
| 1370 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1371 | static PyMethodDef PySSLMethods[] = { |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 1372 | {"do_handshake", (PyCFunction)PySSL_SSLdo_handshake, METH_NOARGS}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1373 | {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS, |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1374 | PySSL_SSLwrite_doc}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1375 | {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS, |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1376 | PySSL_SSLread_doc}, |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 1377 | {"pending", (PyCFunction)PySSL_SSLpending, METH_NOARGS, |
| 1378 | PySSL_SSLpending_doc}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1379 | {"server", (PyCFunction)PySSL_server, METH_NOARGS}, |
| 1380 | {"issuer", (PyCFunction)PySSL_issuer, METH_NOARGS}, |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1381 | {"peer_certificate", (PyCFunction)PySSL_peercert, METH_VARARGS, |
| 1382 | PySSL_peercert_doc}, |
| 1383 | {"cipher", (PyCFunction)PySSL_cipher, METH_NOARGS}, |
Bill Janssen | 934b16d | 2008-06-28 22:19:33 +0000 | [diff] [blame] | 1384 | {"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS, |
| 1385 | PySSL_SSLshutdown_doc}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1386 | {NULL, NULL} |
| 1387 | }; |
| 1388 | |
| 1389 | static PyObject *PySSL_getattr(PySSLObject *self, char *name) |
| 1390 | { |
| 1391 | return Py_FindMethod(PySSLMethods, (PyObject *)self, name); |
| 1392 | } |
| 1393 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1394 | static PyTypeObject PySSL_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1395 | PyVarObject_HEAD_INIT(NULL, 0) |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 1396 | "ssl.SSLContext", /*tp_name*/ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1397 | sizeof(PySSLObject), /*tp_basicsize*/ |
| 1398 | 0, /*tp_itemsize*/ |
| 1399 | /* methods */ |
| 1400 | (destructor)PySSL_dealloc, /*tp_dealloc*/ |
| 1401 | 0, /*tp_print*/ |
| 1402 | (getattrfunc)PySSL_getattr, /*tp_getattr*/ |
| 1403 | 0, /*tp_setattr*/ |
| 1404 | 0, /*tp_compare*/ |
| 1405 | 0, /*tp_repr*/ |
| 1406 | 0, /*tp_as_number*/ |
| 1407 | 0, /*tp_as_sequence*/ |
| 1408 | 0, /*tp_as_mapping*/ |
| 1409 | 0, /*tp_hash*/ |
| 1410 | }; |
| 1411 | |
| 1412 | #ifdef HAVE_OPENSSL_RAND |
| 1413 | |
| 1414 | /* helper routines for seeding the SSL PRNG */ |
| 1415 | static PyObject * |
| 1416 | PySSL_RAND_add(PyObject *self, PyObject *args) |
| 1417 | { |
| 1418 | char *buf; |
| 1419 | int len; |
| 1420 | double entropy; |
| 1421 | |
| 1422 | if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy)) |
| 1423 | return NULL; |
| 1424 | RAND_add(buf, len, entropy); |
| 1425 | Py_INCREF(Py_None); |
| 1426 | return Py_None; |
| 1427 | } |
| 1428 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1429 | PyDoc_STRVAR(PySSL_RAND_add_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1430 | "RAND_add(string, entropy)\n\ |
| 1431 | \n\ |
| 1432 | Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\ |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1433 | bound on the entropy contained in string. See RFC 1750."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1434 | |
| 1435 | static PyObject * |
| 1436 | PySSL_RAND_status(PyObject *self) |
| 1437 | { |
| 1438 | return PyInt_FromLong(RAND_status()); |
| 1439 | } |
| 1440 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1441 | PyDoc_STRVAR(PySSL_RAND_status_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1442 | "RAND_status() -> 0 or 1\n\ |
| 1443 | \n\ |
| 1444 | Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\ |
| 1445 | It is necessary to seed the PRNG with RAND_add() on some platforms before\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1446 | using the ssl() function."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1447 | |
| 1448 | static PyObject * |
| 1449 | PySSL_RAND_egd(PyObject *self, PyObject *arg) |
| 1450 | { |
| 1451 | int bytes; |
| 1452 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1453 | if (!PyString_Check(arg)) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1454 | return PyErr_Format(PyExc_TypeError, |
| 1455 | "RAND_egd() expected string, found %s", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1456 | Py_TYPE(arg)->tp_name); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1457 | bytes = RAND_egd(PyString_AS_STRING(arg)); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1458 | if (bytes == -1) { |
| 1459 | PyErr_SetString(PySSLErrorObject, |
| 1460 | "EGD connection failed or EGD did not return " |
| 1461 | "enough data to seed the PRNG"); |
| 1462 | return NULL; |
| 1463 | } |
| 1464 | return PyInt_FromLong(bytes); |
| 1465 | } |
| 1466 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1467 | PyDoc_STRVAR(PySSL_RAND_egd_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1468 | "RAND_egd(path) -> bytes\n\ |
| 1469 | \n\ |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1470 | Queries the entropy gather daemon (EGD) on the socket named by 'path'.\n\ |
| 1471 | Returns number of bytes read. Raises SSLError if connection to EGD\n\ |
| 1472 | fails or if it does provide enough data to seed PRNG."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1473 | |
| 1474 | #endif |
| 1475 | |
| 1476 | /* List of functions exported by this module. */ |
| 1477 | |
| 1478 | static PyMethodDef PySSL_methods[] = { |
Guido van Rossum | 780b80d | 2007-08-27 18:42:23 +0000 | [diff] [blame] | 1479 | {"sslwrap", PySSL_sslwrap, |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1480 | METH_VARARGS, ssl_doc}, |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1481 | {"_test_decode_cert", PySSL_test_decode_certificate, |
| 1482 | METH_VARARGS}, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1483 | #ifdef HAVE_OPENSSL_RAND |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1484 | {"RAND_add", PySSL_RAND_add, METH_VARARGS, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1485 | PySSL_RAND_add_doc}, |
| 1486 | {"RAND_egd", PySSL_RAND_egd, METH_O, |
| 1487 | PySSL_RAND_egd_doc}, |
| 1488 | {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS, |
| 1489 | PySSL_RAND_status_doc}, |
| 1490 | #endif |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1491 | {NULL, NULL} /* Sentinel */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1492 | }; |
| 1493 | |
| 1494 | |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1495 | #ifdef WITH_THREAD |
| 1496 | |
| 1497 | /* an implementation of OpenSSL threading operations in terms |
| 1498 | of the Python C thread library */ |
| 1499 | |
| 1500 | static PyThread_type_lock *_ssl_locks = NULL; |
| 1501 | |
| 1502 | static unsigned long _ssl_thread_id_function (void) { |
| 1503 | return PyThread_get_thread_ident(); |
| 1504 | } |
| 1505 | |
| 1506 | static void _ssl_thread_locking_function (int mode, int n, const char *file, int line) { |
| 1507 | /* this function is needed to perform locking on shared data |
| 1508 | structures. (Note that OpenSSL uses a number of global data |
| 1509 | structures that will be implicitly shared whenever multiple threads |
| 1510 | use OpenSSL.) Multi-threaded applications will crash at random if |
| 1511 | it is not set. |
| 1512 | |
| 1513 | locking_function() must be able to handle up to CRYPTO_num_locks() |
| 1514 | different mutex locks. It sets the n-th lock if mode & CRYPTO_LOCK, and |
| 1515 | releases it otherwise. |
| 1516 | |
| 1517 | file and line are the file number of the function setting the |
| 1518 | lock. They can be useful for debugging. |
| 1519 | */ |
| 1520 | |
| 1521 | if ((_ssl_locks == NULL) || |
Neal Norwitz | 5802bb2 | 2008-03-27 05:03:11 +0000 | [diff] [blame] | 1522 | (n < 0) || ((unsigned)n >= _ssl_locks_count)) |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1523 | return; |
| 1524 | |
| 1525 | if (mode & CRYPTO_LOCK) { |
| 1526 | PyThread_acquire_lock(_ssl_locks[n], 1); |
| 1527 | } else { |
| 1528 | PyThread_release_lock(_ssl_locks[n]); |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | static int _setup_ssl_threads(void) { |
| 1533 | |
Neal Norwitz | 5802bb2 | 2008-03-27 05:03:11 +0000 | [diff] [blame] | 1534 | unsigned int i; |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1535 | |
| 1536 | if (_ssl_locks == NULL) { |
| 1537 | _ssl_locks_count = CRYPTO_num_locks(); |
| 1538 | _ssl_locks = (PyThread_type_lock *) |
| 1539 | malloc(sizeof(PyThread_type_lock) * _ssl_locks_count); |
| 1540 | if (_ssl_locks == NULL) |
| 1541 | return 0; |
| 1542 | memset(_ssl_locks, 0, sizeof(PyThread_type_lock) * _ssl_locks_count); |
| 1543 | for (i = 0; i < _ssl_locks_count; i++) { |
| 1544 | _ssl_locks[i] = PyThread_allocate_lock(); |
| 1545 | if (_ssl_locks[i] == NULL) { |
Raymond Hettinger | 8fb67e0 | 2009-01-26 16:55:41 +0000 | [diff] [blame] | 1546 | unsigned int j; |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1547 | for (j = 0; j < i; j++) { |
| 1548 | PyThread_free_lock(_ssl_locks[j]); |
| 1549 | } |
| 1550 | free(_ssl_locks); |
| 1551 | return 0; |
| 1552 | } |
| 1553 | } |
| 1554 | CRYPTO_set_locking_callback(_ssl_thread_locking_function); |
| 1555 | CRYPTO_set_id_callback(_ssl_thread_id_function); |
| 1556 | } |
| 1557 | return 1; |
| 1558 | } |
| 1559 | |
| 1560 | #endif /* def HAVE_THREAD */ |
| 1561 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1562 | PyDoc_STRVAR(module_doc, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1563 | "Implementation module for SSL socket operations. See the socket module\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1564 | for documentation."); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1565 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1566 | PyMODINIT_FUNC |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1567 | init_ssl(void) |
| 1568 | { |
| 1569 | PyObject *m, *d; |
| 1570 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1571 | Py_TYPE(&PySSL_Type) = &PyType_Type; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1572 | |
| 1573 | m = Py_InitModule3("_ssl", PySSL_methods, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1574 | if (m == NULL) |
| 1575 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1576 | d = PyModule_GetDict(m); |
| 1577 | |
| 1578 | /* Load _socket module and its C API */ |
| 1579 | if (PySocketModule_ImportModuleAndAPI()) |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1580 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1581 | |
| 1582 | /* Init OpenSSL */ |
| 1583 | SSL_load_error_strings(); |
Antoine Pitrou | 878602a | 2010-04-21 19:41:28 +0000 | [diff] [blame] | 1584 | SSL_library_init(); |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1585 | #ifdef WITH_THREAD |
| 1586 | /* note that this will start threading if not already started */ |
| 1587 | if (!_setup_ssl_threads()) { |
| 1588 | return; |
| 1589 | } |
| 1590 | #endif |
Antoine Pitrou | 878602a | 2010-04-21 19:41:28 +0000 | [diff] [blame] | 1591 | OpenSSL_add_all_algorithms(); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1592 | |
| 1593 | /* Add symbols to module dict */ |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1594 | PySSLErrorObject = PyErr_NewException("ssl.SSLError", |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1595 | PySocketModule.error, |
| 1596 | NULL); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1597 | if (PySSLErrorObject == NULL) |
| 1598 | return; |
Bill Janssen | 98d19da | 2007-09-10 21:51:02 +0000 | [diff] [blame] | 1599 | if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0) |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1600 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1601 | if (PyDict_SetItemString(d, "SSLType", |
| 1602 | (PyObject *)&PySSL_Type) != 0) |
| 1603 | return; |
| 1604 | PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1605 | PY_SSL_ERROR_ZERO_RETURN); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1606 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1607 | PY_SSL_ERROR_WANT_READ); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1608 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1609 | PY_SSL_ERROR_WANT_WRITE); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1610 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1611 | PY_SSL_ERROR_WANT_X509_LOOKUP); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1612 | PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1613 | PY_SSL_ERROR_SYSCALL); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1614 | PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1615 | PY_SSL_ERROR_SSL); |
| 1616 | PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 1617 | PY_SSL_ERROR_WANT_CONNECT); |
| 1618 | /* non ssl.h errorcodes */ |
| 1619 | PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 1620 | PY_SSL_ERROR_EOF); |
| 1621 | PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 1622 | PY_SSL_ERROR_INVALID_ERROR_CODE); |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1623 | /* cert requirements */ |
| 1624 | PyModule_AddIntConstant(m, "CERT_NONE", |
| 1625 | PY_SSL_CERT_NONE); |
| 1626 | PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 1627 | PY_SSL_CERT_OPTIONAL); |
| 1628 | PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 1629 | PY_SSL_CERT_REQUIRED); |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 1630 | |
Guido van Rossum | 4f2c3dd | 2007-08-25 15:08:43 +0000 | [diff] [blame] | 1631 | /* protocol versions */ |
| 1632 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 1633 | PY_SSL_VERSION_SSL2); |
| 1634 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 1635 | PY_SSL_VERSION_SSL3); |
| 1636 | PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
| 1637 | PY_SSL_VERSION_SSL23); |
| 1638 | PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 1639 | PY_SSL_VERSION_TLS1); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1640 | } |