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