diff --git a/backend/go.mod b/backend/go.mod index a958513..01e1e3b 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -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 ( diff --git a/backend/go.sum b/backend/go.sum index d59439f..d02bfc8 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -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= diff --git a/backend/internal/router/admin/protect.go b/backend/internal/router/admin/protect.go index 986acc3..5974cee 100644 --- a/backend/internal/router/admin/protect.go +++ b/backend/internal/router/admin/protect.go @@ -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 任务列表 diff --git a/backend/internal/router/admin/signprofiles.go b/backend/internal/router/admin/signprofiles.go index 0db4abd..d8a9cd7 100644 --- a/backend/internal/router/admin/signprofiles.go +++ b/backend/internal/router/admin/signprofiles.go @@ -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 +} diff --git a/frontend/src/pages/admin/protect/index.vue b/frontend/src/pages/admin/protect/index.vue index c55b9f4..dcfdd85 100644 --- a/frontend/src/pages/admin/protect/index.vue +++ b/frontend/src/pages/admin/protect/index.vue @@ -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(() => { - + {{ certLabel(p) }} - + {{ daysToExpire(p) }} 天后过期 @@ -443,6 +455,9 @@ onBeforeUnmount(() => {

{{ eligibleProfiles.length ? t('admin.protect.signProfileHint') : t('admin.protect.signProfileEmpty') }}

+

+ {{ t('admin.protect.signProfileExpiredWarn') }} +

{{ t('admin.protect.signProfileUnsupported') }}

diff --git a/frontend/src/plugins/i18n/en.json b/frontend/src/plugins/i18n/en.json index 7eb371b..7b00b59 100644 --- a/frontend/src/plugins/i18n/en.json +++ b/frontend/src/plugins/i18n/en.json @@ -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", diff --git a/frontend/src/plugins/i18n/zh.json b/frontend/src/plugins/i18n/zh.json index af82f2d..2f1b8a2 100644 --- a/frontend/src/plugins/i18n/zh.json +++ b/frontend/src/plugins/i18n/zh.json @@ -2648,7 +2648,8 @@ "signProfileNone": "不重签(保持未签名)", "signProfileHint": "选中证书会在加壳完成后用它对产物做 Authenticode 重签;不选则产物保持未签名", "signProfileEmpty": "还没有可用的同平台证书,先去「签名证书」上传", - "signProfileUnsupported": "该文件类型没有代码签名机制:APK 用服务端 keystore,ELF 不签名" + "signProfileUnsupported": "该文件类型没有代码签名机制:APK 用服务端 keystore,ELF 不签名", + "signProfileExpiredWarn": "所选证书已过期:签名仍会写入产物,但 Windows 会因证书过期判定签名不受信任(自用/内网分发可用,对外发布建议换新证书)" }, "signProfiles": { "title": "签名证书",