title: CertPathValidatorException : Trust anchor for certificate path not found - Retrofit Android
body: <p>I am creating an android application which uses <code>https</code> for communication with the server. I am using <code>retrofit</code> and <code>OkHttp</code> for making requests. These works fine for standard <code>http</code> requests. The following are the steps that I followed.</p>
  
  <p><strong>Step 1 :</strong>
  Acquired the cert file from the server using the command</p>
  
  <pre><code>echo -n | openssl s_client -connect api.****.tk:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' &gt; gtux.cert
  </code></pre>
  
  <p><strong>Step 2 :</strong>
  Converted the cert to a BKS format by using the following commands</p>
  
  <pre><code>keytool -importcert -v -trustcacerts -file "gtux.cert" -alias imeto_alias -keystore "my_keystore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "bcprov-jdk16-146.jar" -storetype BKS
  </code></pre>
  
  <p>It asked me for password and the file was successfully created.</p>
  
  <p><strong>Step 3 :</strong></p>
  
  <p>Create a OkHttpClient and use the same for making https requests</p>
  
  <pre><code>public class MySSLTrust {
  public static OkHttpClient trustcert(Context context){
      OkHttpClient okHttpClient = new OkHttpClient();
      try {
          KeyStore ksTrust = KeyStore.getInstance("BKS");
          InputStream instream = context.getResources().openRawResource(R.raw.my_keystore);
          ksTrust.load(instream, "secret".toCharArray());
          // TrustManager decides which certificate authorities to use.
          TrustManagerFactory tmf = TrustManagerFactory
                  .getInstance(TrustManagerFactory.getDefaultAlgorithm());
          tmf.init(ksTrust);
          SSLContext sslContext = SSLContext.getInstance("TLS");
          sslContext.init(null, tmf.getTrustManagers(), null);
          okHttpClient.setSslSocketFactory(sslContext.getSocketFactory());
      } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException | KeyManagementException e) {
          e.printStackTrace();
      }
      return okHttpClient;
  }
  }
  </code></pre>
  
  <p><strong>Step 4:</strong></p>
  
  <p>RestAdapter has to be created</p>
  
  <pre><code>RestAdapter.Builder()
  .setRequestInterceptor(intercept)
  .setEndpoint("https://api.****.tk")
  .setClient(new OkClient(this))
  .setLogLevel(RestAdapter.LogLevel.FULL)
  .setLog(new AndroidLog("RETROFIT"))
  .build();
  </code></pre>
  
  <p>But finally when run the app it is throwing me <code>CertPathValidatorException : Trust anchor for certificate path not found</code>. Please help me to solve this. Thank you. </p>
  
  <p><strong>Other failure attempts :</strong>
  Tried to install the certificate in my Xperia Z2 and it says the file was installed but when i run the app the same exception is thrown. </p>
  
  <p><strong>Error Log</strong>
  Here is the error log that I got on executing... </p>
  
  <p><a href="https://paste.ee/p/5tZP3">Error Log</a></p>
  
  <p>Pasted there so that it will be easy to read..</p>