title: Java 1.6_u45 + BouncyCastle + TLS1.2 throws handshake_failure(40) (notifySecureRenegotiation)
body: <p>My case is almost close to <a href="https://stackoverflow.com/questions/49405702/java-1-6-bouncycastle-tls1-2-handshake-failure40">Another post here</a> </p>
  
  <p>I am on JDK1.6_u45 trying to connect to a https endpoint that is on TLS1.2 using BouncyCastle.
  I have added the endpoint public key certificate onto cacerts under jre/lib/security</p>
  
  <p>However the error I get follows different stack trace as shown below:</p>
  
  <pre><code>Exception in thread "Main Thread" org.bouncycastle.crypto.tls.TlsFatalAlert: handshake_failure(40)
  at org.bouncycastle.crypto.tls.AbstractTlsPeer.notifySecureRenegotiation(Unknown Source)
  at org.bouncycastle.crypto.tls.TlsClientProtocol.receiveServerHelloMessage(Unknown Source)
  at org.bouncycastle.crypto.tls.TlsClientProtocol.handleHandshakeMessage(Unknown Source)
  at org.bouncycastle.crypto.tls.TlsProtocol.processHandshakeQueue(Unknown Source)
  at org.bouncycastle.crypto.tls.TlsProtocol.processRecord(Unknown Source)
  at org.bouncycastle.crypto.tls.RecordStream.readRecord(Unknown Source)
  at org.bouncycastle.crypto.tls.TlsProtocol.safeReadRecord(Unknown Source)
  at org.bouncycastle.crypto.tls.TlsProtocol.blockForHandshake(Unknown Source)
  at org.bouncycastle.crypto.tls.TlsClientProtocol.connect(Unknown Source)
  at TLSSocketConnectionFactory$1.startHandshake(TLSSocketConnectionFactory.java:498)
  at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:434)
  at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:167)
  at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:134)
  at CopyOfTest.getResponseJsonString(CopyOfTest.java:40)
  at CopyOfTest.main(CopyOfTest.java:15)
  </code></pre>
  
  <p>The BouncyCastle TLSSocketConnectionFactory is same as the one provided in this link, so let me not post it here again. (link: <a href="https://stackoverflow.com/questions/49405702/java-1-6-bouncycastle-tls1-2-handshake-failure40">another post here</a></p>
  
  <p>My test class is as below:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.URL;
  
  import javax.net.ssl.HttpsURLConnection;
  
  import com.google.gson.Gson;
  
  public class CopyOfTest {
      public static void main(String[] args) throws IOException, Exception {
          //below url is a not an actual endpoint. 
          URL url = new URL(
                  "https://abc.def.ghi/Customer/v1/nonexistantlink/?postalCode=80120&amp;clientId=ABC");
          String returnData = getResponseJsonString(url);
          System.out.println("returnData: " + returnData);
          ArrayNameDescDTO msg = new Gson().fromJson(returnData,
                  ArrayNameDescDTO.class);
          System.out.println(msg.toString());
  
  
      }
  
      private static String getResponseJsonString(URL url) throws IOException {
  
  //      Security.addProvider(new BouncyCastleJsseProvider());
  //       SSLContext sslcontext = SSLContext.getInstance("TLS",new BouncyCastleJsseProvider())
  
          HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
          conn.setSSLSocketFactory(new TLSSocketConnectionFactory());
  
          conn.setRequestMethod("GET");
          byte[] message = ("username" + ":" + "andItsPassword").getBytes("UTF-8");
          String encoded = javax.xml.bind.DatatypeConverter.printBase64Binary(message);
          System.out.println("encoded: Basic " + encoded);
          conn.setRequestProperty("Authorization", "Basic " + encoded);
          conn.setConnectTimeout(10000); // 10 sec
  
          conn.connect();
          int status = conn.getResponseCode();
          if (conn.getResponseCode() != 200) {
              throw new RuntimeException("Failed : HTTP error code : "
                      + conn.getResponseCode());
          }
          switch (status) {
          case 200:
          case 201:
              BufferedReader br = new BufferedReader(new InputStreamReader(
                      conn.getInputStream()));
              StringBuilder sb = new StringBuilder();
              String line;
              while ((line = br.readLine()) != null) {
                  sb.append(line + "\n");
              }
              br.close();
              conn.disconnect();
              return sb.toString();
          }
  
          conn.disconnect();
          return null;
  
      }
  }
  </code></pre>
  
  <p>Any suggestions or pointers?</p>