signtool ksp 与 Windows KSP 集成
signtool ksp 管理 Windows sslTrus Key Storage Provider(KSP)。KSP 安装和配置完成后,Microsoft signtool.exe 可以通过 CNG Provider 调用远程签名服务,无需在本地持有私钥。
signtool ksp 全部子命令仅支持 Windows 平台。在非 Windows 平台执行任意 ksp 子命令会返回:
key storage provider is only supported on windows
子命令概览
signtool ksp [command]
| 子命令 | 用途 |
|---|---|
install | 安装并注册 KSP DLL |
uninstall | 注销 KSP 并删除 DLL |
list | 列出已保存的 KSP 密钥配置 |
add | 新增 KSP 密钥配置(交互式) |
del | 删除指定 KSP 密钥配置(交互式) |
--address 参数仅在 ksp add 时有效,用于获取证书 PEM 并保存远程服务地址。
安装 KSP
signtool ksp install
执行后:
- 将内嵌的
sslTrusKSP.dll写入ProgramData/sslTrusKSP/sslTrusKSP.dll。 - 若 ProgramData 中的 DLL 版本与 System32 中不同,则将 DLL 复制到 System32。
- 注册 CNG Provider:
sslTrus Key Storage Provider。 - 保存 KSP 配置文件
ProgramData/sslTrusKSP/config.dat(DPAPI 加密)。
install 会修改系统级 CNG Provider 注册,建议在管理员终端执行。若检测到 DLL 版本相同,会记录 ksp is already installed 并直接返回。
卸载 KSP
signtool ksp uninstall
执行后:
- 注销 CNG Provider:
sslTrus Key Storage Provider。 - 删除
ProgramData/sslTrusKSP/sslTrusKSP.dll。 - 删除 System32 中的
sslTrusKSP.dll。
uninstall 不会删除整个 ProgramData/sslTrusKSP 目录。已保存的 config.dat、证书文件和日志需要手动清理。
查看密钥配置
signtool ksp list
输出字段说明:
| 字段 | 含义 |
|---|---|
No | 序号 |
CertCode | 证书编号 |
ServerUrl | 显示为 nicsrs 或 default |
AccessKey | Access Key |
AccessSecret | 脱敏后的 Access Secret |
- 若没有配置文件,返回
no ksp configuration。 - 若配置存在但密钥列表为空,记录
no ksp key。 AccessSecret脱敏显示,但配置文件本身仍应按敏感文件管理。
添加密钥配置
signtool ksp add
指定 NICSRS 地址:
signtool ksp add --address nicsrs
命令为交互式,依次输入:
Please enter the access key: your-access-key
Please enter the access secret: your-access-secret
Please enter the certificate code: CERT_CODE
执行后:
- 调用远程
/v1/codesign/cert获取证书 PEM。 - 将证书保存为
ProgramData/sslTrusKSP/CERT_CODE.crt。 - 将密钥配置写入
ProgramData/sslTrusKSP/config.dat(DPAPI 加密)。
若证书编号已存在,会提示:
The certificate code already exists, do you want to override it? [y/N]
输入 y 覆盖,其他值或直接回车则放弃。
ksp add 是交互命令,当前不支持 --access-key、--cert-code 等非交互参数。添加配置会访问远程 API,并将证书 PEM 写入本地 ProgramData。
删除密钥配置
signtool ksp del
交互输入证书编号:
Please enter the certificate code: CERT_CODE
执行后从 config.dat 中删除匹配的密钥配置,并重新保存加密后的配置文件。
当前实现只删除配置项,不删除 ProgramData/sslTrusKSP/CERT_CODE.crt 证书文件。
通过 KSP 使用 Microsoft signtool.exe
KSP 配置完成后,使用 Windows SDK 中的 Microsoft signtool.exe 调用 Provider。
以下命令中的 signtool.exe 是 Microsoft Windows SDK 自带的工具,不是锐安信 CLI。如果当前目录或 PATH 中同时存在两者,请使用 Windows SDK 中 signtool.exe 的完整路径以避免混淆。
SHA256 签名示例:
signtool.exe sign /v ^
/csp "sslTrus Key Storage Provider" ^
/kc CERT_CODE ^
/f C:\ProgramData\sslTrusKSP\CERT_CODE.crt ^
/fd SHA256 ^
/tr http://timestamp.acs.microsoft.com ^
/td SHA256 ^
.\EXAMPLE.exe
追加 SHA1 签名示例:
signtool.exe sign /v ^
/csp "sslTrus Key Storage Provider" ^
/kc CERT_CODE ^
/f C:\ProgramData\sslTrusKSP\CERT_CODE.crt ^
/fd SHA1 ^
/tr http://timestamp.acs.microsoft.com ^
/td SHA256 ^
/as ^
.\EXAMPLE.exe
参数说明:
| 参数 | 含义 |
|---|---|
/csp "sslTrus Key Storage Provider" | 指定锐安信安装的 KSP Provider |
/kc CERT_CODE | 指定 KSP 密钥名称,约定为证书编号 |
/f C:\ProgramData\sslTrusKSP\CERT_CODE.crt | 指定 ksp add 保存的证书文件 |
/fd SHA256 或 /fd SHA1 | 文件摘要算法 |
/tr <url> | RFC3161 时间戳服务 |
/td SHA256 | 时间戳摘要算法 |
/as | 追加签名,不覆盖已有签名 |
排障参考
| 错误信息 | 可能原因 | 处理建议 |
|---|---|---|
key storage provider is only supported on windows | 在非 Windows 平台执行了 ksp 命令。 | 只在 Windows 上执行 KSP 操作。 |
no ksp configuration | 尚未创建 KSP 配置文件。 | 先执行 signtool ksp install 和 signtool ksp add。 |
no such certificate code | 删除的证书编号不存在。 | 使用 signtool ksp list 确认证书编号。 |
安全注意事项
signtool ksp install和signtool ksp uninstall会修改 Windows 系统目录和 CNG Provider 注册,通常需要管理员权限。signtool ksp add会访问远程证书接口,并将证书文件和加密配置写入ProgramData/sslTrusKSP。- Access Secret、KSP 配置文件和本地日志均应按敏感信息处理,不要写入日志或版本仓库。