Skip to content

Commit 8c42c52

Browse files
authored
Allow set_tmp_ecdh to take cryptography elliptic curves (#1327)
Deprecate `get_elliptic_curves` and `get_elliptic_curve`
1 parent fb6d150 commit 8c42c52

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

src/OpenSSL/SSL.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
import os
44
import socket
55
import typing
6+
import warnings
67
from errno import errorcode
78
from functools import partial, wraps
89
from itertools import chain, count
910
from sys import platform
1011
from typing import Any, Callable, List, Optional, Sequence, TypeVar
1112
from weakref import WeakValueDictionary
1213

14+
from cryptography.hazmat.primitives.asymmetric import ec
15+
1316
from OpenSSL._util import (
1417
StrOrBytesPath as _StrOrBytesPath,
1518
)
@@ -1358,17 +1361,44 @@ def load_tmp_dh(self, dhfile: _StrOrBytesPath) -> None:
13581361
res = _lib.SSL_CTX_set_tmp_dh(self._context, dh)
13591362
_openssl_assert(res == 1)
13601363

1361-
def set_tmp_ecdh(self, curve: _EllipticCurve) -> None:
1364+
def set_tmp_ecdh(self, curve: _EllipticCurve | ec.EllipticCurve) -> None:
13621365
"""
13631366
Select a curve to use for ECDHE key exchange.
13641367
1365-
:param curve: A curve object to use as returned by either
1368+
:param curve: A curve instance from cryptography
1369+
(:class:`~cryptogragraphy.hazmat.primitives.asymmetric.ec.EllipticCurve`).
1370+
Alternatively (deprecated) a curve object from either
13661371
:meth:`OpenSSL.crypto.get_elliptic_curve` or
13671372
:meth:`OpenSSL.crypto.get_elliptic_curves`.
13681373
13691374
:return: None
13701375
"""
1371-
_lib.SSL_CTX_set_tmp_ecdh(self._context, curve._to_EC_KEY())
1376+
1377+
if isinstance(curve, _EllipticCurve):
1378+
warnings.warn(
1379+
(
1380+
"Passing pyOpenSSL elliptic curves to set_tmp_ecdh is "
1381+
"deprecated. You should use cryptography's elliptic curve "
1382+
"types instead."
1383+
),
1384+
DeprecationWarning,
1385+
stacklevel=2,
1386+
)
1387+
_lib.SSL_CTX_set_tmp_ecdh(self._context, curve._to_EC_KEY())
1388+
else:
1389+
name = curve.name
1390+
if name == "secp192r1":
1391+
name = "prime192v1"
1392+
elif name == "secp256r1":
1393+
name = "prime256v1"
1394+
nid = _lib.OBJ_txt2nid(name.encode())
1395+
if nid == _lib.NID_undef:
1396+
_raise_current_error()
1397+
1398+
ec = _lib.EC_KEY_new_by_curve_name(nid)
1399+
_openssl_assert(ec != _ffi.NULL)
1400+
ec = _ffi.gc(ec, _lib.EC_KEY_free)
1401+
_lib.SSL_CTX_set_tmp_ecdh(self._context, ec)
13721402

13731403
def set_cipher_list(self, cipher_list: bytes) -> None:
13741404
"""

src/OpenSSL/crypto.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,20 @@ def get_elliptic_curves() -> set[_EllipticCurve]:
576576
return _EllipticCurve._get_elliptic_curves(_lib)
577577

578578

579+
_get_elliptic_curves_internal = get_elliptic_curves
580+
581+
utils.deprecated(
582+
get_elliptic_curves,
583+
__name__,
584+
(
585+
"get_elliptic_curves is deprecated. You should use the APIs in "
586+
"cryptography instead."
587+
),
588+
DeprecationWarning,
589+
name="get_elliptic_curves",
590+
)
591+
592+
579593
def get_elliptic_curve(name: str) -> _EllipticCurve:
580594
"""
581595
Return a single curve object selected by name.
@@ -588,12 +602,24 @@ def get_elliptic_curve(name: str) -> _EllipticCurve:
588602
589603
If the named curve is not supported then :py:class:`ValueError` is raised.
590604
"""
591-
for curve in get_elliptic_curves():
605+
for curve in _get_elliptic_curves_internal():
592606
if curve.name == name:
593607
return curve
594608
raise ValueError("unknown curve name", name)
595609

596610

611+
utils.deprecated(
612+
get_elliptic_curve,
613+
__name__,
614+
(
615+
"get_elliptic_curve is deprecated. You should use the APIs in "
616+
"cryptography instead."
617+
),
618+
DeprecationWarning,
619+
name="get_elliptic_curve",
620+
)
621+
622+
597623
@functools.total_ordering
598624
class X509Name:
599625
"""

tests/test_ssl.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import pytest
3838
from cryptography import x509
3939
from cryptography.hazmat.primitives import hashes, serialization
40-
from cryptography.hazmat.primitives.asymmetric import rsa
40+
from cryptography.hazmat.primitives.asymmetric import ec, rsa
4141
from cryptography.x509.oid import NameOID
4242
from pretend import raiser
4343

@@ -1685,6 +1685,14 @@ def test_set_tmp_ecdh(self):
16851685
continue
16861686
# The only easily "assertable" thing is that it does not raise an
16871687
# exception.
1688+
with pytest.deprecated_call():
1689+
context.set_tmp_ecdh(curve)
1690+
1691+
for name in dir(ec.EllipticCurveOID):
1692+
if name.startswith("_"):
1693+
continue
1694+
oid = getattr(ec.EllipticCurveOID, name)
1695+
curve = ec.get_curve_for_oid(oid)
16881696
context.set_tmp_ecdh(curve)
16891697

16901698
def test_set_session_cache_mode_wrong_args(self):

0 commit comments

Comments
 (0)