2014-12-01 17:45:37 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2014-11-08 21:37:57 +00:00
|
|
|
* Copyright (C) 2014 Open Whisper Systems
|
2011-12-20 18:20:44 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.sms;
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
import android.util.Log;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.protocol.WirePrefix;
|
2014-11-12 19:15:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Base64;
|
2014-04-21 15:40:19 +00:00
|
|
|
import org.whispersystems.libaxolotl.protocol.CiphertextMessage;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
import java.io.IOException;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2014-11-08 21:37:57 +00:00
|
|
|
public class SmsTransportDetails {
|
2014-12-01 17:45:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final int SMS_SIZE = 160;
|
|
|
|
public static final int MULTIPART_SMS_SIZE = 153;
|
2013-05-03 23:09:06 +00:00
|
|
|
|
|
|
|
public static final int BASE_MAX_BYTES = Base64.getEncodedBytesForTarget(SMS_SIZE - WirePrefix.PREFIX_SIZE);
|
|
|
|
public static final int SINGLE_MESSAGE_MAX_BYTES = BASE_MAX_BYTES - MultipartSmsTransportMessage.SINGLE_MESSAGE_MULTIPART_OVERHEAD;
|
|
|
|
public static final int MULTI_MESSAGE_MAX_BYTES = BASE_MAX_BYTES - MultipartSmsTransportMessage.MULTI_MESSAGE_MULTIPART_OVERHEAD;
|
|
|
|
public static final int FIRST_MULTI_MESSAGE_MAX_BYTES = BASE_MAX_BYTES - MultipartSmsTransportMessage.FIRST_MULTI_MESSAGE_MULTIPART_OVERHEAD;
|
|
|
|
|
2013-11-01 00:23:45 +00:00
|
|
|
public static final int ENCRYPTED_SINGLE_MESSAGE_BODY_MAX_SIZE = SINGLE_MESSAGE_MAX_BYTES - CiphertextMessage.ENCRYPTED_MESSAGE_OVERHEAD;
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2013-08-20 00:31:34 +00:00
|
|
|
public byte[] getEncodedMessage(byte[] messageWithMac) {
|
2011-12-20 18:20:44 +00:00
|
|
|
String encodedMessage = Base64.encodeBytesWithoutPadding(messageWithMac);
|
|
|
|
Log.w("SmsTransportDetails", "Encoded Message Length: " + encodedMessage.length());
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
return encodedMessage.getBytes();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2013-08-20 00:31:34 +00:00
|
|
|
public byte[] getDecodedMessage(byte[] encodedMessageBytes) throws IOException {
|
2011-12-20 18:20:44 +00:00
|
|
|
String encodedMessage = new String(encodedMessageBytes);
|
|
|
|
return Base64.decodeWithoutPadding(encodedMessage);
|
|
|
|
}
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2013-08-20 00:31:34 +00:00
|
|
|
public byte[] getStrippedPaddingMessageBody(byte[] messageWithPadding) {
|
2011-12-20 18:20:44 +00:00
|
|
|
int paddingBeginsIndex = 0;
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
for (int i=1;i<messageWithPadding.length;i++) {
|
|
|
|
if (messageWithPadding[i] == (byte)0x00) {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
paddingBeginsIndex = i;
|
|
|
|
break;
|
2013-05-03 23:09:06 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (paddingBeginsIndex == 0)
|
|
|
|
return messageWithPadding;
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
byte[] message = new byte[paddingBeginsIndex];
|
|
|
|
System.arraycopy(messageWithPadding, 0, message, 0, message.length);
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return message;
|
|
|
|
}
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public byte[] getPaddedMessageBody(byte[] messageBody) {
|
2013-05-03 23:09:06 +00:00
|
|
|
int paddedBodySize = getMaxBodySizeForBytes(messageBody.length);
|
2013-11-01 00:23:45 +00:00
|
|
|
Log.w("SmsTransportDetails", "Padding message body out to: " + paddedBodySize);
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
byte[] paddedBody = new byte[paddedBodySize];
|
|
|
|
System.arraycopy(messageBody, 0, paddedBody, 0, messageBody.length);
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return paddedBody;
|
2013-05-03 23:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private int getMaxBodySizeForBytes(int bodyLength) {
|
2013-11-01 00:23:45 +00:00
|
|
|
int encryptedBodyLength = bodyLength + CiphertextMessage.ENCRYPTED_MESSAGE_OVERHEAD;
|
2013-05-03 23:09:06 +00:00
|
|
|
int messageRecordsForBody = getMessageCountForBytes(encryptedBodyLength);
|
|
|
|
|
|
|
|
if (messageRecordsForBody == 1) {
|
|
|
|
return ENCRYPTED_SINGLE_MESSAGE_BODY_MAX_SIZE;
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
FIRST_MULTI_MESSAGE_MAX_BYTES +
|
|
|
|
(MULTI_MESSAGE_MAX_BYTES * (messageRecordsForBody-1)) -
|
2013-11-01 00:23:45 +00:00
|
|
|
CiphertextMessage.ENCRYPTED_MESSAGE_OVERHEAD;
|
2013-05-03 23:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getMessageCountForBytes(int bytes) {
|
2011-12-20 18:20:44 +00:00
|
|
|
if (bytes <= SINGLE_MESSAGE_MAX_BYTES)
|
|
|
|
return 1;
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
bytes = Math.max(bytes - FIRST_MULTI_MESSAGE_MAX_BYTES, 0);
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
int messageCount = 1 + (bytes / MULTI_MESSAGE_MAX_BYTES);
|
|
|
|
int remainder = bytes % MULTI_MESSAGE_MAX_BYTES;
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (remainder > 0)
|
|
|
|
messageCount++;
|
2013-05-03 23:09:06 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return messageCount;
|
|
|
|
}
|
|
|
|
}
|