2011-09-22 15 views
4

私はSSL接続をセットアップしたいと思いますが、実際にはSSLハンドシェイクルールとライフサイクルについてすべてを知りません。私は、コード、プログラムが最初に流れる「localCertSelection」メソッドに行く実行し、「ValidateServerCertificate」メソッドに行くとき、私は、コードLocalCertificateSelectionCallbackメソッドで 'remoteCertificate'パラメータが空であるのはなぜですか?

void main() 
{ 
TcpClient client = new TcpClient("192.168.1.160", 4113); 
SslStream sslStream = new SslStream(
       client.GetStream(), 
       false, 
       new RemoteCertificateValidationCallback(ValidateServerCertificate), 
       new LocalCertificateSelectionCallback(localCertSelection) 
       ); 
sslStream.AuthenticateAsClient(serverName); 
} 

public X509Certificate localCertSelection(object sender, string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate, string[] acceptableIssuers) 
     {// why here 'remoteCertificate' parameter is empty? 'acceptableIssuers' and 'localCertificates' too 
      string cert = "MIIEwjCCA6qgAwIBAgIBADANBgkqhkiG9w..."; 
      X509Certificate clientCert = new X509Certificate(System.Text.Encoding.ASCII.GetBytes(cert)); 
      return clientCert; 
     } 

public bool ValidateServerCertificate(
       object sender, 
       X509Certificate certificate, 
       X509Chain chain, 
       SslPolicyErrors sslPolicyErrors) 
     { 
// 'certificate' has data now. it has come from server 
      if (sslPolicyErrors == SslPolicyErrors.None) 
       return true; 

      Console.WriteLine("Certificate error: {0}", sslPolicyErrors); 

      // Do not allow this client to communicate with unauthenticated servers. 
      return false; 
     } 

を書きました。
'localCertSelection'メソッドの 'remoteCertificate'は空ですが、 'ValidateServerCertificate'メソッドの 'certificate'メソッドにはデータがあります。サーバーから来たのですが、なぜ 'sslPolicyErrors'が 'RemoteCertificateNameMismatch | RemoteCertificateChainErrors '? どうしたの?私がしなければならないこと?

答えて

0

"servername"が間違っていると、RemoteCertificateNameMismatchエラーが発生することがあります。私は

sslStream.AuthenticateAsClient(serverName); 

でservernameは「192.168.1.160」、

TcpClient client = new TcpClient("192.168.1.160", 4113); 
と同じルート証明書と間違って何かあれば RemoteCertificateChainErrorsは起こらなければならないことを意味します。証明書を作成するときは、適切なホストをCNに入れなければなりません。 CN = 192.168.1.160。また、ルート証明書を「信頼されたルート証明機関」にインポートすることを忘れないでください。

関連する問題