Skip to content

Commit

Permalink
Refactor from_der_and_sign_algo
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Oct 4, 2023
1 parent c3abdc5 commit 0b14bd5
Showing 1 changed file with 67 additions and 34 deletions.
101 changes: 67 additions & 34 deletions src/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,41 +105,74 @@ impl KeyPair {
pkcs8: &[u8],
alg: &'static SignatureAlgorithm,
) -> Result<Self, Error> {
let pkcs8_vec = pkcs8.to_vec();

let kind = if alg == &PKCS_ED25519 {
KeyPairKind::Ed(Ed25519KeyPair::from_pkcs8_maybe_unchecked(pkcs8)?)
} else if alg == &PKCS_ECDSA_P256_SHA256 {
KeyPairKind::Ec(EcdsaKeyPair::from_pkcs8(
&signature::ECDSA_P256_SHA256_ASN1_SIGNING,
pkcs8,
)?)
} else if alg == &PKCS_ECDSA_P384_SHA384 {
KeyPairKind::Ec(EcdsaKeyPair::from_pkcs8(
&signature::ECDSA_P384_SHA384_ASN1_SIGNING,
pkcs8,
)?)
} else if alg == &PKCS_RSA_SHA256 {
let rsakp = RsaKeyPair::from_pkcs8(pkcs8)?;
KeyPairKind::Rsa(rsakp, &signature::RSA_PKCS1_SHA256)
} else if alg == &PKCS_RSA_SHA384 {
let rsakp = RsaKeyPair::from_pkcs8(pkcs8)?;
KeyPairKind::Rsa(rsakp, &signature::RSA_PKCS1_SHA384)
} else if alg == &PKCS_RSA_SHA512 {
let rsakp = RsaKeyPair::from_pkcs8(pkcs8)?;
KeyPairKind::Rsa(rsakp, &signature::RSA_PKCS1_SHA512)
} else if alg == &PKCS_RSA_PSS_SHA256 {
let rsakp = RsaKeyPair::from_pkcs8(pkcs8)?;
KeyPairKind::Rsa(rsakp, &signature::RSA_PSS_SHA256)
} else {
panic!("Unknown SignatureAlgorithm specified!");
};
let serialized_der = pkcs8.to_vec();

Ok(KeyPair {
kind,
alg,
serialized_der: pkcs8_vec,
})
if alg == &PKCS_ED25519 {
return Ok(KeyPair {
kind: KeyPairKind::Ed(Ed25519KeyPair::from_pkcs8_maybe_unchecked(pkcs8)?),
alg,
serialized_der,
});
}
if alg == &PKCS_ECDSA_P256_SHA256 {
return Ok(KeyPair {
kind: KeyPairKind::Ec(EcdsaKeyPair::from_pkcs8(
&signature::ECDSA_P256_SHA256_ASN1_SIGNING,
pkcs8,
)?),
alg,
serialized_der,
});
}
if alg == &PKCS_ECDSA_P384_SHA384 {
return Ok(KeyPair {
kind: KeyPairKind::Ec(EcdsaKeyPair::from_pkcs8(
&signature::ECDSA_P384_SHA384_ASN1_SIGNING,
pkcs8,
)?),
alg,
serialized_der,
});
}
if alg == &PKCS_RSA_SHA256 {
return Ok(KeyPair {
kind: KeyPairKind::Rsa(
RsaKeyPair::from_pkcs8(pkcs8)?,
&signature::RSA_PKCS1_SHA256,
),
alg,
serialized_der,
});
}
if alg == &PKCS_RSA_SHA384 {
return Ok(KeyPair {
kind: KeyPairKind::Rsa(
RsaKeyPair::from_pkcs8(pkcs8)?,
&signature::RSA_PKCS1_SHA384,
),
alg,
serialized_der,
});
}
if alg == &PKCS_RSA_SHA512 {
return Ok(KeyPair {
kind: KeyPairKind::Rsa(
RsaKeyPair::from_pkcs8(pkcs8)?,
&signature::RSA_PKCS1_SHA512,
),
alg,
serialized_der,
});
}
if alg == &PKCS_RSA_PSS_SHA256 {
return Ok(KeyPair {
kind: KeyPairKind::Rsa(RsaKeyPair::from_pkcs8(pkcs8)?, &signature::RSA_PSS_SHA256),
alg,
serialized_der,
});
}

panic!("Unknown SignatureAlgorithm specified!")
}

pub(crate) fn guess_kind_from_der(der: &[u8]) -> Result<KeyPair, Error> {
Expand Down

0 comments on commit 0b14bd5

Please sign in to comment.