1

I have problem to connect on https get/post using tlsv1.2 via java.

Somebody have a guide or some examples that work? I imported by keytool private and public key setted by password on keystore. I used a SSLSocketFactory but I got only errors https (not handshake). I added parameters in jvm -Dhttps.protocols="TLSv1.2" to enable only version 1.2 of TLS. But nothing ...

An example please or a guide from import certificate to implements the class and using SSLSocketFactory.

Thanks a lot for your help...

SkyBlackHawk
  • 95
  • 10

3 Answers3

1

Java 6 does NOT support tls1.2 link, if used java 6 you cannot enable TLS1.2.

0

I hope this will work

InputStream is = new FileInputStream("cert.crt");

CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate caCert = (X509Certificate)cf.generateCertificate(is);

TrustManagerFactory tmf = TrustManagerFactory
    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null); // You don't need the KeyStore instance to come from a file.
ks.setCertificateEntry("caCert", caCert);

tmf.init(ks);

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);

And to create SocketFactory

SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1.2" }, null,
            SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Shettyh
  • 1,188
  • 14
  • 27
0

I had the same problem and finally build my own "TLSConnectionFactory" based on BouncyCasttle Provider. The code is shared here

So, after you can use this TLSConnectionFactory this simple and quick way:

     String httpsURL =  xxxxxxxxxx
    URL myurl = new URL(httpsURL);      
    HttpsURLConnection  con = (HttpsURLConnection )myurl.openConnection();
    con.setSSLSocketFactory(new TSLSocketConnectionFactory());   
    InputStream ins = con.getInputStream();
Community
  • 1
  • 1
Azimuts
  • 1,212
  • 4
  • 16
  • 35