javax.net.ssl.HttpsURLConnection Problem

Hi,

I want make an HTTPS connection from JBoss:

// build url and connection

URL url =null;

try{

url =new URL(mmsURL);

}

catch (MalformedURLException murle){

logger.fatal("sendMMS() failed build url to mmsURL=" + mmsURL, murle);

thrownew OMAException("MMS::sendMMS() failed build url to mmsURL=" + mmsURL, murle, ErrorIDs.FATAL_ERROR);

}

// open connection

javax.net.ssl.HttpsURLConnection conn =null;

try{

conn = (javax.net.ssl.HttpsURLConnection) url.openConnection();

conn.setSSLSocketFactory(ctx.getSocketFactory());

}

catch (IOException ioe){

logger.fatal("sendMMS() open connection failed to mmsURL=" + mmsURL, ioe);

thrownew OMAException("MMS::sendMMS() open connection failed to mmsURL=" + mmsURL, ioe, ErrorIDs.FATAL_ERROR);

}

But I get an ClassCastException while url.openConnection() an

com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl

What cann I do?

Regards,

Rafal

[1798 byte] By [rafcioa] at [2007-10-1 6:43:09]
# 1

Just check what connection you are getting: instead of

conn = (javax.net.ssl.HttpsURLConnection) url.openConnection();

use

URLConnection con = url.openConnection();

System.out.println("con class: "+con.getClass());

YOu probably don't get a javax.net.ssl.HttpsURLConnection (or some sublcass of it) ...

MartinHilperta at 2007-7-9 16:47:23 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 2
Are you sure, "mmsURL" is a HTTPS URL ("https://...")?
MartinHilperta at 2007-7-9 16:47:23 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 3

After add this line

System.setProperty("java.protocol.handler.pkgs","javax.net.ssl");

it works. But I get now the

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found

Regards,

Rafal

rafcioa at 2007-7-9 16:47:23 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 4

I do this:

SSLContext sslContext = null;

KeyManagerFactory keyManagerFactory = null;

KeyStore keyStore = null;

// read certificate

try {

keyStore = KeyStore.getInstance("PKCS12");

InputStream fis = PropertiesLoader.getInputStream(CERTIFICATE_FILE, getClass());

keyStore.load(fis, password.toCharArray());

}

catch (KeyStoreException kse) {

logger.fatal("sendMMS() failed with KeyStoreException");

throw new OMAException("MMS::sendMMS() failed with KeyStoreException", kse, ErrorIDs.FATAL_ERROR);

}

catch (IOException ioe) {

logger.fatal("sendMMS() failed with IOException");

throw new OMAException("MMS::sendMMS() failed with IOException", ioe, ErrorIDs.FATAL_ERROR);

}

catch (NoSuchAlgorithmException nsae) {

logger.fatal("sendMMS() failed with NoSuchAlgorithmException");

throw new OMAException("MMS::sendMMS() failed with NoSuchAlgorithmException", nsae, ErrorIDs.FATAL_ERROR);

}

catch (CertificateException ce) {

logger.fatal("sendMMS() failed with CertificateException");

throw new OMAException("MMS::sendMMS() failed with CertificateException", ce, ErrorIDs.FATAL_ERROR);

}

try {

keyManagerFactory = KeyManagerFactory.getInstance("SunX509");

keyManagerFactory.init(keyStore, password.toCharArray());

sslContext = SSLContext.getInstance("TLS");

sslContext.init(keyManagerFactory.getKeyManagers(), null, null);

}

catch (KeyStoreException kse) {

logger.fatal("sendMMS() failed with KeyStoreException");

throw new OMAException("MMS::sendMMS() failed with KeyStoreException", kse, ErrorIDs.FATAL_ERROR);

}

catch (NoSuchAlgorithmException nsae) {

logger.fatal("sendMMS() failed with NoSuchAlgorithmException");

throw new OMAException("MMS::sendMMS() failed with NoSuchAlgorithmException", nsae, ErrorIDs.FATAL_ERROR);

}

catch (UnrecoverableKeyException uke) {

logger.fatal("sendMMS() failed with UnrecoverableKeyException");

throw new OMAException("MMS::sendMMS() failed with UnrecoverableKeyException", uke, ErrorIDs.FATAL_ERROR);

}

