Skip to content

Commit

Permalink
Add support for Ed25519 private keys
Browse files Browse the repository at this point in the history
  • Loading branch information
obelisk committed Feb 7, 2021
1 parent 20c1f09 commit f6ab0d0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sshcerts"
version = "0.3.6"
version = "0.3.7"
authors = ["Mitchell Grenier <[email protected]>"]
edition = "2018"
license-file = "LICENSE"
Expand Down
6 changes: 3 additions & 3 deletions src/ssh/privkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down Expand Up @@ -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()))),
};

Expand Down
27 changes: 27 additions & 0 deletions tests/privkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit f6ab0d0

Please sign in to comment.