stunnel
クライアントモードのように機能するC#でSSLトンネルを構築しようとしています。つまり、暗号化されていない接続を暗号化された接続に転送します。
なぜこれが機能しないのですか?
using System;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Collections;
using System.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Text;
using System.IO;
namespace Teststunnel
{
public class SslWrapper
{
public SslWrapper(IPEndPoint remote, int port)
{
listener = new TcpListener (port);
Console.Error.WriteLine ("Established listener");
listenport = port;
server = remote;
listener.Start ();
new Thread (mainEventLoop).Start();
}
private TcpListener listener;
private int listenport;
private IPEndPoint server;
private void mainEventLoop()
{
restart:
Console.Error.WriteLine ("Waiting for connections...");
TcpClient a = listener.AcceptTcpClient ();
Console.Error.WriteLine ("Connection received!");
tunnel (a);
goto restart;
}
private void tunnel(TcpClient nconn)
{
SslStream sstrm = new SslStream (
nconn.GetStream (),
false);
Console.Error.WriteLine (server.ToString());
try
{
sstrm.AuthenticateAsClient (server.ToString());
}
catch (AuthenticationException e)
{
Console.WriteLine("Exception: {0}", e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine ("Authentication failed - closing the connection.");
nconn.Close();
return;
}
}
}
}
メイン関数では、 を使用してこれを呼び出していnew SslWrapper (new IPEndPoint(IPAddress.Parse("198.23.240.183"), 443), 9999);
ます。ただし、接続しようとすると、アプリケーションが次のようにクラッシュします。
未処理の例外: System.IO.IOException: 認証または復号化に失敗しました。---> Mono.Security.Protocol.Tls.TlsException: 認証または復号化に失敗しました。Mono.Security.Protocol.Tls.RecordProtocol.ReadRecordBuffer (Int32 contentType、System.IO.Stream レコード) [0x00000] で:0 Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00000] で: 0
はい、モノを使用しています。ただし、を使用してすべての正しい SSL ルート証明書をインポートしたことは確かですmozroots
。私は何を間違っていますか?これは、Microsoft .NET でも再現できます。