Search in sources :

Example 61 with SignalProtocolAddress

use of org.whispersystems.libsignal.SignalProtocolAddress in project Pix-Art-Messenger by kriztan.

the class AxolotlService method findSessionsForContact.

public Collection<XmppAxolotlSession> findSessionsForContact(Contact contact) {
    SignalProtocolAddress contactAddress = getAddressForJid(contact.getJid());
    ArrayList<XmppAxolotlSession> s = new ArrayList<>(this.sessions.getAll(contactAddress.getName()).values());
    Collections.sort(s);
    return s;
}
Also used : ArrayList(java.util.ArrayList) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Example 62 with SignalProtocolAddress

use of org.whispersystems.libsignal.SignalProtocolAddress in project Pix-Art-Messenger by kriztan.

the class DatabaseBackend method onUpgrade.

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 2 && newVersion >= 2) {
        db.execSQL("update " + Account.TABLENAME + " set " + Account.OPTIONS + " = " + Account.OPTIONS + " | 8");
    }
    if (oldVersion < 3 && newVersion >= 3) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.TYPE + " NUMBER");
    }
    if (oldVersion < 5 && newVersion >= 5) {
        db.execSQL("DROP TABLE " + Contact.TABLENAME);
        db.execSQL(CREATE_CONTATCS_STATEMENT);
        db.execSQL("UPDATE " + Account.TABLENAME + " SET " + Account.ROSTERVERSION + " = NULL");
    }
    if (oldVersion < 6 && newVersion >= 6) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.TRUE_COUNTERPART + " TEXT");
    }
    if (oldVersion < 7 && newVersion >= 7) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.REMOTE_MSG_ID + " TEXT");
        db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.AVATAR + " TEXT");
        db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.AVATAR + " TEXT");
    }
    if (oldVersion < 8 && newVersion >= 8) {
        db.execSQL("ALTER TABLE " + Conversation.TABLENAME + " ADD COLUMN " + Conversation.ATTRIBUTES + " TEXT");
    }
    if (oldVersion < 9 && newVersion >= 9) {
        db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.LAST_TIME + " NUMBER");
        db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.LAST_PRESENCE + " TEXT");
    }
    if (oldVersion < 10 && newVersion >= 10) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.RELATIVE_FILE_PATH + " TEXT");
    }
    if (oldVersion < 11 && newVersion >= 11) {
        db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.GROUPS + " TEXT");
        db.execSQL("delete from " + Contact.TABLENAME);
        db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
    }
    if (oldVersion < 12 && newVersion >= 12) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.SERVER_MSG_ID + " TEXT");
    }
    if (oldVersion < 13 && newVersion >= 13) {
        db.execSQL("delete from " + Contact.TABLENAME);
        db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
    }
    if (oldVersion < 14 && newVersion >= 14) {
        canonicalizeJids(db);
    }
    if (oldVersion < 15 && newVersion >= 15) {
        recreateAxolotlDb(db);
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.FINGERPRINT + " TEXT");
    }
    if (oldVersion < 16 && newVersion >= 16) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.CARBON + " INTEGER");
    }
    if (oldVersion < 19 && newVersion >= 19) {
        db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.DISPLAY_NAME + " TEXT");
    }
    if (oldVersion < 20 && newVersion >= 20) {
        db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.HOSTNAME + " TEXT");
        db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.PORT + " NUMBER DEFAULT 5222");
    }
    if (oldVersion < 26 && newVersion >= 26) {
        db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.STATUS + " TEXT");
        db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.STATUS_MESSAGE + " TEXT");
    }
    if (oldVersion < 41 && newVersion >= 41) {
        db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.RESOURCE + " TEXT");
    }
    /* Any migrations that alter the Account table need to happen BEFORE this migration, as it
		 * depends on account de-serialization.
		 */
    if (oldVersion < 17 && newVersion >= 17 && newVersion < 31) {
        List<Account> accounts = getAccounts(db);
        for (Account account : accounts) {
            String ownDeviceIdString = account.getKey(SQLiteAxolotlStore.JSONKEY_REGISTRATION_ID);
            if (ownDeviceIdString == null) {
                continue;
            }
            int ownDeviceId = Integer.valueOf(ownDeviceIdString);
            SignalProtocolAddress ownAddress = new SignalProtocolAddress(account.getJid().toBareJid().toPreppedString(), ownDeviceId);
            deleteSession(db, account, ownAddress);
            IdentityKeyPair identityKeyPair = loadOwnIdentityKeyPair(db, account);
            if (identityKeyPair != null) {
                String[] selectionArgs = { account.getUuid(), CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize()) };
                ContentValues values = new ContentValues();
                values.put(SQLiteAxolotlStore.TRUSTED, 2);
                db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values, SQLiteAxolotlStore.ACCOUNT + " = ? AND " + SQLiteAxolotlStore.FINGERPRINT + " = ? ", selectionArgs);
            } else {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not load own identity key pair");
            }
        }
    }
    if (oldVersion < 18 && newVersion >= 18) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.READ + " NUMBER DEFAULT 1");
    }
    if (oldVersion < 21 && newVersion >= 21) {
        List<Account> accounts = getAccounts(db);
        for (Account account : accounts) {
            account.unsetPgpSignature();
            db.update(Account.TABLENAME, account.getContentValues(), Account.UUID + "=?", new String[] { account.getUuid() });
        }
    }
    if (oldVersion >= 15 && oldVersion < 22 && newVersion >= 22) {
        db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.CERTIFICATE);
    }
    if (oldVersion < 23 && newVersion >= 23) {
        db.execSQL(CREATE_DISCOVERY_RESULTS_STATEMENT);
    }
    if (oldVersion < 24 && newVersion >= 24) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.EDITED + " TEXT");
    }
    if (oldVersion < 25 && newVersion >= 25) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.OOB + " INTEGER");
    }
    if (oldVersion < 26 && newVersion >= 26) {
        db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
    }
    if (oldVersion < 27 && newVersion >= 27) {
        db.execSQL("DELETE FROM " + ServiceDiscoveryResult.TABLENAME);
    }
    if (oldVersion < 28 && newVersion >= 28) {
        canonicalizeJids(db);
    }
    if (oldVersion < 29 && newVersion >= 29) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.ERROR_MESSAGE + " TEXT");
    }
    if (oldVersion >= 15 && oldVersion < 31 && newVersion >= 31) {
        db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.TRUST + " TEXT");
        db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.ACTIVE + " NUMBER");
        HashMap<Integer, ContentValues> migration = new HashMap<>();
        migration.put(0, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
        migration.put(1, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
        migration.put(2, createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, true));
        migration.put(3, createFingerprintStatusContentValues(FingerprintStatus.Trust.COMPROMISED, false));
        migration.put(4, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
        migration.put(5, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
        migration.put(6, createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, false));
        migration.put(7, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED_X509, true));
        migration.put(8, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED_X509, false));
        for (Map.Entry<Integer, ContentValues> entry : migration.entrySet()) {
            String whereClause = SQLiteAxolotlStore.TRUSTED + "=?";
            String[] where = { String.valueOf(entry.getKey()) };
            db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, entry.getValue(), whereClause, where);
        }
    }
    if (oldVersion >= 15 && oldVersion < 32 && newVersion >= 32) {
        db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.LAST_ACTIVATION + " NUMBER");
        ContentValues defaults = new ContentValues();
        defaults.put(SQLiteAxolotlStore.LAST_ACTIVATION, System.currentTimeMillis());
        db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, defaults, null, null);
    }
    if (oldVersion >= 15 && oldVersion < 33 && newVersion >= 33) {
        String whereClause = SQLiteAxolotlStore.OWN + "=1";
        db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED, true), whereClause, null);
    }
    if (oldVersion < 34 && newVersion >= 34) {
        db.execSQL(CREATE_MESSAGE_TIME_INDEX);
    // do nothing else at this point because we have seperated videos, images, audios and other files in different directories
    }
    if (oldVersion < 35 && newVersion >= 35) {
        db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
    }
    if (oldVersion < 36 && newVersion >= 36) {
        // only rename videos, images, audios and other files directories
        final File oldPicturesDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pix-Art Messenger/Images/");
        final File oldFilesDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pix-Art Messenger/Files/");
        final File oldAudiosDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pix-Art Messenger/Audios/");
        final File oldVideosDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pix-Art Messenger/Videos/");
        if (oldPicturesDirectory.exists() && oldPicturesDirectory.isDirectory()) {
            final File newPicturesDirectory = new File(Environment.getExternalStorageDirectory() + "/Pix-Art Messenger/Media/Pix-Art Messenger Images/");
            newPicturesDirectory.getParentFile().mkdirs();
            final File[] files = oldPicturesDirectory.listFiles();
            if (files == null) {
                return;
            }
            if (oldPicturesDirectory.renameTo(newPicturesDirectory)) {
                Log.d(Config.LOGTAG, "moved " + oldPicturesDirectory.getAbsolutePath() + " to " + newPicturesDirectory.getAbsolutePath());
            }
        }
        if (oldFilesDirectory.exists() && oldFilesDirectory.isDirectory()) {
            final File newFilesDirectory = new File(Environment.getExternalStorageDirectory() + "/Pix-Art Messenger/Media/Pix-Art Messenger Files/");
            newFilesDirectory.mkdirs();
            final File[] files = oldFilesDirectory.listFiles();
            if (files == null) {
                return;
            }
            if (oldFilesDirectory.renameTo(newFilesDirectory)) {
                Log.d(Config.LOGTAG, "moved " + oldFilesDirectory.getAbsolutePath() + " to " + newFilesDirectory.getAbsolutePath());
            }
        }
        if (oldAudiosDirectory.exists() && oldAudiosDirectory.isDirectory()) {
            final File newAudiosDirectory = new File(Environment.getExternalStorageDirectory() + "/Pix-Art Messenger/Media/Pix-Art Messenger Audios/");
            newAudiosDirectory.mkdirs();
            final File[] files = oldAudiosDirectory.listFiles();
            if (files == null) {
                return;
            }
            if (oldAudiosDirectory.renameTo(newAudiosDirectory)) {
                Log.d(Config.LOGTAG, "moved " + oldAudiosDirectory.getAbsolutePath() + " to " + newAudiosDirectory.getAbsolutePath());
            }
        }
        if (oldVideosDirectory.exists() && oldVideosDirectory.isDirectory()) {
            final File newVideosDirectory = new File(Environment.getExternalStorageDirectory() + "/Pix-Art Messenger/Media/Pix-Art Messenger Videos/");
            newVideosDirectory.mkdirs();
            final File[] files = oldVideosDirectory.listFiles();
            if (files == null) {
                return;
            }
            if (oldVideosDirectory.renameTo(newVideosDirectory)) {
                Log.d(Config.LOGTAG, "moved " + oldVideosDirectory.getAbsolutePath() + " to " + newVideosDirectory.getAbsolutePath());
            }
        }
    }
    if (oldVersion < 37 && newVersion >= 37) {
        List<Account> accounts = getAccounts(db);
        for (Account account : accounts) {
            account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, true);
            account.setOption(Account.OPTION_LOGGED_IN_SUCCESSFULLY, false);
            db.update(Account.TABLENAME, account.getContentValues(), Account.UUID + "=?", new String[] { account.getUuid() });
        }
    }
    if (oldVersion < 38 && newVersion >= 38) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.READ_BY_MARKERS + " TEXT");
    }
    if (oldVersion < 39 && newVersion >= 39) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.MARKABLE + " NUMBER DEFAULT 0");
    }
    if (oldVersion < 40 && newVersion >= 40) {
        db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
    }
}
Also used : ContentValues(android.content.ContentValues) Account(de.pixart.messenger.entities.Account) HashMap(java.util.HashMap) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Example 63 with SignalProtocolAddress

