Search in sources :

Example 6 with IdentityKeyPair

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

the class RefreshPreKeysJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws IOException {
    if (!TextSecurePreferences.isPushRegistered(context))
        return;
    int availableKeys = accountManager.getPreKeysCount();
    if (availableKeys >= PREKEY_MINIMUM && TextSecurePreferences.isSignedPreKeyRegistered(context)) {
        Log.w(TAG, "Available keys sufficient: " + availableKeys);
        return;
    }
    List<PreKeyRecord> preKeyRecords = PreKeyUtil.generatePreKeys(context);
    IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context);
    SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKey, false);
    Log.w(TAG, "Registering new prekeys...");
    accountManager.setPreKeys(identityKey.getPublicKey(), signedPreKeyRecord, preKeyRecords);
    PreKeyUtil.setActiveSignedPreKeyId(context, signedPreKeyRecord.getId());
    TextSecurePreferences.setSignedPreKeyRegistered(context, true);
    ApplicationContext.getInstance(context).getJobManager().add(new CleanPreKeysJob(context));
}
Also used : PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Example 7 with IdentityKeyPair

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

the class RotateSignedPreKeyJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws Exception {
    Log.w(TAG, "Rotating signed prekey...");
    IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context);
    SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKey, false);
    accountManager.setSignedPreKey(signedPreKeyRecord);
    PreKeyUtil.setActiveSignedPreKeyId(context, signedPreKeyRecord.getId());
    TextSecurePreferences.setSignedPreKeyRegistered(context, true);
    TextSecurePreferences.setSignedPreKeyFailureCount(context, 0);
    ApplicationContext.getInstance(context).getJobManager().add(new CleanPreKeysJob(context));
}
Also used : IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Example 8 with IdentityKeyPair

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

the class DatabaseBackend method loadOwnIdentityKeyPair.

private IdentityKeyPair loadOwnIdentityKeyPair(SQLiteDatabase db, Account account) {
    String name = account.getJid().toBareJid().toPreppedString();
    IdentityKeyPair identityKeyPair = null;
    Cursor cursor = getIdentityKeyCursor(db, account, name, true);
    if (cursor.getCount() != 0) {
        cursor.moveToFirst();
        try {
            identityKeyPair = new IdentityKeyPair(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
        } catch (InvalidKeyException e) {
            Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Encountered invalid IdentityKey in database for account" + account.getJid().toBareJid() + ", address: " + name);
        }
    }
    cursor.close();
    return identityKeyPair;
}
Also used : IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) Cursor(android.database.Cursor) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException)

Example 9 with IdentityKeyPair

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

the class SQLiteAxolotlStore method loadIdentityKeyPair.

// --------------------------------------
// IdentityKeyStore
// --------------------------------------
private IdentityKeyPair loadIdentityKeyPair() {
    synchronized (mXmppConnectionService) {
        IdentityKeyPair ownKey = mXmppConnectionService.databaseBackend.loadOwnIdentityKeyPair(account);
        if (ownKey != null) {
            return ownKey;
        } else {
            Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Could not retrieve own IdentityKeyPair");
            ownKey = generateIdentityKeyPair();
            mXmppConnectionService.databaseBackend.storeOwnIdentityKeyPair(account, ownKey);
        }
        return ownKey;
    }
}
Also used : IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair)

Example 10 with IdentityKeyPair

use of org.whispersystems.libsignal.IdentityKeyPair 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)

Aggregations

IdentityKeyPair (org.whispersystems.libsignal.IdentityKeyPair)40 SignedPreKeyRecord (org.whispersystems.libsignal.state.SignedPreKeyRecord)12 Test (org.junit.Test)9 IdentityKey (org.whispersystems.libsignal.IdentityKey)9 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)8 PreKeyRecord (org.whispersystems.libsignal.state.PreKeyRecord)7 IOException (java.io.IOException)6 SignalServiceProtos (org.whispersystems.signalservice.internal.push.SignalServiceProtos)6 ECPrivateKey (org.whispersystems.libsignal.ecc.ECPrivateKey)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 SuppressLint (android.annotation.SuppressLint)3 ECKeyPair (org.whispersystems.libsignal.ecc.ECKeyPair)3 SignalServiceAccountManager (org.whispersystems.signalservice.api.SignalServiceAccountManager)3 ContentValues (android.content.ContentValues)2 Context (android.content.Context)2 Cursor (android.database.Cursor)2 NonNull (android.support.annotation.NonNull)2 Account (de.pixart.messenger.entities.Account)2 Account (eu.siacs.conversations.entities.Account)2