diff --git a/src/main/java/org/whispersystems/textsecuregcm/entities/EncryptedOutgoingMessage.java b/src/main/java/org/whispersystems/textsecuregcm/entities/EncryptedOutgoingMessage.java index d286ac578..32a90a504 100644 --- a/src/main/java/org/whispersystems/textsecuregcm/entities/EncryptedOutgoingMessage.java +++ b/src/main/java/org/whispersystems/textsecuregcm/entities/EncryptedOutgoingMessage.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2013 Open WhisperSystems * * This program is free software: you can redistribute it and/or modify @@ -42,21 +42,15 @@ public class EncryptedOutgoingMessage { private static final int MAC_SIZE = 10; private final byte[] serialized; - private final String serializedAndEncoded; public EncryptedOutgoingMessage(Envelope outgoingMessage, String signalingKey) throws CryptoEncodingException { - byte[] plaintext = outgoingMessage.toByteArray(); - SecretKeySpec cipherKey = getCipherKey (signalingKey); - SecretKeySpec macKey = getMacKey(signalingKey); + byte[] plaintext = outgoingMessage.toByteArray(); + SecretKeySpec cipherKey = getCipherKey (signalingKey); + SecretKeySpec macKey = getMacKey(signalingKey); - this.serialized = getCiphertext(plaintext, cipherKey, macKey); - this.serializedAndEncoded = Base64.encodeBytes(this.serialized); - } - - public String toEncodedString() { - return serializedAndEncoded; + this.serialized = getCiphertext(plaintext, cipherKey, macKey); } public byte[] toByteArray() { diff --git a/src/main/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnection.java b/src/main/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnection.java index b6d9465b6..24df71ac9 100644 --- a/src/main/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnection.java +++ b/src/main/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnection.java @@ -31,6 +31,7 @@ import org.whispersystems.websocket.messages.WebSocketResponseMessage; import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.ws.rs.WebApplicationException; +import java.util.Collections; import java.util.Iterator; import java.util.Optional; @@ -111,9 +112,18 @@ public class WebSocketConnection implements DispatchChannel { final boolean requery) { try { - EncryptedOutgoingMessage encryptedMessage = new EncryptedOutgoingMessage(message, device.getSignalingKey()); - Optional body = Optional.ofNullable(encryptedMessage.toByteArray()); - ListenableFuture response = client.sendRequest("PUT", "/api/v1/message", null, body); + String header; + Optional body; + + if (Util.isEmpty(device.getSignalingKey())) { + header = "X-Signal-Key: false"; + body = Optional.ofNullable(message.toByteArray()); + } else { + header = "X-Signal-Key: true"; + body = Optional.ofNullable(new EncryptedOutgoingMessage(message, device.getSignalingKey()).toByteArray()); + } + + ListenableFuture response = client.sendRequest("PUT", "/api/v1/message", Collections.singletonList(header), body); Futures.addCallback(response, new FutureCallback() { @Override