use of org.whispersystems.libsignal.SignalProtocolAddress in project Signal-Android by signalapp.

the class SignalServiceMessageSender method getEncryptedMessages.

private OutgoingPushMessageList getEncryptedMessages(PushServiceSocket socket, SignalServiceAddress recipient, Optional<UnidentifiedAccess> unidentifiedAccess, long timestamp, EnvelopeContent plaintext, boolean online) throws IOException, InvalidKeyException, UntrustedIdentityException {
    List<OutgoingPushMessage> messages = new LinkedList<>();
    List<Integer> subDevices = store.getSubDeviceSessions(recipient.getIdentifier());
    List<Integer> deviceIds = new ArrayList<>(subDevices.size() + 1);
    deviceIds.add(SignalServiceAddress.DEFAULT_DEVICE_ID);
    deviceIds.addAll(subDevices);
    if (recipient.matches(localAddress)) {
        deviceIds.remove(Integer.valueOf(localDeviceId));
    }
    for (int deviceId : deviceIds) {
        if (deviceId == SignalServiceAddress.DEFAULT_DEVICE_ID || store.containsSession(new SignalProtocolAddress(recipient.getIdentifier(), deviceId))) {
            messages.add(getEncryptedMessage(socket, recipient, unidentifiedAccess, deviceId, plaintext));
        }
    }
    return new OutgoingPushMessageList(recipient.getIdentifier(), timestamp, messages, online);
}
Also used : OutgoingPushMessageList(org.whispersystems.signalservice.internal.push.OutgoingPushMessageList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ContentHint(org.whispersystems.signalservice.api.crypto.ContentHint) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) OutgoingPushMessage(org.whispersystems.signalservice.internal.push.OutgoingPushMessage)

