1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
use std::fmt; use std::fs::File; use std::io::{self, Read}; use std::path::Path; use super::error::{Error, ErrorKind, Result}; use super::keytype::{KeyType, KeyTypeKind}; use super::reader::Reader; use super::writer::Writer; use sha2::{Digest, Sha256, Sha384, Sha512}; /// A type which represents the different kinds a public key can be. #[derive(Debug, PartialEq, Clone)] pub enum PublicKeyKind { /// Represents an RSA public key. Rsa(RsaPublicKey), /// Represents an ECDSA public key. Ecdsa(EcdsaPublicKey), /// Represents an ED25519 public key. Ed25519(Ed25519PublicKey), } /// RSA public key. /// The format of RSA public keys is described in RFC 4253, section 6.6 #[derive(Debug, PartialEq, Clone)] pub struct RsaPublicKey { /// Exponent of key. pub e: Vec<u8>, /// Modulus of key. pub n: Vec<u8>, } /// ECDSA public key. /// The format of ECDSA public keys is described in RFC 5656, section 3.1. #[derive(Debug, PartialEq, Clone)] pub struct EcdsaPublicKey { /// The curve being used. pub curve: Curve, /// The public key. pub key: Vec<u8>, } /// ED25519 public key. /// The format of ED25519 public keys is described in https://tools.ietf.org/html/draft-bjh21-ssh-ed25519-02 #[derive(Debug, PartialEq, Clone)] pub struct Ed25519PublicKey { /// The public key. pub key: Vec<u8>, } /// Represents the different kinds of supported curves. #[derive(Debug, PartialEq, Clone)] pub enum CurveKind { /// Represents a NIST P-256 curve. Nistp256, /// Represents a NIST P-384 curve. Nistp384, /// Represents a NIST P-521 curve. Nistp521, } /// A type which represents a cryptographic curve. #[derive(Debug, PartialEq, Clone)] pub struct Curve { /// The curve kind. pub kind: CurveKind, /// Curve identifier. pub identifier: &'static str, } impl Curve { /// Creates a new `Curve` from the given identifier. /// /// # Example /// ```rust /// # use rustica_keys::ssh::{Curve, CurveKind}; /// let curve = Curve::from_identifier("nistp256").unwrap(); /// assert_eq!(curve.kind, CurveKind::Nistp256); /// ``` 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) } } /// A type which represents an OpenSSH public key. #[derive(Debug, PartialEq, Clone)] pub struct PublicKey { /// Key type. pub key_type: KeyType, /// The kind of public key. pub kind: PublicKeyKind, /// Associated comment, if any. pub comment: Option<String>, } impl fmt::Display for PublicKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let comment = match &self.comment { Some(c) => c, None => "", }; write!( f, "{} {} {}", self.key_type, base64::encode(&self.encode()), comment ) } } /// The `FingerprintKind` enum represents the different fingerprint representation. #[derive(Debug, PartialEq)] pub enum FingerprintKind { /// A kind used to represent the fingerprint using SHA256. Sha256, /// A kind used to represent the fingerprint using SHA384. Sha384, /// A kind used to represent the fingerprint using SHA512. Sha512, } impl fmt::Display for FingerprintKind { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let kind = match *self { FingerprintKind::Sha256 => "SHA256", FingerprintKind::Sha384 => "SHA384", FingerprintKind::Sha512 => "SHA512", }; write!(f, "{}", kind) } } /// A type that represents an OpenSSH public key fingerprint. #[derive(Debug)] pub struct Fingerprint { /// The kind used to represent the fingerprint. pub kind: FingerprintKind, /// The computed fingerprint. pub hash: String, } impl fmt::Display for Fingerprint { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}:{}", self.kind, self.hash) } } impl Fingerprint { /// Computes the fingerprint of a byte sequence using a given fingerprint representation. /// /// This method computes a fingerprint the way OpenSSH does it and is generally being /// used to compute the fingerprint of an already encoded OpenSSH public key. /// /// # Example /// ```rust /// # use rustica_keys::ssh::{Fingerprint, FingerprintKind}; /// let fp = Fingerprint::compute(FingerprintKind::Sha256, "some data".as_bytes()); /// assert_eq!(fp.kind, FingerprintKind::Sha256); /// assert_eq!(fp.hash, "EweZDmulyhRes16ZGCqb7EZTG8VN32VqYCx4D6AkDe4"); /// ``` pub fn compute<T: ?Sized + AsRef<[u8]>>(kind: FingerprintKind, data: &T) -> Fingerprint { let digest = match kind { FingerprintKind::Sha256 => Sha256::digest(&data.as_ref()).to_vec(), FingerprintKind::Sha384 => Sha384::digest(&data.as_ref()).to_vec(), FingerprintKind::Sha512 => Sha512::digest(&data.as_ref()).to_vec(), }; let mut encoded = base64::encode(&digest); // Trim padding characters from end let hash = match encoded.find('=') { Some(offset) => encoded.drain(..offset).collect(), None => encoded, }; Fingerprint { kind, hash, } } } impl PublicKey { /// Reads an OpenSSH public key from a given path. /// /// # Examples /// /// ```rust /// # use rustica_keys::ssh::PublicKey; /// # fn example() { /// let key = PublicKey::from_path("/path/to/id_ed25519.pub"); /// # } /// ``` pub fn from_path<P: AsRef<Path>>(path: P) -> Result<PublicKey> { let mut contents = String::new(); File::open(path)?.read_to_string(&mut contents)?; PublicKey::from_string(&contents) } /// Reads an OpenSSH public key from a given string. /// /// # Examples /// /// ```rust /// # use rustica_keys::ssh::PublicKey; /// let key = PublicKey::from_string("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHkbe7gwx7s0dlApEEzpUyOAPrzPLy4czEZw/sh8m8rd me@home").unwrap(); /// let fp = key.fingerprint(); /// assert_eq!(fp.hash, "ciQkdxjFUhk2E2vRkWJD9kB8pi+EneOkaCJJHNWzPC4"); /// ``` pub fn from_string(contents: &str) -> Result<PublicKey> { let mut iter = contents.split_whitespace(); let kt_name = iter .next() .ok_or_else(|| Error::with_kind(ErrorKind::InvalidFormat))?; let data = iter .next() .ok_or_else(|| Error::with_kind(ErrorKind::InvalidFormat))?; let comment = iter.next().map(String::from); let key_type = KeyType::from_name(&kt_name)?; let decoded = base64::decode(&data)?; let mut reader = Reader::new(&decoded); // Validate key type before reading rest of the data let kt_from_reader = reader.read_string()?; if kt_name != kt_from_reader { return Err(Error::with_kind(ErrorKind::KeyTypeMismatch)); } // Construct a new `PublicKey` value and preserve the `comment` value. let k = PublicKey::from_reader(&kt_name, &mut reader)?; let key = PublicKey { key_type, kind: k.kind, comment, }; Ok(key) } /// Reads a public key from a given byte sequence. /// /// The byte sequence is expected to be the base64 decoded body of the public key. /// /// # Example /// /// ```rust /// # use rustica_keys::ssh::PublicKey; /// let data = vec![0, 0, 0, 11, 115, 115, 104, 45, /// 101, 100, 50, 53, 53, 49, 57, /// 0, 0, 0, 32, 121, 27, 123, 184, /// 48, 199, 187, 52, 118, 80, 41, 16, /// 76, 233, 83, 35, 128, 62, 188, /// 207, 47, 46, 28, 204, 70, 112, /// 254, 200, 124, 155, 202, 221]; /// /// let key = PublicKey::from_bytes(&data).unwrap(); /// let fp = key.fingerprint(); /// assert_eq!(fp.hash, "ciQkdxjFUhk2E2vRkWJD9kB8pi+EneOkaCJJHNWzPC4"); /// ``` pub fn from_bytes<T: ?Sized + AsRef<[u8]>>(data: &T) -> Result<PublicKey> { let mut reader = Reader::new(&data); let kt_name = reader.read_string()?; PublicKey::from_reader(&kt_name, &mut reader) } // This function is used for extracting a public key from an existing reader, e.g. // we already have a reader for reading an OpenSSH certificate key and // we want to extract the public key information from it. pub(crate) fn from_reader(kt_name: &str, reader: &mut Reader) -> Result<PublicKey> { let kt = KeyType::from_name(&kt_name)?; let kind = match kt.kind { KeyTypeKind::Rsa | KeyTypeKind::RsaCert => { let k = RsaPublicKey { e: reader.read_mpint()?, n: reader.read_mpint()?, }; PublicKeyKind::Rsa(k) } KeyTypeKind::Ecdsa | KeyTypeKind::EcdsaCert => { let identifier = reader.read_string()?; let curve = Curve::from_identifier(&identifier)?; let key = reader.read_bytes()?; let k = EcdsaPublicKey { curve, key, }; PublicKeyKind::Ecdsa(k) } KeyTypeKind::Ed25519 | KeyTypeKind::Ed25519Cert => { let k = Ed25519PublicKey { key: reader.read_bytes()?, }; PublicKeyKind::Ed25519(k) } }; let key = PublicKey { key_type: kt, kind, comment: None, }; Ok(key) } /// Returns the number of bits of the public key. /// /// # Example /// /// ```rust /// # use rustica_keys::ssh::PublicKey; /// let key = PublicKey::from_string("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHkbe7gwx7s0dlApEEzpUyOAPrzPLy4czEZw/sh8m8rd me@home").unwrap(); /// assert_eq!(key.bits(), 256); /// ``` pub fn bits(&self) -> usize { match self.kind { // For RSA public key the size of the key is the number of bits of the modulus PublicKeyKind::Rsa(ref k) => k.n.len() * 8, // ECDSA key size depends on the curve PublicKeyKind::Ecdsa(ref k) => match k.curve.kind { CurveKind::Nistp256 => 256, CurveKind::Nistp384 => 384, CurveKind::Nistp521 => 521, }, // ED25519 key size is 256 bits // https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-03#section-5.5 PublicKeyKind::Ed25519(_) => 256, } } /// Encodes the public key in an OpenSSH compatible format. /// /// # Example /// ```rust /// # use rustica_keys::ssh::PublicKey; /// let key = PublicKey::from_string("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHkbe7gwx7s0dlApEEzpUyOAPrzPLy4czEZw/sh8m8rd me@home").unwrap(); /// assert_eq!(key.encode(), vec![0, 0, 0, 11, 115, 115, 104, 45, 101, 100, 50, 53, 53, 49, 57, 0, 0, 0, 32, 121, 27, 123, 184, 48, 199, 187, 52, 118, 80, 41, 16, 76, 233, 83, 35, 128, 62, 188, 207, 47, 46, 28, 204, 70, 112, 254, 200, 124, 155, 202, 221]); /// ``` pub fn encode(&self) -> Vec<u8> { let mut w = Writer::new(); w.write_string(self.key_type.plain); match self.kind { PublicKeyKind::Rsa(ref k) => { w.write_mpint(&k.e); w.write_mpint(&k.n); } PublicKeyKind::Ecdsa(ref k) => { w.write_string(&k.curve.identifier); w.write_bytes(&k.key); } PublicKeyKind::Ed25519(ref k) => { w.write_bytes(&k.key); } } w.into_bytes() } /// Computes the fingerprint of the public key using the /// default OpenSSH fingerprint representation with SHA256. /// /// # Example /// /// ```rust /// # use rustica_keys::ssh::{FingerprintKind, PublicKey}; /// # fn example() { /// let key = PublicKey::from_path("/path/to/id_ed25519.pub").unwrap(); /// let fp = key.fingerprint(); /// println!("{}", fp.hash); /// # } /// ``` pub fn fingerprint(&self) -> Fingerprint { self.fingerprint_with(FingerprintKind::Sha256) } /// Computes the fingerprint of the public key using a given /// fingerprint representation. /// /// # Example /// /// ```rust /// # use rustica_keys::ssh::{FingerprintKind, PublicKey}; /// # fn example() { /// let key = PublicKey::from_path("/path/to/id_ed25519.pub").unwrap(); /// let sha512fp = key.fingerprint_with(FingerprintKind::Sha512); /// println!("{}", sha512fp.hash); /// # } /// ``` pub fn fingerprint_with(&self, kind: FingerprintKind) -> Fingerprint { Fingerprint::compute(kind, &self.encode()) } /// Writes the public key to a given writer. /// /// # Example /// ```rust /// # use rustica_keys::ssh::PublicKey; /// use std::fs::File; /// # fn example() { /// let key = PublicKey::from_string("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA...").unwrap(); /// let mut file = File::create("/path/to/id_ed25519.pub").unwrap(); /// key.write(&mut file).unwrap(); /// # } /// ``` pub fn write<W: io::Write>(&self, w: &mut W) -> io::Result<()> { let encoded = self.encode(); let data = base64::encode(&encoded); match self.comment { Some(ref c) => w.write_fmt(format_args!("{} {} {}\n", self.key_type.name, data, c)), None => w.write_fmt(format_args!("{} {}\n", self.key_type.name, data)), } } }