@@ -244,11 +244,18 @@ def to_cryptography_key(self):
244
244
245
245
.. versionadded:: 16.1.0
246
246
"""
247
+ from cryptography .hazmat .primitives .serialization import (
248
+ load_der_private_key ,
249
+ load_der_public_key ,
250
+ )
251
+
247
252
backend = _get_backend ()
248
253
if self ._only_public :
249
- return backend ._evp_pkey_to_public_key (self ._pkey )
254
+ der = dump_publickey (FILETYPE_ASN1 , self )
255
+ return load_der_public_key (der , backend )
250
256
else :
251
- return backend ._evp_pkey_to_private_key (self ._pkey )
257
+ der = dump_privatekey (FILETYPE_ASN1 , self )
258
+ return load_der_private_key (der , None , backend )
252
259
253
260
@classmethod
254
261
def from_cryptography_key (cls , crypto_key ):
@@ -262,7 +269,6 @@ def from_cryptography_key(cls, crypto_key):
262
269
263
270
.. versionadded:: 16.1.0
264
271
"""
265
- pkey = cls ()
266
272
if not isinstance (
267
273
crypto_key ,
268
274
(
@@ -274,11 +280,25 @@ def from_cryptography_key(cls, crypto_key):
274
280
):
275
281
raise TypeError ("Unsupported key type" )
276
282
277
- pkey ._pkey = crypto_key ._evp_pkey
283
+ from cryptography .hazmat .primitives .serialization import (
284
+ Encoding ,
285
+ NoEncryption ,
286
+ PrivateFormat ,
287
+ PublicFormat ,
288
+ )
289
+
278
290
if isinstance (crypto_key , (rsa .RSAPublicKey , dsa .DSAPublicKey )):
279
- pkey ._only_public = True
280
- pkey ._initialized = True
281
- return pkey
291
+ return load_publickey (
292
+ FILETYPE_ASN1 ,
293
+ crypto_key .public_bytes (
294
+ Encoding .DER , PublicFormat .SubjectPublicKeyInfo
295
+ ),
296
+ )
297
+ else :
298
+ der = crypto_key .private_bytes (
299
+ Encoding .DER , PrivateFormat .PKCS8 , NoEncryption ()
300
+ )
301
+ return load_privatekey (FILETYPE_ASN1 , der )
282
302
283
303
def generate_key (self , type , bits ):
284
304
"""
@@ -888,12 +908,12 @@ def to_cryptography(self):
888
908
889
909
.. versionadded:: 17.1.0
890
910
"""
891
- from cryptography .hazmat . backends . openssl . x509 import (
892
- _CertificateSigningRequest ,
893
- )
911
+ from cryptography .x509 import load_der_x509_csr
912
+
913
+ der = dump_certificate_request ( FILETYPE_ASN1 , self )
894
914
895
915
backend = _get_backend ()
896
- return _CertificateSigningRequest ( backend , self . _req )
916
+ return load_der_x509_csr ( der , backend )
897
917
898
918
@classmethod
899
919
def from_cryptography (cls , crypto_req ):
@@ -910,9 +930,10 @@ def from_cryptography(cls, crypto_req):
910
930
if not isinstance (crypto_req , x509 .CertificateSigningRequest ):
911
931
raise TypeError ("Must be a certificate signing request" )
912
932
913
- req = cls ()
914
- req ._req = crypto_req ._x509_req
915
- return req
933
+ from cryptography .hazmat .primitives .serialization import Encoding
934
+
935
+ der = crypto_req .public_bytes (Encoding .DER )
936
+ return load_certificate_request (FILETYPE_ASN1 , der )
916
937
917
938
def set_pubkey (self , pkey ):
918
939
"""
@@ -1109,10 +1130,11 @@ def to_cryptography(self):
1109
1130
1110
1131
.. versionadded:: 17.1.0
1111
1132
"""
1112
- from cryptography .hazmat . backends . openssl . x509 import _Certificate
1133
+ from cryptography .x509 import load_der_x509_certificate
1113
1134
1135
+ der = dump_certificate (FILETYPE_ASN1 , self )
1114
1136
backend = _get_backend ()
1115
- return _Certificate ( backend , self . _x509 )
1137
+ return load_der_x509_certificate ( der , backend )
1116
1138
1117
1139
@classmethod
1118
1140
def from_cryptography (cls , crypto_cert ):
@@ -1129,9 +1151,10 @@ def from_cryptography(cls, crypto_cert):
1129
1151
if not isinstance (crypto_cert , x509 .Certificate ):
1130
1152
raise TypeError ("Must be a certificate" )
1131
1153
1132
- cert = cls ()
1133
- cert ._x509 = crypto_cert ._x509
1134
- return cert
1154
+ from cryptography .hazmat .primitives .serialization import Encoding
1155
+
1156
+ der = crypto_cert .public_bytes (Encoding .DER )
1157
+ return load_certificate (FILETYPE_ASN1 , der )
1135
1158
1136
1159
def set_version (self , version ):
1137
1160
"""
@@ -2259,12 +2282,12 @@ def to_cryptography(self):
2259
2282
2260
2283
.. versionadded:: 17.1.0
2261
2284
"""
2262
- from cryptography .hazmat . backends . openssl . x509 import (
2263
- _CertificateRevocationList ,
2264
- )
2285
+ from cryptography .x509 import load_der_x509_crl
2286
+
2287
+ der = dump_crl ( FILETYPE_ASN1 , self )
2265
2288
2266
2289
backend = _get_backend ()
2267
- return _CertificateRevocationList ( backend , self . _crl )
2290
+ return load_der_x509_crl ( der , backend )
2268
2291
2269
2292
@classmethod
2270
2293
def from_cryptography (cls , crypto_crl ):
@@ -2281,9 +2304,10 @@ def from_cryptography(cls, crypto_crl):
2281
2304
if not isinstance (crypto_crl , x509 .CertificateRevocationList ):
2282
2305
raise TypeError ("Must be a certificate revocation list" )
2283
2306
2284
- crl = cls ()
2285
- crl ._crl = crypto_crl ._x509_crl
2286
- return crl
2307
+ from cryptography .hazmat .primitives .serialization import Encoding
2308
+
2309
+ der = crypto_crl .public_bytes (Encoding .DER )
2310
+ return load_crl (FILETYPE_ASN1 , der )
2287
2311
2288
2312
def get_revoked (self ):
2289
2313
"""
0 commit comments