Skip to content

Commit 598894f

Browse files
committed
Issue #26470: Port ssl and hashlib module to OpenSSL 1.1.0.
1 parent b3b7a5a commit 598894f

File tree

6 files changed

+393
-164
lines changed

6 files changed

+393
-164
lines changed

Doc/library/ssl.rst

+81-21
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ instead.
178178
use. Typically, the server chooses a particular protocol version, and the
179179
client must adapt to the server's choice. Most of the versions are not
180180
interoperable with the other versions. If not specified, the default is
181-
:data:`PROTOCOL_SSLv23`; it provides the most compatibility with other
181+
:data:`PROTOCOL_TLS`; it provides the most compatibility with other
182182
versions.
183183

184184
Here's a table showing which versions in a client (down the side) can connect
@@ -187,11 +187,11 @@ instead.
187187
.. table::
188188

189189
======================== ========= ========= ========== ========= =========== ===========
190-
*client* / **server** **SSLv2** **SSLv3** **SSLv23** **TLSv1** **TLSv1.1** **TLSv1.2**
190+
*client* / **server** **SSLv2** **SSLv3** **TLS** **TLSv1** **TLSv1.1** **TLSv1.2**
191191
------------------------ --------- --------- ---------- --------- ----------- -----------
192192
*SSLv2* yes no yes no no no
193193
*SSLv3* no yes yes no no no
194-
*SSLv23* no yes yes yes yes yes
194+
*TLS* (*SSLv23*) no yes yes yes yes yes
195195
*TLSv1* no no yes yes no no
196196
*TLSv1.1* no no yes no yes no
197197
*TLSv1.2* no no yes no no yes
@@ -244,7 +244,7 @@ purposes.
244244
:const:`None`, this function can choose to trust the system's default
245245
CA certificates instead.
246246

247-
The settings are: :data:`PROTOCOL_SSLv23`, :data:`OP_NO_SSLv2`, and
247+
The settings are: :data:`PROTOCOL_TLS`, :data:`OP_NO_SSLv2`, and
248248
:data:`OP_NO_SSLv3` with high encryption cipher suites without RC4 and
249249
without unauthenticated cipher suites. Passing :data:`~Purpose.SERVER_AUTH`
250250
as *purpose* sets :data:`~SSLContext.verify_mode` to :data:`CERT_REQUIRED`
@@ -316,6 +316,11 @@ Random generation
316316

317317
.. versionadded:: 3.3
318318

319+
.. deprecated:: 3.5.3
320+
321+
OpenSSL has deprecated :func:`ssl.RAND_pseudo_bytes`, use
322+
:func:`ssl.RAND_bytes` instead.
323+
319324
.. function:: RAND_status()
320325

321326
Return ``True`` if the SSL pseudo-random number generator has been seeded
@@ -334,7 +339,7 @@ Random generation
334339
See http://egd.sourceforge.net/ or http://prngd.sourceforge.net/ for sources
335340
of entropy-gathering daemons.
336341

337-
Availability: not available with LibreSSL.
342+
Availability: not available with LibreSSL and OpenSSL > 1.1.0
338343

339344
.. function:: RAND_add(bytes, entropy)
340345

@@ -409,7 +414,7 @@ Certificate handling
409414
previously. Return an integer (no fractions of a second in the
410415
input format)
411416

412-
.. function:: get_server_certificate(addr, ssl_version=PROTOCOL_SSLv23, ca_certs=None)
417+
.. function:: get_server_certificate(addr, ssl_version=PROTOCOL_TLS, ca_certs=None)
413418

414419
Given the address ``addr`` of an SSL-protected server, as a (*hostname*,
415420
*port-number*) pair, fetches the server's certificate, and returns it as a
@@ -425,7 +430,7 @@ Certificate handling
425430

426431
.. versionchanged:: 3.5
427432
The default *ssl_version* is changed from :data:`PROTOCOL_SSLv3` to
428-
:data:`PROTOCOL_SSLv23` for maximum compatibility with modern servers.
433+
:data:`PROTOCOL_TLS` for maximum compatibility with modern servers.
429434

430435
.. function:: DER_cert_to_PEM_cert(DER_cert_bytes)
431436

@@ -451,6 +456,9 @@ Certificate handling
451456
* :attr:`openssl_capath_env` - OpenSSL's environment key that points to a capath,
452457
* :attr:`openssl_capath` - hard coded path to a capath directory
453458

