【问题标题】:Nuke-build sign assemblies with Certificate from Azure Key Vault使用 Azure Key Vault 中的证书 Nuke 构建签名程序集
【发布时间】:2021-06-29 09:29:17
【问题描述】:

我正在尝试使用 Azure Key Vault 中的证书来签署我的程序集。

到目前为止,我已将证书从本地服务器复制到我的构建目录并使用以下方式签名:

    SignToolSettings settings = new SignToolSettings()
        .SetFileDigestAlgorithm("SHA256")
        .SetFile(CertFileNameAndPath)
        .SetFiles(fileNames)
        .SetPassword(password)
        .SetTimestampServerDigestAlgorithm("SHA256")
        .SetRfc3161TimestampServerUrl("http://timestamp.globalsign.com/tsa/r6advanced1");
    SignToolTasks.SignTool(settings);

我可以使用此 Nuke 设置从 Azure Key Vault 下载证书:

[KeyVaultSettings(
    BaseUrlParameterName = nameof(KeyVaultBaseUrl), 
    ClientIdParameterName = nameof(KeyVaultClientId),
    ClientSecretParameterName = nameof(KeyVaultClientSecret))] 
readonly KeyVaultSettings KeyVaultSettings;

[KeyVault] readonly KeyVault KeyVault;

[Parameter] readonly string KeyVaultBaseUrl;
[Parameter] readonly string KeyVaultClientId;
[Parameter] readonly string KeyVaultClientSecret;

[KeyVaultCertificate("MyCertificateIdentifier")] KeyVaultCertificate Certificate;

Certificate.Cer 现在包含 1276 字节的“某物”。

我已尝试将这 1276 个字节保存为我的证书.pfx,但这不能用作代码签名证书。

在 Powershell 中我可以这样做:

$vaultName = "MyStorage"
$certificateName = "MyCertificate"
$pfxPath = ".\$certificateName.pfx"
$password = "MyPassword"

Connect-AzureRmAccount
$pfxSecret = Get-AzureKeyVaultSecret -VaultName $vaultName -Name $certificateName
$pfxUnprotectedBytes = [Convert]::FromBase64String($pfxSecret.SecretValueText)
$pfx = New-Object Security.Cryptography.X509Certificates.X509Certificate2
$pfx.Import($pfxUnprotectedBytes, $null, [Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)
$pfxProtectedBytes = $pfx.Export([Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12, $password)
[IO.File]::WriteAllBytes($pfxPath, $pfxProtectedBytes)

所以我尝试对 Nuke 收到的证书做同样的事情(它看起来不像 base64 编码,所以我跳过了解码):

    var pfx = new System.Security.Cryptography.X509Certificates.X509Certificate2(Certificate.Cer, (string)null, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable);
    var pfxProtectedBytes = pfx.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Pkcs12, "MyPassword");
    File.WriteAllBytes(CertFileNameAndPath, pfxProtectedBytes);

但还是没有运气。

当然可以选择保存到磁盘文件,但如果 SignToolSettings 直接接受 KeyVaultCertificate 会更好。

【问题讨论】:

    标签: nuke-build


    【解决方案1】:

    我对此的解决方案确实是一种解决方法。

    而不是引用证书:

    [KeyVaultCertificate("MyCertificateIdentifier")] KeyVaultCertificate Certificate;
    

    我将其作为字符串引用:

    [KeyVaultSecret("MyCertificateIdentifier")] string CertificateBase64;
    

    这个字符串可以被 Base64 解码并保存到一个本地文件,我可以使用我的标准 SignTool。

    注意有一个专用的AzureSignTool,它直接在 Azure 上运行,所以你根本不需要下载证书。不过还没试过。

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 2019-12-19
      • 1970-01-01
      • 2018-09-11
      • 2020-07-10
      • 2020-01-27
      • 2019-10-16
      • 2020-08-11
      相关资源
      最近更新 更多