From edb4a701241a09d5adc5d2aff9e8d4c9601bce36 Mon Sep 17 00:00:00 2001 From: Amias Date: Wed, 28 Oct 2026 13:29:02 +0100 Subject: [PATCH] added pem and der import and export to public and private keys. --- biscuit_auth.pyi | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ biscuit_test.py | 32 +++++++++++++++++++++++++++++ src/lib.rs | 48 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 130 insertions(+), 2 deletions(-) diff --git a/biscuit_auth.pyi b/biscuit_auth.pyi index da07790..a38502c 100644 --- a/biscuit_auth.pyi +++ b/biscuit_auth.pyi @@ -467,6 +467,58 @@ class PublicKey: # :rtype: list def to_bytes(self) -> bytes: ... + # Serializes a public key to DER format + # + # :return: the public key DER bytes + # :rtype: bytes + def to_der(self) -> bytes: ... + + # Serializes a public key to PEM format + # + # :return: the public key PEM string + # :rtype: str + def to_pem(self) -> str: ... + + # Deserializes a public key from raw bytes + # + # :param data: the raw bytes + # :type data: bytes + # :return: the public key + # :rtype: PublicKey + @classmethod + def from_bytes(cls, data: bytes, alg: Algorithm) -> PublicKey: ... + + # Deserializes a public key from DER bytes + # + # :param der: the DER bytes + # :type der: bytes + # :return: the public key + # :rtype: PublicKey + @classmethod + def from_der(cls, der: bytes) -> PublicKey: ... + + # Deserializes a public key from a PEM string + # + # :param pem: the PEM string + # :type pem: str + # :return: the public key + # :rtype: PublicKey + @classmethod + def from_pem(cls, pem: str) -> PublicKey: ... + + # Deserializes a public key from a hexadecimal string + # + # :param data: the hex-encoded string + # :type data: str + # :return: the public key + # :rtype: PublicKey + def __new__(cls, data: str) -> PublicKey: ... + # Serializes a public key to raw bytes + # + # :return: the public key bytes + # :rtype: list + def to_bytes(self) -> bytes: ... + # Deserializes a public key from raw bytes # # :param data: the raw bytes diff --git a/biscuit_test.py b/biscuit_test.py index 9ec6d06..0043c94 100644 --- a/biscuit_test.py +++ b/biscuit_test.py @@ -511,3 +511,35 @@ def test_read_third_party_facts(): facts = authorizer.query(rule) assert facts[0].terms[0] == "56" + +def test_private_key_to_and_from_pem(): + keypair = KeyPair() + private_key_pem = keypair.private_key.to_pem() + assert type(private_key_pem) == str + + private_key = PrivateKey.from_pem(pem=private_key_pem) + assert repr(keypair.private_key) == repr(private_key) + +def test_private_key_to_and_from_der(): + keypair = KeyPair() + private_key_der = keypair.private_key.to_der() + assert type(private_key_der) == bytes + + private_key = PrivateKey.from_der(private_key_der) + assert repr(keypair.private_key) == repr(private_key) + +def test_public_key_to_and_from_pem(): + keypair = KeyPair() + public_key_pem = keypair.public_key.to_pem() + assert type(public_key_pem) == str + + public_key = PublicKey.from_pem(pem=public_key_pem) + assert repr(keypair.public_key) == repr(public_key) + +def test_public_key_to_and_from_der(): + keypair = KeyPair() + public_key_der = keypair.public_key.to_der() + assert type(public_key_der) == bytes + + public_key = PublicKey.from_der(public_key_der) + assert repr(keypair.public_key) == repr(public_key) \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index ab08894..0f24396 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1166,6 +1166,28 @@ impl PyPublicKey { self.0.to_string() } + /// Serializes a public key to DER format + /// + /// :return: the public key DER bytes + /// :rtype: bytes + pub fn to_der(&self) -> PyResult> { + self.0 + .to_der() + .map(|der| der.to_vec()) + .map_err(|error| PyValueError::new_err(error.to_string())) + } + + /// Serializes a public key to PEM format + /// + /// :return: the public key PEM string + /// :rtype: str + pub fn to_pem(&self) -> PyResult { + self.0 + .to_pem() + .map(|pem| pem.to_string()) + .map_err(|error| PyValueError::new_err(error.to_string())) + } + /// Deserializes a public key from raw bytes /// /// :param data: the raw bytes @@ -1230,14 +1252,36 @@ pub struct PyPrivateKey(PrivateKey); #[pymethods] impl PyPrivateKey { - /// Serializes a public key to raw bytes + /// Serializes a private key to raw bytes /// - /// :return: the public key bytes + /// :return: the private key bytes /// :rtype: list pub fn to_bytes(&self) -> Vec { self.0.to_bytes().deref().clone() } + /// Serializes a private key to DER format + /// + /// :return: the private key DER bytes + /// :rtype: bytes + pub fn to_der(&self) -> PyResult> { + self.0 + .to_der() + .map(|der| der.to_vec()) + .map_err(|error| PyValueError::new_err(error.to_string())) + } + + /// Serializes a private key to PEM format + /// + /// :return: the private key PEM string + /// :rtype: str + pub fn to_pem(&self) -> PyResult { + self.0 + .to_pem() + .map(|pem| pem.to_string()) + .map_err(|error| PyValueError::new_err(error.to_string())) + } + /// Serializes a private key to a hexadecimal string /// /// :return: the private key bytes (hex-encoded)