annotate Modules/_ssl.c @ 25135:0f0016632be4 legacy-trunk

_formatparam(), set_param(): RFC 2231 encoding support by Oleg Broytmann in SF patch #600096. Specifically, the former function now encodes the triplets, while the latter adds optional charset and language arguments.
author Barry Warsaw <barry@python.org>
date Fri, 06 Sep 2002 03:55:04 +0000
parents f66b3a3509c7
children 1ae0b142370e
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);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
67
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
68 #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
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 /* 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
71 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
72 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
73 */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
74
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
75 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
76 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
77 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
78 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
79 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
80 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
81 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
82
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
83 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
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 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
86
0bc3a41d4e40 Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents: 21959
diff changeset
87 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
88 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
89 errstr = "TLS/SSL connection has been closed";
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
90 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
91 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
92 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
93 errstr = "The operation did not complete (read)";
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
94 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
95 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
96 case SSL_ERROR_WANT_WRITE:
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
97 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
98 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
99 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
100 case SSL_ERROR_WANT_X509_LOOKUP:
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
101 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
102 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
103 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
104 case SSL_ERROR_WANT_CONNECT:
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
105 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
106 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
107 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
108 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
109 {
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 unsigned long e = ERR_get_error();
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
111 if (e == 0) {
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
112 if (ret == 0) {
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
113 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
114 errstr = "EOF occurred in violation of protocol";
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
115 } 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
116 /* 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
117 return obj->Socket->errorhandler();
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
118 } else { /* possible? */
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
119 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
120 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
121 }
0bc3a41d4e40 Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents: 21959
diff changeset
122 } else {
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
123 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
124 /* 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
125 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
126 }
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 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
128 }
0bc3a41d4e40 Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents: 21959
diff changeset
129 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
130 {
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 unsigned long e = ERR_get_error();
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
132 p = PY_SSL_ERROR_SSL;
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
133 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
134 /* XXX Protected by global interpreter lock */
0bc3a41d4e40 Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents: 21959
diff changeset
135 errstr = ERR_error_string(e, NULL);
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
136 else { /* possible? */
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
137 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
138 }
0bc3a41d4e40 Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents: 21959
diff changeset
139 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
140 }
0bc3a41d4e40 Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents: 21959
diff changeset
141 default:
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
142 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
143 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
144 }
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 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
146 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
147 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
148 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
149 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
150 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
151 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
152 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
153
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
154 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
155 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
156 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
157 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
158 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
159 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
160 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
161 PyErr_SetObject(PySSLErrorObject, v);
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 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
166 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
167 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
168 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
169 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
170 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
171
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
172 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
173 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
174 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
175 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
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 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
178 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
179 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
180 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
181 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
182 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
183
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
184 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
185 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
186 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
187 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
188
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
189 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
190 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
191 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
192 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
193 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
194 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
195 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
196
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
197 if (key_file) {
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
198 Py_BEGIN_ALLOW_THREADS
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
199 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
200 SSL_FILETYPE_PEM);
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
201 Py_END_ALLOW_THREADS
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
202 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
203 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
204 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
205 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
206
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
207 Py_BEGIN_ALLOW_THREADS
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
208 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
209 cert_file);
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
210 Py_END_ALLOW_THREADS
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
211 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
212 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
213 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
214 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
215 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
216
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
217 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
218 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
219 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
220 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
221 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
222 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
223 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
224 SSL_set_connect_state(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
225
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
226
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 /* 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
228 /* XXX If SSL_connect() returns 0, it's also a failure. */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
229 ret = SSL_connect(self->ssl);
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
230 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
231 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
232 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
233 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
234 }
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->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
236
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
237 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
238 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
239 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
240 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
241 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
242 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
243 }
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
244 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
245 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
246 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
247 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
248 fail:
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
249 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
250 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
251 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
252 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
253 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
254
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
255 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
256 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
257 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
258 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
259 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
260 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
261 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
262
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
263 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
264 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
265 (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
266 &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
267 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
268
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
269 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
270 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
271 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
272 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
273 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
274
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
275 PyDoc_STRVAR(ssl_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
276 "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
277
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
278 /* 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
279
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
280 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
281 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
282 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
283 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
284 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
285
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
286 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
287 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
288 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
289 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
290 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
291
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
292
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
293 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
294 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
295 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
296 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
297 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
298 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
299 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
300 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
301 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
302 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
303 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
304
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
305 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
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 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
308 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
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 if (!PyArg_ParseTuple(args, "s#:write", &data, &len))
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
311 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
312
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
313 Py_BEGIN_ALLOW_THREADS
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
314 len = SSL_write(self->ssl, data, len);
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
315 Py_END_ALLOW_THREADS
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
316 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
317 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
318 else
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 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
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
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
322 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
323 "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
324 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
325 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
326 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
327
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
328 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
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 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
331 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
332 int len = 1024;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
333
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
334 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
335 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
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 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
338 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
339
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
340 Py_BEGIN_ALLOW_THREADS
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
341 count = SSL_read(self->ssl, PyString_AsString(buf), len);
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
342 Py_END_ALLOW_THREADS
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
343 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
344 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
345 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
346 }
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23190
diff changeset
347 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
348 _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
349 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
350 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
351
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
352 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
353 "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
354 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
355 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
356
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
357 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
358 {"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
359 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
360 {"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
361 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
362 {"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
363 {"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
364 {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
365 };
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
366
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
367 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
368 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
369 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
370 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
371
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24086
diff changeset
372 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
373 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
374 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
375 "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
376 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
377 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
378 /* methods */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
379 (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
380 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
381 (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
382 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
383 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
384 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
385 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
386 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
387 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
388 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
389 };
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
390
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
391 #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
392
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
393 /* 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
394 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
395 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
396 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
397 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
398 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
399 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
400
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
401 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
402 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
403 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
404 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
405 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
406 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
407
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
408 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
409 "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
410 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
411 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
412 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
413
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
414 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
415 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
416 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
417 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
418 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
419
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
420 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
421 "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
422 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
423 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
424 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
425 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
426
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
427 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
428 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
429 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
430 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
431
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
432 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
433 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
434 "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
435 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
436 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
437 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
438 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
439 "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
440 "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
441 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
442 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
443 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
444 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
445
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
446 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
447 "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
448 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
449 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
450 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
451 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
452
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
453 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
454
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
455 /* 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
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 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
458 {"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
459 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
460 #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
461 {"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
462 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
463 {"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
464 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
465 {"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
466 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
467 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
468 {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
469 };
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
470
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
471
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
472 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
473 "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
474 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
475
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24398
diff changeset
476 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
477 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
478 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
479 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
480
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
481 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
482
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
483 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
484 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
485
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
486 /* 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
487 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
488 return;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
489
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
490 /* 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
491 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
492 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
493
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
494 /* Add symbols to module dict */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
495 PySSLErrorObject = PyErr_NewException("socket.sslerror", 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
496 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
497 return;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
498 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
499 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
500 (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
501 return;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
502 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
503 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
504 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
505 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
506 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
507 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
508 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
509 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
510 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
511 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
512 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
513 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
514 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
515 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
516 /* 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
517 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
518 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
519 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
520 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
521
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
522 }