From f6ab0d0b7588a05fdeaca6283be6e7fb623ffbc7 Mon Sep 17 00:00:00 2001 From: Mitchell Grenier Date: Sun, 7 Feb 2021 16:25:58 -0500 Subject: [PATCH] Add support for Ed25519 private keys --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/ssh/privkey.rs | 6 +++--- tests/privkey.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fc1928c..01f86f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -812,7 +812,7 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" [[package]] name = "sshcerts" -version = "0.3.6" +version = "0.3.7" dependencies = [ "base64 0.12.3", "clap", diff --git a/Cargo.toml b/Cargo.toml index 81cb3d5..d11c071 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sshcerts" -version = "0.3.6" +version = "0.3.7" authors = ["Mitchell Grenier "] edition = "2018" license-file = "LICENSE" diff --git a/src/ssh/privkey.rs b/src/ssh/privkey.rs index 9314a60..3b09c26 100644 --- a/src/ssh/privkey.rs +++ b/src/ssh/privkey.rs @@ -95,7 +95,6 @@ impl PrivateKey { let decoded = base64::decode(encoded_key)?; let mut reader = Reader::new(&decoded); - // Construct a new `PrivateKey` let k = PrivateKey::from_reader(&mut reader)?; @@ -171,13 +170,14 @@ impl PrivateKey { PrivateKeyKind::Ecdsa(k) } - /*KeyTypeKind::Ed25519 => { + KeyTypeKind::Ed25519 => { + let _pubkey = reader.read_bytes()?; let k = Ed25519PrivateKey { key: reader.read_bytes()?, }; PrivateKeyKind::Ed25519(k) - }*/ + } _ => return Err(Error::with_kind(ErrorKind::UnknownKeyType(kt.name.to_string()))), }; diff --git a/tests/privkey.rs b/tests/privkey.rs index 278f969..10a220d 100644 --- a/tests/privkey.rs +++ b/tests/privkey.rs @@ -51,3 +51,30 @@ fn parse_ecdsa_256_private_key() { }; assert_eq!(hex::encode(&key.key), "008641adbf4f7b49be0646c7bf4a1551f69d9b791ebf836de34ef372e36212a1dc"); } + +#[test] +fn parse_ed25519_private_key() { + let privkey = concat!( + "-----BEGIN OPENSSH PRIVATE KEY-----\n", + "b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW\n", + "QyNTUxOQAAACAztFUA/UyHSAmS1hVsLX+7PP2hDb3vLcBkxJjVdJsoeQAAAJgzkRiyM5EY\n", + "sgAAAAtzc2gtZWQyNTUxOQAAACAztFUA/UyHSAmS1hVsLX+7PP2hDb3vLcBkxJjVdJsoeQ\n", + "AAAEDJnaJY4O5n62ipU6NGquweXk5WDdCvMDO8Y6IxtsSxLTO0VQD9TIdICZLWFWwtf7s8\n", + "/aENve8twGTEmNV0myh5AAAAE29iZWxpc2tAZXhjbGF2ZS5sYW4BAg==\n", + "-----END OPENSSH PRIVATE KEY-----"); + + let privkey = PrivateKey::from_string(privkey); + match &privkey { + Ok(_) => (), + Err(e) => println!("{}", e), + }; + assert!(privkey.is_ok()); + let privkey = privkey.unwrap(); + assert_eq!(privkey.pubkey.fingerprint().hash, "QAtqtvvCePelMMUNPP7madH2zNa1ATxX1nt9L/0C5+M"); + + let key = match privkey.kind { + PrivateKeyKind::Ed25519(key) => key, + _ => panic!("Wrong key type detected"), + }; + assert_eq!(hex::encode(&key.key), "c99da258e0ee67eb68a953a346aaec1e5e4e560dd0af3033bc63a231b6c4b12d33b45500fd4c87480992d6156c2d7fbb3cfda10dbdef2dc064c498d5749b2879"); +} \ No newline at end of file