Skip to main content

rustica_keys/ssh/
keytype.rs

1use super::error::{Error, ErrorKind, Result};
2use std::fmt;
3/// A type which represents the various kinds of keys.
4#[derive(Debug, PartialEq, Clone)]
5pub enum KeyTypeKind {
6    /// Represents an RSA key type.
7    Rsa,
8
9    /// Represents an ED25519 key type.
10    Ed25519,
11
12    /// Represents an ECDSA key type.
13    Ecdsa,
14
15    /// Represents an RSA certificate key type.
16    RsaCert,
17
18    /// Represents an ED25519 certificate key type.
19    Ed25519Cert,
20
21    /// Represents an ECDSA certificate key type.
22    EcdsaCert,
23}
24
25/// `KeyType` represents the type of an OpenSSH key.
26#[derive(Debug, PartialEq, Clone)]
27pub struct KeyType {
28    /// Name of the key type.
29    pub name: &'static str,
30
31    /// Short name of the key type.
32    pub short_name: &'static str,
33
34    /// Indicates whether the key type represents a certificate or not.
35    pub is_cert: bool,
36
37    /// Kind of the key type.
38    pub kind: KeyTypeKind,
39
40    /// The cert-less equivalent to a certified key type.
41    pub plain: &'static str,
42}
43
44/// Represents the different kinds of supported curves.
45#[derive(Debug, PartialEq, Clone)]
46pub enum CurveKind {
47    /// Represents a NIST P-256 curve.
48    Nistp256,
49
50    /// Represents a NIST P-384 curve.
51    Nistp384,
52
53    /// Represents a NIST P-521 curve.
54    Nistp521,
55}
56
57/// A type which represents a cryptographic curve.
58#[derive(Debug, PartialEq, Clone)]
59pub struct Curve {
60    /// The curve kind.
61    pub kind: CurveKind,
62
63    /// Curve identifier.
64    pub identifier: &'static str,
65}
66
67impl Curve {
68    /// Creates a new `Curve` from the given identifier.
69    ///
70    /// # Example
71    /// ```rust
72    /// # use rustica_keys::ssh::{Curve, CurveKind};
73    /// let curve = Curve::from_identifier("nistp256").unwrap();
74    /// assert_eq!(curve.kind, CurveKind::Nistp256);
75    /// ```
76    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    /// Creates a new `KeyType` from a given name.
100    ///
101    /// # Example
102    /// ```rust
103    /// # use rustica_keys::ssh::{KeyType, KeyTypeKind};
104    /// let kt = KeyType::from_name("ssh-rsa").unwrap();
105    /// assert_eq!(kt.kind, KeyTypeKind::Rsa);
106    /// ```
107    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}