3
3
import calendar
4
4
import datetime
5
5
import functools
6
+ import sys
6
7
import typing
7
8
import warnings
8
9
from base64 import b16encode
14
15
Union ,
15
16
)
16
17
18
+ if sys .version_info >= (3 , 13 ):
19
+ from warnings import deprecated
20
+ elif sys .version_info < (3 , 8 ):
21
+ _T = typing .TypeVar ("T" )
22
+
23
+ def deprecated (msg : str , ** kwargs : object ) -> Callable [[_T ], _T ]:
24
+ return lambda f : f
25
+ else :
26
+ from typing_extensions import deprecated
27
+
17
28
from cryptography import utils , x509
18
29
from cryptography .hazmat .primitives .asymmetric import (
19
30
dsa ,
@@ -529,6 +540,10 @@ def _to_EC_KEY(self) -> Any:
529
540
return _ffi .gc (key , _lib .EC_KEY_free )
530
541
531
542
543
+ @deprecated (
544
+ "get_elliptic_curves is deprecated. You should use the APIs in "
545
+ "cryptography instead."
546
+ )
532
547
def get_elliptic_curves () -> set [_EllipticCurve ]:
533
548
"""
534
549
Return a set of objects representing the elliptic curves supported in the
@@ -544,20 +559,10 @@ def get_elliptic_curves() -> set[_EllipticCurve]:
544
559
return _EllipticCurve ._get_elliptic_curves (_lib )
545
560
546
561
547
- _get_elliptic_curves_internal = get_elliptic_curves
548
-
549
- utils .deprecated (
550
- get_elliptic_curves ,
551
- __name__ ,
552
- (
553
- "get_elliptic_curves is deprecated. You should use the APIs in "
554
- "cryptography instead."
555
- ),
556
- DeprecationWarning ,
557
- name = "get_elliptic_curves" ,
562
+ @deprecated (
563
+ "get_elliptic_curve is deprecated. You should use the APIs in "
564
+ "cryptography instead."
558
565
)
559
-
560
-
561
566
def get_elliptic_curve (name : str ) -> _EllipticCurve :
562
567
"""
563
568
Return a single curve object selected by name.
@@ -570,24 +575,12 @@ def get_elliptic_curve(name: str) -> _EllipticCurve:
570
575
571
576
If the named curve is not supported then :py:class:`ValueError` is raised.
572
577
"""
573
- for curve in _get_elliptic_curves_internal ():
578
+ for curve in get_elliptic_curves ():
574
579
if curve .name == name :
575
580
return curve
576
581
raise ValueError ("unknown curve name" , name )
577
582
578
583
579
- utils .deprecated (
580
- get_elliptic_curve ,
581
- __name__ ,
582
- (
583
- "get_elliptic_curve is deprecated. You should use the APIs in "
584
- "cryptography instead."
585
- ),
586
- DeprecationWarning ,
587
- name = "get_elliptic_curve" ,
588
- )
589
-
590
-
591
584
@functools .total_ordering
592
585
class X509Name :
593
586
"""
@@ -783,6 +776,10 @@ def get_components(self) -> list[tuple[bytes, bytes]]:
783
776
return result
784
777
785
778
779
+ @deprecated (
780
+ "X509Extension support in pyOpenSSL is deprecated. You should use the "
781
+ "APIs in cryptography."
782
+ )
786
783
class X509Extension :
787
784
"""
788
785
An X.509 v3 certificate extension.
@@ -953,19 +950,10 @@ def get_data(self) -> bytes:
953
950
return _ffi .buffer (char_result , result_length )[:]
954
951
955
952
956
- _X509ExtensionInternal = X509Extension
957
- utils .deprecated (
958
- X509Extension ,
959
- __name__ ,
960
- (
961
- "X509Extension support in pyOpenSSL is deprecated. You should use the "
962
- "APIs in cryptography."
963
- ),
964
- DeprecationWarning ,
965
- name = "X509Extension" ,
953
+ @deprecated (
954
+ "CSR support in pyOpenSSL is deprecated. You should use the APIs "
955
+ "in cryptography."
966
956
)
967
-
968
-
969
957
class X509Req :
970
958
"""
971
959
An X.509 certificate signing requests.
@@ -1091,9 +1079,7 @@ def get_subject(self) -> X509Name:
1091
1079
1092
1080
return name
1093
1081
1094
- def add_extensions (
1095
- self , extensions : Iterable [_X509ExtensionInternal ]
1096
- ) -> None :
1082
+ def add_extensions (self , extensions : Iterable [X509Extension ]) -> None :
1097
1083
"""
1098
1084
Add extensions to the certificate signing request.
1099
1085
@@ -1117,7 +1103,7 @@ def add_extensions(
1117
1103
stack = _ffi .gc (stack , _lib .sk_X509_EXTENSION_free )
1118
1104
1119
1105
for ext in extensions :
1120
- if not isinstance (ext , _X509ExtensionInternal ):
1106
+ if not isinstance (ext , X509Extension ):
1121
1107
raise ValueError ("One of the elements is not an X509Extension" )
1122
1108
1123
1109
# TODO push can fail (here and elsewhere)
@@ -1126,7 +1112,7 @@ def add_extensions(
1126
1112
add_result = _lib .X509_REQ_add_extensions (self ._req , stack )
1127
1113
_openssl_assert (add_result == 1 )
1128
1114
1129
- def get_extensions (self ) -> list [_X509ExtensionInternal ]:
1115
+ def get_extensions (self ) -> list [X509Extension ]:
1130
1116
"""
1131
1117
Get X.509 extensions in the certificate signing request.
1132
1118
@@ -1156,7 +1142,7 @@ def get_extensions(self) -> list[_X509ExtensionInternal]:
1156
1142
)
1157
1143
1158
1144
for i in range (_lib .sk_X509_EXTENSION_num (native_exts_obj )):
1159
- ext = _X509ExtensionInternal .__new__ (_X509ExtensionInternal )
1145
+ ext = X509Extension .__new__ (X509Extension )
1160
1146
extension = _lib .X509_EXTENSION_dup (
1161
1147
_lib .sk_X509_EXTENSION_value (native_exts_obj , i )
1162
1148
)
@@ -1210,20 +1196,6 @@ def verify(self, pkey: PKey) -> bool:
1210
1196
return result
1211
1197
1212
1198
1213
- _X509ReqInternal = X509Req
1214
-
1215
- utils .deprecated (
1216
- X509Req ,
1217
- __name__ ,
1218
- (
1219
- "CSR support in pyOpenSSL is deprecated. You should use the APIs "
1220
- "in cryptography."
1221
- ),
1222
- DeprecationWarning ,
1223
- name = "X509Req" ,
1224
- )
1225
-
1226
-
1227
1199
class X509 :
1228
1200
"""
1229
1201
An X.509 certificate.
@@ -1655,9 +1627,7 @@ def get_extension_count(self) -> int:
1655
1627
"""
1656
1628
return _lib .X509_get_ext_count (self ._x509 )
1657
1629
1658
- def add_extensions (
1659
- self , extensions : Iterable [_X509ExtensionInternal ]
1660
- ) -> None :
1630
+ def add_extensions (self , extensions : Iterable [X509Extension ]) -> None :
1661
1631
"""
1662
1632
Add extensions to the certificate.
1663
1633
@@ -1676,13 +1646,13 @@ def add_extensions(
1676
1646
)
1677
1647
1678
1648
for ext in extensions :
1679
- if not isinstance (ext , _X509ExtensionInternal ):
1649
+ if not isinstance (ext , X509Extension ):
1680
1650
raise ValueError ("One of the elements is not an X509Extension" )
1681
1651
1682
1652
add_result = _lib .X509_add_ext (self ._x509 , ext ._extension , - 1 )
1683
1653
_openssl_assert (add_result == 1 )
1684
1654
1685
- def get_extension (self , index : int ) -> _X509ExtensionInternal :
1655
+ def get_extension (self , index : int ) -> X509Extension :
1686
1656
"""
1687
1657
Get a specific extension of the certificate by index.
1688
1658
@@ -1706,7 +1676,7 @@ def get_extension(self, index: int) -> _X509ExtensionInternal:
1706
1676
stacklevel = 2 ,
1707
1677
)
1708
1678
1709
- ext = _X509ExtensionInternal .__new__ (_X509ExtensionInternal )
1679
+ ext = X509Extension .__new__ (X509Extension )
1710
1680
ext ._extension = _lib .X509_get_ext (self ._x509 , index )
1711
1681
if ext ._extension == _ffi .NULL :
1712
1682
raise IndexError ("extension index out of bounds" )
@@ -2461,7 +2431,7 @@ def load_certificate_request(type: int, buffer: bytes) -> X509Req:
2461
2431
2462
2432
_openssl_assert (req != _ffi .NULL )
2463
2433
2464
- x509req = _X509ReqInternal .__new__ (_X509ReqInternal )
2434
+ x509req = X509Req .__new__ (X509Req )
2465
2435
x509req ._req = _ffi .gc (req , _lib .X509_REQ_free )
2466
2436
return x509req
2467
2437
0 commit comments