session-android/src/org/thoughtcrime/securesms/database/EncryptingSmsDatabase.java

79 lines
3.4 KiB
Java
Raw Normal View History

2012-10-01 02:56:29 +00:00
/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
2012-10-01 02:56:29 +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-10-01 02:56:29 +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;
2012-10-01 02:56:29 +00:00
import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
2011-12-20 18:20:44 +00:00
import org.thoughtcrime.securesms.crypto.AsymmetricMasterCipher;
import org.thoughtcrime.securesms.crypto.AsymmetricMasterSecret;
import org.thoughtcrime.securesms.crypto.MasterCipher;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.protocol.Prefix;
import org.thoughtcrime.securesms.sms.TextMessage;
2011-12-20 18:20:44 +00:00
public class EncryptingSmsDatabase extends SmsDatabase {
public EncryptingSmsDatabase(Context context, SQLiteOpenHelper databaseHelper) {
super(context, databaseHelper);
2012-10-01 02:56:29 +00:00
}
2011-12-20 18:20:44 +00:00
private String getAsymmetricEncryptedBody(AsymmetricMasterSecret masterSecret, String body) {
AsymmetricMasterCipher bodyCipher = new AsymmetricMasterCipher(masterSecret);
return Prefix.ASYMMETRIC_LOCAL_ENCRYPT + bodyCipher.encryptBody(body);
}
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
private String getEncryptedBody(MasterSecret masterSecret, String body) {
MasterCipher bodyCipher = new MasterCipher(masterSecret);
2012-10-01 02:56:29 +00:00
return Prefix.SYMMETRIC_ENCRYPT + bodyCipher.encryptBody(body);
2011-12-20 18:20:44 +00:00
}
private long insertMessageSent(MasterSecret masterSecret, String address, long threadId, String body, long date, int type) {
String encryptedBody = getEncryptedBody(masterSecret, body);
2012-10-01 02:56:29 +00:00
return insertMessageSent(address, threadId, encryptedBody, date, type);
2011-12-20 18:20:44 +00:00
}
public void updateSecureMessageBody(MasterSecret masterSecret, long messageId, String body) {
String encryptedBody = getEncryptedBody(masterSecret, body);
updateMessageBodyAndType(messageId, encryptedBody, Types.SECURE_RECEIVED_TYPE);
}
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
public void updateMessageBody(MasterSecret masterSecret, long messageId, String body) {
String encryptedBody = getEncryptedBody(masterSecret, body);
updateMessageBodyAndType(messageId, encryptedBody, Types.INBOX_TYPE);
}
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
public long insertMessageSent(MasterSecret masterSecret, String address, long threadId, String body, long date) {
return insertMessageSent(masterSecret, address, threadId, body, date, Types.ENCRYPTED_OUTBOX_TYPE);
}
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
public long insertSecureMessageSent(MasterSecret masterSecret, String address, long threadId, String body, long date) {
return insertMessageSent(masterSecret, address, threadId, body, date, Types.ENCRYPTING_TYPE);
}
2012-10-01 02:56:29 +00:00
public long insertMessageReceived(MasterSecret masterSecret, TextMessage message, String body) {
2011-12-20 18:20:44 +00:00
String encryptedBody = getEncryptedBody(masterSecret, body);
return insertMessageReceived(message, encryptedBody);
}
2012-10-01 02:56:29 +00:00
public long insertMessageReceived(AsymmetricMasterSecret masterSecret, TextMessage message, String body) {
2011-12-20 18:20:44 +00:00
String encryptedBody = getAsymmetricEncryptedBody(masterSecret, body);
return insertSecureMessageReceived(message, encryptedBody);
}
}