Custodian should instantiate socket at the point of connecting, secret owner parse payload

This commit is contained in:
ameba23
2021-04-16 21:17:10 +02:00
parent 00de32aa87
commit e392a0dd8b
2 changed files with 33 additions and 20 deletions

View File

@@ -31,7 +31,7 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
private Observer observer; private Observer observer;
private final ClientHelper clientHelper; private final ClientHelper clientHelper;
private InetSocketAddress remoteSocketAddress; private InetSocketAddress remoteSocketAddress;
private final Socket socket = new Socket(); private Socket socket;
private final AuthenticatedCipher cipher; private final AuthenticatedCipher cipher;
private byte[] payload; private byte[] payload;
// private final StreamReaderFactory streamReaderFactory; // private final StreamReaderFactory streamReaderFactory;
@@ -104,6 +104,7 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
observer.onStateChanged(new CustodianTask.State.SendingShard()); observer.onStateChanged(new CustodianTask.State.SendingShard());
try { try {
LOG.info("Connecting to secret owner " + remoteSocketAddress); LOG.info("Connecting to secret owner " + remoteSocketAddress);
socket = new Socket();
socket.connect(remoteSocketAddress, TIMEOUT); socket.connect(remoteSocketAddress, TIMEOUT);
LOG.info("Connected to secret owner " + remoteSocketAddress); LOG.info("Connected to secret owner " + remoteSocketAddress);
@@ -131,15 +132,18 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
observer.onStateChanged(new CustodianTask.State.ReceivingAck()); observer.onStateChanged(new CustodianTask.State.ReceivingAck());
} catch (IOException e) { } catch (IOException e) {
if (e instanceof SocketTimeoutException) { if (e instanceof SocketTimeoutException) {
LOG.warning("Timed out connecting to secret owner");
observer.onStateChanged(new CustodianTask.State.Failure( observer.onStateChanged(new CustodianTask.State.Failure(
State.Failure.Reason.NO_CONNECTION)); State.Failure.Reason.NO_CONNECTION));
return; return;
} }
LOG.warning("IO Error connecting to secret owner " + e.getMessage());
observer.onStateChanged(new CustodianTask.State.Failure( observer.onStateChanged(new CustodianTask.State.Failure(
State.Failure.Reason.QR_CODE_INVALID)); State.Failure.Reason.QR_CODE_INVALID));
return; return;
// } // }
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
LOG.warning("Security error "+ e.getMessage());
observer.onStateChanged(new CustodianTask.State.Failure( observer.onStateChanged(new CustodianTask.State.Failure(
State.Failure.Reason.OTHER)); State.Failure.Reason.OTHER));
return; return;
@@ -164,11 +168,11 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
observer.onStateChanged(new CustodianTask.State.Success()); observer.onStateChanged(new CustodianTask.State.Success());
socket.close(); socket.close();
} catch (IOException e) { } catch (IOException e) {
LOG.warning("IO Error reading ack" + e.getMessage()); LOG.warning("IO Error reading ack " + e.getMessage());
observer.onStateChanged(new CustodianTask.State.Failure( observer.onStateChanged(new CustodianTask.State.Failure(
State.Failure.Reason.QR_CODE_INVALID)); State.Failure.Reason.QR_CODE_INVALID));
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
LOG.warning("Security Error reading ack" + e.getMessage()); LOG.warning("Security Error reading ack " + e.getMessage());
observer.onStateChanged(new CustodianTask.State.Failure( observer.onStateChanged(new CustodianTask.State.Failure(
State.Failure.Reason.OTHER)); State.Failure.Reason.OTHER));
} }

View File

