Blame SOURCES/00294-define-TLS-cipher-suite-on-build-time.patch

900f19
diff --git a/Lib/ssl.py b/Lib/ssl.py
900f19
index 1f3a31a..b54a684 100644
900f19
--- a/Lib/ssl.py
900f19
+++ b/Lib/ssl.py
900f19
@@ -116,6 +116,7 @@ except ImportError:
900f19
 
900f19
 
900f19
 from _ssl import HAS_SNI, HAS_ECDH, HAS_NPN, HAS_ALPN, HAS_TLSv1_3
900f19
+from _ssl import _DEFAULT_CIPHERS
900f19
 from _ssl import _OPENSSL_API_VERSION
900f19
 
900f19
 
900f19
@@ -174,48 +175,7 @@ else:
900f19
     CHANNEL_BINDING_TYPES = []
900f19
 
900f19
 
900f19
-# Disable weak or insecure ciphers by default
900f19
-# (OpenSSL's default setting is 'DEFAULT:!aNULL:!eNULL')
900f19
-# Enable a better set of ciphers by default
900f19
-# This list has been explicitly chosen to:
900f19
-#   * TLS 1.3 ChaCha20 and AES-GCM cipher suites
900f19
-#   * Prefer cipher suites that offer perfect forward secrecy (DHE/ECDHE)
900f19
-#   * Prefer ECDHE over DHE for better performance
900f19
-#   * Prefer AEAD over CBC for better performance and security
900f19
-#   * Prefer AES-GCM over ChaCha20 because most platforms have AES-NI
900f19
-#     (ChaCha20 needs OpenSSL 1.1.0 or patched 1.0.2)
900f19
-#   * Prefer any AES-GCM and ChaCha20 over any AES-CBC for better
900f19
-#     performance and security
900f19
-#   * Then Use HIGH cipher suites as a fallback
900f19
-#   * Disable NULL authentication, NULL encryption, 3DES and MD5 MACs
900f19
-#     for security reasons
900f19
-_DEFAULT_CIPHERS = (
900f19
-    'TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:'
900f19
-    'TLS13-AES-128-GCM-SHA256:'
900f19
-    'ECDH+AESGCM:ECDH+CHACHA20:DH+AESGCM:DH+CHACHA20:ECDH+AES256:DH+AES256:'
900f19
-    'ECDH+AES128:DH+AES:ECDH+HIGH:DH+HIGH:RSA+AESGCM:RSA+AES:RSA+HIGH:'
900f19
-    '!aNULL:!eNULL:!MD5:!3DES'
900f19
-    )
900f19
-
900f19
-# Restricted and more secure ciphers for the server side
900f19
-# This list has been explicitly chosen to:
900f19
-#   * TLS 1.3 ChaCha20 and AES-GCM cipher suites
900f19
-#   * Prefer cipher suites that offer perfect forward secrecy (DHE/ECDHE)
900f19
-#   * Prefer ECDHE over DHE for better performance
900f19
-#   * Prefer AEAD over CBC for better performance and security
900f19
-#   * Prefer AES-GCM over ChaCha20 because most platforms have AES-NI
900f19
-#   * Prefer any AES-GCM and ChaCha20 over any AES-CBC for better
900f19
-#     performance and security
900f19
-#   * Then Use HIGH cipher suites as a fallback
900f19
-#   * Disable NULL authentication, NULL encryption, MD5 MACs, DSS, RC4, and
900f19
-#     3DES for security reasons
900f19
-_RESTRICTED_SERVER_CIPHERS = (
900f19
-    'TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:'
900f19
-    'TLS13-AES-128-GCM-SHA256:'
900f19
-    'ECDH+AESGCM:ECDH+CHACHA20:DH+AESGCM:DH+CHACHA20:ECDH+AES256:DH+AES256:'
900f19
-    'ECDH+AES128:DH+AES:ECDH+HIGH:DH+HIGH:RSA+AESGCM:RSA+AES:RSA+HIGH:'
900f19
-    '!aNULL:!eNULL:!MD5:!DSS:!RC4:!3DES'
900f19
-)
900f19
+_RESTRICTED_SERVER_CIPHERS = _DEFAULT_CIPHERS
900f19
 
900f19
 
900f19
 class CertificateError(ValueError):
900f19
@@ -389,8 +349,6 @@ class SSLContext(_SSLContext):
900f19
 
900f19
     def __new__(cls, protocol=PROTOCOL_TLS, *args, **kwargs):
900f19
         self = _SSLContext.__new__(cls, protocol)
900f19
-        if protocol != _SSLv2_IF_EXISTS:
900f19
-            self.set_ciphers(_DEFAULT_CIPHERS)
900f19
         return self
