churchyard / rpms / python3

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