111 lines
3.3 KiB
Go
111 lines
3.3 KiB
Go
package plugin
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// EnvPluginExtraKeys optionally points to a JSON file or inline JSON array of
|
|
// extra ed25519 public signing keys (useful for local/enterprise verification
|
|
// and integration tests without recompiling the binary).
|
|
const EnvPluginExtraKeys = "REIMS_GO_PLUGIN_EXTRA_KEYS"
|
|
|
|
type rawSigningKey struct {
|
|
KeyID string `json:"key_id"`
|
|
Publisher string `json:"publisher"`
|
|
Trust TrustLevel `json:"trust"`
|
|
PublicKey string `json:"public_key"` // hex or standard base64 (32 bytes)
|
|
}
|
|
|
|
// builtinTrustedKeys holds official/verified ed25519 public keys published by
|
|
// the reims-go organization. Populate entries once org release keys are minted
|
|
// in reims-go/plugins/keys/.
|
|
var builtinTrustedKeys = []rawSigningKey{}
|
|
|
|
// DefaultKeyring returns the trusted ed25519 signing keyring built into the
|
|
// host binary plus any optional extra keys configured via REIMS_GO_PLUGIN_EXTRA_KEYS.
|
|
func DefaultKeyring() []SigningKey {
|
|
var out []SigningKey
|
|
for _, rk := range builtinTrustedKeys {
|
|
if k, err := parseSigningKey(rk); err == nil {
|
|
out = append(out, k)
|
|
}
|
|
}
|
|
if extra := strings.TrimSpace(os.Getenv(EnvPluginExtraKeys)); extra != "" {
|
|
raw := []byte(extra)
|
|
if !strings.HasPrefix(extra, "[") {
|
|
if fileBytes, err := os.ReadFile(extra); err == nil {
|
|
raw = fileBytes
|
|
}
|
|
}
|
|
var list []rawSigningKey
|
|
if err := json.Unmarshal(raw, &list); err == nil {
|
|
for _, rk := range list {
|
|
if k, err := parseSigningKey(rk); err == nil {
|
|
out = append(out, k)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func parseSigningKey(rk rawSigningKey) (SigningKey, error) {
|
|
pubBytes, err := decodeEd25519KeyBytes(rk.PublicKey, ed25519.PublicKeySize)
|
|
if err != nil {
|
|
return SigningKey{}, fmt.Errorf("invalid public key %q: %w", rk.KeyID, err)
|
|
}
|
|
trust := rk.Trust
|
|
if trust != TrustOfficial && trust != TrustVerified {
|
|
trust = TrustVerified
|
|
}
|
|
return SigningKey{
|
|
KeyID: strings.TrimSpace(rk.KeyID),
|
|
Publisher: strings.TrimSpace(rk.Publisher),
|
|
Trust: trust,
|
|
PublicKey: ed25519.PublicKey(pubBytes),
|
|
}, nil
|
|
}
|
|
|
|
// ParseEd25519PrivateKey decodes a 32-byte seed or 64-byte ed25519 private key
|
|
// from hex or base64 text (or from a file path containing that text).
|
|
func ParseEd25519PrivateKey(spec string) (ed25519.PrivateKey, error) {
|
|
s := strings.TrimSpace(spec)
|
|
if s == "" {
|
|
return nil, fmt.Errorf("empty private key")
|
|
}
|
|
if st, err := os.Stat(s); err == nil && !st.IsDir() {
|
|
raw, rErr := os.ReadFile(s)
|
|
if rErr != nil {
|
|
return nil, rErr
|
|
}
|
|
s = strings.TrimSpace(string(raw))
|
|
}
|
|
if b, err := decodeEd25519KeyBytes(s, ed25519.PrivateKeySize); err == nil {
|
|
return ed25519.PrivateKey(b), nil
|
|
}
|
|
if seed, err := decodeEd25519KeyBytes(s, ed25519.SeedSize); err == nil {
|
|
return ed25519.NewKeyFromSeed(seed), nil
|
|
}
|
|
return nil, fmt.Errorf("expected 32-byte seed or 64-byte ed25519 private key in hex or base64")
|
|
}
|
|
|
|
func decodeEd25519KeyBytes(raw string, wantLen int) ([]byte, error) {
|
|
s := strings.TrimSpace(raw)
|
|
if b, err := hex.DecodeString(s); err == nil && len(b) == wantLen {
|
|
return b, nil
|
|
}
|
|
if b, err := base64.StdEncoding.DecodeString(s); err == nil && len(b) == wantLen {
|
|
return b, nil
|
|
}
|
|
if b, err := base64.RawStdEncoding.DecodeString(s); err == nil && len(b) == wantLen {
|
|
return b, nil
|
|
}
|
|
return nil, fmt.Errorf("key length != %d", wantLen)
|
|
}
|