@@ -6,20 +6,19 @@ import org.briarproject.bramble.api.crypto.AgreementPublicKey;
import org.briarproject.bramble.api.crypto.AuthenticatedCipher; import org.briarproject.bramble.api.crypto.AuthenticatedCipher;
import org.briarproject.bramble.api.crypto.CryptoComponent; import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.data.BdfList; import org.briarproject.bramble.api.data.BdfList;
import org.briarproject.bramble.api.lifecycle.IoExecutor; import org.briarproject.briar.api.socialbackup.MessageParser;
import org.briarproject.briar.api.socialbackup.ReturnShardPayload;
import org.briarproject.briar.api.socialbackup.recovery.SecretOwnerTask; import org.briarproject.briar.api.socialbackup.recovery.SecretOwnerTask;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.util.concurrent.Executor;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.inject.Inject; import javax.inject.Inject;
@@ -35,6 +34,8 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
private Observer observer; private Observer observer;
private ServerSocket serverSocket; private ServerSocket serverSocket;
private Socket socket; private Socket socket;
private MessageParser messageParser;
// private final StreamReaderFactory streamReaderFactory; // private final StreamReaderFactory streamReaderFactory;
// private final StreamWriterFactory streamWriterFactory; // private final StreamWriterFactory streamWriterFactory;
@@ -55,7 +56,8 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
this.observer = observer; this.observer = observer;
if (inetAddress == null) { if (inetAddress == null) {
LOG.warning("Cannot retrieve local IP address, failing."); LOG.warning("Cannot retrieve local IP address, failing.");
observer.onStateChanged(new State.Failure(State.Failure.Reason.NO_CONNECTION)); observer.onStateChanged(
new State.Failure(State.Failure.Reason.NO_CONNECTION));
} }
LOG.info("InetAddress is " + inetAddress); LOG.info("InetAddress is " + inetAddress);
socketAddress = new InetSocketAddress(inetAddress, PORT); socketAddress = new InetSocketAddress(inetAddress, PORT);
@@ -69,7 +71,8 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
} catch (IOException e) { } catch (IOException e) {
LOG.warning( LOG.warning(
"IO Error when listening on local socket" + e.getMessage()); "IO Error when listening on local socket" + e.getMessage());
observer.onStateChanged(new State.Failure(State.Failure.Reason.NO_CONNECTION)); observer.onStateChanged(
new State.Failure(State.Failure.Reason.NO_CONNECTION));
// TODO could try incrementing the port number // TODO could try incrementing the port number
return; return;
} }
@@ -80,27 +83,27 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
payloadList.add(localKeyPair.getPublic().getEncoded()); payloadList.add(localKeyPair.getPublic().getEncoded());
payloadList.add(socketAddress.getAddress().getAddress()); payloadList.add(socketAddress.getAddress().getAddress());
payloadList.add(socketAddress.getPort()); payloadList.add(socketAddress.getPort());
LOG.info("changing state to listening");
observer.onStateChanged( observer.onStateChanged(
new State.Listening(clientHelper.toByteArray(payloadList))); new State.Listening(clientHelper.toByteArray(payloadList)));
LOG.info("changing state to listening done");
} catch (FormatException e) { } catch (FormatException e) {
LOG.warning("Error encoding QR code"); LOG.warning("Error encoding QR code");
observer.onStateChanged(new State.Failure(State.Failure.Reason.OTHER)); observer.onStateChanged(
new State.Failure(State.Failure.Reason.OTHER));
return; return;
} }
LOG.info("receiving payload"); LOG.info("Receiving payload");
receivePayload(); receivePayload();
} }
private void receivePayload() { private void receivePayload() {
try { try {
LOG.info("Accepting connections"); LOG.info("Waiting for a connection...");
socket = serverSocket.accept(); socket = serverSocket.accept();
LOG.info("Client connected"); LOG.info("Client connected");
observer.onStateChanged(new State.ReceivingShard()); observer.onStateChanged(new State.ReceivingShard());
DataInputStream inputStream = new DataInputStream(socket.getInputStream()); DataInputStream inputStream =
new DataInputStream(socket.getInputStream());
AgreementPublicKey remotePublicKey = AgreementPublicKey remotePublicKey =
new AgreementPublicKey( new AgreementPublicKey(
@@ -123,8 +126,10 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
// byte[] payloadClear = read(clearInputStream, payloadLength); // byte[] payloadClear = read(clearInputStream, payloadLength);
byte[] payloadClear = decrypt(payloadRaw, payloadNonce); byte[] payloadClear = decrypt(payloadRaw, payloadNonce);
ReturnShardPayload returnShardPayload = ReturnShardPayload
.fromList(clientHelper.toList(payloadClear));
LOG.info("Payload decrypted: " + new String(payloadClear)); LOG.info("Payload decrypted and parsed successfully");
// StreamWriter streamWriter = streamWriterFactory.createContactExchangeStreamWriter(socket.getOutputStream(), sharedSecret); // StreamWriter streamWriter = streamWriterFactory.createContactExchangeStreamWriter(socket.getOutputStream(), sharedSecret);
// OutputStream outputStream = streamWriter.getOutputStream(); // OutputStream outputStream = streamWriter.getOutputStream();
@@ -140,14 +145,16 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
serverSocket.close(); serverSocket.close();
observer.onStateChanged(new State.Success(payloadClear)); observer.onStateChanged(new State.Success(returnShardPayload));
} catch (IOException e) { } catch (IOException e) {
LOG.warning("IO Error receiving payload " + e.getMessage()); LOG.warning("IO Error receiving payload " + e.getMessage());
// TODO reasons // TODO reasons
observer.onStateChanged(new State.Failure(State.Failure.Reason.NO_CONNECTION)); observer.onStateChanged(
new State.Failure(State.Failure.Reason.NO_CONNECTION));
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
LOG.warning("Security Error receiving payload " + e.getMessage()); LOG.warning("Security Error receiving payload " + e.getMessage());
observer.onStateChanged(new State.Failure(State.Failure.Reason.SECURITY)); observer.onStateChanged(
new State.Failure(State.Failure.Reason.SECURITY));
} }
} }
@@ -159,12 +166,14 @@ public class SecretOwnerTaskImpl extends ReturnShardTaskImpl
try { try {
serverSocket.close(); serverSocket.close();
} catch (IOException e) { } catch (IOException e) {
observer.onStateChanged(new State.Failure(State.Failure.Reason.OTHER)); observer.onStateChanged(
new State.Failure(State.Failure.Reason.OTHER));
} }
} }
if (observer != null) { if (observer != null) {
observer.onStateChanged(new State.Failure(State.Failure.Reason.OTHER)); observer.onStateChanged(
new State.Failure(State.Failure.Reason.OTHER));
} }
} }
} }