annotate Modules/_ssl.c @ 83447:678564fc0962 3.3

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