【问题标题】:Validate certificate chain with powershell使用 powershell 验证证书链
【发布时间】:2013-12-10 18:31:04
【问题描述】:

我正在尝试编写一个脚本来验证 PowerShell 中的证书链(链中的所有证书都未过期)并找到最接近过期的证书。我正在使用以下脚本来查找颁发者证书:

Get-ChildItem -Recurse -Path Cert: | Where-Object { $_.Subject -eq $Certificate.Issuer }

由于某些证书的某些原因,我获得了不止一个具有不同指纹的证书,它们具有相同的颁发者名称,我预计应该只有一个。

证书是否有任何其他属性可以唯一标识颁发者证书?也许还有其他方法可以验证证书链?

【问题讨论】:

    标签: powershell ssl certificate


    【解决方案1】:

    我需要使用私钥清点所有证书的到期日期。 下面的代码示例在 Powershell 3.0 下测试 Try/Catch 结构允许从没有私钥的证书中抑制令人讨厌的红色错误文本。

    Set-Strictmode -Version Latest
    $arrCerts = Get-Childitem CERT:\ -Recurse                   
    foreach ($objItem in $arrCerts) {
       Try   { $blnFound = ($objItem.HasPrivateKey -eq $True) } 
       Catch { $blnFound = $False }                             
       if ($blnFound) {                                         
           $arrSplit = $objItem.PSParentPath -split "::"        
           write-host 'Path        '$arrSplit[1]                
           write-host 'Subject     '$objItem.SubjectName.Name   
           write-host 'Expires     '$objItem.NotAfter           
           write-host 'Private Key '$objItem.HasPrivateKey      
           write-host
           }
       }                                                        
    

    【讨论】:

      【解决方案2】:

      查看测试证书:http://poshcode.org/1633

      为证书链和吊销测试指定的证书

      4.0 中包含一个 Test-Certificate cmdlet
      http://technet.microsoft.com/en-us/library/hh848639.aspx

      我在本地主机上运行它只是为了测试它,

      Get-childitem cert: -recurse | %{ write-host $_ ; Test-Certificate -cert $_ }
      

      当链中的证书过期时,它会给出一个很好的错误。

      警告:链状态: CERT_TRUST_IS_NOT_TIME_VALID 测试证书:验证时所需的证书不在其有效期内 当前系统时钟或签名文件中的时间戳。

      【讨论】:

      • 谢谢。使用 X509Chain 类我可以轻松检索证书链。
      猜你喜欢
      • 1970-01-01
      • 2021-07-22
      • 2017-04-16
      • 2017-08-15
      • 1970-01-01
      • 2022-08-19
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多