1use super::error::{Error, ErrorKind, Result};
2use std::fmt;
3#[derive(Debug, PartialEq, Clone)]
5pub enum KeyTypeKind {
6 Rsa,
8
9 Ed25519,
11
12 Ecdsa,
14
15 RsaCert,
17
18 Ed25519Cert,
20
21 EcdsaCert,
23}
24
25#[derive(Debug, PartialEq, Clone)]
27pub struct KeyType {
28 pub name: &'static str,
30
31 pub short_name: &'static str,
33
34 pub is_cert: bool,
36
37 pub kind: KeyTypeKind,
39
40 pub plain: &'static str,
42}
43
44#[derive(Debug, PartialEq, Clone)]
46pub enum CurveKind {
47 Nistp256,
49
50 Nistp384,
52
53 Nistp521,
55}
56
57#[derive(Debug, PartialEq, Clone)]
59pub struct Curve {
60 pub kind: CurveKind,
62
63 pub identifier: &'static str,
65}
66
67impl Curve {
68 pub fn from_identifier(id: &str) -> Result<Curve> {
77 let curve = match id {
78 "nistp256" => Curve {
79 kind: CurveKind::Nistp256,
80 identifier: "nistp256",
81 },
82 "nistp384" => Curve {
83 kind: CurveKind::Nistp384,
84 identifier: "nistp384",
85 },
86 "nistp521" => Curve {
87 kind: CurveKind::Nistp521,
88 identifier: "nistp521",
89 },
90 _ => return Err(Error::with_kind(ErrorKind::UnknownCurve(id.to_string()))),
91 };
92
93 Ok(curve)
94 }
95}
96
97
98impl KeyType {
99 pub fn from_name(name: &str) -> Result<KeyType> {
108 let kt = match name {
109 "ssh-rsa" => KeyType {
110 name: "ssh-rsa",
111 plain: "ssh-rsa",
112 short_name: "RSA",
113 is_cert: false,
114 kind: KeyTypeKind::Rsa,
115 },
116 "rsa-sha2-512" => KeyType {
117 name: "rsa-sha2-512",
118 plain: "rsa-sha2-512",
119 short_name: "RSA",
120 is_cert: false,
121 kind: KeyTypeKind::Rsa,
122 },
123 "ssh-rsa-cert-v01@openssh.com" => KeyType {
124 name: "ssh-rsa-cert-v01@openssh.com",
125 plain: "ssh-rsa",
126 short_name: "RSA-CERT",
127 is_cert: true,
128 kind: KeyTypeKind::RsaCert,
129 },
130 "ecdsa-sha2-nistp256" => KeyType {
131 name: "ecdsa-sha2-nistp256",
132 plain: "ecdsa-sha2-nistp256",
133 short_name: "ECDSA",
134 is_cert: false,
135 kind: KeyTypeKind::Ecdsa,
136 },
137 "ecdsa-sha2-nistp384" => KeyType {
138 name: "ecdsa-sha2-nistp384",
139 plain: "ecdsa-sha2-nistp384",
140 short_name: "ECDSA",
141 is_cert: false,
142 kind: KeyTypeKind::Ecdsa,
143 },
144 "ecdsa-sha2-nistp521" => KeyType {
145 name: "ecdsa-sha2-nistp521",
146 plain: "ecdsa-sha2-nistp521",
147 short_name: "ECDSA",
148 is_cert: false,
149 kind: KeyTypeKind::Ecdsa,
150 },
151 "ecdsa-sha2-nistp256-cert-v01@openssh.com" => KeyType {
152 name: "ecdsa-sha2-nistp256-cert-v01@openssh.com",
153 plain: "ecdsa-sha2-nistp256",
154 short_name: "ECDSA-CERT",
155 is_cert: true,
156 kind: KeyTypeKind::EcdsaCert,
157 },
158 "ecdsa-sha2-nistp384-cert-v01@openssh.com" => KeyType {
159 name: "ecdsa-sha2-nistp384-cert-v01@openssh.com",
160 plain: "ecdsa-sha2-nistp384",
161 short_name: "ECDSA-CERT",
162 is_cert: true,
163 kind: KeyTypeKind::EcdsaCert,
164 },
165 "ecdsa-sha2-nistp521-cert-v01@openssh.com" => KeyType {
166 name: "ecdsa-sha2-nistp521-cert-v01@openssh.com",
167 plain: "ecdsa-sha2-nistp521",
168 short_name: "ECDSA-CERT",
169 is_cert: true,
170 kind: KeyTypeKind::EcdsaCert,
171 },
172 "ssh-ed25519" => KeyType {
173 name: "ssh-ed25519",
174 plain: "ssh-ed25519",
175 short_name: "ED25519",
176 is_cert: false,
177 kind: KeyTypeKind::Ed25519,
178 },
179 "ssh-ed25519-cert-v01@openssh.com" => KeyType {
180 name: "ssh-ed25519-cert-v01@openssh.com",
181 plain: "ssh-ed25519",
182 short_name: "ED25519-CERT",
183 is_cert: true,
184 kind: KeyTypeKind::Ed25519Cert,
185 },
186 _ => {
187 return Err(Error::with_kind(ErrorKind::UnknownKeyType(
188 name.to_string(),
189 )))
190 }
191 };
192
193 Ok(kt)
194 }
195}
196
197impl fmt::Display for KeyType {
198 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
199 write!(f, "{}", self.name)
200 }
201}