Reputation: 11
Ok pals, so I have created a java chat using TCP client/server socket. I create the server, and then i create clients that connect to the server so that they can communicate between each other through the server! The code: Server:
public ServerChat() throws Exception{
ServerSocket soc=new ServerSocket(5217);
while(true)
{
Socket CSoc=soc.accept();
//and then the code for handling the messages, we don't need that
}
}
And the client:
Server soc=new Socket("127.0.0.1",5217);
Thread t=new Thread(this);
t.start();
//i Have ommited many parts of the code, but you know, i focus on the connection part of the problem!`
So now, I want to use SSL protection(so i can see the changes in wireshark). I used OpenSSL(it is required for my assingment) to create a root CA and device certificates.I put the files on the src folder on netbeans, and copied the code to two new classes, SSLServer and SSLClient, and experimented a bit on the SSL part! So:
public SSLServer() throws Exception{
SSLContext sslContext=????//supposed to add the files somehow here?
SSLServerSocketFactory factory=(SSLServerSocketFactory)slContext.getServerSocketFactory();
SSLServerSocket sslserversocket=(SSLServerSocket) factory.createServerSocket(1234);
while(true)
{
SSLSocket sslsocket=(SSLSocket)sslserversocket.accept();
}
}
Same thing for the client. So I am a bit stuck on the SSLContext part! I read many threads here but still.. Is the part below SSLContext correct? And how do I use the certificates in SSLContext? EDIT: Maybe this will work? :
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("keystoreFile"), "keystorePassword".toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
kmf.init(ks, "keystorePassword".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
tmf.init(ks);
SSLContext sc = SSLContext.getInstance("TLS");
TrustManager[] trustManagers = tmf.getTrustManagers();
sc.init(kmf.getKeyManagers(), trustManagers, null);
SSLServerSocketFactory ssf = sc.getServerSocketFactory();
SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(serverport);
SSLSocket c = (SSLSocket) s.accept();
Upvotes: 0
Views: 1974
Reputation: 11
anyway, i found some links that helped me! if anyone is interested,
importing an existing x509 certificate and private key in Java keystore to use in ssl
http://www.agentbob.info/agentbob/79-AB.html
Upvotes: 1