~ubuntu-branches/ubuntu/karmic/m2crypto/karmic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
--- _ssl.i.orig	Tue Sep 18 06:06:34 2001
+++ _ssl.i	Thu Dec 12 08:57:39 2002
@@ -238,9 +238,18 @@
     return ret;
 }
 
+void SetWantRWError(int which) {
+    PyObject *o = Py_BuildValue("(is)", which, 
+                                
ERR_reason_error_string(ERR_get_error()));
+    if (o != NULL) {
+        PyErr_SetObject(_ssl_err, o);
+        Py_DECREF(o);
+    }
+}
+
 PyObject *ssl_accept(SSL *ssl) {
     PyObject *obj;
-    int r, err;
+    int r, err, which;
     PyThreadState *_save;
 
     if (thread_mode) {
@@ -252,14 +261,15 @@
         _save = (PyThreadState *)SSL_get_app_data(ssl);
         PyEval_RestoreThread(_save);
     }
-    switch (SSL_get_error(ssl, r)) {
+    switch ((which = SSL_get_error(ssl, r))) {
         case SSL_ERROR_NONE:
         case SSL_ERROR_ZERO_RETURN:
             obj = PyInt_FromLong((long)1);
             break;
         case SSL_ERROR_WANT_WRITE:
         case SSL_ERROR_WANT_READ:
-            obj = PyInt_FromLong((long)0);
+            SetWantRWError(which);
+            obj = NULL;
             break;
         case SSL_ERROR_SSL:
             PyErr_SetString(_ssl_err, 
ERR_reason_error_string(ERR_get_error()));
@@ -281,7 +291,7 @@
 
 PyObject *ssl_connect(SSL *ssl) {
     PyObject *obj;
-    int r, err;
+    int r, err, which;
     PyThreadState *_save;
 
     if (thread_mode) {
@@ -293,14 +303,15 @@
         _save = (PyThreadState *)SSL_get_app_data(ssl);
         PyEval_RestoreThread(_save);
     }
-    switch (SSL_get_error(ssl, r)) {
+    switch ((which = SSL_get_error(ssl, r))) {
         case SSL_ERROR_NONE:
         case SSL_ERROR_ZERO_RETURN:
             obj = PyInt_FromLong((long)1);
             break;
         case SSL_ERROR_WANT_WRITE:
         case SSL_ERROR_WANT_READ:
-            obj = PyInt_FromLong((long)0);
+            SetWantRWError(which);
+            obj = NULL;
             break;
         case SSL_ERROR_SSL:
             PyErr_SetString(_ssl_err, 
ERR_reason_error_string(ERR_get_error()));
@@ -327,7 +338,7 @@
 PyObject *ssl_read(SSL *ssl, int num) {
     PyObject *obj;
     void *buf;
-    int r, err;
+    int r, err, which;
     PyThreadState *_save;
 
     if (!(buf = PyMem_Malloc(num))) {
@@ -343,7 +354,7 @@
         _save = (PyThreadState *)SSL_get_app_data(ssl);
         PyEval_RestoreThread(_save);
     }
-    switch (SSL_get_error(ssl, r)) {
+    switch ((which = SSL_get_error(ssl, r))) {
         case SSL_ERROR_NONE:
         case SSL_ERROR_ZERO_RETURN:
             buf = PyMem_Realloc(buf, r);
@@ -352,8 +363,8 @@
         case SSL_ERROR_WANT_WRITE:
         case SSL_ERROR_WANT_READ:
         case SSL_ERROR_WANT_X509_LOOKUP:
-            Py_INCREF(Py_None);
-            obj = Py_None;
+            SetWantRWError(which);
+            obj = NULL;
             break;
         case SSL_ERROR_SSL:
             PyErr_SetString(_ssl_err, 
ERR_reason_error_string(ERR_get_error()));
@@ -377,14 +388,14 @@
 PyObject *ssl_read_nbio(SSL *ssl, int num) {
     PyObject *obj;
     void *buf;
-    int r, err;
+    int r, err, which;
 
     if (!(buf = PyMem_Malloc(num))) {
         PyErr_SetString(PyExc_MemoryError, "ssl_read");
         return NULL;
     }
     r = SSL_read(ssl, buf, num);
-    switch (SSL_get_error(ssl, r)) {
+    switch ((which = SSL_get_error(ssl, r))) {
         case SSL_ERROR_NONE:
         case SSL_ERROR_ZERO_RETURN:
             buf = PyMem_Realloc(buf, r);
@@ -392,6 +403,9 @@
             break;
         case SSL_ERROR_WANT_WRITE:
         case SSL_ERROR_WANT_READ:
+            SetWantRWError(which);
+            obj = NULL;
+            break;
         case SSL_ERROR_WANT_X509_LOOKUP:
             Py_INCREF(Py_None);
             obj = Py_None;
@@ -417,7 +431,7 @@
 
 int ssl_write(SSL *ssl, PyObject *blob) {
     const void *buf;
-    int len, r, err;
+    int len, r, err, which;
     PyThreadState *_save;
 
 #if PYTHON_API_VERSION >= 1009
@@ -440,12 +454,14 @@
         _save = (PyThreadState *)SSL_get_app_data(ssl);
         PyEval_RestoreThread(_save);
     }
-    switch (SSL_get_error(ssl, r)) {
+    switch ((which = SSL_get_error(ssl, r))) {
         case SSL_ERROR_NONE:
         case SSL_ERROR_ZERO_RETURN:
             return r;
         case SSL_ERROR_WANT_WRITE:
         case SSL_ERROR_WANT_READ:
+            SetWantRWError(which);
+            return -1;
         case SSL_ERROR_WANT_X509_LOOKUP:
             return -1;
         case SSL_ERROR_SSL:
@@ -466,7 +482,7 @@
 
 int ssl_write_nbio(SSL *ssl, PyObject *blob) {
     const void *buf;
-    int len, r, err;
+    int len, r, err, which;
 
 #if PYTHON_API_VERSION >= 1009
     if (PyObject_AsReadBuffer(blob, &buf, &len) == -1)
@@ -480,12 +496,14 @@
     buf = (const void *)PyString_AsString(blob);
 #endif
     r = SSL_write(ssl, buf, len);
-    switch (SSL_get_error(ssl, r)) {
+    switch ((which = SSL_get_error(ssl, r))) {
         case SSL_ERROR_NONE:
         case SSL_ERROR_ZERO_RETURN:
             return r;
         case SSL_ERROR_WANT_WRITE:
         case SSL_ERROR_WANT_READ:
+            SetWantRWError(which);
+            return -1;
         case SSL_ERROR_WANT_X509_LOOKUP:
             return -1;
         case SSL_ERROR_SSL: