server: versions now negotiated again
authorNiki Roo <niki@nikiroo.be>
Fri, 3 May 2019 15:43:38 +0000 (17:43 +0200)
committerNiki Roo <niki@nikiroo.be>
Fri, 3 May 2019 15:43:38 +0000 (17:43 +0200)
src/be/nikiroo/utils/serial/server/ConnectAction.java
src/be/nikiroo/utils/serial/server/ConnectActionClient.java
src/be/nikiroo/utils/serial/server/ConnectActionServer.java

index b9bf2240ef3cd62c6a2c6f0abc7506c6ac0cca0b..6a19368bbf97824deb624ffaf9eac238b44dfad7 100644 (file)
@@ -159,7 +159,29 @@ abstract class ConnectAction {
                        try {
                                out = new BufferedOutputStream(s.getOutputStream());
                                try {
-                                       action(server ? serverVersion : clientVersion);
+                                       // Negotiate version
+                                       Version version;
+                                       if (server) {
+                                               String HELLO = recString();
+                                               if (HELLO == null || !HELLO.startsWith("VERSION ")) {
+                                                       throw new SSLException(
+                                                                       "Client used bad encryption key");
+                                               }
+                                               version = negotiateVersion(new Version(
+                                                               HELLO.substring("VERSION ".length())));
+                                               sendString("VERSION " + version);
+                                       } else {
+                                               String HELLO = sendString("VERSION " + clientVersion);
+                                               if (HELLO == null || !HELLO.startsWith("VERSION ")) {
+                                                       throw new SSLException(
+                                                                       "Server did not accept the encryption key");
+                                               }
+                                               version = new Version(HELLO.substring("VERSION "
+                                                               .length()));
+                                       }
+
+                                       // Actual code
+                                       action(version);
                                } finally {
                                        out.close();
                                }
index f556cf64e43fb81bc5d5447891a4a5dcc708bed1..cb6bef3988c94e8f07d0306a8714ff85f3a9b1d8 100644 (file)
@@ -4,8 +4,6 @@ import java.io.IOException;
 import java.net.Socket;
 import java.net.UnknownHostException;
 
-import javax.net.ssl.SSLException;
-
 import be.nikiroo.utils.Version;
 
 /**
@@ -106,7 +104,6 @@ abstract class ConnectActionClient {
                action = new ConnectAction(s, false, key, clientVersion) {
                        @Override
                        protected void action(Version serverVersion) throws Exception {
-                               ConnectActionClient.this.clientHello();
                                ConnectActionClient.this.action(serverVersion);
                        }
 
@@ -124,25 +121,6 @@ abstract class ConnectActionClient {
                };
        }
 
-       /**
-        * Send the HELLO message (send a String "HELLO" to the server, to check I/O
-        * and encryption modes).
-        * <p>
-        * Will automatically handle the answer (the server must answer "HELLO" in
-        * kind).
-        * 
-        * @throws IOException
-        *             in case of I/O error
-        * @throws SSLException
-        *             in case of encryption error
-        */
-       protected void clientHello() throws IOException {
-               String HELLO = action.sendString("HELLO");
-               if (!"HELLO".equals(HELLO)) {
-                       throw new SSLException("Server did not accept the encryption key");
-               }
-       }
-
        /**
         * Actually start the process and call the action (synchronous).
         */
index 04bf3360512567163e021308f88f35c249e2f124..350d3fe40a1320b68f555458a43c3ceec11e8c25 100644 (file)
@@ -1,10 +1,7 @@
 package be.nikiroo.utils.serial.server;
 
-import java.io.IOException;
 import java.net.Socket;
 
-import javax.net.ssl.SSLException;
-
 import be.nikiroo.utils.Version;
 
 /**
@@ -53,7 +50,6 @@ abstract class ConnectActionServer {
                action = new ConnectAction(s, true, key, serverVersion) {
                        @Override
                        protected void action(Version clientVersion) throws Exception {
-                               ConnectActionServer.this.serverHello();
                                ConnectActionServer.this.action(clientVersion);
                        }
 
@@ -69,23 +65,6 @@ abstract class ConnectActionServer {
                };
        }
 
-       /**
-        * Send the HELLO message (check that the client sends a String "HELLO" and
-        * send it back, to check I/O and encryption modes).
-        * 
-        * @throws IOException
-        *             in case of I/O error
-        * @throws SSLException
-        *             in case of encryption error
-        */
-       protected void serverHello() throws IOException, SSLException {
-               String HELLO = action.recString();
-               if (!"HELLO".equals(HELLO)) {
-                       throw new SSLException("Server did not accept the encryption key");
-               }
-               action.sendString(HELLO);
-       }
-
        /**
         * Actually start the process and call the action (synchronous).
         */