Java Code Examples for org.whispersystems.libsignal.SessionBuilder

The following examples show how to use org.whispersystems.libsignal.SessionBuilder. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: bcm-android   Author: bcmapp   File: PushDecryptJob.java    License: GNU General Public License v3.0 4 votes vote down vote up
private void handleCorruptMessage(@NonNull MasterSecretUnion masterSecret,
                                  @NonNull SignalServiceProtos.Envelope envelope) {
    ALog.i(TAG, "handleCorruptMessage");

    ThreadRepo threadRepo = repository.getThreadRepo();
    String dataJson = threadRepo.getDecryptFailData(envelope.getSource());
    DecryptFailData data;
    if (dataJson != null && !dataJson.isEmpty()) {
        data = GsonUtils.INSTANCE.fromJson(dataJson, DecryptFailData.class);
    } else {
        data = new DecryptFailData();
    }
    if (data.getLastDeleteSessionTime() + 50000 < System.currentTimeMillis()) {
        ALog.i(TAG, "Last delete session time is 5s ago. Delete Session.");
        SignalProtocolStore store = new SignalProtocolStoreImpl(context, accountContext);
        SignalProtocolAddress address = new SignalProtocolAddress(envelope.getSource(), SignalServiceAddress.DEFAULT_DEVICE_ID);
        if (store.containsSession(address)) {
            store.deleteSession(address);
        }
        try {
            Optional<String> relay = Optional.absent();
            if (envelope.hasRelay()) {
                relay = Optional.of(envelope.getRelay());
            }
            List<PreKeyBundle> preKeyBundles = ChatHttp.INSTANCE.get(accountContext).getPreKeys(new SignalServiceAddress(envelope.getSource(), relay), SignalServiceAddress.DEFAULT_DEVICE_ID);
            for (PreKeyBundle preKey : preKeyBundles) {

                String identityKeyString = new String(EncryptUtils.base64Encode(preKey.getIdentityKey().serialize()));
                if (!AddressUtil.INSTANCE.isValid(envelope.getSource(), identityKeyString)) {
                    ALog.e(TAG, "getPreKeys error identity key got");
                    continue;
                }

                SessionBuilder sessionBuilder = new SessionBuilder(store, new SignalProtocolAddress(envelope.getSource(), SignalServiceAddress.DEFAULT_DEVICE_ID));
                sessionBuilder.process(preKey);

            }
        } catch (Throwable ex) {
            ALog.w(TAG, "Untrusted identity key from handleMismatchedDevices");
        }
        data.setLastDeleteSessionTime(System.currentTimeMillis());
        threadRepo.setDecryptFailData(envelope.getSource(), data.toJson());
    }

    //If the decryption fails, do not insert the library, and send a new receipt directly
    long messageId = envelope.getTimestamp();
    Recipient recipient = Recipient.from(accountContext, envelope.getSource(), false);
    String message = new AmeGroupMessage<>(AmeGroupMessage.RECEIPT, new AmeGroupMessage.ReceiptContent(messageId)).toString();
    OutgoingLocationMessage outgoingLocationMessage = new OutgoingLocationMessage(recipient, message, 0);
    if (masterSecret.getMasterSecret().isPresent()) {
        long threadId = threadRepo.getThreadIdIfExist(recipient);
        MessageSender.sendHideMessage(AppContextHolder.APP_CONTEXT, threadId, accountContext, AmeGroupMessage.RECEIPT, outgoingLocationMessage);
    }
}