|
@ -5,29 +5,31 @@ use std::str::FromStr; |
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
pub enum KeyType {
|
|
|
pub enum KeyType {
|
|
|
Curve25519,
|
|
|
|
|
|
EcdsaP256,
|
|
|
|
|
|
EcdsaP384,
|
|
|
|
|
|
Rsa2048,
|
|
|
Rsa2048,
|
|
|
Rsa4096,
|
|
|
Rsa4096,
|
|
|
|
|
|
EcdsaP256,
|
|
|
|
|
|
EcdsaP384,
|
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
|
Ed25519,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl KeyType {
|
|
|
impl KeyType {
|
|
|
pub fn get_default_signature_alg(&self) -> JwsSignatureAlgorithm {
|
|
|
pub fn get_default_signature_alg(&self) -> JwsSignatureAlgorithm {
|
|
|
match self {
|
|
|
match self {
|
|
|
KeyType::Curve25519 => JwsSignatureAlgorithm::Ed25519,
|
|
|
|
|
|
|
|
|
KeyType::Rsa2048 | KeyType::Rsa4096 => JwsSignatureAlgorithm::Rs256,
|
|
|
KeyType::EcdsaP256 => JwsSignatureAlgorithm::Es256,
|
|
|
KeyType::EcdsaP256 => JwsSignatureAlgorithm::Es256,
|
|
|
KeyType::EcdsaP384 => JwsSignatureAlgorithm::Es384,
|
|
|
KeyType::EcdsaP384 => JwsSignatureAlgorithm::Es384,
|
|
|
KeyType::Rsa2048 | KeyType::Rsa4096 => JwsSignatureAlgorithm::Rs256,
|
|
|
|
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
|
KeyType::Ed25519 => JwsSignatureAlgorithm::Ed25519,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn check_alg_compatibility(&self, alg: &JwsSignatureAlgorithm) -> Result<(), Error> {
|
|
|
pub fn check_alg_compatibility(&self, alg: &JwsSignatureAlgorithm) -> Result<(), Error> {
|
|
|
let ok = match self {
|
|
|
let ok = match self {
|
|
|
KeyType::Curve25519 | KeyType::EcdsaP256 | KeyType::EcdsaP384 => {
|
|
|
|
|
|
*alg == self.get_default_signature_alg()
|
|
|
|
|
|
}
|
|
|
|
|
|
KeyType::Rsa2048 | KeyType::Rsa4096 => *alg == JwsSignatureAlgorithm::Rs256,
|
|
|
KeyType::Rsa2048 | KeyType::Rsa4096 => *alg == JwsSignatureAlgorithm::Rs256,
|
|
|
|
|
|
KeyType::EcdsaP256 | KeyType::EcdsaP384 => *alg == self.get_default_signature_alg(),
|
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
|
KeyType::Ed25519 => *alg == self.get_default_signature_alg(),
|
|
|
};
|
|
|
};
|
|
|
if ok {
|
|
|
if ok {
|
|
|
Ok(())
|
|
|
Ok(())
|
|
@ -46,11 +48,12 @@ impl FromStr for KeyType { |
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Error> {
|
|
|
fn from_str(s: &str) -> Result<Self, Error> {
|
|
|
match s.to_lowercase().as_str() {
|
|
|
match s.to_lowercase().as_str() {
|
|
|
"ed25519" => Ok(KeyType::Curve25519),
|
|
|
|
|
|
"ecdsa_p256" => Ok(KeyType::EcdsaP256),
|
|
|
|
|
|
"ecdsa_p384" => Ok(KeyType::EcdsaP384),
|
|
|
|
|
|
"rsa2048" => Ok(KeyType::Rsa2048),
|
|
|
"rsa2048" => Ok(KeyType::Rsa2048),
|
|
|
"rsa4096" => Ok(KeyType::Rsa4096),
|
|
|
"rsa4096" => Ok(KeyType::Rsa4096),
|
|
|
|
|
|
"ecdsa_p256" => Ok(KeyType::EcdsaP256),
|
|
|
|
|
|
"ecdsa_p384" => Ok(KeyType::EcdsaP384),
|
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
|
"ed25519" => Ok(KeyType::Ed25519),
|
|
|
_ => Err(format!("{}: unknown algorithm.", s).into()),
|
|
|
_ => Err(format!("{}: unknown algorithm.", s).into()),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@ -59,11 +62,12 @@ impl FromStr for KeyType { |
|
|
impl fmt::Display for KeyType {
|
|
|
impl fmt::Display for KeyType {
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
let s = match self {
|
|
|
let s = match self {
|
|
|
KeyType::Curve25519 => "ed25519",
|
|
|
|
|
|
KeyType::EcdsaP256 => "ecdsa-p256",
|
|
|
|
|
|
KeyType::EcdsaP384 => "ecdsa-p384",
|
|
|
|
|
|
KeyType::Rsa2048 => "rsa2048",
|
|
|
KeyType::Rsa2048 => "rsa2048",
|
|
|
KeyType::Rsa4096 => "rsa4096",
|
|
|
KeyType::Rsa4096 => "rsa4096",
|
|
|
|
|
|
KeyType::EcdsaP256 => "ecdsa-p256",
|
|
|
|
|
|
KeyType::EcdsaP384 => "ecdsa-p384",
|
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
|
KeyType::Ed25519 => "ed25519",
|
|
|
};
|
|
|
};
|
|
|
write!(f, "{}", s)
|
|
|
write!(f, "{}", s)
|
|
|
}
|
|
|
}
|
|
|