Merge remote-tracking branch 'origin/feat/release-signing' into feat/autoupdate
# Conflicts: # README.md
This commit is contained in:
commit
431048e244
10 files changed
+677
-6
No files matched your search
@@ -0,0 +1,4 @@
|
||||
# Keys that sign UptimeSebi releases, one per line. Keep retired keys
|
||||
# listed so agents can update across a rotation.
|
||||
untrusted comment: uptimesebi release key 8359FCBB1309986B
|
||||
RWRrmAkTu/xZg/o8QCsko5rUujqW/N165cj+dyEcp5CDXPTE2jKbLkyA
|
||||
@@ -0,0 +1,193 @@
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package signing
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSignVerify(t *testing.T) {
|
||||
k, err := GenerateKey()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pub := k.Public()
|
||||
msg := []byte("abc uptimesebi_1.0.0_linux_amd64.tar.gz\n")
|
||||
sig, err := Sign(k, msg, "v1.0.0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := Verify([]PublicKey{pub}, msg, sig); err != nil || got != "v1.0.0" {
|
||||
t.Fatalf("Verify = %q, %v", got, err)
|
||||
}
|
||||
|
||||
other, _ := GenerateKey()
|
||||
if _, err := Verify([]PublicKey{other.Public()}, msg, sig); !errors.Is(err, ErrUnknownKey) {
|
||||
t.Errorf("unknown key: %v", err)
|
||||
}
|
||||
if _, err := Verify([]PublicKey{other.Public(), pub}, msg, sig); err != nil {
|
||||
t.Errorf("rotated key list: %v", err)
|
||||
}
|
||||
if _, err := Verify([]PublicKey{pub}, append(msg, 'x'), sig); !errors.Is(err, ErrSignature) {
|
||||
t.Errorf("changed file: %v", err)
|
||||
}
|
||||
forged := bytes.Replace(sig, []byte("trusted comment: v1.0.0"), []byte("trusted comment: v9.0.0"), 1)
|
||||
if _, err := Verify([]PublicKey{pub}, msg, forged); !errors.Is(err, ErrSignature) {
|
||||
t.Errorf("changed trusted comment: %v", err)
|
||||
}
|
||||
if _, err := Sign(k, msg, "a\nb"); err == nil {
|
||||
t.Error("multi-line trusted comment accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoding(t *testing.T) {
|
||||
k, _ := GenerateKey()
|
||||
back, err := ParsePrivateKey(k.String())
|
||||
if err != nil || back.ID != k.ID || !back.Key.Equal(k.Key) {
|
||||
t.Fatalf("private key round trip: %v", err)
|
||||
}
|
||||
keys, err := ParsePublicKeys("# comment\nuntrusted comment: minisign public key\n" + k.Public().String() + "\n\n")
|
||||
if err != nil || len(keys) != 1 || keys[0].ID != k.ID || !keys[0].Key.Equal(k.Public().Key) {
|
||||
t.Fatalf("public keys = %v, %v", keys, err)
|
||||
}
|
||||
if !strings.HasPrefix(k.Public().String(), "RW") {
|
||||
t.Errorf("public key %s does not look like minisign's", k.Public())
|
||||
}
|
||||
if _, err := ParsePublicKey("RWQ" + strings.Repeat("A", 10)); err == nil {
|
||||
t.Error("short key accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReleaseKeys(t *testing.T) {
|
||||
if len(ReleaseKeys()) == 0 {
|
||||
t.Error("no release key")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user