feat(protect): 支持用过期证书签名 + 上传时解析证书到期时间
Docker Build / Build and Push Docker Image (push) Successful in 1m11s
Docker Build / Deploy to Server (push) Successful in 2s

需求:pfx 证书过期后仍要能用来签产物。

1) 去掉「证书已过期」的硬拒绝。私服/内网分发场景下「有签名」比「链被信任」
   更重要,过期证书照样能写入 Authenticode。改为:允许,但在创建任务的响应
   里带 warning,前端选中过期证书时显示琥珀色提示条,下拉选项本身也标
   「已过期」。安全相关的校验(平台不匹配、已停用、非本人、APK/ELF)全部保留。

2) 上传证书时解析 Subject 与 NotAfter(纯 Go,software.sslmate.com/src/go-pkcs12)。
   之前这两个字段从没被填过 —— verify 的镜像是 Alpine,里面没有 openssl CLI,
   列表里「到期时间」永远是「无到期信息」,过期提醒形同虚设。
   解析失败不阻断上传(口令错也能存下来),只是没有到期信息。

3) 「N 天后过期」只对未过期且 30 天内生效;过期项不再显示负数天数。

实测(本地 osslsigncode 2.9,用 openssl 生成 2020-2021 的真过期证书):
- 上传 → not_after=2021-01-01 落库,Subject=CN=VFY Expired Signer,O=Hermes
- 带该证书加壳 → 任务 done,响应 warning=「所选证书已于 2021-01-01 过期…」
- osslsigncode verify 产物:Signer CN=VFY Expired Signer,
  message digest 与计算值一致 → 签名确实写进去了
