【问题标题】:Signing executables fails inside Docker container在 Docker 容器中签署可执行文件失败
【发布时间】:2020-07-15 12:54:27
【问题描述】:

我正在尝试使用docker-windows 设置在Gitlab Pipeline 中签署.exe.dll 文件,使用docker 映像:
mcr.microsoft.com/dotnet/framework/sdk:4.8-windowsservercore-ltsc2019

我尝试调用这些命令:
> sn.exe -R myfile.exe myKey.snk
> signtool.exe sign /v /f myCert.p12 /p myPassword /fd sha256 /tr "http://sha256timestamp.ws.symantec.com/sha256/timestamp" /td sha256 myFile.exe

在我的机器上本地执行此操作时,文件会成功签名:

> sn.exe -R myfile.exe myKey.snk
Microsoft (R) .NET Framework Strong Name Utility  Version 4.0.30319.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly 'myFile.exe' successfully re-signed
> signtool.exe sign /v /f myCert.p12 /p myPassword /fd sha256 /tr "http://sha256timestamp.ws.symantec.com/sha256/timestamp" /td sha256 myFile.exe
The following certificate was selected:
    Issued to: someone
    Issued by: some-private-ca
    Expires:   Fri Aug 28 09:40:11 2020
    SHA1 hash: hash

Done Adding Additional Store
Successfully signed: myFile.exe

Number of files successfully Signed: 1
Number of warnings: 0
Number of errors: 0

但是,使用 Gitlab 管道强名称工具 (sn.exe) 和 signtool.exe 失败:

> sn.exe -R myfile.exe myKey.snk
Microsoft (R) .NET Framework Strong Name Utility  Version 4.0.30319.0
Copyright (c) Microsoft Corporation.  All rights reserved.
Failed to re-sign the assembly -- Error code: 80131701
> signtool.exe sign /v /f myCert.p12 /p myPassword /fd sha256 /tr "http://sha256timestamp.ws.symantec.com/sha256/timestamp" /td sha256 myFile.exe
The following certificate was selected:
Done Adding Additional Store

我没有找出错误代码80131701 指的是什么。
在某些情况下,人们会在System.Runtime.InteropServices.COMException (0x80131701) 上收到错误代码。

这可能是由于我的计算机上存在的 docker 映像中缺少某些证书造成的吗?

【问题讨论】:

  • 搜索我找到的答案someone else also having problems with signing inside Docker。但是错误消息不同。
  • 我已经通过在我的机器上运行相同的 docker 容器进行了尝试,它按预期工作。
  • 我已经用C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\sn.exe 替换了本地sn.exe,现在sn.exe 部分工作。 signtool.exe 仍然没有检测到任何证书。
  • 我尝试在 Gitlab CI Runner 机器上安装来自 myCert.p12 证书链的证书...但没有任何帮助。
  • 似乎签名仅适用于 提供的安装了 Windows 的机器。 Gitlab Runner 机器是普通的 Windows Server。而能够执行签名的开发人员机器是使用来自 的专有 Windows 安装程序安装的。

标签: windows docker certificate signtool sn.exe


【解决方案1】:

为了修复 sn comamnd,我不得不将本地 sn.exe 文件替换为 C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\sn.exe

> C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\sn.exe -R myfile.exe myKey.snk

要修复signtool命令,需要在docker容器中导入证书:

> Set-Content myCert.pfx -Encoding Byte -Value ([System.Convert]::FromBase64String(myCert.p12))
> Import-PfxCertificate -FilePath myCert.pfx -Password (ConvertTo-SecureString -String myPassword -AsPlainText -Force) -CertStoreLocation Cert:\LocalMachine\Root
> $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
> $cert.Import(myCert.pfx, myPassword, 'DefaultKeySet')
> Set-AuthenticodeSignature -Cert myCert.pfx -TimeStampServer http://sha256timestamp.ws.symantec.com/sha256/timestamp -FilePath myFile.exe -HashAlgorithm SHA256

【讨论】:

    猜你喜欢
    • 2021-05-21
    • 2018-06-09
    • 2020-08-27
    • 2021-07-19
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    相关资源
    最近更新 更多