use std::fmt;
use std::net::SocketAddr;
use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use serde::{Deserialize, Serialize};
use crate::invite::{decode, encode, is_relay_url};
use crate::portable::{
DevicePublicKey, PortableIdentityState, PortableUserId, UserDeviceCredential,
};
pub const PAIR_ALPN: &[u8] = b"spoolframe/pair/1";
pub const CODE_LEN: usize = 32;
pub const CODE_TTL_SECS: u64 = 5 * 60;
pub const MAX_LINK: usize = 1024;
pub const MAX_ADDRS: usize = 8;
pub const MAX_RELAYS: usize = 2;
pub const MAX_NAME: usize = 32;
pub const MAX_FRAME: u32 = 256 << 10;
const LINK_PREFIX: &str = "spoolframe://pair?";
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct PairingCode(#[serde(with = "serde_bytes")] [u8; CODE_LEN]);
impl PairingCode {
pub fn from_bytes(bytes: [u8; CODE_LEN]) -> Self {
Self(bytes)
}
pub fn as_bytes(&self) -> &[u8; CODE_LEN] {
&self.0
}
pub fn matches(&self, other: &PairingCode) -> bool {
let diff = self
.0
.iter()
.zip(other.0.iter())
.fold(0u8, |acc, (a, b)| acc | (a ^ b));
std::hint::black_box(diff) == 0
}
pub fn to_text(&self) -> String {
URL_SAFE_NO_PAD.encode(self.0)
}
pub fn from_text(text: &str) -> Option<Self> {
if text.len() != 43 {
return None;
}
let bytes = URL_SAFE_NO_PAD.decode(text).ok()?;
Some(Self(bytes.try_into().ok()?))
}
}
impl fmt::Debug for PairingCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("PairingCode(<redacted>)")
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct PairingLink {
pub endpoint: [u8; 32],
pub code: PairingCode,
pub name: String,
pub addrs: Vec<SocketAddr>,
pub relays: Vec<String>,
}
impl fmt::Debug for PairingLink {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PairingLink")
.field("endpoint", &hex(&self.endpoint))
.field("name", &self.name)
.field("addrs", &self.addrs)
.field("relays", &self.relays)
.finish_non_exhaustive()
}
}
impl PairingLink {
pub fn link(&self) -> String {
let mut link = format!(
"{LINK_PREFIX}e={}&c={}",
hex(&self.endpoint),
self.code.to_text()
);
let mut add = |key: &str, value: &str| {
link.push('&');
link.push_str(key);
link.push('=');
link.push_str(&encode(value));
};
add("n", &cut(&self.name));
for addr in self.addrs.iter().take(MAX_ADDRS) {
add("a", &addr.to_string());
}
for relay in self.relays.iter().take(MAX_RELAYS) {
add("r", relay);
}
link
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum LinkError {
#[error("not a Spoolframe pairing code")]
NotALink,
#[error("the pairing code is longer than any device shows")]
TooLong,
#[error("the pairing code is damaged: {0}")]
Damaged(&'static str),
}
pub fn parse_link(text: &str) -> Result<PairingLink, LinkError> {
let text = text.trim();
if text.len() > MAX_LINK {
return Err(LinkError::TooLong);
}
let query = text.strip_prefix(LINK_PREFIX).ok_or(LinkError::NotALink)?;
let mut endpoint = None;
let mut code = None;
let mut name = String::new();
let mut addrs = Vec::new();
let mut relays = Vec::new();
for pair in query.split('&').filter(|p| !p.is_empty()) {
let (key, raw) = pair.split_once('=').unwrap_or((pair, ""));
match key {
"e" => {
if endpoint.is_some() {
return Err(LinkError::Damaged("two endpoints"));
}
endpoint = Some(unhex(raw).ok_or(LinkError::Damaged("not an endpoint id"))?);
}
"c" => {
if code.is_some() {
return Err(LinkError::Damaged("two codes"));
}
code = Some(PairingCode::from_text(raw).ok_or(LinkError::Damaged("not a code"))?);
}
"n" => name = cut(&decode(raw).ok_or(LinkError::Damaged("the name"))?),
"a" if addrs.len() < MAX_ADDRS => {
if let Some(addr) = decode(raw).and_then(|a| a.parse().ok()) {
addrs.push(addr);
}
}
"r" if relays.len() < MAX_RELAYS => {
if let Some(relay) = decode(raw).filter(|r| is_relay_url(r)) {
relays.push(relay);
}
}
_ => {}
}
}
Ok(PairingLink {
endpoint: endpoint.ok_or(LinkError::Damaged("no endpoint id"))?,
code: code.ok_or(LinkError::Damaged("no code"))?,
name,
addrs,
relays,
})
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum PhoneFrame {
Hello {
code: PairingCode,
identity: PortableUserId,
#[serde(default, skip_serializing_if = "Option::is_none")]
name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
phone_name: Option<String>,
},
Credential {
credential: Box<UserDeviceCredential>,
state: Option<Box<PortableIdentityState>>,
servers: Vec<PairedServer>,
},
Declined,
}
pub const MAX_PAIRED_SERVERS: usize = 16;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PairedServer {
pub endpoint_id: String,
pub server_id: String,
pub name: String,
#[serde(with = "crate::messages::socket_addrs_as_text")]
pub addrs: Vec<SocketAddr>,
pub relays: Vec<String>,
}
impl PairedServer {
pub fn within_bounds(mut self) -> Option<Self> {
let key = &self.endpoint_id;
if key.len() != 64 || !key.bytes().all(|b| b.is_ascii_hexdigit()) {
return None;
}
self.endpoint_id = key.to_ascii_lowercase();
if self.server_id.is_empty() || self.server_id.len() > 128 {
return None;
}
crate::messages::cut(&mut self.name);
self.addrs.truncate(MAX_ADDRS);
self.relays.retain(|r| is_relay_url(r));
self.relays.truncate(MAX_RELAYS);
Some(self)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum DeviceFrame {
Key {
device_key: DevicePublicKey,
name: String,
},
Done,
Refused { reason: Refusal },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, thiserror::Error)]
#[serde(rename_all = "snake_case")]
pub enum Refusal {
#[error("this device does not allow sign-ins from other people's accounts")]
NotAllowed,
#[error("the pairing code does not match")]
WrongCode,
#[error("the pairing code has expired")]
Expired,
#[error("the device did not accept the credential")]
CredentialRefused,
#[error("the device could not finish pairing")]
Failed,
#[error("the pairing was turned down on the device")]
Rejected,
#[serde(other)]
#[error("the device refused the pairing")]
Unknown,
}
fn cut(name: &str) -> String {
name.chars().take(MAX_NAME).collect()
}
pub fn cut_name(name: &str) -> String {
cut(name)
}
fn hex(bytes: &[u8; 32]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
fn unhex(text: &str) -> Option<[u8; 32]> {
if text.len() != 64 || !text.is_ascii() {
return None;
}
let mut bytes = [0u8; 32];
for (i, byte) in bytes.iter_mut().enumerate() {
*byte = u8::from_str_radix(&text[2 * i..2 * i + 2], 16).ok()?;
}
Some(bytes)
}
pub const RELAY_CODE_LEN: usize = 10;
pub const RELAY_TTL_SECS: u64 = 5 * 60;
const CROCKFORD: &[u8; 32] = b"0123456789ABCDEFGHJKMNPQRSTVWXYZ";
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct RelayCode(String);
impl RelayCode {
pub fn from_random(bytes: [u8; RELAY_CODE_LEN]) -> Self {
Self(
bytes
.iter()
.map(|b| CROCKFORD[usize::from(b % 32)] as char)
.collect(),
)
}
pub fn parse(text: &str) -> Option<Self> {
let code: String = text
.chars()
.filter(|c| !c.is_whitespace() && *c != '-')
.map(|c| match c.to_ascii_uppercase() {
'I' | 'L' => '1',
'O' => '0',
c => c,
})
.collect();
(code.len() == RELAY_CODE_LEN && code.bytes().all(|b| CROCKFORD.contains(&b)))
.then_some(Self(code))
}
pub fn display(&self) -> String {
format!("{}-{}", &self.0[..5], &self.0[5..])
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Debug for RelayCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("RelayCode(<redacted>)")
}
}
impl TryFrom<String> for RelayCode {
type Error = &'static str;
fn try_from(text: String) -> Result<Self, Self::Error> {
Self::parse(&text).ok_or("a relay code is 10 characters of Crockford's base32")
}
}
impl From<RelayCode> for String {
fn from(code: RelayCode) -> Self {
code.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PairingOffer {
pub code: RelayCode,
pub device_key: DevicePublicKey,
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PairingAnswer {
pub credential: Box<UserDeviceCredential>,
pub state: Option<Box<PortableIdentityState>>,
pub servers: Vec<PairedServer>,
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> PairingLink {
PairingLink {
endpoint: [0xab; 32],
code: PairingCode::from_bytes([7; 32]),
name: "Living room TV & co".into(),
addrs: vec![
"192.168.1.20:51234".parse().unwrap(),
"[fe80::1]:51234".parse().unwrap(),
],
relays: vec!["https://relay.example.org/".into()],
}
}
#[test]
fn a_link_reads_back_as_written() {
let link = sample();
let text = link.link();
assert!(text.starts_with("spoolframe://pair?e=abab"), "{text}");
assert_eq!(parse_link(&text).unwrap(), link);
}
#[test]
fn a_link_at_every_limit_fits() {
let link = PairingLink {
name: "名".repeat(200),
addrs: (0..20)
.map(|n| {
format!("[fd12:3456:789a:1:2:3:4:{n:x}]:65535")
.parse()
.unwrap()
})
.collect(),
relays: (0..5)
.map(|n| format!("https://relay-{n}.some-long-domain-name.example.org/"))
.collect(),
..sample()
};
let text = link.link();
assert!(text.len() <= MAX_LINK, "{} bytes", text.len());
let read = parse_link(&text).unwrap();
assert_eq!(read.name.chars().count(), MAX_NAME);
assert_eq!(
(read.addrs.len(), read.relays.len()),
(MAX_ADDRS, MAX_RELAYS)
);
}
#[test]
fn what_is_not_a_link_is_refused() {
let good = sample().link();
assert_eq!(parse_link("https://example.org"), Err(LinkError::NotALink));
assert_eq!(
parse_link(&good.replace("spoolframe://pair?", "spoolframe://invite?")),
Err(LinkError::NotALink)
);
assert_eq!(
parse_link(&format!("{good}&x={}", "y".repeat(MAX_LINK))),
Err(LinkError::TooLong)
);
let code = PairingCode::from_bytes([7; 32]).to_text();
for (damaged, why) in [
(good.replace(&code, ""), "not a code"),
(good.replace(&code, &code[..42]), "not a code"),
(good.replace(&format!("c={code}"), "x=1"), "no code"),
(good.replace("e=abab", "e=zzab"), "not an endpoint id"),
(
good.replace(&format!("e={}", "ab".repeat(32)), ""),
"no endpoint id",
),
(format!("{good}&c={code}"), "two codes"),
(format!("{good}&e={}", "cd".repeat(32)), "two endpoints"),
(format!("{good}&n=%ZZ"), "the name"),
] {
assert_eq!(
parse_link(&damaged),
Err(LinkError::Damaged(why)),
"{damaged}"
);
}
}
#[test]
fn unknown_parameters_and_unreadable_hints_are_ignored() {
let text = format!(
"{}&v=2&a=not-an-address&r=ftp%3A%2F%2Fnope",
sample().link()
);
assert_eq!(parse_link(&text).unwrap(), sample());
}
#[test]
fn the_code_is_kept_out_of_debug() {
let link = sample();
let shown = format!("{link:?} {:?}", link.code);
assert!(!shown.contains(&link.code.to_text()), "{shown}");
assert!(!shown.contains("7, 7"), "{shown}");
}
#[test]
fn codes_match_only_themselves() {
let code = PairingCode::from_bytes([7; 32]);
assert!(code.matches(&PairingCode::from_bytes([7; 32])));
let mut last = [7; 32];
last[31] = 8;
assert!(!code.matches(&PairingCode::from_bytes(last)));
let mut first = [7; 32];
first[0] = 0;
assert!(!code.matches(&PairingCode::from_bytes(first)));
}
#[test]
fn mangled_links_never_panic_and_what_reads_round_trips() {
let base = sample().link().into_bytes();
let mut state = 0x9e37_79b9_7f4a_7c15u64;
let mut next = move || {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
state
};
for _ in 0..20_000 {
let mut bytes = base.clone();
for _ in 0..(next() % 6 + 1) {
let at = (next() as usize) % bytes.len();
match next() % 3 {
0 => bytes[at] = next() as u8,
1 => {
bytes.remove(at);
}
_ => bytes.insert(at, b"&=%:[]"[(next() % 6) as usize]),
}
if bytes.is_empty() {
break;
}
}
let Ok(text) = std::str::from_utf8(&bytes) else {
continue;
};
if let Ok(link) = parse_link(text) {
assert_eq!(parse_link(&link.link()), Ok(link));
}
}
}
#[test]
fn a_hello_carries_the_guest_s_name_or_none() {
let identity = PortableUserId::from_bytes(
ed25519_dalek::SigningKey::from_bytes(&[7; 32])
.verifying_key()
.to_bytes(),
)
.unwrap();
let hello = PhoneFrame::Hello {
code: PairingCode::from_bytes([7; 32]),
identity,
name: Some("Steve".into()),
phone_name: Some("Steve's iPhone".into()),
};
let read: PhoneFrame =
crate::codec::decode(&crate::codec::encode(&hello).unwrap()).unwrap();
assert_eq!(read, hello);
assert_eq!(serde_json::to_value(&hello).unwrap()["name"], "Steve");
let cbor = crate::codec::encode(&hello).unwrap();
let mut value: ciborium::Value = ciborium::from_reader(cbor.as_slice()).unwrap();
let ciborium::Value::Map(fields) = &mut value else {
panic!("a map");
};
fields.retain(|(k, _)| !matches!(k.as_text(), Some("name" | "phone_name")));
let old: PhoneFrame = crate::codec::decode(&crate::codec::encode(&value).unwrap()).unwrap();
assert!(
matches!(
old,
PhoneFrame::Hello {
name: None,
phone_name: None,
..
}
),
"{old:?}"
);
}
#[test]
fn a_rejection_is_its_own_reason() {
let rejected = DeviceFrame::Refused {
reason: Refusal::Rejected,
};
assert_eq!(
serde_json::to_string(&rejected).unwrap(),
r#"{"type":"refused","reason":"rejected"}"#
);
let read: DeviceFrame =
crate::codec::decode(&crate::codec::encode(&rejected).unwrap()).unwrap();
assert_eq!(read, rejected);
let newer: Refusal = serde_json::from_str(r#""too_tired""#).unwrap();
assert_eq!(newer, Refusal::Unknown);
}
#[test]
fn frames_are_tagged_by_type() {
let declined = crate::codec::encode(&PhoneFrame::Declined).unwrap();
let read: PhoneFrame = crate::codec::decode(&declined).unwrap();
assert_eq!(read, PhoneFrame::Declined);
let refused = DeviceFrame::Refused {
reason: Refusal::NotAllowed,
};
let json = serde_json::to_string(&refused).unwrap();
assert_eq!(json, r#"{"type":"refused","reason":"not_allowed"}"#);
}
fn credential_frame(servers: Vec<PairedServer>) -> PhoneFrame {
use ed25519_dalek::{Signer, SigningKey};
let root = SigningKey::from_bytes(&[7; 32]);
let device = SigningKey::from_bytes(&[9; 32]);
let credential = UserDeviceCredential::new(
PortableUserId::from_bytes(root.verifying_key().to_bytes()).unwrap(),
DevicePublicKey::from_bytes(device.verifying_key().to_bytes()).unwrap(),
1,
1_791_504_000,
1_791_504_000 + 3600,
|m| root.sign(m),
);
PhoneFrame::Credential {
credential: Box::new(credential),
state: None,
servers,
}
}
fn home() -> PairedServer {
PairedServer {
endpoint_id: "ab".repeat(32),
server_id: "q7k3mpa9trdwf2h".into(),
name: "Home Media".into(),
addrs: vec![
"192.168.1.10:7420".parse().unwrap(),
"[2001:db8::1]:7420".parse().unwrap(),
],
relays: vec!["https://relay.example.org/".into()],
}
}
#[test]
fn a_credential_frame_carries_its_servers() {
let frame = credential_frame(vec![home()]);
let read: PhoneFrame =
crate::codec::decode(&crate::codec::encode(&frame).unwrap()).unwrap();
assert_eq!(read, frame);
let json = serde_json::to_value(&frame).unwrap();
assert_eq!(json["servers"][0]["addrs"][1], "[2001:db8::1]:7420");
}
#[test]
fn a_credential_frame_without_servers_is_refused() {
let mut json = serde_json::to_value(credential_frame(vec![home()])).unwrap();
json.as_object_mut().unwrap().remove("servers");
let error = serde_json::from_value::<PhoneFrame>(json).unwrap_err();
assert!(
error.to_string().contains("missing field `servers`"),
"{error}"
);
}
#[test]
fn a_paired_server_is_held_to_bounds() {
assert_eq!(home().within_bounds(), Some(home()));
let loud = PairedServer {
endpoint_id: "AB".repeat(32),
name: "x".repeat(500),
addrs: (0..20)
.map(|n| format!("10.0.0.{n}:7420").parse().unwrap())
.collect(),
relays: vec![
"ftp://nope".into(),
"https://a.example/".into(),
"https://b.example/".into(),
"https://c.example/".into(),
],
..home()
};
let held = loud.within_bounds().unwrap();
assert_eq!(held.endpoint_id, "ab".repeat(32));
assert_eq!(held.name.chars().count(), crate::MAX_HELLO_TEXT);
assert_eq!(held.addrs.len(), MAX_ADDRS);
assert_eq!(held.relays, ["https://a.example/", "https://b.example/"]);
for bad in [
PairedServer {
endpoint_id: "not a key".into(),
..home()
},
PairedServer {
server_id: String::new(),
..home()
},
] {
assert_eq!(bad.within_bounds(), None);
}
}
}