feat(sdk): 应用心跳 CRL 并实现离线强制校验点
- 心跳响应携带的 CRL 验签后应用,版本单调防回滚,命中当前 jti 立即转 REVOKED - 新增 WithValidationServer + pol.forced_check:文件模式到期强制在线校验,验签 verdict 并检查时效,失败转 EXPIRED - 未配置校验源时保持完全离线兼容(告警跳过) - 新增 CRL 应用/回滚防护/强制校验/伪造 verdict 测试
This commit is contained in:
6 files changed
+577
-20
No files matched your search
+108
-5
@@ -65,11 +65,12 @@ type proxyEntry struct {
|
||||
// TrustChain 是验证方的信任根集合:root 公钥 + signing 公钥(按 kid)+
|
||||
// 受约束 Proxy Key(按 kid)+ 本地 CRL。并发安全:读多写少,RWMutex 保护。
|
||||
type TrustChain struct {
|
||||
mu sync.RWMutex
|
||||
root ed25519.PublicKey
|
||||
keys map[string]signingEntry
|
||||
proxies map[string]proxyEntry
|
||||
revoked map[string]struct{}
|
||||
mu sync.RWMutex
|
||||
root ed25519.PublicKey
|
||||
keys map[string]signingEntry
|
||||
proxies map[string]proxyEntry
|
||||
revoked map[string]struct{}
|
||||
crlVersion int64
|
||||
}
|
||||
|
||||
// NewTrustChain 以 root 公钥为锚点创建空信任链。
|
||||
@@ -218,6 +219,108 @@ func (tc *TrustChain) RevokedList() []string {
|
||||
return out
|
||||
}
|
||||
|
||||
// CRLPayload 是服务端物化 CRL 的载荷(docs/design/04-crypto.md §4)。
|
||||
type CRLPayload struct {
|
||||
V int `json:"v"`
|
||||
Version int64 `json:"version"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
Revoked []string `json:"revoked"`
|
||||
}
|
||||
|
||||
// LoadCRL 校验并应用服务端下发的 CRL(typ=crl,active signing key 签名)。
|
||||
// 版本单调:版本号不大于本地已应用版本时忽略(防回滚),返回 applied=false。
|
||||
// 已吊销条目只增不减——即使新 CRL 遗漏旧条目也不会“复活”。
|
||||
func (tc *TrustChain) LoadCRL(token string) (version int64, applied bool, err error) {
|
||||
h, payload, sig, err := license.Decode(token)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
if h.Typ != license.TypCRL {
|
||||
return 0, false, fmt.Errorf("%w: typ %q 非 crl", license.ErrUnsupported, h.Typ)
|
||||
}
|
||||
pub, ok := tc.KeyFor(h.Kid)
|
||||
if !ok {
|
||||
return 0, false, fmt.Errorf("%w: kid %q", ErrUnknownKey, h.Kid)
|
||||
}
|
||||
content, err := license.SignedContent(token)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
if !ed25519.Verify(pub, []byte(content), sig) {
|
||||
return 0, false, ErrBadSignature
|
||||
}
|
||||
var p CRLPayload
|
||||
if err := json.Unmarshal(payload, &p); err != nil {
|
||||
return 0, false, fmt.Errorf("%w: crl payload: %v", license.ErrMalformed, err)
|
||||
}
|
||||
if p.V != 1 || p.Version <= 0 {
|
||||
return 0, false, fmt.Errorf("%w: crl 版本字段非法(v=%d version=%d)", license.ErrMalformed, p.V, p.Version)
|
||||
}
|
||||
|
||||
tc.mu.Lock()
|
||||
defer tc.mu.Unlock()
|
||||
if p.Version <= tc.crlVersion {
|
||||
return p.Version, false, nil
|
||||
}
|
||||
for _, jti := range p.Revoked {
|
||||
if jti != "" {
|
||||
tc.revoked[jti] = struct{}{}
|
||||
}
|
||||
}
|
||||
tc.crlVersion = p.Version
|
||||
return p.Version, true, nil
|
||||
}
|
||||
|
||||
// CRLVersion 返回本地已应用的最大 CRL 版本(0 = 尚未应用)。
|
||||
func (tc *TrustChain) CRLVersion() int64 {
|
||||
tc.mu.RLock()
|
||||
defer tc.mu.RUnlock()
|
||||
return tc.crlVersion
|
||||
}
|
||||
|
||||
// Verdict 是在线校验结论(typ=verdict)的载荷。
|
||||
type Verdict struct {
|
||||
JTI string `json:"jti"`
|
||||
State string `json:"state"`
|
||||
At int64 `json:"at"`
|
||||
}
|
||||
|
||||
// VerdictMaxAgeSeconds 是 verdict 的保鲜窗口:过期结论不得用于放行(防重放)。
|
||||
const VerdictMaxAgeSeconds int64 = 600
|
||||
|
||||
// VerifyVerdict 校验在线校验结论的签名与时效,返回可采信的结论。
|
||||
func (tc *TrustChain) VerifyVerdict(token string, now int64) (*Verdict, error) {
|
||||
h, payload, sig, err := license.Decode(token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if h.Typ != license.TypVerdict {
|
||||
return nil, fmt.Errorf("%w: typ %q 非 verdict", license.ErrUnsupported, h.Typ)
|
||||
}
|
||||
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 v Verdict
|
||||
if err := json.Unmarshal(payload, &v); err != nil {
|
||||
return nil, fmt.Errorf("%w: verdict payload: %v", license.ErrMalformed, err)
|
||||
}
|
||||
if v.JTI == "" || v.State == "" {
|
||||
return nil, fmt.Errorf("%w: verdict 缺少 jti/state", license.ErrMalformed)
|
||||
}
|
||||
if v.At != 0 && now-v.At > VerdictMaxAgeSeconds {
|
||||
return nil, fmt.Errorf("%w: verdict 已过期(age=%ds)", license.ErrMalformed, now-v.At)
|
||||
}
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
// SignBundle 把当前 signing 集合与 CRL 打包为 root 签名的 trust bundle token。
|
||||
func (tc *TrustChain) SignBundle(rootPriv ed25519.PrivateKey) (string, error) {
|
||||
tc.mu.RLock()
|
||||
|
||||
Reference in new issue
Block a user