comparison Modules/socketmodule.c @ 24087:355e46cc124f 2.1

Backport variety of SSL fixes accumulated on the trunk.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Tue, 02 Jul 2002 19:05:33 +0000
parents e69b21f2529e
children
comparison
equal deleted inserted replaced
23966:25b0af27ddc8 24087:355e46cc124f
187 /* fdopen() isn't declared in stdio.h (sigh) */ 187 /* fdopen() isn't declared in stdio.h (sigh) */
188 #include <GUSI.h> 188 #include <GUSI.h>
189 #endif 189 #endif
190 190
191 #ifdef USE_SSL 191 #ifdef USE_SSL
192 enum py_ssl_error {
193 /* these mirror ssl.h */
194 PY_SSL_ERROR_NONE,
195 PY_SSL_ERROR_SSL,
196 PY_SSL_ERROR_WANT_READ,
197 PY_SSL_ERROR_WANT_WRITE,
198 PY_SSL_ERROR_WANT_X509_LOOKUP,
199 PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */
200 PY_SSL_ERROR_ZERO_RETURN,
201 PY_SSL_ERROR_WANT_CONNECT,
202 /* start of non ssl.h errorcodes */
203 PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */
204 PY_SSL_ERROR_INVALID_ERROR_CODE
205 };
206
192 #include "openssl/rsa.h" 207 #include "openssl/rsa.h"
193 #include "openssl/crypto.h" 208 #include "openssl/crypto.h"
194 #include "openssl/x509.h" 209 #include "openssl/x509.h"
195 #include "openssl/pem.h" 210 #include "openssl/pem.h"
196 #include "openssl/ssl.h" 211 #include "openssl/ssl.h"
197 #include "openssl/err.h" 212 #include "openssl/err.h"
213
214 #define X509_NAME_MAXLEN 256
215
216 /* RAND_* APIs got added to OpenSSL in 0.9.5 */
217 #if OPENSSL_VERSION_NUMBER >= 0x0090500fL
218 # define HAVE_OPENSSL_RAND 1
219 #include "openssl/rand.h"
220 #else
221 # undef HAVE_OPENSSL_RAND
222 #endif
223
198 #endif /* USE_SSL */ 224 #endif /* USE_SSL */
199 225
200 #if defined(MS_WINDOWS) || defined(__BEOS__) 226 #if defined(MS_WINDOWS) || defined(__BEOS__)
201 /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */ 227 /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
202 /* seem to be a few differences in the API */ 228 /* seem to be a few differences in the API */
231 by this module (but not argument type or memory errors, etc.). */ 257 by this module (but not argument type or memory errors, etc.). */
232 258
233 static PyObject *PySocket_Error; 259 static PyObject *PySocket_Error;
234 260
235 #ifdef USE_SSL 261 #ifdef USE_SSL
236 static PyObject *SSLErrorObject; 262 static PyObject *PySSLErrorObject;
237 #endif /* USE_SSL */ 263 #endif /* USE_SSL */
238 264
239 265
240 #ifdef RISCOS 266 #ifdef RISCOS
241 /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */ 267 /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
395 #ifdef USE_SSL 421 #ifdef USE_SSL
396 422
397 typedef struct { 423 typedef struct {
398 PyObject_HEAD 424 PyObject_HEAD
399 PySocketSockObject *Socket; /* Socket on which we're layered */ 425 PySocketSockObject *Socket; /* Socket on which we're layered */
400 PyObject *x_attr; /* Attributes dictionary */
401 SSL_CTX* ctx; 426 SSL_CTX* ctx;
402 SSL* ssl; 427 SSL* ssl;
403 X509* server_cert; 428 X509* server_cert;
404 BIO* sbio; 429 BIO* sbio;
405 char server[256]; 430 char server[X509_NAME_MAXLEN];
406 char issuer[256]; 431 char issuer[X509_NAME_MAXLEN];
407 432
408 } SSLObject; 433 } PySSLObject;
409 434
410 staticforward PyTypeObject SSL_Type; 435 staticforward PyTypeObject PySSL_Type;
411 staticforward PyObject *SSL_SSLwrite(SSLObject *self, PyObject *args); 436 staticforward PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args);
412 staticforward PyObject *SSL_SSLread(SSLObject *self, PyObject *args); 437 staticforward PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args);
413 438
414 #define SSLObject_Check(v) ((v)->ob_type == &SSL_Type) 439 #define PySSLObject_Check(v) ((v)->ob_type == &PySSL_Type)
415 440
416 #endif /* USE_SSL */ 441 #endif /* USE_SSL */
417 442
418 /* A forward reference to the Socktype type object. 443 /* A forward reference to the Socktype type object.
419 The Socktype variable contains pointers to various functions, 444 The Socktype variable contains pointers to various functions,
2160 } 2185 }
2161 2186
2162 2187
2163 #ifdef USE_SSL 2188 #ifdef USE_SSL
2164 2189
2165 /* This is a C function to be called for new object initialization */ 2190 /* XXX It might be helpful to augment the error message generated
2166 static SSLObject * 2191 below with the name of the SSL function that generated the error.
2167 newSSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file) 2192 I expect it's obvious most of the time.
2168 { 2193 */
2169 SSLObject *self; 2194
2170 2195 static PyObject *
2171 self = PyObject_New(SSLObject, &SSL_Type); /* Create new object */ 2196 PySSL_SetError(PySSLObject *obj, int ret)
2197 {
2198 PyObject *v, *n, *s;
2199 char *errstr;
2200 int err;
2201 enum py_ssl_error p;
2202
2203 assert(ret <= 0);
2204
2205 err = SSL_get_error(obj->ssl, ret);
2206
2207 switch (err) {
2208 case SSL_ERROR_ZERO_RETURN:
2209 errstr = "TLS/SSL connection has been closed";
2210 p = PY_SSL_ERROR_ZERO_RETURN;
2211 break;
2212 case SSL_ERROR_WANT_READ:
2213 errstr = "The operation did not complete (read)";
2214 p = PY_SSL_ERROR_WANT_READ;
2215 break;
2216 case SSL_ERROR_WANT_WRITE:
2217 p = PY_SSL_ERROR_WANT_WRITE;
2218 errstr = "The operation did not complete (write)";
2219 break;
2220 case SSL_ERROR_WANT_X509_LOOKUP:
2221 p = PY_SSL_ERROR_WANT_X509_LOOKUP;
2222 errstr = "The operation did not complete (X509 lookup)";
2223 break;
2224 case SSL_ERROR_WANT_CONNECT:
2225 p = PY_SSL_ERROR_WANT_CONNECT;
2226 errstr = "The operation did not complete (connect)";
2227 break;
2228 case SSL_ERROR_SYSCALL:
2229 {
2230 unsigned long e = ERR_get_error();
2231 if (e == 0) {
2232 if (ret == 0) {
2233 p = PY_SSL_ERROR_EOF;
2234 errstr = "EOF occurred in violation of protocol";
2235 } else if (ret == -1) {
2236 /* the underlying BIO reported an I/O error */
2237 return PySocket_Err();
2238 } else { /* possible? */
2239 p = PY_SSL_ERROR_SYSCALL;
2240 errstr = "Some I/O error occurred";
2241 }
2242 } else {
2243 p = PY_SSL_ERROR_SYSCALL;
2244 /* XXX Protected by global interpreter lock */
2245 errstr = ERR_error_string(e, NULL);
2246 }
2247 break;
2248 }
2249 case SSL_ERROR_SSL:
2250 {
2251 unsigned long e = ERR_get_error();
2252 p = PY_SSL_ERROR_SSL;
2253 if (e != 0)
2254 /* XXX Protected by global interpreter lock */
2255 errstr = ERR_error_string(e, NULL);
2256 else { /* possible? */
2257 errstr = "A failure in the SSL library occurred";
2258 }
2259 break;
2260 }
2261 default:
2262 p = PY_SSL_ERROR_INVALID_ERROR_CODE;
2263 errstr = "Invalid error code";
2264 }
2265 n = PyInt_FromLong((long) p);
2266 if (n == NULL)
2267 return NULL;
2268 v = PyTuple_New(2);
2269 if (v == NULL) {
2270 Py_DECREF(n);
2271 return NULL;
2272 }
2273
2274 s = PyString_FromString(errstr);
2275 if (s == NULL) {
2276 Py_DECREF(v);
2277 Py_DECREF(n);
2278 }
2279 PyTuple_SET_ITEM(v, 0, n);
2280 PyTuple_SET_ITEM(v, 1, s);
2281 PyErr_SetObject(PySSLErrorObject, v);
2282 return NULL;
2283 }
2284
2285 static PySSLObject *
2286 newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file)
2287 {
2288 PySSLObject *self;
2289 char *errstr = NULL;
2290 int ret;
2291
2292 self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */
2172 if (self == NULL){ 2293 if (self == NULL){
2173 PyErr_SetObject(SSLErrorObject, 2294 errstr = "newPySSLObject error";
2174 PyString_FromString("newSSLObject error")); 2295 goto fail;
2175 return NULL; 2296 }
2176 } 2297 memset(self->server, '\0', sizeof(char) * X509_NAME_MAXLEN);
2177 memset(self->server, '\0', sizeof(char) * 256); 2298 memset(self->issuer, '\0', sizeof(char) * X509_NAME_MAXLEN);
2178 memset(self->issuer, '\0', sizeof(char) * 256); 2299 self->server_cert = NULL;
2179 2300 self->ssl = NULL;
2180 self->x_attr = PyDict_New(); 2301 self->ctx = NULL;
2302 self->Socket = NULL;
2303
2304 if ((key_file && !cert_file) || (!key_file && cert_file)) {
2305 errstr = "Both the key & certificate files must be specified";
2306 goto fail;
2307 }
2308
2181 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */ 2309 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
2182 if (self->ctx == NULL) { 2310 if (self->ctx == NULL) {
2183 PyErr_SetObject(SSLErrorObject, 2311 errstr = "SSL_CTX_new error";
2184 PyString_FromString("SSL_CTX_new error")); 2312 goto fail;
2185 PyObject_Del(self); 2313 }
2186 return NULL; 2314
2187 } 2315 if (key_file) {
2188
2189 if ( (key_file && !cert_file) || (!key_file && cert_file) )
2190 {
2191 PyErr_SetObject(SSLErrorObject,
2192 PyString_FromString(
2193 "Both the key & certificate files must be specified"));
2194 PyObject_Del(self);
2195 return NULL;
2196 }
2197
2198 if (key_file && cert_file)
2199 {
2200 if (SSL_CTX_use_PrivateKey_file(self->ctx, key_file, 2316 if (SSL_CTX_use_PrivateKey_file(self->ctx, key_file,
2201 SSL_FILETYPE_PEM) < 1) 2317 SSL_FILETYPE_PEM) < 1) {
2202 { 2318 errstr = "SSL_CTX_use_PrivateKey_file error";
2203 PyErr_SetObject(SSLErrorObject, 2319 goto fail;
2204 PyString_FromString(
2205 "SSL_CTX_use_PrivateKey_file error"));
2206 PyObject_Del(self);
2207 return NULL;
2208 } 2320 }
2209 2321
2210 if (SSL_CTX_use_certificate_chain_file(self->ctx, 2322 if (SSL_CTX_use_certificate_chain_file(self->ctx,
2211 cert_file) < 1) 2323 cert_file) < 1) {
2212 { 2324 errstr = "SSL_CTX_use_certificate_chain_file error";
2213 PyErr_SetObject(SSLErrorObject, 2325 goto fail;
2214 PyString_FromString(
2215 "SSL_CTX_use_certificate_chain_file error"));
2216 PyObject_Del(self);
2217 return NULL;
2218 } 2326 }
2219 } 2327 }
2220 2328
2221 SSL_CTX_set_verify(self->ctx, 2329 SSL_CTX_set_verify(self->ctx,
2222 SSL_VERIFY_NONE, NULL); /* set verify lvl */ 2330 SSL_VERIFY_NONE, NULL); /* set verify lvl */
2223 self->ssl = SSL_new(self->ctx); /* New ssl struct */ 2331 self->ssl = SSL_new(self->ctx); /* New ssl struct */
2224 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */ 2332 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
2225 SSL_set_connect_state(self->ssl); 2333 SSL_set_connect_state(self->ssl);
2226 2334
2227 if ((SSL_connect(self->ssl)) == -1) { 2335 /* Actually negotiate SSL connection */
2228 /* Actually negotiate SSL connection */ 2336 /* XXX If SSL_connect() returns 0, it's also a failure. */
2229 PyErr_SetObject(SSLErrorObject, 2337 ret = SSL_connect(self->ssl);
2230 PyString_FromString("SSL_connect error")); 2338 if (ret <= 0) {
2231 PyObject_Del(self); 2339 PySSL_SetError(self, ret);
2232 return NULL; 2340 goto fail;
2233 } 2341 }
2234 self->ssl->debug = 1; 2342 self->ssl->debug = 1;
2235 2343
2236 if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) { 2344 if ((self->server_cert = SSL_get_peer_certificate(self->ssl))) {
2237 X509_NAME_oneline(X509_get_subject_name(self->server_cert), 2345 X509_NAME_oneline(X509_get_subject_name(self->server_cert),
2238 self->server, 256); 2346 self->server, X509_NAME_MAXLEN);
2239 X509_NAME_oneline(X509_get_issuer_name(self->server_cert), 2347 X509_NAME_oneline(X509_get_issuer_name(self->server_cert),
2240 self->issuer, 256); 2348 self->issuer, X509_NAME_MAXLEN);
2241 } 2349 }
2242 self->x_attr = NULL;
2243 self->Socket = Sock; 2350 self->Socket = Sock;
2244 Py_INCREF(self->Socket); 2351 Py_INCREF(self->Socket);
2245 return self; 2352 return self;
2246 } 2353 fail:
2247 2354 if (errstr)
2248 /* This is the Python function called for new object initialization */ 2355 PyErr_SetString(PySSLErrorObject, errstr);
2356 Py_DECREF(self);
2357 return NULL;
2358 }
2359
2360
2249 static PyObject * 2361 static PyObject *
2250 PySocket_ssl(PyObject *self, PyObject *args) 2362 PySocket_ssl(PyObject *self, PyObject *args)
2251 { 2363 {
2252 SSLObject *rv; 2364 PySSLObject *rv;
2253 PySocketSockObject *Sock; 2365 PySocketSockObject *Sock;
2254 char *key_file; 2366 char *key_file = NULL;
2255 char *cert_file; 2367 char *cert_file = NULL;
2256 2368
2257 if (!PyArg_ParseTuple(args, "O!zz:ssl", 2369 if (!PyArg_ParseTuple(args, "O!|zz:ssl", &PySocketSock_Type,
2258 &PySocketSock_Type, (PyObject*)&Sock, 2370 (PyObject*)&Sock, &key_file, &cert_file))
2259 &key_file, &cert_file) ) 2371 return NULL;
2260 return NULL; 2372
2261 2373 rv = newPySSLObject(Sock, key_file, cert_file);
2262 rv = newSSLObject(Sock, key_file, cert_file); 2374 if (rv == NULL)
2263 if ( rv == NULL )
2264 return NULL; 2375 return NULL;
2265 return (PyObject *)rv; 2376 return (PyObject *)rv;
2266 } 2377 }
2267 2378
2268 static char ssl_doc[] = 2379 static char ssl_doc[] = "ssl(socket, [keyfile, certfile]) -> sslobject";
2269 "ssl(socket, keyfile, certfile) -> sslobject"; 2380
2270 2381 /* SSL object methods */
2271 static PyObject * 2382
2272 SSL_server(SSLObject *self, PyObject *args) 2383 static PyObject *
2273 { 2384 PySSL_server(PySSLObject *self, PyObject *args)
2385 {
2386 if (!PyArg_ParseTuple(args, ":server"))
2387 return NULL;
2274 return PyString_FromString(self->server); 2388 return PyString_FromString(self->server);
2275 } 2389 }
2276 2390
2277 static PyObject * 2391 static PyObject *
2278 SSL_issuer(SSLObject *self, PyObject *args) 2392 PySSL_issuer(PySSLObject *self, PyObject *args)
2279 { 2393 {
2394 if (!PyArg_ParseTuple(args, ":issuer"))
2395 return NULL;
2280 return PyString_FromString(self->issuer); 2396 return PyString_FromString(self->issuer);
2281 } 2397 }
2282 2398
2283 2399
2284 /* SSL object methods */ 2400 static void PySSL_dealloc(PySSLObject *self)
2285
2286 static PyMethodDef SSLMethods[] = {
2287 { "write", (PyCFunction)SSL_SSLwrite, 1 },
2288 { "read", (PyCFunction)SSL_SSLread, 1 },
2289 { "server", (PyCFunction)SSL_server, 1 },
2290 { "issuer", (PyCFunction)SSL_issuer, 1 },
2291 { NULL, NULL}
2292 };
2293
2294 static void SSL_dealloc(SSLObject *self)
2295 { 2401 {
2296 if (self->server_cert) /* Possible not to have one? */ 2402 if (self->server_cert) /* Possible not to have one? */
2297 X509_free (self->server_cert); 2403 X509_free (self->server_cert);
2298 SSL_CTX_free(self->ctx); 2404 if (self->ssl)
2299 SSL_free(self->ssl); 2405 SSL_free(self->ssl);
2300 Py_XDECREF(self->x_attr); 2406 if (self->ctx)
2407 SSL_CTX_free(self->ctx);
2301 Py_XDECREF(self->Socket); 2408 Py_XDECREF(self->Socket);
2302 PyObject_Del(self); 2409 PyObject_Del(self);
2303 } 2410 }
2304 2411
2305 static PyObject *SSL_getattr(SSLObject *self, char *name) 2412 static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args)
2306 { 2413 {
2307 return Py_FindMethod(SSLMethods, (PyObject *)self, name); 2414 char *data;
2308 } 2415 int len;
2309 2416
2310 staticforward PyTypeObject SSL_Type = { 2417 if (!PyArg_ParseTuple(args, "s#:write", &data, &len))
2418 return NULL;
2419
2420 Py_BEGIN_ALLOW_THREADS
2421 len = SSL_write(self->ssl, data, len);
2422 Py_END_ALLOW_THREADS
2423 if (len > 0)
2424 return PyInt_FromLong(len);
2425 else
2426 return PySSL_SetError(self, len);
2427 }
2428
2429 static char PySSL_SSLwrite_doc[] = \
2430 "write(s) -> len\n\
2431 \n\
2432 Writes the string s into the SSL object. Returns the number\n\
2433 of bytes written.";
2434
2435 static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args)
2436 {
2437 PyObject *buf;
2438 int count = 0;
2439 int len = 1024;
2440
2441 if (!PyArg_ParseTuple(args, "|i:read", &len))
2442 return NULL;
2443
2444 if (!(buf = PyString_FromStringAndSize((char *) 0, len)))
2445 return NULL;
2446
2447 Py_BEGIN_ALLOW_THREADS
2448 count = SSL_read(self->ssl, PyString_AsString(buf), len);
2449 Py_END_ALLOW_THREADS
2450 if (count <= 0) {
2451 Py_DECREF(buf);
2452 return PySSL_SetError(self, count);
2453 }
2454 if (count != len)
2455 _PyString_Resize(&buf, count);
2456 return buf;
2457 }
2458
2459 static char PySSL_SSLread_doc[] = \
2460 "read([len]) -> string\n\
2461 \n\
2462 Read up to len bytes from the SSL socket.";
2463
2464 static PyMethodDef PySSLMethods[] = {
2465 {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS,
2466 PySSL_SSLwrite_doc},
2467 {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS,
2468 PySSL_SSLread_doc},
2469 {"server", (PyCFunction)PySSL_server, METH_VARARGS},
2470 {"issuer", (PyCFunction)PySSL_issuer, METH_VARARGS},
2471 {NULL, NULL}
2472 };
2473
2474 static PyObject *PySSL_getattr(PySSLObject *self, char *name)
2475 {
2476 return Py_FindMethod(PySSLMethods, (PyObject *)self, name);
2477 }
2478
2479 staticforward PyTypeObject PySSL_Type = {
2311 PyObject_HEAD_INIT(NULL) 2480 PyObject_HEAD_INIT(NULL)
2312 0, /*ob_size*/ 2481 0, /*ob_size*/
2313 "SSL", /*tp_name*/ 2482 "socket.SSL", /*tp_name*/
2314 sizeof(SSLObject), /*tp_basicsize*/ 2483 sizeof(PySSLObject), /*tp_basicsize*/
2315 0, /*tp_itemsize*/ 2484 0, /*tp_itemsize*/
2316 /* methods */ 2485 /* methods */
2317 (destructor)SSL_dealloc, /*tp_dealloc*/ 2486 (destructor)PySSL_dealloc, /*tp_dealloc*/
2318 0, /*tp_print*/ 2487 0, /*tp_print*/
2319 (getattrfunc)SSL_getattr, /*tp_getattr*/ 2488 (getattrfunc)PySSL_getattr, /*tp_getattr*/
2320 0, /*tp_setattr*/ 2489 0, /*tp_setattr*/
2321 0, /*tp_compare*/ 2490 0, /*tp_compare*/
2322 0, /*tp_repr*/ 2491 0, /*tp_repr*/
2323 0, /*tp_as_number*/ 2492 0, /*tp_as_number*/
2324 0, /*tp_as_sequence*/ 2493 0, /*tp_as_sequence*/
2325 0, /*tp_as_mapping*/ 2494 0, /*tp_as_mapping*/
2326 0, /*tp_hash*/ 2495 0, /*tp_hash*/
2327 }; 2496 };
2328 2497
2329 2498 #ifdef HAVE_OPENSSL_RAND
2330 2499
2331 static PyObject *SSL_SSLwrite(SSLObject *self, PyObject *args) 2500 /* helper routines for seeding the SSL PRNG */
2332 { 2501 static PyObject *
2333 char *data; 2502 PySSL_RAND_add(PyObject *self, PyObject *args)
2334 size_t len; 2503 {
2335 2504 char *buf;
2336 if (!PyArg_ParseTuple(args, "s#:write", &data, &len)) 2505 int len;
2337 return NULL; 2506 double entropy;
2338 2507
2339 Py_BEGIN_ALLOW_THREADS 2508 if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy))
2340 len = SSL_write(self->ssl, data, len); 2509 return NULL;
2341 Py_END_ALLOW_THREADS 2510 RAND_add(buf, len, entropy);
2342 return PyInt_FromLong((long)len); 2511 Py_INCREF(Py_None);
2343 } 2512 return Py_None;
2344 2513 }
2345 static PyObject *SSL_SSLread(SSLObject *self, PyObject *args) 2514
2346 { 2515 static char PySSL_RAND_add_doc[] = \
2347 PyObject *buf; 2516 "RAND_add(string, entropy)\n\
2348 int count = 0; 2517 \n\
2349 int len = 1024; 2518 Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\
2350 int res; 2519 bound on the entropy contained in string.";
2351 2520
2352 PyArg_ParseTuple(args, "|i:read", &len); 2521 static PyObject *
2353 2522 PySSL_RAND_status(PyObject *self)
2354 if (!(buf = PyString_FromStringAndSize((char *) 0, len))) 2523 {
2355 return NULL; /* Error object should already be set */ 2524 return PyInt_FromLong(RAND_status());
2356 2525 }
2357 Py_BEGIN_ALLOW_THREADS 2526
2358 count = SSL_read(self->ssl, PyString_AsString(buf), len); 2527 static char PySSL_RAND_status_doc[] = \
2359 Py_END_ALLOW_THREADS 2528 "RAND_status() -> 0 or 1\n\
2360 res = SSL_get_error(self->ssl, count); 2529 \n\
2361 2530 Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\
2362 switch (res) { 2531 It is necessary to seed the PRNG with RAND_add() on some platforms before\n\
2363 case SSL_ERROR_NONE: 2532 using the ssl() function.";
2364 assert(count > 0); 2533
2365 break; 2534 static PyObject *
2366 case SSL_ERROR_ZERO_RETURN: /* normal EOF */ 2535 PySSL_RAND_egd(PyObject *self, PyObject *args)
2367 assert(count == 0); 2536 {
2368 break; 2537 int bytes;
2369 default: 2538 char *s;
2370 Py_DECREF(buf); 2539
2371 return PyErr_SetFromErrno(SSLErrorObject); 2540 if (!PyArg_ParseTuple(args, "s:RAND_egd", &s))
2372 } 2541 return NULL;
2373 2542
2374 fflush(stderr); 2543 bytes = RAND_egd(s);
2375 2544 if (bytes == -1) {
2376 if (count < 0) { 2545 PyErr_SetString(PySSLErrorObject,
2377 Py_DECREF(buf); 2546 "EGD connection failed or EGD did not return "
2378 return PyErr_SetFromErrno(SSLErrorObject); 2547 "enough data to seed the PRNG");
2379 } 2548 return NULL;
2380 2549 }
2381 if (count != len && _PyString_Resize(&buf, count) < 0) 2550 return PyInt_FromLong(bytes);
2382 return NULL; 2551 }
2383 return buf; 2552
2384 } 2553 static char PySSL_RAND_egd_doc[] = \
2554 "RAND_egd(path) -> bytes\n\
2555 \n\
2556 Queries the entropy gather daemon (EGD) on socket path. Returns number\n\
2557 of bytes read. Raises socket.sslerror if connection to EGD fails or\n\
2558 if it does provide enough data to seed PRNG.";
2559
2560 #endif
2385 2561
2386 #endif /* USE_SSL */ 2562 #endif /* USE_SSL */
2387 2563
2388 2564
2389 /* List of functions exported by this module. */ 2565 /* List of functions exported by this module. */
2420 {"inet_ntoa", PySocket_inet_ntoa, 2596 {"inet_ntoa", PySocket_inet_ntoa,
2421 METH_VARARGS, inet_ntoa_doc}, 2597 METH_VARARGS, inet_ntoa_doc},
2422 #ifdef USE_SSL 2598 #ifdef USE_SSL
2423 {"ssl", PySocket_ssl, 2599 {"ssl", PySocket_ssl,
2424 METH_VARARGS, ssl_doc}, 2600 METH_VARARGS, ssl_doc},
2601 #ifdef HAVE_OPENSSL_RAND
2602 {"RAND_add", PySSL_RAND_add, METH_VARARGS,
2603 PySSL_RAND_add_doc},
2604 {"RAND_egd", PySSL_RAND_egd, METH_VARARGS,
2605 PySSL_RAND_egd_doc},
2606 {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_VARARGS,
2607 PySSL_RAND_status_doc},
2608 #endif
2425 #endif /* USE_SSL */ 2609 #endif /* USE_SSL */
2426 {NULL, NULL} /* Sentinel */ 2610 {NULL, NULL} /* Sentinel */
2427 }; 2611 };
2428 2612
2429 2613
2580 return; 2764 return;
2581 #endif /* __TOS_OS2__ */ 2765 #endif /* __TOS_OS2__ */
2582 #endif /* MS_WINDOWS */ 2766 #endif /* MS_WINDOWS */
2583 #endif /* RISCOS */ 2767 #endif /* RISCOS */
2584 #ifdef USE_SSL 2768 #ifdef USE_SSL
2585 SSL_Type.ob_type = &PyType_Type; 2769 PySSL_Type.ob_type = &PyType_Type;
2586 #endif 2770 #endif
2587 m = Py_InitModule3("_socket", PySocket_methods, module_doc); 2771 m = Py_InitModule3("_socket", PySocket_methods, module_doc);
2588 d = PyModule_GetDict(m); 2772 d = PyModule_GetDict(m);
2589 PySocket_Error = PyErr_NewException("socket.error", NULL, NULL); 2773 PySocket_Error = PyErr_NewException("socket.error", NULL, NULL);
2590 if (PySocket_Error == NULL) 2774 if (PySocket_Error == NULL)
2591 return; 2775 return;
2592 #ifdef USE_SSL 2776 #ifdef USE_SSL
2593 SSL_load_error_strings(); 2777 SSL_load_error_strings();
2594 SSLeay_add_ssl_algorithms(); 2778 SSLeay_add_ssl_algorithms();
2595 SSLErrorObject = PyErr_NewException("socket.sslerror", NULL, NULL); 2779 PySSLErrorObject = PyErr_NewException("socket.sslerror", NULL, NULL);
2596 if (SSLErrorObject == NULL) 2780 if (PySSLErrorObject == NULL)
2597 return; 2781 return;
2598 PyDict_SetItemString(d, "sslerror", SSLErrorObject); 2782 PyDict_SetItemString(d, "sslerror", PySSLErrorObject);
2599 Py_INCREF(&SSL_Type); 2783 Py_INCREF(&PySSL_Type);
2600 if (PyDict_SetItemString(d, "SSLType", 2784 if (PyDict_SetItemString(d, "SSLType",
2601 (PyObject *)&SSL_Type) != 0) 2785 (PyObject *)&PySSL_Type) != 0)
2602 return; 2786 return;
2603 #endif /* USE_SSL */ 2787 #endif /* USE_SSL */
2604 PyDict_SetItemString(d, "error", PySocket_Error); 2788 PyDict_SetItemString(d, "error", PySocket_Error);
2605 PySocketSock_Type.ob_type = &PyType_Type; 2789 PySocketSock_Type.ob_type = &PyType_Type;
2606 PySocketSock_Type.tp_doc = sockettype_doc; 2790 PySocketSock_Type.tp_doc = sockettype_doc;
2967 /* IPX options */ 3151 /* IPX options */
2968 #ifdef IPX_TYPE 3152 #ifdef IPX_TYPE
2969 insint(d, "IPX_TYPE", IPX_TYPE); 3153 insint(d, "IPX_TYPE", IPX_TYPE);
2970 #endif 3154 #endif
2971 3155
3156 #ifdef USE_SSL
3157 PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN",
3158 PY_SSL_ERROR_ZERO_RETURN);
3159 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ",
3160 PY_SSL_ERROR_WANT_READ);
3161 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE",
3162 PY_SSL_ERROR_WANT_WRITE);
3163 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP",
3164 PY_SSL_ERROR_WANT_X509_LOOKUP);
3165 PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL",
3166 PY_SSL_ERROR_SYSCALL);
3167 PyModule_AddIntConstant(m, "SSL_ERROR_SSL",
3168 PY_SSL_ERROR_SSL);
3169 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT",
3170 PY_SSL_ERROR_WANT_CONNECT);
3171 /* non ssl.h errorcodes */
3172 PyModule_AddIntConstant(m, "SSL_ERROR_EOF",
3173 PY_SSL_ERROR_EOF);
3174 PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE",
3175 PY_SSL_ERROR_INVALID_ERROR_CODE);
3176 #endif
3177
2972 /* Initialize gethostbyname lock */ 3178 /* Initialize gethostbyname lock */
2973 #ifdef USE_GETHOSTBYNAME_LOCK 3179 #ifdef USE_GETHOSTBYNAME_LOCK
2974 gethostbyname_lock = PyThread_allocate_lock(); 3180 gethostbyname_lock = PyThread_allocate_lock();
2975 #endif 3181 #endif
2976 } 3182 }