簡體   English   中英

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

[英]Verifying the chain of a self-signed certificate when using SslStream

我有一條鐵鏈。

-----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托管一個TcpListener

namespace 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.pem

CONNECTED(00000005)
depth=0 CN = SERVER
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 CN = SERVER
verify error:num=21:unable to verify the first certificate
verify return:1
---
Certificate chain
 0 s:CN = SERVER
   i:CN = Intermediate
---
Server certificate
-----BEGIN CERTIFICATE-----
// My Server certificate
-----END CERTIFICATE-----
subject=CN = SERVER

issuer=CN = Intermediate

---
No client certificate CA names sent
Client Certificate Types: RSA sign, DSA sign, ECDSA sign
Requested Signature Algorithms: RSA+SHA256:RSA+SHA384:RSA+SHA1:ECDSA+SHA256:ECDSA+SHA384:ECDSA+SHA1:DSA+SHA1:RSA+SHA512:ECDSA+SHA512
Shared Requested Signature Algorithms: RSA+SHA256:RSA+SHA384:RSA+SHA1:ECDSA+SHA256:ECDSA+SHA384:ECDSA+SHA1:DSA+SHA1:RSA+SHA512:ECDSA+SHA512
Peer signing digest: SHA256
Peer signature type: RSA
Server Temp Key: ECDH, P-384, 384 bits
---
SSL handshake has read 1439 bytes and written 481 bytes
Verification error: unable to verify the first certificate
---
New, TLSv1.2, Cipher is ECDHE-RSA-AES256-GCM-SHA384
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : ECDHE-RSA-AES256-GCM-SHA384
    Session-ID: E82C0000B86186D0051CFE6290C12F0D62C4D376B7E40437029B8B85687C4B18
    Session-ID-ctx:
    Master-Key: 13681EAE940F241726072A4586A96A9FEEEF29B8309B9122FA2F07AC7C9F949128CB66D0F9C430E1D2480E61E287C578
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    Start Time: 1566533377
    Timeout   : 7200 (sec)
    Verify return code: 21 (unable to verify the first certificate)
    Extended master secret: yes
---
140266287337920:error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown:../ssl/record/rec_layer_s3.c:1528:SSL alert number 46

我似乎並沒有提出中間證書或根證書,以便它可以驗證鏈。 我在這里想念什么?

在我的情況下,客戶端將具有根證書公用密鑰。

  • 即使PFX包含整個鏈,使用單證書構造函數也使其僅加載具有私鑰的證書,其余的將被丟棄。
  • 即使使用了將PFX作為集合加載的方法,SslStream也僅使用集合來查找可接受的服務器證書(具有私鑰和正確的EKU值),然后忽略其余的證書。

僅當X509Chain通過系統環境上下文找到中間證書時,才發送中間證書。 如果您的證書不是公共信任的,則針對您的方案的最佳答案是將中間(以及根)(可選)添加到CurrentUser \\ CA( X509StoreName.CertificateAuthority )證書存儲中。 “ CA”存儲區不提供信任,它只是系統看到的所有中間發行人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,調用X509Chain.Build on serverCert,僅在第一個證書(可能不是最后一個證書)之后添加證書...僅取決於預期有多少額外的東西在PFX中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM