【问题标题】:Retrieve x509 Certificate 'Description' property using Powershell使用 Powershell 检索 x509 证书“描述”属性
【发布时间】:2018-04-26 02:27:19
【问题描述】:

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

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

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

谢谢

【问题讨论】:

  • 它与证书对象一起存储在注册表中(例如,LocalMachine-cert HKLM:\SOFTWARE\Microsoft\SystemCertificates\MY\Certificates\)。如果您在那里搜索您的证书,您将在二进制 blob 属性中看到它。不知道实际呈现的是哪种对象类型。
  • 将记录的C# example here转换为PowerShell。

标签: powershell ssl-certificate x509certificate


【解决方案1】:

嗯,在发布的那一刻,任何评论都不正确或相关。描述不是 X.509 证书对象的一部分,它是特定于供应商(Microsoft,在当前情况下)的附加属性。该属性是通过证书存储附加的,并且不存在于它之外。

PowerShell 和 .NET 都没有提供从证书中读取存储附加属性的本地方式(尽管可以使用友好名称等某些内容)。相反,您需要通过 p/invoke 调用 CertGetCertificateContextProperty 非托管函数:

$Cert = gi Cert:\CurrentUser\My\510F2809B505D9B32F167F6E71001B429CE801B8
$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 变量中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 2020-01-27
    相关资源
    最近更新 更多