use self::{cookie::Cookie, csn::CombinedSequenceSnapshot, messages::*};
use super::*;
#[test]
fn first_message_wrong_destination() {
let ks = KeyPair::new();
let mut s = InitiatorSignaling::new(ks, Tasks(vec![]), None, None, None);
let msg = ServerHello::random().into_message();
let cs = CombinedSequenceSnapshot::random();
let nonce = Nonce::new(Cookie::random(), Address(0), Address(1), cs);
let obox = OpenBox::<Message>::new(msg, nonce);
let bbox = obox.encode();
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
assert_eq!(
s.handle_message(bbox),
Err(SignalingError::InvalidNonce(
"Bad destination: 0x01 (our identity is unknown)".into()
))
);
}
#[test]
fn wrong_source_initiator() {
let ks = KeyPair::new();
let mut s = InitiatorSignaling::new(ks, Tasks(vec![]), None, None, None);
let make_msg = |src: u8, dest: u8| {
let msg = ServerHello::random().into_message();
let cs = CombinedSequenceSnapshot::random();
let nonce = Nonce::new(Cookie::random(), Address(src), Address(dest), cs);
let obox = OpenBox::<Message>::new(msg, nonce);
let bbox = obox.encode();
bbox
};
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
let actions = s.handle_message(make_msg(0x01, 0x00)).unwrap();
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
assert_eq!(actions, vec![]);
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
let actions = s.handle_message(make_msg(0xff, 0x00)).unwrap();
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
assert_eq!(actions, vec![]);
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
let actions = s.handle_message(make_msg(0x00, 0x00)).unwrap();
assert_eq!(
s.server().handshake_state(),
ServerHandshakeState::ClientInfoSent
);
assert_eq!(actions.len(), 1);
}
#[test]
fn wrong_source_responder() {
let ks = KeyPair::new();
let initiator_pubkey = PublicKey::from([0u8; 32]);
let mut s = ResponderSignaling::new(ks, initiator_pubkey, None, None, Tasks(vec![]), None);
let make_msg = |src: u8, dest: u8| {
let msg = ServerHello::random().into_message();
let cs = CombinedSequenceSnapshot::random();
let nonce = Nonce::new(Cookie::random(), Address(src), Address(dest), cs);
let obox = OpenBox::<Message>::new(msg, nonce);
let bbox = obox.encode();
bbox
};
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
let actions = s
.handle_message(make_msg(0x03, 0x00))
.expect("handle_message 1");
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
assert_eq!(actions, vec![]);
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
let actions = s
.handle_message(make_msg(0x01, 0x00))
.expect("handle_message 2");
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
assert_eq!(actions, vec![]);
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
let actions = s
.handle_message(make_msg(0x00, 0x00))
.expect("handle_message 3");
assert_eq!(
s.server().handshake_state(),
ServerHandshakeState::ClientInfoSent
);
assert_eq!(actions.len(), 2);
}
#[test]
fn first_message_bad_overflow_number() {
let ks = KeyPair::new();
let mut s = InitiatorSignaling::new(ks, Tasks(vec![]), None, None, None);
let msg = ServerHello::random().into_message();
let cs = CombinedSequenceSnapshot::new(1, 1234);
let nonce = Nonce::new(Cookie::random(), Address(0), Address(0), cs);
let obox = OpenBox::<Message>::new(msg, nonce);
let bbox = obox.encode();
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
assert_eq!(
s.handle_message(bbox),
Err(SignalingError::InvalidNonce(
"First message from server must have set the overflow number to 0".into()
))
);
}
fn _test_sequence_number(
first: CombinedSequenceSnapshot,
second: CombinedSequenceSnapshot,
) -> SignalingResult<Vec<HandleAction>> {
let ks = KeyPair::new();
let mut s = InitiatorSignaling::new(ks, Tasks(vec![]), None, None, None);
let msg = ServerHello::random().into_message();
let nonce = Nonce::new(Cookie::random(), Address(0), Address(0), first);
let obox = OpenBox::<Message>::new(msg, nonce);
let bbox = obox.encode();
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
let actions = s.handle_message(bbox);
assert!(actions.is_ok());
let msg = ServerAuth::for_initiator(s.server().cookie_pair().ours.clone(), None, vec![])
.into_message();
let nonce = Nonce::new(Cookie::random(), Address(0), Address(0), second);
let obox = OpenBox::<Message>::new(msg, nonce);
let bbox = obox.encode();
assert_eq!(
s.server().handshake_state(),
ServerHandshakeState::ClientInfoSent
);
s.handle_message(bbox)
}
#[test]
fn sequence_number_not_incremented() {
let err = _test_sequence_number(
CombinedSequenceSnapshot::new(0, 1234),
CombinedSequenceSnapshot::new(0, 1234),
)
.unwrap_err();
assert_eq!(
err,
SignalingError::InvalidNonce("The server CSN hasn't been incremented".into())
);
}
#[test]
fn sequence_number_decremented() {
let err = _test_sequence_number(
CombinedSequenceSnapshot::new(0, 1234),
CombinedSequenceSnapshot::new(0, 1233),
)
.unwrap_err();
assert_eq!(
err,
SignalingError::InvalidNonce("The server CSN is lower than last time".into())
);
}
#[test]
fn sequence_number_reset() {
let err = _test_sequence_number(
CombinedSequenceSnapshot::new(0, 1234),
CombinedSequenceSnapshot::new(0, 0),
)
.unwrap_err();
assert_eq!(
err,
SignalingError::InvalidNonce("The server CSN is lower than last time".into())
);
}
#[test]
fn cookie_differs_from_own() {
let ks = KeyPair::new();
let mut s = InitiatorSignaling::new(ks, Tasks(vec![]), None, None, None);
let msg = ServerHello::random().into_message();
let cookie = s.server().cookie_pair.ours.clone();
let nonce = Nonce::new(
cookie,
Address(0),
Address(0),
CombinedSequenceSnapshot::random(),
);
let obox = OpenBox::<Message>::new(msg, nonce);
let bbox = obox.encode();
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
assert_eq!(
s.handle_message(bbox),
Err(SignalingError::InvalidNonce(
"Cookie from server is identical to our own cookie".into()
))
);
}
#[test]
fn cookie_did_not_change() {
let ks = KeyPair::new();
let mut s = InitiatorSignaling::new(ks, Tasks(vec![]), None, None, None);
let msg = ServerHello::random().into_message();
let nonce = Nonce::new(
Cookie::random(),
Address(0),
Address(0),
CombinedSequenceSnapshot::new(0, 123),
);
let bbox = OpenBox::<Message>::new(msg, nonce).encode();
assert_eq!(s.server().handshake_state(), ServerHandshakeState::New);
assert!(s.handle_message(bbox).is_ok());
assert_eq!(
s.server().handshake_state(),
ServerHandshakeState::ClientInfoSent
);
let msg =
ServerAuth::for_initiator(s.server().cookie_pair.ours.clone(), None, vec![]).into_message();
let nonce = Nonce::new(
Cookie::random(),
Address(0),
Address(1),
CombinedSequenceSnapshot::new(0, 124),
);
let bbox = OpenBox::<Message>::new(msg, nonce)
.encrypt(
&s.common().permanent_keypair,
s.server().session_key.as_ref().unwrap(),
)
.unwrap();
assert_eq!(
s.handle_message(bbox),
Err(SignalingError::InvalidNonce(
"Cookie from server has changed".into()
)),
);
}