Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying download file from HTTPS URL in my Java program.
I have provided KEYSTORE, TRUSTSTORE, HttpConnection, Socket. But I am getting following exception.
I just want handshake to be done then I Will go for download code.PLease help,its urgent.

error:
main, WRITE: TLSv1 Handshake, length = 32
main, READ: TLSv1 Alert, length = 18
main, RECV TLSv1 ALERT: fatal, handshake_failure
%% Invalidated: [Session-3, SSL_RSA_WITH_RC4_128_MD5]
main, called closeSocket()
main, handling exception: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
main, called close()
main, called closeInternal(true)
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at Test1.main(Test1.java:131)

code:


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.Properties;

import javax.net.ssl.HandshakeCompletedEvent;
import javax.net.ssl.HandshakeCompletedListener;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

public class Test1 {

public static void main( String[] args )
throws Exception
{
// Use the public key from the AIDAP server as the trust store for this client.
// (note: created this keystore using InstallCerts.java from sun.com)
Properties systemProps = System.getProperties();
systemProps.put( "javax.net.ssl.trustStore", "c:/certs/cacerts");

//systemProps.put( "javax.net.ssl.keyStore", "c:/certs/retail.pfx");
systemProps.put( "javax.net.debug", "ssl");

System.setProperties(systemProps);
System.out.println("******************************************************");


try {
// Open a secure connection.
URL url = new URL( "https://mis.ercot.com/misapp/servlets/mirDownload?doclookupId=98241494"; );
// String requestParams = "uid=API_35077&password=changeit&active=y&type=jks";
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

// Set up the connection properties
con.setRequestProperty( "Connection", "close" );
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setConnectTimeout( 3000000 );
con.setReadTimeout( 3000000 );
con.setRequestMethod( "POST" );
con.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
// con.setRequestProperty( "Content-Length", Integer.toString(requestParams.length()) );

File pKeyFile = new File("c:\\certs\\retail.pfx");
String pKeyPassword = "lseret";
String host= "mis.ercot.com";
//KeyManagerFactory keyManagerFactory = //KeyManagerFactory.getInstance("IbmX509", "IBMJSSE2");
System.out.println("===========KEY STORE START======================");
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509", "SunJSSE");

KeyStore keyStore = KeyStore.getInstance("PKCS12");
InputStream keyInput = new FileInputStream(pKeyFile);
keyStore.load(keyInput, pKeyPassword.toCharArray());
//keyInput.close();
System.out.println("===========KEY STORE END======================");
keyManagerFactory.init(keyStore, pKeyPassword.toCharArray());
System.out.println("===========KEY STORE END1111111111======================");
File pTrustFile= new File("c:\\certs\\cacerts");
String pTrustPassword= "changeit";

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509", "SunJSSE");
//TrustManagerFactory.getInstance("IbmX509", "IBMJSSE2");

KeyStore trustStore= KeyStore.getInstance("jks");
InputStream trustinput = new FileInputStream(pTrustFile);
System.out.println("------------ FILE STREAM-------"+trustinput.available());
trustStore.load(trustinput,pTrustPassword.toCharArray());
// trustinput.close();
trustManagerFactory.init(trustStore);

//System.out.println("Truststore provider:" +trustManagerFactory.getProvider().getName());
SSLContext context = SSLContext.getInstance("TLS", "SunJSSE");
// SSLContext ctx = SSLContext.getInstance("TLS", "IBMJSSE2");
context.init(keyManagerFactory.getKeyManagers(),trustManagerFactory.getTrustManagers(),
new java.security.SecureRandom());
SSLSocketFactory sockFact = context.getSocketFactory();
con.setSSLSocketFactory( sockFact );
System.out.println("====================***********");

SSLSocket socket =(SSLSocket) sockFact.createSocket(host,443);

//socket.setSoTimeout(3000000);
socket.addHandshakeCompletedListener(
new HandshakeCompletedListener() {
public void handshakeCompleted(HandshakeCompletedEvent hce) {
System.out.println("***************Handshake:************");
System.out.println(" ******* getCipherSuite :" + hce.getCipherSuite());

}
});

// Force the handshake.
socket.startHandshake();
System.out.println(">>>>>>>>>>>>>>>>>>DONE<<<<<<<<<<<<<<<<<<<<");


// Send the request
// OutputStream outputStream = con.getOutputStream();
//outputStream.write( requestParams.getBytes("UTF-8") );
//outputStream.close();

//Thread.sleep(35000);
System.out.println("========Before Response code.============");
// Check for errors
// int responseCode = con.getResponseCode();
//System.out.println("========After Response code.============"+responseCode);

InputStream in = con.getInputStream();//can be with socket as well.
OutputStream out = con.getOutputStream();
System.out.println("========Before Response code.============"+in.available());
byte[] buffer = new byte[1024];
int numRead;
long numWritten = 0;
System.out.println("============ before while loop..");
while ((numRead = in.read(buffer)) != -1) {
System.out.println("========== numread=="+numRead);
numWritten += numRead;
}
System.out.println("============ after while loop..");
/*InputStream inputStream;
if (responseCode == HttpURLConnection.HTTP_OK) {
inputStream = con.getInputStream();
} else {
inputStream = con.getErrorStream();
}
System.out.println("=============22=======");
// Process the response
BufferedReader reader;
String line = null;
reader = new BufferedReader( new InputStreamReader( inputStream ) );
while( ( line = reader.readLine() ) != null )
{
System.out.println( line );
}

inputStream.close();*/
} catch (Exception e) { e.printStackTrace(); }
}
}



ANY help is Appreciated...
 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic