|
|
|
@ -16,20 +16,53 @@
|
|
|
|
|
*/
|
|
|
|
|
package org.whispersystems.textsecure.push;
|
|
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import org.whispersystems.textsecure.crypto.TransportDetails;
|
|
|
|
|
import org.whispersystems.textsecure.util.Base64;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
public class PushTransportDetails implements TransportDetails {
|
|
|
|
|
|
|
|
|
|
private final int messageVersion;
|
|
|
|
|
|
|
|
|
|
public PushTransportDetails(int messageVersion) {
|
|
|
|
|
this.messageVersion = messageVersion;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public byte[] getStrippedPaddingMessageBody(byte[] messageWithPadding) {
|
|
|
|
|
return messageWithPadding;
|
|
|
|
|
if (messageVersion < 2) throw new AssertionError("Unknown version: " + messageVersion);
|
|
|
|
|
else if (messageVersion == 2) return messageWithPadding;
|
|
|
|
|
|
|
|
|
|
int paddingStart = 0;
|
|
|
|
|
|
|
|
|
|
for (int i=messageWithPadding.length-1;i>=0;i--) {
|
|
|
|
|
if (messageWithPadding[i] == (byte)0x80) {
|
|
|
|
|
paddingStart = i;
|
|
|
|
|
break;
|
|
|
|
|
} else if (messageWithPadding[i] != (byte)0x00) {
|
|
|
|
|
Log.w("PushTransportDetails", "Padding byte is malformed, returning unstripped padding.");
|
|
|
|
|
return messageWithPadding;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] strippedMessage = new byte[messageWithPadding.length - paddingStart];
|
|
|
|
|
System.arraycopy(messageWithPadding, 0, strippedMessage, 0, strippedMessage.length);
|
|
|
|
|
|
|
|
|
|
return strippedMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public byte[] getPaddedMessageBody(byte[] messageBody) {
|
|
|
|
|
return messageBody;
|
|
|
|
|
if (messageVersion < 2) throw new AssertionError("Unknown version: " + messageVersion);
|
|
|
|
|
else if (messageVersion == 2) return messageBody;
|
|
|
|
|
|
|
|
|
|
byte[] paddedMessage = new byte[getPaddedMessageLength(messageBody.length)];
|
|
|
|
|
System.arraycopy(messageBody, 0, paddedMessage, 0, messageBody.length);
|
|
|
|
|
paddedMessage[messageBody.length] = (byte)0x80;
|
|
|
|
|
|
|
|
|
|
return paddedMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -41,4 +74,15 @@ public class PushTransportDetails implements TransportDetails {
|
|
|
|
|
public byte[] getDecodedMessage(byte[] encodedMessageBytes) throws IOException {
|
|
|
|
|
return encodedMessageBytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int getPaddedMessageLength(int messageLength) {
|
|
|
|
|
int messageLengthWithTerminator = messageLength + 1;
|
|
|
|
|
int messagePartCount = messageLengthWithTerminator / 160;
|
|
|
|
|
|
|
|
|
|
if (messageLengthWithTerminator % 160 != 0) {
|
|
|
|
|
messagePartCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messagePartCount * 160;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|