# codesign Keychain 弹窗挂起构建

## 现象与根因

通过 SSH、CI 执行器或后台编程 Agent 运行 `codesign` 与 `xcodebuild archive` 时，进程常无限挂起等待系统弹窗，或直接报错 `errSecInternalComponent` 或 `errSecInteractionNotAllowed`（`User interaction is not allowed`）。macOS 会根据进程分区列表和图形会话锁状态限制访问 `login.keychain-db` 中的私钥，任何非交互式签名调用都会触发后台进程无法点击的系统授权框。

## 诊断与修复命令

检查当前可用的代码签名证书以及 Keychain 锁定配置：

```sh
# 列出当前 Keychain 搜索路径下有效的代码签名证书
security find-identity -v -p codesigning

# 查看 Keychain 搜索列表与自动锁定策略
security list-keychains -d user
security show-keychain-info ~/Library/Keychains/login.keychain-db
```

若现有脚本暂时仍需调用系统 `/usr/bin/codesign`，需在当前会话解锁 Keychain 并设置分区白名单：

```sh
# 解锁登录 Keychain
security unlock-keychain -p "<KEYCHAIN_PASSWORD>" ~/Library/Keychains/login.keychain-db

# 允许 codesign 无弹窗访问私钥
security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "<KEYCHAIN_PASSWORD>" ~/Library/Keychains/login.keychain-db
```

改为使用 XCLoom 在交互式 TTY 中一次导入 Development 证书，后续后台与编程 Agent 运行不再访问系统 Keychain（仅在专用发布机上导入 Distribution / Release 证书时再加 `--include-distribution`）：

```sh
# 查看 Keychain 中可导入的签名身份
xcloom sign list-keychain --json

# 仅需在交互终端执行一次（默认仅导入 Development 证书）
xcloom sign import-keychain --team-id <TEAM_ID>
# 仅限专用发布机（附加 Apple Distribution 证书）：
# xcloom sign import-keychain --team-id <TEAM_ID> --include-distribution
```

## XCLoom 如何防止复发

`xcloom sign import-keychain` 强制要求交互式 TTY（阻止无头 Agent 自动导出 Keychain 凭据）。默认仅导入 Development 身份，Distribution 与 Release 导入必须显式传入 `--include-distribution`（否则会报 `XCLOOM-SIGN-DISTRIBUTION-OPT-IN-REQUIRED`）。XCLoom 在排除 Time Machine 备份的 `0700` 团队目录内保存加密的 `kc_<fp>.p12`、`0600` 权限的 `.p12.password` 与公钥 `kc_<fp>.cer`，并在释放内存时清零敏感缓冲区。这套布局防的是磁盘残留明文 `.key.pem`、Time Machine 备份外泄、其他 macOS 账号跨用户读取以及无头 Agent 绕过 TTY 导出 Keychain；它不防同一 macOS 用户下的其他未沙箱化进程（同目录密码文件用于无人值守签名）。

后续 `xcloom build`、`xcloom archive` 与 `xcloom preview` 均在进程内直接完成 CMS 签名，完全不触发 macOS Keychain 弹窗。针对包含 27 个 bundle 的 `85 MB` 应用，XCLoom 自签 CMS 签名耗时 `0.43s`（9 次中位数），产物通过 `/usr/bin/codesign --verify --deep --strict` 严格校验；若证书尚未导入，工具会直接返回 `XCLOOM-SIGN-KEYCHAIN-UNIMPORTED` 并给出精确导入命令。

安全边界说明：由于无人值守本地签名需要在当前 macOS 用户下同时读取上述 `0600` 权限的 `.p12` 与 `.p12.password`，与你运行在同一 macOS 用户下的非沙箱编程 Agent 同样能访问当前用户拥有的文件。因此若你不信任 Agent 读取本地文件，建议编程 Agent 主机仅保留 Development 证书，并将 Apple Distribution 签名隔离到独立的系统用户或发布主机。

## 常见问题

### XCLoom 生成的 CMS 签名能通过 macOS 和 App Store Connect 校验吗？

XCLoom 生成标准的 Mach-O CodeDirectory 与 CMS 签名块，产物在本机通过 `/usr/bin/codesign --verify --deep --strict` 严格校验（针对含 27 个 bundle 的 `85 MB` 应用，自签 CMS 9 次中位数耗时 `0.43s`）。带 RFC 3161 TSA 时间戳的完整 Apple Distribution 签名、物理 iPhone 安装及 App Store Connect 接收情况尚未真机实测；随时可继续使用标准 `codesign` 或 `xcodebuild` 执行发布签名。

### 使用 XCLoom 后还需要在 CI 脚本里明文保存登录 Keychain 密码吗？

不需要。本机在交互式 TTY 中通过 `xcloom sign import-keychain` 一次导入（默认 Development，独立发布用户或发布主机上的 Distribution/Release 签名需加上 `--include-distribution`，或在 CI 上直接传入 `--p12` / `--cert` + `--key`）后，无需再向 `security unlock-keychain` 传递登录 Keychain 密码。

### 看到 `XCLOOM-SIGN-KEYCHAIN-UNIMPORTED` 报错该如何处理？

该错误表示系统 Keychain 中存在匹配当前 `DEVELOPMENT_TEAM` 的有效证书，但尚未导入 XCLoom 凭据库。仅需 Development 签名时在交互终端执行 `xcloom sign import-keychain --team-id <TEAM_ID>`，用于 Distribution 与 Release 签名时加上 `--include-distribution`（未加该参数会返回 `XCLOOM-SIGN-DISTRIBUTION-OPT-IN-REQUIRED`）。

## 相关链接

- [ITMS-90046 与上传后拒审](/zh/errors/itms-90046-and-app-store-preflight/)
- [iOS 编程 Agent 70% 的时间在等 Xcode](/zh/blog/70-percent-xcode-wait-claude-code-ios/)
- [XCLoom 基准测试数据](/zh/benchmarks/)
- [iOS 构建与模拟器排错索引](/zh/errors/)
