2012-08-08 02:03:00 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-08-08 02:03:00 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-08-08 02:03:00 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2013-11-10 12:15:29 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2013-08-18 01:37:18 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.IdentityKey;
|
|
|
|
import org.whispersystems.textsecure.crypto.InvalidKeyException;
|
|
|
|
import org.whispersystems.textsecure.crypto.MasterCipher;
|
|
|
|
import org.whispersystems.textsecure.crypto.MasterSecret;
|
2013-11-10 12:15:29 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.ecc.Curve;
|
2013-07-10 02:48:33 +00:00
|
|
|
import org.whispersystems.textsecure.util.Base64;
|
2012-08-08 02:03:00 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public class IdentityDatabase extends Database {
|
|
|
|
|
|
|
|
private static final Uri CHANGE_URI = Uri.parse("content://textsecure/identities");
|
2012-08-08 02:03:00 +00:00
|
|
|
|
|
|
|
private static final String TABLE_NAME = "identities";
|
2011-12-20 18:20:44 +00:00
|
|
|
private static final String ID = "_id";
|
2013-05-23 23:36:24 +00:00
|
|
|
public static final String RECIPIENT = "recipient";
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final String IDENTITY_KEY = "key";
|
|
|
|
public static final String MAC = "mac";
|
2012-08-08 02:03:00 +00:00
|
|
|
|
2013-05-23 23:36:24 +00:00
|
|
|
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
|
|
|
|
" (" + ID + " INTEGER PRIMARY KEY, " +
|
|
|
|
RECIPIENT + " INTEGER UNIQUE, " +
|
|
|
|
IDENTITY_KEY + " TEXT, " +
|
|
|
|
MAC + " TEXT);";
|
2012-08-08 02:03:00 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public IdentityDatabase(Context context, SQLiteOpenHelper databaseHelper) {
|
|
|
|
super(context, databaseHelper);
|
|
|
|
}
|
2012-08-08 02:03:00 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public Cursor getIdentities() {
|
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null);
|
2012-08-08 02:03:00 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (cursor != null)
|
|
|
|
cursor.setNotificationUri(context.getContentResolver(), CHANGE_URI);
|
2012-08-08 02:03:00 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return cursor;
|
|
|
|
}
|
2012-08-08 02:03:00 +00:00
|
|
|
|
2013-05-23 23:36:24 +00:00
|
|
|
public boolean isValidIdentity(MasterSecret masterSecret,
|
|
|
|
Recipient recipient,
|
|
|
|
IdentityKey theirIdentity)
|
|
|
|
{
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
2013-05-23 23:36:24 +00:00
|
|
|
String number = recipient.getNumber();
|
|
|
|
long recipientId = DatabaseFactory.getAddressDatabase(context).getCanonicalAddress(number);
|
|
|
|
MasterCipher masterCipher = new MasterCipher(masterSecret);
|
2011-12-20 18:20:44 +00:00
|
|
|
Cursor cursor = null;
|
2012-08-08 02:03:00 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2013-05-23 23:36:24 +00:00
|
|
|
cursor = database.query(TABLE_NAME, null, RECIPIENT + " = ?",
|
|
|
|
new String[] {recipientId+""}, null, null,null);
|
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
String serializedIdentity = cursor.getString(cursor.getColumnIndexOrThrow(IDENTITY_KEY));
|
|
|
|
String mac = cursor.getString(cursor.getColumnIndexOrThrow(MAC));
|
|
|
|
|
|
|
|
if (!masterCipher.verifyMacFor(recipientId + serializedIdentity, Base64.decode(mac))) {
|
|
|
|
Log.w("IdentityDatabase", "MAC failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdentityKey ourIdentity = new IdentityKey(Base64.decode(serializedIdentity), 0);
|
2013-11-10 12:15:29 +00:00
|
|
|
|
|
|
|
if (theirIdentity.getPublicKey().getType() == Curve.DJB_TYPE &&
|
|
|
|
ourIdentity.getPublicKey().getType() == Curve.NIST_TYPE)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-23 23:36:24 +00:00
|
|
|
return ourIdentity.equals(theirIdentity);
|
|
|
|
} else {
|
|
|
|
return true;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("IdentityDatabase", e);
|
2013-05-23 23:36:24 +00:00
|
|
|
return false;
|
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
Log.w("IdentityDatabase", e);
|
|
|
|
return false;
|
2011-12-20 18:20:44 +00:00
|
|
|
} finally {
|
2013-05-23 23:36:24 +00:00
|
|
|
if (cursor != null) {
|
2011-12-20 18:20:44 +00:00
|
|
|
cursor.close();
|
2013-05-23 23:36:24 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-08 02:03:00 +00:00
|
|
|
|
2013-05-23 23:36:24 +00:00
|
|
|
public void saveIdentity(MasterSecret masterSecret, Recipient recipient, IdentityKey identityKey)
|
|
|
|
{
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
2013-05-23 23:36:24 +00:00
|
|
|
String number = recipient.getNumber();
|
|
|
|
long recipientId = DatabaseFactory.getAddressDatabase(context).getCanonicalAddress(number);
|
2011-12-20 18:20:44 +00:00
|
|
|
MasterCipher masterCipher = new MasterCipher(masterSecret);
|
|
|
|
String identityKeyString = Base64.encodeBytes(identityKey.serialize());
|
2013-05-23 23:36:24 +00:00
|
|
|
String macString = Base64.encodeBytes(masterCipher.getMacFor(recipientId +
|
|
|
|
identityKeyString));
|
2012-08-08 02:03:00 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
ContentValues contentValues = new ContentValues();
|
2013-05-23 23:36:24 +00:00
|
|
|
contentValues.put(RECIPIENT, recipientId);
|
2011-12-20 18:20:44 +00:00
|
|
|
contentValues.put(IDENTITY_KEY, identityKeyString);
|
|
|
|
contentValues.put(MAC, macString);
|
2012-08-08 02:03:00 +00:00
|
|
|
|
2013-05-23 23:36:24 +00:00
|
|
|
database.replace(TABLE_NAME, null, contentValues);
|
2012-08-08 02:03:00 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
context.getContentResolver().notifyChange(CHANGE_URI, null);
|
|
|
|
}
|
|
|
|
|
2013-05-23 23:36:24 +00:00
|
|
|
public void deleteIdentity(long id) {
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
2013-05-23 23:36:24 +00:00
|
|
|
database.delete(TABLE_NAME, ID_WHERE, new String[] {id+""});
|
2012-08-08 02:03:00 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
context.getContentResolver().notifyChange(CHANGE_URI, null);
|
|
|
|
}
|
|
|
|
|
2013-05-23 23:36:24 +00:00
|
|
|
public Reader readerFor(MasterSecret masterSecret, Cursor cursor) {
|
|
|
|
return new Reader(masterSecret, cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Reader {
|
|
|
|
private final Cursor cursor;
|
|
|
|
private final MasterCipher cipher;
|
|
|
|
|
|
|
|
public Reader(MasterSecret masterSecret, Cursor cursor) {
|
|
|
|
this.cursor = cursor;
|
|
|
|
this.cipher = new MasterCipher(masterSecret);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Identity getCurrent() {
|
|
|
|
long recipientId = cursor.getLong(cursor.getColumnIndexOrThrow(RECIPIENT));
|
|
|
|
Recipients recipients = RecipientFactory.getRecipientsForIds(context, recipientId + "", true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
String identityKeyString = cursor.getString(cursor.getColumnIndexOrThrow(IDENTITY_KEY));
|
|
|
|
String mac = cursor.getString(cursor.getColumnIndexOrThrow(MAC));
|
|
|
|
|
|
|
|
if (!cipher.verifyMacFor(recipientId + identityKeyString, Base64.decode(mac))) {
|
|
|
|
return new Identity(recipients, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
IdentityKey identityKey = new IdentityKey(Base64.decode(identityKeyString), 0);
|
|
|
|
return new Identity(recipients, identityKey);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("IdentityDatabase", e);
|
|
|
|
return new Identity(recipients, null);
|
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
Log.w("IdentityDatabase", e);
|
|
|
|
return new Identity(recipients, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Identity {
|
|
|
|
private final Recipients recipients;
|
|
|
|
private final IdentityKey identityKey;
|
|
|
|
|
|
|
|
public Identity(Recipients recipients, IdentityKey identityKey) {
|
|
|
|
this.recipients = recipients;
|
|
|
|
this.identityKey = identityKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Recipients getRecipients() {
|
|
|
|
return recipients;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IdentityKey getIdentityKey() {
|
|
|
|
return identityKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|