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>
143 lines
5.1 KiB
Go
143 lines
5.1 KiB
Go
package trust
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"aead.dev/minisign"
|
|
)
|
|
|
|
// ArtifactType is the OCI artifactType of a publisher signature referrer,
|
|
// MediaType the media type of its payload blob (the same string).
|
|
const (
|
|
ArtifactType = "application/vnd.yetano.signature.v1+json"
|
|
MediaType = ArtifactType
|
|
)
|
|
|
|
// Signature is the payload of one publisher signature, pushed as an OCI 1.1
|
|
// referrer on the signed manifest's digest. PublicKey and Value are the
|
|
// exact text-format bytes minisign itself reads and writes (the same bytes
|
|
// a ".pub" file and a ".minisig" file would contain): either can be
|
|
// written to disk verbatim and verified with the standalone `minisign` CLI
|
|
// against the digest recorded in Digest.
|
|
type Signature struct {
|
|
SchemaVersion int `json:"schema_version"`
|
|
Signer string `json:"signer,omitempty"`
|
|
Algorithm string `json:"algorithm"`
|
|
PublicKey string `json:"public_key"`
|
|
Digest string `json:"digest"`
|
|
Value string `json:"signature"`
|
|
SignedAt time.Time `json:"signed_at"`
|
|
}
|
|
|
|
// VerifiedSignature is one signature referrer and the outcome of checking
|
|
// it cryptographically against the subject digest it claims to sign.
|
|
type VerifiedSignature struct {
|
|
Signature Signature
|
|
KeyID KeyID
|
|
// Key is the signing key in KeyText form and Fingerprint its
|
|
// fingerprint, set whenever the key could be decoded. A pin
|
|
// compares Key, never just KeyID (see SameKey).
|
|
Key string `json:",omitempty"`
|
|
Fingerprint string `json:",omitempty"`
|
|
Valid bool
|
|
Error string
|
|
}
|
|
|
|
// Payload is one signature referrer as fetched: its payload, or why it
|
|
// could not be fetched.
|
|
type Payload struct {
|
|
Data []byte
|
|
Err error
|
|
}
|
|
|
|
// Verify checks every signature referrer fetched for the manifest
|
|
// subjectDigest. It does not consult a trust policy: Policy.Evaluate and
|
|
// Reconcile decide whether a valid signer is trusted. Valid signatures
|
|
// that share a key ID but were made with different keys are all made
|
|
// invalid (see the comment in the code).
|
|
func Verify(subjectDigest string, payloads []Payload) []VerifiedSignature {
|
|
results := make([]VerifiedSignature, 0, len(payloads))
|
|
for _, p := range payloads {
|
|
if p.Err != nil {
|
|
results = append(results, VerifiedSignature{Error: p.Err.Error()})
|
|
continue
|
|
}
|
|
results = append(results, VerifyPayload(subjectDigest, p.Data))
|
|
}
|
|
return rejectConflictingIDs(results)
|
|
}
|
|
|
|
// rejectConflictingIDs invalidates valid signatures that share a key ID
|
|
// but were made with different keys: at most one of them can be the
|
|
// key the ID belongs to, and which one is not decidable here, so
|
|
// neither may count -- otherwise whichever comes first would decide
|
|
// what a pin records (see Reconcile).
|
|
func rejectConflictingIDs(results []VerifiedSignature) []VerifiedSignature {
|
|
for i := range results {
|
|
for j := range results {
|
|
a, b := &results[i], &results[j]
|
|
if i == j || !a.Valid || !b.Valid || !a.KeyID.Is(b.KeyID) || SameKeyText(a.Key, b.Key) {
|
|
continue
|
|
}
|
|
for _, v := range []*VerifiedSignature{a, b} {
|
|
v.Valid = false
|
|
v.Error = fmt.Sprintf("several different keys claim key id %s", v.KeyID)
|
|
}
|
|
}
|
|
}
|
|
return results
|
|
}
|
|
|
|
// VerifyPayload checks one signature referrer's payload (the JSON a
|
|
// Signature marshals to) against subjectDigest, the digest of the
|
|
// manifest it is attached to. Like Verify it does not consult a trust
|
|
// policy.
|
|
func VerifyPayload(subjectDigest string, payload []byte) VerifiedSignature {
|
|
var sig Signature
|
|
if err := json.Unmarshal(payload, &sig); err != nil {
|
|
return VerifiedSignature{Error: fmt.Sprintf("parse signature: %v", err)}
|
|
}
|
|
return VerifySignature(subjectDigest, sig)
|
|
}
|
|
|
|
// VerifySignature checks sig against subjectDigest.
|
|
func VerifySignature(subjectDigest string, sig Signature) VerifiedSignature {
|
|
if sig.Algorithm != "minisign" {
|
|
return VerifiedSignature{Signature: sig, Error: fmt.Sprintf("unsupported algorithm %q", sig.Algorithm)}
|
|
}
|
|
var pub minisign.PublicKey
|
|
if err := pub.UnmarshalText([]byte(sig.PublicKey)); err != nil {
|
|
return VerifiedSignature{Signature: sig, Error: fmt.Sprintf("decode public key: %v", err)}
|
|
}
|
|
v := VerifiedSignature{Signature: sig, KeyID: ID(pub), Key: KeyText(pub), Fingerprint: Fingerprint(pub)}
|
|
if sig.Digest != subjectDigest {
|
|
v.Error = "signature digest does not match the resolved package digest"
|
|
return v
|
|
}
|
|
if !minisign.Verify(pub, []byte(sig.Digest), []byte(sig.Value)) {
|
|
v.Error = "cryptographic verification failed"
|
|
return v
|
|
}
|
|
v.Valid = true
|
|
return v
|
|
}
|
|
|
|
// ErrRefused marks an error that refuses content on trust grounds: a
|
|
// pinned key that does not match, a policy that rejects it. Programs map
|
|
// it to their exit code for a trust refusal (errors.Is).
|
|
var ErrRefused = errors.New("refused by the trust policy")
|
|
|
|
// refused is an error that Is ErrRefused, with its own message.
|
|
type refused struct{ msg string }
|
|
|
|
func (r *refused) Error() string { return r.msg }
|
|
func (r *refused) Is(target error) bool { return target == ErrRefused }
|
|
|
|
// Refusedf is an error that Is ErrRefused.
|
|
func Refusedf(format string, args ...any) error {
|
|
return &refused{msg: fmt.Sprintf(format, args...)}
|
|
}
|