459+
Availability: LibreSSL ignores the environment vars
460+
:attr:`openssl_cafile_env` and :attr:`openssl_capath_env`
461+
454462
.. versionadded:: 3.4
455463

456464
.. function:: enum_certificates(store_name)
@@ -568,11 +576,21 @@ Constants
568576

569577
.. versionadded:: 3.4.4
570578

571-
.. data:: PROTOCOL_SSLv23
579+
.. data:: PROTOCOL_TLS
572580

573581
Selects the highest protocol version that both the client and server support.
574582
Despite the name, this option can select "TLS" protocols as well as "SSL".
575583

584+
.. versionadded:: 3.5.3
585+
586+
.. data:: PROTOCOL_SSLv23
587+
588+
Alias for data:`PROTOCOL_TLS`.
589+
590+
.. deprecated:: 3.5.3
591+
592+
Use data:`PROTOCOL_TLS` instead.
593+
576594
.. data:: PROTOCOL_SSLv2
577595

578596
Selects SSL version 2 as the channel encryption protocol.
@@ -584,6 +602,10 @@ Constants
584602

585603
SSL version 2 is insecure. Its use is highly discouraged.
586604

605+
.. deprecated:: 3.5.3
606+
607+
OpenSSL has removed support for SSLv2.
608+
587609
.. data:: PROTOCOL_SSLv3
588610

589611
Selects SSL version 3 as the channel encryption protocol.
@@ -595,17 +617,32 @@ Constants
595617

596618
SSL version 3 is insecure. Its use is highly discouraged.
597619

620+
.. deprecated:: 3.5.3
621+
622+
OpenSSL has deprecated all version specific protocols. Use the default
623+
protocol data:`PROTOCOL_TLS` with flags like data:`OP_NO_SSLv3` instead.
624+
598625
.. data:: PROTOCOL_TLSv1
599626

600627
Selects TLS version 1.0 as the channel encryption protocol.
601628

629+
.. deprecated:: 3.5.3
630+
631+
OpenSSL has deprecated all version specific protocols. Use the default
632+
protocol data:`PROTOCOL_TLS` with flags like data:`OP_NO_SSLv3` instead.
633+
602634
.. data:: PROTOCOL_TLSv1_1
603635

604636
Selects TLS version 1.1 as the channel encryption protocol.
605637
Available only with openssl version 1.0.1+.
606638

607639
.. versionadded:: 3.4
608640

641+
.. deprecated:: 3.5.3
642+
643+
OpenSSL has deprecated all version specific protocols. Use the default
644+
protocol data:`PROTOCOL_TLS` with flags like data:`OP_NO_SSLv3` instead.
645+
609646
.. data:: PROTOCOL_TLSv1_2
610647

611648
Selects TLS version 1.2 as the channel encryption protocol. This is the
@@ -614,6 +651,11 @@ Constants
614651

615652
.. versionadded:: 3.4
616653

654+
.. deprecated:: 3.5.3
655+
656+
OpenSSL has deprecated all version specific protocols. Use the default
657+
protocol data:`PROTOCOL_TLS` with flags like data:`OP_NO_SSLv3` instead.
658+
617659
.. data:: OP_ALL
618660

619661
Enables workarounds for various bugs present in other SSL implementations.
@@ -625,39 +667,48 @@ Constants
625667
.. data:: OP_NO_SSLv2
626668

627669
Prevents an SSLv2 connection. This option is only applicable in
628-
conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from
670+
conjunction with :const:`PROTOCOL_TLS`. It prevents the peers from
629671
choosing SSLv2 as the protocol version.
630672

631673
.. versionadded:: 3.2
632674

675+
.. deprecated:: 3.5.3
676+
677+
SSLv2 is deprecated
678+
679+
633680
.. data:: OP_NO_SSLv3
634681

635682
Prevents an SSLv3 connection. This option is only applicable in
636-
conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from
683+
conjunction with :const:`PROTOCOL_TLS`. It prevents the peers from
637684
choosing SSLv3 as the protocol version.
638685

639686
.. versionadded:: 3.2
640687

688+
.. deprecated:: 3.5.3
689+
690+
SSLv3 is deprecated
691+
641692
.. data:: OP_NO_TLSv1
642693

643694
Prevents a TLSv1 connection. This option is only applicable in
644-
conjunction with :const:`PROTOCOL_SSLv23`. It prevents the peers from
695+
conjunction with :const:`PROTOCOL_TLS`. It prevents the peers from
645696
choosing TLSv1 as the protocol version.
646697

