signtool 无法使用来自 AWS CloudHSM 的密钥对二进制文件进行签名

signtool fails to sign a binary with a key from a AWS CloudHSM

我们将使用 AWS CloudHSM 服务来确保所有代码签名证书的安全,并在我们的构建服务器上执行代码签名。我们的构建服务器是 Windows Server 2010,所以我在那里安装了 AWS CloudHSM 客户端。我激活了集群,所有命令行实用程序都按预期工作:我能够登录、添加密钥、查找密钥等。我们想继续使用 signtool 来签署我们的二进制文件,所以我认为我们可以使用密钥存储提供程序 (KSP)对于 AWS CloudHSM,它们与其他工具一起安装。

Cavium KSP 和 CNG 提供程序已成功安装,并且在 windows 加密提供程序的列表中可见。我在这里定义了环境变量 ( https://docs.aws.amazon.com/cloudhsm/latest/userguide/ksp-library-prereq.html ).

我通过 certutil 添加证书到 HSM 存储:

Certutil -CSP "Cavium Key Storage Provider" -user -importPFX  "certificate.pfx"
SDK Version: 2.03
Enter PFX password:
Certificate "myCertificate" added to store.

CertUtil: -importPFX command completed successfully.

证书已成功添加,当我从 key_mgmt_util.exe 控制台执行 findKey 命令时出现。

之后,我尝试使用 https://docs.microsoft.com/en-us/windows-hardware/test/hlk/user/hlk-signing-with-an-hsm:

中所述的证书对二进制文件进行签名
signtool_64 sign /n myCertificate "test.exe"

signtool sign /sha1 4F555EF9FAB8E86A2F84ACF325362A29FB64AF66 "test.exe"

但我遇到无法解决的错误

    SDK Version: 2.03
    Done Adding Additional Store
    SignTool Error: An error occurred while attempting to load the signing
    certificate from: C:\temp\test.exe

我还尝试指定密钥存储提供程序和密钥容器

signtool sign /csp "Cavium Key Storage Provider" /k CARoot-877f51a1-90ee-4c10-8feb-02925caab4fb test.exe

返回给我的

SignTool Error: An unexpected internal error has occurred.
Error information: "Could not associate private key with certificate." (-2147024
891/0x80070005)

signtool sign /f certificate.pem /csp "Cavium Key Storage Provider"
/k CARoot-877f51a1-90ee-4c10-8feb-02925caab4fb test.exe

有其他错误信息

SignTool Error: The specified private key does not match the public key of the selected certificate.

在我看来,存储的证书有问题,但我不知道如何解决这个问题。 test.exe 存在于磁盘上,可以使用来自其他提供商的证书或在指定 pfx 文件时使用 signtool 进行签名。

我做错了什么? Amazon CloudHSM 客户端是否与 signtool 兼容,或者我还可以如何使用 Amazon CloudHSM 作为密钥存储在 Windows 上签署二进制文件?

我写信给 AWS supported,他们回复说:

"This issue seems to be caused by trying to store the certificate on the HSM, and referencing the certificate with SignTool. Although the certutil command shows "CertUtil: -importPFX command completed successfully.", CloudHSM doesn't currently support certificate storage. This feature will be added however, and when it's released will be added to the version history page.

You should be able to use SignTool by referencing the certificate locally (.crt/.cer), and using the private key of the certificate stored on the HSM:

c:> signtool sign /f certname.cer /csp "Cavium Key Storage Provider" /k kontainer_name test.exe

但这种方法对我也不起作用。所以我还在等待他们的帮助

你试过了吗

setx /m n3fips_partition <my hsm id>
setx n3fips_password=CU-username:CU-password

signtool sign /f /csp "Cavium Key Storage Provider" /k <container name> test.exe

我不知道容器名称应该是什么。通常有一个工具可以在 HSM 分区和容器之间进行映射。

关于这个主题的 cloudhsm v2 文档可以在她那里找到 https://docs.aws.amazon.com/cloudhsm/latest/userguide/ksp-library-prereq.html

https://docs.microsoft.com/en-us/windows/desktop/seccrypto/signtool

在注册表中搜索我找到的 Cavium

Cavium CNG 提供商和 Cavium 密钥存储提供商。也许您需要映射到 ksp 的 cng?

此外,该项目的文档在 github 上,文档作者似乎是贡献者 https://github.com/awsdocs/aws-cloudhsm-user-guide/blob/master/doc_source/ksp-library-install.md

你运行 csp ksp注册工具了吗?

我刚刚写了一篇涵盖这种情况的文章 Signing executables with Microsoft SignTool.exe using AWS CloudHSM-backed certificates

总结一下:

  • 您需要确保拥有 CloudHSM 的最新二进制文件。

  • 检查创建证书时(如果您自行签名)是否创建了 Windows 中的相关密钥容器。

  • 运行 certutil -repairstore(如果需要)。
  • 使用 SignTool 时,请检查您是否指定了 证书哈希


如果您需要进一步的帮助,请一如既往地联系 AWS Support 或查看 AWS 论坛。