feat: update example ssh agent

This commit is contained in:
2022-04-25 00:34:48 +08:00
parent 962750c670
commit 9b35e28819

View File

@@ -1,23 +1,20 @@
use ssh_agent::proto::{from_bytes, RsaPublicKey, to_bytes};
use ssh_agent::proto::message::{self, Message, SignRequest};
use ssh_agent::proto::signature::{self, Signature};
use ssh_agent::proto::public_key::PublicKey;
use ssh_agent::proto::private_key::{PrivateKey, RsaPrivateKey};
use ssh_agent::agent::Agent;
use std::sync::RwLock;
use std::error::Error; use std::error::Error;
use std::fs::remove_file; use std::fs::remove_file;
use std::sync::RwLock;
use openssl::sign::Signer;
use openssl::rsa::Rsa;
use openssl::pkey::PKey;
use openssl::hash::MessageDigest;
use openssl::bn::BigNum; use openssl::bn::BigNum;
use openssl::hash::MessageDigest;
use openssl::pkey::PKey;
use openssl::pkey::Private; use openssl::pkey::Private;
use openssl::rsa::Rsa;
use openssl::sign::Signer;
use rust_util::information; use rust_util::information;
use ssh_key::MPInt; use ssh_agent::agent::Agent;
use ssh_key::public::KeyData; use ssh_agent::proto::{from_bytes, RsaPublicKey, to_bytes};
use ssh_agent::proto::message::{self, Message, SignRequest};
use ssh_agent::proto::private_key::{PrivateKey, RsaPrivateKey};
use ssh_agent::proto::public_key::PublicKey;
use ssh_agent::proto::signature::{self, Signature};
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug)]
struct Identity { struct Identity {
@@ -33,21 +30,17 @@ struct KeyStorage {
impl KeyStorage { impl KeyStorage {
fn new() -> Self { fn new() -> Self {
let rsa = Rsa::generate(2048).unwrap(); let rsa = Rsa::generate(2048).unwrap();
let mut n = rsa.n().to_vec();
if n[0] >= 128 {
n.insert(0, 0x00);
}
let pubkey = PublicKey::Rsa(RsaPublicKey { let pubkey = PublicKey::Rsa(RsaPublicKey {
e: rsa.e().to_vec(), e: with_sign(rsa.e().to_vec()),
n: n, n: with_sign(rsa.n().to_vec()),
}); });
let privkey = PrivateKey::Rsa(RsaPrivateKey { let privkey = PrivateKey::Rsa(RsaPrivateKey {
e: rsa.e().to_vec(), e: with_sign(rsa.e().to_vec()),
n: rsa.n().to_vec(), n: with_sign(rsa.n().to_vec()),
d: rsa.d().to_vec(), d: with_sign(rsa.d().to_vec()),
iqmp: rsa.iqmp().unwrap().to_vec(), iqmp: with_sign(rsa.iqmp().unwrap().to_vec()),
p: rsa.p().unwrap().to_vec(), p: with_sign(rsa.p().unwrap().to_vec()),
q: rsa.q().unwrap().to_vec(), q: with_sign(rsa.q().unwrap().to_vec()),
}); });
let ident = Identity { let ident = Identity {
pubkey, pubkey,
@@ -55,11 +48,18 @@ impl KeyStorage {
comment: "testkey".to_string(), comment: "testkey".to_string(),
}; };
let pubkey = ssh_key::PublicKey::from(KeyData::Rsa(ssh_key::public::RsaPublicKey { let mut ssh_key = vec![];
e: MPInt::from_bytes(&rsa.e().to_vec()).unwrap(), let ssh_rsa_bytes = "ssh-rsa".as_bytes();
n: MPInt::from_bytes(&rsa.n().to_vec()).unwrap(), ssh_key.extend_from_slice(&(ssh_rsa_bytes.len() as u32).to_be_bytes()[..]);
})); ssh_key.extend_from_slice(ssh_rsa_bytes);
information!("{}", pubkey.to_string()); let e = with_sign(rsa.e().to_vec());
ssh_key.extend_from_slice(&(e.len() as u32).to_be_bytes()[..]);
ssh_key.extend_from_slice(&e);
let n = with_sign(rsa.n().to_vec());
ssh_key.extend_from_slice(&(n.len() as u32).to_be_bytes()[..]);
ssh_key.extend_from_slice(&n);
information!("{:?}", ssh_key);
information!("ssh-rsa {} {}", base64::encode(&ssh_key), ident.comment);
Self { Self {
identities: RwLock::new(vec![ident]) identities: RwLock::new(vec![ident])
} }
@@ -216,3 +216,10 @@ fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
agent.run_unix(socket)?; agent.run_unix(socket)?;
Ok(()) Ok(())
} }
pub fn with_sign(mut vec: Vec<u8>) -> Vec<u8> {
if vec.len() > 0 && vec[0] >= 128 {
vec.insert(0, 0x00);
}
vec
}