# 代码签名

代码签名是一种软件安全技术，它将密码学数字签名附加到可执行代码、脚本或软件包上，使得任何运行或分发这些内容的人都能验证代码的发布者是谁，以及代码自签名以来未被篡改。它提供的是代码完整性，而非文件完整性：例如，Authenticode 会故意将文件的某些部分保持为未签名状态，以便后续添加签名和时间戳；证书标识的是软件的发布者，而非某个特定的软件对象。<sup>[1](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms537361%28v=vs.85%29)</sup><sup> • </sup><sup>[2](https://cabforum.org/uploads/CA-Browser-Forum-CSCBR-3.11.0.pdf)</sup> 有效的签名并不代表代码质量：CA/Browser Forum 的扩展验证指南明确指出，此类证书“不保证代码不存在漏洞、恶意软件、缺陷或其他问题。”<sup>[3](https://cabforum.org/uploads/EV-Code-Signing-v.1.2.pdf)</sup> 学术界的综述也持同样观点：即使密码学上可确保来源，也“不保证正确性或安全性”，尽管它确实能降低诸如中间人替换下载内容等风险。<sup>[4](https://arxiv.org/html/2407.03949)</sup>

| 关键事实 | 详情 |
|---|---|
| 签名证明什么 | 发布者身份以及代码自签名以来未变更；不证明代码安全或无漏洞<sup>[2](https://cabforum.org/uploads/CA-Browser-Forum-CSCBR-3.11.0.pdf)</sup><sup> • </sup><sup>[3](https://cabforum.org/uploads/EV-Code-Signing-v.1.2.pdf)</sup> |
| 核心机制 | 对代码做哈希，用发布者私钥对哈希签名，再通过 X.509 证书链验证<sup>[5](https://docs.oracle.com/cd/E19957-01/816-6171-10/)</sup><sup> • </sup><sup>[6](https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/AboutCS/AboutCS.html)</sup> |
| 算法（公共 CA） | 最低 RSA-3072；自 2021 年 6 月 1 日起禁止使用 SHA-1<sup>[2](https://cabforum.org/uploads/CA-Browser-Forum-CSCBR-3.11.0.pdf)</sup> |
| 密钥存储 | 自 2023 年 6 月 1 日起，订户私钥必须在硬件加密模块中生成、存储和使用<sup>[2](https://cabforum.org/uploads/CA-Browser-Forum-CSCBR-3.11.0.pdf)</sup> |
| 证书费用 | OV 证书约每年 150–300 美元；EV 证书每年 400 美元以上<sup>[7](https://learn.microsoft.com/en-us/windows/apps/package-and-deploy/code-signing-options)</sup> |
| 时间戳 | 可信时间戳记录签名时间，使签名在证书过期后仍然有效<sup>[8](https://developer.apple.com/documentation/technotes/tn3161-inside-code-signing-certificates/)</sup> |
| 主要失效模式 | 密钥被盗与撤销机制薄弱：已签名的恶意软件即使在证书过期后仍可保持受信任状态<sup>[9](https://www.usenix.org/system/files/conference/usenixsecurity18/sec18-kim.pdf)</sup> |

## 工作原理

代码签名采用“先哈希后签名”的构造。签名工具计算代码的单向哈希值，再用签名者的私钥加密该哈希值；加密后的哈希值即为数字签名。<sup>[5](https://docs.oracle.com/cd/E19957-01/816-6171-10/)</sup> Apple 的文档将同样的结构描述为“封印”（seal），即代码各部分校验和或哈希值的集合，外加对封印的数字签名，以及签名者的证书链。验证时重新计算哈希值并与存储的值比对；任何修改都会改变摘要值，从而表明遭到篡改。<sup>[6](https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/AboutCS/AboutCS.html)</sup>

验证分为两个不同步骤：首先确认签名本身，即签名后没有任何改动；然后评估签名者的公钥在相关上下文中是否受信任。<sup>[8](https://developer.apple.com/documentation/technotes/tn3161-inside-code-signing-certificates/)</sup> 信任通过证书颁发机构签发的 X.509 版本 3 证书来表达；在 Authenticode 中，PE 文件的 PKCS #7 签名内容存储在二进制文件的附加节中。<sup>[1](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms537361%28v=vs.85%29)</sup>

**时间戳与撤销**决定签名有效期的长短。macOS 上的 Developer ID 签名内嵌安全时间戳，记录代码签名的时间，系统会检查证书在签名时是否有效，因此签名在证书过期后仍然有效。<sup>[8](https://developer.apple.com/documentation/technotes/tn3161-inside-code-signing-certificates/)</sup> 由于可信时间戳延长了已签名二进制文件的寿命，代码签名 CA 必须无限期维护 CRL 和 OCSP（用于检查证书是否被撤销的在线协议）撤销服务，这一点与 Web PKI 不同。<sup>[9](https://www.usenix.org/system/files/conference/usenixsecurity18/sec18-kim.pdf)</sup> 现行 CA/Browser Forum 规则要求 CA 至少支持 RSA-3072，并禁止在代码签名证书中使用 SHA-1。<sup>[2](https://cabforum.org/uploads/CA-Browser-Forum-CSCBR-3.11.0.pdf)</sup>

## 如何操作

开发者从证书颁发机构获取代码签名证书，CA 会验证发布者的身份。DigiCert、Sectigo 等 CA 签发的组织验证（OV）证书通常每年费用为 150–300 美元，而 EV 证书每年费用为 400 美元以上。<sup>[7](https://learn.microsoft.com/en-us/windows/apps/package-and-deploy/code-signing-options)</sup> 自 2023 年 6 月 1 日起，根据 CA/Browser Forum 的密钥存储要求，SSL.com 等 CA 仅在 FIPS 140-2 USB 令牌上或通过云签名服务签发 OV 和 IV 代码签名证书。<sup>[10](https://www.ssl.com/guide/esigner-pricing-for-code-signing/)</sup>

签名步骤本身因平台而异。归档文件中包含：清单中每个文件的摘要条目、一个 .SF 签名文件，以及一个包含签名和签名者证书的签名块文件（通常为 .DSA）。<sup>[11](https://docs.oracle.com/javase/tutorial/deployment/jar/intro.html)</sup> 在 Apple 平台上，签名需要证书及匹配的私钥，二者合称“代码签名身份”，私钥可保存在智能卡或硬件令牌上。<sup>[8](https://developer.apple.com/documentation/technotes/tn3161-inside-code-signing-certificates/)</sup>

## 起源

代码签名在 1996 年实现了广泛部署。一项由 40 多家公司支持的代码签名提案被提交至[万维网联盟](https://www.edgepedia.org/zh/world-wide-web-consortium)，同年 3 月，40 多家公司宣布支持该倡议，VeriSign 和 GTE 提供底层的证书颁发机构基础设施。<sup>[12](https://news.microsoft.com/source/1996/03/12/industry-embraces-microsofts-internet-digital-signature-initiative/)</sup><sup> • </sup><sup>[13](https://news.microsoft.com/source/1996/08/07/microsoft-and-verisign-provide-first-technology-for-secure-downloading-of-software-over-the-internet/)</sup> Authenticode 结合 VeriSign 面向软件发布者的数字 ID，在 [Internet Explorer](https://www.edgepedia.org/zh/internet-explorer) 3.0 beta 2 中得到支持，并且免费提供。<sup>[13](https://news.microsoft.com/source/1996/08/07/microsoft-and-verisign-provide-first-technology-for-secure-downloading-of-software-over-the-internet/)</sup>

对象签名（Object Signing）使用 X.509 v3 证书和 PKCS #7，让用户能够获取关于下载的 Java 小程序、[JavaScript](https://www.edgepedia.org/zh/javascript) 和插件的可靠信息，并采用跨平台的 Java 归档（JAR）格式将签名与文件关联而不修改文件本身。<sup>[5](https://docs.oracle.com/cd/E19957-01/816-6171-10/)</sup> Java 平台从 JDK 1.1 起支持数字签名的 JAR 字节码，在 Java 2 中支持大幅扩展。<sup>[14](https://www.securingjava.com/chapter-three/chapter-three-3.html)</sup> 硬件密钥保护分阶段实现：微软要求 CA 遵循 CASC 指南，自 2017 年 2 月 1 日起将私钥存储在 USB 令牌或 HSM 等安全密码硬件上，并使用符合 RFC-3161 的时间戳机构，<sup>[15](https://obj.umiacs.umd.edu/papers_for_stories/kim_ACMCCS2017.pdf)</sup> 而 CA/Browser Forum 自 2023 年 6 月 1 日起强制要求订户密钥使用硬件加密模块。<sup>[2](https://cabforum.org/uploads/CA-Browser-Forum-CSCBR-3.11.0.pdf)</sup>

## 变体

**Authenticode** 是微软的数字签名格式，基于 PKCS #7 标准，用于对 PE 文件等受支持文件格式中的可执行代码进行签名。<sup>[1](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms537361%28v=vs.85%29)</sup> **Apple Developer ID 与公证**适用于 macOS：在 macOS 10.15 或更高版本上，所有在 App Store 之外分发的应用都必须使用 Apple 签发的 Developer ID 证书签名并经 Apple 公证，才能在默认 Gatekeeper 设置下运行。签名由开发者执行，证明软件自签名以来未被篡改；公证可由分发链中的任何人执行，证明 Apple 已检查代码是否含有恶意软件。公证输出是存储在 Apple 服务器上的票据（ticket），可附加到应用上而不使开发者的签名失效。<sup>[16](https://support.apple.com/guide/security/app-code-signing-process-sec3ad8e6e53/1/web/1)</sup>

**Android** 支持三种应用签名方案：v1 基于 JAR 签名；v2 随 Android 7.0 发布；v3 随 Android 9 发布并增加了密钥轮换。v1 签名不保护 APK 的某些部分（如 ZIP 元数据），给验证者留下了相当大的攻击面；v2 及更高版本将 APK 视为一个整体数据块，对整个文件检查签名，因此任何修改都会使签名失效，且验证速度大幅提升。<sup>[17](https://source.android.com/docs/security/features/apksigning)</sup> **Sigstore** 采取了不同的方法：无密钥签名将身份而非密钥与制品签名关联。Fulcio 是一个证书颁发机构，将 [OpenID Connect](https://www.edgepedia.org/zh/openid-connect) 身份绑定到公钥，并将签发的证书写入可公开审计的透明度日志；它签发将临时密钥绑定到身份的短期证书，签名事件记录在签名透明度日志 Rekor 中；借助临时密钥和短期证书，Fulcio 无需撤销列表。cosign 工具支持这种无密钥模式作为默认方式，此外还支持硬件和 KMS 签名、自带 PKI 以及在 OCI 注册表中进行容器签名。<sup>[18](https://docs.sigstore.dev/cosign/signing/overview/)</sup><sup> • </sup><sup>[19](https://github.com/sigstore/architecture-docs/blob/main/fulcio-spec.md)</sup><sup> • </sup><sup>[20](https://github.com/sigstore/cosign?tab=readme-ov-file)</sup>

## 应用

在业界实践中，签名正日益嵌入 CI/CD 流程：在一项 2025 年的访谈研究中，来自 11 个组织的 14 名受访者报告对构建产物进行签名，来自 6 个组织的 10 名受访者在签名的同时使用证明（attestation）、SBOM 和其他元数据，尽管一些团队将签名视为“打勾”形式，并不验证签名。<sup>[21](https://www.usenix.org/system/files/usenixsecurity25-kalu.pdf)</sup> 云签名服务将密钥存储在通过 FIPS 140-2 三级验证的 HSM 中，并与 signtool.exe 集成，可在任何支持标准 Windows 签名的流水线中工作，包括 [GitHub Actions](https://www.edgepedia.org/zh/github-actions)、Jenkins、Azure DevOps、CircleCI、GitLab CI 和 TeamCity。<sup>[22](https://www.ssl.com/products/software-integrity/signing-service/)</sup> Microsoft Store 的 MSIX 发布免费，微软在认证后会对包重新签名；Azure Artifact Signing 服务（原 Trusted Signing）费用约为每月 9.99 美元。<sup>[7](https://learn.microsoft.com/en-us/windows/apps/package-and-deploy/code-signing-options)</sup>

## 局限与替代方案

NIST 列举了代码签名系统的五类威胁：签名私钥被盗、签发未授权的证书、对证书或密钥的错误信任、对未授权或恶意代码的签名，以及使用不安全的密码学。NIST 建议将签名密钥存储在功能最简的 HSM 中，该 HSM 所在机器应仅安装最少的应用并保持最少的网络连接，并指出某些代码签名系统中有限的撤销机制加剧了密钥被盗的威胁。<sup>[23](https://nvlpubs.nist.gov/nistpubs/CSWP/NIST.CSWP.01262018.pdf)</sup>

**撤销是最薄弱的环节。** Authenticode 验证允许在撤销日期之前签名的带时间戳可执行文件在撤销后继续通过验证，这是软撤销问题；硬撤销是指 CA 将撤销日期设为证书的签发日期。<sup>[24](https://software.imdea.org/~juanca/papers/malsign_ccs15.pdf)</sup> CA/Browser Forum 的规则反映了这种张力：在密钥泄露或用于可疑代码时，CA 应在 24 小时内撤销，且必须在 5 天内撤销，而软件应将撤销日期之前加盖时间戳的对象视为有效。<sup>[2](https://cabforum.org/uploads/CA-Browser-Forum-CSCBR-3.11.0.pdf)</sup> 在 Windows 代码签名 PKI 中，145,582 张叶证书中有 788 张（0.5%）既不含 CRL 也不含 OCSP 地址，使客户端无法检查撤销状态；而且由于代码签名信任模型默认有效，正确签名并加盖时间戳的恶意软件即使在证书过期后仍然有效。<sup>[9](https://www.usenix.org/system/files/conference/usenixsecurity18/sec18-kim.pdf)</sup> 一项 2026 年 NDSS 关于滥用生态系统的研究发现，在 1,354 张与带时间戳的滥用样本关联的已撤销证书中，有 322 张（23.78%）的撤销日期不正确；另有 3,789 张证书（38.96%）满足“幽灵证书”条件，即过期或已撤销的中间证书在结构上阻止了被滥用的终端实体证书的撤销；同一研究发现 CA 在滥用治理中扮演被动角色，依赖外部报告，很少主动监控已签发的证书。<sup>[25](https://www.ndss-symposium.org/wp-content/uploads/2026-f2857-paper.pdf)</sup> 一项 2017 年对排名前十的代码签名 CA 的调查发现，当时只有 [Certum](https://www.edgepedia.org/zh/certum) 遵循了 CASC 指南，表明代码签名仍然容易受到证书盗窃和欺诈性申请的影响。<sup>[15](https://obj.umiacs.umd.edu/papers_for_stories/kim_ACMCCS2017.pdf)</sup>

**替代方案是重构信任，而非扩展信任。**更新框架（The Update Framework，TUF）专为更新系统设计——在更新系统中，单个受信任密钥的泄露历来是致命性的；它采用职责分离、多重签名阈值以及显式和隐式密钥撤销来抵御部分泄露，其作者指出，从密钥泄露的角度看，Authenticode 风格的 PKI 信任严格劣于直接密钥信任，因为可被攻击的密钥更多。<sup>[26](https://theupdateframework.io/papers/survivable-key-compromise-ccs2010.pdf)</sup> Sigstore 的无密钥签名解决了运维层面的根本原因：在一项业界访谈研究中，密钥管理问题是被报告最多的技术挑战，受访者将无密钥签名视为消除长期静态密钥的解决方案。来自 6 个组织的 7 名受访者认为仅靠签名不足以证明来源，只有与证明和 SBOM 配合才有价值。<sup>[21](https://www.usenix.org/system/files/usenixsecurity25-kalu.pdf)</sup>

近期的规则变化塑造着当前实践。对于 2026 年 3 月 1 日或之后签发的证书，CA/Browser Forum 基线要求有效期不超过 460 天。<sup>[2](https://cabforum.org/uploads/CA-Browser-Forum-CSCBR-3.11.0.pdf)</sup> 自 2025 年 4 月 15 日起，时间戳机构必须将带有时间戳 EKU 的根 CA 和从属 CA 证书的私钥保护在离线硬件加密模块中。<sup>[2](https://cabforum.org/uploads/CA-Browser-Forum-CSCBR-3.11.0.pdf)</sup> 此前，EV 代码签名证书可立即获得 SmartScreen 信誉，而标准证书必须逐步积累信誉；<sup>[15](https://obj.umiacs.umd.edu/papers_for_stories/kim_ACMCCS2017.pdf)</sup> 自 2024 年起，根据微软现行文档，EV 证书不再即时绕过 SmartScreen，而是与 OV 证书经历相同的信誉积累过程。<sup>[7](https://learn.microsoft.com/en-us/windows/apps/package-and-deploy/code-signing-options)</sup> 微软还宣布了 Windows 的密码学过渡计划：Windows 签名将在 2026 年底前转向 RSA-3072 和 SHA-384，Windows 生产签名将在 2027 年默认过渡到后量子密码学，可能采用混合签名构造。<sup>[27](https://support.microsoft.com/en-us/servicing/os/windows/docs/2026/08/next-generation-code-signing)</sup> NIST 此前曾警告，足以破解现有密码体系的量子计算机可能使先前部署的代码签名系统变得不安全。<sup>[23](https://nvlpubs.nist.gov/nistpubs/CSWP/NIST.CSWP.01262018.pdf)</sup>

## References

1. [ms537361(v=vs.85) (learn.microsoft.com)](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms537361%28v=vs.85%29)
2. [Baseline Requirements for the Issuance and Management of Publicly-Trusted Code Signing Certificates, v3.11](https://cabforum.org/uploads/CA-Browser-Forum-CSCBR-3.11.0.pdf)
3. [Guidelines For The Issuance And Management Of Extended Validation Code Signing Certificates (v1.2)](https://cabforum.org/uploads/EV-Code-Signing-v.1.2.pdf)
4. [Establishing Provenance Before Coding: Traditional and Next-Gen Software Signing](https://arxiv.org/html/2407.03949)
5. [Netscape Object Signing: Establishing Trust for Downloaded Software](https://docs.oracle.com/cd/E19957-01/816-6171-10/)
6. [About Code Signing (Apple Code Signing Guide)](https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/AboutCS/AboutCS.html)
7. [Code signing options for Windows app developers - Microsoft Learn](https://learn.microsoft.com/en-us/windows/apps/package-and-deploy/code-signing-options)
8. [TN3161: Inside Code Signing: Certificates](https://developer.apple.com/documentation/technotes/tn3161-inside-code-signing-certificates/)
9. [The Broken Shield: Measuring Revocation Effectiveness in the Windows Code-Signing PKI](https://www.usenix.org/system/files/conference/usenixsecurity18/sec18-kim.pdf)
10. [eSigner Pricing for Code Signing - SSL.com](https://www.ssl.com/guide/esigner-pricing-for-code-signing/)
11. [Understanding Signing and Verification (The Java Tutorials)](https://docs.oracle.com/javase/tutorial/deployment/jar/intro.html)
12. [Industry Embraces Microsoft's Internet Digital Signature Initiative](https://news.microsoft.com/source/1996/03/12/industry-embraces-microsofts-internet-digital-signature-initiative/)
13. [Microsoft and VeriSign Provide First Technology For Secure Downloading of Software Over the Internet](https://news.microsoft.com/source/1996/08/07/microsoft-and-verisign-provide-first-technology-for-secure-downloading-of-software-over-the-internet/)
14. [Signed Code (Ch. 3, Sec. 3) [Securing Java]](https://www.securingjava.com/chapter-three/chapter-three-3.html)
15. [Certified Malware: Measuring Breaches of Trust in the Windows Code-Signing PKI](https://obj.umiacs.umd.edu/papers_for_stories/kim_ACMCCS2017.pdf)
16. [App code signing process in macOS - Apple Support](https://support.apple.com/guide/security/app-code-signing-process-sec3ad8e6e53/1/web/1)
17. [App signing | Android Open Source Project](https://source.android.com/docs/security/features/apksigning)
18. [Overview - Sigstore](https://docs.sigstore.dev/cosign/signing/overview/)
19. [fulcio-spec.md](https://github.com/sigstore/architecture-docs/blob/main/fulcio-spec.md)
20. [sigstore/cosign](https://github.com/sigstore/cosign?tab=readme-ov-file)
21. [An Industry Interview Study of Software Signing for Supply Chain Security](https://www.usenix.org/system/files/usenixsecurity25-kalu.pdf)
22. [eSigner Cloud Code Signing Service - SSL.com](https://www.ssl.com/products/software-integrity/signing-service/)
23. [Security Considerations for Code Signing (NIST CSWP)](https://nvlpubs.nist.gov/nistpubs/CSWP/NIST.CSWP.01262018.pdf)
24. [Certified PUP: Abuse in Authenticode Code Signing](https://software.imdea.org/~juanca/papers/malsign_ccs15.pdf)
25. [Understanding the Status and Strategies of the Code Signing Abuse Ecosystem](https://www.ndss-symposium.org/wp-content/uploads/2026-f2857-paper.pdf)
26. [Survivable Key Compromise in Software Update Systems](https://theupdateframework.io/papers/survivable-key-compromise-ccs2010.pdf)
27. [Preparing the Windows ecosystem for next-generation code signing (KB 5125813)](https://support.microsoft.com/en-us/servicing/os/windows/docs/2026/08/next-generation-code-signing)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Sep 30, 2026 · Last review: Sep 30, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 网络与安全 › Titles 2 to Ma

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/code-signing

English original: https://www.edgepedia.org/code-signing
