【问题标题】:Retrieve KeySpec Value from Certificate Using PowerShell使用 PowerShell 从证书中检索 KeySpec 值
【发布时间】:2021-11-29 06:06:04
【问题描述】:

我正在尝试验证机器存储中的证书是否将 KeySpec 设置为 AT_KEYEXCHANGE。使用certutil.exe 确实提供了此信息,但需要进行字符串解析。我宁愿避免字符串解析,以避免对 certutil.exe 输出的假设,我不知道在不同版本的 Windows 中总是正确的。

我查看了System.Security.Cryptography.X509Certificates.X509Certificate2System.Security.Cryptography.X509Certificates.RSACertificateExtensions 的属性和方法。

如何从证书存储中的证书中检索 KeySpec?

【问题讨论】:

  • 除了没有得到广泛支持外,我没有找到很多关于此的内容。我发现最好的是命名空间 System.Security.Permissions(来自同名的 dll)具有 KeyContainerPermissionAttribute.KeySpec 属性。 docs.microsoft.com/en-us/dotnet/api/…
  • 你试过“Get-Certificate”吗?
  • Get-Certificate 用于“向注册服务器提交证书请求并安装响应或检索先前提交的请求的证书”。我正在尝试从机器存储中已经存在的证书中获取 KeySpec 属性。

标签: powershell certificate


【解决方案1】:

herehere 的帮助下,我找到了KeySpec。这 CspKeyContainerInfo 类包含一个名为 KeyNumber 的属性,这就是 certutil 所指的 KeySpec。

我找到了两种方法。一种仅适用于 PowerShell 5,另一种适用于 PowerShell 5 和 7。

仅限 PowerShell 5

$Cert = (Get-ChildItem -Path Cert:\LocalMachine\My)[1]
$Cert.PrivateKey.CspKeyContainerInfo.KeyNumber

PowerShell 5 和 7

$Cert = (Get-ChildItem -Path Cert:\LocalMachine\My)[1]
$PrivateKey = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($Cert)
$CngProvider = [System.Security.Cryptography.CngProvider]::new($PrivateKey.Key.Provider)
$CngKey = [System.Security.Cryptography.CngKey]::Open($PrivateKey.Key.KeyName,  $CngProvider, [System.Security.Cryptography.CngKeyOpenOptions]::MachineKey)
$CspParameters = [System.Security.Cryptography.CspParameters]::New(1, $CngKey.Provider, $CngKey.KeyName)
$CspParameters.Flags = [System.Security.Cryptography.CspProviderFlags]::UseMachineKeyStore
$CspKeyContainerInfo = [System.Security.Cryptography.CspKeyContainerInfo]::New($CspParameters)
$CspKeyContainerInfo.KeyNumber

【讨论】:

    【解决方案2】:

    我怀疑以下内容不是直接您正在寻找的内容,但也许它最终包含您正在寻找的信息;它基于将Get-ChildItem 应用于PowerShell 的Cert: 驱动器:

    Get-ChildItem Cert:\LocalMachine -Recurse | 
      Where-Object { -not $_.PSIsContainer -and $_.EnhancedKeyUsageList }
        Format-List @{ 
                      Name='KeyUsage'
                      Expression={ ($_.EnhancedKeyUsageList.FriendlyName) -join ', ' } 
                    },
                    Subject,
                    Thumbprint
    

    注意:Windows PowerShellPowerShell (Core) 7.1 之间的行为在默认输出格式和报告非空 @ 的证书数量方面都发生了变化987654325@ 属性值:Windows PowerShell 报告更多

    在 PowerShell (Core) 7.1 中,上面的结果类似于:

    KeyUsage   : Code Signing, Time Stamping, Encrypting File System
    Subject    : CN=UTN-USERFirst-Object, OU=http://www.usertrust.com, O=The USERTRUST Network, L=Salt Lake City, S=UT, C=US
    Thumbprint : 6E6D0A31B454AF8E8F06CFEB438351056204C28C
    
    KeyUsage   : Server Authentication, Client Authentication, , 
    Subject    : OU=www.verisign.com/CPS Incorp.by Ref. LIABILITY LTD.(c)97 VeriSign, OU=VeriSign International Server CA - Class 3, OU="VeriSign, Inc.", O=VeriSign Trust Network
    Thumbprint : 13E8AB4167D5830F9440093564AC0211C2D26E62
    
    
    KeyUsage   : Code Signing, Windows Hardware Driver Verification
    Subject    : CN=Microsoft Windows Hardware Compatibility, OU=Microsoft Corporation, OU=Microsoft Windows Hardware Compatibility Intermediate CA, OU=Copyright (c) 1997 Microsoft Corp.
    Thumbprint : 75F7C7CDC6900B145CF9242910EC037D423F369F
    
    KeyUsage   : Code Signing, Time Stamping, Encrypting File System
    Subject    : CN=UTN-USERFirst-Object, OU=http://www.usertrust.com, O=The USERTRUST Network, L=Salt Lake City, S=UT, C=US
    Thumbprint : 59E6BAC5EFE4C1A11B889146BD983F468C5103BA
    
    KeyUsage   : Server Authentication
    Subject    : CN=localhost
    Thumbprint : 81F6D78B7A53AE3D03264D178A2E0FEBC978C4D8
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多