feat: implement license platform business flows and delivery tooling
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation.

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-09 15:33:22 +08:00
parent 3ab651d3a8
commit 54c318c615
142 changed files with 11105 additions and 178 deletions

View File

@@ -0,0 +1,91 @@
package httpapi
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"os"
"path/filepath"
"regexp"
"sort"
)
type VerificationKey struct {
KeyID string `json:"key_id"`
PublicKeyPEM string `json:"public_key_pem"`
Revoked bool `json:"revoked"`
}
type Keyring struct {
Current string `json:"current_key_id"`
Keys []VerificationKey `json:"keys"`
}
var keyIDPattern = regexp.MustCompile(`^[a-zA-Z0-9_-]{1,64}$`)
func LoadKeyring(path, privatePEM string) (*Keyring, error) {
if path == "" {
return nil, nil
}
if !filepath.IsAbs(path) {
return nil, errors.New("keyring file must be absolute")
}
info, err := os.Stat(path)
if err != nil || !info.Mode().IsRegular() || info.Size() > 65536 {
return nil, errors.New("invalid keyring file")
}
data, err := os.ReadFile(path)
if err != nil {
return nil, errors.New("keyring unavailable")
}
var ring Keyring
if json.Unmarshal(data, &ring) != nil {
return nil, errors.New("invalid keyring JSON")
}
if err := ring.Validate(privatePEM); err != nil {
return nil, err
}
return &ring, nil
}
func (r *Keyring) Validate(privatePEM string) error {
if !keyIDPattern.MatchString(r.Current) || len(r.Keys) == 0 || len(r.Keys) > 32 {
return errors.New("invalid keyring")
}
private, err := readSigningKey(privatePEM)
if err != nil {
return errors.New("invalid current private key")
}
seen := map[string]bool{}
current := false
for _, k := range r.Keys {
if !keyIDPattern.MatchString(k.KeyID) || seen[k.KeyID] {
return errors.New("duplicate or invalid key ID")
}
seen[k.KeyID] = true
block, rest := pem.Decode([]byte(k.PublicKeyPEM))
if block == nil || len(rest) != 0 {
return errors.New("invalid public key")
}
parsed, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return errors.New("invalid public key")
}
pub, ok := parsed.(*ecdsa.PublicKey)
if !ok || pub.Curve != elliptic.P256() {
return errors.New("public key must be P256")
}
if k.KeyID == r.Current {
if k.Revoked || !pub.Equal(&private.PublicKey) {
return errors.New("current key revoked or mismatched")
}
current = true
}
}
if !current {
return errors.New("current key missing")
}
sort.Slice(r.Keys, func(i, j int) bool { return r.Keys[i].KeyID < r.Keys[j].KeyID })
return nil
}