mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Encoding and decoding for returned social backups
This commit is contained in:
@@ -30,13 +30,14 @@ class BackupPayloadEncoderImpl implements BackupPayloadEncoder {
|
||||
private final ClientHelper clientHelper;
|
||||
private final Provider<AuthenticatedCipher> cipherProvider;
|
||||
private final SecureRandom secureRandom;
|
||||
private final MessageEncoder messageEncoder;
|
||||
private final org.briarproject.briar.api.socialbackup.MessageEncoder
|
||||
messageEncoder;
|
||||
|
||||
@Inject
|
||||
BackupPayloadEncoderImpl(ClientHelper clientHelper,
|
||||
Provider<AuthenticatedCipher> cipherProvider,
|
||||
SecureRandom secureRandom,
|
||||
MessageEncoder messageEncoder) {
|
||||
org.briarproject.briar.api.socialbackup.MessageEncoder messageEncoder) {
|
||||
this.clientHelper = clientHelper;
|
||||
this.cipherProvider = cipherProvider;
|
||||
this.secureRandom = secureRandom;
|
||||
|
||||
@@ -4,17 +4,22 @@ import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.client.ClientHelper;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.api.socialbackup.BackupPayload;
|
||||
import org.briarproject.briar.api.socialbackup.ReturnShardPayload;
|
||||
import org.briarproject.briar.api.socialbackup.Shard;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import dagger.Reusable;
|
||||
|
||||
import static org.briarproject.briar.socialbackup.MessageType.BACKUP;
|
||||
import static org.briarproject.briar.socialbackup.MessageType.SHARD;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
class MessageEncoderImpl implements MessageEncoder {
|
||||
class MessageEncoderImpl implements
|
||||
org.briarproject.briar.api.socialbackup.MessageEncoder {
|
||||
|
||||
private final ClientHelper clientHelper;
|
||||
|
||||
@@ -43,6 +48,23 @@ class MessageEncoderImpl implements MessageEncoder {
|
||||
return encodeBody(body);
|
||||
}
|
||||
|
||||
public byte[] encodeReturnShardPayload(ReturnShardPayload returnShardPayload) {
|
||||
Shard shard = returnShardPayload.getShard();
|
||||
BdfList shardList = BdfList.of(
|
||||
SHARD.getValue(),
|
||||
shard.getSecretId(),
|
||||
shard.getShard()
|
||||
);
|
||||
org.briarproject.briar.api.socialbackup.BackupPayload backupPayload = returnShardPayload.getBackupPayload();
|
||||
|
||||
BdfList body = BdfList.of(
|
||||
shardList,
|
||||
returnShardPayload.getBackupPayload().getBytes()
|
||||
);
|
||||
|
||||
return encodeBody(body);
|
||||
}
|
||||
|
||||
private byte[] encodeBody(BdfList body) {
|
||||
try {
|
||||
return clientHelper.toByteArray(body);
|
||||
|
||||
@@ -3,14 +3,19 @@ package org.briarproject.briar.socialbackup;
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.api.socialbackup.BackupPayload;
|
||||
import org.briarproject.briar.api.socialbackup.ReturnShardPayload;
|
||||
import org.briarproject.briar.api.socialbackup.Shard;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.briarproject.bramble.util.ValidationUtils.checkSize;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
class MessageParserImpl implements MessageParser {
|
||||
class MessageParserImpl implements
|
||||
org.briarproject.briar.api.socialbackup.MessageParser {
|
||||
|
||||
@Inject
|
||||
MessageParserImpl() {
|
||||
@@ -30,4 +35,13 @@ class MessageParserImpl implements MessageParser {
|
||||
// Message type, backup payload
|
||||
return new org.briarproject.briar.api.socialbackup.BackupPayload(body.getRaw(1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnShardPayload parseReturnShardPayload(BdfList body)
|
||||
throws FormatException {
|
||||
checkSize(body, 2);
|
||||
Shard shard = parseShardMessage(body.getList(0));
|
||||
org.briarproject.briar.api.socialbackup.BackupPayload backupPayload = new BackupPayload(body.getRaw(1));
|
||||
return new ReturnShardPayload(shard, backupPayload);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ class SocialBackupExchangeManagerImpl implements SocialBackupExchangeManager {
|
||||
@Override
|
||||
public void sendReturnShard(DuplexTransportConnection conn,
|
||||
SecretKey masterKey,
|
||||
boolean verified) throws IOException, DbException {
|
||||
boolean verified, ReturnShardPayload returnShardPayload) throws IOException, DbException {
|
||||
boolean alice = true;
|
||||
// Get the transport connection's input and output streams
|
||||
InputStream in = conn.getReader().getInputStream();
|
||||
@@ -243,7 +243,7 @@ class SocialBackupExchangeManagerImpl implements SocialBackupExchangeManager {
|
||||
ReturnShardPayload returnShardPayload,
|
||||
long timestamp) throws IOException {
|
||||
// BdfList authorList = clientHelper.toList(author);
|
||||
BdfDictionary props = clientHelper.toDictionary(properties);
|
||||
// BdfDictionary props = clientHelper.toDictionary(properties);
|
||||
Shard shard = returnShardPayload.getShard();
|
||||
BdfList shardList = BdfList.of(shard.getSecretId(), shard.getShard());
|
||||
BdfList payload = BdfList.of(shardList,
|
||||
|
||||
@@ -45,6 +45,7 @@ import org.briarproject.briar.api.conversation.DeletionResult;
|
||||
import org.briarproject.briar.api.socialbackup.BackupExistsException;
|
||||
import org.briarproject.briar.api.socialbackup.BackupMetadata;
|
||||
import org.briarproject.briar.api.socialbackup.DarkCrystal;
|
||||
import org.briarproject.briar.api.socialbackup.ReturnShardPayload;
|
||||
import org.briarproject.briar.api.socialbackup.Shard;
|
||||
import org.briarproject.briar.api.socialbackup.BackupPayload;
|
||||
import org.briarproject.briar.api.socialbackup.ShardMessageHeader;
|
||||
@@ -86,8 +87,10 @@ class SocialBackupManagerImpl extends ConversationClientImpl
|
||||
private final BackupMetadataParser backupMetadataParser;
|
||||
private final BackupMetadataEncoder backupMetadataEncoder;
|
||||
private final BackupPayloadEncoder backupPayloadEncoder;
|
||||
private final MessageParser messageParser;
|
||||
private final MessageEncoder messageEncoder;
|
||||
private final org.briarproject.briar.api.socialbackup.MessageParser
|
||||
messageParser;
|
||||
private final org.briarproject.briar.api.socialbackup.MessageEncoder
|
||||
messageEncoder;
|
||||
private final IdentityManager identityManager;
|
||||
private final ContactManager contactManager;
|
||||
private final CryptoComponent crypto;
|
||||
@@ -106,8 +109,8 @@ class SocialBackupManagerImpl extends ConversationClientImpl
|
||||
BackupMetadataParser backupMetadataParser,
|
||||
BackupMetadataEncoder backupMetadataEncoder,
|
||||
BackupPayloadEncoder backupPayloadEncoder,
|
||||
MessageParser messageParser,
|
||||
MessageEncoder messageEncoder,
|
||||
org.briarproject.briar.api.socialbackup.MessageParser messageParser,
|
||||
org.briarproject.briar.api.socialbackup.MessageEncoder messageEncoder,
|
||||
IdentityManager identityManager,
|
||||
ContactManager contactManager,
|
||||
CryptoComponent crypto,
|
||||
@@ -231,6 +234,11 @@ class SocialBackupManagerImpl extends ConversationClientImpl
|
||||
return false;
|
||||
}
|
||||
|
||||
public ReturnShardPayload getReturnShardPayload(Transaction txn, ContactId contactId) throws DbException {
|
||||
GroupId groupId = getContactGroup(db.getContact(txn, contactId)).getId();
|
||||
return new ReturnShardPayload(getRemoteShard(txn, groupId), getRemoteBackup(txn, groupId));
|
||||
}
|
||||
|
||||
public boolean amCustodian(Transaction txn, ContactId contactId) {
|
||||
try {
|
||||
GroupId groupId = getContactGroup(db.getContact(txn, contactId)).getId();
|
||||
@@ -488,6 +496,19 @@ class SocialBackupManagerImpl extends ConversationClientImpl
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private BackupPayload getRemoteBackup(Transaction txn, GroupId g)
|
||||
throws DbException {
|
||||
try {
|
||||
Pair<MessageId, BdfDictionary> prev =
|
||||
findMessage(txn, g, BACKUP, false);
|
||||
if (prev == null) return null;
|
||||
BdfList body = clientHelper.getMessageAsList(txn, prev.getFirst());
|
||||
return messageParser.parseBackupMessage(body);
|
||||
} catch (FormatException e) {
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
private void updateBackup(Transaction txn, List<ContactData> contactData)
|
||||
throws DbException {
|
||||
BackupMetadata backupMetadata = requireNonNull(getBackupMetadata(txn));
|
||||
|
||||
@@ -82,12 +82,12 @@ public class SocialBackupModule {
|
||||
}
|
||||
|
||||
@Provides
|
||||
MessageEncoder messageEncoder(MessageEncoderImpl messageEncoder) {
|
||||
org.briarproject.briar.api.socialbackup.MessageEncoder messageEncoder(MessageEncoderImpl messageEncoder) {
|
||||
return messageEncoder;
|
||||
}
|
||||
|
||||
@Provides
|
||||
MessageParser messageParser(MessageParserImpl messageParser) {
|
||||
org.briarproject.briar.api.socialbackup.MessageParser messageParser(MessageParserImpl messageParser) {
|
||||
return messageParser;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user