KeyVault生成带有可导出私钥的证书

Ale*_*ith 13 powershell x509 azure-powershell private-key azure-keyvault

我正在尝试使用"Self"发行者 KeyVault中创建自签名证书.

$policy = New-AzureKeyVaultCertificatePolicy -SubjectName "CN=$($certificateName)" -IssuerName "Self" -ValidityInMonths 12 

$policy.Exportable = $true

Add-AzureKeyVaultCertificate -VaultName $vaultName -Name $certificateName -CertificatePolicy $policy
Run Code Online (Sandbox Code Playgroud)

但是,当获得证书时,它似乎没有私钥.

直接在KeyVault中创建证书似乎没有在线覆盖,在深入研究PowerShell cmdlet的其余API文档和源代码之后,我很难过.

我希望这是我错过的一些简单的事情,因为我希望避免在本地创建证书.

Adr*_*ano 29

如果您想要检索证书及其私钥,则可以通过以下方式将其导出到磁盘上的PFX文件(使用空密码):

$vaultName = "my-vault-name"
$certificateName = "my-cert-name"
$pfxPath = [Environment]::GetFolderPath("Desktop") + "\$certificateName.pfx"

$pfxSecret = Get-AzureKeyVaultSecret -VaultName $vaultName -Name $certificateName
$pfxUnprotectedBytes = [Convert]::FromBase64String($pfxSecret.SecretValueText)
[IO.File]::WriteAllBytes($pfxPath, $pfxUnprotectedBytes)
Run Code Online (Sandbox Code Playgroud)

如果您想在内存中查看私钥本身而不写入磁盘,请尝试:

$vaultName = "my-vault-name"
$certificateName = "my-cert-name"
$pfxPath = [Environment]::GetFolderPath("Desktop") + "\$certificateName.pfx"

$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)
$pfx.PrivateKey.ExportParameters($true)
Run Code Online (Sandbox Code Playgroud)

除了指数和模数之外,它还将显示私有参数.

如果您想使用自己的密码保护磁盘上的PFX文件(根据此博客文章中的"检索pfx文件并添加密码"说明),请尝试:

$vaultName = "my-vault-name"
$certificateName = "my-cert-name"
$pfxPath = [Environment]::GetFolderPath("Desktop") + "\$certificateName.pfx"
$password = "my-password"

$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)
Run Code Online (Sandbox Code Playgroud)

如此此处的REST API文档中所述,Azure Key Vault(AKV)通过三个相互关联的资源表示给定的X.509证书:AKV证书,AKV密钥和AKV秘密.所有这三个将共享相同的名称和相同的版本-验证这一点,检查Id,KeyId以及SecretId从响应特性Get-AzureKeyVaultCertificate.

这三种资源中的每一种都为查看给定的X.509证书提供了不同的视角:

  • AKV证书提供X.509证书的公钥和证书元数据.它包含公钥的模数和指数(ne),以及其他证书元数据(指纹,有效期,主题名称等).在PowerShell中,您可以通过以下方式获取此信息:
(Get-AzureKeyVaultCertificate -VaultName $vaultName -Name $certificateName).Certificate
Run Code Online (Sandbox Code Playgroud)
  • AKV密钥提供X.509证书的私钥.如果相应的证书被标记为不可导出,则执行加密操作(如签名)非常有用.在PowerShell中,您只能通过以下方式获取此私钥的公共部分:
(Get-AzureKeyVaultKey -VaultName $vaultName -Name $certificateName).Key
Run Code Online (Sandbox Code Playgroud)
  • AKV-secret提供了一种导出完整X.509证书的方法,包括其私钥(如果其策略允许私钥导出).如上所示,可以在PowerShell中通过以下方式获取当前的base64编码证书:
(Get-AzureKeyVaultSecret -VaultName $vaultName -Name $certificateName).SecretValueText
Run Code Online (Sandbox Code Playgroud)

  • 非常彻底的回应,谢谢阿德里亚诺!我已经开始意识到我需要得到秘密,因为powershell API并不是最简单的理解 - .Certificate是一个陷阱:-)这有助于解释事情! (4认同)