teter-core 26.11.7

What teter's clients share without I/O: end-to-end encryption, account crypto, signed commands
Documentation
//! The account's machines and sessions as the relay lists them (#87,
//! #133), checked and opened with the account's keys: what a device may
//! trust of a `Sessions` answer, never the relay's word.

use teter_common::account::{
    MACHINE_NAME_INFO, MachineSessions, SessionRecordInfo, record_bytes, session_key_info,
    session_name_info,
};

use crate::account::{AccountKeys, cert_holds, verify};
use crate::e2e::Secret;

/// Whether `machine` carries the account's approval (#133): its ID, key and
/// sealed name signed by the account, checked against `keys`.
#[must_use]
pub fn approved(keys: &AccountKeys, machine: &MachineSessions) -> bool {
    cert_holds(
        &keys.public_keys(),
        &machine.id,
        &machine.public_key,
        &machine.sealed_name,
        &machine.cert,
    )
}

/// Whether `record` of `machine` is signed by the machine's key.
#[must_use]
pub fn signed(machine: &MachineSessions, record: &SessionRecordInfo) -> bool {
    verify(
        &machine.public_key,
        &record_bytes(
            &machine.id,
            &record.channel,
            &record.sealed_name,
            &record.sealed_key,
        ),
        &record.signature,
    )
}

/// `sealed`, opened with `keys` for `info`, as text.
fn opened(keys: &AccountKeys, sealed: &[u8], info: &[u8]) -> Option<String> {
    keys.open_sealed(info, sealed)
        .ok()
        .and_then(|plain| String::from_utf8(plain.to_vec()).ok())
}

/// `machine`'s display name, if it opens with `keys`.
#[must_use]
pub fn machine_name(keys: &AccountKeys, machine: &MachineSessions) -> Option<String> {
    opened(keys, &machine.sealed_name, MACHINE_NAME_INFO)
}

/// The name of `record` of `machine`, if it opens with `keys`.
#[must_use]
pub fn session_name(
    keys: &AccountKeys,
    machine: &MachineSessions,
    record: &SessionRecordInfo,
) -> Option<String> {
    opened(
        keys,
        &record.sealed_name,
        &session_name_info(&machine.id, &record.channel),
    )
}

/// The end-to-end secret of `record`, if it opens with `keys`.
#[must_use]
pub fn session_secret(keys: &AccountKeys, record: &SessionRecordInfo) -> Option<Secret> {
    let text = keys
        .open_sealed(&session_key_info(&record.channel), &record.sealed_key)
        .ok()?;
    Secret::parse(std::str::from_utf8(&text).ok()?).ok()
}