用户要求:驱动证书和应用证书要分清,有的只能签一种,有的两种都能。 判据是证书 EKU(internal/protect/usage.go): - 应用:Code Signing 1.3.6.1.5.5.7.3.3(老微软证书还有 1.3.6.1.4.1.311.2.1.21/22) - 驱动:Kernel Mode Code Signing 1.3.6.1.4.1.311.61.1.1(KMCS),以及 ELAM/HAL 等 - 没有 EKU 扩展的老证书:按通用处理(能签应用与驱动),单独标「未声明用途」 - 坑:Go 会把 KMCS 解析成 x509.ExtKeyUsageMicrosoftKernelCodeSigning(13), 只查 UnknownExtKeyUsage 会把驱动证书误判成「应用」——两处都要看。 行为: - 身份列表带 usage(应用/驱动/两者/不可用),前端下拉直接标用途 - 选证书后按上传文件类型过滤身份(.exe 只列应用用途,.sys 只列驱动用途), 并显示「该文件里:可签应用 N 套 / 可签驱动 M 套」,没有对应用途时琥珀提示 - 应用证书签 .sys、纯 KMCS 证书签 exe 都在建任务前 400 拦住,带 EKU 说明 - 内核驱动(.sys / PE 子系统 NATIVE)走「仅签名」:绝不注入用户态壳 (注进内核模块会加载失败),产物无 .vfy 节,只有 Authenticode 签名 - 上传白名单放行 .sys/.efi 实测(真实 79 套包 + 自造三种 EKU 证书 + 假 .sys): - 真实驱动签名包:应用 79 套 / 驱动 0 套(OID 显示 CodeSigning + 商业代码签名) - 应用证书 + .sys → 400;纯驱动证书 + exe → 400;双用途证书 + exe → 200 - 驱动证书 + .sys → 仅签名,产物 2878976→2880512,签名者 CN=DriverSigner, 产物里 no VFY3(确认没加壳) - 修掉一个隐蔽 bug:驱动分支里用 := 遮蔽了外层 out,导致产物落盘 0 字节 而任务显示 done、output_size 却有值
592 lines
17 KiB
Go
592 lines
17 KiB
Go
package protect
|
||
|
||
// 吊销与拉黑检测。
|
||
//
|
||
// 为什么要单独做:证书「过期」和证书「被吊销」「进了黑名单」是三件事。
|
||
// - 过期:NotAfter 已过,签名照样能写进去(Windows 判定不受信任)
|
||
// - 吊销:CA 主动作废(私钥泄露 / 公司改名 / 违规),OCSP 或 CRL 会答 revoked
|
||
// - 拉黑:微软维护的 Disallowed Certificate Trust List(被吊销后仍被信任根里的机器信任
|
||
// 的证书都在这张表里),以及平台自己的黑名单
|
||
// 一张过期证书可能同时被吊销;泄露的代码签名证书通常三者全中。
|
||
//
|
||
// 现实约束:这批老证书的 OCSP/CRL 端点经常已经下线或拒答(实测 VeriSign 对
|
||
// 2010 年代的旧 CA 直接回 unauthorized),所以状态必须允许「查不到」,不能把
|
||
// 查不到当成没问题,也不能当成有问题。
|
||
|
||
import (
|
||
"bytes"
|
||
"crypto/md5"
|
||
"crypto/sha1"
|
||
"crypto/sha256"
|
||
"crypto/x509"
|
||
"encoding/asn1"
|
||
"encoding/hex"
|
||
"encoding/pem"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
|
||
"golang.org/x/crypto/ocsp"
|
||
)
|
||
|
||
// 吊销状态取值
|
||
const (
|
||
RevokeGood = "good" // 明确未吊销
|
||
RevokeRevoked = "revoked" // 明确已吊销
|
||
RevokeUnknown = "unknown" // 查不到(端点下线 / 拒答 / 没配 OCSO 与 CRL)
|
||
RevokeNotChecked = "not_checked" // 还没查过
|
||
)
|
||
|
||
// RevokeInfo 一张证书的吊销 / 拉黑结论
|
||
type RevokeInfo struct {
|
||
Status string `json:"status"` // good/revoked/unknown/not_checked
|
||
Source string `json:"source"` // ocsp / crl / msban / local / 空
|
||
Reason string `json:"reason"` // 吊销原因(keyCompromise 等)
|
||
Detail string `json:"detail"` // 人话说明
|
||
CheckedAt string `json:"checked_at"`
|
||
}
|
||
|
||
// 内存缓存:同一张证书没必要每次都去网络上问一遍
|
||
var (
|
||
revokeMu sync.RWMutex
|
||
revokeCache = map[string]cachedRevoke{}
|
||
revokeTTL = 6 * time.Hour
|
||
|
||
banMu sync.RWMutex
|
||
banSet = map[string]bool{}
|
||
banFetched time.Time
|
||
banTTL = 24 * time.Hour
|
||
)
|
||
|
||
type cachedRevoke struct {
|
||
info RevokeInfo
|
||
at time.Time
|
||
}
|
||
|
||
var httpClient = &http.Client{Timeout: 8 * time.Second}
|
||
|
||
// CheckRevocation 查一张证书的吊销状态。
|
||
// issuer 可以为 nil(不在证书包里时由 AIA 去取),取不到就只能回 unknown。
|
||
func CheckRevocation(cert *x509.Certificate, issuer *x509.Certificate) RevokeInfo {
|
||
key := fingerprintHex(cert)
|
||
// 「查不到」不能缓存 6 小时:CA 端点一时抽风就把结论钉死了,
|
||
// 未知结果只缓 10 分钟,明确结论才缓 6 小时。
|
||
revokeMu.RLock()
|
||
if c, ok := revokeCache[key]; ok {
|
||
ttl := revokeTTL
|
||
if c.info.Status == RevokeUnknown || c.info.Status == RevokeNotChecked {
|
||
ttl = 10 * time.Minute
|
||
}
|
||
if time.Since(c.at) < ttl {
|
||
revokeMu.RUnlock()
|
||
return c.info
|
||
}
|
||
}
|
||
revokeMu.RUnlock()
|
||
|
||
info := checkRevocationUncached(cert, issuer)
|
||
|
||
revokeMu.Lock()
|
||
revokeCache[key] = cachedRevoke{info: info, at: time.Now()}
|
||
revokeMu.Unlock()
|
||
return info
|
||
}
|
||
|
||
func checkRevocationUncached(cert *x509.Certificate, issuer *x509.Certificate) RevokeInfo {
|
||
now := time.Now().Format("2006-01-02 15:04:05")
|
||
|
||
if issuer == nil {
|
||
issuer = fetchIssuer(cert)
|
||
}
|
||
|
||
var details []string
|
||
|
||
// ① OCSP(首选:快、结论新)
|
||
if issuer != nil && len(cert.OCSPServer) > 0 {
|
||
if info, ok := tryOCSP(cert, issuer); ok {
|
||
info.CheckedAt = now
|
||
return info
|
||
}
|
||
details = append(details, "OCSP 无有效应答")
|
||
}
|
||
|
||
// ② CRL(回落)
|
||
if len(cert.CRLDistributionPoints) > 0 {
|
||
if info, ok := tryCRL(cert); ok {
|
||
info.CheckedAt = now
|
||
return info
|
||
}
|
||
details = append(details, "CRL 无有效应答")
|
||
}
|
||
|
||
if len(cert.OCSPServer) == 0 && len(cert.CRLDistributionPoints) == 0 {
|
||
details = append(details, "证书里没有 OCSP / CRL 地址")
|
||
}
|
||
if issuer == nil {
|
||
details = append(details, "拿不到签发者证书,无法验证应答")
|
||
}
|
||
|
||
return RevokeInfo{
|
||
Status: RevokeUnknown,
|
||
Detail: strings.Join(details, ";"),
|
||
CheckedAt: now,
|
||
}
|
||
}
|
||
|
||
// tryOCSP 发一次 OCSP 请求
|
||
func tryOCSP(cert, issuer *x509.Certificate) (RevokeInfo, bool) {
|
||
req, err := ocsp.CreateRequest(cert, issuer, nil)
|
||
if err != nil {
|
||
return RevokeInfo{}, false
|
||
}
|
||
for _, url := range cert.OCSPServer {
|
||
if !strings.HasPrefix(url, "http") {
|
||
continue
|
||
}
|
||
httpReq, err := http.NewRequest("POST", url, bytes.NewReader(req))
|
||
if err != nil {
|
||
continue
|
||
}
|
||
httpReq.Header.Set("Content-Type", "application/ocsp-request")
|
||
resp, err := httpClient.Do(httpReq)
|
||
if err != nil {
|
||
continue
|
||
}
|
||
body, rerr := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||
resp.Body.Close()
|
||
if rerr != nil || resp.StatusCode != 200 {
|
||
continue
|
||
}
|
||
parsed, perr := ocsp.ParseResponseForCert(body, cert, issuer)
|
||
if perr != nil {
|
||
continue
|
||
}
|
||
// 应答过期了就别当结论
|
||
if !parsed.NextUpdate.IsZero() && parsed.NextUpdate.Before(time.Now()) {
|
||
continue
|
||
}
|
||
switch parsed.Status {
|
||
case ocsp.Good:
|
||
return RevokeInfo{Status: RevokeGood, Source: "ocsp",
|
||
Detail: "OCSP 应答:未吊销(" + url + ")"}, true
|
||
case ocsp.Revoked:
|
||
return RevokeInfo{Status: RevokeRevoked, Source: "ocsp",
|
||
Reason: revokeReasonText(parsed.RevocationReason),
|
||
Detail: "OCSP 应答:已吊销,吊销时间 " + parsed.RevokedAt.Format("2006-01-02") + "(" + url + ")",
|
||
CheckedAt: time.Now().Format("2006-01-02 15:04:05")}, true
|
||
}
|
||
}
|
||
return RevokeInfo{}, false
|
||
}
|
||
|
||
// tryCRL 拉 CRL 判一次
|
||
func tryCRL(cert *x509.Certificate) (RevokeInfo, bool) {
|
||
for _, url := range cert.CRLDistributionPoints {
|
||
if !strings.HasPrefix(url, "http") {
|
||
continue
|
||
}
|
||
resp, err := httpClient.Get(url)
|
||
if err != nil {
|
||
continue
|
||
}
|
||
body, rerr := io.ReadAll(io.LimitReader(resp.Body, 8<<20))
|
||
resp.Body.Close()
|
||
if rerr != nil || resp.StatusCode != 200 {
|
||
continue
|
||
}
|
||
crl, perr := parseCRL(body)
|
||
if perr != nil {
|
||
continue
|
||
}
|
||
if !crl.NextUpdate.IsZero() && crl.NextUpdate.Before(time.Now()) {
|
||
continue // CRL 本身过期,不能当结论
|
||
}
|
||
for _, e := range crl.RevokedCertificateEntries {
|
||
if e.SerialNumber.Cmp(cert.SerialNumber) == 0 {
|
||
return RevokeInfo{Status: RevokeRevoked, Source: "crl",
|
||
Reason: revokeReasonText(e.ReasonCode),
|
||
Detail: "CRL 里查到此序列号,吊销时间 " + e.RevocationTime.Format("2006-01-02"),
|
||
CheckedAt: time.Now().Format("2006-01-02 15:04:05")}, true
|
||
}
|
||
}
|
||
return RevokeInfo{Status: RevokeGood, Source: "crl",
|
||
Detail: "CRL 里没有此序列号(" + url + ")",
|
||
CheckedAt: time.Now().Format("2006-01-02 15:04:05")}, true
|
||
}
|
||
return RevokeInfo{}, false
|
||
}
|
||
|
||
// parseCRL 同时兼容 DER 与 PEM 两种 CRL
|
||
func parseCRL(data []byte) (*x509.RevocationList, error) {
|
||
if blk, _ := pem.Decode(data); blk != nil {
|
||
data = blk.Bytes
|
||
}
|
||
return x509.ParseRevocationList(data)
|
||
}
|
||
|
||
// fetchIssuer 从 AIA 的 caIssuers 地址把签发者证书取回来
|
||
func fetchIssuer(cert *x509.Certificate) *x509.Certificate {
|
||
for _, url := range cert.IssuingCertificateURL {
|
||
if !strings.HasPrefix(url, "http") {
|
||
continue
|
||
}
|
||
resp, err := httpClient.Get(url)
|
||
if err != nil {
|
||
continue
|
||
}
|
||
body, rerr := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||
resp.Body.Close()
|
||
if rerr != nil || resp.StatusCode != 200 {
|
||
continue
|
||
}
|
||
if c, err := x509.ParseCertificate(body); err == nil {
|
||
return c
|
||
}
|
||
// 常见形态是 PKCS#7 证书包
|
||
if blk, _ := pem.Decode(body); blk != nil {
|
||
if c, err := x509.ParseCertificate(blk.Bytes); err == nil {
|
||
return c
|
||
}
|
||
}
|
||
if certs := certsFromPKCS7(body); len(certs) > 0 {
|
||
return certs[0]
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// certsFromPKCS7 从 PKCS#7 / certs-only 包里抠证书。
|
||
// 只认嵌着的证书结构,不做验签——这里只需要签发者公钥去校验 OCSP 应答。
|
||
func certsFromPKCS7(data []byte) []*x509.Certificate {
|
||
var out []*x509.Certificate
|
||
var ci struct {
|
||
ContentType asn1.ObjectIdentifier
|
||
Content asn1.RawValue `asn1:"explicit,tag:0"`
|
||
}
|
||
if _, err := asn1.Unmarshal(data, &ci); err != nil {
|
||
return nil
|
||
}
|
||
rest := ci.Content.Bytes
|
||
for len(rest) > 0 {
|
||
var raw asn1.RawValue
|
||
var err error
|
||
rest, err = asn1.Unmarshal(rest, &raw)
|
||
if err != nil {
|
||
break
|
||
}
|
||
if c, cerr := x509.ParseCertificate(raw.FullBytes); cerr == nil {
|
||
out = append(out, c)
|
||
continue
|
||
}
|
||
// 证书可能嵌在更深一层
|
||
inner := raw.Bytes
|
||
for len(inner) > 0 {
|
||
var r2 asn1.RawValue
|
||
var e2 error
|
||
inner, e2 = asn1.Unmarshal(inner, &r2)
|
||
if e2 != nil {
|
||
break
|
||
}
|
||
if c, cerr := x509.ParseCertificate(r2.FullBytes); cerr == nil {
|
||
out = append(out, c)
|
||
}
|
||
}
|
||
if len(out) >= 8 {
|
||
break
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
func revokeReasonText(code int) string {
|
||
switch code {
|
||
case ocsp.KeyCompromise:
|
||
return "私钥泄露"
|
||
case ocsp.CACompromise:
|
||
return "CA 被攻破"
|
||
case ocsp.AffiliationChanged:
|
||
return "主体信息变更"
|
||
case ocsp.Superseded:
|
||
return "被新证书取代"
|
||
case ocsp.CessationOfOperation:
|
||
return "停止运营"
|
||
case ocsp.CertificateHold:
|
||
return "暂时冻结"
|
||
case ocsp.RemoveFromCRL:
|
||
return "已从 CRL 移除"
|
||
case ocsp.PrivilegeWithdrawn:
|
||
return "权限被撤销"
|
||
case ocsp.AACompromise:
|
||
return "属性机构被攻破"
|
||
}
|
||
if code == 0 {
|
||
return "未给出原因"
|
||
}
|
||
return fmt.Sprintf("原因码 %d", code)
|
||
}
|
||
|
||
func fingerprintHex(c *x509.Certificate) string {
|
||
s := sha256.Sum256(c.Raw)
|
||
return hex.EncodeToString(s[:])
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// 微软拉黑名单(Disallowed Certificate Trust List)
|
||
//
|
||
// 这张表就是 Windows 判断「这张证书虽然链能验通,但已经被官方作废」的依据。
|
||
// 文件是 CAB 包里的一个 CTL(PKCS#7 signedData,contentType 1.3.6.1.4.1.311.10.1),
|
||
// 条目里存的是证书哈希。解 CAB 用系统 cabextract(Alpine 里 apk add cabextract)。
|
||
// ---------------------------------------------------------------------------
|
||
|
||
// DisallowedSet 返回微软拉黑名单里的证书哈希集合(hex 小写)。
|
||
// 缓存 24 小时;拉不到就返回上一次的结果(没有就返回空集合)。
|
||
func DisallowedSet(cacheDir string) map[string]bool {
|
||
banMu.RLock()
|
||
if time.Since(banFetched) < banTTL && len(banSet) > 0 {
|
||
out := banSet
|
||
banMu.RUnlock()
|
||
return out
|
||
}
|
||
banMu.RUnlock()
|
||
|
||
banMu.Lock()
|
||
defer banMu.Unlock()
|
||
|
||
// 磁盘缓存:多进程/重启后不用重新下载
|
||
file := filepath.Join(cacheDir, "msban.txt")
|
||
if cacheDir != "" {
|
||
if st, err := os.Stat(file); err == nil && time.Since(st.ModTime()) < banTTL {
|
||
if b, err := os.ReadFile(file); err == nil {
|
||
banSet = parseHashLines(b)
|
||
banFetched = time.Now()
|
||
return banSet
|
||
}
|
||
}
|
||
}
|
||
|
||
set, err := downloadDisallowed()
|
||
if err != nil || len(set) == 0 {
|
||
// 拉不到就沿用旧结果,别把「没查到」当成「没被拉黑」
|
||
if len(banSet) > 0 {
|
||
banFetched = time.Now()
|
||
return banSet
|
||
}
|
||
return map[string]bool{}
|
||
}
|
||
banSet = set
|
||
banFetched = time.Now()
|
||
if cacheDir != "" {
|
||
os.MkdirAll(cacheDir, 0o700)
|
||
var sb strings.Builder
|
||
for h := range set {
|
||
sb.WriteString(h + "\n")
|
||
}
|
||
os.WriteFile(file, []byte(sb.String()), 0o600)
|
||
}
|
||
return banSet
|
||
}
|
||
|
||
func parseHashLines(b []byte) map[string]bool {
|
||
out := map[string]bool{}
|
||
for _, line := range strings.Split(string(b), "\n") {
|
||
line = strings.TrimSpace(line)
|
||
if len(line) >= 32 {
|
||
out[strings.ToLower(line)] = true
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
// downloadDisallowed 下载并解出 CTL,提取里面所有证书哈希
|
||
func downloadDisallowed() (map[string]bool, error) {
|
||
const url = "http://ctldl.windowsupdate.com/msdownload/update/v3/static/trustedr/en/disallowedcertstl.cab"
|
||
resp, err := httpClient.Get(url)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer resp.Body.Close()
|
||
if resp.StatusCode != 200 {
|
||
return nil, fmt.Errorf("下载拉黑名单失败: HTTP %d", resp.StatusCode)
|
||
}
|
||
cab, err := io.ReadAll(io.LimitReader(resp.Body, 16<<20))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
stl, err := cabExtractFirst(cab)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return ctlHashes(stl), nil
|
||
}
|
||
|
||
// cabExtractFirst 用系统 cabextract 解出 CAB 里第一个文件
|
||
func cabExtractFirst(cab []byte) ([]byte, error) {
|
||
dir, err := os.MkdirTemp("", "msban_")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer os.RemoveAll(dir)
|
||
src := filepath.Join(dir, "in.cab")
|
||
if err := os.WriteFile(src, cab, 0o600); err != nil {
|
||
return nil, err
|
||
}
|
||
if out, err := runCmd(dir, "cabextract", "-q", "-d", dir, src); err != nil {
|
||
return nil, fmt.Errorf("解 CAB 失败(镜像里需要 cabextract): %v %s", err, tail(out, 200))
|
||
}
|
||
entries, err := os.ReadDir(dir)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
for _, e := range entries {
|
||
if !e.IsDir() && strings.HasSuffix(strings.ToLower(e.Name()), ".stl") {
|
||
return os.ReadFile(filepath.Join(dir, e.Name()))
|
||
}
|
||
}
|
||
return nil, fmt.Errorf("CAB 里没找到 .stl")
|
||
}
|
||
|
||
// ctlHashes 从 CTL 里把所有证书哈希抠出来。
|
||
// 条目的哈希长度随算法变化(历史上有 16/20/32 字节),所以这里不写死长度,
|
||
// 统一收集 SEQUENCE 内的原始字节,再用多种算法分别比对。
|
||
func ctlHashes(der []byte) map[string]bool {
|
||
out := map[string]bool{}
|
||
i := 0
|
||
for i < len(der)-2 {
|
||
if der[i] != 0x04 { // OCTET STRING
|
||
i++
|
||
continue
|
||
}
|
||
l := int(der[i+1])
|
||
if l == 0x81 && i+2 < len(der) {
|
||
l = int(der[i+2])
|
||
i++
|
||
}
|
||
start := i + 2
|
||
if l >= 16 && l <= 64 && start+l <= len(der) {
|
||
h := der[start : start+l]
|
||
out[strings.ToLower(hex.EncodeToString(h))] = true
|
||
}
|
||
i = start + l
|
||
}
|
||
return out
|
||
}
|
||
|
||
// BanState 判断一张证书是否在微软拉黑名单或平台黑名单里
|
||
func BanState(cert *x509.Certificate, localFingerprints map[string]string) RevokeInfo {
|
||
fp := fingerprintHex(cert)
|
||
|
||
if localFingerprints != nil {
|
||
if reason, ok := localFingerprints[strings.ToLower(fp)]; ok {
|
||
return RevokeInfo{Status: RevokeRevoked, Source: "local",
|
||
Reason: "平台黑名单", Detail: "该证书已被本平台拉黑" + ifStr(reason, ":"+reason),
|
||
CheckedAt: time.Now().Format("2006-01-02 15:04:05")}
|
||
}
|
||
}
|
||
|
||
set := DisallowedSet("")
|
||
if len(set) == 0 {
|
||
return RevokeInfo{Status: RevokeNotChecked, Detail: "微软拉黑名单未获取到"}
|
||
}
|
||
md5sum := md5Hex(cert.Raw)
|
||
sha1sum := sha1Hex(cert.Raw)
|
||
for _, cand := range []string{
|
||
strings.ToLower(fp), // sha256 全量
|
||
strings.ToLower(fp[:32]), // sha256 前 16 字节
|
||
md5sum, // md5
|
||
sha1sum, // sha1
|
||
strings.ToLower(sha1sum[:32]), // sha1 前 16 字节
|
||
} {
|
||
if set[cand] {
|
||
return RevokeInfo{Status: RevokeRevoked, Source: "msban",
|
||
Reason: "微软拉黑名单",
|
||
Detail: "该证书在微软 Disallowed Certificate Trust List 里(Windows 会拒绝其签名)",
|
||
CheckedAt: time.Now().Format("2006-01-02 15:04:05")}
|
||
}
|
||
}
|
||
return RevokeInfo{Status: RevokeGood, Source: "msban",
|
||
Detail: "不在微软拉黑名单里", CheckedAt: time.Now().Format("2006-01-02 15:04:05")}
|
||
}
|
||
|
||
func ifStr(cond, s string) string {
|
||
if cond == "" {
|
||
return ""
|
||
}
|
||
return s
|
||
}
|
||
|
||
func md5Hex(b []byte) string {
|
||
sum := md5Sum(b)
|
||
return hex.EncodeToString(sum[:])
|
||
}
|
||
|
||
func sha1Hex(b []byte) string {
|
||
sum := sha1.Sum(b)
|
||
return hex.EncodeToString(sum[:])
|
||
}
|
||
|
||
func md5Sum(b []byte) [16]byte {
|
||
return md5.Sum(b)
|
||
}
|
||
|
||
// CheckProfileIdentity 对某条证书记录里的第 identity 套身份做吊销 + 拉黑检查。
|
||
// 供加壳前把关使用:已吊销 / 被拉黑的证书直接拦,查不到只提示不拦。
|
||
func CheckProfileIdentity(profileID uint, identity int, localBan map[string]string) (rev RevokeInfo, ban RevokeInfo, subject string, err error) {
|
||
dir := ProfileDir(profileID)
|
||
certData, err := ReadEncrypted(filepath.Join(dir, "cert.enc"))
|
||
if err != nil {
|
||
return rev, ban, "", fmt.Errorf("读取证书失败: %w", err)
|
||
}
|
||
password := ""
|
||
if b, e := ReadEncrypted(filepath.Join(dir, "pass.enc")); e == nil {
|
||
password = string(b)
|
||
}
|
||
full, err := ParseIdentitiesFull(certData, password)
|
||
if err != nil {
|
||
return rev, ban, "", err
|
||
}
|
||
if len(full) == 0 {
|
||
return rev, ban, "", fmt.Errorf("证书里没有可签名的身份")
|
||
}
|
||
if identity <= 0 {
|
||
identity = 1
|
||
}
|
||
if identity > len(full) {
|
||
return rev, ban, "", fmt.Errorf("证书里只有 %d 套身份", len(full))
|
||
}
|
||
d := full[identity-1]
|
||
return CheckRevocation(d.Cert, d.Issuer), BanState(d.Cert, localBan), d.Subject, nil
|
||
}
|
||
|
||
// ProfileIdentityUsage 取某条证书记录里第 identity 套身份的用途(应用/驱动)
|
||
func ProfileIdentityUsage(profileID uint, identity int) (CertUsage, string, error) {
|
||
dir := ProfileDir(profileID)
|
||
certData, err := ReadEncrypted(filepath.Join(dir, "cert.enc"))
|
||
if err != nil {
|
||
return CertUsage{}, "", fmt.Errorf("读取证书失败: %w", err)
|
||
}
|
||
password := ""
|
||
if b, e := ReadEncrypted(filepath.Join(dir, "pass.enc")); e == nil {
|
||
password = string(b)
|
||
}
|
||
full, err := ParseIdentitiesFull(certData, password)
|
||
if err != nil {
|
||
return CertUsage{}, "", err
|
||
}
|
||
if len(full) == 0 {
|
||
return CertUsage{}, "", fmt.Errorf("证书里没有可签名的身份")
|
||
}
|
||
if identity <= 0 {
|
||
identity = 1
|
||
}
|
||
if identity > len(full) {
|
||
return CertUsage{}, "", fmt.Errorf("证书里只有 %d 套身份", len(full))
|
||
}
|
||
return full[identity-1].Usage, full[identity-1].Subject, nil
|
||
}
|