Search in sources :

Example 31 with IdentityKeyPair

use of org.whispersystems.libsignal.IdentityKeyPair in project Smack by igniterealtime.

the class LegacySignalOmemoKeyUtilTest method getFingerprintTest.

@Test
public void getFingerprintTest() {
    IdentityKeyPair ikp = keyUtil.generateOmemoIdentityKeyPair();
    IdentityKey ik = ikp.getPublicKey();
    assertTrue("Length of fingerprint must be 64.", keyUtil.getFingerprintOfIdentityKey(ik).length() == 64);
}
Also used : IdentityKey(org.whispersystems.libsignal.IdentityKey) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) Test(org.junit.Test)

Example 32 with IdentityKeyPair

use of org.whispersystems.libsignal.IdentityKeyPair in project Conversations by siacs.

the class SQLiteAxolotlStore method generateIdentityKeyPair.

private static IdentityKeyPair generateIdentityKeyPair() {
    Log.i(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Generating axolotl IdentityKeyPair...");
    ECKeyPair identityKeyPairKeys = Curve.generateKeyPair();
    return new IdentityKeyPair(new IdentityKey(identityKeyPairKeys.getPublicKey()), identityKeyPairKeys.getPrivateKey());
}
Also used : IdentityKey(org.whispersystems.libsignal.IdentityKey) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair)

Example 33 with IdentityKeyPair

use of org.whispersystems.libsignal.IdentityKeyPair in project Conversations by siacs.

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 < 40 && newVersion >= 40) {
        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().asBareJid().toString(), 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().asBareJid() + ": 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);
        final File oldPicturesDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/Conversations/");
        final File oldFilesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/");
        final File newFilesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Files/");
        final File newVideosDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Videos/");
        if (oldPicturesDirectory.exists() && oldPicturesDirectory.isDirectory()) {
            final File newPicturesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Images/");
            newPicturesDirectory.getParentFile().mkdirs();
            if (oldPicturesDirectory.renameTo(newPicturesDirectory)) {
                Log.d(Config.LOGTAG, "moved " + oldPicturesDirectory.getAbsolutePath() + " to " + newPicturesDirectory.getAbsolutePath());
            }
        }
        if (oldFilesDirectory.exists() && oldFilesDirectory.isDirectory()) {
            newFilesDirectory.mkdirs();
            newVideosDirectory.mkdirs();
            final File[] files = oldFilesDirectory.listFiles();
            if (files == null) {
                return;
            }
            for (File file : files) {
                if (file.getName().equals(".nomedia")) {
                    if (file.delete()) {
                        Log.d(Config.LOGTAG, "deleted nomedia file in " + oldFilesDirectory.getAbsolutePath());
                    }
                } else if (file.isFile()) {
                    final String name = file.getName();
                    boolean isVideo = false;
                    int start = name.lastIndexOf('.') + 1;
                    if (start < name.length()) {
                        String mime = MimeUtils.guessMimeTypeFromExtension(name.substring(start));
                        isVideo = mime != null && mime.startsWith("video/");
                    }
                    File dst = new File((isVideo ? newVideosDirectory : newFilesDirectory).getAbsolutePath() + "/" + file.getName());
                    if (file.renameTo(dst)) {
                        Log.d(Config.LOGTAG, "moved " + file + " to " + dst);
                    }
                }
            }
        }
    }
    if (oldVersion < 35 && newVersion >= 35) {
        db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
    }
    if (oldVersion < 36 && newVersion >= 36) {
        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 < 37 && newVersion >= 37) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.READ_BY_MARKERS + " TEXT");
    }
    if (oldVersion < 38 && newVersion >= 38) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.MARKABLE + " NUMBER DEFAULT 0");
    }
    if (oldVersion < 39 && newVersion >= 39) {
        db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
    }
    if (QuickConversationsService.isQuicksy() && oldVersion < 43 && newVersion >= 43) {
        List<Account> accounts = getAccounts(db);
        for (Account account : accounts) {
            account.setOption(Account.OPTION_MAGIC_CREATE, true);
            db.update(Account.TABLENAME, account.getContentValues(), Account.UUID + "=?", new String[] { account.getUuid() });
        }
    }
    if (oldVersion < 44 && newVersion >= 44) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.DELETED + " NUMBER DEFAULT 0");
        db.execSQL(CREATE_MESSAGE_DELETED_INDEX);
        db.execSQL(CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX);
        db.execSQL(CREATE_MESSAGE_TYPE_INDEX);
    }
    if (oldVersion < 45 && newVersion >= 45) {
        db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.BODY_LANGUAGE);
    }
    if (oldVersion < 46 && newVersion >= 46) {
        final long start = SystemClock.elapsedRealtime();
        db.rawQuery("PRAGMA secure_delete = FALSE", null).close();
        db.execSQL("update " + Message.TABLENAME + " set " + Message.EDITED + "=NULL");
        db.rawQuery("PRAGMA secure_delete=ON", null).close();
        final long diff = SystemClock.elapsedRealtime() - start;
        Log.d(Config.LOGTAG, "deleted old edit information in " + diff + "ms");
    }
    if (oldVersion < 47 && newVersion >= 47) {
        db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.PRESENCE_NAME + " TEXT");
    }
    if (oldVersion < 48 && newVersion >= 48) {
        db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.RTP_CAPABILITY + " TEXT");
    }
    if (oldVersion < 49 && newVersion >= 49) {
        db.beginTransaction();
        db.execSQL("DROP TRIGGER IF EXISTS after_message_insert;");
        db.execSQL("DROP TRIGGER IF EXISTS after_message_update;");
        db.execSQL("DROP TRIGGER IF EXISTS after_message_delete;");
        db.execSQL("DROP TABLE IF EXISTS messages_index;");
        // a hack that should not be necessary, but
        // there was at least one occurence when SQLite failed at this
        db.execSQL("DROP TABLE IF EXISTS messages_index_docsize;");
        db.execSQL("DROP TABLE IF EXISTS messages_index_segdir;");
        db.execSQL("DROP TABLE IF EXISTS messages_index_segments;");
        db.execSQL("DROP TABLE IF EXISTS messages_index_stat;");
        db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
        db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
        db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
        db.execSQL(CREATE_MESSAGE_DELETE_TRIGGER);
        db.setTransactionSuccessful();
        db.endTransaction();
        requiresMessageIndexRebuild = true;
    }
}
Also used : ContentValues(android.content.ContentValues) Account(eu.siacs.conversations.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 34 with IdentityKeyPair

use of org.whispersystems.libsignal.IdentityKeyPair in project Conversations by siacs.

the class DatabaseBackend method loadOwnIdentityKeyPair.

private IdentityKeyPair loadOwnIdentityKeyPair(SQLiteDatabase db, Account account) {
    String name = account.getJid().asBareJid().toString();
    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().asBareJid() + ", address: " + name);
        }
    }
    cursor.close();
    return identityKeyPair;
}
Also used : IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) Cursor(android.database.Cursor) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException)

