90 lines
2.9 KiB
Go
90 lines
2.9 KiB
Go
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
|
||
|
|
}
|