blob: f7994a382ed9c55ad66daf8eedf4a8adc8ace015 [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 Py_buffer pbuf;
429
430 if (!PyArg_ParseTuple(args, "s*|i:send", &pbuf, &flags))
431 return NULL;
432
433 buf = pbuf.buf;
434 len = pbuf.len;
435#else
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500436
437 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
438 return NULL;
Jean-Paul Calderonea5d6ea62011-01-05 19:57:08 -0500439#endif
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500440
441 MY_BEGIN_ALLOW_THREADS(self->tstate)
442 ret = SSL_write(self->ssl, buf, len);
443 MY_END_ALLOW_THREADS(self->tstate)
444
Jean-Paul Calderone5163f572011-04-22 18:38:16 -0400445#if PY_VERSION_HEX >= 0x02060000
Jean-Paul Calderonea5d6ea62011-01-05 19:57:08 -0500446 PyBuffer_Release(&pbuf);
447#endif
448
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500449 if (PyErr_Occurred())
450 {
451 flush_error_queue();
452 return NULL;
453 }
454
455 err = SSL_get_error(self->ssl, ret);
456 if (err == SSL_ERROR_NONE)
457 {
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400458 return PyLong_FromLong((long)ret);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500459 }
460 else
461 {
462 handle_ssl_errors(self->ssl, err, ret);
463 return NULL;
464 }
465}
466
467static char ssl_Connection_sendall_doc[] = "\n\
468Send \"all\" data on the connection. This calls send() repeatedly until\n\
469all data is sent. If an error occurs, it's impossible to tell how much data\n\
470has been sent.\n\
471\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900472:param buf: The string to send\n\
473:param flags: (optional) Included for compatibility with the socket\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400474 API, the value is ignored\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900475:return: The number of bytes written\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500476";
477static PyObject *
478ssl_Connection_sendall(ssl_ConnectionObj *self, PyObject *args)
479{
480 char *buf;
481 int len, ret, err, flags;
482 PyObject *pyret = Py_None;
483
Jean-Paul Calderone5163f572011-04-22 18:38:16 -0400484#if PY_VERSION_HEX >= 0x02060000
Jean-Paul Calderone691e6c92011-01-21 22:04:35 -0500485 Py_buffer pbuf;
486
487 if (!PyArg_ParseTuple(args, "s*|i:sendall", &pbuf, &flags))
488 return NULL;
489
490 buf = pbuf.buf;
491 len = pbuf.len;
492#else
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500493 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
494 return NULL;
Jean-Paul Calderone691e6c92011-01-21 22:04:35 -0500495#endif
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500496
497 do {
498 MY_BEGIN_ALLOW_THREADS(self->tstate)
499 ret = SSL_write(self->ssl, buf, len);
500 MY_END_ALLOW_THREADS(self->tstate)
501 if (PyErr_Occurred())
502 {
503 flush_error_queue();
504 pyret = NULL;
505 break;
506 }
507 err = SSL_get_error(self->ssl, ret);
508 if (err == SSL_ERROR_NONE)
509 {
510 buf += ret;
511 len -= ret;
512 }
513 else if (err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL ||
514 err == SSL_ERROR_ZERO_RETURN)
515 {
516 handle_ssl_errors(self->ssl, err, ret);
517 pyret = NULL;
518 break;
Jean-Paul Calderone691e6c92011-01-21 22:04:35 -0500519 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500520 } while (len > 0);
521
Jean-Paul Calderone5163f572011-04-22 18:38:16 -0400522#if PY_VERSION_HEX >= 0x02060000
Jean-Paul Calderone691e6c92011-01-21 22:04:35 -0500523 PyBuffer_Release(&pbuf);
524#endif
525
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500526 Py_XINCREF(pyret);
527 return pyret;
528}
529
530static char ssl_Connection_recv_doc[] = "\n\
531Receive data on the connection. NOTE: If you get one of the WantRead,\n\
532WantWrite or WantX509Lookup exceptions on this, you have to call the\n\
533method again with the SAME buffer.\n\
534\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900535:param bufsiz: The maximum number of bytes to read\n\
536:param flags: (optional) Included for compatibility with the socket\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400537 API, the value is ignored\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900538:return: The string read from the Connection\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500539";
540static PyObject *
541ssl_Connection_recv(ssl_ConnectionObj *self, PyObject *args)
542{
543 int bufsiz, ret, err, flags;
544 PyObject *buf;
545
546 if (!PyArg_ParseTuple(args, "i|i:recv", &bufsiz, &flags))
547 return NULL;
548
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400549 buf = PyBytes_FromStringAndSize(NULL, bufsiz);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500550 if (buf == NULL)
551 return NULL;
552
553 MY_BEGIN_ALLOW_THREADS(self->tstate)
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400554 ret = SSL_read(self->ssl, PyBytes_AsString(buf), bufsiz);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500555 MY_END_ALLOW_THREADS(self->tstate)
556
557 if (PyErr_Occurred())
558 {
559 Py_DECREF(buf);
560 flush_error_queue();
561 return NULL;
562 }
563
564 err = SSL_get_error(self->ssl, ret);
565 if (err == SSL_ERROR_NONE)
566 {
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400567 if (ret != bufsiz && _PyBytes_Resize(&buf, ret) < 0)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500568 return NULL;
569 return buf;
570 }
571 else
572 {
573 handle_ssl_errors(self->ssl, err, ret);
574 Py_DECREF(buf);
575 return NULL;
576 }
577}
578
Rick Deanb71c0d22009-04-01 14:09:23 -0500579static char ssl_Connection_bio_read_doc[] = "\n\
580When using non-socket connections this function reads\n\
581the \"dirty\" data that would have traveled away on the network.\n\
582\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900583:param bufsiz: The maximum number of bytes to read\n\
584:return: The string read.\n\
Rick Deanb71c0d22009-04-01 14:09:23 -0500585";
586static PyObject *
587ssl_Connection_bio_read(ssl_ConnectionObj *self, PyObject *args)
588{
Jean-Paul Calderoneaff0fc42009-04-27 17:13:34 -0400589 int bufsiz, ret;
Rick Deanb71c0d22009-04-01 14:09:23 -0500590 PyObject *buf;
591
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -0400592 if (self->from_ssl == NULL)
Rick Deanb71c0d22009-04-01 14:09:23 -0500593 {
594 PyErr_SetString(PyExc_TypeError, "Connection sock was not None");
595 return NULL;
596 }
597
598 if (!PyArg_ParseTuple(args, "i:bio_read", &bufsiz))
599 return NULL;
600
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400601 buf = PyBytes_FromStringAndSize(NULL, bufsiz);
Rick Deanb71c0d22009-04-01 14:09:23 -0500602 if (buf == NULL)
603 return NULL;
604
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400605 ret = BIO_read(self->from_ssl, PyBytes_AsString(buf), bufsiz);
Rick Deanb71c0d22009-04-01 14:09:23 -0500606
607 if (PyErr_Occurred())
608 {
609 Py_DECREF(buf);
610 flush_error_queue();
611 return NULL;
612 }
613
Jean-Paul Calderoneaff0fc42009-04-27 17:13:34 -0400614 if (ret <= 0) {
615 /*
616 * There was a problem with the BIO_read of some sort.
617 */
618 handle_bio_errors(self->from_ssl, ret);
Rick Deanb71c0d22009-04-01 14:09:23 -0500619 Py_DECREF(buf);
620 return NULL;
621 }
Jean-Paul Calderoneaff0fc42009-04-27 17:13:34 -0400622
623 /*
624 * Shrink the string to match the number of bytes we actually read.
625 */
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400626 if (ret != bufsiz && _PyBytes_Resize(&buf, ret) < 0)
Jean-Paul Calderoneaff0fc42009-04-27 17:13:34 -0400627 {
628 Py_DECREF(buf);
629 return NULL;
630 }
631 return buf;
Rick Deanb71c0d22009-04-01 14:09:23 -0500632}
633
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500634static char ssl_Connection_renegotiate_doc[] = "\n\
635Renegotiate the session\n\
636\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900637:return: True if the renegotiation can be started, false otherwise\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500638";
639static PyObject *
Jean-Paul Calderone8ea22522010-07-29 09:39:39 -0400640ssl_Connection_renegotiate(ssl_ConnectionObj *self, PyObject *args) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500641 int ret;
642
Jean-Paul Calderone8ea22522010-07-29 09:39:39 -0400643 if (!PyArg_ParseTuple(args, ":renegotiate")) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500644 return NULL;
Jean-Paul Calderone8ea22522010-07-29 09:39:39 -0400645 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500646
647 MY_BEGIN_ALLOW_THREADS(self->tstate);
648 ret = SSL_renegotiate(self->ssl);
649 MY_END_ALLOW_THREADS(self->tstate);
650
Jean-Paul Calderone8ea22522010-07-29 09:39:39 -0400651 if (PyErr_Occurred()) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500652 flush_error_queue();
653 return NULL;
654 }
655
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400656 return PyLong_FromLong((long)ret);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500657}
658
659static char ssl_Connection_do_handshake_doc[] = "\n\
660Perform an SSL handshake (usually called after renegotiate() or one of\n\
661set_*_state()). This can raise the same exceptions as send and recv.\n\
662\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900663:return: None.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500664";
665static PyObject *
666ssl_Connection_do_handshake(ssl_ConnectionObj *self, PyObject *args)
667{
668 int ret, err;
669
670 if (!PyArg_ParseTuple(args, ":do_handshake"))
671 return NULL;
672
673 MY_BEGIN_ALLOW_THREADS(self->tstate);
674 ret = SSL_do_handshake(self->ssl);
675 MY_END_ALLOW_THREADS(self->tstate);
676
677 if (PyErr_Occurred())
678 {
679 flush_error_queue();
680 return NULL;
681 }
682
683 err = SSL_get_error(self->ssl, ret);
684 if (err == SSL_ERROR_NONE)
685 {
686 Py_INCREF(Py_None);
687 return Py_None;
688 }
689 else
690 {
691 handle_ssl_errors(self->ssl, err, ret);
692 return NULL;
693 }
694}
695
696#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x00907000L
697static char ssl_Connection_renegotiate_pending_doc[] = "\n\
698Check if there's a renegotiation in progress, it will return false once\n\
699a renegotiation is finished.\n\
700\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900701:return: Whether there's a renegotiation in progress\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500702";
703static PyObject *
704ssl_Connection_renegotiate_pending(ssl_ConnectionObj *self, PyObject *args)
705{
706 if (!PyArg_ParseTuple(args, ":renegotiate_pending"))
707 return NULL;
708
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400709 return PyLong_FromLong((long)SSL_renegotiate_pending(self->ssl));
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500710}
711#endif
712
713static char ssl_Connection_total_renegotiations_doc[] = "\n\
714Find out the total number of renegotiations.\n\
715\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900716:return: The number of renegotiations.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500717";
718static PyObject *
719ssl_Connection_total_renegotiations(ssl_ConnectionObj *self, PyObject *args)
720{
721 if (!PyArg_ParseTuple(args, ":total_renegotiations"))
722 return NULL;
723
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400724 return PyLong_FromLong(SSL_total_renegotiations(self->ssl));
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500725}
726
727static char ssl_Connection_set_accept_state_doc[] = "\n\
728Set the connection to work in server mode. The handshake will be handled\n\
729automatically by read/write.\n\
730\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900731:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500732";
733static PyObject *
734ssl_Connection_set_accept_state(ssl_ConnectionObj *self, PyObject *args)
735{
736 if (!PyArg_ParseTuple(args, ":set_accept_state"))
737 return NULL;
738
739 SSL_set_accept_state(self->ssl);
740
741 Py_INCREF(Py_None);
742 return Py_None;
743}
744
745static char ssl_Connection_set_connect_state_doc[] = "\n\
746Set the connection to work in client mode. The handshake will be handled\n\
747automatically by read/write.\n\
748\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900749:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500750";
751static PyObject *
752ssl_Connection_set_connect_state(ssl_ConnectionObj *self, PyObject *args)
753{
754 if (!PyArg_ParseTuple(args, ":set_connect_state"))
755 return NULL;
756
757 SSL_set_connect_state(self->ssl);
758
759 Py_INCREF(Py_None);
760 return Py_None;
761}
762
763static char ssl_Connection_connect_doc[] = "\n\
764Connect to remote host and set up client-side SSL\n\
765\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900766:param addr: A remote address\n\
767:return: What the socket's connect method returns\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500768";
769static PyObject *
770ssl_Connection_connect(ssl_ConnectionObj *self, PyObject *args)
771{
772 PyObject *meth, *ret;
773
774 if ((meth = PyObject_GetAttrString(self->socket, "connect")) == NULL)
775 return NULL;
776
777 SSL_set_connect_state(self->ssl);
778
779 ret = PyEval_CallObject(meth, args);
780 Py_DECREF(meth);
781 if (ret == NULL)
782 return NULL;
783
784 return ret;
785}
786
787static char ssl_Connection_connect_ex_doc[] = "\n\
788Connect to remote host and set up client-side SSL. Note that if the socket's\n\
789connect_ex method doesn't return 0, SSL won't be initialized.\n\
790\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900791:param addr: A remove address\n\
792:return: What the socket's connect_ex method returns\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500793";
794static PyObject *
795ssl_Connection_connect_ex(ssl_ConnectionObj *self, PyObject *args)
796{
797 PyObject *meth, *ret;
798
799 if ((meth = PyObject_GetAttrString(self->socket, "connect_ex")) == NULL)
800 return NULL;
801
802 SSL_set_connect_state(self->ssl);
803
804 ret = PyEval_CallObject(meth, args);
805 Py_DECREF(meth);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500806 return ret;
807}
808
809static char ssl_Connection_accept_doc[] = "\n\
810Accept incoming connection and set up SSL on it\n\
811\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900812:return: A (conn,addr) pair where conn is a Connection and addr is an\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400813 address\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500814";
815static PyObject *
816ssl_Connection_accept(ssl_ConnectionObj *self, PyObject *args)
817{
818 PyObject *tuple, *socket, *address, *meth;
819 ssl_ConnectionObj *conn;
820
821 if ((meth = PyObject_GetAttrString(self->socket, "accept")) == NULL)
822 return NULL;
823 tuple = PyEval_CallObject(meth, args);
824 Py_DECREF(meth);
825 if (tuple == NULL)
826 return NULL;
827
828 socket = PyTuple_GetItem(tuple, 0);
829 Py_INCREF(socket);
830 address = PyTuple_GetItem(tuple, 1);
831 Py_INCREF(address);
832 Py_DECREF(tuple);
833
834 conn = ssl_Connection_New(self->context, socket);
835 Py_DECREF(socket);
836 if (conn == NULL)
837 {
838 Py_DECREF(address);
839 return NULL;
840 }
841
842 SSL_set_accept_state(conn->ssl);
843
844 tuple = Py_BuildValue("(OO)", conn, address);
845
846 Py_DECREF(conn);
847 Py_DECREF(address);
848
849 return tuple;
850}
851
Jean-Paul Calderone3ad85d42009-04-30 20:24:35 -0400852static char ssl_Connection_bio_shutdown_doc[] = "\n\
853When using non-socket connections this function signals end of\n\
854data on the input for this connection.\n\
855\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900856:return: None\n\
Jean-Paul Calderone3ad85d42009-04-30 20:24:35 -0400857";
858
859static PyObject *
860ssl_Connection_bio_shutdown(ssl_ConnectionObj *self, PyObject *args)
861{
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -0400862 if (self->from_ssl == NULL)
863 {
864 PyErr_SetString(PyExc_TypeError, "Connection sock was not None");
865 return NULL;
866 }
867
Jean-Paul Calderone3ad85d42009-04-30 20:24:35 -0400868 BIO_set_mem_eof_return(self->into_ssl, 0);
869 Py_INCREF(Py_None);
870 return Py_None;
871}
872
873
874
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500875static char ssl_Connection_shutdown_doc[] = "\n\
876Send closure alert\n\
877\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900878:return: True if the shutdown completed successfully (i.e. both sides\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400879 have sent closure alerts), false otherwise (i.e. you have to\n\
880 wait for a ZeroReturnError on a recv() method call\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500881";
882static PyObject *
883ssl_Connection_shutdown(ssl_ConnectionObj *self, PyObject *args)
884{
885 int ret;
886
887 if (!PyArg_ParseTuple(args, ":shutdown"))
888 return NULL;
889
890 MY_BEGIN_ALLOW_THREADS(self->tstate)
891 ret = SSL_shutdown(self->ssl);
892 MY_END_ALLOW_THREADS(self->tstate)
893
894 if (PyErr_Occurred())
895 {
896 flush_error_queue();
897 return NULL;
898 }
899
900 if (ret < 0)
901 {
Rick Deand369c932009-07-08 11:48:33 -0500902 exception_from_error_queue(ssl_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500903 return NULL;
904 }
905 else if (ret > 0)
906 {
907 Py_INCREF(Py_True);
908 return Py_True;
909 }
910 else
911 {
912 Py_INCREF(Py_False);
913 return Py_False;
914 }
915}
916
917static char ssl_Connection_get_cipher_list_doc[] = "\n\
918Get the session cipher list\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500919\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900920:return: A list of cipher strings\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500921";
922static PyObject *
923ssl_Connection_get_cipher_list(ssl_ConnectionObj *self, PyObject *args)
924{
925 int idx = 0;
926 const char *ret;
927 PyObject *lst, *item;
928
929 if (!PyArg_ParseTuple(args, ":get_cipher_list"))
930 return NULL;
931
932 lst = PyList_New(0);
933 while ((ret = SSL_get_cipher_list(self->ssl, idx)) != NULL)
934 {
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400935 item = PyText_FromString(ret);
Ziga Seilnachtfdeadb12009-09-01 16:35:50 +0200936 PyList_Append(lst, item);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500937 Py_DECREF(item);
938 idx++;
939 }
940 return lst;
941}
942
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200943static char ssl_Connection_get_client_ca_list_doc[] = "\n\
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200944Get CAs whose certificates are suggested for client authentication.\n\
945\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +0900946:return: If this is a server connection, a list of X509Names representing\n\
Jonathan Ballet648875f2011-07-16 14:14:58 +0900947 the acceptable CAs as set by :py:meth:`OpenSSL.SSL.Context.set_client_ca_list` or\n\
948 :py:meth:`OpenSSL.SSL.Context.add_client_ca`. If this is a client connection,\n\
Jean-Paul Calderone35788902009-10-24 13:48:08 -0400949 the list of such X509Names sent by the server, or an empty list if that\n\
950 has not yet happened.\n\
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200951";
952
953static PyObject *
Jean-Paul Calderone6e2b6852009-10-24 14:04:30 -0400954ssl_Connection_get_client_ca_list(ssl_ConnectionObj *self, PyObject *args) {
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200955 STACK_OF(X509_NAME) *CANames;
956 PyObject *CAList;
957 int i, n;
958
Ziga Seilnachtf93bf102009-10-23 09:51:07 +0200959 if (!PyArg_ParseTuple(args, ":get_client_ca_list")) {
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200960 return NULL;
961 }
962 CANames = SSL_get_client_CA_list(self->ssl);
963 if (CANames == NULL) {
964 return PyList_New(0);
965 }
966 n = sk_X509_NAME_num(CANames);
967 CAList = PyList_New(n);
968 if (CAList == NULL) {
969 return NULL;
970 }
971 for (i = 0; i < n; i++) {
972 X509_NAME *CAName;
973 PyObject *CA;
974
975 CAName = X509_NAME_dup(sk_X509_NAME_value(CANames, i));
976 if (CAName == NULL) {
977 Py_DECREF(CAList);
978 exception_from_error_queue(ssl_Error);
979 return NULL;
980 }
Jean-Paul Calderone305626a2010-10-31 20:51:17 -0400981 CA = (PyObject *)new_x509name(CAName, 1);
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200982 if (CA == NULL) {
983 X509_NAME_free(CAName);
984 Py_DECREF(CAList);
985 return NULL;
986 }
987 if (PyList_SetItem(CAList, i, CA)) {
988 Py_DECREF(CA);
989 Py_DECREF(CAList);
990 return NULL;
991 }
992 }
993 return CAList;
994}
995
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500996static char ssl_Connection_makefile_doc[] = "\n\
997The makefile() method is not implemented, since there is no dup semantics\n\
998for SSL connections\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500999\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001000:raise NotImplementedError\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001001";
1002static PyObject *
1003ssl_Connection_makefile(ssl_ConnectionObj *self, PyObject *args)
1004{
1005 PyErr_SetString(PyExc_NotImplementedError, "Cannot make file object of SSL.Connection");
1006 return NULL;
1007}
1008
1009static char ssl_Connection_get_app_data_doc[] = "\n\
1010Get application data\n\
1011\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001012:return: The application data\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001013";
1014static PyObject *
1015ssl_Connection_get_app_data(ssl_ConnectionObj *self, PyObject *args)
1016{
1017 if (!PyArg_ParseTuple(args, ":get_app_data"))
1018 return NULL;
1019
1020 Py_INCREF(self->app_data);
1021 return self->app_data;
1022}
1023
1024static char ssl_Connection_set_app_data_doc[] = "\n\
1025Set application data\n\
1026\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001027:param data - The application data\n\
1028:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001029";
1030static PyObject *
1031ssl_Connection_set_app_data(ssl_ConnectionObj *self, PyObject *args)
1032{
1033 PyObject *data;
1034
1035 if (!PyArg_ParseTuple(args, "O:set_app_data", &data))
1036 return NULL;
1037
1038 Py_DECREF(self->app_data);
1039 Py_INCREF(data);
1040 self->app_data = data;
1041
1042 Py_INCREF(Py_None);
1043 return Py_None;
1044}
1045
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001046static char ssl_Connection_get_shutdown_doc[] = "\n\
1047Get shutdown state\n\
1048\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001049:return: The shutdown state, a bitvector of SENT_SHUTDOWN, RECEIVED_SHUTDOWN.\n\
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001050";
1051static PyObject *
1052ssl_Connection_get_shutdown(ssl_ConnectionObj *self, PyObject *args)
1053{
1054 if (!PyArg_ParseTuple(args, ":get_shutdown"))
1055 return NULL;
1056
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001057 return PyLong_FromLong((long)SSL_get_shutdown(self->ssl));
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001058}
1059
1060static char ssl_Connection_set_shutdown_doc[] = "\n\
1061Set shutdown state\n\
1062\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001063:param state - bitvector of SENT_SHUTDOWN, RECEIVED_SHUTDOWN.\n\
1064:return: None\n\
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001065";
1066static PyObject *
1067ssl_Connection_set_shutdown(ssl_ConnectionObj *self, PyObject *args)
1068{
1069 int shutdown;
1070
1071 if (!PyArg_ParseTuple(args, "i:set_shutdown", &shutdown))
1072 return NULL;
1073
1074 SSL_set_shutdown(self->ssl, shutdown);
1075 Py_INCREF(Py_None);
1076 return Py_None;
1077}
1078
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001079static char ssl_Connection_state_string_doc[] = "\n\
1080Get a verbose state description\n\
1081\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001082:return: A string representing the state\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001083";
1084static PyObject *
1085ssl_Connection_state_string(ssl_ConnectionObj *self, PyObject *args)
1086{
1087 if (!PyArg_ParseTuple(args, ":state_string"))
1088 return NULL;
1089
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001090 return PyText_FromString(SSL_state_string_long(self->ssl));
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001091}
1092
Rick Deanb71c0d22009-04-01 14:09:23 -05001093static char ssl_Connection_client_random_doc[] = "\n\
1094Get a copy of the client hello nonce.\n\
1095\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001096:return: A string representing the state\n\
Rick Deanb71c0d22009-04-01 14:09:23 -05001097";
1098static PyObject *
1099ssl_Connection_client_random(ssl_ConnectionObj *self, PyObject *args)
1100{
1101 if (!PyArg_ParseTuple(args, ":client_random"))
1102 return NULL;
1103
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -04001104 if (self->ssl->session == NULL) {
Rick Deanb71c0d22009-04-01 14:09:23 -05001105 Py_INCREF(Py_None);
1106 return Py_None;
1107 }
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001108 return PyBytes_FromStringAndSize( (const char *) self->ssl->s3->client_random, SSL3_RANDOM_SIZE);
Rick Deanb71c0d22009-04-01 14:09:23 -05001109}
1110
1111static char ssl_Connection_server_random_doc[] = "\n\
1112Get a copy of the server hello nonce.\n\
1113\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001114:return: A string representing the state\n\
Rick Deanb71c0d22009-04-01 14:09:23 -05001115";
1116static PyObject *
1117ssl_Connection_server_random(ssl_ConnectionObj *self, PyObject *args)
1118{
1119 if (!PyArg_ParseTuple(args, ":server_random"))
1120 return NULL;
1121
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -04001122 if (self->ssl->session == NULL) {
Rick Deanb71c0d22009-04-01 14:09:23 -05001123 Py_INCREF(Py_None);
1124 return Py_None;
1125 }
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001126 return PyBytes_FromStringAndSize( (const char *) self->ssl->s3->server_random, SSL3_RANDOM_SIZE);
Rick Deanb71c0d22009-04-01 14:09:23 -05001127}
1128
1129static char ssl_Connection_master_key_doc[] = "\n\
1130Get a copy of the master key.\n\
1131\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001132:return: A string representing the state\n\
Rick Deanb71c0d22009-04-01 14:09:23 -05001133";
1134static PyObject *
1135ssl_Connection_master_key(ssl_ConnectionObj *self, PyObject *args)
1136{
1137 if (!PyArg_ParseTuple(args, ":master_key"))
1138 return NULL;
1139
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -04001140 if (self->ssl->session == NULL) {
Rick Deanb71c0d22009-04-01 14:09:23 -05001141 Py_INCREF(Py_None);
1142 return Py_None;
1143 }
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001144 return PyBytes_FromStringAndSize( (const char *) self->ssl->session->master_key, self->ssl->session->master_key_length);
Rick Deanb71c0d22009-04-01 14:09:23 -05001145}
1146
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001147static char ssl_Connection_sock_shutdown_doc[] = "\n\
1148See shutdown(2)\n\
1149\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001150:return: What the socket's shutdown() method returns\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001151";
1152static PyObject *
1153ssl_Connection_sock_shutdown(ssl_ConnectionObj *self, PyObject *args)
1154{
1155 PyObject *meth, *ret;
1156
1157 if ((meth = PyObject_GetAttrString(self->socket, "shutdown")) == NULL)
1158 return NULL;
1159 ret = PyEval_CallObject(meth, args);
1160 Py_DECREF(meth);
1161 return ret;
1162}
1163
1164static char ssl_Connection_get_peer_certificate_doc[] = "\n\
1165Retrieve the other side's certificate (if any)\n\
1166\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001167:return: The peer's certificate\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001168";
1169static PyObject *
1170ssl_Connection_get_peer_certificate(ssl_ConnectionObj *self, PyObject *args)
1171{
1172 X509 *cert;
1173
1174 if (!PyArg_ParseTuple(args, ":get_peer_certificate"))
1175 return NULL;
1176
1177 cert = SSL_get_peer_certificate(self->ssl);
1178 if (cert != NULL)
1179 {
Jean-Paul Calderone5c0f1f62010-10-31 21:36:43 -04001180 return (PyObject *)new_x509(cert, 1);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001181 }
1182 else
1183 {
1184 Py_INCREF(Py_None);
1185 return Py_None;
1186 }
1187}
1188
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001189static char ssl_Connection_get_peer_cert_chain_doc[] = "\n\
1190Retrieve the other side's certificate (if any)\n\
1191\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001192:return: A list of X509 instances giving the peer's certificate chain,\n\
Jean-Paul Calderone0a7e06a2011-05-19 18:49:35 -04001193 or None if it does not have one.\n\
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001194";
1195static PyObject *
Jean-Paul Calderonefcced2a2011-05-19 18:02:11 -04001196ssl_Connection_get_peer_cert_chain(ssl_ConnectionObj *self, PyObject *args) {
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001197 STACK_OF(X509) *sk;
Jean-Paul Calderone0a7e06a2011-05-19 18:49:35 -04001198 PyObject *chain;
1199 crypto_X509Obj *cert;
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001200 Py_ssize_t i;
1201
Jean-Paul Calderonefcced2a2011-05-19 18:02:11 -04001202 if (!PyArg_ParseTuple(args, ":get_peer_cert_chain")) {
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001203 return NULL;
Jean-Paul Calderonefcced2a2011-05-19 18:02:11 -04001204 }
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001205
1206 sk = SSL_get_peer_cert_chain(self->ssl);
Jean-Paul Calderonefcced2a2011-05-19 18:02:11 -04001207 if (sk != NULL) {
Jean-Paul Calderone0a7e06a2011-05-19 18:49:35 -04001208 chain = PyList_New(sk_X509_num(sk));
Jean-Paul Calderonefcced2a2011-05-19 18:02:11 -04001209 for (i = 0; i < sk_X509_num(sk); i++) {
Jean-Paul Calderone13d190b2011-05-20 19:14:50 -04001210 cert = new_x509(sk_X509_value(sk, i), 1);
Jean-Paul Calderone0a7e06a2011-05-19 18:49:35 -04001211 if (!cert) {
1212 /* XXX Untested */
1213 Py_DECREF(chain);
1214 return NULL;
1215 }
1216 CRYPTO_add(&cert->x509->references, 1, CRYPTO_LOCK_X509);
1217 PyList_SET_ITEM(chain, i, (PyObject *)cert);
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001218 }
Jean-Paul Calderone0a7e06a2011-05-19 18:49:35 -04001219 return chain;
Jean-Paul Calderonefcced2a2011-05-19 18:02:11 -04001220 } else {
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001221 Py_INCREF(Py_None);
1222 return Py_None;
1223 }
1224
1225}
1226
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001227static char ssl_Connection_want_read_doc[] = "\n\
1228Checks if more data has to be read from the transport layer to complete an\n\
1229operation.\n\
1230\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001231:return: True iff more data has to be read\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001232";
1233static PyObject *
1234ssl_Connection_want_read(ssl_ConnectionObj *self, PyObject *args)
1235{
1236 if (!PyArg_ParseTuple(args, ":want_read"))
1237 return NULL;
1238
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001239 return PyLong_FromLong((long)SSL_want_read(self->ssl));
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001240}
1241
1242static char ssl_Connection_want_write_doc[] = "\n\
1243Checks if there is data to write to the transport layer to complete an\n\
1244operation.\n\
1245\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001246:return: True iff there is data to write\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001247";
1248static PyObject *
1249ssl_Connection_want_write(ssl_ConnectionObj *self, PyObject *args)
1250{
1251 if (!PyArg_ParseTuple(args, ":want_write"))
1252 return NULL;
1253
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -04001254 return PyLong_FromLong((long)SSL_want_write(self->ssl));
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001255}
1256
Jean-Paul Calderone64eaffc2012-02-13 11:53:49 -05001257static char ssl_Connection_get_session_doc[] = "\n\
1258Returns the Session currently used.\n\
1259\n\
1260@return: An instance of :py:class:`OpenSSL.SSL.Session` or :py:obj:`None` if\n\
1261 no session exists.\n\
1262";
1263static PyObject *
1264ssl_Connection_get_session(ssl_ConnectionObj *self, PyObject *args) {
1265 ssl_SessionObj *session;
1266 SSL_SESSION *native_session;
1267
1268 if (!PyArg_ParseTuple(args, ":get_session")) {
1269 return NULL;
1270 }
1271
1272 native_session = SSL_get1_session(self->ssl);
1273
1274 if (native_session == NULL) {
1275 Py_INCREF(Py_None);
1276 return Py_None;
1277 }
1278
1279 session = ssl_Session_from_SSL_SESSION(native_session);
1280 if (!session) {
1281 Py_INCREF(Py_None);
1282 return Py_None;
1283 }
1284
1285 return (PyObject*)session;
1286}
1287
Jean-Paul Calderonefef5c4b2012-02-14 16:31:52 -05001288static char ssl_Connection_set_session_doc[] = "\n\
1289Set the session to be used when the TLS/SSL connection is established.\n\
1290\n\
1291:param session: A Session instance representing the session to use.\n\
1292:returns: None\n\
1293";
1294static PyObject *
1295ssl_Connection_set_session(ssl_ConnectionObj *self, PyObject *args) {
1296 ssl_SessionObj *session;
1297
1298 if (!PyArg_ParseTuple(args, "O!:set_session", &ssl_Session_Type, &session)) {
1299 return NULL;
1300 }
1301
1302 if (SSL_set_session(self->ssl, session->session) == 0) {
1303 /* XXX Under what conditions does this fail? I have no idea.
1304 */
1305 exception_from_error_queue(ssl_Error);
1306 return NULL;
1307 }
1308
1309 Py_INCREF(Py_None);
1310 return Py_None;
1311}
1312
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001313/*
1314 * Member methods in the Connection object
1315 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
1316 * { 'name', (PyCFunction)ssl_Connection_name, METH_VARARGS }
1317 * for convenience
1318 * ADD_ALIAS(name,real) creates an "alias" of the ssl_Connection_real
1319 * function with the name 'name'
1320 */
1321#define ADD_METHOD(name) \
1322 { #name, (PyCFunction)ssl_Connection_##name, METH_VARARGS, ssl_Connection_##name##_doc }
1323#define ADD_ALIAS(name,real) \
1324 { #name, (PyCFunction)ssl_Connection_##real, METH_VARARGS, ssl_Connection_##real##_doc }
1325static PyMethodDef ssl_Connection_methods[] =
1326{
1327 ADD_METHOD(get_context),
Jean-Paul Calderone95613b72011-05-25 22:30:21 -04001328 ADD_METHOD(set_context),
Jean-Paul Calderonec4cb6582011-05-26 18:47:00 -04001329 ADD_METHOD(get_servername),
1330 ADD_METHOD(set_tlsext_host_name),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001331 ADD_METHOD(pending),
1332 ADD_METHOD(send),
1333 ADD_ALIAS (write, send),
1334 ADD_METHOD(sendall),
1335 ADD_METHOD(recv),
1336 ADD_ALIAS (read, recv),
Rick Deanb71c0d22009-04-01 14:09:23 -05001337 ADD_METHOD(bio_read),
1338 ADD_METHOD(bio_write),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001339 ADD_METHOD(renegotiate),
1340 ADD_METHOD(do_handshake),
1341#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x00907000L
1342 ADD_METHOD(renegotiate_pending),
1343#endif
1344 ADD_METHOD(total_renegotiations),
1345 ADD_METHOD(connect),
1346 ADD_METHOD(connect_ex),
1347 ADD_METHOD(accept),
Jean-Paul Calderone3ad85d42009-04-30 20:24:35 -04001348 ADD_METHOD(bio_shutdown),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001349 ADD_METHOD(shutdown),
1350 ADD_METHOD(get_cipher_list),
Ziga Seilnachtf93bf102009-10-23 09:51:07 +02001351 ADD_METHOD(get_client_ca_list),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001352 ADD_METHOD(makefile),
1353 ADD_METHOD(get_app_data),
1354 ADD_METHOD(set_app_data),
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -05001355 ADD_METHOD(get_shutdown),
1356 ADD_METHOD(set_shutdown),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001357 ADD_METHOD(state_string),
Rick Deanb71c0d22009-04-01 14:09:23 -05001358 ADD_METHOD(server_random),
1359 ADD_METHOD(client_random),
1360 ADD_METHOD(master_key),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001361 ADD_METHOD(sock_shutdown),
1362 ADD_METHOD(get_peer_certificate),
Jean-Paul Calderone95b92c72011-05-17 15:45:21 -04001363 ADD_METHOD(get_peer_cert_chain),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001364 ADD_METHOD(want_read),
1365 ADD_METHOD(want_write),
1366 ADD_METHOD(set_accept_state),
1367 ADD_METHOD(set_connect_state),
Jean-Paul Calderone64eaffc2012-02-13 11:53:49 -05001368 ADD_METHOD(get_session),
Jean-Paul Calderonefef5c4b2012-02-14 16:31:52 -05001369 ADD_METHOD(set_session),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001370 { NULL, NULL }
1371};
1372#undef ADD_ALIAS
1373#undef ADD_METHOD
1374
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001375static char ssl_Connection_doc[] = "\n\
1376Connection(context, socket) -> Connection instance\n\
1377\n\
1378Create a new Connection object, using the given OpenSSL.SSL.Context instance\n\
1379and socket.\n\
1380\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +09001381:param context: An SSL Context to use for this connection\n\
1382:param socket: The socket to use for transport layer\n\
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001383";
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001384
1385/*
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001386 * Initializer used by ssl_Connection_new and ssl_Connection_New. *Not*
1387 * tp_init. This takes an already allocated ssl_ConnectionObj, a context, and
1388 * a optionally a socket, and glues them all together.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001389 */
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001390static ssl_ConnectionObj*
1391ssl_Connection_init(ssl_ConnectionObj *self, ssl_ContextObj *ctx, PyObject *sock) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001392 int fd;
1393
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001394 Py_INCREF(ctx);
1395 self->context = ctx;
1396
1397 Py_INCREF(sock);
1398 self->socket = sock;
1399
1400 self->ssl = NULL;
Jean-Paul Calderonefc4ed0f2009-04-27 11:51:27 -04001401 self->from_ssl = NULL;
1402 self->into_ssl = NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001403
1404 Py_INCREF(Py_None);
1405 self->app_data = Py_None;
1406
1407 self->tstate = NULL;
1408
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001409 self->ssl = SSL_new(self->context->ctx);
1410 SSL_set_app_data(self->ssl, self);
Rick Deanb71c0d22009-04-01 14:09:23 -05001411
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -04001412 if (self->socket == Py_None)
Rick Deanb71c0d22009-04-01 14:09:23 -05001413 {
1414 /* If it's not a socket or file, treat it like a memory buffer,
1415 * so crazy people can do things like EAP-TLS. */
1416 self->into_ssl = BIO_new(BIO_s_mem());
1417 self->from_ssl = BIO_new(BIO_s_mem());
Jean-Paul Calderone07acf3f2009-05-05 13:23:28 -04001418 if (self->into_ssl == NULL || self->from_ssl == NULL)
Rick Deanb71c0d22009-04-01 14:09:23 -05001419 goto error;
1420 SSL_set_bio(self->ssl, self->into_ssl, self->from_ssl);
1421 }
1422 else
1423 {
1424 fd = PyObject_AsFileDescriptor(self->socket);
1425 if (fd < 0)
1426 {
1427 Py_DECREF(self);
1428 return NULL;
1429 }
1430 else
1431 {
1432 SSL_set_fd(self->ssl, (SOCKET_T)fd);
1433 }
1434 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001435 return self;
Rick Deanb71c0d22009-04-01 14:09:23 -05001436
1437error:
1438 BIO_free(self->into_ssl); /* NULL safe */
1439 BIO_free(self->from_ssl); /* NULL safe */
1440 Py_DECREF(self);
1441 return NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001442}
1443
1444/*
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001445 * Constructor for Connection objects
1446 *
1447 * Arguments: ctx - An SSL Context to use for this connection
1448 * sock - The socket to use for transport layer
1449 * Returns: The newly created Connection object
1450 */
1451ssl_ConnectionObj *
1452ssl_Connection_New(ssl_ContextObj *ctx, PyObject *sock) {
1453 ssl_ConnectionObj *self;
1454
1455 self = PyObject_GC_New(ssl_ConnectionObj, &ssl_Connection_Type);
1456 if (self == NULL) {
1457 return NULL;
1458 }
1459 self = ssl_Connection_init(self, ctx, sock);
1460 if (self == NULL) {
1461 return NULL;
1462 }
1463 PyObject_GC_Track((PyObject *)self);
1464 return self;
1465}
1466
1467static PyObject*
1468ssl_Connection_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) {
1469 ssl_ConnectionObj *self;
1470 ssl_ContextObj *ctx;
1471 PyObject *sock;
1472 static char *kwlist[] = {"context", "socket", NULL};
1473
1474 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!O:Connection", kwlist,
1475 &ssl_Context_Type, &ctx, &sock)) {
1476 return NULL;
1477 }
1478
1479 self = (ssl_ConnectionObj *)subtype->tp_alloc(subtype, 1);
1480 if (self == NULL) {
1481 return NULL;
1482 }
1483
1484 return (PyObject *)ssl_Connection_init(self, ctx, sock);
1485}
1486
1487/*
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001488 * Find attribute
1489 *
1490 * Arguments: self - The Connection object
1491 * name - The attribute name
1492 * Returns: A Python object for the attribute, or NULL if something went
1493 * wrong
1494 */
1495static PyObject *
Jean-Paul Calderone997be7e2010-08-11 22:40:07 -04001496ssl_Connection_getattro(ssl_ConnectionObj *self, PyObject *nameobj) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001497 PyObject *meth;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001498
Jean-Paul Calderone997be7e2010-08-11 22:40:07 -04001499 meth = PyObject_GenericGetAttr((PyObject*)self, nameobj);
1500 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001501 PyErr_Clear();
1502 /* Try looking it up in the "socket" instead. */
Jean-Paul Calderone997be7e2010-08-11 22:40:07 -04001503 meth = PyObject_GenericGetAttr(self->socket, nameobj);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001504 }
1505
1506 return meth;
1507}
1508
1509/*
1510 * Call the visitproc on all contained objects.
1511 *
1512 * Arguments: self - The Connection object
1513 * visit - Function to call
1514 * arg - Extra argument to visit
1515 * Returns: 0 if all goes well, otherwise the return code from the first
1516 * call that gave non-zero result.
1517 */
1518static int
1519ssl_Connection_traverse(ssl_ConnectionObj *self, visitproc visit, void *arg)
1520{
1521 int ret = 0;
1522
1523 if (ret == 0 && self->context != NULL)
1524 ret = visit((PyObject *)self->context, arg);
1525 if (ret == 0 && self->socket != NULL)
1526 ret = visit(self->socket, arg);
1527 if (ret == 0 && self->app_data != NULL)
1528 ret = visit(self->app_data, arg);
1529 return ret;
1530}
1531
1532/*
1533 * Decref all contained objects and zero the pointers.
1534 *
1535 * Arguments: self - The Connection object
1536 * Returns: Always 0.
1537 */
1538static int
1539ssl_Connection_clear(ssl_ConnectionObj *self)
1540{
1541 Py_XDECREF(self->context);
1542 self->context = NULL;
1543 Py_XDECREF(self->socket);
1544 self->socket = NULL;
1545 Py_XDECREF(self->app_data);
1546 self->app_data = NULL;
Rick Deanb71c0d22009-04-01 14:09:23 -05001547 self->into_ssl = NULL; /* was cleaned up by SSL_free() */
1548 self->from_ssl = NULL; /* was cleaned up by SSL_free() */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001549 return 0;
1550}
1551
1552/*
1553 * Deallocate the memory used by the Connection object
1554 *
1555 * Arguments: self - The Connection object
1556 * Returns: None
1557 */
1558static void
1559ssl_Connection_dealloc(ssl_ConnectionObj *self)
1560{
1561 PyObject_GC_UnTrack(self);
1562 if (self->ssl != NULL)
1563 SSL_free(self->ssl);
1564 ssl_Connection_clear(self);
1565 PyObject_GC_Del(self);
1566}
1567
1568PyTypeObject ssl_Connection_Type = {
Jean-Paul Calderoneb6d75252010-08-11 23:55:45 -04001569 PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001570 "OpenSSL.SSL.Connection",
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001571 sizeof(ssl_ConnectionObj),
1572 0,
1573 (destructor)ssl_Connection_dealloc,
1574 NULL, /* print */
Jean-Paul Calderone997be7e2010-08-11 22:40:07 -04001575 NULL, /* tp_getattr */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001576 NULL, /* setattr */
1577 NULL, /* compare */
1578 NULL, /* repr */
1579 NULL, /* as_number */
1580 NULL, /* as_sequence */
1581 NULL, /* as_mapping */
1582 NULL, /* hash */
1583 NULL, /* call */
1584 NULL, /* str */
Jean-Paul Calderone997be7e2010-08-11 22:40:07 -04001585 (getattrofunc)ssl_Connection_getattro, /* getattro */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001586 NULL, /* setattro */
1587 NULL, /* as_buffer */
1588 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001589 ssl_Connection_doc, /* doc */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001590 (traverseproc)ssl_Connection_traverse,
1591 (inquiry)ssl_Connection_clear,
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001592 NULL, /* tp_richcompare */
1593 0, /* tp_weaklistoffset */
1594 NULL, /* tp_iter */
1595 NULL, /* tp_iternext */
1596 ssl_Connection_methods, /* tp_methods */
1597 NULL, /* tp_members */
1598 NULL, /* tp_getset */
1599 NULL, /* tp_base */
1600 NULL, /* tp_dict */
1601 NULL, /* tp_descr_get */
1602 NULL, /* tp_descr_set */
1603 0, /* tp_dictoffset */
1604 NULL, /* tp_init */
1605 NULL, /* tp_alloc */
1606 ssl_Connection_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001607};
1608
1609
1610/*
1611 * Initiailze the Connection part of the SSL sub module
1612 *
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001613 * Arguments: dict - The OpenSSL.SSL module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001614 * Returns: 1 for success, 0 otherwise
1615 */
1616int
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001617init_ssl_connection(PyObject *module) {
1618
1619 if (PyType_Ready(&ssl_Connection_Type) < 0) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001620 return 0;
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001621 }
1622
Jean-Paul Calderone86ad7112010-05-11 16:08:45 -04001623 /* PyModule_AddObject steals a reference.
1624 */
1625 Py_INCREF((PyObject *)&ssl_Connection_Type);
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001626 if (PyModule_AddObject(module, "Connection", (PyObject *)&ssl_Connection_Type) != 0) {
1627 return 0;
1628 }
1629
Jean-Paul Calderoneaed23582011-03-12 22:45:02 -05001630 /* PyModule_AddObject steals a reference.
1631 */
1632 Py_INCREF((PyObject *)&ssl_Connection_Type);
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -04001633 if (PyModule_AddObject(module, "ConnectionType", (PyObject *)&ssl_Connection_Type) != 0) {
1634 return 0;
1635 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001636
1637 return 1;
1638}
1639