mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 20:29:52 +01:00
Logging and improvements to BackupPayloadDecoderImpl
This commit is contained in:
@@ -10,6 +10,7 @@ import org.briarproject.bramble.api.crypto.AuthenticatedCipher;
|
|||||||
import org.briarproject.bramble.api.crypto.PrivateKey;
|
import org.briarproject.bramble.api.crypto.PrivateKey;
|
||||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
|
import org.briarproject.bramble.api.crypto.SignaturePrivateKey;
|
||||||
import org.briarproject.bramble.api.data.BdfList;
|
import org.briarproject.bramble.api.data.BdfList;
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
import org.briarproject.bramble.api.identity.Author;
|
||||||
import org.briarproject.bramble.api.identity.Identity;
|
import org.briarproject.bramble.api.identity.Identity;
|
||||||
@@ -25,18 +26,22 @@ import java.security.SecureRandom;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Provider;
|
import javax.inject.Provider;
|
||||||
|
|
||||||
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.briar.socialbackup.SocialBackupConstants.AUTH_TAG_BYTES;
|
import static org.briarproject.briar.socialbackup.SocialBackupConstants.AUTH_TAG_BYTES;
|
||||||
import static org.briarproject.briar.socialbackup.SocialBackupConstants.NONCE_BYTES;
|
import static org.briarproject.briar.socialbackup.SocialBackupConstants.NONCE_BYTES;
|
||||||
|
|
||||||
public class BackupPayloadDecoderImpl {
|
public class BackupPayloadDecoderImpl implements BackupPayloadDecoder {
|
||||||
private final ClientHelper clientHelper;
|
private final ClientHelper clientHelper;
|
||||||
private final Provider<AuthenticatedCipher> cipherProvider;
|
private final Provider<AuthenticatedCipher> cipherProvider;
|
||||||
private final SecureRandom secureRandom;
|
private final SecureRandom secureRandom;
|
||||||
private final MessageParser messageParser;
|
private final MessageParser messageParser;
|
||||||
|
private static final Logger LOG =
|
||||||
|
getLogger(BackupPayloadDecoderImpl.class.getName());
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
BackupPayloadDecoderImpl(ClientHelper clientHelper,
|
BackupPayloadDecoderImpl(ClientHelper clientHelper,
|
||||||
@@ -59,7 +64,8 @@ public class BackupPayloadDecoderImpl {
|
|||||||
System.arraycopy(ciphertextWithNonce, 0, nonce, 0, NONCE_BYTES);
|
System.arraycopy(ciphertextWithNonce, 0, nonce, 0, NONCE_BYTES);
|
||||||
|
|
||||||
byte[] ciphertext = new byte[ciphertextWithNonce.length - NONCE_BYTES];
|
byte[] ciphertext = new byte[ciphertextWithNonce.length - NONCE_BYTES];
|
||||||
System.arraycopy(ciphertextWithNonce, nonce.length, ciphertext, 0, ciphertext.length);
|
System.arraycopy(ciphertextWithNonce, nonce.length, ciphertext, 0,
|
||||||
|
ciphertext.length);
|
||||||
|
|
||||||
AuthenticatedCipher cipher = cipherProvider.get();
|
AuthenticatedCipher cipher = cipherProvider.get();
|
||||||
cipher.init(false, secret, nonce);
|
cipher.init(false, secret, nonce);
|
||||||
@@ -68,17 +74,23 @@ public class BackupPayloadDecoderImpl {
|
|||||||
int decrypted = cipher.process(ciphertext, 0,
|
int decrypted = cipher.process(ciphertext, 0,
|
||||||
ciphertext.length, plaintext, 0);
|
ciphertext.length, plaintext, 0);
|
||||||
if (decrypted != plaintext.length) throw new AssertionError();
|
if (decrypted != plaintext.length) throw new AssertionError();
|
||||||
|
LOG.info("Backup payload decrypted");
|
||||||
|
|
||||||
BdfList backup = clientHelper.toList(plaintext);
|
BdfList backup = clientHelper.toList(plaintext);
|
||||||
int version = backup.getLong(0).intValue();
|
int version = backup.getLong(0).intValue();
|
||||||
|
LOG.info("Backup payload has version number " + version);
|
||||||
|
|
||||||
BdfList bdfIdentity = backup.getList(1);
|
BdfList bdfIdentity = backup.getList(1);
|
||||||
BdfList bdfContactData = backup.getList(2);
|
BdfList bdfContactData = backup.getList(2);
|
||||||
|
|
||||||
|
Author a = clientHelper
|
||||||
|
.parseAndValidateAuthor(bdfIdentity.getList(0));
|
||||||
|
PrivateKey signaturePrivateKey =
|
||||||
|
new SignaturePrivateKey(bdfIdentity.getRaw(1));
|
||||||
LocalAuthor localAuthor =
|
LocalAuthor localAuthor =
|
||||||
(LocalAuthor) clientHelper
|
new LocalAuthor(a.getId(), a.getFormatVersion(), a.getName(),
|
||||||
.parseAndValidateAuthor(bdfIdentity.getList(0));
|
a.getPublicKey(), signaturePrivateKey);
|
||||||
//TODO
|
LOG.info("LocalAuthor parsed successfully. Name is " + a.getName());
|
||||||
byte[] authorPrivateKeyBytes = bdfIdentity.getRaw(1);
|
|
||||||
|
|
||||||
PublicKey handshakePublicKey =
|
PublicKey handshakePublicKey =
|
||||||
new AgreementPublicKey(bdfIdentity.getRaw(2));
|
new AgreementPublicKey(bdfIdentity.getRaw(2));
|
||||||
@@ -89,6 +101,7 @@ public class BackupPayloadDecoderImpl {
|
|||||||
|
|
||||||
Identity identity = new Identity(localAuthor, handshakePublicKey,
|
Identity identity = new Identity(localAuthor, handshakePublicKey,
|
||||||
handShakePrivateKey, created);
|
handShakePrivateKey, created);
|
||||||
|
LOG.info("New identity created");
|
||||||
|
|
||||||
List<ContactData> contactDataList = new ArrayList();
|
List<ContactData> contactDataList = new ArrayList();
|
||||||
|
|
||||||
@@ -97,26 +110,41 @@ public class BackupPayloadDecoderImpl {
|
|||||||
|
|
||||||
Author author =
|
Author author =
|
||||||
clientHelper.parseAndValidateAuthor(bdfData.getList(0));
|
clientHelper.parseAndValidateAuthor(bdfData.getList(0));
|
||||||
String alias = bdfData.getString(1);
|
LOG.info("Contact author parsed");
|
||||||
|
|
||||||
|
String alias = bdfData.getOptionalString(1);
|
||||||
|
LOG.info("Contact alias is: " + alias);
|
||||||
|
|
||||||
// 2 - public key or null
|
// 2 - public key or null
|
||||||
byte[] publicKeyBytes = bdfData.getRaw(2);
|
byte[] handshakePublicKeyBytes = bdfData.getOptionalRaw(2);
|
||||||
|
PublicKey contactHandshakePublicKey = (handshakePublicKeyBytes == null)
|
||||||
|
? null
|
||||||
|
: new AgreementPublicKey(handshakePublicKeyBytes);
|
||||||
|
LOG.info("Contact handshake pk parsed");
|
||||||
|
|
||||||
// 3 - properties dictionary
|
// 3 - properties dictionary
|
||||||
Map<TransportId, TransportProperties> properties = clientHelper
|
Map<TransportId, TransportProperties> properties = clientHelper
|
||||||
.parseAndValidateTransportPropertiesMap(
|
.parseAndValidateTransportPropertiesMap(
|
||||||
bdfData.getDictionary(3));
|
bdfData.getDictionary(3));
|
||||||
|
LOG.info("Contact transport properties parsed");
|
||||||
|
|
||||||
// 4 shard or null
|
// 4 shard or null
|
||||||
BdfList shardList = bdfData.getList(4);
|
BdfList shardList = bdfData.getOptionalList(4);
|
||||||
Shard shard = shardList == null ? null :
|
Shard shard = (shardList == null) ? null :
|
||||||
messageParser.parseShardMessage(shardList);
|
messageParser.parseShardMessage(shardList);
|
||||||
|
// TODO validate shard
|
||||||
|
LOG.info("Contact shard parsed");
|
||||||
|
|
||||||
ContactId contactId = new ContactId(i);
|
ContactId contactId = new ContactId(i);
|
||||||
Contact contact =
|
Contact contact =
|
||||||
new Contact(contactId, author, author.getId(), alias,
|
new Contact(contactId, author, author.getId(), alias,
|
||||||
handshakePublicKey, false);
|
contactHandshakePublicKey, false);
|
||||||
ContactData contactData =
|
ContactData contactData =
|
||||||
new ContactData(contact, properties, shard);
|
new ContactData(contact, properties, shard);
|
||||||
contactDataList.add(contactData);
|
contactDataList.add(contactData);
|
||||||
|
LOG.info("Contact added");
|
||||||
}
|
}
|
||||||
|
LOG.info("All contacts added");
|
||||||
return new SocialBackup(identity, contactDataList, version);
|
return new SocialBackup(identity, contactDataList, version);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,17 +87,26 @@ public class SocialBackupModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
org.briarproject.briar.api.socialbackup.MessageEncoder messageEncoder(MessageEncoderImpl messageEncoder) {
|
BackupPayloadDecoder backupPayloadDecoder(
|
||||||
|
BackupPayloadDecoderImpl backupPayloadDecoder) {
|
||||||
|
return backupPayloadDecoder;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
org.briarproject.briar.api.socialbackup.MessageEncoder messageEncoder(
|
||||||
|
MessageEncoderImpl messageEncoder) {
|
||||||
return messageEncoder;
|
return messageEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
org.briarproject.briar.api.socialbackup.MessageParser messageParser(MessageParserImpl messageParser) {
|
org.briarproject.briar.api.socialbackup.MessageParser messageParser(
|
||||||
|
MessageParserImpl messageParser) {
|
||||||
return messageParser;
|
return messageParser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
SocialBackupExchangeManager socialBackupExchangeManager(SocialBackupExchangeManagerImpl socialBackupExchangeManager) {
|
SocialBackupExchangeManager socialBackupExchangeManager(
|
||||||
|
SocialBackupExchangeManagerImpl socialBackupExchangeManager) {
|
||||||
return socialBackupExchangeManager;
|
return socialBackupExchangeManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user