Skip to content

Commit

Permalink
Remove the guard crate
Browse files Browse the repository at this point in the history
  • Loading branch information
honzasp committed Jun 4, 2023
1 parent 9c1de06 commit 9a710c6
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
14 changes: 10 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# Makiko changelog

## 0.2.1
## Unreleased

- Deprecate `Pubkey::algos_secure()` and `Pubkey::algos_compatible_less_secure()`, replace with
`Pubkey::algos()`
- Replace the `guard` crate with (now stabilized) `let else` expressions
supported directly by the compiler. Fixes compatibility with Rust 1.70.0.

## 0.2.0
## 0.2.1 (2023-02-09)

- Deprecated `Pubkey::algos_secure()` and
`Pubkey::algos_compatible_less_secure()`, replace with
`Pubkey::algos()`

## 0.2.0 (2022-10-01)

The first generally usable version.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ ecdsa = {version = "0.15", features = ["signing", "verifying"]}
ed25519 = {version = "1.5", features = ["pkcs8"]}
ed25519-dalek = "1.0"
futures-core = "0.3"
guard = "0.5"
hex-literal = "0.3"
hmac = "0.12"
log = "0.4"
Expand Down
9 changes: 4 additions & 5 deletions src/client/channel_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use bytes::Bytes;
use futures_core::ready;
use guard::guard;
use parking_lot::Mutex;
use std::cmp::min;
use std::collections::VecDeque;
Expand Down Expand Up @@ -182,18 +181,18 @@ fn send_channel_request(st: &mut ClientState, channel_st: &ChannelState, req: &C
}

pub(super) fn recv_channel_success(channel_st: &mut ChannelState) -> ResultRecvState {
guard!{let Some(reply) = channel_st.recv_replies.pop_front() else {
let Some(reply) = channel_st.recv_replies.pop_front() else {
return Err(Error::Protocol("received SSH_MSG_CHANNEL_SUCCESS, but no reply was expected"))
}};
};
log::debug!("received SSH_MSG_CHANNEL_SUCCESS for our channel {}", channel_st.our_id);
let _: Result<_, _> = reply.reply_tx.send(ChannelReply::Success);
Ok(None)
}

