use super::error::{Error, ErrorKind, Result};
use std::fmt;
#[derive(Debug, PartialEq, Clone)]
pub enum KeyTypeKind {
Rsa,
Ed25519,
Ecdsa,
RsaCert,
Ed25519Cert,
EcdsaCert,
}
#[derive(Debug, PartialEq, Clone)]
pub struct KeyType {
pub name: &'static str,
pub short_name: &'static str,
pub is_cert: bool,
pub kind: KeyTypeKind,
pub plain: &'static str,
}
#[derive(Debug, PartialEq, Clone)]
pub enum CurveKind {
Nistp256,
Nistp384,
Nistp521,
}
#[derive(Debug, PartialEq, Clone)]
pub struct Curve {
pub kind: CurveKind,
pub identifier: &'static str,
}
impl Curve {
pub fn from_identifier(id: &str) -> Result<Curve> {
let curve = match id {
"nistp256" => Curve {
kind: CurveKind::Nistp256,
identifier: "nistp256",
},
"nistp384" => Curve {
kind: CurveKind::Nistp384,
identifier: "nistp384",
},
"nistp521" => Curve {
kind: CurveKind::Nistp521,
identifier: "nistp521",
},
_ => return Err(Error::with_kind(ErrorKind::UnknownCurve(id.to_string()))),
};
Ok(curve)
}
}
impl KeyType {
pub fn from_name(name: &str) -> Result<KeyType> {
let kt = match name {
"ssh-rsa" => KeyType {
name: "ssh-rsa",
plain: "ssh-rsa",
short_name: "RSA",
is_cert: false,
kind: KeyTypeKind::Rsa,
},
"rsa-sha2-512" => KeyType {
name: "rsa-sha2-512",
plain: "rsa-sha2-512",
short_name: "RSA",
is_cert: false,
kind: KeyTypeKind::Rsa,
},
"ssh-rsa-cert-v01@openssh.com" => KeyType {
name: "ssh-rsa-cert-v01@openssh.com",
plain: "ssh-rsa",
short_name: "RSA-CERT",
is_cert: true,
kind: KeyTypeKind::RsaCert,
},
"ecdsa-sha2-nistp256" => KeyType {
name: "ecdsa-sha2-nistp256",
plain: "ecdsa-sha2-nistp256",
short_name: "ECDSA",
is_cert: false,
kind: KeyTypeKind::Ecdsa,
},
"ecdsa-sha2-nistp384" => KeyType {
name: "ecdsa-sha2-nistp384",
plain: "ecdsa-sha2-nistp384",
short_name: "ECDSA",
is_cert: false,
kind: KeyTypeKind::Ecdsa,
},
"ecdsa-sha2-nistp521" => KeyType {
name: "ecdsa-sha2-nistp521",
plain: "ecdsa-sha2-nistp521",
short_name: "ECDSA",
is_cert: false,
kind: KeyTypeKind::Ecdsa,
},
"ecdsa-sha2-nistp256-cert-v01@openssh.com" => KeyType {
name: "ecdsa-sha2-nistp256-cert-v01@openssh.com",
plain: "ecdsa-sha2-nistp256",
short_name: "ECDSA-CERT",
is_cert: true,
kind: KeyTypeKind::EcdsaCert,
},
"ecdsa-sha2-nistp384-cert-v01@openssh.com" => KeyType {
name: "ecdsa-sha2-nistp384-cert-v01@openssh.com",
plain: "ecdsa-sha2-nistp384",
short_name: "ECDSA-CERT",
is_cert: true,
kind: KeyTypeKind::EcdsaCert,
},
"ecdsa-sha2-nistp521-cert-v01@openssh.com" => KeyType {
name: "ecdsa-sha2-nistp521-cert-v01@openssh.com",
plain: "ecdsa-sha2-nistp521",
short_name: "ECDSA-CERT",
is_cert: true,
kind: KeyTypeKind::EcdsaCert,
},
"ssh-ed25519" => KeyType {
name: "ssh-ed25519",
plain: "ssh-ed25519",
short_name: "ED25519",
is_cert: false,
kind: KeyTypeKind::Ed25519,
},
"ssh-ed25519-cert-v01@openssh.com" => KeyType {
name: "ssh-ed25519-cert-v01@openssh.com",
plain: "ssh-ed25519",
short_name: "ED25519-CERT",
is_cert: true,
kind: KeyTypeKind::Ed25519Cert,
},
_ => {
return Err(Error::with_kind(ErrorKind::UnknownKeyType(
name.to_string(),
)))
}
};
Ok(kt)
}
}
impl fmt::Display for KeyType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name)
}
}