AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SSL certificate not valid and expired

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Security and Java
View previous topic :: View next topic  
Author Message
Maros Kollar
Guest





PostPosted: Sun Apr 17, 2005 1:50 pm    Post subject: SSL certificate not valid and expired Reply with quote



Hi there,

i'm trying to access a website from java over https. However the
certificate is self-signed and also expired (this doesn't matter since
this is only a developement server). To bypass the default security
manager which doesn't allow me to access this site I have written the
following code. However i get an runtime error saying that the
KeyManagerFactoryImpl is not initialized. I'm an absolute java beginner,
so any help is highly appreciated!

kind regards
Maros

* CODE *
package myname.restarter;

import java.net.*;
import java.io.*;
import java.security.*;
import java.security.cert.*;
import javax.net.ssl.*;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.TrustManager;

class Restart {
public boolean success;

Restart () {
try {
success = false;
TrustManager[] trustall = new TrustManager[] {
X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[]
x509Certificates,String authType) {
}
public void checkClientTrusted(X509Certificate[]
x509Certificates,String authType) {
}
}
};

System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
SSLContext sc = SSLContext.getInstance("SSL");
KeyManagerFactory factory = KeyManagerFactory.getInstance("SunX509");
sc.init(factory.getKeyManagers(),trustall,new
java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
URL restarturl = new URL("https://myurl.com");
HttpURLConnection restartcon = (HttpURLConnection)
restarturl.openConnection();
restartcon.setDoInput( true );
restartcon.setRequestMethod("GET");
restartcon.setRequestProperty(
"Authorization",userNamePasswordBase64("username","password") );
restartcon.connect();
if (restartcon.getResponseCode() != HttpURLConnection.HTTP_OK) {
success = true;
}
}
catch (MalformedURLException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
catch (NoSuchAlgorithmException e) {e.printStackTrace();}
catch (KeyManagementException e) {e.printStackTrace();}
}

private static String userNamePasswordBase64( String username, String
password ) {
String s = username + ":" + password;
String encs = new sun.misc.BASE64Encoder().encode(s.getBytes());
return "Basic " + encs;
}
}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Security and Java All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.