| 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::Range; |
| 8 |
|
| 9 |
use neqo_common::{qtrace, Decoder}; |
| 10 |
|
| 11 |
/// Finds the range where the SNI extension lives, or returns `None`. |
| 12 |
#[must_use] |
| 13 |
pub fn find_sni(buf: &[u8]) -> Option<Range<usize>> { |
| 14 |
#[must_use] |
| 15 |
fn skip(dec: &mut Decoder, len: usize) -> Option<()> { |
| 16 |
if len > dec.remaining() { |
| 17 |
return None; |
| 18 |
} |
| 19 |
dec.skip(len); |
| 20 |
Some(()) |
| 21 |
} |
| 22 |
|
| 23 |
#[must_use] |
| 24 |
fn skip_vec<T>(dec: &mut Decoder) -> Option<()> |
| 25 |
where |
| 26 |
T: TryFrom<u64>, |
| 27 |
usize: TryFrom<T>, |
| 28 |
{ |
| 29 |
let len = dec.decode_uint::<T>()?; |
| 30 |
skip(dec, usize::try_from(len).ok()?) |
| 31 |
} |
| 32 |
|
| 33 |
let mut dec = Decoder::from(buf); |
| 34 |
|
| 35 |
// Return if buf is empty or does not contain a ClientHello (first byte == 1) |
| 36 |
if buf.is_empty() || dec.decode_uint::<u8>()? != 1 { |
| 37 |
return None; |
| 38 |
} |
| 39 |
skip(&mut dec, 3 + 2 + 32)?; // Skip length, version, random |
| 40 |
skip_vec::<u8>(&mut dec)?; // Skip session_id |
| 41 |
skip_vec::<u16>(&mut dec)?; // Skip cipher_suites |
| 42 |
skip_vec::<u8>(&mut dec)?; // Skip compression_methods |
| 43 |
skip(&mut dec, 2)?; |
| 44 |
|
| 45 |
while dec.remaining() >= 4 { |
| 46 |
let ext_type: u16 = dec.decode_uint()?; |
| 47 |
let ext_len: u16 = dec.decode_uint()?; |
| 48 |
if ext_type == 0 { |
| 49 |
// SNI! |
| 50 |
let sni_len = dec.decode_uint::<u16>()?; |
| 51 |
if sni_len < 3 { |
| 52 |
return None; |
| 53 |
} |
| 54 |
skip(&mut dec, 3)?; // Skip name_type and host_name length |
| 55 |
let start = dec.offset(); |
| 56 |
let end = start + usize::from(sni_len) - 3; |
| 57 |
if end > dec.offset() + dec.remaining() { |
| 58 |
return None; |
| 59 |
} |
| 60 |
qtrace!( |
| 61 |
"SNI range {start}..{end}: {:?}", |
| 62 |
String::from_utf8_lossy(&buf[start..end]) |
| 63 |
); |
| 64 |
return Some(start..end); |
| 65 |
} |
| 66 |
// Skip extension |
| 67 |
skip(&mut dec, ext_len.into())?; |
| 68 |
} |
| 69 |
None |
| 70 |
} |
| 71 |
|
| 72 |
#[cfg(test)] |
| 73 |
mod tests { |
| 74 |
const BUF_WITH_SNI: &[u8] = &[ |
| 75 |
0x01, // msg_type == 1 (ClientHello) |
| 76 |
0x00, 0x01, 0xfc, // length (arbitrary) |
| 77 |
0x03, 0x03, // version (TLS 1.2) |
| 78 |
0x0e, 0x2d, 0x03, 0x37, 0xd9, 0x14, 0x2b, 0x32, 0x4e, 0xa8, 0xcf, 0x1f, 0xfa, 0x5b, 0x6c, |
| 79 |
0xeb, 0xdd, 0x10, 0xa6, 0x49, 0x6e, 0xbf, 0xe4, 0x32, 0x3d, 0x0c, 0xe4, 0xbf, 0x90, 0xcf, |
| 80 |
0x08, 0x42, // random |
| 81 |
0x00, // session_id length |
| 82 |
0x00, 0x08, // cipher_suites length |
| 83 |
0x13, 0x01, 0x13, 0x03, 0x13, 0x02, 0xca, 0xca, // cipher_suites |
| 84 |
0x01, // compression_methods length |
| 85 |
0x00, // compression_methods |
| 86 |
0x01, 0xcb, // extensions length |
| 87 |
0xff, 0x01, 0x00, 0x01, 0x00, // renegiation_info |
| 88 |
0x00, 0x2d, 0x00, 0x03, 0x02, 0x01, 0x87, // psk_exchange_modes |
| 89 |
// SNI extension |
| 90 |
0x00, 0x00, // Extension type (SNI) |
| 91 |
0x00, 0x0e, // Extension length |
| 92 |
0x00, 0x0c, // Server Name List length |
| 93 |
0x00, // Name type (host_name) |
| 94 |
0x00, 0x09, // Host name length |
| 95 |
0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, // server_name: "localhost" |
| 96 |
0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, // status_request |
| 97 |
]; |
| 98 |
|
| 99 |
#[test] |
| 100 |
fn find_sni() { |
| 101 |
// ClientHello with SNI extension |
| 102 |
let range = super::find_sni(BUF_WITH_SNI).unwrap(); |
| 103 |
let expected_range = BUF_WITH_SNI.len() - 18..BUF_WITH_SNI.len() - 9; |
| 104 |
assert_eq!(range, expected_range); |
| 105 |
assert_eq!(&BUF_WITH_SNI[range], b"localhost"); |
| 106 |
} |
| 107 |
|
| 108 |
#[test] |
| 109 |
fn find_sni_no_sni() { |
| 110 |
// ClientHello without SNI extension |
| 111 |
let mut buf = Vec::from(&BUF_WITH_SNI[..BUF_WITH_SNI.len() - 39]); |
| 112 |
let len = buf.len(); |
| 113 |
assert_eq!(&buf[len - 2..len], &[0x01, 0xcb], "check extensions length"); |
| 114 |
buf[len - 2..len].copy_from_slice(&[0x00, 0x00]); // Set extensions length to 0 |
| 115 |
assert!(super::find_sni(&buf).is_none()); |
| 116 |
} |
| 117 |
|
| 118 |
#[test] |
| 119 |
fn find_sni_invalid_sni() { |
| 120 |
// ClientHello with an SNI extension truncated somewhere in the hostname |
| 121 |
let truncated = &BUF_WITH_SNI[..BUF_WITH_SNI.len() - 15]; |
| 122 |
assert!(super::find_sni(truncated).is_none()); |
| 123 |
} |
| 124 |
|
| 125 |
#[test] |
| 126 |
fn find_sni_invalid_sni_length() { |
| 127 |
// ClientHello with an SNI extension with an invalid length |
| 128 |
let mut buf = Vec::from(BUF_WITH_SNI); |
| 129 |
let len = buf.len(); |
| 130 |
assert_eq!(&buf[len - 23..len - 21], &[0x00, 0x0c], "check SNI length"); |
| 131 |
buf[len - 23..len - 21].copy_from_slice(&[0x00, 0x02]); // Set Server Name List length to 2 |
| 132 |
assert!(super::find_sni(&buf).is_none()); |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn find_sni_no_ci() { |
| 137 |
// Not a ClientHello (msg_type != 1) |
| 138 |
let buf = [0; 1]; |
| 139 |
assert!(super::find_sni(&buf).is_none()); |
| 140 |
} |
| 141 |
|
| 142 |
#[test] |
| 143 |
fn find_sni_malformed_ci() { |
| 144 |
// Buffer starting with `1` but otherwise malformed |
| 145 |
let buf = [1; 1]; |
| 146 |
assert!(super::find_sni(&buf).is_none()); |
| 147 |
} |
| 148 |
} |