| 1 | use super::error::{Error, ErrorKind, Result}; |
| 2 | use std::fmt; |
| 3 | /// A type which represents the various kinds of keys. |
| 4 | #[derive(Clone, Debug, PartialEq)] |
| 5 | pub enum KeyTypeKind { |
| 6 | /// Represents an RSA key type. |
| 7 | Rsa, |
| 8 | |
| 9 | /// Represents a DSA key type. |
| 10 | Dsa, |
| 11 | |
| 12 | /// Represents an ED25519 key type. |
| 13 | Ed25519, |
| 14 | |
| 15 | /// Represents an ECDSA key type. |
| 16 | Ecdsa, |
| 17 | |
| 18 | /// Represents an RSA certificate key type. |
| 19 | RsaCert, |
| 20 | |
| 21 | /// Represents a DSA certificate key type. |
| 22 | DsaCert, |
| 23 | |
| 24 | /// Represents an ED25519 certificate key type. |
| 25 | Ed25519Cert, |
| 26 | |
| 27 | /// Represents an ECDSA certificate key type. |
| 28 | EcdsaCert, |
| 29 | |
| 30 | /// Represents an ED25519 key type, generated by a security key. |
| 31 | Ed25519Sk, |
| 32 | |
| 33 | /// Represents an ECDSA key type, generated by a security key. |
| 34 | EcdsaSk, |
| 35 | } |
| 36 | |
| 37 | /// `KeyType` represents the type of an OpenSSH key. |
| 38 | #[derive(Clone, Debug, PartialEq)] |
| 39 | pub struct KeyType { |
| 40 | /// Name of the key type. |
| 41 | pub name: &'static str, |
| 42 | |
| 43 | /// Short name of the key type. |
| 44 | pub short_name: &'static str, |
| 45 | |
| 46 | /// Indicates whether the key type represents a certificate or not. |
| 47 | pub is_cert: bool, |
| 48 | |
| 49 | /// Indicates whether the key type is used with a security key or not |
| 50 | pub is_sk: bool, |
| 51 | |
| 52 | /// Kind of the key type. |
| 53 | pub kind: KeyTypeKind, |
| 54 | |
| 55 | /// The cert-less equivalent to a certified key type. |
| 56 | pub plain: &'static str, |
| 57 | } |
| 58 | |
| 59 | impl KeyType { |
| 60 | /// Creates a new `KeyType` from a given name. |
| 61 | /// |
| 62 | /// # Example |
| 63 | /// ```rust |
| 64 | /// # use sshkeys; |
| 65 | /// let kt = sshkeys::KeyType::from_name("ssh-rsa").unwrap(); |
| 66 | /// assert_eq!(kt.kind, sshkeys::KeyTypeKind::Rsa); |
| 67 | /// ``` |
| 68 | pub fn from_name(name: &str) -> Result<KeyType> { |
| 69 | let kt = match name { |
| 70 | "ssh-rsa" => KeyType { |
| 71 | name: "ssh-rsa", |
| 72 | plain: "ssh-rsa", |
| 73 | short_name: "RSA", |
| 74 | is_cert: false, |
| 75 | is_sk: false, |
| 76 | kind: KeyTypeKind::Rsa, |
| 77 | }, |
| 78 | "[email protected]" => KeyType { |
| 79 | name: "[email protected]", |
| 80 | plain: "ssh-rsa", |
| 81 | short_name: "RSA-CERT", |
| 82 | is_cert: true, |
| 83 | is_sk: false, |
| 84 | kind: KeyTypeKind::RsaCert, |
| 85 | }, |
| 86 | "ssh-dss" => KeyType { |
| 87 | name: "ssh-dss", |
| 88 | plain: "ssh-dss", |
| 89 | short_name: "DSA", |
| 90 | is_cert: false, |
| 91 | is_sk: false, |
| 92 | kind: KeyTypeKind::Dsa, |
| 93 | }, |
| 94 | "[email protected]" => KeyType { |
| 95 | name: "[email protected]", |
| 96 | plain: "ssh-dss", |
| 97 | short_name: "DSA-CERT", |
| 98 | is_cert: true, |
| 99 | is_sk: false, |
| 100 | kind: KeyTypeKind::DsaCert, |
| 101 | }, |
| 102 | "ecdsa-sha2-nistp256" => KeyType { |
| 103 | name: "ecdsa-sha2-nistp256", |
| 104 | plain: "ecdsa-sha2-nistp256", |
| 105 | short_name: "ECDSA", |
| 106 | is_cert: false, |
| 107 | is_sk: false, |
| 108 | kind: KeyTypeKind::Ecdsa, |
| 109 | }, |
| 110 | "ecdsa-sha2-nistp384" => KeyType { |
| 111 | name: "ecdsa-sha2-nistp384", |
| 112 | plain: "ecdsa-sha2-nistp384", |
| 113 | short_name: "ECDSA", |
| 114 | is_cert: false, |
| 115 | is_sk: false, |
| 116 | kind: KeyTypeKind::Ecdsa, |
| 117 | }, |
| 118 | "ecdsa-sha2-nistp521" => KeyType { |
| 119 | name: "ecdsa-sha2-nistp521", |
| 120 | plain: "ecdsa-sha2-nistp521", |
| 121 | short_name: "ECDSA", |
| 122 | is_cert: false, |
| 123 | is_sk: false, |
| 124 | kind: KeyTypeKind::Ecdsa, |
| 125 | }, |
| 126 | "[email protected]" => KeyType { |
| 127 | name: "[email protected]", |
| 128 | plain: "ecdsa-sha2-nistp256", |
| 129 | short_name: "ECDSA-CERT", |
| 130 | is_cert: true, |
| 131 | is_sk: false, |
| 132 | kind: KeyTypeKind::EcdsaCert, |
| 133 | }, |
| 134 | "[email protected]" => KeyType { |
| 135 | name: "[email protected]", |
| 136 | plain: "ecdsa-sha2-nistp384", |
| 137 | short_name: "ECDSA-CERT", |
| 138 | is_cert: true, |
| 139 | is_sk: false, |
| 140 | kind: KeyTypeKind::EcdsaCert, |
| 141 | }, |
| 142 | "[email protected]" => KeyType { |
| 143 | name: "[email protected]", |
| 144 | plain: "ecdsa-sha2-nistp521", |
| 145 | short_name: "ECDSA-CERT", |
| 146 | is_cert: true, |
| 147 | is_sk: false, |
| 148 | kind: KeyTypeKind::EcdsaCert, |
| 149 | }, |
| 150 | "ssh-ed25519" => KeyType { |
| 151 | name: "ssh-ed25519", |
| 152 | plain: "ssh-ed25519", |
| 153 | short_name: "ED25519", |
| 154 | is_cert: false, |
| 155 | is_sk: false, |
| 156 | kind: KeyTypeKind::Ed25519, |
| 157 | }, |
| 158 | "[email protected]" => KeyType { |
| 159 | name: "[email protected]", |
| 160 | plain: "ssh-ed25519", |
| 161 | short_name: "ED25519-CERT", |
| 162 | is_cert: true, |
| 163 | is_sk: false, |
| 164 | kind: KeyTypeKind::Ed25519Cert, |
| 165 | }, |
| 166 | "[email protected]" => KeyType { |
| 167 | name: "[email protected]", |
| 168 | plain: "[email protected]", |
| 169 | short_name: "ECDSA-SK", |
| 170 | is_cert: false, |
| 171 | is_sk: true, |
| 172 | kind: KeyTypeKind::EcdsaSk, |
| 173 | }, |
| 174 | "[email protected]" => KeyType { |
| 175 | name: "[email protected]", |
| 176 | plain: "[email protected]", |
| 177 | short_name: "ED25519-SK", |
| 178 | is_cert: false, |
| 179 | is_sk: true, |
| 180 | kind: KeyTypeKind::Ed25519Sk, |
| 181 | }, |
| 182 | _ => { |
| 183 | return Err(Error::with_kind(ErrorKind::UnknownKeyType( |
| 184 | name.to_string(), |
| 185 | ))) |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | Ok(kt) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | impl fmt::Display for KeyType { |
| 194 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 195 | write!(f, "{}", self.name) |
| 196 | } |
| 197 | } |