首页 > 解决方案 > Import-AzKeyVaultCertificate cmdlet 导致:密钥对处于指定状态的用户无效

问题描述

我正在尝试将 .pfx 证书导入 Azure 密钥库,但遇到了一些问题。

Import-AzKeyVaultCertificate -VaultName "SecHash03" -Name "CodeSigning" -FilePath "\path\to\my\cert.pfx"

结果是:

Import-AzKeyVaultCertificate : Key not valid for use in specified state.
At line:1 char:1
+ Import-AzKeyVaultCertificate -VaultName SecHash03 -Name " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Import-AzKeyVaultCertificate], CryptographicException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.KeyVault.ImportAzureKeyVaultCertificate

我正在使用 certreq 从与 CA 位于同一域的机器中向企业 CA 请求此证书。导入证书不需要密码。然后计划将该证书上传到上述 Azure 密钥库。

我尝试使用 Azure 门户导入此证书,效果很好;导入和使用都很好。因此,正如另一个类似的 Stackoverflow 答案( Importing certificate to Azure Key Vault: Key not valid for use in specified state)中所建议的那样,这不是角色的问题。

请指教!

标签: azureimportcertificateazure-powershellazure-keyvault

解决方案


据我所知,当您将预先存在的 .pfx 文件证书导入 Azure 密钥保管库时,您需要提供用于保护证书的密码,因为您需要在私钥中导出证书并包含所有证书如果可能,在证书路径中。例如,

# Export the cert to a PFX with password
$password = ConvertTo-SecureString "Password!" -AsPlainText -Force
Export-PfxCertificate -Cert "cert:\CurrentUser\My\$($cert.Thumbprint)" -FilePath C:\temp\cert2.pfx -Password $password

# Upload to Key Vault
Import-AzureKeyVaultCertificate -VaultName noel-temp -Name cert2 -FilePath C:\temp\cert2.pfx -Password $password

或者,

如果您使用受支持的 CA,您甚至可以将 Key Vault 配置为代表您注册证书。不泄露密钥!为简单起见,这些示例中的策略将设置为从 Key Vault 生成自签名证书。

# Have Key Vault create the certificate with a simple policy
$policy = New-AzureKeyVaultCertificatePolicy -SubjectName "CN=mycluster.southcentralus.cloudapp.azure.com" -IssuerName Self -ValidityInMonths 12
Add-AzureKeyVaultCertificate -VaultName noel-temp -Name cert1 -CertificatePolicy $policy

# Download the secret (private key information) associated with the cert
$secret = Get-AzureKeyVaultSecret -VaultName noel-temp -Name cert1
$secretBytes = [System.Convert]::FromBase64String($secret.SecretValueText)
[System.IO.File]::WriteAllBytes("C:\temp\cert1.pfx", $secretBytes)

# Import the certificate to CurrentUser\My
Import-PfxCertificate -FilePath C:\temp\cert1.pfx -CertStoreLocation cert:\CurrentUser\My -Exportable

您可以从这两个链接中获得更多详细信息:

将证书导入 Key Vault

通过 Azure Key Vault 管理证书


推荐阅读