trust: the verifying half of publisher signatures (own module)
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>
This commit is contained in:
1 parent
764b6f36ea
commit
35f8b7d18a
11 files changed
+1921
-3
No files matched your search
@@ -0,0 +1,315 @@
|
||||
package trust
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"aead.dev/minisign"
|
||||
)
|
||||
|
||||
// KnownKey is one accepted signing key for a repository -- either the
|
||||
// first valid signature this host ever saw for it (pinned automatically
|
||||
// by Reconcile), or one added deliberately with AddKnownKey.
|
||||
type KnownKey struct {
|
||||
KeyID KeyID `json:"key_id"`
|
||||
// PublicKey is the accepted key itself (KeyText form). A signature
|
||||
// only matches this entry if it was made with this exact key
|
||||
// (SameKey); the ID alone proves nothing, see SameKey.
|
||||
//
|
||||
// Entries written before 2026-10-28 have only the ID. Such an entry
|
||||
// is completed with the key of the first valid signature carrying
|
||||
// its ID (with a warning), see Reconcile.
|
||||
// TODO: drop that fallback (and treat an ID-only entry as matching
|
||||
// nothing) once every host has re-pinned.
|
||||
PublicKey string `json:"public_key,omitempty"`
|
||||
Signer string `json:"signer,omitempty"`
|
||||
AddedAt time.Time `json:"added_at"`
|
||||
}
|
||||
|
||||
// KnownKeys is a host's pinned-key store, keyed by repository -- the
|
||||
// "registry/repository" portion of a reference (RepositoryKey), never
|
||||
// including a tag or digest, since a repository's signing keys are
|
||||
// expected to stay the same across its own versions. This is
|
||||
// deliberately keyed differently from Policy.Rule (which matches by
|
||||
// namespace *pattern* and package type): a known key is pinned per
|
||||
// concrete repository actually pulled/installed, not pre-declared.
|
||||
//
|
||||
// Each repository accepts a *list* of keys, not exactly one -- the same
|
||||
// way SSH's known_hosts can hold more than one key per host (different
|
||||
// key types, or two people's keys during a deliberate rotation
|
||||
// overlap). A pull/install is accepted as long as at least one of its
|
||||
// currently-valid signatures matches *any* key in the repository's
|
||||
// list; see Reconcile.
|
||||
type KnownKeys map[string][]KnownKey
|
||||
|
||||
// DefaultKnownKeysPath is where the pinned keys are below home (Home).
|
||||
func DefaultKnownKeysPath(home string) string { return filepath.Join(home, "known-keys.json") }
|
||||
|
||||
// LoadKnownKeys reads the known-keys file, or returns an empty store if
|
||||
// it does not exist yet (nothing pinned so far -- the first pull of
|
||||
// anything establishes its first pin).
|
||||
func LoadKnownKeys(path string) (KnownKeys, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if os.IsNotExist(err) {
|
||||
return KnownKeys{}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var k KnownKeys
|
||||
if err := json.Unmarshal(data, &k); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if k == nil {
|
||||
k = KnownKeys{}
|
||||
}
|
||||
return k, nil
|
||||
}
|
||||
|
||||
// SaveKnownKeys atomically writes the known-keys file.
|
||||
func SaveKnownKeys(path string, k KnownKeys) error {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.MarshalIndent(k, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data = append(data, '\n')
|
||||
tmp, err := os.CreateTemp(filepath.Dir(path), ".known-keys-*.json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(tmp.Name())
|
||||
if _, err := tmp.Write(data); err != nil {
|
||||
tmp.Close()
|
||||
return err
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp.Name(), path)
|
||||
}
|
||||
|
||||
// ForgetKnownKey removes *every* accepted key for repoKey, if any -- the
|
||||
// deliberate, separate step an operator takes after independently
|
||||
// verifying a repository's new signing key really is legitimate (see
|
||||
// docs/security-and-trust.md's "Known-key pinning" section), mirroring
|
||||
// "ssh-keygen -R host" rather than offering a single "--force" flag that
|
||||
// would make blindly accepting a changed key one keystroke away.
|
||||
// Reports whether any keys actually existed to remove. To drop only one
|
||||
// of several accepted keys (e.g. retiring one half of a rotation
|
||||
// overlap) without losing the others, use RemoveKnownKey instead.
|
||||
func ForgetKnownKey(path, repoKey string) (bool, error) {
|
||||
keys, err := LoadKnownKeys(path)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(keys[repoKey]) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
delete(keys, repoKey)
|
||||
return true, SaveKnownKeys(path, keys)
|
||||
}
|
||||
|
||||
// AddKnownKey adds keyID (with an optional signer label) to repoKey's
|
||||
// accepted list, creating the entry if none exists yet. This is for
|
||||
// deliberately pre-trusting a key an operator has independently
|
||||
// verified -- the same as appending "ssh-keyscan" output to known_hosts
|
||||
// before ever connecting, or accepting a second key during a planned
|
||||
// rotation overlap -- rather than only ever pinning whatever the first
|
||||
// pull happens to show. publicKey (KeyText form) is the key itself; an
|
||||
// empty publicKey (only an ID was given) is pinned by the first valid
|
||||
// signature carrying that ID, like an entry from before keys were
|
||||
// recorded. A no-op, not an error, if keyID is already present for
|
||||
// repoKey with the same key, or without one (then publicKey fills it
|
||||
// in); an error if it is present with a different key.
|
||||
func AddKnownKey(path, repoKey string, keyID KeyID, publicKey, signer string) error {
|
||||
keys, err := LoadKnownKeys(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i, k := range keys[repoKey] {
|
||||
if !k.KeyID.Is(keyID) {
|
||||
continue
|
||||
}
|
||||
switch {
|
||||
case publicKey == "" || SameKeyText(k.PublicKey, publicKey):
|
||||
return nil
|
||||
case k.PublicKey == "":
|
||||
keys[repoKey][i].PublicKey = publicKey
|
||||
return SaveKnownKeys(path, keys)
|
||||
default:
|
||||
return fmt.Errorf("%s already accepts a different key with id %s", repoKey, keyID)
|
||||
}
|
||||
}
|
||||
keys[repoKey] = append(keys[repoKey], KnownKey{KeyID: keyID.Canonical(), PublicKey: publicKey, Signer: signer, AddedAt: time.Now().UTC()})
|
||||
return SaveKnownKeys(path, keys)
|
||||
}
|
||||
|
||||
// KnownPublicKey returns the recorded key (KeyText form) of any accepted
|
||||
// entry with keyID, in any repository, or "" if none records one. It
|
||||
// lets a bare key ID typed by an operator be resolved to the key it was
|
||||
// pinned with.
|
||||
func KnownPublicKey(keys KnownKeys, keyID KeyID) string {
|
||||
for _, list := range keys {
|
||||
for _, k := range list {
|
||||
if k.KeyID.Is(keyID) && k.PublicKey != "" {
|
||||
return k.PublicKey
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// RemoveKnownKey removes exactly one key from repoKey's accepted list,
|
||||
// leaving any other accepted keys for that repository in place --
|
||||
// unlike ForgetKnownKey, which drops the whole list at once. Reports
|
||||
// whether that key was actually present. Removing every key one at a
|
||||
// time this way and removing them all via ForgetKnownKey end up
|
||||
// equivalent, but ForgetKnownKey is the clearer way to say "start over
|
||||
// from scratch" for one repository.
|
||||
func RemoveKnownKey(path, repoKey string, keyID KeyID) (bool, error) {
|
||||
keys, err := LoadKnownKeys(path)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
existing := keys[repoKey]
|
||||
kept := existing[:0]
|
||||
found := false
|
||||
for _, k := range existing {
|
||||
if k.KeyID.Is(keyID) {
|
||||
found = true
|
||||
continue
|
||||
}
|
||||
kept = append(kept, k)
|
||||
}
|
||||
if !found {
|
||||
return false, nil
|
||||
}
|
||||
if len(kept) == 0 {
|
||||
delete(keys, repoKey)
|
||||
} else {
|
||||
keys[repoKey] = kept
|
||||
}
|
||||
return true, SaveKnownKeys(path, keys)
|
||||
}
|
||||
|
||||
// Reconcile pins repoKey's first valid signer the first time one
|
||||
// is seen for it, accepts a later pull/install as long as at least one
|
||||
// of its currently-valid signatures was made with *any* already-accepted
|
||||
// key (whether pinned automatically or added with AddKnownKey), and
|
||||
// refuses (ErrRefused) one whose valid signatures match none of
|
||||
// them. A match means the same key (SameKey), not just the same key ID:
|
||||
// a signature whose key merely claims an accepted ID is refused. An
|
||||
// unsigned pull (no valid signature at all) is a no-op either way: TOFU
|
||||
// pinning only ever protects a repository once it has shown at least one
|
||||
// real signature, it never demands one out of nothing.
|
||||
//
|
||||
// An accepted entry that records only an ID (written before keys were
|
||||
// recorded) is completed with the key of the first valid signature
|
||||
// carrying that ID, with a warning. TODO: remove this fallback in a
|
||||
// later version (see KnownKey.PublicKey).
|
||||
func Reconcile(path, repoKey string, verified []VerifiedSignature) error {
|
||||
var valid []VerifiedSignature
|
||||
for _, v := range verified {
|
||||
if v.Valid {
|
||||
valid = append(valid, v)
|
||||
}
|
||||
}
|
||||
if len(valid) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
keys, err := LoadKnownKeys(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
accepted := keys[repoKey]
|
||||
if len(accepted) == 0 {
|
||||
v := valid[0]
|
||||
keys[repoKey] = []KnownKey{{KeyID: v.KeyID.Canonical(), PublicKey: v.Key, Signer: v.Signature.Signer, AddedAt: time.Now().UTC()}}
|
||||
return SaveKnownKeys(path, keys)
|
||||
}
|
||||
var forged *VerifiedSignature
|
||||
for _, v := range valid {
|
||||
for i, k := range accepted {
|
||||
if !v.KeyID.Is(k.KeyID) {
|
||||
continue
|
||||
}
|
||||
if k.PublicKey == "" && v.Key != "" {
|
||||
accepted[i].PublicKey = v.Key
|
||||
Warn("%s: accepted key %s had no recorded public key; pinned it now to %s (check this fingerprint against the publisher's)", repoKey, k.KeyID, v.Fingerprint)
|
||||
return SaveKnownKeys(path, keys)
|
||||
}
|
||||
if k.PublicKey == "" || SameKeyText(k.PublicKey, v.Key) {
|
||||
return nil
|
||||
}
|
||||
forged = &v
|
||||
}
|
||||
}
|
||||
acceptedIDs := make([]KeyID, len(accepted))
|
||||
for i, k := range accepted {
|
||||
acceptedIDs[i] = k.KeyID
|
||||
}
|
||||
if forged != nil {
|
||||
return Refusedf(
|
||||
"refusing %s: it is signed with a key that claims the accepted key id %s but is a different key (fingerprint %s) -- "+
|
||||
"a key id is only a label, so this signature does not come from the accepted key",
|
||||
repoKey, forged.KeyID, forged.Fingerprint)
|
||||
}
|
||||
first := valid[0].KeyID
|
||||
return Refusedf(
|
||||
"refusing %s: signing key changed -- accepted key(s) %v, but this content is signed with %s (fingerprint %s) instead. "+
|
||||
"If you have independently verified the new key's fingerprint (e.g. against the publisher's own announcement), either "+
|
||||
"run \"yet trust known-keys add %s <public-key-file>\" to accept it alongside the existing one(s), or "+
|
||||
"\"yet trust known-keys forget %s\" to replace them outright, then try again.",
|
||||
repoKey, acceptedIDs, first, valid[0].Fingerprint, repoKey, repoKey)
|
||||
}
|
||||
|
||||
// RepositoryKey returns ref's "registry/repository" identity, the
|
||||
// KnownKeys map key -- deliberately excluding any tag or digest, computed
|
||||
// with no network call.
|
||||
func RepositoryKey(registry, repository string) string {
|
||||
return fmt.Sprintf("%s/%s", registry, repository)
|
||||
}
|
||||
|
||||
// AcceptedKeys returns the keys accepted for repoKey that record their
|
||||
// public key; an ID-only entry (see KnownKey.PublicKey) is not usable
|
||||
// to check a signature that does not carry its own key, and is skipped.
|
||||
func AcceptedKeys(keys KnownKeys, repoKey string) []minisign.PublicKey {
|
||||
var out []minisign.PublicKey
|
||||
for _, k := range keys[repoKey] {
|
||||
if pub, err := ParseKeyText(k.PublicKey); k.PublicKey != "" && err == nil {
|
||||
out = append(out, pub)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// UnrecordedKeys counts the accepted keys known-keys and the trust
|
||||
// policy name by ID only, without the key itself (see
|
||||
// KnownKey.PublicKey, Policy.Keys) -- what a doctor reports until they
|
||||
// are pinned.
|
||||
func UnrecordedKeys(keys KnownKeys, policy Policy) (known, policyIDs int) {
|
||||
for _, list := range keys {
|
||||
for _, k := range list {
|
||||
if k.PublicKey == "" {
|
||||
known++
|
||||
}
|
||||
}
|
||||
}
|
||||
seen := map[KeyID]bool{}
|
||||
for _, rule := range policy.Rules {
|
||||
for _, id := range rule.AllowedKeys {
|
||||
if policy.KeyFor(id) == "" && !seen[id.Canonical()] {
|
||||
seen[id.Canonical()] = true
|
||||
policyIDs++
|
||||
}
|
||||
}
|
||||
}
|
||||
return known, policyIDs
|
||||
}
|
||||
Reference in new issue
Block a user