| 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 |
#![allow( |
| 8 |
clippy::module_name_repetitions, |
| 9 |
reason = "<https://github.com/mozilla/neqo/issues/2284#issuecomment-2782711813>" |
| 10 |
)] |
| 11 |
|
| 12 |
use enum_map::Enum; |
| 13 |
use neqo_common::qdebug; |
| 14 |
|
| 15 |
use crate::{Error, Res}; |
| 16 |
|
| 17 |
pub type WireVersion = u32; |
| 18 |
|
| 19 |
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Enum)] |
| 20 |
#[repr(u32)] |
| 21 |
pub enum Version { |
| 22 |
Version2 = 0x6b33_43cf, |
| 23 |
#[default] |
| 24 |
Version1 = 1, |
| 25 |
#[cfg(feature = "draft-29")] |
| 26 |
Draft29 = 0xff00_0000 + 29, |
| 27 |
} |
| 28 |
|
| 29 |
impl Version { |
| 30 |
#[must_use] |
| 31 |
pub const fn wire_version(self) -> WireVersion { |
| 32 |
self as u32 |
| 33 |
} |
| 34 |
|
| 35 |
pub(crate) const fn initial_salt(self) -> &'static [u8] { |
| 36 |
const INITIAL_SALT_V2: &[u8] = &[ |
| 37 |
0x0d, 0xed, 0xe3, 0xde, 0xf7, 0x00, 0xa6, 0xdb, 0x81, 0x93, 0x81, 0xbe, 0x6e, 0x26, |
| 38 |
0x9d, 0xcb, 0xf9, 0xbd, 0x2e, 0xd9, |
| 39 |
]; |
| 40 |
const INITIAL_SALT_V1: &[u8] = &[ |
| 41 |
0x38, 0x76, 0x2c, 0xf7, 0xf5, 0x59, 0x34, 0xb3, 0x4d, 0x17, 0x9a, 0xe6, 0xa4, 0xc8, |
| 42 |
0x0c, 0xad, 0xcc, 0xbb, 0x7f, 0x0a, |
| 43 |
]; |
| 44 |
#[cfg(feature = "draft-29")] |
| 45 |
const INITIAL_SALT_29_32: &[u8] = &[ |
| 46 |
0xaf, 0xbf, 0xec, 0x28, 0x99, 0x93, 0xd2, 0x4c, 0x9e, 0x97, 0x86, 0xf1, 0x9c, 0x61, |
| 47 |
0x11, 0xe0, 0x43, 0x90, 0xa8, 0x99, |
| 48 |
]; |
| 49 |
match self { |
| 50 |
Self::Version2 => INITIAL_SALT_V2, |
| 51 |
Self::Version1 => INITIAL_SALT_V1, |
| 52 |
#[cfg(feature = "draft-29")] |
| 53 |
Self::Draft29 => INITIAL_SALT_29_32, |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
pub(crate) const fn label_prefix(self) -> &'static str { |
| 58 |
match self { |
| 59 |
Self::Version2 => "quicv2 ", |
| 60 |
Self::Version1 => "quic ", |
| 61 |
#[cfg(feature = "draft-29")] |
| 62 |
Self::Draft29 => "quic ", |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
pub(crate) const fn retry_secret(self) -> &'static [u8] { |
| 67 |
const RETRY_SECRET_V2: &[u8] = &[ |
| 68 |
0xc4, 0xdd, 0x24, 0x84, 0xd6, 0x81, 0xae, 0xfa, 0x4f, 0xf4, 0xd6, 0x9c, 0x2c, 0x20, |
| 69 |
0x29, 0x99, 0x84, 0xa7, 0x65, 0xa5, 0xd3, 0xc3, 0x19, 0x82, 0xf3, 0x8f, 0xc7, 0x41, |
| 70 |
0x62, 0x15, 0x5e, 0x9f, |
| 71 |
]; |
| 72 |
const RETRY_SECRET_V1: &[u8] = &[ |
| 73 |
0xd9, 0xc9, 0x94, 0x3e, 0x61, 0x01, 0xfd, 0x20, 0x00, 0x21, 0x50, 0x6b, 0xcc, 0x02, |
| 74 |
0x81, 0x4c, 0x73, 0x03, 0x0f, 0x25, 0xc7, 0x9d, 0x71, 0xce, 0x87, 0x6e, 0xca, 0x87, |
| 75 |
0x6e, 0x6f, 0xca, 0x8e, |
| 76 |
]; |
| 77 |
#[cfg(feature = "draft-29")] |
| 78 |
const RETRY_SECRET_29: &[u8] = &[ |
| 79 |
0x8b, 0x0d, 0x37, 0xeb, 0x85, 0x35, 0x02, 0x2e, 0xbc, 0x8d, 0x76, 0xa2, 0x07, 0xd8, |
| 80 |
0x0d, 0xf2, 0x26, 0x46, 0xec, 0x06, 0xdc, 0x80, 0x96, 0x42, 0xc3, 0x0a, 0x8b, 0xaa, |
| 81 |
0x2b, 0xaa, 0xff, 0x4c, |
| 82 |
]; |
| 83 |
match self { |
| 84 |
Self::Version2 => RETRY_SECRET_V2, |
| 85 |
Self::Version1 => RETRY_SECRET_V1, |
| 86 |
#[cfg(feature = "draft-29")] |
| 87 |
Self::Draft29 => RETRY_SECRET_29, |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
pub(crate) const fn is_draft(self) -> bool { |
| 92 |
#[cfg(feature = "draft-29")] |
| 93 |
return matches!(self, Self::Draft29); |
| 94 |
#[cfg(not(feature = "draft-29"))] |
| 95 |
false |
| 96 |
} |
| 97 |
|
| 98 |
/// Determine if `self` can be upgraded to `other` compatibly. |
| 99 |
#[must_use] |
| 100 |
pub fn is_compatible(self, other: Self) -> bool { |
| 101 |
self == other |
| 102 |
|| matches!( |
| 103 |
(self, other), |
| 104 |
(Self::Version1, Self::Version2) | (Self::Version2, Self::Version1) |
| 105 |
) |
| 106 |
} |
| 107 |
|
| 108 |
#[must_use] |
| 109 |
pub fn all() -> Vec<Self> { |
| 110 |
vec![ |
| 111 |
Self::Version2, |
| 112 |
Self::Version1, |
| 113 |
#[cfg(feature = "draft-29")] |
| 114 |
Self::Draft29, |
| 115 |
] |
| 116 |
} |
| 117 |
|
| 118 |
pub fn compatible<'a>( |
| 119 |
self, |
| 120 |
all: impl IntoIterator<Item = &'a Self>, |
| 121 |
) -> impl Iterator<Item = &'a Self> { |
| 122 |
all.into_iter().filter(move |&v| self.is_compatible(*v)) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
impl TryFrom<WireVersion> for Version { |
| 127 |
type Error = Error; |
| 128 |
|
| 129 |
fn try_from(wire: WireVersion) -> Res<Self> { |
| 130 |
if wire == 1 { |
| 131 |
Ok(Self::Version1) |
| 132 |
} else if wire == 0x6b33_43cf { |
| 133 |
Ok(Self::Version2) |
| 134 |
} else { |
| 135 |
#[cfg(feature = "draft-29")] |
| 136 |
if wire == 0xff00_0000 + 29 { |
| 137 |
return Ok(Self::Draft29); |
| 138 |
} |
| 139 |
Err(Error::VersionNegotiation) |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
#[derive(Debug, Clone)] |
| 145 |
pub struct VersionConfig { |
| 146 |
/// The version that a client uses to establish a connection. |
| 147 |
/// |
| 148 |
/// For a client, this is the version that is sent out in an Initial packet. |
| 149 |
/// A client that resumes will set this to the version from the original |
| 150 |
/// connection. |
| 151 |
/// A client that handles a Version Negotiation packet will be initialized with |
| 152 |
/// a version chosen from the packet, but it will then have this value overridden |
| 153 |
/// to match the original configuration so that the version negotiation can be |
| 154 |
/// authenticated. |
| 155 |
/// |
| 156 |
/// For a server `Connection`, this is the only type of Initial packet that |
| 157 |
/// can be accepted; the correct value is set by `Server`, see below. |
| 158 |
/// |
| 159 |
/// For a `Server`, this value is not used; if an Initial packet is received |
| 160 |
/// in a supported version (as listed in `versions`), new instances of |
| 161 |
/// `Connection` will be created with this value set to match what was received. |
| 162 |
/// |
| 163 |
/// An invariant here is that this version is always listed in `all`. |
| 164 |
initial: Version, |
| 165 |
/// The set of versions that are enabled, in preference order. For a server, |
| 166 |
/// only the relative order of compatible versions matters. |
| 167 |
all: Vec<Version>, |
| 168 |
} |
| 169 |
|
| 170 |
impl VersionConfig { |
| 171 |
/// # Panics |
| 172 |
/// When `all` does not include `initial`. |
| 173 |
#[must_use] |
| 174 |
pub fn new(initial: Version, all: Vec<Version>) -> Self { |
| 175 |
assert!(all.contains(&initial)); |
| 176 |
Self { initial, all } |
| 177 |
} |
| 178 |
|
| 179 |
#[must_use] |
| 180 |
pub const fn initial(&self) -> Version { |
| 181 |
self.initial |
| 182 |
} |
| 183 |
|
| 184 |
#[must_use] |
| 185 |
pub fn all(&self) -> &[Version] { |
| 186 |
&self.all |
| 187 |
} |
| 188 |
|
| 189 |
/// Overwrite the initial value; used by the `Server` when handling new connections |
| 190 |
/// and by the client on resumption. |
| 191 |
pub(crate) fn set_initial(&mut self, initial: Version) { |
| 192 |
qdebug!( |
| 193 |
"Overwrite initial version {:?} ==> {initial:?}", |
| 194 |
self.initial |
| 195 |
); |
| 196 |
assert!(self.all.contains(&initial)); |
| 197 |
self.initial = initial; |
| 198 |
} |
| 199 |
|
| 200 |
pub fn compatible(&self) -> impl Iterator<Item = &Version> { |
| 201 |
self.initial.compatible(&self.all) |
| 202 |
} |
| 203 |
|
| 204 |
fn find_preferred<'a>( |
| 205 |
preferences: impl IntoIterator<Item = &'a Version>, |
| 206 |
vn: &[WireVersion], |
| 207 |
) -> Option<Version> { |
| 208 |
for v in preferences { |
| 209 |
if vn.contains(&v.wire_version()) { |
| 210 |
return Some(*v); |
| 211 |
} |
| 212 |
} |
| 213 |
None |
| 214 |
} |
| 215 |
|
| 216 |
/// Determine the preferred version based on a version negotiation packet. |
| 217 |
pub(crate) fn preferred(&self, vn: &[WireVersion]) -> Option<Version> { |
| 218 |
Self::find_preferred(&self.all, vn) |
| 219 |
} |
| 220 |
|
| 221 |
/// Determine the preferred version based on a set of compatible versions. |
| 222 |
pub(crate) fn preferred_compatible(&self, vn: &[WireVersion]) -> Option<Version> { |
| 223 |
Self::find_preferred(self.compatible(), vn) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
impl Default for VersionConfig { |
| 228 |
fn default() -> Self { |
| 229 |
Self::new(Version::default(), Version::all()) |
| 230 |
} |
| 231 |
} |