我正在通过 Powershell 在 Windows 2012 服务器上安装客户端证书。 安装客户端证书需要两个步骤:
- 在个人商店(“我的”)上安装证书。
- 在 Trusted 中安装该证书的根证书 根证书颁发机构商店。
第 1 步相当简单。 但是,第 2 步很棘手。首先,我不知道证书链的长度。手动执行时,您需要导出链中的每个证书,直到到达根(您只能导出链的第一个元素)。然后,您将根证书安装在受信任的商店中。
所以,我的问题是:如何获取证书的根证书? 我的想法是获取证书链并以某种方式对其进行处理,直到获得根证书。关于如何做到这一点有什么想法吗?
最佳答案
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
}
}
关于powershell - 使用 powershell 导出根证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32631343/