annotate Modules/_ssl.c @ 81343:3a7cd8efc19f

#16852: merge with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Thu, 10 Jan 2013 03:29:45 +0200
parents 52b4d9bfc9ea
children 44e614446f78
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
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
43 enum py_ssl_error {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
44 /* these mirror ssl.h */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
45 PY_SSL_ERROR_NONE,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
46 PY_SSL_ERROR_SSL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
47 PY_SSL_ERROR_WANT_READ,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
48 PY_SSL_ERROR_WANT_WRITE,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
49 PY_SSL_ERROR_WANT_X509_LOOKUP,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
50 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
51 PY_SSL_ERROR_ZERO_RETURN,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
52 PY_SSL_ERROR_WANT_CONNECT,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
53 /* start of non ssl.h errorcodes */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
54 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
55 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
56 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
57 };
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
58
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
59 enum py_ssl_server_or_client {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
60 PY_SSL_CLIENT,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
61 PY_SSL_SERVER
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
62 };
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
63
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
64 enum py_ssl_cert_requirements {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
65 PY_SSL_CERT_NONE,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
66 PY_SSL_CERT_OPTIONAL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
67 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
68 };
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
69
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
70 enum py_ssl_version {
69997
b7abf0590e1c Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66806
diff changeset
71 #ifndef OPENSSL_NO_SSL2
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
72 PY_SSL_VERSION_SSL2,
69997
b7abf0590e1c Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66806
diff changeset
73 #endif
b7abf0590e1c Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66806
diff changeset
74 PY_SSL_VERSION_SSL3=1,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
75 PY_SSL_VERSION_SSL23,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
76 PY_SSL_VERSION_TLS1
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
77 };
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
78
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
79 struct py_ssl_error_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
80 const char *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
81 int library, 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
82 };
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
83
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
84 struct py_ssl_library_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
85 const char *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
86 int 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
87 };
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
88
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
89 /* Include symbols from _socket module */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
90 #include "socketmodule.h"
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
91
53816
911df82bc25d add a replacement API for PyCObject, PyCapsule #5630
Benjamin Peterson <benjamin@python.org>
parents: 51922
diff changeset
92 static PySocketModule_APIObject PySocketModule;
911df82bc25d add a replacement API for PyCObject, PyCapsule #5630
Benjamin Peterson <benjamin@python.org>
parents: 51922
diff changeset
93
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
94 #if defined(HAVE_POLL_H)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
95 #include <poll.h>
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
96 #elif defined(HAVE_SYS_POLL_H)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
97 #include <sys/poll.h>
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
98 #endif
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
99
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
100 /* Include OpenSSL header files */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
101 #include "openssl/rsa.h"
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
102 #include "openssl/crypto.h"
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
103 #include "openssl/x509.h"
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
104 #include "openssl/x509v3.h"
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
105 #include "openssl/pem.h"
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
106 #include "openssl/ssl.h"
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
107 #include "openssl/err.h"
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
108 #include "openssl/rand.h"
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
109
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
110 /* Include generated data (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
111 #include "_ssl_data.h"
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
112
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
113 /* SSL error object */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
114 static PyObject *PySSLErrorObject;
73150
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
115 static PyObject *PySSLZeroReturnErrorObject;
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
116 static PyObject *PySSLWantReadErrorObject;
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
117 static PyObject *PySSLWantWriteErrorObject;
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
118 static PyObject *PySSLSyscallErrorObject;
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
119 static PyObject *PySSLEOFErrorObject;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
120
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
121 /* Error mappings */
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
122 static PyObject *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
123 static PyObject *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
124 static PyObject *lib_codes_to_names;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
125
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
126 #ifdef WITH_THREAD
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
127
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
128 /* 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
129 /* 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
130
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
131 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
132
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
133 #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
134
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
135 /* 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
136
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
137 #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
138
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
139 /* 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
140 #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
141 # 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
142 #else
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
143 # 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
144 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
145
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
146 /* 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
147 * 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
148 * 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
149 #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
150 # 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
151 #else
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
152 # 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
153 #endif
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
154
71440
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
155 /* 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
156 * 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
157 #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
158
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
159 /* 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
160 #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
161 # 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
162 #else
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
163 # 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
164 #endif
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
165
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
166 /* 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
167 #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
168 # 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
169 #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
170
75043
1a06f0a8120f Fix last remaining build issues of _ssl under old OpenSSLs. Patch by Vinay.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75005
diff changeset
171 /* 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
172 #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
173 # 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
174 #endif
1a06f0a8120f Fix last remaining build issues of _ssl under old OpenSSLs. Patch by Vinay.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75005
diff changeset
175
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
176
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
177 typedef struct {
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
178 PyObject_HEAD
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
179 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
180 #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
181 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
182 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
183 #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
184 #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
185 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
186 #endif
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
187 } 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
188
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
189 typedef struct {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
190 PyObject_HEAD
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
191 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
192 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
193 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
194 X509 *peer_cert;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
195 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
196 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
197 } 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
198
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
199 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
200 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
201
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
202 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
203 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
204 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
205 int writing);
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
206 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
207 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
208
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
209 #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
210 #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
211
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
212 typedef enum {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
213 SOCKET_IS_NONBLOCKING,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
214 SOCKET_IS_BLOCKING,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
215 SOCKET_HAS_TIMED_OUT,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
216 SOCKET_HAS_BEEN_CLOSED,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
217 SOCKET_TOO_LARGE_FOR_SELECT,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
218 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
219 } 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
220
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
221 /* 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
222 #define STRINGIFY1(x) #x
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
223 #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
224 #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
225 #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
226
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
227
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
228 /*
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
229 * 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
230 */
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
231
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
232 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
233 "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
234
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
235 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
236 "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
237
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
238 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
239 "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
240 "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
241
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
242 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
243 "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
244 "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
245
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
246 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
247 "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
248
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
249 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
250 "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
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 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
253 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
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 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
256 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
257 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
258 }
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 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
260 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
261 }
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
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 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
264 {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
265 {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
266 {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
267 {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
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
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 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
271 "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
272 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
273 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
274 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
275 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
276 };
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 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
279 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
280 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
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 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
283 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
284 _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
285 _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
286
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 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
288 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
289
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 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
291 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
292 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
293 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
294 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
295 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
296 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
297 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
298 /* 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
299 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
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 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
302 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
303 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
304 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
305 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
306 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
307 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
308 }
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 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
310 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
311 }
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 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
313 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
314
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 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
316 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
317 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
318 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
319 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
320 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
321 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
322 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
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 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
325 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
326 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
327 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
328 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
329 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
330 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
331 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
332 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
333 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
334 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
335 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
336 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
337 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
338 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
339 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
340 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
341 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
342 }
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
343
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
344 static PyObject *
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
345 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
346 {
73150
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
347 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
348 char *errstr = NULL;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
349 int err;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
350 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
351 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
352
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
353 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
354 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
355
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
356 if (obj->ssl != NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
357 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
358
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
359 switch (err) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
360 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
361 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
362 type = PySSLZeroReturnErrorObject;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
363 p = PY_SSL_ERROR_ZERO_RETURN;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
364 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
365 case SSL_ERROR_WANT_READ:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
366 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
367 type = PySSLWantReadErrorObject;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
368 p = PY_SSL_ERROR_WANT_READ;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
369 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
370 case SSL_ERROR_WANT_WRITE:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
371 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
372 type = PySSLWantWriteErrorObject;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
373 errstr = "The operation did not complete (write)";
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
374 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
375 case SSL_ERROR_WANT_X509_LOOKUP:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
376 p = PY_SSL_ERROR_WANT_X509_LOOKUP;
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
377 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
378 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
379 case SSL_ERROR_WANT_CONNECT:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
380 p = PY_SSL_ERROR_WANT_CONNECT;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
381 errstr = "The operation did not complete (connect)";
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
382 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
383 case SSL_ERROR_SYSCALL:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
384 {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
385 if (e == 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
386 PySocketSockObject *s
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
387 = (PySocketSockObject *) PyWeakref_GetObject(obj->Socket);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
388 if (ret == 0 || (((PyObject *)s) == Py_None)) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
389 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
390 type = PySSLEOFErrorObject;
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
391 errstr = "EOF occurred in violation of protocol";
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
392 } else if (ret == -1) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
393 /* underlying BIO reported an I/O error */
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
394 Py_INCREF(s);
61646
8b6401ae1262 Merged revisions 81242 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61537
diff changeset
395 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
396 s->errorhandler();
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
397 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
398 return NULL;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
399 } else { /* possible? */
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
400 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
401 type = PySSLSyscallErrorObject;
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
402 errstr = "Some I/O error occurred";
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
403 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
404 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
405 p = PY_SSL_ERROR_SYSCALL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
406 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
407 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
408 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
409 case SSL_ERROR_SSL:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
410 {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
411 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
412 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
413 /* possible? */
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
414 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
415 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
416 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
417 default:
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
418 p = PY_SSL_ERROR_INVALID_ERROR_CODE;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
419 errstr = "Invalid error code";
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
420 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
421 }
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
422 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
423 ERR_clear_error();
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
424 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
425 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
426
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
427 static PyObject *
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
428 _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
429
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
430 if (errstr == NULL)
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
431 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
432 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
433 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
434 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
435 ERR_clear_error();
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
436 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
437 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
438
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
439 /*
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
440 * 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
441 */
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
442
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
443 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
444 newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
445 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
446 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
447 {
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
448 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
449 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
450
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
451 self = PyObject_New(PySSLSocket, &PySSLSocket_Type);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
452 if (self == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
453 return NULL;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
454
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
455 self->peer_cert = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
456 self->ssl = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
457 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
458 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
459 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
460
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
461 /* Make sure the SSL error state is initialized */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
462 (void) ERR_get_state();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
463 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
464
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
465 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
466 self->ssl = SSL_new(ctx);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
467 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
468 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
469 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
470 #ifdef SSL_MODE_AUTO_RETRY
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
471 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
472 #endif
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
473
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
474 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
475 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
476 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
477 #endif
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
478
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
479 /* 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
480 * to non-blocking mode (blocking is the default)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
481 */
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
482 if (sock->sock_timeout >= 0.0) {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
483 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
484 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
485 }
27557
5eefec94a784 SF patch 676472 by Geoff Talvola, reviewed by Ben Laurie.
Guido van Rossum <guido@python.org>
parents: 27430
diff changeset
486
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
487 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
488 if (socket_type == PY_SSL_CLIENT)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
489 SSL_set_connect_state(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
490 else
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
491 SSL_set_accept_state(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
492 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
493
71440
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
494 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
495 self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
496 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
497 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
498
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
499 /* 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
500
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
501 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
502 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
503 int ret;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
504 int err;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
505 int sockstate, nonblocking;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
506 PySocketSockObject *sock
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
507 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
60936
5ed34f3f6c35 Merged revisions 80454 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60901
diff changeset
508
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
509 if (((PyObject*)sock) == Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
510 _setSSLError("Underlying socket connection gone",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
511 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
512 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
513 }
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
514 Py_INCREF(sock);
60936
5ed34f3f6c35 Merged revisions 80454 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60901
diff changeset
515
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
516 /* 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
517 nonblocking = (sock->sock_timeout >= 0.0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
518 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
519 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
520
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
521 /* Actually negotiate SSL connection */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
522 /* 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
523 do {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
524 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
525 ret = SSL_do_handshake(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
526 err = SSL_get_error(self->ssl, ret);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
527 PySSL_END_ALLOW_THREADS
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
528 if (PyErr_CheckSignals())
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
529 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
530 if (err == SSL_ERROR_WANT_READ) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
531 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
532 } else if (err == SSL_ERROR_WANT_WRITE) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
533 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
534 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
535 sockstate = SOCKET_OPERATION_OK;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
536 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
537 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
538 PyErr_SetString(PySocketModule.timeout_error,
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
539 ERRSTR("The handshake operation timed out"));
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
540 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
541 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
542 PyErr_SetString(PySSLErrorObject,
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
543 ERRSTR("Underlying socket has been closed."));
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
544 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
545 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
546 PyErr_SetString(PySSLErrorObject,
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
547 ERRSTR("Underlying socket too large for select()."));
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
548 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
549 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
550 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
551 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
552 } 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
553 Py_DECREF(sock);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
554 if (ret < 1)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
555 return PySSL_SetError(self, ret, __FILE__, __LINE__);
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
556
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
557 if (self->peer_cert)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
558 X509_free (self->peer_cert);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
559 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
560 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
561 PySSL_END_ALLOW_THREADS
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
562
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
563 Py_INCREF(Py_None);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
564 return Py_None;
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
565
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
566 error:
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
567 Py_DECREF(sock);
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
568 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
569 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
570
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
571 static PyObject *
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
572 _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
573
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
574 char namebuf[X509_NAME_MAXLEN];
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
575 int buflen;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
576 PyObject *name_obj;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
577 PyObject *value_obj;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
578 PyObject *attr;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
579 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
580
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
581 buflen = OBJ_obj2txt(namebuf, sizeof(namebuf), name, 0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
582 if (buflen < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
583 _setSSLError(NULL, 0, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
584 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
585 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
586 name_obj = PyUnicode_FromStringAndSize(namebuf, buflen);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
587 if (name_obj == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
588 goto fail;
43948
8e0c4c710375 Make read() and certificate() return bytes instead of bytearray instances.
Guido van Rossum <guido@python.org>
parents: 43869
diff changeset
589
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
590 buflen = ASN1_STRING_to_UTF8(&valuebuf, value);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
591 if (buflen < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
592 _setSSLError(NULL, 0, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
593 Py_DECREF(name_obj);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
594 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
595 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
596 value_obj = PyUnicode_DecodeUTF8((char *) valuebuf,
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
597 buflen, "strict");
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
598 OPENSSL_free(valuebuf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
599 if (value_obj == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
600 Py_DECREF(name_obj);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
601 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
602 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
603 attr = PyTuple_New(2);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
604 if (attr == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
605 Py_DECREF(name_obj);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
606 Py_DECREF(value_obj);
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 PyTuple_SET_ITEM(attr, 0, name_obj);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
610 PyTuple_SET_ITEM(attr, 1, value_obj);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
611 return attr;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
612
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
613 fail:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
614 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
615 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
616
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
617 static PyObject *
43292
876b7ef4775d Merged revisions 57778-58052 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42954
diff changeset
618 _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
619 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
620 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
621 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
622 PyObject *rdnt;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
623 PyObject *attr = NULL; /* tuple to hold an attribute */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
624 int entry_count = X509_NAME_entry_count(xname);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
625 X509_NAME_ENTRY *entry;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
626 ASN1_OBJECT *name;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
627 ASN1_STRING *value;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
628 int index_counter;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
629 int rdn_level = -1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
630 int retcode;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
631
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
632 dn = PyList_New(0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
633 if (dn == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
634 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
635 /* 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
636 rdn = PyList_New(0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
637 if (rdn == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
638 goto fail0;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
639
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
640 for (index_counter = 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
641 index_counter < entry_count;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
642 index_counter++)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
643 {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
644 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
645
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
646 /* 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
647 if (rdn_level >= 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
648 if (rdn_level != entry->set) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
649 /* yes, new RDN */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
650 /* add old RDN to DN */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
651 rdnt = PyList_AsTuple(rdn);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
652 Py_DECREF(rdn);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
653 if (rdnt == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
654 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
655 retcode = PyList_Append(dn, rdnt);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
656 Py_DECREF(rdnt);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
657 if (retcode < 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
658 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
659 /* create new RDN */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
660 rdn = PyList_New(0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
661 if (rdn == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
662 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
663 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
664 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
665 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
666
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
667 /* now add this attribute to the current RDN */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
668 name = X509_NAME_ENTRY_get_object(entry);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
669 value = X509_NAME_ENTRY_get_data(entry);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
670 attr = _create_tuple_for_attribute(name, value);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
671 /*
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
672 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
673 entry->set,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
674 PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 0)),
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
675 PyBytes_AS_STRING(PyTuple_GET_ITEM(attr, 1)));
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
676 */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
677 if (attr == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
678 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
679 retcode = PyList_Append(rdn, attr);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
680 Py_DECREF(attr);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
681 if (retcode < 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
682 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
683 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
684 /* 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
685 if (rdn != NULL) {
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
686 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
687 rdnt = PyList_AsTuple(rdn);
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
688 Py_DECREF(rdn);
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
689 if (rdnt == NULL)
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
690 goto fail0;
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
691 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
692 Py_DECREF(rdnt);
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
693 if (retcode < 0)
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
694 goto fail0;
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
695 }
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
696 else {
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
697 Py_DECREF(rdn);
f3a4c2b34973 Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
Antoine Pitrou <solipsis@pitrou.net>
parents: 74648
diff changeset
698 }
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
699 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
700
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
701 /* convert list to tuple */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
702 rdnt = PyList_AsTuple(dn);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
703 Py_DECREF(dn);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
704 if (rdnt == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
705 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
706 return rdnt;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
707
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
708 fail1:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
709 Py_XDECREF(rdn);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
710
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
711 fail0:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
712 Py_XDECREF(dn);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
713 return NULL;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
714 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
715
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
716 static PyObject *
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
717 _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
718
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
719 /* this code follows the procedure outlined in
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
720 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
721 function to extract the STACK_OF(GENERAL_NAME),
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
722 then iterates through the stack to add the
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
723 names. */
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
724
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
725 int i, j;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
726 PyObject *peer_alt_names = Py_None;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
727 PyObject *v, *t;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
728 X509_EXTENSION *ext = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
729 GENERAL_NAMES *names = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
730 GENERAL_NAME *name;
65272
4755744724f2 Merged revisions 85432 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64404
diff changeset
731 const X509V3_EXT_METHOD *method;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
732 BIO *biobuf = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
733 char buf[2048];
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
734 char *vptr;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
735 int len;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
736 /* 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
737 #if OPENSSL_VERSION_NUMBER >= 0x009060dfL
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
738 const unsigned char *p;
59279
951c97e2878d Merged revisions 78598 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 54917
diff changeset
739 #else
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
740 unsigned char *p;
59279
951c97e2878d Merged revisions 78598 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 54917
diff changeset
741 #endif
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
742
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
743 if (certificate == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
744 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
745
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
746 /* get a memory buffer */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
747 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
748
72567
65e7f40fefd4 Issue #13034: When decoding some SSL certificates, the subjectAltName extension could be unreported.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72115
diff changeset
749 i = -1;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
750 while ((i = X509_get_ext_by_NID(
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
751 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
752
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
753 if (peer_alt_names == Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
754 peer_alt_names = PyList_New(0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
755 if (peer_alt_names == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
756 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
757 }
43948
8e0c4c710375 Make read() and certificate() return bytes instead of bytearray instances.
Guido van Rossum <guido@python.org>
parents: 43869
diff changeset
758
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
759 /* now decode the altName */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
760 ext = X509_get_ext(certificate, i);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
761 if(!(method = X509V3_EXT_get(ext))) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
762 PyErr_SetString
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
763 (PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
764 ERRSTR("No method for internalizing subjectAltName!"));
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
765 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
766 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
767
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
768 p = ext->value->data;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
769 if (method->it)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
770 names = (GENERAL_NAMES*)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
771 (ASN1_item_d2i(NULL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
772 &p,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
773 ext->value->length,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
774 ASN1_ITEM_ptr(method->it)));
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
775 else
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
776 names = (GENERAL_NAMES*)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
777 (method->d2i(NULL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
778 &p,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
779 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
780
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
781 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
782
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
783 /* 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
784
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
785 name = sk_GENERAL_NAME_value(names, j);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
786 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
787
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
788 /* we special-case DirName as a tuple of
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
789 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
790
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
791 t = PyTuple_New(2);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
792 if (t == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
793 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
794 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
795
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
796 v = PyUnicode_FromString("DirName");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
797 if (v == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
798 Py_DECREF(t);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
799 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
800 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
801 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
802
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
803 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
804 if (v == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
805 Py_DECREF(t);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
806 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
807 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
808 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
809
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
810 } else {
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
811
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
812 /* 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
813
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
814 (void) BIO_reset(biobuf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
815 GENERAL_NAME_print(biobuf, name);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
816 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
817 if (len < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
818 _setSSLError(NULL, 0, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
819 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
820 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
821 vptr = strchr(buf, ':');
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
822 if (vptr == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
823 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
824 t = PyTuple_New(2);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
825 if (t == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
826 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
827 v = PyUnicode_FromStringAndSize(buf, (vptr - buf));
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
828 if (v == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
829 Py_DECREF(t);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
830 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
831 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
832 PyTuple_SET_ITEM(t, 0, v);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
833 v = PyUnicode_FromStringAndSize((vptr + 1),
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
834 (len - (vptr - buf + 1)));
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
835 if (v == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
836 Py_DECREF(t);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
837 goto fail;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
838 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
839 PyTuple_SET_ITEM(t, 1, v);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
840 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
841
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
842 /* 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
843
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
844 if (PyList_Append(peer_alt_names, t) < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
845 Py_DECREF(t);
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 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
848 Py_DECREF(t);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
849 }
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
850 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
851 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
852 BIO_free(biobuf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
853 if (peer_alt_names != Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
854 v = PyList_AsTuple(peer_alt_names);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
855 Py_DECREF(peer_alt_names);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
856 return v;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
857 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
858 return peer_alt_names;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
859 }
43948
8e0c4c710375 Make read() and certificate() return bytes instead of bytearray instances.
Guido van Rossum <guido@python.org>
parents: 43869
diff changeset
860
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
861
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
862 fail:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
863 if (biobuf != NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
864 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
865
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
866 if (peer_alt_names != Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
867 Py_XDECREF(peer_alt_names);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
868 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
869
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
870 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
871 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
872
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
873 static PyObject *
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
874 _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
875
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
876 PyObject *retval = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
877 BIO *biobuf = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
878 PyObject *peer;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
879 PyObject *peer_alt_names = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
880 PyObject *issuer;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
881 PyObject *version;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
882 PyObject *sn_obj;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
883 ASN1_INTEGER *serialNumber;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
884 char buf[2048];
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
885 int len;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
886 ASN1_TIME *notBefore, *notAfter;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
887 PyObject *pnotBefore, *pnotAfter;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
888
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
889 retval = PyDict_New();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
890 if (retval == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
891 return NULL;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
892
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
893 peer = _create_tuple_for_X509_NAME(
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
894 X509_get_subject_name(certificate));
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
895 if (peer == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
896 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
897 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
898 Py_DECREF(peer);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
899 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
900 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
901 Py_DECREF(peer);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
902
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
903 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
904 X509_get_issuer_name(certificate));
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
905 if (issuer == NULL)
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
906 goto fail0;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
907 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
908 Py_DECREF(issuer);
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
909 goto fail0;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
910 }
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
911 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
912
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
913 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
914 if (PyDict_SetItemString(retval, "version", version) < 0) {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
915 Py_DECREF(version);
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
916 goto fail0;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
917 }
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
918 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
919
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
920 /* get a memory buffer */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
921 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
922
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
923 (void) BIO_reset(biobuf);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
924 serialNumber = X509_get_serialNumber(certificate);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
925 /* 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
926 i2a_ASN1_INTEGER(biobuf, serialNumber);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
927 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
928 if (len < 0) {
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
929 _setSSLError(NULL, 0, __FILE__, __LINE__);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
930 goto fail1;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
931 }
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
932 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
933 if (sn_obj == NULL)
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
934 goto fail1;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
935 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
936 Py_DECREF(sn_obj);
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
937 goto fail1;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
938 }
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
939 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
940
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
941 (void) BIO_reset(biobuf);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
942 notBefore = X509_get_notBefore(certificate);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
943 ASN1_TIME_print(biobuf, notBefore);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
944 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
945 if (len < 0) {
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
946 _setSSLError(NULL, 0, __FILE__, __LINE__);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
947 goto fail1;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
948 }
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
949 pnotBefore = PyUnicode_FromStringAndSize(buf, len);
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
950 if (pnotBefore == NULL)
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
951 goto fail1;
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
952 if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
953 Py_DECREF(pnotBefore);
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
954 goto fail1;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
955 }
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
956 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
957
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
958 (void) BIO_reset(biobuf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
959 notAfter = X509_get_notAfter(certificate);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
960 ASN1_TIME_print(biobuf, notAfter);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
961 len = BIO_gets(biobuf, buf, sizeof(buf)-1);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
962 if (len < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
963 _setSSLError(NULL, 0, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
964 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
965 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
966 pnotAfter = PyUnicode_FromStringAndSize(buf, len);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
967 if (pnotAfter == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
968 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
969 if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
970 Py_DECREF(pnotAfter);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
971 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
972 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
973 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
974
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
975 /* 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
976
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
977 peer_alt_names = _get_peer_alt_names(certificate);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
978 if (peer_alt_names == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
979 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
980 else if (peer_alt_names != Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
981 if (PyDict_SetItemString(retval, "subjectAltName",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
982 peer_alt_names) < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
983 Py_DECREF(peer_alt_names);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
984 goto fail1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
985 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
986 Py_DECREF(peer_alt_names);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
987 }
43948
8e0c4c710375 Make read() and certificate() return bytes instead of bytearray instances.
Guido van Rossum <guido@python.org>
parents: 43869
diff changeset
988
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
989 BIO_free(biobuf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
990 return retval;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
991
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
992 fail1:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
993 if (biobuf != NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
994 BIO_free(biobuf);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
995 fail0:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
996 Py_XDECREF(retval);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
997 return NULL;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42847
diff changeset
998 }
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
999
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1000
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1001 static PyObject *
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1002 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
1003
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1004 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
1005 PyObject *filename;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1006 X509 *x=NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1007 BIO *cert;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1008
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
1009 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
1010 PyUnicode_FSConverter, &filename))
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1011 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1012
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1013 if ((cert=BIO_new(BIO_s_file())) == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1014 PyErr_SetString(PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1015 "Can't malloc memory to read file");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1016 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1017 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1018
61639
08abbdc99be6 Issue #8477: _ssl._test_decode_cert() supports str with surrogates and bytes
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61637
diff changeset
1019 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
1020 PyErr_SetString(PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1021 "Can't open file");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1022 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1023 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1024
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1025 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
1026 if (x == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1027 PyErr_SetString(PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1028 "Error decoding PEM-encoded file");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1029 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1030 }
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 retval = _decode_certificate(x);
63694
c4bbda2d4c49 Merged revisions 83677 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62489
diff changeset
1033 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
1034
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1035 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
1036 Py_DECREF(filename);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1037 if (cert != NULL) BIO_free(cert);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1038 return retval;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1039 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1040
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1041
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1042 static PyObject *
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1043 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
1044 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1045 PyObject *retval = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1046 int len;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1047 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
1048 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
1049
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
1050 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
1051 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1052
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1053 if (!self->peer_cert)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1054 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
1055
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
1056 if (binary_mode) {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1057 /* 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
1058
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1059 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
1060
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1061 bytes_buf = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1062 len = i2d_X509(self->peer_cert, &bytes_buf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1063 if (len < 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1064 PySSL_SetError(self, len, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1065 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1066 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1067 /* this is actually an immutable bytes sequence */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1068 retval = PyBytes_FromStringAndSize
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1069 ((const char *) bytes_buf, len);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1070 OPENSSL_free(bytes_buf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1071 return retval;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1072
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1073 } else {
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1074 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
1075 if ((verification & SSL_VERIFY_PEER) == 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1076 return PyDict_New();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1077 else
66114
eeebb622fd08 Issue #10022: The dictionary returned by the `getpeercert()` method
Antoine Pitrou <solipsis@pitrou.net>
parents: 65593
diff changeset
1078 return _decode_certificate(self->peer_cert);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1079 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1080 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1081
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1082 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
1083 "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
1084 \n\
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1085 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
1086 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
1087 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
1088 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
1089 \n\
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1090 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
1091 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
1092 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
1093
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1094 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
1095
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1096 PyObject *retval, *v;
65272
4755744724f2 Merged revisions 85432 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64404
diff changeset
1097 const SSL_CIPHER *current;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1098 char *cipher_name;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1099 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
1100
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1101 if (self->ssl == NULL)
66806
800563ac057e Merged revisions 87140 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65272
diff changeset
1102 Py_RETURN_NONE;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1103 current = SSL_get_current_cipher(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1104 if (current == NULL)
66806
800563ac057e Merged revisions 87140 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65272
diff changeset
1105 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
1106
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1107 retval = PyTuple_New(3);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1108 if (retval == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1109 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1110
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1111 cipher_name = (char *) SSL_CIPHER_get_name(current);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1112 if (cipher_name == NULL) {
66806
800563ac057e Merged revisions 87140 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65272
diff changeset
1113 Py_INCREF(Py_None);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1114 PyTuple_SET_ITEM(retval, 0, Py_None);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1115 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1116 v = PyUnicode_FromString(cipher_name);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1117 if (v == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1118 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1119 PyTuple_SET_ITEM(retval, 0, v);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1120 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1121 cipher_protocol = SSL_CIPHER_get_version(current);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1122 if (cipher_protocol == NULL) {
66806
800563ac057e Merged revisions 87140 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65272
diff changeset
1123 Py_INCREF(Py_None);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1124 PyTuple_SET_ITEM(retval, 1, Py_None);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1125 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1126 v = PyUnicode_FromString(cipher_protocol);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1127 if (v == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1128 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1129 PyTuple_SET_ITEM(retval, 1, v);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1130 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1131 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
1132 if (v == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1133 goto fail0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1134 PyTuple_SET_ITEM(retval, 2, v);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1135 return retval;
43948
8e0c4c710375 Make read() and certificate() return bytes instead of bytearray instances.
Guido van Rossum <guido@python.org>
parents: 43869
diff changeset
1136
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1137 fail0:
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1138 Py_DECREF(retval);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1139 return NULL;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1140 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
1141
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
1142 #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
1143 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
1144 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
1145 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
1146
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
1147 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
1148 &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
1149
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
1150 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
1151 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
1152 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
1153 }
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
1154 #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
1155
74091
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1156 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
1157 #ifdef OPENSSL_NO_COMP
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1158 Py_RETURN_NONE;
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1159 #else
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1160 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
1161 const char *short_name;
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1162
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1163 if (self->ssl == NULL)
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1164 Py_RETURN_NONE;
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1165 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
1166 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
1167 Py_RETURN_NONE;
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1168 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
1169 if (short_name == NULL)
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1170 Py_RETURN_NONE;
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1171 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
1172 #endif
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1173 }
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1174
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
1175 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
1176 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
1177 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
1178 }
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
1179
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
1180 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
1181 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
1182
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
1183 if (PyObject_TypeCheck(value, &PySSLContext_Type)) {
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
1184
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
1185 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
1186 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
1187 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
1188 SSL_set_SSL_CTX(self->ssl, self->ctx->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
1189 } 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
1190 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
1191 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
1192 }
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
1193
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
1194 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
1195 }
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
1196
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
1197 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
1198 "_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
1199 \
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 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
1201 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
1202 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
1203 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
1204
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
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1207 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
1208 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1209 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
1210 X509_free (self->peer_cert);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1211 if (self->ssl)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1212 SSL_free(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1213 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
1214 Py_XDECREF(self->ctx);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1215 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
1216 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1217
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1218 /* 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
1219 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
1220 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
1221 */
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
1222
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1223 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
1224 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
1225 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1226 fd_set fds;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1227 struct timeval tv;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1228 int rc;
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1229
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1230 /* 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
1231 if (s->sock_timeout < 0.0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1232 return SOCKET_IS_BLOCKING;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1233 else if (s->sock_timeout == 0.0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1234 return SOCKET_IS_NONBLOCKING;
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1235
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1236 /* Guard against closed socket */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1237 if (s->sock_fd < 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1238 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
1239
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1240 /* 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
1241 * 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
1242 #ifdef HAVE_POLL
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1243 {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1244 struct pollfd pollfd;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1245 int timeout;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1246
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1247 pollfd.fd = s->sock_fd;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1248 pollfd.events = writing ? POLLOUT : POLLIN;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1249
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1250 /* 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
1251 timeout = (int)(s->sock_timeout * 1000 + 0.5);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1252 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1253 rc = poll(&pollfd, 1, timeout);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1254 PySSL_END_ALLOW_THREADS
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1255
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1256 goto normal_return;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1257 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1258 #endif
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1259
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1260 /* 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
1261 if (!_PyIsSelectable_fd(s->sock_fd))
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1262 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
1263
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1264 /* Construct the arguments to select */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1265 tv.tv_sec = (int)s->sock_timeout;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1266 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
1267 FD_ZERO(&fds);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1268 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
1269
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1270 /* See if the socket is ready */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1271 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1272 if (writing)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1273 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
1274 else
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1275 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
1276 PySSL_END_ALLOW_THREADS
27430
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1277
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1278 #ifdef HAVE_POLL
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1279 normal_return:
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1280 #endif
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1281 /* 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
1282 (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
1283 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
1284 }
1ae0b142370e Support socket timeout in SSL, by Geoff Talvola.
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1285
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1286 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
1287 {
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
1288 Py_buffer buf;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1289 int len;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1290 int sockstate;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1291 int err;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1292 int nonblocking;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1293 PySocketSockObject *sock
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1294 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
44305
19f894ae864a update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents: 44206
diff changeset
1295
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1296 if (((PyObject*)sock) == Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1297 _setSSLError("Underlying socket connection gone",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1298 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1299 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1300 }
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1301 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
1302
62480
21e86dd328f1 Issue #8682: The ssl module now temporary increments the reference count of
Antoine Pitrou <solipsis@pitrou.net>
parents: 61782
diff changeset
1303 if (!PyArg_ParseTuple(args, "y*:write", &buf)) {
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1304 Py_DECREF(sock);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1305 return NULL;
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1306 }
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1307
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1308 /* 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
1309 nonblocking = (sock->sock_timeout >= 0.0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1310 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
1311 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
1312
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1313 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
1314 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
1315 PyErr_SetString(PySocketModule.timeout_error,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1316 "The write operation timed out");
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1317 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1318 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1319 PyErr_SetString(PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1320 "Underlying socket has been closed.");
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1321 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1322 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1323 PyErr_SetString(PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1324 "Underlying socket too large for select().");
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1325 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1326 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1327 do {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1328 PySSL_BEGIN_ALLOW_THREADS
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
1329 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
1330 err = SSL_get_error(self->ssl, len);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1331 PySSL_END_ALLOW_THREADS
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
1332 if (PyErr_CheckSignals()) {
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1333 goto error;
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
1334 }
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1335 if (err == SSL_ERROR_WANT_READ) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
1336 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
1337 } else if (err == SSL_ERROR_WANT_WRITE) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
1338 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
1339 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1340 sockstate = SOCKET_OPERATION_OK;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1341 }
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_IS_NONBLOCKING) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1351 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1352 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1353 } 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
1354
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1355 Py_DECREF(sock);
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
1356 PyBuffer_Release(&buf);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1357 if (len > 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1358 return PyLong_FromLong(len);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1359 else
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1360 return PySSL_SetError(self, len, __FILE__, __LINE__);
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1361
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1362 error:
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1363 Py_DECREF(sock);
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
1364 PyBuffer_Release(&buf);
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1365 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
1366 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1367
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1368 PyDoc_STRVAR(PySSL_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
1369 "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
1370 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1371 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
1372 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
1373
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1374 static PyObject *PySSL_SSLpending(PySSLSocket *self)
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1375 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1376 int count = 0;
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1377
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1378 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1379 count = SSL_pending(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1380 PySSL_END_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1381 if (count < 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1382 return PySSL_SetError(self, count, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1383 else
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1384 return PyLong_FromLong(count);
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1385 }
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1386
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1387 PyDoc_STRVAR(PySSL_SSLpending_doc,
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1388 "pending() -> count\n\
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1389 \n\
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1390 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
1391 pending on the connection.\n");
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1392
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1393 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
1394 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1395 PyObject *dest = NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1396 Py_buffer buf;
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1397 char *mem;
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1398 int len, count;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1399 int buf_passed = 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1400 int sockstate;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1401 int err;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1402 int nonblocking;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1403 PySocketSockObject *sock
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1404 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
44305
19f894ae864a update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents: 44206
diff changeset
1405
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1406 if (((PyObject*)sock) == Py_None) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1407 _setSSLError("Underlying socket connection gone",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1408 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1409 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1410 }
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1411 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
1412
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1413 buf.obj = NULL;
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1414 buf.buf = NULL;
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1415 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
1416 goto error;
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1417
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1418 if ((buf.buf == NULL) && (buf.obj == NULL)) {
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1419 dest = PyBytes_FromStringAndSize(NULL, len);
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1420 if (dest == NULL)
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1421 goto error;
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1422 mem = PyBytes_AS_STRING(dest);
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1423 }
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1424 else {
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1425 buf_passed = 1;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1426 mem = buf.buf;
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1427 if (len <= 0 || len > buf.len) {
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1428 len = (int) buf.len;
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1429 if (buf.len != len) {
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1430 PyErr_SetString(PyExc_OverflowError,
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1431 "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
1432 goto error;
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1433 }
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1434 }
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1435 }
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1436
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1437 /* 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
1438 nonblocking = (sock->sock_timeout >= 0.0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1439 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
1440 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
1441
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1442 /* 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
1443 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1444 count = SSL_pending(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1445 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
1446
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1447 if (!count) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1448 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
1449 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
1450 PyErr_SetString(PySocketModule.timeout_error,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1451 "The read operation timed out");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1452 goto error;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1453 } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1454 PyErr_SetString(PySSLErrorObject,
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
1455 "Underlying socket too large for select().");
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1456 goto error;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1457 } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1458 count = 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1459 goto done;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1460 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1461 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1462 do {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1463 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1464 count = SSL_read(self->ssl, mem, len);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1465 err = SSL_get_error(self->ssl, count);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1466 PySSL_END_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1467 if (PyErr_CheckSignals())
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1468 goto error;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1469 if (err == SSL_ERROR_WANT_READ) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
1470 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
1471 } else if (err == SSL_ERROR_WANT_WRITE) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
1472 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
1473 } else if ((err == SSL_ERROR_ZERO_RETURN) &&
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1474 (SSL_get_shutdown(self->ssl) ==
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1475 SSL_RECEIVED_SHUTDOWN))
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1476 {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1477 count = 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1478 goto done;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1479 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1480 sockstate = SOCKET_OPERATION_OK;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1481 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1482 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
1483 PyErr_SetString(PySocketModule.timeout_error,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1484 "The read operation timed out");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1485 goto error;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1486 } else if (sockstate == SOCKET_IS_NONBLOCKING) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1487 break;
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 } 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
1490 if (count <= 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1491 PySSL_SetError(self, count, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1492 goto error;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1493 }
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1494
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1495 done:
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1496 Py_DECREF(sock);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1497 if (!buf_passed) {
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1498 _PyBytes_Resize(&dest, count);
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1499 return dest;
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1500 }
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1501 else {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1502 PyBuffer_Release(&buf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1503 return PyLong_FromLong(count);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1504 }
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1505
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1506 error:
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1507 Py_DECREF(sock);
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1508 if (!buf_passed)
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1509 Py_XDECREF(dest);
64404
988718e9f93d Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63694
diff changeset
1510 else
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1511 PyBuffer_Release(&buf);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1512 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
1513 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1514
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1515 PyDoc_STRVAR(PySSL_SSLread_doc,
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
1516 "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
1517 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1518 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
1519
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1520 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
1521 {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1522 int err, ssl_err, sockstate, nonblocking;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1523 int zeros = 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1524 PySocketSockObject *sock
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1525 = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1526
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1527 /* Guard against closed socket */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1528 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
1529 _setSSLError("Underlying socket connection gone",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1530 PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1531 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1532 }
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1533 Py_INCREF(sock);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1534
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1535 /* 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
1536 nonblocking = (sock->sock_timeout >= 0.0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1537 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
1538 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
1539
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1540 while (1) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1541 PySSL_BEGIN_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1542 /* 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
1543 * Otherwise OpenSSL might read in too much data,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1544 * eating clear text data that happens to be
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1545 * transmitted after the SSL shutdown.
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1546 * Should be safe to call repeatedly everytime this
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1547 * 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
1548 * condition is met.
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1549 */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1550 if (self->shutdown_seen_zero)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1551 SSL_set_read_ahead(self->ssl, 0);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1552 err = SSL_shutdown(self->ssl);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1553 PySSL_END_ALLOW_THREADS
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1554 /* 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
1555 if (err > 0)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1556 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1557 if (err == 0) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1558 /* Don't loop endlessly; instead preserve legacy
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1559 behaviour of trying SSL_shutdown() only twice.
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1560 This looks necessary for OpenSSL < 0.9.8m */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1561 if (++zeros > 1)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1562 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1563 /* Shutdown was sent, now try receiving */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1564 self->shutdown_seen_zero = 1;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1565 continue;
48681
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1566 }
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 /* Possibly retry shutdown until timeout or failure */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1569 ssl_err = SSL_get_error(self->ssl, err);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1570 if (ssl_err == SSL_ERROR_WANT_READ)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1571 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
1572 else if (ssl_err == SSL_ERROR_WANT_WRITE)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1573 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
1574 else
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1575 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1576 if (sockstate == SOCKET_HAS_TIMED_OUT) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1577 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
1578 PyErr_SetString(PySocketModule.timeout_error,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1579 "The read operation timed out");
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1580 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
1581 PyErr_SetString(PySocketModule.timeout_error,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1582 "The write operation timed out");
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1583 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1584 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1585 else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1586 PyErr_SetString(PySSLErrorObject,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1587 "Underlying socket too large for select().");
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1588 goto error;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1589 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1590 else if (sockstate != SOCKET_OPERATION_OK)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1591 /* Retain the SSL error code */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1592 break;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1593 }
60901
fc97784e5cc2 Merged revisions 80394 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60818
diff changeset
1594
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1595 if (err < 0) {
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1596 Py_DECREF(sock);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1597 return PySSL_SetError(self, err, __FILE__, __LINE__);
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1598 }
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1599 else
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1600 /* It's already INCREF'ed */
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1601 return (PyObject *) sock;
62481
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1602
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1603 error:
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1604 Py_DECREF(sock);
b8b0a15f8a7c Merged revisions 82204 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61646
diff changeset
1605 return NULL;
48681
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1606 }
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1607
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1608 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
1609 "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
1610 \n\
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
1611 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
1612 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
1613
71440
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1614 #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
1615 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
1616 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
1617 {
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1618 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
1619 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
1620 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
1621
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1622 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
1623 /* 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
1624 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
1625 }
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1626 else {
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1627 /* 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
1628 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
1629 }
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1630
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1631 /* 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
1632 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
1633 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
1634 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
1635
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1636 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
1637
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1638 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
1639 }
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1640
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1641 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
1642 "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
1643 \n\
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1644 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
1645 \n\
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1646 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
1647
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
1648 #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
1649
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
1650 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
1651 {"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
1652 (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
1653 {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
1654 };
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
1655
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1656 static PyMethodDef PySSLMethods[] = {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1657 {"do_handshake", (PyCFunction)PySSL_SSLdo_handshake, METH_NOARGS},
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1658 {"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1659 PySSL_SSLwrite_doc},
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1660 {"read", (PyCFunction)PySSL_SSLread, METH_VARARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1661 PySSL_SSLread_doc},
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1662 {"pending", (PyCFunction)PySSL_SSLpending, METH_NOARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1663 PySSL_SSLpending_doc},
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1664 {"peer_certificate", (PyCFunction)PySSL_peercert, METH_VARARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1665 PySSL_peercert_doc},
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1666 {"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
1667 #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
1668 {"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
1669 #endif
74091
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
1670 {"compression", (PyCFunction)PySSL_compression, METH_NOARGS},
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1671 {"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1672 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
1673 #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
1674 {"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
1675 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
1676 #endif
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1677 {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
1678 };
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1679
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1680 static PyTypeObject PySSLSocket_Type = {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1681 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
1682 "_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
1683 sizeof(PySSLSocket), /*tp_basicsize*/
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1684 0, /*tp_itemsize*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1685 /* methods */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1686 (destructor)PySSL_dealloc, /*tp_dealloc*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1687 0, /*tp_print*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1688 0, /*tp_getattr*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1689 0, /*tp_setattr*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1690 0, /*tp_reserved*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1691 0, /*tp_repr*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1692 0, /*tp_as_number*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1693 0, /*tp_as_sequence*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1694 0, /*tp_as_mapping*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1695 0, /*tp_hash*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1696 0, /*tp_call*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1697 0, /*tp_str*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1698 0, /*tp_getattro*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1699 0, /*tp_setattro*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1700 0, /*tp_as_buffer*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1701 Py_TPFLAGS_DEFAULT, /*tp_flags*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1702 0, /*tp_doc*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1703 0, /*tp_traverse*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1704 0, /*tp_clear*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1705 0, /*tp_richcompare*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1706 0, /*tp_weaklistoffset*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1707 0, /*tp_iter*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1708 0, /*tp_iternext*/
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
1709 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
1710 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
1711 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
1712 };
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1713
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1714
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1715 /*
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1716 * _SSLContext objects
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1717 */
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1718
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1719 static PyObject *
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1720 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
1721 {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1722 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
1723 PySSLContext *self;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1724 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
1725 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
1726
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1727 if (!PyArg_ParseTupleAndKeywords(
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1728 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
1729 &proto_version))
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1730 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1731
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1732 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
1733 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
1734 ctx = SSL_CTX_new(TLSv1_method());
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1735 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
1736 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
1737 #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
1738 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
1739 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
1740 #endif
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1741 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
1742 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
1743 else
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1744 proto_version = -1;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1745 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
1746
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1747 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
1748 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
1749 "invalid protocol version");
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1750 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1751 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1752 if (ctx == NULL) {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1753 PyErr_SetString(PySSLErrorObject,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1754 "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
1755 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1756 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1757
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1758 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
1759 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
1760 if (self == NULL) {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1761 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
1762 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1763 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1764 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
1765 #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
1766 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
1767 #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
1768 #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
1769 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
1770 #endif
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1771 /* Defaults */
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1772 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
1773 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
1774 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
1775
65255
c48c09e693e6 Define a "session_id_context" at context creation. This is recommended
Antoine Pitrou <solipsis@pitrou.net>
parents: 65246
diff changeset
1776 #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
1777 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
1778 sizeof(SID_CTX));
c48c09e693e6 Define a "session_id_context" at context creation. This is recommended
Antoine Pitrou <solipsis@pitrou.net>
parents: 65246
diff changeset
1779 #undef SID_CTX
c48c09e693e6 Define a "session_id_context" at context creation. This is recommended
Antoine Pitrou <solipsis@pitrou.net>
parents: 65246
diff changeset
1780
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1781 return (PyObject *)self;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1782 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1783
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
1784 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
1785 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
1786 {
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
1787 #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
1788 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
1789 #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
1790 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
1791 }
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
1792
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
1793 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
1794 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
1795 {
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
1796 #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
1797 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
1798 #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
1799 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
1800 }
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
1801
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1802 static void
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1803 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
1804 {
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
1805 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
1806 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
1807 #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
1808 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
1809 #endif
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1810 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
1811 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1812
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1813 static PyObject *
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1814 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
1815 {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1816 int ret;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1817 const char *cipherlist;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1818
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1819 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
1820 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1821 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
1822 if (ret == 0) {
61637
2ca4626d9c93 Fix (hopefully) the remaining test_ssl buildbot failures
Antoine Pitrou <solipsis@pitrou.net>
parents: 61635
diff changeset
1823 /* 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
1824 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
1825 is done. */
2ca4626d9c93 Fix (hopefully) the remaining test_ssl buildbot failures
Antoine Pitrou <solipsis@pitrou.net>
parents: 61635
diff changeset
1826 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
1827 PyErr_SetString(PySSLErrorObject,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1828 "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
1829 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1830 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1831 Py_RETURN_NONE;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1832 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1833
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
1834 #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
1835 /* 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
1836 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
1837 _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
1838 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
1839 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
1840 {
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 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
1842
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 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
1844 *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
1845 *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
1846 } 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
1847 *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
1848 *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
1849 }
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
1850
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
1851 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
1852 }
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
1853 /* 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
1854 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
1855 _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
1856 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
1857 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
1858 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
1859 {
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
1860 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
1861
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
1862 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
1863 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
1864
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
1865 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
1866 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
1867 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
1868 } 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
1869 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
1870 }
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
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 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
1873 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
1874 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
1875
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 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
1877 }
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 #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
1879
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 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
1881 _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
1882 {
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 #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
1884 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
1885
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 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
1887 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
1888
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
1889 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
1890 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
1891 }
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
1892
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
1893 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
1894 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
1895 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
1896 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
1897 }
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 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
1899 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
1900
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 /* 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
1902 * 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
1903 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
1904 _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
1905 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
1906 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
1907 _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
1908 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
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 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
1911 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
1912 #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
1913 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
1914 "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
1915 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
1916 #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
1917 }
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
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1919 static PyObject *
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1920 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
1921 {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1922 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
1923 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
1924 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
1925 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
1926 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
1927 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
1928 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
1929 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1930 PyErr_SetString(PySSLErrorObject,
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1931 "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
1932 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1933 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1934
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1935 static int
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1936 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
1937 {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1938 int n, mode;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1939 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
1940 return -1;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1941 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
1942 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
1943 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
1944 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
1945 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
1946 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
1947 else {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1948 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
1949 "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
1950 return -1;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1951 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1952 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
1953 return 0;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
1954 }
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 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
1957 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
1958 {
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1959 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
1960 }
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1961
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1962 static int
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1963 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
1964 {
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1965 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
1966 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
1967 return -1;
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1968 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
1969 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
1970 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
1971 if (clear) {
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1972 #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
1973 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
1974 #else
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1975 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
1976 "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
1977 return -1;
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1978 #endif
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1979 }
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1980 if (set)
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1981 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
1982 return 0;
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1983 }
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
1984
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
1985 typedef struct {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
1986 PyThreadState *thread_state;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
1987 PyObject *callable;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
1988 char *password;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
1989 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
1990 int error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
1991 } _PySSLPasswordInfo;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
1992
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
1993 static int
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
1994 _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
1995 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
1996 {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
1997 /* 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
1998 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
1999 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
2000 by the caller */
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2001 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
2002 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
2003 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
2004
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2005 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
2006 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
2007 if (!password_bytes) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2008 goto error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2009 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2010 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
2011 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
2012 } 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
2013 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
2014 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
2015 } 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
2016 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
2017 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
2018 } else {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2019 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
2020 goto error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2021 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2022
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2023 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
2024 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
2025 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
2026 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
2027 "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
2028 goto error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2029 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2030 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
2031 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
2032
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2033 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
2034 return 1;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2035
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2036 error:
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2037 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
2038 return 0;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2039 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2040
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2041 static int
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2042 _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
2043 {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2044 _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
2045 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
2046
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2047 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
2048
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2049 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
2050 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
2051 if (!fn_ret) {
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2052 /* 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
2053 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
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 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
2058 "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
2059 goto error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2060 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2061 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
2062 }
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 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
2065 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
2066 "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
2067 goto error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2068 }
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 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
2071 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
2072 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
2073
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2074 error:
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2075 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
2076 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
2077 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
2078 return -1;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2079 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2080
61782
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
2081 static PyObject *
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2082 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
2083 {
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2084 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
2085 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
2086 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
2087 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
2088 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
2089 _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
2090 int r;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2091
64306
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2092 errno = 0;
64358
063b554d3153 Try to fix some buildbot failures on test_ssl
Antoine Pitrou <solipsis@pitrou.net>
parents: 64352
diff changeset
2093 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
2094 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
2095 "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
2096 &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
2097 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2098 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
2099 keyfile = NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2100 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
2101 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
2102 "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
2103 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2104 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2105 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
2106 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
2107 "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
2108 goto error;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2109 }
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2110 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
2111 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
2112 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
2113 } 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
2114 "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
2115 goto error;
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2116 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2117 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
2118 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
2119 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2120 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
2121 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
2122 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
2123 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
2124 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
2125 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
2126 ERR_clear_error();
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2127 /* 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
2128 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2129 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
2130 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
2131 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
2132 }
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2133 else {
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2134 _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
2135 }
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2136 goto error;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2137 }
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2138 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
2139 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
2140 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
2141 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
2142 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
2143 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
2144 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
2145 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
2146 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
2147 ERR_clear_error();
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2148 /* 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
2149 }
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2150 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
2151 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
2152 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
2153 }
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2154 else {
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2155 _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
2156 }
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2157 goto error;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2158 }
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2159 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
2160 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
2161 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
2162 if (r != 1) {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2163 _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
2164 goto error;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2165 }
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2166 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
2167 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
2168 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
2169 Py_RETURN_NONE;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2170
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2171 error:
72071
cdc6c1b072a5 Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
Antoine Pitrou <solipsis@pitrou.net>
parents: 71440
diff changeset
2172 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
2173 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
2174 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
2175 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
2176 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
2177 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2178 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2179
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2180 static PyObject *
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2181 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
2182 {
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2183 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
2184 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
2185 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
2186 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
2187 int r;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2188
64306
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2189 errno = 0;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2190 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
2191 "|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
2192 &cafile, &capath))
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2193 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2194 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
2195 cafile = NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2196 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
2197 capath = NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2198 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
2199 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
2200 "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
2201 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2202 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2203 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
2204 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
2205 "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
2206 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2207 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2208 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
2209 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
2210 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
2211 "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
2212 return NULL;
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 if (cafile)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2215 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
2216 if (capath)
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2217 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
2218 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
2219 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
2220 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
2221 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
2222 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
2223 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
2224 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
2225 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
2226 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
2227 }
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2228 else {
9297974604ff Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63692
diff changeset
2229 _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
2230 }
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2231 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2232 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2233 Py_RETURN_NONE;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2234 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2235
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2236 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
2237 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
2238 {
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2239 FILE *f;
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2240 DH *dh;
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2241
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2242 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
2243 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
2244 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
2245 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
2246 return NULL;
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2247 }
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2248 errno = 0;
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2249 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
2250 dh = PEM_read_DHparams(f, NULL, NULL, NULL);
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2251 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
2252 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
2253 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
2254 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
2255 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
2256 }
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2257 else {
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2258 _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
2259 }
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2260 return NULL;
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2261 }
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2262 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
2263 _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
2264 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
2265 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
2266 }
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2267
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74103
diff changeset
2268 static PyObject *
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2269 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
2270 {
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2271 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
2272 PySocketSockObject *sock;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2273 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
2274 char *hostname = NULL;
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2275 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
2276
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2277 /* 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
2278 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
2279 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
2280 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
2281 &sock, &server_side,
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2282 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
2283 PyErr_Clear();
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2284 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
2285 PySocketModule.Sock_Type,
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2286 &sock, &server_side,
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2287 "idna", &hostname))
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2288 return NULL;
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2289 #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2290 PyMem_Free(hostname);
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2291 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
2292 "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
2293 return NULL;
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2294 #endif
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2295 }
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2296
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
2297 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
2298 hostname);
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2299 if (hostname != NULL)
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2300 PyMem_Free(hostname);
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
2301 return res;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2302 }
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2303
65223
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2304 static PyObject *
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2305 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
2306 {
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2307 int r;
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2308 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
2309 if (!stats)
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2310 return NULL;
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2311
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2312 #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
2313 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
2314 if (value == NULL) \
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2315 goto error; \
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2316 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
2317 Py_DECREF(value); \
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2318 if (r < 0) \
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2319 goto error;
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2320
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2321 ADD_STATS(number, "number");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2322 ADD_STATS(connect, "connect");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2323 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
2324 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
2325 ADD_STATS(accept, "accept");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2326 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
2327 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
2328 ADD_STATS(accept, "accept");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2329 ADD_STATS(hits, "hits");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2330 ADD_STATS(misses, "misses");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2331 ADD_STATS(timeouts, "timeouts");
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2332 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
2333
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2334 #undef ADD_STATS
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2335
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2336 return stats;
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2337
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2338 error:
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2339 Py_DECREF(stats);
9c723afa44da Issue #10075: Add a session_stats() method to SSLContext objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64403
diff changeset
2340 return NULL;
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
66217
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2343 static PyObject *
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2344 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
2345 {
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2346 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
2347 _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
2348 return NULL;
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2349 }
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2350 Py_RETURN_NONE;
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2351 }
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2352
74103
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
2353 #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
2354 static PyObject *
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2355 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
2356 {
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2357 PyObject *name_bytes;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2358 int nid;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2359 EC_KEY *key;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2360
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2361 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
2362 return NULL;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2363 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
2364 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
2365 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
2366 if (nid == 0) {
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2367 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
2368 "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
2369 return NULL;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2370 }
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2371 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
2372 if (key == NULL) {
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2373 _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
2374 return NULL;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2375 }
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2376 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
2377 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
2378 Py_RETURN_NONE;
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2379 }
74103
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
2380 #endif
74084
8b729d65cfd2 Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
Antoine Pitrou <solipsis@pitrou.net>
parents: 74075
diff changeset
2381
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
2382 #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
2383 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
2384 _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
2385 {
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
2386 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
2387 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
2388 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
2389 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
2390 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
2391 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
2392 /* 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
2393 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
2394 PyGILState_STATE gstate;
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
2395 const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
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
2396
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
2397 gstate = PyGILState_Ensure();
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
2398
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
2399 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
2400 /* 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
2401 * callback is in progress */
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
2402 PyGILState_Release(gstate);
81299
52b4d9bfc9ea Fix returning uninitialized variable (issue #8109).
Antoine Pitrou <solipsis@pitrou.net>
parents: 81298
diff changeset
2403 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
2404 }
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
2405
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
2406 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
2407 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
2408 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
2409 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
2410 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
2411 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
2412 }
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
2413
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
2414 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
2415 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
2416 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
2417 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
2418 }
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_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
2420 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
2421 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
2422 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
2423 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
2424 }
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 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
2426 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
2427 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
2428 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
2429 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
2430
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
2431 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
2432 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
2433 *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
2434 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
2435 }
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 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
2437 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
2438 *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
2439 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
2440 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
2441 *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
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 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
2444 }
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 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
2446 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
2447 }
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 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
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 PyGILState_Release(gstate);
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 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
2453
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 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 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
2456 *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
2457 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
2458 PyGILState_Release(gstate);
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 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
2460 }
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 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
2463 "set_servername_callback(method)\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
2464 \
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 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
2466 the SSL/TLS client in the SNI extension.\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
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 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
2469 with the SSLSocket, the server name as a string, and the SSLContext object.\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
2470 See RFC 6066 for details of the SNI");
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 #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
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 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
2474 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
2475 {
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 #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
2477 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
2478
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 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
2480 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
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 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
2483 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
2484 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
2485 }
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 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
2487 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
2488 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
2489 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
2490 "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
2491 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
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 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
2494 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
2495 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
2496 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
2497 }
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 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
2499 #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
2500 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
2501 "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
2502 "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
2503 "is not in the current OpenSSL library.");
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 #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
2505 }
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
2506
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2507 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
2508 {"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
2509 (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
2510 {"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
2511 (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
2512 {NULL}, /* sentinel */
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2513 };
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2514
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2515 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
2516 {"_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
2517 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
2518 {"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
2519 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
2520 {"_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
2521 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
2522 {"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
2523 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
2524 {"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
2525 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
2526 {"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
2527 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
2528 {"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
2529 METH_NOARGS, NULL},
66217
7826d907b765 Issue #10443: Add the SSLContext.set_default_verify_paths() method.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66114
diff changeset
2530 {"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
2531 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
2532 #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
2533 {"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
2534 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
2535 #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
2536 {"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
2537 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
2538 {NULL, NULL} /* sentinel */
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2539 };
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2540
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2541 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
2542 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
2543 "_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
2544 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
2545 0, /*tp_itemsize*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2546 (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
2547 0, /*tp_print*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2548 0, /*tp_getattr*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2549 0, /*tp_setattr*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2550 0, /*tp_reserved*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2551 0, /*tp_repr*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2552 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
2553 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
2554 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
2555 0, /*tp_hash*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2556 0, /*tp_call*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2557 0, /*tp_str*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2558 0, /*tp_getattro*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2559 0, /*tp_setattro*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2560 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
2561 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
2562 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
2563 (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
2564 (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
2565 0, /*tp_richcompare*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2566 0, /*tp_weaklistoffset*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2567 0, /*tp_iter*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2568 0, /*tp_iternext*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2569 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
2570 0, /*tp_members*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2571 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
2572 0, /*tp_base*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2573 0, /*tp_dict*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2574 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
2575 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
2576 0, /*tp_dictoffset*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2577 0, /*tp_init*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2578 0, /*tp_alloc*/
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2579 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
2580 };
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
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2584 #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
2585
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2586 /* 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
2587 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
2588 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
2589 {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2590 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
2591 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
2592 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
2593
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2594 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
2595 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
2596 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
2597 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
2598 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
2599 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2600
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
2601 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
2602 "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
2603 \n\
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2604 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
2605 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
2606
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2607 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
2608 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
2609 {
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2610 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
2611 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
2612 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
2613 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
2614 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
2615
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2616 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
2617 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
2618 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
2619 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
2620 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
2621 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
2622 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
2623 }
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2624 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
2625 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
2626 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
2627 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
2628 }
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2629 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
2630
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2631 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
2632 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
2633 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
2634 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
2635 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
2636 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
2637 }
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2638 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
2639 }
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2640
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2641 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
2642 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
2643 {
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2644 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
2645 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
2646 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
2647 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
2648 }
5c716437a83a Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69999
diff changeset
2649
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 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
2651 "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
2652 \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
2653 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
2654
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 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
2656 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
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 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
2659 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
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 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
2662 }
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
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 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
2665 "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
2666 \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
2667 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
2668 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
2669
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 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
2671 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
2672 {
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
2673 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
2674 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2675
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
2676 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
2677 "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
2678 \n\
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
2679 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
2680 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
2681 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
2682
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2683 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
2684 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
2685 {
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
2686 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
2687 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
2688
78983
827bb0554f1f Closes #15793: Stack corruption in ssl.RAND_egd()
Jesus Cea <jcea@jcea.es>
parents: 78604
diff changeset
2689 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
2690 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
2691 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
2692
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
2693 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
2694 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
2695 if (bytes == -1) {
61537
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
2696 PyErr_SetString(PySSLErrorObject,
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
2697 "EGD connection failed or EGD did not return "
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
2698 "enough data to seed the PRNG");
330273ec6028 Merged revisions 81116 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61246
diff changeset
2699 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
2700 }
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
2701 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
2702 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2703
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
2704 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
2705 "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
2706 \n\
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2707 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
2708 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
2709 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
2710
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2711 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2712
48681
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
2713
3c8ef3466923 clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents: 48660
diff changeset
2714
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2715 /* 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
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 static PyMethodDef PySSL_methods[] = {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2718 {"_test_decode_cert", PySSL_test_decode_certificate,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2719 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
2720 #ifdef HAVE_OPENSSL_RAND
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2721 {"RAND_add", PySSL_RAND_add, METH_VARARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2722 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
2723 {"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
2724 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
2725 {"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
2726 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
2727 {"RAND_egd", PySSL_RAND_egd, METH_VARARGS,
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2728 PySSL_RAND_egd_doc},
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2729 {"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2730 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
2731 #endif
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2732 {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
2733 };
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2734
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2735
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2736 #ifdef WITH_THREAD
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2737
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2738 /* 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
2739 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
2740
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2741 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
2742
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2743 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
2744 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
2745 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2746
43869
b59825d9db8f get SSL support to work again
Bill Janssen <janssen@parc.com>
parents: 43400
diff changeset
2747 static void _ssl_thread_locking_function
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2748 (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
2749 /* 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
2750 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
2751 structures that will be implicitly shared whenever multiple
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2752 threads use OpenSSL.) Multi-threaded applications will
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2753 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
2754
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2755 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
2756 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
2757 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
2758
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2759 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
2760 lock. They can be useful for debugging.
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2761 */
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2762
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2763 if ((_ssl_locks == NULL) ||
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2764 (n < 0) || ((unsigned)n >= _ssl_locks_count))
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2765 return;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2766
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2767 if (mode & CRYPTO_LOCK) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2768 PyThread_acquire_lock(_ssl_locks[n], 1);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2769 } else {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2770 PyThread_release_lock(_ssl_locks[n]);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2771 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2772 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2773
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2774 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
2775
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2776 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
2777
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2778 if (_ssl_locks == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2779 _ssl_locks_count = CRYPTO_num_locks();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2780 _ssl_locks = (PyThread_type_lock *)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2781 malloc(sizeof(PyThread_type_lock) * _ssl_locks_count);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2782 if (_ssl_locks == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2783 return 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2784 memset(_ssl_locks, 0,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2785 sizeof(PyThread_type_lock) * _ssl_locks_count);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2786 for (i = 0; i < _ssl_locks_count; i++) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2787 _ssl_locks[i] = PyThread_allocate_lock();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2788 if (_ssl_locks[i] == NULL) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2789 unsigned int j;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2790 for (j = 0; j < i; j++) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2791 PyThread_free_lock(_ssl_locks[j]);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2792 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2793 free(_ssl_locks);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2794 return 0;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2795 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2796 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2797 CRYPTO_set_locking_callback(_ssl_thread_locking_function);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2798 CRYPTO_set_id_callback(_ssl_thread_id_function);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2799 }
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2800 return 1;
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2801 }
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2802
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2803 #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
2804
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
2805 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
2806 "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
2807 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
2808
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
2809
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
2810 static struct PyModuleDef _sslmodule = {
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2811 PyModuleDef_HEAD_INIT,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2812 "_ssl",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2813 module_doc,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2814 -1,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2815 PySSL_methods,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2816 NULL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2817 NULL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2818 NULL,
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2819 NULL
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
2820 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
2821
71255
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2822
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2823 static void
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2824 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
2825 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
2826 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
2827 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
2828 {
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2829 *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
2830 libver >>= 4;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2831 *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
2832 libver >>= 8;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2833 *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
2834 libver >>= 8;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2835 *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
2836 libver >>= 8;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2837 *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
2838 }
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
2839
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24398
diff changeset
2840 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
2841 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
2842 {
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
2843 PyObject *m, *d, *r;
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
2844 unsigned long libver;
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
2845 unsigned int major, minor, fix, patch, status;
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2846 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
2847 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
2848 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
2849
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2850 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
2851 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2852 if (PyType_Ready(&PySSLSocket_Type) < 0)
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2853 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
2854
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2855 m = PyModule_Create(&_sslmodule);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2856 if (m == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2857 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2858 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
2859
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2860 /* Load _socket module and its C API */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2861 socket_api = PySocketModule_ImportModuleAndAPI();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2862 if (!socket_api)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2863 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2864 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
2865
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2866 /* Init OpenSSL */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2867 SSL_load_error_strings();
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2868 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
2869 #ifdef WITH_THREAD
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2870 /* 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
2871 if (!_setup_ssl_threads()) {
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2872 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2873 }
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43292
diff changeset
2874 #endif
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2875 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
2876
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2877 /* 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
2878 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
2879 PySSLErrorObject = PyType_FromSpec(&sslerror_type_spec);
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2880 if (PySSLErrorObject == NULL)
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2881 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
2882
73150
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2883 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
2884 "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
2885 PySSLErrorObject, NULL);
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2886 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
2887 "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
2888 PySSLErrorObject, NULL);
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2889 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
2890 "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
2891 PySSLErrorObject, NULL);
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2892 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
2893 "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
2894 PySSLErrorObject, NULL);
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2895 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
2896 "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
2897 PySSLErrorObject, NULL);
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2898 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
2899 || PySSLWantReadErrorObject == NULL
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2900 || PySSLWantWriteErrorObject == NULL
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2901 || PySSLSyscallErrorObject == NULL
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2902 || PySSLEOFErrorObject == NULL)
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2903 return NULL;
2c4a9c778bb3 Issue #11183: Add finer-grained exceptions to the ssl module, so that
Antoine Pitrou <solipsis@pitrou.net>
parents: 73059
diff changeset
2904 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
2905 || 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
2906 || 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
2907 || 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
2908 || 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
2909 || PyDict_SetItemString(d, "SSLEOFError", PySSLEOFErrorObject) != 0)
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2910 return NULL;
61635
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2911 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
2912 (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
2913 return NULL;
4bb03cc1d421 Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61535
diff changeset
2914 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
2915 (PyObject *)&PySSLSocket_Type) != 0)
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2916 return NULL;
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2917 PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2918 PY_SSL_ERROR_ZERO_RETURN);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2919 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2920 PY_SSL_ERROR_WANT_READ);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2921 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2922 PY_SSL_ERROR_WANT_WRITE);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2923 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2924 PY_SSL_ERROR_WANT_X509_LOOKUP);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2925 PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2926 PY_SSL_ERROR_SYSCALL);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2927 PyModule_AddIntConstant(m, "SSL_ERROR_SSL",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2928 PY_SSL_ERROR_SSL);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2929 PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2930 PY_SSL_ERROR_WANT_CONNECT);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2931 /* non ssl.h errorcodes */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2932 PyModule_AddIntConstant(m, "SSL_ERROR_EOF",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2933 PY_SSL_ERROR_EOF);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2934 PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2935 PY_SSL_ERROR_INVALID_ERROR_CODE);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2936 /* cert requirements */
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2937 PyModule_AddIntConstant(m, "CERT_NONE",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2938 PY_SSL_CERT_NONE);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2939 PyModule_AddIntConstant(m, "CERT_OPTIONAL",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2940 PY_SSL_CERT_OPTIONAL);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2941 PyModule_AddIntConstant(m, "CERT_REQUIRED",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2942 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
2943
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
2944 /* 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
2945 /* 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
2946 /* 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
2947
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
2948 #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
2949 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
2950 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
2951
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
2952 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
2953 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
2954 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
2955 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
2956 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
2957 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
2958 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
2959 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
2960 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
2961 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
2962 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
2963 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
2964 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
2965 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
2966 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
2967 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
2968 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
2969 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
2970 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
2971 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
2972 ADD_AD_CONSTANT(NO_RENEGOTIATION);
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
2973 ADD_AD_CONSTANT(UNSUPPORTED_EXTENSION);
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
2974 ADD_AD_CONSTANT(CERTIFICATE_UNOBTAINABLE);
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
2975 ADD_AD_CONSTANT(UNRECOGNIZED_NAME);
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
2976 /* Not all constants are in old OpenSSL versions */
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
2977 #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
2978 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
2979 #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
2980 #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
2981 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
2982 #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
2983 #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
2984 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
2985 #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
2986
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 #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
2988
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2989 /* protocol versions */
69997
b7abf0590e1c Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66806
diff changeset
2990 #ifndef OPENSSL_NO_SSL2
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2991 PyModule_AddIntConstant(m, "PROTOCOL_SSLv2",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2992 PY_SSL_VERSION_SSL2);
69997
b7abf0590e1c Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66806
diff changeset
2993 #endif
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2994 PyModule_AddIntConstant(m, "PROTOCOL_SSLv3",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2995 PY_SSL_VERSION_SSL3);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2996 PyModule_AddIntConstant(m, "PROTOCOL_SSLv23",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2997 PY_SSL_VERSION_SSL23);
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2998 PyModule_AddIntConstant(m, "PROTOCOL_TLSv1",
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
2999 PY_SSL_VERSION_TLS1);
60373
9e5e9d8fa847 Merged revisions 79812 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60034
diff changeset
3000
61782
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
3001 /* 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
3002 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
3003 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
3004 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
3005 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
3006 PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1);
74075
c706f76c9ea8 Issue #13635: Add ssl.OP_CIPHER_SERVER_PREFERENCE, so that SSL servers
Antoine Pitrou <solipsis@pitrou.net>
parents: 73712
diff changeset
3007 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
3008 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
3009 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
3010 #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
3011 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
3012 #endif
74091
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
3013 #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
3014 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
3015 SSL_OP_NO_COMPRESSION);
20b52be99b5d Issue #13634: Add support for querying and disabling SSL compression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74084
diff changeset
3016 #endif
61782
5ee47f0b26b0 Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents: 61644
diff changeset
3017
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3018 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3019 r = Py_True;
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3020 #else
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3021 r = Py_False;
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3022 #endif
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3023 Py_INCREF(r);
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65267
diff changeset
3024 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
3025
71440
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
3026 #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
3027 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
3028 #else
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
3029 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
3030 #endif
cb44fef5ea1d Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
Antoine Pitrou <solipsis@pitrou.net>
parents: 71256
diff changeset
3031 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
3032 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
3033
74103
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
3034 #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
3035 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
3036 #else
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
3037 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
3038 #endif
ec44f2e82707 Fix ssl module compilation if ECDH support was disabled in the OpenSSL build.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74091
diff changeset
3039 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
3040 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
3041
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
3042 #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
3043 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
3044 #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
3045 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
3046 #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
3047 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
3048 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
3049
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
3050 /* 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
3051 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
3052 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
3053 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
3054 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
3055 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
3056 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
3057 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
3058 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
3059 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
3060 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
3061 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
3062 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
3063 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
3064 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
3065 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
3066 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
3067 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
3068 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
3069 }
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
3070 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
3071 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
3072 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
3073 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
3074
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
3075 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
3076 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
3077 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
3078 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
3079 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
3080 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
3081 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
3082 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
3083 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
3084 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
3085 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
3086 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
3087 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
3088 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
3089 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
3090 }
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
3091 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
3092 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
3093
61244
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3094 /* OpenSSL version */
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3095 /* 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
3096 which could be different from the headers version.
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3097 */
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3098 libver = SSLeay();
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3099 r = PyLong_FromUnsignedLong(libver);
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3100 if (r == NULL)
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3101 return NULL;
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3102 if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r))
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3103 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
3104 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
3105 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
3106 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
3107 return NULL;
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3108 r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION));
35221c0158b5 Merged revisions 80789 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61017
diff changeset
3109 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
3110 return NULL;
60373
9e5e9d8fa847 Merged revisions 79812 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60034
diff changeset
3111
71255
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
3112 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
3113 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
3114 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
3115 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
3116 return NULL;
52ed0c6bb461 Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents: 69998
diff changeset
3117
61246
50a4ec37361d Merged revisions 80790 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61018
diff changeset
3118 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
3119 }