Skip to content

Commit

Permalink
pk: Remove obsoleted functions
Browse files Browse the repository at this point in the history
`Cipher.to_PEM()` and `Cipher.to_DER()` have been marked obsolete
for some time now.  Use the `export*` methods instead.
  • Loading branch information
Synss committed Feb 22, 2024
1 parent 43eb429 commit 3068d97
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 46 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[next]

* pk: Remove obsoleted to_PEM and to_DER functions.

[2.9.2] - 2024-02-18

* ci: Update wheels to mbedtls 2.28.7
Expand Down
8 changes: 3 additions & 5 deletions src/mbedtls/pk.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class CipherBase:
) -> None: ...
def __hash__(self) -> int: ...
def __eq__(self, other: object) -> bool: ...
def __str__(self) -> _PEM: ...
def __bytes__(self) -> _DER: ...
def to_bytes(self) -> _DER: ...
@classmethod
def from_buffer(
cls: Type[_TCipherBase],
Expand Down Expand Up @@ -110,11 +113,6 @@ class CipherBase:
) -> bool: ...
def encrypt(self, message: bytes) -> bytes: ...
def decrypt(self, message: bytes) -> bytes: ...
def to_PEM(self) -> KeyPair: ...
def __str__(self) -> _PEM: ...
def to_DER(self) -> KeyPair: ...
def to_bytes(self) -> _DER: ...
def __bytes__(self) -> _DER: ...

class RSA(CipherBase):
def __init__(
Expand Down
50 changes: 10 additions & 40 deletions src/mbedtls/pk.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ cdef class CipherBase:
elif not (self._has_private() and other._has_private()):
return other.export_public_key() == self.export_public_key()

def __str__(self):
return self.export_key(format="PEM")

def __bytes__(self):
return self.to_bytes()

def to_bytes(self):
"""Return the private key in DER format."""
return self.export_key(format="DER")

@classmethod
def from_buffer(
cls,
Expand Down Expand Up @@ -501,46 +511,6 @@ cdef class CipherBase:
return self._public_to_PEM()
raise ValueError(format)

def to_PEM(self):
"""Return the RSA in PEM format.
Warning:
This function is obsolete.
Return:
tuple(str, str): The private key and the public key.
See Also:
export_key(), export_public_key()
"""
return KeyPair(self.export_key("PEM"), self.export_public_key("PEM"))

def __str__(self):
return self.export_key(format="PEM")

def to_DER(self):
"""Return the RSA in DER format.
Warning:
This function is obsolete.
Return:
tuple(bytes, bytes): The private key and the public key.
See Also:
export_key(), export_public_key()
"""
return KeyPair(self.export_key("DER"), self.export_public_key("DER"))

def to_bytes(self):
"""Return the private key in DER format."""
return self.export_key(format="DER")

def __bytes__(self):
return self.to_bytes()


cdef class RSA(CipherBase):

Expand Down
1 change: 0 additions & 1 deletion tests/test_pk.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ def test_export_private_key(
assert pem.startswith(f"-----BEGIN {cipher_tag} PRIVATE KEY-----\n")
assert pem.endswith(f"-----END {cipher_tag} PRIVATE KEY-----\n")
assert pem == str(cipher)
assert pem == cipher.to_PEM().private
assert cipher == type(cipher).from_PEM(pem)

@pytest.mark.parametrize(
Expand Down

0 comments on commit 3068d97

Please sign in to comment.