坚持使用mitm https代理中的AuthenticateAsServer方法

问题描述 投票:1回答:1

我正在尝试编写一个简单的https mitm代理,当我处理请求时会出现问题:

public async Task Run(NetworkStream client, NetworkStream host) {
        try {
            //getting the cert
            var certificate = new X509Certificate(@"[PATH_TO_CERT]", "[PASSWORD]");
            //creating client's Ssl Stream
            var clientStream = new SslStream(client, false);
            //there the program freezes
            clientStream.AuthenticateAsServer(certificate, false, SslProtocols.Default, false);

            //creating server's Ssl Stream
            var serverSslStream = new SslStream(host, false, SslValidationCallback, null);
            serverSslStream.AuthenticateAsClient("[HOSTNAME]");

            //...

        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
            throw;
        }

    }

发送客户端请求后,程序冻结此行

clientStream.AuthenticateAsServer(certificate, false, SslProtocols.Default, false);

并且它不会抛出任何异常。起初我认为问题出在客户端的流中,所以我试图将它的TcpClient作为方法参数传递,但没有任何改变。

我的自签名证书和.pfx文件的创建方式如下:

makecert -n CN=*.[HOSTNAME].com -ic MyCA.cer -iv MyCA.pvk -a sha1 -sky exchange -pe -sr currentuser -ss my SslServer.cer 
makecert.exe -pe -n "CN=*.[HOSTNAME].com" -a sha1 -sky exchange -eku 1.3.6.1.5.5.7.3.1 -ic CA.cer -iv CA.pvk -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12 -sv server.pvk server.cer

pvk2pfx -pvk MyCA.pvk -pi [PASSWORD] -spc MyCA.cer -pfx MyPFX.pfx -f

所以我认为问题出在这一行

var certificate = new X509Certificate(@"[path to the cert]", "[password]");

我将cer路径替换为pfx路径,我甚至下载了原始的crt文件new X509Certificate(@"[path to the original cert]");,但这些都没有用。

我不知道问题出在哪里,我试过不同的客户,结果是一样的。

我的Visual Studio版本是15.7.27703.2018,.Net是4.7.1。

任何可以帮助我的提示,建议或链接?

c# .net visual-studio sslstream
1个回答
0
投票

原来我需要等待使用它。

最终代码如下所示:

//getting the cert
var certificate = new X509Certificate2(@"[PATH_TO_CERT]", "[PASSWORD]");
//creating client's Ssl Stream
var clientStream = new SslStream(client, false);
await clientStream.AuthenticateAsServerAsync(certificate, false, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Ssl3, false);
© www.soinside.com 2019 - 2024. All rights reserved.