【问题标题】:Export root certificate using powershell使用 powershell 导出根证书
【发布时间】:2015-09-17 13:19:50
【问题描述】:

我正在通过 Powershell 在 Windows 2012 服务器上安装客户端证书。 安装客户端证书需要两个步骤:

  1. 在个人存储(“我的”)上安装证书。
  2. 在 Trusted 中安装该证书的根证书 根证书颁发机构存储。

第 1 步相当简单。 但是,第 2 步很棘手。首先,我不知道证书链的长度。手工操作时,需要去导出链中的每个证书,直到到达根(只能导出链的第一个元素)。然后,在 Trusted Store 中安装根证书。

那么,我的问题是:如何获得证书的根证书? 我的想法是获取证书链并以某种方式对其进行处理,直到您获得根证书。关于如何做到这一点的任何想法?

【问题讨论】:

  • 除非 Powershell 5.0 发生变化——证书处理在 Powershell 中有点麻烦——你必须求助于 .NET 代码来处理它。您需要查看 .NET 库的 System.Security.Cryptography.X509Certificates 位以获取有关从何处开始的提示。

标签: powershell ssl certificate windows-server-2012


【解决方案1】:

GodEater 的建议帮助了我,通过查看此页面 https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates(v=vs.110).aspx 我想出了如何做到这一点:-

如果您将 pkcs12 证书导入 System.Security.Cryptography.X509Certificates.X509Certificate2Collection

当您查看对象时,两个证书都在那里,因此只需遍历对象并将每个证书添加到正确的存储中即可:-

$fileName = "cert.p12";
$password = "Password"
$certRootStore = "localmachine";
$certStore = "Root";
$certStore2 = "My";
$X509Flags = "PersistKeySet,MachineKeySet";
$pfx = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection;
$pfx.Import($fileName, $Password, $X509Flags);
foreach ($cert in $pfx) {
    if ($cert.Subject -match "CN=Your Cert Auth Name") {
        $store = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store -ArgumentList $certStore,$certRootStore;
        $store.Open("MaxAllowed");$store.Add($cert);
        $store.Close | Out-Null
    }
    else {
        $store = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store -ArgumentList $certStore2,$certRootStore;
        $store.Open("MaxAllowed");
        $store.Add($cert);
        $store.Close | Out-Null
    }
}

【讨论】:

    猜你喜欢
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 2013-12-10
    • 2014-12-02
    相关资源
    最近更新 更多