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