title: TrustManagerFactory - Ignore certificate check
body: <p>I am using a library to connect to Azure and it uses okhttp3 to handle connections.
  There is a part of this library that calls <strong>okhttp3.internal.Util#platformTrustManager</strong>:</p>
  
  <pre class="lang-java prettyprint-override"><code> public static X509TrustManager platformTrustManager() {
          try {
              TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
              trustManagerFactory.init((KeyStore)null);
              TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
              if (trustManagers.length == 1 &amp;&amp; trustManagers[0] instanceof X509TrustManager) {
                  return (X509TrustManager)trustManagers[0];
              } else {
                  throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
              }
          } catch (GeneralSecurityException var2) {
              throw assertionError("No System TLS", var2);
          }
      }
  </code></pre>
  
  <p>And the <strong>javax.net.ssl.TrustManagerFactory#getInstance(java.lang.String)</strong>:</p>
  
  <pre class="lang-java prettyprint-override"><code>public static final TrustManagerFactory getInstance(String var0) throws NoSuchAlgorithmException {
          Instance var1 = GetInstance.getInstance("TrustManagerFactory", TrustManagerFactorySpi.class, var0);
          return new TrustManagerFactory((TrustManagerFactorySpi)var1.impl, var1.provider, var0);
      }
  </code></pre>
  
  <p>For some reason, I am getting <strong>javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target</strong></p>
  
  <pre><code>Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
      at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1946)
      at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:316)
      at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:310)
      at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1639)
      at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:223)
      at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1037)
      at sun.security.ssl.Handshaker.process_record(Handshaker.java:965)
      at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1064)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)
      at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:320)
      at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:284)
      at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:169)
      at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:258)
      at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
      at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
      at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
      at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
      at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
      at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:127)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
      at com.microsoft.rest.retry.RetryHandler.intercept(RetryHandler.java:75)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
      at com.microsoft.rest.interceptors.CustomHeadersInterceptor.intercept(CustomHeadersInterceptor.java:140)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
      at com.microsoft.rest.interceptors.UserAgentInterceptor.intercept(UserAgentInterceptor.java:83)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
      at com.microsoft.azure.credentials.AzureTokenCredentialsInterceptor.intercept(AzureTokenCredentialsInterceptor.java:40)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
      at com.microsoft.azure.management.resources.fluentcore.utils.ResourceManagerThrottlingInterceptor.intercept(ResourceManagerThrottlingInterceptor.java:54)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
      at com.microsoft.azure.management.resources.fluentcore.utils.ProviderRegistrationInterceptor.intercept(ProviderRegistrationInterceptor.java:40)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
      at com.microsoft.rest.interceptors.BaseUrlHandler.intercept(BaseUrlHandler.java:43)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
      at com.microsoft.rest.interceptors.RequestIdHeaderInterceptor.intercept(RequestIdHeaderInterceptor.java:29)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
      at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:257)
      at okhttp3.RealCall.execute(RealCall.java:93)
      at retrofit2.OkHttpCall.execute(OkHttpCall.java:186)
      at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:40)
      at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:24)
      at rx.Observable.unsafeSubscribe(Observable.java:10327)
      at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
      at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
      at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
      at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
      at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
      at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
      at rx.Observable.subscribe(Observable.java:10423)
      at rx.Observable.subscribe(Observable.java:10390)
      at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:443)
      ... 35 more
  </code></pre>
  
  <p>The certificates are validated at <strong>sun.security.ssl.ClientHandshaker#serverCertificate</strong>:</p>
  
  <pre class="lang-java prettyprint-override"><code> ((X509ExtendedTrustManager)var6).checkServerTrusted((X509Certificate[])var2.clone(), var4, this.conn);
  </code></pre>
  
  <p>It's a real valid certificate but the error persists.</p>
  
  <p>I am not sure how to fix this. I wanted to disable the certificate validation altogether but I also can't seem to find a way to do it in this setup.</p>
  
  <p>Why would a valid certificate throw this error?</p>
  
  <p>How can I "install" this certificate (I can only get it as a text, by debugging), or disable the validation?</p>
  
  <p>Also, I am running this on Windows.</p>