finish porting credentials.go

This commit is contained in:
c0repwn3r 2023-03-29 12:44:54 -04:00
parent 553a95d6bc
commit 201374fba4
Signed by: core
GPG Key ID: FDBF740DADDCEECF
1 changed files with 9 additions and 4 deletions

View File

@ -1,7 +1,7 @@
//! Contains the `Credentials` struct, which contains all keys, IDs, organizations and other identity-related and security-related data that is persistent in a `Client` //! Contains the `Credentials` struct, which contains all keys, IDs, organizations and other identity-related and security-related data that is persistent in a `Client`
use std::error::Error; use std::error::Error;
use trifid_pki::cert::{deserialize_ed25519_public, serialize_ed25519_public}; use trifid_pki::cert::{deserialize_ed25519_public_many, serialize_ed25519_public};
use trifid_pki::ed25519_dalek::{SigningKey, VerifyingKey}; use trifid_pki::ed25519_dalek::{SigningKey, VerifyingKey};
/// Contains information necessary to make requests against the `DNClient` API. /// Contains information necessary to make requests against the `DNClient` API.
@ -27,11 +27,16 @@ pub fn ed25519_public_keys_to_pem(keys: &[VerifyingKey]) -> Vec<u8> {
res res
} }
pub fn ed25519_public_keys_from_pem(pem: Vec<u8>) -> Result<Vec<VerifyingKey>, Box<dyn Error>> { /// Converts a set of PEM-encoded ed25519 public keys, and converts them into an array of `VerifyingKey`s.
/// # Errors
/// This function will return an error if the PEM could not be decoded, or if any of the encoded keys are invalid.
pub fn ed25519_public_keys_from_pem(pem: &[u8]) -> Result<Vec<VerifyingKey>, Box<dyn Error>> {
let pems = deserialize_ed25519_public_many(pem)?;
let mut keys = vec![]; let mut keys = vec![];
for key in keys.chunks(32) { #[allow(clippy::unwrap_used)]
for pem in pems {
keys.push(VerifyingKey::from_bytes(&pem.try_into().unwrap_or_else(|_| unreachable!()))?);
} }
Ok(keys) Ok(keys)