Add account management interface to libtextsecure api

This commit is contained in:
Moxie Marlinspike 2014-11-09 20:35:08 -08:00
parent ae178fc4ec
commit 601e233d47
32 changed files with 463 additions and 491 deletions

View File

@ -0,0 +1,76 @@
package org.whispersystems.textsecure.api;
import org.whispersystems.libaxolotl.IdentityKey;
import org.whispersystems.libaxolotl.state.PreKeyRecord;
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.push.ContactTokenDetails;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.push.SignedPreKeyEntity;
import java.io.IOException;
import java.util.List;
import java.util.Set;
public class TextSecureAccountManager {
private final PushServiceSocket pushServiceSocket;
public TextSecureAccountManager(String url, PushServiceSocket.TrustStore trustStore,
String user, String password)
{
this.pushServiceSocket = new PushServiceSocket(url, trustStore, user, password);
}
public void setGcmId(Optional<String> gcmRegistrationId) throws IOException {
if (gcmRegistrationId.isPresent()) {
this.pushServiceSocket.registerGcmId(gcmRegistrationId.get());
} else {
this.pushServiceSocket.unregisterGcmId();
}
}
public void requestSmsVerificationCode() throws IOException {
this.pushServiceSocket.createAccount(false);
}
public void requestVoiceVerificationCode() throws IOException {
this.pushServiceSocket.createAccount(true);
}
public void verifyAccount(String verificationCode, String signalingKey,
boolean supportsSms, int axolotlRegistrationId)
throws IOException
{
this.pushServiceSocket.verifyAccount(verificationCode, signalingKey,
supportsSms, axolotlRegistrationId);
}
public void setPreKeys(IdentityKey identityKey, PreKeyRecord lastResortKey,
SignedPreKeyRecord signedPreKey, List<PreKeyRecord> oneTimePreKeys)
throws IOException
{
this.pushServiceSocket.registerPreKeys(identityKey, lastResortKey, signedPreKey, oneTimePreKeys);
}
public int getPreKeysCount() throws IOException {
return this.pushServiceSocket.getAvailablePreKeys();
}
public void setSignedPreKey(SignedPreKeyRecord signedPreKey) throws IOException {
this.pushServiceSocket.setCurrentSignedPreKey(signedPreKey);
}
public SignedPreKeyEntity getSignedPreKey() throws IOException {
return this.pushServiceSocket.getCurrentSignedPreKey();
}
public Optional<ContactTokenDetails> getContact(String contactToken) throws IOException {
return Optional.fromNullable(this.pushServiceSocket.getContactTokenDetails(contactToken));
}
public List<ContactTokenDetails> getContacts(Set<String> contactTokens) {
return this.pushServiceSocket.retrieveDirectory(contactTokens);
}
}

View File

