use super::flight6::*;
use super::*;
use crate::cipher_suite::*;
use crate::client_certificate_type::*;
use crate::compression_methods::*;
use crate::config::*;
use crate::content::*;
use crate::crypto::*;
use crate::curve::named_curve::*;
use crate::curve::*;
use crate::extension::extension_supported_point_formats::*;
use crate::extension::extension_use_extended_master_secret::*;
use crate::extension::extension_use_srtp::*;
use crate::extension::*;
use crate::handshake::handshake_message_certificate::*;
use crate::handshake::handshake_message_certificate_request::*;
use crate::handshake::handshake_message_server_hello::*;
use crate::handshake::handshake_message_server_hello_done::*;
use crate::handshake::handshake_message_server_key_exchange::*;
use crate::handshake::*;
use crate::prf::*;
use crate::record_layer::record_layer_header::*;
use crate::record_layer::*;
use crate::signature_hash_algorithm::*;
use shared::error::Error;
use crate::extension::renegotiation_info::ExtensionRenegotiationInfo;
use log::*;
use std::fmt;
use std::io::BufWriter;
#[cfg(test)]
use std::sync::Arc;
#[derive(Debug, PartialEq)]
pub(crate) struct Flight4;
impl fmt::Display for Flight4 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Flight 4")
}
}
impl Flight for Flight4 {
fn parse(
&self,
state: &mut State,
cache: &HandshakeCache,
cfg: &HandshakeConfig,
) -> Result<Box<dyn Flight>, (Option<Alert>, Option<Error>)> {
let (seq, msgs) = match cache.full_pull_map(
state.handshake_recv_sequence,
&[
HandshakeCachePullRule {
typ: HandshakeType::Certificate,
epoch: cfg.initial_epoch,
is_client: true,
optional: true,
},
HandshakeCachePullRule {
typ: HandshakeType::ClientKeyExchange,
epoch: cfg.initial_epoch,
is_client: true,
optional: false,
},
HandshakeCachePullRule {
typ: HandshakeType::CertificateVerify,
epoch: cfg.initial_epoch,
is_client: true,
optional: true,
},
],
) {
Ok((seq, msgs)) => (seq, msgs),
Err(_) => return Err((None, None)),
};
let client_key_exchange = if let Some(HandshakeMessage::ClientKeyExchange(h)) =
msgs.get(&HandshakeType::ClientKeyExchange)
{
h
} else {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::InternalError,
}),
None,
));
};
if let Some(message) = msgs.get(&HandshakeType::Certificate) {
let h = match message {
HandshakeMessage::Certificate(h) => h,
_ => {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::InternalError,
}),
None,
));
}
};
state.peer_certificates.clone_from(&h.certificate);
trace!(
"[handshake] PeerCertificates4 {}",
state.peer_certificates.len()
);
}
if let Some(message) = msgs.get(&HandshakeType::CertificateVerify) {
let h = match message {
HandshakeMessage::CertificateVerify(h) => h,
_ => {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::InternalError,
}),
None,
));
}
};
if state.peer_certificates.is_empty() {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::UnexpectedMessage,
}),
Some(Error::ErrCertificateVerifyNoCertificate),
));
}
let plain_text = cache.pull_and_merge(&[
HandshakeCachePullRule {
typ: HandshakeType::ClientHello,
epoch: cfg.initial_epoch,
is_client: true,
optional: false,
},
HandshakeCachePullRule {
typ: HandshakeType::ServerHello,
epoch: cfg.initial_epoch,
is_client: false,
optional: false,
},
HandshakeCachePullRule {
typ: HandshakeType::Certificate,
epoch: cfg.initial_epoch,
is_client: false,
optional: false,
},
HandshakeCachePullRule {
typ: HandshakeType::ServerKeyExchange,
epoch: cfg.initial_epoch,
is_client: false,
optional: false,
},
HandshakeCachePullRule {
typ: HandshakeType::CertificateRequest,
epoch: cfg.initial_epoch,
is_client: false,
optional: false,
},
HandshakeCachePullRule {
typ: HandshakeType::ServerHelloDone,
epoch: cfg.initial_epoch,
is_client: false,
optional: false,
},
HandshakeCachePullRule {
typ: HandshakeType::Certificate,
epoch: cfg.initial_epoch,
is_client: true,
optional: false,
},
HandshakeCachePullRule {
typ: HandshakeType::ClientKeyExchange,
epoch: cfg.initial_epoch,
is_client: true,
optional: false,
},
]);
let mut valid_signature_scheme = false;
for ss in &cfg.local_signature_schemes {
if ss.hash == h.algorithm.hash && ss.signature == h.algorithm.signature {
valid_signature_scheme = true;
break;
}
}
if !valid_signature_scheme {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::IllegalParameter,
}),
Some(Error::ErrNoAvailableSignatureSchemes),
));
}
let mut verified_key = plain_text;
verified_key.push(h.algorithm.hash as u8);
verified_key.push(h.algorithm.signature as u8);
verified_key.extend_from_slice(&h.signature);
if state.verified_certificate_verify.as_deref() != Some(verified_key.as_slice()) {
let plain_text = &verified_key[..verified_key.len() - 2 - h.signature.len()];
let verifies_chain =
cfg.client_auth as u8 >= ClientAuthType::VerifyClientCertIfGiven as u8;
let mut chains = vec![];
if !verifies_chain
&& let Some(verify_peer_certificate) = &cfg.verify_peer_certificate
&& let Err(err) = verify_peer_certificate(&state.peer_certificates, &chains)
{
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::BadCertificate,
}),
Some(err),
));
}
if let Err(err) = verify_certificate_verify(
cfg.provider().crypto(),
plain_text,
&h.algorithm,
&h.signature,
&state.peer_certificates,
cfg.insecure_verification,
) {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::DecryptError,
}),
Some(err),
));
}
let mut verified = false;
if verifies_chain {
if let Some(client_cert_verifier) = &cfg.client_cert_verifier {
chains = match verify_client_cert(
&state.peer_certificates,
client_cert_verifier,
) {
Ok(chains) => chains,
Err(err) => {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::BadCertificate,
}),
Some(err),
));
}
};
} else {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::BadCertificate,
}),
Some(Error::ErrInvalidCertificate),
));
}
verified = true;
if let Some(verify_peer_certificate) = &cfg.verify_peer_certificate
&& let Err(err) = verify_peer_certificate(&state.peer_certificates, &chains)
{
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::BadCertificate,
}),
Some(err),
));
}
}
state.peer_certificates_verified = verified;
state.verified_certificate_verify = Some(verified_key);
}
} else if !state.peer_certificates.is_empty() {
return Err((None, None));
}
{
let cipher_suite = state
.cipher_suite
.as_ref()
.map(|cipher_suite| (cipher_suite.is_initialized(), cipher_suite.hash_func()));
if let Some((cipher_suite_is_initialized, cipher_suite_hash_func)) = cipher_suite
&& !cipher_suite_is_initialized
{
let mut server_random = vec![];
{
let mut writer = BufWriter::<&mut Vec<u8>>::new(server_random.as_mut());
let _ = state.local_random.marshal(&mut writer);
}
let mut client_random = vec![];
{
let mut writer = BufWriter::<&mut Vec<u8>>::new(client_random.as_mut());
let _ = state.remote_random.marshal(&mut writer);
}
let mut pre_master_secret = crypto::SecretVec::new(vec![]);
if state.is_cipher_suite_psk() {
let Some(local_psk_callback) = &cfg.local_psk_callback else {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::InternalError,
}),
Some(Error::ErrPskAndIdentityMustBeSetForClient),
));
};
let psk = match local_psk_callback(&client_key_exchange.identity_hint) {
Ok(psk) => crypto::SecretVec::new(psk),
Err(err) => {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::InternalError,
}),
Some(err),
));
}
};
state
.identity_hint
.clone_from(&client_key_exchange.identity_hint);
pre_master_secret = prf_psk_pre_master_secret(psk.as_ref());
} else if let Some(local_keypair) = &mut state.local_keypair {
let curve = local_keypair.curve;
pre_master_secret = match prf_pre_master_secret(
&client_key_exchange.public_key,
local_keypair,
curve,
) {
Ok(pre_master_secret) => pre_master_secret,
Err(err) => {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::IllegalParameter,
}),
Some(err),
));
}
};
}
if state.extended_master_secret {
let hf = cipher_suite_hash_func;
let session_hash = match cache.session_hash(
cfg.provider().crypto(),
hf,
cfg.initial_epoch,
&[],
) {
Ok(s) => s,
Err(err) => {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::InternalError,
}),
Some(err),
));
}
};
state.master_secret = match prf_extended_master_secret(
cfg.provider().crypto(),
pre_master_secret.as_ref(),
&session_hash,
cipher_suite_hash_func,
) {
Ok(ms) => ms,
Err(err) => {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::InternalError,
}),
Some(err),
));
}
};
} else {
state.master_secret = match prf_master_secret(
cfg.provider().crypto(),
pre_master_secret.as_ref(),
&client_random,
&server_random,
cipher_suite_hash_func,
) {
Ok(ms) => ms,
Err(err) => {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::InternalError,
}),
Some(err),
));
}
};
}
drop(pre_master_secret);
if let Some(cipher_suite) = &mut state.cipher_suite
&& let Err(err) = cipher_suite.init(
cfg.provider().clone(),
state.master_secret.as_ref(),
&client_random,
&server_random,
false,
)
{
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::InternalError,
}),
Some(err),
));
}
}
}
let (seq, msgs) = match cache.full_pull_map(
seq,
&[HandshakeCachePullRule {
typ: HandshakeType::Finished,
epoch: cfg.initial_epoch + 1,
is_client: true,
optional: false,
}],
) {
Ok((seq, msgs)) => (seq, msgs),
Err(_) => return Err((None, None)),
};
state.handshake_recv_sequence = seq;
let finished =
if let Some(HandshakeMessage::Finished(h)) = msgs.get(&HandshakeType::Finished) {
h
} else {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::InternalError,
}),
None,
));
};
verify_client_finished(state, cache, cfg, finished)?;
if state.is_cipher_suite_psk() {
return Ok(Box::new(Flight6 {}) as Box<dyn Flight>);
}
match cfg.client_auth {
ClientAuthType::RequireAnyClientCert => {
trace!(
"{} peer_certificates.len() {}",
srv_cli_str(state.is_client),
state.peer_certificates.len(),
);
if state.peer_certificates.is_empty() {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: missing_client_certificate_alert(cfg),
}),
Some(Error::ErrClientCertificateRequired),
));
}
}
ClientAuthType::VerifyClientCertIfGiven => {
if !state.peer_certificates.is_empty() && !state.peer_certificates_verified {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::BadCertificate,
}),
Some(Error::ErrClientCertificateNotVerified),
));
}
}
ClientAuthType::RequireAndVerifyClientCert => {
if state.peer_certificates.is_empty() {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: missing_client_certificate_alert(cfg),
}),
Some(Error::ErrClientCertificateRequired),
));
}
if !state.peer_certificates_verified {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::BadCertificate,
}),
Some(Error::ErrClientCertificateNotVerified),
));
}
}
ClientAuthType::NoClientCert | ClientAuthType::RequestClientCert => {
return Ok(Box::new(Flight6 {}) as Box<dyn Flight>);
}
}
Ok(Box::new(Flight6 {}) as Box<dyn Flight>)
}
fn generate(
&self,
state: &mut State,
_cache: &HandshakeCache,
cfg: &HandshakeConfig,
) -> Result<Vec<Packet>, (Option<Alert>, Option<Error>)> {
let extensions = server_hello_extensions(state, cfg);
let mut pkts = vec![Packet {
record: RecordLayer::new(
PROTOCOL_VERSION1_2,
0,
Content::Handshake(Handshake::new(HandshakeMessage::ServerHello(
HandshakeMessageServerHello {
session_id: state.session_id.clone(),
version: PROTOCOL_VERSION1_2,
random: state.local_random.clone(),
cipher_suite: {
if let Some(cipher_suite) = &state.cipher_suite {
cipher_suite.id()
} else {
CipherSuiteId::Unsupported
}
},
compression_method: default_compression_methods().ids[0],
extensions,
},
))),
),
should_encrypt: false,
reset_local_sequence_number: false,
}];
if !state.is_cipher_suite_psk() {
let certificate = match cfg.get_certificate(&cfg.server_name) {
Ok(cert) => cert,
Err(err) => {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::HandshakeFailure,
}),
Some(err),
));
}
};
pkts.push(Packet {
record: RecordLayer::new(
PROTOCOL_VERSION1_2,
0,
Content::Handshake(Handshake::new(HandshakeMessage::Certificate(
HandshakeMessageCertificate {
certificate: certificate
.certificate
.iter()
.map(|x| x.as_ref().to_owned())
.collect(),
},
))),
),
should_encrypt: false,
reset_local_sequence_number: false,
});
let mut server_random = vec![];
{
let mut writer = BufWriter::<&mut Vec<u8>>::new(server_random.as_mut());
let _ = state.local_random.marshal(&mut writer);
}
let mut client_random = vec![];
{
let mut writer = BufWriter::<&mut Vec<u8>>::new(client_random.as_mut());
let _ = state.remote_random.marshal(&mut writer);
}
let signature_hash_algo = match state.local_signature_scheme {
Some(scheme) => scheme,
None => {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::HandshakeFailure,
}),
Some(Error::ErrNoAvailableSignatureSchemes),
));
}
};
if let Some(local_keypair) = &state.local_keypair {
let signature = match generate_key_signature(
&client_random,
&server_random,
&local_keypair.public_key,
state.named_curve,
&signature_hash_algo,
&certificate.private_key,
) {
Ok(s) => s,
Err(err) => {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::InternalError,
}),
Some(err),
));
}
};
state.local_key_signature = signature;
pkts.push(Packet {
record: RecordLayer::new(
PROTOCOL_VERSION1_2,
0,
Content::Handshake(Handshake::new(HandshakeMessage::ServerKeyExchange(
HandshakeMessageServerKeyExchange {
identity_hint: vec![],
elliptic_curve_type: EllipticCurveType::NamedCurve,
named_curve: state.named_curve,
public_key: local_keypair.public_key.clone(),
algorithm: SignatureHashAlgorithm {
hash: signature_hash_algo.hash,
signature: signature_hash_algo.signature,
},
signature: state.local_key_signature.clone(),
},
))),
),
should_encrypt: false,
reset_local_sequence_number: false,
});
}
if cfg.client_auth as u8 > ClientAuthType::NoClientCert as u8 {
pkts.push(Packet {
record: RecordLayer::new(
PROTOCOL_VERSION1_2,
0,
Content::Handshake(Handshake::new(HandshakeMessage::CertificateRequest(
HandshakeMessageCertificateRequest {
certificate_types: vec![
ClientCertificateType::RsaSign,
ClientCertificateType::EcdsaSign,
],
signature_hash_algorithms: cfg.local_signature_schemes.clone(),
},
))),
),
should_encrypt: false,
reset_local_sequence_number: false,
});
}
} else if let Some(local_psk_identity_hint) = &cfg.local_psk_identity_hint {
pkts.push(Packet {
record: RecordLayer::new(
PROTOCOL_VERSION1_2,
0,
Content::Handshake(Handshake::new(HandshakeMessage::ServerKeyExchange(
HandshakeMessageServerKeyExchange {
identity_hint: local_psk_identity_hint.clone(),
elliptic_curve_type: EllipticCurveType::Unsupported,
named_curve: NamedCurve::Unsupported,
public_key: vec![],
algorithm: SignatureHashAlgorithm {
hash: HashAlgorithm::Unsupported,
signature: SignatureAlgorithm::Unsupported,
},
signature: vec![],
},
))),
),
should_encrypt: false,
reset_local_sequence_number: false,
});
}
pkts.push(Packet {
record: RecordLayer::new(
PROTOCOL_VERSION1_2,
0,
Content::Handshake(Handshake::new(HandshakeMessage::ServerHelloDone(
HandshakeMessageServerHelloDone {},
))),
),
should_encrypt: false,
reset_local_sequence_number: false,
});
Ok(pkts)
}
}
pub(crate) fn server_hello_extensions(state: &mut State, cfg: &HandshakeConfig) -> Vec<Extension> {
let mut extensions = vec![];
if state.remote_connection_id.is_some() {
if state.local_connection_id.is_none() {
state.local_connection_id = cfg.connection_id_offer();
}
if let Some(connection_id) = &state.local_connection_id {
extensions.push(Extension::connection_id(connection_id));
} else {
state.remote_connection_id = None;
}
}
if let Some(protocol) = &state.application_protocol {
extensions.push(Extension::alpn(std::slice::from_ref(protocol)));
}
if state.secure_renegotiation {
extensions.push(Extension::RenegotiationInfo(
ExtensionRenegotiationInfo::default(),
));
}
if (cfg.extended_master_secret == ExtendedMasterSecretType::Request
|| cfg.extended_master_secret == ExtendedMasterSecretType::Require)
&& state.extended_master_secret
{
extensions.push(Extension::UseExtendedMasterSecret(
ExtensionUseExtendedMasterSecret { supported: true },
));
}
if state.srtp_protection_profile != SrtpProtectionProfile::Unsupported {
extensions.push(Extension::UseSrtp(ExtensionUseSrtp {
protection_profiles: vec![state.srtp_protection_profile],
mki: vec![],
}));
}
if !state.is_cipher_suite_psk() && state.remote_offered_point_formats {
extensions.push(Extension::SupportedPointFormats(
ExtensionSupportedPointFormats {
point_formats: vec![ELLIPTIC_CURVE_POINT_FORMAT_UNCOMPRESSED],
},
));
}
if state.new_session_ticket {
extensions.push(crate::resumption::session_ticket_extension(&[]));
}
extensions
}
pub(crate) fn verify_client_finished(
state: &State,
cache: &HandshakeCache,
cfg: &HandshakeConfig,
finished: &crate::handshake::handshake_message_finished::HandshakeMessageFinished,
) -> Result<(), (Option<Alert>, Option<Error>)> {
let rule = |typ, is_client| HandshakeCachePullRule {
typ,
epoch: cfg.initial_epoch,
is_client,
optional: false,
};
let plain_text = cache.pull_and_merge(&[
rule(HandshakeType::ClientHello, true),
rule(HandshakeType::ServerHello, false),
rule(HandshakeType::Certificate, false),
rule(HandshakeType::ServerKeyExchange, false),
rule(HandshakeType::CertificateRequest, false),
rule(HandshakeType::ServerHelloDone, false),
rule(HandshakeType::Certificate, true),
rule(HandshakeType::ClientKeyExchange, true),
rule(HandshakeType::CertificateVerify, true),
]);
let internal_error = |err| {
(
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::InternalError,
}),
Some(err),
)
};
let Some(cipher_suite) = &state.cipher_suite else {
return Err(internal_error(Error::ErrCipherSuiteUnset));
};
let expected_verify_data = prf_verify_data_client(
cfg.provider().crypto(),
state.master_secret.as_ref(),
&plain_text,
cipher_suite.hash_func(),
)
.map_err(internal_error)?;
if !crypto::constant_time_eq(&expected_verify_data, &finished.verify_data) {
return Err((
Some(Alert {
alert_level: AlertLevel::Fatal,
alert_description: AlertDescription::DecryptError,
}),
Some(Error::ErrVerifyDataMismatch),
));
}
Ok(())
}
fn missing_client_certificate_alert(cfg: &HandshakeConfig) -> AlertDescription {
if cfg.bad_certificate_for_a_missing_client_certificate {
AlertDescription::BadCertificate
} else {
AlertDescription::HandshakeFailure
}
}
#[cfg(test)]
mod tests {
use super::*;
use shared::error::Result;
struct MockCipherSuite {}
impl CipherSuite for MockCipherSuite {
fn to_string(&self) -> String {
"MockCipherSuite".into()
}
fn id(&self) -> CipherSuiteId {
unimplemented!();
}
fn certificate_type(&self) -> ClientCertificateType {
unimplemented!();
}
fn hash_func(&self) -> CipherSuiteHash {
unimplemented!();
}
fn is_psk(&self) -> bool {
false
}
fn is_initialized(&self) -> bool {
panic!("is_initialized called with Certificate but not CertificateVerify");
}
fn record_expansion(&self) -> usize {
unimplemented!();
}
fn init(
&mut self,
_provider: Arc<dyn crypto::RTCCryptoProvider>,
_master_secret: &[u8],
_client_random: &[u8],
_server_random: &[u8],
_is_client: bool,
) -> Result<()> {
unimplemented!();
}
fn encrypt(&mut self, _pkt_rlh: &RecordLayerHeader, _raw: &[u8]) -> Result<Vec<u8>> {
unimplemented!();
}
fn decrypt(&mut self, _input: &[u8]) -> Result<Vec<u8>> {
unimplemented!();
}
}
#[test]
fn test_flight4_process_certificateverify() {
let provider =
crypto::default_provider().expect("a built-in crypto provider is enabled for tests");
let mut state = State {
cipher_suite: Some(Box::new(MockCipherSuite {})),
..State::new(provider, false)
};
let raw_certificate = vec![
0x0b, 0x00, 0x01, 0x9b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x9b, 0x00, 0x01,
0x98, 0x00, 0x01, 0x95, 0x30, 0x82, 0x01, 0x91, 0x30, 0x82, 0x01, 0x38, 0xa0, 0x03,
0x02, 0x01, 0x02, 0x02, 0x11, 0x01, 0x65, 0x03, 0x3f, 0x4d, 0x0b, 0x9a, 0x62, 0x91,
0xdb, 0x4d, 0x28, 0x2c, 0x1f, 0xd6, 0x73, 0x32, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86,
0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x00, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x32,
0x30, 0x35, 0x31, 0x35, 0x31, 0x38, 0x34, 0x33, 0x35, 0x35, 0x5a, 0x17, 0x0d, 0x32,
0x32, 0x30, 0x36, 0x31, 0x35, 0x31, 0x38, 0x34, 0x33, 0x35, 0x35, 0x5a, 0x30, 0x00,
0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06,
0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xc3,
0xb7, 0x13, 0x1a, 0x0a, 0xfc, 0xd0, 0x82, 0xf8, 0x94, 0x5e, 0xc0, 0x77, 0x07, 0x81,
0x28, 0xc9, 0xcb, 0x08, 0x84, 0x50, 0x6b, 0xf0, 0x22, 0xe8, 0x79, 0xb9, 0x15, 0x33,
0xc4, 0x56, 0xa1, 0xd3, 0x1b, 0x24, 0xe3, 0x61, 0xbd, 0x4d, 0x65, 0x80, 0x6b, 0x5d,
0x96, 0x48, 0xa2, 0x44, 0x9e, 0xce, 0xe8, 0x65, 0xd6, 0x3c, 0xe0, 0x9b, 0x6b, 0xa1,
0x36, 0x34, 0xb2, 0x39, 0xe2, 0x03, 0x00, 0xa3, 0x81, 0x92, 0x30, 0x81, 0x8f, 0x30,
0x0e, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x01, 0x01, 0xff, 0x04, 0x04, 0x03, 0x02, 0x02,
0xa4, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06, 0x08,
0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05,
0x05, 0x07, 0x03, 0x01, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff,
0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
0x04, 0x16, 0x04, 0x14, 0xb1, 0x1a, 0xe3, 0xeb, 0x6f, 0x7c, 0xc3, 0x8f, 0xba, 0x6f,
0x1c, 0xe8, 0xf0, 0x23, 0x08, 0x50, 0x8d, 0x3c, 0xea, 0x31, 0x30, 0x2e, 0x06, 0x03,
0x55, 0x1d, 0x11, 0x01, 0x01, 0xff, 0x04, 0x24, 0x30, 0x22, 0x82, 0x20, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02,
0x03, 0x47, 0x00, 0x30, 0x44, 0x02, 0x20, 0x06, 0x31, 0x43, 0xac, 0x03, 0x45, 0x79,
0x3c, 0xd7, 0x5f, 0x6e, 0x6a, 0xf8, 0x0e, 0xfd, 0x35, 0x49, 0xee, 0x1b, 0xbc, 0x47,
0xce, 0xe3, 0x39, 0xec, 0xe4, 0x62, 0xe1, 0x30, 0x1a, 0xa1, 0x89, 0x02, 0x20, 0x35,
0xcd, 0x7a, 0x15, 0x68, 0x09, 0x50, 0x49, 0x9e, 0x3e, 0x05, 0xd7, 0xc2, 0x69, 0x3f,
0x9c, 0x0c, 0x98, 0x92, 0x65, 0xec, 0xae, 0x44, 0xfe, 0xe5, 0x68, 0xb8, 0x09, 0x78,
0x7f, 0x6b, 0x77,
];
let raw_client_key_exchange = vec![
0x10, 0x00, 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x20, 0x96,
0xed, 0x0c, 0xee, 0xf3, 0x11, 0xb1, 0x9d, 0x8b, 0x1c, 0x02, 0x7f, 0x06, 0x7c, 0x57,
0x7a, 0x14, 0xa6, 0x41, 0xde, 0x63, 0x57, 0x9e, 0xcd, 0x34, 0x54, 0xba, 0x37, 0x4d,
0x34, 0x15, 0x18,
];
let mut cache = HandshakeCache::new();
cache.push(raw_certificate, 0, 0, HandshakeType::Certificate, true);
cache.push(
raw_client_key_exchange,
0,
1,
HandshakeType::ClientKeyExchange,
true,
);
let cfg = HandshakeConfig::new(
crypto::default_provider().expect("a built-in crypto provider is enabled for tests"),
);
let f = Flight4 {};
let res = f.parse(&mut state, &cache, &cfg);
assert!(res.is_err());
}
fn client_flight_cache(
certificate: &Certificate,
algorithm: SignatureHashAlgorithm,
valid_signature: bool,
) -> HandshakeCache {
use crate::handshake::handshake_message_certificate_verify::HandshakeMessageCertificateVerify;
use crate::handshake::handshake_message_client_key_exchange::HandshakeMessageClientKeyExchange;
fn marshal(message: HandshakeMessage, message_sequence: u16) -> Vec<u8> {
let mut handshake = Handshake::new(message);
handshake.handshake_header.message_sequence = message_sequence;
let mut data = vec![];
handshake.marshal(&mut data).unwrap();
data
}
let raw_certificate = marshal(
HandshakeMessage::Certificate(HandshakeMessageCertificate {
certificate: certificate
.certificate
.iter()
.map(|c| c.as_ref().to_vec())
.collect(),
}),
0,
);
let raw_client_key_exchange = marshal(
HandshakeMessage::ClientKeyExchange(HandshakeMessageClientKeyExchange {
identity_hint: vec![],
public_key: vec![0x42; 32],
}),
1,
);
let signature = if valid_signature {
let mut plain_text = raw_certificate.clone();
plain_text.extend_from_slice(&raw_client_key_exchange);
generate_certificate_verify(&plain_text, &algorithm, &certificate.private_key).unwrap()
} else {
vec![0x30; 70]
};
let raw_certificate_verify = marshal(
HandshakeMessage::CertificateVerify(HandshakeMessageCertificateVerify {
algorithm,
signature,
}),
2,
);
let mut cache = HandshakeCache::new();
assert!(cache.push(raw_certificate, 0, 0, HandshakeType::Certificate, true));
assert!(cache.push(
raw_client_key_exchange,
0,
1,
HandshakeType::ClientKeyExchange,
true,
));
assert!(cache.push(
raw_certificate_verify,
0,
2,
HandshakeType::CertificateVerify,
true,
));
cache
}
fn config_counting_peer_checks(
algorithm: SignatureHashAlgorithm,
peer_check: Result<()>,
) -> (HandshakeConfig, Arc<std::sync::atomic::AtomicUsize>) {
let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let mut cfg = HandshakeConfig::new(
crypto::default_provider().expect("a built-in crypto provider is enabled for tests"),
);
cfg.local_signature_schemes = vec![algorithm];
cfg.client_auth = ClientAuthType::RequireAnyClientCert;
let counter = Arc::clone(&calls);
let peer_check = peer_check.map_err(|err| err.to_string());
let check: VerifyPeerCertificateFn = Arc::new(
move |_certificates: &[Vec<u8>],
_chains: &[rustls::pki_types::CertificateDer<'static>]| {
counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
peer_check.clone().map_err(Error::Other)
},
);
cfg.verify_peer_certificate = Some(check);
(cfg, calls)
}
const ECDSA_SHA256: SignatureHashAlgorithm = SignatureHashAlgorithm {
hash: HashAlgorithm::Sha256,
signature: SignatureAlgorithm::Ecdsa,
};
#[test]
fn test_flight4_verifies_a_certificate_verify_once() {
let provider =
crypto::default_provider().expect("a built-in crypto provider is enabled for tests");
let certificate =
Certificate::generate_self_signed(vec!["localhost".to_owned()], provider.crypto())
.unwrap();
let cache = client_flight_cache(&certificate, ECDSA_SHA256, true);
let (cfg, calls) = config_counting_peer_checks(ECDSA_SHA256, Ok(()));
let mut state = State::new(provider, false);
for _ in 0..5 {
let res = Flight4 {}.parse(&mut state, &cache, &cfg);
assert!(matches!(res, Err((None, None))));
}
assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
assert!(state.verified_certificate_verify.is_some());
}
#[test]
fn test_flight4_checks_the_peer_certificate_before_the_signature() {
let provider =
crypto::default_provider().expect("a built-in crypto provider is enabled for tests");
let certificate =
Certificate::generate_self_signed(vec!["localhost".to_owned()], provider.crypto())
.unwrap();
let cache = client_flight_cache(&certificate, ECDSA_SHA256, false);
let (cfg, calls) = config_counting_peer_checks(
ECDSA_SHA256,
Err(Error::Other("fingerprint mismatch".to_owned())),
);
let mut state = State::new(provider, false);
let res = Flight4 {}.parse(&mut state, &cache, &cfg);
match res {
Err((Some(alert), Some(Error::Other(message)))) => {
assert_eq!(alert.alert_level, AlertLevel::Fatal);
assert_eq!(message, "fingerprint mismatch");
}
Err((alert, err)) => panic!("unexpected result: {alert:?} {err:?}"),
Ok(next) => panic!("unexpected flight {next}"),
}
assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
assert!(state.verified_certificate_verify.is_none());
}
#[test]
fn test_flight4_bad_certificate_verify_fails() {
let provider =
crypto::default_provider().expect("a built-in crypto provider is enabled for tests");
let certificate =
Certificate::generate_self_signed(vec!["localhost".to_owned()], provider.crypto())
.unwrap();
let cache = client_flight_cache(&certificate, ECDSA_SHA256, false);
let (cfg, _calls) = config_counting_peer_checks(ECDSA_SHA256, Ok(()));
let mut state = State::new(provider, false);
let res = Flight4 {}.parse(&mut state, &cache, &cfg);
assert!(matches!(res, Err((Some(_), Some(_)))));
assert!(state.verified_certificate_verify.is_none());
}
}