Repository navigation
Expand file tree
/
Copy pathsslcompat.c
More file actions
354 lines (272 loc) · 9.44 KB
/
Copy pathsslcompat.c
File metadata and controls
354 lines (272 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* This file is part of Gruvi. Gruvi is free software available under the
* terms of the MIT license. See the file "LICENSE" that was provided
* together with this source file for the licensing terms.
*
* Copyright (c) 2012-2017 the Gruvi authors. See the file "AUTHORS" for a
* complete list.
*/
#include <Python.h>
/*
* This module backports the async SSL support from Python 3.5 to earlier
* versions. It supports Python 2.7, 3.3 and 3.4.
* */
#if !(PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION == 7) \
&& !(PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 3) \
&& !(PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 4)
# error "This module is for Python 2.7, 3.3 and 3.4 only."
#endif
#include <string.h>
#include <openssl/ssl.h>
/* Some useful error handling macros. */
#define RETURN_ERROR(fmt, ...) \
do { \
if ((fmt) != NULL) PyErr_Format(SSLError, fmt, ## __VA_ARGS__); \
goto error; \
} while (0)
#define RETURN_NONE(dest) \
do { Py_INCREF(Py_None); dest = Py_None; goto error; } while (0)
/* Useful Python 2/3 portability macros. Kudos to
* http://python3porting.com/cextensions.html. */
#if PY_MAJOR_VERSION >= 3
# define MOD_OK(val) (val)
# define MOD_ERROR NULL
# define MOD_INITFUNC(name) PyMODINIT_FUNC PyInit_ ## name(void)
# define INIT_MODULE(mod, name, methods, doc) \
do { \
static struct PyModuleDef moduledef = { \
PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
mod = PyModule_Create(&moduledef); \
} while (0)
# define BUF_FMT "y*"
#else
# define MOD_OK(value)
# define MOD_ERROR
# define MOD_INITFUNC(name) void init ## name(void)
# define INIT_MODULE(mod, name, methods, doc) \
do { mod = Py_InitModule3(name, methods, doc); } while (0)
# define BUF_FMT "s*"
#endif
/* Globals */
static PyObject *SSLError = NULL;
/* The MemoryBIO object has been copied and adapted from Python 3.5 */
typedef struct {
PyObject_HEAD
BIO *bio;
int eof_written;
} PySSLMemoryBIO;
static PyTypeObject PySSLMemoryBIO_Type;
/* Define a structure that has the same layout as PySSLObject in the _ssl
* module. This allows us to compile this module separately from the Python
* source tree.
*
* Of course we need to be very careful here that things match.
*/
typedef struct
{
PyObject_HEAD
PyObject *Socket;
#if PY_MAJOR_VERSION == 2
SSL_CTX *ctx;
#endif
SSL *ssl;
} PySSLObject;
#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION == 7 && PY_MICRO_VERSION < 9
# define SSL_OBJ_NAME "ssl.SSLContext"
#else
# define SSL_OBJ_NAME "_ssl._SSLSocket"
#endif
#define CHECK_SSL_OBJ(obj) \
do { \
if (strcmp(Py_TYPE(obj)->tp_name, SSL_OBJ_NAME)) \
RETURN_ERROR("expecting a " SSL_OBJ_NAME " instance"); \
} while (0)
/* MemoryBIO type */
static PyObject *
memory_bio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
BIO *bio = NULL;
PySSLMemoryBIO *self = NULL;
if (!PyArg_ParseTuple(args, ":MemoryBIO"))
RETURN_ERROR(NULL);
bio = BIO_new(BIO_s_mem());
if (bio == NULL)
RETURN_ERROR("failed to allocate BIO");
/* Since our BIO is non-blocking an empty read() does not indicate EOF,
* just that no data is currently available. The SSL routines should retry
* the read, which we can achieve by calling BIO_set_retry_read(). */
BIO_set_retry_read(bio);
BIO_set_mem_eof_return(bio, -1);
self = (PySSLMemoryBIO *) type->tp_alloc(type, 0);
if (self == NULL)
RETURN_ERROR(NULL);
self->bio = bio; bio = NULL;
self->eof_written = 0;
error:
if (bio != NULL) BIO_free(bio);
return (PyObject *) self;
}
static void
memory_bio_dealloc(PySSLMemoryBIO *self)
{
BIO_free(self->bio);
Py_TYPE(self)->tp_free(self);
}
static PyObject *
memory_bio_get_pending(PySSLMemoryBIO *self, void *c)
{
return PyLong_FromLong(BIO_ctrl_pending(self->bio));
}
static PyObject *
memory_bio_get_eof(PySSLMemoryBIO *self, void *c)
{
return PyBool_FromLong((BIO_ctrl_pending(self->bio) == 0) && self->eof_written);
}
static PyObject *
memory_bio_read(PySSLMemoryBIO *self, PyObject *args)
{
int len = -1, avail, nbytes;
PyObject *Pret = NULL;
if (!PyArg_ParseTuple(args, "|i:read", &len))
RETURN_ERROR(NULL);
avail = BIO_ctrl_pending(self->bio);
if ((len < 0) || (len > avail))
len = avail;
Pret = PyBytes_FromStringAndSize(NULL, len);
if (Pret == NULL)
RETURN_ERROR(NULL);
if (len > 0) {
nbytes = BIO_read(self->bio, PyBytes_AS_STRING(Pret), len);
/* There should never be any short reads but check anyway. */
if ((nbytes < len) && (_PyBytes_Resize(&Pret, len) < 0)) {
Py_DECREF(Pret); Pret = NULL;
RETURN_ERROR(NULL);
}
}
error:
return Pret;
}
static PyObject *
memory_bio_write(PySSLMemoryBIO *self, PyObject *args)
{
PyObject *Pret = NULL;
Py_buffer buf;
int nbytes;
buf.buf = NULL;
if (!PyArg_ParseTuple(args, BUF_FMT ":write", &buf))
RETURN_ERROR(NULL);
if (buf.len > INT_MAX)
RETURN_ERROR("string longer than %d bytes", INT_MAX);
if (self->eof_written)
RETURN_ERROR("cannot write() after write_eof()");
nbytes = BIO_write(self->bio, buf.buf, buf.len);
if (nbytes < 0)
RETURN_ERROR("BIO_write() failed");
Pret = PyLong_FromLong(nbytes);
error:
if (buf.buf != NULL) PyBuffer_Release(&buf);
return Pret;
}
static PyObject *
memory_bio_write_eof(PySSLMemoryBIO *self, PyObject *args)
{
self->eof_written = 1;
/* After an EOF is written, a zero return from read() should be a real EOF
* i.e. it should not be retried. Clear the SHOULD_RETRY flag. */
BIO_clear_retry_flags(self->bio);
BIO_set_mem_eof_return(self->bio, 0);
Py_RETURN_NONE;
}
static PyGetSetDef memory_bio_getsetlist[] =
{
{"pending", (getter) memory_bio_get_pending, NULL, NULL},
{"eof", (getter) memory_bio_get_eof, NULL, NULL},
{NULL, NULL}
};
static PyMethodDef memory_bio_methods[] =
{
{"read", (PyCFunction) memory_bio_read, METH_VARARGS, NULL},
{"write", (PyCFunction) memory_bio_write, METH_VARARGS, NULL},
{"write_eof", (PyCFunction) memory_bio_write_eof, METH_NOARGS, NULL},
{NULL, NULL}
};
static PyTypeObject PySSLMemoryBIO_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"_sslcompat.MemoryBIO", /*tp_name*/
sizeof(PySSLMemoryBIO), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)memory_bio_dealloc, /*tp_dealloc*/
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Py_TPFLAGS_DEFAULT, /*tp_flags*/
0, 0, 0, 0, 0, 0, 0,
memory_bio_methods, /*tp_methods*/
0, /*tp_members*/
memory_bio_getsetlist, /*tp_getset*/
0, 0, 0, 0, 0, 0, 0,
memory_bio_new, /*tp_new*/
};
/* This method is where the money it. It replaces the BIO on the SSL
* structure that the PySSLSocket is managing, without any knowledge or
* cooperation from PySSLSocket itself.
*
* For this to work it is important that the socket is in non-blocking
* mode. Otherwise _ssl will attempt to select on it which would be
* meaningless.
* */
static PyObject *
sslcompat_replace_bio(PyObject *self, PyObject *args)
{
PyObject *Pret = NULL;
PySSLObject *sslob;
PySSLMemoryBIO *inbio, *outbio;
if (!PyArg_ParseTuple(args, "OO!O!:replace_bio", &sslob, &PySSLMemoryBIO_Type,
&inbio, &PySSLMemoryBIO_Type, &outbio))
RETURN_ERROR(NULL);
CHECK_SSL_OBJ(sslob);
/* BIOs are reference counted and SSL_set_bio borrows our reference.
* To prevent a double free in memory_bio_dealloc() we need to take an
* extra reference here. */
CRYPTO_add(&inbio->bio->references, 1, CRYPTO_LOCK_BIO);
CRYPTO_add(&outbio->bio->references, 1, CRYPTO_LOCK_BIO);
/* Now replace the socket BIO with the memory BIOs */
SSL_set_bio(sslob->ssl, inbio->bio, outbio->bio);
RETURN_NONE(Pret);
error:
return Pret;
}
static PyMethodDef sslcompat_methods[] =
{
{"replace_bio", (PyCFunction) sslcompat_replace_bio, METH_VARARGS},
{NULL, NULL}
};
/* Main module */
PyDoc_STRVAR(sslcompat_doc, "Backport of Python 3.5+ async ssl support");
MOD_INITFUNC(_sslcompat)
{
PyObject *Pmodule, *Pdict, *Perrors, *Pstr;
INIT_MODULE(Pmodule, "_sslcompat", sslcompat_methods, sslcompat_doc);
if (!((Pdict = PyModule_GetDict(Pmodule))))
return MOD_ERROR;
if (!(SSLError = PyErr_NewException("_sslcompat.Error", NULL, NULL)))
return MOD_ERROR;
if (PyDict_SetItemString(Pdict, "Error", SSLError) < 0)
return MOD_ERROR;
if (PyType_Ready(&PySSLMemoryBIO_Type) < 0)
return MOD_ERROR;
if (PyDict_SetItemString(Pdict, "MemoryBIO", (PyObject *) &PySSLMemoryBIO_Type) < 0)
return MOD_ERROR;
/* Expose error codes that are needed (just 1 for now). */
if (!(Perrors = PyDict_New()))
return MOD_ERROR;
if (!(Pstr = PyBytes_FromString("PROTOCOL_IS_SHUTDOWN")))
return MOD_ERROR;
if (PyDict_SetItem(Perrors, PyLong_FromLong(SSL_R_PROTOCOL_IS_SHUTDOWN), Pstr) < 0)
return MOD_ERROR;
if (PyDict_SetItemString(Pdict, "errorcode", Perrors) < 0)
return MOD_ERROR;
/* Don't initialize the SSL library here. The following is done by _ssl:
SSL_load_error_strings();
SSL_library_init();
*/
return MOD_OK(Pmodule);
}