使用 Powershell 检索 x509 证书 'Description' 属性

Retrieve x509 Certificate 'Description' property using Powershell

我正在尝试撤回 属性 证书的描述 Windows。它不是标准的 x509 证书 属性.

我找到的唯一参考是使用 capicom (How can I access Certificate ExtendedProperties using powershell?),它现在不受支持,无论如何也不会帮助我,因为我将 运行 这个远程。

有谁知道访问此 属性 的其他方法吗?

谢谢

好吧,在发帖时,这两条评论都不正确或不相关。说明不是 X.509 证书对象的一部分,它是特定于供应商的(在当前情况下为 Microsoft)附加 属性。 属性 通过证书存储附加并且不存在于它之外。

PowerShell 和 .NET 均未提供从证书中读取存储附加属性的本机方式(尽管可以使用友好名称等内容)。相反,您需要通过 p/invoke:

调用 CertGetCertificateContextProperty 非托管函数
$Cert = gi Cert:\CurrentUser\My0F2809B505D9B32F167F6E71001B429CE801B8
$signature = @"
[DllImport("Crypt32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool CertGetCertificateContextProperty(
    IntPtr pCertContext,
    uint dwPropId,
    Byte[] pvData,
    ref uint pcbData
);
"@
Add-Type -MemberDefinition $signature -Namespace PKI -Name Crypt32
$pcbData = 0
# if the function returns False, then description is not specified.
$CERT_DESCRIPTION_PROP_ID = 13
if ([PKI.Crypt32]::CertGetCertificateContextProperty($Cert.Handle,$CERT_DESCRIPTION_PROP_ID,$null,[ref]$pcbData)) {
    # allocate a buffer to store property value
    $pvData = New-Object byte[] -ArgumentList $pcbData
    # call the function again to write actual data into allocated buffer
    [void][PKI.Crypt32]::CertGetCertificateContextProperty($Cert.Handle,$CERT_DESCRIPTION_PROP_ID,$pvData,[ref]$pcbData)
    # Description is null-terminated unicode string
    $description = [Text.Encoding]::Unicode.GetString($pvData).TrimEnd()
}
Write-Host $description

将第一行更改为用于检索证书的行。证书对象必须存储在 $cert 变量中。