Search in sources :

Example 31 with ProfileKey

use of org.signal.zkgroup.profiles.ProfileKey in project Signal-Android by WhisperSystems.

the class RetrieveProfileJob method setProfileName.

private void setProfileName(Recipient recipient, String profileName) {
    try {
        ProfileKey profileKey = ProfileKeyUtil.profileKeyOrNull(recipient.getProfileKey());
        if (profileKey == null)
            return;
        String plaintextProfileName = Util.emptyIfNull(ProfileUtil.decryptString(profileKey, profileName));
        ProfileName remoteProfileName = ProfileName.fromSerialized(plaintextProfileName);
        ProfileName localProfileName = recipient.getProfileName();
        if (!remoteProfileName.equals(localProfileName)) {
            Log.i(TAG, "Profile name updated. Writing new value.");
            SignalDatabase.recipients().setProfileName(recipient.getId(), remoteProfileName);
            String remoteDisplayName = remoteProfileName.toString();
            String localDisplayName = localProfileName.toString();
            if (!recipient.isBlocked() && !recipient.isGroup() && !recipient.isSelf() && !localDisplayName.isEmpty() && !remoteDisplayName.equals(localDisplayName)) {
                Log.i(TAG, "Writing a profile name change event for " + recipient.getId());
                SignalDatabase.sms().insertProfileNameChangeMessages(recipient, remoteDisplayName, localDisplayName);
            } else {
                Log.i(TAG, String.format(Locale.US, "Name changed, but wasn't relevant to write an event. blocked: %s, group: %s, self: %s, firstSet: %s, displayChange: %s", recipient.isBlocked(), recipient.isGroup(), recipient.isSelf(), localDisplayName.isEmpty(), !remoteDisplayName.equals(localDisplayName)));
            }
        }
        if (TextUtils.isEmpty(plaintextProfileName)) {
            Log.i(TAG, "No profile name set for " + recipient.getId());
        }
    } catch (InvalidCiphertextException e) {
        Log.w(TAG, "Bad profile key for " + recipient.getId());
    } catch (IOException e) {
        Log.w(TAG, e);
    }
}
Also used : InvalidCiphertextException(org.whispersystems.signalservice.api.crypto.InvalidCiphertextException) ProfileName(org.thoughtcrime.securesms.profiles.ProfileName) IOException(java.io.IOException) ProfileKey(org.signal.zkgroup.profiles.ProfileKey)

Example 32 with ProfileKey

use of org.signal.zkgroup.profiles.ProfileKey in project Signal-Android by WhisperSystems.

the class RefreshOwnProfileJob method setProfileName.

private void setProfileName(@Nullable String encryptedName) {
    try {
        ProfileKey profileKey = ProfileKeyUtil.getSelfProfileKey();
        String plaintextName = ProfileUtil.decryptString(profileKey, encryptedName);
        ProfileName profileName = ProfileName.fromSerialized(plaintextName);
        Log.d(TAG, "Saving " + (!Util.isEmpty(plaintextName) ? "non-" : "") + "empty name.");
        SignalDatabase.recipients().setProfileName(Recipient.self().getId(), profileName);
    } catch (InvalidCiphertextException | IOException e) {
        Log.w(TAG, e);
    }
}
Also used : InvalidCiphertextException(org.whispersystems.signalservice.api.crypto.InvalidCiphertextException) ProfileName(org.thoughtcrime.securesms.profiles.ProfileName) IOException(java.io.IOException) ProfileKey(org.signal.zkgroup.profiles.ProfileKey)

Example 33 with ProfileKey

use of org.signal.zkgroup.profiles.ProfileKey in project Signal-Android by WhisperSystems.

the class RefreshOwnProfileJob method setProfileAbout.