Example 64 with SignalProtocolAddress

use of org.whispersystems.libsignal.SignalProtocolAddress in project Signal-Android by signalapp.

the class SignalServiceMessageSender method handleMismatchedDevices.

private void handleMismatchedDevices(PushServiceSocket socket, SignalServiceAddress recipient, MismatchedDevices mismatchedDevices) throws IOException, UntrustedIdentityException {
    try {
        Log.w(TAG, "[handleMismatchedDevices] Address: " + recipient.getIdentifier() + ", ExtraDevices: " + mismatchedDevices.getExtraDevices() + ", MissingDevices: " + mismatchedDevices.getMissingDevices());
        archiveSessions(recipient, mismatchedDevices.getExtraDevices());
        for (int missingDeviceId : mismatchedDevices.getMissingDevices()) {
            PreKeyBundle preKey = socket.getPreKey(recipient, missingDeviceId);
            try {
                SignalSessionBuilder sessionBuilder = new SignalSessionBuilder(sessionLock, new SessionBuilder(store, new SignalProtocolAddress(recipient.getIdentifier(), missingDeviceId)));
                sessionBuilder.process(preKey);
            } catch (org.whispersystems.libsignal.UntrustedIdentityException e) {
                throw new UntrustedIdentityException("Untrusted identity key!", recipient.getIdentifier(), preKey.getIdentityKey());
            }
        }
    } catch (InvalidKeyException e) {
        throw new IOException(e);
    }
}
Also used : PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) SignalSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalSessionBuilder) UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) SignalGroupSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalGroupSessionBuilder) GroupSessionBuilder(org.whispersystems.libsignal.groups.GroupSessionBuilder) SessionBuilder(org.whispersystems.libsignal.SessionBuilder) SignalSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalSessionBuilder) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) ContentHint(org.whispersystems.signalservice.api.crypto.ContentHint) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Example 65 with SignalProtocolAddress

