annotate Modules/_ssl.c @ 38664:7619216bbc19 2.4

Bug #1502728: Correctly link against librt library on HP-UX. (backport from rev. 46743)
author Georg Brandl <georg@python.org>
date Thu, 08 Jun 2006 12:54:16 +0000
parents a61d3afdfc4b
children 6fb46e87d5f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 BIO* sbio;
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 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
60 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
61
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
62 } PySSLObject;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
63
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24086
diff changeset
64 static PyTypeObject PySSL_Type;
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24086
diff changeset
65 static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args);
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24086
diff changeset
66 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
67 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
68 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
69
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
70 #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
71
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
72 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
73 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
74 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
75 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
76 SOCKET_HAS_BEEN_CLOSED,
36488
a2b6ddab66d7 Try to improve name based on discussion on python-checkins with Jim Jewett
Neal Norwitz <nnorwitz@gmail.com>
parents: 36486
diff changeset
77 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
78 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
79 } 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
80
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
81 /* 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
82 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
83 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
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
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
86 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
87 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
88 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
89 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
90 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
91 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
92 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
93
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
94 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
95
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
96 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
97
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 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
99 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
100 errstr = "TLS/SSL connection has been closed";
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
101 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
102 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
103 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
104 errstr = "The operation did not complete (read)";
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
105 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
106 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
107 case SSL_ERROR_WANT_WRITE:
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
108 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
109 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
110 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
111 case SSL_ERROR_WANT_X509_LOOKUP:
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
112 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
113 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
114 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
115 case SSL_ERROR_WANT_CONNECT:
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
116 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
117 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
118 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
119 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
120 {
0bc3a41d4e40 Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents: 21959
diff changeset
121 unsigned long e = ERR_get_error();
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
122 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
123 if (ret == 0 || !obj->Socket) {
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
124 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
125 errstr = "EOF occurred in violation of protocol";
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
126 } 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
127 /* 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
128 return obj->Socket->errorhandler();
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
129 } else { /* possible? */
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
130 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
131 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
132 }
0bc3a41d4e40 Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents: 21959
diff changeset
133 } else {
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
134 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
135 /* 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
136 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
137 }
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 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
139 }
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 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
141 {
0bc3a41d4e40 Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents: 21959
diff changeset
142 unsigned long e = ERR_get_error();
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
143 p = PY_SSL_ERROR_SSL;
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
144 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
145 /* 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
146 errstr = ERR_error_string(e, NULL);
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
147 else { /* possible? */
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
148 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
149 }
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 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
151 }
0bc3a41d4e40 Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents: 21959
diff changeset
152 default:
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
153 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
154 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
155 }
0bc3a41d4e40 Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents: 21959
diff changeset
156 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
157 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
158 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
159 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
160 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
161 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
162 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
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
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
165 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
166 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
167 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
168 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
169 }
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, 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
171 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
172 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
173 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
174 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
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
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
177 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
178 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
179 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
180 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
181 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
182 int ret;
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
183 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
184 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
185
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
186 self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
187 if (self == NULL){
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
188 errstr = "newPySSLObject error";
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
189 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
190 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
191 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
192 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
193 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
194 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
195 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
196 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
197
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
198 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
199 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
200 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
201 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
202
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
203 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
204 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
205 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
206 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
207 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
208 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
209 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
210
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
211 if (key_file) {
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
212 Py_BEGIN_ALLOW_THREADS
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
213 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
214 SSL_FILETYPE_PEM);
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
215 Py_END_ALLOW_THREADS
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
216 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
217 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
218 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
219 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
220
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
221 Py_BEGIN_ALLOW_THREADS
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
222 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
223 cert_file);
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
224 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
225 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
226 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
227 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
228 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
229 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
230 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
231
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
232 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
233 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
234 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
235 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
236 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
237 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
238
30723
6f15e73b59f2 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 29695
diff changeset
239 /* 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
240 * 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
241 */
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
242 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
243 /* 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
244 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
245 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
246 }
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
247
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
248 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
249 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
250 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
251
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
252 /* 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
253 /* 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
254 sockstate = 0;
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
255 do {
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
256 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
257 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
258 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
259 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
260 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
261 goto fail;
a4d6bc71f091 Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents: 27594
diff changeset
262 }
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
263 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
264 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
265 } 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
266 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
267 } 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
268 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
269 }
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 if (sockstate == 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
271 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
272 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
273 } 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
274 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
275 goto fail;
36488
a2b6ddab66d7 Try to improve name based on discussion on python-checkins with Jim Jewett
Neal Norwitz <nnorwitz@gmail.com>
parents: 36486
diff changeset
276 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
36453
af67b6265af6 Backport:
Neal Norwitz <nnorwitz@gmail.com>
parents: 32960
diff changeset
277 PyErr_SetString(PySSLErrorObject, "Underlying socket too large for select().");
af67b6265af6 Backport:
Neal Norwitz <nnorwitz@gmail.com>
parents: 32960
diff changeset
278 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
279 } 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
280 break;
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
281 }
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
282 } 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
283 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
284 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
285 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
286 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
287 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
288
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
289 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
290 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
291 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
292 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
293 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
294 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
295 }
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
296 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
297 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
298 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
299 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
300 fail:
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
301 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
302 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
303 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
304 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
305 }
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 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
308 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
309 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
310 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
311 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
312 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
313 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
314
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
315 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
316 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
317 (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
318 &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 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
320
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
321 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
322 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
323 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
324 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
325 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
326
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
327 PyDoc_STRVAR(ssl_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
328 "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
329
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
330 /* 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
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 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
333 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
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 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
336 }
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 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
339 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
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 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
342 }
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
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
345 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
346 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
347 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
348 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
349 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
350 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
351 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
352 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
353 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
354 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
355 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
356
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
357 /* 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
358 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
359 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
360 */
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
361
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
362 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
363 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
364 {
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
365 fd_set fds;
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
366 struct timeval tv;
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
367 int rc;
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
368
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
369 /* 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
370 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
371 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
372 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
373 return SOCKET_IS_NONBLOCKING;
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
374
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
375 /* Guard against closed socket */
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
376 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
377 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
378
36453
af67b6265af6 Backport:
Neal Norwitz <nnorwitz@gmail.com>
parents: 32960
diff changeset
379 /* Guard against socket too large for select*/
36486
65ae730b6231 Backport r42314 which fixed Windows breakage when checking that socket
Neal Norwitz <nnorwitz@gmail.com>
parents: 36453
diff changeset
380 #ifndef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
36453
af67b6265af6 Backport:
Neal Norwitz <nnorwitz@gmail.com>
parents: 32960
diff changeset
381 if (s->sock_fd >= FD_SETSIZE)
36488
a2b6ddab66d7 Try to improve name based on discussion on python-checkins with Jim Jewett
Neal Norwitz <nnorwitz@gmail.com>
parents: 36486
diff changeset
382 return SOCKET_TOO_LARGE_FOR_SELECT;
36486
65ae730b6231 Backport r42314 which fixed Windows breakage when checking that socket
Neal Norwitz <nnorwitz@gmail.com>
parents: 36453
diff changeset
383 #endif
36453
af67b6265af6 Backport:
Neal Norwitz <nnorwitz@gmail.com>
parents: 32960
diff changeset
384
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
385 /* Construct the arguments to select */
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
386 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
387 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
388 FD_ZERO(&fds);
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
389 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
390
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
391 /* 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
392 Py_BEGIN_ALLOW_THREADS
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
393 if (writing)
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
394 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
395 else
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
396 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
397 Py_END_ALLOW_THREADS
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
398
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
399 /* 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
400 (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
401 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
402 }
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
403
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
404 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
405 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
406 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
407 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
408 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
409 int sockstate;
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
410 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
411
30795
9da537167b74 Patch #803998: Correctly check for error in SSL_write.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
412 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
413 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
414
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
415 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
416 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
417 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
418 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
419 } 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
420 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
421 return NULL;
36488
a2b6ddab66d7 Try to improve name based on discussion on python-checkins with Jim Jewett
Neal Norwitz <nnorwitz@gmail.com>
parents: 36486
diff changeset
422 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
36453
af67b6265af6 Backport:
Neal Norwitz <nnorwitz@gmail.com>
parents: 32960
diff changeset
423 PyErr_SetString(PySSLErrorObject, "Underlying socket too large for select().");
af67b6265af6 Backport:
Neal Norwitz <nnorwitz@gmail.com>
parents: 32960
diff changeset
424 return NULL;
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
425 }
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
426 do {
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
427 err = 0;
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
428 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
429 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
430 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
431 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
432 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
433 return NULL;
a4d6bc71f091 Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents: 27594
diff changeset
434 }
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
435 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
436 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
437 } 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
438 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
439 } 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
440 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
441 }
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 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
443 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
444 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
445 } 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
446 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
447 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
448 } 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
449 break;
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
450 }
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
451 } 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
452 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
453 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
454 else
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
455 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
456 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
457
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
458 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
459 "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
460 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
461 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
462 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
463
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
464 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
465 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
466 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
467 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
468 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
469 int sockstate;
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
470 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
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 (!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
473 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
474
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
475 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
476 return NULL;
37340
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
477
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
478 /* first check if there are bytes ready to be read */
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
479 Py_BEGIN_ALLOW_THREADS
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
480 count = SSL_pending(self->ssl);
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
481 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
482
37340
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
483 if (!count) {
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
484 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
485 if (sockstate == SOCKET_HAS_TIMED_OUT) {
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
486 PyErr_SetString(PySSLErrorObject, "The read operation timed out");
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
487 Py_DECREF(buf);
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
488 return NULL;
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
489 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
490 PyErr_SetString(PySSLErrorObject, "Underlying socket too large for select().");
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
491 return NULL;
a61d3afdfc4b Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36488
diff changeset
492 }
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
493 }
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
494 do {
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
495 err = 0;
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
496 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
497 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
498 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
499 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
500 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
501 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
502 return NULL;
a4d6bc71f091 Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents: 27594
diff changeset
503 }
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
504 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
505 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
506 } 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
507 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
508 } 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
509 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
510 }
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
511 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
512 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
513 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
514 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
515 } 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
516 break;
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
517 }
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
518 } 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
519 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
520 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
521 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
522 }
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23190
diff changeset
523 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
524 _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
525 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
526 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
527
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
528 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
529 "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
530 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
531 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
532
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
533 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
534 {"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
535 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
536 {"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
537 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
538 {"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
539 {"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
540 {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
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
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
543 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
544 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
545 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
546 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
547
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24086
diff changeset
548 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
549 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
550 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
551 "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
552 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
553 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
554 /* methods */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
555 (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
556 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
557 (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
558 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
559 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
560 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
561 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
562 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
563 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
564 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
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
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
567 #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
568
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
569 /* 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
570 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
571 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
572 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
573 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
574 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
575 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
576
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
577 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
578 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
579 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
580 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
581 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
582 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
583
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
584 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
585 "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
586 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
587 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
588 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
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 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
591 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
592 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
593 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
594 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
595
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
596 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
597 "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
598 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
599 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
600 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
601 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
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 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
604 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
605 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
606 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
607
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
608 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
609 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
610 "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
611 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
612 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
613 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
614 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
615 "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
616 "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
617 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
618 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
619 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
620 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
621
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
622 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
623 "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
624 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
625 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
626 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
627 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
628
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
629 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
630
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
631 /* 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
632
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
633 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
634 {"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
635 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
636 #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
637 {"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
638 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
639 {"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
640 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
641 {"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
642 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
643 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
644 {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
645 };
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
646
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
647
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
648 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
649 "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
650 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
651
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24398
diff changeset
652 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
653 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
654 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
655 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
656
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
657 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
658
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
659 m = Py_InitModule3("_ssl", PySSL_methods, module_doc);
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
660 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
661
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
662 /* 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
663 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
664 return;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
665
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
666 /* 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
667 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
668 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
669
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
670 /* Add symbols to module dict */
31790
29da8bdfeb4e Make socket.sslerror a subclass of socket.error .
Brett Cannon <bcannon@gmail.com>
parents: 30795
diff changeset
671 PySSLErrorObject = PyErr_NewException("socket.sslerror",
29da8bdfeb4e Make socket.sslerror a subclass of socket.error .
Brett Cannon <bcannon@gmail.com>
parents: 30795
diff changeset
672 PySocketModule.error,
29da8bdfeb4e Make socket.sslerror a subclass of socket.error .
Brett Cannon <bcannon@gmail.com>
parents: 30795
diff changeset
673 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
674 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
675 return;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
676 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
677 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
678 (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
679 return;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
680 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
681 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
682 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
683 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
684 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
685 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
686 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
687 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
688 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
689 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
690 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
691 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
692 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
693 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
694 /* 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
695 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
696 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
697 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
698 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
699
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
700 }