churchyard / rpms / python3

Forked from rpms/python3 6 years ago
Clone

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

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