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::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::hash::MessageDigest;
use openssl::pkey::PKey;
use openssl::pkey::Private;
use openssl::rsa::Rsa;
use openssl::sign::Signer;
use rust_util::information;
use ssh_key::MPInt;
use ssh_key::public::KeyData;
use ssh_agent::agent::Agent;
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)]
struct Identity {
@@ -33,21 +30,17 @@ struct KeyStorage {
impl KeyStorage {
fn new() -> Self {
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 {
e: rsa.e().to_vec(),
n: n,
e: with_sign(rsa.e().to_vec()),
n: with_sign(rsa.n().to_vec()),
});
let privkey = PrivateKey::Rsa(RsaPrivateKey {
e: rsa.e().to_vec(),
n: rsa.n().to_vec(),
d: rsa.d().to_vec(),
iqmp: rsa.iqmp().unwrap().to_vec(),
p: rsa.p().unwrap().to_vec(),
q: rsa.q().unwrap().to_vec(),
e: with_sign(rsa.e().to_vec()),
n: with_sign(rsa.n().to_vec()),
d: with_sign(rsa.d().to_vec()),
iqmp: with_sign(rsa.iqmp().unwrap().to_vec()),
p: with_sign(rsa.p().unwrap().to_vec()),
q: with_sign(rsa.q().unwrap().to_vec()),
});
let ident = Identity {
pubkey,
@@ -55,11 +48,18 @@ impl KeyStorage {
comment: "testkey".to_string(),
};
let pubkey = ssh_key::PublicKey::from(KeyData::Rsa(ssh_key::public::RsaPublicKey {
e: MPInt::from_bytes(&rsa.e().to_vec()).unwrap(),
n: MPInt::from_bytes(&rsa.n().to_vec()).unwrap(),
}));
information!("{}", pubkey.to_string());
let mut ssh_key = vec![];
let ssh_rsa_bytes = "ssh-rsa".as_bytes();
ssh_key.extend_from_slice(&(ssh_rsa_bytes.len() as u32).to_be_bytes()[..]);
ssh_key.extend_from_slice(ssh_rsa_bytes);
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 {
identities: RwLock::new(vec![ident])
}
@@ -215,4 +215,11 @@ fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
information!("Start unix socket: {}", socket);
agent.run_unix(socket)?;
Ok(())
}
pub fn with_sign(mut vec: Vec<u8>) -> Vec<u8> {
if vec.len() > 0 && vec[0] >= 128 {
vec.insert(0, 0x00);
}
vec
}