| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
#![expect(clippy::unwrap_used, reason = "OK for tests.")] |
| 8 |
|
| 9 |
use std::{mem, time::Instant}; |
| 10 |
|
| 11 |
use neqo_common::qinfo; |
| 12 |
use neqo_crypto::{ |
| 13 |
AntiReplay, AuthenticationStatus, Client, HandshakeState, RecordList, Res, ResumptionToken, |
| 14 |
SecretAgent, Server, ZeroRttCheckResult, ZeroRttChecker, |
| 15 |
}; |
| 16 |
use test_fixture::{anti_replay, fixture_init, now}; |
| 17 |
|
| 18 |
/// Consume records until the handshake state changes. |
| 19 |
#[allow( |
| 20 |
clippy::allow_attributes, |
| 21 |
clippy::missing_panics_doc, |
| 22 |
reason = "OK for tests." |
| 23 |
)] |
| 24 |
#[allow( |
| 25 |
clippy::allow_attributes, |
| 26 |
clippy::missing_errors_doc, |
| 27 |
reason = "OK for tests." |
| 28 |
)] |
| 29 |
pub fn forward_records( |
| 30 |
now: Instant, |
| 31 |
agent: &mut SecretAgent, |
| 32 |
records_in: RecordList, |
| 33 |
) -> Res<RecordList> { |
| 34 |
let mut expected_state = match agent.state() { |
| 35 |
HandshakeState::New => HandshakeState::New, |
| 36 |
_ => HandshakeState::InProgress, |
| 37 |
}; |
| 38 |
let mut records_out = RecordList::default(); |
| 39 |
for record in records_in { |
| 40 |
assert_eq!(records_out.len(), 0); |
| 41 |
assert_eq!(*agent.state(), expected_state); |
| 42 |
|
| 43 |
records_out = agent.handshake_raw(now, Some(record))?; |
| 44 |
expected_state = HandshakeState::InProgress; |
| 45 |
} |
| 46 |
Ok(records_out) |
| 47 |
} |
| 48 |
|
| 49 |
fn handshake(now: Instant, client: &mut SecretAgent, server: &mut SecretAgent) { |
| 50 |
let mut a = client; |
| 51 |
let mut b = server; |
| 52 |
let mut records = a.handshake_raw(now, None).unwrap(); |
| 53 |
let is_done = |agent: &mut SecretAgent| agent.state().is_final(); |
| 54 |
while !is_done(b) { |
| 55 |
records = if let Ok(r) = forward_records(now, b, records) { |
| 56 |
r |
| 57 |
} else { |
| 58 |
// TODO(mt) take the alert generated by the failed handshake |
| 59 |
// and allow it to be sent to the peer. |
| 60 |
return; |
| 61 |
}; |
| 62 |
|
| 63 |
if *b.state() == HandshakeState::AuthenticationPending { |
| 64 |
b.authenticated(AuthenticationStatus::Ok); |
| 65 |
records = if let Ok(r) = b.handshake_raw(now, None) { |
| 66 |
r |
| 67 |
} else { |
| 68 |
// TODO(mt) - as above. |
| 69 |
return; |
| 70 |
} |
| 71 |
} |
| 72 |
mem::swap(&mut a, &mut b); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
#[allow( |
| 77 |
clippy::allow_attributes, |
| 78 |
clippy::missing_panics_doc, |
| 79 |
reason = "OK for tests." |
| 80 |
)] |
| 81 |
pub fn connect_at(now: Instant, client: &mut SecretAgent, server: &mut SecretAgent) { |
| 82 |
handshake(now, client, server); |
| 83 |
qinfo!("client: {:?}", client.state()); |
| 84 |
qinfo!("server: {:?}", server.state()); |
| 85 |
assert!(client.state().is_connected()); |
| 86 |
assert!(server.state().is_connected()); |
| 87 |
} |
| 88 |
|
| 89 |
pub fn connect(client: &mut SecretAgent, server: &mut SecretAgent) { |
| 90 |
connect_at(now(), client, server); |
| 91 |
} |
| 92 |
|
| 93 |
#[allow( |
| 94 |
clippy::allow_attributes, |
| 95 |
clippy::missing_panics_doc, |
| 96 |
dead_code, |
| 97 |
reason = "OK for tests." |
| 98 |
)] |
| 99 |
pub fn connect_fail(client: &mut SecretAgent, server: &mut SecretAgent) { |
| 100 |
handshake(now(), client, server); |
| 101 |
assert!(!client.state().is_connected()); |
| 102 |
assert!(!server.state().is_connected()); |
| 103 |
} |
| 104 |
|
| 105 |
#[allow( |
| 106 |
clippy::allow_attributes, |
| 107 |
dead_code, |
| 108 |
reason = "Yes, we currently don't construct WithoutZeroRtt and WithZeroRtt." |
| 109 |
)] |
| 110 |
#[derive(Clone, Copy, Debug)] |
| 111 |
pub enum Resumption { |
| 112 |
WithoutZeroRtt, |
| 113 |
WithZeroRtt, |
| 114 |
} |
| 115 |
|
| 116 |
pub const ZERO_RTT_TOKEN_DATA: &[u8] = b"zero-rtt-token"; |
| 117 |
|
| 118 |
#[derive(Debug)] |
| 119 |
pub struct PermissiveZeroRttChecker { |
| 120 |
resuming: bool, |
| 121 |
} |
| 122 |
|
| 123 |
impl Default for PermissiveZeroRttChecker { |
| 124 |
fn default() -> Self { |
| 125 |
Self { resuming: true } |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
impl ZeroRttChecker for PermissiveZeroRttChecker { |
| 130 |
fn check(&self, token: &[u8]) -> ZeroRttCheckResult { |
| 131 |
if self.resuming { |
| 132 |
assert_eq!(ZERO_RTT_TOKEN_DATA, token); |
| 133 |
} else { |
| 134 |
assert!(token.is_empty()); |
| 135 |
} |
| 136 |
ZeroRttCheckResult::Accept |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
#[allow(clippy::allow_attributes, dead_code, reason = "False positive.")] |
| 141 |
fn zero_rtt_setup(mode: Resumption, client: &Client, server: &mut Server) -> Option<AntiReplay> { |
| 142 |
matches!(mode, Resumption::WithZeroRtt).then(|| { |
| 143 |
client.enable_0rtt().expect("should enable 0-RTT on client"); |
| 144 |
|
| 145 |
let anti_replay = anti_replay(); |
| 146 |
server |
| 147 |
.enable_0rtt( |
| 148 |
&anti_replay, |
| 149 |
0xffff_ffff, |
| 150 |
Box::new(PermissiveZeroRttChecker { resuming: false }), |
| 151 |
) |
| 152 |
.expect("should enable 0-RTT on server"); |
| 153 |
anti_replay |
| 154 |
}) |
| 155 |
} |
| 156 |
|
| 157 |
#[allow( |
| 158 |
clippy::allow_attributes, |
| 159 |
clippy::missing_panics_doc, |
| 160 |
dead_code, |
| 161 |
reason = "OK for tests." |
| 162 |
)] |
| 163 |
#[must_use] |
| 164 |
pub fn resumption_setup(mode: Resumption) -> (Option<AntiReplay>, ResumptionToken) { |
| 165 |
fixture_init(); |
| 166 |
|
| 167 |
let mut client = Client::new("server.example", true).expect("should create client"); |
| 168 |
let mut server = Server::new(&["key"]).expect("should create server"); |
| 169 |
let anti_replay = zero_rtt_setup(mode, &client, &mut server); |
| 170 |
|
| 171 |
connect(&mut client, &mut server); |
| 172 |
|
| 173 |
assert!(!client.info().unwrap().resumed()); |
| 174 |
assert!(!server.info().unwrap().resumed()); |
| 175 |
assert!(!client.info().unwrap().early_data_accepted()); |
| 176 |
assert!(!server.info().unwrap().early_data_accepted()); |
| 177 |
|
| 178 |
let server_records = server |
| 179 |
.send_ticket(now(), ZERO_RTT_TOKEN_DATA) |
| 180 |
.expect("ticket sent"); |
| 181 |
assert_eq!(server_records.len(), 1); |
| 182 |
let client_records = client |
| 183 |
.handshake_raw(now(), server_records.into_iter().next()) |
| 184 |
.expect("records ingested"); |
| 185 |
assert_eq!(client_records.len(), 0); |
| 186 |
|
| 187 |
// `client` is about to go out of scope, |
| 188 |
// but we only need to keep the resumption token, so clone it. |
| 189 |
let token = client.resumption_token().expect("token is present"); |
| 190 |
(anti_replay, token) |
| 191 |
} |