Files
iceking2nd fc4ef4e644
sdk-ci / test (push) Successful in 2m19s
sdk-release / release (push) Failing after 50s
sdk-release / cleanup (push) Successful in 5s
refactor(crypto): 拆分签发与验证,新增 sign 包
- crypto 仅保留验证:验签、信任链、CRL/verdict、指纹
- 新增 sign 包:SignToken/SignBundle、密钥生成、私钥加解密
- TrustChain 移除 SignBundle 方法,改由 Bundle() 导出快照,
  避免反射可达性把 SignToken 链入纯验证二进制
- Bundle 快照单次读锁,修复旧 SignBundle 递归读锁死锁窗口
- 新增 golden token 与 go tool nm 符号级回归测试
2026-11-15 13:26:50 +08:00

134 lines
4.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package crypto
import (
"crypto/ed25519"
"encoding/json"
"fmt"
"git.wxccs.org/EntitleForge/EntitleForge-SDK-go/license"
)
// DefaultClockSkewSeconds 是时间窗校验容忍的时钟偏移(±10 分钟),
// 依据 docs/design/04-crypto.md §6。
const DefaultClockSkewSeconds int64 = 600
// VerifyToken 验证 license / lease token 并返回解析后的载荷。
// 编排顺序(docs/design/03-license-format.md §7):
// 结构解码 → typ 白名单 → 信任链找公钥(signing / 受约束 proxy)→ Ed25519 验签 →
// 严格 payload 解析 → 语义校验 → 本地 CRL → 时间窗 →(proxy 路径)授权上限校验。
func VerifyToken(token string, tc *TrustChain, now int64) (*license.Payload, error) {
h, payload, sig, err := license.Decode(token)
if err != nil {
return nil, err
}
if h.Typ != license.TypLicense && h.Typ != license.TypLease {
return nil, fmt.Errorf("%w: typ %q 不能按 license/lease 验证", license.ErrUnsupported, h.Typ)
}
// proxy 路径优先(kid 命中受约束 Proxy Key)
if pub, entry, ok := tc.ProxyKeyFor(h.Kid); ok {
return verifyProxyLease(token, h, payload, sig, pub, entry, tc, now)
}
pub, ok := tc.KeyFor(h.Kid)
if !ok {
return nil, fmt.Errorf("%w: kid %q", ErrUnknownKey, h.Kid)
}
content, err := license.SignedContent(token)
if err != nil {
return nil, err
}
if !ed25519.Verify(pub, []byte(content), sig) {
return nil, ErrBadSignature
}
var p license.Payload
if err := json.Unmarshal(payload, &p); err != nil {
return nil, fmt.Errorf("%w: payload: %v", license.ErrMalformed, err)
}
if err := p.Validate(); err != nil {
return nil, err
}
if tc.IsRevoked(p.JTI) {
return nil, ErrRevoked
}
if err := p.TimeValid(now, DefaultClockSkewSeconds); err != nil {
return nil, err
}
return &p, nil
}
// verifyProxyLease 验证 Proxy Key 派发的 per-workload lease:
// 验签 + 常规校验 + 受约束(Proxy scope 必带、ent ⊆ 证书上限、证书未过期、
// 集群一致)。任何越权(数量超限/新增授权/档位抬升/隐匿 scope)拒绝。
func verifyProxyLease(token string, h license.Header, payload, sig []byte,
pub ed25519.PublicKey, entry proxyEntry, tc *TrustChain, now int64) (*license.Payload, error) {
content, err := license.SignedContent(token)
if err != nil {
return nil, err
}
if !ed25519.Verify(pub, []byte(content), sig) {
return nil, ErrBadSignature
}
if entry.notAfter != 0 && entry.notAfter <= now {
return nil, fmt.Errorf("%w: proxycert 已过期", ErrUnknownKey)
}
var p license.Payload
if err := json.Unmarshal(payload, &p); err != nil {
return nil, fmt.Errorf("%w: payload: %v", license.ErrMalformed, err)
}
if err := p.Validate(); err != nil {
return nil, err
}
if tc.IsRevoked(p.JTI) {
return nil, ErrRevoked
}
if err := p.TimeValid(now, DefaultClockSkewSeconds); err != nil {
return nil, err
}
// 受约束校验(docs/design/08-k8s-proxy.md §3:Proxy 被攻破也无法扩权)
if p.Proxy == nil {
return nil, fmt.Errorf("%w: proxy lease 缺少受约束声明", ErrBadSignature)
}
if entry.cluster != "" && p.Proxy.Cluster != entry.cluster {
return nil, fmt.Errorf("%w: proxy cluster 不符", ErrBadSignature)
}
for fid, ent := range p.Ent {
max, ok := entry.maxEnt[fid]
if !ok {
return nil, fmt.Errorf("%w: proxy 越权(授权项 %q 未在证书上限内)", ErrBadSignature, fid)
}
if !withinEntitlement(ent, max) {
return nil, fmt.Errorf("%w: proxy 越权(授权项 %q 超出上限)", ErrBadSignature, fid)
}
}
return &p, nil
}
// withinEntitlement 判断授权值是否不超上限(保守策略):
// q: v ≤ max;b: true 需 max 为 true;t: 必须等于证书档位(档位序在调用方);
// c: 列表必须为上限子集。
func withinEntitlement(v, max license.Entitlement) bool {
switch v.Type {
case license.TypeQuantity:
return max.Type == license.TypeQuantity && v.Int <= max.Int
case license.TypeBool:
return max.Type == license.TypeBool && (!v.Bool || max.Bool)
case license.TypeTier:
return max.Type == license.TypeTier && v.Str == max.Str
case license.TypeConstraint:
if max.Type != license.TypeConstraint {
return false
}
for _, item := range v.List {
if !max.Allows(item) {
return false
}
}
return true
}
return false
}