private void setProfileAbout(@Nullable String encryptedAbout, @Nullable String encryptedEmoji) {
    try {
        ProfileKey profileKey = ProfileKeyUtil.getSelfProfileKey();
        String plaintextAbout = ProfileUtil.decryptString(profileKey, encryptedAbout);
        String plaintextEmoji = ProfileUtil.decryptString(profileKey, encryptedEmoji);
        Log.d(TAG, "Saving " + (!Util.isEmpty(plaintextAbout) ? "non-" : "") + "empty about.");
        Log.d(TAG, "Saving " + (!Util.isEmpty(plaintextEmoji) ? "non-" : "") + "empty emoji.");
        SignalDatabase.recipients().setAbout(Recipient.self().getId(), plaintextAbout, plaintextEmoji);
    } catch (InvalidCiphertextException | IOException e) {
        Log.w(TAG, e);
    }
}
Also used : InvalidCiphertextException(org.whispersystems.signalservice.api.crypto.InvalidCiphertextException) IOException(java.io.IOException) ProfileKey(org.signal.zkgroup.profiles.ProfileKey)

Example 34 with ProfileKey

use of org.signal.zkgroup.profiles.ProfileKey in project Signal-Android by WhisperSystems.

the class MultiDeviceContactUpdateJob method generateFullContactUpdate.

private void generateFullContactUpdate() throws IOException, UntrustedIdentityException, NetworkException {
    boolean isAppVisible = ApplicationDependencies.getAppForegroundObserver().isForegrounded();
    long timeSinceLastSync = System.currentTimeMillis() - TextSecurePreferences.getLastFullContactSyncTime(context);
    Log.d(TAG, "Requesting a full contact sync. forced = " + forceSync + ", appVisible = " + isAppVisible + ", timeSinceLastSync = " + timeSinceLastSync + " ms");
    if (!forceSync && !isAppVisible && timeSinceLastSync < FULL_SYNC_TIME) {
        Log.i(TAG, "App is backgrounded and the last contact sync was too soon (" + timeSinceLastSync + " ms ago). Marking that we need a sync. Skipping multi-device contact update...");
        TextSecurePreferences.setNeedsFullContactSync(context, true);
        return;
    }
    TextSecurePreferences.setLastFullContactSyncTime(context, System.currentTimeMillis());
    TextSecurePreferences.setNeedsFullContactSync(context, false);
    WriteDetails writeDetails = createTempFile();
    try {
        DeviceContactsOutputStream out = new DeviceContactsOutputStream(writeDetails.outputStream);
        List<Recipient> recipients = SignalDatabase.recipients().getRecipientsForMultiDeviceSync();
        Map<RecipientId, Integer> inboxPositions = SignalDatabase.threads().getInboxPositions();
        Set<RecipientId> archived = SignalDatabase.threads().getArchivedRecipients();
        for (Recipient recipient : recipients) {
            Optional<IdentityRecord> identity = ApplicationDependencies.getProtocolStore().aci().identities().getIdentityRecord(recipient.getId());
            Optional<VerifiedMessage> verified = getVerifiedMessage(recipient, identity);
            Optional<String> name = Optional.fromNullable(recipient.isSystemContact() ? recipient.getDisplayName(context) : recipient.getGroupName(context));
            Optional<ProfileKey> profileKey = ProfileKeyUtil.profileKeyOptional(recipient.getProfileKey());
            boolean blocked = recipient.isBlocked();
            Optional<Integer> expireTimer = recipient.getExpiresInSeconds() > 0 ? Optional.of(recipient.getExpiresInSeconds()) : Optional.absent();
            Optional<Integer> inboxPosition = Optional.fromNullable(inboxPositions.get(recipient.getId()));
            out.write(new DeviceContact(RecipientUtil.toSignalServiceAddress(context, recipient), name, getAvatar(recipient.getId(), recipient.getContactUri()), Optional.of(ChatColorsMapper.getMaterialColor(recipient.getChatColors()).serialize()), verified, profileKey, blocked, expireTimer, inboxPosition, archived.contains(recipient.getId())));
        }
        Recipient self = Recipient.self();
        byte[] profileKey = self.getProfileKey();
        if (profileKey != null) {
            out.write(new DeviceContact(RecipientUtil.toSignalServiceAddress(context, self), Optional.absent(), Optional.absent(), Optional.of(ChatColorsMapper.getMaterialColor(self.getChatColors()).serialize()), Optional.absent(), ProfileKeyUtil.profileKeyOptionalOrThrow(self.getProfileKey()), false, self.getExpiresInSeconds() > 0 ? Optional.of(self.getExpiresInSeconds()) : Optional.absent(), Optional.fromNullable(inboxPositions.get(self.getId())), archived.contains(self.getId())));
        }
        out.close();
        long length = BlobProvider.getInstance().calculateFileSize(context, writeDetails.uri);
        sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), BlobProvider.getInstance().getStream(context, writeDetails.uri), length, true);
    } catch (InvalidNumberException e) {
        Log.w(TAG, e);
    } finally {
        BlobProvider.getInstance().delete(context, writeDetails.uri);
    }
}
Also used : DeviceContact(org.whispersystems.signalservice.api.messages.multidevice.DeviceContact) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) DeviceContactsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream) InvalidNumberException(org.whispersystems.signalservice.api.util.InvalidNumberException) IdentityRecord(org.thoughtcrime.securesms.database.model.IdentityRecord) Recipient(org.thoughtcrime.securesms.recipients.Recipient) ProfileKey(org.signal.zkgroup.profiles.ProfileKey) VerifiedMessage(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage)

