use indicatrix_net::messages::PeerRole;
use std::path::{Path, PathBuf};
pub const WORKER_CN_PREFIX: &str = "worker:";
pub const VIEWER_ALLOWLIST_FILE: &str = "allowlist-viewers.txt";
pub const WORKER_ALLOWLIST_FILE: &str = "allowlist-workers.txt";
pub const LEGACY_ALLOWLIST_FILE: &str = "allowlist.txt";
#[must_use]
pub fn common_name_for(role: PeerRole, label: &str) -> Option<String> {
match role {
PeerRole::Viewer => (!label.starts_with(WORKER_CN_PREFIX)).then(|| label.to_string()),
PeerRole::Worker => Some(if label.starts_with(WORKER_CN_PREFIX) {
label.to_string()
} else {
format!("{WORKER_CN_PREFIX}{label}")
}),
}
}
#[must_use]
pub fn role_of_common_name(common_name: Option<&str>) -> PeerRole {
if common_name.is_some_and(|cn| cn.starts_with(WORKER_CN_PREFIX)) {
PeerRole::Worker
} else {
PeerRole::Viewer
}
}
pub fn role_of_certificate(der: &[u8]) -> Result<PeerRole, String> {
subject_common_name(der)
.map(|cn| role_of_common_name(cn.as_deref()))
.map_err(|e| format!("could not read the client certificate's subject: {e}"))
}
#[must_use]
pub fn worker_label_of_common_name(common_name: &str) -> Option<&str> {
common_name
.strip_prefix(WORKER_CN_PREFIX)
.filter(|label| !label.is_empty())
}
#[must_use]
pub fn worker_label_of_certificate(der: &[u8]) -> Option<String> {
let common_name = subject_common_name(der).ok().flatten()?;
worker_label_of_common_name(&common_name).map(str::to_string)
}
#[must_use]
pub const fn role_name(role: PeerRole) -> &'static str {
match role {
PeerRole::Viewer => "viewer",
PeerRole::Worker => "worker",
}
}
#[must_use]
pub fn viewer_allowlist_in(dir: &Path) -> PathBuf {
let current = dir.join(VIEWER_ALLOWLIST_FILE);
let legacy = dir.join(LEGACY_ALLOWLIST_FILE);
if !current.exists() && legacy.exists() {
legacy
} else {
current
}
}
#[must_use]
pub fn worker_allowlist_in(dir: &Path) -> PathBuf {
dir.join(WORKER_ALLOWLIST_FILE)
}
#[must_use]
pub fn allowlist_in(dir: &Path, role: PeerRole) -> PathBuf {
match role {
PeerRole::Viewer => viewer_allowlist_in(dir),
PeerRole::Worker => worker_allowlist_in(dir),
}
}
#[must_use]
pub fn pki_dir_of(ca_path: &Path) -> PathBuf {
ca_path
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."))
.to_path_buf()
}
const TAG_SEQUENCE: u8 = 0x30;
const TAG_SET: u8 = 0x31;
const TAG_OID: u8 = 0x06;
const TAG_EXPLICIT_VERSION: u8 = 0xA0;
const OID_COMMON_NAME: &[u8] = &[0x55, 0x04, 0x03];
fn take_tlv(input: &[u8]) -> Result<(u8, &[u8], &[u8]), &'static str> {
let (&tag, after_tag) = input.split_first().ok_or("truncated tag")?;
let (&first_len, after_len) = after_tag.split_first().ok_or("truncated length")?;
let (len, body) = if first_len < 0x80 {
(usize::from(first_len), after_len)
} else {
let count = usize::from(first_len & 0x7F);
if count == 0 || count > 4 || after_len.len() < count {
return Err("unsupported length encoding");
}
let len = after_len[..count]
.iter()
.fold(0_usize, |acc, &b| (acc << 8) | usize::from(b));
(len, &after_len[count..])
};
if body.len() < len {
return Err("length runs past the end of the certificate");
}
Ok((tag, &body[..len], &body[len..]))
}
fn take_expected(input: &[u8], expected: u8) -> Result<(&[u8], &[u8]), &'static str> {
let (tag, content, rest) = take_tlv(input)?;
if tag == expected {
Ok((content, rest))
} else {
Err("unexpected DER tag")
}
}
fn decode_string(tag: u8, bytes: &[u8]) -> Result<String, &'static str> {
match tag {
0x0C | 0x13 | 0x16 | 0x14 => std::str::from_utf8(bytes)
.map(str::to_string)
.map_err(|_| "Common Name is not valid UTF-8"),
0x1E => {
if bytes.len() & 1 == 1 {
return Err("odd-length BMPString");
}
let units: Vec<u16> = (0..bytes.len() / 2)
.map(|i| u16::from_be_bytes([bytes[2 * i], bytes[2 * i + 1]]))
.collect();
String::from_utf16(&units).map_err(|_| "invalid BMPString")
}
_ => Err("unsupported Common Name string type"),
}
}
pub fn subject_common_name(der: &[u8]) -> Result<Option<String>, &'static str> {
let (certificate, _) = take_expected(der, TAG_SEQUENCE)?;
let (mut tbs, _) = take_expected(certificate, TAG_SEQUENCE)?;
if tbs.first() == Some(&TAG_EXPLICIT_VERSION) {
tbs = take_tlv(tbs)?.2;
}
for _ in 0..4 {
tbs = take_tlv(tbs)?.2;
}
let (mut subject, _) = take_expected(tbs, TAG_SEQUENCE)?;
while !subject.is_empty() {
let (mut set, rest) = take_expected(subject, TAG_SET)?;
subject = rest;
while !set.is_empty() {
let (attribute, rest) = take_expected(set, TAG_SEQUENCE)?;
set = rest;
let (oid, value) = take_expected(attribute, TAG_OID)?;
if oid == OID_COMMON_NAME {
let (tag, bytes, _) = take_tlv(value)?;
return decode_string(tag, bytes).map(Some);
}
}
}
Ok(None)
}
#[cfg(test)]
mod tests {
use super::*;
fn cert_with_cn(cn: Option<&str>) -> Vec<u8> {
let mut params = rcgen::CertificateParams::new(Vec::<String>::new()).unwrap();
params.distinguished_name = rcgen::DistinguishedName::new();
if let Some(cn) = cn {
params
.distinguished_name
.push(rcgen::DnType::CommonName, cn);
}
params
.distinguished_name
.push(rcgen::DnType::OrganizationName, "indicatrix");
let key = rcgen::KeyPair::generate().unwrap();
params.self_signed(&key).unwrap().der().to_vec()
}
#[test]
fn subject_common_name_reads_the_subject_of_a_real_certificate() {
let der = cert_with_cn(Some("worker:render-box"));
assert_eq!(
subject_common_name(&der).unwrap().as_deref(),
Some("worker:render-box")
);
assert_eq!(role_of_certificate(&der).unwrap(), PeerRole::Worker);
assert_eq!(
worker_label_of_certificate(&der).as_deref(),
Some("render-box")
);
let viewer = cert_with_cn(Some("laptop"));
assert_eq!(role_of_certificate(&viewer).unwrap(), PeerRole::Viewer);
assert_eq!(worker_label_of_certificate(&viewer), None);
assert_eq!(worker_label_of_common_name("worker:"), None);
let no_cn = cert_with_cn(None);
assert_eq!(subject_common_name(&no_cn).unwrap(), None);
assert_eq!(role_of_certificate(&no_cn).unwrap(), PeerRole::Viewer);
}
#[test]
fn subject_common_name_rejects_garbage_without_panicking() {
assert!(subject_common_name(&[]).is_err());
assert!(subject_common_name(&[0x30, 0x82, 0xFF]).is_err());
let der = cert_with_cn(Some("laptop"));
assert!(subject_common_name(&der[..der.len() / 3]).is_err());
}
#[test]
fn common_names_encode_and_decode_the_role() {
assert_eq!(
common_name_for(PeerRole::Viewer, "laptop").as_deref(),
Some("laptop")
);
assert_eq!(common_name_for(PeerRole::Viewer, "worker:sneaky"), None);
assert_eq!(
common_name_for(PeerRole::Worker, "gpu-box").as_deref(),
Some("worker:gpu-box")
);
assert_eq!(
common_name_for(PeerRole::Worker, "worker:gpu-box").as_deref(),
Some("worker:gpu-box")
);
assert_eq!(role_of_common_name(Some("worker:x")), PeerRole::Worker);
assert_eq!(role_of_common_name(Some("Worker:x")), PeerRole::Viewer);
assert_eq!(role_of_common_name(None), PeerRole::Viewer);
}
#[test]
fn the_legacy_allowlist_serves_viewers_until_the_new_file_exists() {
let dir = std::env::temp_dir().join(format!(
"indicatrix-worker-role-test-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::create_dir_all(&dir).unwrap();
assert_eq!(viewer_allowlist_in(&dir), dir.join(VIEWER_ALLOWLIST_FILE));
std::fs::write(dir.join(LEGACY_ALLOWLIST_FILE), "").unwrap();
assert_eq!(viewer_allowlist_in(&dir), dir.join(LEGACY_ALLOWLIST_FILE));
std::fs::write(dir.join(VIEWER_ALLOWLIST_FILE), "").unwrap();
assert_eq!(viewer_allowlist_in(&dir), dir.join(VIEWER_ALLOWLIST_FILE));
assert_eq!(
allowlist_in(&dir, PeerRole::Worker),
dir.join(WORKER_ALLOWLIST_FILE)
);
std::fs::remove_dir_all(&dir).ok();
}
}