Search in sources :

Example 16 with PreKeyRecord

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

the class RefreshPreKeysJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws IOException {
    if (!TextSecurePreferences.isPushRegistered(context))
        return;
    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);
    PreKeyRecord lastResortKeyRecord = PreKeyUtil.generateLastResortKey(context);
    IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context);
    SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKey, false);
    Log.w(TAG, "Registering new prekeys...");
    accountManager.setPreKeys(identityKey.getPublicKey(), lastResortKeyRecord, signedPreKeyRecord, preKeyRecords);
    PreKeyUtil.setActiveSignedPreKeyId(context, signedPreKeyRecord.getId());
    TextSecurePreferences.setSignedPreKeyRegistered(context, true);
    ApplicationContext.getInstance(context).getJobManager().add(new CleanPreKeysJob(context));
}
Also used : PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Example 17 with PreKeyRecord

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

the class RegistrationService method handleCommonRegistration.

private void handleCommonRegistration(SignalServiceAccountManager accountManager, String number, String password, String signalingKey, boolean supportsGcm) throws IOException {
    setState(new RegistrationState(RegistrationState.STATE_GENERATING_KEYS, number));
    Recipient self = RecipientFactory.getRecipientsFromString(this, number, false).getPrimaryRecipient();
    IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(this);
    List<PreKeyRecord> records = PreKeyUtil.generatePreKeys(this);
    PreKeyRecord lastResort = PreKeyUtil.generateLastResortKey(this);
    SignedPreKeyRecord signedPreKey = PreKeyUtil.generateSignedPreKey(this, identityKey, true);
    accountManager.setPreKeys(identityKey.getPublicKey(), lastResort, signedPreKey, records);
    setState(new RegistrationState(RegistrationState.STATE_GCM_REGISTERING, number));
    if (supportsGcm) {
        String gcmRegistrationId = GoogleCloudMessaging.getInstance(this).register(GcmRefreshJob.REGISTRATION_ID);
        accountManager.setGcmId(Optional.of(gcmRegistrationId));
        TextSecurePreferences.setGcmRegistrationId(this, gcmRegistrationId);
        TextSecurePreferences.setGcmDisabled(this, false);
    } else {
        TextSecurePreferences.setGcmDisabled(this, true);
    }
    TextSecurePreferences.setWebsocketRegistered(this, true);
    DatabaseFactory.getIdentityDatabase(this).saveIdentity(self.getRecipientId(), identityKey.getPublicKey());
    DirectoryHelper.refreshDirectory(this, accountManager, number);
    DirectoryRefreshListener.schedule(this);
    RotateSignedPreKeyListener.schedule(this);
}
Also used : PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Example 18 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project Signal-Android by signalapp.

the class PreKeyUtil method generatePreKeys.

public static synchronized List<PreKeyRecord> generatePreKeys(Context context) {
    PreKeyStore preKeyStore = new TextSecurePreKeyStore(context);
    List<PreKeyRecord> records = new LinkedList<>();
    int preKeyIdOffset = TextSecurePreferences.getNextPreKeyId(context);
    for (int i = 0; i < BATCH_SIZE; i++) {
        int preKeyId = (preKeyIdOffset + i) % Medium.MAX_VALUE;
        ECKeyPair keyPair = Curve.generateKeyPair();
        PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
        preKeyStore.storePreKey(preKeyId, record);
        records.add(record);
    }
    TextSecurePreferences.setNextPreKeyId(context, (preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE);
    return records;
}
Also used : TextSecurePreKeyStore(org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) PreKeyStore(org.whispersystems.libsignal.state.PreKeyStore) TextSecurePreKeyStore(org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore) SignedPreKeyStore(org.whispersystems.libsignal.state.SignedPreKeyStore) LinkedList(java.util.LinkedList)

Example 19 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project Signal-Android by signalapp.

the class OneTimePreKeyDatabase method getPreKey.

@Nullable
public PreKeyRecord getPreKey(int keyId) {
    SQLiteDatabase database = databaseHelper.getReadableDatabase();
    try (Cursor cursor = database.query(TABLE_NAME, null, KEY_ID + " = ?", new String[] { String.valueOf(keyId) }, null, null, null)) {
        if (cursor != null && cursor.moveToFirst()) {
            try {
                ECPublicKey publicKey = Curve.decodePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PUBLIC_KEY))), 0);
                ECPrivateKey privateKey = Curve.decodePrivatePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PRIVATE_KEY))));
                return new PreKeyRecord(keyId, new ECKeyPair(publicKey, privateKey));
            } catch (InvalidKeyException | IOException e) {
                Log.w(TAG, e);
            }
        }
    }
    return null;
}
Also used : ECPrivateKey(org.whispersystems.libsignal.ecc.ECPrivateKey) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) IOException(java.io.IOException) Cursor(android.database.Cursor) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) Nullable(android.support.annotation.Nullable)

Example 20 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project toshi-android-client by toshiapp.

the class PreKeyUtil method generatePreKeys.

public static List<PreKeyRecord> generatePreKeys(Context context) {
    PreKeyStore preKeyStore = new SignalPreKeyStore();
    List<PreKeyRecord> records = new LinkedList<>();
    int preKeyIdOffset = getNextPreKeyId(context);
    for (int i = 0; i < BATCH_SIZE; i++) {
        int preKeyId = (preKeyIdOffset + i) % Medium.MAX_VALUE;
        ECKeyPair keyPair = Curve.generateKeyPair();
        PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
        preKeyStore.storePreKey(preKeyId, record);
        records.add(record);
    }
    setNextPreKeyId(context, (preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE);
    return records;
}
Also used : SignalPreKeyStore(com.toshi.crypto.signal.store.SignalPreKeyStore) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) SignalPreKeyStore(com.toshi.crypto.signal.store.SignalPreKeyStore) PreKeyStore(org.whispersystems.libsignal.state.PreKeyStore) SignedPreKeyStore(org.whispersystems.libsignal.state.SignedPreKeyStore) LinkedList(java.util.LinkedList)

Aggregations

PreKeyRecord (org.whispersystems.libsignal.state.PreKeyRecord)34 SignedPreKeyRecord (org.whispersystems.libsignal.state.SignedPreKeyRecord)33 LinkedList (java.util.LinkedList)9 IdentityKeyPair (org.whispersystems.libsignal.IdentityKeyPair)8 ECKeyPair (org.whispersystems.libsignal.ecc.ECKeyPair)8 InvalidKeyIdException (org.whispersystems.libsignal.InvalidKeyIdException)6 IOException (java.io.IOException)5 PreKeyStore (org.whispersystems.libsignal.state.PreKeyStore)5 SignedPreKeyStore (org.whispersystems.libsignal.state.SignedPreKeyStore)5 File (java.io.File)4 SignalServiceAccountManager (org.whispersystems.signalservice.api.SignalServiceAccountManager)4 SignedPreKeyEntity (org.whispersystems.signalservice.api.push.SignedPreKeyEntity)4 Cursor (android.database.Cursor)3 TextSecurePreKeyStore (org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore)3 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)3 ECPublicKey (org.whispersystems.libsignal.ecc.ECPublicKey)3 ContentValues (android.content.ContentValues)2 NonNull (androidx.annotation.NonNull)2 SignalPreKeyStore (com.toshi.crypto.signal.store.SignalPreKeyStore)2 Element (de.pixart.messenger.xml.Element)2