Moved from yet's internal/trust without changing behavior or file formats: KeyPair, GenerateKey, SaveKeyPair, LoadPrivateKey, ParsePrivateKey, LoadPublicKey, MarshalPublicKey, SignEd25519, VerifyEd25519, ExportPEM, ImportPEM, and the package signature as SignDigest. New: Sign and VerifyMessage, a detached minisign signature over any bytes and its check (returns the trusted comment; another key with a colliding ID fails), and KeyFromRef, a key pair from a core/secretref reference with error messages that never carry the secret. A golden vector (testdata/sign-vector.*) pins the output, and VerifySignature is tested against what SignDigest makes. For yabar's evidence signing (ADR-0037 there, owner decision Q-5 option A); yet keeps registry signing and its auto-sign key store. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
166 lines
5.4 KiB
Go
166 lines
5.4 KiB
Go
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")
|
|
}
|
|
}
|