Releases carry SHA256SUMS signed in the minisign format (prehashed Ed25519, the release tag as trusted comment) and an SPDX SBOM of the Go modules and the npm packages the frontend build bundles. internal/signing signs and verifies; the release keys are committed in release.pub and compiled in, so the self-updating agents can check their downloads. Without the RELEASE_SIGNING_KEY secret a release is published unsigned with a warning. Closes #34 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
194 lines
6.0 KiB
Go
194 lines
6.0 KiB
Go
// Package signing signs and verifies release files in the minisign format,
|
|
// so that anyone can check a release with the minisign tool and the
|
|
// self-updating agents can check what they download.
|
|
//
|
|
// Only prehashed signatures (algorithm "ED": Ed25519 over the BLAKE2b-512
|
|
// hash of the file) are produced and accepted.
|
|
package signing
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
_ "embed"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"golang.org/x/crypto/blake2b"
|
|
)
|
|
|
|
var (
|
|
// ErrSignature reports a signature that does not match.
|
|
ErrSignature = errors.New("signing: signature does not match")
|
|
// ErrUnknownKey reports a signature made with a key not trusted.
|
|
ErrUnknownKey = errors.New("signing: signed with an unknown key")
|
|
)
|
|
|
|
const (
|
|
keyAlg = "Ed"
|
|
prehashedAlg = "ED"
|
|
idLen = 8
|
|
)
|
|
|
|
// PublicKey is a minisign public key.
|
|
type PublicKey struct {
|
|
ID [idLen]byte
|
|
Key ed25519.PublicKey
|
|
}
|
|
|
|
// PrivateKey is a signing key with the ID of its public key.
|
|
type PrivateKey struct {
|
|
ID [idLen]byte
|
|
Key ed25519.PrivateKey
|
|
}
|
|
|
|
// Public returns the key that verifies k's signatures.
|
|
func (k PrivateKey) Public() PublicKey {
|
|
return PublicKey{ID: k.ID, Key: k.Key.Public().(ed25519.PublicKey)}
|
|
}
|
|
|
|
// String encodes the key like minisign does on the second line of a .pub
|
|
// file.
|
|
func (k PublicKey) String() string {
|
|
raw := append(append([]byte(keyAlg), k.ID[:]...), k.Key...)
|
|
return base64.StdEncoding.EncodeToString(raw)
|
|
}
|
|
|
|
// IDString is the key ID as minisign prints it.
|
|
func (k PublicKey) IDString() string {
|
|
var b strings.Builder
|
|
for i := idLen - 1; i >= 0; i-- {
|
|
fmt.Fprintf(&b, "%02X", k.ID[i])
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// ParsePublicKey reads a key in minisign's encoding.
|
|
func ParsePublicKey(s string) (PublicKey, error) {
|
|
raw, err := base64.StdEncoding.DecodeString(strings.TrimSpace(s))
|
|
if err != nil || len(raw) != 2+idLen+ed25519.PublicKeySize || string(raw[:2]) != keyAlg {
|
|
return PublicKey{}, errors.New("signing: not a minisign public key")
|
|
}
|
|
var k PublicKey
|
|
copy(k.ID[:], raw[2:])
|
|
k.Key = ed25519.PublicKey(raw[2+idLen:])
|
|
return k, nil
|
|
}
|
|
|
|
// ParsePublicKeys reads one key per line, skipping blank lines, comments
|
|
// starting with # and minisign's "untrusted comment:" lines.
|
|
func ParsePublicKeys(text string) ([]PublicKey, error) {
|
|
var keys []PublicKey
|
|
for line := range strings.Lines(text) {
|
|
line = strings.TrimSpace(line)
|
|
if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, "untrusted comment:") {
|
|
continue
|
|
}
|
|
k, err := ParsePublicKey(line)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys = append(keys, k)
|
|
}
|
|
return keys, nil
|
|
}
|
|
|
|
// GenerateKey creates a new signing key.
|
|
func GenerateKey() (PrivateKey, error) {
|
|
_, priv, err := ed25519.GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
return PrivateKey{}, err
|
|
}
|
|
k := PrivateKey{Key: priv}
|
|
if _, err := rand.Read(k.ID[:]); err != nil {
|
|
return PrivateKey{}, err
|
|
}
|
|
return k, nil
|
|
}
|
|
|
|
// String encodes the key for storage in a secret: base64 of the key ID and
|
|
// the Ed25519 seed. It is not minisign's encrypted key format.
|
|
func (k PrivateKey) String() string {
|
|
return base64.StdEncoding.EncodeToString(append(k.ID[:], k.Key.Seed()...))
|
|
}
|
|
|
|
// ParsePrivateKey reads a key written by PrivateKey.String.
|
|
func ParsePrivateKey(s string) (PrivateKey, error) {
|
|
raw, err := base64.StdEncoding.DecodeString(strings.TrimSpace(s))
|
|
if err != nil || len(raw) != idLen+ed25519.SeedSize {
|
|
return PrivateKey{}, errors.New("signing: not a signing key")
|
|
}
|
|
var k PrivateKey
|
|
copy(k.ID[:], raw)
|
|
k.Key = ed25519.NewKeyFromSeed(raw[idLen:])
|
|
return k, nil
|
|
}
|
|
|
|
func digest(msg []byte) []byte {
|
|
sum := blake2b.Sum512(msg)
|
|
return sum[:]
|
|
}
|
|
|
|
// Sign returns a minisign signature file for msg. The trusted comment is
|
|
// signed too; it must be a single line.
|
|
func Sign(k PrivateKey, msg []byte, trusted string) ([]byte, error) {
|
|
if strings.ContainsAny(trusted, "\r\n") {
|
|
return nil, errors.New("signing: the trusted comment must be one line")
|
|
}
|
|
sig := ed25519.Sign(k.Key, digest(msg))
|
|
global := ed25519.Sign(k.Key, append(append([]byte{}, sig...), trusted...))
|
|
var b bytes.Buffer
|
|
fmt.Fprintf(&b, "untrusted comment: signature from uptimesebi release key %s\n", k.Public().IDString())
|
|
b.WriteString(base64.StdEncoding.EncodeToString(append(append([]byte(prehashedAlg), k.ID[:]...), sig...)) + "\n")
|
|
b.WriteString("trusted comment: " + trusted + "\n")
|
|
b.WriteString(base64.StdEncoding.EncodeToString(global) + "\n")
|
|
return b.Bytes(), nil
|
|
}
|
|
|
|
// Verify checks a minisign signature file for msg against the trusted
|
|
// keys and returns its trusted comment.
|
|
func Verify(keys []PublicKey, msg, sigFile []byte) (string, error) {
|
|
lines := strings.Split(strings.TrimRight(string(sigFile), "\n"), "\n")
|
|
if len(lines) != 4 || !strings.HasPrefix(lines[0], "untrusted comment:") || !strings.HasPrefix(lines[2], "trusted comment: ") {
|
|
return "", errors.New("signing: not a minisign signature")
|
|
}
|
|
raw, err := base64.StdEncoding.DecodeString(strings.TrimSpace(lines[1]))
|
|
if err != nil || len(raw) != 2+idLen+ed25519.SignatureSize {
|
|
return "", errors.New("signing: not a minisign signature")
|
|
}
|
|
if string(raw[:2]) != prehashedAlg {
|
|
return "", errors.New("signing: only prehashed signatures are accepted")
|
|
}
|
|
global, err := base64.StdEncoding.DecodeString(strings.TrimSpace(lines[3]))
|
|
if err != nil || len(global) != ed25519.SignatureSize {
|
|
return "", errors.New("signing: not a minisign signature")
|
|
}
|
|
sig := raw[2+idLen:]
|
|
trusted := strings.TrimSuffix(strings.TrimPrefix(lines[2], "trusted comment: "), "\r")
|
|
for _, k := range keys {
|
|
if !bytes.Equal(k.ID[:], raw[2:2+idLen]) {
|
|
continue
|
|
}
|
|
if !ed25519.Verify(k.Key, digest(msg), sig) || !ed25519.Verify(k.Key, append(append([]byte{}, sig...), trusted...), global) {
|
|
return "", ErrSignature
|
|
}
|
|
return trusted, nil
|
|
}
|
|
return "", ErrUnknownKey
|
|
}
|
|
|
|
//go:embed release.pub
|
|
var releaseKeys string
|
|
|
|
// ReleaseKeys are the keys that sign UptimeSebi releases. Old keys stay
|
|
// listed after a rotation so that agents can update across it.
|
|
func ReleaseKeys() []PublicKey {
|
|
keys, err := ParsePublicKeys(releaseKeys)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return keys
|
|
}
|