Skip to content

Commit a1f1105

Browse files
committed
Reformat source code
1 parent 5a36763 commit a1f1105

13 files changed

+235
-242
lines changed

examples/key_storage.rs

+39-43
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,84 @@
11
use log::info;
22

3-
use ssh_agent::proto::{from_bytes, to_bytes};
3+
use ssh_agent::agent::Agent;
44
use ssh_agent::proto::message::{self, Message, SignRequest};
5-
use ssh_agent::proto::signature::{self, Signature};
6-
use ssh_agent::proto::public_key::PublicKey;
75
use ssh_agent::proto::private_key::{PrivateKey, RsaPrivateKey};
8-
use ssh_agent::agent::Agent;
6+
use ssh_agent::proto::public_key::PublicKey;
7+
use ssh_agent::proto::signature::{self, Signature};
8+
use ssh_agent::proto::{from_bytes, to_bytes};
99

10-
use std::sync::RwLock;
1110
use std::error::Error;
1211
use std::fs::remove_file;
12+
use std::sync::RwLock;
1313

14-
use openssl::sign::Signer;
15-
use openssl::rsa::Rsa;
16-
use openssl::pkey::PKey;
17-
use openssl::hash::MessageDigest;
1814
use openssl::bn::BigNum;
15+
use openssl::hash::MessageDigest;
16+
use openssl::pkey::PKey;
1917
use openssl::pkey::Private;
18+
use openssl::rsa::Rsa;
19+
use openssl::sign::Signer;
2020

2121
#[derive(Clone, PartialEq, Debug)]
2222
struct Identity {
2323
pubkey: PublicKey,
2424
privkey: PrivateKey,
25-
comment: String
25+
comment: String,
2626
}
2727

2828
struct KeyStorage {
29-
identities: RwLock<Vec<Identity>>
29+
identities: RwLock<Vec<Identity>>,
3030
}
3131

