From 2b60c9807882f86cda4259b2ad9d6ecc9ad58a3b Mon Sep 17 00:00:00 2001 From: "baranyildirim@gmail.com" Date: Fri, 8 Mar 2024 06:37:54 +0000 Subject: [PATCH] Add pem/der loaders to PublicKey Expose pem feature in biscuit-parser crate as well --- biscuit-auth/Cargo.toml | 2 +- biscuit-auth/src/crypto/mod.rs | 16 ++++++++++++++++ biscuit-parser/Cargo.toml | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/biscuit-auth/Cargo.toml b/biscuit-auth/Cargo.toml index 8e6b0148..1478c0b2 100644 --- a/biscuit-auth/Cargo.toml +++ b/biscuit-auth/Cargo.toml @@ -25,7 +25,7 @@ bwk = ["chrono", "serde"] docsrs = [] uuid = ["dep:uuid"] # used to expose pem/der loaders for keypairs -pem = ["ed25519-dalek/pem"] +pem = ["ed25519-dalek/pem", "ed25519-dalek/pkcs8"] [dependencies] rand_core = "^0.6" diff --git a/biscuit-auth/src/crypto/mod.rs b/biscuit-auth/src/crypto/mod.rs index 56eb13e5..74402797 100644 --- a/biscuit-auth/src/crypto/mod.rs +++ b/biscuit-auth/src/crypto/mod.rs @@ -12,6 +12,8 @@ use crate::{error::Format, format::schema}; use super::error; #[cfg(feature = "pem")] use ed25519_dalek::pkcs8::DecodePrivateKey; +#[cfg(feature = "pem")] +use ed25519_dalek::pkcs8::DecodePublicKey; use ed25519_dalek::*; use nom::Finish; @@ -170,6 +172,20 @@ impl PublicKey { } } + #[cfg(feature = "pem")] + pub fn from_public_key_der(bytes: &[u8]) -> Result { + let verification_key = ed25519_dalek::VerifyingKey::from_public_key_der(bytes) + .map_err(|e| error::Format::InvalidKey(e.to_string()))?; + Ok(PublicKey(verification_key)) + } + + #[cfg(feature = "pem")] + pub fn from_public_key_pem(pem: &str) -> Result { + let verification_key = ed25519_dalek::VerifyingKey::from_public_key_pem(pem) + .map_err(|e| error::Format::InvalidKey(e.to_string()))?; + Ok(PublicKey(verification_key)) + } + pub fn print(&self) -> String { self.to_string() } diff --git a/biscuit-parser/Cargo.toml b/biscuit-parser/Cargo.toml index 2192de03..077cc457 100644 --- a/biscuit-parser/Cargo.toml +++ b/biscuit-parser/Cargo.toml @@ -21,5 +21,6 @@ time = {version = "0.3.7", features = ["formatting", "parsing"]} [features] datalog-macro = [] +pem = [] # used by biscuit-wasm to serialize errors to JSON serde-error = ["serde"]