web-dev-qa-db-ja.com

KeyVaultは、エクスポート可能な秘密鍵で証明書を生成しました

「自己」発行者を使用して自己署名証明書in KeyVaultを作成しようとしています。

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

$policy.Exportable = $true

Add-AzureKeyVaultCertificate -VaultName $vaultName -Name $certificateName -CertificatePolicy $policy

ただし、証明書を取り戻すと、秘密鍵がないようです。

KeyVaultで直接証明書を作成することはオンラインではあまり取り上げられていないようです。Powershellコマンドレットの残りのAPIドキュメントとソースコードを調べた後、困惑しました。

ローカルで証明書を作成しないようにしたいので、これが見逃した単純なものであることを願っています。

14
Alex KeySmith

証明書と秘密鍵を取得する場合は、次の方法でディスク上の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)

ディスクに書き込まずに、メモリ内の秘密キー自体だけを表示したい場合は、次を試してください。

$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)

これは、指数と係数に加えてプライベートパラメータを表示します。

自分のパスワードでディスク上の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)

REST API docs here and here )で述べたように、Azure Key Vault(AKV)は3つの相互に関連する特定のX.509証明書を表しますリソース:AKV証明書、AKVキー、およびAKVシークレット。3つすべてが同じ名前と同じバージョンを共有します-これを確認するには、IdKeyId、およびGet-AzureKeyVaultCertificateからの応答のSecretIdプロパティ。

これら3つのリソースはそれぞれ、特定のX.509証明書を表示するための異なる視点を提供します。

  • AKV証明書は、X.509証明書の公開キーと証明書のメタデータを提供します。公開鍵の係数と指数(ne)、およびその他の証明書のメタデータ(拇印、有効期限、サブジェクト名など)が含まれています。 PowerShellでは、次の方法で取得できます。
(Get-AzureKeyVaultCertificate -VaultName $vaultName -Name $certificateName).Certificate
  • AKVキーは、X.509証明書の秘密キーを提供します。対応する証明書がエクスポート不可としてマークされている場合、署名などの暗号化操作を実行するのに役立ちます。 PowerShellでは、次の方法でのみ取得できますこの秘密鍵の公開部分
(Get-AzureKeyVaultKey -VaultName $vaultName -Name $certificateName).Key
  • AKVシークレットは、プライベートキーを含むX.509証明書全体をエクスポートするの方法を提供します(ポリシーでプライベートキーのエクスポートが許可されている場合)。上記のように、現在のbase64でエンコードされた証明書は、PowerShellで次のように取得できます。
(Get-AzureKeyVaultSecret -VaultName $vaultName -Name $certificateName).SecretValueText
34
Adriano