Search in sources :

Example 6 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project toshi-android-client by toshiapp.

the class ProtocolStore method generateLastResortKey.

private PreKeyRecord generateLastResortKey() {
    final PreKeyRecord lrk = PreKeyUtil.generateLastResortKey(BaseApplication.get());
    SignalPreferences.setSerializedLastResortKey(lrk.serialize());
    return lrk;
}
Also used : PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Example 7 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project Conversations by siacs.

the class AxolotlService method publishBundlesIfNeeded.

public void publishBundlesIfNeeded(final boolean announce, final boolean wipe) {
    if (pepBroken) {
        Log.d(Config.LOGTAG, getLogprefix(account) + "publishBundlesIfNeeded called, but PEP is broken. Ignoring... ");
        return;
    }
    if (account.getXmppConnection().getFeatures().pepPublishOptions()) {
        this.changeAccessMode.set(account.isOptionSet(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE));
    } else {
        if (account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, true)) {
            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server doesn’t support publish-options. setting for later access mode change");
            mXmppConnectionService.databaseBackend.updateAccount(account);
        }
    }
    if (this.changeAccessMode.get()) {
        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server gained publish-options capabilities. changing access model");
    }
    IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(account.getJid().asBareJid(), getOwnDeviceId());
    mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(Account account, IqPacket packet) {
            if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
                // ignore timeout. do nothing
                return;
            }
            if (packet.getType() == IqPacket.TYPE.ERROR) {
                Element error = packet.findChild("error");
                if (error == null || !error.hasChild("item-not-found")) {
                    pepBroken = true;
                    Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "request for device bundles came back with something other than item-not-found" + packet);
                    return;
                }
            }
            PreKeyBundle bundle = mXmppConnectionService.getIqParser().bundle(packet);
            Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
            boolean flush = false;
            if (bundle == null) {
                Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid bundle:" + packet);
                bundle = new PreKeyBundle(-1, -1, -1, null, -1, null, null, null);
                flush = true;
            }
            if (keys == null) {
                Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
            }
            try {
                boolean changed = false;
                // Validate IdentityKey
                IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
                if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
                    Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
                    changed = true;
                }
                // Validate signedPreKeyRecord + ID
                SignedPreKeyRecord signedPreKeyRecord;
                int numSignedPreKeys = axolotlStore.getSignedPreKeysCount();
                try {
                    signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
                    if (flush || !bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey()) || !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
                        Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
                        signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
                        axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
                        changed = true;
                    }
                } catch (InvalidKeyIdException e) {
                    Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
                    signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
                    axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
                    changed = true;
                }
                // Validate PreKeys
                Set<PreKeyRecord> preKeyRecords = new HashSet<>();
                if (keys != null) {
                    for (Integer id : keys.keySet()) {
                        try {
                            PreKeyRecord preKeyRecord = axolotlStore.loadPreKey(id);
                            if (preKeyRecord.getKeyPair().getPublicKey().equals(keys.get(id))) {
                                preKeyRecords.add(preKeyRecord);
                            }
                        } catch (InvalidKeyIdException ignored) {
                        }
                    }
                }
                int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
                if (newKeys > 0) {
                    List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(axolotlStore.getCurrentPreKeyId() + 1, newKeys);
                    preKeyRecords.addAll(newRecords);
                    for (PreKeyRecord record : newRecords) {
                        axolotlStore.storePreKey(record.getId(), record);
                    }
                    changed = true;
                    Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding " + newKeys + " new preKeys to PEP.");
                }
                if (changed || changeAccessMode.get()) {
                    if (account.getPrivateKeyAlias() != null && Config.X509_VERIFICATION) {
                        mXmppConnectionService.publishDisplayName(account);
                        publishDeviceVerificationAndBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
                    } else {
                        publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
                    }
                } else {
                    Log.d(Config.LOGTAG, getLogprefix(account) + "Bundle " + getOwnDeviceId() + " in PEP was current");
                    if (wipe) {
                        wipeOtherPepDevices();
                    } else if (announce) {
                        Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
                        publishOwnDeviceIdIfNeeded();
                    }
                }
            } catch (InvalidKeyException e) {
                Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
            }
        }
    });
}
Also used : Account(eu.siacs.conversations.entities.Account) Set(java.util.Set) HashSet(java.util.HashSet) OnIqPacketReceived(eu.siacs.conversations.xmpp.OnIqPacketReceived) Element(eu.siacs.conversations.xml.Element) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket) PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) OmemoVerifiedRtpContentMap(eu.siacs.conversations.xmpp.jingle.OmemoVerifiedRtpContentMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) RtpContentMap(eu.siacs.conversations.xmpp.jingle.RtpContentMap) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Example 8 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project Signal-Android by WhisperSystems.

