SSL认证适用于本地主机,但不适用于计算机名称或IP如何解决?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (325)

我们有一个Web应用程序在服务器上运行,它通过XDomainRequest发布http请求(因为IE9)。

有很多客户端计算机的控制台应用程序通过套接字侦听器侦听端口。客户使用IE9浏览器打开Web应用程序,当他们点击一个链接时,网页发送这样的请求:

https:// localhost:portNumber / applicationName / doSomething ”“ https:// computerName:portNumber / applicationName / doSomething ”“ https:// ipAddress:portNumber / applicationName / doSomething

第二个和第三个请求用于控制其他用户计算机的应用程序。

问题是,如果请求与本地主机一起提供,控制台应用程序在读取传入数据和发回响应方面没有问题。但是,如果请求带有计算机名称或IP地址,则浏览器会显示认证警告,并希望用户单击“继续访问此网站(不推荐)”链接。

我们认为通过代码创建三个不同的证书。但是,即使使用sslstream和其中的三个,我们也不能决定选择真正的认证,因为我们首先进行身份验证,然后接收数据。所以当我们捕捉到传入的请求时,认证必须已经完成。

另一种方法是强制套接字侦听器或sslstream将所有这三个请求作为localhost运行。因此,对于每一个身份验证将作为本地主机。但我找不到一个真正的方法。

这是代码。我给出了代码,因为可能是SslStream有一些错误的用法。

using System;
using System.Net.Sockets;
using System.Net;
using System.Configuration;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Forms;
using System.IO;
using System.Net.Security;
using System.Security.Authentication;
using System.Threading;
using System.Text;

namespace StackOverFlowProject
{
    class StackOverFlowSample
    {
        private static ManualResetEvent _manualResetEvent = new ManualResetEvent(false);
        private static X509Certificate _cert = null;

        static void Main(string[] args)
        {
            StackOverFlowSample stackOverFlowSample = new StackOverFlowSample();
            stackOverFlowSample.StartListening();
        }

        private void StartListening()
        {
            GetCertificate();

            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 1234);

            if (localEndPoint != null)
            {
                Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                if (listener != null)
                {
                    listener.Bind(localEndPoint);
                    listener.Listen(10);

                    Console.WriteLine("Socket listener is running. Waiting for requests...");

                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
                }
            }
        }

