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