the class RegistrationRepository method generateAndRegisterPreKeys.

private void generateAndRegisterPreKeys(@NonNull ServiceIdType serviceIdType, @NonNull SignalServiceAccountManager accountManager, @NonNull SignalProtocolStore protocolStore, @NonNull PreKeyMetadataStore metadataStore) throws IOException {
    SignedPreKeyRecord signedPreKey = PreKeyUtil.generateAndStoreSignedPreKey(protocolStore, metadataStore, true);
    List<PreKeyRecord> oneTimePreKeys = PreKeyUtil.generateAndStoreOneTimePreKeys(protocolStore, metadataStore);
    accountManager.setPreKeys(serviceIdType, protocolStore.getIdentityKeyPair().getPublicKey(), signedPreKey, oneTimePreKeys);
    metadataStore.setSignedPreKeyRegistered(true);
}
Also used : PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Example 9 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project Signal-Android by WhisperSystems.

the class PushServiceSocket method registerPreKeys.

public void registerPreKeys(ServiceIdType serviceIdType, IdentityKey identityKey, SignedPreKeyRecord signedPreKey, List<PreKeyRecord> records) throws IOException {
    List<PreKeyEntity> entities = new LinkedList<>();
    for (PreKeyRecord record : records) {
        PreKeyEntity entity = new PreKeyEntity(record.getId(), record.getKeyPair().getPublicKey());
        entities.add(entity);
    }
    SignedPreKeyEntity signedPreKeyEntity = new SignedPreKeyEntity(signedPreKey.getId(), signedPreKey.getKeyPair().getPublicKey(), signedPreKey.getSignature());
    makeServiceRequest(String.format(Locale.US, PREKEY_PATH, "", serviceIdType.queryParam()), "PUT", JsonUtil.toJson(new PreKeyState(entities, signedPreKeyEntity, identityKey)));
}
Also used : SignedPreKeyEntity(org.whispersystems.signalservice.api.push.SignedPreKeyEntity) SignedPreKeyEntity(org.whispersystems.signalservice.api.push.SignedPreKeyEntity) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) LinkedList(java.util.LinkedList)

Example 10 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project Signal-Android by WhisperSystems.

the class PreKeyMigrationHelper method migratePreKeys.

