2007-08-25 12:08:43 -03:00
|
|
|
# Wrapper module for _ssl, providing some additional facilities
|
|
|
|
# implemented in Python. Written by Bill Janssen.
|
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
"""This module provides some more Pythonic support for SSL.
|
2007-08-25 12:08:43 -03:00
|
|
|
|
|
|
|
Object types:
|
|
|
|
|
2007-09-10 18:51:02 -03:00
|
|
|
SSLSocket -- subtype of socket.socket which does SSL over the socket
|
2007-08-25 12:08:43 -03:00
|
|
|
|
|
|
|
Exceptions:
|
|
|
|
|
2007-09-10 18:51:02 -03:00
|
|
|
SSLError -- exception raised for I/O errors
|
2007-08-25 12:08:43 -03:00
|
|
|
|
|
|
|
Functions:
|
|
|
|
|
|
|
|
cert_time_to_seconds -- convert time string used for certificate
|
|
|
|
notBefore and notAfter functions to integer
|
|
|
|
seconds past the Epoch (the time values
|
|
|
|
returned from time.time())
|
|
|
|
|
|
|
|
fetch_server_certificate (HOST, PORT) -- fetch the certificate provided
|
|
|
|
by the server running on HOST at port PORT. No
|
|
|
|
validation of the certificate is performed.
|
|
|
|
|
|
|
|
Integer constants:
|
|
|
|
|
|
|
|
SSL_ERROR_ZERO_RETURN
|
|
|
|
SSL_ERROR_WANT_READ
|
|
|
|
SSL_ERROR_WANT_WRITE
|
|
|
|
SSL_ERROR_WANT_X509_LOOKUP
|
|
|
|
SSL_ERROR_SYSCALL
|
|
|
|
SSL_ERROR_SSL
|
|
|
|
SSL_ERROR_WANT_CONNECT
|
|
|
|
|
|
|
|
SSL_ERROR_EOF
|
|
|
|
SSL_ERROR_INVALID_ERROR_CODE
|
|
|
|
|
|
|
|
The following group define certificate requirements that one side is
|
|
|
|
allowing/requiring from the other side:
|
|
|
|
|
|
|
|
CERT_NONE - no certificates from the other side are required (or will
|
|
|
|
be looked at if provided)
|
|
|
|
CERT_OPTIONAL - certificates are not required, but if provided will be
|
|
|
|
validated, and if validation fails, the connection will
|
|
|
|
also fail
|
|
|
|
CERT_REQUIRED - certificates are required, and will be validated, and
|
|
|
|
if validation fails, the connection will also fail
|
|
|
|
|
|
|
|
The following constants identify various SSL protocol variants:
|
|
|
|
|
|
|
|
PROTOCOL_SSLv2
|
|
|
|
PROTOCOL_SSLv3
|
|
|
|
PROTOCOL_SSLv23
|
2016-09-05 18:37:13 -03:00
|
|
|
PROTOCOL_TLS
|
2007-08-25 12:08:43 -03:00
|
|
|
PROTOCOL_TLSv1
|
2014-08-20 16:14:50 -03:00
|
|
|
PROTOCOL_TLSv1_1
|
|
|
|
PROTOCOL_TLSv1_2
|
|
|
|
|
|
|
|
The following constants identify various SSL alert message descriptions as per
|
|
|
|
http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6
|
|
|
|
|
|
|
|
ALERT_DESCRIPTION_CLOSE_NOTIFY
|
|
|
|
ALERT_DESCRIPTION_UNEXPECTED_MESSAGE
|
|
|
|
ALERT_DESCRIPTION_BAD_RECORD_MAC
|
|
|
|
ALERT_DESCRIPTION_RECORD_OVERFLOW
|
|
|
|
ALERT_DESCRIPTION_DECOMPRESSION_FAILURE
|
|
|
|
ALERT_DESCRIPTION_HANDSHAKE_FAILURE
|
|
|
|
ALERT_DESCRIPTION_BAD_CERTIFICATE
|
|
|
|
ALERT_DESCRIPTION_UNSUPPORTED_CERTIFICATE
|
|
|
|
ALERT_DESCRIPTION_CERTIFICATE_REVOKED
|
|
|
|
ALERT_DESCRIPTION_CERTIFICATE_EXPIRED
|
|
|
|
ALERT_DESCRIPTION_CERTIFICATE_UNKNOWN
|
|
|
|
ALERT_DESCRIPTION_ILLEGAL_PARAMETER
|
|
|
|
ALERT_DESCRIPTION_UNKNOWN_CA
|
|
|
|
ALERT_DESCRIPTION_ACCESS_DENIED
|
|
|
|
ALERT_DESCRIPTION_DECODE_ERROR
|
|
|
|
ALERT_DESCRIPTION_DECRYPT_ERROR
|
|
|
|
ALERT_DESCRIPTION_PROTOCOL_VERSION
|
|
|
|
ALERT_DESCRIPTION_INSUFFICIENT_SECURITY
|
|
|
|
ALERT_DESCRIPTION_INTERNAL_ERROR
|
|
|
|
ALERT_DESCRIPTION_USER_CANCELLED
|
|
|
|
ALERT_DESCRIPTION_NO_RENEGOTIATION
|
|
|
|
ALERT_DESCRIPTION_UNSUPPORTED_EXTENSION
|
|
|
|
ALERT_DESCRIPTION_CERTIFICATE_UNOBTAINABLE
|
|
|
|
ALERT_DESCRIPTION_UNRECOGNIZED_NAME
|
|
|
|
ALERT_DESCRIPTION_BAD_CERTIFICATE_STATUS_RESPONSE
|
|
|
|
ALERT_DESCRIPTION_BAD_CERTIFICATE_HASH_VALUE
|
|
|
|
ALERT_DESCRIPTION_UNKNOWN_PSK_IDENTITY
|
2007-08-25 12:08:43 -03:00
|
|
|
"""
|
|
|
|
|
2008-02-23 13:40:11 -04:00
|
|
|
import textwrap
|
2014-08-20 16:14:50 -03:00
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
from collections import namedtuple
|
|
|
|
from contextlib import closing
|
2007-08-25 12:08:43 -03:00
|
|
|
|
|
|
|
import _ssl # if we can't import it, let the error propagate
|
2007-09-10 18:51:02 -03:00
|
|
|
|
2010-04-05 18:35:07 -03:00
|
|
|
from _ssl import OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_INFO, OPENSSL_VERSION
|
2014-08-20 16:14:50 -03:00
|
|
|
from _ssl import _SSLContext
|
|
|
|
from _ssl import (
|
|
|
|
SSLError, SSLZeroReturnError, SSLWantReadError, SSLWantWriteError,
|
|
|
|
SSLSyscallError, SSLEOFError,
|
|
|
|
)
|
2007-08-25 12:08:43 -03:00
|
|
|
from _ssl import CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED
|
2014-08-20 16:14:50 -03:00
|
|
|
from _ssl import txt2obj as _txt2obj, nid2obj as _nid2obj
|
2015-01-06 08:53:37 -04:00
|
|
|
from _ssl import RAND_status, RAND_add
|
|
|
|
try:
|
|
|
|
from _ssl import RAND_egd
|
|
|
|
except ImportError:
|
|
|
|
# LibreSSL does not provide RAND_egd
|
|
|
|
pass
|
2014-08-20 16:14:50 -03:00
|
|
|
|
|
|
|
def _import_symbols(prefix):
|
|
|
|
for n in dir(_ssl):
|
|
|
|
if n.startswith(prefix):
|
|
|
|
globals()[n] = getattr(_ssl, n)
|
|
|
|
|
|
|
|
_import_symbols('OP_')
|
|
|
|
_import_symbols('ALERT_DESCRIPTION_')
|
|
|
|
_import_symbols('SSL_ERROR_')
|
|
|
|
_import_symbols('PROTOCOL_')
|
2015-03-05 00:18:57 -04:00
|
|
|
_import_symbols('VERIFY_')
|
2014-08-20 16:14:50 -03:00
|
|
|
|
2017-09-08 02:31:17 -03:00
|
|
|
from _ssl import HAS_SNI, HAS_ECDH, HAS_NPN, HAS_ALPN, HAS_TLSv1_3
|
2014-08-20 16:14:50 -03:00
|
|
|
|
|
|
|
from _ssl import _OPENSSL_API_VERSION
|
|
|
|
|
2016-09-05 18:37:13 -03:00
|
|
|
_PROTOCOL_NAMES = {value: name for name, value in globals().items()
|
|
|
|
if name.startswith('PROTOCOL_')
|
|
|
|
and name != 'PROTOCOL_SSLv23'}
|
|
|
|
PROTOCOL_SSLv23 = PROTOCOL_TLS
|
2014-08-20 16:14:50 -03:00
|
|
|
|
2011-05-09 20:52:03 -03:00
|
|
|
try:
|
2012-01-03 17:46:48 -04:00
|
|
|
_SSLv2_IF_EXISTS = PROTOCOL_SSLv2
|
2014-08-20 16:14:50 -03:00
|
|
|
except NameError:
|
2012-01-03 17:46:48 -04:00
|
|
|
_SSLv2_IF_EXISTS = None
|
2007-08-29 19:35:05 -03:00
|
|
|
|
2010-04-23 19:54:59 -03:00
|
|
|
from socket import socket, _fileobject, _delegate_methods, error as socket_error
|
2014-08-20 16:14:50 -03:00
|
|
|
if sys.platform == "win32":
|
|
|
|
from _ssl import enum_certificates, enum_crls
|
|
|
|
|
|
|
|
from socket import socket, AF_INET, SOCK_STREAM, create_connection
|
|
|
|
from socket import SOL_SOCKET, SO_TYPE
|
2007-09-16 19:06:00 -03:00
|
|
|
import base64 # for DER-to-PEM translation
|
2010-04-26 14:23:33 -03:00
|
|
|
import errno
|
2016-05-26 16:17:21 -03:00
|
|
|
import warnings
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
if _ssl.HAS_TLS_UNIQUE:
|
|
|
|
CHANNEL_BINDING_TYPES = ['tls-unique']
|
|
|
|
else:
|
|
|
|
CHANNEL_BINDING_TYPES = []
|
|
|
|
|
2016-09-06 15:06:47 -03:00
|
|
|
|
2012-01-03 17:46:48 -04:00
|
|
|
# Disable weak or insecure ciphers by default
|
|
|
|
# (OpenSSL's default setting is 'DEFAULT:!aNULL:!eNULL')
|
2014-08-20 16:14:50 -03:00
|
|
|
# Enable a better set of ciphers by default
|
|
|
|
# This list has been explicitly chosen to:
|
2017-09-08 02:31:17 -03:00
|
|
|
# * TLS 1.3 ChaCha20 and AES-GCM cipher suites
|
2014-08-20 16:14:50 -03:00
|
|
|
# * Prefer cipher suites that offer perfect forward secrecy (DHE/ECDHE)
|
|
|
|
# * Prefer ECDHE over DHE for better performance
|
2016-09-06 15:06:47 -03:00
|
|
|
# * Prefer AEAD over CBC for better performance and security
|
|
|
|
# * Prefer AES-GCM over ChaCha20 because most platforms have AES-NI
|
|
|
|
# (ChaCha20 needs OpenSSL 1.1.0 or patched 1.0.2)
|
|
|
|
# * Prefer any AES-GCM and ChaCha20 over any AES-CBC for better
|
|
|
|
# performance and security
|
2014-08-20 16:14:50 -03:00
|
|
|
# * Then Use HIGH cipher suites as a fallback
|
2016-09-06 15:06:47 -03:00
|
|
|
# * Disable NULL authentication, NULL encryption, 3DES and MD5 MACs
|
|
|
|
# for security reasons
|
2014-08-20 16:14:50 -03:00
|
|
|
_DEFAULT_CIPHERS = (
|
2017-09-08 02:31:17 -03:00
|
|
|
'TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:'
|
|
|
|
'TLS13-AES-128-GCM-SHA256:'
|
2016-09-06 15:06:47 -03:00
|
|
|
'ECDH+AESGCM:ECDH+CHACHA20:DH+AESGCM:DH+CHACHA20:ECDH+AES256:DH+AES256:'
|
|
|
|
'ECDH+AES128:DH+AES:ECDH+HIGH:DH+HIGH:RSA+AESGCM:RSA+AES:RSA+HIGH:'
|
|
|
|
'!aNULL:!eNULL:!MD5:!3DES'
|
|
|
|
)
|
2014-08-20 16:14:50 -03:00
|
|
|
|
|
|
|
# Restricted and more secure ciphers for the server side
|
|
|
|
# This list has been explicitly chosen to:
|
2017-09-08 02:31:17 -03:00
|
|
|
# * TLS 1.3 ChaCha20 and AES-GCM cipher suites
|
2014-08-20 16:14:50 -03:00
|
|
|
# * Prefer cipher suites that offer perfect forward secrecy (DHE/ECDHE)
|
|
|
|
# * Prefer ECDHE over DHE for better performance
|
2016-09-06 15:06:47 -03:00
|
|
|
# * Prefer AEAD over CBC for better performance and security
|
|
|
|
# * Prefer AES-GCM over ChaCha20 because most platforms have AES-NI
|
|
|
|
# * Prefer any AES-GCM and ChaCha20 over any AES-CBC for better
|
|
|
|
# performance and security
|
2014-08-20 16:14:50 -03:00
|
|
|
# * Then Use HIGH cipher suites as a fallback
|
2016-09-06 15:06:47 -03:00
|
|
|
# * Disable NULL authentication, NULL encryption, MD5 MACs, DSS, RC4, and
|
|
|
|
# 3DES for security reasons
|
2014-08-20 16:14:50 -03:00
|
|
|
_RESTRICTED_SERVER_CIPHERS = (
|
2017-09-08 02:31:17 -03:00
|
|
|
'TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:'
|
|
|
|
'TLS13-AES-128-GCM-SHA256:'
|
2016-09-06 15:06:47 -03:00
|
|
|
'ECDH+AESGCM:ECDH+CHACHA20:DH+AESGCM:DH+CHACHA20:ECDH+AES256:DH+AES256:'
|
|
|
|
'ECDH+AES128:DH+AES:ECDH+HIGH:DH+HIGH:RSA+AESGCM:RSA+AES:RSA+HIGH:'
|
|
|
|
'!aNULL:!eNULL:!MD5:!DSS:!RC4:!3DES'
|
2014-08-20 16:14:50 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CertificateError(ValueError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def _dnsname_match(dn, hostname, max_wildcards=1):
|
|
|
|
"""Matching according to RFC 6125, section 6.4.3
|
|
|
|
|
|
|
|
http://tools.ietf.org/html/rfc6125#section-6.4.3
|
|
|
|
"""
|
|
|
|
pats = []
|
|
|
|
if not dn:
|
|
|
|
return False
|
|
|
|
|
|
|
|
pieces = dn.split(r'.')
|
|
|
|
leftmost = pieces[0]
|
|
|
|
remainder = pieces[1:]
|
|
|
|
|
|
|
|
wildcards = leftmost.count('*')
|
|
|
|
if wildcards > max_wildcards:
|
|
|
|
# Issue #17980: avoid denials of service by refusing more
|
|
|
|
# than one wildcard per fragment. A survery of established
|
|
|
|
# policy among SSL implementations showed it to be a
|
|
|
|
# reasonable choice.
|
|
|
|
raise CertificateError(
|
|
|
|
"too many wildcards in certificate DNS name: " + repr(dn))
|
|
|
|
|
|
|
|
# speed up common case w/o wildcards
|
|
|
|
if not wildcards:
|
|
|
|
return dn.lower() == hostname.lower()
|
|
|
|
|
|
|
|
# RFC 6125, section 6.4.3, subitem 1.
|
|
|
|
# The client SHOULD NOT attempt to match a presented identifier in which
|
|
|
|
# the wildcard character comprises a label other than the left-most label.
|
|
|
|
if leftmost == '*':
|
|
|
|
# When '*' is a fragment by itself, it matches a non-empty dotless
|
|
|
|
# fragment.
|
|
|
|
pats.append('[^.]+')
|
|
|
|
elif leftmost.startswith('xn--') or hostname.startswith('xn--'):
|
|
|
|
# RFC 6125, section 6.4.3, subitem 3.
|
|
|
|
# The client SHOULD NOT attempt to match a presented identifier
|
|
|
|
# where the wildcard character is embedded within an A-label or
|
|
|
|
# U-label of an internationalized domain name.
|
|
|
|
pats.append(re.escape(leftmost))
|
|
|
|
else:
|
|
|
|
# Otherwise, '*' matches any dotless string, e.g. www*
|
|
|
|
pats.append(re.escape(leftmost).replace(r'\*', '[^.]*'))
|
|
|
|
|
|
|
|
# add the remaining fragments, ignore any wildcards
|
|
|
|
for frag in remainder:
|
|
|
|
pats.append(re.escape(frag))
|
|
|
|
|
|
|
|
pat = re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE)
|
|
|
|
return pat.match(hostname)
|
|
|
|
|
|
|
|
|
|
|
|
def match_hostname(cert, hostname):
|
|
|
|
"""Verify that *cert* (in decoded format as returned by
|
|
|
|
SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125
|
|
|
|
rules are followed, but IP addresses are not accepted for *hostname*.
|
|
|
|
|
|
|
|
CertificateError is raised on failure. On success, the function
|
|
|
|
returns nothing.
|
|
|
|
"""
|
|
|
|
if not cert:
|
|
|
|
raise ValueError("empty or no certificate, match_hostname needs a "
|
|
|
|
"SSL socket or SSL context with either "
|
|
|
|
"CERT_OPTIONAL or CERT_REQUIRED")
|
|
|
|
dnsnames = []
|
|
|
|
san = cert.get('subjectAltName', ())
|
|
|
|
for key, value in san:
|
|
|
|
if key == 'DNS':
|
|
|
|
if _dnsname_match(value, hostname):
|
|
|
|
return
|
|
|
|
dnsnames.append(value)
|
|
|
|
if not dnsnames:
|
|
|
|
# The subject is only checked when there is no dNSName entry
|
|
|
|
# in subjectAltName
|
|
|
|
for sub in cert.get('subject', ()):
|
|
|
|
for key, value in sub:
|
|
|
|
# XXX according to RFC 2818, the most specific Common Name
|
|
|
|
# must be used.
|
|
|
|
if key == 'commonName':
|
|
|
|
if _dnsname_match(value, hostname):
|
|
|
|
return
|
|
|
|
dnsnames.append(value)
|
|
|
|
if len(dnsnames) > 1:
|
|
|
|
raise CertificateError("hostname %r "
|
|
|
|
"doesn't match either of %s"
|
|
|
|
% (hostname, ', '.join(map(repr, dnsnames))))
|
|
|
|
elif len(dnsnames) == 1:
|
|
|
|
raise CertificateError("hostname %r "
|
|
|
|
"doesn't match %r"
|
|
|
|
% (hostname, dnsnames[0]))
|
|
|
|
else:
|
|
|
|
raise CertificateError("no appropriate commonName or "
|
|
|
|
"subjectAltName fields were found")
|
|
|
|
|
|
|
|
|
|
|
|
DefaultVerifyPaths = namedtuple("DefaultVerifyPaths",
|
|
|
|
"cafile capath openssl_cafile_env openssl_cafile openssl_capath_env "
|
|
|
|
"openssl_capath")
|
|
|
|
|
|
|
|
def get_default_verify_paths():
|
|
|
|
"""Return paths to default cafile and capath.
|
|
|
|
"""
|
|
|
|
parts = _ssl.get_default_verify_paths()
|
|
|
|
|
|
|
|
# environment vars shadow paths
|
|
|
|
cafile = os.environ.get(parts[0], parts[1])
|
|
|
|
capath = os.environ.get(parts[2], parts[3])
|
|
|
|
|
|
|
|
return DefaultVerifyPaths(cafile if os.path.isfile(cafile) else None,
|
|
|
|
capath if os.path.isdir(capath) else None,
|
|
|
|
*parts)
|
|
|
|
|
|
|
|
|
|
|
|
class _ASN1Object(namedtuple("_ASN1Object", "nid shortname longname oid")):
|
|
|
|
"""ASN.1 object identifier lookup
|
|
|
|
"""
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def __new__(cls, oid):
|
|
|
|
return super(_ASN1Object, cls).__new__(cls, *_txt2obj(oid, name=False))
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def fromnid(cls, nid):
|
|
|
|
"""Create _ASN1Object from OpenSSL numeric ID
|
|
|
|
"""
|
|
|
|
return super(_ASN1Object, cls).__new__(cls, *_nid2obj(nid))
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def fromname(cls, name):
|
|
|
|
"""Create _ASN1Object from short name, long name or OID
|
|
|
|
"""
|
|
|
|
return super(_ASN1Object, cls).__new__(cls, *_txt2obj(name, name=True))
|
|
|
|
|
|
|
|
|
|
|
|
class Purpose(_ASN1Object):
|
|
|
|
"""SSLContext purpose flags with X509v3 Extended Key Usage objects
|
|
|
|
"""
|
|
|
|
|
|
|
|
Purpose.SERVER_AUTH = Purpose('1.3.6.1.5.5.7.3.1')
|
|
|
|
Purpose.CLIENT_AUTH = Purpose('1.3.6.1.5.5.7.3.2')
|
|
|
|
|
|
|
|
|
|
|
|
class SSLContext(_SSLContext):
|
|
|
|
"""An SSLContext holds various SSL-related configuration options and
|
|
|
|
data, such as certificates and possibly a private key."""
|
|
|
|
|
|
|
|
__slots__ = ('protocol', '__weakref__')
|
|
|
|
_windows_cert_stores = ("CA", "ROOT")
|
|
|
|
|
|
|
|
def __new__(cls, protocol, *args, **kwargs):
|
|
|
|
self = _SSLContext.__new__(cls, protocol)
|
|
|
|
if protocol != _SSLv2_IF_EXISTS:
|
|
|
|
self.set_ciphers(_DEFAULT_CIPHERS)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __init__(self, protocol):
|
|
|
|
self.protocol = protocol
|
|
|
|
|
|
|
|
def wrap_socket(self, sock, server_side=False,
|
|
|
|
do_handshake_on_connect=True,
|
|
|
|
suppress_ragged_eofs=True,
|
|
|
|
server_hostname=None):
|
|
|
|
return SSLSocket(sock=sock, server_side=server_side,
|
|
|
|
do_handshake_on_connect=do_handshake_on_connect,
|
|
|
|
suppress_ragged_eofs=suppress_ragged_eofs,
|
|
|
|
server_hostname=server_hostname,
|
|
|
|
_context=self)
|
|
|
|
|
|
|
|
def set_npn_protocols(self, npn_protocols):
|
|
|
|
protos = bytearray()
|
|
|
|
for protocol in npn_protocols:
|
|
|
|
b = protocol.encode('ascii')
|
|
|
|
if len(b) == 0 or len(b) > 255:
|
|
|
|
raise SSLError('NPN protocols must be 1 to 255 in length')
|
|
|
|
protos.append(len(b))
|
|
|
|
protos.extend(b)
|
|
|
|
|
|
|
|
self._set_npn_protocols(protos)
|
|
|
|
|
2015-01-23 17:35:37 -04:00
|
|
|
def set_alpn_protocols(self, alpn_protocols):
|
|
|
|
protos = bytearray()
|
|
|
|
for protocol in alpn_protocols:
|
|
|
|
b = protocol.encode('ascii')
|
|
|
|
if len(b) == 0 or len(b) > 255:
|
|
|
|
raise SSLError('ALPN protocols must be 1 to 255 in length')
|
|
|
|
protos.append(len(b))
|
|
|
|
protos.extend(b)
|
|
|
|
|
|
|
|
self._set_alpn_protocols(protos)
|
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
def _load_windows_store_certs(self, storename, purpose):
|
|
|
|
certs = bytearray()
|
2016-05-26 16:17:21 -03:00
|
|
|
try:
|
|
|
|
for cert, encoding, trust in enum_certificates(storename):
|
|
|
|
# CA certs are never PKCS#7 encoded
|
|
|
|
if encoding == "x509_asn":
|
|
|
|
if trust is True or purpose.oid in trust:
|
|
|
|
certs.extend(cert)
|
|
|
|
except OSError:
|
|
|
|
warnings.warn("unable to enumerate Windows certificate store")
|
2016-03-17 19:02:19 -03:00
|
|
|
if certs:
|
|
|
|
self.load_verify_locations(cadata=certs)
|
2014-08-20 16:14:50 -03:00
|
|
|
return certs
|
|
|
|
|
|
|
|
def load_default_certs(self, purpose=Purpose.SERVER_AUTH):
|
|
|
|
if not isinstance(purpose, _ASN1Object):
|
|
|
|
raise TypeError(purpose)
|
|
|
|
if sys.platform == "win32":
|
|
|
|
for storename in self._windows_cert_stores:
|
|
|
|
self._load_windows_store_certs(storename, purpose)
|
2014-10-03 18:27:05 -03:00
|
|
|
self.set_default_verify_paths()
|
2014-08-20 16:14:50 -03:00
|
|
|
|
|
|
|
|
|
|
|
def create_default_context(purpose=Purpose.SERVER_AUTH, cafile=None,
|
|
|
|
capath=None, cadata=None):
|
|
|
|
"""Create a SSLContext object with default settings.
|
|
|
|
|
|
|
|
NOTE: The protocol and settings may change anytime without prior
|
|
|
|
deprecation. The values represent a fair balance between maximum
|
|
|
|
compatibility and security.
|
|
|
|
"""
|
|
|
|
if not isinstance(purpose, _ASN1Object):
|
|
|
|
raise TypeError(purpose)
|
|
|
|
|
2016-09-05 18:37:13 -03:00
|
|
|
context = SSLContext(PROTOCOL_TLS)
|
2014-08-20 16:14:50 -03:00
|
|
|
|
|
|
|
# SSLv2 considered harmful.
|
|
|
|
context.options |= OP_NO_SSLv2
|
|
|
|
|
|
|
|
# SSLv3 has problematic security and is only required for really old
|
|
|
|
# clients such as IE6 on Windows XP
|
|
|
|
context.options |= OP_NO_SSLv3
|
|
|
|
|
|
|
|
# disable compression to prevent CRIME attacks (OpenSSL 1.0+)
|
|
|
|
context.options |= getattr(_ssl, "OP_NO_COMPRESSION", 0)
|
|
|
|
|
|
|
|
if purpose == Purpose.SERVER_AUTH:
|
|
|
|
# verify certs and host name in client mode
|
|
|
|
context.verify_mode = CERT_REQUIRED
|
|
|
|
context.check_hostname = True
|
|
|
|
elif purpose == Purpose.CLIENT_AUTH:
|
|
|
|
# Prefer the server's ciphers by default so that we get stronger
|
|
|
|
# encryption
|
|
|
|
context.options |= getattr(_ssl, "OP_CIPHER_SERVER_PREFERENCE", 0)
|
|
|
|
|
|
|
|
# Use single use keys in order to improve forward secrecy
|
|
|
|
context.options |= getattr(_ssl, "OP_SINGLE_DH_USE", 0)
|
|
|
|
context.options |= getattr(_ssl, "OP_SINGLE_ECDH_USE", 0)
|
|
|
|
|
|
|
|
# disallow ciphers with known vulnerabilities
|
|
|
|
context.set_ciphers(_RESTRICTED_SERVER_CIPHERS)
|
|
|
|
|
|
|
|
if cafile or capath or cadata:
|
|
|
|
context.load_verify_locations(cafile, capath, cadata)
|
|
|
|
elif context.verify_mode != CERT_NONE:
|
|
|
|
# no explicit cafile, capath or cadata but the verify mode is
|
|
|
|
# CERT_OPTIONAL or CERT_REQUIRED. Let's try to load default system
|
|
|
|
# root CA certificates for the given purpose. This may fail silently.
|
|
|
|
context.load_default_certs(purpose)
|
|
|
|
return context
|
|
|
|
|
2016-09-05 18:37:13 -03:00
|
|
|
def _create_unverified_context(protocol=PROTOCOL_TLS, cert_reqs=None,
|
2014-08-20 16:14:50 -03:00
|
|
|
check_hostname=False, purpose=Purpose.SERVER_AUTH,
|
|
|
|
certfile=None, keyfile=None,
|
|
|
|
cafile=None, capath=None, cadata=None):
|
|
|
|
"""Create a SSLContext object for Python stdlib modules
|
|
|
|
|
|
|
|
All Python stdlib modules shall use this function to create SSLContext
|
|
|
|
objects in order to keep common settings in one place. The configuration
|
|
|
|
is less restrict than create_default_context()'s to increase backward
|
|
|
|
compatibility.
|
|
|
|
"""
|
|
|
|
if not isinstance(purpose, _ASN1Object):
|
|
|
|
raise TypeError(purpose)
|
|
|
|
|
|
|
|
context = SSLContext(protocol)
|
|
|
|
# SSLv2 considered harmful.
|
|
|
|
context.options |= OP_NO_SSLv2
|
2014-10-17 14:28:30 -03:00
|
|
|
# SSLv3 has problematic security and is only required for really old
|
|
|
|
# clients such as IE6 on Windows XP
|
|
|
|
context.options |= OP_NO_SSLv3
|
2014-08-20 16:14:50 -03:00
|
|
|
|
|
|
|
if cert_reqs is not None:
|
|
|
|
context.verify_mode = cert_reqs
|
|
|
|
context.check_hostname = check_hostname
|
|
|
|
|
|
|
|
if keyfile and not certfile:
|
|
|
|
raise ValueError("certfile must be specified")
|
|
|
|
if certfile or keyfile:
|
|
|
|
context.load_cert_chain(certfile, keyfile)
|
|
|
|
|
|
|
|
# load CA root certs
|
|
|
|
if cafile or capath or cadata:
|
|
|
|
context.load_verify_locations(cafile, capath, cadata)
|
|
|
|
elif context.verify_mode != CERT_NONE:
|
|
|
|
# no explicit cafile, capath or cadata but the verify mode is
|
|
|
|
# CERT_OPTIONAL or CERT_REQUIRED. Let's try to load default system
|
|
|
|
# root CA certificates for the given purpose. This may fail silently.
|
|
|
|
context.load_default_certs(purpose)
|
|
|
|
|
|
|
|
return context
|
2012-01-03 17:46:48 -04:00
|
|
|
|
2014-11-23 23:02:02 -04:00
|
|
|
# Backwards compatibility alias, even though it's not a public name.
|
|
|
|
_create_stdlib_context = _create_unverified_context
|
|
|
|
|
2016-03-20 09:39:15 -03:00
|
|
|
# PEP 493: Verify HTTPS by default, but allow envvar to override that
|
|
|
|
_https_verify_envvar = 'PYTHONHTTPSVERIFY'
|
|
|
|
|
|
|
|
def _get_https_context_factory():
|
|
|
|
if not sys.flags.ignore_environment:
|
|
|
|
config_setting = os.environ.get(_https_verify_envvar)
|
|
|
|
if config_setting == '0':
|
|
|
|
return _create_unverified_context
|
|
|
|
return create_default_context
|
|
|
|
|
|
|
|
_create_default_https_context = _get_https_context_factory()
|
|
|
|
|
|
|
|
# PEP 493: "private" API to configure HTTPS defaults without monkeypatching
|
|
|
|
def _https_verify_certificates(enable=True):
|
|
|
|
"""Verify server HTTPS certificates by default?"""
|
|
|
|
global _create_default_https_context
|
|
|
|
if enable:
|
|
|
|
_create_default_https_context = create_default_context
|
|
|
|
else:
|
|
|
|
_create_default_https_context = _create_unverified_context
|
|
|
|
|
2014-11-23 23:02:02 -04:00
|
|
|
|
2010-01-18 05:10:26 -04:00
|
|
|
class SSLSocket(socket):
|
2007-08-29 19:35:05 -03:00
|
|
|
"""This class implements a subtype of socket.socket that wraps
|
|
|
|
the underlying OS socket in an SSL context when necessary, and
|
|
|
|
provides read and write methods over that channel."""
|
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
def __init__(self, sock=None, keyfile=None, certfile=None,
|
2007-08-25 12:08:43 -03:00
|
|
|
server_side=False, cert_reqs=CERT_NONE,
|
2016-09-05 18:37:13 -03:00
|
|
|
ssl_version=PROTOCOL_TLS, ca_certs=None,
|
2008-06-28 19:19:33 -03:00
|
|
|
do_handshake_on_connect=True,
|
2014-08-20 16:14:50 -03:00
|
|
|
family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None,
|
|
|
|
suppress_ragged_eofs=True, npn_protocols=None, ciphers=None,
|
|
|
|
server_hostname=None,
|
|
|
|
_context=None):
|
|
|
|
|
2014-11-23 13:16:48 -04:00
|
|
|
self._makefile_refs = 0
|
2014-08-20 16:14:50 -03:00
|
|
|
if _context:
|
|
|
|
self._context = _context
|
|
|
|
else:
|
|
|
|
if server_side and not certfile:
|
|
|
|
raise ValueError("certfile must be specified for server-side "
|
|
|
|
"operations")
|
|
|
|
if keyfile and not certfile:
|
|
|
|
raise ValueError("certfile must be specified")
|
|
|
|
if certfile and not keyfile:
|
|
|
|
keyfile = certfile
|
|
|
|
self._context = SSLContext(ssl_version)
|
|
|
|
self._context.verify_mode = cert_reqs
|
|
|
|
if ca_certs:
|
|
|
|
self._context.load_verify_locations(ca_certs)
|
|
|
|
if certfile:
|
|
|
|
self._context.load_cert_chain(certfile, keyfile)
|
|
|
|
if npn_protocols:
|
|
|
|
self._context.set_npn_protocols(npn_protocols)
|
|
|
|
if ciphers:
|
|
|
|
self._context.set_ciphers(ciphers)
|
|
|
|
self.keyfile = keyfile
|
|
|
|
self.certfile = certfile
|
|
|
|
self.cert_reqs = cert_reqs
|
|
|
|
self.ssl_version = ssl_version
|
|
|
|
self.ca_certs = ca_certs
|
|
|
|
self.ciphers = ciphers
|
2013-12-28 12:26:33 -04:00
|
|
|
# Can't use sock.type as other flags (such as SOCK_NONBLOCK) get
|
|
|
|
# mixed in.
|
|
|
|
if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
|
|
|
|
raise NotImplementedError("only stream sockets are supported")
|
2007-08-25 12:08:43 -03:00
|
|
|
socket.__init__(self, _sock=sock._sock)
|
2010-04-23 19:54:59 -03:00
|
|
|
# The initializer for socket overrides the methods send(), recv(), etc.
|
|
|
|
# in the instancce, which we don't need -- but we want to provide the
|
|
|
|
# methods defined in SSLSocket.
|
|
|
|
for attr in _delegate_methods:
|
|
|
|
try:
|
|
|
|
delattr(self, attr)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2014-08-20 16:14:50 -03:00
|
|
|
if server_side and server_hostname:
|
|
|
|
raise ValueError("server_hostname can only be specified "
|
|
|
|
"in client mode")
|
|
|
|
if self._context.check_hostname and not server_hostname:
|
2014-11-23 22:13:31 -04:00
|
|
|
raise ValueError("check_hostname requires server_hostname")
|
2014-08-20 16:14:50 -03:00
|
|
|
self.server_side = server_side
|
|
|
|
self.server_hostname = server_hostname
|
|
|
|
self.do_handshake_on_connect = do_handshake_on_connect
|
|
|
|
self.suppress_ragged_eofs = suppress_ragged_eofs
|
2008-06-28 19:19:33 -03:00
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
# See if we are connected
|
2007-08-29 19:35:05 -03:00
|
|
|
try:
|
2014-08-20 16:14:50 -03:00
|
|
|
self.getpeername()
|
|
|
|
except socket_error as e:
|
2010-04-26 14:23:33 -03:00
|
|
|
if e.errno != errno.ENOTCONN:
|
|
|
|
raise
|
2014-08-20 16:14:50 -03:00
|
|
|
connected = False
|
2007-08-25 12:08:43 -03:00
|
|
|
else:
|
2014-08-20 16:14:50 -03:00
|
|
|
connected = True
|
|
|
|
|
|
|
|
self._closed = False
|
|
|
|
self._sslobj = None
|
|
|
|
self._connected = connected
|
|
|
|
if connected:
|
|
|
|
# create the SSL object
|
|
|
|
try:
|
|
|
|
self._sslobj = self._context._wrap_socket(self._sock, server_side,
|
|
|
|
server_hostname, ssl_sock=self)
|
|
|
|
if do_handshake_on_connect:
|
|
|
|
timeout = self.gettimeout()
|
|
|
|
if timeout == 0.0:
|
|
|
|
# non-blocking
|
|
|
|
raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
|
|
|
|
self.do_handshake()
|
|
|
|
|
|
|
|
except (OSError, ValueError):
|
|
|
|
self.close()
|
|
|
|
raise
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
@property
|
|
|
|
def context(self):
|
|
|
|
return self._context
|
|
|
|
|
|
|
|
@context.setter
|
|
|
|
def context(self, ctx):
|
|
|
|
self._context = ctx
|
|
|
|
self._sslobj.context = ctx
|
|
|
|
|
|
|
|
def dup(self):
|
|
|
|
raise NotImplemented("Can't dup() %s instances" %
|
|
|
|
self.__class__.__name__)
|
|
|
|
|
|
|
|
def _checkClosed(self, msg=None):
|
|
|
|
# raise an exception here if you wish to check for spurious closes
|
|
|
|
pass
|
2007-08-30 14:07:28 -03:00
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
def _check_connected(self):
|
|
|
|
if not self._connected:
|
|
|
|
# getpeername() will raise ENOTCONN if the socket is really
|
|
|
|
# not connected; note that we can be connected even without
|
|
|
|
# _connected being set, e.g. if connect() first returned
|
|
|
|
# EAGAIN.
|
|
|
|
self.getpeername()
|
|
|
|
|
2016-03-27 21:22:09 -03:00
|
|
|
def read(self, len=1024, buffer=None):
|
2007-08-30 14:07:28 -03:00
|
|
|
"""Read up to LEN bytes and return them.
|
|
|
|
Return zero-length string on EOF."""
|
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
self._checkClosed()
|
|
|
|
if not self._sslobj:
|
|
|
|
raise ValueError("Read on closed or unwrapped SSL socket.")
|
2008-06-28 19:19:33 -03:00
|
|
|
try:
|
2014-08-20 16:14:50 -03:00
|
|
|
if buffer is not None:
|
|
|
|
v = self._sslobj.read(len, buffer)
|
|
|
|
else:
|
2016-03-27 21:22:09 -03:00
|
|
|
v = self._sslobj.read(len)
|
2014-08-20 16:14:50 -03:00
|
|
|
return v
|
|
|
|
except SSLError as x:
|
2008-06-28 19:19:33 -03:00
|
|
|
if x.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
|
2014-08-20 16:14:50 -03:00
|
|
|
if buffer is not None:
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return b''
|
2008-06-28 19:19:33 -03:00
|
|
|
else:
|
|
|
|
raise
|
2007-08-25 12:08:43 -03:00
|
|
|
|
|
|
|
def write(self, data):
|
2007-08-30 14:07:28 -03:00
|
|
|
"""Write DATA to the underlying SSL channel. Returns
|
|
|
|
number of bytes of DATA actually transmitted."""
|
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
self._checkClosed()
|
|
|
|
if not self._sslobj:
|
|
|
|
raise ValueError("Write on closed or unwrapped SSL socket.")
|
2007-08-25 12:08:43 -03:00
|
|
|
return self._sslobj.write(data)
|
|
|
|
|
2007-09-10 18:51:02 -03:00
|
|
|
def getpeercert(self, binary_form=False):
|
2007-08-30 14:07:28 -03:00
|
|
|
"""Returns a formatted version of the data in the
|
|
|
|
certificate provided by the other end of the SSL channel.
|
|
|
|
Return None if no certificate was provided, {} if a
|
|
|
|
certificate was provided, but not validated."""
|
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
self._checkClosed()
|
|
|
|
self._check_connected()
|
2007-09-10 18:51:02 -03:00
|
|
|
return self._sslobj.peer_certificate(binary_form)
|
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
def selected_npn_protocol(self):
|
|
|
|
self._checkClosed()
|
|
|
|
if not self._sslobj or not _ssl.HAS_NPN:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self._sslobj.selected_npn_protocol()
|
2007-09-10 18:51:02 -03:00
|
|
|
|
2015-01-23 17:35:37 -04:00
|
|
|
def selected_alpn_protocol(self):
|
|
|
|
self._checkClosed()
|
|
|
|
if not self._sslobj or not _ssl.HAS_ALPN:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self._sslobj.selected_alpn_protocol()
|
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
def cipher(self):
|
|
|
|
self._checkClosed()
|
2007-09-10 18:51:02 -03:00
|
|
|
if not self._sslobj:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self._sslobj.cipher()
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
def compression(self):
|
|
|
|
self._checkClosed()
|
|
|
|
if not self._sslobj:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self._sslobj.compression()
|
|
|
|
|
2010-01-18 05:10:26 -04:00
|
|
|
def send(self, data, flags=0):
|
2014-08-20 16:14:50 -03:00
|
|
|
self._checkClosed()
|
2007-08-29 19:35:05 -03:00
|
|
|
if self._sslobj:
|
|
|
|
if flags != 0:
|
|
|
|
raise ValueError(
|
|
|
|
"non-zero flags not allowed in calls to send() on %s" %
|
|
|
|
self.__class__)
|
2014-08-20 16:14:50 -03:00
|
|
|
try:
|
|
|
|
v = self._sslobj.write(data)
|
|
|
|
except SSLError as x:
|
|
|
|
if x.args[0] == SSL_ERROR_WANT_READ:
|
|
|
|
return 0
|
|
|
|
elif x.args[0] == SSL_ERROR_WANT_WRITE:
|
|
|
|
return 0
|
2008-06-28 19:19:33 -03:00
|
|
|
else:
|
2014-08-20 16:14:50 -03:00
|
|
|
raise
|
|
|
|
else:
|
|
|
|
return v
|
2007-08-29 19:35:05 -03:00
|
|
|
else:
|
2010-09-14 11:37:18 -03:00
|
|
|
return self._sock.send(data, flags)
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2010-09-14 11:37:18 -03:00
|
|
|
def sendto(self, data, flags_or_addr, addr=None):
|
2014-08-20 16:14:50 -03:00
|
|
|
self._checkClosed()
|
2007-08-29 19:35:05 -03:00
|
|
|
if self._sslobj:
|
2008-06-28 19:19:33 -03:00
|
|
|
raise ValueError("sendto not allowed on instances of %s" %
|
2007-08-29 19:35:05 -03:00
|
|
|
self.__class__)
|
2010-09-14 11:37:18 -03:00
|
|
|
elif addr is None:
|
|
|
|
return self._sock.sendto(data, flags_or_addr)
|
2007-08-29 19:35:05 -03:00
|
|
|
else:
|
2010-09-14 11:37:18 -03:00
|
|
|
return self._sock.sendto(data, flags_or_addr, addr)
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
|
2010-01-18 05:10:26 -04:00
|
|
|
def sendall(self, data, flags=0):
|
2014-08-20 16:14:50 -03:00
|
|
|
self._checkClosed()
|
2007-08-29 19:35:05 -03:00
|
|
|
if self._sslobj:
|
|
|
|
if flags != 0:
|
|
|
|
raise ValueError(
|
|
|
|
"non-zero flags not allowed in calls to sendall() on %s" %
|
|
|
|
self.__class__)
|
2008-06-28 19:19:33 -03:00
|
|
|
amount = len(data)
|
|
|
|
count = 0
|
|
|
|
while (count < amount):
|
|
|
|
v = self.send(data[count:])
|
|
|
|
count += v
|
|
|
|
return amount
|
2007-08-29 19:35:05 -03:00
|
|
|
else:
|
|
|
|
return socket.sendall(self, data, flags)
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2010-01-18 05:10:26 -04:00
|
|
|
def recv(self, buflen=1024, flags=0):
|
2014-08-20 16:14:50 -03:00
|
|
|
self._checkClosed()
|
2007-08-29 19:35:05 -03:00
|
|
|
if self._sslobj:
|
|
|
|
if flags != 0:
|
|
|
|
raise ValueError(
|
2010-03-21 16:33:38 -03:00
|
|
|
"non-zero flags not allowed in calls to recv() on %s" %
|
2007-08-29 19:35:05 -03:00
|
|
|
self.__class__)
|
2010-03-21 16:33:38 -03:00
|
|
|
return self.read(buflen)
|
2007-08-29 19:35:05 -03:00
|
|
|
else:
|
2010-09-14 11:37:18 -03:00
|
|
|
return self._sock.recv(buflen, flags)
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2010-01-18 05:10:26 -04:00
|
|
|
def recv_into(self, buffer, nbytes=None, flags=0):
|
2014-08-20 16:14:50 -03:00
|
|
|
self._checkClosed()
|
2008-09-08 13:37:24 -03:00
|
|
|
if buffer and (nbytes is None):
|
|
|
|
nbytes = len(buffer)
|
|
|
|
elif nbytes is None:
|
|
|
|
nbytes = 1024
|
|
|
|
if self._sslobj:
|
|
|
|
if flags != 0:
|
|
|
|
raise ValueError(
|
|
|
|
"non-zero flags not allowed in calls to recv_into() on %s" %
|
|
|
|
self.__class__)
|
2014-08-20 16:14:50 -03:00
|
|
|
return self.read(nbytes, buffer)
|
2008-09-08 13:37:24 -03:00
|
|
|
else:
|
2010-09-14 11:37:18 -03:00
|
|
|
return self._sock.recv_into(buffer, nbytes, flags)
|
2008-09-08 13:37:24 -03:00
|
|
|
|
2010-09-14 11:37:18 -03:00
|
|
|
def recvfrom(self, buflen=1024, flags=0):
|
2014-08-20 16:14:50 -03:00
|
|
|
self._checkClosed()
|
2007-08-29 19:35:05 -03:00
|
|
|
if self._sslobj:
|
2008-06-28 19:19:33 -03:00
|
|
|
raise ValueError("recvfrom not allowed on instances of %s" %
|
2007-08-29 19:35:05 -03:00
|
|
|
self.__class__)
|
|
|
|
else:
|
2010-09-14 11:37:18 -03:00
|
|
|
return self._sock.recvfrom(buflen, flags)
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2010-01-18 05:10:26 -04:00
|
|
|
def recvfrom_into(self, buffer, nbytes=None, flags=0):
|
2014-08-20 16:14:50 -03:00
|
|
|
self._checkClosed()
|
2008-09-08 13:37:24 -03:00
|
|
|
if self._sslobj:
|
|
|
|
raise ValueError("recvfrom_into not allowed on instances of %s" %
|
|
|
|
self.__class__)
|
|
|
|
else:
|
2010-09-14 11:37:18 -03:00
|
|
|
return self._sock.recvfrom_into(buffer, nbytes, flags)
|
2008-09-08 13:37:24 -03:00
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
|
2010-01-18 05:10:26 -04:00
|
|
|
def pending(self):
|
2014-08-20 16:14:50 -03:00
|
|
|
self._checkClosed()
|
2008-06-28 19:19:33 -03:00
|
|
|
if self._sslobj:
|
|
|
|
return self._sslobj.pending()
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
2010-01-18 05:10:26 -04:00
|
|
|
def shutdown(self, how):
|
2014-08-20 16:14:50 -03:00
|
|
|
self._checkClosed()
|
2007-09-16 19:06:00 -03:00
|
|
|
self._sslobj = None
|
2007-08-29 19:35:05 -03:00
|
|
|
socket.shutdown(self, how)
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2010-01-18 05:10:26 -04:00
|
|
|
def close(self):
|
2008-06-28 19:19:33 -03:00
|
|
|
if self._makefile_refs < 1:
|
|
|
|
self._sslobj = None
|
|
|
|
socket.close(self)
|
|
|
|
else:
|
|
|
|
self._makefile_refs -= 1
|
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
def unwrap(self):
|
|
|
|
if self._sslobj:
|
|
|
|
s = self._sslobj.shutdown()
|
|
|
|
self._sslobj = None
|
|
|
|
return s
|
|
|
|
else:
|
|
|
|
raise ValueError("No SSL wrapper around " + str(self))
|
2008-06-28 19:19:33 -03:00
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
def _real_close(self):
|
|
|
|
self._sslobj = None
|
|
|
|
socket._real_close(self)
|
2008-06-28 19:19:33 -03:00
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
def do_handshake(self, block=False):
|
|
|
|
"""Perform a TLS/SSL handshake."""
|
|
|
|
self._check_connected()
|
|
|
|
timeout = self.gettimeout()
|
|
|
|
try:
|
|
|
|
if timeout == 0.0 and block:
|
|
|
|
self.settimeout(None)
|
|
|
|
self._sslobj.do_handshake()
|
|
|
|
finally:
|
|
|
|
self.settimeout(timeout)
|
|
|
|
|
|
|
|
if self.context.check_hostname:
|
|
|
|
if not self.server_hostname:
|
|
|
|
raise ValueError("check_hostname needs server_hostname "
|
|
|
|
"argument")
|
|
|
|
match_hostname(self.getpeercert(), self.server_hostname)
|
|
|
|
|
|
|
|
def _real_connect(self, addr, connect_ex):
|
|
|
|
if self.server_side:
|
|
|
|
raise ValueError("can't connect in server-side mode")
|
2007-08-25 12:08:43 -03:00
|
|
|
# Here we assume that the socket is client-side, and not
|
|
|
|
# connected at the time of the call. We connect it, then wrap it.
|
2011-02-26 19:35:27 -04:00
|
|
|
if self._connected:
|
2007-09-10 18:51:02 -03:00
|
|
|
raise ValueError("attempt to connect already-connected SSLSocket!")
|
2014-08-20 16:14:50 -03:00
|
|
|
self._sslobj = self.context._wrap_socket(self._sock, False, self.server_hostname, ssl_sock=self)
|
2011-02-26 19:35:27 -04:00
|
|
|
try:
|
2014-08-20 16:14:50 -03:00
|
|
|
if connect_ex:
|
2012-12-28 14:03:43 -04:00
|
|
|
rc = socket.connect_ex(self, addr)
|
2011-02-26 19:35:27 -04:00
|
|
|
else:
|
2012-12-28 14:03:43 -04:00
|
|
|
rc = None
|
|
|
|
socket.connect(self, addr)
|
|
|
|
if not rc:
|
2014-08-20 16:14:50 -03:00
|
|
|
self._connected = True
|
2012-12-28 14:03:43 -04:00
|
|
|
if self.do_handshake_on_connect:
|
|
|
|
self.do_handshake()
|
|
|
|
return rc
|
2014-08-20 16:14:50 -03:00
|
|
|
except (OSError, ValueError):
|
2012-12-28 14:03:43 -04:00
|
|
|
self._sslobj = None
|
|
|
|
raise
|
2011-02-26 19:35:27 -04:00
|
|
|
|
|
|
|
def connect(self, addr):
|
|
|
|
"""Connects to remote ADDR, and then wraps the connection in
|
|
|
|
an SSL channel."""
|
|
|
|
self._real_connect(addr, False)
|
|
|
|
|
|
|
|
def connect_ex(self, addr):
|
|
|
|
"""Connects to remote ADDR, and then wraps the connection in
|
|
|
|
an SSL channel."""
|
|
|
|
return self._real_connect(addr, True)
|
2007-08-25 12:08:43 -03:00
|
|
|
|
|
|
|
def accept(self):
|
2007-08-30 14:07:28 -03:00
|
|
|
"""Accepts a new connection from a remote client, and returns
|
|
|
|
a tuple containing that new connection wrapped with a server-side
|
|
|
|
SSL channel, and the address of the remote client."""
|
|
|
|
|
2007-08-29 19:35:05 -03:00
|
|
|
newsock, addr = socket.accept(self)
|
2014-08-20 16:14:50 -03:00
|
|
|
newsock = self.context.wrap_socket(newsock,
|
|
|
|
do_handshake_on_connect=self.do_handshake_on_connect,
|
|
|
|
suppress_ragged_eofs=self.suppress_ragged_eofs,
|
|
|
|
server_side=True)
|
|
|
|
return newsock, addr
|
2007-09-10 18:51:02 -03:00
|
|
|
|
2007-09-16 19:06:00 -03:00
|
|
|
def makefile(self, mode='r', bufsize=-1):
|
|
|
|
|
2008-09-08 13:37:24 -03:00
|
|
|
"""Make and return a file-like object that
|
|
|
|
works with the SSL connection. Just use the code
|
|
|
|
from the socket module."""
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2008-06-28 19:19:33 -03:00
|
|
|
self._makefile_refs += 1
|
2010-04-23 20:25:45 -03:00
|
|
|
# close=True so as to decrement the reference count when done with
|
|
|
|
# the file-like object.
|
|
|
|
return _fileobject(self, mode, bufsize, close=True)
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
def get_channel_binding(self, cb_type="tls-unique"):
|
|
|
|
"""Get channel binding data for current connection. Raise ValueError
|
|
|
|
if the requested `cb_type` is not supported. Return bytes of the data
|
|
|
|
or None if the data is not available (e.g. before the handshake).
|
|
|
|
"""
|
|
|
|
if cb_type not in CHANNEL_BINDING_TYPES:
|
|
|
|
raise ValueError("Unsupported channel binding type")
|
|
|
|
if cb_type != "tls-unique":
|
|
|
|
raise NotImplementedError(
|
|
|
|
"{0} channel binding type not implemented"
|
|
|
|
.format(cb_type))
|
|
|
|
if self._sslobj is None:
|
|
|
|
return None
|
|
|
|
return self._sslobj.tls_unique_cb()
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2014-09-04 17:33:22 -03:00
|
|
|
def version(self):
|
|
|
|
"""
|
|
|
|
Return a string identifying the protocol version used by the
|
|
|
|
current SSL channel, or None if there is no established channel.
|
|
|
|
"""
|
|
|
|
if self._sslobj is None:
|
|
|
|
return None
|
|
|
|
return self._sslobj.version()
|
|
|
|
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2007-09-10 18:51:02 -03:00
|
|
|
def wrap_socket(sock, keyfile=None, certfile=None,
|
|
|
|
server_side=False, cert_reqs=CERT_NONE,
|
2016-09-05 18:37:13 -03:00
|
|
|
ssl_version=PROTOCOL_TLS, ca_certs=None,
|
2008-06-28 19:19:33 -03:00
|
|
|
do_handshake_on_connect=True,
|
2014-08-20 16:14:50 -03:00
|
|
|
suppress_ragged_eofs=True,
|
|
|
|
ciphers=None):
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
return SSLSocket(sock=sock, keyfile=keyfile, certfile=certfile,
|
2007-09-10 18:51:02 -03:00
|
|
|
server_side=server_side, cert_reqs=cert_reqs,
|
2008-06-28 19:19:33 -03:00
|
|
|
ssl_version=ssl_version, ca_certs=ca_certs,
|
|
|
|
do_handshake_on_connect=do_handshake_on_connect,
|
2010-04-17 14:10:38 -03:00
|
|
|
suppress_ragged_eofs=suppress_ragged_eofs,
|
|
|
|
ciphers=ciphers)
|
2008-06-28 19:19:33 -03:00
|
|
|
|
2007-08-25 12:08:43 -03:00
|
|
|
# some utility functions
|
|
|
|
|
|
|
|
def cert_time_to_seconds(cert_time):
|
2014-08-20 16:14:50 -03:00
|
|
|
"""Return the time in seconds since the Epoch, given the timestring
|
|
|
|
representing the "notBefore" or "notAfter" date from a certificate
|
|
|
|
in ``"%b %d %H:%M:%S %Y %Z"`` strptime format (C locale).
|
|
|
|
|
|
|
|
"notBefore" or "notAfter" dates must use UTC (RFC 5280).
|
|
|
|
|
|
|
|
Month is one of: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
|
|
|
|
UTC should be specified as GMT (see ASN1_TIME_print())
|
|
|
|
"""
|
|
|
|
from time import strptime
|
|
|
|
from calendar import timegm
|
|
|
|
|
|
|
|
months = (
|
|
|
|
"Jan","Feb","Mar","Apr","May","Jun",
|
|
|
|
"Jul","Aug","Sep","Oct","Nov","Dec"
|
|
|
|
)
|
|
|
|
time_format = ' %d %H:%M:%S %Y GMT' # NOTE: no month, fixed GMT
|
|
|
|
try:
|
|
|
|
month_number = months.index(cert_time[:3].title()) + 1
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError('time data %r does not match '
|
|
|
|
'format "%%b%s"' % (cert_time, time_format))
|
|
|
|
else:
|
|
|
|
# found valid month
|
|
|
|
tt = strptime(cert_time[3:], time_format)
|
|
|
|
# return an integer, the previous mktime()-based implementation
|
|
|
|
# returned a float (fractional seconds are always zero here).
|
|
|
|
return timegm((tt[0], month_number) + tt[2:6])
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2007-09-16 19:06:00 -03:00
|
|
|
PEM_HEADER = "-----BEGIN CERTIFICATE-----"
|
|
|
|
PEM_FOOTER = "-----END CERTIFICATE-----"
|
|
|
|
|
|
|
|
def DER_cert_to_PEM_cert(der_cert_bytes):
|
|
|
|
"""Takes a certificate in binary DER format and returns the
|
|
|
|
PEM version of it as a string."""
|
|
|
|
|
2014-08-20 16:14:50 -03:00
|
|
|
f = base64.standard_b64encode(der_cert_bytes).decode('ascii')
|
|
|
|
return (PEM_HEADER + '\n' +
|
|
|
|
textwrap.fill(f, 64) + '\n' +
|
|
|
|
PEM_FOOTER + '\n')
|
2007-09-16 19:06:00 -03:00
|
|
|
|
|
|
|
def PEM_cert_to_DER_cert(pem_cert_string):
|
|
|
|
"""Takes a certificate in ASCII PEM format and returns the
|
|
|
|
DER-encoded version of it as a byte sequence"""
|
|
|
|
|
|
|
|
if not pem_cert_string.startswith(PEM_HEADER):
|
|
|
|
raise ValueError("Invalid PEM encoding; must start with %s"
|
|
|
|
% PEM_HEADER)
|
|
|
|
if not pem_cert_string.strip().endswith(PEM_FOOTER):
|
|
|
|
raise ValueError("Invalid PEM encoding; must end with %s"
|
|
|
|
% PEM_FOOTER)
|
|
|
|
d = pem_cert_string.strip()[len(PEM_HEADER):-len(PEM_FOOTER)]
|
2014-08-20 16:14:50 -03:00
|
|
|
return base64.decodestring(d.encode('ASCII', 'strict'))
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2016-09-05 18:37:13 -03:00
|
|
|
def get_server_certificate(addr, ssl_version=PROTOCOL_TLS, ca_certs=None):
|
2007-09-16 19:06:00 -03:00
|
|
|
"""Retrieve the certificate from the server at the specified address,
|
|
|
|
and return it as a PEM-encoded string.
|
|
|
|
If 'ca_certs' is specified, validate the server cert against it.
|
|
|
|
If 'ssl_version' is specified, use it in the connection attempt."""
|
|
|
|
|
|
|
|
host, port = addr
|
2014-08-20 16:14:50 -03:00
|
|
|
if ca_certs is not None:
|
2007-09-16 19:06:00 -03:00
|
|
|
cert_reqs = CERT_REQUIRED
|
|
|
|
else:
|
|
|
|
cert_reqs = CERT_NONE
|
2014-08-20 16:14:50 -03:00
|
|
|
context = _create_stdlib_context(ssl_version,
|
|
|
|
cert_reqs=cert_reqs,
|
|
|
|
cafile=ca_certs)
|
|
|
|
with closing(create_connection(addr)) as sock:
|
|
|
|
with closing(context.wrap_socket(sock)) as sslsock:
|
|
|
|
dercert = sslsock.getpeercert(True)
|
2007-09-16 19:06:00 -03:00
|
|
|
return DER_cert_to_PEM_cert(dercert)
|
|
|
|
|
2010-01-18 05:10:26 -04:00
|
|
|
def get_protocol_name(protocol_code):
|
2011-05-09 20:52:03 -03:00
|
|
|
return _PROTOCOL_NAMES.get(protocol_code, '<unknown>')
|
2007-09-16 19:06:00 -03:00
|
|
|
|
|
|
|
|
2007-08-25 12:08:43 -03:00
|
|
|
# a replacement for the old socket.ssl function
|
|
|
|
|
2010-01-18 05:10:26 -04:00
|
|
|
def sslwrap_simple(sock, keyfile=None, certfile=None):
|
2007-08-30 14:07:28 -03:00
|
|
|
"""A replacement for the old socket.ssl function. Designed
|
|
|
|
for compability with Python 2.5 and earlier. Will disappear in
|
|
|
|
Python 3.0."""
|
2008-09-29 15:56:38 -03:00
|
|
|
if hasattr(sock, "_sock"):
|
|
|
|
sock = sock._sock
|
|
|
|
|
2014-10-02 00:53:01 -03:00
|
|
|
ctx = SSLContext(PROTOCOL_SSLv23)
|
|
|
|
if keyfile or certfile:
|
|
|
|
ctx.load_cert_chain(certfile, keyfile)
|
|
|
|
ssl_sock = ctx._wrap_socket(sock, server_side=False)
|
2008-09-29 15:56:38 -03:00
|
|
|
try:
|
|
|
|
sock.getpeername()
|
2008-12-31 00:08:55 -04:00
|
|
|
except socket_error:
|
2008-09-29 15:56:38 -03:00
|
|
|
# no, no connection yet
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# yes, do the handshake
|
|
|
|
ssl_sock.do_handshake()
|
|
|
|
|
2008-06-28 19:19:33 -03:00
|
|
|
return ssl_sock
|