Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * connection.c |
| 3 | * |
| 4 | * Copyright (C) AB Strakt 2001, All rights reserved |
Jean-Paul Calderone | 8b63d45 | 2008-03-21 18:31:12 -0400 | [diff] [blame] | 5 | * Copyright (C) Jean-Paul Calderone 2008, All rights reserved |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 6 | * |
| 7 | * SSL Connection objects and methods. |
| 8 | * See the file RATIONALE for a short explanation of why this module was written. |
| 9 | * |
| 10 | * Reviewed 2001-07-23 |
| 11 | */ |
| 12 | #include <Python.h> |
Jean-Paul Calderone | 12ea9a0 | 2008-02-22 12:24:39 -0500 | [diff] [blame] | 13 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 14 | #ifndef MS_WINDOWS |
| 15 | # include <sys/socket.h> |
| 16 | # include <netinet/in.h> |
| 17 | # if !(defined(__BEOS__) || defined(__CYGWIN__)) |
| 18 | # include <netinet/tcp.h> |
| 19 | # endif |
| 20 | #else |
| 21 | # include <winsock.h> |
Jean-Paul Calderone | 12ea9a0 | 2008-02-22 12:24:39 -0500 | [diff] [blame] | 22 | # include <wincrypt.h> |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 23 | #endif |
| 24 | |
Jean-Paul Calderone | 12ea9a0 | 2008-02-22 12:24:39 -0500 | [diff] [blame] | 25 | #define SSL_MODULE |
Jean-Paul Calderone | 3ad85d4 | 2009-04-30 20:24:35 -0400 | [diff] [blame] | 26 | #include <openssl/bio.h> |
Jean-Paul Calderone | 12ea9a0 | 2008-02-22 12:24:39 -0500 | [diff] [blame] | 27 | #include <openssl/err.h> |
Jean-Paul Calderone | 12ea9a0 | 2008-02-22 12:24:39 -0500 | [diff] [blame] | 28 | #include "ssl.h" |
| 29 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 30 | /** |
| 31 | * If we are on UNIX, fine, just use PyErr_SetFromErrno. If we are on Windows, |
| 32 | * apply some black winsock voodoo. This is basically just copied from Python's |
| 33 | * socketmodule.c |
| 34 | * |
| 35 | * Arguments: None |
| 36 | * Returns: None |
| 37 | */ |
| 38 | static void |
| 39 | syscall_from_errno(void) |
| 40 | { |
| 41 | #ifdef MS_WINDOWS |
| 42 | int errnum = WSAGetLastError(); |
| 43 | if (errnum) |
| 44 | { |
| 45 | static struct { int num; const char *msg; } *msgp, msgs[] = { |
| 46 | { WSAEINTR, "Interrupted system call" }, |
| 47 | { WSAEBADF, "Bad file descriptor" }, |
| 48 | { WSAEACCES, "Permission denied" }, |
| 49 | { WSAEFAULT, "Bad address" }, |
| 50 | { WSAEINVAL, "Invalid argument" }, |
| 51 | { WSAEMFILE, "Too many open files" }, |
| 52 | { WSAEWOULDBLOCK, "The socket operation could not complete " |
| 53 | "without blocking" }, |
| 54 | { WSAEINPROGRESS, "Operation now in progress" }, |
| 55 | { WSAEALREADY, "Operation already in progress" }, |
| 56 | { WSAENOTSOCK, "Socket operation on non-socket" }, |
| 57 | { WSAEDESTADDRREQ, "Destination address required" }, |
| 58 | { WSAEMSGSIZE, "Message too long" }, |
| 59 | { WSAEPROTOTYPE, "Protocol wrong type for socket" }, |
| 60 | { WSAENOPROTOOPT, "Protocol not available" }, |
| 61 | { WSAEPROTONOSUPPORT, "Protocol not supported" }, |
| 62 | { WSAESOCKTNOSUPPORT, "Socket type not supported" }, |
| 63 | { WSAEOPNOTSUPP, "Operation not supported" }, |
| 64 | { WSAEPFNOSUPPORT, "Protocol family not supported" }, |
| 65 | { WSAEAFNOSUPPORT, "Address family not supported" }, |
| 66 | { WSAEADDRINUSE, "Address already in use" }, |
| 67 | { WSAEADDRNOTAVAIL, "Can't assign requested address" }, |
| 68 | { WSAENETDOWN, "Network is down" }, |
| 69 | { WSAENETUNREACH, "Network is unreachable" }, |
| 70 | { WSAENETRESET, "Network dropped connection on reset" }, |
| 71 | { WSAECONNABORTED, "Software caused connection abort" }, |
| 72 | { WSAECONNRESET, "Connection reset by peer" }, |
| 73 | { WSAENOBUFS, "No buffer space available" }, |
| 74 | { WSAEISCONN, "Socket is already connected" }, |
| 75 | { WSAENOTCONN, "Socket is not connected" }, |
| 76 | { WSAESHUTDOWN, "Can't send after socket shutdown" }, |
| 77 | { WSAETOOMANYREFS, "Too many references: can't splice" }, |
| 78 | { WSAETIMEDOUT, "Operation timed out" }, |
| 79 | { WSAECONNREFUSED, "Connection refused" }, |
| 80 | { WSAELOOP, "Too many levels of symbolic links" }, |
| 81 | { WSAENAMETOOLONG, "File name too long" }, |
| 82 | { WSAEHOSTDOWN, "Host is down" }, |
| 83 | { WSAEHOSTUNREACH, "No route to host" }, |
| 84 | { WSAENOTEMPTY, "Directory not empty" }, |
| 85 | { WSAEPROCLIM, "Too many processes" }, |
| 86 | { WSAEUSERS, "Too many users" }, |
| 87 | { WSAEDQUOT, "Disc quota exceeded" }, |
| 88 | { WSAESTALE, "Stale NFS file handle" }, |
| 89 | { WSAEREMOTE, "Too many levels of remote in path" }, |
| 90 | { WSASYSNOTREADY, "Network subsystem is unvailable" }, |
| 91 | { WSAVERNOTSUPPORTED, "WinSock version is not supported" }, |
| 92 | { WSANOTINITIALISED, "Successful WSAStartup() not yet performed" }, |
| 93 | { WSAEDISCON, "Graceful shutdown in progress" }, |
| 94 | /* Resolver errors */ |
| 95 | { WSAHOST_NOT_FOUND, "No such host is known" }, |
| 96 | { WSATRY_AGAIN, "Host not found, or server failed" }, |
| 97 | { WSANO_RECOVERY, "Unexpected server error encountered" }, |
| 98 | { WSANO_DATA, "Valid name without requested data" }, |
| 99 | { WSANO_ADDRESS, "No address, look for MX record" }, |
| 100 | { 0, NULL } |
| 101 | }; |
| 102 | PyObject *v; |
| 103 | const char *msg = "winsock error"; |
| 104 | |
| 105 | for (msgp = msgs; msgp->msg; msgp++) |
| 106 | { |
| 107 | if (errnum == msgp->num) |
| 108 | { |
| 109 | msg = msgp->msg; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | v = Py_BuildValue("(is)", errnum, msg); |
| 115 | if (v != NULL) |
| 116 | { |
| 117 | PyErr_SetObject(ssl_SysCallError, v); |
| 118 | Py_DECREF(v); |
| 119 | } |
| 120 | return; |
| 121 | } |
| 122 | #else |
| 123 | PyErr_SetFromErrno(ssl_SysCallError); |
| 124 | #endif |
| 125 | } |
| 126 | |
| 127 | /* |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 128 | * Handle errors raised by BIO functions. |
| 129 | * |
| 130 | * Arguments: bio - The BIO object |
| 131 | * ret - The return value of the BIO_ function. |
| 132 | * Returns: None, the calling function should return NULL; |
| 133 | */ |
| 134 | static void |
| 135 | handle_bio_errors(BIO* bio, int ret) |
| 136 | { |
| 137 | if (BIO_should_retry(bio)) { |
| 138 | if (BIO_should_read(bio)) { |
| 139 | PyErr_SetNone(ssl_WantReadError); |
| 140 | } else if (BIO_should_write(bio)) { |
| 141 | PyErr_SetNone(ssl_WantWriteError); |
| 142 | } else if (BIO_should_io_special(bio)) { |
| 143 | /* |
| 144 | * It's somewhat unclear what this means. From the OpenSSL source, |
| 145 | * it seems like it should not be triggered by the memory BIO, so |
| 146 | * for the time being, this case shouldn't come up. The SSL BIO |
| 147 | * (which I think should be named the socket BIO) may trigger this |
| 148 | * case if its socket is not yet connected or it is busy doing |
| 149 | * something related to x509. |
| 150 | */ |
| 151 | PyErr_SetString(PyExc_ValueError, "BIO_should_io_special"); |
| 152 | } else { |
| 153 | /* |
| 154 | * I hope this is dead code. The BIO documentation suggests that |
| 155 | * one of the above three checks should always be true. |
| 156 | */ |
| 157 | PyErr_SetString(PyExc_ValueError, "unknown bio failure"); |
| 158 | } |
| 159 | } else { |
| 160 | /* |
| 161 | * If we aren't to retry, it's really an error, so fall back to the |
| 162 | * normal error reporting code. However, the BIO interface does not |
| 163 | * specify a uniform error reporting mechanism. We can only hope that |
| 164 | * the code which triggered the error also kindly pushed something onto |
| 165 | * the error stack. |
| 166 | */ |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 167 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | /* |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 172 | * Handle errors raised by SSL I/O functions. NOTE: Not SSL_shutdown ;) |
| 173 | * |
| 174 | * Arguments: ssl - The SSL object |
| 175 | * err - The return code from SSL_get_error |
| 176 | * ret - The return code from the SSL I/O function |
| 177 | * Returns: None, the calling function should return NULL |
| 178 | */ |
| 179 | static void |
| 180 | handle_ssl_errors(SSL *ssl, int err, int ret) |
| 181 | { |
| 182 | switch (err) |
| 183 | { |
| 184 | /* |
| 185 | * Strange as it may seem, ZeroReturn is not an error per se. It means |
| 186 | * that the SSL Connection has been closed correctly (note, not the |
| 187 | * transport layer!), i.e. closure alerts have been exchanged. This is |
| 188 | * an exception since |
| 189 | * + There's an SSL "error" code for it |
| 190 | * + You have to deal with it in any case, close the transport layer |
| 191 | * etc |
| 192 | */ |
| 193 | case SSL_ERROR_ZERO_RETURN: |
| 194 | PyErr_SetNone(ssl_ZeroReturnError); |
| 195 | break; |
| 196 | |
| 197 | /* |
| 198 | * The WantXYZ exceptions don't mean that there's an error, just that |
| 199 | * nothing could be read/written just now, maybe because the transport |
| 200 | * layer would block on the operation, or that there's not enough data |
| 201 | * available to fill an entire SSL record. |
| 202 | */ |
| 203 | case SSL_ERROR_WANT_READ: |
| 204 | PyErr_SetNone(ssl_WantReadError); |
| 205 | break; |
| 206 | |
| 207 | case SSL_ERROR_WANT_WRITE: |
| 208 | PyErr_SetNone(ssl_WantWriteError); |
| 209 | break; |
| 210 | |
| 211 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 212 | PyErr_SetNone(ssl_WantX509LookupError); |
| 213 | break; |
| 214 | |
| 215 | case SSL_ERROR_SYSCALL: |
| 216 | if (ERR_peek_error() == 0) |
| 217 | { |
| 218 | if (ret < 0) |
| 219 | { |
| 220 | syscall_from_errno(); |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | PyObject *v; |
| 225 | |
| 226 | v = Py_BuildValue("(is)", -1, "Unexpected EOF"); |
| 227 | if (v != NULL) |
| 228 | { |
| 229 | PyErr_SetObject(ssl_SysCallError, v); |
| 230 | Py_DECREF(v); |
| 231 | } |
| 232 | } |
| 233 | break; |
| 234 | } |
| 235 | |
| 236 | /* NOTE: Fall-through here, we don't want to duplicate code, right? */ |
| 237 | |
| 238 | case SSL_ERROR_SSL: |
| 239 | ; |
| 240 | default: |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 241 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 242 | break; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Here be member methods of the Connection "class" |
| 248 | */ |
| 249 | |
| 250 | static char ssl_Connection_get_context_doc[] = "\n\ |
| 251 | Get session context\n\ |
| 252 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 253 | @return: A Context object\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 254 | "; |
| 255 | static PyObject * |
Jean-Paul Calderone | 1d69a72 | 2010-07-29 09:05:53 -0400 | [diff] [blame] | 256 | ssl_Connection_get_context(ssl_ConnectionObj *self, PyObject *args) { |
| 257 | if (!PyArg_ParseTuple(args, ":get_context")) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 258 | return NULL; |
Jean-Paul Calderone | 1d69a72 | 2010-07-29 09:05:53 -0400 | [diff] [blame] | 259 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 260 | |
| 261 | Py_INCREF(self->context); |
| 262 | return (PyObject *)self->context; |
| 263 | } |
| 264 | |
| 265 | static char ssl_Connection_pending_doc[] = "\n\ |
| 266 | Get the number of bytes that can be safely read from the connection\n\ |
| 267 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 268 | @return: The number of bytes available in the receive buffer.\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 269 | "; |
| 270 | static PyObject * |
Jean-Paul Calderone | 1d69a72 | 2010-07-29 09:05:53 -0400 | [diff] [blame] | 271 | ssl_Connection_pending(ssl_ConnectionObj *self, PyObject *args) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 272 | int ret; |
| 273 | |
Jean-Paul Calderone | 1d69a72 | 2010-07-29 09:05:53 -0400 | [diff] [blame] | 274 | if (!PyArg_ParseTuple(args, ":pending")) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 275 | return NULL; |
Jean-Paul Calderone | 1d69a72 | 2010-07-29 09:05:53 -0400 | [diff] [blame] | 276 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 277 | |
| 278 | ret = SSL_pending(self->ssl); |
| 279 | return PyInt_FromLong((long)ret); |
| 280 | } |
Jean-Paul Calderone | 1d69a72 | 2010-07-29 09:05:53 -0400 | [diff] [blame] | 281 | |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 282 | static char ssl_Connection_bio_write_doc[] = "\n\ |
| 283 | When using non-socket connections this function sends\n\ |
| 284 | \"dirty\" data that would have traveled in on the network.\n\ |
| 285 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 286 | @param buf: The string to put into the memory BIO.\n\ |
| 287 | @return: The number of bytes written\n\ |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 288 | "; |
| 289 | static PyObject * |
| 290 | ssl_Connection_bio_write(ssl_ConnectionObj *self, PyObject *args) |
| 291 | { |
| 292 | char *buf; |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 293 | int len, ret; |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 294 | |
Jean-Paul Calderone | 07acf3f | 2009-05-05 13:23:28 -0400 | [diff] [blame] | 295 | if (self->into_ssl == NULL) |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 296 | { |
| 297 | PyErr_SetString(PyExc_TypeError, "Connection sock was not None"); |
| 298 | return NULL; |
| 299 | } |
| 300 | |
Jean-Paul Calderone | fc4ed0f | 2009-04-27 11:51:27 -0400 | [diff] [blame] | 301 | if (!PyArg_ParseTuple(args, "s#|i:bio_write", &buf, &len)) |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 302 | return NULL; |
| 303 | |
| 304 | ret = BIO_write(self->into_ssl, buf, len); |
| 305 | |
| 306 | if (PyErr_Occurred()) |
| 307 | { |
| 308 | flush_error_queue(); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 312 | if (ret <= 0) { |
| 313 | /* |
| 314 | * There was a problem with the BIO_write of some sort. |
| 315 | */ |
Jean-Paul Calderone | 3ad85d4 | 2009-04-30 20:24:35 -0400 | [diff] [blame] | 316 | handle_bio_errors(self->into_ssl, ret); |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 317 | return NULL; |
| 318 | } |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 319 | |
| 320 | return PyInt_FromLong((long)ret); |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 321 | } |
| 322 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 323 | static char ssl_Connection_send_doc[] = "\n\ |
| 324 | Send data on the connection. NOTE: If you get one of the WantRead,\n\ |
| 325 | WantWrite or WantX509Lookup exceptions on this, you have to call the\n\ |
| 326 | method again with the SAME buffer.\n\ |
| 327 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 328 | @param buf: The string to send\n\ |
Jean-Paul Calderone | 40b32a2 | 2010-01-27 16:56:44 -0500 | [diff] [blame] | 329 | @param flags: (optional) Included for compatibility with the socket\n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 330 | API, the value is ignored\n\ |
| 331 | @return: The number of bytes written\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 332 | "; |
| 333 | static PyObject * |
| 334 | ssl_Connection_send(ssl_ConnectionObj *self, PyObject *args) |
| 335 | { |
| 336 | char *buf; |
| 337 | int len, ret, err, flags; |
| 338 | |
| 339 | if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags)) |
| 340 | return NULL; |
| 341 | |
| 342 | MY_BEGIN_ALLOW_THREADS(self->tstate) |
| 343 | ret = SSL_write(self->ssl, buf, len); |
| 344 | MY_END_ALLOW_THREADS(self->tstate) |
| 345 | |
| 346 | if (PyErr_Occurred()) |
| 347 | { |
| 348 | flush_error_queue(); |
| 349 | return NULL; |
| 350 | } |
| 351 | |
| 352 | err = SSL_get_error(self->ssl, ret); |
| 353 | if (err == SSL_ERROR_NONE) |
| 354 | { |
| 355 | return PyInt_FromLong((long)ret); |
| 356 | } |
| 357 | else |
| 358 | { |
| 359 | handle_ssl_errors(self->ssl, err, ret); |
| 360 | return NULL; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | static char ssl_Connection_sendall_doc[] = "\n\ |
| 365 | Send \"all\" data on the connection. This calls send() repeatedly until\n\ |
| 366 | all data is sent. If an error occurs, it's impossible to tell how much data\n\ |
| 367 | has been sent.\n\ |
| 368 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 369 | @param buf: The string to send\n\ |
Jean-Paul Calderone | 40b32a2 | 2010-01-27 16:56:44 -0500 | [diff] [blame] | 370 | @param flags: (optional) Included for compatibility with the socket\n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 371 | API, the value is ignored\n\ |
| 372 | @return: The number of bytes written\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 373 | "; |
| 374 | static PyObject * |
| 375 | ssl_Connection_sendall(ssl_ConnectionObj *self, PyObject *args) |
| 376 | { |
| 377 | char *buf; |
| 378 | int len, ret, err, flags; |
| 379 | PyObject *pyret = Py_None; |
| 380 | |
| 381 | if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags)) |
| 382 | return NULL; |
| 383 | |
| 384 | do { |
| 385 | MY_BEGIN_ALLOW_THREADS(self->tstate) |
| 386 | ret = SSL_write(self->ssl, buf, len); |
| 387 | MY_END_ALLOW_THREADS(self->tstate) |
| 388 | if (PyErr_Occurred()) |
| 389 | { |
| 390 | flush_error_queue(); |
| 391 | pyret = NULL; |
| 392 | break; |
| 393 | } |
| 394 | err = SSL_get_error(self->ssl, ret); |
| 395 | if (err == SSL_ERROR_NONE) |
| 396 | { |
| 397 | buf += ret; |
| 398 | len -= ret; |
| 399 | } |
| 400 | else if (err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL || |
| 401 | err == SSL_ERROR_ZERO_RETURN) |
| 402 | { |
| 403 | handle_ssl_errors(self->ssl, err, ret); |
| 404 | pyret = NULL; |
| 405 | break; |
| 406 | } |
| 407 | } while (len > 0); |
| 408 | |
| 409 | Py_XINCREF(pyret); |
| 410 | return pyret; |
| 411 | } |
| 412 | |
| 413 | static char ssl_Connection_recv_doc[] = "\n\ |
| 414 | Receive data on the connection. NOTE: If you get one of the WantRead,\n\ |
| 415 | WantWrite or WantX509Lookup exceptions on this, you have to call the\n\ |
| 416 | method again with the SAME buffer.\n\ |
| 417 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 418 | @param bufsiz: The maximum number of bytes to read\n\ |
Jean-Paul Calderone | 40b32a2 | 2010-01-27 16:56:44 -0500 | [diff] [blame] | 419 | @param flags: (optional) Included for compatibility with the socket\n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 420 | API, the value is ignored\n\ |
| 421 | @return: The string read from the Connection\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 422 | "; |
| 423 | static PyObject * |
| 424 | ssl_Connection_recv(ssl_ConnectionObj *self, PyObject *args) |
| 425 | { |
| 426 | int bufsiz, ret, err, flags; |
| 427 | PyObject *buf; |
| 428 | |
| 429 | if (!PyArg_ParseTuple(args, "i|i:recv", &bufsiz, &flags)) |
| 430 | return NULL; |
| 431 | |
| 432 | buf = PyString_FromStringAndSize(NULL, bufsiz); |
| 433 | if (buf == NULL) |
| 434 | return NULL; |
| 435 | |
| 436 | MY_BEGIN_ALLOW_THREADS(self->tstate) |
| 437 | ret = SSL_read(self->ssl, PyString_AsString(buf), bufsiz); |
| 438 | MY_END_ALLOW_THREADS(self->tstate) |
| 439 | |
| 440 | if (PyErr_Occurred()) |
| 441 | { |
| 442 | Py_DECREF(buf); |
| 443 | flush_error_queue(); |
| 444 | return NULL; |
| 445 | } |
| 446 | |
| 447 | err = SSL_get_error(self->ssl, ret); |
| 448 | if (err == SSL_ERROR_NONE) |
| 449 | { |
| 450 | if (ret != bufsiz && _PyString_Resize(&buf, ret) < 0) |
| 451 | return NULL; |
| 452 | return buf; |
| 453 | } |
| 454 | else |
| 455 | { |
| 456 | handle_ssl_errors(self->ssl, err, ret); |
| 457 | Py_DECREF(buf); |
| 458 | return NULL; |
| 459 | } |
| 460 | } |
| 461 | |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 462 | static char ssl_Connection_bio_read_doc[] = "\n\ |
| 463 | When using non-socket connections this function reads\n\ |
| 464 | the \"dirty\" data that would have traveled away on the network.\n\ |
| 465 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 466 | @param bufsiz: The maximum number of bytes to read\n\ |
| 467 | @return: The string read.\n\ |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 468 | "; |
| 469 | static PyObject * |
| 470 | ssl_Connection_bio_read(ssl_ConnectionObj *self, PyObject *args) |
| 471 | { |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 472 | int bufsiz, ret; |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 473 | PyObject *buf; |
| 474 | |
Jean-Paul Calderone | 07acf3f | 2009-05-05 13:23:28 -0400 | [diff] [blame] | 475 | if (self->from_ssl == NULL) |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 476 | { |
| 477 | PyErr_SetString(PyExc_TypeError, "Connection sock was not None"); |
| 478 | return NULL; |
| 479 | } |
| 480 | |
| 481 | if (!PyArg_ParseTuple(args, "i:bio_read", &bufsiz)) |
| 482 | return NULL; |
| 483 | |
| 484 | buf = PyString_FromStringAndSize(NULL, bufsiz); |
| 485 | if (buf == NULL) |
| 486 | return NULL; |
| 487 | |
| 488 | ret = BIO_read(self->from_ssl, PyString_AsString(buf), bufsiz); |
| 489 | |
| 490 | if (PyErr_Occurred()) |
| 491 | { |
| 492 | Py_DECREF(buf); |
| 493 | flush_error_queue(); |
| 494 | return NULL; |
| 495 | } |
| 496 | |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 497 | if (ret <= 0) { |
| 498 | /* |
| 499 | * There was a problem with the BIO_read of some sort. |
| 500 | */ |
| 501 | handle_bio_errors(self->from_ssl, ret); |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 502 | Py_DECREF(buf); |
| 503 | return NULL; |
| 504 | } |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 505 | |
| 506 | /* |
| 507 | * Shrink the string to match the number of bytes we actually read. |
| 508 | */ |
| 509 | if (ret != bufsiz && _PyString_Resize(&buf, ret) < 0) |
| 510 | { |
| 511 | Py_DECREF(buf); |
| 512 | return NULL; |
| 513 | } |
| 514 | return buf; |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 515 | } |
| 516 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 517 | static char ssl_Connection_renegotiate_doc[] = "\n\ |
| 518 | Renegotiate the session\n\ |
| 519 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 520 | @return: True if the renegotiation can be started, false otherwise\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 521 | "; |
| 522 | static PyObject * |
Jean-Paul Calderone | 8ea2252 | 2010-07-29 09:39:39 -0400 | [diff] [blame^] | 523 | ssl_Connection_renegotiate(ssl_ConnectionObj *self, PyObject *args) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 524 | int ret; |
| 525 | |
Jean-Paul Calderone | 8ea2252 | 2010-07-29 09:39:39 -0400 | [diff] [blame^] | 526 | if (!PyArg_ParseTuple(args, ":renegotiate")) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 527 | return NULL; |
Jean-Paul Calderone | 8ea2252 | 2010-07-29 09:39:39 -0400 | [diff] [blame^] | 528 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 529 | |
| 530 | MY_BEGIN_ALLOW_THREADS(self->tstate); |
| 531 | ret = SSL_renegotiate(self->ssl); |
| 532 | MY_END_ALLOW_THREADS(self->tstate); |
| 533 | |
Jean-Paul Calderone | 8ea2252 | 2010-07-29 09:39:39 -0400 | [diff] [blame^] | 534 | if (PyErr_Occurred()) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 535 | flush_error_queue(); |
| 536 | return NULL; |
| 537 | } |
| 538 | |
| 539 | return PyInt_FromLong((long)ret); |
| 540 | } |
| 541 | |
| 542 | static char ssl_Connection_do_handshake_doc[] = "\n\ |
| 543 | Perform an SSL handshake (usually called after renegotiate() or one of\n\ |
| 544 | set_*_state()). This can raise the same exceptions as send and recv.\n\ |
| 545 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 546 | @return: None.\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 547 | "; |
| 548 | static PyObject * |
| 549 | ssl_Connection_do_handshake(ssl_ConnectionObj *self, PyObject *args) |
| 550 | { |
| 551 | int ret, err; |
| 552 | |
| 553 | if (!PyArg_ParseTuple(args, ":do_handshake")) |
| 554 | return NULL; |
| 555 | |
| 556 | MY_BEGIN_ALLOW_THREADS(self->tstate); |
| 557 | ret = SSL_do_handshake(self->ssl); |
| 558 | MY_END_ALLOW_THREADS(self->tstate); |
| 559 | |
| 560 | if (PyErr_Occurred()) |
| 561 | { |
| 562 | flush_error_queue(); |
| 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | err = SSL_get_error(self->ssl, ret); |
| 567 | if (err == SSL_ERROR_NONE) |
| 568 | { |
| 569 | Py_INCREF(Py_None); |
| 570 | return Py_None; |
| 571 | } |
| 572 | else |
| 573 | { |
| 574 | handle_ssl_errors(self->ssl, err, ret); |
| 575 | return NULL; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 580 | static char ssl_Connection_renegotiate_pending_doc[] = "\n\ |
| 581 | Check if there's a renegotiation in progress, it will return false once\n\ |
| 582 | a renegotiation is finished.\n\ |
| 583 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 584 | @return: Whether there's a renegotiation in progress\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 585 | "; |
| 586 | static PyObject * |
| 587 | ssl_Connection_renegotiate_pending(ssl_ConnectionObj *self, PyObject *args) |
| 588 | { |
| 589 | if (!PyArg_ParseTuple(args, ":renegotiate_pending")) |
| 590 | return NULL; |
| 591 | |
| 592 | return PyInt_FromLong((long)SSL_renegotiate_pending(self->ssl)); |
| 593 | } |
| 594 | #endif |
| 595 | |
| 596 | static char ssl_Connection_total_renegotiations_doc[] = "\n\ |
| 597 | Find out the total number of renegotiations.\n\ |
| 598 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 599 | @return: The number of renegotiations.\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 600 | "; |
| 601 | static PyObject * |
| 602 | ssl_Connection_total_renegotiations(ssl_ConnectionObj *self, PyObject *args) |
| 603 | { |
| 604 | if (!PyArg_ParseTuple(args, ":total_renegotiations")) |
| 605 | return NULL; |
| 606 | |
| 607 | return PyInt_FromLong(SSL_total_renegotiations(self->ssl)); |
| 608 | } |
| 609 | |
| 610 | static char ssl_Connection_set_accept_state_doc[] = "\n\ |
| 611 | Set the connection to work in server mode. The handshake will be handled\n\ |
| 612 | automatically by read/write.\n\ |
| 613 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 614 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 615 | "; |
| 616 | static PyObject * |
| 617 | ssl_Connection_set_accept_state(ssl_ConnectionObj *self, PyObject *args) |
| 618 | { |
| 619 | if (!PyArg_ParseTuple(args, ":set_accept_state")) |
| 620 | return NULL; |
| 621 | |
| 622 | SSL_set_accept_state(self->ssl); |
| 623 | |
| 624 | Py_INCREF(Py_None); |
| 625 | return Py_None; |
| 626 | } |
| 627 | |
| 628 | static char ssl_Connection_set_connect_state_doc[] = "\n\ |
| 629 | Set the connection to work in client mode. The handshake will be handled\n\ |
| 630 | automatically by read/write.\n\ |
| 631 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 632 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 633 | "; |
| 634 | static PyObject * |
| 635 | ssl_Connection_set_connect_state(ssl_ConnectionObj *self, PyObject *args) |
| 636 | { |
| 637 | if (!PyArg_ParseTuple(args, ":set_connect_state")) |
| 638 | return NULL; |
| 639 | |
| 640 | SSL_set_connect_state(self->ssl); |
| 641 | |
| 642 | Py_INCREF(Py_None); |
| 643 | return Py_None; |
| 644 | } |
| 645 | |
| 646 | static char ssl_Connection_connect_doc[] = "\n\ |
| 647 | Connect to remote host and set up client-side SSL\n\ |
| 648 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 649 | @param addr: A remote address\n\ |
| 650 | @return: What the socket's connect method returns\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 651 | "; |
| 652 | static PyObject * |
| 653 | ssl_Connection_connect(ssl_ConnectionObj *self, PyObject *args) |
| 654 | { |
| 655 | PyObject *meth, *ret; |
| 656 | |
| 657 | if ((meth = PyObject_GetAttrString(self->socket, "connect")) == NULL) |
| 658 | return NULL; |
| 659 | |
| 660 | SSL_set_connect_state(self->ssl); |
| 661 | |
| 662 | ret = PyEval_CallObject(meth, args); |
| 663 | Py_DECREF(meth); |
| 664 | if (ret == NULL) |
| 665 | return NULL; |
| 666 | |
| 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | static char ssl_Connection_connect_ex_doc[] = "\n\ |
| 671 | Connect to remote host and set up client-side SSL. Note that if the socket's\n\ |
| 672 | connect_ex method doesn't return 0, SSL won't be initialized.\n\ |
| 673 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 674 | @param addr: A remove address\n\ |
| 675 | @return: What the socket's connect_ex method returns\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 676 | "; |
| 677 | static PyObject * |
| 678 | ssl_Connection_connect_ex(ssl_ConnectionObj *self, PyObject *args) |
| 679 | { |
| 680 | PyObject *meth, *ret; |
| 681 | |
| 682 | if ((meth = PyObject_GetAttrString(self->socket, "connect_ex")) == NULL) |
| 683 | return NULL; |
| 684 | |
| 685 | SSL_set_connect_state(self->ssl); |
| 686 | |
| 687 | ret = PyEval_CallObject(meth, args); |
| 688 | Py_DECREF(meth); |
| 689 | if (ret == NULL) |
| 690 | return NULL; |
| 691 | if (PyInt_Check(ret) && PyInt_AsLong(ret) != 0) |
| 692 | return ret; |
| 693 | |
| 694 | return ret; |
| 695 | } |
| 696 | |
| 697 | static char ssl_Connection_accept_doc[] = "\n\ |
| 698 | Accept incoming connection and set up SSL on it\n\ |
| 699 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 700 | @return: A (conn,addr) pair where conn is a Connection and addr is an\n\ |
| 701 | address\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 702 | "; |
| 703 | static PyObject * |
| 704 | ssl_Connection_accept(ssl_ConnectionObj *self, PyObject *args) |
| 705 | { |
| 706 | PyObject *tuple, *socket, *address, *meth; |
| 707 | ssl_ConnectionObj *conn; |
| 708 | |
| 709 | if ((meth = PyObject_GetAttrString(self->socket, "accept")) == NULL) |
| 710 | return NULL; |
| 711 | tuple = PyEval_CallObject(meth, args); |
| 712 | Py_DECREF(meth); |
| 713 | if (tuple == NULL) |
| 714 | return NULL; |
| 715 | |
| 716 | socket = PyTuple_GetItem(tuple, 0); |
| 717 | Py_INCREF(socket); |
| 718 | address = PyTuple_GetItem(tuple, 1); |
| 719 | Py_INCREF(address); |
| 720 | Py_DECREF(tuple); |
| 721 | |
| 722 | conn = ssl_Connection_New(self->context, socket); |
| 723 | Py_DECREF(socket); |
| 724 | if (conn == NULL) |
| 725 | { |
| 726 | Py_DECREF(address); |
| 727 | return NULL; |
| 728 | } |
| 729 | |
| 730 | SSL_set_accept_state(conn->ssl); |
| 731 | |
| 732 | tuple = Py_BuildValue("(OO)", conn, address); |
| 733 | |
| 734 | Py_DECREF(conn); |
| 735 | Py_DECREF(address); |
| 736 | |
| 737 | return tuple; |
| 738 | } |
| 739 | |
Jean-Paul Calderone | 3ad85d4 | 2009-04-30 20:24:35 -0400 | [diff] [blame] | 740 | static char ssl_Connection_bio_shutdown_doc[] = "\n\ |
| 741 | When using non-socket connections this function signals end of\n\ |
| 742 | data on the input for this connection.\n\ |
| 743 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 744 | @return: None\n\ |
Jean-Paul Calderone | 3ad85d4 | 2009-04-30 20:24:35 -0400 | [diff] [blame] | 745 | "; |
| 746 | |
| 747 | static PyObject * |
| 748 | ssl_Connection_bio_shutdown(ssl_ConnectionObj *self, PyObject *args) |
| 749 | { |
Jean-Paul Calderone | 07acf3f | 2009-05-05 13:23:28 -0400 | [diff] [blame] | 750 | if (self->from_ssl == NULL) |
| 751 | { |
| 752 | PyErr_SetString(PyExc_TypeError, "Connection sock was not None"); |
| 753 | return NULL; |
| 754 | } |
| 755 | |
Jean-Paul Calderone | 3ad85d4 | 2009-04-30 20:24:35 -0400 | [diff] [blame] | 756 | BIO_set_mem_eof_return(self->into_ssl, 0); |
| 757 | Py_INCREF(Py_None); |
| 758 | return Py_None; |
| 759 | } |
| 760 | |
| 761 | |
| 762 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 763 | static char ssl_Connection_shutdown_doc[] = "\n\ |
| 764 | Send closure alert\n\ |
| 765 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 766 | @return: True if the shutdown completed successfully (i.e. both sides\n\ |
| 767 | have sent closure alerts), false otherwise (i.e. you have to\n\ |
| 768 | wait for a ZeroReturnError on a recv() method call\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 769 | "; |
| 770 | static PyObject * |
| 771 | ssl_Connection_shutdown(ssl_ConnectionObj *self, PyObject *args) |
| 772 | { |
| 773 | int ret; |
| 774 | |
| 775 | if (!PyArg_ParseTuple(args, ":shutdown")) |
| 776 | return NULL; |
| 777 | |
| 778 | MY_BEGIN_ALLOW_THREADS(self->tstate) |
| 779 | ret = SSL_shutdown(self->ssl); |
| 780 | MY_END_ALLOW_THREADS(self->tstate) |
| 781 | |
| 782 | if (PyErr_Occurred()) |
| 783 | { |
| 784 | flush_error_queue(); |
| 785 | return NULL; |
| 786 | } |
| 787 | |
| 788 | if (ret < 0) |
| 789 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 790 | exception_from_error_queue(ssl_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 791 | return NULL; |
| 792 | } |
| 793 | else if (ret > 0) |
| 794 | { |
| 795 | Py_INCREF(Py_True); |
| 796 | return Py_True; |
| 797 | } |
| 798 | else |
| 799 | { |
| 800 | Py_INCREF(Py_False); |
| 801 | return Py_False; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | static char ssl_Connection_get_cipher_list_doc[] = "\n\ |
| 806 | Get the session cipher list\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 807 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 808 | @return: A list of cipher strings\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 809 | "; |
| 810 | static PyObject * |
| 811 | ssl_Connection_get_cipher_list(ssl_ConnectionObj *self, PyObject *args) |
| 812 | { |
| 813 | int idx = 0; |
| 814 | const char *ret; |
| 815 | PyObject *lst, *item; |
| 816 | |
| 817 | if (!PyArg_ParseTuple(args, ":get_cipher_list")) |
| 818 | return NULL; |
| 819 | |
| 820 | lst = PyList_New(0); |
| 821 | while ((ret = SSL_get_cipher_list(self->ssl, idx)) != NULL) |
| 822 | { |
| 823 | item = PyString_FromString(ret); |
Ziga Seilnacht | fdeadb1 | 2009-09-01 16:35:50 +0200 | [diff] [blame] | 824 | PyList_Append(lst, item); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 825 | Py_DECREF(item); |
| 826 | idx++; |
| 827 | } |
| 828 | return lst; |
| 829 | } |
| 830 | |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 831 | static char ssl_Connection_get_client_ca_list_doc[] = "\n\ |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 832 | Get CAs whose certificates are suggested for client authentication.\n\ |
| 833 | \n\ |
Jean-Paul Calderone | 3578890 | 2009-10-24 13:48:08 -0400 | [diff] [blame] | 834 | @return: If this is a server connection, a list of X509Names representing\n\ |
| 835 | the acceptable CAs as set by L{OpenSSL.SSL.Context.set_client_ca_list} or\n\ |
| 836 | L{OpenSSL.SSL.Context.add_client_ca}. If this is a client connection,\n\ |
| 837 | the list of such X509Names sent by the server, or an empty list if that\n\ |
| 838 | has not yet happened.\n\ |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 839 | "; |
| 840 | |
| 841 | static PyObject * |
Jean-Paul Calderone | 6e2b685 | 2009-10-24 14:04:30 -0400 | [diff] [blame] | 842 | ssl_Connection_get_client_ca_list(ssl_ConnectionObj *self, PyObject *args) { |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 843 | STACK_OF(X509_NAME) *CANames; |
| 844 | PyObject *CAList; |
| 845 | int i, n; |
| 846 | |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 847 | if (!PyArg_ParseTuple(args, ":get_client_ca_list")) { |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 848 | return NULL; |
| 849 | } |
| 850 | CANames = SSL_get_client_CA_list(self->ssl); |
| 851 | if (CANames == NULL) { |
| 852 | return PyList_New(0); |
| 853 | } |
| 854 | n = sk_X509_NAME_num(CANames); |
| 855 | CAList = PyList_New(n); |
| 856 | if (CAList == NULL) { |
| 857 | return NULL; |
| 858 | } |
| 859 | for (i = 0; i < n; i++) { |
| 860 | X509_NAME *CAName; |
| 861 | PyObject *CA; |
| 862 | |
| 863 | CAName = X509_NAME_dup(sk_X509_NAME_value(CANames, i)); |
| 864 | if (CAName == NULL) { |
| 865 | Py_DECREF(CAList); |
| 866 | exception_from_error_queue(ssl_Error); |
| 867 | return NULL; |
| 868 | } |
| 869 | CA = (PyObject *)crypto_X509Name_New(CAName, 1); |
| 870 | if (CA == NULL) { |
| 871 | X509_NAME_free(CAName); |
| 872 | Py_DECREF(CAList); |
| 873 | return NULL; |
| 874 | } |
| 875 | if (PyList_SetItem(CAList, i, CA)) { |
| 876 | Py_DECREF(CA); |
| 877 | Py_DECREF(CAList); |
| 878 | return NULL; |
| 879 | } |
| 880 | } |
| 881 | return CAList; |
| 882 | } |
| 883 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 884 | static char ssl_Connection_makefile_doc[] = "\n\ |
| 885 | The makefile() method is not implemented, since there is no dup semantics\n\ |
| 886 | for SSL connections\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 887 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 888 | @raise NotImplementedError\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 889 | "; |
| 890 | static PyObject * |
| 891 | ssl_Connection_makefile(ssl_ConnectionObj *self, PyObject *args) |
| 892 | { |
| 893 | PyErr_SetString(PyExc_NotImplementedError, "Cannot make file object of SSL.Connection"); |
| 894 | return NULL; |
| 895 | } |
| 896 | |
| 897 | static char ssl_Connection_get_app_data_doc[] = "\n\ |
| 898 | Get application data\n\ |
| 899 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 900 | @return: The application data\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 901 | "; |
| 902 | static PyObject * |
| 903 | ssl_Connection_get_app_data(ssl_ConnectionObj *self, PyObject *args) |
| 904 | { |
| 905 | if (!PyArg_ParseTuple(args, ":get_app_data")) |
| 906 | return NULL; |
| 907 | |
| 908 | Py_INCREF(self->app_data); |
| 909 | return self->app_data; |
| 910 | } |
| 911 | |
| 912 | static char ssl_Connection_set_app_data_doc[] = "\n\ |
| 913 | Set application data\n\ |
| 914 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 915 | @param data - The application data\n\ |
| 916 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 917 | "; |
| 918 | static PyObject * |
| 919 | ssl_Connection_set_app_data(ssl_ConnectionObj *self, PyObject *args) |
| 920 | { |
| 921 | PyObject *data; |
| 922 | |
| 923 | if (!PyArg_ParseTuple(args, "O:set_app_data", &data)) |
| 924 | return NULL; |
| 925 | |
| 926 | Py_DECREF(self->app_data); |
| 927 | Py_INCREF(data); |
| 928 | self->app_data = data; |
| 929 | |
| 930 | Py_INCREF(Py_None); |
| 931 | return Py_None; |
| 932 | } |
| 933 | |
Jean-Paul Calderone | 72b8f0f | 2008-02-21 23:57:40 -0500 | [diff] [blame] | 934 | static char ssl_Connection_get_shutdown_doc[] = "\n\ |
| 935 | Get shutdown state\n\ |
| 936 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 937 | @return: The shutdown state, a bitvector of SENT_SHUTDOWN, RECEIVED_SHUTDOWN.\n\ |
Jean-Paul Calderone | 72b8f0f | 2008-02-21 23:57:40 -0500 | [diff] [blame] | 938 | "; |
| 939 | static PyObject * |
| 940 | ssl_Connection_get_shutdown(ssl_ConnectionObj *self, PyObject *args) |
| 941 | { |
| 942 | if (!PyArg_ParseTuple(args, ":get_shutdown")) |
| 943 | return NULL; |
| 944 | |
| 945 | return PyInt_FromLong((long)SSL_get_shutdown(self->ssl)); |
| 946 | } |
| 947 | |
| 948 | static char ssl_Connection_set_shutdown_doc[] = "\n\ |
| 949 | Set shutdown state\n\ |
| 950 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 951 | @param state - bitvector of SENT_SHUTDOWN, RECEIVED_SHUTDOWN.\n\ |
| 952 | @return: None\n\ |
Jean-Paul Calderone | 72b8f0f | 2008-02-21 23:57:40 -0500 | [diff] [blame] | 953 | "; |
| 954 | static PyObject * |
| 955 | ssl_Connection_set_shutdown(ssl_ConnectionObj *self, PyObject *args) |
| 956 | { |
| 957 | int shutdown; |
| 958 | |
| 959 | if (!PyArg_ParseTuple(args, "i:set_shutdown", &shutdown)) |
| 960 | return NULL; |
| 961 | |
| 962 | SSL_set_shutdown(self->ssl, shutdown); |
| 963 | Py_INCREF(Py_None); |
| 964 | return Py_None; |
| 965 | } |
| 966 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 967 | static char ssl_Connection_state_string_doc[] = "\n\ |
| 968 | Get a verbose state description\n\ |
| 969 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 970 | @return: A string representing the state\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 971 | "; |
| 972 | static PyObject * |
| 973 | ssl_Connection_state_string(ssl_ConnectionObj *self, PyObject *args) |
| 974 | { |
| 975 | if (!PyArg_ParseTuple(args, ":state_string")) |
| 976 | return NULL; |
| 977 | |
| 978 | return PyString_FromString(SSL_state_string_long(self->ssl)); |
| 979 | } |
| 980 | |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 981 | static char ssl_Connection_client_random_doc[] = "\n\ |
| 982 | Get a copy of the client hello nonce.\n\ |
| 983 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 984 | @return: A string representing the state\n\ |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 985 | "; |
| 986 | static PyObject * |
| 987 | ssl_Connection_client_random(ssl_ConnectionObj *self, PyObject *args) |
| 988 | { |
| 989 | if (!PyArg_ParseTuple(args, ":client_random")) |
| 990 | return NULL; |
| 991 | |
Jean-Paul Calderone | 07acf3f | 2009-05-05 13:23:28 -0400 | [diff] [blame] | 992 | if (self->ssl->session == NULL) { |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 993 | Py_INCREF(Py_None); |
| 994 | return Py_None; |
| 995 | } |
| 996 | return PyString_FromStringAndSize( (const char *) self->ssl->s3->client_random, SSL3_RANDOM_SIZE); |
| 997 | } |
| 998 | |
| 999 | static char ssl_Connection_server_random_doc[] = "\n\ |
| 1000 | Get a copy of the server hello nonce.\n\ |
| 1001 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 1002 | @return: A string representing the state\n\ |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 1003 | "; |
| 1004 | static PyObject * |
| 1005 | ssl_Connection_server_random(ssl_ConnectionObj *self, PyObject *args) |
| 1006 | { |
| 1007 | if (!PyArg_ParseTuple(args, ":server_random")) |
| 1008 | return NULL; |
| 1009 | |
Jean-Paul Calderone | 07acf3f | 2009-05-05 13:23:28 -0400 | [diff] [blame] | 1010 | if (self->ssl->session == NULL) { |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 1011 | Py_INCREF(Py_None); |
| 1012 | return Py_None; |
| 1013 | } |
| 1014 | return PyString_FromStringAndSize( (const char *) self->ssl->s3->server_random, SSL3_RANDOM_SIZE); |
| 1015 | } |
| 1016 | |
| 1017 | static char ssl_Connection_master_key_doc[] = "\n\ |
| 1018 | Get a copy of the master key.\n\ |
| 1019 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 1020 | @return: A string representing the state\n\ |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 1021 | "; |
| 1022 | static PyObject * |
| 1023 | ssl_Connection_master_key(ssl_ConnectionObj *self, PyObject *args) |
| 1024 | { |
| 1025 | if (!PyArg_ParseTuple(args, ":master_key")) |
| 1026 | return NULL; |
| 1027 | |
Jean-Paul Calderone | 07acf3f | 2009-05-05 13:23:28 -0400 | [diff] [blame] | 1028 | if (self->ssl->session == NULL) { |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 1029 | Py_INCREF(Py_None); |
| 1030 | return Py_None; |
| 1031 | } |
| 1032 | return PyString_FromStringAndSize( (const char *) self->ssl->session->master_key, self->ssl->session->master_key_length); |
| 1033 | } |
| 1034 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1035 | static char ssl_Connection_sock_shutdown_doc[] = "\n\ |
| 1036 | See shutdown(2)\n\ |
| 1037 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 1038 | @return: What the socket's shutdown() method returns\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1039 | "; |
| 1040 | static PyObject * |
| 1041 | ssl_Connection_sock_shutdown(ssl_ConnectionObj *self, PyObject *args) |
| 1042 | { |
| 1043 | PyObject *meth, *ret; |
| 1044 | |
| 1045 | if ((meth = PyObject_GetAttrString(self->socket, "shutdown")) == NULL) |
| 1046 | return NULL; |
| 1047 | ret = PyEval_CallObject(meth, args); |
| 1048 | Py_DECREF(meth); |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
| 1052 | static char ssl_Connection_get_peer_certificate_doc[] = "\n\ |
| 1053 | Retrieve the other side's certificate (if any)\n\ |
| 1054 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 1055 | @return: The peer's certificate\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1056 | "; |
| 1057 | static PyObject * |
| 1058 | ssl_Connection_get_peer_certificate(ssl_ConnectionObj *self, PyObject *args) |
| 1059 | { |
| 1060 | X509 *cert; |
| 1061 | |
| 1062 | if (!PyArg_ParseTuple(args, ":get_peer_certificate")) |
| 1063 | return NULL; |
| 1064 | |
| 1065 | cert = SSL_get_peer_certificate(self->ssl); |
| 1066 | if (cert != NULL) |
| 1067 | { |
| 1068 | return (PyObject *)crypto_X509_New(cert, 1); |
| 1069 | } |
| 1070 | else |
| 1071 | { |
| 1072 | Py_INCREF(Py_None); |
| 1073 | return Py_None; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | static char ssl_Connection_want_read_doc[] = "\n\ |
| 1078 | Checks if more data has to be read from the transport layer to complete an\n\ |
| 1079 | operation.\n\ |
| 1080 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 1081 | @return: True iff more data has to be read\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1082 | "; |
| 1083 | static PyObject * |
| 1084 | ssl_Connection_want_read(ssl_ConnectionObj *self, PyObject *args) |
| 1085 | { |
| 1086 | if (!PyArg_ParseTuple(args, ":want_read")) |
| 1087 | return NULL; |
| 1088 | |
| 1089 | return PyInt_FromLong((long)SSL_want_read(self->ssl)); |
| 1090 | } |
| 1091 | |
| 1092 | static char ssl_Connection_want_write_doc[] = "\n\ |
| 1093 | Checks if there is data to write to the transport layer to complete an\n\ |
| 1094 | operation.\n\ |
| 1095 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 1096 | @return: True iff there is data to write\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1097 | "; |
| 1098 | static PyObject * |
| 1099 | ssl_Connection_want_write(ssl_ConnectionObj *self, PyObject *args) |
| 1100 | { |
| 1101 | if (!PyArg_ParseTuple(args, ":want_write")) |
| 1102 | return NULL; |
| 1103 | |
| 1104 | return PyInt_FromLong((long)SSL_want_write(self->ssl)); |
| 1105 | } |
| 1106 | |
| 1107 | /* |
| 1108 | * Member methods in the Connection object |
| 1109 | * ADD_METHOD(name) expands to a correct PyMethodDef declaration |
| 1110 | * { 'name', (PyCFunction)ssl_Connection_name, METH_VARARGS } |
| 1111 | * for convenience |
| 1112 | * ADD_ALIAS(name,real) creates an "alias" of the ssl_Connection_real |
| 1113 | * function with the name 'name' |
| 1114 | */ |
| 1115 | #define ADD_METHOD(name) \ |
| 1116 | { #name, (PyCFunction)ssl_Connection_##name, METH_VARARGS, ssl_Connection_##name##_doc } |
| 1117 | #define ADD_ALIAS(name,real) \ |
| 1118 | { #name, (PyCFunction)ssl_Connection_##real, METH_VARARGS, ssl_Connection_##real##_doc } |
| 1119 | static PyMethodDef ssl_Connection_methods[] = |
| 1120 | { |
| 1121 | ADD_METHOD(get_context), |
| 1122 | ADD_METHOD(pending), |
| 1123 | ADD_METHOD(send), |
| 1124 | ADD_ALIAS (write, send), |
| 1125 | ADD_METHOD(sendall), |
| 1126 | ADD_METHOD(recv), |
| 1127 | ADD_ALIAS (read, recv), |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 1128 | ADD_METHOD(bio_read), |
| 1129 | ADD_METHOD(bio_write), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1130 | ADD_METHOD(renegotiate), |
| 1131 | ADD_METHOD(do_handshake), |
| 1132 | #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 1133 | ADD_METHOD(renegotiate_pending), |
| 1134 | #endif |
| 1135 | ADD_METHOD(total_renegotiations), |
| 1136 | ADD_METHOD(connect), |
| 1137 | ADD_METHOD(connect_ex), |
| 1138 | ADD_METHOD(accept), |
Jean-Paul Calderone | 3ad85d4 | 2009-04-30 20:24:35 -0400 | [diff] [blame] | 1139 | ADD_METHOD(bio_shutdown), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1140 | ADD_METHOD(shutdown), |
| 1141 | ADD_METHOD(get_cipher_list), |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 1142 | ADD_METHOD(get_client_ca_list), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1143 | ADD_METHOD(makefile), |
| 1144 | ADD_METHOD(get_app_data), |
| 1145 | ADD_METHOD(set_app_data), |
Jean-Paul Calderone | 72b8f0f | 2008-02-21 23:57:40 -0500 | [diff] [blame] | 1146 | ADD_METHOD(get_shutdown), |
| 1147 | ADD_METHOD(set_shutdown), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1148 | ADD_METHOD(state_string), |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 1149 | ADD_METHOD(server_random), |
| 1150 | ADD_METHOD(client_random), |
| 1151 | ADD_METHOD(master_key), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1152 | ADD_METHOD(sock_shutdown), |
| 1153 | ADD_METHOD(get_peer_certificate), |
| 1154 | ADD_METHOD(want_read), |
| 1155 | ADD_METHOD(want_write), |
| 1156 | ADD_METHOD(set_accept_state), |
| 1157 | ADD_METHOD(set_connect_state), |
| 1158 | { NULL, NULL } |
| 1159 | }; |
| 1160 | #undef ADD_ALIAS |
| 1161 | #undef ADD_METHOD |
| 1162 | |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1163 | static char ssl_Connection_doc[] = "\n\ |
| 1164 | Connection(context, socket) -> Connection instance\n\ |
| 1165 | \n\ |
| 1166 | Create a new Connection object, using the given OpenSSL.SSL.Context instance\n\ |
| 1167 | and socket.\n\ |
| 1168 | \n\ |
| 1169 | @param context: An SSL Context to use for this connection\n\ |
| 1170 | @param socket: The socket to use for transport layer\n\ |
| 1171 | "; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1172 | |
| 1173 | /* |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1174 | * Initializer used by ssl_Connection_new and ssl_Connection_New. *Not* |
| 1175 | * tp_init. This takes an already allocated ssl_ConnectionObj, a context, and |
| 1176 | * a optionally a socket, and glues them all together. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1177 | */ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1178 | static ssl_ConnectionObj* |
| 1179 | ssl_Connection_init(ssl_ConnectionObj *self, ssl_ContextObj *ctx, PyObject *sock) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1180 | int fd; |
| 1181 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1182 | Py_INCREF(ctx); |
| 1183 | self->context = ctx; |
| 1184 | |
| 1185 | Py_INCREF(sock); |
| 1186 | self->socket = sock; |
| 1187 | |
| 1188 | self->ssl = NULL; |
Jean-Paul Calderone | fc4ed0f | 2009-04-27 11:51:27 -0400 | [diff] [blame] | 1189 | self->from_ssl = NULL; |
| 1190 | self->into_ssl = NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1191 | |
| 1192 | Py_INCREF(Py_None); |
| 1193 | self->app_data = Py_None; |
| 1194 | |
| 1195 | self->tstate = NULL; |
| 1196 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1197 | self->ssl = SSL_new(self->context->ctx); |
| 1198 | SSL_set_app_data(self->ssl, self); |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 1199 | |
Jean-Paul Calderone | 07acf3f | 2009-05-05 13:23:28 -0400 | [diff] [blame] | 1200 | if (self->socket == Py_None) |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 1201 | { |
| 1202 | /* If it's not a socket or file, treat it like a memory buffer, |
| 1203 | * so crazy people can do things like EAP-TLS. */ |
| 1204 | self->into_ssl = BIO_new(BIO_s_mem()); |
| 1205 | self->from_ssl = BIO_new(BIO_s_mem()); |
Jean-Paul Calderone | 07acf3f | 2009-05-05 13:23:28 -0400 | [diff] [blame] | 1206 | if (self->into_ssl == NULL || self->from_ssl == NULL) |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 1207 | goto error; |
| 1208 | SSL_set_bio(self->ssl, self->into_ssl, self->from_ssl); |
| 1209 | } |
| 1210 | else |
| 1211 | { |
| 1212 | fd = PyObject_AsFileDescriptor(self->socket); |
| 1213 | if (fd < 0) |
| 1214 | { |
| 1215 | Py_DECREF(self); |
| 1216 | return NULL; |
| 1217 | } |
| 1218 | else |
| 1219 | { |
| 1220 | SSL_set_fd(self->ssl, (SOCKET_T)fd); |
| 1221 | } |
| 1222 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1223 | return self; |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 1224 | |
| 1225 | error: |
| 1226 | BIO_free(self->into_ssl); /* NULL safe */ |
| 1227 | BIO_free(self->from_ssl); /* NULL safe */ |
| 1228 | Py_DECREF(self); |
| 1229 | return NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /* |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1233 | * Constructor for Connection objects |
| 1234 | * |
| 1235 | * Arguments: ctx - An SSL Context to use for this connection |
| 1236 | * sock - The socket to use for transport layer |
| 1237 | * Returns: The newly created Connection object |
| 1238 | */ |
| 1239 | ssl_ConnectionObj * |
| 1240 | ssl_Connection_New(ssl_ContextObj *ctx, PyObject *sock) { |
| 1241 | ssl_ConnectionObj *self; |
| 1242 | |
| 1243 | self = PyObject_GC_New(ssl_ConnectionObj, &ssl_Connection_Type); |
| 1244 | if (self == NULL) { |
| 1245 | return NULL; |
| 1246 | } |
| 1247 | self = ssl_Connection_init(self, ctx, sock); |
| 1248 | if (self == NULL) { |
| 1249 | return NULL; |
| 1250 | } |
| 1251 | PyObject_GC_Track((PyObject *)self); |
| 1252 | return self; |
| 1253 | } |
| 1254 | |
| 1255 | static PyObject* |
| 1256 | ssl_Connection_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) { |
| 1257 | ssl_ConnectionObj *self; |
| 1258 | ssl_ContextObj *ctx; |
| 1259 | PyObject *sock; |
| 1260 | static char *kwlist[] = {"context", "socket", NULL}; |
| 1261 | |
| 1262 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!O:Connection", kwlist, |
| 1263 | &ssl_Context_Type, &ctx, &sock)) { |
| 1264 | return NULL; |
| 1265 | } |
| 1266 | |
| 1267 | self = (ssl_ConnectionObj *)subtype->tp_alloc(subtype, 1); |
| 1268 | if (self == NULL) { |
| 1269 | return NULL; |
| 1270 | } |
| 1271 | |
| 1272 | return (PyObject *)ssl_Connection_init(self, ctx, sock); |
| 1273 | } |
| 1274 | |
| 1275 | /* |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1276 | * Find attribute |
| 1277 | * |
| 1278 | * Arguments: self - The Connection object |
| 1279 | * name - The attribute name |
| 1280 | * Returns: A Python object for the attribute, or NULL if something went |
| 1281 | * wrong |
| 1282 | */ |
| 1283 | static PyObject * |
| 1284 | ssl_Connection_getattr(ssl_ConnectionObj *self, char *name) |
| 1285 | { |
| 1286 | PyObject *meth; |
| 1287 | |
| 1288 | meth = Py_FindMethod(ssl_Connection_methods, (PyObject *)self, name); |
| 1289 | |
| 1290 | if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1291 | { |
| 1292 | PyErr_Clear(); |
| 1293 | /* Try looking it up in the "socket" instead. */ |
| 1294 | meth = PyObject_GetAttrString(self->socket, name); |
| 1295 | } |
| 1296 | |
| 1297 | return meth; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | * Call the visitproc on all contained objects. |
| 1302 | * |
| 1303 | * Arguments: self - The Connection object |
| 1304 | * visit - Function to call |
| 1305 | * arg - Extra argument to visit |
| 1306 | * Returns: 0 if all goes well, otherwise the return code from the first |
| 1307 | * call that gave non-zero result. |
| 1308 | */ |
| 1309 | static int |
| 1310 | ssl_Connection_traverse(ssl_ConnectionObj *self, visitproc visit, void *arg) |
| 1311 | { |
| 1312 | int ret = 0; |
| 1313 | |
| 1314 | if (ret == 0 && self->context != NULL) |
| 1315 | ret = visit((PyObject *)self->context, arg); |
| 1316 | if (ret == 0 && self->socket != NULL) |
| 1317 | ret = visit(self->socket, arg); |
| 1318 | if (ret == 0 && self->app_data != NULL) |
| 1319 | ret = visit(self->app_data, arg); |
| 1320 | return ret; |
| 1321 | } |
| 1322 | |
| 1323 | /* |
| 1324 | * Decref all contained objects and zero the pointers. |
| 1325 | * |
| 1326 | * Arguments: self - The Connection object |
| 1327 | * Returns: Always 0. |
| 1328 | */ |
| 1329 | static int |
| 1330 | ssl_Connection_clear(ssl_ConnectionObj *self) |
| 1331 | { |
| 1332 | Py_XDECREF(self->context); |
| 1333 | self->context = NULL; |
| 1334 | Py_XDECREF(self->socket); |
| 1335 | self->socket = NULL; |
| 1336 | Py_XDECREF(self->app_data); |
| 1337 | self->app_data = NULL; |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 1338 | self->into_ssl = NULL; /* was cleaned up by SSL_free() */ |
| 1339 | self->from_ssl = NULL; /* was cleaned up by SSL_free() */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | /* |
| 1344 | * Deallocate the memory used by the Connection object |
| 1345 | * |
| 1346 | * Arguments: self - The Connection object |
| 1347 | * Returns: None |
| 1348 | */ |
| 1349 | static void |
| 1350 | ssl_Connection_dealloc(ssl_ConnectionObj *self) |
| 1351 | { |
| 1352 | PyObject_GC_UnTrack(self); |
| 1353 | if (self->ssl != NULL) |
| 1354 | SSL_free(self->ssl); |
| 1355 | ssl_Connection_clear(self); |
| 1356 | PyObject_GC_Del(self); |
| 1357 | } |
| 1358 | |
| 1359 | PyTypeObject ssl_Connection_Type = { |
| 1360 | PyObject_HEAD_INIT(NULL) |
| 1361 | 0, |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1362 | "OpenSSL.SSL.Connection", |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1363 | sizeof(ssl_ConnectionObj), |
| 1364 | 0, |
| 1365 | (destructor)ssl_Connection_dealloc, |
| 1366 | NULL, /* print */ |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1367 | (getattrfunc)ssl_Connection_getattr, /* tp_getattr */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1368 | NULL, /* setattr */ |
| 1369 | NULL, /* compare */ |
| 1370 | NULL, /* repr */ |
| 1371 | NULL, /* as_number */ |
| 1372 | NULL, /* as_sequence */ |
| 1373 | NULL, /* as_mapping */ |
| 1374 | NULL, /* hash */ |
| 1375 | NULL, /* call */ |
| 1376 | NULL, /* str */ |
| 1377 | NULL, /* getattro */ |
| 1378 | NULL, /* setattro */ |
| 1379 | NULL, /* as_buffer */ |
| 1380 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1381 | ssl_Connection_doc, /* doc */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1382 | (traverseproc)ssl_Connection_traverse, |
| 1383 | (inquiry)ssl_Connection_clear, |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1384 | NULL, /* tp_richcompare */ |
| 1385 | 0, /* tp_weaklistoffset */ |
| 1386 | NULL, /* tp_iter */ |
| 1387 | NULL, /* tp_iternext */ |
| 1388 | ssl_Connection_methods, /* tp_methods */ |
| 1389 | NULL, /* tp_members */ |
| 1390 | NULL, /* tp_getset */ |
| 1391 | NULL, /* tp_base */ |
| 1392 | NULL, /* tp_dict */ |
| 1393 | NULL, /* tp_descr_get */ |
| 1394 | NULL, /* tp_descr_set */ |
| 1395 | 0, /* tp_dictoffset */ |
| 1396 | NULL, /* tp_init */ |
| 1397 | NULL, /* tp_alloc */ |
| 1398 | ssl_Connection_new, /* tp_new */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1399 | }; |
| 1400 | |
| 1401 | |
| 1402 | /* |
| 1403 | * Initiailze the Connection part of the SSL sub module |
| 1404 | * |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1405 | * Arguments: dict - The OpenSSL.SSL module |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1406 | * Returns: 1 for success, 0 otherwise |
| 1407 | */ |
| 1408 | int |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1409 | init_ssl_connection(PyObject *module) { |
| 1410 | |
| 1411 | if (PyType_Ready(&ssl_Connection_Type) < 0) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1412 | return 0; |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | if (PyModule_AddObject(module, "Connection", (PyObject *)&ssl_Connection_Type) != 0) { |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
| 1419 | if (PyModule_AddObject(module, "ConnectionType", (PyObject *)&ssl_Connection_Type) != 0) { |
| 1420 | return 0; |
| 1421 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1422 | |
| 1423 | return 1; |
| 1424 | } |
| 1425 | |