public static boolean migratePreKeys(Context context, SQLiteDatabase database) {
    File[] preKeyFiles = getPreKeyDirectory(context).listFiles();
    boolean clean = true;
    if (preKeyFiles != null) {
        for (File preKeyFile : preKeyFiles) {
            if (!"index.dat".equals(preKeyFile.getName())) {
                try {
                    PreKeyRecord preKey = new PreKeyRecord(loadSerializedRecord(preKeyFile));
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(OneTimePreKeyDatabase.KEY_ID, preKey.getId());
                    contentValues.put(OneTimePreKeyDatabase.PUBLIC_KEY, Base64.encodeBytes(preKey.getKeyPair().getPublicKey().serialize()));
                    contentValues.put(OneTimePreKeyDatabase.PRIVATE_KEY, Base64.encodeBytes(preKey.getKeyPair().getPrivateKey().serialize()));
                    database.insert(OneTimePreKeyDatabase.TABLE_NAME, null, contentValues);
                    Log.i(TAG, "Migrated one-time prekey: " + preKey.getId());
                } catch (IOException | InvalidMessageException e) {
                    Log.w(TAG, e);
                    clean = false;
                }
            }
        }
    }
    File[] signedPreKeyFiles = getSignedPreKeyDirectory(context).listFiles();
    if (signedPreKeyFiles != null) {
        for (File signedPreKeyFile : signedPreKeyFiles) {
            if (!"index.dat".equals(signedPreKeyFile.getName())) {
                try {
                    SignedPreKeyRecord signedPreKey = new SignedPreKeyRecord(loadSerializedRecord(signedPreKeyFile));
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(SignedPreKeyDatabase.KEY_ID, signedPreKey.getId());
                    contentValues.put(SignedPreKeyDatabase.PUBLIC_KEY, Base64.encodeBytes(signedPreKey.getKeyPair().getPublicKey().serialize()));
                    contentValues.put(SignedPreKeyDatabase.PRIVATE_KEY, Base64.encodeBytes(signedPreKey.getKeyPair().getPrivateKey().serialize()));
                    contentValues.put(SignedPreKeyDatabase.SIGNATURE, Base64.encodeBytes(signedPreKey.getSignature()));
                    contentValues.put(SignedPreKeyDatabase.TIMESTAMP, signedPreKey.getTimestamp());
                    database.insert(SignedPreKeyDatabase.TABLE_NAME, null, contentValues);
                    Log.i(TAG, "Migrated signed prekey: " + signedPreKey.getId());
                } catch (IOException | InvalidMessageException e) {
                    Log.w(TAG, e);
                    clean = false;
                }
            }
        }
    }
    File oneTimePreKeyIndex = new File(getPreKeyDirectory(context), PreKeyIndex.FILE_NAME);
    File signedPreKeyIndex = new File(getSignedPreKeyDirectory(context), SignedPreKeyIndex.FILE_NAME);
    if (oneTimePreKeyIndex.exists()) {
        try {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(oneTimePreKeyIndex));
            PreKeyIndex index = JsonUtils.fromJson(reader, PreKeyIndex.class);
            reader.close();
            Log.i(TAG, "Setting next prekey id: " + index.nextPreKeyId);
            SignalStore.account().aciPreKeys().setNextOneTimePreKeyId(index.nextPreKeyId);
        } catch (IOException e) {
            Log.w(TAG, e);
        }
    }
    if (signedPreKeyIndex.exists()) {
        try {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(signedPreKeyIndex));
            SignedPreKeyIndex index = JsonUtils.fromJson(reader, SignedPreKeyIndex.class);
            reader.close();
            Log.i(TAG, "Setting next signed prekey id: " + index.nextSignedPreKeyId);
            Log.i(TAG, "Setting active signed prekey id: " + index.activeSignedPreKeyId);
            SignalStore.account().aciPreKeys().setNextSignedPreKeyId(index.nextSignedPreKeyId);
            SignalStore.account().aciPreKeys().setActiveSignedPreKeyId(index.activeSignedPreKeyId);
        } catch (IOException e) {
            Log.w(TAG, e);
        }
    }
    return clean;
}
Also used : ContentValues(android.content.ContentValues) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) File(java.io.File) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Aggregations

PreKeyRecord (org.whispersystems.libsignal.state.PreKeyRecord)34 SignedPreKeyRecord (org.whispersystems.libsignal.state.SignedPreKeyRecord)33 LinkedList (java.util.LinkedList)9 IdentityKeyPair (org.whispersystems.libsignal.IdentityKeyPair)8 ECKeyPair (org.whispersystems.libsignal.ecc.ECKeyPair)8 InvalidKeyIdException (org.whispersystems.libsignal.InvalidKeyIdException)6 IOException (java.io.IOException)5 PreKeyStore (org.whispersystems.libsignal.state.PreKeyStore)5 SignedPreKeyStore (org.whispersystems.libsignal.state.SignedPreKeyStore)5 File (java.io.File)4 SignalServiceAccountManager (org.whispersystems.signalservice.api.SignalServiceAccountManager)4 SignedPreKeyEntity (org.whispersystems.signalservice.api.push.SignedPreKeyEntity)4 Cursor (android.database.Cursor)3 TextSecurePreKeyStore (org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore)3 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)3 ECPublicKey (org.whispersystems.libsignal.ecc.ECPublicKey)3 ContentValues (android.content.ContentValues)2 NonNull (androidx.annotation.NonNull)2 SignalPreKeyStore (com.toshi.crypto.signal.store.SignalPreKeyStore)2 Element (de.pixart.messenger.xml.Element)2