Skip to main content

teter_core/
sessions.rs

1//! The account's machines and sessions as the relay lists them (#87,
2//! #133), checked and opened with the account's keys: what a device may
3//! trust of a `Sessions` answer, never the relay's word.
4
5use teter_common::account::{
6    MACHINE_NAME_INFO, MachineSessions, SessionRecordInfo, record_bytes, session_key_info,
7    session_name_info,
8};
9
10use crate::account::{AccountKeys, cert_holds, verify};
11use crate::e2e::Secret;
12
13/// Whether `machine` carries the account's approval (#133): its ID, key and
14/// sealed name signed by the account, checked against `keys`.
15#[must_use]
16pub fn approved(keys: &AccountKeys, machine: &MachineSessions) -> bool {
17    cert_holds(
18        &keys.public_keys(),
19        &machine.id,
20        &machine.public_key,
21        &machine.sealed_name,
22        &machine.cert,
23    )
24}
25
26/// Whether `record` of `machine` is signed by the machine's key.
27#[must_use]
28pub fn signed(machine: &MachineSessions, record: &SessionRecordInfo) -> bool {
29    verify(
30        &machine.public_key,
31        &record_bytes(
32            &machine.id,
33            &record.channel,
34            &record.sealed_name,
35            &record.sealed_key,
36        ),
37        &record.signature,
38    )
39}
40
41/// `sealed`, opened with `keys` for `info`, as text.
42fn opened(keys: &AccountKeys, sealed: &[u8], info: &[u8]) -> Option<String> {
43    keys.open_sealed(info, sealed)
44        .ok()
45        .and_then(|plain| String::from_utf8(plain.to_vec()).ok())
46}
47
48/// `machine`'s display name, if it opens with `keys`.
49#[must_use]
50pub fn machine_name(keys: &AccountKeys, machine: &MachineSessions) -> Option<String> {
51    opened(keys, &machine.sealed_name, MACHINE_NAME_INFO)
52}
53
54/// The name of `record` of `machine`, if it opens with `keys`.
55#[must_use]
56pub fn session_name(
57    keys: &AccountKeys,
58    machine: &MachineSessions,
59    record: &SessionRecordInfo,
60) -> Option<String> {
61    opened(
62        keys,
63        &record.sealed_name,
64        &session_name_info(&machine.id, &record.channel),
65    )
66}
67
68/// The end-to-end secret of `record`, if it opens with `keys`.
69#[must_use]
70pub fn session_secret(keys: &AccountKeys, record: &SessionRecordInfo) -> Option<Secret> {
71    let text = keys
72        .open_sealed(&session_key_info(&record.channel), &record.sealed_key)
73        .ok()?;
74    Secret::parse(std::str::from_utf8(&text).ok()?).ok()
75}