Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e1495430c5 |
No files matched your search
+35
-7
@@ -1,18 +1,22 @@
|
|||||||
# yetano.dev/yetano/core/trust
|
# yetano.dev/yetano/core/trust
|
||||||
|
|
||||||
The verifying half of the family's publisher signatures, shared by every
|
The family's minisign (Ed25519) signatures: verifying, shared by every
|
||||||
program that pulls signed content: yet (packages) and yacota (images)
|
program that pulls signed content -- yet (packages) and yacota (images)
|
||||||
check against the same files, so a host makes one trust decision.
|
check against the same files, so a host makes one trust decision -- and
|
||||||
|
signing, shared by every program that signs: yet (packages,
|
||||||
|
attestations) and yabar (evidence records, ADR-0037 there).
|
||||||
|
|
||||||
```text
|
```text
|
||||||
module yetano.dev/yetano/core/trust tags: trust/v0.1.0, ... go 1.25
|
module yetano.dev/yetano/core/trust tags: trust/v0.1.0, trust/v0.2.0, ... go 1.25
|
||||||
```
|
```
|
||||||
|
|
||||||
A nested module of the core repository, like `core/tui`, so the main core
|
A nested module of the core repository, like `core/tui`, so the main core
|
||||||
module stays free of minisign. Depends on `aead.dev/minisign` (MIT) and
|
module stays free of minisign. Depends on `aead.dev/minisign` (MIT) and
|
||||||
core (`core/paths`, for `Home`). Moved from `yet/internal/trust`
|
core (`core/paths`, for `Home`; `core/secretref`, for `KeyFromRef`).
|
||||||
(2026-11-12) without changing behavior or file formats; signing, key
|
Moved from `yet/internal/trust` without changing behavior or file
|
||||||
storage, PEM and the registry side stay in yet.
|
formats: the verifying half on 2026-11-12 (trust/v0.1.0), the signing
|
||||||
|
half on 2026-11-27 (trust/v0.2.0). Pushing signatures to a registry and
|
||||||
|
where auto-sign keeps its keys stay in yet.
|
||||||
|
|
||||||
## What it is
|
## What it is
|
||||||
|
|
||||||
@@ -33,6 +37,22 @@ storage, PEM and the registry side stay in yet.
|
|||||||
- **Where:** `Home(scope)` is yet's data directory (core/paths' DataDir
|
- **Where:** `Home(scope)` is yet's data directory (core/paths' DataDir
|
||||||
for "yet"), `DefaultPolicyPath(home)`, `DefaultKnownKeysPath(home)`.
|
for "yet"), `DefaultPolicyPath(home)`, `DefaultKnownKeysPath(home)`.
|
||||||
|
|
||||||
|
- **Keys:** `KeyPair`, `GenerateKey`, `SaveKeyPair(path, kp, password)`
|
||||||
|
(private key owner-only, `<path>.pub` beside it; the minisign CLI's
|
||||||
|
formats, scrypt-encrypted with a password), `LoadPrivateKey`,
|
||||||
|
`ParsePrivateKey`, `LoadPublicKey`, `MarshalPublicKey`, and
|
||||||
|
`KeyFromRef(ctx, resolver, keyRef, passwordRef)` for a key held as a
|
||||||
|
secret reference (`secret://...` in yaseva, `env:`, `file:`).
|
||||||
|
- **Signing:** `SignDigest(kp, digest)` makes the package signature
|
||||||
|
`VerifySignature` checks; `Sign(kp, message, trustedComment,
|
||||||
|
untrustedComment)` makes a detached `.minisig` over any bytes, and
|
||||||
|
`VerifyMessage(pub, message, sig)` checks it and returns the trusted
|
||||||
|
comment (`minisign -Vm` checks it too); `SignEd25519`/`VerifyEd25519`
|
||||||
|
are plain Ed25519 for DSSE envelopes; `ExportPEM`/`ImportPEM` for
|
||||||
|
cosign and openssl.
|
||||||
|
- **Vector:** `testdata/sign-vector.*` -- a published test key, a
|
||||||
|
message and its signature; every version must produce and accept it.
|
||||||
|
|
||||||
## Use
|
## Use
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -45,5 +65,13 @@ policy, _ := trust.LoadPolicy(trust.DefaultPolicyPath(home))
|
|||||||
if d := policy.Evaluate(name, kind, verified); !d.Trusted { ... }
|
if d := policy.Evaluate(name, kind, verified); !d.Trusted { ... }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Signing:
|
||||||
|
|
||||||
|
```go
|
||||||
|
kp, err := trust.KeyFromRef(ctx, secretref.New(""), "secret://yabar/evidence/minisign", "")
|
||||||
|
sig, err := trust.Sign(kp, recordBytes, "yabar evidence <test_id> <repository_id>", "")
|
||||||
|
comment, err := trust.VerifyMessage(pub, recordBytes, sig)
|
||||||
|
```
|
||||||
|
|
||||||
`Warn` (a variable) reports warnings that are not errors, on standard
|
`Warn` (a variable) reports warnings that are not errors, on standard
|
||||||
error by default.
|
error by default.
|
||||||
@@ -0,0 +1,191 @@
|
|||||||
|
package trust
|
||||||
|
|
||||||
|
// The signing half of publisher signatures, moved from yet's
|
||||||
|
// internal/trust (2026-11-27, YABAR-036a) without changing behavior or
|
||||||
|
// file formats: key pairs in minisign's own file formats, the package
|
||||||
|
// signature over an OCI digest (SignDigest), detached signatures over
|
||||||
|
// any bytes (Sign, VerifyMessage), plain Ed25519 for DSSE envelopes,
|
||||||
|
// and PEM for cosign and openssl. Where keys are kept for auto-signing
|
||||||
|
// and pushing signatures to a registry stay in yet.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto"
|
||||||
|
"crypto/ed25519"
|
||||||
|
"crypto/sha512"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"aead.dev/minisign"
|
||||||
|
)
|
||||||
|
|
||||||
|
// KeyPair is a minisign key pair. The private key never leaves the host's
|
||||||
|
// filesystem; only the public key is shared with other hosts (as a plain
|
||||||
|
// ".pub" file) and embedded in signatures.
|
||||||
|
type KeyPair struct {
|
||||||
|
Public minisign.PublicKey
|
||||||
|
Private minisign.PrivateKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateKey creates a new minisign key pair.
|
||||||
|
func GenerateKey() (KeyPair, error) {
|
||||||
|
pub, priv, err := minisign.GenerateKey(nil)
|
||||||
|
if err != nil {
|
||||||
|
return KeyPair{}, err
|
||||||
|
}
|
||||||
|
return KeyPair{Public: pub, Private: priv}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SaveKeyPair writes kp's private key to path (owner-only permissions) and
|
||||||
|
// its public key to path+".pub" (world-readable: it is meant to be shared).
|
||||||
|
// This mirrors the real minisign CLI's `-p file.pub -s file` convention, so
|
||||||
|
// `trust policy allow` only ever needs the ".pub" file, never the private
|
||||||
|
// key -- a relying party registering a publisher's trust never needs (and
|
||||||
|
// should never be handed) their private key.
|
||||||
|
//
|
||||||
|
// If password is non-empty, the private key is scrypt-encrypted exactly as
|
||||||
|
// `minisign -G` with a passphrase would; if empty, it is written
|
||||||
|
// unencrypted, matching the minisign CLI's own behavior when a blank
|
||||||
|
// passphrase is given. Either way the file is only ever readable by its
|
||||||
|
// owner; a password additionally protects it if the file itself leaks.
|
||||||
|
func SaveKeyPair(path string, kp KeyPair, password string) error {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var privData []byte
|
||||||
|
var err error
|
||||||
|
if password != "" {
|
||||||
|
privData, err = minisign.EncryptKey(password, kp.Private)
|
||||||
|
} else {
|
||||||
|
privData, err = kp.Private.MarshalText()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("encode private key: %w", err)
|
||||||
|
}
|
||||||
|
if err := writeOwnerOnly(path, privData); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
pubData, err := MarshalPublicKey(kp.Public)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("encode public key: %w", err)
|
||||||
|
}
|
||||||
|
return writeFile(path+".pub", pubData, 0o644)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadPrivateKey reads a key pair previously written by SaveKeyPair,
|
||||||
|
// decrypting it with password if it is encrypted (password is ignored, and
|
||||||
|
// may be empty, for an unencrypted key).
|
||||||
|
func LoadPrivateKey(path, password string) (KeyPair, error) {
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return KeyPair{}, err
|
||||||
|
}
|
||||||
|
return ParsePrivateKey(data, password)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParsePrivateKey parses a minisign private key file's contents, the
|
||||||
|
// same bytes LoadPrivateKey reads from disk, so a key can also come from
|
||||||
|
// somewhere other than a file (a yaseva secret, see KeyStore).
|
||||||
|
func ParsePrivateKey(data []byte, password string) (KeyPair, error) {
|
||||||
|
var err error
|
||||||
|
var priv minisign.PrivateKey
|
||||||
|
if minisign.IsEncrypted(data) {
|
||||||
|
priv, err = minisign.DecryptKey(password, data)
|
||||||
|
if err != nil {
|
||||||
|
return KeyPair{}, fmt.Errorf("decrypt private key (wrong password?): %w", err)
|
||||||
|
}
|
||||||
|
} else if err := priv.UnmarshalText(data); err != nil {
|
||||||
|
return KeyPair{}, fmt.Errorf("parse private key: %w", err)
|
||||||
|
}
|
||||||
|
pub, ok := priv.Public().(minisign.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return KeyPair{}, fmt.Errorf("derive public key: unexpected type %T", priv.Public())
|
||||||
|
}
|
||||||
|
return KeyPair{Public: pub, Private: priv}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadPublicKey reads a standalone ".pub" file, for a trust policy that
|
||||||
|
// must never require (or accept) private key material.
|
||||||
|
func LoadPublicKey(path string) (minisign.PublicKey, error) {
|
||||||
|
return minisign.PublicKeyFromFile(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalPublicKey encodes pub as a minisign ".pub" file. It is
|
||||||
|
// aead.dev/minisign's own encoding, except that the comment line carries
|
||||||
|
// the 16-digit key ID (the library writes it without leading zeros).
|
||||||
|
func MarshalPublicKey(pub minisign.PublicKey) ([]byte, error) {
|
||||||
|
data, err := pub.MarshalText()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
_, rest, ok := bytes.Cut(data, []byte("\n"))
|
||||||
|
if !ok {
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
out := fmt.Appendf(nil, "untrusted comment: minisign public key: %s\n", ID(pub))
|
||||||
|
out = append(out, rest...)
|
||||||
|
if !bytes.HasSuffix(out, []byte("\n")) {
|
||||||
|
out = append(out, '\n')
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeOwnerOnly(path string, data []byte) error {
|
||||||
|
return writeFile(path, data, 0o600)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeFile(path string, data []byte, mode os.FileMode) error {
|
||||||
|
tmp, err := os.CreateTemp(filepath.Dir(path), ".key-*")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() { _ = os.Remove(tmp.Name()) }()
|
||||||
|
if err := os.Chmod(tmp.Name(), mode); err != nil {
|
||||||
|
_ = tmp.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SignEd25519 signs message with kp as a plain Ed25519 signature
|
||||||
|
// (minisign's non-prehashed "Ed" algorithm): the raw 64 bytes DSSE
|
||||||
|
// envelopes carry and cosign and other Ed25519 verifiers check against
|
||||||
|
// ExportPEM's key. Package signatures (Sign) keep using minisign's own
|
||||||
|
// format.
|
||||||
|
func SignEd25519(kp KeyPair, message []byte) ([]byte, error) {
|
||||||
|
text, err := kp.Private.Sign(nil, message, crypto.Hash(0))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var sig minisign.Signature
|
||||||
|
if err := sig.UnmarshalText(text); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if sig.Algorithm != minisign.EdDSA {
|
||||||
|
return nil, fmt.Errorf("unexpected minisign algorithm %d", sig.Algorithm)
|
||||||
|
}
|
||||||
|
return sig.Signature[:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// VerifyEd25519 checks an Ed25519 signature over message against pub:
|
||||||
|
// plain Ed25519 (what SignEd25519 makes) or Ed25519ph with SHA-512
|
||||||
|
// (what cosign makes with an Ed25519 key).
|
||||||
|
func VerifyEd25519(pub minisign.PublicKey, message, sig []byte) bool {
|
||||||
|
raw := Ed25519Key(pub)
|
||||||
|
if raw == nil || len(sig) != ed25519.SignatureSize {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if ed25519.Verify(raw, message, sig) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
digest := sha512.Sum512(message)
|
||||||
|
return ed25519.VerifyWithOptions(raw, digest[:], sig, &ed25519.Options{Hash: crypto.SHA512}) == nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package trust
|
||||||
|
|
||||||
|
// Moved from yet's internal/trust with the signing half (2026-11-27).
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/ed25519"
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/binary"
|
||||||
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"aead.dev/minisign"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ExportPEM encodes pub's Ed25519 key as a PEM "PUBLIC KEY" block
|
||||||
|
// (PKIX), the form cosign, openssl and most other tools read -- e.g.
|
||||||
|
// "cosign verify-attestation --key exported.pem". Only the key leaves:
|
||||||
|
// a PEM file has no room for minisign's key ID, and nothing private is
|
||||||
|
// ever exported.
|
||||||
|
func ExportPEM(pub minisign.PublicKey) ([]byte, error) {
|
||||||
|
raw := Ed25519Key(pub)
|
||||||
|
if raw == nil {
|
||||||
|
return nil, errors.New("not an Ed25519 minisign public key")
|
||||||
|
}
|
||||||
|
der, err := x509.MarshalPKIXPublicKey(raw)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: der}), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ImportPEM reads a PEM "PUBLIC KEY" block holding an Ed25519 key
|
||||||
|
// (e.g. a cosign.pub made from an Ed25519 key, or ExportPEM's output)
|
||||||
|
// and returns it as a minisign public key, so it can be pinned and
|
||||||
|
// trusted like any other (known-keys add, trust policy allow).
|
||||||
|
//
|
||||||
|
// PEM carries no key ID, so one is chosen: knownID's answer if it
|
||||||
|
// returns one for this key (a key this host already knows, e.g. one
|
||||||
|
// exported earlier, gets its old ID back), otherwise one derived from
|
||||||
|
// the key (the first 8 bytes of its SHA-256). Either way trust decisions
|
||||||
|
// compare the key itself, never the ID (see SameKey).
|
||||||
|
func ImportPEM(data []byte, knownID func(ed25519.PublicKey) (KeyID, bool)) (minisign.PublicKey, error) {
|
||||||
|
block, _ := pem.Decode(data)
|
||||||
|
if block == nil {
|
||||||
|
return minisign.PublicKey{}, errors.New("no PEM block found")
|
||||||
|
}
|
||||||
|
if block.Type != "PUBLIC KEY" {
|
||||||
|
return minisign.PublicKey{}, fmt.Errorf("PEM block is %q, want \"PUBLIC KEY\" (never import a private key)", block.Type)
|
||||||
|
}
|
||||||
|
parsed, err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return minisign.PublicKey{}, fmt.Errorf("parse public key: %w", err)
|
||||||
|
}
|
||||||
|
raw, ok := parsed.(ed25519.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return minisign.PublicKey{}, fmt.Errorf("unsupported key type %T: only Ed25519 keys can be imported (cosign generate-key-pair makes ECDSA P-256 keys)", parsed)
|
||||||
|
}
|
||||||
|
var id uint64
|
||||||
|
if knownID != nil {
|
||||||
|
if known, ok := knownID(raw); ok {
|
||||||
|
v, err := ParseKeyID(string(known))
|
||||||
|
if err == nil {
|
||||||
|
id = parseUint(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if id == 0 {
|
||||||
|
sum := sha256.Sum256(raw)
|
||||||
|
id = binary.LittleEndian.Uint64(sum[:8])
|
||||||
|
}
|
||||||
|
return minisignKey(id, raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
// minisignKey builds a minisign public key from an ID and a raw key.
|
||||||
|
func minisignKey(id uint64, raw ed25519.PublicKey) (minisign.PublicKey, error) {
|
||||||
|
buf := make([]byte, 0, 2+8+ed25519.PublicKeySize)
|
||||||
|
buf = append(buf, 'E', 'd')
|
||||||
|
buf = binary.LittleEndian.AppendUint64(buf, id)
|
||||||
|
buf = append(buf, raw...)
|
||||||
|
return ParseKeyText(base64.StdEncoding.EncodeToString(buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseUint(id KeyID) uint64 {
|
||||||
|
v, _ := strconv.ParseUint(string(id), 16, 64)
|
||||||
|
return v
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package trust
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"yetano.dev/yetano/core/secretref"
|
||||||
|
)
|
||||||
|
|
||||||
|
// KeyFromRef loads a signing key pair from a secret reference
|
||||||
|
// (secret://, env:, file:, base64: -- core/secretref), for example
|
||||||
|
// "secret://yabar/evidence/minisign" in yaseva. The value is a minisign
|
||||||
|
// private key file's contents. passwordRef ("" for none) is the
|
||||||
|
// reference of the password of an encrypted key. Error messages never
|
||||||
|
// contain the secret.
|
||||||
|
func KeyFromRef(ctx context.Context, r *secretref.Resolver, keyRef, passwordRef string) (KeyPair, error) {
|
||||||
|
data, err := r.Resolve(ctx, keyRef)
|
||||||
|
if err != nil {
|
||||||
|
return KeyPair{}, fmt.Errorf("signing key %s: %s", keyRef, r.Redact(err.Error()))
|
||||||
|
}
|
||||||
|
var password string
|
||||||
|
if passwordRef != "" {
|
||||||
|
if password, err = r.ResolveString(ctx, passwordRef); err != nil {
|
||||||
|
return KeyPair{}, fmt.Errorf("signing key password %s: %s", passwordRef, r.Redact(err.Error()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
kp, err := ParsePrivateKey(data, password)
|
||||||
|
if err != nil {
|
||||||
|
return KeyPair{}, fmt.Errorf("signing key %s: %s", keyRef, r.Redact(err.Error()))
|
||||||
|
}
|
||||||
|
return kp, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package trust
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"aead.dev/minisign"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SignDigest makes the publisher signature of an OCI manifest digest:
|
||||||
|
// the Signature payload yet pushes as a referrer and VerifySignature
|
||||||
|
// checks. The trusted comment is "yet package digest: <digest>" (the
|
||||||
|
// format yet has always written).
|
||||||
|
func SignDigest(kp KeyPair, digest string) (Signature, error) {
|
||||||
|
trustedComment := "yet package digest: " + digest
|
||||||
|
sig := minisign.SignWithComments(kp.Private, []byte(digest), trustedComment, "")
|
||||||
|
pubText, err := kp.Public.MarshalText()
|
||||||
|
if err != nil {
|
||||||
|
return Signature{}, err
|
||||||
|
}
|
||||||
|
return Signature{
|
||||||
|
SchemaVersion: 1,
|
||||||
|
Algorithm: "minisign",
|
||||||
|
PublicKey: string(pubText),
|
||||||
|
Digest: digest,
|
||||||
|
Value: string(sig),
|
||||||
|
SignedAt: time.Now().UTC(),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sign makes a detached minisign signature of message: the text of a
|
||||||
|
// ".minisig" file, which "minisign -V -m <file>" verifies against the
|
||||||
|
// key's ".pub" file. trustedComment is signed with it (one line);
|
||||||
|
// untrustedComment is not ("" for minisign's default).
|
||||||
|
func Sign(kp KeyPair, message []byte, trustedComment, untrustedComment string) ([]byte, error) {
|
||||||
|
if strings.ContainsAny(trustedComment, "\r\n") || strings.ContainsAny(untrustedComment, "\r\n") {
|
||||||
|
return nil, errors.New("a minisign comment is one line")
|
||||||
|
}
|
||||||
|
return minisign.SignWithComments(kp.Private, message, trustedComment, untrustedComment), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrBadSignature: a signature does not verify (wrong key, changed
|
||||||
|
// message or changed trusted comment).
|
||||||
|
var ErrBadSignature = errors.New("signature does not verify")
|
||||||
|
|
||||||
|
// VerifyMessage checks a detached minisign signature (Sign's output, or
|
||||||
|
// a ".minisig" file from the minisign tool) of message against pub and
|
||||||
|
// returns its trusted comment, which the signature covers. A signature
|
||||||
|
// by another key fails, also when its key ID collides with pub's.
|
||||||
|
func VerifyMessage(pub minisign.PublicKey, message, signature []byte) (trustedComment string, err error) {
|
||||||
|
var sig minisign.Signature
|
||||||
|
if err := sig.UnmarshalText(signature); err != nil {
|
||||||
|
return "", fmt.Errorf("parse signature: %w", err)
|
||||||
|
}
|
||||||
|
if sig.KeyID != pub.ID() {
|
||||||
|
return "", fmt.Errorf("%w: signed by key %s, not %s", ErrBadSignature, KeyIDFromUint(sig.KeyID), ID(pub))
|
||||||
|
}
|
||||||
|
if !minisign.Verify(pub, message, signature) {
|
||||||
|
return "", ErrBadSignature
|
||||||
|
}
|
||||||
|
return sig.TrustedComment, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,165 @@
|
|||||||
|
package trust
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"aead.dev/minisign"
|
||||||
|
|
||||||
|
"yetano.dev/yetano/core/secretref"
|
||||||
|
)
|
||||||
|
|
||||||
|
func vectorKey(t *testing.T) KeyPair {
|
||||||
|
t.Helper()
|
||||||
|
kp, err := LoadPrivateKey("testdata/sign-vector.key", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return kp
|
||||||
|
}
|
||||||
|
|
||||||
|
// The golden vector: a fixed key and message give exactly the committed
|
||||||
|
// .minisig (Ed25519 is deterministic), and it verifies with the .pub.
|
||||||
|
// "minisign -Vm testdata/sign-vector.msg -p testdata/sign-vector.pub"
|
||||||
|
// verifies it as well.
|
||||||
|
func TestSignVector(t *testing.T) {
|
||||||
|
kp := vectorKey(t)
|
||||||
|
msg, _ := os.ReadFile("testdata/sign-vector.msg")
|
||||||
|
want, _ := os.ReadFile("testdata/sign-vector.msg.minisig")
|
||||||
|
got, err := Sign(kp, msg, "yabar evidence test-vector 00000000000000000000000000000000", "")
|
||||||
|
if err != nil || !bytes.Equal(got, want) {
|
||||||
|
t.Fatalf("signature differs from the vector (%v):\n%s", err, got)
|
||||||
|
}
|
||||||
|
pub, err := LoadPublicKey("testdata/sign-vector.pub")
|
||||||
|
if err != nil || !SameKey(pub, kp.Public) {
|
||||||
|
t.Fatalf("public key: %v", err)
|
||||||
|
}
|
||||||
|
tc, err := VerifyMessage(pub, msg, want)
|
||||||
|
if err != nil || tc != "yabar evidence test-vector 00000000000000000000000000000000" {
|
||||||
|
t.Fatalf("verify: %q %v", tc, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestVerifyMessageRejects(t *testing.T) {
|
||||||
|
kp := vectorKey(t)
|
||||||
|
msg := []byte("evidence")
|
||||||
|
sig, _ := Sign(kp, msg, "trusted", "untrusted")
|
||||||
|
if _, err := VerifyMessage(kp.Public, []byte("evidencE"), sig); !errors.Is(err, ErrBadSignature) {
|
||||||
|
t.Fatalf("changed message: %v", err)
|
||||||
|
}
|
||||||
|
changed := bytes.Replace(sig, []byte("trusted comment: trusted"), []byte("trusted comment: trustee"), 1)
|
||||||
|
if _, err := VerifyMessage(kp.Public, msg, changed); !errors.Is(err, ErrBadSignature) {
|
||||||
|
t.Fatalf("changed trusted comment: %v", err)
|
||||||
|
}
|
||||||
|
// The untrusted comment is not covered.
|
||||||
|
free := bytes.Replace(sig, []byte("untrusted comment: untrusted"), []byte("untrusted comment: whatever"), 1)
|
||||||
|
if _, err := VerifyMessage(kp.Public, msg, free); err != nil {
|
||||||
|
t.Fatalf("untrusted comment: %v", err)
|
||||||
|
}
|
||||||
|
other, _ := GenerateKey()
|
||||||
|
if _, err := VerifyMessage(other.Public, msg, sig); !errors.Is(err, ErrBadSignature) {
|
||||||
|
t.Fatalf("other key: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := VerifyMessage(kp.Public, msg, []byte("garbage")); err == nil {
|
||||||
|
t.Fatal("garbage verified")
|
||||||
|
}
|
||||||
|
if _, err := Sign(kp, msg, "two\nlines", ""); err == nil {
|
||||||
|
t.Fatal("a two-line comment was accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// What SignDigest makes, VerifySignature (the verifying half every
|
||||||
|
// consumer uses) accepts, and only for that digest.
|
||||||
|
func TestSignDigestVerifies(t *testing.T) {
|
||||||
|
kp := vectorKey(t)
|
||||||
|
const d = "sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
|
||||||
|
sig, err := SignDigest(kp, d)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if v := VerifySignature(d, sig); !v.Valid || v.KeyID != ID(kp.Public) {
|
||||||
|
t.Fatalf("verify: %+v", v)
|
||||||
|
}
|
||||||
|
if v := VerifySignature("sha256:"+strings.Repeat("0", 64), sig); v.Valid {
|
||||||
|
t.Fatal("verified for another digest")
|
||||||
|
}
|
||||||
|
tc, err := VerifyMessage(kp.Public, []byte(d), []byte(sig.Value))
|
||||||
|
if err != nil || tc != "yet package digest: "+d {
|
||||||
|
t.Fatalf("trusted comment %q %v", tc, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestKeyFiles(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
kp, _ := GenerateKey()
|
||||||
|
for _, pw := range []string{"", "secret"} {
|
||||||
|
p := filepath.Join(dir, "k"+pw)
|
||||||
|
if err := SaveKeyPair(p, kp, pw); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
got, err := LoadPrivateKey(p, pw)
|
||||||
|
if err != nil || !SameKey(got.Public, kp.Public) {
|
||||||
|
t.Fatalf("password %q: %v", pw, err)
|
||||||
|
}
|
||||||
|
pub, err := LoadPublicKey(p + ".pub")
|
||||||
|
if err != nil || !SameKey(pub, kp.Public) {
|
||||||
|
t.Fatalf(".pub: %v", err)
|
||||||
|
}
|
||||||
|
if fi, _ := os.Stat(p); os.PathSeparator == '/' && fi.Mode().Perm() != 0o600 {
|
||||||
|
t.Fatalf("private key mode %v", fi.Mode())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := LoadPrivateKey(filepath.Join(dir, "ksecret"), "wrong"); err == nil {
|
||||||
|
t.Fatal("wrong password accepted")
|
||||||
|
}
|
||||||
|
b, _ := MarshalPublicKey(kp.Public)
|
||||||
|
if !bytes.HasPrefix(b, []byte("untrusted comment: minisign public key: "+string(ID(kp.Public))+"\n")) {
|
||||||
|
t.Fatalf("public key file: %s", b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEd25519AndPEM(t *testing.T) {
|
||||||
|
kp := vectorKey(t)
|
||||||
|
sig, err := SignEd25519(kp, []byte("statement"))
|
||||||
|
if err != nil || !VerifyEd25519(kp.Public, []byte("statement"), sig) || VerifyEd25519(kp.Public, []byte("statemenT"), sig) {
|
||||||
|
t.Fatalf("ed25519: %v", err)
|
||||||
|
}
|
||||||
|
p, err := ExportPEM(kp.Public)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
back, err := ImportPEM(p, nil)
|
||||||
|
if err != nil || !SameKey(back, kp.Public) {
|
||||||
|
t.Fatalf("PEM round trip: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestKeyFromRef(t *testing.T) {
|
||||||
|
kp := vectorKey(t)
|
||||||
|
priv, _ := os.ReadFile("testdata/sign-vector.key")
|
||||||
|
r := secretref.New("")
|
||||||
|
got, err := KeyFromRef(context.Background(), r, "base64:"+base64.StdEncoding.EncodeToString(priv), "")
|
||||||
|
if err != nil || !SameKey(got.Public, kp.Public) {
|
||||||
|
t.Fatalf("base64 ref: %v", err)
|
||||||
|
}
|
||||||
|
enc, _ := minisign.EncryptKey("pw", kp.Private)
|
||||||
|
t.Setenv("TRUST_TEST_KEY", string(enc))
|
||||||
|
t.Setenv("TRUST_TEST_PW", "pw")
|
||||||
|
if got, err = KeyFromRef(context.Background(), r, "env:TRUST_TEST_KEY", "env:TRUST_TEST_PW"); err != nil || !SameKey(got.Public, kp.Public) {
|
||||||
|
t.Fatalf("encrypted env ref: %v", err)
|
||||||
|
}
|
||||||
|
t.Setenv("TRUST_TEST_PW", "wrong-password")
|
||||||
|
_, err = KeyFromRef(context.Background(), secretref.New(""), "env:TRUST_TEST_KEY", "env:TRUST_TEST_PW")
|
||||||
|
if err == nil || strings.Contains(err.Error(), "wrong-password") {
|
||||||
|
t.Fatalf("wrong password: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := KeyFromRef(context.Background(), r, "not a reference", ""); err == nil {
|
||||||
|
t.Fatal("plaintext accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
untrusted comment: minisign encrypted secret key
|
||||||
|
RWQAAEIyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMIu3pReIO0jw8qjsUD5/2J9b2y+5BNJ45wLRkzgleRSUugHITToa6mGDGFfLcgDIs+wEX4vFIUfnrLqym+u2yLqBzAYgET+YAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
yabar evidence vector
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
untrusted comment:
|
||||||
|
RWQwi7elF4g7SHc9svLZOCFGGEyXs7gZW6saVrYobxgEdJnDM9hIxHIlC+qxicazJFYu2E6TG3Qq62NTae3k0rDlomfkxxz8oQA=
|
||||||
|
trusted comment: yabar evidence test-vector 00000000000000000000000000000000
|
||||||
|
o0/jQwycdwDK6BrI5jc6N47P2ixexYe07twyQ587cJBWapfKNch5EmMgvvwR6thKci3Lg0dpuanBDFdVP9QcCw==
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
untrusted comment: minisign public key: 483B8817A5B78B30
|
||||||
|
RWQwi7elF4g7SGGDGFfLcgDIs+wEX4vFIUfnrLqym+u2yLqBzAYgET+Y
|
||||||
Reference in new issue
Block a user