teter_common/command.rs
1//! Commands from a device of the account to one of its machines (#88): to
2//! start or end a session, and later others.
3//!
4//! They travel as `input` and `output` on a command channel of the
5//! machine's relay session, so the relay carries them like any other bytes.
6//! The machine answers a [`ToMachine::Hello`] with a fresh
7//! [`FromMachine::Challenge`]: a nonce and a one-time X25519 key, signed by
8//! the machine's key. The device signs the command, bound to the machine and
9//! the nonce, with the account's key, and seals it to the one-time key. The
10//! machine opens it, checks the signature against the account key it pinned
11//! at registration, uses the nonce once, runs the command and seals the
12//! outcome to the account. The relay learns that a command went and its
13//! size.
14
15use serde::{Deserialize, Serialize};
16
17/// What a command channel's name starts with. Session channels are hex, so
18/// the `m` keeps the two apart.
19pub const CHANNEL_PREFIX: &str = "cmd";
20
21/// Whether `channel` is a command channel.
22pub fn is_command_channel(channel: &str) -> bool {
23 channel.starts_with(CHANNEL_PREFIX)
24}
25
26/// The most a command frame may take: room for a list of a few hundred
27/// sessions (bytes go as JSON numbers).
28pub const MAX_FRAME: usize = 256 * 1024;
29
30/// From the device, as the data of an `input`.
31#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
32#[serde(tag = "type", rename_all = "snake_case")]
33pub enum ToMachine {
34 /// Asks for a challenge.
35 Hello,
36 /// A [`Signed`] command, sealed to the challenge's one-time key.
37 Command {
38 /// The sealed command.
39 sealed: Vec<u8>,
40 },
41}
42
43/// From the machine, as the data of an `output`.
44#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
45#[serde(tag = "type", rename_all = "snake_case")]
46pub enum FromMachine {
47 /// A fresh challenge, good for one command on this channel.
48 Challenge {
49 /// The nonce the command is bound to.
50 nonce: Vec<u8>,
51 /// The one-time X25519 key the command is sealed to.
52 key: Vec<u8>,
53 /// The machine's signature of [`challenge_bytes`].
54 signature: Vec<u8>,
55 },
56 /// The [`Outcome`], sealed to the account.
57 Reply {
58 /// The sealed outcome.
59 sealed: Vec<u8>,
60 /// The machine's signature of [`reply_bytes`]: anyone can seal to
61 /// the account's public keys, only the machine signs.
62 signature: Vec<u8>,
63 },
64 /// The command was refused before it ran: no challenge, a used one, one
65 /// that expired, or one that did not open or was not signed by the
66 /// account.
67 Refused {
68 /// Why.
69 reason: String,
70 },
71}
72
73/// What the device asks.
74#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
75#[serde(tag = "type", rename_all = "snake_case")]
76pub enum Command {
77 /// The machine's sessions (#78).
78 List,
79 /// Registers session `name` and starts it; the link serves it (#78).
80 New {
81 /// The session's name.
82 name: String,
83 },
84 /// Ends session `name`, as `teter kill` does on the machine.
85 Kill {
86 /// The session's name.
87 name: String,
88 },
89}
90
91/// A command and the account's signature of [`command_bytes`].
92#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
93pub struct Signed {
94 /// The command.
95 pub command: Command,
96 /// The signature.
97 pub signature: Vec<u8>,
98}
99
100/// How a command went.
101#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
102#[serde(tag = "type", rename_all = "snake_case")]
103pub enum Outcome {
104 /// It ran.
105 Done,
106 /// The answer to [`Command::List`].
107 Sessions {
108 /// The sessions, by name.
109 sessions: Vec<SessionInfo>,
110 },
111 /// It ran and failed.
112 Failed {
113 /// Why.
114 reason: String,
115 },
116}
117
118/// One of the machine's sessions, as [`Command::List`] gives it.
119#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
120pub struct SessionInfo {
121 /// Its name.
122 pub name: String,
123 /// Its opaque ID: the channel it is served on when it runs, else its
124 /// registry ID; empty for one running unregistered that holds no
125 /// channel yet.
126 pub id: String,
127 /// Whether its session process runs now.
128 pub running: bool,
129 /// Its terminal's size, columns and rows, when it runs (#135); `None`
130 /// from a machine older than the field.
131 #[serde(default)]
132 pub size: Option<(u16, u16)>,
133}
134
135/// What the machine signs in a challenge.
136pub fn challenge_bytes(machine: &str, nonce: &[u8], key: &[u8]) -> Vec<u8> {
137 let mut bytes = b"teter-challenge-v1 ".to_vec();
138 bytes.extend_from_slice(machine.as_bytes());
139 bytes.push(0);
140 bytes.extend_from_slice(nonce);
141 bytes.extend_from_slice(key);
142 bytes
143}
144
145/// What the account signs: the command, bound to the machine and the
146/// challenge's nonce.
147pub fn command_bytes(machine: &str, nonce: &[u8], command: &Command) -> Vec<u8> {
148 let mut bytes = b"teter-command-v1 ".to_vec();
149 bytes.extend_from_slice(machine.as_bytes());
150 bytes.push(0);
151 bytes.extend_from_slice(nonce);
152 // A command always serializes.
153 bytes.extend_from_slice(&serde_json::to_vec(command).unwrap_or_default());
154 bytes
155}
156
157/// What the machine signs in a reply: the sealed outcome, bound to the
158/// machine and the challenge's nonce.
159pub fn reply_bytes(machine: &str, nonce: &[u8], sealed: &[u8]) -> Vec<u8> {
160 let mut bytes = b"teter-reply-v1 ".to_vec();
161 bytes.extend_from_slice(machine.as_bytes());
162 bytes.push(0);
163 bytes.extend_from_slice(nonce);
164 bytes.extend_from_slice(sealed);
165 bytes
166}
167
168/// What a sealed command is bound to.
169pub fn command_info(machine: &str, nonce: &[u8]) -> Vec<u8> {
170 let mut info = b"command ".to_vec();
171 info.extend_from_slice(machine.as_bytes());
172 info.push(b' ');
173 info.extend_from_slice(crate::account::hex(nonce).as_bytes());
174 info
175}
176
177/// What a sealed outcome is bound to.
178pub fn reply_info(machine: &str, nonce: &[u8]) -> Vec<u8> {
179 let mut info = b"command reply ".to_vec();
180 info.extend_from_slice(machine.as_bytes());
181 info.push(b' ');
182 info.extend_from_slice(crate::account::hex(nonce).as_bytes());
183 info
184}