annotate Modules/_ssl.c @ 83191:0437c19cdad0

gibibytes (Arfrever)
author Antoine Pitrou <solipsis@pitrou.net>
date Sun, 07 Apr 2013 23:46:52 +0200
parents 411e5726f84d
children 4ae6095b4638
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?
60901
fc97784e5cc2 Merged revisions 80394 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60818
diff changeset
12
fc97784e5cc2 Merged revisions 80394 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60818
diff changeset
13 XXX integrate several "shutdown modes" as suggested in
fc97784e5cc2 Merged revisions 80394 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60818
diff changeset
14 http://bugs.python.org/issue8108#msg102867 ?
21959
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
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
17 #include "Python.h"
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
18
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
19 #ifdef WITH_THREAD
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
20 #include "pythread.h"
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
21 #define PySSL_BEGIN_ALLOW_THREADS_S(save) \
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
22 do { if (_ssl_locks_count>0) { (save) = PyEval_SaveThread(); } } while (0)
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
23 #define PySSL_END_ALLOW_THREADS_S(save) \
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
24 do { if (_ssl_locks_count>0) { PyEval_RestoreThread(save); } } while (0)
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
25 #define PySSL_BEGIN_ALLOW_THREADS { \
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
26 PyThreadState *_save = NULL; \
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
27 PySSL_BEGIN_ALLOW_THREADS_S(_save);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
28 #define PySSL_BLOCK_THREADS PySSL_END_ALLOW_THREADS_S(_save);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
29 #define PySSL_UNBLOCK_THREADS PySSL_BEGIN_ALLOW_THREADS_S(_save);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
30 #define PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS_S(_save); }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
31
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
32 #else /* no WITH_THREAD */
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
33
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
34 #define PySSL_BEGIN_ALLOW_THREADS_S(save)
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
35 #define PySSL_END_ALLOW_THREADS_S(save)
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
36 #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
37 #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
38 #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
39 #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
40
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
41 #endif
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
42
82992
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
43 /* Include symbols from _socket module */
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
44 #include "socketmodule.h"
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
45
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
46 static PySocketModule_APIObject PySocketModule;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
47
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
48 #if defined(HAVE_POLL_H)
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
49 #include <poll.h>
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
50 #elif defined(HAVE_SYS_POLL_H)
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
51 #include <sys/poll.h>
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
52 #endif
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
53
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
54 /* Include OpenSSL header files */
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
55 #include "openssl/rsa.h"
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
56 #include "openssl/crypto.h"
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
57 #include "openssl/x509.h"
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
58 #include "openssl/x509v3.h"
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
59 #include "openssl/pem.h"
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
60 #include "openssl/ssl.h"
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
61 #include "openssl/err.h"
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
62 #include "openssl/rand.h"
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
63
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
64 /* SSL error object */
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
65 static PyObject *PySSLErrorObject;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
66 static PyObject *PySSLZeroReturnErrorObject;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
67 static PyObject *PySSLWantReadErrorObject;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
68 static PyObject *PySSLWantWriteErrorObject;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
69 static PyObject *PySSLSyscallErrorObject;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
70 static PyObject *PySSLEOFErrorObject;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
71
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
72 /* Error mappings */
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
73 static PyObject *err_codes_to_names;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
74 static PyObject *err_names_to_codes;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
75 static PyObject *lib_codes_to_names;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
76
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
77 struct py_ssl_error_code {
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
78 const char *mnemonic;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
79 int library, reason;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
80 };
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
81 struct py_ssl_library_code {
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
82 const char *library;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
83 int code;
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
84 };
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
85
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
86 /* Include generated data (error codes) */
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
87 #include "_ssl_data.h"
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
88
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
89 /* Openssl comes with TLSv1.1 and TLSv1.2 between 1.0.0h and 1.0.1
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
90 http://www.openssl.org/news/changelog.html
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
91 */
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
92 #if OPENSSL_VERSION_NUMBER >= 0x10001000L
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
93 # define HAVE_TLSv1_2 1
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
94 #else
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
95 # define HAVE_TLSv1_2 0
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
96 #endif
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
97
83026
411e5726f84d Fix comment about the OpenSSL version in which SNI version was introduced.
Antoine Pitrou <solipsis@pitrou.net>
parents: 83025
diff changeset
98 /* SNI support (client- and server-side) appeared in OpenSSL 1.0.0.
83021
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
99 * This includes the SSL_set_SSL_CTX() function.
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
100 */
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
101 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
102 # define HAVE_SNI 1
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
103 #else
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
104 # define HAVE_SNI 0
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
105 #endif
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
106
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
107 enum py_ssl_error {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
108 /* these mirror ssl.h */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
109 PY_SSL_ERROR_NONE,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
110 PY_SSL_ERROR_SSL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
111 PY_SSL_ERROR_WANT_READ,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
112 PY_SSL_ERROR_WANT_WRITE,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
113 PY_SSL_ERROR_WANT_X509_LOOKUP,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
114 PY_SSL_ERROR_SYSCALL, /* look at error stack/return value/errno */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
115 PY_SSL_ERROR_ZERO_RETURN,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
116 PY_SSL_ERROR_WANT_CONNECT,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
117 /* start of non ssl.h errorcodes */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
118 PY_SSL_ERROR_EOF, /* special case of SSL_ERROR_SYSCALL */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
119 PY_SSL_ERROR_NO_SOCKET, /* socket has been GC'd */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
120 PY_SSL_ERROR_INVALID_ERROR_CODE
23190
0bc3a41d4e40 Forward port of patch # 500311: Work around for buggy https servers.
Martin v. Löwis <martin@v.loewis.de>
parents: 21959
diff changeset
121 };
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
122
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
123 enum py_ssl_server_or_client {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
124 PY_SSL_CLIENT,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
125 PY_SSL_SERVER
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
126 };
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
127
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
128 enum py_ssl_cert_requirements {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
129 PY_SSL_CERT_NONE,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
130 PY_SSL_CERT_OPTIONAL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
131 PY_SSL_CERT_REQUIRED
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
132 };
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
133
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
134 enum py_ssl_version {
69997
b7abf0590e1c Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66806
diff changeset
135 #ifndef OPENSSL_NO_SSL2
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
136 PY_SSL_VERSION_SSL2,
69997
b7abf0590e1c Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66806
diff changeset
137 #endif
b7abf0590e1c Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66806
diff changeset
138 PY_SSL_VERSION_SSL3=1,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
139 PY_SSL_VERSION_SSL23,
82992
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
140 #if HAVE_TLSv1_2
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
141 PY_SSL_VERSION_TLS1,
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
142 PY_SSL_VERSION_TLS1_1,
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
143 PY_SSL_VERSION_TLS1_2
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
144 #else
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
145 PY_SSL_VERSION_TLS1
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
146 #endif
82992
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
147 };
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
148
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
149 #ifdef WITH_THREAD
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
150
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
151 /* 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
152 /* 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
153
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
154 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
155
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
156 #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
157
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
158 /* 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
159
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
160 #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
161
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
162 /* 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
163 #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
164 # 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
165 #else
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
166 # 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
167 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
168
65246
23ec0be07e20 Fix compile on NetBSD 5.0 (or anything else using an old 0.9.9-dev OpenSSL).
Gregory P. Smith <greg@mad-scientist.com>
parents: 65223
diff changeset
169 /* SSL_CTX_clear_options() and SSL_clear_options() were first added in
23ec0be07e20 Fix compile on NetBSD 5.0 (or anything else using an old 0.9.9-dev OpenSSL).
Gregory P. Smith <greg@mad-scientist.com>
parents: 65223
diff changeset
170 * OpenSSL 0.9.8m but do not appear in some 0.9.9-dev versions such the
23ec0be07e20 Fix compile on NetBSD 5.0 (or anything else using an old 0.9.9-dev OpenSSL).
Gregory P. Smith <greg@mad-scientist.com>
parents: 65223
diff changeset
171 * 0.9.9 from "May 2008" that NetBSD 5.0 uses. */
23ec0be07e20 Fix compile on NetBSD 5.0 (or anything else using an old 0.9.9-dev OpenSSL).
Gregory P. Smith <greg@mad-scientist.com>
parents: 65223
diff changeset
172 #if OPENSSL_VERSION_NUMBER >= 0x009080dfL && OPENSSL_VERSION_NUMBER != 0x00909000L
61782
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
173 # define HAVE_SSL_CTX_CLEAR_OPTIONS
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
174 #else
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
175 # undef HAVE_SSL_CTX_CLEAR_OPTIONS
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
176 #endif
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
177
71440
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
178 /* In case of 'tls-unique' it will be 12 bytes for TLS, 36 bytes for
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
179 * older SSL, but let's be safe */
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
180 #define PySSL_CB_MAXLEN 128
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
181
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
182 /* SSL_get_finished got added to OpenSSL in 0.9.5 */
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
183 #if OPENSSL_VERSION_NUMBER >= 0x0090500fL
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
184 # define HAVE_OPENSSL_FINISHED 1
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
185 #else
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
186 # define HAVE_OPENSSL_FINISHED 0
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
187 #endif
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
188
75005
06ed9b3f02af Try to really fix compilation failures of the _ssl module under very old OpenSSLs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74995
diff changeset
189 /* ECDH support got added to OpenSSL in 0.9.8 */
06ed9b3f02af Try to really fix compilation failures of the _ssl module under very old OpenSSLs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74995
diff changeset
190 #if OPENSSL_VERSION_NUMBER < 0x0090800fL && !defined(OPENSSL_NO_ECDH)
06ed9b3f02af Try to really fix compilation failures of the _ssl module under very old OpenSSLs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74995
diff changeset
191 # define OPENSSL_NO_ECDH
06ed9b3f02af Try to really fix compilation failures of the _ssl module under very old OpenSSLs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74995
diff changeset
192 #endif
06ed9b3f02af Try to really fix compilation failures of the _ssl module under very old OpenSSLs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74995
diff changeset
193
75043
1a06f0a8120f Fix last remaining build issues of _ssl under old OpenSSLs. Patch by Vinay.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75005
diff changeset
194 /* compression support got added to OpenSSL in 0.9.8 */
1a06f0a8120f Fix last remaining build issues of _ssl under old OpenSSLs. Patch by Vinay.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75005
diff changeset
195 #if OPENSSL_VERSION_NUMBER < 0x0090800fL && !defined(OPENSSL_NO_COMP)
1a06f0a8120f Fix last remaining build issues of _ssl under old OpenSSLs. Patch by Vinay.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75005
diff changeset
196 # define OPENSSL_NO_COMP
1a06f0a8120f Fix last remaining build issues of _ssl under old OpenSSLs. Patch by Vinay.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75005
diff changeset
197 #endif
1a06f0a8120f Fix last remaining build issues of _ssl under old OpenSSLs. Patch by Vinay.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75005
diff changeset
198
75005
06ed9b3f02af Try to really fix compilation failures of the _ssl module under very old OpenSSLs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74995
diff changeset
199
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
200 typedef struct {
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
201 PyObject_HEAD
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
202 SSL_CTX *ctx;
75865
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
203 #ifdef OPENSSL_NPN_NEGOTIATED
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
204 char *npn_protocols;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
205 int npn_protocols_len;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
206 #endif
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
207 #ifndef OPENSSL_NO_TLSEXT
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
208 PyObject *set_hostname;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
209 #endif
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
210 } PySSLContext;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
211
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
212 typedef struct {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
213 PyObject_HEAD
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
214 PyObject *Socket; /* weakref to socket on which we're layered */
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
215 SSL *ssl;
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
216 PySSLContext *ctx; /* weakref to SSL context */
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
217 X509 *peer_cert;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
218 int shutdown_seen_zero;
71440
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
219 enum py_ssl_server_or_client socket_type;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
220 } PySSLSocket;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
221
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
222 static PyTypeObject PySSLContext_Type;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
223 static PyTypeObject PySSLSocket_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
224
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
225 static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
226 static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
227 static int check_socket_and_wait_for_timeout(PySocketSockObject *s,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
228 int writing);
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
229 static PyObject *PySSL_peercert(PySSLSocket *self, PyObject *args);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
230 static PyObject *PySSL_cipher(PySSLSocket *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
231
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
232 #define PySSLContext_Check(v) (Py_TYPE(v) == &PySSLContext_Type)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
233 #define PySSLSocket_Check(v) (Py_TYPE(v) == &PySSLSocket_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
234
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
235 typedef enum {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
236 SOCKET_IS_NONBLOCKING,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
237 SOCKET_IS_BLOCKING,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
238 SOCKET_HAS_TIMED_OUT,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
239 SOCKET_HAS_BEEN_CLOSED,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
240 SOCKET_TOO_LARGE_FOR_SELECT,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
241 SOCKET_OPERATION_OK
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
242 } 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
243
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
244 /* 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
245 #define STRINGIFY1(x) #x
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
246 #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
247 #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
248 #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
249
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
250
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
251 /*
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
252 * SSL errors.
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
253 */
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
254
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
255 PyDoc_STRVAR(SSLError_doc,
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
256 "An error occurred in the SSL implementation.");
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
257
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
258 PyDoc_STRVAR(SSLZeroReturnError_doc,
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
259 "SSL/TLS session closed cleanly.");
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
260
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
261 PyDoc_STRVAR(SSLWantReadError_doc,
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
262 "Non-blocking SSL socket needs to read more data\n"
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
263 "before the requested operation can be completed.");
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
264
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
265 PyDoc_STRVAR(SSLWantWriteError_doc,
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
266 "Non-blocking SSL socket needs to write more data\n"
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
267 "before the requested operation can be completed.");
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
268
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
269 PyDoc_STRVAR(SSLSyscallError_doc,
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
270 "System error when attempting SSL operation.");
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
271
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
272 PyDoc_STRVAR(SSLEOFError_doc,
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
273 "SSL/TLS connection terminated abruptly.");
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
274
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
275 static PyObject *
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
276 SSLError_str(PyOSErrorObject *self)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
277 {
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
278 if (self->strerror != NULL && PyUnicode_Check(self->strerror)) {
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
279 Py_INCREF(self->strerror);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
280 return self->strerror;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
281 }
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
282 else
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
283 return PyObject_Str(self->args);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
284 }
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
285
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
286 static PyType_Slot sslerror_type_slots[] = {
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
287 {Py_tp_base, NULL}, /* Filled out in module init as it's not a constant */
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
288 {Py_tp_doc, SSLError_doc},
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
289 {Py_tp_str, SSLError_str},
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
290 {0, 0},
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
291 };
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
292
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
293 static PyType_Spec sslerror_type_spec = {
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
294 "ssl.SSLError",
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
295 sizeof(PyOSErrorObject),
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
296 0,
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
297 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
298 sslerror_type_slots
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
299 };
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
300
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
301 static void
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
302 fill_and_set_sslerror(PyObject *type, int ssl_errno, const char *errstr,
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
303 int lineno, unsigned long errcode)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
304 {
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
305 PyObject *err_value = NULL, *reason_obj = NULL, *lib_obj = NULL;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
306 PyObject *init_value, *msg, *key;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
307 _Py_IDENTIFIER(reason);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
308 _Py_IDENTIFIER(library);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
309
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
310 if (errcode != 0) {
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
311 int lib, reason;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
312
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
313 lib = ERR_GET_LIB(errcode);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
314 reason = ERR_GET_REASON(errcode);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
315 key = Py_BuildValue("ii", lib, reason);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
316 if (key == NULL)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
317 goto fail;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
318 reason_obj = PyDict_GetItem(err_codes_to_names, key);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
319 Py_DECREF(key);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
320 if (reason_obj == NULL) {
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
321 /* XXX if reason < 100, it might reflect a library number (!!) */
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
322 PyErr_Clear();
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
323 }
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
324 key = PyLong_FromLong(lib);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
325 if (key == NULL)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
326 goto fail;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
327 lib_obj = PyDict_GetItem(lib_codes_to_names, key);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
328 Py_DECREF(key);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
329 if (lib_obj == NULL) {
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
330 PyErr_Clear();
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
331 }
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
332 if (errstr == NULL)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
333 errstr = ERR_reason_error_string(errcode);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
334 }
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
335 if (errstr == NULL)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
336 errstr = "unknown error";
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
337
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
338 if (reason_obj && lib_obj)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
339 msg = PyUnicode_FromFormat("[%S: %S] %s (_ssl.c:%d)",
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
340 lib_obj, reason_obj, errstr, lineno);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
341 else if (lib_obj)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
342 msg = PyUnicode_FromFormat("[%S] %s (_ssl.c:%d)",
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
343 lib_obj, errstr, lineno);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
344 else
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
345 msg = PyUnicode_FromFormat("%s (_ssl.c:%d)", errstr, lineno);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
346
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
347 if (msg == NULL)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
348 goto fail;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
349 init_value = Py_BuildValue("iN", ssl_errno, msg);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
350 err_value = PyObject_CallObject(type, init_value);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
351 Py_DECREF(init_value);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
352 if (err_value == NULL)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
353 goto fail;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
354 if (reason_obj == NULL)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
355 reason_obj = Py_None;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
356 if (_PyObject_SetAttrId(err_value, &PyId_reason, reason_obj))
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
357 goto fail;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
358 if (lib_obj == NULL)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
359 lib_obj = Py_None;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
360 if (_PyObject_SetAttrId(err_value, &PyId_library, lib_obj))
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
361 goto fail;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
362 PyErr_SetObject(type, err_value);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
363 fail:
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
364 Py_XDECREF(err_value);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
365 }
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
366
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
367 static PyObject *
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
368 PySSL_SetError(PySSLSocket *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
369 {
73150
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
370 PyObject *type = PySSLErrorObject;
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
371 char *errstr = NULL;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
372 int err;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
373 enum py_ssl_error p = PY_SSL_ERROR_NONE;
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
374 unsigned long e = 0;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
375
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
376 assert(ret <= 0);
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
377 e = ERR_peek_last_error();
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
378
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
379 if (obj->ssl != NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
380 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
381
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
382 switch (err) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
383 case SSL_ERROR_ZERO_RETURN:
73150
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
384 errstr = "TLS/SSL connection has been closed (EOF)";
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
385 type = PySSLZeroReturnErrorObject;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
386 p = PY_SSL_ERROR_ZERO_RETURN;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
387 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
388 case SSL_ERROR_WANT_READ:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
389 errstr = "The operation did not complete (read)";
73150
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
390 type = PySSLWantReadErrorObject;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
391 p = PY_SSL_ERROR_WANT_READ;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
392 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
393 case SSL_ERROR_WANT_WRITE:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
394 p = PY_SSL_ERROR_WANT_WRITE;
73150
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
395 type = PySSLWantWriteErrorObject;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
396 errstr = "The operation did not complete (write)";
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
397 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
398 case SSL_ERROR_WANT_X509_LOOKUP:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
399 p = PY_SSL_ERROR_WANT_X509_LOOKUP;
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
400 errstr = "The operation did not complete (X509 lookup)";
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
401 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
402 case SSL_ERROR_WANT_CONNECT:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
403 p = PY_SSL_ERROR_WANT_CONNECT;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
404 errstr = "The operation did not complete (connect)";
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
405 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
406 case SSL_ERROR_SYSCALL:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
407 {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
408 if (e == 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
409 PySocketSockObject *s
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
410 = (PySocketSockObject *) PyWeakref_GetObject(obj->Socket);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
411 if (ret == 0 || (((PyObject *)s) == Py_None)) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
412 p = PY_SSL_ERROR_EOF;
73150
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
413 type = PySSLEOFErrorObject;
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
414 errstr = "EOF occurred in violation of protocol";
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
415 } else if (ret == -1) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
416 /* underlying BIO reported an I/O error */
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
417 Py_INCREF(s);
61646
8b6401ae1262 Merged revisions 81242 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61537
diff changeset
418 ERR_clear_error();
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
419 s->errorhandler();
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
420 Py_DECREF(s);
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
421 return NULL;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
422 } else { /* possible? */
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
423 p = PY_SSL_ERROR_SYSCALL;
73150
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
424 type = PySSLSyscallErrorObject;
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
425 errstr = "Some I/O error occurred";
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
426 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
427 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
428 p = PY_SSL_ERROR_SYSCALL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
429 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
430 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
431 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
432 case SSL_ERROR_SSL:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
433 {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
434 p = PY_SSL_ERROR_SSL;
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
435 if (e == 0)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
436 /* possible? */
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
437 errstr = "A failure in the SSL library occurred";
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
438 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
439 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
440 default:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
441 p = PY_SSL_ERROR_INVALID_ERROR_CODE;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
442 errstr = "Invalid error code";
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
443 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
444 }
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
445 fill_and_set_sslerror(type, p, errstr, lineno, e);
61646
8b6401ae1262 Merged revisions 81242 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61537
diff changeset
446 ERR_clear_error();
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
447 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
448 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
449
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
450 static PyObject *
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
451 _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
452
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
453 if (errstr == NULL)
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
454 errcode = ERR_peek_last_error();
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
455 else
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
456 errcode = 0;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
457 fill_and_set_sslerror(PySSLErrorObject, errcode, errstr, lineno, errcode);
61646
8b6401ae1262 Merged revisions 81242 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61537
diff changeset
458 ERR_clear_error();
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
459 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
460 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
461
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
462 /*
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
463 * SSL objects
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
464 */
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
465
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
466 static PySSLSocket *
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
467 newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
468 enum py_ssl_server_or_client socket_type,
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
469 char *server_hostname)
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
470 {
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
471 PySSLSocket *self;
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
472 SSL_CTX *ctx = sslctx->ctx;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
473
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
474 self = PyObject_New(PySSLSocket, &PySSLSocket_Type);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
475 if (self == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
476 return NULL;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
477
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
478 self->peer_cert = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
479 self->ssl = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
480 self->Socket = NULL;
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
481 self->ctx = sslctx;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
482 Py_INCREF(sslctx);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
483
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
484 /* Make sure the SSL error state is initialized */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
485 (void) ERR_get_state();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
486 ERR_clear_error();
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
487
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
488 PySSL_BEGIN_ALLOW_THREADS
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
489 self->ssl = SSL_new(ctx);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
490 PySSL_END_ALLOW_THREADS
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
491 SSL_set_app_data(self->ssl,self);
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
492 SSL_set_fd(self->ssl, sock->sock_fd);
60464
837f3bb1575f Merged revisions 79912 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60038
diff changeset
493 #ifdef SSL_MODE_AUTO_RETRY
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
494 SSL_set_mode(self->ssl, SSL_MODE_AUTO_RETRY);
60464
837f3bb1575f Merged revisions 79912 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60038
diff changeset
495 #endif
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
496
83021
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
497 #if HAVE_SNI
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
498 if (server_hostname != NULL)
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
499 SSL_set_tlsext_host_name(self->ssl, server_hostname);
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
500 #endif
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
501
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
502 /* If the socket is in non-blocking mode or timeout mode, set the BIO
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
503 * to non-blocking mode (blocking is the default)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
504 */
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
505 if (sock->sock_timeout >= 0.0) {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
506 BIO_set_nbio(SSL_get_rbio(self->ssl), 1);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
507 BIO_set_nbio(SSL_get_wbio(self->ssl), 1);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
508 }
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
509
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
510 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
511 if (socket_type == PY_SSL_CLIENT)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
512 SSL_set_connect_state(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
513 else
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
514 SSL_set_accept_state(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
515 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
516
71440
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
517 self->socket_type = socket_type;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
518 self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
519 return 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
520 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
521
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
522 /* 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
523
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
524 static PyObject *PySSL_SSLdo_handshake(PySSLSocket *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
525 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
526 int ret;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
527 int err;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
528 int sockstate, nonblocking;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
529 PySocketSockObject *sock
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
530 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
60936
5ed34f3f6c35 Merged revisions 80454 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60901
diff changeset
531
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
532 if (((PyObject*)sock) == Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
533 _setSSLError("Underlying socket connection gone",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
534 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
535 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
536 }
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
537 Py_INCREF(sock);
60936
5ed34f3f6c35 Merged revisions 80454 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60901
diff changeset
538
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
539 /* just in case the blocking state of the socket has been changed */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
540 nonblocking = (sock->sock_timeout >= 0.0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
541 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
542 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
543
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
544 /* Actually negotiate SSL connection */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
545 /* XXX If SSL_do_handshake() returns 0, it's also a failure. */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
546 do {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
547 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
548 ret = SSL_do_handshake(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
549 err = SSL_get_error(self->ssl, ret);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
550 PySSL_END_ALLOW_THREADS
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
551 if (PyErr_CheckSignals())
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
552 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
553 if (err == SSL_ERROR_WANT_READ) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
554 sockstate = check_socket_and_wait_for_timeout(sock, 0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
555 } else if (err == SSL_ERROR_WANT_WRITE) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
556 sockstate = check_socket_and_wait_for_timeout(sock, 1);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
557 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
558 sockstate = SOCKET_OPERATION_OK;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
559 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
560 if (sockstate == SOCKET_HAS_TIMED_OUT) {
66663
3d9a86b8cc98 Issue #10272: The ssl module now raises socket.timeout instead of a generic
Antoine Pitrou <solipsis@pitrou.net>
parents: 66217
diff changeset
561 PyErr_SetString(PySocketModule.timeout_error,
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
562 ERRSTR("The handshake operation timed out"));
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
563 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
564 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
565 PyErr_SetString(PySSLErrorObject,
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
566 ERRSTR("Underlying socket has been closed."));
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
567 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
568 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
569 PyErr_SetString(PySSLErrorObject,
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
570 ERRSTR("Underlying socket too large for select()."));
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
571 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
572 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
573 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
574 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
575 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
576 Py_DECREF(sock);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
577 if (ret < 1)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
578 return PySSL_SetError(self, ret, __FILE__, __LINE__);
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
579
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
580 if (self->peer_cert)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
581 X509_free (self->peer_cert);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
582 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
583 self->peer_cert = SSL_get_peer_certificate(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
584 PySSL_END_ALLOW_THREADS
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
585
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
586 Py_INCREF(Py_None);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
587 return Py_None;
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
588
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
589 error:
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
590 Py_DECREF(sock);
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
591 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
592 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
593
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
594 static PyObject *
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
595 _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
596
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
597 char namebuf[X509_NAME_MAXLEN];
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
598 int buflen;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
599 PyObject *name_obj;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
600 PyObject *value_obj;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
601 PyObject *attr;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
602 unsigned char *valuebuf = NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
603
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
604 buflen = OBJ_obj2txt(namebuf, sizeof(namebuf), name, 0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
605 if (buflen < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
606 _setSSLError(NULL, 0, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
607 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
608 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
609 name_obj = PyUnicode_FromStringAndSize(namebuf, buflen);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
610 if (name_obj == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
611 goto fail;
43948
8e0c4c710375 Make read() and certificate() return bytes instead of bytearray instances.
Guido van Rossum <guido@python.org>
parents: 43869
diff changeset
612
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
613 buflen = ASN1_STRING_to_UTF8(&valuebuf, value);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
614 if (buflen < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
615 _setSSLError(NULL, 0, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
616 Py_DECREF(name_obj);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
617 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
618 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
619 value_obj = PyUnicode_DecodeUTF8((char *) valuebuf,
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
620 buflen, "strict");
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
621 OPENSSL_free(valuebuf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
622 if (value_obj == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
623 Py_DECREF(name_obj);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
624 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
625 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
626 attr = PyTuple_New(2);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
627 if (attr == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
628 Py_DECREF(name_obj);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
629 Py_DECREF(value_obj);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
630 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
631 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
632 PyTuple_SET_ITEM(attr, 0, name_obj);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
633 PyTuple_SET_ITEM(attr, 1, value_obj);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
634 return attr;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
635
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
636 fail:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
637 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
638 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
639
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
640 static PyObject *
43292
876b7ef4775d Merged revisions 57778-58052 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42954
diff changeset
641 _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
642 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
643 PyObject *dn = NULL; /* tuple which represents the "distinguished name" */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
644 PyObject *rdn = NULL; /* tuple to hold a "relative distinguished name" */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
645 PyObject *rdnt;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
646 PyObject *attr = NULL; /* tuple to hold an attribute */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
647 int entry_count = X509_NAME_entry_count(xname);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
648 X509_NAME_ENTRY *entry;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
649 ASN1_OBJECT *name;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
650 ASN1_STRING *value;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
651 int index_counter;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
652 int rdn_level = -1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
653 int retcode;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
654
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
655 dn = PyList_New(0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
656 if (dn == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
657 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
658 /* now create another tuple to hold the top-level RDN */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
659 rdn = PyList_New(0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
660 if (rdn == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
661 goto fail0;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
662
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
663 for (index_counter = 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
664 index_counter < entry_count;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
665 index_counter++)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
666 {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
667 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
668
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
669 /* check to see if we've gotten to a new RDN */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
670 if (rdn_level >= 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
671 if (rdn_level != entry->set) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
672 /* yes, new RDN */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
673 /* add old RDN to DN */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
674 rdnt = PyList_AsTuple(rdn);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
675 Py_DECREF(rdn);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
676 if (rdnt == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
677 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
678 retcode = PyList_Append(dn, rdnt);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
679 Py_DECREF(rdnt);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
680 if (retcode < 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
681 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
682 /* create new RDN */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
683 rdn = PyList_New(0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
684 if (rdn == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
685 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
686 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
687 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
688 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
689
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
690 /* now add this attribute to the current RDN */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
691 name = X509_NAME_ENTRY_get_object(entry);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
692 value = X509_NAME_ENTRY_get_data(entry);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
693 attr = _create_tuple_for_attribute(name, value);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
694 /*
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
695 fprintf(stderr, "RDN level %d, attribute %s: %s\n",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
696 entry->set,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
697 PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)),
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
698 PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1)));
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
699 */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
700 if (attr == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
701 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
702 retcode = PyList_Append(rdn, attr);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
703 Py_DECREF(attr);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
704 if (retcode < 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
705 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
706 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
707 /* now, there's typically a dangling RDN */
74963
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
708 if (rdn != NULL) {
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
709 if (PyList_GET_SIZE(rdn) > 0) {
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
710 rdnt = PyList_AsTuple(rdn);
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
711 Py_DECREF(rdn);
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
712 if (rdnt == NULL)
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
713 goto fail0;
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
714 retcode = PyList_Append(dn, rdnt);
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
715 Py_DECREF(rdnt);
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
716 if (retcode < 0)
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
717 goto fail0;
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
718 }
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
719 else {
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
720 Py_DECREF(rdn);
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
721 }
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
722 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
723
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
724 /* convert list to tuple */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
725 rdnt = PyList_AsTuple(dn);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
726 Py_DECREF(dn);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
727 if (rdnt == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
728 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
729 return rdnt;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
730
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
731 fail1:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
732 Py_XDECREF(rdn);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
733
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
734 fail0:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
735 Py_XDECREF(dn);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
736 return NULL;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
737 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
738
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
739 static PyObject *
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
740 _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
741
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
742 /* this code follows the procedure outlined in
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
743 OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print()
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
744 function to extract the STACK_OF(GENERAL_NAME),
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
745 then iterates through the stack to add the
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
746 names. */
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
747
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
748 int i, j;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
749 PyObject *peer_alt_names = Py_None;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
750 PyObject *v, *t;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
751 X509_EXTENSION *ext = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
752 GENERAL_NAMES *names = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
753 GENERAL_NAME *name;
65272
4755744724f2 Merged revisions 85432 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64404
diff changeset
754 const X509V3_EXT_METHOD *method;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
755 BIO *biobuf = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
756 char buf[2048];
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
757 char *vptr;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
758 int len;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
759 /* Issue #2973: ASN1_item_d2i() API changed in OpenSSL 0.9.6m */
59279
951c97e2878d Merged revisions 78598 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 54917
diff changeset
760 #if OPENSSL_VERSION_NUMBER >= 0x009060dfL
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
761 const unsigned char *p;
59279
951c97e2878d Merged revisions 78598 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 54917
diff changeset
762 #else
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
763 unsigned char *p;
59279
951c97e2878d Merged revisions 78598 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 54917
diff changeset
764 #endif
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
765
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
766 if (certificate == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
767 return peer_alt_names;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
768
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
769 /* get a memory buffer */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
770 biobuf = BIO_new(BIO_s_mem());
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
771
72567
65e7f40fefd4 Issue #13034: When decoding some SSL certificates, the subjectAltName extension could be unreported.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72115
diff changeset
772 i = -1;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
773 while ((i = X509_get_ext_by_NID(
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
774 certificate, NID_subject_alt_name, i)) >= 0) {
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
775
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
776 if (peer_alt_names == Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
777 peer_alt_names = PyList_New(0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
778 if (peer_alt_names == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
779 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
780 }
43948
8e0c4c710375 Make read() and certificate() return bytes instead of bytearray instances.
Guido van Rossum <guido@python.org>
parents: 43869
diff changeset
781
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
782 /* now decode the altName */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
783 ext = X509_get_ext(certificate, i);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
784 if(!(method = X509V3_EXT_get(ext))) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
785 PyErr_SetString
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
786 (PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
787 ERRSTR("No method for internalizing subjectAltName!"));
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
788 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
789 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
790
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
791 p = ext->value->data;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
792 if (method->it)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
793 names = (GENERAL_NAMES*)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
794 (ASN1_item_d2i(NULL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
795 &p,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
796 ext->value->length,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
797 ASN1_ITEM_ptr(method->it)));
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
798 else
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
799 names = (GENERAL_NAMES*)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
800 (method->d2i(NULL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
801 &p,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
802 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
803
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
804 for(j = 0; j < sk_GENERAL_NAME_num(names); j++) {
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
805
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
806 /* get a rendering of each name in the set of names */
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
807
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
808 name = sk_GENERAL_NAME_value(names, j);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
809 if (name->type == GEN_DIRNAME) {
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
810
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
811 /* we special-case DirName as a tuple of
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
812 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
813
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
814 t = PyTuple_New(2);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
815 if (t == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
816 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
817 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
818
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
819 v = PyUnicode_FromString("DirName");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
820 if (v == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
821 Py_DECREF(t);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
822 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
823 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
824 PyTuple_SET_ITEM(t, 0, v);
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
825
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
826 v = _create_tuple_for_X509_NAME (name->d.dirn);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
827 if (v == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
828 Py_DECREF(t);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
829 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
830 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
831 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
832
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
833 } else {
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
834
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
835 /* for everything else, we use the OpenSSL print form */
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
836
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
837 (void) BIO_reset(biobuf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
838 GENERAL_NAME_print(biobuf, name);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
839 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
840 if (len < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
841 _setSSLError(NULL, 0, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
842 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
843 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
844 vptr = strchr(buf, ':');
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
845 if (vptr == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
846 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
847 t = PyTuple_New(2);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
848 if (t == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
849 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
850 v = PyUnicode_FromStringAndSize(buf, (vptr - buf));
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
851 if (v == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
852 Py_DECREF(t);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
853 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
854 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
855 PyTuple_SET_ITEM(t, 0, v);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
856 v = PyUnicode_FromStringAndSize((vptr + 1),
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
857 (len - (vptr - buf + 1)));
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
858 if (v == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
859 Py_DECREF(t);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
860 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
861 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
862 PyTuple_SET_ITEM(t, 1, v);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
863 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
864
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
865 /* and add that rendering to the list */
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
866
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
867 if (PyList_Append(peer_alt_names, t) < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
868 Py_DECREF(t);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
869 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
870 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
871 Py_DECREF(t);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
872 }
73711
80d491aaeed2 Issue #13458: Fix a memory leak in the ssl module when decoding a certificate with a subjectAltName.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72567
diff changeset
873 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
874 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
875 BIO_free(biobuf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
876 if (peer_alt_names != Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
877 v = PyList_AsTuple(peer_alt_names);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
878 Py_DECREF(peer_alt_names);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
879 return v;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
880 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
881 return peer_alt_names;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
882 }
43948
8e0c4c710375 Make read() and certificate() return bytes instead of bytearray instances.
Guido van Rossum <guido@python.org>
parents: 43869
diff changeset
883
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
884
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
885 fail:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
886 if (biobuf != NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
887 BIO_free(biobuf);
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
888
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
889 if (peer_alt_names != Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
890 Py_XDECREF(peer_alt_names);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
891 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
892
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
893 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
894 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
895
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
896 static PyObject *
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
897 _decode_certificate(X509 *certificate) {
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
898
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
899 PyObject *retval = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
900 BIO *biobuf = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
901 PyObject *peer;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
902 PyObject *peer_alt_names = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
903 PyObject *issuer;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
904 PyObject *version;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
905 PyObject *sn_obj;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
906 ASN1_INTEGER *serialNumber;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
907 char buf[2048];
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
908 int len;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
909 ASN1_TIME *notBefore, *notAfter;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
910 PyObject *pnotBefore, *pnotAfter;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
911
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
912 retval = PyDict_New();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
913 if (retval == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
914 return NULL;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
915
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
916 peer = _create_tuple_for_X509_NAME(
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
917 X509_get_subject_name(certificate));
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
918 if (peer == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
919 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
920 if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
921 Py_DECREF(peer);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
922 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
923 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
924 Py_DECREF(peer);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
925
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
926 issuer = _create_tuple_for_X509_NAME(
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
927 X509_get_issuer_name(certificate));
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
928 if (issuer == NULL)
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
929 goto fail0;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
930 if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
931 Py_DECREF(issuer);
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
932 goto fail0;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
933 }
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
934 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
935
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
936 version = PyLong_FromLong(X509_get_version(certificate) + 1);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
937 if (PyDict_SetItemString(retval, "version", version) < 0) {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
938 Py_DECREF(version);
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
939 goto fail0;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
940 }
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
941 Py_DECREF(version);
43948
8e0c4c710375 Make read() and certificate() return bytes instead of bytearray instances.
Guido van Rossum <guido@python.org>
parents: 43869
diff changeset
942
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
943 /* get a memory buffer */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
944 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
945
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
946 (void) BIO_reset(biobuf);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
947 serialNumber = X509_get_serialNumber(certificate);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
948 /* should not exceed 20 octets, 160 bits, so buf is big enough */
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
949 i2a_ASN1_INTEGER(biobuf, serialNumber);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
950 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
951 if (len < 0) {
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
952 _setSSLError(NULL, 0, __FILE__, __LINE__);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
953 goto fail1;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
954 }
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
955 sn_obj = PyUnicode_FromStringAndSize(buf, len);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
956 if (sn_obj == NULL)
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
957 goto fail1;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
958 if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
959 Py_DECREF(sn_obj);
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
960 goto fail1;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
961 }
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
962 Py_DECREF(sn_obj);
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
963
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
964 (void) BIO_reset(biobuf);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
965 notBefore = X509_get_notBefore(certificate);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
966 ASN1_TIME_print(biobuf, notBefore);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
967 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
968 if (len < 0) {
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
969 _setSSLError(NULL, 0, __FILE__, __LINE__);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
970 goto fail1;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
971 }
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
972 pnotBefore = PyUnicode_FromStringAndSize(buf, len);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
973 if (pnotBefore == NULL)
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
974 goto fail1;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
975 if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
976 Py_DECREF(pnotBefore);
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
977 goto fail1;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
978 }
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
979 Py_DECREF(pnotBefore);
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
980
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
981 (void) BIO_reset(biobuf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
982 notAfter = X509_get_notAfter(certificate);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
983 ASN1_TIME_print(biobuf, notAfter);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
984 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
985 if (len < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
986 _setSSLError(NULL, 0, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
987 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
988 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
989 pnotAfter = PyUnicode_FromStringAndSize(buf, len);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
990 if (pnotAfter == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
991 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
992 if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
993 Py_DECREF(pnotAfter);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
994 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
995 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
996 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
997
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
998 /* Now look for subjectAltName */
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
999
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1000 peer_alt_names = _get_peer_alt_names(certificate);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1001 if (peer_alt_names == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1002 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1003 else if (peer_alt_names != Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1004 if (PyDict_SetItemString(retval, "subjectAltName",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1005 peer_alt_names) < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1006 Py_DECREF(peer_alt_names);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1007 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1008 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1009 Py_DECREF(peer_alt_names);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1010 }
43948
8e0c4c710375 Make read() and certificate() return bytes instead of bytearray instances.
Guido van Rossum <guido@python.org>
parents: 43869
diff changeset
1011
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1012 BIO_free(biobuf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1013 return retval;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
1014
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
1015 fail1:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1016 if (biobuf != NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1017 BIO_free(biobuf);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
1018 fail0:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1019 Py_XDECREF(retval);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1020 return NULL;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
1021 }
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1022
43400
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 static PyObject *
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1025 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
1026
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1027 PyObject *retval = NULL;
61639
08abbdc99be6 Issue #8477: _ssl._test_decode_cert() supports str with surrogates and bytes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61637
diff changeset
1028 PyObject *filename;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1029 X509 *x=NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1030 BIO *cert;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1031
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
1032 if (!PyArg_ParseTuple(args, "O&:test_decode_certificate",
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
1033 PyUnicode_FSConverter, &filename))
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1034 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1035
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1036 if ((cert=BIO_new(BIO_s_file())) == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1037 PyErr_SetString(PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1038 "Can't malloc memory to read file");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1039 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1040 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1041
61639
08abbdc99be6 Issue #8477: _ssl._test_decode_cert() supports str with surrogates and bytes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61637
diff changeset
1042 if (BIO_read_filename(cert, PyBytes_AsString(filename)) <= 0) {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1043 PyErr_SetString(PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1044 "Can't open file");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1045 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1046 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1047
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1048 x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1049 if (x == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1050 PyErr_SetString(PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1051 "Error decoding PEM-encoded file");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1052 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1053 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1054
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
1055 retval = _decode_certificate(x);
63694
c4bbda2d4c49 Merged revisions 83677 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62489
diff changeset
1056 X509_free(x);
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1057
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1058 fail0:
61639
08abbdc99be6 Issue #8477: _ssl._test_decode_cert() supports str with surrogates and bytes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61637
diff changeset
1059 Py_DECREF(filename);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1060 if (cert != NULL) BIO_free(cert);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1061 return retval;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1062 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1063
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1064
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1065 static PyObject *
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1066 PySSL_peercert(PySSLSocket *self, PyObject *args)
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1067 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1068 PyObject *retval = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1069 int len;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1070 int verification;
78605
b878df1d23b1 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77578 78604
diff changeset
1071 int binary_mode = 0;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1072
78605
b878df1d23b1 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77578 78604
diff changeset
1073 if (!PyArg_ParseTuple(args, "|p:peer_certificate", &binary_mode))
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1074 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1075
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1076 if (!self->peer_cert)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1077 Py_RETURN_NONE;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1078
78605
b878df1d23b1 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77578 78604
diff changeset
1079 if (binary_mode) {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1080 /* return cert in DER-encoded format */
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1081
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1082 unsigned char *bytes_buf = NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1083
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1084 bytes_buf = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1085 len = i2d_X509(self->peer_cert, &bytes_buf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1086 if (len < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1087 PySSL_SetError(self, len, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1088 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1089 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1090 /* this is actually an immutable bytes sequence */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1091 retval = PyBytes_FromStringAndSize
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1092 ((const char *) bytes_buf, len);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1093 OPENSSL_free(bytes_buf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1094 return retval;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1095
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1096 } else {
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1097 verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl));
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1098 if ((verification & SSL_VERIFY_PEER) == 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1099 return PyDict_New();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1100 else
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
1101 return _decode_certificate(self->peer_cert);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1102 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1103 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1104
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1105 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
1106 "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
1107 \n\
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1108 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
1109 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
1110 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
1111 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
1112 \n\
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1113 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
1114 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
1115 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
1116
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1117 static PyObject *PySSL_cipher (PySSLSocket *self) {
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1118
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1119 PyObject *retval, *v;
65272
4755744724f2 Merged revisions 85432 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64404
diff changeset
1120 const SSL_CIPHER *current;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1121 char *cipher_name;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1122 char *cipher_protocol;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1123
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1124 if (self->ssl == NULL)
66806
800563ac057e Merged revisions 87140 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65272
diff changeset
1125 Py_RETURN_NONE;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1126 current = SSL_get_current_cipher(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1127 if (current == NULL)
66806
800563ac057e Merged revisions 87140 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65272
diff changeset
1128 Py_RETURN_NONE;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1129
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1130 retval = PyTuple_New(3);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1131 if (retval == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1132 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1133
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1134 cipher_name = (char *) SSL_CIPHER_get_name(current);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1135 if (cipher_name == NULL) {
66806
800563ac057e Merged revisions 87140 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65272
diff changeset
1136 Py_INCREF(Py_None);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1137 PyTuple_SET_ITEM(retval, 0, Py_None);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1138 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1139 v = PyUnicode_FromString(cipher_name);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1140 if (v == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1141 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1142 PyTuple_SET_ITEM(retval, 0, v);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1143 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1144 cipher_protocol = SSL_CIPHER_get_version(current);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1145 if (cipher_protocol == NULL) {
66806
800563ac057e Merged revisions 87140 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65272
diff changeset
1146 Py_INCREF(Py_None);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1147 PyTuple_SET_ITEM(retval, 1, Py_None);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1148 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1149 v = PyUnicode_FromString(cipher_protocol);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1150 if (v == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1151 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1152 PyTuple_SET_ITEM(retval, 1, v);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1153 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1154 v = PyLong_FromLong(SSL_CIPHER_get_bits(current, NULL));
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1155 if (v == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1156 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1157 PyTuple_SET_ITEM(retval, 2, v);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1158 return retval;
43948
8e0c4c710375 Make read() and certificate() return bytes instead of bytearray instances.
Guido van Rossum <guido@python.org>
parents: 43869
diff changeset
1159
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1160 fail0:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1161 Py_DECREF(retval);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1162 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1163 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1164
75865
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1165 #ifdef OPENSSL_NPN_NEGOTIATED
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1166 static PyObject *PySSL_selected_npn_protocol(PySSLSocket *self) {
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1167 const unsigned char *out;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1168 unsigned int outlen;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1169
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1170 SSL_get0_next_proto_negotiated(self->ssl,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1171 &out, &outlen);
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1172
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1173 if (out == NULL)
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1174 Py_RETURN_NONE;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1175 return PyUnicode_FromStringAndSize((char *) out, outlen);
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1176 }
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1177 #endif
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1178
74091
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1179 static PyObject *PySSL_compression(PySSLSocket *self) {
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1180 #ifdef OPENSSL_NO_COMP
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1181 Py_RETURN_NONE;
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1182 #else
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1183 const COMP_METHOD *comp_method;
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1184 const char *short_name;
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1185
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1186 if (self->ssl == NULL)
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1187 Py_RETURN_NONE;
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1188 comp_method = SSL_get_current_compression(self->ssl);
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1189 if (comp_method == NULL || comp_method->type == NID_undef)
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1190 Py_RETURN_NONE;
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1191 short_name = OBJ_nid2sn(comp_method->type);
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1192 if (short_name == NULL)
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1193 Py_RETURN_NONE;
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1194 return PyUnicode_DecodeFSDefault(short_name);
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1195 #endif
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1196 }
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1197
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1198 static PySSLContext *PySSL_get_context(PySSLSocket *self, void *closure) {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1199 Py_INCREF(self->ctx);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1200 return self->ctx;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1201 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1202
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1203 static int PySSL_set_context(PySSLSocket *self, PyObject *value,
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1204 void *closure) {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1205
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1206 if (PyObject_TypeCheck(value, &PySSLContext_Type)) {
83021
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
1207 #if !HAVE_SNI
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
1208 PyErr_SetString(PyExc_NotImplementedError, "setting a socket's "
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
1209 "context is not supported by your OpenSSL library");
83023
ad9effdc0ac3 Further compiling fixes (issue #17581)
Antoine Pitrou <solipsis@pitrou.net>
parents: 83021
diff changeset
1210 return -1;
83021
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
1211 #else
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1212 Py_INCREF(value);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1213 Py_DECREF(self->ctx);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1214 self->ctx = (PySSLContext *) value;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1215 SSL_set_SSL_CTX(self->ssl, self->ctx->ctx);
83021
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
1216 #endif
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1217 } else {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1218 PyErr_SetString(PyExc_TypeError, "The value must be a SSLContext");
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1219 return -1;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1220 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1221
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1222 return 0;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1223 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1224
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1225 PyDoc_STRVAR(PySSL_set_context_doc,
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1226 "_setter_context(ctx)\n\
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1227 \
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1228 This changes the context associated with the SSLSocket. This is typically\n\
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1229 used from within a callback function set by the set_servername_callback\n\
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1230 on the SSLContext to change the certificate information associated with the\n\
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1231 SSLSocket before the cryptographic exchange handshake messages\n");
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1232
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1233
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1234
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1235 static void PySSL_dealloc(PySSLSocket *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
1236 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1237 if (self->peer_cert) /* Possible not to have one? */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1238 X509_free (self->peer_cert);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1239 if (self->ssl)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1240 SSL_free(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1241 Py_XDECREF(self->Socket);
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1242 Py_XDECREF(self->ctx);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1243 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
1244 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1245
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1246 /* 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
1247 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
1248 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
1249 */
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
1250
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1251 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
1252 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
1253 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1254 fd_set fds;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1255 struct timeval tv;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1256 int rc;
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1257
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1258 /* Nothing to do unless we're in timeout mode (not non-blocking) */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1259 if (s->sock_timeout < 0.0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1260 return SOCKET_IS_BLOCKING;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1261 else if (s->sock_timeout == 0.0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1262 return SOCKET_IS_NONBLOCKING;
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1263
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1264 /* Guard against closed socket */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1265 if (s->sock_fd < 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1266 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
1267
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1268 /* Prefer poll, if available, since you can poll() any fd
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1269 * which can't be done with select(). */
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1270 #ifdef HAVE_POLL
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1271 {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1272 struct pollfd pollfd;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1273 int timeout;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1274
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1275 pollfd.fd = s->sock_fd;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1276 pollfd.events = writing ? POLLOUT : POLLIN;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1277
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1278 /* s->sock_timeout is in seconds, timeout in ms */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1279 timeout = (int)(s->sock_timeout * 1000 + 0.5);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1280 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1281 rc = poll(&pollfd, 1, timeout);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1282 PySSL_END_ALLOW_THREADS
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1283
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1284 goto normal_return;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1285 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1286 #endif
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1287
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1288 /* Guard against socket too large for select*/
72115
852ca32eb18d Issue #12287: Fix a stack corruption in ossaudiodev module when the FD is
Charles-François Natali <neologix@free.fr>
parents: 71255
diff changeset
1289 if (!_PyIsSelectable_fd(s->sock_fd))
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1290 return SOCKET_TOO_LARGE_FOR_SELECT;
36452
7789cc557e1e Bug #876637, prevent stack corruption when socket descriptor
Neal Norwitz <nnorwitz@gmail.com>
parents: 36451
diff changeset
1291
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1292 /* Construct the arguments to select */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1293 tv.tv_sec = (int)s->sock_timeout;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1294 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1295 FD_ZERO(&fds);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1296 FD_SET(s->sock_fd, &fds);
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1297
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1298 /* See if the socket is ready */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1299 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1300 if (writing)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1301 rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1302 else
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1303 rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1304 PySSL_END_ALLOW_THREADS
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1305
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1306 #ifdef HAVE_POLL
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1307 normal_return:
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1308 #endif
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1309 /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1310 (when we are able to write or when there's something to read) */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1311 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
1312 }
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1313
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1314 static PyObject *PySSL_SSLwrite(PySSLSocket *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
1315 {
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
1316 Py_buffer buf;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1317 int len;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1318 int sockstate;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1319 int err;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1320 int nonblocking;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1321 PySocketSockObject *sock
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1322 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
44305
19f894ae864a update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents: 44206
diff changeset
1323
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1324 if (((PyObject*)sock) == Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1325 _setSSLError("Underlying socket connection gone",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1326 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1327 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1328 }
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1329 Py_INCREF(sock);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1330
62480
21e86dd328f1 Issue #8682: The ssl module now temporary increments the reference count of
Antoine Pitrou <solipsis@pitrou.net>
parents: 61782
diff changeset
1331 if (!PyArg_ParseTuple(args, "y*:write", &buf)) {
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1332 Py_DECREF(sock);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1333 return NULL;
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1334 }
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1335
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1336 /* just in case the blocking state of the socket has been changed */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1337 nonblocking = (sock->sock_timeout >= 0.0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1338 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1339 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1340
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1341 sockstate = check_socket_and_wait_for_timeout(sock, 1);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1342 if (sockstate == SOCKET_HAS_TIMED_OUT) {
66663
3d9a86b8cc98 Issue #10272: The ssl module now raises socket.timeout instead of a generic
Antoine Pitrou <solipsis@pitrou.net>
parents: 66217
diff changeset
1343 PyErr_SetString(PySocketModule.timeout_error,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1344 "The write operation timed out");
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1345 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1346 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1347 PyErr_SetString(PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1348 "Underlying socket has been closed.");
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1349 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1350 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1351 PyErr_SetString(PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1352 "Underlying socket too large for select().");
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1353 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1354 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1355 do {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1356 PySSL_BEGIN_ALLOW_THREADS
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
1357 len = SSL_write(self->ssl, buf.buf, buf.len);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1358 err = SSL_get_error(self->ssl, len);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1359 PySSL_END_ALLOW_THREADS
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
1360 if (PyErr_CheckSignals()) {
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1361 goto error;
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
1362 }
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1363 if (err == SSL_ERROR_WANT_READ) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
1364 sockstate = check_socket_and_wait_for_timeout(sock, 0);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1365 } else if (err == SSL_ERROR_WANT_WRITE) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
1366 sockstate = check_socket_and_wait_for_timeout(sock, 1);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1367 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1368 sockstate = SOCKET_OPERATION_OK;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1369 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1370 if (sockstate == SOCKET_HAS_TIMED_OUT) {
66663
3d9a86b8cc98 Issue #10272: The ssl module now raises socket.timeout instead of a generic
Antoine Pitrou <solipsis@pitrou.net>
parents: 66217
diff changeset
1371 PyErr_SetString(PySocketModule.timeout_error,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1372 "The write operation timed out");
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1373 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1374 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1375 PyErr_SetString(PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1376 "Underlying socket has been closed.");
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1377 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1378 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1379 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1380 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1381 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1382
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1383 Py_DECREF(sock);
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
1384 PyBuffer_Release(&buf);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1385 if (len > 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1386 return PyLong_FromLong(len);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1387 else
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1388 return PySSL_SetError(self, len, __FILE__, __LINE__);
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1389
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1390 error:
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1391 Py_DECREF(sock);
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
1392 PyBuffer_Release(&buf);
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1393 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
1394 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1395
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1396 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
1397 "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
1398 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1399 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
1400 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
1401
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1402 static PyObject *PySSL_SSLpending(PySSLSocket *self)
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1403 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1404 int count = 0;
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1405
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1406 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1407 count = SSL_pending(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1408 PySSL_END_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1409 if (count < 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1410 return PySSL_SetError(self, count, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1411 else
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1412 return PyLong_FromLong(count);
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1413 }
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1414
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1415 PyDoc_STRVAR(PySSL_SSLpending_doc,
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1416 "pending() -> count\n\
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1417 \n\
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1418 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
1419 pending on the connection.\n");
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1420
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1421 static PyObject *PySSL_SSLread(PySSLSocket *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
1422 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1423 PyObject *dest = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1424 Py_buffer buf;
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1425 char *mem;
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1426 int len, count;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1427 int buf_passed = 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1428 int sockstate;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1429 int err;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1430 int nonblocking;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1431 PySocketSockObject *sock
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1432 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
44305
19f894ae864a update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents: 44206
diff changeset
1433
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1434 if (((PyObject*)sock) == Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1435 _setSSLError("Underlying socket connection gone",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1436 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1437 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1438 }
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1439 Py_INCREF(sock);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1440
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1441 buf.obj = NULL;
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1442 buf.buf = NULL;
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1443 if (!PyArg_ParseTuple(args, "i|w*:read", &len, &buf))
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1444 goto error;
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1445
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1446 if ((buf.buf == NULL) && (buf.obj == NULL)) {
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1447 dest = PyBytes_FromStringAndSize(NULL, len);
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1448 if (dest == NULL)
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1449 goto error;
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1450 mem = PyBytes_AS_STRING(dest);
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1451 }
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1452 else {
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1453 buf_passed = 1;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1454 mem = buf.buf;
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1455 if (len <= 0 || len > buf.len) {
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1456 len = (int) buf.len;
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1457 if (buf.len != len) {
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1458 PyErr_SetString(PyExc_OverflowError,
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1459 "maximum length can't fit in a C 'int'");
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1460 goto error;
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1461 }
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1462 }
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1463 }
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1464
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1465 /* just in case the blocking state of the socket has been changed */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1466 nonblocking = (sock->sock_timeout >= 0.0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1467 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1468 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
1469
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1470 /* first check if there are bytes ready to be read */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1471 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1472 count = SSL_pending(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1473 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
1474
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1475 if (!count) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1476 sockstate = check_socket_and_wait_for_timeout(sock, 0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1477 if (sockstate == SOCKET_HAS_TIMED_OUT) {
66663
3d9a86b8cc98 Issue #10272: The ssl module now raises socket.timeout instead of a generic
Antoine Pitrou <solipsis@pitrou.net>
parents: 66217
diff changeset
1478 PyErr_SetString(PySocketModule.timeout_error,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1479 "The read operation timed out");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1480 goto error;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1481 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1482 PyErr_SetString(PySSLErrorObject,
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
1483 "Underlying socket too large for select().");
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1484 goto error;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1485 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1486 count = 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1487 goto done;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1488 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1489 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1490 do {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1491 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1492 count = SSL_read(self->ssl, mem, len);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1493 err = SSL_get_error(self->ssl, count);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1494 PySSL_END_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1495 if (PyErr_CheckSignals())
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1496 goto error;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1497 if (err == SSL_ERROR_WANT_READ) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
1498 sockstate = check_socket_and_wait_for_timeout(sock, 0);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1499 } else if (err == SSL_ERROR_WANT_WRITE) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
1500 sockstate = check_socket_and_wait_for_timeout(sock, 1);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1501 } else if ((err == SSL_ERROR_ZERO_RETURN) &&
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1502 (SSL_get_shutdown(self->ssl) ==
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1503 SSL_RECEIVED_SHUTDOWN))
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1504 {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1505 count = 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1506 goto done;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1507 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1508 sockstate = SOCKET_OPERATION_OK;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1509 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1510 if (sockstate == SOCKET_HAS_TIMED_OUT) {
66663
3d9a86b8cc98 Issue #10272: The ssl module now raises socket.timeout instead of a generic
Antoine Pitrou <solipsis@pitrou.net>
parents: 66217
diff changeset
1511 PyErr_SetString(PySocketModule.timeout_error,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1512 "The read operation timed out");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1513 goto error;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1514 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1515 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1516 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1517 } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1518 if (count <= 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1519 PySSL_SetError(self, count, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1520 goto error;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1521 }
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1522
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1523 done:
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1524 Py_DECREF(sock);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1525 if (!buf_passed) {
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1526 _PyBytes_Resize(&dest, count);
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1527 return dest;
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1528 }
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1529 else {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1530 PyBuffer_Release(&buf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1531 return PyLong_FromLong(count);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1532 }
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1533
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1534 error:
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1535 Py_DECREF(sock);
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1536 if (!buf_passed)
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1537 Py_XDECREF(dest);
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1538 else
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1539 PyBuffer_Release(&buf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1540 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
1541 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1542
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1543 PyDoc_STRVAR(PySSL_SSLread_doc,
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1544 "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
1545 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1546 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
1547
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1548 static PyObject *PySSL_SSLshutdown(PySSLSocket *self)
48681
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1549 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1550 int err, ssl_err, sockstate, nonblocking;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1551 int zeros = 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1552 PySocketSockObject *sock
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1553 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1554
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1555 /* Guard against closed socket */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1556 if ((((PyObject*)sock) == Py_None) || (sock->sock_fd < 0)) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1557 _setSSLError("Underlying socket connection gone",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1558 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1559 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1560 }
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1561 Py_INCREF(sock);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1562
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1563 /* Just in case the blocking state of the socket has been changed */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1564 nonblocking = (sock->sock_timeout >= 0.0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1565 BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1566 BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
48681
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1567
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1568 while (1) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1569 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1570 /* Disable read-ahead so that unwrap can work correctly.
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1571 * Otherwise OpenSSL might read in too much data,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1572 * eating clear text data that happens to be
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1573 * transmitted after the SSL shutdown.
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1574 * Should be safe to call repeatedly everytime this
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1575 * function is used and the shutdown_seen_zero != 0
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1576 * condition is met.
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1577 */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1578 if (self->shutdown_seen_zero)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1579 SSL_set_read_ahead(self->ssl, 0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1580 err = SSL_shutdown(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1581 PySSL_END_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1582 /* If err == 1, a secure shutdown with SSL_shutdown() is complete */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1583 if (err > 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1584 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1585 if (err == 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1586 /* Don't loop endlessly; instead preserve legacy
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1587 behaviour of trying SSL_shutdown() only twice.
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1588 This looks necessary for OpenSSL < 0.9.8m */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1589 if (++zeros > 1)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1590 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1591 /* Shutdown was sent, now try receiving */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1592 self->shutdown_seen_zero = 1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1593 continue;
48681
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1594 }
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1595
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1596 /* Possibly retry shutdown until timeout or failure */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1597 ssl_err = SSL_get_error(self->ssl, err);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1598 if (ssl_err == SSL_ERROR_WANT_READ)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1599 sockstate = check_socket_and_wait_for_timeout(sock, 0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1600 else if (ssl_err == SSL_ERROR_WANT_WRITE)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1601 sockstate = check_socket_and_wait_for_timeout(sock, 1);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1602 else
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1603 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1604 if (sockstate == SOCKET_HAS_TIMED_OUT) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1605 if (ssl_err == SSL_ERROR_WANT_READ)
66663
3d9a86b8cc98 Issue #10272: The ssl module now raises socket.timeout instead of a generic
Antoine Pitrou <solipsis@pitrou.net>
parents: 66217
diff changeset
1606 PyErr_SetString(PySocketModule.timeout_error,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1607 "The read operation timed out");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1608 else
66663
3d9a86b8cc98 Issue #10272: The ssl module now raises socket.timeout instead of a generic
Antoine Pitrou <solipsis@pitrou.net>
parents: 66217
diff changeset
1609 PyErr_SetString(PySocketModule.timeout_error,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1610 "The write operation timed out");
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1611 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1612 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1613 else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1614 PyErr_SetString(PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1615 "Underlying socket too large for select().");
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1616 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1617 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1618 else if (sockstate != SOCKET_OPERATION_OK)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1619 /* Retain the SSL error code */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1620 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1621 }
60901
fc97784e5cc2 Merged revisions 80394 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60818
diff changeset
1622
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1623 if (err < 0) {
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1624 Py_DECREF(sock);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1625 return PySSL_SetError(self, err, __FILE__, __LINE__);
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1626 }
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1627 else
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1628 /* It's already INCREF'ed */
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1629 return (PyObject *) sock;
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1630
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1631 error:
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1632 Py_DECREF(sock);
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1633 return NULL;
48681
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1634 }
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1635
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1636 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
1637 "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
1638 \n\
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1639 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
1640 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
1641
71440
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1642 #if HAVE_OPENSSL_FINISHED
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1643 static PyObject *
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1644 PySSL_tls_unique_cb(PySSLSocket *self)
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1645 {
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1646 PyObject *retval = NULL;
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1647 char buf[PySSL_CB_MAXLEN];
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1648 int len;
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1649
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1650 if (SSL_session_reused(self->ssl) ^ !self->socket_type) {
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1651 /* if session is resumed XOR we are the client */
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1652 len = SSL_get_finished(self->ssl, buf, PySSL_CB_MAXLEN);
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1653 }
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1654 else {
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1655 /* if a new session XOR we are the server */
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1656 len = SSL_get_peer_finished(self->ssl, buf, PySSL_CB_MAXLEN);
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1657 }
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1658
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1659 /* It cannot be negative in current OpenSSL version as of July 2011 */
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1660 assert(len >= 0);
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1661 if (len == 0)
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1662 Py_RETURN_NONE;
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1663
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1664 retval = PyBytes_FromStringAndSize(buf, len);
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1665
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1666 return retval;
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1667 }
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1668
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1669 PyDoc_STRVAR(PySSL_tls_unique_cb_doc,
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1670 "tls_unique_cb() -> bytes\n\
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1671 \n\
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1672 Returns the 'tls-unique' channel binding data, as defined by RFC 5929.\n\
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1673 \n\
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1674 If the TLS handshake is not yet complete, None is returned");
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1675
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1676 #endif /* HAVE_OPENSSL_FINISHED */
48681
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1677
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1678 static PyGetSetDef ssl_getsetlist[] = {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1679 {"context", (getter) PySSL_get_context,
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1680 (setter) PySSL_set_context, PySSL_set_context_doc},
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1681 {NULL}, /* sentinel */
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1682 };
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1683
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1684 static PyMethodDef PySSLMethods[] = {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1685 {"do_handshake", (PyCFunction)PySSL_SSLdo_handshake, METH_NOARGS},
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1686 {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1687 PySSL_SSLwrite_doc},
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1688 {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1689 PySSL_SSLread_doc},
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1690 {"pending", (PyCFunction)PySSL_SSLpending, METH_NOARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1691 PySSL_SSLpending_doc},
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1692 {"peer_certificate", (PyCFunction)PySSL_peercert, METH_VARARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1693 PySSL_peercert_doc},
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1694 {"cipher", (PyCFunction)PySSL_cipher, METH_NOARGS},
75865
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1695 #ifdef OPENSSL_NPN_NEGOTIATED
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1696 {"selected_npn_protocol", (PyCFunction)PySSL_selected_npn_protocol, METH_NOARGS},
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1697 #endif
74091
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1698 {"compression", (PyCFunction)PySSL_compression, METH_NOARGS},
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1699 {"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1700 PySSL_SSLshutdown_doc},
71440
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1701 #if HAVE_OPENSSL_FINISHED
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1702 {"tls_unique_cb", (PyCFunction)PySSL_tls_unique_cb, METH_NOARGS,
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1703 PySSL_tls_unique_cb_doc},
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1704 #endif
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1705 {NULL, 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
1706 };
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1707
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1708 static PyTypeObject PySSLSocket_Type = {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1709 PyVarObject_HEAD_INIT(NULL, 0)
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1710 "_ssl._SSLSocket", /*tp_name*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1711 sizeof(PySSLSocket), /*tp_basicsize*/
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1712 0, /*tp_itemsize*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1713 /* methods */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1714 (destructor)PySSL_dealloc, /*tp_dealloc*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1715 0, /*tp_print*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1716 0, /*tp_getattr*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1717 0, /*tp_setattr*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1718 0, /*tp_reserved*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1719 0, /*tp_repr*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1720 0, /*tp_as_number*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1721 0, /*tp_as_sequence*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1722 0, /*tp_as_mapping*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1723 0, /*tp_hash*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1724 0, /*tp_call*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1725 0, /*tp_str*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1726 0, /*tp_getattro*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1727 0, /*tp_setattro*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1728 0, /*tp_as_buffer*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1729 Py_TPFLAGS_DEFAULT, /*tp_flags*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1730 0, /*tp_doc*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1731 0, /*tp_traverse*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1732 0, /*tp_clear*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1733 0, /*tp_richcompare*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1734 0, /*tp_weaklistoffset*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1735 0, /*tp_iter*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1736 0, /*tp_iternext*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1737 PySSLMethods, /*tp_methods*/
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1738 0, /*tp_members*/
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1739 ssl_getsetlist, /*tp_getset*/
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1740 };
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1741
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1742
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1743 /*
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1744 * _SSLContext objects
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1745 */
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1746
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1747 static PyObject *
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1748 context_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1749 {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1750 char *kwlist[] = {"protocol", NULL};
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1751 PySSLContext *self;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1752 int proto_version = PY_SSL_VERSION_SSL23;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1753 SSL_CTX *ctx = NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1754
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1755 if (!PyArg_ParseTupleAndKeywords(
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1756 args, kwds, "i:_SSLContext", kwlist,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1757 &proto_version))
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1758 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1759
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1760 PySSL_BEGIN_ALLOW_THREADS
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1761 if (proto_version == PY_SSL_VERSION_TLS1)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1762 ctx = SSL_CTX_new(TLSv1_method());
82992
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
1763 #if HAVE_TLSv1_2
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
1764 else if (proto_version == PY_SSL_VERSION_TLS1_1)
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
1765 ctx = SSL_CTX_new(TLSv1_1_method());
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
1766 else if (proto_version == PY_SSL_VERSION_TLS1_2)
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
1767 ctx = SSL_CTX_new(TLSv1_2_method());
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
1768 #endif
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1769 else if (proto_version == PY_SSL_VERSION_SSL3)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1770 ctx = SSL_CTX_new(SSLv3_method());
69998
20beec22764f (Merge 3.1) Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69114 69997
diff changeset
1771 #ifndef OPENSSL_NO_SSL2
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1772 else if (proto_version == PY_SSL_VERSION_SSL2)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1773 ctx = SSL_CTX_new(SSLv2_method());
69998
20beec22764f (Merge 3.1) Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69114 69997
diff changeset
1774 #endif
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1775 else if (proto_version == PY_SSL_VERSION_SSL23)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1776 ctx = SSL_CTX_new(SSLv23_method());
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1777 else
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1778 proto_version = -1;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1779 PySSL_END_ALLOW_THREADS
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1780
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1781 if (proto_version == -1) {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1782 PyErr_SetString(PyExc_ValueError,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1783 "invalid protocol version");
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1784 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1785 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1786 if (ctx == NULL) {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1787 PyErr_SetString(PySSLErrorObject,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1788 "failed to allocate SSL context");
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1789 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1790 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1791
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1792 assert(type != NULL && type->tp_alloc != NULL);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1793 self = (PySSLContext *) type->tp_alloc(type, 0);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1794 if (self == NULL) {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1795 SSL_CTX_free(ctx);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1796 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1797 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1798 self->ctx = ctx;
79114
4cf53684e14e Issue #15977: Fix memory leak in Modules/_ssl.c when the function _set_npn_protocols() is called multiple times
Christian Heimes <christian@cheimes.de>
parents: 78605
diff changeset
1799 #ifdef OPENSSL_NPN_NEGOTIATED
4cf53684e14e Issue #15977: Fix memory leak in Modules/_ssl.c when the function _set_npn_protocols() is called multiple times
Christian Heimes <christian@cheimes.de>
parents: 78605
diff changeset
1800 self->npn_protocols = NULL;
4cf53684e14e Issue #15977: Fix memory leak in Modules/_ssl.c when the function _set_npn_protocols() is called multiple times
Christian Heimes <christian@cheimes.de>
parents: 78605
diff changeset
1801 #endif
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1802 #ifndef OPENSSL_NO_TLSEXT
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1803 self->set_hostname = NULL;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1804 #endif
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1805 /* Defaults */
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1806 SSL_CTX_set_verify(self->ctx, SSL_VERIFY_NONE, NULL);
74648
4386686a035d Issue #13885: CVE-2011-3389: the _ssl module would always disable the CBC IV attack countermeasure.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73711 74647
diff changeset
1807 SSL_CTX_set_options(self->ctx,
4386686a035d Issue #13885: CVE-2011-3389: the _ssl module would always disable the CBC IV attack countermeasure.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73711 74647
diff changeset
1808 SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1809
65255
c48c09e693e6 Define a "session_id_context" at context creation. This is recommended
Antoine Pitrou <solipsis@pitrou.net>
parents: 65246
diff changeset
1810 #define SID_CTX "Python"
c48c09e693e6 Define a "session_id_context" at context creation. This is recommended
Antoine Pitrou <solipsis@pitrou.net>
parents: 65246
diff changeset
1811 SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX,
c48c09e693e6 Define a "session_id_context" at context creation. This is recommended
Antoine Pitrou <solipsis@pitrou.net>
parents: 65246
diff changeset
1812 sizeof(SID_CTX));
c48c09e693e6 Define a "session_id_context" at context creation. This is recommended
Antoine Pitrou <solipsis@pitrou.net>
parents: 65246
diff changeset
1813 #undef SID_CTX
c48c09e693e6 Define a "session_id_context" at context creation. This is recommended
Antoine Pitrou <solipsis@pitrou.net>
parents: 65246
diff changeset
1814
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1815 return (PyObject *)self;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1816 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1817
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1818 static int
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1819 context_traverse(PySSLContext *self, visitproc visit, void *arg)
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1820 {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1821 #ifndef OPENSSL_NO_TLSEXT
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1822 Py_VISIT(self->set_hostname);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1823 #endif
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1824 return 0;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1825 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1826
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1827 static int
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1828 context_clear(PySSLContext *self)
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1829 {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1830 #ifndef OPENSSL_NO_TLSEXT
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1831 Py_CLEAR(self->set_hostname);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1832 #endif
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1833 return 0;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1834 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1835
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1836 static void
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1837 context_dealloc(PySSLContext *self)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1838 {
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
1839 context_clear(self);
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1840 SSL_CTX_free(self->ctx);
75865
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1841 #ifdef OPENSSL_NPN_NEGOTIATED
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1842 PyMem_Free(self->npn_protocols);
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1843 #endif
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1844 Py_TYPE(self)->tp_free(self);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1845 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1846
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1847 static PyObject *
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1848 set_ciphers(PySSLContext *self, PyObject *args)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1849 {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1850 int ret;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1851 const char *cipherlist;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1852
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1853 if (!PyArg_ParseTuple(args, "s:set_ciphers", &cipherlist))
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1854 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1855 ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1856 if (ret == 0) {
61637
2ca4626d9c93 Fix (hopefully) the remaining test_ssl buildbot failures
Antoine Pitrou <solipsis@pitrou.net>
parents: 61635
diff changeset
1857 /* Clearing the error queue is necessary on some OpenSSL versions,
2ca4626d9c93 Fix (hopefully) the remaining test_ssl buildbot failures
Antoine Pitrou <solipsis@pitrou.net>
parents: 61635
diff changeset
1858 otherwise the error will be reported again when another SSL call
2ca4626d9c93 Fix (hopefully) the remaining test_ssl buildbot failures
Antoine Pitrou <solipsis@pitrou.net>
parents: 61635
diff changeset
1859 is done. */
2ca4626d9c93 Fix (hopefully) the remaining test_ssl buildbot failures
Antoine Pitrou <solipsis@pitrou.net>
parents: 61635
diff changeset
1860 ERR_clear_error();
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1861 PyErr_SetString(PySSLErrorObject,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1862 "No cipher can be selected.");
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1863 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1864 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1865 Py_RETURN_NONE;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1866 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1867
75865
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1868 #ifdef OPENSSL_NPN_NEGOTIATED
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1869 /* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1870 static int
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1871 _advertiseNPN_cb(SSL *s,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1872 const unsigned char **data, unsigned int *len,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1873 void *args)
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1874 {
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1875 PySSLContext *ssl_ctx = (PySSLContext *) args;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1876
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1877 if (ssl_ctx->npn_protocols == NULL) {
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1878 *data = (unsigned char *) "";
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1879 *len = 0;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1880 } else {
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1881 *data = (unsigned char *) ssl_ctx->npn_protocols;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1882 *len = ssl_ctx->npn_protocols_len;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1883 }
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1884
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1885 return SSL_TLSEXT_ERR_OK;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1886 }
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1887 /* this callback gets passed to SSL_CTX_set_next_proto_select_cb */
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1888 static int
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1889 _selectNPN_cb(SSL *s,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1890 unsigned char **out, unsigned char *outlen,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1891 const unsigned char *server, unsigned int server_len,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1892 void *args)
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1893 {
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1894 PySSLContext *ssl_ctx = (PySSLContext *) args;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1895
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1896 unsigned char *client = (unsigned char *) ssl_ctx->npn_protocols;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1897 int client_len;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1898
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1899 if (client == NULL) {
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1900 client = (unsigned char *) "";
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1901 client_len = 0;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1902 } else {
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1903 client_len = ssl_ctx->npn_protocols_len;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1904 }
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1905
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1906 SSL_select_next_proto(out, outlen,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1907 server, server_len,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1908 client, client_len);
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1909
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1910 return SSL_TLSEXT_ERR_OK;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1911 }
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1912 #endif
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1913
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1914 static PyObject *
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1915 _set_npn_protocols(PySSLContext *self, PyObject *args)
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1916 {
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1917 #ifdef OPENSSL_NPN_NEGOTIATED
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1918 Py_buffer protos;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1919
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1920 if (!PyArg_ParseTuple(args, "y*:set_npn_protocols", &protos))
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1921 return NULL;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1922
79114
4cf53684e14e Issue #15977: Fix memory leak in Modules/_ssl.c when the function _set_npn_protocols() is called multiple times
Christian Heimes <christian@cheimes.de>
parents: 78605
diff changeset
1923 if (self->npn_protocols != NULL) {
4cf53684e14e Issue #15977: Fix memory leak in Modules/_ssl.c when the function _set_npn_protocols() is called multiple times
Christian Heimes <christian@cheimes.de>
parents: 78605
diff changeset
1924 PyMem_Free(self->npn_protocols);
4cf53684e14e Issue #15977: Fix memory leak in Modules/_ssl.c when the function _set_npn_protocols() is called multiple times
Christian Heimes <christian@cheimes.de>
parents: 78605
diff changeset
1925 }
4cf53684e14e Issue #15977: Fix memory leak in Modules/_ssl.c when the function _set_npn_protocols() is called multiple times
Christian Heimes <christian@cheimes.de>
parents: 78605
diff changeset
1926
75865
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1927 self->npn_protocols = PyMem_Malloc(protos.len);
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1928 if (self->npn_protocols == NULL) {
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1929 PyBuffer_Release(&protos);
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1930 return PyErr_NoMemory();
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1931 }
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1932 memcpy(self->npn_protocols, protos.buf, protos.len);
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1933 self->npn_protocols_len = (int) protos.len;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1934
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1935 /* set both server and client callbacks, because the context can
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1936 * be used to create both types of sockets */
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1937 SSL_CTX_set_next_protos_advertised_cb(self->ctx,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1938 _advertiseNPN_cb,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1939 self);
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1940 SSL_CTX_set_next_proto_select_cb(self->ctx,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1941 _selectNPN_cb,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1942 self);
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1943
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1944 PyBuffer_Release(&protos);
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1945 Py_RETURN_NONE;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1946 #else
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1947 PyErr_SetString(PyExc_NotImplementedError,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1948 "The NPN extension requires OpenSSL 1.0.1 or later.");
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1949 return NULL;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1950 #endif
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1951 }
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
1952
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1953 static PyObject *
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1954 get_verify_mode(PySSLContext *self, void *c)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1955 {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1956 switch (SSL_CTX_get_verify_mode(self->ctx)) {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1957 case SSL_VERIFY_NONE:
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1958 return PyLong_FromLong(PY_SSL_CERT_NONE);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1959 case SSL_VERIFY_PEER:
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1960 return PyLong_FromLong(PY_SSL_CERT_OPTIONAL);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1961 case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT:
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1962 return PyLong_FromLong(PY_SSL_CERT_REQUIRED);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1963 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1964 PyErr_SetString(PySSLErrorObject,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1965 "invalid return value from SSL_CTX_get_verify_mode");
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1966 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1967 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1968
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1969 static int
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1970 set_verify_mode(PySSLContext *self, PyObject *arg, void *c)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1971 {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1972 int n, mode;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1973 if (!PyArg_Parse(arg, "i", &n))
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1974 return -1;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1975 if (n == PY_SSL_CERT_NONE)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1976 mode = SSL_VERIFY_NONE;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1977 else if (n == PY_SSL_CERT_OPTIONAL)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1978 mode = SSL_VERIFY_PEER;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1979 else if (n == PY_SSL_CERT_REQUIRED)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1980 mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1981 else {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1982 PyErr_SetString(PyExc_ValueError,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1983 "invalid value for verify_mode");
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1984 return -1;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1985 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1986 SSL_CTX_set_verify(self->ctx, mode, NULL);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1987 return 0;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1988 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1989
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1990 static PyObject *
61782
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1991 get_options(PySSLContext *self, void *c)
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1992 {
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1993 return PyLong_FromLong(SSL_CTX_get_options(self->ctx));
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1994 }
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1995
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1996 static int
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1997 set_options(PySSLContext *self, PyObject *arg, void *c)
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1998 {
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1999 long new_opts, opts, set, clear;
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2000 if (!PyArg_Parse(arg, "l", &new_opts))
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2001 return -1;
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2002 opts = SSL_CTX_get_options(self->ctx);
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2003 clear = opts & ~new_opts;
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2004 set = ~opts & new_opts;
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2005 if (clear) {
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2006 #ifdef HAVE_SSL_CTX_CLEAR_OPTIONS
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2007 SSL_CTX_clear_options(self->ctx, clear);
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2008 #else
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2009 PyErr_SetString(PyExc_ValueError,
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2010 "can't clear options before OpenSSL 0.9.8m");
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2011 return -1;
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2012 #endif
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2013 }
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2014 if (set)
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2015 SSL_CTX_set_options(self->ctx, set);
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2016 return 0;
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2017 }
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2018
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2019 typedef struct {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2020 PyThreadState *thread_state;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2021 PyObject *callable;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2022 char *password;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2023 Py_ssize_t size;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2024 int error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2025 } _PySSLPasswordInfo;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2026
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2027 static int
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2028 _pwinfo_set(_PySSLPasswordInfo *pw_info, PyObject* password,
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2029 const char *bad_type_error)
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2030 {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2031 /* Set the password and size fields of a _PySSLPasswordInfo struct
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2032 from a unicode, bytes, or byte array object.
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2033 The password field will be dynamically allocated and must be freed
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2034 by the caller */
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2035 PyObject *password_bytes = NULL;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2036 const char *data = NULL;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2037 Py_ssize_t size;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2038
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2039 if (PyUnicode_Check(password)) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2040 password_bytes = PyUnicode_AsEncodedString(password, NULL, NULL);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2041 if (!password_bytes) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2042 goto error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2043 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2044 data = PyBytes_AS_STRING(password_bytes);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2045 size = PyBytes_GET_SIZE(password_bytes);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2046 } else if (PyBytes_Check(password)) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2047 data = PyBytes_AS_STRING(password);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2048 size = PyBytes_GET_SIZE(password);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2049 } else if (PyByteArray_Check(password)) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2050 data = PyByteArray_AS_STRING(password);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2051 size = PyByteArray_GET_SIZE(password);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2052 } else {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2053 PyErr_SetString(PyExc_TypeError, bad_type_error);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2054 goto error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2055 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2056
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2057 free(pw_info->password);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2058 pw_info->password = malloc(size);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2059 if (!pw_info->password) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2060 PyErr_SetString(PyExc_MemoryError,
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2061 "unable to allocate password buffer");
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2062 goto error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2063 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2064 memcpy(pw_info->password, data, size);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2065 pw_info->size = size;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2066
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2067 Py_XDECREF(password_bytes);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2068 return 1;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2069
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2070 error:
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2071 Py_XDECREF(password_bytes);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2072 return 0;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2073 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2074
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2075 static int
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2076 _password_callback(char *buf, int size, int rwflag, void *userdata)
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2077 {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2078 _PySSLPasswordInfo *pw_info = (_PySSLPasswordInfo*) userdata;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2079 PyObject *fn_ret = NULL;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2080
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2081 PySSL_END_ALLOW_THREADS_S(pw_info->thread_state);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2082
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2083 if (pw_info->callable) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2084 fn_ret = PyObject_CallFunctionObjArgs(pw_info->callable, NULL);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2085 if (!fn_ret) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2086 /* TODO: It would be nice to move _ctypes_add_traceback() into the
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2087 core python API, so we could use it to add a frame here */
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2088 goto error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2089 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2090
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2091 if (!_pwinfo_set(pw_info, fn_ret,
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2092 "password callback must return a string")) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2093 goto error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2094 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2095 Py_CLEAR(fn_ret);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2096 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2097
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2098 if (pw_info->size > size) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2099 PyErr_Format(PyExc_ValueError,
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2100 "password cannot be longer than %d bytes", size);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2101 goto error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2102 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2103
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2104 PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2105 memcpy(buf, pw_info->password, pw_info->size);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2106 return pw_info->size;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2107
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2108 error:
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2109 Py_XDECREF(fn_ret);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2110 PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2111 pw_info->error = 1;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2112 return -1;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2113 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2114
61782
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2115 static PyObject *
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2116 load_cert_chain(PySSLContext *self, PyObject *args, PyObject *kwds)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2117 {
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2118 char *kwlist[] = {"certfile", "keyfile", "password", NULL};
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2119 PyObject *certfile, *keyfile = NULL, *password = NULL;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2120 PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL;
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2121 pem_password_cb *orig_passwd_cb = self->ctx->default_passwd_callback;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2122 void *orig_passwd_userdata = self->ctx->default_passwd_callback_userdata;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2123 _PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 };
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2124 int r;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2125
64306
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2126 errno = 0;
64358
063b554d3153 Try to fix some buildbot failures on test_ssl
Antoine Pitrou <solipsis@pitrou.net>
parents: 64352
diff changeset
2127 ERR_clear_error();
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2128 if (!PyArg_ParseTupleAndKeywords(args, kwds,
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2129 "O|OO:load_cert_chain", kwlist,
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2130 &certfile, &keyfile, &password))
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2131 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2132 if (keyfile == Py_None)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2133 keyfile = NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2134 if (!PyUnicode_FSConverter(certfile, &certfile_bytes)) {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2135 PyErr_SetString(PyExc_TypeError,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2136 "certfile should be a valid filesystem path");
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2137 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2138 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2139 if (keyfile && !PyUnicode_FSConverter(keyfile, &keyfile_bytes)) {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2140 PyErr_SetString(PyExc_TypeError,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2141 "keyfile should be a valid filesystem path");
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2142 goto error;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2143 }
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2144 if (password && password != Py_None) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2145 if (PyCallable_Check(password)) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2146 pw_info.callable = password;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2147 } else if (!_pwinfo_set(&pw_info, password,
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2148 "password should be a string or callable")) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2149 goto error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2150 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2151 SSL_CTX_set_default_passwd_cb(self->ctx, _password_callback);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2152 SSL_CTX_set_default_passwd_cb_userdata(self->ctx, &pw_info);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2153 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2154 PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state);
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2155 r = SSL_CTX_use_certificate_chain_file(self->ctx,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2156 PyBytes_AS_STRING(certfile_bytes));
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2157 PySSL_END_ALLOW_THREADS_S(pw_info.thread_state);
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2158 if (r != 1) {
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2159 if (pw_info.error) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2160 ERR_clear_error();
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2161 /* the password callback has already set the error information */
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2162 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2163 else if (errno != 0) {
64352
2dc17dcc9734 Issue #9693 - msg 115273: attempt to fix ssl module failures on certain OpenSSL versions by calling ERR_clear_error() before raising IOError
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64306
diff changeset
2164 ERR_clear_error();
64306
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2165 PyErr_SetFromErrno(PyExc_IOError);
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2166 }
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2167 else {
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2168 _setSSLError(NULL, 0, __FILE__, __LINE__);
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2169 }
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2170 goto error;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2171 }
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2172 PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state);
69114
88ed3de28520 Issue #11746: Fix SSLContext.load_cert_chain() to accept elliptic curve private keys.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
2173 r = SSL_CTX_use_PrivateKey_file(self->ctx,
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2174 PyBytes_AS_STRING(keyfile ? keyfile_bytes : certfile_bytes),
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2175 SSL_FILETYPE_PEM);
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2176 PySSL_END_ALLOW_THREADS_S(pw_info.thread_state);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2177 Py_CLEAR(keyfile_bytes);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2178 Py_CLEAR(certfile_bytes);
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2179 if (r != 1) {
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2180 if (pw_info.error) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2181 ERR_clear_error();
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2182 /* the password callback has already set the error information */
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2183 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2184 else if (errno != 0) {
64352
2dc17dcc9734 Issue #9693 - msg 115273: attempt to fix ssl module failures on certain OpenSSL versions by calling ERR_clear_error() before raising IOError
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64306
diff changeset
2185 ERR_clear_error();
64306
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2186 PyErr_SetFromErrno(PyExc_IOError);
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2187 }
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2188 else {
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2189 _setSSLError(NULL, 0, __FILE__, __LINE__);
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2190 }
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2191 goto error;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2192 }
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2193 PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state);
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2194 r = SSL_CTX_check_private_key(self->ctx);
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2195 PySSL_END_ALLOW_THREADS_S(pw_info.thread_state);
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2196 if (r != 1) {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2197 _setSSLError(NULL, 0, __FILE__, __LINE__);
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2198 goto error;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2199 }
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2200 SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2201 SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2202 free(pw_info.password);
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2203 Py_RETURN_NONE;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2204
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2205 error:
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2206 SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2207 SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata);
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2208 free(pw_info.password);
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2209 Py_XDECREF(keyfile_bytes);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2210 Py_XDECREF(certfile_bytes);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2211 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2212 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2213
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2214 static PyObject *
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2215 load_verify_locations(PySSLContext *self, PyObject *args, PyObject *kwds)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2216 {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2217 char *kwlist[] = {"cafile", "capath", NULL};
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2218 PyObject *cafile = NULL, *capath = NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2219 PyObject *cafile_bytes = NULL, *capath_bytes = NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2220 const char *cafile_buf = NULL, *capath_buf = NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2221 int r;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2222
64306
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2223 errno = 0;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2224 if (!PyArg_ParseTupleAndKeywords(args, kwds,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2225 "|OO:load_verify_locations", kwlist,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2226 &cafile, &capath))
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2227 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2228 if (cafile == Py_None)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2229 cafile = NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2230 if (capath == Py_None)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2231 capath = NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2232 if (cafile == NULL && capath == NULL) {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2233 PyErr_SetString(PyExc_TypeError,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2234 "cafile and capath cannot be both omitted");
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2235 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2236 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2237 if (cafile && !PyUnicode_FSConverter(cafile, &cafile_bytes)) {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2238 PyErr_SetString(PyExc_TypeError,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2239 "cafile should be a valid filesystem path");
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2240 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2241 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2242 if (capath && !PyUnicode_FSConverter(capath, &capath_bytes)) {
67792
852186e5ae2a Issue #10989: Fix a crash on SSLContext.load_verify_locations(None, True).
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66802
diff changeset
2243 Py_XDECREF(cafile_bytes);
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2244 PyErr_SetString(PyExc_TypeError,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2245 "capath should be a valid filesystem path");
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2246 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2247 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2248 if (cafile)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2249 cafile_buf = PyBytes_AS_STRING(cafile_bytes);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2250 if (capath)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2251 capath_buf = PyBytes_AS_STRING(capath_bytes);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2252 PySSL_BEGIN_ALLOW_THREADS
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2253 r = SSL_CTX_load_verify_locations(self->ctx, cafile_buf, capath_buf);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2254 PySSL_END_ALLOW_THREADS
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2255 Py_XDECREF(cafile_bytes);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2256 Py_XDECREF(capath_bytes);
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2257 if (r != 1) {
64306
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2258 if (errno != 0) {
64352
2dc17dcc9734 Issue #9693 - msg 115273: attempt to fix ssl module failures on certain OpenSSL versions by calling ERR_clear_error() before raising IOError
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64306
diff changeset
2259 ERR_clear_error();
64306
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2260 PyErr_SetFromErrno(PyExc_IOError);
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2261 }
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2262 else {
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2263 _setSSLError(NULL, 0, __FILE__, __LINE__);
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2264 }
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2265 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2266 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2267 Py_RETURN_NONE;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2268 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2269
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2270 static PyObject *
74129
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2271 load_dh_params(PySSLContext *self, PyObject *filepath)
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2272 {
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2273 FILE *f;
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2274 DH *dh;
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2275
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2276 f = _Py_fopen(filepath, "rb");
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2277 if (f == NULL) {
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2278 if (!PyErr_Occurred())
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2279 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath);
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2280 return NULL;
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2281 }
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2282 errno = 0;
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2283 PySSL_BEGIN_ALLOW_THREADS
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2284 dh = PEM_read_DHparams(f, NULL, NULL, NULL);
81463
523d5b1e3fd5 SSLContext.load_dh_params() now properly closes the input file.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2285 fclose(f);
74129
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2286 PySSL_END_ALLOW_THREADS
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2287 if (dh == NULL) {
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2288 if (errno != 0) {
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2289 ERR_clear_error();
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2290 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath);
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2291 }
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2292 else {
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2293 _setSSLError(NULL, 0, __FILE__, __LINE__);
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2294 }
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2295 return NULL;
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2296 }
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2297 if (SSL_CTX_set_tmp_dh(self->ctx, dh) == 0)
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2298 _setSSLError(NULL, 0, __FILE__, __LINE__);
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2299 DH_free(dh);
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2300 Py_RETURN_NONE;
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2301 }
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2302
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2303 static PyObject *
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2304 context_wrap_socket(PySSLContext *self, PyObject *args, PyObject *kwds)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2305 {
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2306 char *kwlist[] = {"sock", "server_side", "server_hostname", NULL};
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2307 PySocketSockObject *sock;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2308 int server_side = 0;
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2309 char *hostname = NULL;
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2310 PyObject *hostname_obj, *res;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2311
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2312 /* server_hostname is either None (or absent), or to be encoded
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2313 using the idna encoding. */
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2314 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!i|O!:_wrap_socket", kwlist,
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2315 PySocketModule.Sock_Type,
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2316 &sock, &server_side,
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2317 Py_TYPE(Py_None), &hostname_obj)) {
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2318 PyErr_Clear();
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2319 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!iet:_wrap_socket", kwlist,
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2320 PySocketModule.Sock_Type,
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2321 &sock, &server_side,
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2322 "idna", &hostname))
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2323 return NULL;
83021
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
2324 #if !HAVE_SNI
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2325 PyMem_Free(hostname);
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2326 PyErr_SetString(PyExc_ValueError, "server_hostname is not supported "
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2327 "by your OpenSSL library");
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2328 return NULL;
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2329 #endif
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2330 }
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2331
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2332 res = (PyObject *) newPySSLSocket(self, sock, server_side,
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2333 hostname);
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2334 if (hostname != NULL)
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2335 PyMem_Free(hostname);
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2336 return res;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2337 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2338
65223
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2339 static PyObject *
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2340 session_stats(PySSLContext *self, PyObject *unused)
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2341 {
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2342 int r;
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2343 PyObject *value, *stats = PyDict_New();
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2344 if (!stats)
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2345 return NULL;
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2346
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2347 #define ADD_STATS(SSL_NAME, KEY_NAME) \
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2348 value = PyLong_FromLong(SSL_CTX_sess_ ## SSL_NAME (self->ctx)); \
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2349 if (value == NULL) \
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2350 goto error; \
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2351 r = PyDict_SetItemString(stats, KEY_NAME, value); \
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2352 Py_DECREF(value); \
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2353 if (r < 0) \
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2354 goto error;
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2355
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2356 ADD_STATS(number, "number");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2357 ADD_STATS(connect, "connect");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2358 ADD_STATS(connect_good, "connect_good");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2359 ADD_STATS(connect_renegotiate, "connect_renegotiate");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2360 ADD_STATS(accept, "accept");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2361 ADD_STATS(accept_good, "accept_good");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2362 ADD_STATS(accept_renegotiate, "accept_renegotiate");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2363 ADD_STATS(accept, "accept");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2364 ADD_STATS(hits, "hits");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2365 ADD_STATS(misses, "misses");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2366 ADD_STATS(timeouts, "timeouts");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2367 ADD_STATS(cache_full, "cache_full");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2368
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2369 #undef ADD_STATS
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2370
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2371 return stats;
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2372
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2373 error:
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2374 Py_DECREF(stats);
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2375 return NULL;
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2376 }
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2377
66217
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2378 static PyObject *
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2379 set_default_verify_paths(PySSLContext *self, PyObject *unused)
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2380 {
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2381 if (!SSL_CTX_set_default_verify_paths(self->ctx)) {
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2382 _setSSLError(NULL, 0, __FILE__, __LINE__);
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2383 return NULL;
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2384 }
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2385 Py_RETURN_NONE;
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2386 }
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2387
74103
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
2388 #ifndef OPENSSL_NO_ECDH
74084
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2389 static PyObject *
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2390 set_ecdh_curve(PySSLContext *self, PyObject *name)
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2391 {
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2392 PyObject *name_bytes;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2393 int nid;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2394 EC_KEY *key;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2395
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2396 if (!PyUnicode_FSConverter(name, &name_bytes))
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2397 return NULL;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2398 assert(PyBytes_Check(name_bytes));
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2399 nid = OBJ_sn2nid(PyBytes_AS_STRING(name_bytes));
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2400 Py_DECREF(name_bytes);
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2401 if (nid == 0) {
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2402 PyErr_Format(PyExc_ValueError,
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2403 "unknown elliptic curve name %R", name);
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2404 return NULL;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2405 }
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2406 key = EC_KEY_new_by_curve_name(nid);
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2407 if (key == NULL) {
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2408 _setSSLError(NULL, 0, __FILE__, __LINE__);
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2409 return NULL;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2410 }
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2411 SSL_CTX_set_tmp_ecdh(self->ctx, key);
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2412 EC_KEY_free(key);
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2413 Py_RETURN_NONE;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2414 }
74103
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
2415 #endif
74084
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2416
83021
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
2417 #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT)
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2418 static int
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2419 _servername_callback(SSL *s, int *al, void *args)
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2420 {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2421 int ret;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2422 PySSLContext *ssl_ctx = (PySSLContext *) args;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2423 PySSLSocket *ssl;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2424 PyObject *servername_o;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2425 PyObject *servername_idna;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2426 PyObject *result;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2427 /* The high-level ssl.SSLSocket object */
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2428 PyObject *ssl_socket;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2429 const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
81559
8630fa732cf6 Issue #16982: Fix --without-threads build failure.
Stefan Krah <skrah@bytereef.org>
parents: 81464
diff changeset
2430 #ifdef WITH_THREAD
8630fa732cf6 Issue #16982: Fix --without-threads build failure.
Stefan Krah <skrah@bytereef.org>
parents: 81464
diff changeset
2431 PyGILState_STATE gstate = PyGILState_Ensure();
8630fa732cf6 Issue #16982: Fix --without-threads build failure.
Stefan Krah <skrah@bytereef.org>
parents: 81464
diff changeset
2432 #endif
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2433
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2434 if (ssl_ctx->set_hostname == NULL) {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2435 /* remove race condition in this the call back while if removing the
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2436 * callback is in progress */
81559
8630fa732cf6 Issue #16982: Fix --without-threads build failure.
Stefan Krah <skrah@bytereef.org>
parents: 81464
diff changeset
2437 #ifdef WITH_THREAD
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2438 PyGILState_Release(gstate);
81559
8630fa732cf6 Issue #16982: Fix --without-threads build failure.
Stefan Krah <skrah@bytereef.org>
parents: 81464
diff changeset
2439 #endif
81299
52b4d9bfc9ea Fix returning uninitialized variable (issue #8109).
Antoine Pitrou <solipsis@pitrou.net>
parents: 81298
diff changeset
2440 return SSL_TLSEXT_ERR_OK;
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2441 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2442
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2443 ssl = SSL_get_app_data(s);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2444 assert(PySSLSocket_Check(ssl));
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2445 ssl_socket = PyWeakref_GetObject(ssl->Socket);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2446 Py_INCREF(ssl_socket);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2447 if (ssl_socket == Py_None) {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2448 goto error;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2449 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2450
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2451 servername_o = PyBytes_FromString(servername);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2452 if (servername_o == NULL) {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2453 PyErr_WriteUnraisable((PyObject *) ssl_ctx);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2454 goto error;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2455 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2456 servername_idna = PyUnicode_FromEncodedObject(servername_o, "idna", NULL);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2457 if (servername_idna == NULL) {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2458 PyErr_WriteUnraisable(servername_o);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2459 Py_DECREF(servername_o);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2460 goto error;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2461 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2462 Py_DECREF(servername_o);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2463 result = PyObject_CallFunctionObjArgs(ssl_ctx->set_hostname, ssl_socket,
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2464 servername_idna, ssl_ctx, NULL);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2465 Py_DECREF(ssl_socket);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2466 Py_DECREF(servername_idna);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2467
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2468 if (result == NULL) {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2469 PyErr_WriteUnraisable(ssl_ctx->set_hostname);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2470 *al = SSL_AD_HANDSHAKE_FAILURE;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2471 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2472 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2473 else {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2474 if (result != Py_None) {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2475 *al = (int) PyLong_AsLong(result);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2476 if (PyErr_Occurred()) {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2477 PyErr_WriteUnraisable(result);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2478 *al = SSL_AD_INTERNAL_ERROR;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2479 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2480 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2481 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2482 else {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2483 ret = SSL_TLSEXT_ERR_OK;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2484 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2485 Py_DECREF(result);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2486 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2487
81559
8630fa732cf6 Issue #16982: Fix --without-threads build failure.
Stefan Krah <skrah@bytereef.org>
parents: 81464
diff changeset
2488 #ifdef WITH_THREAD
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2489 PyGILState_Release(gstate);
81559
8630fa732cf6 Issue #16982: Fix --without-threads build failure.
Stefan Krah <skrah@bytereef.org>
parents: 81464
diff changeset
2490 #endif
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2491 return ret;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2492
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2493 error:
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2494 Py_DECREF(ssl_socket);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2495 *al = SSL_AD_INTERNAL_ERROR;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2496 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
81559
8630fa732cf6 Issue #16982: Fix --without-threads build failure.
Stefan Krah <skrah@bytereef.org>
parents: 81464
diff changeset
2497 #ifdef WITH_THREAD
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2498 PyGILState_Release(gstate);
81559
8630fa732cf6 Issue #16982: Fix --without-threads build failure.
Stefan Krah <skrah@bytereef.org>
parents: 81464
diff changeset
2499 #endif
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2500 return ret;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2501 }
83024
fc4f68b51ba0 Fix previous fix (the cause was actually a misplaced #endif, or so it seems)
Antoine Pitrou <solipsis@pitrou.net>
parents: 83023
diff changeset
2502 #endif
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2503
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2504 PyDoc_STRVAR(PySSL_set_servername_callback_doc,
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2505 "set_servername_callback(method)\n\
83025
b45630913715 Improve set_servername_callback docstring.
Antoine Pitrou <solipsis@pitrou.net>
parents: 83024
diff changeset
2506 \n\
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2507 This sets a callback that will be called when a server name is provided by\n\
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2508 the SSL/TLS client in the SNI extension.\n\
83025
b45630913715 Improve set_servername_callback docstring.
Antoine Pitrou <solipsis@pitrou.net>
parents: 83024
diff changeset
2509 \n\
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2510 If the argument is None then the callback is disabled. The method is called\n\
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2511 with the SSLSocket, the server name as a string, and the SSLContext object.\n\
83025
b45630913715 Improve set_servername_callback docstring.
Antoine Pitrou <solipsis@pitrou.net>
parents: 83024
diff changeset
2512 See RFC 6066 for details of the SNI extension.");
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2513
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2514 static PyObject *
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2515 set_servername_callback(PySSLContext *self, PyObject *args)
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2516 {
83021
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
2517 #if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT)
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2518 PyObject *cb;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2519
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2520 if (!PyArg_ParseTuple(args, "O", &cb))
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2521 return NULL;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2522
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2523 Py_CLEAR(self->set_hostname);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2524 if (cb == Py_None) {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2525 SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2526 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2527 else {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2528 if (!PyCallable_Check(cb)) {
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2529 SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2530 PyErr_SetString(PyExc_TypeError,
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2531 "not a callable object");
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2532 return NULL;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2533 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2534 Py_INCREF(cb);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2535 self->set_hostname = cb;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2536 SSL_CTX_set_tlsext_servername_callback(self->ctx, _servername_callback);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2537 SSL_CTX_set_tlsext_servername_arg(self->ctx, self);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2538 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2539 Py_RETURN_NONE;
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2540 #else
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2541 PyErr_SetString(PyExc_NotImplementedError,
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2542 "The TLS extension servername callback, "
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2543 "SSL_CTX_set_tlsext_servername_callback, "
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2544 "is not in the current OpenSSL library.");
83023
ad9effdc0ac3 Further compiling fixes (issue #17581)
Antoine Pitrou <solipsis@pitrou.net>
parents: 83021
diff changeset
2545 return NULL;
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2546 #endif
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2547 }
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2548
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2549 static PyGetSetDef context_getsetlist[] = {
61782
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2550 {"options", (getter) get_options,
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2551 (setter) set_options, NULL},
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2552 {"verify_mode", (getter) get_verify_mode,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2553 (setter) set_verify_mode, NULL},
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2554 {NULL}, /* sentinel */
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2555 };
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2556
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2557 static struct PyMethodDef context_methods[] = {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2558 {"_wrap_socket", (PyCFunction) context_wrap_socket,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2559 METH_VARARGS | METH_KEYWORDS, NULL},
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2560 {"set_ciphers", (PyCFunction) set_ciphers,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2561 METH_VARARGS, NULL},
75865
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
2562 {"_set_npn_protocols", (PyCFunction) _set_npn_protocols,
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
2563 METH_VARARGS, NULL},
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2564 {"load_cert_chain", (PyCFunction) load_cert_chain,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2565 METH_VARARGS | METH_KEYWORDS, NULL},
74129
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2566 {"load_dh_params", (PyCFunction) load_dh_params,
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2567 METH_O, NULL},
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2568 {"load_verify_locations", (PyCFunction) load_verify_locations,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2569 METH_VARARGS | METH_KEYWORDS, NULL},
65223
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2570 {"session_stats", (PyCFunction) session_stats,
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2571 METH_NOARGS, NULL},
66217
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2572 {"set_default_verify_paths", (PyCFunction) set_default_verify_paths,
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2573 METH_NOARGS, NULL},
74103
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
2574 #ifndef OPENSSL_NO_ECDH
74084
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2575 {"set_ecdh_curve", (PyCFunction) set_ecdh_curve,
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2576 METH_O, NULL},
74103
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
2577 #endif
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2578 {"set_servername_callback", (PyCFunction) set_servername_callback,
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2579 METH_VARARGS, PySSL_set_servername_callback_doc},
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2580 {NULL, NULL} /* sentinel */
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2581 };
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2582
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2583 static PyTypeObject PySSLContext_Type = {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2584 PyVarObject_HEAD_INIT(NULL, 0)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2585 "_ssl._SSLContext", /*tp_name*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2586 sizeof(PySSLContext), /*tp_basicsize*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2587 0, /*tp_itemsize*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2588 (destructor)context_dealloc, /*tp_dealloc*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2589 0, /*tp_print*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2590 0, /*tp_getattr*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2591 0, /*tp_setattr*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2592 0, /*tp_reserved*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2593 0, /*tp_repr*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2594 0, /*tp_as_number*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2595 0, /*tp_as_sequence*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2596 0, /*tp_as_mapping*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2597 0, /*tp_hash*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2598 0, /*tp_call*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2599 0, /*tp_str*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2600 0, /*tp_getattro*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2601 0, /*tp_setattro*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2602 0, /*tp_as_buffer*/
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2603 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2604 0, /*tp_doc*/
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2605 (traverseproc) context_traverse, /*tp_traverse*/
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2606 (inquiry) context_clear, /*tp_clear*/
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2607 0, /*tp_richcompare*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2608 0, /*tp_weaklistoffset*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2609 0, /*tp_iter*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2610 0, /*tp_iternext*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2611 context_methods, /*tp_methods*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2612 0, /*tp_members*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2613 context_getsetlist, /*tp_getset*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2614 0, /*tp_base*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2615 0, /*tp_dict*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2616 0, /*tp_descr_get*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2617 0, /*tp_descr_set*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2618 0, /*tp_dictoffset*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2619 0, /*tp_init*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2620 0, /*tp_alloc*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2621 context_new, /*tp_new*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2622 };
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2623
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2624
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2625
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2626 #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
2627
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2628 /* 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
2629 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
2630 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
2631 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2632 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
2633 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
2634 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
2635
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2636 if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy))
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
2637 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
2638 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
2639 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
2640 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
2641 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2642
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
2643 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
2644 "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
2645 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2646 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
2647 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
2648
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2649 static PyObject *
70328
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2650 PySSL_RAND(int len, int pseudo)
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2651 {
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2652 int ok;
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2653 PyObject *bytes;
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2654 unsigned long err;
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2655 const char *errstr;
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2656 PyObject *v;
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2657
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2658 bytes = PyBytes_FromStringAndSize(NULL, len);
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2659 if (bytes == NULL)
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2660 return NULL;
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2661 if (pseudo) {
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2662 ok = RAND_pseudo_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len);
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2663 if (ok == 0 || ok == 1)
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2664 return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False);
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2665 }
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2666 else {
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2667 ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len);
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2668 if (ok == 1)
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2669 return bytes;
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2670 }
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2671 Py_DECREF(bytes);
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2672
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2673 err = ERR_get_error();
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2674 errstr = ERR_reason_error_string(err);
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2675 v = Py_BuildValue("(ks)", err, errstr);
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2676 if (v != NULL) {
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2677 PyErr_SetObject(PySSLErrorObject, v);
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2678 Py_DECREF(v);
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2679 }
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2680 return NULL;
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2681 }
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2682
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2683 static PyObject *
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2684 PySSL_RAND_bytes(PyObject *self, PyObject *args)
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2685 {
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2686 int len;
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2687 if (!PyArg_ParseTuple(args, "i:RAND_bytes", &len))
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2688 return NULL;
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2689 return PySSL_RAND(len, 0);
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2690 }
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2691
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2692 PyDoc_STRVAR(PySSL_RAND_bytes_doc,
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2693 "RAND_bytes(n) -> bytes\n\
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2694 \n\
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2695 Generate n cryptographically strong pseudo-random bytes.");
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2696
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2697 static PyObject *
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2698 PySSL_RAND_pseudo_bytes(PyObject *self, PyObject *args)
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2699 {
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2700 int len;
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2701 if (!PyArg_ParseTuple(args, "i:RAND_pseudo_bytes", &len))
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2702 return NULL;
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2703 return PySSL_RAND(len, 1);
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2704 }
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2705
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2706 PyDoc_STRVAR(PySSL_RAND_pseudo_bytes_doc,
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2707 "RAND_pseudo_bytes(n) -> (bytes, is_cryptographic)\n\
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2708 \n\
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2709 Generate n pseudo-random bytes. is_cryptographic is True if the bytes\
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2710 generated are cryptographically strong.");
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2711
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2712 static PyObject *
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2713 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
2714 {
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
2715 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
2716 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2717
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
2718 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
2719 "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
2720 \n\
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
2721 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
2722 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
2723 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
2724
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2725 static PyObject *
61641
a09eb3d59ff7 Issue #8477: ssl.RAND_egd() supports str with surrogates and bytes for the path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61639
diff changeset
2726 PySSL_RAND_egd(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
2727 {
61641
a09eb3d59ff7 Issue #8477: ssl.RAND_egd() supports str with surrogates and bytes for the path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61639
diff changeset
2728 PyObject *path;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2729 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
2730
78983
827bb0554f1f Closes #15793: Stack corruption in ssl.RAND_egd()
Jesus Cea <jcea@jcea.es>
parents: 78604
diff changeset
2731 if (!PyArg_ParseTuple(args, "O&:RAND_egd",
61641
a09eb3d59ff7 Issue #8477: ssl.RAND_egd() supports str with surrogates and bytes for the path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61639
diff changeset
2732 PyUnicode_FSConverter, &path))
a09eb3d59ff7 Issue #8477: ssl.RAND_egd() supports str with surrogates and bytes for the path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61639
diff changeset
2733 return NULL;
a09eb3d59ff7 Issue #8477: ssl.RAND_egd() supports str with surrogates and bytes for the path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61639
diff changeset
2734
a09eb3d59ff7 Issue #8477: ssl.RAND_egd() supports str with surrogates and bytes for the path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61639
diff changeset
2735 bytes = RAND_egd(PyBytes_AsString(path));
a09eb3d59ff7 Issue #8477: ssl.RAND_egd() supports str with surrogates and bytes for the path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61639
diff changeset
2736 Py_DECREF(path);
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2737 if (bytes == -1) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
2738 PyErr_SetString(PySSLErrorObject,
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
2739 "EGD connection failed or EGD did not return "
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
2740 "enough data to seed the PRNG");
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
2741 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
2742 }
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
2743 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
2744 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2745
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
2746 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
2747 "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
2748 \n\
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2749 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
2750 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
2751 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
2752
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2753 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2754
48681
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
2755
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
2756
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2757 /* 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
2758
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2759 static PyMethodDef PySSL_methods[] = {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2760 {"_test_decode_cert", PySSL_test_decode_certificate,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2761 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
2762 #ifdef HAVE_OPENSSL_RAND
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2763 {"RAND_add", PySSL_RAND_add, METH_VARARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2764 PySSL_RAND_add_doc},
70328
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2765 {"RAND_bytes", PySSL_RAND_bytes, METH_VARARGS,
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2766 PySSL_RAND_bytes_doc},
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2767 {"RAND_pseudo_bytes", PySSL_RAND_pseudo_bytes, METH_VARARGS,
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2768 PySSL_RAND_pseudo_bytes_doc},
61641
a09eb3d59ff7 Issue #8477: ssl.RAND_egd() supports str with surrogates and bytes for the path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61639
diff changeset
2769 {"RAND_egd", PySSL_RAND_egd, METH_VARARGS,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2770 PySSL_RAND_egd_doc},
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2771 {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2772 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
2773 #endif
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2774 {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
2775 };
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2776
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2777
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2778 #ifdef WITH_THREAD
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2779
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2780 /* 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
2781 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
2782
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2783 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
2784
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2785 static unsigned long _ssl_thread_id_function (void) {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2786 return PyThread_get_thread_ident();
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2787 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2788
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
2789 static void _ssl_thread_locking_function
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2790 (int mode, int n, const char *file, int line) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2791 /* this function is needed to perform locking on shared data
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2792 structures. (Note that OpenSSL uses a number of global data
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2793 structures that will be implicitly shared whenever multiple
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2794 threads use OpenSSL.) Multi-threaded applications will
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2795 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
2796
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2797 locking_function() must be able to handle up to
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2798 CRYPTO_num_locks() different mutex locks. It sets the n-th
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2799 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
2800
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2801 file and line are the file number of the function setting the
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2802 lock. They can be useful for debugging.
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2803 */
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2804
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2805 if ((_ssl_locks == NULL) ||
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2806 (n < 0) || ((unsigned)n >= _ssl_locks_count))
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2807 return;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2808
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2809 if (mode & CRYPTO_LOCK) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2810 PyThread_acquire_lock(_ssl_locks[n], 1);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2811 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2812 PyThread_release_lock(_ssl_locks[n]);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2813 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2814 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2815
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2816 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
2817
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2818 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
2819
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2820 if (_ssl_locks == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2821 _ssl_locks_count = CRYPTO_num_locks();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2822 _ssl_locks = (PyThread_type_lock *)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2823 malloc(sizeof(PyThread_type_lock) * _ssl_locks_count);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2824 if (_ssl_locks == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2825 return 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2826 memset(_ssl_locks, 0,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2827 sizeof(PyThread_type_lock) * _ssl_locks_count);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2828 for (i = 0; i < _ssl_locks_count; i++) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2829 _ssl_locks[i] = PyThread_allocate_lock();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2830 if (_ssl_locks[i] == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2831 unsigned int j;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2832 for (j = 0; j < i; j++) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2833 PyThread_free_lock(_ssl_locks[j]);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2834 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2835 free(_ssl_locks);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2836 return 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2837 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2838 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2839 CRYPTO_set_locking_callback(_ssl_thread_locking_function);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2840 CRYPTO_set_id_callback(_ssl_thread_id_function);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2841 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2842 return 1;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2843 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2844
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2845 #endif /* def HAVE_THREAD */
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2846
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
2847 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
2848 "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
2849 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
2850
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
2851
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
2852 static struct PyModuleDef _sslmodule = {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2853 PyModuleDef_HEAD_INIT,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2854 "_ssl",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2855 module_doc,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2856 -1,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2857 PySSL_methods,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2858 NULL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2859 NULL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2860 NULL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2861 NULL
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
2862 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
2863
71255
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2864
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2865 static void
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2866 parse_openssl_version(unsigned long libver,
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2867 unsigned int *major, unsigned int *minor,
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2868 unsigned int *fix, unsigned int *patch,
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2869 unsigned int *status)
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2870 {
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2871 *status = libver & 0xF;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2872 libver >>= 4;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2873 *patch = libver & 0xFF;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2874 libver >>= 8;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2875 *fix = libver & 0xFF;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2876 libver >>= 8;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2877 *minor = libver & 0xFF;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2878 libver >>= 8;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2879 *major = libver & 0xFF;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2880 }
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2881
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24398
diff changeset
2882 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
2883 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
2884 {
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
2885 PyObject *m, *d, *r;
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
2886 unsigned long libver;
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
2887 unsigned int major, minor, fix, patch, status;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2888 PySocketModule_APIObject *socket_api;
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
2889 struct py_ssl_error_code *errcode;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
2890 struct py_ssl_library_code *libcode;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2891
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2892 if (PyType_Ready(&PySSLContext_Type) < 0)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2893 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2894 if (PyType_Ready(&PySSLSocket_Type) < 0)
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2895 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
2896
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2897 m = PyModule_Create(&_sslmodule);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2898 if (m == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2899 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2900 d = PyModule_GetDict(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
2901
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2902 /* Load _socket module and its C API */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2903 socket_api = PySocketModule_ImportModuleAndAPI();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2904 if (!socket_api)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2905 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2906 PySocketModule = *socket_api;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2907
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2908 /* Init OpenSSL */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2909 SSL_load_error_strings();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2910 SSL_library_init();
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2911 #ifdef WITH_THREAD
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2912 /* note that this will start threading if not already started */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2913 if (!_setup_ssl_threads()) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2914 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2915 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2916 #endif
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2917 OpenSSL_add_all_algorithms();
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2918
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2919 /* Add symbols to module dict */
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
2920 sslerror_type_slots[0].pfunc = PyExc_OSError;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
2921 PySSLErrorObject = PyType_FromSpec(&sslerror_type_spec);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2922 if (PySSLErrorObject == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2923 return NULL;
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
2924
73150
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2925 PySSLZeroReturnErrorObject = PyErr_NewExceptionWithDoc(
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2926 "ssl.SSLZeroReturnError", SSLZeroReturnError_doc,
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2927 PySSLErrorObject, NULL);
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2928 PySSLWantReadErrorObject = PyErr_NewExceptionWithDoc(
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2929 "ssl.SSLWantReadError", SSLWantReadError_doc,
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2930 PySSLErrorObject, NULL);
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2931 PySSLWantWriteErrorObject = PyErr_NewExceptionWithDoc(
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2932 "ssl.SSLWantWriteError", SSLWantWriteError_doc,
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2933 PySSLErrorObject, NULL);
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2934 PySSLSyscallErrorObject = PyErr_NewExceptionWithDoc(
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2935 "ssl.SSLSyscallError", SSLSyscallError_doc,
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2936 PySSLErrorObject, NULL);
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2937 PySSLEOFErrorObject = PyErr_NewExceptionWithDoc(
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2938 "ssl.SSLEOFError", SSLEOFError_doc,
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2939 PySSLErrorObject, NULL);
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2940 if (PySSLZeroReturnErrorObject == NULL
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2941 || PySSLWantReadErrorObject == NULL
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2942 || PySSLWantWriteErrorObject == NULL
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2943 || PySSLSyscallErrorObject == NULL
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2944 || PySSLEOFErrorObject == NULL)
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2945 return NULL;
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2946 if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2947 || PyDict_SetItemString(d, "SSLZeroReturnError", PySSLZeroReturnErrorObject) != 0
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2948 || PyDict_SetItemString(d, "SSLWantReadError", PySSLWantReadErrorObject) != 0
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2949 || PyDict_SetItemString(d, "SSLWantWriteError", PySSLWantWriteErrorObject) != 0
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2950 || PyDict_SetItemString(d, "SSLSyscallError", PySSLSyscallErrorObject) != 0
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2951 || PyDict_SetItemString(d, "SSLEOFError", PySSLEOFErrorObject) != 0)
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2952 return NULL;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2953 if (PyDict_SetItemString(d, "_SSLContext",
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2954 (PyObject *)&PySSLContext_Type) != 0)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2955 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2956 if (PyDict_SetItemString(d, "_SSLSocket",
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2957 (PyObject *)&PySSLSocket_Type) != 0)
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2958 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2959 PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2960 PY_SSL_ERROR_ZERO_RETURN);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2961 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2962 PY_SSL_ERROR_WANT_READ);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2963 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2964 PY_SSL_ERROR_WANT_WRITE);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2965 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2966 PY_SSL_ERROR_WANT_X509_LOOKUP);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2967 PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2968 PY_SSL_ERROR_SYSCALL);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2969 PyModule_AddIntConstant(m, "SSL_ERROR_SSL",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2970 PY_SSL_ERROR_SSL);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2971 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2972 PY_SSL_ERROR_WANT_CONNECT);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2973 /* non ssl.h errorcodes */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2974 PyModule_AddIntConstant(m, "SSL_ERROR_EOF",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2975 PY_SSL_ERROR_EOF);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2976 PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2977 PY_SSL_ERROR_INVALID_ERROR_CODE);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2978 /* cert requirements */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2979 PyModule_AddIntConstant(m, "CERT_NONE",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2980 PY_SSL_CERT_NONE);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2981 PyModule_AddIntConstant(m, "CERT_OPTIONAL",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2982 PY_SSL_CERT_OPTIONAL);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2983 PyModule_AddIntConstant(m, "CERT_REQUIRED",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2984 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
2985
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2986 /* Alert Descriptions from ssl.h */
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2987 /* note RESERVED constants no longer intended for use have been removed */
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2988 /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6 */
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2989
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2990 #define ADD_AD_CONSTANT(s) \
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2991 PyModule_AddIntConstant(m, "ALERT_DESCRIPTION_"#s, \
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2992 SSL_AD_##s)
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2993
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2994 ADD_AD_CONSTANT(CLOSE_NOTIFY);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2995 ADD_AD_CONSTANT(UNEXPECTED_MESSAGE);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2996 ADD_AD_CONSTANT(BAD_RECORD_MAC);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2997 ADD_AD_CONSTANT(RECORD_OVERFLOW);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2998 ADD_AD_CONSTANT(DECOMPRESSION_FAILURE);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
2999 ADD_AD_CONSTANT(HANDSHAKE_FAILURE);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3000 ADD_AD_CONSTANT(BAD_CERTIFICATE);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3001 ADD_AD_CONSTANT(UNSUPPORTED_CERTIFICATE);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3002 ADD_AD_CONSTANT(CERTIFICATE_REVOKED);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3003 ADD_AD_CONSTANT(CERTIFICATE_EXPIRED);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3004 ADD_AD_CONSTANT(CERTIFICATE_UNKNOWN);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3005 ADD_AD_CONSTANT(ILLEGAL_PARAMETER);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3006 ADD_AD_CONSTANT(UNKNOWN_CA);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3007 ADD_AD_CONSTANT(ACCESS_DENIED);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3008 ADD_AD_CONSTANT(DECODE_ERROR);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3009 ADD_AD_CONSTANT(DECRYPT_ERROR);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3010 ADD_AD_CONSTANT(PROTOCOL_VERSION);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3011 ADD_AD_CONSTANT(INSUFFICIENT_SECURITY);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3012 ADD_AD_CONSTANT(INTERNAL_ERROR);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3013 ADD_AD_CONSTANT(USER_CANCELLED);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3014 ADD_AD_CONSTANT(NO_RENEGOTIATION);
83021
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
3015 /* Not all constants are in old OpenSSL versions */
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
3016 #ifdef SSL_AD_UNSUPPORTED_EXTENSION
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3017 ADD_AD_CONSTANT(UNSUPPORTED_EXTENSION);
83021
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
3018 #endif
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
3019 #ifdef SSL_AD_CERTIFICATE_UNOBTAINABLE
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3020 ADD_AD_CONSTANT(CERTIFICATE_UNOBTAINABLE);
83021
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
3021 #endif
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
3022 #ifdef SSL_AD_UNRECOGNIZED_NAME
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3023 ADD_AD_CONSTANT(UNRECOGNIZED_NAME);
83021
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
3024 #endif
81298
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3025 #ifdef SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3026 ADD_AD_CONSTANT(BAD_CERTIFICATE_STATUS_RESPONSE);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3027 #endif
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3028 #ifdef SSL_AD_BAD_CERTIFICATE_HASH_VALUE
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3029 ADD_AD_CONSTANT(BAD_CERTIFICATE_HASH_VALUE);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3030 #endif
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3031 #ifdef SSL_AD_UNKNOWN_PSK_IDENTITY
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3032 ADD_AD_CONSTANT(UNKNOWN_PSK_IDENTITY);
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3033 #endif
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3034
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3035 #undef ADD_AD_CONSTANT
927afb7bca2a Issue #8109: The ssl module now has support for server-side SNI, thanks to a :meth:`SSLContext.set_servername_callback` method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 79136
diff changeset
3036
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
3037 /* protocol versions */
69997
b7abf0590e1c Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66806
diff changeset
3038 #ifndef OPENSSL_NO_SSL2
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
3039 PyModule_AddIntConstant(m, "PROTOCOL_SSLv2",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
3040 PY_SSL_VERSION_SSL2);
69997
b7abf0590e1c Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66806
diff changeset
3041 #endif
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
3042 PyModule_AddIntConstant(m, "PROTOCOL_SSLv3",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
3043 PY_SSL_VERSION_SSL3);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
3044 PyModule_AddIntConstant(m, "PROTOCOL_SSLv23",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
3045 PY_SSL_VERSION_SSL23);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
3046 PyModule_AddIntConstant(m, "PROTOCOL_TLSv1",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
3047 PY_SSL_VERSION_TLS1);
82992
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
3048 #if HAVE_TLSv1_2
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
3049 PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_1",
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
3050 PY_SSL_VERSION_TLS1_1);
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
3051 PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_2",
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
3052 PY_SSL_VERSION_TLS1_2);
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
3053 #endif
60373
9e5e9d8fa847 Merged revisions 79812 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60034
diff changeset
3054
61782
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
3055 /* protocol options */
74648
4386686a035d Issue #13885: CVE-2011-3389: the _ssl module would always disable the CBC IV attack countermeasure.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73711 74647
diff changeset
3056 PyModule_AddIntConstant(m, "OP_ALL",
4386686a035d Issue #13885: CVE-2011-3389: the _ssl module would always disable the CBC IV attack countermeasure.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73711 74647
diff changeset
3057 SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
61782
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
3058 PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2);
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
3059 PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3);
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
3060 PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1);
82992
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
3061 #if HAVE_TLSv1_2
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
3062 PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1);
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
3063 PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2);
02a89bd646ca Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial patch by Michele Orrù.
Antoine Pitrou <solipsis@pitrou.net>
parents: 81559
diff changeset
3064 #endif
74075
c706f76c9ea8 Issue #13635: Add ssl.OP_CIPHER_SERVER_PREFERENCE, so that SSL servers
Antoine Pitrou <solipsis@pitrou.net>
parents: 73712
diff changeset
3065 PyModule_AddIntConstant(m, "OP_CIPHER_SERVER_PREFERENCE",
c706f76c9ea8 Issue #13635: Add ssl.OP_CIPHER_SERVER_PREFERENCE, so that SSL servers
Antoine Pitrou <solipsis@pitrou.net>
parents: 73712
diff changeset
3066 SSL_OP_CIPHER_SERVER_PREFERENCE);
74129
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
3067 PyModule_AddIntConstant(m, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE);
74995
c1a07c8092f7 Fix compilation when SSL_OP_SINGLE_ECDH_USE isn't defined
Antoine Pitrou <solipsis@pitrou.net>
parents: 74964
diff changeset
3068 #ifdef SSL_OP_SINGLE_ECDH_USE
74084
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
3069 PyModule_AddIntConstant(m, "OP_SINGLE_ECDH_USE", SSL_OP_SINGLE_ECDH_USE);
74995
c1a07c8092f7 Fix compilation when SSL_OP_SINGLE_ECDH_USE isn't defined
Antoine Pitrou <solipsis@pitrou.net>
parents: 74964
diff changeset
3070 #endif
74091
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
3071 #ifdef SSL_OP_NO_COMPRESSION
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
3072 PyModule_AddIntConstant(m, "OP_NO_COMPRESSION",
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
3073 SSL_OP_NO_COMPRESSION);
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
3074 #endif
61782
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
3075
83021
02f9335b7efb Issue #17581: try to fix building on old OpenSSL versions
Antoine Pitrou <solipsis@pitrou.net>
parents: 82992
diff changeset
3076 #if HAVE_SNI
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3077 r = Py_True;
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3078 #else
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3079 r = Py_False;
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3080 #endif
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3081 Py_INCREF(r);
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3082 PyModule_AddObject(m, "HAS_SNI", r);
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3083
71440
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
3084 #if HAVE_OPENSSL_FINISHED
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
3085 r = Py_True;
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
3086 #else
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
3087 r = Py_False;
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
3088 #endif
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
3089 Py_INCREF(r);
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
3090 PyModule_AddObject(m, "HAS_TLS_UNIQUE", r);
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
3091
74103
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
3092 #ifdef OPENSSL_NO_ECDH
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
3093 r = Py_False;
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
3094 #else
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
3095 r = Py_True;
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
3096 #endif
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
3097 Py_INCREF(r);
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
3098 PyModule_AddObject(m, "HAS_ECDH", r);
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
3099
75865
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
3100 #ifdef OPENSSL_NPN_NEGOTIATED
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
3101 r = Py_True;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
3102 #else
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
3103 r = Py_False;
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
3104 #endif
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
3105 Py_INCREF(r);
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
3106 PyModule_AddObject(m, "HAS_NPN", r);
2514a4e2b3ce Issue #14204: The ssl module now has support for the Next Protocol Negotiation extension, if available in the underlying OpenSSL library.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75043
diff changeset
3107
77578
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3108 /* Mappings for error codes */
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3109 err_codes_to_names = PyDict_New();
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3110 err_names_to_codes = PyDict_New();
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3111 if (err_codes_to_names == NULL || err_names_to_codes == NULL)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3112 return NULL;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3113 errcode = error_codes;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3114 while (errcode->mnemonic != NULL) {
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3115 PyObject *mnemo, *key;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3116 mnemo = PyUnicode_FromString(errcode->mnemonic);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3117 key = Py_BuildValue("ii", errcode->library, errcode->reason);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3118 if (mnemo == NULL || key == NULL)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3119 return NULL;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3120 if (PyDict_SetItem(err_codes_to_names, key, mnemo))
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3121 return NULL;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3122 if (PyDict_SetItem(err_names_to_codes, mnemo, key))
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3123 return NULL;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3124 Py_DECREF(key);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3125 Py_DECREF(mnemo);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3126 errcode++;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3127 }
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3128 if (PyModule_AddObject(m, "err_codes_to_names", err_codes_to_names))
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3129 return NULL;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3130 if (PyModule_AddObject(m, "err_names_to_codes", err_names_to_codes))
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3131 return NULL;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3132
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3133 lib_codes_to_names = PyDict_New();
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3134 if (lib_codes_to_names == NULL)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3135 return NULL;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3136 libcode = library_codes;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3137 while (libcode->library != NULL) {
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3138 PyObject *mnemo, *key;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3139 key = PyLong_FromLong(libcode->code);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3140 mnemo = PyUnicode_FromString(libcode->library);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3141 if (key == NULL || mnemo == NULL)
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3142 return NULL;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3143 if (PyDict_SetItem(lib_codes_to_names, key, mnemo))
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3144 return NULL;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3145 Py_DECREF(key);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3146 Py_DECREF(mnemo);
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3147 libcode++;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3148 }
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3149 if (PyModule_AddObject(m, "lib_codes_to_names", lib_codes_to_names))
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3150 return NULL;
96513d71e650 Issue #14837: SSL errors now have `library` and `reason` attributes describing precisely what happened and in which OpenSSL submodule.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75865
diff changeset
3151
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3152 /* OpenSSL version */
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3153 /* SSLeay() gives us the version of the library linked against,
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3154 which could be different from the headers version.
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3155 */
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3156 libver = SSLeay();
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3157 r = PyLong_FromUnsignedLong(libver);
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3158 if (r == NULL)
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3159 return NULL;
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3160 if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r))
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3161 return NULL;
71255
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
3162 parse_openssl_version(libver, &major, &minor, &fix, &patch, &status);
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3163 r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3164 if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r))
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3165 return NULL;
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3166 r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION));
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3167 if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r))
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3168 return NULL;
60373
9e5e9d8fa847 Merged revisions 79812 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60034
diff changeset
3169
71255
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
3170 libver = OPENSSL_VERSION_NUMBER;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
3171 parse_openssl_version(libver, &major, &minor, &fix, &patch, &status);
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
3172 r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
3173 if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r))
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
3174 return NULL;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
3175
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
3176 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
3177 }