yog 0.0.110

yog: the standalone server for litany loops — the world, the balls and the conversations, behind one wire
//! The key exchange both ends land on (bl-75ca): the hybrid post-quantum
//! group when the peer has it, plain X25519 when the peer has nothing newer —
//! in both directions, because the server and its clients upgrade separately.
//!
//! Each handshake is real and whole — certificates verified both ways over the
//! minted material — and runs in memory, one end's bytes handed to the other.

use super::*;
use rustls::crypto::SupportedKxGroup;
use rustls::crypto::aws_lc_rs::kx_group::{X25519, X25519MLKEM768};
use rustls::pki_types::ServerName;
use rustls::{ClientConnection, NamedGroup, ServerConnection};
use std::net::Ipv4Addr;

/// A peer that offers exactly one key exchange group and nothing else.
fn only(group: &'static dyn SupportedKxGroup) -> Arc<CryptoProvider> {
    Arc::new(CryptoProvider {
        kx_groups: vec![group],
        ..rustls::crypto::aws_lc_rs::default_provider()
    })
}

/// Run one handshake to completion and name the group it agreed on. `None`
/// for a provider is the shipping configuration, untouched.
fn agreed(
    server: Option<Arc<CryptoProvider>>,
    client: Option<Arc<CryptoProvider>>,
) -> Option<NamedGroup> {
    let tmp = TempDir::new().expect("tmp");
    mint(tmp.path());
    let server_m = material(tmp.path(), Role::Server, "127.0.0.1:0");
    let client_m = material(tmp.path(), Role::Client, "127.0.0.1:0");
    let server = server.map_or_else(|| server_config(&server_m), |p| server_with(&server_m, p));
    let client = client.map_or_else(|| client_config(&client_m), |p| client_with(&client_m, p));
    let name = ServerName::IpAddress(Ipv4Addr::LOCALHOST.into());
    let mut client = ClientConnection::new(client.expect("client"), name).expect("tls");
    let mut server = ServerConnection::new(server.expect("server")).expect("tls");
    while client.is_handshaking() || server.is_handshaking() {
        let mut flight = Vec::new();
        client.write_tls(&mut flight).expect("client writes");
        server
            .read_tls(&mut flight.as_slice())
            .expect("server reads");
        server.process_new_packets().expect("server accepts");
        let mut flight = Vec::new();
        server.write_tls(&mut flight).expect("server writes");
        client
            .read_tls(&mut flight.as_slice())
            .expect("client reads");
        client.process_new_packets().expect("client accepts");
    }
    let group = client
        .negotiated_key_exchange_group()
        .map(SupportedKxGroup::name);
    assert_eq!(
        group,
        server
            .negotiated_key_exchange_group()
            .map(SupportedKxGroup::name)
    );
    group
}

/// Two shipping ends choose the hybrid group — it is preferred, not merely
/// available.
#[test]
fn two_shipping_ends_agree_on_the_hybrid_group() {
    assert_eq!(agreed(None, None), Some(NamedGroup::X25519MLKEM768));
}

/// A client that offers ONLY the hybrid group is served: the server really
/// does speak it, rather than listing it and falling back.
#[test]
fn a_hybrid_only_client_is_served() {
    let group = agreed(None, Some(only(X25519MLKEM768)));
    assert_eq!(group, Some(NamedGroup::X25519MLKEM768));
}

/// A server that accepts ONLY the hybrid group is reached by the shipping
/// client.
#[test]
fn a_hybrid_only_server_is_reached() {
    let group = agreed(Some(only(X25519MLKEM768)), None);
    assert_eq!(group, Some(NamedGroup::X25519MLKEM768));
}

/// A client from before the change — X25519 and nothing newer — still lands
/// on the upgraded server.
#[test]
fn an_x25519_only_client_is_still_served() {
    assert_eq!(agreed(None, Some(only(X25519))), Some(NamedGroup::X25519));
}

/// The other order of upgrade: the shipping client still reaches a server
/// that has only X25519.
#[test]
fn an_x25519_only_server_is_still_reached() {
    assert_eq!(agreed(Some(only(X25519)), None), Some(NamedGroup::X25519));
}