blob: a2075a2143f63f5e5e0e9bf6f8842ca46bf2f5a3 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * connection.c
3 *
Jean-Paul Calderone8671c852011-03-02 19:26:20 -05004 * Copyright (C) AB Strakt
5 * Copyright (C) Jean-Paul Calderone
6 * See LICENSE for details.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05007 *
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 Calderone12ea9a02008-02-22 12:24:39 -050014
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050015#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 Calderone12ea9a02008-02-22 12:24:39 -050023# include <wincrypt.h>
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050024#endif
25
Jean-Paul Calderone12ea9a02008-02-22 12:24:39 -050026#define SSL_MODULE
Jean-Paul Calderone3ad85d42009-04-30 20:24:35 -040027#include <openssl/bio.h>
Jean-Paul Calderone12ea9a02008-02-22 12:24:39 -050028#include <openssl/err.h>
Jean-Paul Calderone12ea9a02008-02-22 12:24:39 -050029#include "ssl.h"
30
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050031/**
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 */
39static void
40syscall_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 Calderoneaff0fc42009-04-27 17:13:34 -0400129 * 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 */
135static void
136handle_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 Deand369c932009-07-08 11:48:33 -0500168 exception_from_error_queue(ssl_Error);
Jean-Paul Calderoneaff0fc42009-04-27 17:13:34 -0400169 }
170}
171
172/*
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500173 * 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 */
180static void
181handle_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 Deand369c932009-07-08 11:48:33 -0500242 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500243 break;
244 }
245}
246
247/*
248 * Here be member methods of the Connection "class"
249 */
250
251static char ssl_Connection_get_context_doc[] = "\n\
252Get session context\n\
253\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900254:return: A Context object\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500255";
256static PyObject *
Jean-Paul Calderone1d69a722010-07-29 09:05:53 -0400257ssl_Connection_get_context(ssl_ConnectionObj *self, PyObject *args) {
258 if (!PyArg_ParseTuple(args, ":get_context")) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500259 return NULL;
Jean-Paul Calderone1d69a722010-07-29 09:05:53 -0400260 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500261
262 Py_INCREF(self->context);
263 return (PyObject *)self->context;
264}
265
Jean-Paul Calderone95613b72011-05-25 22:30:21 -0400266static char ssl_Connection_set_context_doc[] = "\n\
267Switch this connection to a new session context\n\
268\n\
Jonathan Ballet648875f2011-07-16 14:14:58 +0900269:param context: A :py:class:`Context` instance giving the new session context to use.\n\
Jean-Paul Calderone95613b72011-05-25 22:30:21 -0400270\n\
271";
272static PyObject *
273ssl_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 Calderonec4cb6582011-05-26 18:47:00 -0400304static char ssl_Connection_get_servername_doc[] = "\n\
305Retrieve the servername extension value if provided in the client hello\n\
306message, or None if there wasn't one.\n\
307\n\
Jonathan Ballet648875f2011-07-16 14:14:58 +0900308:return: A byte string giving the server name or :py:data:`None`.\n\
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -0400309\n\
310";
311static PyObject *
312ssl_Connection_get_servername(ssl_ConnectionObj *self, PyObject *args) {
313 int type = TLSEXT_NAMETYPE_host_name;
314 const char *name;
315
Jean-Paul Calderone871a4d82011-05-26 19:24:02 -0400316 if (!PyArg_ParseTuple(args, ":get_servername")) {
317 return NULL;
318 }
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -0400319
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
331static char ssl_Connection_set_tlsext_host_name_doc[] = "\n\
332Set the value of the servername extension to send in the client hello.\n\
333\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900334:param name: A byte string giving the name.\n\
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -0400335\n\
336";
337static PyObject *
338ssl_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 Calderone95613b72011-05-25 22:30:21 -0400353
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500354static char ssl_Connection_pending_doc[] = "\n\
355Get the number of bytes that can be safely read from the connection\n\
356\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900357:return: The number of bytes available in the receive buffer.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500358";
359static PyObject *
Jean-Paul Calderone1d69a722010-07-29 09:05:53 -0400360ssl_Connection_pending(ssl_ConnectionObj *self, PyObject *args) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500361 int ret;
362
Jean-Paul Calderone1d69a722010-07-29 09:05:53 -0400363 if (!PyArg_ParseTuple(args, ":pending")) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500364 return NULL;
Jean-Paul Calderone1d69a722010-07-29 09:05:53 -0400365 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500366
367 ret = SSL_pending(self->ssl);
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400368 return PyLong_FromLong((long)ret);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500369}
Jean-Paul Calderone1d69a722010-07-29 09:05:53 -0400370
Rick Deanb71c0d22009-04-01 14:09:23 -0500371static char ssl_Connection_bio_write_doc[] = "\n\
372When using non-socket connections this function sends\n\
373\"dirty\" data that would have traveled in on the network.\n\
374\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900375:param buf: The string to put into the memory BIO.\n\
376:return: The number of bytes written\n\
Rick Deanb71c0d22009-04-01 14:09:23 -0500377";
378static PyObject *
379ssl_Connection_bio_write(ssl_ConnectionObj *self, PyObject *args)
380{
381 char *buf;
Jean-Paul Calderoneaff0fc42009-04-27 17:13:34 -0400382 int len, ret;
Rick Deanb71c0d22009-04-01 14:09:23 -0500383
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -0400384 if (self->into_ssl == NULL)
Rick Deanb71c0d22009-04-01 14:09:23 -0500385 {
386 PyErr_SetString(PyExc_TypeError, "Connection sock was not None");
387 return NULL;
388 }
389
Jean-Paul Calderonefc4ed0f2009-04-27 11:51:27 -0400390 if (!PyArg_ParseTuple(args, "s#|i:bio_write", &buf, &len))
Rick Deanb71c0d22009-04-01 14:09:23 -0500391 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 Calderoneaff0fc42009-04-27 17:13:34 -0400401 if (ret <= 0) {
402 /*
403 * There was a problem with the BIO_write of some sort.
404 */
Jean-Paul Calderone3ad85d42009-04-30 20:24:35 -0400405 handle_bio_errors(self->into_ssl, ret);
Rick Deanb71c0d22009-04-01 14:09:23 -0500406 return NULL;
407 }
Jean-Paul Calderoneaff0fc42009-04-27 17:13:34 -0400408
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400409 return PyLong_FromLong((long)ret);
Rick Deanb71c0d22009-04-01 14:09:23 -0500410}
411
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500412static char ssl_Connection_send_doc[] = "\n\
413Send data on the connection. NOTE: If you get one of the WantRead,\n\
414WantWrite or WantX509Lookup exceptions on this, you have to call the\n\
415method again with the SAME buffer.\n\
416\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900417:param buf: The string to send\n\
418:param flags: (optional) Included for compatibility with the socket\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400419 API, the value is ignored\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900420:return: The number of bytes written\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500421";
422static PyObject *
Jean-Paul Calderonea5d6ea62011-01-05 19:57:08 -0500423ssl_Connection_send(ssl_ConnectionObj *self, PyObject *args) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500424 int len, ret, err, flags;
Jean-Paul Calderonea5d6ea62011-01-05 19:57:08 -0500425 char *buf;
426
Jean-Paul Calderone5163f572011-04-22 18:38:16 -0400427#if PY_VERSION_HEX >= 0x02060000
Jean-Paul Calderonea5d6ea62011-01-05 19:57:08 -0500428
Jean-Paul Calderone294dc1e2012-03-10 16:55:52 -0800429 Py_buffer *pbuf = PyMem_Malloc(sizeof *pbuf);
430
431 if (!PyArg_ParseTuple(args, "s*|i:send", pbuf, &flags)) {
Jean-Paul Calderonea5d6ea62011-01-05 19:57:08 -0500432 return NULL;
Jean-Paul Calderone294dc1e2012-03-10 16:55:52 -0800433 }
Jean-Paul Calderonea5d6ea62011-01-05 19:57:08 -0500434
Jean-Paul Calderone294dc1e2012-03-10 16:55:52 -0800435 buf = pbuf->buf;
436 len = pbuf->len;
Jean-Paul Calderonea5d6ea62011-01-05 19:57:08 -0500437#else
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500438
Jean-Paul Calderone294dc1e2012-03-10 16:55:52 -0800439 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags)) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500440 return NULL;
Jean-Paul Calderone294dc1e2012-03-10 16:55:52 -0800441 }
Jean-Paul Calderonea5d6ea62011-01-05 19:57:08 -0500442#endif
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500443
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 Calderone5163f572011-04-22 18:38:16 -0400448#if PY_VERSION_HEX >= 0x02060000
Jean-Paul Calderone294dc1e2012-03-10 16:55:52 -0800449 PyBuffer_Release(pbuf);
450 PyMem_Free(pbuf);
Jean-Paul Calderonea5d6ea62011-01-05 19:57:08 -0500451#endif
452
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500453 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 Calderone83dbcfd2010-08-11 20:20:57 -0400462 return PyLong_FromLong((long)ret);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500463 }
464 else
465 {
466 handle_ssl_errors(self->ssl, err, ret);
467 return NULL;
468 }
469}
470
471static char ssl_Connection_sendall_doc[] = "\n\
472Send \"all\" data on the connection. This calls send() repeatedly until\n\
473all data is sent. If an error occurs, it's impossible to tell how much data\n\
474has been sent.\n\
475\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900476:param buf: The string to send\n\
477:param flags: (optional) Included for compatibility with the socket\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400478 API, the value is ignored\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900479:return: The number of bytes written\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500480";
481static PyObject *
482ssl_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 Calderone5163f572011-04-22 18:38:16 -0400488#if PY_VERSION_HEX >= 0x02060000
Jean-Paul Calderone294dc1e2012-03-10 16:55:52 -0800489 Py_buffer *pbuf = PyMem_Malloc(sizeof *pbuf);
Jean-Paul Calderone691e6c92011-01-21 22:04:35 -0500490
Jean-Paul Calderone294dc1e2012-03-10 16:55:52 -0800491 if (!PyArg_ParseTuple(args, "s*|i:sendall", pbuf, &flags)) {
Jean-Paul Calderone691e6c92011-01-21 22:04:35 -0500492 return NULL;
Jean-Paul Calderone294dc1e2012-03-10 16:55:52 -0800493 }
Jean-Paul Calderone691e6c92011-01-21 22:04:35 -0500494
Jean-Paul Calderone294dc1e2012-03-10 16:55:52 -0800495 buf = pbuf->buf;
496 len = pbuf->len;
Jean-Paul Calderone691e6c92011-01-21 22:04:35 -0500497#else
Jean-Paul Calderone294dc1e2012-03-10 16:55:52 -0800498 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags)) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500499 return NULL;
Jean-Paul Calderone294dc1e2012-03-10 16:55:52 -0800500 }
Jean-Paul Calderone691e6c92011-01-21 22:04:35 -0500501#endif
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500502
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 Calderone691e6c92011-01-21 22:04:35 -0500525 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500526 } while (len > 0);
527
Jean-Paul Calderone5163f572011-04-22 18:38:16 -0400528#if PY_VERSION_HEX >= 0x02060000
Jean-Paul Calderone294dc1e2012-03-10 16:55:52 -0800529 PyBuffer_Release(pbuf);
530 PyMem_Free(pbuf);
Jean-Paul Calderone691e6c92011-01-21 22:04:35 -0500531#endif
532
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500533 Py_XINCREF(pyret);
534 return pyret;
535}
536
537static char ssl_Connection_recv_doc[] = "\n\
538Receive data on the connection. NOTE: If you get one of the WantRead,\n\
539WantWrite or WantX509Lookup exceptions on this, you have to call the\n\
540method again with the SAME buffer.\n\
541\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900542:param bufsiz: The maximum number of bytes to read\n\
543:param flags: (optional) Included for compatibility with the socket\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400544 API, the value is ignored\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900545:return: The string read from the Connection\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500546";
547static PyObject *
548ssl_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 Calderone83dbcfd2010-08-11 20:20:57 -0400556 buf = PyBytes_FromStringAndSize(NULL, bufsiz);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500557 if (buf == NULL)
558 return NULL;
559
560 MY_BEGIN_ALLOW_THREADS(self->tstate)
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400561 ret = SSL_read(self->ssl, PyBytes_AsString(buf), bufsiz);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500562 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 Calderone83dbcfd2010-08-11 20:20:57 -0400574 if (ret != bufsiz && _PyBytes_Resize(&buf, ret) < 0)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500575 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 Deanb71c0d22009-04-01 14:09:23 -0500586static char ssl_Connection_bio_read_doc[] = "\n\
587When using non-socket connections this function reads\n\
588the \"dirty\" data that would have traveled away on the network.\n\
589\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900590:param bufsiz: The maximum number of bytes to read\n\
591:return: The string read.\n\
Rick Deanb71c0d22009-04-01 14:09:23 -0500592";
593static PyObject *
594ssl_Connection_bio_read(ssl_ConnectionObj *self, PyObject *args)
595{
Jean-Paul Calderoneaff0fc42009-04-27 17:13:34 -0400596 int bufsiz, ret;
Rick Deanb71c0d22009-04-01 14:09:23 -0500597 PyObject *buf;
598
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -0400599 if (self->from_ssl == NULL)
Rick Deanb71c0d22009-04-01 14:09:23 -0500600 {
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 Calderone83dbcfd2010-08-11 20:20:57 -0400608 buf = PyBytes_FromStringAndSize(NULL, bufsiz);
Rick Deanb71c0d22009-04-01 14:09:23 -0500609 if (buf == NULL)
610 return NULL;
611
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400612 ret = BIO_read(self->from_ssl, PyBytes_AsString(buf), bufsiz);
Rick Deanb71c0d22009-04-01 14:09:23 -0500613
614 if (PyErr_Occurred())
615 {
616 Py_DECREF(buf);
617 flush_error_queue();
618 return NULL;
619 }
620
Jean-Paul Calderoneaff0fc42009-04-27 17:13:34 -0400621 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 Deanb71c0d22009-04-01 14:09:23 -0500626 Py_DECREF(buf);
627 return NULL;
628 }
Jean-Paul Calderoneaff0fc42009-04-27 17:13:34 -0400629
630 /*
631 * Shrink the string to match the number of bytes we actually read.
632 */
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400633 if (ret != bufsiz && _PyBytes_Resize(&buf, ret) < 0)
Jean-Paul Calderoneaff0fc42009-04-27 17:13:34 -0400634 {
635 Py_DECREF(buf);
636 return NULL;
637 }
638 return buf;
Rick Deanb71c0d22009-04-01 14:09:23 -0500639}
640
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500641static char ssl_Connection_renegotiate_doc[] = "\n\
642Renegotiate the session\n\
643\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900644:return: True if the renegotiation can be started, false otherwise\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500645";
646static PyObject *
Jean-Paul Calderone8ea22522010-07-29 09:39:39 -0400647ssl_Connection_renegotiate(ssl_ConnectionObj *self, PyObject *args) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500648 int ret;
649
Jean-Paul Calderone8ea22522010-07-29 09:39:39 -0400650 if (!PyArg_ParseTuple(args, ":renegotiate")) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500651 return NULL;
Jean-Paul Calderone8ea22522010-07-29 09:39:39 -0400652 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500653
654 MY_BEGIN_ALLOW_THREADS(self->tstate);
655 ret = SSL_renegotiate(self->ssl);
656 MY_END_ALLOW_THREADS(self->tstate);
657
Jean-Paul Calderone8ea22522010-07-29 09:39:39 -0400658 if (PyErr_Occurred()) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500659 flush_error_queue();
660 return NULL;
661 }
662
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400663 return PyLong_FromLong((long)ret);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500664}
665
666static char ssl_Connection_do_handshake_doc[] = "\n\
667Perform an SSL handshake (usually called after renegotiate() or one of\n\
668set_*_state()). This can raise the same exceptions as send and recv.\n\
669\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900670:return: None.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500671";
672static PyObject *
673ssl_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
704static char ssl_Connection_renegotiate_pending_doc[] = "\n\
705Check if there's a renegotiation in progress, it will return false once\n\
706a renegotiation is finished.\n\
707\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900708:return: Whether there's a renegotiation in progress\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500709";
710static PyObject *
711ssl_Connection_renegotiate_pending(ssl_ConnectionObj *self, PyObject *args)
712{
713 if (!PyArg_ParseTuple(args, ":renegotiate_pending"))
714 return NULL;
715
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400716 return PyLong_FromLong((long)SSL_renegotiate_pending(self->ssl));
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500717}
718#endif
719
720static char ssl_Connection_total_renegotiations_doc[] = "\n\
721Find out the total number of renegotiations.\n\
722\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900723:return: The number of renegotiations.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500724";
725static PyObject *
726ssl_Connection_total_renegotiations(ssl_ConnectionObj *self, PyObject *args)
727{
728 if (!PyArg_ParseTuple(args, ":total_renegotiations"))
729 return NULL;
730
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400731 return PyLong_FromLong(SSL_total_renegotiations(self->ssl));
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500732}
733
734static char ssl_Connection_set_accept_state_doc[] = "\n\
735Set the connection to work in server mode. The handshake will be handled\n\
736automatically by read/write.\n\
737\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900738:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500739";
740static PyObject *
741ssl_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
752static char ssl_Connection_set_connect_state_doc[] = "\n\
753Set the connection to work in client mode. The handshake will be handled\n\
754automatically by read/write.\n\
755\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900756:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500757";
758static PyObject *
759ssl_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
770static char ssl_Connection_connect_doc[] = "\n\
771Connect to remote host and set up client-side SSL\n\
772\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900773:param addr: A remote address\n\
774:return: What the socket's connect method returns\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500775";
776static PyObject *
777ssl_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
794static char ssl_Connection_connect_ex_doc[] = "\n\
795Connect to remote host and set up client-side SSL. Note that if the socket's\n\
796connect_ex method doesn't return 0, SSL won't be initialized.\n\
797\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900798:param addr: A remove address\n\
799:return: What the socket's connect_ex method returns\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500800";
801static PyObject *
802ssl_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 Calderone897bc252008-02-18 20:50:23 -0500813 return ret;
814}
815
816static char ssl_Connection_accept_doc[] = "\n\
817Accept incoming connection and set up SSL on it\n\
818\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900819:return: A (conn,addr) pair where conn is a Connection and addr is an\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400820 address\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500821";
822static PyObject *
823ssl_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 Calderone3ad85d42009-04-30 20:24:35 -0400859static char ssl_Connection_bio_shutdown_doc[] = "\n\
860When using non-socket connections this function signals end of\n\
861data on the input for this connection.\n\
862\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900863:return: None\n\
Jean-Paul Calderone3ad85d42009-04-30 20:24:35 -0400864";
865
866static PyObject *
867ssl_Connection_bio_shutdown(ssl_ConnectionObj *self, PyObject *args)
868{
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -0400869 if (self->from_ssl == NULL)
870 {
871 PyErr_SetString(PyExc_TypeError, "Connection sock was not None");
872 return NULL;
873 }
874
Jean-Paul Calderone3ad85d42009-04-30 20:24:35 -0400875 BIO_set_mem_eof_return(self->into_ssl, 0);
876 Py_INCREF(Py_None);
877 return Py_None;
878}
879
880
881
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500882static char ssl_Connection_shutdown_doc[] = "\n\
883Send closure alert\n\
884\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900885:return: True if the shutdown completed successfully (i.e. both sides\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400886 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 Calderone897bc252008-02-18 20:50:23 -0500888";
889static PyObject *
890ssl_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 Deand369c932009-07-08 11:48:33 -0500909 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500910 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
924static char ssl_Connection_get_cipher_list_doc[] = "\n\
925Get the session cipher list\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500926\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900927:return: A list of cipher strings\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500928";
929static PyObject *
930ssl_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 Calderone83dbcfd2010-08-11 20:20:57 -0400942 item = PyText_FromString(ret);
Ziga Seilnachtfdeadb12009-09-01 16:35:50 +0200943 PyList_Append(lst, item);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500944 Py_DECREF(item);
945 idx++;
946 }
947 return lst;
948}
949
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200950static char ssl_Connection_get_client_ca_list_doc[] = "\n\
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200951Get CAs whose certificates are suggested for client authentication.\n\
952\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900953:return: If this is a server connection, a list of X509Names representing\n\
Jonathan Ballet648875f2011-07-16 14:14:58 +0900954 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 Calderone35788902009-10-24 13:48:08 -0400956 the list of such X509Names sent by the server, or an empty list if that\n\
957 has not yet happened.\n\
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200958";
959
960static PyObject *
Jean-Paul Calderone6e2b6852009-10-24 14:04:30 -0400961ssl_Connection_get_client_ca_list(ssl_ConnectionObj *self, PyObject *args) {
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200962 STACK_OF(X509_NAME) *CANames;
963 PyObject *CAList;
964 int i, n;
965
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200966 if (!PyArg_ParseTuple(args, ":get_client_ca_list")) {
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200967 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 Calderone305626a2010-10-31 20:51:17 -0400988 CA = (PyObject *)new_x509name(CAName, 1);
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200989 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 Calderone897bc252008-02-18 20:50:23 -05001003static char ssl_Connection_makefile_doc[] = "\n\
1004The makefile() method is not implemented, since there is no dup semantics\n\
1005for SSL connections\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001006\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001007:raise NotImplementedError\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001008";
1009static PyObject *
1010ssl_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
1016static char ssl_Connection_get_app_data_doc[] = "\n\
1017Get application data\n\
1018\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001019:return: The application data\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001020";
1021static PyObject *
1022ssl_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
1031static char ssl_Connection_set_app_data_doc[] = "\n\
1032Set application data\n\
1033\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001034:param data - The application data\n\
1035:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001036";
1037static PyObject *
1038ssl_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 Calderone72b8f0f2008-02-21 23:57:40 -05001053static char ssl_Connection_get_shutdown_doc[] = "\n\
1054Get shutdown state\n\
1055\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001056:return: The shutdown state, a bitvector of SENT_SHUTDOWN, RECEIVED_SHUTDOWN.\n\
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001057";
1058static PyObject *
1059ssl_Connection_get_shutdown(ssl_ConnectionObj *self, PyObject *args)
1060{
1061 if (!PyArg_ParseTuple(args, ":get_shutdown"))
1062 return NULL;
1063
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001064 return PyLong_FromLong((long)SSL_get_shutdown(self->ssl));
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001065}
1066
1067static char ssl_Connection_set_shutdown_doc[] = "\n\
1068Set shutdown state\n\
1069\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001070:param state - bitvector of SENT_SHUTDOWN, RECEIVED_SHUTDOWN.\n\
1071:return: None\n\
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001072";
1073static PyObject *
1074ssl_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 Calderone897bc252008-02-18 20:50:23 -05001086static char ssl_Connection_state_string_doc[] = "\n\
1087Get a verbose state description\n\
1088\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001089:return: A string representing the state\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001090";
1091static PyObject *
1092ssl_Connection_state_string(ssl_ConnectionObj *self, PyObject *args)
1093{
1094 if (!PyArg_ParseTuple(args, ":state_string"))
1095 return NULL;
1096
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001097 return PyText_FromString(SSL_state_string_long(self->ssl));
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001098}
1099
Rick Deanb71c0d22009-04-01 14:09:23 -05001100static char ssl_Connection_client_random_doc[] = "\n\
1101Get a copy of the client hello nonce.\n\
1102\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001103:return: A string representing the state\n\
Rick Deanb71c0d22009-04-01 14:09:23 -05001104";
1105static PyObject *
1106ssl_Connection_client_random(ssl_ConnectionObj *self, PyObject *args)
1107{
1108 if (!PyArg_ParseTuple(args, ":client_random"))
1109 return NULL;
1110
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -04001111 if (self->ssl->session == NULL) {
Rick Deanb71c0d22009-04-01 14:09:23 -05001112 Py_INCREF(Py_None);
1113 return Py_None;
1114 }
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001115 return PyBytes_FromStringAndSize( (const char *) self->ssl->s3->client_random, SSL3_RANDOM_SIZE);
Rick Deanb71c0d22009-04-01 14:09:23 -05001116}
1117
1118static char ssl_Connection_server_random_doc[] = "\n\
1119Get a copy of the server hello nonce.\n\
1120\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001121:return: A string representing the state\n\
Rick Deanb71c0d22009-04-01 14:09:23 -05001122";
1123static PyObject *
1124ssl_Connection_server_random(ssl_ConnectionObj *self, PyObject *args)
1125{
1126 if (!PyArg_ParseTuple(args, ":server_random"))
1127 return NULL;
1128
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -04001129 if (self->ssl->session == NULL) {
Rick Deanb71c0d22009-04-01 14:09:23 -05001130 Py_INCREF(Py_None);
1131 return Py_None;
1132 }
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001133 return PyBytes_FromStringAndSize( (const char *) self->ssl->s3->server_random, SSL3_RANDOM_SIZE);
Rick Deanb71c0d22009-04-01 14:09:23 -05001134}
1135
1136static char ssl_Connection_master_key_doc[] = "\n\
1137Get a copy of the master key.\n\
1138\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001139:return: A string representing the state\n\
Rick Deanb71c0d22009-04-01 14:09:23 -05001140";
1141static PyObject *
1142ssl_Connection_master_key(ssl_ConnectionObj *self, PyObject *args)
1143{
1144 if (!PyArg_ParseTuple(args, ":master_key"))
1145 return NULL;
1146
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -04001147 if (self->ssl->session == NULL) {
Rick Deanb71c0d22009-04-01 14:09:23 -05001148 Py_INCREF(Py_None);
1149 return Py_None;
1150 }
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001151 return PyBytes_FromStringAndSize( (const char *) self->ssl->session->master_key, self->ssl->session->master_key_length);
Rick Deanb71c0d22009-04-01 14:09:23 -05001152}
1153
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001154static char ssl_Connection_sock_shutdown_doc[] = "\n\
1155See shutdown(2)\n\
1156\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001157:return: What the socket's shutdown() method returns\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001158";
1159static PyObject *
1160ssl_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
1171static char ssl_Connection_get_peer_certificate_doc[] = "\n\
1172Retrieve the other side's certificate (if any)\n\
1173\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001174:return: The peer's certificate\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001175";
1176static PyObject *
1177ssl_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 Calderone5c0f1f62010-10-31 21:36:43 -04001187 return (PyObject *)new_x509(cert, 1);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001188 }
1189 else
1190 {
1191 Py_INCREF(Py_None);
1192 return Py_None;
1193 }
1194}
1195
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001196static char ssl_Connection_get_peer_cert_chain_doc[] = "\n\
1197Retrieve the other side's certificate (if any)\n\
1198\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001199:return: A list of X509 instances giving the peer's certificate chain,\n\
Jean-Paul Calderone0a7e06a2011-05-19 18:49:35 -04001200 or None if it does not have one.\n\
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001201";
1202static PyObject *
Jean-Paul Calderonefcced2a2011-05-19 18:02:11 -04001203ssl_Connection_get_peer_cert_chain(ssl_ConnectionObj *self, PyObject *args) {
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001204 STACK_OF(X509) *sk;
Jean-Paul Calderone0a7e06a2011-05-19 18:49:35 -04001205 PyObject *chain;
1206 crypto_X509Obj *cert;
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001207 Py_ssize_t i;
1208
Jean-Paul Calderonefcced2a2011-05-19 18:02:11 -04001209 if (!PyArg_ParseTuple(args, ":get_peer_cert_chain")) {
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001210 return NULL;
Jean-Paul Calderonefcced2a2011-05-19 18:02:11 -04001211 }
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001212
1213 sk = SSL_get_peer_cert_chain(self->ssl);
Jean-Paul Calderonefcced2a2011-05-19 18:02:11 -04001214 if (sk != NULL) {
Jean-Paul Calderone0a7e06a2011-05-19 18:49:35 -04001215 chain = PyList_New(sk_X509_num(sk));
Jean-Paul Calderonefcced2a2011-05-19 18:02:11 -04001216 for (i = 0; i < sk_X509_num(sk); i++) {
Jean-Paul Calderone13d190b2011-05-20 19:14:50 -04001217 cert = new_x509(sk_X509_value(sk, i), 1);
Jean-Paul Calderone0a7e06a2011-05-19 18:49:35 -04001218 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 Calderone95b92c72011-05-17 15:45:21 -04001225 }
Jean-Paul Calderone0a7e06a2011-05-19 18:49:35 -04001226 return chain;
Jean-Paul Calderonefcced2a2011-05-19 18:02:11 -04001227 } else {
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001228 Py_INCREF(Py_None);
1229 return Py_None;
1230 }
1231
1232}
1233
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001234static char ssl_Connection_want_read_doc[] = "\n\
1235Checks if more data has to be read from the transport layer to complete an\n\
1236operation.\n\
1237\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001238:return: True iff more data has to be read\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001239";
1240static PyObject *
1241ssl_Connection_want_read(ssl_ConnectionObj *self, PyObject *args)
1242{
1243 if (!PyArg_ParseTuple(args, ":want_read"))
1244 return NULL;
1245
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001246 return PyLong_FromLong((long)SSL_want_read(self->ssl));
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001247}
1248
1249static char ssl_Connection_want_write_doc[] = "\n\
1250Checks if there is data to write to the transport layer to complete an\n\
1251operation.\n\
1252\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001253:return: True iff there is data to write\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001254";
1255static PyObject *
1256ssl_Connection_want_write(ssl_ConnectionObj *self, PyObject *args)
1257{
1258 if (!PyArg_ParseTuple(args, ":want_write"))
1259 return NULL;
1260
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001261 return PyLong_FromLong((long)SSL_want_write(self->ssl));
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001262}
1263
Jean-Paul Calderone64eaffc2012-02-13 11:53:49 -05001264static char ssl_Connection_get_session_doc[] = "\n\
1265Returns 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";
1270static PyObject *
1271ssl_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 Calderonefef5c4b2012-02-14 16:31:52 -05001295static char ssl_Connection_set_session_doc[] = "\n\
1296Set 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";
1301static PyObject *
1302ssl_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 Calderone5ea41492012-02-14 16:51:35 -05001310 /* 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 Calderonefef5c4b2012-02-14 16:31:52 -05001312 */
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 Calderone897bc252008-02-18 20:50:23 -05001321/*
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 }
1333static PyMethodDef ssl_Connection_methods[] =
1334{
1335 ADD_METHOD(get_context),
Jean-Paul Calderone95613b72011-05-25 22:30:21 -04001336 ADD_METHOD(set_context),
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -04001337 ADD_METHOD(get_servername),
1338 ADD_METHOD(set_tlsext_host_name),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001339 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 Deanb71c0d22009-04-01 14:09:23 -05001345 ADD_METHOD(bio_read),
1346 ADD_METHOD(bio_write),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001347 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 Calderone3ad85d42009-04-30 20:24:35 -04001356 ADD_METHOD(bio_shutdown),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001357 ADD_METHOD(shutdown),
1358 ADD_METHOD(get_cipher_list),
Ziga Seilnachtf93bf102009-10-23 09:51:07 +02001359 ADD_METHOD(get_client_ca_list),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001360 ADD_METHOD(makefile),
1361 ADD_METHOD(get_app_data),
1362 ADD_METHOD(set_app_data),
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001363 ADD_METHOD(get_shutdown),
1364 ADD_METHOD(set_shutdown),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001365 ADD_METHOD(state_string),
Rick Deanb71c0d22009-04-01 14:09:23 -05001366 ADD_METHOD(server_random),
1367 ADD_METHOD(client_random),
1368 ADD_METHOD(master_key),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001369 ADD_METHOD(sock_shutdown),
1370 ADD_METHOD(get_peer_certificate),
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001371 ADD_METHOD(get_peer_cert_chain),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001372 ADD_METHOD(want_read),
1373 ADD_METHOD(want_write),
1374 ADD_METHOD(set_accept_state),
1375 ADD_METHOD(set_connect_state),
Jean-Paul Calderone64eaffc2012-02-13 11:53:49 -05001376 ADD_METHOD(get_session),
Jean-Paul Calderonefef5c4b2012-02-14 16:31:52 -05001377 ADD_METHOD(set_session),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001378 { NULL, NULL }
1379};
1380#undef ADD_ALIAS
1381#undef ADD_METHOD
1382
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001383static char ssl_Connection_doc[] = "\n\
1384Connection(context, socket) -> Connection instance\n\
1385\n\
1386Create a new Connection object, using the given OpenSSL.SSL.Context instance\n\
1387and socket.\n\
1388\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001389:param context: An SSL Context to use for this connection\n\
1390:param socket: The socket to use for transport layer\n\
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001391";
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001392
1393/*
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001394 * 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 Calderone897bc252008-02-18 20:50:23 -05001397 */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001398static ssl_ConnectionObj*
1399ssl_Connection_init(ssl_ConnectionObj *self, ssl_ContextObj *ctx, PyObject *sock) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001400 int fd;
1401
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001402 Py_INCREF(ctx);
1403 self->context = ctx;
1404
1405 Py_INCREF(sock);
1406 self->socket = sock;
1407
1408 self->ssl = NULL;
Jean-Paul Calderonefc4ed0f2009-04-27 11:51:27 -04001409 self->from_ssl = NULL;
1410 self->into_ssl = NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001411
1412 Py_INCREF(Py_None);
1413 self->app_data = Py_None;
1414
1415 self->tstate = NULL;
1416
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001417 self->ssl = SSL_new(self->context->ctx);
1418 SSL_set_app_data(self->ssl, self);
Rick Deanb71c0d22009-04-01 14:09:23 -05001419
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -04001420 if (self->socket == Py_None)
Rick Deanb71c0d22009-04-01 14:09:23 -05001421 {
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 Calderone07acf3f2009-05-05 13:23:28 -04001426 if (self->into_ssl == NULL || self->from_ssl == NULL)
Rick Deanb71c0d22009-04-01 14:09:23 -05001427 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 Calderone897bc252008-02-18 20:50:23 -05001443 return self;
Rick Deanb71c0d22009-04-01 14:09:23 -05001444
1445error:
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 Calderone897bc252008-02-18 20:50:23 -05001450}
1451
1452/*
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001453 * 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 */
1459ssl_ConnectionObj *
1460ssl_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
1475static PyObject*
1476ssl_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 Calderone897bc252008-02-18 20:50:23 -05001496 * 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 */
1503static PyObject *
Jean-Paul Calderone997be7e2010-08-11 22:40:07 -04001504ssl_Connection_getattro(ssl_ConnectionObj *self, PyObject *nameobj) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001505 PyObject *meth;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001506
Jean-Paul Calderone997be7e2010-08-11 22:40:07 -04001507 meth = PyObject_GenericGetAttr((PyObject*)self, nameobj);
1508 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001509 PyErr_Clear();
1510 /* Try looking it up in the "socket" instead. */
Jean-Paul Calderone997be7e2010-08-11 22:40:07 -04001511 meth = PyObject_GenericGetAttr(self->socket, nameobj);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001512 }
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 */
1526static int
1527ssl_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 */
1546static int
1547ssl_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 Deanb71c0d22009-04-01 14:09:23 -05001555 self->into_ssl = NULL; /* was cleaned up by SSL_free() */
1556 self->from_ssl = NULL; /* was cleaned up by SSL_free() */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001557 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 */
1566static void
1567ssl_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
1576PyTypeObject ssl_Connection_Type = {
Jean-Paul Calderoneb6d75252010-08-11 23:55:45 -04001577 PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001578 "OpenSSL.SSL.Connection",
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001579 sizeof(ssl_ConnectionObj),
1580 0,
1581 (destructor)ssl_Connection_dealloc,
1582 NULL, /* print */
Jean-Paul Calderone997be7e2010-08-11 22:40:07 -04001583 NULL, /* tp_getattr */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001584 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 Calderone997be7e2010-08-11 22:40:07 -04001593 (getattrofunc)ssl_Connection_getattro, /* getattro */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001594 NULL, /* setattro */
1595 NULL, /* as_buffer */
1596 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001597 ssl_Connection_doc, /* doc */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001598 (traverseproc)ssl_Connection_traverse,
1599 (inquiry)ssl_Connection_clear,
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001600 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 Calderone897bc252008-02-18 20:50:23 -05001615};
1616
1617
1618/*
1619 * Initiailze the Connection part of the SSL sub module
1620 *
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001621 * Arguments: dict - The OpenSSL.SSL module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001622 * Returns: 1 for success, 0 otherwise
1623 */
1624int
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001625init_ssl_connection(PyObject *module) {
1626
1627 if (PyType_Ready(&ssl_Connection_Type) < 0) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001628 return 0;
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001629 }
1630
Jean-Paul Calderone86ad7112010-05-11 16:08:45 -04001631 /* PyModule_AddObject steals a reference.
1632 */
1633 Py_INCREF((PyObject *)&ssl_Connection_Type);
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001634 if (PyModule_AddObject(module, "Connection", (PyObject *)&ssl_Connection_Type) != 0) {
1635 return 0;
1636 }
1637
Jean-Paul Calderoneaed23582011-03-12 22:45:02 -05001638 /* PyModule_AddObject steals a reference.
1639 */
1640 Py_INCREF((PyObject *)&ssl_Connection_Type);
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001641 if (PyModule_AddObject(module, "ConnectionType", (PyObject *)&ssl_Connection_Type) != 0) {
1642 return 0;
1643 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001644
1645 return 1;
1646}
1647