server: better handling of crypt error
[fanfix.git] / src / be / nikiroo / utils / serial / server / ConnectAction.java
index cef10adf1c80736a4603c8d99c1fe4c82710d480..be987083c5e54e3db1228f563b4ba1bf322a8918 100644 (file)
@@ -8,6 +8,7 @@ import java.net.Socket;
 
 import javax.net.ssl.SSLException;
 
+import be.nikiroo.utils.CryptUtils;
 import be.nikiroo.utils.Version;
 import be.nikiroo.utils.serial.Exporter;
 import be.nikiroo.utils.serial.Importer;
@@ -27,6 +28,8 @@ abstract class ConnectAction {
        private Version version;
        private Version clientVersion;
 
+       private CryptUtils crypt;
+
        private Object lock = new Object();
        private BufferedReader in;
        private OutputStreamWriter out;
@@ -65,7 +68,8 @@ abstract class ConnectAction {
         * Handler called when an unexpected error occurs in the code.
         * 
         * @param e
-        *            the exception that occurred
+        *            the exception that occurred, SSLException usually denotes a
+        *            crypt error
         */
        abstract protected void onError(Exception e);
 
@@ -77,12 +81,19 @@ abstract class ConnectAction {
         * @param server
         *            TRUE for a server action, FALSE for a client action (will
         *            impact the process)
+        * @param key
+        *            an optional key to encrypt all the communications (if NULL,
+        *            everything will be sent in clear text)
         * @param version
         *            the version of this client-or-server
         */
-       protected ConnectAction(Socket s, boolean server, Version version) {
+       protected ConnectAction(Socket s, boolean server, String key,
+                       Version version) {
                this.s = s;
                this.server = server;
+               if (key != null) {
+                       crypt = new CryptUtils(key);
+               }
 
                if (version == null) {
                        this.version = new Version();
@@ -131,7 +142,14 @@ abstract class ConnectAction {
                                out = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
                                try {
                                        if (server) {
-                                               String line = in.readLine();
+                                               String line;
+                                               try {
+                                                       line = readLine(in);
+                                               } catch (SSLException e) {
+                                                       out.write("Unauthorized\n");
+                                                       throw e;
+                                               }
+
                                                if (line != null && line.startsWith("VERSION ")) {
                                                        // "VERSION client-version" (VERSION 1.0.0)
                                                        Version clientVersion = new Version(
@@ -163,19 +181,6 @@ abstract class ConnectAction {
                                in = null;
                        }
                } catch (Exception e) {
-                       if (e instanceof SSLException) {
-                               String ciphers = "";
-                               for (String cipher : Server.getAnonCiphers()) {
-                                       if (!ciphers.isEmpty()) {
-                                               ciphers += ", ";
-                                       }
-                                       ciphers += cipher;
-                               }
-
-                               e = new SSLException("SSL error (available SSL ciphers: "
-                                               + ciphers + ")", e);
-                       }
-
                        onError(e);
                } finally {
                        try {
@@ -268,12 +273,12 @@ abstract class ConnectAction {
         * 
         * @throws IOException
         *             in case of I/O error
+        * @throws SSLException
+        *             in case of crypt error
         */
        protected String sendString(String line) throws IOException {
                synchronized (lock) {
-                       out.write(line);
-                       out.write("\n");
-                       bytesSent += line.length() + 1;
+                       writeLine(out, line);
 
                        if (server) {
                                out.flush();
@@ -298,6 +303,8 @@ abstract class ConnectAction {
         * 
         * @throws IOException
         *             in case of I/O error
+        * @throws SSLException
+        *             in case of crypt error
         */
        protected String recString() throws IOException {
                synchronized (lock) {
@@ -307,15 +314,62 @@ abstract class ConnectAction {
                                        contentToSend = false;
                                }
 
-                               String line = in.readLine();
-                               if (line != null) {
-                                       bytesReceived += line.length();
-                               }
-
-                               return line;
+                               return readLine(in);
                        }
 
                        return null;
                }
        }
+
+       /**
+        * Read a possibly encrypted line.
+        * 
+        * @param in
+        *            the stream to read from
+        * @return the unencrypted line
+        * 
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        * @throws SSLException
+        *             in case of crypt error
+        */
+       private String readLine(BufferedReader in) throws IOException {
+               String line = in.readLine();
+               if (line != null) {
+                       bytesReceived += line.length();
+                       if (crypt != null) {
+                               line = crypt.decrypt64s(line, false);
+                       }
+               }
+
+               return line;
+       }
+
+       /**
+        * Write a line, possible encrypted.
+        * 
+        * @param out
+        *            the stream to write to
+        * @param line
+        *            the line to write
+        * @throws IOException
+        *             in case of I/O error
+        * @throws SSLException
+        *             in case of crypt error
+        */
+       private void writeLine(OutputStreamWriter out, String line)
+                       throws IOException {
+               if (crypt == null) {
+                       out.write(line);
+                       bytesSent += line.length();
+               } else {
+                       // TODO: how NOT to create so many big Strings?
+                       String b64 = crypt.encrypt64(line, false);
+                       out.write(b64);
+                       bytesSent += b64.length();
+               }
+               out.write("\n");
+               bytesSent++;
+       }
 }
\ No newline at end of file