annotate Modules/_ssl.c @ 51271:87fbbf22adf3 3.0

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