Search in sources :

Example 1 with StorageManifest

use of org.whispersystems.signalservice.internal.storage.protos.StorageManifest in project Signal-Android by WhisperSystems.

the class SignalServiceAccountManager method getStorageManifest.

public Optional<SignalStorageManifest> getStorageManifest(StorageKey storageKey) throws IOException {
    try {
        String authToken = this.pushServiceSocket.getStorageAuth();
        StorageManifest storageManifest = this.pushServiceSocket.getStorageManifest(authToken);
        return Optional.of(SignalStorageModels.remoteToLocalStorageManifest(storageManifest, storageKey));
    } catch (InvalidKeyException | NotFoundException e) {
        Log.w(TAG, "Error while fetching manifest.", e);
        return Optional.absent();
    }
}
Also used : NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) ByteString(com.google.protobuf.ByteString) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) StorageManifest(org.whispersystems.signalservice.internal.storage.protos.StorageManifest) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest)

Example 2 with StorageManifest

use of org.whispersystems.signalservice.internal.storage.protos.StorageManifest in project Signal-Android by WhisperSystems.

the class SignalServiceAccountManager method writeStorageRecords.

/**
 * @return If there was a conflict, the latest {@link SignalStorageManifest}. Otherwise absent.
 */
private Optional<SignalStorageManifest> writeStorageRecords(StorageKey storageKey, SignalStorageManifest manifest, List<SignalStorageRecord> inserts, List<byte[]> deletes, boolean clearAll) throws IOException, InvalidKeyException {
    ManifestRecord.Builder manifestRecordBuilder = ManifestRecord.newBuilder().setVersion(manifest.getVersion());
    for (StorageId id : manifest.getStorageIds()) {
        ManifestRecord.Identifier idProto = ManifestRecord.Identifier.newBuilder().setRaw(ByteString.copyFrom(id.getRaw())).setType(ManifestRecord.Identifier.Type.forNumber(id.getType())).build();
        manifestRecordBuilder.addIdentifiers(idProto);
    }
    String authToken = this.pushServiceSocket.getStorageAuth();
    StorageManifestKey manifestKey = storageKey.deriveManifestKey(manifest.getVersion());
    byte[] encryptedRecord = SignalStorageCipher.encrypt(manifestKey, manifestRecordBuilder.build().toByteArray());
    StorageManifest storageManifest = StorageManifest.newBuilder().setVersion(manifest.getVersion()).setValue(ByteString.copyFrom(encryptedRecord)).build();
    WriteOperation.Builder writeBuilder = WriteOperation.newBuilder().setManifest(storageManifest);
    for (SignalStorageRecord insert : inserts) {
        writeBuilder.addInsertItem(SignalStorageModels.localToRemoteStorageRecord(insert, storageKey));
    }
    if (clearAll) {
        writeBuilder.setClearAll(true);
    } else {
        for (byte[] delete : deletes) {
            writeBuilder.addDeleteKey(ByteString.copyFrom(delete));
        }
    }
    Optional<StorageManifest> conflict = this.pushServiceSocket.writeStorageContacts(authToken, writeBuilder.build());
    if (conflict.isPresent()) {
        StorageManifestKey conflictKey = storageKey.deriveManifestKey(conflict.get().getVersion());
        byte[] rawManifestRecord = SignalStorageCipher.decrypt(conflictKey, conflict.get().getValue().toByteArray());
        ManifestRecord record = ManifestRecord.parseFrom(rawManifestRecord);
        List<StorageId> ids = new ArrayList<>(record.getIdentifiersCount());
        for (ManifestRecord.Identifier id : record.getIdentifiersList()) {
            ids.add(StorageId.forType(id.getRaw().toByteArray(), id.getType().getNumber()));
        }
        SignalStorageManifest conflictManifest = new SignalStorageManifest(record.getVersion(), ids);
        return Optional.of(conflictManifest);
    } else {
        return Optional.absent();
    }
}
Also used : SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest) ManifestRecord(org.whispersystems.signalservice.internal.storage.protos.ManifestRecord) StorageManifestKey(org.whispersystems.signalservice.api.storage.StorageManifestKey) ArrayList(java.util.ArrayList) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) ByteString(com.google.protobuf.ByteString) StorageId(org.whispersystems.signalservice.api.storage.StorageId) StorageManifest(org.whispersystems.signalservice.internal.storage.protos.StorageManifest) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest) WriteOperation(org.whispersystems.signalservice.internal.storage.protos.WriteOperation)

