1 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個贊
即使 PFX 包含整個鏈,使用單證書構(gòu)造函數(shù)也會使其僅加載具有私鑰的證書,其余的都會被丟棄。
即使使用 load-the-PFX-as-a-collection 方法,SslStream 也僅使用集合來查找可接受的服務(wù)器證書(具有私鑰和正確的 EKU 值),然后忽略其余部分。
僅當(dāng) X509Chain 通過系統(tǒng)環(huán)境上下文找到中間證書時才會發(fā)送中間證書。如果您的證書不是公共信任的,那么您的方案的最佳答案是將中間證書(以及可選的根證書)添加到 CurrentUser\CA ( )X509StoreName.CertificateAuthority證書存儲中?!癈A”存儲不提供信任,它只是系統(tǒng)見過的所有中間發(fā)行者 CA 的一個抓包,系統(tǒng)在構(gòu)建新鏈時將其用作緩存。
您可以在啟動時以編程方式執(zhí)行此操作
X509Certificate2 serverCertificate = null;
using (X509Store store = new X509Store(StoreName.CertificateAuthority, StoreLocation.CurrentUser))
{
store.Open(OpenFlags.ReadWrite);
X509Certificate2Collection coll = new X509Certificate2Collection();
coll.Import("certificate.pfx");
foreach (X509Certificate2 cert in coll)
{
if (cert.HasPrivateKey)
{
// Maybe apply more complex logic if you really expect multiple private-key certs.
if (serverCertificate == null)
{
serverCertificate = cert;
}
else
{
cert.Dispose();
}
}
else
{
// This handles duplicates (as long as no custom properties have been applied using MMC)
store.Add(cert);
cert.Dispose();
}
}
}
// tcpListener, et al.
其他選項(xiàng):將整個集合輸入 X509Chain.ChainPolicy.ExtraStore,在 serverCert 上調(diào)用 X509Chain.Build,僅在第一個證書之后添加證書(也可以不添加最后一個證書)...僅取決于預(yù)計需要多少額外內(nèi)容位于 PFX 中。
- 1 回答
- 0 關(guān)注
- 131 瀏覽
添加回答
舉報