title: (Java) No Required SSL certificate was sent (Works fine when used in curl call)
body: <p>So, I'm trying to test out a connection to my company's web server (using 2-way SSL) by using a client certificate in a Java program.
  I have tried using the same certificate in a curl call (separated cert and key) and managed to get the desired response.
  But when I tried using it in my Java program (combined into pkcs12 format), it gives a 400 Response saying No Required SSL certificate was sent. Why is this happening? </p>
  
  <pre><code>public static void main(String[] args) {
      System.out.println("Taufiq's mutual SSL-authentication test");
      org.apache.log4j.BasicConfigurator.configure();
      Logger.getRootLogger().setLevel(Level.INFO);
  
      try {
          final String CERT_ALIAS = "something", CERT_PASSWORD = "something";
  
          KeyStore identityKeyStore = KeyStore.getInstance("pkcs12");
          FileInputStream identityKeyStoreFile = new FileInputStream(new File("src/Cert.p12"));
          identityKeyStore.load(identityKeyStoreFile, CERT_PASSWORD.toCharArray());
  
          KeyStore trustKeyStore = KeyStore.getInstance("jks");
          FileInputStream trustKeyStoreFile = new FileInputStream(new File("src/truststore.jks"));
          trustKeyStore.load(trustKeyStoreFile, CERT_PASSWORD.toCharArray());
  
          SSLContext sslContext = SSLContexts.custom()
              // load identity keystore
              .loadKeyMaterial(identityKeyStore, CERT_PASSWORD.toCharArray(), new PrivateKeyStrategy() {
                  @Override
                  public String chooseAlias(Map&lt;String, PrivateKeyDetails&gt; aliases, Socket socket) {
                      return CERT_ALIAS;
                  }
              })
              // load trust keystore
              .loadTrustMaterial(trustKeyStore, null)
              .build();
  
          SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
              new String[]{"TLSv1.2", "TLSv1.1"},
              null,
              SSLConnectionSocketFactory.getDefaultHostnameVerifier());
  
          CloseableHttpClient client = HttpClients.custom()
              .setSSLSocketFactory(sslConnectionSocketFactory)
              .build();
  
          // Call a SSL-endpoint
          callEndPoint (client);
      } catch (Exception ex) {
          System.out.println("Boom, we failed: " + ex);
          ex.printStackTrace();
      }
  }
  
  private static void callEndPoint(CloseableHttpClient aHTTPClient) {
  
      try {          
          String ServerUrl = "My Company URL";
          System.out.println("Calling URL: " + ServerUrl);
          HttpPost post = new HttpPost(ServerUrl);
          post.setHeader("Content-type", "application/json");
  
          System.out.println("**POST** request Url: " + post.getURI());
  
          HttpResponse response = aHTTPClient.execute(post);
  
          int responseCode = response.getStatusLine().getStatusCode();
          System.out.println("Response Code: " + responseCode);
          System.out.println("Content:-\n");
          BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
          String line;
          while ((line = rd.readLine()) != null) {
              System.out.println(line);
          }
      } catch (Exception ex) {
          System.out.println("Boom, we failed: " + ex);
          ex.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>Sample Curl call:
      curl -v --key key.pem --pass ****** --cert cert.pem MyCompanyURL</p>