Skip to content

Commit

Permalink
Allow owned and borrowed data being passed into from_raw
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Oct 3, 2023
1 parent 3eabb76 commit 6cd164e
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use pem::Pem;
use ring::rand::SystemRandom;
use ring::signature::KeyPair as RingKeyPair;
use ring::signature::{self, EcdsaKeyPair, Ed25519KeyPair, RsaEncoding, RsaKeyPair};
use std::borrow::Cow;
use std::convert::TryFrom;
use std::fmt;
use yasna::DERWriter;
Expand Down Expand Up @@ -143,20 +144,22 @@ impl KeyPair {
})
}

pub(crate) fn from_raw(
pkcs8: &[u8],
pub(crate) fn from_raw<'b>(
pkcs8: impl Into<Cow<'b, [u8]>>,
) -> Result<(KeyPairKind, &'static SignatureAlgorithm), RcgenError> {
let (kind, alg) = if let Ok(edkp) = Ed25519KeyPair::from_pkcs8_maybe_unchecked(pkcs8) {
let pkcs8 = pkcs8.into();

let (kind, alg) = if let Ok(edkp) = Ed25519KeyPair::from_pkcs8_maybe_unchecked(&pkcs8) {
(KeyPairKind::Ed(edkp), &PKCS_ED25519)
} else if let Ok(eckp) =
EcdsaKeyPair::from_pkcs8(&signature::ECDSA_P256_SHA256_ASN1_SIGNING, pkcs8)
EcdsaKeyPair::from_pkcs8(&signature::ECDSA_P256_SHA256_ASN1_SIGNING, &pkcs8)
{
(KeyPairKind::Ec(eckp), &PKCS_ECDSA_P256_SHA256)
} else if let Ok(eckp) =
EcdsaKeyPair::from_pkcs8(&signature::ECDSA_P384_SHA384_ASN1_SIGNING, pkcs8)
EcdsaKeyPair::from_pkcs8(&signature::ECDSA_P384_SHA384_ASN1_SIGNING, &pkcs8)
{
(KeyPairKind::Ec(eckp), &PKCS_ECDSA_P384_SHA384)
} else if let Ok(rsakp) = RsaKeyPair::from_pkcs8(pkcs8) {
} else if let Ok(rsakp) = RsaKeyPair::from_pkcs8(&pkcs8) {
(
KeyPairKind::Rsa(rsakp, &signature::RSA_PKCS1_SHA256),
&PKCS_RSA_SHA256,
Expand Down

0 comments on commit 6cd164e

Please sign in to comment.