亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 SslStream 時驗證自簽名證書鏈

使用 SslStream 時驗證自簽名證書鏈

C#
慕尼黑8549860 2023-09-16 17:50:45
我有一個鏈.pem-----BEGIN CERTIFICATE-----// My server cert signed by intemediate CA-----END CERTIFICATE----------BEGIN CERTIFICATE-----// My intermediate cert signed by root CA-----END CERTIFICATE----------BEGIN CERTIFICATE-----// My self signed root cert-----END CERTIFICATE-----以及server.key.pem-----BEGIN RSA PRIVATE KEY-----// Private key for server cert-----END RSA PRIVATE KEY-----從那里,我生成一個 pfx 文件 - 其中包含服務器證書及其私鑰以及鏈的其余部分。openssl pkcs12 -export -out certificate.pfx -inkey server.key.pem -in chain.pem我將導出密碼留空接下來我用 SslStream 托管一個 TcpListenernamespace fun_with_ssl{    internal class Program    {        public static int Main(string[] args)        {            var serverCertificate = new X509Certificate2("certificate.pfx");            var listener = new TcpListener(IPAddress.Any, 1443);            listener.Start();            while (true)            {                using (var client = listener.AcceptTcpClient())                using (var sslStream = new SslStream(client.GetStream(), false))                {                    sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls12, false);                    //send/receive from the sslStream                }            }        }    }}但是當我嘗試從 openssl 檢查鏈時,它失敗了openssl s_client -connect 127.0.0.1:1443 -CAfile ca.cert.pemCONNECTED(00000005)depth=0 CN = SERVERverify error:num=20:unable to get local issuer certificateverify return:1depth=0 CN = SERVERverify error:num=21:unable to verify the first certificateverify return:1---Certificate chain 0 s:CN = SERVER   i:CN = Intermediate---Server certificate-----BEGIN CERTIFICATE-----// My Server certificate-----END CERTIFICATE-----subject=CN = SERVER我似乎沒有提供中間證書或根證書,以便它可以驗證鏈。我在這里缺少什么?在我的場景中,客戶端將擁有根證書公鑰。
查看完整描述

1 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

即使 PFX 包含整個鏈,使用單證書構造函數也會使其僅加載具有私鑰的證書,其余的都會被丟棄。

即使使用 load-the-PFX-as-a-collection 方法,SslStream 也僅使用集合來查找可接受的服務器證書(具有私鑰和正確的 EKU 值),然后忽略其余部分。

僅當 X509Chain 通過系統環境上下文找到中間證書時才會發送中間證書。如果您的證書不是公共信任的,那么您的方案的最佳答案是將中間證書(以及可選的根證書)添加到 CurrentUser\CA ( )X509StoreName.CertificateAuthority證書存儲中?!癈A”存儲不提供信任,它只是系統見過的所有中間發行者 CA 的一個抓包,系統在構建新鏈時將其用作緩存。


您可以在啟動時以編程方式執行此操作


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.

其他選項:將整個集合輸入 X509Chain.ChainPolicy.ExtraStore,在 serverCert 上調用 X509Chain.Build,僅在第一個證書之后添加證書(也可以不添加最后一個證書)...僅取決于預計需要多少額外內容位于 PFX 中。


查看完整回答
反對 回復 2023-09-16
  • 1 回答
  • 0 關注
  • 135 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號