Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pem/der loaders to PublicKey #212

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion biscuit-auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions biscuit-auth/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -170,6 +172,20 @@ impl PublicKey {
}
}

#[cfg(feature = "pem")]
pub fn from_public_key_der(bytes: &[u8]) -> Result<Self, error::Format> {
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<Self, error::Format> {
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()
}
Expand Down
1 change: 1 addition & 0 deletions biscuit-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]