diff --git a/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadDecoderImpl.java b/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadDecoderImpl.java index 145a0ef5a..a2672016b 100644 --- a/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadDecoderImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadDecoderImpl.java @@ -10,6 +10,7 @@ import org.briarproject.bramble.api.crypto.AuthenticatedCipher; import org.briarproject.bramble.api.crypto.PrivateKey; import org.briarproject.bramble.api.crypto.PublicKey; 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.identity.Author; import org.briarproject.bramble.api.identity.Identity; @@ -25,18 +26,22 @@ import java.security.SecureRandom; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.logging.Logger; import javax.inject.Inject; 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.NONCE_BYTES; -public class BackupPayloadDecoderImpl { +public class BackupPayloadDecoderImpl implements BackupPayloadDecoder { private final ClientHelper clientHelper; private final Provider cipherProvider; private final SecureRandom secureRandom; private final MessageParser messageParser; + private static final Logger LOG = + getLogger(BackupPayloadDecoderImpl.class.getName()); @Inject BackupPayloadDecoderImpl(ClientHelper clientHelper, @@ -59,7 +64,8 @@ public class BackupPayloadDecoderImpl { System.arraycopy(ciphertextWithNonce, 0, nonce, 0, 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(); cipher.init(false, secret, nonce); @@ -68,17 +74,23 @@ public class BackupPayloadDecoderImpl { int decrypted = cipher.process(ciphertext, 0, ciphertext.length, plaintext, 0); if (decrypted != plaintext.length) throw new AssertionError(); + LOG.info("Backup payload decrypted"); BdfList backup = clientHelper.toList(plaintext); int version = backup.getLong(0).intValue(); + LOG.info("Backup payload has version number " + version); + BdfList bdfIdentity = backup.getList(1); BdfList bdfContactData = backup.getList(2); + Author a = clientHelper + .parseAndValidateAuthor(bdfIdentity.getList(0)); + PrivateKey signaturePrivateKey = + new SignaturePrivateKey(bdfIdentity.getRaw(1)); LocalAuthor localAuthor = - (LocalAuthor) clientHelper - .parseAndValidateAuthor(bdfIdentity.getList(0)); - //TODO - byte[] authorPrivateKeyBytes = bdfIdentity.getRaw(1); + new LocalAuthor(a.getId(), a.getFormatVersion(), a.getName(), + a.getPublicKey(), signaturePrivateKey); + LOG.info("LocalAuthor parsed successfully. Name is " + a.getName()); PublicKey handshakePublicKey = new AgreementPublicKey(bdfIdentity.getRaw(2)); @@ -89,6 +101,7 @@ public class BackupPayloadDecoderImpl { Identity identity = new Identity(localAuthor, handshakePublicKey, handShakePrivateKey, created); + LOG.info("New identity created"); List contactDataList = new ArrayList(); @@ -97,26 +110,41 @@ public class BackupPayloadDecoderImpl { Author author = 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 - 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 Map properties = clientHelper .parseAndValidateTransportPropertiesMap( bdfData.getDictionary(3)); + LOG.info("Contact transport properties parsed"); + // 4 shard or null - BdfList shardList = bdfData.getList(4); - Shard shard = shardList == null ? null : + BdfList shardList = bdfData.getOptionalList(4); + Shard shard = (shardList == null) ? null : messageParser.parseShardMessage(shardList); + // TODO validate shard + LOG.info("Contact shard parsed"); + ContactId contactId = new ContactId(i); Contact contact = new Contact(contactId, author, author.getId(), alias, - handshakePublicKey, false); + contactHandshakePublicKey, false); ContactData contactData = new ContactData(contact, properties, shard); contactDataList.add(contactData); + LOG.info("Contact added"); } + LOG.info("All contacts added"); return new SocialBackup(identity, contactDataList, version); } } diff --git a/briar-core/src/main/java/org/briarproject/briar/socialbackup/SocialBackupModule.java b/briar-core/src/main/java/org/briarproject/briar/socialbackup/SocialBackupModule.java index d66338dbb..0a5530e8f 100644 --- a/briar-core/src/main/java/org/briarproject/briar/socialbackup/SocialBackupModule.java +++ b/briar-core/src/main/java/org/briarproject/briar/socialbackup/SocialBackupModule.java @@ -87,17 +87,26 @@ public class SocialBackupModule { } @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; } @Provides - org.briarproject.briar.api.socialbackup.MessageParser messageParser(MessageParserImpl messageParser) { + org.briarproject.briar.api.socialbackup.MessageParser messageParser( + MessageParserImpl messageParser) { return messageParser; } @Provides - SocialBackupExchangeManager socialBackupExchangeManager(SocialBackupExchangeManagerImpl socialBackupExchangeManager) { + SocialBackupExchangeManager socialBackupExchangeManager( + SocialBackupExchangeManagerImpl socialBackupExchangeManager) { return socialBackupExchangeManager; }