refactor(crypto): 拆分签发与验证,新增 sign 包
- crypto 仅保留验证:验签、信任链、CRL/verdict、指纹 - 新增 sign 包:SignToken/SignBundle、密钥生成、私钥加解密 - TrustChain 移除 SignBundle 方法,改由 Bundle() 导出快照, 避免反射可达性把 SignToken 链入纯验证二进制 - Bundle 快照单次读锁,修复旧 SignBundle 递归读锁死锁窗口 - 新增 golden token 与 go tool nm 符号级回归测试
This commit is contained in:
21 files changed
+461
-168
No files matched your search
@@ -0,0 +1,146 @@
|
||||
package sign
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ed25519"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.wxccs.org/EntitleForge/EntitleForge-SDK-go/crypto"
|
||||
)
|
||||
|
||||
// goldenBundleToken 由拆分前的 v0.5.x 实现生成,锁定线格式零变更。
|
||||
const goldenBundleToken = "eyJ2IjoxLCJhbGciOiJFZDI1NTE5Iiwia2lkIjoicm9vdCIsInR5cCI6InRydXN0In0.eyJ2IjoxLCJzaWduaW5nIjpbeyJraWQiOiJraWQtYWN0aXZlIiwia2V5IjoiZ1RsM0RxaDlGMTlXbzFSbXcweC16TXVOaXBHMDdqZWlYZllQVzRfSnM1USIsInN0YXR1cyI6ImFjdGl2ZSIsIm5vdF9hZnRlciI6MH0seyJraWQiOiJraWQtcmV0aXJlZCIsImtleSI6IjdVa294aWpSd3NicTZRTTRrRm1WWVNsWkp6cGNZX2syTnNGR0ZLeUhOOUUiLCJzdGF0dXMiOiJyZXRpcmVkIiwibm90X2FmdGVyIjoxNzk0MjAxNjAwfV0sImNybCI6WyIwMUpEUkVWT0tFRDAwMDAwMDAwMDAwMDAwMCIsIjAxSkRSRVZPS0VEMDAwMDAwMDAwMDAwMDAxIl19.GUzfW--VNynzVWWlbX2HssxelfcIiEmeLm1R5WjwNGnDTUSnD-XG2x2fo3Hd7rsPXYmKV7nVshZq8Hj3yudiDA"
|
||||
|
||||
// fixedKey 由固定种子派生密钥,保证 bundle 输出可逐字节比较。
|
||||
func fixedKey(seed byte) *KeyPair {
|
||||
s := bytes.Repeat([]byte{seed}, ed25519.SeedSize)
|
||||
priv := ed25519.NewKeyFromSeed(s)
|
||||
return &KeyPair{Pub: priv.Public().(ed25519.PublicKey), Priv: priv}
|
||||
}
|
||||
|
||||
func fixedChain(t *testing.T) (*KeyPair, *crypto.TrustChain) {
|
||||
t.Helper()
|
||||
root := fixedKey(0x01)
|
||||
sk1 := fixedKey(0x02)
|
||||
sk2 := fixedKey(0x03)
|
||||
chain := crypto.NewTrustChain(root.Pub)
|
||||
if err := chain.AddSigningKey("kid-active", sk1.Pub, crypto.KeyStatusActive, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := chain.AddSigningKey("kid-retired", sk2.Pub, crypto.KeyStatusRetired, 1794201600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := chain.Revoke("01JDREVOKED000000000000000"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := chain.Revoke("01JDREVOKED000000000000001"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return root, chain
|
||||
}
|
||||
|
||||
func TestSignBundle_GoldenToken(t *testing.T) {
|
||||
root, chain := fixedChain(t)
|
||||
got, err := SignBundle(chain, root.Priv)
|
||||
if err != nil {
|
||||
t.Fatalf("SignBundle: %v", err)
|
||||
}
|
||||
if got != goldenBundleToken {
|
||||
t.Errorf("bundle token 与拆分前不一致(线格式发生变更):\ngot %s\nwant %s", got, goldenBundleToken)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignBundle_RoundTrip(t *testing.T) {
|
||||
root := fixedKey(0x01)
|
||||
sk, _ := GenerateSigningKey()
|
||||
chain := crypto.NewTrustChain(root.Pub)
|
||||
if err := chain.AddSigningKey(sk.Kid, sk.Pub, crypto.KeyStatusActive, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := chain.Revoke("01JDREVOKED000000000000000"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
bundleToken, err := SignBundle(chain, root.Priv)
|
||||
if err != nil {
|
||||
t.Fatalf("SignBundle: %v", err)
|
||||
}
|
||||
|
||||
// 校验方:仅持有 root 公钥即可装载信任链
|
||||
fresh := crypto.NewTrustChain(root.Pub)
|
||||
if err := fresh.LoadBundle(bundleToken, root.Pub); err != nil {
|
||||
t.Fatalf("LoadBundle: %v", err)
|
||||
}
|
||||
if pub, ok := fresh.KeyFor(sk.Kid); !ok || string(pub) != string(sk.Pub) {
|
||||
t.Error("装载后 signing key 应可用")
|
||||
}
|
||||
if !fresh.IsRevoked("01JDREVOKED000000000000000") {
|
||||
t.Error("装载后 CRL 应生效")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignBundle_RejectsForgedRoot(t *testing.T) {
|
||||
root := fixedKey(0x01)
|
||||
evil := fixedKey(0x04)
|
||||
chain := crypto.NewTrustChain(root.Pub)
|
||||
if err := chain.AddSigningKey("kid-evil", evil.Pub, crypto.KeyStatusActive, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
forged, err := SignBundle(chain, evil.Priv)
|
||||
if err != nil {
|
||||
t.Fatalf("SignBundle: %v", err)
|
||||
}
|
||||
if err := crypto.NewTrustChain(root.Pub).LoadBundle(forged, root.Pub); err == nil {
|
||||
t.Error("非真 root 签名的 bundle 应被拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignBundle_RejectsTamperedPayload(t *testing.T) {
|
||||
root, chain := fixedChain(t)
|
||||
bundleToken, err := SignBundle(chain, root.Priv)
|
||||
if err != nil {
|
||||
t.Fatalf("SignBundle: %v", err)
|
||||
}
|
||||
|
||||
parts := strings.Split(bundleToken, ".")
|
||||
var bundle crypto.TrustBundle
|
||||
raw, _ := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if err := json.Unmarshal(raw, &bundle); err != nil {
|
||||
t.Fatalf("payload JSON: %v", err)
|
||||
}
|
||||
bundle.Signing = append(bundle.Signing, crypto.SigningKeyInfo{
|
||||
Kid: "ffffffffffffffff", Pub: base64.RawURLEncoding.EncodeToString(fixedKey(0x05).Pub), Status: crypto.KeyStatusActive,
|
||||
})
|
||||
nb, _ := json.Marshal(bundle)
|
||||
tampered := parts[0] + "." + base64.RawURLEncoding.EncodeToString(nb) + "." + parts[2]
|
||||
if err := crypto.NewTrustChain(root.Pub).LoadBundle(tampered, root.Pub); err == nil {
|
||||
t.Error("篡改载荷的 bundle 应被拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrustChain_BundleExcludesDestroyed(t *testing.T) {
|
||||
root, chain := fixedChain(t)
|
||||
destroyed := fixedKey(0x06)
|
||||
if err := chain.AddSigningKey("kid-destroyed", destroyed.Pub, crypto.KeyStatusDestroyed, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
bundle := chain.Bundle()
|
||||
for _, info := range bundle.Signing {
|
||||
if info.Kid == "kid-destroyed" {
|
||||
t.Fatal("已销毁密钥不应进入 bundle 快照")
|
||||
}
|
||||
}
|
||||
token, err := SignBundle(chain, root.Priv)
|
||||
if err != nil {
|
||||
t.Fatalf("SignBundle: %v", err)
|
||||
}
|
||||
parts := strings.Split(token, ".")
|
||||
payload, _ := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if bytes.Contains(payload, []byte("kid-destroyed")) {
|
||||
t.Error("已销毁密钥不应出现在签发的 bundle token 中")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user