- 界面:下拉列出过期项并标注「已过期」,选中后出现琥珀色提示条
This commit is contained in:
hermes
2026-11-22 11:02:23 +08:00
parent 6340ba9e38
commit f6a4c7d40a
7 changed files with 73 additions and 10 deletions
+1
View File
@@ -15,6 +15,7 @@ require (
golang.org/x/text v0.20.0
gorm.io/driver/mysql v1.5.1
gorm.io/gorm v1.30.0
software.sslmate.com/src/go-pkcs12 v0.4.0
)
require (
+2
View File
@@ -619,3 +619,5 @@ rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
software.sslmate.com/src/go-pkcs12 v0.4.0 h1:H2g08FrTvSFKUj+D309j1DPfk5APnIdAQAB8aEykJ5k=
software.sslmate.com/src/go-pkcs12 v0.4.0/go.mod h1:Qiz0EyvDRJjjxGyUQa2cCNZn/wMyzrRJ/qcDXOQazLI=
+11 -4
View File
@@ -98,6 +98,7 @@ func HandleCreateProtectTask(c *gin.Context) {
// 先 CreateAndRun(内部已 go execute)再 UPDATE,协程可能先读到 NULL,
// 证书被静默忽略:任务成功、产物却没重签。
var signProfileID *uint
var warnings []string
if v := c.PostForm("sign_profile_id"); v != "" {
pid, _ := strconv.ParseUint(v, 10, 64)
if pid == 0 {
@@ -113,9 +114,11 @@ func HandleCreateProtectTask(c *gin.Context) {
response.ParamError(c, "该证书已停用,不能用于重签")
return
}
// 过期证书允许使用:私服/内网分发场景下「有签名」比「签名被链信任」更重要,
// 但要在响应里明确告诉调用方,别让人以为签了一张受信任的证书。
if sp.NotAfter != nil && sp.NotAfter.Before(time.Now()) {
response.ParamError(c, "该证书已于 "+sp.NotAfter.Format("2006-01-02")+" 过期,请换一张")
return
warnings = append(warnings, "所选证书已于 "+sp.NotAfter.Format("2006-01-02")+
" 过期:仍会写入签名,但 Windows 会因证书过期判定签名不受信任")
}
// 平台必须和文件类型对得上:PE 要 windows 证书,IPA 要 iOS 证书;
// APK/ELF 不吃签名证书,传了直接拒,免得以为签过了。
@@ -144,11 +147,15 @@ func HandleCreateProtectTask(c *gin.Context) {
return
}
response.SuccessWithMessage(c, "任务已创建,正在加壳", gin.H{
payload := gin.H{
"task_id": task.ID,
"arch": task.Arch,
"status": task.Status,
})
}
if len(warnings) > 0 {
payload["warning"] = strings.Join(warnings, ";")
}
response.SuccessWithMessage(c, "任务已创建,正在加壳", payload)
}
// HandleListProtectTasks 任务列表
@@ -4,12 +4,14 @@ package admin
// 供加壳产物重签时取用;任何响应都不回证书内容与口令。
import (
"crypto/x509"
"io"
"mime/multipart"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"verification-platform-backend/internal/database"
"verification-platform-backend/internal/model"
@@ -17,6 +19,7 @@ import (
"verification-platform-backend/pkg/response"
"github.com/gin-gonic/gin"
"software.sslmate.com/src/go-pkcs12"
)
func SetupSignProfileRoutes(r *gin.RouterGroup) {
@@ -79,11 +82,19 @@ func HandleCreateSignProfile(c *gin.Context) {
}
}
// 解析证书本身:Subject 与到期时间。
// 生产镜像是 Alpine,没有 openssl CLI,所以用纯 Go 解 PKCS#12。
// 解析失败不影响上传(口令错/格式怪也能存下来给别的工具用),只是列表上
// 看不到到期时间,也就没有过期提醒。
subject, notAfter := parseCertInfo(certData, password)
sp := model.SignProfile{
UserID: userID,
Platform: platform,
Name: name,
CertFormat: strings.TrimPrefix(ext, "."),
Subject: subject,
NotAfter: notAfter,
Status: "active",
}
if err := database.DB.Create(&sp).Error; err != nil {
@@ -191,3 +202,28 @@ func readUploaded(fh *multipart.FileHeader) ([]byte, error) {
defer f.Close()
return io.ReadAll(io.LimitReader(f, 16<<20))
}
// parseCertInfo 从 pfx/p12 里取主题与到期时间(纯 Go,不依赖 openssl CLI)。
// 口令错、格式不认识时返回空值,不阻断上传。
func parseCertInfo(certData []byte, password string) (subject string, notAfter *time.Time) {
blocks, err := pkcs12.ToPEM(certData, password)
if err != nil && password != "" {
// 有些导出工具对空口令的 pfx 也要显式传空串,换个姿势再试一次
blocks, err = pkcs12.ToPEM(certData, "")
}
if err != nil {
return "", nil
}
for _, b := range blocks {
if b.Type != "CERTIFICATE" {
continue
}
cert, perr := x509.ParseCertificate(b.Bytes)
if perr != nil {
continue
}
// 取第一张证书(叶子证书通常在最前)
return cert.Subject.String(), &cert.NotAfter
}
return "", nil
}
+19 -4
View File
@@ -70,15 +70,27 @@ const fileKind = computed(() => {
const wantPlatform = computed(() => (fileKind.value === 'pe' ? 'windows' : fileKind.value === 'ipa' ? 'ios' : ''))
const certSupported = computed(() => wantPlatform.value !== '')
// 过期证书也列出来:私服/内网分发时「有签名」比「链被信任」更重要,
// 但要明确标出来,别让人以为签了一张受信任的证书。
const eligibleProfiles = computed(() => {
const want = wantPlatform.value
if (!want) return []
const now = Date.now()
return signProfiles.value.filter(p => p.platform === want && p.status === 'active' && (!p.not_after || new Date(p.not_after).getTime() > now))
return signProfiles.value.filter(p => p.platform === want && p.status === 'active')
})
const selectedProfile = computed(() =>
eligibleProfiles.value.find(p => String(p.id) === selectedSignProfile.value) || null)
const selectedExpired = computed(() => {
const p = selectedProfile.value
if (!p || !p.not_after) return false
return new Date(p.not_after).getTime() < Date.now()
})
function certLabel(p: SignProfile) {
const exp = p.not_after ? new Date(p.not_after).toISOString().slice(0, 10) : '无到期信息'
const left = p.not_after ? Math.ceil((new Date(p.not_after).getTime() - Date.now()) / 86400000) : null
if (left !== null && left < 0) return `${p.name}(到期 ${exp} · 已过期)`
return `${p.name}(到期 ${exp})`
}
@@ -428,9 +440,9 @@ onBeforeUnmount(() => {
<UiSelectValue :placeholder="t('admin.protect.signProfileNone')" />
</UiSelectTrigger>
<UiSelectContent>
<UiSelectItem v-for="p in eligibleProfiles" :key="p.id" :value="String(p.id)">
<UiSelectItem v-for="p in eligibleProfiles" :key="p.id" :value="String(p.id)" :class="daysToExpire(p) !== null && daysToExpire(p)! < 0 ? 'text-amber-600 dark:text-amber-400' : ''">
{{ certLabel(p) }}
<span v-if="daysToExpire(p) !== null && daysToExpire(p)! <= 30" class="ml-2 text-xs text-amber-600">
<span v-if="daysToExpire(p) !== null && daysToExpire(p)! > 0 && daysToExpire(p)! <= 30" class="ml-2 text-xs text-amber-600">
{{ daysToExpire(p) }} 天后过期
</span>
</UiSelectItem>
@@ -443,6 +455,9 @@ onBeforeUnmount(() => {
<p v-if="certSupported" class="text-xs text-muted-foreground">
{{ eligibleProfiles.length ? t('admin.protect.signProfileHint') : t('admin.protect.signProfileEmpty') }}
</p>
<p v-if="selectedExpired" class="rounded-md border border-amber-500/40 bg-amber-500/10 px-3 py-2 text-xs text-amber-600 dark:text-amber-400">
{{ t('admin.protect.signProfileExpiredWarn') }}
</p>
<p v-else-if="fileKind" class="text-xs text-muted-foreground">
{{ t('admin.protect.signProfileUnsupported') }}
</p>
+2 -1
View File
@@ -2629,7 +2629,8 @@
"signProfileNone": "Do not re-sign (keep unsigned)",
"signProfileHint": "The artifact is Authenticode re-signed with the selected certificate after patching; leave empty to keep it unsigned",
"signProfileEmpty": "No active certificate for this platform — upload one in Signing certificates first",
"signProfileUnsupported": "This file type has no code-signing: APK uses the server keystore, ELF is not signed"
"signProfileUnsupported": "This file type has no code-signing: APK uses the server keystore, ELF is not signed",
"signProfileExpiredWarn": "The selected certificate has expired: the signature is still written into the artifact, but Windows treats it as untrusted. Fine for internal distribution; use a valid certificate for public releases."
},
"signProfiles": {
"title": "Signing certificates",
+2 -1
View File
@@ -2648,7 +2648,8 @@
"signProfileNone": "不重签(保持未签名)",
"signProfileHint": "选中证书会在加壳完成后用它对产物做 Authenticode 重签;不选则产物保持未签名",
"signProfileEmpty": "还没有可用的同平台证书,先去「签名证书」上传",
"signProfileUnsupported": "该文件类型没有代码签名机制:APK 用服务端 keystore,ELF 不签名"
"signProfileUnsupported": "该文件类型没有代码签名机制:APK 用服务端 keystore,ELF 不签名",
"signProfileExpiredWarn": "所选证书已过期:签名仍会写入产物,但 Windows 会因证书过期判定签名不受信任(自用/内网分发可用,对外发布建议换新证书)"
},
"signProfiles": {
"title": "签名证书",