Full SSH support for RSA.

This commit is contained in:
2019-04-14 22:12:36 -07:00
parent 8c87f945a1
commit 031b4be14e
5 changed files with 297 additions and 46 deletions

View File

@@ -5,7 +5,7 @@ mod rsa;
pub use self::errors::{SSHKeyParseError,SSHKeyRenderError};
use base64::{decode,encode};
use base64::decode;
use self::frame::*;
use std::fs::File;
use std::io::{Cursor,Read,Write};
@@ -137,17 +137,12 @@ pub fn write_ssh_keyfile<KP,P>(path: P, x: &KP, comment: &str) -> Result<(),SSHK
file.write_all(&bytes)?;
file.sync_all()?;
Ok(())
}
#[cfg(test)]
use cryptonum::unsigned::{U1024};
#[cfg(test)]
use dsa::{DSAKeyPair,DSAPublicKey,L1024N160};
#[cfg(test)]
use rsa::{RSAKeyPair,RSAPublicKey,SIGNING_HASH_SHA256};
use rsa::{RSAPair,RSAPublic,SIGNING_HASH_SHA256};
#[cfg(test)]
use sha2::Sha256;
@@ -210,41 +205,37 @@ fn read_dsa_examples() {
#[cfg(test)]
#[test]
fn read_rsa_examples() {
let test_files = ["rsa1024-1", "rsa1024-2", "rsa1024-3"];
let test_files = ["rsa1024-1", "rsa1024-2", "rsa1024-3",
"rsa2048-1", "rsa2048-2", "rsa2048-3",
"rsa3072-1", "rsa3072-2", "rsa3072-3",
"rsa4096-1", "rsa4096-2", "rsa4096-3",
"rsa8192-1", "rsa8192-2", "rsa8192-3"];
for file in test_files.iter() {
let path = format!("testdata/ssh/{}",file);
let mkeypair = load_ssh_keyfile(path);
let mkeypair = load_ssh_keyfile::<RSAPair,String>(path);
match mkeypair {
Err(e) => assert!(false, format!("reading error: {:?}", e)),
Ok((keypair, comment)) => {
let buffer = [0,1,2,3,4,6,2];
let _ : RSAKeyPair<U1024> = keypair;
let sig = keypair.private.sign(&SIGNING_HASH_SHA256, &buffer);
assert!(keypair.public.verify(&SIGNING_HASH_SHA256, &buffer, &sig));
let buffer2 = [0,1,2,3,4,6,5];
assert!(!keypair.public.verify(&SIGNING_HASH_SHA256, &buffer2, &sig));
let sig = keypair.sign(&SIGNING_HASH_SHA256, &buffer);
assert!(keypair.verify(&SIGNING_HASH_SHA256, &buffer, &sig));
match encode_ssh(&keypair, &comment) {
Err(e2) => assert!(false, format!("render error: {:?}", e2)),
Ok(encodedstr) => {
match decode_ssh(&encodedstr) {
Err(e3) => assert!(false, format!("reparse error: {:?}", e3)),
Ok((keypair2,comment2)) => {
let _ : RSAKeyPair<U1024> = keypair2;
assert_eq!(keypair.public.n,keypair2.public.n,"failed to reparse key pair (n)");
assert_eq!(keypair.public.e,keypair2.public.e,"failed to reparse key pair (e)");
assert_eq!(keypair.private.nu,keypair2.private.nu,"failed to reparse key pair (n)");
assert_eq!(keypair.private.d,keypair2.private.d,"failed to reparse key pair (d)");
assert_eq!(keypair,keypair2,"failed to reparse key pair");
assert_eq!(comment,comment2,"failed to reparse comment");
let ppath = format!("testdata/ssh/{}.pub",file);
match load_ssh_pubkeys::<RSAKeyPair<U1024>,String>(ppath) {
match load_ssh_pubkeys::<RSAPair,String>(ppath) {
Err(e4) => assert!(false, format!("pubkey error: {:?}", e4)),
Ok(pubkeys) => {
let _ : Vec<(RSAPublicKey<U1024>,String)> = pubkeys;
let _ : Vec<(RSAPublic,String)> = pubkeys;
for (pubkey, comment3) in pubkeys {
assert_eq!(pubkey.n, keypair.public.n, "public key check (n)");
assert_eq!(pubkey.e, keypair.public.e, "public key check (e)");
assert_eq!(comment, comment3, "public key check comment")
assert_eq!(pubkey, keypair.public(), "public key check");
assert_eq!(comment, comment3, "public key check comment");
}
}
}