Skip to content

Commit

Permalink
Move Backups.proto to Wire.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Feb 14, 2023
1 parent 055ceba commit af6f16b
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.signal.core.util.StreamUtil;
import org.signal.libsignal.protocol.kdf.HKDF;
import org.signal.libsignal.protocol.util.ByteUtil;
import org.thoughtcrime.securesms.backup.proto.BackupFrame;
import org.thoughtcrime.securesms.backup.proto.Header;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -45,21 +47,21 @@ class BackupRecordInputStream extends FullBackupBase.BackupStream {
byte[] headerFrame = new byte[headerLength];
StreamUtil.readFully(in, headerFrame);

BackupProtos.BackupFrame frame = BackupProtos.BackupFrame.parseFrom(headerFrame);
BackupFrame frame = BackupFrame.ADAPTER.decode(headerFrame);

if (!frame.hasHeader()) {
if (frame.header_ == null) {
throw new IOException("Backup stream does not start with header!");
}

BackupProtos.Header header = frame.getHeader();
Header header = frame.header_;

this.iv = header.getIv().toByteArray();
this.iv = header.iv.toByteArray();

if (iv.length != 16) {
throw new IOException("Invalid IV length!");
}

byte[] key = getBackupKey(passphrase, header.hasSalt() ? header.getSalt().toByteArray() : null);
byte[] key = getBackupKey(passphrase, header.salt != null ? header.salt.toByteArray() : null);
byte[] derived = HKDF.deriveSecrets(key, "Backup Export".getBytes(), 64);
byte[][] split = ByteUtil.split(derived, 32, 32);

Expand All @@ -76,7 +78,7 @@ class BackupRecordInputStream extends FullBackupBase.BackupStream {
}
}

BackupProtos.BackupFrame readFrame() throws IOException {
BackupFrame readFrame() throws IOException {
return readFrame(in);
}

Expand Down Expand Up @@ -128,7 +130,7 @@ void readAttachmentTo(OutputStream out, int length) throws IOException {
}
}

private BackupProtos.BackupFrame readFrame(InputStream in) throws IOException {
private BackupFrame readFrame(InputStream in) throws IOException {
try {
byte[] length = new byte[4];
StreamUtil.readFully(in, length);
Expand All @@ -151,7 +153,7 @@ private BackupProtos.BackupFrame readFrame(InputStream in) throws IOException {

byte[] plaintext = cipher.doFinal(frame, 0, frame.length - 10);

return BackupProtos.BackupFrame.parseFrom(plaintext);
return BackupFrame.ADAPTER.decode(plaintext);
} catch (InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
throw new AssertionError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package org.thoughtcrime.securesms.backup

import org.greenrobot.eventbus.EventBus
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.backup.BackupProtos.BackupFrame
import org.thoughtcrime.securesms.backup.proto.Attachment
import org.thoughtcrime.securesms.backup.proto.Avatar
import org.thoughtcrime.securesms.backup.proto.BackupFrame
import org.thoughtcrime.securesms.backup.proto.Sticker
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
Expand All @@ -23,11 +26,11 @@ object BackupVerifier {
var frame: BackupFrame = inputStream.readFrame()

cipherStream.use {
while (!frame.end && !cancellationSignal.isCanceled) {
while (frame.end != true && !cancellationSignal.isCanceled) {
val verified = when {
frame.hasAttachment() -> verifyAttachment(frame.attachment, inputStream)
frame.hasSticker() -> verifySticker(frame.sticker, inputStream)
frame.hasAvatar() -> verifyAvatar(frame.avatar, inputStream)
frame.attachment != null -> verifyAttachment(frame.attachment!!, inputStream)
frame.sticker != null -> verifySticker(frame.sticker!!, inputStream)
frame.avatar != null -> verifyAvatar(frame.avatar!!, inputStream)
else -> true
}

Expand All @@ -48,9 +51,9 @@ object BackupVerifier {
return true
}

private fun verifyAttachment(attachment: BackupProtos.Attachment, inputStream: BackupRecordInputStream): Boolean {
private fun verifyAttachment(attachment: Attachment, inputStream: BackupRecordInputStream): Boolean {
try {
inputStream.readAttachmentTo(NullOutputStream, attachment.length)
inputStream.readAttachmentTo(NullOutputStream, attachment.length ?: 0)
} catch (e: IOException) {
Log.w(TAG, "Bad attachment id: ${attachment.attachmentId} len: ${attachment.length}", e)
return false
Expand All @@ -59,19 +62,19 @@ object BackupVerifier {
return true
}

private fun verifySticker(sticker: BackupProtos.Sticker, inputStream: BackupRecordInputStream): Boolean {
private fun verifySticker(sticker: Sticker, inputStream: BackupRecordInputStream): Boolean {
try {
inputStream.readAttachmentTo(NullOutputStream, sticker.length)
inputStream.readAttachmentTo(NullOutputStream, sticker.length ?: 0)
} catch (e: IOException) {
Log.w(TAG, "Bad sticker id: ${sticker.rowId} len: ${sticker.length}", e)
return false
}
return true
}

private fun verifyAvatar(avatar: BackupProtos.Avatar, inputStream: BackupRecordInputStream): Boolean {
private fun verifyAvatar(avatar: Avatar, inputStream: BackupRecordInputStream): Boolean {
try {
inputStream.readAttachmentTo(NullOutputStream, avatar.length)
inputStream.readAttachmentTo(NullOutputStream, avatar.length ?: 0)
} catch (e: IOException) {
Log.w(TAG, "Bad avatar id: ${avatar.recipientId} len: ${avatar.length}", e)
return false
Expand Down

0 comments on commit af6f16b

Please sign in to comment.