Example 35 with IdentityKeyPair

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

the class RefreshPreKeysJob method refreshKeys.

/**
 * @return True if we need to clean prekeys, otherwise false.
 */
private boolean refreshKeys(@NonNull ServiceIdType serviceIdType, @NonNull SignalProtocolStore protocolStore, @NonNull PreKeyMetadataStore metadataStore) throws IOException {
    String logPrefix = "[" + serviceIdType + "] ";
    SignalServiceAccountManager accountManager = ApplicationDependencies.getSignalServiceAccountManager();
    int availableKeys = accountManager.getPreKeysCount(serviceIdType);
    log(TAG, logPrefix + "Available keys: " + availableKeys);
    if (availableKeys >= PREKEY_MINIMUM && metadataStore.isSignedPreKeyRegistered()) {
        log(TAG, logPrefix + "Available keys sufficient.");
        return false;
    }
    List<PreKeyRecord> preKeyRecords = PreKeyUtil.generateAndStoreOneTimePreKeys(protocolStore, metadataStore);
    SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateAndStoreSignedPreKey(protocolStore, metadataStore, false);
    IdentityKeyPair identityKey = protocolStore.getIdentityKeyPair();
    log(TAG, logPrefix + "Registering new prekeys...");
    accountManager.setPreKeys(serviceIdType, identityKey.getPublicKey(), signedPreKeyRecord, preKeyRecords);
    metadataStore.setActiveSignedPreKeyId(signedPreKeyRecord.getId());
    metadataStore.setSignedPreKeyRegistered(true);
    log(TAG, logPrefix + "Need to clean prekeys.");
    return true;
}
Also used : SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) NetworkConstraint(org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint)

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