647698
.. versionadded:: 3.2
648699

649700
.. data:: OP_NO_TLSv1_1
650701

651702
Prevents a TLSv1.1 connection. This option is only applicable in conjunction
652-
with :const:`PROTOCOL_SSLv23`. It prevents the peers from choosing TLSv1.1 as
703+
with :const:`PROTOCOL_TLS`. It prevents the peers from choosing TLSv1.1 as
653704
the protocol version. Available only with openssl version 1.0.1+.
654705

655706
.. versionadded:: 3.4
656707

657708
.. data:: OP_NO_TLSv1_2
658709

659710
Prevents a TLSv1.2 connection. This option is only applicable in conjunction
660-
with :const:`PROTOCOL_SSLv23`. It prevents the peers from choosing TLSv1.2 as
711+
with :const:`PROTOCOL_TLS`. It prevents the peers from choosing TLSv1.2 as
661712
the protocol version. Available only with openssl version 1.0.1+.
662713

663714
.. versionadded:: 3.4
@@ -1081,17 +1132,21 @@ such as SSL configuration options, certificate(s) and private key(s).
10811132
It also manages a cache of SSL sessions for server-side sockets, in order
10821133
to speed up repeated connections from the same clients.
10831134

1084-
.. class:: SSLContext(protocol)
1135+
.. class:: SSLContext(protocol=PROTOCOL_TLS)
10851136

1086-
Create a new SSL context. You must pass *protocol* which must be one
1137+
Create a new SSL context. You may pass *protocol* which must be one
10871138
of the ``PROTOCOL_*`` constants defined in this module.
1088-
:data:`PROTOCOL_SSLv23` is currently recommended for maximum
1089-
interoperability.
1139+
:data:`PROTOCOL_TLS` is currently recommended for maximum
1140+
interoperability and default value.
10901141

10911142
.. seealso::
10921143
:func:`create_default_context` lets the :mod:`ssl` module choose
10931144
security settings for a given purpose.
10941145

1146+
.. versionchanged:: 3.5.3
1147+
1148+
:data:`PROTOCOL_TLS` is the default value.
1149+
10951150

10961151
:class:`SSLContext` objects have the following methods and attributes:
10971152

@@ -1232,6 +1287,9 @@ to speed up repeated connections from the same clients.
12321287
This method will raise :exc:`NotImplementedError` if :data:`HAS_ALPN` is
12331288
False.
12341289

1290+
OpenSSL 1.1.0+ will abort the handshake and raise :exc:`SSLError` when
1291+
both sides support ALPN but cannot agree on a protocol.
1292+
12351293
.. versionadded:: 3.5
12361294

12371295
.. method:: SSLContext.set_npn_protocols(protocols)
@@ -1598,7 +1656,7 @@ If you prefer to tune security settings yourself, you might create
15981656
a context from scratch (but beware that you might not get the settings
15991657
right)::
16001658

1601-
>>> context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
1659+
>>> context = ssl.SSLContext(ssl.PROTOCOL_TLS)
16021660
>>> context.verify_mode = ssl.CERT_REQUIRED
16031661
>>> context.check_hostname = True
16041662
>>> context.load_verify_locations("/etc/ssl/certs/ca-bundle.crt")
@@ -1999,15 +2057,17 @@ Protocol versions
19992057

20002058
SSL versions 2 and 3 are considered insecure and are therefore dangerous to
20012059
use. If you want maximum compatibility between clients and servers, it is
2002-
recommended to use :const:`PROTOCOL_SSLv23` as the protocol version and then
2060+
recommended to use :const:`PROTOCOL_TLS` as the protocol version and then
20032061
disable SSLv2 and SSLv3 explicitly using the :data:`SSLContext.options`
20042062
attribute::
20052063

2006-
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
2064+
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
20072065
context.options |= ssl.OP_NO_SSLv2
20082066
context.options |= ssl.OP_NO_SSLv3
2067+
context.options |= ssl.OP_NO_TLSv1
2068+
context.options |= ssl.OP_NO_TLSv1_1
20092069

