2026-11-22 12:03:02 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
// pfxprobe 本地诊断:把证书文件丢进去,看清它到底是什么、有几套签名身份。
|
|
|
|
|
|
//
|
|
|
|
|
|
// go run ./cmd/pfxprobe -file cert.pfx -pass 口令
|
|
|
|
|
|
//
|
|
|
|
|
|
// 排查「上传后解析失败」时先跑这个:它把文件真实形态、证书/私钥数量、
|
|
|
|
|
|
// 每套身份的到期时间全部列出来,失败也会给出具体原因。
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"crypto/x509"
|
|
|
|
|
|
"encoding/pem"
|
|
|
|
|
|
"flag"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"os"
|
2026-11-22 12:33:14 +08:00
|
|
|
|
"strings"
|
2026-11-22 12:03:02 +08:00
|
|
|
|
|
|
|
|
|
|
"verification-platform-backend/internal/protect"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
file := flag.String("file", "", "证书文件路径(.pfx/.p12/.pem)")
|
|
|
|
|
|
pass := flag.String("pass", "", "pfx 口令")
|
2026-11-22 12:33:14 +08:00
|
|
|
|
rev := flag.Bool("rev", false, "顺带查吊销 / 微软拉黑名单(需要网络)")
|
|
|
|
|
|
revLimit := flag.Int("rev-limit", 3, "最多查几套身份的吊销状态")
|
2026-11-22 12:03:02 +08:00
|
|
|
|
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")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-22 12:33:14 +08:00
|
|
|
|
// 吊销 / 拉黑:默认只查前几套,避免几十套身份时把时间都花在网络上
|
|
|
|
|
|
checkRev := *rev
|
2026-11-22 12:03:02 +08:00
|
|
|
|
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)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-22 12:33:14 +08:00
|
|
|
|
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)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-22 12:03:02 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-22 12:33:14 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-22 12:03:02 +08:00
|
|
|
|
func min(a, b int) int {
|
|
|
|
|
|
if a < b {
|
|
|
|
|
|
return a
|
|
|
|
|
|
}
|
|
|
|
|
|
return b
|
|
|
|
|
|
}
|