Example 35 with ProfileKey

use of org.signal.zkgroup.profiles.ProfileKey in project Signal-Android by WhisperSystems.

the class RotateProfileKeyJob method onRun.

@Override
public void onRun() {
    ProfileKey newProfileKey = ProfileKeyUtil.createNew();
    Recipient self = Recipient.self();
    SignalDatabase.recipients().setProfileKey(self.getId(), newProfileKey);
    ApplicationDependencies.getJobManager().add(new ProfileUploadJob());
    ApplicationDependencies.getJobManager().add(new RefreshAttributesJob());
    ApplicationDependencies.getJobManager().add(new MultiDeviceProfileKeyUpdateJob());
    updateProfileKeyOnAllV2Groups();
}
Also used : Recipient(org.thoughtcrime.securesms.recipients.Recipient) ProfileKey(org.signal.zkgroup.profiles.ProfileKey)

Aggregations

ProfileKey (org.signal.zkgroup.profiles.ProfileKey)150 Test (org.junit.Test)102 UUID (java.util.UUID)90 DecryptedGroup (org.signal.storageservice.protos.groups.local.DecryptedGroup)50 ProtoTestUtils.randomProfileKey (org.whispersystems.signalservice.api.groupsv2.ProtoTestUtils.randomProfileKey)50 DecryptedGroupChange (org.signal.storageservice.protos.groups.local.DecryptedGroupChange)34 ProtoTestUtils.newProfileKey (org.whispersystems.signalservice.api.groupsv2.ProtoTestUtils.newProfileKey)28 ProtoTestUtils.withProfileKey (org.whispersystems.signalservice.api.groupsv2.ProtoTestUtils.withProfileKey)28 IOException (java.io.IOException)24 GroupChange (org.signal.storageservice.protos.groups.GroupChange)14 DecryptedMember (org.signal.storageservice.protos.groups.local.DecryptedMember)14 InvalidCiphertextException (org.whispersystems.signalservice.api.crypto.InvalidCiphertextException)12 InvalidInputException (org.signal.zkgroup.InvalidInputException)8 ProfileKeyCredential (org.signal.zkgroup.profiles.ProfileKeyCredential)8 Recipient (org.thoughtcrime.securesms.recipients.Recipient)8 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)8 NonNull (androidx.annotation.NonNull)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 RecipientDatabase (org.thoughtcrime.securesms.database.RecipientDatabase)6 IdentityKey (org.whispersystems.libsignal.IdentityKey)6