Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid private key by from_keystr for OpenSSH format #5

Merged
merged 2 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/format/ossh_privkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use crate::sshbuf::{SshBuf, SshReadExt, SshWriteExt};
use bcrypt_pbkdf::bcrypt_pbkdf;
use byteorder::WriteBytesExt;
use cryptovec::CryptoVec;
use openssl::bn::BigNum;
use openssl::dsa::Dsa;
use openssl::rsa::RsaPrivateKeyBuilder;
use openssl::rsa::Rsa;
use rand::prelude::*;
use rand::rngs::StdRng;
use std::io::{Cursor, Read, Write};
Expand Down Expand Up @@ -131,13 +132,13 @@ fn decode_key(reader: &mut SshBuf) -> OsshResult<KeyPair> {
let n = reader.read_mpint()?;
let e = reader.read_mpint()?;
let d = reader.read_mpint()?;
let mut _iqmp = reader.read_mpint()?;
let iqmp = reader.read_mpint()?;
let p = reader.read_mpint()?;
let q = reader.read_mpint()?;
let rsa = RsaPrivateKeyBuilder::new(n, e, d)?
.set_factors(p, q)?
.build();
_iqmp.clear(); // Explicity clear the sensitive data
let one = BigNum::from_u32(1)?;
let dmp1 = &d % &(&p - &one);
let dmq1 = &d % &(&q - &one);
let rsa = Rsa::from_private_components(n, e, d, p, q, dmp1, dmq1, iqmp)?;
match keyname {
RSA_NAME => RsaKeyPair::from_ossl_rsa(rsa, RsaSignature::SHA1),
RSA_SHA256_NAME => RsaKeyPair::from_ossl_rsa(rsa, RsaSignature::SHA2_256),
Expand Down
11 changes: 11 additions & 0 deletions tests/keyfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ fn verify_key<P: AsRef<Path>>(keyfile: P, passphrase: Option<&str>) {
let pubkey = PublicKey::from_keystr(from_utf8(pubdata.as_slice()).unwrap()).unwrap();

utils::fingerprint_assert(&privkey, &pubkey);

// Make sure that privkey can be serialized
// https://github.com/Leo1003/rust-osshkeys/issues/4
if privkey.keytype() != KeyType::ED25519 {
// ED25519 key can only be stored in OpenSSH format
let _ = privkey.serialize_pem(None).unwrap();
let _ = privkey.serialize_pkcs8(None).unwrap();
}
let _ = privkey
.serialize_openssh(None, osshkeys::cipher::Cipher::Null)
.unwrap();
}

#[test]
Expand Down