This commit is contained in:
Alex Emmet 2026-06-23 23:18:03 +02:00
commit ade0c3cde4
24 changed files with 1701 additions and 321 deletions

View file

@ -1,11 +1,10 @@
use std::fs;
use mtp_client::{ClientConfig, MTPClient, MTPConnection};
use mtp_codec::{CommunicationType, CommunicationValue, DataType, DataValue};
use mtp_crypto::{
Ed25519Signer, EncryptionPrivateKey, EncryptionPublicKey, KeyGroup, Keyring,
KemPrivateKey, KemPublicKey, SignaturePqPrivateKey, SignaturePqPublicKey,
SignaturePrivateKey, SignaturePublicKey,
use mtp::client::{ClientConfig, MTPClient, MTPConnection};
use mtp::codec::{CommunicationType, CommunicationValue, DataType, DataValue};
use mtp::crypto::{
Ed25519Signer, KemPublicKey, Keyring, MlDsaSigner, PublicKeyBundle, SignaturePqPublicKey,
SignaturePublicKey,
};
/*
@ -14,45 +13,39 @@ use mtp_crypto::{
* connection and the local keyring.
*/
async fn connect_or_register(
config: ClientConfig,
host_public_key: SignaturePublicKey,
mut config: ClientConfig,
host_public_key: PublicKeyBundle,
client_key_path: &str,
) -> Result<(MTPConnection, Keyring), Box<dyn std::error::Error>> {
if let Ok(data) = fs::read_to_string(client_key_path) {
let json: serde_json::Value = serde_json::from_str(&data)?;
let client_id = json["client_id"].as_u64().expect("Invalid client_id");
let keyring = Keyring::from_bytes(
&hex::decode(json["keyring"].as_str().expect("Missing keyring"))?,
)?;
let keyring = Keyring::from_bytes(&hex::decode(
json["keyring"].as_str().expect("Missing keyring"),
)?)?;
println!("Loaded client keys (ID: {})", client_id);
let keygroup = KeyGroup::new(
EncryptionPublicKey::new(vec![]),
EncryptionPrivateKey::new(vec![]),
keyring.sig_cl_public_key.clone(),
SignaturePrivateKey::new(keyring.sig_cl_secret_key.as_bytes().to_vec()),
);
let conn = MTPClient::auth_connect(config, client_id, keygroup, host_public_key).await?;
config.client_id = client_id;
let conn = MTPClient::auth_connect(config, &keyring, &host_public_key).await?;
println!("Authenticated (version {})", conn.version);
Ok((conn, keyring))
} else {
println!("No existing keys found - registering new client");
println!("No existing keys found: registering new client");
let (_signer, sig_sk, sig_pk) = Ed25519Signer::generate();
let (_ed_signer, sig_sk, sig_pk) = Ed25519Signer::generate();
let (_pq_signer, sig_pq_sk, sig_pq_pk) = MlDsaSigner::generate();
let keyring = Keyring::new(
KemPublicKey::new(vec![]),
KemPrivateKey::new(vec![]),
SignaturePqPublicKey::new(vec![]),
SignaturePqPrivateKey::new(vec![]),
mtp::crypto::KemPublicKey::new(vec![]),
mtp::crypto::KemPrivateKey::new(vec![]),
sig_pq_pk,
sig_pq_sk,
sig_pk,
sig_sk,
);
// Serialise before the move so we can persist and return the keyring
let keyring_bytes = keyring.to_bytes();
let conn = MTPClient::auth_register(config, keyring, host_public_key).await?;
let conn = MTPClient::auth_register(config, &keyring, &host_public_key).await?;
println!("Registered with ID: {}", conn.client_id);
let json = serde_json::json!({
@ -79,7 +72,10 @@ async fn send_ping_and_receive(conn: &MTPConnection) -> Result<(), Box<dyn std::
let greeting = CommunicationValue::new(CommunicationType::Ping)
.add_typed_default(DataType::Description, DataValue::Str("Hello MTP!".into()))
.add_typed_default(DataType::Timestamp, DataValue::UnsignedNumber(timestamp as u128))
.add_typed_default(
DataType::Timestamp,
DataValue::UnsignedNumber(timestamp as u128),
)
.add_typed_default(DataType::Data, DataValue::UnsignedNumber(42))
.with_sender(conn.client_id);
@ -97,9 +93,17 @@ async fn send_ping_and_receive(conn: &MTPConnection) -> Result<(), Box<dyn std::
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let cert_pem = fs::read("server.pem").expect("Missing server.pem - run server first");
let host_public_key = SignaturePublicKey::new(
fs::read("host_sig_pk.bin").expect("Missing host_sig_pk.bin - run server first"),
let cert_pem = fs::read("server.pem").expect("Missing server.pem: run server first");
let host_public_key = PublicKeyBundle::new(
KemPublicKey::new(
fs::read("host_enc_kem_pk.bin").expect("Missing host_enc_kem_pk.bin: run server first"),
),
SignaturePqPublicKey::new(
fs::read("host_sig_pq_pk.bin").expect("Missing host_sig_pq_pk.bin: run server first"),
),
SignaturePublicKey::new(
fs::read("host_sig_pk.bin").expect("Missing host_sig_pk.bin: run server first"),
),
);
println!("Connecting to 127.0.0.1:8080 ...");
@ -107,6 +111,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = ClientConfig {
url: "https://127.0.0.1:8080".into(),
server_cert: Some(cert_pem),
client_id: 0,
};
let (conn, _keyring) = connect_or_register(config, host_public_key, "client_keys.json").await?;