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