900f19
 
900f19
     def __init__(self, protocol=PROTOCOL_TLS):
900f19
@@ -505,8 +463,6 @@ def create_default_context(purpose=Purpose.SERVER_AUTH, *, cafile=None,
900f19
         # verify certs and host name in client mode
900f19
         context.verify_mode = CERT_REQUIRED
900f19
         context.check_hostname = True
900f19
-    elif purpose == Purpose.CLIENT_AUTH:
900f19
-        context.set_ciphers(_RESTRICTED_SERVER_CIPHERS)
900f19
 
900f19
     if cafile or capath or cadata:
900f19
         context.load_verify_locations(cafile, capath, cadata)
900f19
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
900f19
index 9785a59..34a7ec2 100644
900f19
--- a/Lib/test/test_ssl.py
900f19
+++ b/Lib/test/test_ssl.py
900f19
@@ -18,6 +18,7 @@ import asyncore
900f19
 import weakref
900f19
 import platform
900f19
 import functools
900f19
+import sysconfig
900f19
 try:
900f19
     import ctypes
900f19
 except ImportError:
900f19
@@ -36,7 +37,7 @@ PROTOCOLS = sorted(ssl._PROTOCOL_NAMES)
900f19
 HOST = support.HOST
900f19
 IS_LIBRESSL = ssl.OPENSSL_VERSION.startswith('LibreSSL')
900f19
 IS_OPENSSL_1_1 = not IS_LIBRESSL and ssl.OPENSSL_VERSION_INFO >= (1, 1, 0)
900f19
-
900f19
+PY_SSL_DEFAULT_CIPHERS = sysconfig.get_config_var('PY_SSL_DEFAULT_CIPHERS')
900f19
 
900f19
 def data_file(*name):
900f19
     return os.path.join(os.path.dirname(__file__), *name)
900f19
@@ -889,6 +890,19 @@ class ContextTests(unittest.TestCase):
900f19
         with self.assertRaisesRegex(ssl.SSLError, "No cipher can be selected"):
900f19
             ctx.set_ciphers("^$:,;?*'dorothyx")
900f19
 
900f19
+    @unittest.skipUnless(PY_SSL_DEFAULT_CIPHERS == 1,
900f19
+                         "Test applies only to Python default ciphers")
900f19
+    def test_python_ciphers(self):
900f19
+        ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
900f19
+        ciphers = ctx.get_ciphers()
900f19
+        for suite in ciphers:
900f19
+            name = suite['name']
900f19
+            self.assertNotIn("PSK", name)
900f19
+            self.assertNotIn("SRP", name)
900f19
+            self.assertNotIn("MD5", name)
900f19
+            self.assertNotIn("RC4", name)
900f19
+            self.assertNotIn("3DES", name)
900f19
+
900f19
     @unittest.skipIf(ssl.OPENSSL_VERSION_INFO < (1, 0, 2, 0, 0), 'OpenSSL too old')
900f19
     def test_get_ciphers(self):
900f19
         ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
900f19
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
900f19
index 5e007da..130f006 100644
900f19
--- a/Modules/_ssl.c
900f19
+++ b/Modules/_ssl.c
900f19
@@ -237,6 +237,31 @@ SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
900f19
 
900f19
 #endif /* OpenSSL < 1.1.0 or LibreSSL < 2.7.0 */
900f19
 
900f19
+/* Default cipher suites */
900f19
+#ifndef PY_SSL_DEFAULT_CIPHERS
900f19
+#define PY_SSL_DEFAULT_CIPHERS 1
900f19
+#endif
900f19
+
900f19
+#if PY_SSL_DEFAULT_CIPHERS == 0
900f19
+  #ifndef PY_SSL_DEFAULT_CIPHER_STRING
900f19
+     #error "Py_SSL_DEFAULT_CIPHERS 0 needs Py_SSL_DEFAULT_CIPHER_STRING"
900f19
+  #endif
900f19
+#elif PY_SSL_DEFAULT_CIPHERS == 1
900f19
+/* Python custom selection of sensible ciper suites
900f19
+ * DEFAULT: OpenSSL's default cipher list. Since 1.0.2 the list is in sensible order.
900f19
+ * !aNULL:!eNULL: really no NULL ciphers
900f19
+ * !MD5:!3DES:!DES:!RC4:!IDEA:!SEED: no weak or broken algorithms on old OpenSSL versions.
900f19
+ * !aDSS: no authentication with discrete logarithm DSA algorithm
900f19
+ * !SRP:!PSK: no secure remote password or pre-shared key authentication
900f19
+ */
900f19
+  #define PY_SSL_DEFAULT_CIPHER_STRING "DEFAULT:!aNULL:!eNULL:!MD5:!3DES:!DES:!RC4:!IDEA:!SEED:!aDSS:!SRP:!PSK"
900f19
+#elif PY_SSL_DEFAULT_CIPHERS == 2
900f19
+/* Ignored in SSLContext constructor, only used to as _ssl.DEFAULT_CIPHER_STRING */
900f19
+  #define PY_SSL_DEFAULT_CIPHER_STRING SSL_DEFAULT_CIPHER_LIST
900f19
+#else
900f19
+  #error "Unsupported PY_SSL_DEFAULT_CIPHERS"
900f19
+#endif
900f19
+
900f19
 
900f19
 enum py_ssl_error {
900f19
     /* these mirror ssl.h */
900f19
@@ -2803,7 +2828,12 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
900f19
     /* A bare minimum cipher list without completely broken cipher suites.
900f19
      * It's far from perfect but gives users a better head start. */
900f19
     if (proto_version != PY_SSL_VERSION_SSL2) {
900f19
-        result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL:!MD5");
900f19
+#if PY_SSL_DEFAULT_CIPHERS == 2
900f19
+        /* stick to OpenSSL's default settings */
900f19
+        result = 1;
900f19
+#else
900f19
+        result = SSL_CTX_set_cipher_list(ctx, PY_SSL_DEFAULT_CIPHER_STRING);
900f19
+#endif
900f19
     } else {
900f19
         /* SSLv2 needs MD5 */
900f19
         result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL");
900f19
@@ -5343,6 +5373,9 @@ PyInit__ssl(void)
900f19
                              (PyObject *)&PySSLSession_Type) != 0)
900f19
         return NULL;
900f19
 
900f19
+    PyModule_AddStringConstant(m, "_DEFAULT_CIPHERS",
900f19
+                               PY_SSL_DEFAULT_CIPHER_STRING);
900f19
+
900f19
     PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN",
900f19
                             PY_SSL_ERROR_ZERO_RETURN);
