系统学习 macOS 学习路径:从 BSD 文件命令到系统、网络与开发维护

codesign

查看、验证和创建 macOS 代码签名,是 Gatekeeper 信任链的起点。

补充说明

codesign 用钥匙串中的签名身份为 app、framework、插件和安装包签名,并在产物里写入签名、 entitlements 和时间戳。-s 指定身份,--options runtime 启用硬化运行时,--timestamp 请求可信时间戳,--entitlements 附加权限声明。签名必须由内向外:先签嵌套的 framework、XPC 服务和辅助工具,再签外层 app。

边界:签名只证明“是谁签的、内容有没有被改”,要过 Gatekeeper 还必须有有效的 Developer ID 证书和私钥、启用硬化运行时、完成 Apple 公证(notarization)并 stapling。钥匙串未解锁、私钥 ACL 未放行时,签名会失败或弹窗卡住。临时改动用 ad-hoc 签名(-s -)只能在本地测试,不能分发。

常用示例

# 1. 查看现有签名、Team ID、flags 和时间戳;信息写在 stderr
% codesign --display --verbose=4 /Applications/Safari.app 2>&1 | head -20

# 2. 验证签名完整性和嵌套代码
% codesign --verify --deep --strict --verbose=2 MyApp.app

# 3. 导出 entitlements,确认应用申请了哪些权限
% codesign -d --entitlements :- MyApp.app 2>/dev/null

# 4. 用 Developer ID 身份签名,启用硬化运行时并加时间戳
% codesign -s "Developer ID Application: Example Corp (TEAMID1234)" \
  --options runtime --timestamp --entitlements MyApp.entitlements -f MyApp.app

# 5. 用 Gatekeeper 的评估逻辑检查能否放行
% spctl --assess --type execute -vv MyApp.app

# 6. 公证并等待结果,再 stapling 到 app 上供离线校验
#    先执行 xcrun notarytool store-credentials AC_PASSWORD 保存凭据
% xcrun notarytool submit MyApp.zip --keychain-profile "AC_PASSWORD" --wait
% xcrun stapler staple MyApp.app
% xcrun stapler validate MyApp.app

易错点与排错

相关命令

参考链接

读完自测

合上页面,先回答,再展开对照。

  1. 有了 codesign 签名,为什么应用仍可能被 Gatekeeper 拦截?
  2. 为什么签名要从内向外进行?顺序错了会怎样?
  3. codesign -dv 看不到输出、codesign --verify 又失败时,应该怀疑什么?
答案
  1. Gatekeeper 还要求 Developer ID 身份、硬化运行时、公证和 stapling;只签名不公证仍会被拦截。
  2. 外层签名覆盖嵌套内容,先签外层再改内层会让外层失效;应由内向外逐层签名。
  3. -dv 把信息写到 stderr,需要重定向;同时检查钥匙串是否解锁、身份是否存在、嵌套代码是否都已正确签名。
微信小程序码:扫码打开命令速查小程序

小程序版

微信扫码,随时查命令