3232
impl KeyStorage {
3333
fn new() -> Self {
3434
Self {
35-
identities: RwLock::new(vec![])
35+
identities: RwLock::new(vec![]),
3636
}
3737
}
38-
39-
fn identity_index_from_pubkey(
40-
identities: &Vec<Identity>,
41-
pubkey: &PublicKey
42-
) -> Option<usize> {
38+
39+
fn identity_index_from_pubkey(identities: &Vec<Identity>, pubkey: &PublicKey) -> Option<usize> {
4340
for (index, identity) in identities.iter().enumerate() {
4441
if &identity.pubkey == pubkey {
4542
return Some(index);
4643
}
4744
}
4845
return None;
4946
}
50-
47+
5148
fn identity_from_pubkey(&self, pubkey: &PublicKey) -> Option<Identity> {
5249
let identities = self.identities.read().unwrap();
53-
50+
5451
let index = Self::identity_index_from_pubkey(&identities, pubkey)?;
5552
Some(identities[index].clone())
5653
}
57-
54+
5855
fn identity_add(&self, identity: Identity) {
5956
let mut identities = self.identities.write().unwrap();
6057
if Self::identity_index_from_pubkey(&identities, &identity.pubkey) == None {
6158
identities.push(identity);
6259
}
6360
}
64-
61+
6562
fn identity_remove(&self, pubkey: &PublicKey) -> Result<(), Box<dyn Error>> {
6663
let mut identities = self.identities.write().unwrap();
67-
64+
6865
if let Some(index) = Self::identity_index_from_pubkey(&identities, &pubkey) {
6966
identities.remove(index);
7067
Ok(())
7168
} else {
7269
Err(From::from("Failed to remove identity: identity not found"))
7370
}
7471
}
75-
72+
7673
fn sign(&self, sign_request: &SignRequest) -> Result<Signature, Box<dyn Error>> {
7774
let pubkey: PublicKey = from_bytes(&sign_request.pubkey_blob)?;
78-
75+
7976
if let Some(identity) = self.identity_from_pubkey(&pubkey) {
8077
match identity.privkey {
8178
PrivateKey::Rsa(ref key) => {
8279
let algorithm;
8380
let digest;
84-
81+
8582
if sign_request.flags & signature::RSA_SHA2_512 != 0 {
8683
algorithm = "rsa-sha2-512";
8784
digest = MessageDigest::sha512();
@@ -92,54 +89,54 @@ impl KeyStorage {
9289
algorithm = "ssh-rsa";
9390
digest = MessageDigest::sha1();
9491
}
95-
92+
9693
let keypair = PKey::from_rsa(rsa_openssl_from_ssh(key)?)?;
9794
let mut signer = Signer::new(digest, &keypair)?;
9895
signer.update(&sign_request.data)?;
99-
96+
10097
Ok(Signature {
10198
algorithm: algorithm.to_string(),
102-
blob: signer.sign_to_vec()?
99+
blob: signer.sign_to_vec()?,
103100
})
104-
},
105-
_ => Err(From::from("Signature for key type not implemented"))
101+
}
102+
_ => Err(From::from("Signature for key type not implemented")),
106103
}
107104
} else {
108105
Err(From::from("Failed to create signature: identity not found"))
109106
}
110107
}
111-
112-
fn handle_message(&self, request: Message) -> Result<Message, Box<dyn Error>> {
108+
109+
fn handle_message(&self, request: Message) -> Result<Message, Box<dyn Error>> {
113110
info!("Request: {:?}", request);
114111
let response = match request {
115112
Message::RequestIdentities => {
116113
let mut identities = vec![];
117114
for identity in self.identities.read().unwrap().iter() {
118115
identities.push(message::Identity {
119116
pubkey_blob: to_bytes(&identity.pubkey)?,
120-
comment: identity.comment.clone()
117+
comment: identity.comment.clone(),
121118
})
122119
}
123120
Ok(Message::IdentitiesAnswer(identities))
124-
},
121+
}
125122
Message::RemoveIdentity(identity) => {
126123
let pubkey: PublicKey = from_bytes(&identity.pubkey_blob)?;
127124
self.identity_remove(&pubkey)?;
128125
Ok(Message::Success)
129-
},
126+
}
130127
Message::AddIdentity(identity) => {
131128
self.identity_add(Identity {
132129
pubkey: PublicKey::from(&identity.privkey),
133130
privkey: identity.privkey,
134-
comment: identity.comment
131+
comment: identity.comment,
135132
});
136133
Ok(Message::Success)
137-
},
134+
}
138135
Message::SignRequest(request) => {
139136
let signature = to_bytes(&self.sign(&request)?)?;
140137
Ok(Message::SignResponse(signature))
141-
},
142-
_ => Err(From::from(format!("Unknown message: {:?}", request)))
138+
}
139+
_ => Err(From::from(format!("Unknown message: {:?}", request))),
143140
};
144141
info!("Response {:?}", response);
145142
return response;
@@ -148,7 +145,7 @@ impl KeyStorage {
148145

149146
impl Agent for KeyStorage {
150147
type Error = ();
151-
148+
152149
fn handle(&self, message: Message) -> Result<Message, ()> {
153150
self.handle_message(message).or_else(|error| {
154151
println!("Error handling message - {:?}", error);
@@ -157,7 +154,6 @@ impl Agent for KeyStorage {
157154
}
158155
}
159156

160-
161157
fn rsa_openssl_from_ssh(ssh_rsa: &RsaPrivateKey) -> Result<Rsa<Private>, Box<dyn Error>> {
162158
let n = BigNum::from_slice(&ssh_rsa.n)?;
163159
let e = BigNum::from_slice(&ssh_rsa.e)?;
@@ -167,15 +163,15 @@ fn rsa_openssl_from_ssh(ssh_rsa: &RsaPrivateKey) -> Result<Rsa<Private>, Box<dyn
167163
let q = BigNum::from_slice(&ssh_rsa.q)?;
168164
let dp = &d % &(&p - &BigNum::from_u32(1)?);
169165
let dq = &d % &(&q - &BigNum::from_u32(1)?);
170-
166+
171167
Ok(Rsa::from_private_components(n, e, d, p, q, dp, dq, qi)?)
172168
}
173169

174170
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
175171
let agent = KeyStorage::new();
176172
let socket = "connect.sock";
177173
let _ = remove_file(socket);
178-
174+
179175
env_logger::init();
180176
agent.run_unix(socket)?;
181177
Ok(())

src/agent.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use byteorder::{BigEndian, ReadBytesExt};
2-
use bytes::{BytesMut, BufMut};
2+
use bytes::{BufMut, BytesMut};
33
use futures::future::FutureResult;
44
use log::{error, info};
5-
use tokio::codec::{Framed, Encoder, Decoder};
5+
use tokio::codec::{Decoder, Encoder, Framed};
66
use tokio::net::TcpListener;
77
use tokio::prelude::*;
88
use tokio_uds::UnixListener;
@@ -23,20 +23,20 @@ struct MessageCodec;
2323
impl Decoder for MessageCodec {
2424
type Item = Message;
2525
type Error = AgentError;
26-
26+
2727
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
2828
let mut bytes = &src[..];
29-
29+
3030
if bytes.len() < size_of::<u32>() {
3131
return Ok(None);
3232
}
33-
33+
3434
let length = bytes.read_u32::<BigEndian>()? as usize;
35-
35+
3636
if bytes.len() < length {
3737
return Ok(None);
3838
}
39-
39+
4040
let message: Message = from_bytes(bytes)?;
4141
src.advance(size_of::<u32>() + length);
4242
Ok(Some(message))
@@ -46,7 +46,7 @@ impl Decoder for MessageCodec {
4646
impl Encoder for MessageCodec {
4747
type Item = Message;
4848
type Error = AgentError;
49-
49+
5050
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
5151
let bytes = to_bytes(&to_bytes(&item)?)?;
5252
dst.put(bytes);
@@ -58,35 +58,39 @@ macro_rules! handle_clients {
5858
($self:ident, $socket:ident) => {{
5959
info!("Listening; socket = {:?}", $socket);
6060
let arc_self = Arc::new($self);
61-
$socket.incoming()
61+
$socket
62+
.incoming()
6263
.map_err(|e| error!("Failed to accept socket; error = {:?}", e))
6364
.for_each(move |socket| {
6465
let (write, read) = Framed::new(socket, MessageCodec).split();
6566
let arc_self = arc_self.clone();
66-
let connection = write.send_all(read.and_then(move |message| {
67-
arc_self.handle_async(message).map_err(|e| {
68-
error!("Error handling message; error = {:?}", e);
69-
AgentError::User
70-
})
71-
})).map(|_| ())
72-
.map_err(|e| error!("Error while handling message; error = {:?}", e));
67+
let connection = write
68+
.send_all(read.and_then(move |message| {
69+
arc_self.handle_async(message).map_err(|e| {
70+
error!("Error handling message; error = {:?}", e);
71+
AgentError::User
72+
})
73+
}))
74+
.map(|_| ())
75+
.map_err(|e| error!("Error while handling message; error = {:?}", e));
7376
tokio::spawn(connection)
74-
}).map_err(|e| e.into())
77+
})
78+
.map_err(|e| e.into())
7579
}};
7680
}
7781

7882
pub trait Agent: 'static + Sync + Send + Sized {
7983
type Error: Debug + Send + Sync;
80-
84+
8185
fn handle(&self, message: Message) -> Result<Message, Self::Error>;
82-
86+
8387
fn handle_async(
8488
&self,
85-
message: Message
89+
message: Message,
8690
) -> Box<dyn Future<Item = Message, Error = Self::Error> + Send + Sync> {
8791
Box::new(FutureResult::from(self.handle(message)))
8892
}
89-
93+
9094
fn run_listener(self, socket: UnixListener) -> Result<(), Box<dyn Error + Send + Sync>> {
9195
Ok(tokio::run(handle_clients!(self, socket)))
9296
}

src/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::io;
21
use super::proto::error::ProtoError;
2+
use std::io;
33

44
#[derive(Debug)]
55
pub enum AgentError {
66
User,
77
Proto(ProtoError),
8-
IO(io::Error)
8+
IO(io::Error),
99
}
1010

1111
impl From<ProtoError> for AgentError {
@@ -18,4 +18,4 @@ impl From<io::Error> for AgentError {
1818
fn from(e: io::Error) -> AgentError {
1919
AgentError::IO(e)
2020
}
21-
}
21+
}

0 commit comments

Comments
 (0)