Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/cryptography/hazmat/primitives/asymmetric/dh.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ def __copy__(self) -> DHPublicKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> DHPublicKey:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the __deepcopy__ expected to take in the memo as a second parameter as described on https://docs.python.org/3/library/copy.html#object.__deepcopy__ ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oof yes, not sure how I missed this. Will send a fix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case you missed it, the version in docs/hazmat/primitives/asymmetric/cloudhsm.rst has a memodict (not sure if anything cares about if it has that name of memo, but I'd at least change it to match the name in the documentation. And also note that the parameter is missing in not just this one I commented on, but all the *.py files.

"""
Returns a deep copy.
"""


DHPublicKeyWithSerialization = DHPublicKey
DHPublicKey.register(rust_openssl.dh.DHPublicKey)
Expand Down Expand Up @@ -142,6 +148,12 @@ def __copy__(self) -> DHPrivateKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> DHPrivateKey:
"""
Returns a deep copy.
"""


DHPrivateKeyWithSerialization = DHPrivateKey
DHPrivateKey.register(rust_openssl.dh.DHPrivateKey)
18 changes: 18 additions & 0 deletions src/cryptography/hazmat/primitives/asymmetric/dsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,24 @@ def private_bytes(
Returns the key serialized as bytes.
"""

@abc.abstractmethod
def __eq__(self, other: object) -> bool:
"""
Checks equality.
"""

@abc.abstractmethod
def __copy__(self) -> DSAPrivateKey:
"""
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> DSAPrivateKey:
"""
Returns a deep copy.
"""


DSAPrivateKeyWithSerialization = DSAPrivateKey
DSAPrivateKey.register(rust_openssl.dsa.DSAPrivateKey)
Expand Down Expand Up @@ -142,6 +154,12 @@ def __copy__(self) -> DSAPublicKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> DSAPublicKey:
"""
Returns a deep copy.
"""


DSAPublicKeyWithSerialization = DSAPublicKey
DSAPublicKey.register(rust_openssl.dsa.DSAPublicKey)
Expand Down
12 changes: 12 additions & 0 deletions src/cryptography/hazmat/primitives/asymmetric/ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ def __copy__(self) -> EllipticCurvePrivateKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> EllipticCurvePrivateKey:
"""
Returns a deep copy.
"""


EllipticCurvePrivateKeyWithSerialization = EllipticCurvePrivateKey
EllipticCurvePrivateKey.register(rust_openssl.ec.ECPrivateKey)
Expand Down Expand Up @@ -207,6 +213,12 @@ def __copy__(self) -> EllipticCurvePublicKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> EllipticCurvePublicKey:
"""
Returns a deep copy.
"""


EllipticCurvePublicKeyWithSerialization = EllipticCurvePublicKey
EllipticCurvePublicKey.register(rust_openssl.ec.ECPublicKey)
Expand Down
12 changes: 12 additions & 0 deletions src/cryptography/hazmat/primitives/asymmetric/ed25519.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ def __copy__(self) -> Ed25519PublicKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> Ed25519PublicKey:
"""
Returns a deep copy.
"""


Ed25519PublicKey.register(rust_openssl.ed25519.Ed25519PublicKey)

Expand Down Expand Up @@ -125,5 +131,11 @@ def __copy__(self) -> Ed25519PrivateKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> Ed25519PrivateKey:
"""
Returns a deep copy.
"""


Ed25519PrivateKey.register(rust_openssl.ed25519.Ed25519PrivateKey)
12 changes: 12 additions & 0 deletions src/cryptography/hazmat/primitives/asymmetric/ed448.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ def __copy__(self) -> Ed448PublicKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> Ed448PublicKey:
"""
Returns a deep copy.
"""


if hasattr(rust_openssl, "ed448"):
Ed448PublicKey.register(rust_openssl.ed448.Ed448PublicKey)
Expand Down Expand Up @@ -126,6 +132,12 @@ def __copy__(self) -> Ed448PrivateKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> Ed448PrivateKey:
"""
Returns a deep copy.
"""


if hasattr(rust_openssl, "x448"):
Ed448PrivateKey.register(rust_openssl.ed448.Ed448PrivateKey)
12 changes: 12 additions & 0 deletions src/cryptography/hazmat/primitives/asymmetric/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def __copy__(self) -> RSAPrivateKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> RSAPrivateKey:
"""
Returns a deep copy.
"""


RSAPrivateKeyWithSerialization = RSAPrivateKey
RSAPrivateKey.register(rust_openssl.rsa.RSAPrivateKey)
Expand Down Expand Up @@ -139,6 +145,12 @@ def __copy__(self) -> RSAPublicKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> RSAPublicKey:
"""
Returns a deep copy.
"""


RSAPublicKeyWithSerialization = RSAPublicKey
RSAPublicKey.register(rust_openssl.rsa.RSAPublicKey)
Expand Down
12 changes: 12 additions & 0 deletions src/cryptography/hazmat/primitives/asymmetric/x25519.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ def __copy__(self) -> X25519PublicKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> X25519PublicKey:
"""
Returns a deep copy.
"""


X25519PublicKey.register(rust_openssl.x25519.X25519PublicKey)

Expand Down Expand Up @@ -118,5 +124,11 @@ def __copy__(self) -> X25519PrivateKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> X25519PrivateKey:
"""
Returns a deep copy.
"""


X25519PrivateKey.register(rust_openssl.x25519.X25519PrivateKey)
12 changes: 12 additions & 0 deletions src/cryptography/hazmat/primitives/asymmetric/x448.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ def __copy__(self) -> X448PublicKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> X448PublicKey:
"""
Returns a deep copy.
"""


if hasattr(rust_openssl, "x448"):
X448PublicKey.register(rust_openssl.x448.X448PublicKey)
Expand Down Expand Up @@ -120,6 +126,12 @@ def __copy__(self) -> X448PrivateKey:
Returns a copy.
"""

@abc.abstractmethod
def __deepcopy__(self) -> X448PrivateKey:
"""
Returns a deep copy.
"""


if hasattr(rust_openssl, "x448"):
X448PrivateKey.register(rust_openssl.x448.X448PrivateKey)
22 changes: 22 additions & 0 deletions src/rust/src/backend/dh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@ impl DHPrivateKey {
fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
slf
}

fn __deepcopy__<'p>(
slf: pyo3::PyRef<'p, Self>,
_memo: &pyo3::Bound<'p, pyo3::types::PyDict>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: given these are unused, it's probably better to just type them as PyAny

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done !

) -> CryptographyResult<Self> {
let new_key = Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to do this, because keys are immutable a version of return self (like __copy__) should be enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I didn't know that, I'll basically use the same as __copy__ then. Thanks for the quick review !

pkey: slf.pkey.clone(),
};

Ok(new_key)
}
}

#[pyo3::pymethods]
Expand Down Expand Up @@ -312,6 +323,17 @@ impl DHPublicKey {
fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
slf
}

fn __deepcopy__<'p>(
slf: pyo3::PyRef<'p, Self>,
_memo: &pyo3::Bound<'p, pyo3::types::PyDict>,
) -> CryptographyResult<Self> {
let new_key = Self {
pkey: slf.pkey.clone(),
};

Ok(new_key)
}
}

#[pyo3::pymethods]
Expand Down
22 changes: 22 additions & 0 deletions src/rust/src/backend/dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,22 @@ impl DsaPrivateKey {
)
}

fn __eq__(&self, other: pyo3::PyRef<'_, Self>) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's going on here? Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to uncommit it. I removed it.

self.pkey.public_eq(&other.pkey)
}

fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
slf
}

fn __deepcopy__<'p>(
slf: pyo3::PyRef<'p, Self>,
_memo: &pyo3::Bound<'p, pyo3::types::PyDict>,
) -> CryptographyResult<Self> {
Ok(Self {
pkey: slf.pkey.clone(),
})
}
}

#[pyo3::pymethods]
Expand Down Expand Up @@ -229,6 +242,15 @@ impl DsaPublicKey {
fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
slf
}

fn __deepcopy__<'p>(
slf: pyo3::PyRef<'p, Self>,
_memo: &pyo3::Bound<'p, pyo3::types::PyDict>,
) -> CryptographyResult<Self> {
Ok(Self {
pkey: slf.pkey.clone(),
})
}
}

#[pyo3::pymethods]
Expand Down
28 changes: 28 additions & 0 deletions src/rust/src/backend/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,20 @@ impl ECPrivateKey {
fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
slf
}

fn __deepcopy__<'p>(
slf: pyo3::PyRef<'p, Self>,
py: pyo3::Python<'p>,
_memo: &pyo3::Bound<'p, pyo3::types::PyDict>,
) -> CryptographyResult<Self> {
let ec = slf.pkey.ec_key()?;
let curve = py_curve_from_curve(py, ec.group())?;
let new_key = Self {
pkey: slf.pkey.clone(),
curve: curve.clone().into(),
};
Ok(new_key)
}
}

#[pyo3::pymethods]
Expand Down Expand Up @@ -458,6 +472,20 @@ impl ECPublicKey {
fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
slf
}

fn __deepcopy__<'p>(
slf: pyo3::PyRef<'p, Self>,
py: pyo3::Python<'p>,
_memo: &pyo3::Bound<'p, pyo3::types::PyDict>,
) -> CryptographyResult<Self> {
let ec = slf.pkey.ec_key()?;
let curve = py_curve_from_curve(py, ec.group())?;
let new_key = Self {
pkey: slf.pkey.clone(),
curve: curve.clone().into(),
};
Ok(new_key)
}
}

#[pyo3::pyclass(frozen, module = "cryptography.hazmat.primitives.asymmetric.ec")]
Expand Down
20 changes: 20 additions & 0 deletions src/rust/src/backend/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ impl Ed25519PrivateKey {
fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
slf
}

fn __deepcopy__<'p>(
slf: pyo3::PyRef<'p, Self>,
_memo: &pyo3::Bound<'p, pyo3::types::PyDict>,
) -> CryptographyResult<Self> {
let new_key = Self {
pkey: slf.pkey.clone(),
};
Ok(new_key)
}
}

#[pyo3::pymethods]
Expand Down Expand Up @@ -161,6 +171,16 @@ impl Ed25519PublicKey {
fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
slf
}

fn __deepcopy__<'p>(
slf: pyo3::PyRef<'p, Self>,
_memo: &pyo3::Bound<'p, pyo3::types::PyDict>,
) -> CryptographyResult<Self> {
let new_key = Self {
pkey: slf.pkey.clone(),
};
Ok(new_key)
}
}

#[pyo3::pymodule(gil_used = false)]
Expand Down
Loading
Loading