Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0bebbc3

Browse files
committed
Issue #21015: SSL contexts will now automatically select an elliptic curve for ECDH key exchange on OpenSSL 1.0.2 and later, and otherwise default to "prime256v1".
(should also fix a buildbot failure introduced by #20995)
1 parent 79ccaa2 commit 0bebbc3

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

Lib/test/test_ssl.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,6 +2567,18 @@ def test_default_ciphers(self):
25672567
s.connect((HOST, server.port))
25682568
self.assertIn("no shared cipher", str(server.conn_errors[0]))
25692569

2570+
@unittest.skipUnless(ssl.HAS_ECDH, "test requires ECDH-enabled OpenSSL")
2571+
def test_default_ecdh_curve(self):
2572+
# Issue #21015: elliptic curve-based Diffie Hellman key exchange
2573+
# should be enabled by default on SSL contexts.
2574+
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
2575+
context.load_cert_chain(CERTFILE)
2576+
context.set_ciphers("ECDH")
2577+
with ThreadedEchoServer(context=context) as server:
2578+
with context.wrap_socket(socket.socket()) as s:
2579+
s.connect((HOST, server.port))
2580+
self.assertIn("ECDH", s.cipher()[0])
2581+
25702582
@unittest.skipUnless("tls-unique" in ssl.CHANNEL_BINDING_TYPES,
25712583
"'tls-unique' channel binding not available")
25722584
def test_tls_unique_channel_binding(self):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Core and Builtins
2121
Library
2222
-------
2323

24+
- Issue #21015: SSL contexts will now automatically select an elliptic
25+
curve for ECDH key exchange on OpenSSL 1.0.2 and later, and otherwise
26+
default to "prime256v1".
27+
2428
- Issue #20995: Enhance default ciphers used by the ssl module to enable
2529
better security an prioritize perfect forward secrecy.
2630

Modules/_ssl.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,21 @@ context_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
20592059
options |= SSL_OP_NO_SSLv2;
20602060
SSL_CTX_set_options(self->ctx, options);
20612061

2062+
#ifndef OPENSSL_NO_ECDH
2063+
/* Allow automatic ECDH curve selection (on OpenSSL 1.0.2+), or use
2064+
prime256v1 by default. This is Apache mod_ssl's initialization
2065+
policy, so we should be safe. */
2066+
#if defined(SSL_CTX_set_ecdh_auto)
2067+
SSL_CTX_set_ecdh_auto(self->ctx, 1);
2068+
#else
2069+
{
2070+
EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
2071+
SSL_CTX_set_tmp_ecdh(self->ctx, key);
2072+
EC_KEY_free(key);
2073+
}
2074+
#endif
2075+
#endif
2076+
20622077
#define SID_CTX "Python"
20632078
SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX,
20642079
sizeof(SID_CTX));

0 commit comments

Comments
 (0)