Solved

problem with jsse?????

Posted on 2002-03-25
4
293 Views
Last Modified: 2010-03-31
can somebody provide a sample code for establishing a HTTPURLconnection with a server using jsse.i need to capture the sesssion id's and cookies as well.As i am working with jsse for the first time i am not sure how the to initialise the values for trust manager and key manager.I don't want a thrid party  tool.i want the SUN jsse implementation...
0
Comment
Question by:anuj_roy
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
msterjev earned 125 total points
ID: 6894853
Establishing HttpsURLConnection is the same as HttpURLConnection (with proper cast).So, the problem isn't the code. You must specify some system properties.First at all you need to specify https protocol handler class. In the Sun's implementation you must start you java interpreter with the System property:

-Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol. As always,the server will present its certificate to the client, which must verify it using its trustore. In this case, we can use the default trustore $JREHOME/lib/security/cacerts.Also, you can set system property javax.net.ssl.trustStore to your own trustore. The same is valid for keystore. The system property is javax.net.ssl.keyStore. In order to establish connection this way, the server's name in the URL must be the same with the server name in it's certificate. If you want to avoid this constraint you must write implementation of interface HostnameVerifier. You can contact me for further examples of HttpsURLConnection and row SSL socket communication.
0
 

Author Comment

by:anuj_roy
ID: 6895641
thanx for the reply ,but i have a problem when i am initialising SSLContext ,i have to pass keystore and Truststore values dynamically .i can't put it in the system properties.SO how to do that from the code.that in the constructor of SSLContext when i am passing this values i am  at present initialising it with null and passing it.is it fine or just tell me how to initialise it properly using sun's JSSE.
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6895702
private boolean initializeSSL()
     {
          String sslProvider;
          String keyStore;
          String sslVer;
          String certType;
          String storeType;
          String storePass;
          char[] storePassChars=null;
          SSLContext sslcxt;
          KeyManagerFactory kmf;
          KeyStore ks;
         
          Properties sslconfig=null;
          try
          {
               sslconfig=new Properties();
               sslconfig.load(new FileInputStream("sslconfig.properties"));
          }
          catch(Exception e)
          {
               showException(e);
               System.exit(1);
          }
          try
          {
               sslProvider=sslconfig.getProperty("sslProvider");
               if(sslProvider==null) sslProvider="SunJSSE";
               keyStore=sslconfig.getProperty("keyStore");
               if(keyStore==null) keyStore="mystore";
               sslVer=sslconfig.getProperty("sslVer");
               if(sslVer==null) sslVer="SSL";
               certType=sslconfig.getProperty("certType");
               if(certType==null) certType="SunX509";
               storeType=sslconfig.getProperty("storeType");
               if(storeType==null) storeType="JKS";
               storePass=sslconfig.getProperty("storePass");
               if(storePass!=null)
               {
                    storePassChars=new char[storePass.length()];
                    storePass.getChars(0,storePass.length(),storePassChars,0);
               }
               sslcxt=SSLContext.getInstance(sslVer,sslProvider);
               kmf=KeyManagerFactory.getInstance(certType,sslProvider);
               ks=KeyStore.getInstance(storeType);
               ks.load(new FileInputStream(keyStore),storePassChars);
               kmf.init(ks,storePassChars);
               sslcxt.init(kmf.getKeyManagers(),null,new SecureRandom());
               sslServerSocketFactory=(SSLServerSocketFactory)sslcxt.getServerSocketFactory();
               System.out.println("SSL Server Socket Factory created.");                            
               return true;          
          }catch(Exception e)
          {
               e.printStackTrace();
               return false;
          }
     }
0
 

Author Comment

by:anuj_roy
ID: 6895845
thanx a lot for the reply i will try this out...i want one more help ,i want to get the session id using the http url connection.can u tell me how to go about it????
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now