Example 3 with StorageManifest

use of org.whispersystems.signalservice.internal.storage.protos.StorageManifest in project Signal-Android by WhisperSystems.

the class SignalServiceAccountManager method getStorageManifestVersion.

public long getStorageManifestVersion() throws IOException {
    try {
        String authToken = this.pushServiceSocket.getStorageAuth();
        StorageManifest storageManifest = this.pushServiceSocket.getStorageManifest(authToken);
        return storageManifest.getVersion();
    } catch (NotFoundException e) {
        return 0;
    }
}
Also used : NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) ByteString(com.google.protobuf.ByteString) StorageManifest(org.whispersystems.signalservice.internal.storage.protos.StorageManifest) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest)

Example 4 with StorageManifest

use of org.whispersystems.signalservice.internal.storage.protos.StorageManifest in project Signal-Android by WhisperSystems.

the class SignalServiceAccountManager method getStorageManifestIfDifferentVersion.

public Optional<SignalStorageManifest> getStorageManifestIfDifferentVersion(StorageKey storageKey, long manifestVersion) throws IOException, InvalidKeyException {
    try {
        String authToken = this.pushServiceSocket.getStorageAuth();
        StorageManifest storageManifest = this.pushServiceSocket.getStorageManifestIfDifferentVersion(authToken, manifestVersion);
        if (storageManifest.getValue().isEmpty()) {
            Log.w(TAG, "Got an empty storage manifest!");
            return Optional.absent();
        }
        return Optional.of(SignalStorageModels.remoteToLocalStorageManifest(storageManifest, storageKey));
    } catch (NoContentException e) {
        return Optional.absent();
    }
}
Also used : ByteString(com.google.protobuf.ByteString) NoContentException(org.whispersystems.signalservice.api.push.exceptions.NoContentException) StorageManifest(org.whispersystems.signalservice.internal.storage.protos.StorageManifest) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest)

Example 5 with StorageManifest

use of org.whispersystems.signalservice.internal.storage.protos.StorageManifest in project Signal-Android by WhisperSystems.

the class SignalStorageManifest method deserialize.

public static SignalStorageManifest deserialize(byte[] serialized) {
    try {
        StorageManifest manifest = StorageManifest.parseFrom(serialized);
        ManifestRecord manifestRecord = ManifestRecord.parseFrom(manifest.getValue());
        List<StorageId> ids = new ArrayList<>(manifestRecord.getIdentifiersCount());
        for (ManifestRecord.Identifier id : manifestRecord.getIdentifiersList()) {
            ids.add(StorageId.forType(id.getRaw().toByteArray(), id.getTypeValue()));
        }
        return new SignalStorageManifest(manifest.getVersion(), ids);
    } catch (InvalidProtocolBufferException e) {
        throw new AssertionError(e);
    }
}
Also used : ManifestRecord(org.whispersystems.signalservice.internal.storage.protos.ManifestRecord) ArrayList(java.util.ArrayList) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) StorageManifest(org.whispersystems.signalservice.internal.storage.protos.StorageManifest)

Aggregations

StorageManifest (org.whispersystems.signalservice.internal.storage.protos.StorageManifest)5 ByteString (com.google.protobuf.ByteString)4 SignalStorageManifest (org.whispersystems.signalservice.api.storage.SignalStorageManifest)4 ArrayList (java.util.ArrayList)2 NotFoundException (org.whispersystems.signalservice.api.push.exceptions.NotFoundException)2 ManifestRecord (org.whispersystems.signalservice.internal.storage.protos.ManifestRecord)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)1 NoContentException (org.whispersystems.signalservice.api.push.exceptions.NoContentException)1 SignalStorageRecord (org.whispersystems.signalservice.api.storage.SignalStorageRecord)1 StorageId (org.whispersystems.signalservice.api.storage.StorageId)1 StorageManifestKey (org.whispersystems.signalservice.api.storage.StorageManifestKey)1 WriteOperation (org.whispersystems.signalservice.internal.storage.protos.WriteOperation)1