annotate Modules/_ssl.c @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 43d0b47106df
children c31a7c0c6399
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1 /* SSL socket module
21959
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.
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
4 Re-worked a bit by Bill Janssen to add server-side support and
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
5 certificate decoding. Chris Stawarz contributed some non-blocking
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
6 patches.
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
7
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
8 This module is imported by ssl.py. It should *not* be used
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
9 directly.
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
10
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
11 XXX should partial writes be enabled, SSL_MODE_ENABLE_PARTIAL_WRITE?
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
12
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
13 XXX what about SSL_MODE_AUTO_RETRY?
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
14 */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
15
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
16 #include "Python.h"
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
17
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
18 #ifdef WITH_THREAD
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
19 #include "pythread.h"
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
20 #define PySSL_BEGIN_ALLOW_THREADS { \
45006
fcfa13199ecb Initialize variable to prevent warning on some platform/config.
Neal Norwitz <nnorwitz@gmail.com>
parents: 44350
diff changeset
21 PyThreadState *_save = NULL; \
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
22 if (_ssl_locks_count>0) {_save = PyEval_SaveThread();}
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
23 #define PySSL_BLOCK_THREADS if (_ssl_locks_count>0){PyEval_RestoreThread(_save)};
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
24 #define PySSL_UNBLOCK_THREADS if (_ssl_locks_count>0){_save = PyEval_SaveThread()};
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
25 #define PySSL_END_ALLOW_THREADS if (_ssl_locks_count>0){PyEval_RestoreThread(_save);} \
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
26 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
27
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
28 #else /* no WITH_THREAD */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
29
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
30 #define PySSL_BEGIN_ALLOW_THREADS
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
31 #define PySSL_BLOCK_THREADS
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
32 #define PySSL_UNBLOCK_THREADS
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
33 #define PySSL_END_ALLOW_THREADS
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
34
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
35 #endif
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
36
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
37 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
38 /* these mirror ssl.h */
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
39 PY_SSL_ERROR_NONE,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
40 PY_SSL_ERROR_SSL,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
41 PY_SSL_ERROR_WANT_READ,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
42 PY_SSL_ERROR_WANT_WRITE,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
43 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
44 PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
45 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
46 PY_SSL_ERROR_WANT_CONNECT,
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
47 /* start of non ssl.h errorcodes */
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
48 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
49 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
50 };
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
51
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
52 enum py_ssl_server_or_client {
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
53 PY_SSL_CLIENT,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
54 PY_SSL_SERVER
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
55 };
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
56
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
57 enum py_ssl_cert_requirements {
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
58 PY_SSL_CERT_NONE,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
59 PY_SSL_CERT_OPTIONAL,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
60 PY_SSL_CERT_REQUIRED
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
61 };
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
62
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
63 enum py_ssl_version {
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
64 PY_SSL_VERSION_SSL2,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
65 PY_SSL_VERSION_SSL3,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
66 PY_SSL_VERSION_SSL23,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
67 PY_SSL_VERSION_TLS1,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
68 };
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
69
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
70 /* 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
71 #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
72
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
73 #if defined(HAVE_POLL_H)
39059
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
74 #include <poll.h>
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
75 #elif defined(HAVE_SYS_POLL_H)
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
76 #include <sys/poll.h>
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
77 #endif
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
78
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
79 /* 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
80 #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
81 #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
82 #include "openssl/x509.h"
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
83 #include "openssl/x509v3.h"
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
84 #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
85 #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
86 #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
87 #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
88
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
89 /* 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
90 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
91
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
92 #ifdef WITH_THREAD
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
93
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
94 /* serves as a flag to see whether we've initialized the SSL thread support. */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
95 /* 0 means no, greater than 0 means yes */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
96
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
97 static unsigned int _ssl_locks_count = 0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
98
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
99 #endif /* def WITH_THREAD */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
100
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
101 /* 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
102
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
103 #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
104
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
105 /* 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
106 #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
107 # 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
108 #else
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
109 # 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
110 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
111
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
112 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
113 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
114 PySocketSockObject *Socket; /* Socket on which we're layered */
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
115 SSL_CTX* ctx;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
116 SSL* ssl;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
117 X509* peer_cert;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
118 char server[X509_NAME_MAXLEN];
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
119 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
120
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
121 } PySSLObject;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
122
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24086
diff changeset
123 static PyTypeObject PySSL_Type;
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24086
diff changeset
124 static PyObject *PySSL_SSLwrite(PySSLObject *self, PyObject *args);
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24086
diff changeset
125 static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
126 static int check_socket_and_wait_for_timeout(PySocketSockObject *s,
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
127 int writing);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
128 static PyObject *PySSL_peercert(PySSLObject *self, PyObject *args);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
129 static PyObject *PySSL_cipher(PySSLObject *self);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
130
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44296
diff changeset
131 #define PySSLObject_Check(v) (Py_TYPE(v) == &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
132
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
133 typedef enum {
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
134 SOCKET_IS_NONBLOCKING,
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
135 SOCKET_IS_BLOCKING,
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
136 SOCKET_HAS_TIMED_OUT,
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
137 SOCKET_HAS_BEEN_CLOSED,
36487
07ba8aae3dc6 Try to improve name based on discussion on python-checkins with Jim Jewett
Neal Norwitz <nnorwitz@gmail.com>
parents: 36476
diff changeset
138 SOCKET_TOO_LARGE_FOR_SELECT,
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
139 SOCKET_OPERATION_OK
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
140 } timeout_state;
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
141
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
142 /* Wrap error strings with filename and line # */
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
143 #define STRINGIFY1(x) #x
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
144 #define STRINGIFY2(x) STRINGIFY1(x)
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
145 #define ERRSTR1(x,y,z) (x ":" y ": " z)
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
146 #define ERRSTR(x) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x)
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
147
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
148 /* 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
149 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
150 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
151 */
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 static PyObject *
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
154 PySSL_SetError(PySSLObject *obj, int ret, char *filename, int lineno)
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
155 {
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
156 PyObject *v;
42829
241053739733 Try to get this to build with Visual Studio by moving all the variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 42828
diff changeset
157 char buf[2048];
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
158 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
159 int err;
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
160 enum py_ssl_error p = PY_SSL_ERROR_NONE;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
161
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
162 assert(ret <= 0);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
163
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
164 if (obj->ssl != NULL) {
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
165 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
166
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
167 switch (err) {
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
168 case SSL_ERROR_ZERO_RETURN:
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
169 errstr = "TLS/SSL connection has been closed";
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
170 p = PY_SSL_ERROR_ZERO_RETURN;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
171 break;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
172 case SSL_ERROR_WANT_READ:
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
173 errstr = "The operation did not complete (read)";
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
174 p = PY_SSL_ERROR_WANT_READ;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
175 break;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
176 case SSL_ERROR_WANT_WRITE:
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
177 p = PY_SSL_ERROR_WANT_WRITE;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
178 errstr = "The operation did not complete (write)";
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
179 break;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
180 case SSL_ERROR_WANT_X509_LOOKUP:
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
181 p = PY_SSL_ERROR_WANT_X509_LOOKUP;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
182 errstr =
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
183 "The operation did not complete (X509 lookup)";
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
184 break;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
185 case SSL_ERROR_WANT_CONNECT:
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
186 p = PY_SSL_ERROR_WANT_CONNECT;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
187 errstr = "The operation did not complete (connect)";
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
188 break;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
189 case SSL_ERROR_SYSCALL:
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
190 {
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
191 unsigned long e = ERR_get_error();
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
192 if (e == 0) {
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
193 if (ret == 0 || !obj->Socket) {
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
194 p = PY_SSL_ERROR_EOF;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
195 errstr =
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
196 "EOF occurred in violation of protocol";
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
197 } else if (ret == -1) {
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
198 /* underlying BIO reported an I/O error */
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
199 return obj->Socket->errorhandler();
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
200 } else { /* possible? */
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
201 p = PY_SSL_ERROR_SYSCALL;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
202 errstr = "Some I/O error occurred";
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
203 }
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
204 } else {
24086
86dfb96a1831 Repair badly formatted code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
205 p = PY_SSL_ERROR_SYSCALL;
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
206 /* XXX Protected by global interpreter lock */
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
207 errstr = ERR_error_string(e, NULL);
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
208 }
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
209 break;
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
210 }
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
211 case SSL_ERROR_SSL:
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
212 {
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
213 unsigned long e = ERR_get_error();
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
214 p = PY_SSL_ERROR_SSL;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
215 if (e != 0)
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
216 /* XXX Protected by global interpreter lock */
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
217 errstr = ERR_error_string(e, NULL);
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
218 else { /* possible? */
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
219 errstr =
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
220 "A failure in the SSL library occurred";
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
221 }
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
222 break;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
223 }
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
224 default:
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
225 p = PY_SSL_ERROR_INVALID_ERROR_CODE;
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
226 errstr = "Invalid error code";
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
227 }
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
228 } else {
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
229 errstr = ERR_error_string(ERR_peek_last_error(), NULL);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
230 }
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
231 PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
232 v = Py_BuildValue("(is)", p, buf);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
233 if (v != NULL) {
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
234 PyErr_SetObject(PySSLErrorObject, v);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
235 Py_DECREF(v);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
236 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
237 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
238 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
239
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
240 static PyObject *
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
241 _setSSLError (char *errstr, int errcode, char *filename, int lineno) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
242
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
243 char buf[2048];
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
244 PyObject *v;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
245
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
246 if (errstr == NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
247 errcode = ERR_peek_last_error();
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
248 errstr = ERR_error_string(errcode, NULL);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
249 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
250 PyOS_snprintf(buf, sizeof(buf), "_ssl.c:%d: %s", lineno, errstr);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
251 v = Py_BuildValue("(is)", errcode, buf);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
252 if (v != NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
253 PyErr_SetObject(PySSLErrorObject, v);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
254 Py_DECREF(v);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
255 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
256 return NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
257 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
258
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
259 static PySSLObject *
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
260 newPySSLObject(PySocketSockObject *Sock, char *key_file, char *cert_file,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
261 enum py_ssl_server_or_client socket_type,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
262 enum py_ssl_cert_requirements certreq,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
263 enum py_ssl_version proto_version,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
264 char *cacerts_file)
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
265 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
266 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
267 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
268 int ret;
42829
241053739733 Try to get this to build with Visual Studio by moving all the variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 42828
diff changeset
269 int verification_mode;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
270
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
271 self = PyObject_New(PySSLObject, &PySSL_Type); /* Create new object */
38152
9a637bf4df42 Don't mask a no memory error with a less meaningful one as discussed on python-checkins
Neal Norwitz <nnorwitz@gmail.com>
parents: 38139
diff changeset
272 if (self == NULL)
38139
f1e24452dbba Fix problems found by Coverity.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37784
diff changeset
273 return NULL;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
274 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
275 memset(self->issuer, '\0', sizeof(char) * X509_NAME_MAXLEN);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
276 self->peer_cert = NULL;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
277 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
278 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
279 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
280
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
281 /* Make sure the SSL error state is initialized */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
282 (void) ERR_get_state();
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
283 ERR_clear_error();
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
284
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
285 if ((key_file && !cert_file) || (!key_file && cert_file)) {
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
286 errstr = ERRSTR("Both the key & certificate files "
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
287 "must be specified");
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
288 goto fail;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
289 }
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
290
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
291 if ((socket_type == PY_SSL_SERVER) &&
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
292 ((key_file == NULL) || (cert_file == NULL))) {
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
293 errstr = ERRSTR("Both the key & certificate files "
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
294 "must be specified for server-side operation");
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
295 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
296 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
297
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
298 PySSL_BEGIN_ALLOW_THREADS
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
299 if (proto_version == PY_SSL_VERSION_TLS1)
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
300 self->ctx = SSL_CTX_new(TLSv1_method()); /* Set up context */
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
301 else if (proto_version == PY_SSL_VERSION_SSL3)
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
302 self->ctx = SSL_CTX_new(SSLv3_method()); /* Set up context */
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
303 else if (proto_version == PY_SSL_VERSION_SSL2)
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
304 self->ctx = SSL_CTX_new(SSLv2_method()); /* Set up context */
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
305 else if (proto_version == PY_SSL_VERSION_SSL23)
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
306 self->ctx = SSL_CTX_new(SSLv23_method()); /* Set up context */
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
307 PySSL_END_ALLOW_THREADS
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
308
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
309 if (self->ctx == NULL) {
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
310 errstr = ERRSTR("Invalid SSL protocol variant specified.");
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
311 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
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
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
314 if (certreq != PY_SSL_CERT_NONE) {
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
315 if (cacerts_file == NULL) {
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
316 errstr = ERRSTR("No root certificates specified for "
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
317 "verification of other-side certificates.");
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
318 goto fail;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
319 } else {
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
320 PySSL_BEGIN_ALLOW_THREADS
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
321 ret = SSL_CTX_load_verify_locations(self->ctx,
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
322 cacerts_file,
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
323 NULL);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
324 PySSL_END_ALLOW_THREADS
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
325 if (ret != 1) {
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
326 _setSSLError(NULL, 0, __FILE__, __LINE__);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
327 goto fail;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
328 }
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
329 }
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
330 }
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
331 if (key_file) {
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
332 PySSL_BEGIN_ALLOW_THREADS
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
333 ret = SSL_CTX_use_PrivateKey_file(self->ctx, key_file,
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
334 SSL_FILETYPE_PEM);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
335 PySSL_END_ALLOW_THREADS
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
336 if (ret != 1) {
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
337 _setSSLError(NULL, ret, __FILE__, __LINE__);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
338 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
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
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
341 PySSL_BEGIN_ALLOW_THREADS
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
342 ret = SSL_CTX_use_certificate_chain_file(self->ctx,
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
343 cert_file);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
344 PySSL_END_ALLOW_THREADS
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
345 if (ret != 1) {
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
346 /*
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
347 fprintf(stderr, "ret is %d, errcode is %lu, %lu, with file \"%s\"\n",
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
348 ret, ERR_peek_error(), ERR_peek_last_error(), cert_file);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
349 */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
350 if (ERR_peek_last_error() != 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
351 _setSSLError(NULL, ret, __FILE__, __LINE__);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
352 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
353 }
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
354 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
355 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
356
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
357 /* ssl compatibility */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
358 SSL_CTX_set_options(self->ctx, SSL_OP_ALL);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
359
42829
241053739733 Try to get this to build with Visual Studio by moving all the variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 42828
diff changeset
360 verification_mode = SSL_VERIFY_NONE;
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
361 if (certreq == PY_SSL_CERT_OPTIONAL)
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
362 verification_mode = SSL_VERIFY_PEER;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
363 else if (certreq == PY_SSL_CERT_REQUIRED)
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
364 verification_mode = (SSL_VERIFY_PEER |
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
365 SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
366 SSL_CTX_set_verify(self->ctx, verification_mode,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
367 NULL); /* set verify lvl */
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
368
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
369 PySSL_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
370 self->ssl = SSL_new(self->ctx); /* New ssl struct */
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
371 PySSL_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
372 SSL_set_fd(self->ssl, Sock->sock_fd); /* Set the socket for SSL */
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
373
30723
6f15e73b59f2 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 29695
diff changeset
374 /* If the socket is in non-blocking mode or timeout mode, set the BIO
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
375 * to non-blocking mode (blocking is the default)
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
376 */
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
377 if (Sock->sock_timeout >= 0.0) {
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
378 /* Set both the read and write BIO's to non-blocking mode */
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
379 BIO_set_nbio(SSL_get_rbio(self->ssl), 1);
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
380 BIO_set_nbio(SSL_get_wbio(self->ssl), 1);
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
381 }
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
382
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
383 PySSL_BEGIN_ALLOW_THREADS
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
384 if (socket_type == PY_SSL_CLIENT)
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
385 SSL_set_connect_state(self->ssl);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
386 else
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
387 SSL_set_accept_state(self->ssl);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
388 PySSL_END_ALLOW_THREADS
24398
c7ed925c75b3 Patch #575827: allow threads inside SSL creation.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
389
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
390 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
391 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
392 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
393 fail:
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
394 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
395 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
396 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
397 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
398 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
399
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
400 static PyObject *
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
401 PySSL_sslwrap(PyObject *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
402 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
403 PySocketSockObject *Sock;
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
404 int server_side = 0;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
405 int verification_mode = PY_SSL_CERT_NONE;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
406 int protocol = PY_SSL_VERSION_SSL23;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
407 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
408 char *cert_file = NULL;
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
409 char *cacerts_file = NULL;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
410
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
411 if (!PyArg_ParseTuple(args, "O!i|zziiz:sslwrap",
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
412 PySocketModule.Sock_Type,
40152
e2df1356349f Fix various minor errors in passing arguments to
Martin v. Löwis <martin@v.loewis.de>
parents: 39059
diff changeset
413 &Sock,
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
414 &server_side,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
415 &key_file, &cert_file,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
416 &verification_mode, &protocol,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
417 &cacerts_file))
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
418 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
419
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
420 /*
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
421 fprintf(stderr,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
422 "server_side is %d, keyfile %p, certfile %p, verify_mode %d, "
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
423 "protocol %d, certs %p\n",
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
424 server_side, key_file, cert_file, verification_mode,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
425 protocol, cacerts_file);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
426 */
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
427
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
428 return (PyObject *) newPySSLObject(Sock, key_file, cert_file,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
429 server_side, verification_mode,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
430 protocol, cacerts_file);
21959
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
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
433 PyDoc_STRVAR(ssl_doc,
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
434 "sslwrap(socket, server_side, [keyfile, certfile, certs_mode, protocol,\n"
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
435 " cacertsfile]) -> 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
436
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
437 /* 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
438
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
439 static PyObject *PySSL_SSLdo_handshake(PySSLObject *self)
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
440 {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
441 int ret;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
442 int err;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
443 int sockstate;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
444
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
445 /* Actually negotiate SSL connection */
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
446 /* XXX If SSL_do_handshake() returns 0, it's also a failure. */
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
447 sockstate = 0;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
448 do {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
449 PySSL_BEGIN_ALLOW_THREADS
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
450 ret = SSL_do_handshake(self->ssl);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
451 err = SSL_get_error(self->ssl, ret);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
452 PySSL_END_ALLOW_THREADS
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
453 if(PyErr_CheckSignals()) {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
454 return NULL;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
455 }
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
456 if (err == SSL_ERROR_WANT_READ) {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
457 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
458 } else if (err == SSL_ERROR_WANT_WRITE) {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
459 sockstate = check_socket_and_wait_for_timeout(self->Socket, 1);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
460 } else {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
461 sockstate = SOCKET_OPERATION_OK;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
462 }
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
463 if (sockstate == SOCKET_HAS_TIMED_OUT) {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
464 PyErr_SetString(PySSLErrorObject,
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
465 ERRSTR("The handshake operation timed out"));
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
466 return NULL;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
467 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
468 PyErr_SetString(PySSLErrorObject,
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
469 ERRSTR("Underlying socket has been closed."));
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
470 return NULL;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
471 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
472 PyErr_SetString(PySSLErrorObject,
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
473 ERRSTR("Underlying socket too large for select()."));
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
474 return NULL;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
475 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
476 break;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
477 }
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
478 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
479 if (ret < 1)
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
480 return PySSL_SetError(self, ret, __FILE__, __LINE__);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
481 self->ssl->debug = 1;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
482
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
483 if (self->peer_cert)
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
484 X509_free (self->peer_cert);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
485 PySSL_BEGIN_ALLOW_THREADS
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
486 if ((self->peer_cert = SSL_get_peer_certificate(self->ssl))) {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
487 X509_NAME_oneline(X509_get_subject_name(self->peer_cert),
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
488 self->server, X509_NAME_MAXLEN);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
489 X509_NAME_oneline(X509_get_issuer_name(self->peer_cert),
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
490 self->issuer, X509_NAME_MAXLEN);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
491 }
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
492 PySSL_END_ALLOW_THREADS
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
493
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
494 Py_INCREF(Py_None);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
495 return Py_None;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
496 }
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
497
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
498 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
499 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
500 {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
501 return PyString_FromString(self->server);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
502 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
503
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
504 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
505 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
506 {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
507 return PyString_FromString(self->issuer);
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 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
509
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
510 static PyObject *
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
511 _create_tuple_for_attribute (ASN1_OBJECT *name, ASN1_STRING *value) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
512
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
513 char namebuf[X509_NAME_MAXLEN];
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
514 int buflen;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
515 PyObject *name_obj;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
516 PyObject *value_obj;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
517 PyObject *attr;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
518 unsigned char *valuebuf = NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
519
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
520 buflen = OBJ_obj2txt(namebuf, sizeof(namebuf), name, 0);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
521 if (buflen < 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
522 _setSSLError(NULL, 0, __FILE__, __LINE__);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
523 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
524 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
525 name_obj = PyString_FromStringAndSize(namebuf, buflen);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
526 if (name_obj == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
527 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
528
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
529 buflen = ASN1_STRING_to_UTF8(&valuebuf, value);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
530 if (buflen < 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
531 _setSSLError(NULL, 0, __FILE__, __LINE__);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
532 Py_DECREF(name_obj);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
533 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
534 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
535 value_obj = PyUnicode_DecodeUTF8((char *) valuebuf,
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
536 buflen, "strict");
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
537 OPENSSL_free(valuebuf);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
538 if (value_obj == NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
539 Py_DECREF(name_obj);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
540 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
541 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
542 attr = PyTuple_New(2);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
543 if (attr == NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
544 Py_DECREF(name_obj);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
545 Py_DECREF(value_obj);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
546 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
547 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
548 PyTuple_SET_ITEM(attr, 0, name_obj);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
549 PyTuple_SET_ITEM(attr, 1, value_obj);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
550 return attr;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
551
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
552 fail:
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
553 return NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
554 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
555
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
556 static PyObject *
43243
e2b4fc960830 SSL certificate distinguished names should be represented by tuples
Bill Janssen <janssen@parc.com>
parents: 42908
diff changeset
557 _create_tuple_for_X509_NAME (X509_NAME *xname)
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
558 {
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
559 PyObject *dn = NULL; /* tuple which represents the "distinguished name" */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
560 PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
561 PyObject *rdnt;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
562 PyObject *attr = NULL; /* tuple to hold an attribute */
43243
e2b4fc960830 SSL certificate distinguished names should be represented by tuples
Bill Janssen <janssen@parc.com>
parents: 42908
diff changeset
563 int entry_count = X509_NAME_entry_count(xname);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
564 X509_NAME_ENTRY *entry;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
565 ASN1_OBJECT *name;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
566 ASN1_STRING *value;
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
567 int index_counter;
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
568 int rdn_level = -1;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
569 int retcode;
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
570
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
571 dn = PyList_New(0);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
572 if (dn == NULL)
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
573 return NULL;
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
574 /* now create another tuple to hold the top-level RDN */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
575 rdn = PyList_New(0);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
576 if (rdn == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
577 goto fail0;
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
578
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
579 for (index_counter = 0;
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
580 index_counter < entry_count;
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
581 index_counter++)
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
582 {
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
583 entry = X509_NAME_get_entry(xname, index_counter);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
584
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
585 /* check to see if we've gotten to a new RDN */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
586 if (rdn_level >= 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
587 if (rdn_level != entry->set) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
588 /* yes, new RDN */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
589 /* add old RDN to DN */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
590 rdnt = PyList_AsTuple(rdn);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
591 Py_DECREF(rdn);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
592 if (rdnt == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
593 goto fail0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
594 retcode = PyList_Append(dn, rdnt);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
595 Py_DECREF(rdnt);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
596 if (retcode < 0)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
597 goto fail0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
598 /* create new RDN */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
599 rdn = PyList_New(0);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
600 if (rdn == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
601 goto fail0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
602 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
603 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
604 rdn_level = entry->set;
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
605
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
606 /* now add this attribute to the current RDN */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
607 name = X509_NAME_ENTRY_get_object(entry);
42829
241053739733 Try to get this to build with Visual Studio by moving all the variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 42828
diff changeset
608 value = X509_NAME_ENTRY_get_data(entry);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
609 attr = _create_tuple_for_attribute(name, value);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
610 /*
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
611 fprintf(stderr, "RDN level %d, attribute %s: %s\n",
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
612 entry->set,
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
613 PyString_AS_STRING(PyTuple_GET_ITEM(attr, 0)),
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
614 PyString_AS_STRING(PyTuple_GET_ITEM(attr, 1)));
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
615 */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
616 if (attr == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
617 goto fail1;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
618 retcode = PyList_Append(rdn, attr);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
619 Py_DECREF(attr);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
620 if (retcode < 0)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
621 goto fail1;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
622 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
623 /* now, there's typically a dangling RDN */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
624 if ((rdn != NULL) && (PyList_Size(rdn) > 0)) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
625 rdnt = PyList_AsTuple(rdn);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
626 Py_DECREF(rdn);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
627 if (rdnt == NULL)
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
628 goto fail0;
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
629 retcode = PyList_Append(dn, rdnt);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
630 Py_DECREF(rdnt);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
631 if (retcode < 0)
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
632 goto fail0;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
633 }
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
634
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
635 /* convert list to tuple */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
636 rdnt = PyList_AsTuple(dn);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
637 Py_DECREF(dn);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
638 if (rdnt == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
639 return NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
640 return rdnt;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
641
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
642 fail1:
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
643 Py_XDECREF(rdn);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
644
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
645 fail0:
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
646 Py_XDECREF(dn);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
647 return NULL;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
648 }
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
649
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
650 static PyObject *
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
651 _get_peer_alt_names (X509 *certificate) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
652
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
653 /* this code follows the procedure outlined in
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
654 OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print()
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
655 function to extract the STACK_OF(GENERAL_NAME),
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
656 then iterates through the stack to add the
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
657 names. */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
658
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
659 int i, j;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
660 PyObject *peer_alt_names = Py_None;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
661 PyObject *v, *t;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
662 X509_EXTENSION *ext = NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
663 GENERAL_NAMES *names = NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
664 GENERAL_NAME *name;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
665 X509V3_EXT_METHOD *method;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
666 BIO *biobuf = NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
667 char buf[2048];
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
668 char *vptr;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
669 int len;
54412
1d8f24bf4399 Fix nearly all compilation warnings under Apple gcc-4.0. Tested with OPT="-g
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 51069
diff changeset
670 unsigned char *p;
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
671
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
672 if (certificate == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
673 return peer_alt_names;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
674
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
675 /* get a memory buffer */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
676 biobuf = BIO_new(BIO_s_mem());
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
677
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
678 i = 0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
679 while ((i = X509_get_ext_by_NID(
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
680 certificate, NID_subject_alt_name, i)) >= 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
681
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
682 if (peer_alt_names == Py_None) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
683 peer_alt_names = PyList_New(0);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
684 if (peer_alt_names == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
685 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
686 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
687
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
688 /* now decode the altName */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
689 ext = X509_get_ext(certificate, i);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
690 if(!(method = X509V3_EXT_get(ext))) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
691 PyErr_SetString(PySSLErrorObject,
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
692 ERRSTR("No method for internalizing subjectAltName!"));
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
693 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
694 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
695
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
696 p = ext->value->data;
45006
fcfa13199ecb Initialize variable to prevent warning on some platform/config.
Neal Norwitz <nnorwitz@gmail.com>
parents: 44350
diff changeset
697 if (method->it)
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
698 names = (GENERAL_NAMES*) (ASN1_item_d2i(NULL,
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
699 &p,
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
700 ext->value->length,
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
701 ASN1_ITEM_ptr(method->it)));
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
702 else
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
703 names = (GENERAL_NAMES*) (method->d2i(NULL,
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
704 &p,
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
705 ext->value->length));
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
706
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
707 for(j = 0; j < sk_GENERAL_NAME_num(names); j++) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
708
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
709 /* get a rendering of each name in the set of names */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
710
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
711 name = sk_GENERAL_NAME_value(names, j);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
712 if (name->type == GEN_DIRNAME) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
713
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
714 /* we special-case DirName as a tuple of tuples of attributes */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
715
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
716 t = PyTuple_New(2);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
717 if (t == NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
718 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
719 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
720
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
721 v = PyString_FromString("DirName");
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
722 if (v == NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
723 Py_DECREF(t);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
724 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
725 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
726 PyTuple_SET_ITEM(t, 0, v);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
727
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
728 v = _create_tuple_for_X509_NAME (name->d.dirn);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
729 if (v == NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
730 Py_DECREF(t);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
731 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
732 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
733 PyTuple_SET_ITEM(t, 1, v);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
734
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
735 } else {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
736
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
737 /* for everything else, we use the OpenSSL print form */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
738
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
739 (void) BIO_reset(biobuf);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
740 GENERAL_NAME_print(biobuf, name);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
741 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
742 if (len < 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
743 _setSSLError(NULL, 0, __FILE__, __LINE__);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
744 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
745 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
746 vptr = strchr(buf, ':');
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
747 if (vptr == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
748 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
749 t = PyTuple_New(2);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
750 if (t == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
751 goto fail;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
752 v = PyString_FromStringAndSize(buf, (vptr - buf));
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
753 if (v == NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
754 Py_DECREF(t);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
755 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
756 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
757 PyTuple_SET_ITEM(t, 0, v);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
758 v = PyString_FromStringAndSize((vptr + 1), (len - (vptr - buf + 1)));
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
759 if (v == NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
760 Py_DECREF(t);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
761 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
762 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
763 PyTuple_SET_ITEM(t, 1, v);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
764 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
765
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
766 /* and add that rendering to the list */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
767
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
768 if (PyList_Append(peer_alt_names, t) < 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
769 Py_DECREF(t);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
770 goto fail;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
771 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
772 Py_DECREF(t);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
773 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
774 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
775 BIO_free(biobuf);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
776 if (peer_alt_names != Py_None) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
777 v = PyList_AsTuple(peer_alt_names);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
778 Py_DECREF(peer_alt_names);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
779 return v;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
780 } else {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
781 return peer_alt_names;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
782 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
783
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
784
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
785 fail:
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
786 if (biobuf != NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
787 BIO_free(biobuf);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
788
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
789 if (peer_alt_names != Py_None) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
790 Py_XDECREF(peer_alt_names);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
791 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
792
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
793 return NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
794 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
795
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
796 static PyObject *
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
797 _decode_certificate (X509 *certificate, int verbose) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
798
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
799 PyObject *retval = NULL;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
800 BIO *biobuf = NULL;
42829
241053739733 Try to get this to build with Visual Studio by moving all the variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 42828
diff changeset
801 PyObject *peer;
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
802 PyObject *peer_alt_names = NULL;
42829
241053739733 Try to get this to build with Visual Studio by moving all the variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 42828
diff changeset
803 PyObject *issuer;
241053739733 Try to get this to build with Visual Studio by moving all the variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 42828
diff changeset
804 PyObject *version;
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
805 PyObject *sn_obj;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
806 ASN1_INTEGER *serialNumber;
42829
241053739733 Try to get this to build with Visual Studio by moving all the variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 42828
diff changeset
807 char buf[2048];
241053739733 Try to get this to build with Visual Studio by moving all the variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 42828
diff changeset
808 int len;
241053739733 Try to get this to build with Visual Studio by moving all the variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 42828
diff changeset
809 ASN1_TIME *notBefore, *notAfter;
241053739733 Try to get this to build with Visual Studio by moving all the variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 42828
diff changeset
810 PyObject *pnotBefore, *pnotAfter;
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
811
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
812 retval = PyDict_New();
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
813 if (retval == NULL)
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
814 return NULL;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
815
43243
e2b4fc960830 SSL certificate distinguished names should be represented by tuples
Bill Janssen <janssen@parc.com>
parents: 42908
diff changeset
816 peer = _create_tuple_for_X509_NAME(
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
817 X509_get_subject_name(certificate));
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
818 if (peer == NULL)
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
819 goto fail0;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
820 if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) {
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
821 Py_DECREF(peer);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
822 goto fail0;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
823 }
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
824 Py_DECREF(peer);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
825
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
826 if (verbose) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
827 issuer = _create_tuple_for_X509_NAME(
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
828 X509_get_issuer_name(certificate));
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
829 if (issuer == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
830 goto fail0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
831 if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
832 Py_DECREF(issuer);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
833 goto fail0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
834 }
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
835 Py_DECREF(issuer);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
836
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
837 version = PyInt_FromLong(X509_get_version(certificate) + 1);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
838 if (PyDict_SetItemString(retval, "version", version) < 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
839 Py_DECREF(version);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
840 goto fail0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
841 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
842 Py_DECREF(version);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
843 }
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
844
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
845 /* get a memory buffer */
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
846 biobuf = BIO_new(BIO_s_mem());
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
847
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
848 if (verbose) {
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
849
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
850 (void) BIO_reset(biobuf);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
851 serialNumber = X509_get_serialNumber(certificate);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
852 /* should not exceed 20 octets, 160 bits, so buf is big enough */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
853 i2a_ASN1_INTEGER(biobuf, serialNumber);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
854 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
855 if (len < 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
856 _setSSLError(NULL, 0, __FILE__, __LINE__);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
857 goto fail1;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
858 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
859 sn_obj = PyString_FromStringAndSize(buf, len);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
860 if (sn_obj == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
861 goto fail1;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
862 if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
863 Py_DECREF(sn_obj);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
864 goto fail1;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
865 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
866 Py_DECREF(sn_obj);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
867
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
868 (void) BIO_reset(biobuf);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
869 notBefore = X509_get_notBefore(certificate);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
870 ASN1_TIME_print(biobuf, notBefore);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
871 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
872 if (len < 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
873 _setSSLError(NULL, 0, __FILE__, __LINE__);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
874 goto fail1;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
875 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
876 pnotBefore = PyString_FromStringAndSize(buf, len);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
877 if (pnotBefore == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
878 goto fail1;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
879 if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
880 Py_DECREF(pnotBefore);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
881 goto fail1;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
882 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
883 Py_DECREF(pnotBefore);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
884 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
885
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
886 (void) BIO_reset(biobuf);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
887 notAfter = X509_get_notAfter(certificate);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
888 ASN1_TIME_print(biobuf, notAfter);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
889 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
890 if (len < 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
891 _setSSLError(NULL, 0, __FILE__, __LINE__);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
892 goto fail1;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
893 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
894 pnotAfter = PyString_FromStringAndSize(buf, len);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
895 if (pnotAfter == NULL)
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
896 goto fail1;
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
897 if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) {
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
898 Py_DECREF(pnotAfter);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
899 goto fail1;
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
900 }
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
901 Py_DECREF(pnotAfter);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
902
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
903 /* Now look for subjectAltName */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
904
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
905 peer_alt_names = _get_peer_alt_names(certificate);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
906 if (peer_alt_names == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
907 goto fail1;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
908 else if (peer_alt_names != Py_None) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
909 if (PyDict_SetItemString(retval, "subjectAltName",
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
910 peer_alt_names) < 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
911 Py_DECREF(peer_alt_names);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
912 goto fail1;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
913 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
914 Py_DECREF(peer_alt_names);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
915 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
916
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
917 BIO_free(biobuf);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
918 return retval;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
919
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
920 fail1:
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
921 if (biobuf != NULL)
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
922 BIO_free(biobuf);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
923 fail0:
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
924 Py_XDECREF(retval);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
925 return NULL;
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
926 }
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
927
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
928
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
929 static PyObject *
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
930 PySSL_test_decode_certificate (PyObject *mod, PyObject *args) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
931
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
932 PyObject *retval = NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
933 char *filename = NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
934 X509 *x=NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
935 BIO *cert;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
936 int verbose = 1;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
937
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
938 if (!PyArg_ParseTuple(args, "s|i:test_decode_certificate", &filename, &verbose))
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
939 return NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
940
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
941 if ((cert=BIO_new(BIO_s_file())) == NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
942 PyErr_SetString(PySSLErrorObject, "Can't malloc memory to read file");
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
943 goto fail0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
944 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
945
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
946 if (BIO_read_filename(cert,filename) <= 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
947 PyErr_SetString(PySSLErrorObject, "Can't open file");
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
948 goto fail0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
949 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
950
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
951 x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
952 if (x == NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
953 PyErr_SetString(PySSLErrorObject, "Error decoding PEM-encoded file");
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
954 goto fail0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
955 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
956
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
957 retval = _decode_certificate(x, verbose);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
958
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
959 fail0:
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
960
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
961 if (cert != NULL) BIO_free(cert);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
962 return retval;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
963 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
964
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
965
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
966 static PyObject *
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
967 PySSL_peercert(PySSLObject *self, PyObject *args)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
968 {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
969 PyObject *retval = NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
970 int len;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
971 int verification;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
972 PyObject *binary_mode = Py_None;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
973
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
974 if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode))
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
975 return NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
976
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
977 if (!self->peer_cert)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
978 Py_RETURN_NONE;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
979
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
980 if (PyObject_IsTrue(binary_mode)) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
981 /* return cert in DER-encoded format */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
982
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
983 unsigned char *bytes_buf = NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
984
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
985 bytes_buf = NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
986 len = i2d_X509(self->peer_cert, &bytes_buf);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
987 if (len < 0) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
988 PySSL_SetError(self, len, __FILE__, __LINE__);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
989 return NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
990 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
991 retval = PyString_FromStringAndSize((const char *) bytes_buf, len);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
992 OPENSSL_free(bytes_buf);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
993 return retval;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
994
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
995 } else {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
996
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
997 verification = SSL_CTX_get_verify_mode(self->ctx);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
998 if ((verification & SSL_VERIFY_PEER) == 0)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
999 return PyDict_New();
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1000 else
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1001 return _decode_certificate (self->peer_cert, 0);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1002 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1003 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1004
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1005 PyDoc_STRVAR(PySSL_peercert_doc,
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1006 "peer_certificate([der=False]) -> certificate\n\
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1007 \n\
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1008 Returns the certificate for the peer. If no certificate was provided,\n\
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1009 returns None. If a certificate was provided, but not validated, returns\n\
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1010 an empty dictionary. Otherwise returns a dict containing information\n\
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1011 about the peer certificate.\n\
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1012 \n\
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1013 If the optional argument is True, returns a DER-encoded copy of the\n\
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1014 peer certificate, or None if no certificate was provided. This will\n\
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1015 return the certificate even if it wasn't validated.");
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1016
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1017 static PyObject *PySSL_cipher (PySSLObject *self) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1018
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1019 PyObject *retval, *v;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1020 SSL_CIPHER *current;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1021 char *cipher_name;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1022 char *cipher_protocol;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1023
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1024 if (self->ssl == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1025 return Py_None;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1026 current = SSL_get_current_cipher(self->ssl);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1027 if (current == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1028 return Py_None;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1029
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1030 retval = PyTuple_New(3);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1031 if (retval == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1032 return NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1033
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1034 cipher_name = (char *) SSL_CIPHER_get_name(current);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1035 if (cipher_name == NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1036 PyTuple_SET_ITEM(retval, 0, Py_None);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1037 } else {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1038 v = PyString_FromString(cipher_name);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1039 if (v == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1040 goto fail0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1041 PyTuple_SET_ITEM(retval, 0, v);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1042 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1043 cipher_protocol = SSL_CIPHER_get_version(current);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1044 if (cipher_protocol == NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1045 PyTuple_SET_ITEM(retval, 1, Py_None);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1046 } else {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1047 v = PyString_FromString(cipher_protocol);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1048 if (v == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1049 goto fail0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1050 PyTuple_SET_ITEM(retval, 1, v);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1051 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1052 v = PyInt_FromLong(SSL_CIPHER_get_bits(current, NULL));
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1053 if (v == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1054 goto fail0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1055 PyTuple_SET_ITEM(retval, 2, v);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1056 return retval;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1057
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1058 fail0:
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1059 Py_DECREF(retval);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1060 return NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1061 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1062
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1063 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
1064 {
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1065 if (self->peer_cert) /* Possible not to have one? */
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1066 X509_free (self->peer_cert);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1067 if (self->ssl)
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1068 SSL_free(self->ssl);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1069 if (self->ctx)
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1070 SSL_CTX_free(self->ctx);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1071 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
1072 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
1073 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1074
39059
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1075 /* If the socket has a timeout, do a select()/poll() on the socket.
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1076 The argument writing indicates the direction.
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1077 Returns one of the possibilities in the timeout_state enum (above).
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1078 */
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1079
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1080 static int
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1081 check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing)
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1082 {
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1083 fd_set fds;
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1084 struct timeval tv;
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1085 int rc;
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1086
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1087 /* Nothing to do unless we're in timeout mode (not non-blocking) */
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1088 if (s->sock_timeout < 0.0)
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1089 return SOCKET_IS_BLOCKING;
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1090 else if (s->sock_timeout == 0.0)
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1091 return SOCKET_IS_NONBLOCKING;
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1092
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1093 /* Guard against closed socket */
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1094 if (s->sock_fd < 0)
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1095 return SOCKET_HAS_BEEN_CLOSED;
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1096
39059
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1097 /* Prefer poll, if available, since you can poll() any fd
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1098 * which can't be done with select(). */
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1099 #ifdef HAVE_POLL
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1100 {
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1101 struct pollfd pollfd;
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1102 int timeout;
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1103
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1104 pollfd.fd = s->sock_fd;
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1105 pollfd.events = writing ? POLLOUT : POLLIN;
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1106
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1107 /* s->sock_timeout is in seconds, timeout in ms */
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1108 timeout = (int)(s->sock_timeout * 1000 + 0.5);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1109 PySSL_BEGIN_ALLOW_THREADS
39059
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1110 rc = poll(&pollfd, 1, timeout);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1111 PySSL_END_ALLOW_THREADS
39059
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1112
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1113 goto normal_return;
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1114 }
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1115 #endif
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1116
36452
7789cc557e1e Bug #876637, prevent stack corruption when socket descriptor
Neal Norwitz <nnorwitz@gmail.com>
parents: 36451
diff changeset
1117 /* Guard against socket too large for select*/
36476
c073dffbe70e Introduce Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE.
Martin v. Löwis <martin@v.loewis.de>
parents: 36452
diff changeset
1118 #ifndef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
36452
7789cc557e1e Bug #876637, prevent stack corruption when socket descriptor
Neal Norwitz <nnorwitz@gmail.com>
parents: 36451
diff changeset
1119 if (s->sock_fd >= FD_SETSIZE)
36487
07ba8aae3dc6 Try to improve name based on discussion on python-checkins with Jim Jewett
Neal Norwitz <nnorwitz@gmail.com>
parents: 36476
diff changeset
1120 return SOCKET_TOO_LARGE_FOR_SELECT;
36476
c073dffbe70e Introduce Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE.
Martin v. Löwis <martin@v.loewis.de>
parents: 36452
diff changeset
1121 #endif
36452
7789cc557e1e Bug #876637, prevent stack corruption when socket descriptor
Neal Norwitz <nnorwitz@gmail.com>
parents: 36451
diff changeset
1122
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1123 /* Construct the arguments to select */
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1124 tv.tv_sec = (int)s->sock_timeout;
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1125 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1126 FD_ZERO(&fds);
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1127 FD_SET(s->sock_fd, &fds);
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1128
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1129 /* See if the socket is ready */
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1130 PySSL_BEGIN_ALLOW_THREADS
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1131 if (writing)
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1132 rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1133 else
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1134 rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1135 PySSL_END_ALLOW_THREADS
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1136
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1137 #ifdef HAVE_POLL
39059
52a8738f5ab4 #1494314: Fix a regression with high-numbered sockets in 2.4.3. This
Anthony Baxter <anthonybaxter@gmail.com>
parents: 38152
diff changeset
1138 normal_return:
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1139 #endif
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1140 /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1141 (when we are able to write or when there's something to read) */
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1142 return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK;
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1143 }
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1144
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1145 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
1146 {
56626
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1147 Py_buffer buf;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1148 int len;
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1149 int sockstate;
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1150 int err;
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1151 int nonblocking;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1152
56626
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1153 if (!PyArg_ParseTuple(args, "s*:write", &buf))
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1154 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
1155
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1156 /* just in case the blocking state of the socket has been changed */
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1157 nonblocking = (self->Socket->sock_timeout >= 0.0);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1158 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1159 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1160
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1161 sockstate = check_socket_and_wait_for_timeout(self->Socket, 1);
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1162 if (sockstate == SOCKET_HAS_TIMED_OUT) {
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1163 PyErr_SetString(PySSLErrorObject,
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1164 "The write operation timed out");
56626
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1165 goto error;
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1166 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1167 PyErr_SetString(PySSLErrorObject,
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1168 "Underlying socket has been closed.");
56626
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1169 goto error;
36487
07ba8aae3dc6 Try to improve name based on discussion on python-checkins with Jim Jewett
Neal Norwitz <nnorwitz@gmail.com>
parents: 36476
diff changeset
1170 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1171 PyErr_SetString(PySSLErrorObject,
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1172 "Underlying socket too large for select().");
56626
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1173 goto error;
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1174 }
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1175 do {
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1176 err = 0;
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1177 PySSL_BEGIN_ALLOW_THREADS
56626
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1178 len = SSL_write(self->ssl, buf.buf, buf.len);
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1179 err = SSL_get_error(self->ssl, len);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1180 PySSL_END_ALLOW_THREADS
56626
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1181 if (PyErr_CheckSignals()) {
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1182 goto error;
29629
a4d6bc71f091 Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents: 27594
diff changeset
1183 }
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1184 if (err == SSL_ERROR_WANT_READ) {
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1185 sockstate =
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1186 check_socket_and_wait_for_timeout(self->Socket, 0);
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1187 } else if (err == SSL_ERROR_WANT_WRITE) {
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1188 sockstate =
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1189 check_socket_and_wait_for_timeout(self->Socket, 1);
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1190 } else {
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1191 sockstate = SOCKET_OPERATION_OK;
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1192 }
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1193 if (sockstate == SOCKET_HAS_TIMED_OUT) {
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1194 PyErr_SetString(PySSLErrorObject,
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1195 "The write operation timed out");
56626
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1196 goto error;
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1197 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1198 PyErr_SetString(PySSLErrorObject,
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1199 "Underlying socket has been closed.");
56626
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1200 goto error;
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1201 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1202 break;
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1203 }
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1204 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
56626
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1205
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1206 PyBuffer_Release(&buf);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1207 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
1208 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
1209 else
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1210 return PySSL_SetError(self, len, __FILE__, __LINE__);
56626
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1211
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1212 error:
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1213 PyBuffer_Release(&buf);
43d0b47106df Issue #7133: SSL objects now support the new buffer API.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54412
diff changeset
1214 return NULL;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1215 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1216
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1217 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
1218 "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
1219 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1220 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
1221 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
1222
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1223 static PyObject *PySSL_SSLpending(PySSLObject *self)
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1224 {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1225 int count = 0;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1226
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1227 PySSL_BEGIN_ALLOW_THREADS
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1228 count = SSL_pending(self->ssl);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1229 PySSL_END_ALLOW_THREADS
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1230 if (count < 0)
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1231 return PySSL_SetError(self, count, __FILE__, __LINE__);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1232 else
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1233 return PyInt_FromLong(count);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1234 }
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1235
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1236 PyDoc_STRVAR(PySSL_SSLpending_doc,
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1237 "pending() -> count\n\
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1238 \n\
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1239 Returns the number of already decrypted bytes available for read,\n\
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1240 pending on the connection.\n");
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1241
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1242 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
1243 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1244 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
1245 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
1246 int len = 1024;
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1247 int sockstate;
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1248 int err;
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1249 int nonblocking;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1250
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1251 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
1252 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
1253
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1254 if (!(buf = PyString_FromStringAndSize((char *) 0, len)))
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1255 return NULL;
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1256
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1257 /* just in case the blocking state of the socket has been changed */
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1258 nonblocking = (self->Socket->sock_timeout >= 0.0);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1259 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1260 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1261
37339
6fef957712b9 Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36487
diff changeset
1262 /* first check if there are bytes ready to be read */
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1263 PySSL_BEGIN_ALLOW_THREADS
37339
6fef957712b9 Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36487
diff changeset
1264 count = SSL_pending(self->ssl);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1265 PySSL_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
1266
37339
6fef957712b9 Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36487
diff changeset
1267 if (!count) {
6fef957712b9 Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36487
diff changeset
1268 sockstate = check_socket_and_wait_for_timeout(self->Socket, 0);
6fef957712b9 Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36487
diff changeset
1269 if (sockstate == SOCKET_HAS_TIMED_OUT) {
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1270 PyErr_SetString(PySSLErrorObject,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1271 "The read operation timed out");
37339
6fef957712b9 Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36487
diff changeset
1272 Py_DECREF(buf);
6fef957712b9 Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36487
diff changeset
1273 return NULL;
6fef957712b9 Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36487
diff changeset
1274 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1275 PyErr_SetString(PySSLErrorObject,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1276 "Underlying socket too large for select().");
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1277 Py_DECREF(buf);
37339
6fef957712b9 Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36487
diff changeset
1278 return NULL;
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1279 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1280 if (SSL_get_shutdown(self->ssl) !=
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1281 SSL_RECEIVED_SHUTDOWN)
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1282 {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1283 Py_DECREF(buf);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1284 PyErr_SetString(PySSLErrorObject,
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1285 "Socket closed without SSL shutdown handshake");
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1286 return NULL;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1287 } else {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1288 /* should contain a zero-length string */
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1289 _PyString_Resize(&buf, 0);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1290 return buf;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1291 }
37339
6fef957712b9 Patch #1380952: fix SSL objects timing out on consecutive read()s
Georg Brandl <georg@python.org>
parents: 36487
diff changeset
1292 }
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1293 }
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1294 do {
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1295 err = 0;
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1296 PySSL_BEGIN_ALLOW_THREADS
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1297 count = SSL_read(self->ssl, PyString_AsString(buf), len);
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1298 err = SSL_get_error(self->ssl, count);
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1299 PySSL_END_ALLOW_THREADS
29629
a4d6bc71f091 Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents: 27594
diff changeset
1300 if(PyErr_CheckSignals()) {
a4d6bc71f091 Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents: 27594
diff changeset
1301 Py_DECREF(buf);
a4d6bc71f091 Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents: 27594
diff changeset
1302 return NULL;
a4d6bc71f091 Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents: 27594
diff changeset
1303 }
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1304 if (err == SSL_ERROR_WANT_READ) {
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1305 sockstate =
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1306 check_socket_and_wait_for_timeout(self->Socket, 0);
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1307 } else if (err == SSL_ERROR_WANT_WRITE) {
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1308 sockstate =
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1309 check_socket_and_wait_for_timeout(self->Socket, 1);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1310 } else if ((err == SSL_ERROR_ZERO_RETURN) &&
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1311 (SSL_get_shutdown(self->ssl) ==
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1312 SSL_RECEIVED_SHUTDOWN))
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1313 {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1314 _PyString_Resize(&buf, 0);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1315 return buf;
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1316 } else {
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1317 sockstate = SOCKET_OPERATION_OK;
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1318 }
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1319 if (sockstate == SOCKET_HAS_TIMED_OUT) {
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1320 PyErr_SetString(PySSLErrorObject,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1321 "The read operation timed out");
29629
a4d6bc71f091 Patch #751916: Check for signals, fix some refcounting errors.
Martin v. Löwis <martin@v.loewis.de>
parents: 27594
diff changeset
1322 Py_DECREF(buf);
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1323 return NULL;
32599
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1324 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
598a5ce63322 [Patch #945642] Fix non-blocking SSL sockets, which blocked on reads/writes in Python 2.3.
Andrew M. Kuchling <amk@amk.ca>
parents: 31790
diff changeset
1325 break;
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1326 }
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
1327 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1328 if (count <= 0) {
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1329 Py_DECREF(buf);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1330 return PySSL_SetError(self, count, __FILE__, __LINE__);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1331 }
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23190
diff changeset
1332 if (count != len)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1333 _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
1334 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
1335 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1336
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1337 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
1338 "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
1339 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1340 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
1341
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1342 static PyObject *PySSL_SSLshutdown(PySSLObject *self)
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1343 {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1344 int err;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1345
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1346 /* Guard against closed socket */
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1347 if (self->Socket->sock_fd < 0) {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1348 PyErr_SetString(PySSLErrorObject,
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1349 "Underlying socket has been closed.");
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1350 return NULL;
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1351 }
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1352
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1353 PySSL_BEGIN_ALLOW_THREADS
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1354 err = SSL_shutdown(self->ssl);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1355 if (err == 0) {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1356 /* we need to call it again to finish the shutdown */
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1357 err = SSL_shutdown(self->ssl);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1358 }
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1359 PySSL_END_ALLOW_THREADS
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1360
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1361 if (err < 0)
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1362 return PySSL_SetError(self, err, __FILE__, __LINE__);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1363 else {
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1364 Py_INCREF(self->Socket);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1365 return (PyObject *) (self->Socket);
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1366 }
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1367 }
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1368
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1369 PyDoc_STRVAR(PySSL_SSLshutdown_doc,
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1370 "shutdown(s) -> socket\n\
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1371 \n\
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1372 Does the SSL shutdown handshake with the remote end, and returns\n\
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1373 the underlying socket object.");
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1374
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1375 static PyMethodDef PySSLMethods[] = {
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1376 {"do_handshake", (PyCFunction)PySSL_SSLdo_handshake, METH_NOARGS},
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1377 {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS,
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1378 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
1379 {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS,
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1380 PySSL_SSLread_doc},
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1381 {"pending", (PyCFunction)PySSL_SSLpending, METH_NOARGS,
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1382 PySSL_SSLpending_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
1383 {"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
1384 {"issuer", (PyCFunction)PySSL_issuer, METH_NOARGS},
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1385 {"peer_certificate", (PyCFunction)PySSL_peercert, METH_VARARGS,
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1386 PySSL_peercert_doc},
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1387 {"cipher", (PyCFunction)PySSL_cipher, METH_NOARGS},
47848
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1388 {"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS,
8a281bfc058d various SSL fixes; issues 1251, 3162, 3212
Bill Janssen <janssen@parc.com>
parents: 47527
diff changeset
1389 PySSL_SSLshutdown_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
1390 {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
1391 };
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1392
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1393 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
1394 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1395 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
1396 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1397
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24086
diff changeset
1398 static PyTypeObject PySSL_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 40152
diff changeset
1399 PyVarObject_HEAD_INIT(NULL, 0)
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1400 "ssl.SSLContext", /*tp_name*/
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1401 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
1402 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
1403 /* methods */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1404 (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
1405 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
1406 (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
1407 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
1408 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
1409 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
1410 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
1411 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
1412 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
1413 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
1414 };
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1415
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1416 #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
1417
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1418 /* 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
1419 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
1420 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
1421 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1422 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
1423 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
1424 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
1425
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1426 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
1427 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
1428 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
1429 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
1430 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
1431 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1432
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1433 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
1434 "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
1435 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1436 Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1437 bound on the entropy contained in string. See RFC 1750.");
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1438
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1439 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
1440 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
1441 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1442 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
1443 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1444
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1445 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
1446 "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
1447 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1448 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
1449 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
1450 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
1451
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1452 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
1453 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
1454 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1455 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
1456
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1457 if (!PyString_Check(arg))
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1458 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
1459 "RAND_egd() expected string, found %s",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44296
diff changeset
1460 Py_TYPE(arg)->tp_name);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1461 bytes = RAND_egd(PyString_AS_STRING(arg));
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1462 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
1463 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
1464 "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
1465 "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
1466 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
1467 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1468 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
1469 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1470
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1471 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
1472 "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
1473 \n\
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1474 Queries the entropy gather daemon (EGD) on the socket named by 'path'.\n\
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1475 Returns number of bytes read. Raises SSLError if connection to EGD\n\
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1476 fails or 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
1477
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1478 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1479
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1480 /* 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
1481
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1482 static PyMethodDef PySSL_methods[] = {
42908
5805fe17cb2d > Some of the code sets the error string in this directly before
Guido van Rossum <guido@python.org>
parents: 42831
diff changeset
1483 {"sslwrap", PySSL_sslwrap,
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1484 METH_VARARGS, ssl_doc},
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1485 {"_test_decode_cert", PySSL_test_decode_certificate,
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1486 METH_VARARGS},
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1487 #ifdef HAVE_OPENSSL_RAND
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1488 {"RAND_add", PySSL_RAND_add, METH_VARARGS,
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1489 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
1490 {"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
1491 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
1492 {"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
1493 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
1494 #endif
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1495 {NULL, NULL} /* Sentinel */
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1496 };
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1497
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1498
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1499 #ifdef WITH_THREAD
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1500
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1501 /* an implementation of OpenSSL threading operations in terms
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1502 of the Python C thread library */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1503
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1504 static PyThread_type_lock *_ssl_locks = NULL;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1505
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1506 static unsigned long _ssl_thread_id_function (void) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1507 return PyThread_get_thread_ident();
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1508 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1509
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1510 static void _ssl_thread_locking_function (int mode, int n, const char *file, int line) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1511 /* this function is needed to perform locking on shared data
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1512 structures. (Note that OpenSSL uses a number of global data
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1513 structures that will be implicitly shared whenever multiple threads
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1514 use OpenSSL.) Multi-threaded applications will crash at random if
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1515 it is not set.
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1516
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1517 locking_function() must be able to handle up to CRYPTO_num_locks()
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1518 different mutex locks. It sets the n-th lock if mode & CRYPTO_LOCK, and
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1519 releases it otherwise.
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1520
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1521 file and line are the file number of the function setting the
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1522 lock. They can be useful for debugging.
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1523 */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1524
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1525 if ((_ssl_locks == NULL) ||
46134
313f381fc0a7 Fix compiler warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 45006
diff changeset
1526 (n < 0) || ((unsigned)n >= _ssl_locks_count))
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1527 return;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1528
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1529 if (mode & CRYPTO_LOCK) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1530 PyThread_acquire_lock(_ssl_locks[n], 1);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1531 } else {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1532 PyThread_release_lock(_ssl_locks[n]);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1533 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1534 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1535
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1536 static int _setup_ssl_threads(void) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1537
46134
313f381fc0a7 Fix compiler warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 45006
diff changeset
1538 unsigned int i;
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1539
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1540 if (_ssl_locks == NULL) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1541 _ssl_locks_count = CRYPTO_num_locks();
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1542 _ssl_locks = (PyThread_type_lock *)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1543 malloc(sizeof(PyThread_type_lock) * _ssl_locks_count);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1544 if (_ssl_locks == NULL)
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1545 return 0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1546 memset(_ssl_locks, 0, sizeof(PyThread_type_lock) * _ssl_locks_count);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1547 for (i = 0; i < _ssl_locks_count; i++) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1548 _ssl_locks[i] = PyThread_allocate_lock();
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1549 if (_ssl_locks[i] == NULL) {
51069
36fbaf7538ce Fix signed/unsigned mismatch.
Raymond Hettinger <python@rcn.com>
parents: 47848
diff changeset
1550 unsigned int j;
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1551 for (j = 0; j < i; j++) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1552 PyThread_free_lock(_ssl_locks[j]);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1553 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1554 free(_ssl_locks);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1555 return 0;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1556 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1557 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1558 CRYPTO_set_locking_callback(_ssl_thread_locking_function);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1559 CRYPTO_set_id_callback(_ssl_thread_id_function);
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1560 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1561 return 1;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1562 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1563
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1564 #endif /* def HAVE_THREAD */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1565
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1566 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
1567 "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
1568 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
1569
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24398
diff changeset
1570 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
1571 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
1572 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1573 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
1574
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44296
diff changeset
1575 Py_TYPE(&PySSL_Type) = &PyType_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
1576
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1577 m = Py_InitModule3("_ssl", PySSL_methods, module_doc);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32960
diff changeset
1578 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32960
diff changeset
1579 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
1580 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
1581
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1582 /* 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
1583 if (PySocketModule_ImportModuleAndAPI())
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1584 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
1585
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1586 /* 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
1587 SSL_load_error_strings();
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1588 #ifdef WITH_THREAD
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1589 /* note that this will start threading if not already started */
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1590 if (!_setup_ssl_threads()) {
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1591 return;
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1592 }
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1593 #endif
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1594 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
1595
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1596 /* Add symbols to module dict */
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1597 PySSLErrorObject = PyErr_NewException("ssl.SSLError",
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1598 PySocketModule.error,
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1599 NULL);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1600 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
1601 return;
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43243
diff changeset
1602 if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0)
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1603 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
1604 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
1605 (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
1606 return;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1607 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
1608 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
1609 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
1610 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
1611 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
1612 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
1613 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
1614 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
1615 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
1616 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
1617 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
1618 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
1619 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
1620 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
1621 /* 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
1622 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
1623 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
1624 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
1625 PY_SSL_ERROR_INVALID_ERROR_CODE);
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1626 /* cert requirements */
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1627 PyModule_AddIntConstant(m, "CERT_NONE",
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1628 PY_SSL_CERT_NONE);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1629 PyModule_AddIntConstant(m, "CERT_OPTIONAL",
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1630 PY_SSL_CERT_OPTIONAL);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1631 PyModule_AddIntConstant(m, "CERT_REQUIRED",
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1632 PY_SSL_CERT_REQUIRED);
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
1633
42828
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1634 /* protocol versions */
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1635 PyModule_AddIntConstant(m, "PROTOCOL_SSLv2",
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1636 PY_SSL_VERSION_SSL2);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1637 PyModule_AddIntConstant(m, "PROTOCOL_SSLv3",
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1638 PY_SSL_VERSION_SSL3);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1639 PyModule_AddIntConstant(m, "PROTOCOL_SSLv23",
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1640 PY_SSL_VERSION_SSL23);
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1641 PyModule_AddIntConstant(m, "PROTOCOL_TLSv1",
08b6278aa1f4 Server-side SSL and certificate validation, by Bill Janssen.
Guido van Rossum <guido@python.org>
parents: 42333
diff changeset
1642 PY_SSL_VERSION_TLS1);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1643 }