2010-
The SSL context created above will only allow TLSv1 and later (if
2070+
The SSL context created above will only allow TLSv1.2 and later (if
20112071
supported by your system) connections.
20122072

20132073
Cipher selection

Lib/ssl.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
PROTOCOL_SSLv2
5252
PROTOCOL_SSLv3
5353
PROTOCOL_SSLv23
54+
PROTOCOL_TLS
5455
PROTOCOL_TLSv1
5556
PROTOCOL_TLSv1_1
5657
PROTOCOL_TLSv1_2
@@ -128,9 +129,10 @@ def _import_symbols(prefix):
128129

129130
_IntEnum._convert(
130131
'_SSLMethod', __name__,
131-
lambda name: name.startswith('PROTOCOL_'),
132+
lambda name: name.startswith('PROTOCOL_') and name != 'PROTOCOL_SSLv23',
132133
source=_ssl)
133134

135+
PROTOCOL_SSLv23 = _SSLMethod.PROTOCOL_SSLv23 = _SSLMethod.PROTOCOL_TLS
134136
_PROTOCOL_NAMES = {value: name for name, value in _SSLMethod.__members__.items()}
135137

136138
try:
@@ -357,13 +359,13 @@ class SSLContext(_SSLContext):
357359
__slots__ = ('protocol', '__weakref__')
358360
_windows_cert_stores = ("CA", "ROOT")
359361

360-
def __new__(cls, protocol, *args, **kwargs):
362+
def __new__(cls, protocol=PROTOCOL_TLS, *args, **kwargs):
361363
self = _SSLContext.__new__(cls, protocol)
362364
if protocol != _SSLv2_IF_EXISTS:
363365
self.set_ciphers(_DEFAULT_CIPHERS)
364366
return self
365367

366-
def __init__(self, protocol):
368+
def __init__(self, protocol=PROTOCOL_TLS):
367369
self.protocol = protocol
368370

369371
def wrap_socket(self, sock, server_side=False,
@@ -438,7 +440,7 @@ def create_default_context(purpose=Purpose.SERVER_AUTH, *, cafile=None,
438440
if not isinstance(purpose, _ASN1Object):
439441
raise TypeError(purpose)
440442

441-
context = SSLContext(PROTOCOL_SSLv23)
443+
context = SSLContext(PROTOCOL_TLS)
442444

443445
# SSLv2 considered harmful.
444446
context.options |= OP_NO_SSLv2
@@ -475,7 +477,7 @@ def create_default_context(purpose=Purpose.SERVER_AUTH, *, cafile=None,
475477
context.load_default_certs(purpose)
476478
return context
477479

478-
def _create_unverified_context(protocol=PROTOCOL_SSLv23, *, cert_reqs=None,
480+
def _create_unverified_context(protocol=PROTOCOL_TLS, *, cert_reqs=None,
479481
check_hostname=False, purpose=Purpose.SERVER_AUTH,
480482
certfile=None, keyfile=None,
481483
cafile=None, capath=None, cadata=None):
@@ -666,7 +668,7 @@ class SSLSocket(socket):
666668

667669
def __init__(self, sock=None, keyfile=None, certfile=None,
668670
server_side=False, cert_reqs=CERT_NONE,
669-
ssl_version=PROTOCOL_SSLv23, ca_certs=None,
671+
ssl_version=PROTOCOL_TLS, ca_certs=None,
670672
do_handshake_on_connect=True,
671673
family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None,
672674
suppress_ragged_eofs=True, npn_protocols=None, ciphers=None,
@@ -1056,7 +1058,7 @@ def version(self):
10561058

10571059
def wrap_socket(sock, keyfile=None, certfile=None,
10581060
server_side=False, cert_reqs=CERT_NONE,
1059-
ssl_version=PROTOCOL_SSLv23, ca_certs=None,
1061+
ssl_version=PROTOCOL_TLS, ca_certs=None,
10601062
do_handshake_on_connect=True,
10611063
suppress_ragged_eofs=True,
10621064
ciphers=None):
@@ -1125,7 +1127,7 @@ def PEM_cert_to_DER_cert(pem_cert_string):
11251127
d = pem_cert_string.strip()[len(PEM_HEADER):-len(PEM_FOOTER)]
11261128
return base64.decodebytes(d.encode('ASCII', 'strict'))
11271129

1128-
def get_server_certificate(addr, ssl_version=PROTOCOL_SSLv23, ca_certs=None):
1130+
def get_server_certificate(addr, ssl_version=PROTOCOL_TLS, ca_certs=None):
11291131
"""Retrieve the certificate from the server at the specified address,
11301132
and return it as a PEM-encoded string.
11311133
If 'ca_certs' is specified, validate the server cert against it.

0 commit comments

Comments
 (0)