| 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::cell::RefCell; |
| 8 |
|
| 9 |
use neqo_common::qerror; |
| 10 |
use neqo_crypto::{hkdf, Aead, TLS_AES_128_GCM_SHA256, TLS_VERSION_1_3}; |
| 11 |
|
| 12 |
use crate::{version::Version, Error, Res}; |
| 13 |
|
| 14 |
/// The AEAD used for Retry is fixed, so use thread local storage. |
| 15 |
fn make_aead(version: Version) -> Aead { |
| 16 |
#[cfg(debug_assertions)] |
| 17 |
::neqo_crypto::assert_initialized(); |
| 18 |
|
| 19 |
let secret = hkdf::import_key(TLS_VERSION_1_3, version.retry_secret()).expect("can import key"); |
| 20 |
Aead::new( |
| 21 |
TLS_VERSION_1_3, |
| 22 |
TLS_AES_128_GCM_SHA256, |
| 23 |
&secret, |
| 24 |
version.label_prefix(), |
| 25 |
) |
| 26 |
.expect("can create AEAD") |
| 27 |
} |
| 28 |
#[cfg(feature = "draft-29")] |
| 29 |
thread_local!(static RETRY_AEAD_29: RefCell<Aead> = RefCell::new(make_aead(Version::Draft29))); |
| 30 |
thread_local!(static RETRY_AEAD_V1: RefCell<Aead> = RefCell::new(make_aead(Version::Version1))); |
| 31 |
thread_local!(static RETRY_AEAD_V2: RefCell<Aead> = RefCell::new(make_aead(Version::Version2))); |
| 32 |
|
| 33 |
/// Run a function with the appropriate Retry AEAD. |
| 34 |
pub fn use_aead<F, T>(version: Version, f: F) -> Res<T> |
| 35 |
where |
| 36 |
F: FnOnce(&Aead) -> Res<T>, |
| 37 |
{ |
| 38 |
match version { |
| 39 |
Version::Version2 => &RETRY_AEAD_V2, |
| 40 |
Version::Version1 => &RETRY_AEAD_V1, |
| 41 |
#[cfg(feature = "draft-29")] |
| 42 |
Version::Draft29 => &RETRY_AEAD_29, |
| 43 |
} |
| 44 |
.try_with(|aead| f(&aead.borrow())) |
| 45 |
.map_err(|e| { |
| 46 |
qerror!("Unable to access Retry AEAD: {e:?}"); |
| 47 |
Error::InternalError |
| 48 |
})? |
| 49 |
} |
| 50 |
|
| 51 |
/// Determine how large the expansion is for a given key. |
| 52 |
pub fn expansion(version: Version) -> usize { |
| 53 |
use_aead(version, |aead| Ok(aead.expansion())) |
| 54 |
.unwrap_or_else(|_| panic!("Unable to access Retry AEAD")) |
| 55 |
} |