Mercurial > cpython-lru_cache-weakref
annotate Lib/ssl.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 25c2d24e1b11 |
| children | f475332df9b5 ace54f5e75d7 |
| rev | line source |
|---|---|
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
1 # Wrapper module for _ssl, providing some additional facilities |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
2 # implemented in Python. Written by Bill Janssen. |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
3 |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
4 """This module provides some more Pythonic support for SSL. |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
5 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
6 Object types: |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
7 |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
8 SSLSocket -- subtype of socket.socket which does SSL over the socket |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
9 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
10 Exceptions: |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
11 |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
12 SSLError -- exception raised for I/O errors |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
13 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
14 Functions: |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
15 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
16 cert_time_to_seconds -- convert time string used for certificate |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
17 notBefore and notAfter functions to integer |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
18 seconds past the Epoch (the time values |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
19 returned from time.time()) |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
20 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
21 fetch_server_certificate (HOST, PORT) -- fetch the certificate provided |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
22 by the server running on HOST at port PORT. No |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
23 validation of the certificate is performed. |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
24 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
25 Integer constants: |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
26 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
27 SSL_ERROR_ZERO_RETURN |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
28 SSL_ERROR_WANT_READ |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
29 SSL_ERROR_WANT_WRITE |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
30 SSL_ERROR_WANT_X509_LOOKUP |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
31 SSL_ERROR_SYSCALL |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
32 SSL_ERROR_SSL |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
33 SSL_ERROR_WANT_CONNECT |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
34 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
35 SSL_ERROR_EOF |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
36 SSL_ERROR_INVALID_ERROR_CODE |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
37 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
38 The following group define certificate requirements that one side is |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
39 allowing/requiring from the other side: |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
40 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
41 CERT_NONE - no certificates from the other side are required (or will |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
42 be looked at if provided) |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
43 CERT_OPTIONAL - certificates are not required, but if provided will be |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
44 validated, and if validation fails, the connection will |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
45 also fail |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
46 CERT_REQUIRED - certificates are required, and will be validated, and |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
47 if validation fails, the connection will also fail |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
48 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
49 The following constants identify various SSL protocol variants: |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
50 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
51 PROTOCOL_SSLv2 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
52 PROTOCOL_SSLv3 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
53 PROTOCOL_SSLv23 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
54 PROTOCOL_TLSv1 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
55 """ |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
56 |
|
45471
7dd2c1746267
Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44305
diff
changeset
|
57 import textwrap |
|
65179
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
58 import re |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
59 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
60 import _ssl # if we can't import it, let the error propagate |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
61 |
|
60373
9e5e9d8fa847
Merged revisions 79812 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59906
diff
changeset
|
62 from _ssl import OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_INFO, OPENSSL_VERSION |
|
61635
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
63 from _ssl import _SSLContext, SSLError |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
64 from _ssl import CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED |
|
61782
5ee47f0b26b0
Issue #4870: Add an `options` attribute to SSL contexts, as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
61635
diff
changeset
|
65 from _ssl import OP_ALL, OP_NO_SSLv2, OP_NO_SSLv3, OP_NO_TLSv1 |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
66 from _ssl import RAND_status, RAND_egd, RAND_add |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
67 from _ssl import ( |
|
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
68 SSL_ERROR_ZERO_RETURN, |
|
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
69 SSL_ERROR_WANT_READ, |
|
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
70 SSL_ERROR_WANT_WRITE, |
|
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
71 SSL_ERROR_WANT_X509_LOOKUP, |
|
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
72 SSL_ERROR_SYSCALL, |
|
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
73 SSL_ERROR_SSL, |
|
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
74 SSL_ERROR_WANT_CONNECT, |
|
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
75 SSL_ERROR_EOF, |
|
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
76 SSL_ERROR_INVALID_ERROR_CODE, |
|
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
77 ) |
|
65593
846c0e1342d0
Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents:
65179
diff
changeset
|
78 from _ssl import HAS_SNI |
|
69997
b7abf0590e1c
Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64743
diff
changeset
|
79 from _ssl import PROTOCOL_SSLv3, PROTOCOL_SSLv23, PROTOCOL_TLSv1 |
|
71255
52ed0c6bb461
Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents:
70187
diff
changeset
|
80 from _ssl import _OPENSSL_API_VERSION |
|
52ed0c6bb461
Issue #12440: When testing whether some bits in SSLContext.options can be
Antoine Pitrou <solipsis@pitrou.net>
parents:
70187
diff
changeset
|
81 |
|
69997
b7abf0590e1c
Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64743
diff
changeset
|
82 _PROTOCOL_NAMES = { |
|
b7abf0590e1c
Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64743
diff
changeset
|
83 PROTOCOL_TLSv1: "TLSv1", |
|
b7abf0590e1c
Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64743
diff
changeset
|
84 PROTOCOL_SSLv23: "SSLv23", |
|
b7abf0590e1c
Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64743
diff
changeset
|
85 PROTOCOL_SSLv3: "SSLv3", |
|
b7abf0590e1c
Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64743
diff
changeset
|
86 } |
|
b7abf0590e1c
Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64743
diff
changeset
|
87 try: |
|
b7abf0590e1c
Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64743
diff
changeset
|
88 from _ssl import PROTOCOL_SSLv2 |
|
74248
25c2d24e1b11
Issue #13636: Weak ciphers are now disabled by default in the ssl module
Antoine Pitrou <solipsis@pitrou.net>
parents:
71255
diff
changeset
|
89 _SSLv2_IF_EXISTS = PROTOCOL_SSLv2 |
|
69997
b7abf0590e1c
Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64743
diff
changeset
|
90 except ImportError: |
|
74248
25c2d24e1b11
Issue #13636: Weak ciphers are now disabled by default in the ssl module
Antoine Pitrou <solipsis@pitrou.net>
parents:
71255
diff
changeset
|
91 _SSLv2_IF_EXISTS = None |
|
69997
b7abf0590e1c
Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64743
diff
changeset
|
92 else: |
|
b7abf0590e1c
Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64743
diff
changeset
|
93 _PROTOCOL_NAMES[PROTOCOL_SSLv2] = "SSLv2" |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
94 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
95 from socket import getnameinfo as _getnameinfo |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
96 from socket import error as socket_error |
|
48681
3c8ef3466923
clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents:
48383
diff
changeset
|
97 from socket import socket, AF_INET, SOCK_STREAM |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
98 import base64 # for DER-to-PEM translation |
|
44305
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
99 import traceback |
|
60988
b877d2d04c14
Merged revisions 80509 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60938
diff
changeset
|
100 import errno |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
101 |
|
74248
25c2d24e1b11
Issue #13636: Weak ciphers are now disabled by default in the ssl module
Antoine Pitrou <solipsis@pitrou.net>
parents:
71255
diff
changeset
|
102 # Disable weak or insecure ciphers by default |
|
25c2d24e1b11
Issue #13636: Weak ciphers are now disabled by default in the ssl module
Antoine Pitrou <solipsis@pitrou.net>
parents:
71255
diff
changeset
|
103 # (OpenSSL's default setting is 'DEFAULT:!aNULL:!eNULL') |
|
25c2d24e1b11
Issue #13636: Weak ciphers are now disabled by default in the ssl module
Antoine Pitrou <solipsis@pitrou.net>
parents:
71255
diff
changeset
|
104 _DEFAULT_CIPHERS = 'DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2' |
|
25c2d24e1b11
Issue #13636: Weak ciphers are now disabled by default in the ssl module
Antoine Pitrou <solipsis@pitrou.net>
parents:
71255
diff
changeset
|
105 |
|
61635
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
106 |
|
65179
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
107 class CertificateError(ValueError): |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
108 pass |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
109 |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
110 |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
111 def _dnsname_to_pat(dn): |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
112 pats = [] |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
113 for frag in dn.split(r'.'): |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
114 if frag == '*': |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
115 # When '*' is a fragment by itself, it matches a non-empty dotless |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
116 # fragment. |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
117 pats.append('[^.]+') |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
118 else: |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
119 # Otherwise, '*' matches any dotless fragment. |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
120 frag = re.escape(frag) |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
121 pats.append(frag.replace(r'\*', '[^.]*')) |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
122 return re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE) |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
123 |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
124 |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
125 def match_hostname(cert, hostname): |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
126 """Verify that *cert* (in decoded format as returned by |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
127 SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 rules |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
128 are mostly followed, but IP addresses are not accepted for *hostname*. |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
129 |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
130 CertificateError is raised on failure. On success, the function |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
131 returns nothing. |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
132 """ |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
133 if not cert: |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
134 raise ValueError("empty or no certificate") |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
135 dnsnames = [] |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
136 san = cert.get('subjectAltName', ()) |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
137 for key, value in san: |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
138 if key == 'DNS': |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
139 if _dnsname_to_pat(value).match(hostname): |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
140 return |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
141 dnsnames.append(value) |
|
69874
d4c2a99d1bad
Issue #12000: When a SSL certificate has a subjectAltName without any
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
142 if not dnsnames: |
|
d4c2a99d1bad
Issue #12000: When a SSL certificate has a subjectAltName without any
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
143 # The subject is only checked when there is no dNSName entry |
|
d4c2a99d1bad
Issue #12000: When a SSL certificate has a subjectAltName without any
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
144 # in subjectAltName |
|
65179
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
145 for sub in cert.get('subject', ()): |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
146 for key, value in sub: |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
147 # XXX according to RFC 2818, the most specific Common Name |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
148 # must be used. |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
149 if key == 'commonName': |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
150 if _dnsname_to_pat(value).match(hostname): |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
151 return |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
152 dnsnames.append(value) |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
153 if len(dnsnames) > 1: |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
154 raise CertificateError("hostname %r " |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
155 "doesn't match either of %s" |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
156 % (hostname, ', '.join(map(repr, dnsnames)))) |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
157 elif len(dnsnames) == 1: |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
158 raise CertificateError("hostname %r " |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
159 "doesn't match %r" |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
160 % (hostname, dnsnames[0])) |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
161 else: |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
162 raise CertificateError("no appropriate commonName or " |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
163 "subjectAltName fields were found") |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
164 |
|
4c98eeff37e7
Issue #1589: Add ssl.match_hostname(), to help implement server identity
Antoine Pitrou <solipsis@pitrou.net>
parents:
64741
diff
changeset
|
165 |
|
61635
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
166 class SSLContext(_SSLContext): |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
167 """An SSLContext holds various SSL-related configuration options and |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
168 data, such as certificates and possibly a private key.""" |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
169 |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
170 __slots__ = ('protocol',) |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
171 |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
172 def __new__(cls, protocol, *args, **kwargs): |
|
74248
25c2d24e1b11
Issue #13636: Weak ciphers are now disabled by default in the ssl module
Antoine Pitrou <solipsis@pitrou.net>
parents:
71255
diff
changeset
|
173 self = _SSLContext.__new__(cls, protocol) |
|
25c2d24e1b11
Issue #13636: Weak ciphers are now disabled by default in the ssl module
Antoine Pitrou <solipsis@pitrou.net>
parents:
71255
diff
changeset
|
174 if protocol != _SSLv2_IF_EXISTS: |
|
25c2d24e1b11
Issue #13636: Weak ciphers are now disabled by default in the ssl module
Antoine Pitrou <solipsis@pitrou.net>
parents:
71255
diff
changeset
|
175 self.set_ciphers(_DEFAULT_CIPHERS) |
|
25c2d24e1b11
Issue #13636: Weak ciphers are now disabled by default in the ssl module
Antoine Pitrou <solipsis@pitrou.net>
parents:
71255
diff
changeset
|
176 return self |
|
61635
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
177 |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
178 def __init__(self, protocol): |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
179 self.protocol = protocol |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
180 |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
181 def wrap_socket(self, sock, server_side=False, |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
182 do_handshake_on_connect=True, |
|
65593
846c0e1342d0
Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents:
65179
diff
changeset
|
183 suppress_ragged_eofs=True, |
|
846c0e1342d0
Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents:
65179
diff
changeset
|
184 server_hostname=None): |
|
61635
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
185 return SSLSocket(sock=sock, server_side=server_side, |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
186 do_handshake_on_connect=do_handshake_on_connect, |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
187 suppress_ragged_eofs=suppress_ragged_eofs, |
|
65593
846c0e1342d0
Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents:
65179
diff
changeset
|
188 server_hostname=server_hostname, |
|
61635
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
189 _context=self) |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
190 |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
191 |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
192 class SSLSocket(socket): |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
193 """This class implements a subtype of socket.socket that wraps |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
194 the underlying OS socket in an SSL context when necessary, and |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
195 provides read and write methods over that channel.""" |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
196 |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
197 def __init__(self, sock=None, keyfile=None, certfile=None, |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
198 server_side=False, cert_reqs=CERT_NONE, |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
199 ssl_version=PROTOCOL_SSLv23, ca_certs=None, |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
200 do_handshake_on_connect=True, |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
201 family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None, |
|
61635
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
202 suppress_ragged_eofs=True, ciphers=None, |
|
65593
846c0e1342d0
Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents:
65179
diff
changeset
|
203 server_hostname=None, |
|
61635
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
204 _context=None): |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
205 |
|
61635
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
206 if _context: |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
207 self.context = _context |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
208 else: |
|
64306
9297974604ff
Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64305
diff
changeset
|
209 if server_side and not certfile: |
|
9297974604ff
Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64305
diff
changeset
|
210 raise ValueError("certfile must be specified for server-side " |
|
9297974604ff
Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64305
diff
changeset
|
211 "operations") |
|
64324
84883607e21a
Fix issue #9711: raise ValueError is SSLConnection constructor is invoked with keyfile and not certfile.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64306
diff
changeset
|
212 if keyfile and not certfile: |
|
84883607e21a
Fix issue #9711: raise ValueError is SSLConnection constructor is invoked with keyfile and not certfile.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64306
diff
changeset
|
213 raise ValueError("certfile must be specified") |
|
61635
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
214 if certfile and not keyfile: |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
215 keyfile = certfile |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
216 self.context = SSLContext(ssl_version) |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
217 self.context.verify_mode = cert_reqs |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
218 if ca_certs: |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
219 self.context.load_verify_locations(ca_certs) |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
220 if certfile: |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
221 self.context.load_cert_chain(certfile, keyfile) |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
222 if ciphers: |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
223 self.context.set_ciphers(ciphers) |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
224 self.keyfile = keyfile |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
225 self.certfile = certfile |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
226 self.cert_reqs = cert_reqs |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
227 self.ssl_version = ssl_version |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
228 self.ca_certs = ca_certs |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
229 self.ciphers = ciphers |
|
65593
846c0e1342d0
Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents:
65179
diff
changeset
|
230 if server_side and server_hostname: |
|
846c0e1342d0
Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents:
65179
diff
changeset
|
231 raise ValueError("server_hostname can only be specified " |
|
846c0e1342d0
Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents:
65179
diff
changeset
|
232 "in client mode") |
|
64306
9297974604ff
Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64305
diff
changeset
|
233 self.server_side = server_side |
|
65593
846c0e1342d0
Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents:
65179
diff
changeset
|
234 self.server_hostname = server_hostname |
|
61635
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
235 self.do_handshake_on_connect = do_handshake_on_connect |
|
4bb03cc1d421
Issue #8550: Add first class `SSLContext` objects to the ssl module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60992
diff
changeset
|
236 self.suppress_ragged_eofs = suppress_ragged_eofs |
|
60991
d5590a987cb0
Merged revisions 80515 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60988
diff
changeset
|
237 connected = False |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
238 if sock is not None: |
|
44305
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
239 socket.__init__(self, |
|
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
240 family=sock.family, |
|
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
241 type=sock.type, |
|
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
242 proto=sock.proto, |
|
63856
3f8cef837f07
Issue #8524: Add a forget() method to socket objects, so as to put the
Antoine Pitrou <solipsis@pitrou.net>
parents:
61782
diff
changeset
|
243 fileno=sock.fileno()) |
|
60938
0ff8adde609c
Merged revisions 80456 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59907
diff
changeset
|
244 self.settimeout(sock.gettimeout()) |
|
60991
d5590a987cb0
Merged revisions 80515 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60988
diff
changeset
|
245 # see if it's connected |
|
d5590a987cb0
Merged revisions 80515 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60988
diff
changeset
|
246 try: |
|
d5590a987cb0
Merged revisions 80515 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60988
diff
changeset
|
247 sock.getpeername() |
|
d5590a987cb0
Merged revisions 80515 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60988
diff
changeset
|
248 except socket_error as e: |
|
d5590a987cb0
Merged revisions 80515 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60988
diff
changeset
|
249 if e.errno != errno.ENOTCONN: |
|
d5590a987cb0
Merged revisions 80515 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60988
diff
changeset
|
250 raise |
|
d5590a987cb0
Merged revisions 80515 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60988
diff
changeset
|
251 else: |
|
d5590a987cb0
Merged revisions 80515 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60988
diff
changeset
|
252 connected = True |
|
63890
60076f28c023
Followup to r83869 and issue #8524: rename socket.forget() to socket.detach()
Antoine Pitrou <solipsis@pitrou.net>
parents:
63856
diff
changeset
|
253 sock.detach() |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
254 elif fileno is not None: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
255 socket.__init__(self, fileno=fileno) |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
256 else: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
257 socket.__init__(self, family=family, type=type, proto=proto) |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
258 |
|
60991
d5590a987cb0
Merged revisions 80515 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60988
diff
changeset
|
259 self._closed = False |
|
d5590a987cb0
Merged revisions 80515 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60988
diff
changeset
|
260 self._sslobj = None |
|
68186
1909c12ef06c
Issue #11326: Add the missing connect_ex() implementation for SSL sockets,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65593
diff
changeset
|
261 self._connected = connected |
|
60991
d5590a987cb0
Merged revisions 80515 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60988
diff
changeset
|
262 if connected: |
|
d5590a987cb0
Merged revisions 80515 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60988
diff
changeset
|
263 # create the SSL object |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
264 try: |
|
65593
846c0e1342d0
Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents:
65179
diff
changeset
|
265 self._sslobj = self.context._wrap_socket(self, server_side, |
|
846c0e1342d0
Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents:
65179
diff
changeset
|
266 server_hostname) |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
267 if do_handshake_on_connect: |
|
44161
1a7dfb61b4d8
most recent changes to SSL module to support non-blocking sockets properly
Bill Janssen <janssen@parc.com>
parents:
43882
diff
changeset
|
268 timeout = self.gettimeout() |
|
1a7dfb61b4d8
most recent changes to SSL module to support non-blocking sockets properly
Bill Janssen <janssen@parc.com>
parents:
43882
diff
changeset
|
269 if timeout == 0.0: |
|
1a7dfb61b4d8
most recent changes to SSL module to support non-blocking sockets properly
Bill Janssen <janssen@parc.com>
parents:
43882
diff
changeset
|
270 # non-blocking |
|
1a7dfb61b4d8
most recent changes to SSL module to support non-blocking sockets properly
Bill Janssen <janssen@parc.com>
parents:
43882
diff
changeset
|
271 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets") |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
272 self.do_handshake() |
|
44161
1a7dfb61b4d8
most recent changes to SSL module to support non-blocking sockets properly
Bill Janssen <janssen@parc.com>
parents:
43882
diff
changeset
|
273 |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
274 except socket_error as x: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
275 self.close() |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
276 raise x |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
277 |
|
43882
f6cca4b2260c
Disable dup() of SSLSocket. I don't think it can be made to work.
Guido van Rossum <guido@python.org>
parents:
43881
diff
changeset
|
278 def dup(self): |
|
f6cca4b2260c
Disable dup() of SSLSocket. I don't think it can be made to work.
Guido van Rossum <guido@python.org>
parents:
43881
diff
changeset
|
279 raise NotImplemented("Can't dup() %s instances" % |
|
f6cca4b2260c
Disable dup() of SSLSocket. I don't think it can be made to work.
Guido van Rossum <guido@python.org>
parents:
43881
diff
changeset
|
280 self.__class__.__name__) |
|
f6cca4b2260c
Disable dup() of SSLSocket. I don't think it can be made to work.
Guido van Rossum <guido@python.org>
parents:
43881
diff
changeset
|
281 |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
282 def _checkClosed(self, msg=None): |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
283 # raise an exception here if you wish to check for spurious closes |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
284 pass |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
285 |
|
44305
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
286 def read(self, len=0, buffer=None): |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
287 """Read up to LEN bytes and return them. |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
288 Return zero-length string on EOF.""" |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
289 |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
290 self._checkClosed() |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
291 try: |
|
64404
988718e9f93d
Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60993
diff
changeset
|
292 if buffer is not None: |
|
988718e9f93d
Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60993
diff
changeset
|
293 v = self._sslobj.read(len, buffer) |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
294 else: |
|
44305
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
295 v = self._sslobj.read(len or 1024) |
|
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
296 return v |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
297 except SSLError as x: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
298 if x.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs: |
|
64404
988718e9f93d
Merged revisions 84464 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60993
diff
changeset
|
299 if buffer is not None: |
|
44305
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
300 return 0 |
|
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
301 else: |
|
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
302 return b'' |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
303 else: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
304 raise |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
305 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
306 def write(self, data): |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
307 """Write DATA to the underlying SSL channel. Returns |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
308 number of bytes of DATA actually transmitted.""" |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
309 |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
310 self._checkClosed() |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
311 return self._sslobj.write(data) |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
312 |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
313 def getpeercert(self, binary_form=False): |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
314 """Returns a formatted version of the data in the |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
315 certificate provided by the other end of the SSL channel. |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
316 Return None if no certificate was provided, {} if a |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
317 certificate was provided, but not validated.""" |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
318 |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
319 self._checkClosed() |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
320 return self._sslobj.peer_certificate(binary_form) |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
321 |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
322 def cipher(self): |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
323 self._checkClosed() |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
324 if not self._sslobj: |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
325 return None |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
326 else: |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
327 return self._sslobj.cipher() |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
328 |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
329 def send(self, data, flags=0): |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
330 self._checkClosed() |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
331 if self._sslobj: |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
332 if flags != 0: |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
333 raise ValueError( |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
334 "non-zero flags not allowed in calls to send() on %s" % |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
335 self.__class__) |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
336 while True: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
337 try: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
338 v = self._sslobj.write(data) |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
339 except SSLError as x: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
340 if x.args[0] == SSL_ERROR_WANT_READ: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
341 return 0 |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
342 elif x.args[0] == SSL_ERROR_WANT_WRITE: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
343 return 0 |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
344 else: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
345 raise |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
346 else: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
347 return v |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
348 else: |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
349 return socket.send(self, data, flags) |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
350 |
|
64743
80d1c7734df2
Merged revisions 84807 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64404
diff
changeset
|
351 def sendto(self, data, flags_or_addr, addr=None): |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
352 self._checkClosed() |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
353 if self._sslobj: |
|
48383
f5f70f20b678
fix bad method names in ssl module (and typo in ssl doc)
Bill Janssen <janssen@parc.com>
parents:
45471
diff
changeset
|
354 raise ValueError("sendto not allowed on instances of %s" % |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
355 self.__class__) |
|
64743
80d1c7734df2
Merged revisions 84807 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64404
diff
changeset
|
356 elif addr is None: |
|
80d1c7734df2
Merged revisions 84807 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64404
diff
changeset
|
357 return socket.sendto(self, data, flags_or_addr) |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
358 else: |
|
64743
80d1c7734df2
Merged revisions 84807 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64404
diff
changeset
|
359 return socket.sendto(self, data, flags_or_addr, addr) |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
360 |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
361 def sendall(self, data, flags=0): |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
362 self._checkClosed() |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
363 if self._sslobj: |
|
64305
9cba99068c54
Raise ValuError if non-zero flag argument is provided for sendall() method for conformity with send(), recv() and recv_into()
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63890
diff
changeset
|
364 if flags != 0: |
|
9cba99068c54
Raise ValuError if non-zero flag argument is provided for sendall() method for conformity with send(), recv() and recv_into()
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63890
diff
changeset
|
365 raise ValueError( |
|
9cba99068c54
Raise ValuError if non-zero flag argument is provided for sendall() method for conformity with send(), recv() and recv_into()
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63890
diff
changeset
|
366 "non-zero flags not allowed in calls to sendall() on %s" % |
|
9cba99068c54
Raise ValuError if non-zero flag argument is provided for sendall() method for conformity with send(), recv() and recv_into()
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63890
diff
changeset
|
367 self.__class__) |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
368 amount = len(data) |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
369 count = 0 |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
370 while (count < amount): |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
371 v = self.send(data[count:]) |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
372 count += v |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
373 return amount |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
374 else: |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
375 return socket.sendall(self, data, flags) |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
376 |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
377 def recv(self, buflen=1024, flags=0): |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
378 self._checkClosed() |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
379 if self._sslobj: |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
380 if flags != 0: |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
381 raise ValueError( |
|
59907
9fbe2370dc02
Merged revisions 79287,79289 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58408
diff
changeset
|
382 "non-zero flags not allowed in calls to recv() on %s" % |
|
9fbe2370dc02
Merged revisions 79287,79289 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58408
diff
changeset
|
383 self.__class__) |
|
9fbe2370dc02
Merged revisions 79287,79289 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58408
diff
changeset
|
384 return self.read(buflen) |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
385 else: |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
386 return socket.recv(self, buflen, flags) |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
387 |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
388 def recv_into(self, buffer, nbytes=None, flags=0): |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
389 self._checkClosed() |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
390 if buffer and (nbytes is None): |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
391 nbytes = len(buffer) |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
392 elif nbytes is None: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
393 nbytes = 1024 |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
394 if self._sslobj: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
395 if flags != 0: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
396 raise ValueError( |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
397 "non-zero flags not allowed in calls to recv_into() on %s" % |
|
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
398 self.__class__) |
|
59907
9fbe2370dc02
Merged revisions 79287,79289 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58408
diff
changeset
|
399 return self.read(nbytes, buffer) |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
400 else: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
401 return socket.recv_into(self, buffer, nbytes, flags) |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
402 |
|
64743
80d1c7734df2
Merged revisions 84807 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64404
diff
changeset
|
403 def recvfrom(self, buflen=1024, flags=0): |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
404 self._checkClosed() |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
405 if self._sslobj: |
|
48383
f5f70f20b678
fix bad method names in ssl module (and typo in ssl doc)
Bill Janssen <janssen@parc.com>
parents:
45471
diff
changeset
|
406 raise ValueError("recvfrom not allowed on instances of %s" % |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
407 self.__class__) |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
408 else: |
|
64743
80d1c7734df2
Merged revisions 84807 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64404
diff
changeset
|
409 return socket.recvfrom(self, buflen, flags) |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
410 |
|
49090
ac3b67043361
fixes from issue 3162 for SSL module
Bill Janssen <janssen@parc.com>
parents:
48681
diff
changeset
|
411 def recvfrom_into(self, buffer, nbytes=None, flags=0): |
|
ac3b67043361
fixes from issue 3162 for SSL module
Bill Janssen <janssen@parc.com>
parents:
48681
diff
changeset
|
412 self._checkClosed() |
|
ac3b67043361
fixes from issue 3162 for SSL module
Bill Janssen <janssen@parc.com>
parents:
48681
diff
changeset
|
413 if self._sslobj: |
|
ac3b67043361
fixes from issue 3162 for SSL module
Bill Janssen <janssen@parc.com>
parents:
48681
diff
changeset
|
414 raise ValueError("recvfrom_into not allowed on instances of %s" % |
|
ac3b67043361
fixes from issue 3162 for SSL module
Bill Janssen <janssen@parc.com>
parents:
48681
diff
changeset
|
415 self.__class__) |
|
ac3b67043361
fixes from issue 3162 for SSL module
Bill Janssen <janssen@parc.com>
parents:
48681
diff
changeset
|
416 else: |
|
ac3b67043361
fixes from issue 3162 for SSL module
Bill Janssen <janssen@parc.com>
parents:
48681
diff
changeset
|
417 return socket.recvfrom_into(self, buffer, nbytes, flags) |
|
ac3b67043361
fixes from issue 3162 for SSL module
Bill Janssen <janssen@parc.com>
parents:
48681
diff
changeset
|
418 |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
419 def pending(self): |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
420 self._checkClosed() |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
421 if self._sslobj: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
422 return self._sslobj.pending() |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
423 else: |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
424 return 0 |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
425 |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
426 def shutdown(self, how): |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
427 self._checkClosed() |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
428 self._sslobj = None |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
429 socket.shutdown(self, how) |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
430 |
|
58408
121deed5968e
Merged revisions 77595 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
54568
diff
changeset
|
431 def unwrap(self): |
|
48681
3c8ef3466923
clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents:
48383
diff
changeset
|
432 if self._sslobj: |
|
3c8ef3466923
clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents:
48383
diff
changeset
|
433 s = self._sslobj.shutdown() |
|
3c8ef3466923
clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents:
48383
diff
changeset
|
434 self._sslobj = None |
|
3c8ef3466923
clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents:
48383
diff
changeset
|
435 return s |
|
3c8ef3466923
clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents:
48383
diff
changeset
|
436 else: |
|
3c8ef3466923
clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents:
48383
diff
changeset
|
437 raise ValueError("No SSL wrapper around " + str(self)) |
|
3c8ef3466923
clean up ssl.py; expose unwrap and add test for it
Bill Janssen <janssen@parc.com>
parents:
48383
diff
changeset
|
438 |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
439 def _real_close(self): |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
440 self._sslobj = None |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
441 # self._closed = True |
|
44305
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
442 socket._real_close(self) |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
443 |
|
44161
1a7dfb61b4d8
most recent changes to SSL module to support non-blocking sockets properly
Bill Janssen <janssen@parc.com>
parents:
43882
diff
changeset
|
444 def do_handshake(self, block=False): |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
445 """Perform a TLS/SSL handshake.""" |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
446 |
|
44161
1a7dfb61b4d8
most recent changes to SSL module to support non-blocking sockets properly
Bill Janssen <janssen@parc.com>
parents:
43882
diff
changeset
|
447 timeout = self.gettimeout() |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
448 try: |
|
44161
1a7dfb61b4d8
most recent changes to SSL module to support non-blocking sockets properly
Bill Janssen <janssen@parc.com>
parents:
43882
diff
changeset
|
449 if timeout == 0.0 and block: |
|
1a7dfb61b4d8
most recent changes to SSL module to support non-blocking sockets properly
Bill Janssen <janssen@parc.com>
parents:
43882
diff
changeset
|
450 self.settimeout(None) |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
451 self._sslobj.do_handshake() |
|
44161
1a7dfb61b4d8
most recent changes to SSL module to support non-blocking sockets properly
Bill Janssen <janssen@parc.com>
parents:
43882
diff
changeset
|
452 finally: |
|
1a7dfb61b4d8
most recent changes to SSL module to support non-blocking sockets properly
Bill Janssen <janssen@parc.com>
parents:
43882
diff
changeset
|
453 self.settimeout(timeout) |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
454 |
|
70187
019d8ccdf03b
Issue #12065: connect_ex() on an SSL socket now returns the original errno
Antoine Pitrou <solipsis@pitrou.net>
parents:
69998
diff
changeset
|
455 def _real_connect(self, addr, connect_ex): |
|
64306
9297974604ff
Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64305
diff
changeset
|
456 if self.server_side: |
|
9297974604ff
Fix issue issue9706: provides a better error handling for various SSL operations
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64305
diff
changeset
|
457 raise ValueError("can't connect in server-side mode") |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
458 # Here we assume that the socket is client-side, and not |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
459 # connected at the time of the call. We connect it, then wrap it. |
|
68186
1909c12ef06c
Issue #11326: Add the missing connect_ex() implementation for SSL sockets,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65593
diff
changeset
|
460 if self._connected: |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
461 raise ValueError("attempt to connect already-connected SSLSocket!") |
|
65593
846c0e1342d0
Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents:
65179
diff
changeset
|
462 self._sslobj = self.context._wrap_socket(self, False, self.server_hostname) |
|
44305
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
463 try: |
|
70187
019d8ccdf03b
Issue #12065: connect_ex() on an SSL socket now returns the original errno
Antoine Pitrou <solipsis@pitrou.net>
parents:
69998
diff
changeset
|
464 if connect_ex: |
|
019d8ccdf03b
Issue #12065: connect_ex() on an SSL socket now returns the original errno
Antoine Pitrou <solipsis@pitrou.net>
parents:
69998
diff
changeset
|
465 rc = socket.connect_ex(self, addr) |
|
68186
1909c12ef06c
Issue #11326: Add the missing connect_ex() implementation for SSL sockets,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65593
diff
changeset
|
466 else: |
|
70187
019d8ccdf03b
Issue #12065: connect_ex() on an SSL socket now returns the original errno
Antoine Pitrou <solipsis@pitrou.net>
parents:
69998
diff
changeset
|
467 rc = None |
|
019d8ccdf03b
Issue #12065: connect_ex() on an SSL socket now returns the original errno
Antoine Pitrou <solipsis@pitrou.net>
parents:
69998
diff
changeset
|
468 socket.connect(self, addr) |
|
019d8ccdf03b
Issue #12065: connect_ex() on an SSL socket now returns the original errno
Antoine Pitrou <solipsis@pitrou.net>
parents:
69998
diff
changeset
|
469 if not rc: |
|
019d8ccdf03b
Issue #12065: connect_ex() on an SSL socket now returns the original errno
Antoine Pitrou <solipsis@pitrou.net>
parents:
69998
diff
changeset
|
470 if self.do_handshake_on_connect: |
|
019d8ccdf03b
Issue #12065: connect_ex() on an SSL socket now returns the original errno
Antoine Pitrou <solipsis@pitrou.net>
parents:
69998
diff
changeset
|
471 self.do_handshake() |
|
019d8ccdf03b
Issue #12065: connect_ex() on an SSL socket now returns the original errno
Antoine Pitrou <solipsis@pitrou.net>
parents:
69998
diff
changeset
|
472 self._connected = True |
|
019d8ccdf03b
Issue #12065: connect_ex() on an SSL socket now returns the original errno
Antoine Pitrou <solipsis@pitrou.net>
parents:
69998
diff
changeset
|
473 return rc |
|
019d8ccdf03b
Issue #12065: connect_ex() on an SSL socket now returns the original errno
Antoine Pitrou <solipsis@pitrou.net>
parents:
69998
diff
changeset
|
474 except socket_error: |
|
019d8ccdf03b
Issue #12065: connect_ex() on an SSL socket now returns the original errno
Antoine Pitrou <solipsis@pitrou.net>
parents:
69998
diff
changeset
|
475 self._sslobj = None |
|
019d8ccdf03b
Issue #12065: connect_ex() on an SSL socket now returns the original errno
Antoine Pitrou <solipsis@pitrou.net>
parents:
69998
diff
changeset
|
476 raise |
|
68186
1909c12ef06c
Issue #11326: Add the missing connect_ex() implementation for SSL sockets,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65593
diff
changeset
|
477 |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
478 def connect(self, addr): |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
479 """Connects to remote ADDR, and then wraps the connection in |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
480 an SSL channel.""" |
|
68186
1909c12ef06c
Issue #11326: Add the missing connect_ex() implementation for SSL sockets,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65593
diff
changeset
|
481 self._real_connect(addr, False) |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
482 |
|
68186
1909c12ef06c
Issue #11326: Add the missing connect_ex() implementation for SSL sockets,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65593
diff
changeset
|
483 def connect_ex(self, addr): |
|
1909c12ef06c
Issue #11326: Add the missing connect_ex() implementation for SSL sockets,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65593
diff
changeset
|
484 """Connects to remote ADDR, and then wraps the connection in |
|
1909c12ef06c
Issue #11326: Add the missing connect_ex() implementation for SSL sockets,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65593
diff
changeset
|
485 an SSL channel.""" |
|
1909c12ef06c
Issue #11326: Add the missing connect_ex() implementation for SSL sockets,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65593
diff
changeset
|
486 return self._real_connect(addr, True) |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
487 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
488 def accept(self): |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
489 """Accepts a new connection from a remote client, and returns |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
490 a tuple containing that new connection wrapped with a server-side |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
491 SSL channel, and the address of the remote client.""" |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
492 |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
493 newsock, addr = socket.accept(self) |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
494 return (SSLSocket(sock=newsock, |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
495 keyfile=self.keyfile, certfile=self.certfile, |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
496 server_side=True, |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
497 cert_reqs=self.cert_reqs, |
|
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
498 ssl_version=self.ssl_version, |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
499 ca_certs=self.ca_certs, |
|
60662
e5f6de18b715
Merged revisions 80151 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60373
diff
changeset
|
500 ciphers=self.ciphers, |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
501 do_handshake_on_connect= |
|
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
502 self.do_handshake_on_connect), |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
503 addr) |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
504 |
|
44305
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
505 def __del__(self): |
|
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
506 # sys.stderr.write("__del__ on %s\n" % repr(self)) |
|
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
44206
diff
changeset
|
507 self._real_close() |
|
44206
ce280a04f55e
Reverting revision 59394. We'll sort this out later.
Guido van Rossum <guido@python.org>
parents:
44205
diff
changeset
|
508 |
|
ce280a04f55e
Reverting revision 59394. We'll sort this out later.
Guido van Rossum <guido@python.org>
parents:
44205
diff
changeset
|
509 |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
510 def wrap_socket(sock, keyfile=None, certfile=None, |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
511 server_side=False, cert_reqs=CERT_NONE, |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
512 ssl_version=PROTOCOL_SSLv23, ca_certs=None, |
|
44161
1a7dfb61b4d8
most recent changes to SSL module to support non-blocking sockets properly
Bill Janssen <janssen@parc.com>
parents:
43882
diff
changeset
|
513 do_handshake_on_connect=True, |
|
60662
e5f6de18b715
Merged revisions 80151 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60373
diff
changeset
|
514 suppress_ragged_eofs=True, ciphers=None): |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
515 |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
516 return SSLSocket(sock=sock, keyfile=keyfile, certfile=certfile, |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
517 server_side=server_side, cert_reqs=cert_reqs, |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
518 ssl_version=ssl_version, ca_certs=ca_certs, |
|
44161
1a7dfb61b4d8
most recent changes to SSL module to support non-blocking sockets properly
Bill Janssen <janssen@parc.com>
parents:
43882
diff
changeset
|
519 do_handshake_on_connect=do_handshake_on_connect, |
|
60662
e5f6de18b715
Merged revisions 80151 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60373
diff
changeset
|
520 suppress_ragged_eofs=suppress_ragged_eofs, |
|
e5f6de18b715
Merged revisions 80151 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60373
diff
changeset
|
521 ciphers=ciphers) |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
522 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
523 # some utility functions |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
524 |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
525 def cert_time_to_seconds(cert_time): |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
526 """Takes a date-time string in standard ASN1_print form |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
527 ("MON DAY 24HOUR:MINUTE:SEC YEAR TIMEZONE") and return |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
528 a Python time value in seconds past the epoch.""" |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
529 |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
530 import time |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
531 return time.mktime(time.strptime(cert_time, "%b %d %H:%M:%S %Y GMT")) |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
532 |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
533 PEM_HEADER = "-----BEGIN CERTIFICATE-----" |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
534 PEM_FOOTER = "-----END CERTIFICATE-----" |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
535 |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
536 def DER_cert_to_PEM_cert(der_cert_bytes): |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
537 """Takes a certificate in binary DER format and returns the |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
538 PEM version of it as a string.""" |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
539 |
|
43869
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
540 f = str(base64.standard_b64encode(der_cert_bytes), 'ASCII', 'strict') |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
541 return (PEM_HEADER + '\n' + |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
542 textwrap.fill(f, 64) + '\n' + |
|
b59825d9db8f
get SSL support to work again
Bill Janssen <janssen@parc.com>
parents:
43400
diff
changeset
|
543 PEM_FOOTER + '\n') |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
544 |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
545 def PEM_cert_to_DER_cert(pem_cert_string): |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
546 """Takes a certificate in ASCII PEM format and returns the |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
547 DER-encoded version of it as a byte sequence""" |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
548 |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
549 if not pem_cert_string.startswith(PEM_HEADER): |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
550 raise ValueError("Invalid PEM encoding; must start with %s" |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
551 % PEM_HEADER) |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
552 if not pem_cert_string.strip().endswith(PEM_FOOTER): |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
553 raise ValueError("Invalid PEM encoding; must end with %s" |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
554 % PEM_FOOTER) |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
555 d = pem_cert_string.strip()[len(PEM_HEADER):-len(PEM_FOOTER)] |
|
54568
031b963f2bc2
More codestring -> codebytes.
Georg Brandl <georg@python.org>
parents:
50390
diff
changeset
|
556 return base64.decodebytes(d.encode('ASCII', 'strict')) |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
557 |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
558 def get_server_certificate(addr, ssl_version=PROTOCOL_SSLv3, ca_certs=None): |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
559 """Retrieve the certificate from the server at the specified address, |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
560 and return it as a PEM-encoded string. |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
561 If 'ca_certs' is specified, validate the server cert against it. |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
562 If 'ssl_version' is specified, use it in the connection attempt.""" |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
563 |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
564 host, port = addr |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
565 if (ca_certs is not None): |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
566 cert_reqs = CERT_REQUIRED |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
567 else: |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
568 cert_reqs = CERT_NONE |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
569 s = wrap_socket(socket(), ssl_version=ssl_version, |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
570 cert_reqs=cert_reqs, ca_certs=ca_certs) |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
571 s.connect(addr) |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
572 dercert = s.getpeercert(True) |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
573 s.close() |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
574 return DER_cert_to_PEM_cert(dercert) |
|
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
575 |
|
43875
e20203d0331c
Fix some minor style nits. (I'll leave adding __all__ and making the
Guido van Rossum <guido@python.org>
parents:
43869
diff
changeset
|
576 def get_protocol_name(protocol_code): |
|
69997
b7abf0590e1c
Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64743
diff
changeset
|
577 return _PROTOCOL_NAMES.get(protocol_code, '<unknown>') |