900f19
     PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ",
900f19
diff --git a/configure.ac b/configure.ac
900f19
index 3703701..2eff514 100644
900f19
--- a/configure.ac
900f19
+++ b/configure.ac
900f19
@@ -5598,6 +5598,42 @@ if test "$have_getrandom" = yes; then
900f19
               [Define to 1 if the getrandom() function is available])
900f19
 fi
900f19
 
900f19
+# ssl module default cipher suite string
900f19
+AH_TEMPLATE(PY_SSL_DEFAULT_CIPHERS,
900f19
+  [Default cipher suites list for ssl module.
900f19
+   1: Python's preferred selection, 2: leave OpenSSL defaults untouched, 0: custom string])
900f19
+AH_TEMPLATE(PY_SSL_DEFAULT_CIPHER_STRING,
900f19
+  [Cipher suite string for PY_SSL_DEFAULT_CIPHERS=0]
900f19
+)
900f19
+AC_MSG_CHECKING(for --with-ssl-default-suites)
900f19
+AC_ARG_WITH(ssl-default-suites,
900f19
+            AS_HELP_STRING([--with-ssl-default-suites=@<:@python|openssl|STRING@:>@],
900f19
+                           [Override default cipher suites string,
900f19
+                            python: use Python's preferred selection (default),
900f19
+                            openssl: leave OpenSSL's defaults untouched,
900f19
+                            STRING: use a custom string,
900f19
+                            PROTOCOL_SSLv2 ignores the setting]),
900f19
+[
900f19
+AC_MSG_RESULT($withval)
900f19
+case "$withval" in
900f19
+    python)
900f19
+        AC_DEFINE(PY_SSL_DEFAULT_CIPHERS, 1)
900f19
+        ;;
900f19
+    openssl)
900f19
+        AC_DEFINE(PY_SSL_DEFAULT_CIPHERS, 2)
900f19
+        ;;
900f19
+    *)
900f19
+        AC_DEFINE(PY_SSL_DEFAULT_CIPHERS, 0)
900f19
+        AC_DEFINE_UNQUOTED(PY_SSL_DEFAULT_CIPHER_STRING, "$withval")
900f19
+        ;;
900f19
+esac
900f19
+],
900f19
+[
900f19
+AC_MSG_RESULT(python)
900f19
+AC_DEFINE(PY_SSL_DEFAULT_CIPHERS, 1)
900f19
+])
900f19
+
900f19
+
900f19
 # generate output files
900f19
 AC_CONFIG_FILES(Makefile.pre Modules/Setup.config Misc/python.pc Misc/python-config.sh)
900f19
 AC_CONFIG_FILES([Modules/ld_so_aix], [chmod +x Modules/ld_so_aix])