Skip to content

Commit 4838a0c

Browse files
Support for copy.deepcopy keys (#13759)
* fix: add deep copy for rsa keys * fix: add deep copy for dh keys * fix: add deep copy for ec keys * fix: add deep copy for ed25519 keys * fix: add deep copy for ed448 keys * fix: add deep copy for x25519 keys * fix: add deep copy for x448 keys * fix: forgot skip test when x448 not available * chore: removed __eq__ from dsa because unrelated + fmt on x448 + added deepcopy on example clouhsm * fix: replaced all deepcopy implementations by self * fix: modified tests for coverage * chore: format test x448 * chore: removed cast for key in test_ec * chore: replaced key generation in tests by pre generated keys Signed-off-by: Courtcircuits <[email protected]> * chore: replaced PyDict by PyAny in backend files for deepcopy Signed-off-by: Courtcircuits <[email protected]> --------- Signed-off-by: Courtcircuits <[email protected]>
1 parent d0534d5 commit 4838a0c

File tree

25 files changed

+471
-0
lines changed

25 files changed

+471
-0
lines changed

docs/hazmat/primitives/asymmetric/cloudhsm.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ if you only need a subset of functionality.
122122
... def __copy__(self) -> "CloudRSAPrivateKey":
123123
... return self
124124
...
125+
... def __deepcopy__(self, memodict: dict) -> "CloudRSAPrivateKey":
126+
... return self
127+
...
125128
>>> cloud_private_key = CloudRSAPrivateKey("creds", "key_id")
126129
>>> sig = cloud_private_key.sign(b"message", PKCS1v15(), hashes.SHA256())
127130
>>> isinstance(sig, bytes)

src/cryptography/hazmat/primitives/asymmetric/dh.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ def __copy__(self) -> DHPublicKey:
8787
Returns a copy.
8888
"""
8989

90+
@abc.abstractmethod
91+
def __deepcopy__(self) -> DHPublicKey:
92+
"""
93+
Returns a deep copy.
94+
"""
95+
9096

9197
DHPublicKeyWithSerialization = DHPublicKey
9298
DHPublicKey.register(rust_openssl.dh.DHPublicKey)
@@ -142,6 +148,12 @@ def __copy__(self) -> DHPrivateKey:
142148
Returns a copy.
143149
"""
144150

151+
@abc.abstractmethod
152+
def __deepcopy__(self) -> DHPrivateKey:
153+
"""
154+
Returns a deep copy.
155+
"""
156+
145157

146158
DHPrivateKeyWithSerialization = DHPrivateKey
147159
DHPrivateKey.register(rust_openssl.dh.DHPrivateKey)

src/cryptography/hazmat/primitives/asymmetric/dsa.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ def __copy__(self) -> DSAPrivateKey:
8484
Returns a copy.
8585
"""
8686

87+
@abc.abstractmethod
88+
def __deepcopy__(self) -> DSAPrivateKey:
89+
"""
90+
Returns a deep copy.
91+
"""
92+
8793

8894
DSAPrivateKeyWithSerialization = DSAPrivateKey
8995
DSAPrivateKey.register(rust_openssl.dsa.DSAPrivateKey)
@@ -142,6 +148,12 @@ def __copy__(self) -> DSAPublicKey:
142148
Returns a copy.
143149
"""
144150

151+
@abc.abstractmethod
152+
def __deepcopy__(self) -> DSAPublicKey:
153+
"""
154+
Returns a deep copy.
155+
"""
156+
145157

146158
DSAPublicKeyWithSerialization = DSAPublicKey
147159
DSAPublicKey.register(rust_openssl.dsa.DSAPublicKey)

src/cryptography/hazmat/primitives/asymmetric/ec.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ def __copy__(self) -> EllipticCurvePrivateKey:
134134
Returns a copy.
135135
"""
136136

137+
@abc.abstractmethod
138+
def __deepcopy__(self) -> EllipticCurvePrivateKey:
139+
"""
140+
Returns a deep copy.
141+
"""
142+
137143

138144
EllipticCurvePrivateKeyWithSerialization = EllipticCurvePrivateKey
139145
EllipticCurvePrivateKey.register(rust_openssl.ec.ECPrivateKey)
@@ -207,6 +213,12 @@ def __copy__(self) -> EllipticCurvePublicKey:
207213
Returns a copy.
208214
"""
209215

216+
@abc.abstractmethod
217+
def __deepcopy__(self) -> EllipticCurvePublicKey:
218+
"""
219+
Returns a deep copy.
220+
"""
221+
210222

211223
EllipticCurvePublicKeyWithSerialization = EllipticCurvePublicKey
212224
EllipticCurvePublicKey.register(rust_openssl.ec.ECPublicKey)

src/cryptography/hazmat/primitives/asymmetric/ed25519.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ def __copy__(self) -> Ed25519PublicKey:
6060
Returns a copy.
6161
"""
6262

63+
@abc.abstractmethod
64+
def __deepcopy__(self) -> Ed25519PublicKey:
65+
"""
66+
Returns a deep copy.
67+
"""
68+
6369

6470
Ed25519PublicKey.register(rust_openssl.ed25519.Ed25519PublicKey)
6571

@@ -125,5 +131,11 @@ def __copy__(self) -> Ed25519PrivateKey:
125131
Returns a copy.
126132
"""
127133

134+
@abc.abstractmethod
135+
def __deepcopy__(self) -> Ed25519PrivateKey:
136+
"""
137+
Returns a deep copy.
138+
"""
139+
128140

129141
Ed25519PrivateKey.register(rust_openssl.ed25519.Ed25519PrivateKey)

src/cryptography/hazmat/primitives/asymmetric/ed448.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ def __copy__(self) -> Ed448PublicKey:
6060
Returns a copy.
6161
"""
6262

63+
@abc.abstractmethod
64+
def __deepcopy__(self) -> Ed448PublicKey:
65+
"""
66+
Returns a deep copy.
67+
"""
68+
6369

6470
if hasattr(rust_openssl, "ed448"):
6571
Ed448PublicKey.register(rust_openssl.ed448.Ed448PublicKey)
@@ -126,6 +132,12 @@ def __copy__(self) -> Ed448PrivateKey:
126132
Returns a copy.
127133
"""
128134

135+
@abc.abstractmethod
136+
def __deepcopy__(self) -> Ed448PrivateKey:
137+
"""
138+
Returns a deep copy.
139+
"""
140+
129141

130142
if hasattr(rust_openssl, "x448"):
131143
Ed448PrivateKey.register(rust_openssl.ed448.Ed448PrivateKey)

src/cryptography/hazmat/primitives/asymmetric/rsa.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ def __copy__(self) -> RSAPrivateKey:
6969
Returns a copy.
7070
"""
7171

72+
@abc.abstractmethod
73+
def __deepcopy__(self) -> RSAPrivateKey:
74+
"""
75+
Returns a deep copy.
76+
"""
77+
7278

7379
RSAPrivateKeyWithSerialization = RSAPrivateKey
7480
RSAPrivateKey.register(rust_openssl.rsa.RSAPrivateKey)
@@ -139,6 +145,12 @@ def __copy__(self) -> RSAPublicKey:
139145
Returns a copy.
140146
"""
141147

148+
@abc.abstractmethod
149+
def __deepcopy__(self) -> RSAPublicKey:
150+
"""
151+
Returns a deep copy.
152+
"""
153+
142154

143155
RSAPublicKeyWithSerialization = RSAPublicKey
144156
RSAPublicKey.register(rust_openssl.rsa.RSAPublicKey)

src/cryptography/hazmat/primitives/asymmetric/x25519.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ def __copy__(self) -> X25519PublicKey:
5454
Returns a copy.
5555
"""
5656

57+
@abc.abstractmethod
58+
def __deepcopy__(self) -> X25519PublicKey:
59+
"""
60+
Returns a deep copy.
61+
"""
62+
5763

5864
X25519PublicKey.register(rust_openssl.x25519.X25519PublicKey)
5965

@@ -118,5 +124,11 @@ def __copy__(self) -> X25519PrivateKey:
118124
Returns a copy.
119125
"""
120126

127+
@abc.abstractmethod
128+
def __deepcopy__(self) -> X25519PrivateKey:
129+
"""
130+
Returns a deep copy.
131+
"""
132+
121133

122134
X25519PrivateKey.register(rust_openssl.x25519.X25519PrivateKey)

src/cryptography/hazmat/primitives/asymmetric/x448.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ def __copy__(self) -> X448PublicKey:
5454
Returns a copy.
5555
"""
5656

57+
@abc.abstractmethod
58+
def __deepcopy__(self) -> X448PublicKey:
59+
"""
60+
Returns a deep copy.
61+
"""
62+
5763

5864
if hasattr(rust_openssl, "x448"):
5965
X448PublicKey.register(rust_openssl.x448.X448PublicKey)
@@ -120,6 +126,12 @@ def __copy__(self) -> X448PrivateKey:
120126
Returns a copy.
121127
"""
122128

129+
@abc.abstractmethod
130+
def __deepcopy__(self) -> X448PrivateKey:
131+
"""
132+
Returns a deep copy.
133+
"""
134+
123135

124136
if hasattr(rust_openssl, "x448"):
125137
X448PrivateKey.register(rust_openssl.x448.X448PrivateKey)

src/rust/src/backend/dh.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ impl DHPrivateKey {
247247
fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
248248
slf
249249
}
250+
251+
fn __deepcopy__<'p>(
252+
slf: pyo3::PyRef<'p, Self>,
253+
_memo: &pyo3::Bound<'p, pyo3::PyAny>,
254+
) -> pyo3::PyRef<'p, Self> {
255+
slf
256+
}
250257
}
251258

252259
#[pyo3::pymethods]
@@ -312,6 +319,13 @@ impl DHPublicKey {
312319
fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
313320
slf
314321
}
322+
323+
fn __deepcopy__<'p>(
324+
slf: pyo3::PyRef<'p, Self>,
325+
_memo: &pyo3::Bound<'p, pyo3::types::PyAny>,
326+
) -> pyo3::PyRef<'p, Self> {
327+
slf
328+
}
315329
}
316330

317331
#[pyo3::pymethods]

0 commit comments

Comments
 (0)