I have to do an exercise that does the following:
Establish connection between Server and Client through SSL socket. The The first thing they do is introduce themselves and verify that they are trustworthy through certificates that I made with Keytool de java. ( One for the certificate and another for the server)
And for now I have this:
public class SSLClient {
//aquí heu de ficar les vostres dades
private static String CLAU_CLIENT = "C:\Program Files (x86)\Java\jre1.8.0_161\bin\client_ks";
private static String CLAU_CLIENT_PASSWORD = "456456";
public static void main(String[] args) throws Exception {
//Estableix la key store per validar el certificat del servidor.
System.setProperty("javax.net.ssl.trustStore", CLAU_CLIENT);
System.setProperty("javax.net.ssl.trustStorePassword", CLAU_CLIENT_PASSWORD);
try{
SSLSocketFactory sslFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket CliSocket =(SSLSocket) sslFactory.createSocket("localhost", 4043);
Scanner reader = new Scanner (System.in);
PrintStream writer = new PrintStream(CliSocket.getOutputStream());
System.out.println("Deja una linea blanca para acabar:");
String text = reader.nextLine();
while (!text.equals("")) {
writer.println(text);
writer.flush();
text = reader.nextLine();
}
writer.println("<<FI>>");
writer.flush();
CliSocket.close();
} catch (Exception ex) {
System.out.println("Error en les comuncacions: " + ex);
}
}
}
//private Socket clienteSinCertificado() throws Exception {
//}
//private Socket clienteConCertificado() throws Exception {
//}
//}
SERVER CLASS
public class SSLServer extends Thread {
private Socket socket;
public SSLServer(Socket socket) {
this.socket = socket;
}
private static String SERVIDOR_CLAU = "C:\Program Files (x86)\Java\jre1.8.0_161\bin\server_ks";
private static String SERVIDOR_CLAU_PASSWORD = "123123";
public static void main(String[] args) throws Exception {
System.setProperty("javax.net.ssl.keyStore", SERVIDOR_CLAU);
System.setProperty("javax.net.ssl.keyStorePassword", SERVIDOR_CLAU_PASSWORD);
try {
SSLServerSocketFactory sslFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket srvSocket = (SSLServerSocket) sslFactory.createServerSocket(4043);
new SSLServer(srvSocket.accept()).start();
int numClient = 1;
while (true) {
SSLSocket CliSocket = (SSLSocket) srvSocket.accept();
Scanner reader = new Scanner (CliSocket.getInputStream());
String text = reader.nextLine();
while (!text.equals("<<FIN>>")) {
System.out.println("[Client " +numClient + "] " + text);
System.out.flush();
text = reader.nextLine();
}
System.out.println("[Client " +numClient + "] cerrando conexión...");
CliSocket.close();
numClient++;
}
} catch (Exception ex) {
System.out.println("Error en las comunicaciones: " + ex);
}
}
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(socket.getOutputStream());
String data = br.readLine();
pw.println("Conectando");
pw.close();
socket.close();
} catch (IOException ioe) {
}
}
}
I get the following error:
Communicacions error: java.net.ConnectException: Connection refused: connect
java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl $ DefaultSSLContext) at javax.net.ssl.DefaultSSLServerSocketFactory.throwException (SSLServerSocketFactory.java:160) at javax.net.ssl.DefaultSSLServerSocketFactory.createServerSocket (SSLServerSocketFactory.java:173) at ioc.dam.m9.uf1.eac3.b2.SSLServer.main (SSLServer.java:53) Caused by: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl $ DefaultSSLContext) at java.security.Provider $ Service.newInstance (Provider.java:1617) at sun.security.jca.GetInstance.getInstance (GetInstance.java:236) at sun.security.jca.GetInstance.getInstance (GetInstance.java:164) at javax.net.ssl.SSLContext.getInstance (SSLContext.java:156) at javax.net.ssl.SSLContext.getDefault (SSLContext.java:96) at javax.net.ssl.SSLServerSocketFactory.getDefault (SSLServerSocketFactory.java:113) at ioc.dam.m9.uf1.eac3.b2.SSLServer.main (SSLServer.java:52) Caused by: java.security.PrivilegedActionException: java.io.FileNotFoundException: C: \ Program Files (x86) \ Java \ jre1.8.0_161 \ bin \ server_ks (The system can not find the specified route) at java.security.AccessController.doPrivileged (Native Method) at sun.security.ssl.SSLContextImpl $ DefaultManagersHolder.getKeyManagers (SSLContextImpl.java:822) at sun.security.ssl.SSLContextImpl $ DefaultManagersHolder. (SSLContextImpl.java:758) at sun.security.ssl.SSLContextImpl $ DefaultSSLContext. (SSLContextImpl.java:913) at sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance (Constructor.java:423) at java.security.Provider $ Service.newInstance (Provider.java:1595) ... 6 more Caused by: java.io.FileNotFoundException: C: \ Program Files (x86) \ Java \ jre1.8.0_161 \ bin \ server_ks (The system can not find the specified path) at java.io.FileInputStream.open0 (Native Method) at java.io.FileInputStream.open (FileInputStream.java:195) at java.io.FileInputStream. (FileInputStream.java:138) at java.io.FileInputStream. (FileInputStream.java:93) at sun.security.ssl.SSLContextImpl $ DefaultManagersHolder $ 2.run (SSLContextImpl.java:826) at sun.security.ssl.SSLContextImpl $ DefaultManagersHolder $ 2.run (SSLContextImpl.java:823) ... 15 more
I'm pretty lost with the subject and maybe I have some bad things .. but I do not know what else to do: (
thanks!