pub(super) fn recv_channel_failure(channel_st: &mut ChannelState) -> ResultRecvState {
guard!{let Some(reply) = channel_st.recv_replies.pop_front() else {
let Some(reply) = channel_st.recv_replies.pop_front() else {
return Err(Error::Protocol("received SSH_MSG_CHANNEL_FAILURE, but no reply was expected"))
}};
};
log::debug!("received SSH_MSG_CHANNEL_FAILURE for our channel {}", channel_st.our_id);
let _: Result<_, _> = reply.reply_tx.send(ChannelReply::Failure);
Ok(None)
Expand Down
41 changes: 20 additions & 21 deletions src/client/conn.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bytes::Bytes;
use guard::guard;
use parking_lot::Mutex;
use std::collections::{HashMap, VecDeque};
use std::future::Future as _;
Expand Down Expand Up @@ -248,18 +247,18 @@ fn recv_channel_open_confirmation(
window {}, max packet size {}", our_id, send_window, send_packet_len_max);

let mut channels = st.conn_st.channels.lock();
guard!{let Some(conn_channel_st) = channels.get_mut(&our_id) else {
let Some(conn_channel_st) = channels.get_mut(&our_id) else {
return Err(Error::Protocol("received SSH_MSG_CHANNEL_OPEN_CONFIRMATION for unknown channel"));
}};
};

guard!{let ConnChannelState::Open(_) = conn_channel_st else {
let ConnChannelState::Open(_) = conn_channel_st else {
return Err(Error::Protocol("received SSH_MSG_CHANNEL_OPEN_CONFIRMATION \
for a channel that is not being opened"));
}};
};
// use `replace()` only after we are sure that `*conn_channel_st` is `Open`
guard!{let ConnChannelState::Open(open_st) = replace(conn_channel_st, ConnChannelState::Closed) else {
let ConnChannelState::Open(open_st) = replace(conn_channel_st, ConnChannelState::Closed) else {
unreachable!()
}};
};

let confirm = ConfirmChannel { their_id, send_window, send_packet_len_max, confirm_payload };
*conn_channel_st = init_confirmed_channel(open_st, confirm);
Expand Down Expand Up @@ -303,17 +302,17 @@ fn recv_channel_open_failure(
let description_lang = payload.get_string()?;

let mut channels = st.conn_st.channels.lock();
guard!{let Some(conn_channel_st) = channels.get_mut(&our_id) else {
let Some(conn_channel_st) = channels.get_mut(&our_id) else {
return Err(Error::Protocol("received SSH_MSG_CHANNEL_OPEN_FAILURE for unknown channel"));
}};
guard!{let ConnChannelState::Open(_) = conn_channel_st else {
};
let ConnChannelState::Open(_) = conn_channel_st else {
return Err(Error::Protocol("received SSH_MSG_CHANNEL_OPEN_FAILURE \
for a channel that is not being opened"));
}};
};
// use `replace()` only after we are sure that `*conn_channel_st` is `Open`
guard!{let ConnChannelState::Open(open_st) = replace(conn_channel_st, ConnChannelState::Closed) else {
let ConnChannelState::Open(open_st) = replace(conn_channel_st, ConnChannelState::Closed) else {
unreachable!()
}};
};

log::debug!("received SSH_MSG_CHANNEL_OPEN_FAILURE for our channel {}", our_id);

Expand Down Expand Up @@ -406,12 +405,12 @@ fn recv_channel_packet<F>(

let channels = st.conn_st.channels.clone();
let mut channels = channels.lock();
guard!{let Some(conn_channel_st) = channels.get_mut(&our_id) else {
let Some(conn_channel_st) = channels.get_mut(&our_id) else {
return Err(Error::Protocol(unknown_err));
}};
guard!{let ConnChannelState::Ready(channel_st) = conn_channel_st else {
};
let ConnChannelState::Ready(channel_st) = conn_channel_st else {
return Err(Error::Protocol(not_ready_err));
}};
};

callback(st, channel_st, payload)
}
Expand Down Expand Up @@ -532,19 +531,19 @@ fn send_global_request(st: &mut ClientState, req: &GlobalReq) {
}

fn recv_request_success(st: &mut ClientState, payload: &mut PacketDecode) -> ResultRecvState {
guard!{let Some(reply) = st.conn_st.recv_replies.pop_front() else {
let Some(reply) = st.conn_st.recv_replies.pop_front() else {
return Err(Error::Protocol("received SSH_MSG_REQUEST_SUCCESS, but no reply was expected"))
}};
};
log::debug!("received SSH_MSG_REQUEST_SUCCESS");
let payload = payload.remaining();
let _: Result<_, _> = reply.reply_tx.send(GlobalReply::Success(payload));
Ok(None)
}

fn recv_request_failure(st: &mut ClientState) -> ResultRecvState {
guard!{let Some(reply) = st.conn_st.recv_replies.pop_front() else {
let Some(reply) = st.conn_st.recv_replies.pop_front() else {
return Err(Error::Protocol("received SSH_MSG_REQUEST_FAILURE, but no reply was expected"))
}};
};
log::debug!("received SSH_MSG_REQUEST_FAILURE");
let _: Result<_, _> = reply.reply_tx.send(GlobalReply::Failure);
Ok(None)
Expand Down
9 changes: 4 additions & 5 deletions src/host_file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Support for OpenSSH-compatible `known_hosts` file.

use guard::guard;
use base64::Engine as _;
use bytes::{Bytes, BytesMut};
use hmac::Mac as _;
Expand Down Expand Up @@ -373,9 +372,9 @@ impl EntryBuilder {
fn pattern_matches(pattern: &Pattern, hostname: &str) -> bool {
match pattern {
Pattern::Hashed(pattern) => {
guard!{let Ok(mut hmac) = hmac::Hmac::<sha1::Sha1>::new_from_slice(&pattern.salt) else {
let Ok(mut hmac) = hmac::Hmac::<sha1::Sha1>::new_from_slice(&pattern.salt) else {
return false;
}};
};
hmac.update(hostname.as_bytes());
hmac.verify_slice(&pattern.hash).is_ok()
},
Expand Down Expand Up @@ -417,9 +416,9 @@ fn decode_file(data: Bytes) -> File {

fn decode_line(mut bytes: &[u8], line_i: usize) -> Result<LineContent, &'static str> {
// empty lines are treated as comments
guard!{let Some(first_field) = read_field(&mut bytes) else {
let Some(first_field) = read_field(&mut bytes) else {
return Ok(LineContent::Comment)
}};
};

// first comes the optional marker preceded with '@'
let (pattern_field, marker) = if first_field[0] == b'@' {
Expand Down
5 changes: 2 additions & 3 deletions src/pubkey/ed25519.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bytes::Bytes;
use guard::guard;
use crate::codec::{PacketDecode, PacketEncode};
use crate::error::{Result, Error};
use std::fmt;
Expand Down Expand Up @@ -40,7 +39,7 @@ impl Ed25519Privkey {
}

fn verify(pubkey: &Pubkey, message: &[u8], signature: Bytes) -> Result<SignatureVerified> {
guard!{let Pubkey::Ed25519(pubkey) = pubkey else { return Err(Error::PubkeyFormat) }};
let Pubkey::Ed25519(pubkey) = pubkey else { return Err(Error::PubkeyFormat) };

let mut signature = PacketDecode::new(signature);
if signature.get_string()? != "ssh-ed25519" {
Expand All @@ -57,7 +56,7 @@ fn verify(pubkey: &Pubkey, message: &[u8], signature: Bytes) -> Result<Signature
}

fn sign(privkey: &Privkey, message: &[u8]) -> Result<Bytes> {
guard!{let Privkey::Ed25519(privkey) = privkey else { return Err(Error::PrivkeyFormat) }};
let Privkey::Ed25519(privkey) = privkey else { return Err(Error::PrivkeyFormat) };

use ed25519_dalek::Signer as _;
let ed_signature = privkey.keypair.try_sign(message)
Expand Down
5 changes: 2 additions & 3 deletions src/pubkey/rsa.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bytes::Bytes;
use guard::guard;
use rsa::{PublicKey as _, PublicKeyParts as _, pkcs8};
use sha1::digest;
use std::fmt;
Expand Down Expand Up @@ -62,7 +61,7 @@ impl RsaPrivkey {
}

fn verify<H: RsaHash>(pubkey: &Pubkey, message: &[u8], signature_blob: Bytes) -> Result<SignatureVerified> {
guard!{let Pubkey::Rsa(pubkey) = pubkey else { return Err(Error::PubkeyFormat) }};
let Pubkey::Rsa(pubkey) = pubkey else { return Err(Error::PubkeyFormat) };

let mut signature_blob = PacketDecode::new(signature_blob);
if signature_blob.get_string()? != H::ALGO_NAME {
Expand All @@ -83,7 +82,7 @@ fn verify<H: RsaHash>(pubkey: &Pubkey, message: &[u8], signature_blob: Bytes) ->
}

fn sign<H: RsaHash>(privkey: &Privkey, message: &[u8]) -> Result<Bytes> {
guard!{let Privkey::Rsa(privkey) = privkey else { return Err(Error::PrivkeyFormat) }};
let Privkey::Rsa(privkey) = privkey else { return Err(Error::PrivkeyFormat) };

let mut hasher = H::new();
hasher.update(message);
Expand Down

0 comments on commit 9a710c6

Please sign in to comment.