        private static void GetCertificate()
        {
            byte[] pfxData = File.ReadAllBytes(Application.StartupPath + @"\" + "localhost.pfx");

            _cert = new X509Certificate2(pfxData, "password", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
        }


        private void AcceptCallback(IAsyncResult result)
        {
            Socket listener = null;
            Socket handler = null;
            StateObject state = null;
            SslStream sslStream = null;

            _manualResetEvent.Set();

            listener = (Socket)result.AsyncState;

            handler = listener.EndAccept(result);

            state = new StateObject();

            if (handler.RemoteEndPoint != null)
            {
                state.clientIP = ((IPEndPoint)handler.RemoteEndPoint).Address.ToString();
            }

            sslStream = new SslStream(new NetworkStream(handler, true));
            sslStream.AuthenticateAsServer(_cert, false, SslProtocols.Tls, true);

            sslStream.ReadTimeout = 100000;
            sslStream.WriteTimeout = 100000;

            state.workStream = sslStream;

            if (state.workStream.IsAuthenticated)
            {
                state.workStream.BeginRead(state.buffer, 0, StateObject.BufferSize, ReceiveCallback, state);
            }

            listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
        }

        private void ReceiveCallback(IAsyncResult result)
        {
            StateObject stateObject = null;
            SslStream sslStreamReader = null;

            byte[] byteData = null;

            stateObject = (StateObject)result.AsyncState;
            sslStreamReader = stateObject.workStream;

            int byteCount = sslStreamReader.EndRead(result);

            Decoder decoder = Encoding.UTF8.GetDecoder();
            char[] chars = new char[decoder.GetCharCount(stateObject.buffer, 0, byteCount)];
            decoder.GetChars(stateObject.buffer, 0, byteCount, chars, 0);
            stateObject.sb.Append(chars);

            if (byteCount > 0)
            {
                stateObject.totalReceivedBytes += byteCount;

                string[] lines = stateObject.sb.ToString().Split('\n');

                if (lines[lines.Length - 1] != "<EOF>")
                {
                    // We didn't receive all data. Continue reading...
                    sslStreamReader.BeginRead(stateObject.buffer, 0, stateObject.buffer.Length, new AsyncCallback(ReceiveCallback), stateObject);
                }
                else
                {
                    Console.WriteLine("We received all data. Sending response...");

                    byteData = Encoding.UTF8.GetBytes("Hello! I received your request!");

                    string httpHeaders = "HTTP/1.1" + "\r\n"
                                    + "Cache-Control: no-cache" + "\r\n"
                                    + "Access-Control-Allow-Origin: *" + "\r\n"
                                    + "\r\n";

                    byte[] byteHttpHeaders = Encoding.UTF8.GetBytes(httpHeaders);

                    byte[] concat = new byte[byteHttpHeaders.Length + byteData.Length];
                    Buffer.BlockCopy(byteHttpHeaders, 0, concat, 0, byteHttpHeaders.Length);
                    Buffer.BlockCopy(byteData, 0, concat, byteHttpHeaders.Length, byteData.Length);

                    stateObject.sslStreamReader = sslStreamReader;

                    sslStreamReader.BeginWrite(concat, 0, concat.Length, new AsyncCallback(SendCallback), stateObject);
                }
            }
        }

        private void SendCallback(IAsyncResult ar)
        {
            SslStream sslStreamSender = null;

            StateObject stateObject = (StateObject)ar.AsyncState;

            sslStreamSender = stateObject.sslStreamReader;
            sslStreamSender.EndWrite(ar);

            Console.WriteLine(stateObject.totalReceivedBytes.ToString() + " bytes sent to " + stateObject.clientIP + " address");

            sslStreamSender.Close();
            sslStreamSender.Dispose();
        }

    }

    public class StateObject
    {
        public SslStream workStream = null;

        public SslStream sslStreamReader = null;

        public const int BufferSize = 1024;
        public byte[] buffer = new byte[BufferSize];
        public StringBuilder sb = new StringBuilder();

        public string clientIP = "";

        public int totalReceivedBytes = 0;
    }
}
提问于
用户回答回答于

遇到的证书警告实际上是名称不匹配错误,表明SSL证书中的公用名称(域名)与用于访问网站/服务器的URL /地址不匹配。

https://www.sslshopper.com/ssl-certificate-name-mismatch-error.html

在使用场景中,可能希望从本地主机和IP地址过渡,转而使用利用计算机名称的简单域模型。(例如computerName.someDomain.com

然后,可以获得*.someDomain.com可用于验证进程间通信的通配符证书(例如)。

https://www.sslshopper.com/best-ssl-wildcard-certificate.html

用户回答回答于

你的安全人员是对的。你试图做到这一点的方式不适用于SSL。

如果有证书,它将被设置为验证一个CN。所以,一个简单的例子。Google有一个证书。它对https://*.google.com进行身份验证。这意味着任何对google.com的请求都会具有有效的证书。你的浏览器很开心。

现在打开命令提示符,ping google.com。抓住IP地址(在我的情况下它是216.58.210.14)。输入https://216.58.210.14。浏览器抱怨网站不安全等。原因是服务器可能与之前请求服务的服务器相同,但根据证书,获得该服务的方式无效,因为CN不是谷歌。 com,但是是一个IP地址。

所以如果你有一个需要连接的服务(例如)127.0.0.1,10.92.1.4和myserver.com,你将需要一个对每种情况都有效的证书。

扫码关注云+社区

领取腾讯云代金券