| 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 |
use std::ops::Deref; |
| 8 |
|
| 9 |
use neqo_common::{Decoder, Encoder}; |
| 10 |
use neqo_crypto::{ZeroRttCheckResult, ZeroRttChecker}; |
| 11 |
|
| 12 |
use crate::{Error, Http3Parameters, Res}; |
| 13 |
|
| 14 |
type SettingsType = u64; |
| 15 |
|
| 16 |
/// Increment this version number if a new setting is added and that might |
| 17 |
/// cause 0-RTT to be accepted where shouldn't be. |
| 18 |
const SETTINGS_ZERO_RTT_VERSION: u64 = 1; |
| 19 |
|
| 20 |
const SETTINGS_MAX_HEADER_LIST_SIZE: SettingsType = 0x6; |
| 21 |
const SETTINGS_QPACK_MAX_TABLE_CAPACITY: SettingsType = 0x1; |
| 22 |
const SETTINGS_QPACK_BLOCKED_STREAMS: SettingsType = 0x7; |
| 23 |
const SETTINGS_ENABLE_WEB_TRANSPORT: SettingsType = 0x2b60_3742; |
| 24 |
// draft-ietf-masque-h3-datagram-04. |
| 25 |
// We also use this old value because the current web-platform test only supports |
| 26 |
// this value. |
| 27 |
const SETTINGS_H3_DATAGRAM_DRAFT04: SettingsType = 0x00ff_d277; |
| 28 |
|
| 29 |
const SETTINGS_H3_DATAGRAM: SettingsType = 0x33; |
| 30 |
|
| 31 |
pub const H3_RESERVED_SETTINGS: &[SettingsType] = &[0x2, 0x3, 0x4, 0x5]; |
| 32 |
|
| 33 |
#[derive(Clone, PartialEq, Eq, Debug, Copy)] |
| 34 |
pub enum HSettingType { |
| 35 |
MaxHeaderListSize, |
| 36 |
MaxTableCapacity, |
| 37 |
BlockedStreams, |
| 38 |
EnableWebTransport, |
| 39 |
EnableH3Datagram, |
| 40 |
} |
| 41 |
|
| 42 |
const fn hsetting_default(setting_type: HSettingType) -> u64 { |
| 43 |
match setting_type { |
| 44 |
HSettingType::MaxHeaderListSize => 1 << 62, |
| 45 |
HSettingType::MaxTableCapacity |
| 46 |
| HSettingType::BlockedStreams |
| 47 |
| HSettingType::EnableWebTransport |
| 48 |
| HSettingType::EnableH3Datagram => 0, |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 53 |
pub struct HSetting { |
| 54 |
pub setting_type: HSettingType, |
| 55 |
pub value: u64, |
| 56 |
} |
| 57 |
|
| 58 |
impl HSetting { |
| 59 |
#[must_use] |
| 60 |
pub const fn new(setting_type: HSettingType, value: u64) -> Self { |
| 61 |
Self { |
| 62 |
setting_type, |
| 63 |
value, |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
#[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 69 |
pub struct HSettings { |
| 70 |
settings: Vec<HSetting>, |
| 71 |
} |
| 72 |
|
| 73 |
impl HSettings { |
| 74 |
#[must_use] |
| 75 |
pub fn new(settings: &[HSetting]) -> Self { |
| 76 |
Self { |
| 77 |
settings: settings.to_vec(), |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
#[must_use] |
| 82 |
pub fn get(&self, setting: HSettingType) -> u64 { |
| 83 |
self.settings |
| 84 |
.iter() |
| 85 |
.find(|s| s.setting_type == setting) |
| 86 |
.map_or_else(|| hsetting_default(setting), |v| v.value) |
| 87 |
} |
| 88 |
|
| 89 |
pub fn encode_frame_contents(&self, enc: &mut Encoder) { |
| 90 |
enc.encode_vvec_with(|enc_inner| { |
| 91 |
for iter in &self.settings { |
| 92 |
match iter.setting_type { |
| 93 |
HSettingType::MaxHeaderListSize => { |
| 94 |
enc_inner.encode_varint(SETTINGS_MAX_HEADER_LIST_SIZE); |
| 95 |
enc_inner.encode_varint(iter.value); |
| 96 |
} |
| 97 |
HSettingType::MaxTableCapacity => { |
| 98 |
enc_inner.encode_varint(SETTINGS_QPACK_MAX_TABLE_CAPACITY); |
| 99 |
enc_inner.encode_varint(iter.value); |
| 100 |
} |
| 101 |
HSettingType::BlockedStreams => { |
| 102 |
enc_inner.encode_varint(SETTINGS_QPACK_BLOCKED_STREAMS); |
| 103 |
enc_inner.encode_varint(iter.value); |
| 104 |
} |
| 105 |
HSettingType::EnableWebTransport => { |
| 106 |
enc_inner.encode_varint(SETTINGS_ENABLE_WEB_TRANSPORT); |
| 107 |
enc_inner.encode_varint(iter.value); |
| 108 |
} |
| 109 |
HSettingType::EnableH3Datagram => { |
| 110 |
if iter.value == 1 { |
| 111 |
enc_inner.encode_varint(SETTINGS_H3_DATAGRAM_DRAFT04); |
| 112 |
enc_inner.encode_varint(iter.value); |
| 113 |
enc_inner.encode_varint(SETTINGS_H3_DATAGRAM); |
| 114 |
enc_inner.encode_varint(iter.value); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
}); |
| 120 |
} |
| 121 |
|
| 122 |
/// # Errors |
| 123 |
/// |
| 124 |
/// Returns an error if settings types are reserved of settings value are not permitted. |
| 125 |
pub fn decode_frame_contents(&mut self, dec: &mut Decoder) -> Res<()> { |
| 126 |
while dec.remaining() > 0 { |
| 127 |
let t = dec.decode_varint(); |
| 128 |
let v = dec.decode_varint(); |
| 129 |
|
| 130 |
if let Some(settings_type) = t { |
| 131 |
if H3_RESERVED_SETTINGS.contains(&settings_type) { |
| 132 |
return Err(Error::HttpSettings); |
| 133 |
} |
| 134 |
} |
| 135 |
match (t, v) { |
| 136 |
(Some(SETTINGS_MAX_HEADER_LIST_SIZE), Some(value)) => self |
| 137 |
.settings |
| 138 |
.push(HSetting::new(HSettingType::MaxHeaderListSize, value)), |
| 139 |
(Some(SETTINGS_QPACK_MAX_TABLE_CAPACITY), Some(value)) => self |
| 140 |
.settings |
| 141 |
.push(HSetting::new(HSettingType::MaxTableCapacity, value)), |
| 142 |
(Some(SETTINGS_QPACK_BLOCKED_STREAMS), Some(value)) => self |
| 143 |
.settings |
| 144 |
.push(HSetting::new(HSettingType::BlockedStreams, value)), |
| 145 |
(Some(SETTINGS_ENABLE_WEB_TRANSPORT), Some(value)) => { |
| 146 |
if value > 1 { |
| 147 |
return Err(Error::HttpSettings); |
| 148 |
} |
| 149 |
self.settings |
| 150 |
.push(HSetting::new(HSettingType::EnableWebTransport, value)); |
| 151 |
} |
| 152 |
(Some(SETTINGS_H3_DATAGRAM_DRAFT04), Some(value)) => { |
| 153 |
if value > 1 { |
| 154 |
return Err(Error::HttpSettings); |
| 155 |
} |
| 156 |
if !self |
| 157 |
.settings |
| 158 |
.iter() |
| 159 |
.any(|s| s.setting_type == HSettingType::EnableH3Datagram) |
| 160 |
{ |
| 161 |
self.settings |
| 162 |
.push(HSetting::new(HSettingType::EnableH3Datagram, value)); |
| 163 |
} |
| 164 |
} |
| 165 |
(Some(SETTINGS_H3_DATAGRAM), Some(value)) => { |
| 166 |
if value > 1 { |
| 167 |
return Err(Error::HttpSettings); |
| 168 |
} |
| 169 |
if !self |
| 170 |
.settings |
| 171 |
.iter() |
| 172 |
.any(|s| s.setting_type == HSettingType::EnableH3Datagram) |
| 173 |
{ |
| 174 |
self.settings |
| 175 |
.push(HSetting::new(HSettingType::EnableH3Datagram, value)); |
| 176 |
} |
| 177 |
} |
| 178 |
// other supported settings here |
| 179 |
(Some(_), Some(_)) => {} // ignore unknown setting, it is fine. |
| 180 |
_ => return Err(Error::NotEnoughData), |
| 181 |
} |
| 182 |
} |
| 183 |
Ok(()) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
impl Deref for HSettings { |
| 188 |
type Target = [HSetting]; |
| 189 |
fn deref(&self) -> &Self::Target { |
| 190 |
&self.settings |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
impl From<&Http3Parameters> for HSettings { |
| 195 |
fn from(conn_param: &Http3Parameters) -> Self { |
| 196 |
Self { |
| 197 |
settings: vec![ |
| 198 |
HSetting { |
| 199 |
setting_type: HSettingType::MaxTableCapacity, |
| 200 |
value: conn_param.get_max_table_size_decoder(), |
| 201 |
}, |
| 202 |
HSetting { |
| 203 |
setting_type: HSettingType::BlockedStreams, |
| 204 |
value: u64::from(conn_param.get_max_blocked_streams()), |
| 205 |
}, |
| 206 |
HSetting { |
| 207 |
setting_type: HSettingType::EnableWebTransport, |
| 208 |
value: u64::from(conn_param.get_webtransport()), |
| 209 |
}, |
| 210 |
HSetting { |
| 211 |
setting_type: HSettingType::EnableH3Datagram, |
| 212 |
value: u64::from(conn_param.get_http3_datagram()), |
| 213 |
}, |
| 214 |
], |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
#[derive(Debug)] |
| 220 |
pub struct HttpZeroRttChecker { |
| 221 |
settings: Http3Parameters, |
| 222 |
} |
| 223 |
|
| 224 |
impl HttpZeroRttChecker { |
| 225 |
/// Right now we only have QPACK settings, so that is all this takes. |
| 226 |
#[must_use] |
| 227 |
pub const fn new(settings: Http3Parameters) -> Self { |
| 228 |
Self { settings } |
| 229 |
} |
| 230 |
|
| 231 |
/// Save the settings that matter for 0-RTT. |
| 232 |
#[must_use] |
| 233 |
pub fn save(settings: &Http3Parameters) -> Vec<u8> { |
| 234 |
let mut enc = Encoder::new(); |
| 235 |
enc.encode_varint(SETTINGS_ZERO_RTT_VERSION) |
| 236 |
.encode_varint(SETTINGS_QPACK_MAX_TABLE_CAPACITY) |
| 237 |
.encode_varint(settings.get_max_table_size_decoder()) |
| 238 |
.encode_varint(SETTINGS_QPACK_BLOCKED_STREAMS) |
| 239 |
.encode_varint(settings.get_max_blocked_streams()); |
| 240 |
if settings.get_webtransport() { |
| 241 |
enc.encode_varint(SETTINGS_ENABLE_WEB_TRANSPORT) |
| 242 |
.encode_varint(true); |
| 243 |
} |
| 244 |
if settings.get_http3_datagram() { |
| 245 |
enc.encode_varint(SETTINGS_H3_DATAGRAM).encode_varint(true); |
| 246 |
} |
| 247 |
enc.into() |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
impl ZeroRttChecker for HttpZeroRttChecker { |
| 252 |
fn check(&self, token: &[u8]) -> ZeroRttCheckResult { |
| 253 |
let mut dec = Decoder::from(token); |
| 254 |
|
| 255 |
// Read and check the version. |
| 256 |
if let Some(version) = dec.decode_varint() { |
| 257 |
if version != SETTINGS_ZERO_RTT_VERSION { |
| 258 |
return ZeroRttCheckResult::Reject; |
| 259 |
} |
| 260 |
} else { |
| 261 |
return ZeroRttCheckResult::Fail; |
| 262 |
} |
| 263 |
|
| 264 |
// Now treat the rest as a settings frame. |
| 265 |
let mut settings = HSettings::new(&[]); |
| 266 |
if settings.decode_frame_contents(&mut dec).is_err() { |
| 267 |
return ZeroRttCheckResult::Fail; |
| 268 |
} |
| 269 |
if settings.iter().all(|setting| match setting.setting_type { |
| 270 |
HSettingType::BlockedStreams => { |
| 271 |
u64::from(self.settings.get_max_blocked_streams()) >= setting.value |
| 272 |
} |
| 273 |
HSettingType::MaxTableCapacity => { |
| 274 |
self.settings.get_max_table_size_decoder() >= setting.value |
| 275 |
} |
| 276 |
HSettingType::EnableWebTransport => { |
| 277 |
if setting.value > 1 { |
| 278 |
return false; |
| 279 |
} |
| 280 |
let value = setting.value == 1; |
| 281 |
self.settings.get_webtransport() || !value |
| 282 |
} |
| 283 |
HSettingType::EnableH3Datagram => { |
| 284 |
if setting.value > 1 { |
| 285 |
return false; |
| 286 |
} |
| 287 |
let value = setting.value == 1; |
| 288 |
self.settings.get_http3_datagram() || !value |
| 289 |
} |
| 290 |
HSettingType::MaxHeaderListSize => true, |
| 291 |
}) { |
| 292 |
ZeroRttCheckResult::Accept |
| 293 |
} else { |
| 294 |
ZeroRttCheckResult::Reject |
| 295 |
} |
| 296 |
} |
| 297 |
} |