2016-05-04 30 views
3

バイパス証明書の検証

ServicePointManager.ServerCertificateValidationCallback = ValidateRemoteCertificate; 

... 

private bool ValidateRemoteCertificate(
    object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors) 
{ 
    LogActionToTable(EntrySeverity.Information, 
     $"ValidateRemoteCertificate() Certificate name: {cert.Subject}"); 
    return true; 
} 

Webアプリケーション内のメソッドから呼び出されたときに、ラインがを実行されることはありませんこと、weird十分にユニットテスト(NUnit)から適切に呼び出されます。

リモートAPIへの他の呼び出しの前にこの呼び出しを追加しています。

私は間違っていますか?

私はちょうど行う場合はそれも働いていないのです。

ServicePointManager 
    .ServerCertificateValidationCallback += 
    (sender, cert, chain, sslPolicyErrors) => true; 

は、これは本当に簡単でなければなりませんが、私は数日間、この上だと私は正常に動作し、これを管理することはできません。


私は wsdl.exeツールから生成されたプロキシを介して手動でXMLを構築し、またはと呼ばれるRestSharpコールを使用している場合に関係なく、私は常に呼び出しからこれを取得終了

要求が中止されました:SSL/TLSセキュアチャネルを作成できませんでした。

とは、ログファイルにトレースを有効にする、私が取得:

System.Net Information: 0 : [7912] SecureChannel#14469269 - Certificate is of type X509Certificate2 and contains the private key. System.Net Information: 0 : [7912] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent = Outbound, scc = System.Net.SecureCredential) System.Net Information: 0 : [7912] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = (null), targetName = onni.unicom.fi, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation) System.Net Information: 0 : [7912] InitializeSecurityContext(In-Buffer length=0, Out-Buffer length=170, returned code=ContinueNeeded). System.Net Information: 0 : [7912] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 14899210:2119fd0, targetName = onni.unicom.fi, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation) System.Net Information: 0 : [7912] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=0, returned code=CredentialsNeeded). System.Net Information: 0 : [7912] SecureChannel#14469269 - We have user-provided certificates. The server has specified 1 issuer(s). Looking for certificates that match any of the issuers. System.Net Information: 0 : [7912] SecureChannel#14469269 - Left with 0 client certificates to choose from. System.Net Information: 0 : [7912] Using the cached credential handle. System.Net Information: 0 : [7912] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 14899210:2119fd0, targetName = onni.unicom.fi, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation) System.Net Information: 0 : [7912] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=173, returned code=ContinueNeeded). System.Net Information: 0 : [7912] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 14899210:2119fd0, targetName = onni.unicom.fi, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation) System.Net Information: 0 : [7912] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=0, returned code=CertUnknown). System.Net Error: 0 : [7912] Exception in HttpWebRequest#25731266:: - The request was aborted: Could not create SSL/TLS secure channel.. System.Net Error: 0 : [7912] Exception in HttpWebRequest#25731266::EndGetRequestStream - The request was aborted: Could not create SSL/TLS secure channel..

が最も重要:

System.Net情報:0:[7912] InitializeSecurityContextの(インバッファカウント= 2、アウトバッファ長= 0、戻りコード= CertUnknown)。

ServicePointManager.ServerCertificateValidationCallbackコールでこれをバイパスしないのはなぜですか?


プロキシなしコード:

private CardBalanceInfo ApiGetCardBalance(string cardNumber) 
    { 
     LogActionToTable(EntrySeverity.Information, $"ApiGetCardBalance('{cardNumber}')"); 

     var soapEnvelope = 
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
       "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + 
       " <soap:Header/>" + 
       " <soap:Body>" + 
       " <csi:getBalance xmlns:csi=\"urn:si.tm.webservice.uniassociation.unicom.com\">" + 
       "  <csi:balanceId>" + 
       $"  <entityName>{cardNumber}</entityName>" + 
       "  </csi:balanceId>" + 
       " </csi:getBalance>" + 
       " </soap:Body>" + 
       "</soap:Envelope>"; 

     const string soapUrl = "treasurymanagement/si"; 
     const string soapAction = "http://www.unicom.fi/uAASTreasuryManagementSI/getBalance"; 
     var response = MakeApiCall(soapUrl, soapAction, soapEnvelope); 

     return response.ToBalanceInfo(); 
    } 

プロキシを使用してコード:

private CardBalanceInfo ApiGetCardBalanceFromProxy(string cardNumber) 
    { 
     LogActionToTable(EntrySeverity.Information, $"ApiGetCardBalanceFromProxy('{cardNumber}')"); 

     using (var service = new uAASTreasuryManagementSIService()) 
     { 
      service.Timeout = 10000; // 10 sec 
      service.Url = _settings.BaseUrl.TrimEnd('/') + "/treasurymanagement/si"; 
      service.ClientCertificates.Add(_clientCertificate); 

      var card = new getBalance() 
      { 
       balanceId = new EntityId 
       { 
        entityName = cardNumber 
       } 
      }; 

      var balanceResponse = service.getBalance(card); // get API response 

      LogActionToTable(EntrySeverity.Information, $"ApiGetCardBalanceFromProxy success."); 

      return new CardBalanceInfo() 
      { 
       AvailableBalance = balanceResponse.balance.availableBalanceValue, 
       Balance = balanceResponse.balance.balanceValue, 
       BalanceId = balanceResponse.balance.balanceId.ToString() 
      }; 
     } 
    } 
+0

クライアント側のコードでこれを設定していますか? –

+0

同じコードがDEVやQAなどの下位環境で正しく機能しますか? AppPoolのセキュリティに関連する可能性があります。他のオプションは、検証するクライアント証明書が不足している可能性があります。 – Konstantin

+0

ブラウザでサービスエンドポイントにアクセスすると、何が表示されますか? –

答えて

0

私は、証明書の検証があなたの問題であるとは考えていません。

ServerCertificateValidationCallbackは、証明書のカスタム検証を行うときに使用されます。これは、証明書のホスト名が呼び出しているサービスのURLと一致しない、devまたはqa環境で最も一般的に必要となります。

あなたはこの問題に遭遇してきたときに、次のエラーの詳細をSecurityNeogtiationExceptionに届きますので、あなたは知っているよ:

追加情報を:でSSL/TLSのセキュリティで保護されたチャネルの信頼関係を確立できませんでした権限 '{HOSTNAME}'

ローカルマシン上のServerCertificateValidationCallbackを無効にしたときの状態です。

enter image description here

私はあなたが、要求が中断されました

にあなたの焦点をずらすお勧めします:SSL/TLSのセキュリティで保護されたチャネルを作成できませんでした。

これはまったく異なる問題であるように見えます。そして、もう一つは幸いにも1私はここで答えが開始するには良い場所でしょう:-)に走っていない:The request was aborted: Could not create SSL/TLS secure channel

HTH

+0

私はスタックメッセージ 'returned code = CertUnknown'と次のものはSSLエラーチャネルです...私はそれが好きです:) - プラス、私はすでに' ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; '。戻りコードは実際には '200'です(ホスティングサーバーからPostmanを使ってAPIを正しく呼び出すことができます)。 – balexandre

+0

ところで、あなたのエラーは他のライブラリを使っているのと同じです。(私はHttpWebRequestを使用しています) – balexandre

+0

何かのような音は証明書で "オフ"です。大声で考えてみましょう...発信者が自分の店の証明書を信頼しているため、同じマシンから呼び出すときに機能することは意味があります。 –