blob: 7b8f21a2ec43ed3b8d6289e4f56727cd4fc65452 [file] [log] [blame]
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +00001# Wrapper module for _ssl, providing some additional facilities
2# implemented in Python. Written by Bill Janssen.
3
Benjamin Petersondaeb9252014-08-20 14:14:50 -05004"""This module provides some more Pythonic support for SSL.
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +00005
6Object types:
7
Bill Janssen98d19da2007-09-10 21:51:02 +00008 SSLSocket -- subtype of socket.socket which does SSL over the socket
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +00009
10Exceptions:
11
Bill Janssen98d19da2007-09-10 21:51:02 +000012 SSLError -- exception raised for I/O errors
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +000013
14Functions:
15
16 cert_time_to_seconds -- convert time string used for certificate
17 notBefore and notAfter functions to integer
18 seconds past the Epoch (the time values
19 returned from time.time())
20
21 fetch_server_certificate (HOST, PORT) -- fetch the certificate provided
22 by the server running on HOST at port PORT. No
23 validation of the certificate is performed.
24
25Integer constants:
26
27SSL_ERROR_ZERO_RETURN
28SSL_ERROR_WANT_READ
29SSL_ERROR_WANT_WRITE
30SSL_ERROR_WANT_X509_LOOKUP
31SSL_ERROR_SYSCALL
32SSL_ERROR_SSL
33SSL_ERROR_WANT_CONNECT
34
35SSL_ERROR_EOF
36SSL_ERROR_INVALID_ERROR_CODE
37
38The following group define certificate requirements that one side is
39allowing/requiring from the other side:
40
41CERT_NONE - no certificates from the other side are required (or will
42 be looked at if provided)
43CERT_OPTIONAL - certificates are not required, but if provided will be
44 validated, and if validation fails, the connection will
45 also fail
46CERT_REQUIRED - certificates are required, and will be validated, and
47 if validation fails, the connection will also fail
48
49The following constants identify various SSL protocol variants:
50
51PROTOCOL_SSLv2
52PROTOCOL_SSLv3
53PROTOCOL_SSLv23
54PROTOCOL_TLSv1
Benjamin Petersondaeb9252014-08-20 14:14:50 -050055PROTOCOL_TLSv1_1
56PROTOCOL_TLSv1_2
57
58The following constants identify various SSL alert message descriptions as per
59http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6
60
61ALERT_DESCRIPTION_CLOSE_NOTIFY
62ALERT_DESCRIPTION_UNEXPECTED_MESSAGE
63ALERT_DESCRIPTION_BAD_RECORD_MAC
64ALERT_DESCRIPTION_RECORD_OVERFLOW
65ALERT_DESCRIPTION_DECOMPRESSION_FAILURE
66ALERT_DESCRIPTION_HANDSHAKE_FAILURE
67ALERT_DESCRIPTION_BAD_CERTIFICATE
68ALERT_DESCRIPTION_UNSUPPORTED_CERTIFICATE
69ALERT_DESCRIPTION_CERTIFICATE_REVOKED
70ALERT_DESCRIPTION_CERTIFICATE_EXPIRED
71ALERT_DESCRIPTION_CERTIFICATE_UNKNOWN
72ALERT_DESCRIPTION_ILLEGAL_PARAMETER
73ALERT_DESCRIPTION_UNKNOWN_CA
74ALERT_DESCRIPTION_ACCESS_DENIED
75ALERT_DESCRIPTION_DECODE_ERROR
76ALERT_DESCRIPTION_DECRYPT_ERROR
77ALERT_DESCRIPTION_PROTOCOL_VERSION
78ALERT_DESCRIPTION_INSUFFICIENT_SECURITY
79ALERT_DESCRIPTION_INTERNAL_ERROR
80ALERT_DESCRIPTION_USER_CANCELLED
81ALERT_DESCRIPTION_NO_RENEGOTIATION
82ALERT_DESCRIPTION_UNSUPPORTED_EXTENSION
83ALERT_DESCRIPTION_CERTIFICATE_UNOBTAINABLE
84ALERT_DESCRIPTION_UNRECOGNIZED_NAME
85ALERT_DESCRIPTION_BAD_CERTIFICATE_STATUS_RESPONSE
86ALERT_DESCRIPTION_BAD_CERTIFICATE_HASH_VALUE
87ALERT_DESCRIPTION_UNKNOWN_PSK_IDENTITY
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +000088"""
89
Christian Heimesc5f05e42008-02-23 17:40:11 +000090import textwrap
Benjamin Petersondaeb9252014-08-20 14:14:50 -050091import re
92import sys
93import os
94from collections import namedtuple
95from contextlib import closing
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +000096
97import _ssl # if we can't import it, let the error propagate
Bill Janssen98d19da2007-09-10 21:51:02 +000098
Antoine Pitrouf9de5342010-04-05 21:35:07 +000099from _ssl import OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_INFO, OPENSSL_VERSION
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500100from _ssl import _SSLContext
101from _ssl import (
102 SSLError, SSLZeroReturnError, SSLWantReadError, SSLWantWriteError,
103 SSLSyscallError, SSLEOFError,
104 )
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000105from _ssl import CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500106from _ssl import (VERIFY_DEFAULT, VERIFY_CRL_CHECK_LEAF, VERIFY_CRL_CHECK_CHAIN,
107 VERIFY_X509_STRICT)
108from _ssl import txt2obj as _txt2obj, nid2obj as _nid2obj
Victor Stinner7c906672015-01-06 13:53:37 +0100109from _ssl import RAND_status, RAND_add
110try:
111 from _ssl import RAND_egd
112except ImportError:
113 # LibreSSL does not provide RAND_egd
114 pass
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500115
116def _import_symbols(prefix):
117 for n in dir(_ssl):
118 if n.startswith(prefix):
119 globals()[n] = getattr(_ssl, n)
120
121_import_symbols('OP_')
122_import_symbols('ALERT_DESCRIPTION_')
123_import_symbols('SSL_ERROR_')
124_import_symbols('PROTOCOL_')
125
Benjamin Petersonb10bfbe2015-01-23 16:35:37 -0500126from _ssl import HAS_SNI, HAS_ECDH, HAS_NPN, HAS_ALPN
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500127
128from _ssl import _OPENSSL_API_VERSION
129
130_PROTOCOL_NAMES = {value: name for name, value in globals().items() if name.startswith('PROTOCOL_')}
131
Victor Stinnerb1241f92011-05-10 01:52:03 +0200132try:
Antoine Pitroud76088d2012-01-03 22:46:48 +0100133 _SSLv2_IF_EXISTS = PROTOCOL_SSLv2
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500134except NameError:
Antoine Pitroud76088d2012-01-03 22:46:48 +0100135 _SSLv2_IF_EXISTS = None
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000136
Antoine Pitroudfb299b2010-04-23 22:54:59 +0000137from socket import socket, _fileobject, _delegate_methods, error as socket_error
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500138if sys.platform == "win32":
139 from _ssl import enum_certificates, enum_crls
140
141from socket import socket, AF_INET, SOCK_STREAM, create_connection
142from socket import SOL_SOCKET, SO_TYPE
Bill Janssen296a59d2007-09-16 22:06:00 +0000143import base64 # for DER-to-PEM translation
Antoine Pitrou278d6652010-04-26 17:23:33 +0000144import errno
Bill Janssen98d19da2007-09-10 21:51:02 +0000145
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500146if _ssl.HAS_TLS_UNIQUE:
147 CHANNEL_BINDING_TYPES = ['tls-unique']
148else:
149 CHANNEL_BINDING_TYPES = []
150
Antoine Pitroud76088d2012-01-03 22:46:48 +0100151# Disable weak or insecure ciphers by default
152# (OpenSSL's default setting is 'DEFAULT:!aNULL:!eNULL')
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500153# Enable a better set of ciphers by default
154# This list has been explicitly chosen to:
155# * Prefer cipher suites that offer perfect forward secrecy (DHE/ECDHE)
156# * Prefer ECDHE over DHE for better performance
157# * Prefer any AES-GCM over any AES-CBC for better performance and security
158# * Then Use HIGH cipher suites as a fallback
159# * Then Use 3DES as fallback which is secure but slow
160# * Finally use RC4 as a fallback which is problematic but needed for
161# compatibility some times.
162# * Disable NULL authentication, NULL encryption, and MD5 MACs for security
163# reasons
164_DEFAULT_CIPHERS = (
165 'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:'
166 'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES:ECDH+RC4:'
167 'DH+RC4:RSA+RC4:!aNULL:!eNULL:!MD5'
168)
Antoine Pitroud76088d2012-01-03 22:46:48 +0100169
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500170# Restricted and more secure ciphers for the server side
171# This list has been explicitly chosen to:
172# * Prefer cipher suites that offer perfect forward secrecy (DHE/ECDHE)
173# * Prefer ECDHE over DHE for better performance
174# * Prefer any AES-GCM over any AES-CBC for better performance and security
175# * Then Use HIGH cipher suites as a fallback
176# * Then Use 3DES as fallback which is secure but slow
177# * Disable NULL authentication, NULL encryption, MD5 MACs, DSS, and RC4 for
178# security reasons
179_RESTRICTED_SERVER_CIPHERS = (
180 'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:'
181 'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES:!aNULL:'
182 '!eNULL:!MD5:!DSS:!RC4'
183)
184
185
186class CertificateError(ValueError):
187 pass
188
189
190def _dnsname_match(dn, hostname, max_wildcards=1):
191 """Matching according to RFC 6125, section 6.4.3
192
193 http://tools.ietf.org/html/rfc6125#section-6.4.3
194 """
195 pats = []
196 if not dn:
197 return False
198
199 pieces = dn.split(r'.')
200 leftmost = pieces[0]
201 remainder = pieces[1:]
202
203 wildcards = leftmost.count('*')
204 if wildcards > max_wildcards:
205 # Issue #17980: avoid denials of service by refusing more
206 # than one wildcard per fragment. A survery of established
207 # policy among SSL implementations showed it to be a
208 # reasonable choice.
209 raise CertificateError(
210 "too many wildcards in certificate DNS name: " + repr(dn))
211
212 # speed up common case w/o wildcards
213 if not wildcards:
214 return dn.lower() == hostname.lower()
215
216 # RFC 6125, section 6.4.3, subitem 1.
217 # The client SHOULD NOT attempt to match a presented identifier in which
218 # the wildcard character comprises a label other than the left-most label.
219 if leftmost == '*':
220 # When '*' is a fragment by itself, it matches a non-empty dotless
221 # fragment.
222 pats.append('[^.]+')
223 elif leftmost.startswith('xn--') or hostname.startswith('xn--'):
224 # RFC 6125, section 6.4.3, subitem 3.
225 # The client SHOULD NOT attempt to match a presented identifier
226 # where the wildcard character is embedded within an A-label or
227 # U-label of an internationalized domain name.
228 pats.append(re.escape(leftmost))
229 else:
230 # Otherwise, '*' matches any dotless string, e.g. www*
231 pats.append(re.escape(leftmost).replace(r'\*', '[^.]*'))
232
233 # add the remaining fragments, ignore any wildcards
234 for frag in remainder:
235 pats.append(re.escape(frag))
236
237 pat = re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE)
238 return pat.match(hostname)
239
240
241def match_hostname(cert, hostname):
242 """Verify that *cert* (in decoded format as returned by
243 SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125
244 rules are followed, but IP addresses are not accepted for *hostname*.
245
246 CertificateError is raised on failure. On success, the function
247 returns nothing.
248 """
249 if not cert:
250 raise ValueError("empty or no certificate, match_hostname needs a "
251 "SSL socket or SSL context with either "
252 "CERT_OPTIONAL or CERT_REQUIRED")
253 dnsnames = []
254 san = cert.get('subjectAltName', ())
255 for key, value in san:
256 if key == 'DNS':
257 if _dnsname_match(value, hostname):
258 return
259 dnsnames.append(value)
260 if not dnsnames:
261 # The subject is only checked when there is no dNSName entry
262 # in subjectAltName
263 for sub in cert.get('subject', ()):
264 for key, value in sub:
265 # XXX according to RFC 2818, the most specific Common Name
266 # must be used.
267 if key == 'commonName':
268 if _dnsname_match(value, hostname):
269 return
270 dnsnames.append(value)
271 if len(dnsnames) > 1:
272 raise CertificateError("hostname %r "
273 "doesn't match either of %s"
274 % (hostname, ', '.join(map(repr, dnsnames))))
275 elif len(dnsnames) == 1:
276 raise CertificateError("hostname %r "
277 "doesn't match %r"
278 % (hostname, dnsnames[0]))
279 else:
280 raise CertificateError("no appropriate commonName or "
281 "subjectAltName fields were found")
282
283
284DefaultVerifyPaths = namedtuple("DefaultVerifyPaths",
285 "cafile capath openssl_cafile_env openssl_cafile openssl_capath_env "
286 "openssl_capath")
287
288def get_default_verify_paths():
289 """Return paths to default cafile and capath.
290 """
291 parts = _ssl.get_default_verify_paths()
292
293 # environment vars shadow paths
294 cafile = os.environ.get(parts[0], parts[1])
295 capath = os.environ.get(parts[2], parts[3])
296
297 return DefaultVerifyPaths(cafile if os.path.isfile(cafile) else None,
298 capath if os.path.isdir(capath) else None,
299 *parts)
300
301
302class _ASN1Object(namedtuple("_ASN1Object", "nid shortname longname oid")):
303 """ASN.1 object identifier lookup
304 """
305 __slots__ = ()
306
307 def __new__(cls, oid):
308 return super(_ASN1Object, cls).__new__(cls, *_txt2obj(oid, name=False))
309
310 @classmethod
311 def fromnid(cls, nid):
312 """Create _ASN1Object from OpenSSL numeric ID
313 """
314 return super(_ASN1Object, cls).__new__(cls, *_nid2obj(nid))
315
316 @classmethod
317 def fromname(cls, name):
318 """Create _ASN1Object from short name, long name or OID
319 """
320 return super(_ASN1Object, cls).__new__(cls, *_txt2obj(name, name=True))
321
322
323class Purpose(_ASN1Object):
324 """SSLContext purpose flags with X509v3 Extended Key Usage objects
325 """
326
327Purpose.SERVER_AUTH = Purpose('1.3.6.1.5.5.7.3.1')
328Purpose.CLIENT_AUTH = Purpose('1.3.6.1.5.5.7.3.2')
329
330
331class SSLContext(_SSLContext):
332 """An SSLContext holds various SSL-related configuration options and
333 data, such as certificates and possibly a private key."""
334
335 __slots__ = ('protocol', '__weakref__')
336 _windows_cert_stores = ("CA", "ROOT")
337
338 def __new__(cls, protocol, *args, **kwargs):
339 self = _SSLContext.__new__(cls, protocol)
340 if protocol != _SSLv2_IF_EXISTS:
341 self.set_ciphers(_DEFAULT_CIPHERS)
342 return self
343
344 def __init__(self, protocol):
345 self.protocol = protocol
346
347 def wrap_socket(self, sock, server_side=False,
348 do_handshake_on_connect=True,
349 suppress_ragged_eofs=True,
350 server_hostname=None):
351 return SSLSocket(sock=sock, server_side=server_side,
352 do_handshake_on_connect=do_handshake_on_connect,
353 suppress_ragged_eofs=suppress_ragged_eofs,
354 server_hostname=server_hostname,
355 _context=self)
356
357 def set_npn_protocols(self, npn_protocols):
358 protos = bytearray()
359 for protocol in npn_protocols:
360 b = protocol.encode('ascii')
361 if len(b) == 0 or len(b) > 255:
362 raise SSLError('NPN protocols must be 1 to 255 in length')
363 protos.append(len(b))
364 protos.extend(b)
365
366 self._set_npn_protocols(protos)
367
Benjamin Petersonb10bfbe2015-01-23 16:35:37 -0500368 def set_alpn_protocols(self, alpn_protocols):
369 protos = bytearray()
370 for protocol in alpn_protocols:
371 b = protocol.encode('ascii')
372 if len(b) == 0 or len(b) > 255:
373 raise SSLError('ALPN protocols must be 1 to 255 in length')
374 protos.append(len(b))
375 protos.extend(b)
376
377 self._set_alpn_protocols(protos)
378
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500379 def _load_windows_store_certs(self, storename, purpose):
380 certs = bytearray()
381 for cert, encoding, trust in enum_certificates(storename):
382 # CA certs are never PKCS#7 encoded
383 if encoding == "x509_asn":
384 if trust is True or purpose.oid in trust:
385 certs.extend(cert)
386 self.load_verify_locations(cadata=certs)
387 return certs
388
389 def load_default_certs(self, purpose=Purpose.SERVER_AUTH):
390 if not isinstance(purpose, _ASN1Object):
391 raise TypeError(purpose)
392 if sys.platform == "win32":
393 for storename in self._windows_cert_stores:
394 self._load_windows_store_certs(storename, purpose)
Benjamin Peterson0b30a2b2014-10-03 17:27:05 -0400395 self.set_default_verify_paths()
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500396
397
398def create_default_context(purpose=Purpose.SERVER_AUTH, cafile=None,
399 capath=None, cadata=None):
400 """Create a SSLContext object with default settings.
401
402 NOTE: The protocol and settings may change anytime without prior
403 deprecation. The values represent a fair balance between maximum
404 compatibility and security.
405 """
406 if not isinstance(purpose, _ASN1Object):
407 raise TypeError(purpose)
408
409 context = SSLContext(PROTOCOL_SSLv23)
410
411 # SSLv2 considered harmful.
412 context.options |= OP_NO_SSLv2
413
414 # SSLv3 has problematic security and is only required for really old
415 # clients such as IE6 on Windows XP
416 context.options |= OP_NO_SSLv3
417
418 # disable compression to prevent CRIME attacks (OpenSSL 1.0+)
419 context.options |= getattr(_ssl, "OP_NO_COMPRESSION", 0)
420
421 if purpose == Purpose.SERVER_AUTH:
422 # verify certs and host name in client mode
423 context.verify_mode = CERT_REQUIRED
424 context.check_hostname = True
425 elif purpose == Purpose.CLIENT_AUTH:
426 # Prefer the server's ciphers by default so that we get stronger
427 # encryption
428 context.options |= getattr(_ssl, "OP_CIPHER_SERVER_PREFERENCE", 0)
429
430 # Use single use keys in order to improve forward secrecy
431 context.options |= getattr(_ssl, "OP_SINGLE_DH_USE", 0)
432 context.options |= getattr(_ssl, "OP_SINGLE_ECDH_USE", 0)
433
434 # disallow ciphers with known vulnerabilities
435 context.set_ciphers(_RESTRICTED_SERVER_CIPHERS)
436
437 if cafile or capath or cadata:
438 context.load_verify_locations(cafile, capath, cadata)
439 elif context.verify_mode != CERT_NONE:
440 # no explicit cafile, capath or cadata but the verify mode is
441 # CERT_OPTIONAL or CERT_REQUIRED. Let's try to load default system
442 # root CA certificates for the given purpose. This may fail silently.
443 context.load_default_certs(purpose)
444 return context
445
Benjamin Petersone3e7d402014-11-23 21:02:02 -0600446def _create_unverified_context(protocol=PROTOCOL_SSLv23, cert_reqs=None,
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500447 check_hostname=False, purpose=Purpose.SERVER_AUTH,
448 certfile=None, keyfile=None,
449 cafile=None, capath=None, cadata=None):
450 """Create a SSLContext object for Python stdlib modules
451
452 All Python stdlib modules shall use this function to create SSLContext
453 objects in order to keep common settings in one place. The configuration
454 is less restrict than create_default_context()'s to increase backward
455 compatibility.
456 """
457 if not isinstance(purpose, _ASN1Object):
458 raise TypeError(purpose)
459
460 context = SSLContext(protocol)
461 # SSLv2 considered harmful.
462 context.options |= OP_NO_SSLv2
Antoine Pitrou95b61642014-10-17 19:28:30 +0200463 # SSLv3 has problematic security and is only required for really old
464 # clients such as IE6 on Windows XP
465 context.options |= OP_NO_SSLv3
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500466
467 if cert_reqs is not None:
468 context.verify_mode = cert_reqs
469 context.check_hostname = check_hostname
470
471 if keyfile and not certfile:
472 raise ValueError("certfile must be specified")
473 if certfile or keyfile:
474 context.load_cert_chain(certfile, keyfile)
475
476 # load CA root certs
477 if cafile or capath or cadata:
478 context.load_verify_locations(cafile, capath, cadata)
479 elif context.verify_mode != CERT_NONE:
480 # no explicit cafile, capath or cadata but the verify mode is
481 # CERT_OPTIONAL or CERT_REQUIRED. Let's try to load default system
482 # root CA certificates for the given purpose. This may fail silently.
483 context.load_default_certs(purpose)
484
485 return context
Antoine Pitroud76088d2012-01-03 22:46:48 +0100486
Benjamin Petersone3e7d402014-11-23 21:02:02 -0600487# Used by http.client if no context is explicitly passed.
488_create_default_https_context = create_default_context
489
490
491# Backwards compatibility alias, even though it's not a public name.
492_create_stdlib_context = _create_unverified_context
493
494
Ezio Melottib01f5e62010-01-18 09:10:26 +0000495class SSLSocket(socket):
Bill Janssen426ea0a2007-08-29 22:35:05 +0000496 """This class implements a subtype of socket.socket that wraps
497 the underlying OS socket in an SSL context when necessary, and
498 provides read and write methods over that channel."""
499
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500500 def __init__(self, sock=None, keyfile=None, certfile=None,
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000501 server_side=False, cert_reqs=CERT_NONE,
Bill Janssen934b16d2008-06-28 22:19:33 +0000502 ssl_version=PROTOCOL_SSLv23, ca_certs=None,
503 do_handshake_on_connect=True,
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500504 family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None,
505 suppress_ragged_eofs=True, npn_protocols=None, ciphers=None,
506 server_hostname=None,
507 _context=None):
508
Benjamin Peterson5f6b89b2014-11-23 11:16:48 -0600509 self._makefile_refs = 0
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500510 if _context:
511 self._context = _context
512 else:
513 if server_side and not certfile:
514 raise ValueError("certfile must be specified for server-side "
515 "operations")
516 if keyfile and not certfile:
517 raise ValueError("certfile must be specified")
518 if certfile and not keyfile:
519 keyfile = certfile
520 self._context = SSLContext(ssl_version)
521 self._context.verify_mode = cert_reqs
522 if ca_certs:
523 self._context.load_verify_locations(ca_certs)
524 if certfile:
525 self._context.load_cert_chain(certfile, keyfile)
526 if npn_protocols:
527 self._context.set_npn_protocols(npn_protocols)
528 if ciphers:
529 self._context.set_ciphers(ciphers)
530 self.keyfile = keyfile
531 self.certfile = certfile
532 self.cert_reqs = cert_reqs
533 self.ssl_version = ssl_version
534 self.ca_certs = ca_certs
535 self.ciphers = ciphers
Antoine Pitrou63cc99d2013-12-28 17:26:33 +0100536 # Can't use sock.type as other flags (such as SOCK_NONBLOCK) get
537 # mixed in.
538 if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
539 raise NotImplementedError("only stream sockets are supported")
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000540 socket.__init__(self, _sock=sock._sock)
Antoine Pitroudfb299b2010-04-23 22:54:59 +0000541 # The initializer for socket overrides the methods send(), recv(), etc.
542 # in the instancce, which we don't need -- but we want to provide the
543 # methods defined in SSLSocket.
544 for attr in _delegate_methods:
545 try:
546 delattr(self, attr)
547 except AttributeError:
548 pass
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500549 if server_side and server_hostname:
550 raise ValueError("server_hostname can only be specified "
551 "in client mode")
552 if self._context.check_hostname and not server_hostname:
Benjamin Peterson31aa69e2014-11-23 20:13:31 -0600553 raise ValueError("check_hostname requires server_hostname")
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500554 self.server_side = server_side
555 self.server_hostname = server_hostname
Bill Janssen934b16d2008-06-28 22:19:33 +0000556 self.do_handshake_on_connect = do_handshake_on_connect
557 self.suppress_ragged_eofs = suppress_ragged_eofs
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500558
559 # See if we are connected
560 try:
561 self.getpeername()
562 except socket_error as e:
563 if e.errno != errno.ENOTCONN:
564 raise
565 connected = False
566 else:
567 connected = True
568
569 self._closed = False
570 self._sslobj = None
571 self._connected = connected
572 if connected:
573 # create the SSL object
574 try:
575 self._sslobj = self._context._wrap_socket(self._sock, server_side,
576 server_hostname, ssl_sock=self)
577 if do_handshake_on_connect:
578 timeout = self.gettimeout()
579 if timeout == 0.0:
580 # non-blocking
581 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
582 self.do_handshake()
583
584 except (OSError, ValueError):
585 self.close()
586 raise
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000587
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500588 @property
589 def context(self):
590 return self._context
Bill Janssen24bccf22007-08-30 17:07:28 +0000591
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500592 @context.setter
593 def context(self, ctx):
594 self._context = ctx
595 self._sslobj.context = ctx
596
597 def dup(self):
598 raise NotImplemented("Can't dup() %s instances" %
599 self.__class__.__name__)
600
601 def _checkClosed(self, msg=None):
602 # raise an exception here if you wish to check for spurious closes
603 pass
604
605 def _check_connected(self):
606 if not self._connected:
607 # getpeername() will raise ENOTCONN if the socket is really
608 # not connected; note that we can be connected even without
609 # _connected being set, e.g. if connect() first returned
610 # EAGAIN.
611 self.getpeername()
612
613 def read(self, len=0, buffer=None):
Bill Janssen24bccf22007-08-30 17:07:28 +0000614 """Read up to LEN bytes and return them.
615 Return zero-length string on EOF."""
616
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500617 self._checkClosed()
618 if not self._sslobj:
619 raise ValueError("Read on closed or unwrapped SSL socket.")
Bill Janssen934b16d2008-06-28 22:19:33 +0000620 try:
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500621 if buffer is not None:
622 v = self._sslobj.read(len, buffer)
623 else:
624 v = self._sslobj.read(len or 1024)
625 return v
626 except SSLError as x:
Bill Janssen934b16d2008-06-28 22:19:33 +0000627 if x.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500628 if buffer is not None:
629 return 0
630 else:
631 return b''
Bill Janssen934b16d2008-06-28 22:19:33 +0000632 else:
633 raise
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000634
635 def write(self, data):
Bill Janssen24bccf22007-08-30 17:07:28 +0000636 """Write DATA to the underlying SSL channel. Returns
637 number of bytes of DATA actually transmitted."""
638
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500639 self._checkClosed()
640 if not self._sslobj:
641 raise ValueError("Write on closed or unwrapped SSL socket.")
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000642 return self._sslobj.write(data)
643
Bill Janssen98d19da2007-09-10 21:51:02 +0000644 def getpeercert(self, binary_form=False):
Bill Janssen24bccf22007-08-30 17:07:28 +0000645 """Returns a formatted version of the data in the
646 certificate provided by the other end of the SSL channel.
647 Return None if no certificate was provided, {} if a
648 certificate was provided, but not validated."""
649
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500650 self._checkClosed()
651 self._check_connected()
Bill Janssen98d19da2007-09-10 21:51:02 +0000652 return self._sslobj.peer_certificate(binary_form)
653
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500654 def selected_npn_protocol(self):
655 self._checkClosed()
656 if not self._sslobj or not _ssl.HAS_NPN:
657 return None
658 else:
659 return self._sslobj.selected_npn_protocol()
Bill Janssen98d19da2007-09-10 21:51:02 +0000660
Benjamin Petersonb10bfbe2015-01-23 16:35:37 -0500661 def selected_alpn_protocol(self):
662 self._checkClosed()
663 if not self._sslobj or not _ssl.HAS_ALPN:
664 return None
665 else:
666 return self._sslobj.selected_alpn_protocol()
667
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500668 def cipher(self):
669 self._checkClosed()
Bill Janssen98d19da2007-09-10 21:51:02 +0000670 if not self._sslobj:
671 return None
672 else:
673 return self._sslobj.cipher()
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000674
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500675 def compression(self):
676 self._checkClosed()
677 if not self._sslobj:
678 return None
679 else:
680 return self._sslobj.compression()
681
Ezio Melottib01f5e62010-01-18 09:10:26 +0000682 def send(self, data, flags=0):
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500683 self._checkClosed()
Bill Janssen426ea0a2007-08-29 22:35:05 +0000684 if self._sslobj:
685 if flags != 0:
686 raise ValueError(
687 "non-zero flags not allowed in calls to send() on %s" %
688 self.__class__)
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500689 try:
690 v = self._sslobj.write(data)
691 except SSLError as x:
692 if x.args[0] == SSL_ERROR_WANT_READ:
693 return 0
694 elif x.args[0] == SSL_ERROR_WANT_WRITE:
695 return 0
Bill Janssen934b16d2008-06-28 22:19:33 +0000696 else:
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500697 raise
698 else:
699 return v
Bill Janssen426ea0a2007-08-29 22:35:05 +0000700 else:
Antoine Pitrouf7f390a2010-09-14 14:37:18 +0000701 return self._sock.send(data, flags)
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000702
Antoine Pitrouf7f390a2010-09-14 14:37:18 +0000703 def sendto(self, data, flags_or_addr, addr=None):
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500704 self._checkClosed()
Bill Janssen426ea0a2007-08-29 22:35:05 +0000705 if self._sslobj:
Bill Janssen934b16d2008-06-28 22:19:33 +0000706 raise ValueError("sendto not allowed on instances of %s" %
Bill Janssen426ea0a2007-08-29 22:35:05 +0000707 self.__class__)
Antoine Pitrouf7f390a2010-09-14 14:37:18 +0000708 elif addr is None:
709 return self._sock.sendto(data, flags_or_addr)
Bill Janssen426ea0a2007-08-29 22:35:05 +0000710 else:
Antoine Pitrouf7f390a2010-09-14 14:37:18 +0000711 return self._sock.sendto(data, flags_or_addr, addr)
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000712
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500713
Ezio Melottib01f5e62010-01-18 09:10:26 +0000714 def sendall(self, data, flags=0):
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500715 self._checkClosed()
Bill Janssen426ea0a2007-08-29 22:35:05 +0000716 if self._sslobj:
717 if flags != 0:
718 raise ValueError(
719 "non-zero flags not allowed in calls to sendall() on %s" %
720 self.__class__)
Bill Janssen934b16d2008-06-28 22:19:33 +0000721 amount = len(data)
722 count = 0
723 while (count < amount):
724 v = self.send(data[count:])
725 count += v
726 return amount
Bill Janssen426ea0a2007-08-29 22:35:05 +0000727 else:
728 return socket.sendall(self, data, flags)
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000729
Ezio Melottib01f5e62010-01-18 09:10:26 +0000730 def recv(self, buflen=1024, flags=0):
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500731 self._checkClosed()
Bill Janssen426ea0a2007-08-29 22:35:05 +0000732 if self._sslobj:
733 if flags != 0:
734 raise ValueError(
Antoine Pitrou448da712010-03-21 19:33:38 +0000735 "non-zero flags not allowed in calls to recv() on %s" %
Bill Janssen426ea0a2007-08-29 22:35:05 +0000736 self.__class__)
Antoine Pitrou448da712010-03-21 19:33:38 +0000737 return self.read(buflen)
Bill Janssen426ea0a2007-08-29 22:35:05 +0000738 else:
Antoine Pitrouf7f390a2010-09-14 14:37:18 +0000739 return self._sock.recv(buflen, flags)
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000740
Ezio Melottib01f5e62010-01-18 09:10:26 +0000741 def recv_into(self, buffer, nbytes=None, flags=0):
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500742 self._checkClosed()
Bill Janssen61c001a2008-09-08 16:37:24 +0000743 if buffer and (nbytes is None):
744 nbytes = len(buffer)
745 elif nbytes is None:
746 nbytes = 1024
747 if self._sslobj:
748 if flags != 0:
749 raise ValueError(
750 "non-zero flags not allowed in calls to recv_into() on %s" %
751 self.__class__)
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500752 return self.read(nbytes, buffer)
Bill Janssen61c001a2008-09-08 16:37:24 +0000753 else:
Antoine Pitrouf7f390a2010-09-14 14:37:18 +0000754 return self._sock.recv_into(buffer, nbytes, flags)
Bill Janssen61c001a2008-09-08 16:37:24 +0000755
Antoine Pitrouf7f390a2010-09-14 14:37:18 +0000756 def recvfrom(self, buflen=1024, flags=0):
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500757 self._checkClosed()
Bill Janssen426ea0a2007-08-29 22:35:05 +0000758 if self._sslobj:
Bill Janssen934b16d2008-06-28 22:19:33 +0000759 raise ValueError("recvfrom not allowed on instances of %s" %
Bill Janssen426ea0a2007-08-29 22:35:05 +0000760 self.__class__)
761 else:
Antoine Pitrouf7f390a2010-09-14 14:37:18 +0000762 return self._sock.recvfrom(buflen, flags)
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000763
Ezio Melottib01f5e62010-01-18 09:10:26 +0000764 def recvfrom_into(self, buffer, nbytes=None, flags=0):
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500765 self._checkClosed()
Bill Janssen61c001a2008-09-08 16:37:24 +0000766 if self._sslobj:
767 raise ValueError("recvfrom_into not allowed on instances of %s" %
768 self.__class__)
769 else:
Antoine Pitrouf7f390a2010-09-14 14:37:18 +0000770 return self._sock.recvfrom_into(buffer, nbytes, flags)
Bill Janssen61c001a2008-09-08 16:37:24 +0000771
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500772
Ezio Melottib01f5e62010-01-18 09:10:26 +0000773 def pending(self):
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500774 self._checkClosed()
Bill Janssen934b16d2008-06-28 22:19:33 +0000775 if self._sslobj:
776 return self._sslobj.pending()
777 else:
778 return 0
779
Ezio Melottib01f5e62010-01-18 09:10:26 +0000780 def shutdown(self, how):
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500781 self._checkClosed()
Bill Janssen296a59d2007-09-16 22:06:00 +0000782 self._sslobj = None
Bill Janssen426ea0a2007-08-29 22:35:05 +0000783 socket.shutdown(self, how)
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000784
Ezio Melottib01f5e62010-01-18 09:10:26 +0000785 def close(self):
Bill Janssen934b16d2008-06-28 22:19:33 +0000786 if self._makefile_refs < 1:
787 self._sslobj = None
788 socket.close(self)
789 else:
790 self._makefile_refs -= 1
791
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500792 def unwrap(self):
793 if self._sslobj:
794 s = self._sslobj.shutdown()
795 self._sslobj = None
796 return s
797 else:
798 raise ValueError("No SSL wrapper around " + str(self))
Bill Janssen934b16d2008-06-28 22:19:33 +0000799
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500800 def _real_close(self):
801 self._sslobj = None
802 socket._real_close(self)
803
804 def do_handshake(self, block=False):
Bill Janssen934b16d2008-06-28 22:19:33 +0000805 """Perform a TLS/SSL handshake."""
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500806 self._check_connected()
807 timeout = self.gettimeout()
808 try:
809 if timeout == 0.0 and block:
810 self.settimeout(None)
811 self._sslobj.do_handshake()
812 finally:
813 self.settimeout(timeout)
Bill Janssen934b16d2008-06-28 22:19:33 +0000814
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500815 if self.context.check_hostname:
816 if not self.server_hostname:
817 raise ValueError("check_hostname needs server_hostname "
818 "argument")
819 match_hostname(self.getpeercert(), self.server_hostname)
Bill Janssen934b16d2008-06-28 22:19:33 +0000820
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500821 def _real_connect(self, addr, connect_ex):
822 if self.server_side:
823 raise ValueError("can't connect in server-side mode")
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000824 # Here we assume that the socket is client-side, and not
825 # connected at the time of the call. We connect it, then wrap it.
Antoine Pitroud3f6ea12011-02-26 23:35:27 +0000826 if self._connected:
Bill Janssen98d19da2007-09-10 21:51:02 +0000827 raise ValueError("attempt to connect already-connected SSLSocket!")
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500828 self._sslobj = self.context._wrap_socket(self._sock, False, self.server_hostname, ssl_sock=self)
Antoine Pitroud3f6ea12011-02-26 23:35:27 +0000829 try:
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500830 if connect_ex:
Antoine Pitrou40f12ab2012-12-28 19:03:43 +0100831 rc = socket.connect_ex(self, addr)
Antoine Pitroud3f6ea12011-02-26 23:35:27 +0000832 else:
Antoine Pitrou40f12ab2012-12-28 19:03:43 +0100833 rc = None
834 socket.connect(self, addr)
835 if not rc:
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500836 self._connected = True
Antoine Pitrou40f12ab2012-12-28 19:03:43 +0100837 if self.do_handshake_on_connect:
838 self.do_handshake()
Antoine Pitrou40f12ab2012-12-28 19:03:43 +0100839 return rc
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500840 except (OSError, ValueError):
Antoine Pitrou40f12ab2012-12-28 19:03:43 +0100841 self._sslobj = None
842 raise
Antoine Pitroud3f6ea12011-02-26 23:35:27 +0000843
844 def connect(self, addr):
845 """Connects to remote ADDR, and then wraps the connection in
846 an SSL channel."""
847 self._real_connect(addr, False)
848
849 def connect_ex(self, addr):
850 """Connects to remote ADDR, and then wraps the connection in
851 an SSL channel."""
852 return self._real_connect(addr, True)
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000853
854 def accept(self):
Bill Janssen24bccf22007-08-30 17:07:28 +0000855 """Accepts a new connection from a remote client, and returns
856 a tuple containing that new connection wrapped with a server-side
857 SSL channel, and the address of the remote client."""
858
Bill Janssen426ea0a2007-08-29 22:35:05 +0000859 newsock, addr = socket.accept(self)
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500860 newsock = self.context.wrap_socket(newsock,
861 do_handshake_on_connect=self.do_handshake_on_connect,
862 suppress_ragged_eofs=self.suppress_ragged_eofs,
863 server_side=True)
864 return newsock, addr
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000865
Bill Janssen296a59d2007-09-16 22:06:00 +0000866 def makefile(self, mode='r', bufsize=-1):
867
Bill Janssen61c001a2008-09-08 16:37:24 +0000868 """Make and return a file-like object that
869 works with the SSL connection. Just use the code
870 from the socket module."""
Bill Janssen296a59d2007-09-16 22:06:00 +0000871
Bill Janssen934b16d2008-06-28 22:19:33 +0000872 self._makefile_refs += 1
Antoine Pitroub558f172010-04-23 23:25:45 +0000873 # close=True so as to decrement the reference count when done with
874 # the file-like object.
875 return _fileobject(self, mode, bufsize, close=True)
Bill Janssen296a59d2007-09-16 22:06:00 +0000876
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500877 def get_channel_binding(self, cb_type="tls-unique"):
878 """Get channel binding data for current connection. Raise ValueError
879 if the requested `cb_type` is not supported. Return bytes of the data
880 or None if the data is not available (e.g. before the handshake).
881 """
882 if cb_type not in CHANNEL_BINDING_TYPES:
883 raise ValueError("Unsupported channel binding type")
884 if cb_type != "tls-unique":
885 raise NotImplementedError(
886 "{0} channel binding type not implemented"
887 .format(cb_type))
888 if self._sslobj is None:
889 return None
890 return self._sslobj.tls_unique_cb()
Bill Janssen296a59d2007-09-16 22:06:00 +0000891
Alex Gaynore98205d2014-09-04 13:33:22 -0700892 def version(self):
893 """
894 Return a string identifying the protocol version used by the
895 current SSL channel, or None if there is no established channel.
896 """
897 if self._sslobj is None:
898 return None
899 return self._sslobj.version()
900
Bill Janssen296a59d2007-09-16 22:06:00 +0000901
Bill Janssen98d19da2007-09-10 21:51:02 +0000902def wrap_socket(sock, keyfile=None, certfile=None,
903 server_side=False, cert_reqs=CERT_NONE,
Bill Janssen934b16d2008-06-28 22:19:33 +0000904 ssl_version=PROTOCOL_SSLv23, ca_certs=None,
905 do_handshake_on_connect=True,
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500906 suppress_ragged_eofs=True,
907 ciphers=None):
Bill Janssen98d19da2007-09-10 21:51:02 +0000908
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500909 return SSLSocket(sock=sock, keyfile=keyfile, certfile=certfile,
Bill Janssen98d19da2007-09-10 21:51:02 +0000910 server_side=server_side, cert_reqs=cert_reqs,
Bill Janssen934b16d2008-06-28 22:19:33 +0000911 ssl_version=ssl_version, ca_certs=ca_certs,
912 do_handshake_on_connect=do_handshake_on_connect,
Antoine Pitrou0a6373c2010-04-17 17:10:38 +0000913 suppress_ragged_eofs=suppress_ragged_eofs,
914 ciphers=ciphers)
Bill Janssen934b16d2008-06-28 22:19:33 +0000915
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000916# some utility functions
917
918def cert_time_to_seconds(cert_time):
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500919 """Return the time in seconds since the Epoch, given the timestring
920 representing the "notBefore" or "notAfter" date from a certificate
921 in ``"%b %d %H:%M:%S %Y %Z"`` strptime format (C locale).
Bill Janssen24bccf22007-08-30 17:07:28 +0000922
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500923 "notBefore" or "notAfter" dates must use UTC (RFC 5280).
Bill Janssen24bccf22007-08-30 17:07:28 +0000924
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500925 Month is one of: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
926 UTC should be specified as GMT (see ASN1_TIME_print())
927 """
928 from time import strptime
929 from calendar import timegm
930
931 months = (
932 "Jan","Feb","Mar","Apr","May","Jun",
933 "Jul","Aug","Sep","Oct","Nov","Dec"
934 )
935 time_format = ' %d %H:%M:%S %Y GMT' # NOTE: no month, fixed GMT
936 try:
937 month_number = months.index(cert_time[:3].title()) + 1
938 except ValueError:
939 raise ValueError('time data %r does not match '
940 'format "%%b%s"' % (cert_time, time_format))
941 else:
942 # found valid month
943 tt = strptime(cert_time[3:], time_format)
944 # return an integer, the previous mktime()-based implementation
945 # returned a float (fractional seconds are always zero here).
946 return timegm((tt[0], month_number) + tt[2:6])
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000947
Bill Janssen296a59d2007-09-16 22:06:00 +0000948PEM_HEADER = "-----BEGIN CERTIFICATE-----"
949PEM_FOOTER = "-----END CERTIFICATE-----"
950
951def DER_cert_to_PEM_cert(der_cert_bytes):
Bill Janssen296a59d2007-09-16 22:06:00 +0000952 """Takes a certificate in binary DER format and returns the
953 PEM version of it as a string."""
954
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500955 f = base64.standard_b64encode(der_cert_bytes).decode('ascii')
956 return (PEM_HEADER + '\n' +
957 textwrap.fill(f, 64) + '\n' +
958 PEM_FOOTER + '\n')
Bill Janssen296a59d2007-09-16 22:06:00 +0000959
960def PEM_cert_to_DER_cert(pem_cert_string):
Bill Janssen296a59d2007-09-16 22:06:00 +0000961 """Takes a certificate in ASCII PEM format and returns the
962 DER-encoded version of it as a byte sequence"""
963
964 if not pem_cert_string.startswith(PEM_HEADER):
965 raise ValueError("Invalid PEM encoding; must start with %s"
966 % PEM_HEADER)
967 if not pem_cert_string.strip().endswith(PEM_FOOTER):
968 raise ValueError("Invalid PEM encoding; must end with %s"
969 % PEM_FOOTER)
970 d = pem_cert_string.strip()[len(PEM_HEADER):-len(PEM_FOOTER)]
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500971 return base64.decodestring(d.encode('ASCII', 'strict'))
Bill Janssen296a59d2007-09-16 22:06:00 +0000972
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500973def get_server_certificate(addr, ssl_version=PROTOCOL_SSLv23, ca_certs=None):
Bill Janssen296a59d2007-09-16 22:06:00 +0000974 """Retrieve the certificate from the server at the specified address,
975 and return it as a PEM-encoded string.
976 If 'ca_certs' is specified, validate the server cert against it.
977 If 'ssl_version' is specified, use it in the connection attempt."""
978
979 host, port = addr
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500980 if ca_certs is not None:
Bill Janssen296a59d2007-09-16 22:06:00 +0000981 cert_reqs = CERT_REQUIRED
982 else:
983 cert_reqs = CERT_NONE
Benjamin Petersondaeb9252014-08-20 14:14:50 -0500984 context = _create_stdlib_context(ssl_version,
985 cert_reqs=cert_reqs,
986 cafile=ca_certs)
987 with closing(create_connection(addr)) as sock:
988 with closing(context.wrap_socket(sock)) as sslsock:
989 dercert = sslsock.getpeercert(True)
Bill Janssen296a59d2007-09-16 22:06:00 +0000990 return DER_cert_to_PEM_cert(dercert)
991
Ezio Melottib01f5e62010-01-18 09:10:26 +0000992def get_protocol_name(protocol_code):
Victor Stinnerb1241f92011-05-10 01:52:03 +0200993 return _PROTOCOL_NAMES.get(protocol_code, '<unknown>')
Bill Janssen296a59d2007-09-16 22:06:00 +0000994
995
Guido van Rossum4f2c3dd2007-08-25 15:08:43 +0000996# a replacement for the old socket.ssl function
997
Ezio Melottib01f5e62010-01-18 09:10:26 +0000998def sslwrap_simple(sock, keyfile=None, certfile=None):
Bill Janssen24bccf22007-08-30 17:07:28 +0000999 """A replacement for the old socket.ssl function. Designed
1000 for compability with Python 2.5 and earlier. Will disappear in
1001 Python 3.0."""
Bill Jansseneb257ac2008-09-29 18:56:38 +00001002 if hasattr(sock, "_sock"):
1003 sock = sock._sock
1004
Benjamin Peterson2f334562014-10-01 23:53:01 -04001005 ctx = SSLContext(PROTOCOL_SSLv23)
1006 if keyfile or certfile:
1007 ctx.load_cert_chain(certfile, keyfile)
1008 ssl_sock = ctx._wrap_socket(sock, server_side=False)
Bill Jansseneb257ac2008-09-29 18:56:38 +00001009 try:
1010 sock.getpeername()
Benjamin Peterson941db4d2008-12-31 04:08:55 +00001011 except socket_error:
Bill Jansseneb257ac2008-09-29 18:56:38 +00001012 # no, no connection yet
1013 pass
1014 else:
1015 # yes, do the handshake
1016 ssl_sock.do_handshake()
1017
Bill Janssen934b16d2008-06-28 22:19:33 +00001018 return ssl_sock