Files
verify/backend/cmd/pfxprobe/main.go
hermes 850ab691b7
Docker Build / Build and Push Docker Image (push) Successful in 1m9s
Docker Build / Deploy to Server (push) Successful in 3s
feat(protect): 过期证书可签之外,补上吊销与拉黑标记
用户问:verify 现在能签过期签名吗(signtool hook 日期那套)。能签,f6a4c7d 起就允许
过期证书签并给警告。这次补的是另外两件事——吊销和拉黑,它们和过期是三回事:

- 过期:NotAfter 已过,签名照写(Windows 判不受信任)
- 吊销:CA 主动作废(私钥泄露/停止运营),OCSP 或 CRL 会答 revoked
- 拉黑:微软 Disallowed Certificate Trust List,Windows 会直接拒绝其签名

实现(internal/protect/revocation.go):
- CheckRevocation:优先 OCSP(AIA 取签发者 → 验应答),回落 CRL(CDP),
  要求应答未过期才作结论;结论 good/revoked/unknown,带原因与来源。
  明确结论缓存 6h,「查不到」只缓 10min——端点抽风不该把结论钉死。
- BanState:自动下载并缓存微软拉黑名单(CAB → cabextract → CTL 哈希),
  同时比对平台黑名单;哈希按 md5/sha1/sha256(全量/截断) 多形态比对。
- 加壳前把关:已吊销 / 已被拉黑 → 400 拒绝并给出吊销日期与原因;
  查不到只提示不拦(老证书的 OCSP 端点经常已停服,实测 VeriSign 回 unauthorized)。
- 新表 cert_blacklist(指纹/主题/原因/备注)+ 增删查接口,运维可手动拉黑。
- 身份接口 ?check=N 只查选中的那一套,避免 79 套身份整包联网。
- 前端:选中身份后显示「证书状态:吊销 / 拉黑」徽章(绿/红/灰),悬停看详情。

实测:
- 真实 81 私钥包的 #1:CRL 查询 http://cs-g2-crl.thawte.com/ThawteCSG2.crl → good;
  微软拉黑名单 → good(不在表里)
- 自造 CA + CRL(http 服务)+ 已吊销叶证书:判 revoked,加壳返回
  400「该证书已被 CA 吊销」+ 吊销日期
- 平台黑名单:拉黑后加壳 400「该证书已被本平台拉黑:私钥疑似泄露」,解除后恢复 200
- UI 徽章实测显示「吊销:正常 拉黑:正常」
2026-11-22 12:33:14 +08:00

134 lines
3.6 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 main
// pfxprobe 本地诊断:把证书文件丢进去,看清它到底是什么、有几套签名身份。
//
// go run ./cmd/pfxprobe -file cert.pfx -pass 口令
//
// 排查「上传后解析失败」时先跑这个:它把文件真实形态、证书/私钥数量、
// 每套身份的到期时间全部列出来,失败也会给出具体原因。
import (
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"os"
"strings"
"verification-platform-backend/internal/protect"
)
func main() {
file := flag.String("file", "", "证书文件路径(.pfx/.p12/.pem)")
pass := flag.String("pass", "", "pfx 口令")
rev := flag.Bool("rev", false, "顺带查吊销 / 微软拉黑名单(需要网络)")
revLimit := flag.Int("rev-limit", 3, "最多查几套身份的吊销状态")
flag.Parse()
if *file == "" {
fmt.Fprintln(os.Stderr, "用法: pfxprobe -file cert.pfx [-pass 口令]")
os.Exit(2)
}
data, err := os.ReadFile(*file)
if err != nil {
fmt.Fprintln(os.Stderr, "读文件失败:", err)
os.Exit(2)
}
fmt.Printf("文件: %s\n大小: %d 字节\n", *file, len(data))
fmt.Printf("前 16 字节: % x\n", data[:min(16, len(data))])
// 先直接看 PEM(如果是文本)
if blk, _ := pem.Decode(data); blk != nil {
fmt.Printf("PEM 块类型: %s\n", blk.Type)
if blk.Type == "CERTIFICATE" {
if c, e := x509.ParseCertificate(blk.Bytes); e == nil {
fmt.Printf(" 证书: %s 到期 %s\n", c.Subject, c.NotAfter.Format("2006-01-02"))
}
}
} else {
fmt.Printf("不是 PEM 文本;按 DER/二进制处理\n")
}
// 吊销 / 拉黑:默认只查前几套,避免几十套身份时把时间都花在网络上
checkRev := *rev
info, err := protect.ParsePFX(data, *pass)
if err != nil {
fmt.Printf("\n解析失败: %v\n", err)
if pe, ok := err.(*protect.PFXError); ok {
fmt.Printf("原因: %s\n", pe.Reason)
if pe.Hint != "" {
fmt.Printf("建议: %s\n", pe.Hint)
}
if pe.Detail != "" {
fmt.Printf("底层: %s\n", pe.Detail)
}
}
os.Exit(1)
}
fmt.Printf("\n格式: %s\n证书: %d 张\n私钥: %d 把\n可签名身份: %d 套\n说明: %s\n",
info.Syntax, info.CertCount, info.KeyCount, len(info.Identities), info.Note)
for _, id := range info.Identities {
mark := ""
if id.Expired {
mark = " ← 已过期"
}
fmt.Printf(" 第%d套: %s 到期 %s%s\n", id.Index, id.Subject, id.NotAfter.Format("2006-01-02"), mark)
}
if checkRev {
fmt.Printf("\n=== 吊销 / 拉黑(最多查 %d 套) ===\n", *revLimit)
full, ferr := protect.ParseIdentitiesFull(data, *pass)
if ferr != nil {
fmt.Printf("取身份详情失败: %v\n", ferr)
} else {
for i, id := range full {
if i >= *revLimit {
break
}
rev := protect.CheckRevocation(id.Cert, id.Issuer)
ban := protect.BanState(id.Cert, nil)
fmt.Printf(" 第%d套 %-40s 吊销=%s(%s) 拉黑=%s\n",
id.Index, cnOf(id.Subject), rev.Status, rev.Detail, ban.Status)
}
}
}
ids, serr := protect.SplitPFX(data, *pass)
if serr != nil {
fmt.Printf("\n拆分失败: %v\n", serr)
os.Exit(1)
}
fmt.Printf("\n拆分结果: %d 份单身份 pfx\n", len(ids))
for _, id := range ids {
fmt.Printf(" 第%d套 %s 链 %d 张 重打包 %d 字节 sha256:%s\n",
id.Index, id.Subject, id.ChainLen, len(id.PFX), shortHash(id.PFX))
}
}
func shortHash(b []byte) string {
h := fmt.Sprintf("%x", b)
if len(h) > 12 {
return h[:12]
}
return h
}
func cnOf(subject string) string {
parts := strings.Split(subject, ",")
for _, p := range parts {
p = strings.TrimSpace(p)
if strings.HasPrefix(p, "CN=") {
return strings.TrimPrefix(p, "CN=")
}
}
return subject
}
func min(a, b int) int {
if a < b {
return a
}
return b
}