Mercurial > cpython-lru_cache-weakref
annotate Modules/_ssl.c @ 39042:c02fa187e0f4 legacy-trunk
Part of SF patch #1484695. This removes dead code. The chksum was
already verified in .frombuf() on the lines above. If there was
a problem an exception is raised, so there was no way this condition
could have been true.
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Mon, 10 Jul 2006 00:23:17 +0000 |
| parents | 9a637bf4df42 |
| children | 52a8738f5ab4 |
| rev | line source |
|---|---|
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
1 /* SSL socket module |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
2 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
3 SSL support based on patches by Brian E Gallew and Laszlo Kovacs. |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
4 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
5 This module is imported by socket.py. It should *not* be used |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
6 directly. |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
7 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
8 */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
9 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
10 #include "Python.h" |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
11 enum py_ssl_error { |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
12 /* these mirror ssl.h */ |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
13 PY_SSL_ERROR_NONE, |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
14 PY_SSL_ERROR_SSL, |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
15 PY_SSL_ERROR_WANT_READ, |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
16 PY_SSL_ERROR_WANT_WRITE, |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
17 PY_SSL_ERROR_WANT_X509_LOOKUP, |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
18 PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */ |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
19 PY_SSL_ERROR_ZERO_RETURN, |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
20 PY_SSL_ERROR_WANT_CONNECT, |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
21 /* start of non ssl.h errorcodes */ |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
22 PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */ |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
23 PY_SSL_ERROR_INVALID_ERROR_CODE |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
24 }; |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
25 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
26 /* Include symbols from _socket module */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
27 #include "socketmodule.h" |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
28 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
29 /* Include OpenSSL header files */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
30 #include "openssl/rsa.h" |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
31 #include "openssl/crypto.h" |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
32 #include "openssl/x509.h" |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
33 #include "openssl/pem.h" |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
34 #include "openssl/ssl.h" |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
35 #include "openssl/err.h" |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
36 #include "openssl/rand.h" |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
37 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
38 /* SSL error object */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
39 static PyObject *PySSLErrorObject; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
40 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
41 /* SSL socket object */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
42 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
43 #define X509_NAME_MAXLEN 256 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
44 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
45 /* RAND_* APIs got added to OpenSSL in 0.9.5 */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
46 #if OPENSSL_VERSION_NUMBER >= 0x0090500fL |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
47 # define HAVE_OPENSSL_RAND 1 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
48 #else |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
49 # undef HAVE_OPENSSL_RAND |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
50 #endif |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
51 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
52 typedef struct { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
53 PyObject_HEAD |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
54 PySocketSockObject *Socket; /* Socket on which we're layered */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
55 SSL_CTX* ctx; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
56 SSL* ssl; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
57 X509* server_cert; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
58 char server[X509_NAME_MAXLEN]; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
59 char issuer[X509_NAME_MAXLEN]; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
60 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
61 } PySSLObject; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
62 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24086
diff
changeset
|
63 static PyTypeObject PySSL_Type; |
|
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24086
diff
changeset
|
64 static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args); |
|
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24086
diff
changeset
|
65 static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args); |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
66 static int check_socket_and_wait_for_timeout(PySocketSockObject *s, |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
67 int writing); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
68 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
69 #define PySSLObject_Check(v) ((v)->ob_type == &PySSL_Type) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
70 |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
71 typedef enum { |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
72 SOCKET_IS_NONBLOCKING, |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
73 SOCKET_IS_BLOCKING, |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
74 SOCKET_HAS_TIMED_OUT, |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
75 SOCKET_HAS_BEEN_CLOSED, |
|
36487
07ba8aae3dc6
Try to improve name based on discussion on python-checkins with Jim Jewett
Neal Norwitz <nnorwitz@gmail.com>
parents:
36476
diff
changeset
|
76 SOCKET_TOO_LARGE_FOR_SELECT, |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
77 SOCKET_OPERATION_OK |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
78 } timeout_state; |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
79 |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
80 /* XXX It might be helpful to augment the error message generated |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
81 below with the name of the SSL function that generated the error. |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
82 I expect it's obvious most of the time. |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
83 */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
84 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
85 static PyObject * |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
86 PySSL_SetError(PySSLObject *obj, int ret) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
87 { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
88 PyObject *v, *n, *s; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
89 char *errstr; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
90 int err; |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
91 enum py_ssl_error p; |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
92 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
93 assert(ret <= 0); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
94 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
95 err = SSL_get_error(obj->ssl, ret); |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
96 |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
97 switch (err) { |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
98 case SSL_ERROR_ZERO_RETURN: |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
99 errstr = "TLS/SSL connection has been closed"; |
|
24086
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
100 p = PY_SSL_ERROR_ZERO_RETURN; |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
101 break; |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
102 case SSL_ERROR_WANT_READ: |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
103 errstr = "The operation did not complete (read)"; |
|
24086
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
104 p = PY_SSL_ERROR_WANT_READ; |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
105 break; |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
106 case SSL_ERROR_WANT_WRITE: |
|
24086
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
107 p = PY_SSL_ERROR_WANT_WRITE; |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
108 errstr = "The operation did not complete (write)"; |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
109 break; |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
110 case SSL_ERROR_WANT_X509_LOOKUP: |
|
24086
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
111 p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
112 errstr = "The operation did not complete (X509 lookup)"; |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
113 break; |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
114 case SSL_ERROR_WANT_CONNECT: |
|
24086
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
115 p = PY_SSL_ERROR_WANT_CONNECT; |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
116 errstr = "The operation did not complete (connect)"; |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
117 break; |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
118 case SSL_ERROR_SYSCALL: |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
119 { |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
120 unsigned long e = ERR_get_error(); |
|
24086
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
121 if (e == 0) { |
|
29695
d0dc956467bf
Fix SF #754870, SSL crash interpreter when remote side closes during connect
Neal Norwitz <nnorwitz@gmail.com>
parents:
29629
diff
changeset
|
122 if (ret == 0 || !obj->Socket) { |
|
24086
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
123 p = PY_SSL_ERROR_EOF; |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
124 errstr = "EOF occurred in violation of protocol"; |
|
24086
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
125 } else if (ret == -1) { |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
126 /* the underlying BIO reported an I/O error */ |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
127 return obj->Socket->errorhandler(); |
|
24086
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
128 } else { /* possible? */ |
|
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
129 p = PY_SSL_ERROR_SYSCALL; |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
130 errstr = "Some I/O error occurred"; |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
131 } |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
132 } else { |
|
24086
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
133 p = PY_SSL_ERROR_SYSCALL; |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
134 /* XXX Protected by global interpreter lock */ |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
135 errstr = ERR_error_string(e, NULL); |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
136 } |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
137 break; |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
138 } |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
139 case SSL_ERROR_SSL: |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
140 { |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
141 unsigned long e = ERR_get_error(); |
|
24086
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
142 p = PY_SSL_ERROR_SSL; |
|
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
143 if (e != 0) |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
144 /* XXX Protected by global interpreter lock */ |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
145 errstr = ERR_error_string(e, NULL); |
|
24086
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
146 else { /* possible? */ |
|
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
147 errstr = "A failure in the SSL library occurred"; |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
148 } |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
149 break; |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
150 } |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
151 default: |
|
24086
86dfb96a1831
Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
152 p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
153 errstr = "Invalid error code"; |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
154 } |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
155 n = PyInt_FromLong((long) p); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
156 if (n == NULL) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
157 return NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
158 v = PyTuple_New(2); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
159 if (v == NULL) { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
160 Py_DECREF(n); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
161 return NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
162 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
163 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
164 s = PyString_FromString(errstr); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
165 if (s == NULL) { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
166 Py_DECREF(v); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
167 Py_DECREF(n); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
168 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
169 PyTuple_SET_ITEM(v, 0, n); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
170 PyTuple_SET_ITEM(v, 1, s); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
171 PyErr_SetObject(PySSLErrorObject, v); |
|
32960
7aaa852fa856
Add a missing decref -- PyErr_SetObject increfs the 'object'!
Michael W. Hudson <mwh@python.net>
parents:
32601
diff
changeset
|
172 Py_DECREF(v); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
173 return NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
174 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
175 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
176 static PySSLObject * |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
177 newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
178 { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
179 PySSLObject *self; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
180 char *errstr = NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
181 int ret; |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
182 int err; |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
183 int sockstate; |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
184 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
185 self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */ |
|
38152
9a637bf4df42
Don't mask a no memory error with a less meaningful one as discussed on python-checkins
Neal Norwitz <nnorwitz@gmail.com>
parents:
38139
diff
changeset
|
186 if (self == NULL) |
|
38139
f1e24452dbba
Fix problems found by Coverity.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37784
diff
changeset
|
187 return NULL; |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
188 memset(self->server, '\0', sizeof(char) * X509_NAME_MAXLEN); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
189 memset(self->issuer, '\0', sizeof(char) * X509_NAME_MAXLEN); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
190 self->server_cert = NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
191 self->ssl = NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
192 self->ctx = NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
193 self->Socket = NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
194 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
195 if ((key_file && !cert_file) || (!key_file && cert_file)) { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
196 errstr = "Both the key & certificate files must be specified"; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
197 goto fail; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
198 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
199 |
|
24398
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
200 Py_BEGIN_ALLOW_THREADS |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
201 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */ |
|
24398
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
202 Py_END_ALLOW_THREADS |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
203 if (self->ctx == NULL) { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
204 errstr = "SSL_CTX_new error"; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
205 goto fail; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
206 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
207 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
208 if (key_file) { |
|
24398
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
209 Py_BEGIN_ALLOW_THREADS |
|
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
210 ret = SSL_CTX_use_PrivateKey_file(self->ctx, key_file, |
|
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
211 SSL_FILETYPE_PEM); |
|
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
212 Py_END_ALLOW_THREADS |
|
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
213 if (ret < 1) { |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
214 errstr = "SSL_CTX_use_PrivateKey_file error"; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
215 goto fail; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
216 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
217 |
|
24398
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
218 Py_BEGIN_ALLOW_THREADS |
|
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
219 ret = SSL_CTX_use_certificate_chain_file(self->ctx, |
|
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
220 cert_file); |
|
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
221 Py_END_ALLOW_THREADS |
|
32601
5e1f55ec17d6
[Patch #909007] Enable a bunch of safe bug workarounds in OpenSSL, for compatibility with various broken SSL implementations out there.
Andrew M. Kuchling <amk@amk.ca>
parents:
32599
diff
changeset
|
222 SSL_CTX_set_options(self->ctx, SSL_OP_ALL); /* ssl compatibility */ |
|
24398
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
223 if (ret < 1) { |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
224 errstr = "SSL_CTX_use_certificate_chain_file error"; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
225 goto fail; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
226 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
227 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
228 |
|
24398
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
229 Py_BEGIN_ALLOW_THREADS |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
230 SSL_CTX_set_verify(self->ctx, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
231 SSL_VERIFY_NONE, NULL); /* set verify lvl */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
232 self->ssl = SSL_new(self->ctx); /* New ssl struct */ |
|
24398
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
233 Py_END_ALLOW_THREADS |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
234 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */ |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
235 |
| 30723 | 236 /* If the socket is in non-blocking mode or timeout mode, set the BIO |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
237 * to non-blocking mode (blocking is the default) |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
238 */ |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
239 if (Sock->sock_timeout >= 0.0) { |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
240 /* Set both the read and write BIO's to non-blocking mode */ |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
241 BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
242 BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
243 } |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
244 |
|
24398
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
245 Py_BEGIN_ALLOW_THREADS |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
246 SSL_set_connect_state(self->ssl); |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
247 Py_END_ALLOW_THREADS |
|
24398
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
248 |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
249 /* Actually negotiate SSL connection */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
250 /* XXX If SSL_connect() returns 0, it's also a failure. */ |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
251 sockstate = 0; |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
252 do { |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
253 Py_BEGIN_ALLOW_THREADS |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
254 ret = SSL_connect(self->ssl); |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
255 err = SSL_get_error(self->ssl, ret); |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
256 Py_END_ALLOW_THREADS |
|
29629
a4d6bc71f091
Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents:
27594
diff
changeset
|
257 if(PyErr_CheckSignals()) { |
|
a4d6bc71f091
Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents:
27594
diff
changeset
|
258 goto fail; |
|
a4d6bc71f091
Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents:
27594
diff
changeset
|
259 } |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
260 if (err == SSL_ERROR_WANT_READ) { |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
261 sockstate = check_socket_and_wait_for_timeout(Sock, 0); |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
262 } else if (err == SSL_ERROR_WANT_WRITE) { |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
263 sockstate = check_socket_and_wait_for_timeout(Sock, 1); |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
264 } else { |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
265 sockstate = SOCKET_OPERATION_OK; |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
266 } |
|
36451
8404c9efdc35
Fix indentation (whitespace only).
Neal Norwitz <nnorwitz@gmail.com>
parents:
36324
diff
changeset
|
267 if (sockstate == SOCKET_HAS_TIMED_OUT) { |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
268 PyErr_SetString(PySSLErrorObject, "The connect operation timed out"); |
|
29629
a4d6bc71f091
Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents:
27594
diff
changeset
|
269 goto fail; |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
270 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
271 PyErr_SetString(PySSLErrorObject, "Underlying socket has been closed."); |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
272 goto fail; |
|
36487
07ba8aae3dc6
Try to improve name based on discussion on python-checkins with Jim Jewett
Neal Norwitz <nnorwitz@gmail.com>
parents:
36476
diff
changeset
|
273 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
|
36452
7789cc557e1e
Bug #876637, prevent stack corruption when socket descriptor
Neal Norwitz <nnorwitz@gmail.com>
parents:
36451
diff
changeset
|
274 PyErr_SetString(PySSLErrorObject, "Underlying socket too large for select()."); |
|
7789cc557e1e
Bug #876637, prevent stack corruption when socket descriptor
Neal Norwitz <nnorwitz@gmail.com>
parents:
36451
diff
changeset
|
275 goto fail; |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
276 } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
277 break; |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
278 } |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
279 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
280 if (ret <= 0) { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
281 PySSL_SetError(self, ret); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
282 goto fail; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
283 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
284 self->ssl->debug = 1; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
285 |
|
24398
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
286 Py_BEGIN_ALLOW_THREADS |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
287 if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
288 X509_NAME_oneline(X509_get_subject_name(self->server_cert), |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
289 self->server, X509_NAME_MAXLEN); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
290 X509_NAME_oneline(X509_get_issuer_name(self->server_cert), |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
291 self->issuer, X509_NAME_MAXLEN); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
292 } |
|
24398
c7ed925c75b3
Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
293 Py_END_ALLOW_THREADS |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
294 self->Socket = Sock; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
295 Py_INCREF(self->Socket); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
296 return self; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
297 fail: |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
298 if (errstr) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
299 PyErr_SetString(PySSLErrorObject, errstr); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
300 Py_DECREF(self); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
301 return NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
302 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
303 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
304 static PyObject * |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
305 PySocket_ssl(PyObject *self, PyObject *args) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
306 { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
307 PySSLObject *rv; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
308 PySocketSockObject *Sock; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
309 char *key_file = NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
310 char *cert_file = NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
311 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
312 if (!PyArg_ParseTuple(args, "O!|zz:ssl", |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
313 PySocketModule.Sock_Type, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
314 (PyObject*)&Sock, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
315 &key_file, &cert_file)) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
316 return NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
317 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
318 rv = newPySSLObject(Sock, key_file, cert_file); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
319 if (rv == NULL) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
320 return NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
321 return (PyObject *)rv; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
322 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
323 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
324 PyDoc_STRVAR(ssl_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
325 "ssl(socket, [keyfile, certfile]) -> sslobject"); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
326 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
327 /* SSL object methods */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
328 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
329 static PyObject * |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
330 PySSL_server(PySSLObject *self) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
331 { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
332 return PyString_FromString(self->server); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
333 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
334 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
335 static PyObject * |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
336 PySSL_issuer(PySSLObject *self) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
337 { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
338 return PyString_FromString(self->issuer); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
339 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
340 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
341 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
342 static void PySSL_dealloc(PySSLObject *self) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
343 { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
344 if (self->server_cert) /* Possible not to have one? */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
345 X509_free (self->server_cert); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
346 if (self->ssl) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
347 SSL_free(self->ssl); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
348 if (self->ctx) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
349 SSL_CTX_free(self->ctx); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
350 Py_XDECREF(self->Socket); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
351 PyObject_Del(self); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
352 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
353 |
|
27430
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
354 /* If the socket has a timeout, do a select() on the socket. |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
355 The argument writing indicates the direction. |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
356 Returns one of the possibilities in the timeout_state enum (above). |
|
27430
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
357 */ |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
358 |
|
27430
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
359 static int |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
360 check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing) |
|
27430
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
361 { |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
362 fd_set fds; |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
363 struct timeval tv; |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
364 int rc; |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
365 |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
366 /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
367 if (s->sock_timeout < 0.0) |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
368 return SOCKET_IS_BLOCKING; |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
369 else if (s->sock_timeout == 0.0) |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
370 return SOCKET_IS_NONBLOCKING; |
|
27430
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
371 |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
372 /* Guard against closed socket */ |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
373 if (s->sock_fd < 0) |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
374 return SOCKET_HAS_BEEN_CLOSED; |
|
27430
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
375 |
|
36452
7789cc557e1e
Bug #876637, prevent stack corruption when socket descriptor
Neal Norwitz <nnorwitz@gmail.com>
parents:
36451
diff
changeset
|
376 /* Guard against socket too large for select*/ |
|
36476
c073dffbe70e
Introduce Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE.
Martin v. Löwis <martin@v.loewis.de>
parents:
36452
diff
changeset
|
377 #ifndef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE |
|
36452
7789cc557e1e
Bug #876637, prevent stack corruption when socket descriptor
Neal Norwitz <nnorwitz@gmail.com>
parents:
36451
diff
changeset
|
378 if (s->sock_fd >= FD_SETSIZE) |
|
36487
07ba8aae3dc6
Try to improve name based on discussion on python-checkins with Jim Jewett
Neal Norwitz <nnorwitz@gmail.com>
parents:
36476
diff
changeset
|
379 return SOCKET_TOO_LARGE_FOR_SELECT; |
|
36476
c073dffbe70e
Introduce Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE.
Martin v. Löwis <martin@v.loewis.de>
parents:
36452
diff
changeset
|
380 #endif |
|
36452
7789cc557e1e
Bug #876637, prevent stack corruption when socket descriptor
Neal Norwitz <nnorwitz@gmail.com>
parents:
36451
diff
changeset
|
381 |
|
27430
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
382 /* Construct the arguments to select */ |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
383 tv.tv_sec = (int)s->sock_timeout; |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
384 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
385 FD_ZERO(&fds); |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
386 FD_SET(s->sock_fd, &fds); |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
387 |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
388 /* See if the socket is ready */ |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
389 Py_BEGIN_ALLOW_THREADS |
|
27430
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
390 if (writing) |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
391 rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
392 else |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
393 rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
394 Py_END_ALLOW_THREADS |
|
27430
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
395 |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
396 /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
397 (when we are able to write or when there's something to read) */ |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
398 return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
|
27430
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
399 } |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
400 |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
401 static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
402 { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
403 char *data; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
404 int len; |
|
30795
9da537167b74
Patch #803998: Correctly check for error in SSL_write.
Martin v. Löwis <martin@v.loewis.de>
parents:
30723
diff
changeset
|
405 int count; |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
406 int sockstate; |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
407 int err; |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
408 |
|
30795
9da537167b74
Patch #803998: Correctly check for error in SSL_write.
Martin v. Löwis <martin@v.loewis.de>
parents:
30723
diff
changeset
|
409 if (!PyArg_ParseTuple(args, "s#:write", &data, &count)) |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
410 return NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
411 |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
412 sockstate = check_socket_and_wait_for_timeout(self->Socket, 1); |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
413 if (sockstate == SOCKET_HAS_TIMED_OUT) { |
|
27430
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
414 PyErr_SetString(PySSLErrorObject, "The write operation timed out"); |
|
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
415 return NULL; |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
416 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
417 PyErr_SetString(PySSLErrorObject, "Underlying socket has been closed."); |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
418 return NULL; |
|
36487
07ba8aae3dc6
Try to improve name based on discussion on python-checkins with Jim Jewett
Neal Norwitz <nnorwitz@gmail.com>
parents:
36476
diff
changeset
|
419 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
|
36452
7789cc557e1e
Bug #876637, prevent stack corruption when socket descriptor
Neal Norwitz <nnorwitz@gmail.com>
parents:
36451
diff
changeset
|
420 PyErr_SetString(PySSLErrorObject, "Underlying socket too large for select()."); |
|
7789cc557e1e
Bug #876637, prevent stack corruption when socket descriptor
Neal Norwitz <nnorwitz@gmail.com>
parents:
36451
diff
changeset
|
421 return NULL; |
|
27430
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
422 } |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
423 do { |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
424 err = 0; |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
425 Py_BEGIN_ALLOW_THREADS |
|
30795
9da537167b74
Patch #803998: Correctly check for error in SSL_write.
Martin v. Löwis <martin@v.loewis.de>
parents:
30723
diff
changeset
|
426 len = SSL_write(self->ssl, data, count); |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
427 err = SSL_get_error(self->ssl, len); |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
428 Py_END_ALLOW_THREADS |
|
29629
a4d6bc71f091
Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents:
27594
diff
changeset
|
429 if(PyErr_CheckSignals()) { |
|
a4d6bc71f091
Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents:
27594
diff
changeset
|
430 return NULL; |
|
a4d6bc71f091
Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents:
27594
diff
changeset
|
431 } |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
432 if (err == SSL_ERROR_WANT_READ) { |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
433 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0); |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
434 } else if (err == SSL_ERROR_WANT_WRITE) { |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
435 sockstate = check_socket_and_wait_for_timeout(self->Socket, 1); |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
436 } else { |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
437 sockstate = SOCKET_OPERATION_OK; |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
438 } |
|
36451
8404c9efdc35
Fix indentation (whitespace only).
Neal Norwitz <nnorwitz@gmail.com>
parents:
36324
diff
changeset
|
439 if (sockstate == SOCKET_HAS_TIMED_OUT) { |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
440 PyErr_SetString(PySSLErrorObject, "The write operation timed out"); |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
441 return NULL; |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
442 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
443 PyErr_SetString(PySSLErrorObject, "Underlying socket has been closed."); |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
444 return NULL; |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
445 } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
446 break; |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
447 } |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
448 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
449 if (len > 0) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
450 return PyInt_FromLong(len); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
451 else |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
452 return PySSL_SetError(self, len); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
453 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
454 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
455 PyDoc_STRVAR(PySSL_SSLwrite_doc, |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
456 "write(s) -> len\n\ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
457 \n\ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
458 Writes the string s into the SSL object. Returns the number\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
459 of bytes written."); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
460 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
461 static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
462 { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
463 PyObject *buf; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
464 int count = 0; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
465 int len = 1024; |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
466 int sockstate; |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
467 int err; |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
468 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
469 if (!PyArg_ParseTuple(args, "|i:read", &len)) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
470 return NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
471 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
472 if (!(buf = PyString_FromStringAndSize((char *) 0, len))) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
473 return NULL; |
|
37339
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
474 |
|
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
475 /* first check if there are bytes ready to be read */ |
|
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
476 Py_BEGIN_ALLOW_THREADS |
|
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
477 count = SSL_pending(self->ssl); |
|
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
478 Py_END_ALLOW_THREADS |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
479 |
|
37339
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
480 if (!count) { |
|
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
481 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0); |
|
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
482 if (sockstate == SOCKET_HAS_TIMED_OUT) { |
|
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
483 PyErr_SetString(PySSLErrorObject, "The read operation timed out"); |
|
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
484 Py_DECREF(buf); |
|
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
485 return NULL; |
|
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
486 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
|
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
487 PyErr_SetString(PySSLErrorObject, "Underlying socket too large for select()."); |
|
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
488 return NULL; |
|
6fef957712b9
Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents:
36487
diff
changeset
|
489 } |
|
27430
1ae0b142370e
Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
490 } |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
491 do { |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
492 err = 0; |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
493 Py_BEGIN_ALLOW_THREADS |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
494 count = SSL_read(self->ssl, PyString_AsString(buf), len); |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
495 err = SSL_get_error(self->ssl, count); |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
496 Py_END_ALLOW_THREADS |
|
29629
a4d6bc71f091
Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents:
27594
diff
changeset
|
497 if(PyErr_CheckSignals()) { |
|
a4d6bc71f091
Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents:
27594
diff
changeset
|
498 Py_DECREF(buf); |
|
a4d6bc71f091
Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents:
27594
diff
changeset
|
499 return NULL; |
|
a4d6bc71f091
Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents:
27594
diff
changeset
|
500 } |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
501 if (err == SSL_ERROR_WANT_READ) { |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
502 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0); |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
503 } else if (err == SSL_ERROR_WANT_WRITE) { |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
504 sockstate = check_socket_and_wait_for_timeout(self->Socket, 1); |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
505 } else { |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
506 sockstate = SOCKET_OPERATION_OK; |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
507 } |
|
36451
8404c9efdc35
Fix indentation (whitespace only).
Neal Norwitz <nnorwitz@gmail.com>
parents:
36324
diff
changeset
|
508 if (sockstate == SOCKET_HAS_TIMED_OUT) { |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
509 PyErr_SetString(PySSLErrorObject, "The read operation timed out"); |
|
29629
a4d6bc71f091
Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents:
27594
diff
changeset
|
510 Py_DECREF(buf); |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
511 return NULL; |
|
32599
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
512 } else if (sockstate == SOCKET_IS_NONBLOCKING) { |
|
598a5ce63322
[Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents:
31790
diff
changeset
|
513 break; |
|
27557
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
514 } |
|
5eefec94a784
SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents:
27430
diff
changeset
|
515 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
516 if (count <= 0) { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
517 Py_DECREF(buf); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
518 return PySSL_SetError(self, count); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
519 } |
|
23295
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23190
diff
changeset
|
520 if (count != len) |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23190
diff
changeset
|
521 _PyString_Resize(&buf, count); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
522 return buf; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
523 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
524 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
525 PyDoc_STRVAR(PySSL_SSLread_doc, |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
526 "read([len]) -> string\n\ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
527 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
528 Read up to len bytes from the SSL socket."); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
529 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
530 static PyMethodDef PySSLMethods[] = { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
531 {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
532 PySSL_SSLwrite_doc}, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
533 {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
534 PySSL_SSLread_doc}, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
535 {"server", (PyCFunction)PySSL_server, METH_NOARGS}, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
536 {"issuer", (PyCFunction)PySSL_issuer, METH_NOARGS}, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
537 {NULL, NULL} |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
538 }; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
539 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
540 static PyObject *PySSL_getattr(PySSLObject *self, char *name) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
541 { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
542 return Py_FindMethod(PySSLMethods, (PyObject *)self, name); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
543 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
544 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24086
diff
changeset
|
545 static PyTypeObject PySSL_Type = { |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
546 PyObject_HEAD_INIT(NULL) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
547 0, /*ob_size*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
548 "socket.SSL", /*tp_name*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
549 sizeof(PySSLObject), /*tp_basicsize*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
550 0, /*tp_itemsize*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
551 /* methods */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
552 (destructor)PySSL_dealloc, /*tp_dealloc*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
553 0, /*tp_print*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
554 (getattrfunc)PySSL_getattr, /*tp_getattr*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
555 0, /*tp_setattr*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
556 0, /*tp_compare*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
557 0, /*tp_repr*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
558 0, /*tp_as_number*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
559 0, /*tp_as_sequence*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
560 0, /*tp_as_mapping*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
561 0, /*tp_hash*/ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
562 }; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
563 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
564 #ifdef HAVE_OPENSSL_RAND |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
565 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
566 /* helper routines for seeding the SSL PRNG */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
567 static PyObject * |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
568 PySSL_RAND_add(PyObject *self, PyObject *args) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
569 { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
570 char *buf; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
571 int len; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
572 double entropy; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
573 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
574 if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy)) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
575 return NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
576 RAND_add(buf, len, entropy); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
577 Py_INCREF(Py_None); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
578 return Py_None; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
579 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
580 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
581 PyDoc_STRVAR(PySSL_RAND_add_doc, |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
582 "RAND_add(string, entropy)\n\ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
583 \n\ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
584 Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
585 bound on the entropy contained in string."); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
586 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
587 static PyObject * |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
588 PySSL_RAND_status(PyObject *self) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
589 { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
590 return PyInt_FromLong(RAND_status()); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
591 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
592 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
593 PyDoc_STRVAR(PySSL_RAND_status_doc, |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
594 "RAND_status() -> 0 or 1\n\ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
595 \n\ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
596 Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
597 It is necessary to seed the PRNG with RAND_add() on some platforms before\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
598 using the ssl() function."); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
599 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
600 static PyObject * |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
601 PySSL_RAND_egd(PyObject *self, PyObject *arg) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
602 { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
603 int bytes; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
604 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
605 if (!PyString_Check(arg)) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
606 return PyErr_Format(PyExc_TypeError, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
607 "RAND_egd() expected string, found %s", |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
608 arg->ob_type->tp_name); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
609 bytes = RAND_egd(PyString_AS_STRING(arg)); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
610 if (bytes == -1) { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
611 PyErr_SetString(PySSLErrorObject, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
612 "EGD connection failed or EGD did not return " |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
613 "enough data to seed the PRNG"); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
614 return NULL; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
615 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
616 return PyInt_FromLong(bytes); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
617 } |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
618 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
619 PyDoc_STRVAR(PySSL_RAND_egd_doc, |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
620 "RAND_egd(path) -> bytes\n\ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
621 \n\ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
622 Queries the entropy gather daemon (EGD) on socket path. Returns number\n\ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
623 of bytes read. Raises socket.sslerror if connection to EGD fails or\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
624 if it does provide enough data to seed PRNG."); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
625 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
626 #endif |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
627 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
628 /* List of functions exported by this module. */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
629 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
630 static PyMethodDef PySSL_methods[] = { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
631 {"ssl", PySocket_ssl, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
632 METH_VARARGS, ssl_doc}, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
633 #ifdef HAVE_OPENSSL_RAND |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
634 {"RAND_add", PySSL_RAND_add, METH_VARARGS, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
635 PySSL_RAND_add_doc}, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
636 {"RAND_egd", PySSL_RAND_egd, METH_O, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
637 PySSL_RAND_egd_doc}, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
638 {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
639 PySSL_RAND_status_doc}, |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
640 #endif |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
641 {NULL, NULL} /* Sentinel */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
642 }; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
643 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
644 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
645 PyDoc_STRVAR(module_doc, |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
646 "Implementation module for SSL socket operations. See the socket module\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
647 for documentation."); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
648 |
|
24471
f66b3a3509c7
Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents:
24398
diff
changeset
|
649 PyMODINIT_FUNC |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
650 init_ssl(void) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
651 { |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
652 PyObject *m, *d; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
653 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
654 PySSL_Type.ob_type = &PyType_Type; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
655 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
656 m = Py_InitModule3("_ssl", PySSL_methods, module_doc); |
|
36324
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
32960
diff
changeset
|
657 if (m == NULL) |
|
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
32960
diff
changeset
|
658 return; |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
659 d = PyModule_GetDict(m); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
660 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
661 /* Load _socket module and its C API */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
662 if (PySocketModule_ImportModuleAndAPI()) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
663 return; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
664 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
665 /* Init OpenSSL */ |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
666 SSL_load_error_strings(); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
667 SSLeay_add_ssl_algorithms(); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
668 |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
669 /* Add symbols to module dict */ |
|
31790
29da8bdfeb4e
Make socket.sslerror a subclass of socket.error .
Brett Cannon <bcannon@gmail.com>
parents:
30795
diff
changeset
|
670 PySSLErrorObject = PyErr_NewException("socket.sslerror", |
|
29da8bdfeb4e
Make socket.sslerror a subclass of socket.error .
Brett Cannon <bcannon@gmail.com>
parents:
30795
diff
changeset
|
671 PySocketModule.error, |
|
29da8bdfeb4e
Make socket.sslerror a subclass of socket.error .
Brett Cannon <bcannon@gmail.com>
parents:
30795
diff
changeset
|
672 NULL); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
673 if (PySSLErrorObject == NULL) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
674 return; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
675 PyDict_SetItemString(d, "sslerror", PySSLErrorObject); |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
676 if (PyDict_SetItemString(d, "SSLType", |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
677 (PyObject *)&PySSL_Type) != 0) |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
678 return; |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
679 PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
680 PY_SSL_ERROR_ZERO_RETURN); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
681 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
682 PY_SSL_ERROR_WANT_READ); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
683 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
684 PY_SSL_ERROR_WANT_WRITE); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
685 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
686 PY_SSL_ERROR_WANT_X509_LOOKUP); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
687 PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
688 PY_SSL_ERROR_SYSCALL); |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
689 PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
|
23190
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
690 PY_SSL_ERROR_SSL); |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
691 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
692 PY_SSL_ERROR_WANT_CONNECT); |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
693 /* non ssl.h errorcodes */ |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
694 PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
695 PY_SSL_ERROR_EOF); |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
696 PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
697 PY_SSL_ERROR_INVALID_ERROR_CODE); |
|
0bc3a41d4e40
Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
698 |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
699 } |
