2011-07-04 25 views
2

暗号化された接続が必要なアプリケーションを作成していますが、問題が発生しています。私は同じコンピュータからクライアントとサーバーの両方を実行しています。 SSLを使用しないで接続すると(TCPClientとTCPListener経由ですべて正常に動作しますが、SSLStream classを使用するとSocketException 0x80004005 "Connection Abused Refused"というメッセージが表示され続けます)問題C#SSL接続が積極的に拒否されました

クライアント:。

private void okButton_Click(object sender, RoutedEventArgs e) 
    { 

     string serverCertName = COMPUTER_NAME; 
     string machineName = "127.0.0.1"; 

     SslStream stream = runClient(machineName, serverCertName, int.Parse(clientPort.Text)); 

     if (stream.CanWrite) 
     { 
      MessageBox.Show("We can write to the stream. We have a connection!"); 
     } 
     splitFile(this.sourceName); 
    } 

    public static bool ValidateServerCertificate(
     object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
    { 
     if (sslPolicyErrors == SslPolicyErrors.None) 
     { 
      return true; 
     } 
     //disallow communication with unauthenticated servers 
     return false; 
    } 

    public static SslStream runClient(string machineName, string serverName, int port) 
    { 
     //create client socket, machineName is the host running the server 
     //THIS IS WHERE EVERYTHING FAILS 
     TcpClient client = new TcpClient(machineName, port); 
     SslStream sslStream = new SslStream(client.GetStream(), 
      false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); 

     //server should match that is on that server cert 
     try 
     { 
      sslStream.AuthenticateAsClient(serverName); 
     } 
     catch (AuthenticationException e) 
     { 
      if (e.InnerException != null) 
      { 

      } 
      client.Close(); 
      return null; 
     } 

     //encode message as byte array for testing, etc 
     return sslStream; 
    } 

サーバー:

static X509Certificate serverCertificate = null; 
    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     IPAddress ip = IPAddress.Parse("127.0.0.1"); 
     int port = 13000; 
     string certificate = COMPUTER_NAME; 

     runServer(ip, port, certificate); 
    } 

    /// Start the ssl tcp listener 
    public static void runServer(IPAddress ip, int port, string certificate) 
    { 
     serverCertificate = X509Certificate.CreateFromCertFile(certificate); 
     TcpListener listener = new TcpListener(IPAddress.Any, port); 
     listener.Start(); 

     while (true) 
     { 
      TcpClient client = listener.AcceptTcpClient(); 
      processClient(client); 
     } 
    } 

    //executes if a client has connected. Create the SslStream using the connected client's stream 
    static void processClient(TcpClient client) 
    { 
     SslStream sslStream = new SslStream(client.GetStream(), false); 

     //authenticate the server, but doesn't require the client to authenticate 
     try 
     { 
      sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true); 
      //set timeouts to 5 seconds. Seems like a long time. If nothing happens after 5 sec it won't happen 
      sslStream.ReadTimeout = 5000; 
      sslStream.WriteTimeout = 5000; 
      string data = readMessage(sslStream); 
     } 
     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."); 
      sslStream.Close(); 
      client.Close(); 
      return; 
     } 
     finally 
     { 
      sslStream.Close(); 
      client.Close(); 
     } 
    } 

私はC#での一般的なTCP/IP接続への新ブランドですので、それは私が愚かな何か、間違っていた完全に可能ですが、または過度に複雑な私はfouを持っていた(スタックオーバフローを介して)SslStreamが行く方法でしたが、私はそれを動作させるように見えません。誰にもアイデアはありますか?私は本当に助けに感謝します。

ありがとうございます!

+0

クライアントと呼ばれるコマンドラインは何か分かりますか? C:\> MyServer C:\ myServer.cerのサーバを呼び出すとしましょう。 – 1myb

答えて

1

あなたはクライアントとして認証しようとしていますが、サーバーがサーバーとして認証されていないと思います。試して、これが同時に起こっていることを確認し、それが動作するはずです。

+1

詳細を教えてください。私も同じエラーが発生している – 1myb

関連する問題