使用 notarytool 公证 macOS 应用:有哪些变化,以及如何自动化
自 2023 年 11 月 1 日起,Apple 公证服务不再接受 altool 的上传。本文介绍公证会检查哪些内容、notarytool 如何保存凭据、等待结果并获取日志、为什么要装订(staple)票据,以及如何在 CI 中用脚本完成这些步骤。

公证(notarization)是 Apple 针对在 Mac App Store 之外分发的 Mac 软件所做的自动化安全检查。公证服务会扫描使用 Developer ID 签名的应用、磁盘映像和安装包,检查其中是否有恶意内容和代码签名问题;如果没有发现问题,就会签发一张票据,Gatekeeper 可以在线查找该票据,也可以将它装订在文件上。Apple 强调,这不是 App 审核。自 macOS 10.15 起,2019 年 6 月 1 日之后构建的 Developer ID 软件必须经过公证;而在 macOS Sequoia 中,对于未正确签名或公证的软件,用户不能再通过按住 Control 键点按的方式绕过 Gatekeeper。
有哪些变化:altool 不再用于公证,改用 notarytool
Apple 在 WWDC 2021 上推出了 notarytool,并首次随 Xcode 13 发布。自 2023 年 11 月 1 日起,公证服务不再接受来自 altool 或 Xcode 13 及更早版本的上传;Apple 的公告要求开发者改用 notarytool 或 Xcode 14 及更高版本。宣布这一截止时间的 WWDC22 演讲 当时只说明截止时间定在 2023 年秋季,并表示它涵盖 altool 的所有公证方式以及 Xcode 13 界面中的上传,而 notarytool(包括 Xcode 13 中自带的那一份)将继续可用。此前已经公证的软件不受影响,仍可正常使用。
Apple 的迁移说明 TN3147 列出了实际的差异,并指出 altool 仍适用于其他用途,例如向 App Store 提交应用;Apple 的 App Store Connect 帮助中仍把它列为上传构建版本的工具。凭据的类型保持不变:App 专用密码或 App Store Connect API 密钥。--primary-bundle-id 没有对应的替代选项,因为公证服务从未使用过它。提交 ID 和历史记录与 altool 的相互独立,而且不会发送完成通知邮件:请使用 --wait 或 --webhook。
公证服务的要求
Apple 的公证指南要求:
- 每个可执行文件都有有效签名,且使用 Developer ID 证书签署,而不是 Mac Distribution、ad hoc 或开发证书;
- 应用和命令行目标启用 Hardened Runtime;
- 签名中包含安全时间戳;
- 不得将
com.apple.security.get-task-allow授权设为 true; - 链接到 macOS 10.9 SDK 或更高版本,并且 XML 授权(entitlements)格式正确。
Xcode 的归档与导出流程已经处理了其中的大部分内容。对于手动签名,Apple 的分发签名代码指南指出:先签署嵌套的代码,传入 --timestamp,为主可执行文件加上 -o runtime,并且签名时避免使用 --deep。Hardened Runtime 会阻止某些功能,例如 JIT 编译,除非你添加相应的例外授权。
保存 notarytool 凭据
在长期运行的构建用 Mac 上,可以把凭据一次性保存为一个具名配置文件。使用 Apple ID 时,notarytool 会提示输入 App 专用密码并进行验证:
xcrun notarytool store-credentials "notary-ci" \
--apple-id "[email protected]" --team-id "TEAMID"
如果使用 App Store Connect API 密钥,请传入 --key(即 .p8 文件)、--key-id 和 --issuer;man 手册页说明,团队密钥必须提供 issuer ID,个人密钥则必须省略它。之后的命令使用 --keychain-profile "notary-ci",而 --keychain 用于指定某个具体的钥匙串文件。TN3147 建议用 xcrun notarytool history 进行测试。在用完即弃的运行器上,直接传入密钥相关选项更简单。
使用 --wait 提交并读取日志
公证服务不接受单独的 .app,所以 Apple 的工作流指南用 ditto 将其压缩为 zip。磁盘映像和已签名的扁平安装包可以直接提交。
ditto -c -k --keepParent "MyApp.app" "MyApp.zip"
xcrun notarytool submit "MyApp.zip" --keychain-profile "notary-ci" --wait
xcrun notarytool log <submission-id> --keychain-profile "notary-ci" notary-log.json
--wait 只在处理结束时才返回,因此无需轮询;--timeout 用于限制等待时间。JSON 日志会列出每个问题的严重程度、路径和消息。即使状态为 Accepted,也请阅读日志;Apple 指出,其中可能包含警告。Apple 称,大多数软件在 5 分钟内完成,98% 在 15 分钟内完成,并要求开发者把每天的公证次数控制在 75 次以内。
装订票据并检查 Gatekeeper
Gatekeeper 可以在线获取票据;装订则是把票据附加到文件上,使检查在离线时同样有效。
xcrun stapler staple "MyApp.app"
xcrun stapler validate "MyApp.app"
spctl -vvv --assess --type exec "MyApp.app"
应用、磁盘映像和扁平安装包都可以装订。ZIP 压缩包不能装订,所以请先装订应用,再重新压缩;目前独立的二进制文件同样无法装订。对于嵌套的容器,Apple 的打包指南指出,只需公证最外层的那一个。
常见的拒绝消息与解决办法
| 日志消息 | 通常的解决办法 |
|---|---|
| The signature of the binary is invalid. | 为所有内容签名,并且签名之后不要再修改 bundle。 |
| The binary is not signed with a valid Developer ID certificate. | 使用 Developer ID Application 证书;安装包则使用 Developer ID Installer 证书。 |
| The signature does not include a secure timestamp. | 签名时加上 --timestamp。 |
| The executable does not have the hardened runtime enabled. | 启用该功能,或使用 -o runtime 签名。 |
| The executable requests the com.apple.security.get-task-allow entitlement. | 通过 Xcode 归档或导出,或者在分发构建中将 CODE_SIGN_INJECT_BASE_ENTITLEMENTS 设为 NO。 |
Apple 的问题排查页面建议用 codesign -vvv --deep --strict 测试签名;安装包则使用 pkgutil --check-signature。
最简 CI 脚本示意
这段示例假设你已有一个签名并导出的应用,以及来自 CI 机密存储的团队 App Store Connect API 密钥。和 Apple 的归档脚本示例一样,它使用 PlistBuddy 读取 plist 输出。
set -eu
APP="build/MyApp.app"
codesign -vvv --deep --strict "$APP"
ditto -c -k --keepParent "$APP" upload.zip
xcrun notarytool submit upload.zip \
--key "$ASC_KEY_PATH" --key-id "$ASC_KEY_ID" --issuer "$ASC_ISSUER_ID" \
--wait --timeout 2h --output-format plist > notary.plist || true
STATUS=$(/usr/libexec/PlistBuddy -c "Print :status" notary.plist)
ID=$(/usr/libexec/PlistBuddy -c "Print :id" notary.plist)
xcrun notarytool log "$ID" \
--key "$ASC_KEY_PATH" --key-id "$ASC_KEY_ID" --issuer "$ASC_ISSUER_ID" \
notary-log.json
[ "$STATUS" = "Accepted" ] || exit 1
xcrun stapler staple "$APP"
ditto -c -k --keepParent "$APP" MyApp-notarized.zip
Apple 的示例脚本只读取 status 和 message,因此请先查看一次 notary.plist,以确认提交 ID 对应的键名。任务结束时请删除密钥文件。在受限网络中,Apple 的指南列出了需要放行的地址,包括 S3 上传端点、stapler 使用的 CloudKit 地址段以及 timestamp.apple.com。
资料来源
- Apple Developer Documentation: Notarizing macOS software before distribution
- Apple Developer Documentation: Customizing the notarization workflow
- Apple Developer Documentation: Resolving common notarization issues
- Apple Technote TN3147: Migrating to the latest notarization tool
- Apple Developer Documentation: Hardened Runtime
- Apple Developer Documentation: Creating distribution-signed code for macOS
- Apple Developer Documentation: Packaging Mac software for distribution
- Apple Developer Documentation: Customizing the Xcode archive process
- App Store Connect Help: Upload builds
- Apple Developer News: Apple notary service update (May 26, 2023)
- Apple Developer News: Updates to runtime protection in macOS Sequoia (August 6, 2024)
- WWDC22: What's new in notarization for Mac apps
- notarytool(1) man page (community mirror of the Xcode man pages)
- stapler(1) man page (community mirror of the Xcode man pages)