catch (KeyManagementException kme) {

logger.fatal("sendMMS() failed with KeyManagementException");

throw new OMAException("MMS::sendMMS() failed with KeyManagementException", kme, ErrorIDs.FATAL_ERROR);

}

// build url and connection

URL url = null;

try {

url = new URL(mmsURL);

}

catch (MalformedURLException murle) {

logger.fatal("sendMMS() failed build url to mmsURL=" + mmsURL, murle);

throw new OMAException("MMS::sendMMS() failed build url to mmsURL=" + mmsURL, murle, ErrorIDs.FATAL_ERROR);

}

// open connection

HttpsURLConnection conn = null;

try {

Object o = url.openConnection();

if (o instanceof javax.net.ssl.HttpsURLConnection) {

conn = (javax.net.ssl.HttpsURLConnection) o;

conn.setSSLSocketFactory(sslContext.getSocketFactory());

}

else {

logger.error("sendMMS() class=" + o.getClass());

throw new OMAException("MMS::sendMMS() wrong HTTPS connection [" + o.getClass() + "]", ErrorIDs.FATAL_ERROR);

}

}

catch (IOException ioe) {

logger.fatal("sendMMS() open connection failed to mmsURL=" + mmsURL, ioe);

throw new OMAException("MMS::sendMMS() open connection failed to mmsURL=" + mmsURL, ioe, ErrorIDs.FATAL_ERROR);

}

try {

byte[] bytes = mms.getBytes(Encoding.UTF_8);

List readLines = new ArrayList();

BufferedReader reader = null;

try {

String encodedAuthorization = new String(FastBase64.encode(authorization));

if (logger.isDebugEnabled()) {

logger.debug("sendMMS() Content-Type=" + MimeType.TEXT_XML + "; charset=" + Encoding.UTF_8);

//logger.debug("sendMMS() Content-length=" + String.valueOf(bytes.length));

logger.debug("sendMMS() Authorization=" + "BASIC " + encodedAuthorization);

}

conn.setRequestMethod("POST");

conn.setRequestProperty("Content-Type", MimeType.TEXT_XML + "; charset=" + Encoding.UTF_8);

//conn.setRequestProperty("Content-length", String.valueOf(bytes.length));

conn.setRequestProperty("Authorization", "BASIC " + encodedAuthorization);

conn.setDoInput(true);

conn.setDoOutput(true);

OutputStream out = conn.getOutputStream();

out.write(bytes);

out.flush();

out.close();

// reading response from stream

reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String responseLine = reader.readLine();

do {

readLines.add(responseLine);

responseLine = reader.readLine();

} while (responseLine != null);

}

catch (IOException ioe) {

logger.fatal("sendMMS() failed sending or reading data", ioe);

throw new OMAException("MMS::sendMMS() failed sending or reading data", ioe, ErrorIDs.FATAL_ERROR);

}

finally {

if (reader != null) {

try {

reader.close();

}

catch (IOException ioe) {

logger.fatal("sendMMS() can't close reader", ioe);

}

}

}

My PCKS#12 file is in the Jar file. And I get this Exception:

2005-03-01 17:59:17,371 FATAL [net.magix.mobile.magix.MMS] sendMMS() failed sending or reading data

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found

But if I add this tart parameter -Djavax.net.debug=ssl I can't see any problem.

Regards,

Rafal

rafcioa at 2007-7-9 16:47:23 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 5
It's working now.
rafcioa at 2007-7-9 16:47:23 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 6

It would be appreciated it if you detail the solution after making one, that way people with similar issues can find a ready source for the problem and the solution. There is no benefit to your fellow forum mates posting questions in a public forum if you aren't going to share the answers you've recieved or divined yourself from those questions.

Thanks

sent2null

sent2nulla at 2007-7-9 16:47:23 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 7
Pls share the solution with us. I have the similar problem now. Thanks.
hlia at 2007-7-9 16:47:23 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 8
Hi,Can you please share the solution you got?I am in the same situation trying to resolve this for past 7+days and running out of time. Please take out couple of mins and share ur solution.Appreciate any help.Thanks,--Srini
nagapuria at 2007-7-9 16:47:23 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 9
hican u send me ur https whole code for client-server application
dvipala at 2007-7-9 16:47:23 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...