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