@ -1,11 +1,9 @@
package org.whispersystems.textsecure.api;
import android.content.Context;
import org.whispersystems.libaxolotl.InvalidMessageException;
import org.whispersystems.libaxolotl.state.AxolotlStore;
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentPointer;
import org.whispersystems.textsecure.api.crypto.AttachmentCipherInputStream;
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentPointer;
import org.whispersystems.textsecure.push.PushServiceSocket;
import java.io.File;
@ -18,15 +16,14 @@ public class TextSecureMessageReceiver {
private final AxolotlStore axolotlStore;
private final PushServiceSocket socket;
public TextSecureMessageReceiver(Context context, String signalingKey, String url,
public TextSecureMessageReceiver(String signalingKey, String url,
PushServiceSocket.TrustStore trustStore,
String user, String password,
AxolotlStore axolotlStore)
{
this.axolotlStore = axolotlStore;
this.signalingKey = signalingKey;
this.socket = new PushServiceSocket(context, url, trustStore, user, password);
this.socket = new PushServiceSocket(url, trustStore, user, password);
}
public InputStream retrieveAttachment(TextSecureAttachmentPointer pointer, File destination)

View File

@ -1,6 +1,5 @@
package org.whispersystems.textsecure.api;
import android.content.Context;
import android.util.Log;
import com.google.protobuf.ByteString;
@ -11,12 +10,12 @@ import org.whispersystems.libaxolotl.protocol.CiphertextMessage;
import org.whispersystems.libaxolotl.state.AxolotlStore;
import org.whispersystems.libaxolotl.state.PreKeyBundle;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.crypto.TextSecureCipher;
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
import org.whispersystems.textsecure.api.messages.TextSecureAttachment;
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentStream;
import org.whispersystems.textsecure.api.messages.TextSecureGroup;
import org.whispersystems.textsecure.api.messages.TextSecureMessage;
import org.whispersystems.textsecure.api.crypto.TextSecureCipher;
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
import org.whispersystems.textsecure.push.MismatchedDevices;
import org.whispersystems.textsecure.push.OutgoingPushMessage;
import org.whispersystems.textsecure.push.OutgoingPushMessageList;
@ -48,17 +47,19 @@ public class TextSecureMessageSender {
private final AxolotlStore store;
private final Optional<EventListener> eventListener;
public TextSecureMessageSender(Context context, String url,
PushServiceSocket.TrustStore trustStore,
String user, String password,
AxolotlStore store,
public TextSecureMessageSender(String url, PushServiceSocket.TrustStore trustStore,
String user, String password, AxolotlStore store,
Optional<EventListener> eventListener)
{
this.socket = new PushServiceSocket(context, url, trustStore, user, password);
this.socket = new PushServiceSocket(url, trustStore, user, password);
this.store = store;
this.eventListener = eventListener;
}
public void sendDeliveryReceipt(PushAddress recipient, long messageId) throws IOException {
this.socket.sendReceipt(recipient.getNumber(), messageId, recipient.getRelay());
}
public void sendMessage(PushAddress recipient, TextSecureMessage message)
throws UntrustedIdentityException, IOException
{

View File

@ -1,8 +1,5 @@
package org.whispersystems.textsecure.push;
import android.content.Context;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.storage.RecipientDevice;
public class PushAddress extends RecipientDevice {
@ -24,9 +21,4 @@ public class PushAddress extends RecipientDevice {
return relay;
}
public static PushAddress create(Context context, long recipientId, String e164number, int deviceId) {
String relay = Directory.getInstance(context).getRelay(e164number);
return new PushAddress(recipientId, e164number, deviceId, relay);
}
}

View File

@ -16,7 +16,6 @@
*/
package org.whispersystems.textsecure.push;
import android.content.Context;
import android.util.Log;
import com.google.thoughtcrimegson.Gson;
@ -88,16 +87,14 @@ public class PushServiceSocket {
private static final boolean ENFORCE_SSL = true;
private final Context context;
private final String serviceUrl;
private final String localNumber;
private final String password;
private final TrustManager[] trustManagers;
public PushServiceSocket(Context context, String serviceUrl, TrustStore trustStore,
public PushServiceSocket(String serviceUrl, TrustStore trustStore,
String localNumber, String password)
{
this.context = context.getApplicationContext();
this.serviceUrl = serviceUrl;
this.localNumber = localNumber;
this.password = password;

View File

@ -54,8 +54,9 @@ import com.google.android.gms.gcm.GoogleCloudMessaging;
import org.thoughtcrime.securesms.components.OutgoingSmsPreference;
import org.thoughtcrime.securesms.contacts.ContactAccessor;
import org.thoughtcrime.securesms.contacts.ContactIdentityManager;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.service.KeyCachingService;
import org.thoughtcrime.securesms.util.Dialogs;
import org.thoughtcrime.securesms.util.DynamicLanguage;
@ -64,9 +65,9 @@ import org.thoughtcrime.securesms.util.MemoryCleaner;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Trimmer;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.TextSecureAccountManager;
import org.whispersystems.textsecure.push.exceptions.AuthorizationFailedException;
import org.whispersystems.textsecure.push.PushServiceSocket;
import java.io.IOException;
@ -362,10 +363,10 @@ public class ApplicationPreferencesActivity extends PassphraseRequiredActionBarA
@Override
protected Integer doInBackground(Void... params) {
try {
Context context = getActivity();
PushServiceSocket socket = PushServiceSocketFactory.create(context);
Context context = getActivity();
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);
socket.unregisterGcmId();
accountManager.setGcmId(Optional.<String>absent());
GoogleCloudMessaging.getInstance(context).unregister();
return SUCCESS;

View File

@ -62,10 +62,9 @@ import org.thoughtcrime.securesms.util.SelectedRecipientsAdapter;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.directory.NotInDirectoryException;
import org.thoughtcrime.securesms.database.TextSecureDirectory;
import org.thoughtcrime.securesms.database.NotInDirectoryException;
import org.whispersystems.textsecure.util.InvalidNumberException;
import org.whispersystems.textsecure.util.ListenableFutureTask;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
@ -77,7 +76,6 @@ import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import ws.com.google.android.mms.MmsException;
@ -171,7 +169,7 @@ public class GroupCreateActivity extends PassphraseRequiredActionBarActivity {
private static boolean isActiveInDirectory(Context context, Recipient recipient) {
try {
if (!Directory.getInstance(context).isActiveNumber(Util.canonicalizeNumber(context, recipient.getNumber()))) {
if (!TextSecureDirectory.getInstance(context).isActiveNumber(Util.canonicalizeNumber(context, recipient.getNumber()))) {
return false;
}
} catch (NotInDirectoryException e) {

View File

@ -30,13 +30,13 @@ import android.widget.TextView;
import android.widget.Toast;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.service.RegistrationService;
import org.thoughtcrime.securesms.util.Dialogs;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.whispersystems.textsecure.api.TextSecureAccountManager;
import org.whispersystems.textsecure.push.exceptions.ExpectationFailedException;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.push.exceptions.RateLimitException;
import org.whispersystems.textsecure.util.PhoneNumberFormatter;
import org.whispersystems.textsecure.util.Util;
@ -514,9 +514,11 @@ public class RegistrationProgressActivity extends ActionBarActivity {
@Override
protected Integer doInBackground(Void... params) {
try {
PushServiceSocket socket = PushServiceSocketFactory.create(context, e164number, password);
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);
int registrationId = TextSecurePreferences.getLocalRegistrationId(context);
socket.verifyAccount(code, signalingKey, true, registrationId);
accountManager.verifyAccount(code, signalingKey, true, registrationId);
return SUCCESS;
} catch (ExpectationFailedException e) {
Log.w("RegistrationProgressActivity", e);
@ -605,8 +607,8 @@ public class RegistrationProgressActivity extends ActionBarActivity {
@Override
protected Integer doInBackground(Void... params) {
try {
PushServiceSocket socket = PushServiceSocketFactory.create(context, e164number, password);
socket.createAccount(true);
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);
accountManager.requestVoiceVerificationCode();
return SUCCESS;
} catch (RateLimitException e) {

View File

@ -32,7 +32,7 @@ import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.telephony.PhoneNumberUtils;
import org.whispersystems.textsecure.directory.Directory;
import org.thoughtcrime.securesms.database.TextSecureDirectory;
import java.util.ArrayList;
import java.util.Collection;
@ -94,7 +94,7 @@ public class ContactAccessor {
final ContentResolver resolver = context.getContentResolver();
final String[] inProjection = new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME};
List<String> pushNumbers = Directory.getInstance(context).getActiveNumbers();
List<String> pushNumbers = TextSecureDirectory.getInstance(context).getActiveNumbers();
final Collection<ContactData> lookupData = new ArrayList<ContactData>(pushNumbers.size());
for (String pushNumber : pushNumbers) {

View File

@ -1,4 +1,4 @@
package org.whispersystems.textsecure.directory;
package org.thoughtcrime.securesms.database;
public class NotInDirectoryException extends Throwable {
}

View File

@ -1,4 +1,4 @@
package org.whispersystems.textsecure.directory;
package org.thoughtcrime.securesms.database;
import android.content.ContentValues;
import android.content.Context;
@ -19,7 +19,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Directory {
public class TextSecureDirectory {
private static final int INTRODUCED_CHANGE_FROM_TOKEN_TO_E164_NUMBER = 2;
@ -41,13 +41,13 @@ public class Directory {
TIMESTAMP + " INTEGER);";
private static final Object instanceLock = new Object();
private static volatile Directory instance;
private static volatile TextSecureDirectory instance;
public static Directory getInstance(Context context) {
public static TextSecureDirectory getInstance(Context context) {
if (instance == null) {
synchronized (instanceLock) {
if (instance == null) {
instance = new Directory(context);
instance = new TextSecureDirectory(context);
}
}
}
@ -58,7 +58,7 @@ public class Directory {
private final DatabaseHelper databaseHelper;
private final Context context;
private Directory(Context context) {
private TextSecureDirectory(Context context) {
this.context = context;
this.databaseHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}

View File

@ -10,7 +10,7 @@ import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.EncryptingPartDatabase;
import org.thoughtcrime.securesms.database.PartDatabase;
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
import org.thoughtcrime.securesms.push.TextSecureMessageReceiverFactory;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.jobqueue.JobParameters;
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
@ -89,12 +89,13 @@ public class AttachmentDownloadJob extends MasterSecretJob {
private void retrievePart(MasterSecret masterSecret, PduPart part, long messageId, long partId)
throws IOException
{
TextSecureMessageReceiver receiver = TextSecureMessageReceiverFactory.create(context, masterSecret);
TextSecureMessageReceiver receiver = TextSecureCommunicationFactory.createReceiver(context, masterSecret);
EncryptingPartDatabase database = DatabaseFactory.getEncryptingPartDatabase(context, masterSecret);
File attachmentFile = null;
try {
attachmentFile = createTempFile();
TextSecureAttachmentPointer pointer = createAttachmentPointer(masterSecret, part);
InputStream attachment = receiver.retrieveAttachment(pointer, attachmentFile);

View File

@ -4,16 +4,18 @@ import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import org.thoughtcrime.securesms.Release;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.GroupDatabase;
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.push.TextSecurePushTrustStore;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
import org.thoughtcrime.securesms.util.BitmapDecodingException;
import org.thoughtcrime.securesms.util.BitmapUtil;
import org.thoughtcrime.securesms.util.GroupUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.jobqueue.JobParameters;
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
import org.whispersystems.libaxolotl.InvalidMessageException;
@ -94,8 +96,12 @@ public class AvatarDownloadJob extends MasterSecretJob {
}
private File downloadAttachment(String relay, long contentLocation) throws IOException {
PushServiceSocket socket = PushServiceSocketFactory.create(context);
File destination = File.createTempFile("avatar", "tmp");
PushServiceSocket socket = new PushServiceSocket(Release.PUSH_URL,
new TextSecurePushTrustStore(context),
TextSecurePreferences.getLocalNumber(context),
TextSecurePreferences.getPushServerPassword(context));
File destination = File.createTempFile("avatar", "tmp");
destination.deleteOnExit();

View File

@ -0,0 +1,135 @@
package org.thoughtcrime.securesms.jobs;
import android.content.Context;
import android.util.Log;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.storage.TextSecureAxolotlStore;
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.util.VisibleForTesting;
import org.whispersystems.jobqueue.JobParameters;
import org.whispersystems.libaxolotl.InvalidKeyIdException;
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import org.whispersystems.libaxolotl.state.SignedPreKeyStore;
import org.whispersystems.textsecure.api.TextSecureAccountManager;
import org.whispersystems.textsecure.push.SignedPreKeyEntity;
import org.whispersystems.textsecure.push.exceptions.NonSuccessfulResponseCodeException;
import org.whispersystems.textsecure.push.exceptions.PushNetworkException;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class CleanPreKeysJob extends MasterSecretJob {
private static final String TAG = CleanPreKeysJob.class.getSimpleName();
private static final int ARCHIVE_AGE_DAYS = 15;
public CleanPreKeysJob(Context context) {
super(context, JobParameters.newBuilder()
.withGroupId(CleanPreKeysJob.class.getSimpleName())
.withRequirement(new MasterSecretRequirement(context))
.withRetryCount(5)
.create());
}
@Override
public void onAdded() {
}
@Override
public void onRun() throws RequirementNotMetException, IOException {
try {
MasterSecret masterSecret = getMasterSecret();
SignedPreKeyStore signedPreKeyStore = createSignedPreKeyStore(context, masterSecret);
TextSecureAccountManager accountManager = createAccountManager(context);
SignedPreKeyEntity currentSignedPreKey = accountManager.getSignedPreKey();
if (currentSignedPreKey == null) return;
SignedPreKeyRecord currentRecord = signedPreKeyStore.loadSignedPreKey(currentSignedPreKey.getKeyId());
List<SignedPreKeyRecord> allRecords = signedPreKeyStore.loadSignedPreKeys();
List<SignedPreKeyRecord> oldRecords = removeRecordFrom(currentRecord, allRecords);
Collections.sort(oldRecords, new SignedPreKeySorter());
Log.w(TAG, "Old signed prekey record count: " + oldRecords.size());
if (oldRecords.size() < 2) {
return;
}
SignedPreKeyRecord latestRecord = oldRecords.get(0);
long latestRecordArchiveDuration = System.currentTimeMillis() - latestRecord.getTimestamp();
if (latestRecordArchiveDuration >= TimeUnit.DAYS.toMillis(ARCHIVE_AGE_DAYS)) {
Iterator<SignedPreKeyRecord> iterator = oldRecords.iterator();
iterator.next();
while (iterator.hasNext()) {
SignedPreKeyRecord expiredRecord = iterator.next();
Log.w(TAG, "Removing signed prekey record: " + expiredRecord.getId() + " with timestamp: " + expiredRecord.getTimestamp());
signedPreKeyStore.removeSignedPreKey(expiredRecord.getId());
}
}
} catch (InvalidKeyIdException e) {
Log.w(TAG, e);
}
}
@Override
public boolean onShouldRetry(Throwable throwable) {
if (throwable instanceof RequirementNotMetException) return true;
if (throwable instanceof NonSuccessfulResponseCodeException) return false;
if (throwable instanceof PushNetworkException) return true;
return false;
}
@Override
public void onCanceled() {
Log.w(TAG, "Failed to execute clean signed prekeys task.");
}
private List<SignedPreKeyRecord> removeRecordFrom(SignedPreKeyRecord currentRecord,
List<SignedPreKeyRecord> records)
{
List<SignedPreKeyRecord> others = new LinkedList<>();
for (SignedPreKeyRecord record : records) {
if (record.getId() != currentRecord.getId()) {
others.add(record);
}
}
return others;
}
@VisibleForTesting
protected TextSecureAccountManager createAccountManager(Context context) {
return TextSecureCommunicationFactory.createManager(context);
}
protected SignedPreKeyStore createSignedPreKeyStore(Context context, MasterSecret masterSecret) {
return new TextSecureAxolotlStore(context, masterSecret);
}
private static class SignedPreKeySorter implements Comparator<SignedPreKeyRecord> {
@Override
public int compare(SignedPreKeyRecord lhs, SignedPreKeyRecord rhs) {
if (lhs.getTimestamp() < rhs.getTimestamp()) return -1;
else if (lhs.getTimestamp() > rhs.getTimestamp()) return 1;
else return 0;
}
}
}

View File

@ -4,7 +4,9 @@ import android.content.Context;
import android.util.Log;
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.PreKeyUtil;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.util.ParcelUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.jobqueue.EncryptionKeys;
@ -12,9 +14,7 @@ import org.whispersystems.jobqueue.JobParameters;
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
import org.whispersystems.libaxolotl.IdentityKeyPair;
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.PreKeyUtil;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.api.TextSecureAccountManager;
import java.io.IOException;
@ -43,11 +43,11 @@ public class CreateSignedPreKeyJob extends ContextJob {
return;
}
IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context, masterSecret);
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, masterSecret, identityKeyPair);
PushServiceSocket socket = PushServiceSocketFactory.create(context);
IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context, masterSecret);
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, masterSecret, identityKeyPair);
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);
socket.setCurrentSignedPreKey(signedPreKeyRecord);
accountManager.setSignedPreKey(signedPreKeyRecord);
TextSecurePreferences.setSignedPreKeyRegistered(context, true);
}

View File

@ -3,13 +3,19 @@ package org.thoughtcrime.securesms.jobs;
import android.content.Context;
import android.util.Log;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.Release;
import org.thoughtcrime.securesms.push.TextSecurePushTrustStore;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.jobqueue.JobParameters;
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.TextSecureMessageSender;
import org.whispersystems.textsecure.push.PushAddress;
import org.whispersystems.textsecure.push.exceptions.NonSuccessfulResponseCodeException;
import org.whispersystems.textsecure.push.exceptions.PushNetworkException;
import java.io.IOException;
public class DeliveryReceiptJob extends ContextJob {
private static final String TAG = DeliveryReceiptJob.class.getSimpleName();
@ -34,10 +40,17 @@ public class DeliveryReceiptJob extends ContextJob {
public void onAdded() {}
@Override
public void onRun() throws Throwable {
public void onRun() throws IOException {
Log.w("DeliveryReceiptJob", "Sending delivery receipt...");
PushServiceSocket socket = PushServiceSocketFactory.create(context);
socket.sendReceipt(destination, timestamp, relay);
TextSecureMessageSender messageSender =
new TextSecureMessageSender(Release.PUSH_URL,
new TextSecurePushTrustStore(context),
TextSecurePreferences.getLocalNumber(context),
TextSecurePreferences.getPushServerPassword(context),
null, Optional.<TextSecureMessageSender.EventListener>absent());
PushAddress pushAddress = new PushAddress(-1, destination, 1, relay);
messageSender.sendDeliveryReceipt(pushAddress, timestamp);
}
@Override

View File

@ -8,11 +8,12 @@ import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.jobqueue.JobParameters;
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.TextSecureAccountManager;
import org.whispersystems.textsecure.push.exceptions.NonSuccessfulResponseCodeException;
public class GcmRefreshJob extends ContextJob {
@ -40,10 +41,11 @@ public class GcmRefreshJob extends ContextJob {
Toast.makeText(context, "Unable to register with GCM!", Toast.LENGTH_LONG).show();
}
String gcmId = GoogleCloudMessaging.getInstance(context).register(REGISTRATION_ID);
PushServiceSocket socket = PushServiceSocketFactory.create(context);
String gcmId = GoogleCloudMessaging.getInstance(context).register(REGISTRATION_ID);
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);
accountManager.setGcmId(Optional.of(gcmId));
socket.registerGcmId(gcmId);
TextSecurePreferences.setGcmRegistrationId(context, gcmId);
}
}

View File

@ -10,7 +10,7 @@ import org.thoughtcrime.securesms.database.MmsSmsColumns;
import org.thoughtcrime.securesms.database.NoSuchMessageException;
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
import org.thoughtcrime.securesms.mms.PartParser;
import org.thoughtcrime.securesms.push.TextSecureMessageSenderFactory;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
import org.thoughtcrime.securesms.recipients.Recipients;
@ -106,7 +106,7 @@ public class PushGroupSendJob extends PushSendJob {
private void deliver(MasterSecret masterSecret, SendReq message)
throws IOException, RecipientFormattingException, InvalidNumberException, EncapsulatedExceptions
{
TextSecureMessageSender messageSender = TextSecureMessageSenderFactory.create(context, masterSecret);
TextSecureMessageSender messageSender = TextSecureCommunicationFactory.createSender(context, masterSecret);
byte[] groupId = GroupUtil.getDecodedId(message.getTo()[0].getString());
Recipients recipients = DatabaseFactory.getGroupDatabase(context).getGroupMembers(groupId, false);
List<PushAddress> addresses = getPushAddresses(recipients);

View File

@ -10,7 +10,7 @@ import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.MmsDatabase;
import org.thoughtcrime.securesms.database.NoSuchMessageException;
import org.thoughtcrime.securesms.mms.PartParser;
import org.thoughtcrime.securesms.push.TextSecureMessageSenderFactory;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
@ -98,7 +98,7 @@ public class PushMediaSendJob extends PushSendJob {
InsecureFallbackApprovalException, UntrustedIdentityException
{
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
TextSecureMessageSender messageSender = TextSecureMessageSenderFactory.create(context, masterSecret);
TextSecureMessageSender messageSender = TextSecureCommunicationFactory.createSender(context, masterSecret);
String destination = message.getTo()[0].getString();
boolean isSmsFallbackSupported = isSmsFallbackSupported(context, destination);

View File

@ -10,8 +10,8 @@ import org.whispersystems.jobqueue.JobManager;
import org.whispersystems.jobqueue.JobParameters;
import org.whispersystems.libaxolotl.InvalidVersionException;
import org.whispersystems.textsecure.api.messages.TextSecureEnvelope;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.directory.NotInDirectoryException;
import org.thoughtcrime.securesms.database.TextSecureDirectory;
import org.thoughtcrime.securesms.database.NotInDirectoryException;
import org.whispersystems.textsecure.push.ContactTokenDetails;
import java.io.IOException;
@ -40,7 +40,7 @@ public class PushReceiveJob extends ContextJob {
TextSecureEnvelope envelope = new TextSecureEnvelope(data, sessionKey);
if (!isActiveNumber(context, envelope.getSource())) {
Directory directory = Directory.getInstance(context);
TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
ContactTokenDetails contactTokenDetails = new ContactTokenDetails();
contactTokenDetails.setNumber(envelope.getSource());
@ -85,7 +85,7 @@ public class PushReceiveJob extends ContextJob {
boolean isActiveNumber;
try {
isActiveNumber = Directory.getInstance(context).isActiveNumber(e164number);
isActiveNumber = TextSecureDirectory.getInstance(context).isActiveNumber(e164number);
} catch (NotInDirectoryException e) {
isActiveNumber = false;
}

View File

@ -15,7 +15,7 @@ import org.whispersystems.jobqueue.JobParameters;
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
import org.whispersystems.textsecure.api.messages.TextSecureAttachment;
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentStream;
import org.whispersystems.textsecure.directory.Directory;
import org.thoughtcrime.securesms.database.TextSecureDirectory;
import org.whispersystems.textsecure.push.PushAddress;
import org.whispersystems.textsecure.util.InvalidNumberException;
@ -57,13 +57,13 @@ public abstract class PushSendJob extends MasterSecretJob {
return false;
}
Directory directory = Directory.getInstance(context);
TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
return directory.isSmsFallbackSupported(destination);
}
protected PushAddress getPushAddress(Recipient recipient) throws InvalidNumberException {
String e164number = Util.canonicalizeNumber(context, recipient.getNumber());
String relay = Directory.getInstance(context).getRelay(e164number);
String relay = TextSecureDirectory.getInstance(context).getRelay(e164number);
return new PushAddress(recipient.getRecipientId(), e164number, 1, relay);
}

View File

@ -11,7 +11,7 @@ import org.thoughtcrime.securesms.database.EncryptingSmsDatabase;
import org.thoughtcrime.securesms.database.NoSuchMessageException;
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
import org.thoughtcrime.securesms.notifications.MessageNotifier;
import org.thoughtcrime.securesms.push.TextSecureMessageSenderFactory;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.sms.IncomingIdentityUpdateMessage;
@ -85,7 +85,7 @@ public class PushTextSendJob extends PushSendJob {
try {
PushAddress address = getPushAddress(message.getIndividualRecipient());
TextSecureMessageSender messageSender = TextSecureMessageSenderFactory.create(context, masterSecret);
TextSecureMessageSender messageSender = TextSecureCommunicationFactory.createSender(context, masterSecret);
if (message.isEndSession()) {
messageSender.sendMessage(address, new TextSecureMessage(message.getDateSent(), null,

View File

@ -0,0 +1,85 @@
package org.thoughtcrime.securesms.jobs;
import android.content.Context;
import android.util.Log;
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.PreKeyUtil;
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.jobqueue.JobParameters;
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
import org.whispersystems.libaxolotl.IdentityKeyPair;
import org.whispersystems.libaxolotl.state.PreKeyRecord;
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import org.whispersystems.textsecure.api.TextSecureAccountManager;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.push.exceptions.NonSuccessfulResponseCodeException;
import org.whispersystems.textsecure.push.exceptions.PushNetworkException;
import java.io.IOException;
import java.util.List;
public class RefreshPreKeysJob extends MasterSecretJob {
private static final String TAG = RefreshPreKeysJob.class.getSimpleName();
private static final int PREKEY_MINIMUM = 10;
public RefreshPreKeysJob(Context context) {
super(context, JobParameters.newBuilder()
.withGroupId(RefreshPreKeysJob.class.getSimpleName())
.withRequirement(new NetworkRequirement(context))
.withRequirement(new MasterSecretRequirement(context))
.withRetryCount(5)
.create());
}
@Override
public void onAdded() {
}
@Override
public void onRun() throws RequirementNotMetException, IOException {
if (!TextSecurePreferences.isPushRegistered(context)) return;
MasterSecret masterSecret = getMasterSecret();
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);
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, masterSecret);
PreKeyRecord lastResortKeyRecord = PreKeyUtil.generateLastResortKey(context, masterSecret);
IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context, masterSecret);
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, masterSecret, identityKey);
Log.w(TAG, "Registering new prekeys...");
accountManager.setPreKeys(identityKey.getPublicKey(), lastResortKeyRecord, signedPreKeyRecord, preKeyRecords);
TextSecurePreferences.setSignedPreKeyRegistered(context, true);
// PreKeyService.initiateClean(context, masterSecret);
}
@Override
public boolean onShouldRetry(Throwable throwable) {
if (throwable instanceof RequirementNotMetException) return true;
if (throwable instanceof NonSuccessfulResponseCodeException) return false;
if (throwable instanceof PushNetworkException) return true;
return false;
}
@Override
public void onCanceled() {
}
}

View File

@ -1,22 +0,0 @@
package org.thoughtcrime.securesms.push;
import android.content.Context;
import org.thoughtcrime.securesms.Release;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.textsecure.push.PushServiceSocket;
public class PushServiceSocketFactory {
public static PushServiceSocket create(Context context, String number, String password) {
return new PushServiceSocket(context, Release.PUSH_URL, new TextSecurePushTrustStore(context),
number, password);
}
public static PushServiceSocket create(Context context) {
return create(context,
TextSecurePreferences.getLocalNumber(context),
TextSecurePreferences.getPushServerPassword(context));
}
}

View File

@ -11,13 +11,15 @@ import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.TextSecureAccountManager;
import org.whispersystems.textsecure.api.TextSecureMessageReceiver;
import org.whispersystems.textsecure.api.TextSecureMessageSender;
import static org.whispersystems.textsecure.api.TextSecureMessageSender.EventListener;
public class TextSecureMessageSenderFactory {
public static TextSecureMessageSender create(Context context, MasterSecret masterSecret) {
return new TextSecureMessageSender(context, Release.PUSH_URL,
public class TextSecureCommunicationFactory {
public static TextSecureMessageSender createSender(Context context, MasterSecret masterSecret) {
return new TextSecureMessageSender(Release.PUSH_URL,
new TextSecurePushTrustStore(context),
TextSecurePreferences.getLocalNumber(context),
TextSecurePreferences.getPushServerPassword(context),
@ -25,6 +27,27 @@ public class TextSecureMessageSenderFactory {
Optional.of((EventListener)new SecurityEventListener(context)));
}
public static TextSecureMessageReceiver createReceiver(Context context, MasterSecret masterSecret) {
return new TextSecureMessageReceiver(TextSecurePreferences.getSignalingKey(context),
Release.PUSH_URL,
new TextSecurePushTrustStore(context),
TextSecurePreferences.getLocalNumber(context),
TextSecurePreferences.getPushServerPassword(context),
new TextSecureAxolotlStore(context, masterSecret));
}
public static TextSecureAccountManager createManager(Context context) {
return new TextSecureAccountManager(Release.PUSH_URL,
new TextSecurePushTrustStore(context),
TextSecurePreferences.getLocalNumber(context),
TextSecurePreferences.getPushServerPassword(context));
}
public static TextSecureAccountManager createManager(Context context, String number, String password) {
return new TextSecureAccountManager(Release.PUSH_URL, new TextSecurePushTrustStore(context),
number, password);
}
private static class SecurityEventListener implements EventListener {
private final Context context;

View File

@ -1,21 +0,0 @@
package org.thoughtcrime.securesms.push;
import android.content.Context;
import org.thoughtcrime.securesms.Release;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.storage.TextSecureAxolotlStore;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.textsecure.api.TextSecureMessageReceiver;
public class TextSecureMessageReceiverFactory {
public static TextSecureMessageReceiver create(Context context, MasterSecret masterSecret) {
return new TextSecureMessageReceiver(context,
TextSecurePreferences.getSignalingKey(context),
Release.PUSH_URL,
new TextSecurePushTrustStore(context),
TextSecurePreferences.getLocalNumber(context),
TextSecurePreferences.getPushServerPassword(context),
new TextSecureAxolotlStore(context, masterSecret));
}
}

View File

@ -7,7 +7,6 @@ import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.util.DirectoryHelper;
import java.util.concurrent.Executor;

View File

@ -1,83 +0,0 @@
package org.thoughtcrime.securesms.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.util.Dialogs;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.textsecure.push.PushServiceSocket;
import java.io.IOException;
import java.sql.Connection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class GcmRegistrationService extends Service implements Runnable {
private static final String TAG = GcmRegistrationService.class.getSimpleName();
public static final String REGISTRATION_ID = "312334754206";
private ExecutorService executor;
@Override
public void onCreate() {
super.onCreate();
this.executor = Executors.newSingleThreadExecutor();
}
@Override
public int onStartCommand(Intent intent, int flats, int startId) {
executor.execute(this);
return START_NOT_STICKY;
}
@Override
public void run() {
Log.w(TAG, "Running GCM Registration Service...");
try {
String registrationId = TextSecurePreferences.getGcmRegistrationId(this);
if (registrationId == null) {
Log.w(TAG, "GCM registrationId expired, reregistering...");
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (result != ConnectionResult.SUCCESS) {
Log.w(TAG, "Unable to register with GCM! " + result);
return;
}
String gcmId = GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID);
PushServiceSocket socket = PushServiceSocketFactory.create(this);
socket.registerGcmId(gcmId);
TextSecurePreferences.setGcmRegistrationId(this, gcmId);
stopSelf();
}
} catch (IOException e) {
Log.w(TAG, e);
}
}
@Override
public void onDestroy() {
super.onDestroy();
executor.shutdown();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

View File

@ -1,230 +0,0 @@
package org.thoughtcrime.securesms.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.libaxolotl.IdentityKeyPair;
import org.whispersystems.libaxolotl.InvalidKeyIdException;
import org.whispersystems.libaxolotl.state.PreKeyRecord;
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import org.whispersystems.libaxolotl.state.SignedPreKeyStore;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.PreKeyUtil;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.push.SignedPreKeyEntity;
import org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class PreKeyService extends Service {
private static final String TAG = PreKeyService.class.getSimpleName();
public static final String REFRESH_ACTION = "org.thoughtcrime.securesms.PreKeyService.REFRESH";
public static final String CLEAN_ACTION = "org.thoughtcrime.securesms.PreKeyService.CLEAN";
public static final String CREATE_SIGNED_ACTION = "org.thoughtcrime.securesms.PreKeyService.CREATE_SIGNED";
private static final int PREKEY_MINIMUM = 10;
private final Executor executor = Executors.newSingleThreadExecutor();
public static void initiateRefresh(Context context, MasterSecret masterSecret) {
Intent intent = new Intent(context, PreKeyService.class);
intent.setAction(PreKeyService.REFRESH_ACTION);
intent.putExtra("master_secret", masterSecret);
context.startService(intent);
}
public static void initiateClean(Context context, MasterSecret masterSecret) {
Intent intent = new Intent(context, PreKeyService.class);
intent.setAction(PreKeyService.CLEAN_ACTION);
intent.putExtra("master_secret", masterSecret);
context.startService(intent);
}
public static void initiateCreateSigned(Context context, MasterSecret masterSecret) {
Intent intent = new Intent(context, PreKeyService.class);
intent.setAction(PreKeyService.CREATE_SIGNED_ACTION);
intent.putExtra("master_secret", masterSecret);
context.startService(intent);
}
@Override
public int onStartCommand(Intent intent, int flats, int startId) {
if (intent == null) return START_NOT_STICKY;
if (intent.getAction() == null) return START_NOT_STICKY;
MasterSecret masterSecret = intent.getParcelableExtra("master_secret");
if (masterSecret == null) {
Log.w(TAG, "No master secret!");
return START_NOT_STICKY;
}
switch (intent.getAction()) {
case REFRESH_ACTION: executor.execute(new RefreshTask(this, masterSecret)); break;
case CLEAN_ACTION: executor.execute(new CleanSignedPreKeysTask(this, masterSecret)); break;
case CREATE_SIGNED_ACTION: executor.execute(new CreateSignedPreKeyTask(this, masterSecret)); break;
}
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private static class CreateSignedPreKeyTask implements Runnable {
private final Context context;
private final MasterSecret masterSecret;
public CreateSignedPreKeyTask(Context context, MasterSecret masterSecret) {
this.context = context;
this.masterSecret = masterSecret;
}
@Override
public void run() {
if (TextSecurePreferences.isSignedPreKeyRegistered(context)) {
Log.w(TAG, "Signed prekey already registered...");
return;
}
try {
IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context, masterSecret);
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, masterSecret, identityKeyPair);
PushServiceSocket socket = PushServiceSocketFactory.create(context);
socket.setCurrentSignedPreKey(signedPreKeyRecord);
TextSecurePreferences.setSignedPreKeyRegistered(context, true);
} catch (IOException e) {
Log.w(TAG, e);
}
}
}
static class CleanSignedPreKeysTask implements Runnable {
private final SignedPreKeyStore signedPreKeyStore;
private final PushServiceSocket socket;
public CleanSignedPreKeysTask(Context context, MasterSecret masterSecret) {
this(new TextSecurePreKeyStore(context, masterSecret), PushServiceSocketFactory.create(context));
}
public CleanSignedPreKeysTask(SignedPreKeyStore signedPreKeyStore, PushServiceSocket socket) {
this.signedPreKeyStore = signedPreKeyStore;
this.socket = socket;
}
public void run() {
try {
SignedPreKeyEntity currentSignedPreKey = socket.getCurrentSignedPreKey();
if (currentSignedPreKey == null) return;
SignedPreKeyRecord currentRecord = signedPreKeyStore.loadSignedPreKey(currentSignedPreKey.getKeyId());
List<SignedPreKeyRecord> allRecords = signedPreKeyStore.loadSignedPreKeys();
List<SignedPreKeyRecord> oldRecords = removeCurrentRecord(allRecords, currentRecord);
SignedPreKeyRecord[] oldRecordsArray = oldRecords.toArray(new SignedPreKeyRecord[0]);
Arrays.sort(oldRecordsArray, new SignedPreKeySorter());
Log.w(TAG, "Existing signed prekey record count: " + oldRecordsArray.length);
if (oldRecordsArray.length > 3) {
long oldTimestamp = System.currentTimeMillis() - (14 * 24 * 60 * 60 * 1000);
SignedPreKeyRecord[] deletionCandidates = Arrays.copyOf(oldRecordsArray, oldRecordsArray.length - 1);
for (SignedPreKeyRecord deletionCandidate : deletionCandidates) {
Log.w(TAG, "Old signed prekey record timestamp: " + deletionCandidate.getTimestamp());
if (deletionCandidate.getTimestamp() <= oldTimestamp) {
Log.w(TAG, "Remove signed prekey record: " + deletionCandidate.getId());
signedPreKeyStore.removeSignedPreKey(deletionCandidate.getId());
}
}
}
} catch (IOException | InvalidKeyIdException e) {
Log.w(TAG, e);
}
}
private List<SignedPreKeyRecord> removeCurrentRecord(List<SignedPreKeyRecord> records,
SignedPreKeyRecord currentRecord)
{
List<SignedPreKeyRecord> others = new LinkedList<>();
for (SignedPreKeyRecord record : records) {
if (record.getId() != currentRecord.getId()) {
others.add(record);
}
}
return others;
}
}
private static class RefreshTask implements Runnable {
private final Context context;
private final MasterSecret masterSecret;
public RefreshTask(Context context, MasterSecret masterSecret) {
this.context = context.getApplicationContext();
this.masterSecret = masterSecret;
}
public void run() {
try {
if (!TextSecurePreferences.isPushRegistered(context)) return;
PushServiceSocket socket = PushServiceSocketFactory.create(context);
int availableKeys = socket.getAvailablePreKeys();
if (availableKeys >= PREKEY_MINIMUM && TextSecurePreferences.isSignedPreKeyRegistered(context)) {
Log.w(TAG, "Available keys sufficient: " + availableKeys);
return;
}
List<PreKeyRecord> preKeyRecords = PreKeyUtil.generatePreKeys(context, masterSecret);
PreKeyRecord lastResortKeyRecord = PreKeyUtil.generateLastResortKey(context, masterSecret);
IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context, masterSecret);
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, masterSecret, identityKey);
Log.w(TAG, "Registering new prekeys...");
socket.registerPreKeys(identityKey.getPublicKey(), lastResortKeyRecord,
signedPreKeyRecord, preKeyRecords);
TextSecurePreferences.setSignedPreKeyRegistered(context, true);
PreKeyService.initiateClean(context, masterSecret);
} catch (IOException e) {
Log.w(TAG, e);
}
}
}
private static class SignedPreKeySorter implements Comparator<SignedPreKeyRecord> {
@Override
public int compare(SignedPreKeyRecord lhs, SignedPreKeyRecord rhs) {
if (lhs.getTimestamp() < rhs.getTimestamp()) return -1;
else if (lhs.getTimestamp() > rhs.getTimestamp()) return 1;
else return 0;
}
}
}

View File

@ -14,17 +14,18 @@ import com.google.android.gms.gcm.GoogleCloudMessaging;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.PreKeyUtil;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.util.DirectoryHelper;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.libaxolotl.IdentityKeyPair;
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import org.whispersystems.libaxolotl.state.PreKeyRecord;
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import org.whispersystems.libaxolotl.util.KeyHelper;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.PreKeyUtil;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.TextSecureAccountManager;
import org.whispersystems.textsecure.push.exceptions.ExpectationFailedException;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.util.Util;
import java.io.IOException;
@ -153,9 +154,9 @@ public class RegistrationService extends Service {
MasterSecret masterSecret = intent.getParcelableExtra("master_secret");
try {
PushServiceSocket socket = PushServiceSocketFactory.create(this, number, password);
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(this, number, password);
handleCommonRegistration(masterSecret, socket, number);
handleCommonRegistration(masterSecret, accountManager, number);
markAsVerified(number, password, signalingKey);
@ -191,14 +192,14 @@ public class RegistrationService extends Service {
initializeChallengeListener();
setState(new RegistrationState(RegistrationState.STATE_CONNECTING, number));
PushServiceSocket socket = PushServiceSocketFactory.create(this, number, password);
socket.createAccount(false);
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(this, number, password);
accountManager.requestSmsVerificationCode();
setState(new RegistrationState(RegistrationState.STATE_VERIFYING, number));
String challenge = waitForChallenge();
socket.verifyAccount(challenge, signalingKey, true, registrationId);
accountManager.verifyAccount(challenge, signalingKey, true, registrationId);
handleCommonRegistration(masterSecret, socket, number);
handleCommonRegistration(masterSecret, accountManager, number);
markAsVerified(number, password, signalingKey);
setState(new RegistrationState(RegistrationState.STATE_COMPLETE, number));
@ -224,7 +225,7 @@ public class RegistrationService extends Service {
}
}
private void handleCommonRegistration(MasterSecret masterSecret, PushServiceSocket socket, String number)
private void handleCommonRegistration(MasterSecret masterSecret, TextSecureAccountManager accountManager, String number)
throws IOException
{
setState(new RegistrationState(RegistrationState.STATE_GENERATING_KEYS, number));
@ -232,15 +233,15 @@ public class RegistrationService extends Service {
List<PreKeyRecord> records = PreKeyUtil.generatePreKeys(this, masterSecret);
PreKeyRecord lastResort = PreKeyUtil.generateLastResortKey(this, masterSecret);
SignedPreKeyRecord signedPreKey = PreKeyUtil.generateSignedPreKey(this, masterSecret, identityKey);
socket.registerPreKeys(identityKey.getPublicKey(), lastResort, signedPreKey, records);
accountManager.setPreKeys(identityKey.getPublicKey(),lastResort, signedPreKey, records);
setState(new RegistrationState(RegistrationState.STATE_GCM_REGISTERING, number));
String gcmRegistrationId = GoogleCloudMessaging.getInstance(this).register("312334754206");
TextSecurePreferences.setGcmRegistrationId(this, gcmRegistrationId);
socket.registerGcmId(gcmRegistrationId);
accountManager.setGcmId(Optional.of(gcmRegistrationId));
DirectoryHelper.refreshDirectory(this, socket, number);
DirectoryHelper.refreshDirectory(this, accountManager, number);
DirectoryRefreshListener.schedule(this);
}

View File

@ -25,6 +25,8 @@ import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.EncryptingSmsDatabase;
import org.thoughtcrime.securesms.database.MmsDatabase;
import org.thoughtcrime.securesms.database.NotInDirectoryException;
import org.thoughtcrime.securesms.database.TextSecureDirectory;
import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.database.model.MessageRecord;
import org.thoughtcrime.securesms.jobs.MmsSendJob;
@ -33,17 +35,16 @@ import org.thoughtcrime.securesms.jobs.PushMediaSendJob;
import org.thoughtcrime.securesms.jobs.PushTextSendJob;
import org.thoughtcrime.securesms.jobs.SmsSendJob;
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.GroupUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.jobqueue.JobManager;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.directory.NotInDirectoryException;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.TextSecureAccountManager;
import org.whispersystems.textsecure.push.ContactTokenDetails;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.util.DirectoryUtil;
import org.whispersystems.textsecure.util.InvalidNumberException;
@ -263,24 +264,24 @@ public class MessageSender {
}
private static boolean isPushDestination(Context context, String destination) {
Directory directory = Directory.getInstance(context);
TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
try {
return directory.isActiveNumber(destination);
} catch (NotInDirectoryException e) {
try {
PushServiceSocket socket = PushServiceSocketFactory.create(context);
String contactToken = DirectoryUtil.getDirectoryServerToken(destination);
ContactTokenDetails registeredUser = socket.getContactTokenDetails(contactToken);
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);
String contactToken = DirectoryUtil.getDirectoryServerToken(destination);
Optional<ContactTokenDetails> registeredUser = accountManager.getContact(contactToken);
if (registeredUser == null) {
registeredUser = new ContactTokenDetails();
registeredUser.setNumber(destination);
directory.setNumber(registeredUser, false);
if (!registeredUser.isPresent()) {
registeredUser = Optional.of(new ContactTokenDetails());
registeredUser.get().setNumber(destination);
directory.setNumber(registeredUser.get(), false);
return false;
} else {
registeredUser.setNumber(destination);
directory.setNumber(registeredUser, true);
registeredUser.get().setNumber(destination);
directory.setNumber(registeredUser.get(), true);
return true;
}
} catch (IOException e1) {

View File

@ -5,13 +5,12 @@ import android.util.Log;
import android.widget.Toast;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.database.NotInDirectoryException;
import org.thoughtcrime.securesms.database.TextSecureDirectory;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.whispersystems.textsecure.directory.Directory;
import org.whispersystems.textsecure.directory.NotInDirectoryException;
import org.whispersystems.textsecure.api.TextSecureAccountManager;
import org.whispersystems.textsecure.push.ContactTokenDetails;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.util.DirectoryUtil;
import org.whispersystems.textsecure.util.InvalidNumberException;
@ -54,19 +53,18 @@ public class DirectoryHelper {
}
public static void refreshDirectory(final Context context) {
refreshDirectory(context, PushServiceSocketFactory.create(context));
refreshDirectory(context, TextSecureCommunicationFactory.createManager(context));
}
public static void refreshDirectory(final Context context, final PushServiceSocket socket) {
refreshDirectory(context, socket, TextSecurePreferences.getLocalNumber(context));
public static void refreshDirectory(final Context context, final TextSecureAccountManager accountManager) {
refreshDirectory(context, accountManager, TextSecurePreferences.getLocalNumber(context));
}
public static void refreshDirectory(final Context context, final PushServiceSocket socket, final String localNumber) {
Directory directory = Directory.getInstance(context);
public static void refreshDirectory(final Context context, final TextSecureAccountManager accountManager, final String localNumber) {
TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
Set<String> eligibleContactNumbers = directory.getPushEligibleContactNumbers(localNumber);
Map<String, String> tokenMap = DirectoryUtil.getDirectoryServerTokenMap(eligibleContactNumbers);
List<ContactTokenDetails> activeTokens = socket.retrieveDirectory(tokenMap.keySet());
List<ContactTokenDetails> activeTokens = accountManager.getContacts(tokenMap.keySet());
if (activeTokens != null) {
for (ContactTokenDetails activeToken : activeTokens) {
@ -104,7 +102,7 @@ public class DirectoryHelper {
final String e164number = Util.canonicalizeNumber(context, number);
return Directory.getInstance(context).isActiveNumber(e164number);
return TextSecureDirectory.getInstance(context).isActiveNumber(e164number);
} catch (InvalidNumberException e) {
Log.w(TAG, e);
return false;