| 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::{ |
| 8 |
ffi::CString, |
| 9 |
os::raw::{c_char, c_uint}, |
| 10 |
ptr::{addr_of_mut, null_mut}, |
| 11 |
}; |
| 12 |
|
| 13 |
use neqo_common::qtrace; |
| 14 |
|
| 15 |
use crate::{ |
| 16 |
err::{ssl::SSL_ERROR_ECH_RETRY_WITH_ECH, Error, Res}, |
| 17 |
experimental_api, null_safe_slice, |
| 18 |
p11::{ |
| 19 |
self, Item, PrivateKey, PublicKey, SECITEM_FreeItem, SECItem, SECKEYPrivateKey, |
| 20 |
SECKEYPublicKey, Slot, |
| 21 |
}, |
| 22 |
ssl::{PRBool, PRFileDesc}, |
| 23 |
}; |
| 24 |
pub use crate::{ |
| 25 |
p11::{HpkeAeadId as AeadId, HpkeKdfId as KdfId, HpkeKemId as KemId}, |
| 26 |
ssl::HpkeSymmetricSuite as SymmetricSuite, |
| 27 |
}; |
| 28 |
|
| 29 |
experimental_api!(SSL_EnableTls13GreaseEch( |
| 30 |
fd: *mut PRFileDesc, |
| 31 |
enabled: PRBool, |
| 32 |
)); |
| 33 |
|
| 34 |
experimental_api!(SSL_GetEchRetryConfigs( |
| 35 |
fd: *mut PRFileDesc, |
| 36 |
config: *mut SECItem, |
| 37 |
)); |
| 38 |
|
| 39 |
experimental_api!(SSL_SetClientEchConfigs( |
| 40 |
fd: *mut PRFileDesc, |
| 41 |
config_list: *const u8, |
| 42 |
config_list_len: c_uint, |
| 43 |
)); |
| 44 |
|
| 45 |
experimental_api!(SSL_SetServerEchConfigs( |
| 46 |
fd: *mut PRFileDesc, |
| 47 |
pk: *const SECKEYPublicKey, |
| 48 |
sk: *const SECKEYPrivateKey, |
| 49 |
record: *const u8, |
| 50 |
record_len: c_uint, |
| 51 |
)); |
| 52 |
|
| 53 |
experimental_api!(SSL_EncodeEchConfigId( |
| 54 |
config_id: u8, |
| 55 |
public_name: *const c_char, |
| 56 |
max_name_len: c_uint, |
| 57 |
kem_id: KemId::Type, |
| 58 |
pk: *const SECKEYPublicKey, |
| 59 |
hpke_suites: *const SymmetricSuite, |
| 60 |
hpke_suite_count: c_uint, |
| 61 |
out: *mut u8, |
| 62 |
out_len: *mut c_uint, |
| 63 |
max_len: c_uint, |
| 64 |
)); |
| 65 |
|
| 66 |
/// Convert any result that contains an ECH error into a result with an `EchRetry`. |
| 67 |
pub fn convert_ech_error(fd: *mut PRFileDesc, err: Error) -> Error { |
| 68 |
if let Error::NssError { |
| 69 |
code: SSL_ERROR_ECH_RETRY_WITH_ECH, |
| 70 |
.. |
| 71 |
} = &err |
| 72 |
{ |
| 73 |
let mut item = Item::make_empty(); |
| 74 |
if unsafe { SSL_GetEchRetryConfigs(fd, &mut item).is_err() } { |
| 75 |
return Error::InternalError; |
| 76 |
} |
| 77 |
let buf = unsafe { |
| 78 |
let slc = null_safe_slice(item.data, item.len); |
| 79 |
let buf = Vec::from(slc); |
| 80 |
SECITEM_FreeItem(&mut item, PRBool::from(false)); |
| 81 |
buf |
| 82 |
}; |
| 83 |
Error::EchRetry(buf) |
| 84 |
} else { |
| 85 |
err |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/// Generate a key pair for encrypted client hello (ECH). |
| 90 |
/// |
| 91 |
/// # Errors |
| 92 |
/// |
| 93 |
/// When NSS fails to generate a key pair or when the KEM is not supported. |
| 94 |
/// |
| 95 |
/// # Panics |
| 96 |
/// |
| 97 |
/// When underlying types aren't large enough to hold keys. So never. |
| 98 |
pub fn generate_keys() -> Res<(PrivateKey, PublicKey)> { |
| 99 |
let slot = Slot::internal()?; |
| 100 |
|
| 101 |
let oid_data = unsafe { p11::SECOID_FindOIDByTag(p11::SECOidTag::SEC_OID_CURVE25519) }; |
| 102 |
let oid = unsafe { oid_data.as_ref() }.ok_or(Error::InternalError)?; |
| 103 |
let oid_slc = unsafe { null_safe_slice(oid.oid.data, oid.oid.len) }; |
| 104 |
let mut params: Vec<u8> = Vec::with_capacity(oid_slc.len() + 2); |
| 105 |
params.push(u8::try_from(p11::SEC_ASN1_OBJECT_ID)?); |
| 106 |
params.push(u8::try_from(oid.oid.len)?); |
| 107 |
params.extend_from_slice(oid_slc); |
| 108 |
|
| 109 |
let mut public_ptr: *mut SECKEYPublicKey = null_mut(); |
| 110 |
let mut param_item = Item::wrap(¶ms)?; |
| 111 |
|
| 112 |
// If we have tracing on, try to ensure that key data can be read. |
| 113 |
let insensitive_secret_ptr = if log::log_enabled!(log::Level::Trace) { |
| 114 |
unsafe { |
| 115 |
p11::PK11_GenerateKeyPairWithOpFlags( |
| 116 |
*slot, |
| 117 |
p11::CK_MECHANISM_TYPE::from(p11::CKM_EC_KEY_PAIR_GEN), |
| 118 |
addr_of_mut!(param_item).cast(), |
| 119 |
&mut public_ptr, |
| 120 |
p11::PK11_ATTR_SESSION | p11::PK11_ATTR_INSENSITIVE | p11::PK11_ATTR_PUBLIC, |
| 121 |
p11::CK_FLAGS::from(p11::CKF_DERIVE), |
| 122 |
p11::CK_FLAGS::from(p11::CKF_DERIVE), |
| 123 |
null_mut(), |
| 124 |
) |
| 125 |
} |
| 126 |
} else { |
| 127 |
null_mut() |
| 128 |
}; |
| 129 |
assert_eq!(insensitive_secret_ptr.is_null(), public_ptr.is_null()); |
| 130 |
let secret_ptr = if insensitive_secret_ptr.is_null() { |
| 131 |
unsafe { |
| 132 |
p11::PK11_GenerateKeyPairWithOpFlags( |
| 133 |
*slot, |
| 134 |
p11::CK_MECHANISM_TYPE::from(p11::CKM_EC_KEY_PAIR_GEN), |
| 135 |
addr_of_mut!(param_item).cast(), |
| 136 |
&mut public_ptr, |
| 137 |
p11::PK11_ATTR_SESSION | p11::PK11_ATTR_SENSITIVE | p11::PK11_ATTR_PRIVATE, |
| 138 |
p11::CK_FLAGS::from(p11::CKF_DERIVE), |
| 139 |
p11::CK_FLAGS::from(p11::CKF_DERIVE), |
| 140 |
null_mut(), |
| 141 |
) |
| 142 |
} |
| 143 |
} else { |
| 144 |
insensitive_secret_ptr |
| 145 |
}; |
| 146 |
assert_eq!(secret_ptr.is_null(), public_ptr.is_null()); |
| 147 |
let sk = PrivateKey::from_ptr(secret_ptr)?; |
| 148 |
let pk = PublicKey::from_ptr(public_ptr)?; |
| 149 |
qtrace!("Generated key pair: sk={sk:?} pk={pk:?}"); |
| 150 |
Ok((sk, pk)) |
| 151 |
} |
| 152 |
|
| 153 |
/// Encode a configuration for encrypted client hello (ECH). |
| 154 |
/// |
| 155 |
/// # Errors |
| 156 |
/// |
| 157 |
/// When NSS fails to generate a valid configuration encoding (i.e., unlikely). |
| 158 |
pub fn encode_config(config: u8, public_name: &str, pk: &PublicKey) -> Res<Vec<u8>> { |
| 159 |
// A sensible fixed value for the maximum length of a name. |
| 160 |
const MAX_NAME_LEN: c_uint = 64; |
| 161 |
// Enable a selection of suites. |
| 162 |
// NSS supports SHA-512 as well, which could be added here. |
| 163 |
const SUITES: &[SymmetricSuite] = &[ |
| 164 |
SymmetricSuite { |
| 165 |
kdfId: KdfId::HpkeKdfHkdfSha256, |
| 166 |
aeadId: AeadId::HpkeAeadAes128Gcm, |
| 167 |
}, |
| 168 |
SymmetricSuite { |
| 169 |
kdfId: KdfId::HpkeKdfHkdfSha256, |
| 170 |
aeadId: AeadId::HpkeAeadChaCha20Poly1305, |
| 171 |
}, |
| 172 |
SymmetricSuite { |
| 173 |
kdfId: KdfId::HpkeKdfHkdfSha384, |
| 174 |
aeadId: AeadId::HpkeAeadAes128Gcm, |
| 175 |
}, |
| 176 |
SymmetricSuite { |
| 177 |
kdfId: KdfId::HpkeKdfHkdfSha384, |
| 178 |
aeadId: AeadId::HpkeAeadChaCha20Poly1305, |
| 179 |
}, |
| 180 |
]; |
| 181 |
|
| 182 |
let name = CString::new(public_name)?; |
| 183 |
let mut encoded = [0; 1024]; |
| 184 |
let mut encoded_len = 0; |
| 185 |
unsafe { |
| 186 |
SSL_EncodeEchConfigId( |
| 187 |
config, |
| 188 |
name.as_ptr(), |
| 189 |
MAX_NAME_LEN, |
| 190 |
KemId::HpkeDhKemX25519Sha256, |
| 191 |
**pk, |
| 192 |
SUITES.as_ptr(), |
| 193 |
c_uint::try_from(SUITES.len())?, |
| 194 |
encoded.as_mut_ptr(), |
| 195 |
&mut encoded_len, |
| 196 |
c_uint::try_from(encoded.len())?, |
| 197 |
)?; |
| 198 |
} |
| 199 |
Ok(Vec::from(&encoded[..usize::try_from(encoded_len)?])) |
| 200 |
} |