Files
MrAlders0n a2b81b8c09 fix(polling): accept claims and readings signed in the v2 formats
Poll firmware from 2026-12-03 signs corescope-reading/v2 and corescope-claim/v2, which add poll_key, access and key, and sends requested as false for an own target. Those were all dropped. v1 still verifies, for a message with none of the new fields. reply_hops may now be left out.
2026-12-03 20:37:56 -05:00

169 lines
5.9 KiB
Go

// Copyright 2026 Beacon Contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
package polling
import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net"
"sort"
"strconv"
"strings"
)
// ErrClaimSignature marks a claim whose signature is present and wrong, or that names a server without signing.
var ErrClaimSignature = errors.New("claim: signature does not check out")
const (
claimSigPrefix = "corescope-claim/v2"
claimSigPrefixV1 = "corescope-claim/v1"
)
// The key kinds a signed claim can name for a target.
var signedClaimKeys = map[string]bool{"own": true, "password": true, "shared": true}
// checkClaimSignature reads the claim's poll_url and sig. A claim with neither is unsigned and has no host. One
// with a signature must verify against the observer's key over
//
// corescope-claim/v2|origin_id|poll_url|timestamp|community|accepts_requests|request_slots|max_slots|poll_key|targets
//
// built from the fields as they were sent: origin_id lowercase, booleans as true or false, counts as integers,
// poll_key lowercase or empty, and targets as node_id:state:misses:requested:key:access (requested 1 or 0, access
// empty when absent), lowercase ids, sorted and comma-joined. The observer firmware
// always signs a claim that names its server, so a poll_url without a signature is refused too.
// Poll firmware before 2026-12-03 signs corescope-claim/v1, with no poll_key part and targets that end at
// requested. That is tried only for a claim with no poll_key and no access, so neither can be added unsigned.
// dup says the JSON text named a key twice somewhere; a signed claim with one is refused, because another parser
// could read a different value than the one checked here.
func checkClaimSignature(w claimWire, observer []byte, dup bool) (host string, signed bool, err error) {
if len(w.PollURL) == 0 && len(w.Sig) == 0 {
return "", false, nil
}
if dup {
return "", false, ErrClaimSignature
}
// Either key present means the claim is meant to be signed: both must be strings, null included in "not".
var pollURL, sigHex string
if json.Unmarshal(w.PollURL, &pollURL) != nil || json.Unmarshal(w.Sig, &sigHex) != nil ||
string(w.PollURL) == "null" || string(w.Sig) == "null" || len(w.PollURL) == 0 || len(w.Sig) == 0 {
return "", false, ErrClaimSignature
}
if host = claimPollHost(pollURL); host == "" {
return "", false, fmt.Errorf("claim: poll_url is not a host: %w", ErrClaimSignature)
}
sig, derr := hex.DecodeString(sigHex)
if derr != nil || len(sig) != ed25519.SignatureSize {
return "", false, ErrClaimSignature
}
if w.RequestSlots.v == nil || w.MaxSlots.v == nil {
return "", false, ErrClaimSignature
}
if len(w.Readings) != 0 || len(w.PollKeySig) != 0 {
return "", false, ErrClaimSignature
}
pollKey, v2Fields := "", false
if len(w.PollKey) != 0 {
var ok bool
if pollKey, ok = sigString(w.PollKey); !ok || len(pollKey) != 64 || !sigHexRe.MatchString(pollKey) {
return "", false, ErrClaimSignature
}
v2Fields = true
}
// v2 signs each target's key kind, so it needs a known one; v1 does not sign it.
keysKnown := true
targets := make([]string, 0, len(w.Nodes))
targetsV1 := make([]string, 0, len(w.Nodes))
seen := make(map[string]bool, len(w.Nodes))
for _, n := range w.Nodes {
id := strings.ToLower(n.NodeID)
// A state is a plain word. One with a separator in it could make two targets sign as one.
if n.Misses.v == nil || seen[id] || !signedStateWord(n.State) {
return "", false, ErrClaimSignature
}
seen[id] = true
access := ""
if len(n.Access) != 0 {
var ok bool
if access, ok = sigString(n.Access); !ok || !signedAccess[access] {
return "", false, ErrClaimSignature
}
v2Fields = true
}
keysKnown = keysKnown && signedClaimKeys[n.Key]
requested := "0"
if n.Requested {
requested = "1"
}
t := id + ":" + n.State + ":" + strconv.FormatInt(*n.Misses.v, 10) + ":" + requested
targetsV1 = append(targetsV1, t)
targets = append(targets, t+":"+n.Key+":"+access)
}
sort.Strings(targets)
sort.Strings(targetsV1)
head := []string{
strings.ToLower(w.OriginID), pollURL, w.Timestamp,
strconv.FormatBool(w.Community), strconv.FormatBool(w.AcceptsRequests),
strconv.FormatInt(*w.RequestSlots.v, 10), strconv.FormatInt(*w.MaxSlots.v, 10),
}
key := ed25519.PublicKey(observer)
if keysKnown {
msg := claimSigPrefix + "|" + strings.Join(head, "|") + "|" + strings.ToLower(pollKey) + "|" + strings.Join(targets, ",")
if ed25519.Verify(key, []byte(msg), sig) {
return host, true, nil
}
}
if v2Fields {
return "", false, ErrClaimSignature
}
msg := claimSigPrefixV1 + "|" + strings.Join(head, "|") + "|" + strings.Join(targetsV1, ",")
if !ed25519.Verify(key, []byte(msg), sig) {
return "", false, ErrClaimSignature
}
return host, true, nil
}
// signedStateWord accepts 1 to 32 lowercase letters or underscores, which covers every state the firmware sends
// and keeps the message's separators out of it.
func signedStateWord(s string) bool {
if len(s) < 1 || len(s) > 32 {
return false
}
for i := 0; i < len(s); i++ {
if c := s[i]; (c < 'a' || c > 'z') && c != '_' {
return false
}
}
return true
}
// claimPollHost takes the host out of a claim's poll_url, which is a lowercase host with an optional port. It is
// "" for anything else.
func claimPollHost(s string) string {
if len(s) == 0 || len(s) > 261 {
return ""
}
for i := 0; i < len(s); i++ {
c := s[i]
if (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '.' && c != '-' && c != ':' && c != '[' && c != ']' {
return ""
}
}
host := s
if h, _, err := net.SplitHostPort(s); err == nil {
host = h
} else if strings.HasPrefix(s, "[") != strings.HasSuffix(s, "]") {
return ""
} else {
host = strings.TrimSuffix(strings.TrimPrefix(s, "["), "]")
}
// What is left is a name, an IPv4 address or an IPv6 literal: no brackets, and a colon only in the last.
if host == "" || strings.ContainsAny(host, "[]") || (strings.Contains(host, ":") && net.ParseIP(host) == nil) {
return ""
}
return host
}