diff --git a/CHANGELOG.md b/CHANGELOG.md index 6eb8c38..70c1a8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ The version lives in `CERBERUS_VERSION` in `src/cerberus.c` (`MAJOR.MINOR.PATCH` published only as the [`latest`](https://github.com/OpenIPC/cerberus/releases/latest) release (see the README); this file is the change history, newest first. +## 0.3.2 — 2026-10-28 + +- `cerberus status` prints the device's public key and its fingerprint — the same eight groups the + cloud's panel shows on the device page, so the two can be compared by eye. The fingerprint is + SHA-256 of the raw key, first 16 bytes in hex: `echo | base64 -d | sha256sum` checks it. +- The public key is kept in the store as `cerberus_device_pubkey` (written once, at the first start + that derives it), because `status` must not need randomness and deriving the key does. It is + bound to the private key it came from, so a key replaced by hand shows no fingerprint rather than + the old one; and a store with no room for it costs a warning, never the camera's start. + ## 0.3.1 — 2026-10-17 One release, `latest` (#32), and a version check (#33). diff --git a/README.md b/README.md index 09b0583..2010387 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ getrandom(2) first). cerberus run # enroll + up + watch (the default; what S99cerberus starts) cerberus enroll # enroll only, idempotent cerberus up # bring the interface up from the stored material -cerberus status # what is stored, handshake age, whether the tunnel answers +cerberus status # what is stored, the key fingerprint, handshake age, whether the tunnel answers cerberus keygen # print the keypair (stdout is data, diagnostics go to stderr) cerberus clean # remove the agent: interface, init script, stored material, the binary cerberus motion # tell the running agent there is motion, as majestic's hook does diff --git a/README.ru.md b/README.ru.md index 5d0fe3e..e906077 100644 --- a/README.ru.md +++ b/README.ru.md @@ -154,7 +154,7 @@ fw_setenv cerberus_enrollment_token tok_... cerberus run # enroll + up + watch (по умолчанию; это запускает S99cerberus) cerberus enroll # только энролл, идемпотентно cerberus up # поднять интерфейс из сохранённого -cerberus status # что сохранено, возраст хендшейка, отвечает ли туннель +cerberus status # что сохранено, отпечаток ключа, возраст хендшейка, отвечает ли туннель cerberus keygen # напечатать пару ключей (stdout — данные, диагностика в stderr) cerberus clean # убрать агента: интерфейс, автозапуск, сохранённое и сам бинарь cerberus motion # сказать работающему агенту «движение», как это делает хук majestic diff --git a/README.zh-CN.md b/README.zh-CN.md index 0caa2be..45685ac 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -141,7 +141,7 @@ fw_setenv cerberus_enrollment_token tok_... cerberus run # enroll + up + watch(默认;S99cerberus 启动的就是它) cerberus enroll # 仅注册,幂等 cerberus up # 用已保存的材料启动接口 -cerberus status # 已保存的内容、握手时间、隧道是否有响应 +cerberus status # 已保存的内容、密钥指纹、握手时间、隧道是否有响应 cerberus keygen # 打印密钥对(stdout 为数据,诊断信息输出到 stderr) cerberus clean # 移除代理:接口、启动脚本、已保存的材料以及二进制文件本身 cerberus motion # 像 majestic 的钩子一样,通知正在运行的代理“有移动” diff --git a/install.sh b/install.sh index 97fdedf..8df220f 100755 --- a/install.sh +++ b/install.sh @@ -10,7 +10,7 @@ INIT=/etc/init.d/S99cerberus CONF=/etc/cerberus HOOK=/usr/sbin/motion.sh KEYS="node_id overlay_ip hub_endpoint hub_pubkey allowed_ips network_subnet hub_ip - claim_code device_key api enrollment_token hostname kind iface subnet ca heartbeat + claim_code device_key device_pubkey api enrollment_token hostname kind iface subnet ca heartbeat ping_target ping_interval ping_timeout ping_fails events snapshot_url" say() { echo "cerberus: $*"; } diff --git a/src/cerberus.c b/src/cerberus.c index fb0ba41..430dfd4 100644 --- a/src/cerberus.c +++ b/src/cerberus.c @@ -70,7 +70,7 @@ /* The version of every build, release builds included (#32); `make VERSION=...` overrides it for a test build. */ #ifndef CERBERUS_VERSION -#define CERBERUS_VERSION "0.3.1" +#define CERBERUS_VERSION "0.3.2" #endif /* Where the errors are decoded. Printed after every fatal one: the reader is on a camera @@ -707,8 +707,13 @@ static int store_get(const char *key, char *out, size_t out_sz) return 0; } -/* Sets `key`, or removes it when `value` is NULL. */ -static void store_put(const char *key, const char *value) +/* + * Sets `key`, or removes it when `value` is NULL. A failure is fatal, except with `soft`: then it is a + * warning and -1. Soft is for what the agent can live without — a cache — where stopping a camera + * that has everything it needs, because a full U-Boot environment had no room for one more variable, + * would cost far more than the value is worth. + */ +static int store_put(const char *key, const char *value, int soft) { const char *path; char tmp[512], line[1024]; @@ -717,20 +722,28 @@ static void store_put(const char *key, const char *value) if (store_is_uboot()) { char *argv[] = { "fw_setenv", (char *)key, (char *)value, NULL }; /* no value = unset */ - if (run(argv, NULL, 0) != 0 && value) - die("fw_setenv %s failed — is /etc/fw_env.config right?", key); - return; + if (run(argv, NULL, 0) != 0 && value) { + if (!soft) die("fw_setenv %s failed — is /etc/fw_env.config right?", key); + warn_("fw_setenv %s failed — carrying on without it", key); + return -1; + } + return 0; } /* Rewrite the file with this key replaced. Small and rare enough that a full rewrite beats * carrying an in-memory model of it. */ path = store_file(); in = fopen(path, "r"); - if (!in && !value) return; + if (!in && !value) return 0; mkparent(path); snprintf(tmp, sizeof tmp, "%s.tmp", path); out = fopen(tmp, "w"); - if (!out) die("cannot write %s: %s", tmp, strerror(errno)); + if (!out) { + if (!soft) die("cannot write %s: %s", tmp, strerror(errno)); + warn_("cannot write %s: %s — carrying on without %s", tmp, strerror(errno), key); + if (in) fclose(in); + return -1; + } if (in) { while (fgets(line, sizeof line, in)) { if (!strncmp(line, key, klen) && line[klen] == '=') continue; @@ -741,11 +754,18 @@ static void store_put(const char *key, const char *value) if (value) fprintf(out, "%s=%s\n", key, value); fclose(out); if (chmod(tmp, 0600) != 0) warn_("chmod %s: %s", tmp, strerror(errno)); - if (rename(tmp, path) != 0) die("cannot replace %s: %s", path, strerror(errno)); + if (rename(tmp, path) != 0) { + if (!soft) die("cannot replace %s: %s", path, strerror(errno)); + warn_("cannot replace %s: %s — carrying on without %s", path, strerror(errno), key); + unlink(tmp); + return -1; + } + return 0; } -static void store_set(const char *key, const char *value) { store_put(key, value); } -static void store_unset(const char *key) { store_put(key, NULL); } +static void store_set(const char *key, const char *value) { store_put(key, value, 0); } +static int store_set_soft(const char *key, const char *value) { return store_put(key, value, 1); } +static void store_unset(const char *key) { store_put(key, NULL, 0); } /* * The agent's keys used to be called streq_*: streq_device_key, streq_node_id, ... @@ -1207,6 +1227,26 @@ static void json_put(char *out, size_t out_sz, size_t *n, const char *in, int es /* ------------------------------------------------------------------ device */ +/* + * What ties the cached public key (cerberus_device_pubkey, stored as ":") to the + * private key it was derived from: the first 8 bytes of SHA-256 of the raw private key, in hex. A + * private key replaced or removed by hand then leaves a cache that no longer matches, and `status` + * says the fingerprint is unknown instead of showing another key's as this camera's. Nothing secret + * leaks through it: it sits in the same store as the private key itself. + */ +static int key_binding(const char *priv_b64, char out[17]) +{ + unsigned char raw[32], d[32]; + size_t olen = 0; + int i; + + if (b64_decode(priv_b64, raw, sizeof raw, &olen) != 0 || olen != 32) return -1; + if (sha256_once(raw, sizeof raw, d) != 0) return -1; + for (i = 0; i < 8; i++) sprintf(out + 2 * i, "%02x", d[i]); + mbedtls_platform_zeroize(raw, sizeof raw); + return 0; +} + /* * The device private key, from the same store the join material lives in. * @@ -1224,7 +1264,7 @@ static void json_put(char *out, size_t out_sz, size_t *n, const char *in, int es static void device_key(char priv_b64[64], char pub_b64[64]) { unsigned char priv[32], pub[32]; - char stored[128], line[128]; + char stored[128], line[128], binding[17], cache[128]; size_t olen = 0; FILE *f; @@ -1253,6 +1293,39 @@ static void device_key(char priv_b64[64], char pub_b64[64]) if (x25519_pub(priv, pub) != 0) die("X25519 public key derivation failed"); b64(pub, sizeof pub, pub_b64, 64); + + /* The public half is kept next to the private one for `status`, which prints its fingerprint and + * must not need randomness — X25519 here does, for blinding. One write the first time (or after + * someone replaces the key by hand), none after: on a camera every write is a flash erase. And a + * soft one: it is a cache, and a U-Boot environment with no room left for it must not stop a + * camera that has everything else. */ + if (key_binding(priv_b64, binding) == 0) { + snprintf(cache, sizeof cache, "%s:%s", pub_b64, binding); + if (!store_get("cerberus_device_pubkey", stored, sizeof stored) || strcmp(stored, cache) != 0) + store_set_soft("cerberus_device_pubkey", cache); + } +} + +/* + * The fingerprint the cloud's panel shows next to the node, so the two can be compared by eye: SHA-256 + * of the 32 raw key bytes, the first 16 of it in lowercase hex, eight groups of four. No prefix or salt + * on purpose — `echo | base64 -d | sha256sum` checks it by hand on any machine. The format is + * held by this, the coordinator and the viewer together, each with the same test vector. + */ +static int key_fingerprint(const char *pub_b64, char out[40]) +{ + unsigned char raw[32], d[32]; + size_t olen = 0; + char *p = out; + int i; + + if (b64_decode(pub_b64, raw, sizeof raw, &olen) != 0 || olen != 32) return -1; + if (sha256_once(raw, sizeof raw, d) != 0) return -1; + for (i = 0; i < 16; i++) { + if (i > 0 && i % 2 == 0) *p++ = ' '; + p += sprintf(p, "%02x", d[i]); + } + return 0; } /* @@ -2278,11 +2351,29 @@ static int enroll_with_retry(void) static int cmd_status(void) { - char v[512]; + char v[512], fp[40]; long age; printf("store %s\n", store_is_uboot() ? "u-boot env (fw_setenv)" : store_file()); printf("node %s\n", store_get("cerberus_node_id", v, sizeof v) ? v : "(not enrolled)"); printf("device key %s\n", store_get("cerberus_device_key", v, sizeof v) ? "in the store" : "-"); + /* The cached public key counts only while it belongs to the private key in the store (see + * key_binding). No cache yet — a key from an older agent — or a key replaced by hand since: the + * next start fills it in. No private key at all: there is nothing to fingerprint. */ + if (!store_get("cerberus_device_key", v, sizeof v)) { + printf("fingerprint -\n"); + } else { + char binding[17], cache[128], *sep; + int ok = key_binding(v, binding) == 0 && + store_get("cerberus_device_pubkey", cache, sizeof cache) && + (sep = strchr(cache, ':')) != NULL && strcmp(sep + 1, binding) == 0 && + (*sep = 0, key_fingerprint(cache, fp) == 0); + if (ok) { + printf("public key %s\n", cache); + printf("fingerprint %s\n", fp); + } else { + printf("fingerprint - (known after the next start)\n"); + } + } printf("overlay ip %s\n", store_get("cerberus_overlay_ip", v, sizeof v) ? v : "-"); printf("hub endpoint %s\n", store_get("cerberus_hub_endpoint", v, sizeof v) ? v : "-"); printf("hub public key %s\n", store_get("cerberus_hub_pubkey", v, sizeof v) ? v : "-"); @@ -2490,7 +2581,7 @@ static int cmd_clean(void) /* Both spellings of each: whatever an older install left under streq_* goes too. */ static const char *suffixes[] = { "node_id", "overlay_ip", "hub_endpoint", "hub_pubkey", "allowed_ips", "network_subnet", - "hub_ip", "claim_code", "device_key", "api", "enrollment_token", "hostname", "kind", "iface", + "hub_ip", "claim_code", "device_key", "device_pubkey", "api", "enrollment_token", "hostname", "kind", "iface", "subnet", "ca", "heartbeat", "ping_target", "ping_interval", "ping_timeout", "ping_fails", "events", "snapshot_url", NULL diff --git a/tests/suite.sh b/tests/suite.sh index f982bfe..f38a263 100755 --- a/tests/suite.sh +++ b/tests/suite.sh @@ -105,6 +105,8 @@ f=${FAKE_UBOOT:?} touch "$f" k=$1 shift +# FAKE_UBOOT_FULL=: an environment with no room left for that one, as a real one runs out. +if [ $# -gt 0 ] && [ "$k" = "${FAKE_UBOOT_FULL:-}" ]; then echo "Error: environment overflow, $k not set" >&2; exit 1; fi awk -v k="$k" 'index($0, k "=") != 1' "$f" >"$f.tmp" [ $# -gt 0 ] && printf '%s=%s\n' "$k" "$*" >>"$f.tmp" mv "$f.tmp" "$f" @@ -266,6 +268,33 @@ cp agent.env enrolled.env CERBERUS_ENROLLMENT_TOKEN=$TOKEN $A status >status.out 2>/dev/null check "status names the node" "$(yes_if grep -q "^node *$NODE" status.out)" "yes" check "status shows the claim code" "$(yes_if grep -q "^claim code *$CLAIM" status.out)" "yes" +check "status shows the public key" "$(sed -n 's/^public key *//p' status.out)" "$PUB" +check "and its fingerprint, as sha256sum of the raw key reads it" \ + "$(sed -n 's/^fingerprint *//p' status.out | tr -d ' ')" \ + "$(echo "$PUB" | base64 -d | sha256sum | cut -c1-32)" +# The vector the coordinator's KeyFingerprintTests pins too: two implementations of one format, and a +# drift between them would show an honest camera as a substituted one. +BINDING=$(echo "$PRIV" | base64 -d | sha256sum | cut -c1-16) +check "the cached public key is bound to the private key" \ + "$(sv cerberus_device_pubkey enrolled.env)" "$PUB:$BINDING" +printf 'cerberus_device_key=%s\ncerberus_device_pubkey=HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw=:%s\n' \ + "$PRIV" "$BINDING" >vector.env +check "fingerprint: the cloud's test vector" \ + "$(CERBERUS_STORE=file:$T/vector.env $A status 2>/dev/null | sed -n 's/^fingerprint *//p')" \ + "9946 8313 5dcd 688b cf07 0f57 1e50 618f" +grep -v '^cerberus_device_pubkey=' enrolled.env >oldkey.env +check "a key from an older agent: no fingerprint until the next start, and no key derived for it" \ + "$(CERBERUS_STORE=file:$T/oldkey.env $A status 2>/dev/null | sed -n 's/^fingerprint *//p')$(sv cerberus_device_pubkey oldkey.env)" \ + "- (known after the next start)" +# A private key replaced by hand leaves a cache that belongs to the old one: status must not show the +# old key's fingerprint as this camera's. +sed "s|^cerberus_device_key=.*|cerberus_device_key=$(wg genkey)|" enrolled.env >swapped.env +check "a key replaced by hand: the old fingerprint is not shown" \ + "$(CERBERUS_STORE=file:$T/swapped.env $A status 2>/dev/null | sed -n 's/^fingerprint *//p')" \ + "- (known after the next start)" +grep -v '^cerberus_device_key=' enrolled.env >nokey.env +check "no private key: no fingerprint, whatever the cache says" \ + "$(CERBERUS_STORE=file:$T/nokey.env $A status 2>/dev/null | sed -n 's/^\(public key\|fingerprint\) *//p')" "-" # The probe pings the hub in the network the interface is in (#7): with a networkSubnet and no # hubOverlayIp, the first address of that subnet — not of CERBERUS_SUBNET's default pool. @@ -284,6 +313,17 @@ check "address with the tenant's prefix" "$(yes_if grep -qF "dry-run: ip address check "a route to a hub outside the subnet" "$(yes_if grep -qF "dry-run: ip route replace $HUB_IP/32 dev cbr0" up.log)" "yes" check "the key file does not outlive the call" "$(yes_if test -e $T/.wg.key)" "no" +# The public-key cache is a cache: a U-Boot environment with no room for it must not stop a camera +# that has everything else it needs. +FULL=$T/full-uboot.env +grep -v '^cerberus_device_pubkey=' enrolled.env >$FULL +FAKE_UBOOT_FULL=cerberus_device_pubkey uboot $FULL $A up >full.log 2>&1 +check "a full U-Boot env: up still succeeds" "$?" "0" +check "and programs the interface" "$(yes_if grep -qF "dry-run: wg set cbr0 private-key" full.log)" "yes" +check "and says what it could not keep" "$(yes_if grep -q 'fw_setenv cerberus_device_pubkey failed' full.log)" "yes" +check "status then says the fingerprint is not known yet" \ + "$(uboot $FULL $A status 2>/dev/null | sed -n 's/^fingerprint *//p')" "- (known after the next start)" + # ------------------------------------------------------------------- back-channel section "heartbeat and the back-channel" runfor() { # runfor [env...] — `cerberus run` for a while, then SIGTERM @@ -418,7 +458,8 @@ fi section "the rename: streq_* keys and STREQ_* variables" # A camera installed before the rename: its whole identity under the old names, in U-Boot env. OLD=$T/old-uboot.env -sed -n 's/^cerberus_\(.*\)=\(.*\)$/streq_\1=\2/p' enrolled.env >$OLD +# No device_pubkey: an install from before the rename predates that key too. +grep -v '^cerberus_device_pubkey=' enrolled.env | sed -n 's/^cerberus_\(.*\)=\(.*\)$/streq_\1=\2/p' >$OLD uboot $OLD $A status >mig.out 2>mig.log check "moves its identity to cerberus_*" "$(ub $OLD cerberus_device_key)" "$PRIV" check "and its enrolment" "$(ub $OLD cerberus_node_id)" "$NODE"