Moved from yet/internal/trust, behavior and file formats unchanged, so yacota (images) checks against the same trust-policy.json and known-keys.json as yet (packages): the signature payload and its check (Verify over fetched referrers), known keys with TOFU pinning (Reconcile; refusals are ErrRefused), the trust policy (Evaluate, AllowsCapability), Curated, Pinned and Home (yet's data directory). Signing, key storage and the registry side stay in yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
186 lines
6.2 KiB
Go
186 lines
6.2 KiB
Go
// Package trust is the verifying half of the Yetano family's publisher
|
|
// signatures: the signature payload a referrer carries, its check, the
|
|
// host's pinned keys (known-keys.json) and its trust policy
|
|
// (trust-policy.json). yet signs and manages keys; every program that
|
|
// pulls signed content (yet, yacota, ...) checks it with this package
|
|
// against the same files, so a host makes one trust decision.
|
|
//
|
|
// Signatures use minisign (aead.dev/minisign) over the resolved OCI
|
|
// manifest digest, attached as an OCI 1.1 referrer to that digest: a
|
|
// signature survives a copy or a mirror and cannot be separated from the
|
|
// content it was made for. Moved here from yet/internal/trust
|
|
// (2026-11-12), unchanged in behavior and file formats.
|
|
package trust
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/ed25519"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"aead.dev/minisign"
|
|
)
|
|
|
|
// KeyID is a short, stable label derived from a public key: the same
|
|
// 64-bit key ID minisign itself displays and embeds in a signature (see
|
|
// minisign.PublicKey.ID / minisign.Signature.KeyID), always written as 16
|
|
// uppercase hex digits, like the minisign reference tool prints it.
|
|
//
|
|
// yet before 2026-10-04 wrote IDs without leading zeros
|
|
// ("974656A68B31489" for "0974656A68B31489"), as aead.dev/minisign does.
|
|
// Both forms are accepted wherever an ID is read -- from JSON
|
|
// (UnmarshalJSON) or typed by a user (ParseKeyID) -- and normalized to
|
|
// the 16-digit form, so an old known-keys.json or trust policy keeps
|
|
// matching and is written back in the new form the next time it is
|
|
// saved.
|
|
type KeyID string
|
|
|
|
// ParseKeyID parses a key ID in either form (16 digits, or without
|
|
// leading zeros), in any case, and returns it in canonical form.
|
|
func ParseKeyID(s string) (KeyID, error) {
|
|
s = strings.TrimSpace(s)
|
|
if s == "" || len(s) > 16 {
|
|
return "", fmt.Errorf("invalid key id %q: want up to 16 hex digits", s)
|
|
}
|
|
v, err := strconv.ParseUint(s, 16, 64)
|
|
if err != nil {
|
|
return "", fmt.Errorf("invalid key id %q: want up to 16 hex digits", s)
|
|
}
|
|
return KeyIDFromUint(v), nil
|
|
}
|
|
|
|
// Canonical returns k in canonical 16-digit form, or k upper-cased if it
|
|
// is not a valid key ID. Compare IDs with Is, never with ==, so a value
|
|
// built in either form still matches.
|
|
func (k KeyID) Canonical() KeyID {
|
|
if id, err := ParseKeyID(string(k)); err == nil {
|
|
return id
|
|
}
|
|
return KeyID(strings.ToUpper(string(k)))
|
|
}
|
|
|
|
// Is reports whether k and other name the same key, in either form.
|
|
func (k KeyID) Is(other KeyID) bool {
|
|
return k.Canonical() == other.Canonical()
|
|
}
|
|
|
|
// KeyIDFromUint is the canonical KeyID of minisign's 64-bit key ID.
|
|
func KeyIDFromUint(v uint64) KeyID {
|
|
return KeyID(fmt.Sprintf("%016X", v))
|
|
}
|
|
|
|
// UnmarshalJSON normalizes a stored ID to canonical form. A value that is
|
|
// not a valid key ID is kept as-is (upper-cased) rather than failing the
|
|
// whole file: it then simply never matches a real key.
|
|
func (k *KeyID) UnmarshalJSON(data []byte) error {
|
|
var s string
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
return err
|
|
}
|
|
if id, err := ParseKeyID(s); err == nil {
|
|
*k = id
|
|
} else {
|
|
*k = KeyID(strings.ToUpper(s))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ID derives a stable identifier for a public key: the same 64-bit ID
|
|
// minisign itself uses, as 16 uppercase hex digits (so it matches what
|
|
// `minisign -G` prints and what a signature's own embedded KeyID field
|
|
// carries).
|
|
func ID(pub minisign.PublicKey) KeyID {
|
|
return KeyIDFromUint(pub.ID())
|
|
}
|
|
|
|
// KeyText returns pub in its one-line form: the base64 line of a ".pub"
|
|
// file, without the comment line. It is what known-keys.json and the
|
|
// trust policy record for a pinned key (KnownKey.PublicKey,
|
|
// Policy.Keys), and what ParseKeyText reads back.
|
|
func KeyText(pub minisign.PublicKey) string {
|
|
data, err := pub.MarshalText()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
_, line, ok := bytes.Cut(data, []byte("\n"))
|
|
if !ok {
|
|
return strings.TrimSpace(string(data))
|
|
}
|
|
return strings.TrimSpace(string(line))
|
|
}
|
|
|
|
// ParseKeyText parses a public key in KeyText's one-line form or as a
|
|
// whole ".pub" file.
|
|
func ParseKeyText(s string) (minisign.PublicKey, error) {
|
|
var pub minisign.PublicKey
|
|
err := pub.UnmarshalText([]byte(strings.TrimSpace(s)))
|
|
return pub, err
|
|
}
|
|
|
|
// Ed25519Key returns pub's raw Ed25519 public key. A minisign public key
|
|
// is "Ed" + an 8-byte key ID + these 32 bytes.
|
|
func Ed25519Key(pub minisign.PublicKey) ed25519.PublicKey {
|
|
data, err := pub.MarshalText()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if _, line, ok := bytes.Cut(data, []byte("\n")); ok {
|
|
data = line
|
|
}
|
|
raw, err := base64.StdEncoding.DecodeString(strings.TrimSpace(string(data)))
|
|
if err != nil || len(raw) != 2+8+ed25519.PublicKeySize {
|
|
return nil
|
|
}
|
|
return ed25519.PublicKey(raw[10:])
|
|
}
|
|
|
|
// SameKey reports whether a and b are the same Ed25519 key, whatever key
|
|
// IDs they carry. This, not KeyID.Is, is what decides that a signature
|
|
// was made by a pinned key: a minisign key ID is a free 8-byte label
|
|
// that neither the key nor a signature's Ed25519 part covers, so anyone
|
|
// can give a key of their own the ID of a key someone else trusts.
|
|
func SameKey(a, b minisign.PublicKey) bool {
|
|
ka, kb := Ed25519Key(a), Ed25519Key(b)
|
|
return ka != nil && kb != nil && ka.Equal(kb)
|
|
}
|
|
|
|
// SameKeyText is SameKey for two keys in KeyText (minisign public key)
|
|
// form; false if either does not parse.
|
|
func SameKeyText(a, b string) bool {
|
|
ka, err := ParseKeyText(a)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
kb, err := ParseKeyText(b)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return SameKey(ka, kb)
|
|
}
|
|
|
|
// Fingerprint is pub's key fingerprint, "SHA256:" and the unpadded
|
|
// base64 SHA-256 of the raw Ed25519 key (ssh-keygen -l style). Unlike
|
|
// the key ID it is derived from the key, so it is what to compare
|
|
// against a publisher's out-of-band announcement, and it is the same
|
|
// for a key in minisign and in PEM form.
|
|
func Fingerprint(pub minisign.PublicKey) string {
|
|
raw := Ed25519Key(pub)
|
|
if raw == nil {
|
|
return ""
|
|
}
|
|
sum := sha256.Sum256(raw)
|
|
return "SHA256:" + base64.RawStdEncoding.EncodeToString(sum[:])
|
|
}
|
|
|
|
// Warn reports a trust warning that is not an error (a key pinned
|
|
// after the fact, see Reconcile). A variable so programs can route it
|
|
// and tests can silence or capture it.
|
|
var Warn = func(format string, args ...any) {
|
|
fmt.Fprintf(os.Stderr, "warning: "+format+"\n", args...)
|
|
}
|