improvements

This commit is contained in:
nora 2024-08-11 02:37:22 +02:00
parent 0bfb1818a6
commit faf2010051
5 changed files with 48 additions and 87 deletions

View file

@ -40,6 +40,19 @@ async fn handle_connection(next: (TcpStream, SocketAddr)) -> Result<()> {
info!(?addr, "Received a new connection"); info!(?addr, "Received a new connection");
//let rng = vec![
// 0x14, 0xa2, 0x04, 0xa5, 0x4b, 0x2f, 0x5f, 0xa7, 0xff, 0x53, 0x13, 0x67, 0x57, 0x67, 0xbc,
// 0x55, 0x3f, 0xc0, 0x6c, 0x0d, 0x07, 0x8f, 0xe2, 0x75, 0x95, 0x18, 0x4b, 0xd2, 0xcb, 0xd0,
// 0x64, 0x06,
//];
//struct HardcodedRng(Vec<u8>);
//impl ssh_transport::SshRng for HardcodedRng {
// fn fill_bytes(&mut self, dest: &mut [u8]) {
// dest.copy_from_slice(&self.0[..dest.len()]);
// self.0.splice(0..dest.len(), []);
// }
//}
let mut state = ServerConnection::new(ThreadRngRand); let mut state = ServerConnection::new(ThreadRngRand);
loop { loop {

View file

@ -27,7 +27,7 @@ pub(crate) struct Plaintext;
impl Keys for Plaintext { impl Keys for Plaintext {
fn decrypt_len(&mut self, _: &mut [u8; 4], _: u64) {} fn decrypt_len(&mut self, _: &mut [u8; 4], _: u64) {}
fn decrypt_packet(&mut self, raw: RawPacket, _: u64) -> Result<Packet> { fn decrypt_packet(&mut self, raw: RawPacket, _: u64) -> Result<Packet> {
Packet::from_raw(&raw.rest()) Packet::from_raw(raw.rest())
} }
fn encrypt_packet_to_msg(&mut self, packet: Packet, _: u64) -> Msg { fn encrypt_packet_to_msg(&mut self, packet: Packet, _: u64) -> Msg {
Msg(MsgKind::PlaintextPacket(packet)) Msg(MsgKind::PlaintextPacket(packet))
@ -196,8 +196,6 @@ impl SshChaCha20Poly1305 {
fn encrypt_packet(&mut self, packet: Packet, packet_number: u64) -> EncryptedPacket { fn encrypt_packet(&mut self, packet: Packet, packet_number: u64) -> EncryptedPacket {
let mut bytes = packet.to_bytes(false); let mut bytes = packet.to_bytes(false);
dbg!(u32::from_be_bytes(bytes[0..4].try_into().unwrap()));
// Prepare the main cipher. // Prepare the main cipher.
let mut main_cipher = <SshChaCha20 as chacha20::cipher::KeyIvInit>::new( let mut main_cipher = <SshChaCha20 as chacha20::cipher::KeyIvInit>::new(
&self.main_key, &self.main_key,
@ -223,12 +221,9 @@ impl SshChaCha20Poly1305 {
// Now, MAC the length || content, and push that to the end. // Now, MAC the length || content, and push that to the end.
let mac = poly1305::Poly1305::new(&poly1305_key.into()).compute_unpadded(&bytes); let mac = poly1305::Poly1305::new(&poly1305_key.into()).compute_unpadded(&bytes);
dbg!(bytes.len());
bytes.extend_from_slice(&mac); bytes.extend_from_slice(&mac);
dbg!(bytes.len());
EncryptedPacket::from_encrypted_full_bytes(bytes) EncryptedPacket::from_encrypted_full_bytes(bytes)
} }
} }

View file

@ -86,7 +86,8 @@ impl rand::RngCore for SshRngRandAdapter<'_> {
} }
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> std::result::Result<(), rand::Error> { fn try_fill_bytes(&mut self, dest: &mut [u8]) -> std::result::Result<(), rand::Error> {
Ok(self.fill_bytes(dest)) self.fill_bytes(dest);
Ok(())
} }
} }
@ -113,7 +114,7 @@ impl ServerConnection {
pub fn recv_bytes(&mut self, bytes: &[u8]) -> Result<()> { pub fn recv_bytes(&mut self, bytes: &[u8]) -> Result<()> {
if let ServerState::ProtoExchange { received } = &mut self.state { if let ServerState::ProtoExchange { received } = &mut self.state {
received.extend_from_slice(bytes); received.extend_from_slice(bytes);
if received.windows(2).find(|win| win == b"\r\n").is_some() { if received.windows(2).any(|win| win == b"\r\n") {
// TODO: care that its SSH 2.0 instead of anythin anything else // TODO: care that its SSH 2.0 instead of anythin anything else
// The client will not send any more information than this until we respond, so discord the rest of the bytes. // The client will not send any more information than this until we respond, so discord the rest of the bytes.
let client_identification = received.to_owned(); let client_identification = received.to_owned();
@ -229,7 +230,7 @@ impl ServerConnection {
let client_public_key = dh.e; // Q_C let client_public_key = dh.e; // Q_C
let shared_secret = let shared_secret =
secret.diffie_hellman(&client_public_key.to_x25519_public_key()?); // K secret.diffie_hellman(&client_public_key.as_x25519_public_key()?); // K
let pub_hostkey = SshPublicKey { let pub_hostkey = SshPublicKey {
format: b"ssh-ed25519", format: b"ssh-ed25519",
@ -262,7 +263,7 @@ impl ServerConnection {
// For normal DH as in RFC4253, e and f are mpints. // For normal DH as in RFC4253, e and f are mpints.
// But for ECDH as defined in RFC5656, Q_C and Q_S are strings. // But for ECDH as defined in RFC5656, Q_C and Q_S are strings.
// <https://datatracker.ietf.org/doc/html/rfc5656#section-4> // <https://datatracker.ietf.org/doc/html/rfc5656#section-4>
hash_string(&mut hash, &client_public_key.0); // Q_C hash_string(&mut hash, client_public_key.0); // Q_C
hash_string(&mut hash, server_public_key.as_bytes()); // Q_S hash_string(&mut hash, server_public_key.as_bytes()); // Q_S
hash_mpint(&mut hash, shared_secret.as_bytes()); // K hash_mpint(&mut hash, shared_secret.as_bytes()); // K
@ -295,7 +296,7 @@ impl ServerConnection {
}; };
} }
ServerState::NewKeys { h, k } => { ServerState::NewKeys { h, k } => {
if packet.payload != &[Packet::SSH_MSG_NEWKEYS] { if packet.payload != [Packet::SSH_MSG_NEWKEYS] {
return Err(client_error!("did not send SSH_MSG_NEWKEYS")); return Err(client_error!("did not send SSH_MSG_NEWKEYS"));
} }
@ -308,7 +309,7 @@ impl ServerConnection {
self.packet_transport.set_key(h, k); self.packet_transport.set_key(h, k);
} }
ServerState::ServiceRequest => { ServerState::ServiceRequest => {
if packet.payload.get(0) != Some(&Packet::SSH_MSG_SERVICE_REQUEST) { if packet.payload.first() != Some(&Packet::SSH_MSG_SERVICE_REQUEST) {
return Err(client_error!("did not send SSH_MSG_SERVICE_REQUEST")); return Err(client_error!("did not send SSH_MSG_SERVICE_REQUEST"));
} }
let mut p = Parser::new(&packet.payload[1..]); let mut p = Parser::new(&packet.payload[1..]);
@ -319,7 +320,6 @@ impl ServerConnection {
return Err(client_error!("only supports ssh-userauth")); return Err(client_error!("only supports ssh-userauth"));
} }
// TODO: encrypt this!
self.packet_transport.queue_packet(Packet { self.packet_transport.queue_packet(Packet {
payload: { payload: {
let mut writer = Writer::new(); let mut writer = Writer::new();
@ -432,75 +432,19 @@ mod tests {
// KEX Init // KEX Init
Part { Part {
client: &hex!( client: &hex!(
" "000005fc0714b76523360210e3119b17bb2ea2301b0800000131736e747275703736317832353531392d736861353132406f70656e7373682e636f6d2c637572766532353531392d7368613235362c637572766532353531392d736861323536406c69627373682e6f72672c656364682d736861322d6e697374703235362c656364682d736861322d6e697374703338342c656364682d736861322d6e697374703532312c6469666669652d68656c6c6d616e2d67726f75702d65786368616e67652d7368613235362c6469666669652d68656c6c6d616e2d67726f757031362d7368613531322c6469666669652d68656c6c6d616e2d67726f757031382d7368613531322c6469666669652d68656c6c6d616e2d67726f757031342d7368613235362c6578742d696e666f2d632c6b65782d7374726963742d632d763030406f70656e7373682e636f6d000001cf7373682d656432353531392d636572742d763031406f70656e7373682e636f6d2c65636473612d736861322d6e697374703235362d636572742d763031406f70656e7373682e636f6d2c65636473612d736861322d6e697374703338342d636572742d763031406f70656e7373682e636f6d2c65636473612d736861322d6e697374703532312d636572742d763031406f70656e7373682e636f6d2c736b2d7373682d656432353531392d636572742d763031406f70656e7373682e636f6d2c736b2d65636473612d736861322d6e697374703235362d636572742d763031406f70656e7373682e636f6d2c7273612d736861322d3531322d636572742d763031406f70656e7373682e636f6d2c7273612d736861322d3235362d636572742d763031406f70656e7373682e636f6d2c7373682d656432353531392c65636473612d736861322d6e697374703235362c65636473612d736861322d6e697374703338342c65636473612d736861322d6e697374703532312c736b2d7373682d65643235353139406f70656e7373682e636f6d2c736b2d65636473612d736861322d6e69737470323536406f70656e7373682e636f6d2c7273612d736861322d3531322c7273612d736861322d3235360000006c63686163686132302d706f6c7931333035406f70656e7373682e636f6d2c6165733132382d6374722c6165733139322d6374722c6165733235362d6374722c6165733132382d67636d406f70656e7373682e636f6d2c6165733235362d67636d406f70656e7373682e636f6d0000006c63686163686132302d706f6c7931333035406f70656e7373682e636f6d2c6165733132382d6374722c6165733139322d6374722c6165733235362d6374722c6165733132382d67636d406f70656e7373682e636f6d2c6165733235362d67636d406f70656e7373682e636f6d000000d5756d61632d36342d65746d406f70656e7373682e636f6d2c756d61632d3132382d65746d406f70656e7373682e636f6d2c686d61632d736861322d3235362d65746d406f70656e7373682e636f6d2c686d61632d736861322d3531322d65746d406f70656e7373682e636f6d2c686d61632d736861312d65746d406f70656e7373682e636f6d2c756d61632d3634406f70656e7373682e636f6d2c756d61632d313238406f70656e7373682e636f6d2c686d61632d736861322d3235362c686d61632d736861322d3531322c686d61632d73686131000000d5756d61632d36342d65746d406f70656e7373682e636f6d2c756d61632d3132382d65746d406f70656e7373682e636f6d2c686d61632d736861322d3235362d65746d406f70656e7373682e636f6d2c686d61632d736861322d3531322d65746d406f70656e7373682e636f6d2c686d61632d736861312d65746d406f70656e7373682e636f6d2c756d61632d3634406f70656e7373682e636f6d2c756d61632d313238406f70656e7373682e636f6d2c686d61632d736861322d3235362c686d61632d736861322d3531322c686d61632d736861310000001a6e6f6e652c7a6c6962406f70656e7373682e636f6d2c7a6c69620000001a6e6f6e652c7a6c6962406f70656e7373682e636f6d2c7a6c69620000000000000000000000000000000000000000"
000005fc071401af35150e67f2bc6dc4bc6b5330901900000131736e74727570373631783235353
1392d736861353132406f70656e7373682e636f6d2c637572766532353531392d7368613235362c
637572766532353531392d736861323536406c69627373682e6f72672c656364682d736861322d6
e697374703235362c656364682d736861322d6e697374703338342c656364682d736861322d6e69
7374703532312c6469666669652d68656c6c6d616e2d67726f75702d65786368616e67652d73686
13235362c6469666669652d68656c6c6d616e2d67726f757031362d7368613531322c6469666669
652d68656c6c6d616e2d67726f757031382d7368613531322c6469666669652d68656c6c6d616e2
d67726f757031342d7368613235362c6578742d696e666f2d632c6b65782d7374726963742d632d
763030406f70656e7373682e636f6d000001cf7373682d656432353531392d636572742d7630314
06f70656e7373682e636f6d2c65636473612d736861322d6e697374703235362d636572742d7630
31406f70656e7373682e636f6d2c65636473612d736861322d6e697374703338342d636572742d7
63031406f70656e7373682e636f6d2c65636473612d736861322d6e697374703532312d63657274
2d763031406f70656e7373682e636f6d2c736b2d7373682d656432353531392d636572742d76303
1406f70656e7373682e636f6d2c736b2d65636473612d736861322d6e697374703235362d636572
742d763031406f70656e7373682e636f6d2c7273612d736861322d3531322d636572742d7630314
06f70656e7373682e636f6d2c7273612d736861322d3235362d636572742d763031406f70656e73
73682e636f6d2c7373682d656432353531392c65636473612d736861322d6e697374703235362c6
5636473612d736861322d6e697374703338342c65636473612d736861322d6e697374703532312c
736b2d7373682d65643235353139406f70656e7373682e636f6d2c736b2d65636473612d7368613
22d6e69737470323536406f70656e7373682e636f6d2c7273612d736861322d3531322c7273612d
736861322d3235360000006c63686163686132302d706f6c7931333035406f70656e7373682e636
f6d2c6165733132382d6374722c6165733139322d6374722c6165733235362d6374722c61657331
32382d67636d406f70656e7373682e636f6d2c6165733235362d67636d406f70656e7373682e636
f6d0000006c63686163686132302d706f6c7931333035406f70656e7373682e636f6d2c61657331
32382d6374722c6165733139322d6374722c6165733235362d6374722c6165733132382d67636d4
06f70656e7373682e636f6d2c6165733235362d67636d406f70656e7373682e636f6d000000d575
6d61632d36342d65746d406f70656e7373682e636f6d2c756d61632d3132382d65746d406f70656
e7373682e636f6d2c686d61632d736861322d3235362d65746d406f70656e7373682e636f6d2c68
6d61632d736861322d3531322d65746d406f70656e7373682e636f6d2c686d61632d736861312d6
5746d406f70656e7373682e636f6d2c756d61632d3634406f70656e7373682e636f6d2c756d6163
2d313238406f70656e7373682e636f6d2c686d61632d736861322d3235362c686d61632d7368613
22d3531322c686d61632d73686131000000d5756d61632d36342d65746d406f70656e7373682e63
6f6d2c756d61632d3132382d65746d406f70656e7373682e636f6d2c686d61632d736861322d323
5362d65746d406f70656e7373682e636f6d2c686d61632d736861322d3531322d65746d406f7065
6e7373682e636f6d2c686d61632d736861312d65746d406f70656e7373682e636f6d2c756d61632
d3634406f70656e7373682e636f6d2c756d61632d313238406f70656e7373682e636f6d2c686d61
632d736861322d3235362c686d61632d736861322d3531322c686d61632d736861310000001a6e6
f6e652c7a6c6962406f70656e7373682e636f6d2c7a6c69620000001a6e6f6e652c7a6c6962406f
70656e7373682e636f6d2c7a6c69620000000000000000000000000000000000000000
"
), ),
server: &hex!( server: &hex!(
" "000000bc05140000000000000000000000000000000000000011637572766532353531392d7368613235360000000b7373682d656432353531390000001d63686163686132302d706f6c7931333035406f70656e7373682e636f6d0000001d63686163686132302d706f6c7931333035406f70656e7373682e636f6d0000000d686d61632d736861322d3235360000000d686d61632d736861322d323536000000046e6f6e65000000046e6f6e65000000000000000000000000000000000000"
000000c40d140000000000000000000000000000000000000011637572766532353531392d73686
13235360000000b7373682d656432353531390000001d63686163686132302d706f6c7931333035
406f70656e7373682e636f6d0000001d63686163686132302d706f6c7931333035406f70656e737
3682e636f6d0000000d686d61632d736861322d3235360000000d686d61632d736861322d323536
000000046e6f6e65000000046e6f6e6500000000000000000000000000000000000000000000000
00000
"
), ),
}, },
// ECDH KEX Init // ECDH KEX Init
Part { Part {
client: &hex!( client: &hex!(
" "0000002c061e000000203c37b81a887449b168cd9128d8b8bf034f17ac6374f814fca2f4583ec60b9b05000000000000"
0000002c061e0000002086ac62fd02ac3333e2470f6024d0027696b29056f281f6fde0c05956fcf
d3a53000000000000
"
), ),
server: &hex!( server: &hex!(
" "000000bc081f000000330000000b7373682d6564323535313900000020e939cdfa6fc0d737333b534e913dd332c8d5179fe00c3045575217224b19b8f6000000203b92eb7008cc13056bc9f198049f75d5832f3650969dfcccd80841431b350160000000530000000b7373682d6564323535313900000040c9ae31b043d2a964265ffa7672e99a136053cc29fa17a0e432a62c742bb187aee16527e299b601593ebf5cb255d39f2edbafc32236c17adbfcf6f01527827b060000000000000000"
000000bc081f000000330000000b7373682d6564323535313900000020e939cdfa6fc0d737333b5
34e913dd332c8d5179fe00c3045575217224b19b8f6000000203b92eb7008cc13056bc9f198049f
75d5832f3650969dfcccd80841431b350160000000530000000b7373682d6564323535313900000
04096ba808246f3b76270475d495330bfe174043609e81be35eadcabc0537ddcf8c4502831e9fef
f2ef0e49cbe93e1747c01e2c9a6d19839648694defeb2adc77060000000000000000
"
), ),
}, },
// New Keys // New Keys
@ -508,13 +452,24 @@ mod tests {
client: &hex!("0000000c0a1500000000000000000000"), client: &hex!("0000000c0a1500000000000000000000"),
server: &hex!("0000000c0a1500000000000000000000"), server: &hex!("0000000c0a1500000000000000000000"),
}, },
// Service Request (encrypted)
Part {
client: &hex!("c514026ef814ab7e1d5854df6af106eda203e10935ab887151e16d85024713c5e1b51435072e599eab5662e0"),
server: &hex!("76eecb34af5ba93308499b41fc3c9bfc7dad89208fb26b0ae04baaed4515a788c45f81930eabc45f0f42c142"),
},
]; ];
let mut con = ServerConnection::new(HardcodedRng(rng)); let mut con = ServerConnection::new(HardcodedRng(rng));
for part in conversation { for part in conversation {
con.recv_bytes(&part.client).unwrap(); con.recv_bytes(&part.client).unwrap();
eprintln!("client: {:x?}", part.client);
let bytes = con.next_msg_to_send().unwrap().to_bytes(); let bytes = con.next_msg_to_send().unwrap().to_bytes();
assert_eq!(part.server, bytes); if part.server != bytes {
panic!(
"expected != found\nexpected: {:x?}\nfound: {:x?}",
part.server, bytes
);
}
} }
} }
} }

View file

@ -32,7 +32,7 @@ impl Msg {
match self.0 { match self.0 {
MsgKind::ServerProtocolInfo => crate::SERVER_IDENTIFICATION.to_vec(), MsgKind::ServerProtocolInfo => crate::SERVER_IDENTIFICATION.to_vec(),
MsgKind::PlaintextPacket(v) => v.to_bytes(true), MsgKind::PlaintextPacket(v) => v.to_bytes(true),
MsgKind::EncryptedPacket(v) => v.to_bytes(), MsgKind::EncryptedPacket(v) => v.into_bytes(),
} }
} }
} }
@ -135,16 +135,16 @@ impl Packet {
pub(crate) const SSH_MSG_KEXDH_REPLY: u8 = 31; pub(crate) const SSH_MSG_KEXDH_REPLY: u8 = 31;
pub(crate) fn from_raw(bytes: &[u8]) -> Result<Self> { pub(crate) fn from_raw(bytes: &[u8]) -> Result<Self> {
let Some(padding_length) = bytes.get(0) else { let Some(padding_length) = bytes.first() else {
return Err(client_error!("empty packet")); return Err(client_error!("empty packet"));
}; };
// TODO: mac?
let Some(payload_len) = (bytes.len() - 1).checked_sub(*padding_length as usize) else { let Some(payload_len) = (bytes.len() - 1).checked_sub(*padding_length as usize) else {
return Err(client_error!("packet padding longer than packet")); return Err(client_error!("packet padding longer than packet"));
}; };
let payload = &bytes[1..][..payload_len]; let payload = &bytes[1..][..payload_len];
// TODO: this fails with OpenSSH client... why? // TODO: handle the annoying decryption special case differnt where its +0 instead of +4
//if (bytes.len() + 4) % 8 != 0 { //if (bytes.len() + 4) % 8 != 0 {
// return Err(client_error!("full packet length must be multiple of 8: {}", bytes.len())); // return Err(client_error!("full packet length must be multiple of 8: {}", bytes.len()));
//} //}
@ -188,7 +188,7 @@ pub(crate) struct EncryptedPacket {
data: Vec<u8>, data: Vec<u8>,
} }
impl EncryptedPacket { impl EncryptedPacket {
pub(crate) fn to_bytes(self) -> Vec<u8> { pub(crate) fn into_bytes(self) -> Vec<u8> {
self.data self.data
} }
pub(crate) fn from_encrypted_full_bytes(data: Vec<u8>) -> Self { pub(crate) fn from_encrypted_full_bytes(data: Vec<u8>) -> Self {
@ -307,8 +307,8 @@ impl SshPublicKey<'_> {
data.u32((4 + self.format.len() + 4 + self.data.len()) as u32); data.u32((4 + self.format.len() + 4 + self.data.len()) as u32);
// ed25519-specific! // ed25519-specific!
// <https://datatracker.ietf.org/doc/html/rfc8709#section-4> // <https://datatracker.ietf.org/doc/html/rfc8709#section-4>
data.string(&self.format); data.string(self.format);
data.string(&self.data); data.string(self.data);
data.finish() data.finish()
} }
} }
@ -334,14 +334,13 @@ impl<'a> DhKeyExchangeInitReplyPacket<'a> {
data.u32((4 + self.signature.format.len() + 4 + self.signature.data.len()) as u32); data.u32((4 + self.signature.format.len() + 4 + self.signature.data.len()) as u32);
// <https://datatracker.ietf.org/doc/html/rfc8709#section-6> // <https://datatracker.ietf.org/doc/html/rfc8709#section-6>
data.string(&self.signature.format); data.string(self.signature.format);
data.string(&self.signature.data); data.string(self.signature.data);
data.finish() data.finish()
} }
} }
pub(crate) struct RawPacket { pub(crate) struct RawPacket {
pub len: usize,
pub mac_len: usize, pub mac_len: usize,
pub raw: Vec<u8>, pub raw: Vec<u8>,
} }
@ -434,7 +433,6 @@ impl PacketParser {
RawPacket { RawPacket {
raw: std::mem::take(&mut self.raw_data), raw: std::mem::take(&mut self.raw_data),
mac_len: keys.additional_mac_len(), mac_len: keys.additional_mac_len(),
len: packet_length,
}, },
))) )))
} else { } else {

View file

@ -44,7 +44,7 @@ impl<'a> Parser<'a> {
match b { match b {
0 => Ok(false), 0 => Ok(false),
1 => Ok(true), 1 => Ok(true),
_ => return Err(crate::client_error!("invalid bool: {b}")), _ => Err(crate::client_error!("invalid bool: {b}")),
} }
} }
@ -141,7 +141,7 @@ impl Debug for NameList<'_> {
pub struct MpInt<'a>(pub(crate) &'a [u8]); pub struct MpInt<'a>(pub(crate) &'a [u8]);
impl<'a> MpInt<'a> { impl<'a> MpInt<'a> {
pub(crate) fn to_x25519_public_key(&self) -> Result<x25519_dalek::PublicKey> { pub(crate) fn as_x25519_public_key(&self) -> Result<x25519_dalek::PublicKey> {
let Ok(arr) = <[u8; 32]>::try_from(self.0) else { let Ok(arr) = <[u8; 32]>::try_from(self.0) else {
return Err(crate::client_error!( return Err(crate::client_error!(
"invalid x25519 public key length, should be 32, was: {}", "invalid x25519 public key length, should be 32, was: {}",