From 8d988f54277cf0c46082c0efc67a9b787c254f2b Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Mon, 4 Apr 2022 16:05:55 +0800 Subject: [PATCH] feat: to_pem -> bytes_to_pem --- src/fido.rs | 26 ++------------------------ src/pkiutil.rs | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/fido.rs b/src/fido.rs index 655ca8d..c6d3f18 100644 --- a/src/fido.rs +++ b/src/fido.rs @@ -7,6 +7,7 @@ use base64::URL_SAFE_NO_PAD; use rand::Rng; use rust_util::XResult; use serde::{Deserialize, Serialize}; +use crate::pkiutil::bytes_to_pem; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct U2FDeviceInfo { @@ -74,7 +75,7 @@ impl U2fRegistrationData { client_data: client_data.into(), registration_data: base64::encode(®ister_result.0), attestation_cert_pem: registration.attestation_cert.map(|c| { - to_pem(&c, "CERTIFICATE", 64) + bytes_to_pem("CERTIFICATE", c) }), pub_key: hex::encode(registration.pub_key), key_handle: hex::encode(registration.key_handle), @@ -138,29 +139,6 @@ pub fn start_status_updater() -> Sender { status_tx } -pub fn to_pem(bs: &[u8], sub: &str, w: usize) -> String { - let mut s = String::with_capacity(bs.len() * 2); - s.push_str(&format!("-----BEGIN {}-----", sub)); - let b64 = base64::encode(bs).chars().collect::>(); - let mut b64 = b64.as_slice(); - while !b64.is_empty() { - s.push('\n'); - if b64.len() >= w { - for c in b64.iter().take(w) { - s.push(*c); - } - b64 = &b64[w..]; - } else { - for c in b64 { - s.push(*c); - } - b64 = &[]; - } - } - s.push_str(&format!("\n-----END {}-----", sub)); - s -} - pub fn to_hex(data: &[u8], joiner: &str) -> String { let parts: Vec = data.iter().map(|byte| format!("{:02x}", byte)).collect(); parts.join(joiner) diff --git a/src/pkiutil.rs b/src/pkiutil.rs index a6b688d..c9ce25a 100644 --- a/src/pkiutil.rs +++ b/src/pkiutil.rs @@ -6,6 +6,29 @@ use sequoia_openpgp::crypto::mpi::PublicKey; use crate::digest::sha256_bytes; +// pub fn to_pem(bs: &[u8], sub: &str, w: usize) -> String { +// let mut s = String::with_capacity(bs.len() * 2); +// s.push_str(&format!("-----BEGIN {}-----", sub)); +// let b64 = base64::encode(bs).chars().collect::>(); +// let mut b64 = b64.as_slice(); +// while !b64.is_empty() { +// s.push('\n'); +// if b64.len() >= w { +// for c in b64.iter().take(w) { +// s.push(*c); +// } +// b64 = &b64[w..]; +// } else { +// for c in b64 { +// s.push(*c); +// } +// b64 = &[]; +// } +// } +// s.push_str(&format!("\n-----END {}-----", sub)); +// s +// } + pub fn bytes_to_pem(tag: &str, contents: T) -> String where T: Into> { let cert_public_key_pem_obj = Pem { tag: tag.to_string(),