Make encoding/decoding more explicit.

This commit is contained in:
Moxie Marlinspike
2013-08-31 09:28:49 -07:00
parent 0cc5837d7f
commit cddba2738f
10 changed files with 52 additions and 47 deletions

View File

@@ -16,16 +16,13 @@
*/
package org.whispersystems.textsecure.crypto.protocol;
import org.whispersystems.textsecure.crypto.MessageCipher;
import org.whispersystems.textsecure.crypto.IdentityKey;
import org.whispersystems.textsecure.crypto.InvalidKeyException;
import org.whispersystems.textsecure.crypto.InvalidVersionException;
import org.whispersystems.textsecure.crypto.MessageCipher;
import org.whispersystems.textsecure.crypto.PublicKey;
import org.whispersystems.textsecure.util.Base64;
import org.whispersystems.textsecure.util.Conversions;
import java.io.IOException;
/**
* Class responsible for parsing and constructing PreKeyBundle messages.
*
@@ -53,27 +50,25 @@ public class PreKeyBundleMessage {
private final PublicKey publicKey;
private final byte[] bundledMessage;
public PreKeyBundleMessage(String message) throws InvalidKeyException, InvalidVersionException {
try {
this.messageBytes = Base64.decodeWithoutPadding(message);
this.messageVersion = Conversions.highBitsToInt(this.messageBytes[VERSION_OFFSET]);
public PreKeyBundleMessage(byte[] messageBytes)
throws InvalidKeyException, InvalidVersionException
{
this.messageBytes = messageBytes;
this.messageVersion = Conversions.highBitsToInt(this.messageBytes[VERSION_OFFSET]);
if (messageVersion > MessageCipher.SUPPORTED_VERSION)
throw new InvalidVersionException("Key exchange with version: " + messageVersion +
" but we only support: " + MessageCipher.SUPPORTED_VERSION);
if (messageVersion > MessageCipher.SUPPORTED_VERSION)
throw new InvalidVersionException("Key exchange with version: " + messageVersion +
" but we only support: " + MessageCipher.SUPPORTED_VERSION);
this.supportedVersion = Conversions.lowBitsToInt(messageBytes[VERSION_OFFSET]);
this.publicKey = new PublicKey(messageBytes, PUBLIC_KEY_OFFSET);
this.identityKey = new IdentityKey(messageBytes, IDENTITY_KEY_OFFSET);
this.preKeyId = Conversions.byteArrayToMedium(messageBytes, PREKEY_ID_OFFSET);
this.bundledMessage = new byte[messageBytes.length - IDENTITY_KEY_LENGTH];
this.supportedVersion = Conversions.lowBitsToInt(messageBytes[VERSION_OFFSET]);
this.publicKey = new PublicKey(messageBytes, PUBLIC_KEY_OFFSET);
this.identityKey = new IdentityKey(messageBytes, IDENTITY_KEY_OFFSET);
this.preKeyId = Conversions.byteArrayToMedium(messageBytes, PREKEY_ID_OFFSET);
this.bundledMessage = new byte[messageBytes.length - IDENTITY_KEY_LENGTH];
this.bundledMessage[VERSION_OFFSET] = this.messageBytes[VERSION_OFFSET];
System.arraycopy(messageBytes, IDENTITY_KEY_OFFSET+IDENTITY_KEY_LENGTH, bundledMessage, VERSION_OFFSET+VERSION_LENGTH, bundledMessage.length-VERSION_LENGTH);
} catch (IOException e) {
throw new InvalidKeyException(e);
}
this.bundledMessage[VERSION_OFFSET] = this.messageBytes[VERSION_OFFSET];
System.arraycopy(messageBytes, IDENTITY_KEY_OFFSET+IDENTITY_KEY_LENGTH, bundledMessage, VERSION_OFFSET+VERSION_LENGTH, bundledMessage.length-VERSION_LENGTH);
}
public PreKeyBundleMessage(IdentityKey identityKey, byte[] bundledMessage) {
@@ -116,8 +111,8 @@ public class PreKeyBundleMessage {
return publicKey;
}
public String getBundledMessage() {
return Base64.encodeBytesWithoutPadding(bundledMessage);
public byte[] getBundledMessage() {
return bundledMessage;
}
public int getPreKeyId() {

View File

@@ -50,6 +50,7 @@ public class IncomingPushMessage implements PushMessage, Parcelable {
this.destinations = new LinkedList<String>();
this.attachments = new LinkedList<PushAttachmentPointer>();
this.type = in.readInt();
this.source = in.readString();
in.readStringList(destinations);
this.message = new byte[in.readInt()];
@@ -70,15 +71,8 @@ public class IncomingPushMessage implements PushMessage, Parcelable {
return attachments;
}
public String getMessageText() {
if (type == TYPE_MESSAGE_CIPHERTEXT ||
type == TYPE_MESSAGE_KEY_EXCHANGE ||
type == TYPE_MESSAGE_PREKEY_BUNDLE)
{
return Base64.encodeBytesWithoutPadding(message);
}
return new String(message);
public byte[] getBody() {
return message;
}
public List<String> getDestinations() {
@@ -96,6 +90,7 @@ public class IncomingPushMessage implements PushMessage, Parcelable {
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(type);
dest.writeString(source);
dest.writeStringList(destinations);
dest.writeInt(message.length);

View File

@@ -0,0 +1,27 @@
package org.whispersystems.textsecure.push;
import org.whispersystems.textsecure.crypto.TransportDetails;
import java.io.IOException;
public class RawTransportDetails implements TransportDetails {
@Override
public byte[] getStrippedPaddingMessageBody(byte[] messageWithPadding) {
return messageWithPadding;
}
@Override
public byte[] getPaddedMessageBody(byte[] messageBody) {
return messageBody;
}
@Override
public byte[] getEncodedMessage(byte[] messageWithMac) {
return messageWithMac;
}
@Override
public byte[] getDecodedMessage(byte[] encodedMessageBytes) throws IOException {
return encodedMessageBytes;
}
}

View File

@@ -68,7 +68,13 @@ public abstract class Record {
}
private static File getAddressFile(Context context, String directory, String address) {
return new File(context.getFilesDir().getAbsolutePath() + File.separatorChar + directory, address);
File parent = new File(context.getFilesDir(), directory);
if (!parent.exists()) {
parent.mkdirs();
}
return new File(parent, address);
}
protected byte[] readBlob(FileInputStream in) throws IOException {