use of org.whispersystems.libsignal.SignalProtocolAddress in project Signal-Android by signalapp.

the class SignalServiceMessageSender method sendGroupMessage.

/**
 * Will send a message using sender keys to all of the specified recipients. It is assumed that
 * all of the recipients have UUIDs.
 *
 * This method will handle sending out SenderKeyDistributionMessages as necessary.
 */
private List<SendMessageResult> sendGroupMessage(DistributionId distributionId, List<SignalServiceAddress> recipients, List<UnidentifiedAccess> unidentifiedAccess, long timestamp, Content content, ContentHint contentHint, byte[] groupId, boolean online, SenderKeyGroupEvents sendEvents) throws IOException, UntrustedIdentityException, NoSessionException, InvalidKeyException, InvalidRegistrationIdException {
    if (recipients.isEmpty()) {
        Log.w(TAG, "[sendGroupMessage][" + timestamp + "] Empty recipient list!");
        return Collections.emptyList();
    }
    Preconditions.checkArgument(recipients.size() == unidentifiedAccess.size(), "[" + timestamp + "] Unidentified access mismatch!");
    Map<ServiceId, UnidentifiedAccess> accessBySid = new HashMap<>();
    Iterator<SignalServiceAddress> addressIterator = recipients.iterator();
    Iterator<UnidentifiedAccess> accessIterator = unidentifiedAccess.iterator();
    while (addressIterator.hasNext()) {
        accessBySid.put(addressIterator.next().getServiceId(), accessIterator.next());
    }
    for (int i = 0; i < RETRY_COUNT; i++) {
        GroupTargetInfo targetInfo = buildGroupTargetInfo(recipients);
        Set<SignalProtocolAddress> sharedWith = store.getSenderKeySharedWith(distributionId);
        List<SignalServiceAddress> needsSenderKey = targetInfo.destinations.stream().filter(a -> !sharedWith.contains(a)).map(a -> ServiceId.parseOrThrow(a.getName())).distinct().map(SignalServiceAddress::new).collect(Collectors.toList());
        if (needsSenderKey.size() > 0) {
            Log.i(TAG, "[sendGroupMessage][" + timestamp + "] Need to send the distribution message to " + needsSenderKey.size() + " addresses.");
            SenderKeyDistributionMessage message = getOrCreateNewGroupSession(distributionId);
            List<Optional<UnidentifiedAccessPair>> access = needsSenderKey.stream().map(r -> {
                UnidentifiedAccess targetAccess = accessBySid.get(r.getServiceId());
                return Optional.of(new UnidentifiedAccessPair(targetAccess, targetAccess));
            }).collect(Collectors.toList());
            List<SendMessageResult> results = sendSenderKeyDistributionMessage(distributionId, needsSenderKey, access, message, groupId);
            List<SignalServiceAddress> successes = results.stream().filter(SendMessageResult::isSuccess).map(SendMessageResult::getAddress).collect(Collectors.toList());
            Set<String> successSids = successes.stream().map(a -> a.getServiceId().toString()).collect(Collectors.toSet());
            Set<SignalProtocolAddress> successAddresses = targetInfo.destinations.stream().filter(a -> successSids.contains(a.getName())).collect(Collectors.toSet());
            store.markSenderKeySharedWith(distributionId, successAddresses);
            Log.i(TAG, "[sendGroupMessage][" + timestamp + "] Successfully sent sender keys to " + successes.size() + "/" + needsSenderKey.size() + " recipients.");
            int failureCount = results.size() - successes.size();
            if (failureCount > 0) {
                Log.w(TAG, "[sendGroupMessage][" + timestamp + "] Failed to send sender keys to " + failureCount + " recipients. Sending back failed results now.");
                List<SendMessageResult> trueFailures = results.stream().filter(r -> !r.isSuccess()).collect(Collectors.toList());
                Set<ServiceId> failedAddresses = trueFailures.stream().map(result -> result.getAddress().getServiceId()).collect(Collectors.toSet());
                List<SendMessageResult> fakeNetworkFailures = recipients.stream().filter(r -> !failedAddresses.contains(r.getServiceId())).map(SendMessageResult::networkFailure).collect(Collectors.toList());
                List<SendMessageResult> modifiedResults = new LinkedList<>();
                modifiedResults.addAll(trueFailures);
                modifiedResults.addAll(fakeNetworkFailures);
                return modifiedResults;
            } else {
                targetInfo = buildGroupTargetInfo(recipients);
            }
        }
        sendEvents.onSenderKeyShared();
        SignalServiceCipher cipher = new SignalServiceCipher(localAddress, localDeviceId, store, sessionLock, null);
        SenderCertificate senderCertificate = unidentifiedAccess.get(0).getUnidentifiedCertificate();
        byte[] ciphertext;
        try {
            ciphertext = cipher.encryptForGroup(distributionId, targetInfo.destinations, senderCertificate, content.toByteArray(), contentHint, groupId);
        } catch (org.whispersystems.libsignal.UntrustedIdentityException e) {
            throw new UntrustedIdentityException("Untrusted during group encrypt", e.getName(), e.getUntrustedIdentity());
        }
        sendEvents.onMessageEncrypted();
        byte[] joinedUnidentifiedAccess = new byte[16];
        for (UnidentifiedAccess access : unidentifiedAccess) {
            joinedUnidentifiedAccess = ByteArrayUtil.xor(joinedUnidentifiedAccess, access.getUnidentifiedAccessKey());
        }
        try {
            try {
                SendGroupMessageResponse response = new MessagingService.SendResponseProcessor<>(messagingService.sendToGroup(ciphertext, joinedUnidentifiedAccess, timestamp, online).blockingGet()).getResultOrThrow();
                return transformGroupResponseToMessageResults(targetInfo.devices, response, content);
            } catch (InvalidUnidentifiedAccessHeaderException | NotFoundException | GroupMismatchedDevicesException | GroupStaleDevicesException e) {
                // Non-technical failures shouldn't be retried with socket
                throw e;
            } catch (WebSocketUnavailableException e) {
                Log.i(TAG, "[sendGroupMessage][" + timestamp + "] Pipe unavailable, falling back... (" + e.getClass().getSimpleName() + ": " + e.getMessage() + ")");
            } catch (IOException e) {
                Log.w(TAG, "[sendGroupMessage][" + timestamp + "] Pipe failed, falling back... (" + e.getClass().getSimpleName() + ": " + e.getMessage() + ")");
            }
            SendGroupMessageResponse response = socket.sendGroupMessage(ciphertext, joinedUnidentifiedAccess, timestamp, online);
            return transformGroupResponseToMessageResults(targetInfo.devices, response, content);
        } catch (GroupMismatchedDevicesException e) {
            Log.w(TAG, "[sendGroupMessage][" + timestamp + "] Handling mismatched devices. (" + e.getMessage() + ")");
            for (GroupMismatchedDevices mismatched : e.getMismatchedDevices()) {
                SignalServiceAddress address = new SignalServiceAddress(ACI.parseOrThrow(mismatched.getUuid()), Optional.absent());
                handleMismatchedDevices(socket, address, mismatched.getDevices());
            }
        } catch (GroupStaleDevicesException e) {
            Log.w(TAG, "[sendGroupMessage][" + timestamp + "] Handling stale devices. (" + e.getMessage() + ")");
            for (GroupStaleDevices stale : e.getStaleDevices()) {
                SignalServiceAddress address = new SignalServiceAddress(ACI.parseOrThrow(stale.getUuid()), Optional.absent());
                handleStaleDevices(address, stale.getDevices());
            }
        }
        Log.w(TAG, "[sendGroupMessage][" + timestamp + "] Attempt failed (i = " + i + ")");
    }
    throw new IOException("Failed to resolve conflicts after " + RETRY_COUNT + " attempts!");
}
Also used : ServerRejectedException(org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException) GroupContext(org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContext) CallingResponse(org.whispersystems.signalservice.api.messages.calls.CallingResponse) StickerPackOperationMessage(org.whispersystems.signalservice.api.messages.multidevice.StickerPackOperationMessage) TypingMessage(org.whispersystems.signalservice.internal.push.SignalServiceProtos.TypingMessage) PaddingInputStream(org.whispersystems.signalservice.internal.crypto.PaddingInputStream) DataMessage(org.whispersystems.signalservice.internal.push.SignalServiceProtos.DataMessage) ReceiptMessage(org.whispersystems.signalservice.internal.push.SignalServiceProtos.ReceiptMessage) SecureRandom(java.security.SecureRandom) Future(java.util.concurrent.Future) Preconditions(org.whispersystems.libsignal.util.guava.Preconditions) SignalGroupSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalGroupSessionBuilder) GroupMismatchedDevicesException(org.whispersystems.signalservice.internal.push.exceptions.GroupMismatchedDevicesException) SenderCertificate(org.signal.libsignal.metadata.certificate.SenderCertificate) Map(java.util.Map) GroupStaleDevicesException(org.whispersystems.signalservice.internal.push.exceptions.GroupStaleDevicesException) AttachmentPointerUtil(org.whispersystems.signalservice.api.util.AttachmentPointerUtil) StaleDevicesException(org.whispersystems.signalservice.internal.push.exceptions.StaleDevicesException) SendMessageResponse(org.whispersystems.signalservice.internal.push.SendMessageResponse) ClientZkProfileOperations(org.signal.zkgroup.profiles.ClientZkProfileOperations) PartialSendCompleteListener(org.whispersystems.signalservice.internal.push.http.PartialSendCompleteListener) ACI(org.whispersystems.signalservice.api.push.ACI) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) InvalidUnidentifiedAccessHeaderException(org.whispersystems.signalservice.internal.push.exceptions.InvalidUnidentifiedAccessHeaderException) Set(java.util.Set) OutgoingPushMessageList(org.whispersystems.signalservice.internal.push.OutgoingPushMessageList) Executors(java.util.concurrent.Executors) CredentialsProvider(org.whispersystems.signalservice.api.util.CredentialsProvider) SignalServiceGroupContext(org.whispersystems.signalservice.api.messages.SignalServiceGroupContext) SignalServiceGroupV2(org.whispersystems.signalservice.api.messages.SignalServiceGroupV2) GroupSessionBuilder(org.whispersystems.libsignal.groups.GroupSessionBuilder) Base64(org.whispersystems.util.Base64) MismatchedDevices(org.whispersystems.signalservice.internal.push.MismatchedDevices) ContentHint(org.whispersystems.signalservice.api.crypto.ContentHint) SignalServiceTypingMessage(org.whispersystems.signalservice.api.messages.SignalServiceTypingMessage) MalformedResponseException(org.whispersystems.signalservice.api.push.exceptions.MalformedResponseException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) ArrayList(java.util.ArrayList) UnidentifiedAccess(org.whispersystems.signalservice.api.crypto.UnidentifiedAccess) Verified(org.whispersystems.signalservice.internal.push.SignalServiceProtos.Verified) BlockedListMessage(org.whispersystems.signalservice.api.messages.multidevice.BlockedListMessage) SignalServiceGroup(org.whispersystems.signalservice.api.messages.SignalServiceGroup) SignalServiceCallMessage(org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage) SignalServiceAttachmentRemoteId(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId) ReadMessage(org.whispersystems.signalservice.api.messages.multidevice.ReadMessage) PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) ViewOnceOpenMessage(org.whispersystems.signalservice.api.messages.multidevice.ViewOnceOpenMessage) MessageRequestResponseMessage(org.whispersystems.signalservice.api.messages.multidevice.MessageRequestResponseMessage) GroupMismatchedDevices(org.whispersystems.signalservice.internal.push.GroupMismatchedDevices) KeysMessage(org.whispersystems.signalservice.api.messages.multidevice.KeysMessage) DistributionId(org.whispersystems.signalservice.api.push.DistributionId) CallMessage(org.whispersystems.signalservice.internal.push.SignalServiceProtos.CallMessage) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) IOException(java.io.IOException) Optional(org.whispersystems.libsignal.util.guava.Optional) ExecutionException(java.util.concurrent.ExecutionException) UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) AttachmentPointer(org.whispersystems.signalservice.internal.push.SignalServiceProtos.AttachmentPointer) ServiceId(org.whispersystems.signalservice.api.push.ServiceId) VerifiedMessage(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage) Uint64Util(org.whispersystems.signalservice.api.util.Uint64Util) ResumableUploadSpec(org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec) EnvelopeContent(org.whispersystems.signalservice.api.crypto.EnvelopeContent) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) SendMessageResult(org.whispersystems.signalservice.api.messages.SendMessageResult) Util(org.whispersystems.signalservice.internal.util.Util) SentTranscriptMessage(org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage) ProvisioningProtos(org.whispersystems.signalservice.internal.push.ProvisioningProtos) SyncMessage(org.whispersystems.signalservice.internal.push.SignalServiceProtos.SyncMessage) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) AttachmentCipherOutputStream(org.whispersystems.signalservice.api.crypto.AttachmentCipherOutputStream) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) PushAttachmentData(org.whispersystems.signalservice.internal.push.PushAttachmentData) PlaintextContent(org.whispersystems.libsignal.protocol.PlaintextContent) PushServiceSocket(org.whispersystems.signalservice.internal.push.PushServiceSocket) SenderKeyDistributionMessage(org.whispersystems.libsignal.protocol.SenderKeyDistributionMessage) MismatchedDevicesException(org.whispersystems.signalservice.internal.push.exceptions.MismatchedDevicesException) ByteArrayUtil(org.whispersystems.util.ByteArrayUtil) SignalServiceConfiguration(org.whispersystems.signalservice.internal.configuration.SignalServiceConfiguration) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) GroupContextV2(org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContextV2) List(java.util.List) ViewedMessage(org.whispersystems.signalservice.api.messages.multidevice.ViewedMessage) WebSocketUnavailableException(org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException) StaleDevices(org.whispersystems.signalservice.internal.push.StaleDevices) SendGroupMessageResponse(org.whispersystems.signalservice.internal.push.SendGroupMessageResponse) SharedContact(org.whispersystems.signalservice.api.messages.shared.SharedContact) AttachmentV2UploadAttributes(org.whispersystems.signalservice.internal.push.AttachmentV2UploadAttributes) NoSessionException(org.whispersystems.libsignal.NoSessionException) SignalServiceReceiptMessage(org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage) AnswerMessage(org.whispersystems.signalservice.api.messages.calls.AnswerMessage) ConfigurationMessage(org.whispersystems.signalservice.api.messages.multidevice.ConfigurationMessage) NullMessage(org.whispersystems.signalservice.internal.push.SignalServiceProtos.NullMessage) CancelationSignal(org.whispersystems.signalservice.internal.push.http.CancelationSignal) HashMap(java.util.HashMap) InvalidRegistrationIdException(org.whispersystems.libsignal.InvalidRegistrationIdException) OfferMessage(org.whispersystems.signalservice.api.messages.calls.OfferMessage) UnidentifiedAccessPair(org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair) AttachmentV3UploadAttributes(org.whispersystems.signalservice.internal.push.AttachmentV3UploadAttributes) AttachmentCipherOutputStreamFactory(org.whispersystems.signalservice.internal.push.http.AttachmentCipherOutputStreamFactory) SessionBuilder(org.whispersystems.libsignal.SessionBuilder) OpaqueMessage(org.whispersystems.signalservice.api.messages.calls.OpaqueMessage) Pair(org.whispersystems.libsignal.util.Pair) MessagingService(org.whispersystems.signalservice.api.services.MessagingService) AuthorizationFailedException(org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException) Log(org.whispersystems.libsignal.logging.Log) AttachmentService(org.whispersystems.signalservice.api.services.AttachmentService) Uint64RangeException(org.whispersystems.signalservice.api.util.Uint64RangeException) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) OutgoingPushMessage(org.whispersystems.signalservice.internal.push.OutgoingPushMessage) DecryptionErrorMessage(org.whispersystems.libsignal.protocol.DecryptionErrorMessage) Iterator(java.util.Iterator) OutgoingPaymentMessage(org.whispersystems.signalservice.api.messages.multidevice.OutgoingPaymentMessage) SignalSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalSessionBuilder) UnregisteredUserException(org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException) ProofRequiredException(org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException) GroupStaleDevices(org.whispersystems.signalservice.internal.push.GroupStaleDevices) IceUpdateMessage(org.whispersystems.signalservice.api.messages.calls.IceUpdateMessage) Content(org.whispersystems.signalservice.internal.push.SignalServiceProtos.Content) SignalServiceCipher(org.whispersystems.signalservice.api.crypto.SignalServiceCipher) SignalServiceSyncMessage(org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage) Collections(java.util.Collections) SignalServiceAttachmentStream(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream) InputStream(java.io.InputStream) UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) GroupMismatchedDevicesException(org.whispersystems.signalservice.internal.push.exceptions.GroupMismatchedDevicesException) HashMap(java.util.HashMap) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) ByteString(com.google.protobuf.ByteString) ServiceId(org.whispersystems.signalservice.api.push.ServiceId) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) GroupStaleDevices(org.whispersystems.signalservice.internal.push.GroupStaleDevices) SendGroupMessageResponse(org.whispersystems.signalservice.internal.push.SendGroupMessageResponse) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) GroupMismatchedDevices(org.whispersystems.signalservice.internal.push.GroupMismatchedDevices) SenderCertificate(org.signal.libsignal.metadata.certificate.SenderCertificate) Optional(org.whispersystems.libsignal.util.guava.Optional) SignalServiceCipher(org.whispersystems.signalservice.api.crypto.SignalServiceCipher) UnidentifiedAccessPair(org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair) WebSocketUnavailableException(org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException) IOException(java.io.IOException) ContentHint(org.whispersystems.signalservice.api.crypto.ContentHint) SendMessageResult(org.whispersystems.signalservice.api.messages.SendMessageResult) LinkedList(java.util.LinkedList) UnidentifiedAccess(org.whispersystems.signalservice.api.crypto.UnidentifiedAccess) MessagingService(org.whispersystems.signalservice.api.services.MessagingService) GroupStaleDevicesException(org.whispersystems.signalservice.internal.push.exceptions.GroupStaleDevicesException) SenderKeyDistributionMessage(org.whispersystems.libsignal.protocol.SenderKeyDistributionMessage) InvalidUnidentifiedAccessHeaderException(org.whispersystems.signalservice.internal.push.exceptions.InvalidUnidentifiedAccessHeaderException)

Aggregations

SignalProtocolAddress (org.whispersystems.libsignal.SignalProtocolAddress)77 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)14 PreKeyBundle (org.whispersystems.libsignal.state.PreKeyBundle)11 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)11 ArrayList (java.util.ArrayList)10 Recipient (org.thoughtcrime.securesms.recipients.Recipient)10 IOException (java.io.IOException)9 SessionBuilder (org.whispersystems.libsignal.SessionBuilder)9 Optional (org.whispersystems.libsignal.util.guava.Optional)9 UntrustedIdentityException (org.whispersystems.signalservice.api.crypto.UntrustedIdentityException)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 List (java.util.List)8 ContentHint (org.whispersystems.signalservice.api.crypto.ContentHint)8 DistributionId (org.whispersystems.signalservice.api.push.DistributionId)8 NonNull (androidx.annotation.NonNull)6 LinkedList (java.util.LinkedList)6 Collectors (java.util.stream.Collectors)6 SenderKeyDistributionMessage (org.whispersystems.libsignal.protocol.SenderKeyDistributionMessage)6 SignalServiceMessageSender (org.whispersystems.signalservice.api.SignalServiceMessageSender)6