Search in sources :

Example 6 with AxolotlAddress

use of org.whispersystems.libaxolotl.AxolotlAddress in project Conversations by siacs.

the class SQLiteAxolotlStore method deleteAllSessions.

/**
	 * Remove the {@link SessionRecord}s corresponding to all devices of a recipientId.
	 *
	 * @param name the name of the remote client.
	 */
@Override
public void deleteAllSessions(String name) {
    AxolotlAddress address = new AxolotlAddress(name, 0);
    mXmppConnectionService.databaseBackend.deleteAllSessions(account, address);
}
Also used : AxolotlAddress(org.whispersystems.libaxolotl.AxolotlAddress)

Example 7 with AxolotlAddress

use of org.whispersystems.libaxolotl.AxolotlAddress 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");
    } else if (oldVersion < 22 && newVersion >= 22) {
        db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.CERTIFICATE);
    }
    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");
    }
    /* 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) {
        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);
            AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toPreppedString(), ownDeviceId);
            deleteSession(db, account, ownAddress);
            IdentityKeyPair identityKeyPair = loadOwnIdentityKeyPair(db, account);
            if (identityKeyPair != null) {
                String[] selectionArgs = { account.getUuid(), identityKeyPair.getPublicKey().getFingerprint().replaceAll("\\s", "") };
                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 < 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 < 30 && newVersion >= 30) {
        db.execSQL(CREATE_START_TIMES_TABLE);
    }
    if (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 < 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 < 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);
    }
}
Also used : ContentValues(android.content.ContentValues) Account(eu.siacs.conversations.entities.Account) HashMap(java.util.HashMap) AxolotlAddress(org.whispersystems.libaxolotl.AxolotlAddress) IdentityKeyPair(org.whispersystems.libaxolotl.IdentityKeyPair) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File)

Example 8 with AxolotlAddress

use of org.whispersystems.libaxolotl.AxolotlAddress in project Conversations by siacs.

the class AxolotlService method verifySessionWithPEP.

private void verifySessionWithPEP(final XmppAxolotlSession session) {
    Log.d(Config.LOGTAG, "trying to verify fresh session (" + session.getRemoteAddress().getName() + ") with pep");
    final AxolotlAddress address = session.getRemoteAddress();
    final IdentityKey identityKey = session.getIdentityKey();
    try {
        IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveVerificationForDevice(Jid.fromString(address.getName()), address.getDeviceId());
        mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {

            @Override
            public void onIqPacketReceived(Account account, IqPacket packet) {
                Pair<X509Certificate[], byte[]> verification = mXmppConnectionService.getIqParser().verification(packet);
                if (verification != null) {
                    try {
                        Signature verifier = Signature.getInstance("sha256WithRSA");
                        verifier.initVerify(verification.first[0]);
                        verifier.update(identityKey.serialize());
                        if (verifier.verify(verification.second)) {
                            try {
                                mXmppConnectionService.getMemorizingTrustManager().getNonInteractive().checkClientTrusted(verification.first, "RSA");
                                String fingerprint = session.getFingerprint();
                                Log.d(Config.LOGTAG, "verified session with x.509 signature. fingerprint was: " + fingerprint);
                                setFingerprintTrust(fingerprint, FingerprintStatus.createActiveVerified(true));
                                axolotlStore.setFingerprintCertificate(fingerprint, verification.first[0]);
                                fetchStatusMap.put(address, FetchStatus.SUCCESS_VERIFIED);
                                Bundle information = CryptoHelper.extractCertificateInformation(verification.first[0]);
                                try {
                                    final String cn = information.getString("subject_cn");
                                    final Jid jid = Jid.fromString(address.getName());
                                    Log.d(Config.LOGTAG, "setting common name for " + jid + " to " + cn);
                                    account.getRoster().getContact(jid).setCommonName(cn);
                                } catch (final InvalidJidException ignored) {
                                //ignored
                                }
                                finishBuildingSessionsFromPEP(address);
                                return;
                            } catch (Exception e) {
                                Log.d(Config.LOGTAG, "could not verify certificate");
                            }
                        }
                    } catch (Exception e) {
                        Log.d(Config.LOGTAG, "error during verification " + e.getMessage());
                    }
                } else {
                    Log.d(Config.LOGTAG, "no verification found");
                }
                fetchStatusMap.put(address, FetchStatus.SUCCESS);
                finishBuildingSessionsFromPEP(address);
            }
        });
    } catch (InvalidJidException e) {
        fetchStatusMap.put(address, FetchStatus.SUCCESS);
        finishBuildingSessionsFromPEP(address);
    }
}
Also used : Account(eu.siacs.conversations.entities.Account) IdentityKey(org.whispersystems.libaxolotl.IdentityKey) OnIqPacketReceived(eu.siacs.conversations.xmpp.OnIqPacketReceived) Jid(eu.siacs.conversations.xmpp.jid.Jid) Bundle(android.os.Bundle) PreKeyBundle(org.whispersystems.libaxolotl.state.PreKeyBundle) AxolotlAddress(org.whispersystems.libaxolotl.AxolotlAddress) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) X509Certificate(java.security.cert.X509Certificate) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) InvalidKeyException(org.whispersystems.libaxolotl.InvalidKeyException) InvalidKeyIdException(org.whispersystems.libaxolotl.InvalidKeyIdException) UntrustedIdentityException(org.whispersystems.libaxolotl.UntrustedIdentityException) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket) Signature(java.security.Signature) Pair(android.util.Pair) IdentityKeyPair(org.whispersystems.libaxolotl.IdentityKeyPair)

Example 9 with AxolotlAddress

use of org.whispersystems.libaxolotl.AxolotlAddress in project Conversations by siacs.

the class AxolotlService method findSessionsForContact.

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

Example 10 with AxolotlAddress

use of org.whispersystems.libaxolotl.AxolotlAddress in project Conversations by siacs.

the class AxolotlService method findDevicesWithoutSession.

public Set<AxolotlAddress> findDevicesWithoutSession(final Conversation conversation) {
    Set<AxolotlAddress> addresses = new HashSet<>();
    for (Jid jid : getCryptoTargets(conversation)) {
        Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + jid);
        if (deviceIds.get(jid) != null) {
            for (Integer foreignId : this.deviceIds.get(jid)) {
                AxolotlAddress address = new AxolotlAddress(jid.toPreppedString(), foreignId);
                if (sessions.get(address) == null) {
                    IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
                    if (identityKey != null) {
                        Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
                        XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
                        sessions.put(address, session);
                    } else {
                        Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + jid + ":" + foreignId);
                        if (fetchStatusMap.get(address) != FetchStatus.ERROR) {
                            addresses.add(address);
                        } else {
                            Log.d(Config.LOGTAG, getLogprefix(account) + "skipping over " + address + " because it's broken");
                        }
                    }
                }
            }
        } else {
            Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
        }
    }
    if (deviceIds.get(account.getJid().toBareJid()) != null) {
        for (Integer ownId : this.deviceIds.get(account.getJid().toBareJid())) {
            AxolotlAddress address = new AxolotlAddress(account.getJid().toBareJid().toPreppedString(), ownId);
            if (sessions.get(address) == null) {
                IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
                if (identityKey != null) {
                    Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
                    XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
                    sessions.put(address, session);
                } else {
                    Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + ownId);
                    if (fetchStatusMap.get(address) != FetchStatus.ERROR) {
                        addresses.add(address);
                    } else {
                        Log.d(Config.LOGTAG, getLogprefix(account) + "skipping over " + address + " because it's broken");
                    }
                }
            }
        }
    }
    return addresses;
}
Also used : IdentityKey(org.whispersystems.libaxolotl.IdentityKey) Jid(eu.siacs.conversations.xmpp.jid.Jid) AxolotlAddress(org.whispersystems.libaxolotl.AxolotlAddress) HashSet(java.util.HashSet)

Aggregations

AxolotlAddress (org.whispersystems.libaxolotl.AxolotlAddress)10 HashSet (java.util.HashSet)3 Account (eu.siacs.conversations.entities.Account)2 Jid (eu.siacs.conversations.xmpp.jid.Jid)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 IdentityKey (org.whispersystems.libaxolotl.IdentityKey)2 IdentityKeyPair (org.whispersystems.libaxolotl.IdentityKeyPair)2 ContentValues (android.content.ContentValues)1 Bundle (android.os.Bundle)1 Pair (android.util.Pair)1 OnIqPacketReceived (eu.siacs.conversations.xmpp.OnIqPacketReceived)1 InvalidJidException (eu.siacs.conversations.xmpp.jid.InvalidJidException)1 IqPacket (eu.siacs.conversations.xmpp.stanzas.IqPacket)1 File (java.io.File)1 Signature (java.security.Signature)1 X509Certificate (java.security.cert.X509Certificate)1 InvalidKeyException (org.whispersystems.libaxolotl.InvalidKeyException)1 InvalidKeyIdException (org.whispersystems.libaxolotl.InvalidKeyIdException)1