Search in sources :

Example 1 with PreKeyRecord

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

the class PreKeyUtil method generatePreKeys.

public static List<PreKeyRecord> generatePreKeys(Context context) {
    PreKeyStore preKeyStore = new TextSecurePreKeyStore(context);
    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 : 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 2 with PreKeyRecord

use of org.whispersystems.libsignal.state.PreKeyRecord in project libsignal-service-java by signalapp.

the class PushServiceSocket method registerPreKeys.

public void registerPreKeys(IdentityKey identityKey, SignedPreKeyRecord signedPreKey, List<PreKeyRecord> records) throws IOException {
    List<PreKeyEntity> entities = new LinkedList<>();
    for (PreKeyRecord record : records) {
        PreKeyEntity entity = new PreKeyEntity(record.getId(), record.getKeyPair().getPublicKey());
        entities.add(entity);
    }
    SignedPreKeyEntity signedPreKeyEntity = new SignedPreKeyEntity(signedPreKey.getId(), signedPreKey.getKeyPair().getPublicKey(), signedPreKey.getSignature());
    makeServiceRequest(String.format(PREKEY_PATH, ""), "PUT", JsonUtil.toJson(new PreKeyState(entities, signedPreKeyEntity, identityKey)));
}
Also used : SignedPreKeyEntity(org.whispersystems.signalservice.api.push.SignedPreKeyEntity) SignedPreKeyEntity(org.whispersystems.signalservice.api.push.SignedPreKeyEntity) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) LinkedList(java.util.LinkedList)

Example 3 with PreKeyRecord

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

the class RegistrationActivity method verifyAccount.

private void verifyAccount(@NonNull String code, @Nullable String pin) throws IOException {
    int registrationId = KeyHelper.generateRegistrationId(false);
    TextSecurePreferences.setLocalRegistrationId(RegistrationActivity.this, registrationId);
    SessionUtil.archiveAllSessions(RegistrationActivity.this);
    String signalingKey = Util.getSecret(52);
    accountManager.verifyAccountWithCode(code, signalingKey, registrationId, !registrationState.gcmToken.isPresent(), pin);
    IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(RegistrationActivity.this);
    List<PreKeyRecord> records = PreKeyUtil.generatePreKeys(RegistrationActivity.this);
    SignedPreKeyRecord signedPreKey = PreKeyUtil.generateSignedPreKey(RegistrationActivity.this, identityKey, true);
    accountManager.setPreKeys(identityKey.getPublicKey(), signedPreKey, records);
    if (registrationState.gcmToken.isPresent()) {
        accountManager.setGcmId(registrationState.gcmToken);
    }
    TextSecurePreferences.setGcmRegistrationId(RegistrationActivity.this, registrationState.gcmToken.orNull());
    TextSecurePreferences.setGcmDisabled(RegistrationActivity.this, !registrationState.gcmToken.isPresent());
    TextSecurePreferences.setWebsocketRegistered(RegistrationActivity.this, true);
    DatabaseFactory.getIdentityDatabase(RegistrationActivity.this).saveIdentity(Address.fromSerialized(registrationState.e164number), identityKey.getPublicKey(), IdentityDatabase.VerifiedStatus.VERIFIED, true, System.currentTimeMillis(), true);
    TextSecurePreferences.setVerifying(RegistrationActivity.this, false);
    TextSecurePreferences.setPushRegistered(RegistrationActivity.this, true);
    TextSecurePreferences.setLocalNumber(RegistrationActivity.this, registrationState.e164number);
    TextSecurePreferences.setPushServerPassword(RegistrationActivity.this, registrationState.password);
    TextSecurePreferences.setSignalingKey(RegistrationActivity.this, signalingKey);
    TextSecurePreferences.setSignedPreKeyRegistered(RegistrationActivity.this, true);
    TextSecurePreferences.setPromptedPushRegistration(RegistrationActivity.this, true);
    TextSecurePreferences.setUnauthorizedReceived(RegistrationActivity.this, false);
}
Also used : SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SpannableString(android.text.SpannableString) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) TextPaint(android.text.TextPaint) SuppressLint(android.annotation.SuppressLint)

Example 4 with PreKeyRecord

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

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);
    IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context);
    SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKey, false);
    Log.w(TAG, "Registering new prekeys...");
    accountManager.setPreKeys(identityKey.getPublicKey(), 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 5 with PreKeyRecord

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

the class PreKeyMigrationHelper method migratePreKeys.

static boolean migratePreKeys(Context context, SQLiteDatabase database) {
    File[] preKeyFiles = getPreKeyDirectory(context).listFiles();
    boolean clean = true;
    if (preKeyFiles != null) {
        for (File preKeyFile : preKeyFiles) {
            if (!"index.dat".equals(preKeyFile.getName())) {
                try {
                    PreKeyRecord preKey = new PreKeyRecord(loadSerializedRecord(preKeyFile));
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(OneTimePreKeyDatabase.KEY_ID, preKey.getId());
                    contentValues.put(OneTimePreKeyDatabase.PUBLIC_KEY, Base64.encodeBytes(preKey.getKeyPair().getPublicKey().serialize()));
                    contentValues.put(OneTimePreKeyDatabase.PRIVATE_KEY, Base64.encodeBytes(preKey.getKeyPair().getPrivateKey().serialize()));
                    database.insert(OneTimePreKeyDatabase.TABLE_NAME, null, contentValues);
                    Log.w(TAG, "Migrated one-time prekey: " + preKey.getId());
                } catch (IOException | InvalidMessageException e) {
                    Log.w(TAG, e);
                    clean = false;
                }
            }
        }
    }
    File[] signedPreKeyFiles = getSignedPreKeyDirectory(context).listFiles();
    if (signedPreKeyFiles != null) {
        for (File signedPreKeyFile : signedPreKeyFiles) {
            if (!"index.dat".equals(signedPreKeyFile.getName())) {
                try {
                    SignedPreKeyRecord signedPreKey = new SignedPreKeyRecord(loadSerializedRecord(signedPreKeyFile));
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(SignedPreKeyDatabase.KEY_ID, signedPreKey.getId());
                    contentValues.put(SignedPreKeyDatabase.PUBLIC_KEY, Base64.encodeBytes(signedPreKey.getKeyPair().getPublicKey().serialize()));
                    contentValues.put(SignedPreKeyDatabase.PRIVATE_KEY, Base64.encodeBytes(signedPreKey.getKeyPair().getPrivateKey().serialize()));
                    contentValues.put(SignedPreKeyDatabase.SIGNATURE, Base64.encodeBytes(signedPreKey.getSignature()));
                    contentValues.put(SignedPreKeyDatabase.TIMESTAMP, signedPreKey.getTimestamp());
                    database.insert(SignedPreKeyDatabase.TABLE_NAME, null, contentValues);
                    Log.w(TAG, "Migrated signed prekey: " + signedPreKey.getId());
                } catch (IOException | InvalidMessageException e) {
                    Log.w(TAG, e);
                    clean = false;
                }
            }
        }
    }
    File oneTimePreKeyIndex = new File(getPreKeyDirectory(context), PreKeyIndex.FILE_NAME);
    File signedPreKeyIndex = new File(getSignedPreKeyDirectory(context), SignedPreKeyIndex.FILE_NAME);
    if (oneTimePreKeyIndex.exists()) {
        try {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(oneTimePreKeyIndex));
            PreKeyIndex index = JsonUtils.fromJson(reader, PreKeyIndex.class);
            reader.close();
            Log.w(TAG, "Setting next prekey id: " + index.nextPreKeyId);
            TextSecurePreferences.setNextPreKeyId(context, index.nextPreKeyId);
        } catch (IOException e) {
            Log.w(TAG, e);
        }
    }
    if (signedPreKeyIndex.exists()) {
        try {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(signedPreKeyIndex));
            SignedPreKeyIndex index = JsonUtils.fromJson(reader, SignedPreKeyIndex.class);
            reader.close();
            Log.w(TAG, "Setting next signed prekey id: " + index.nextSignedPreKeyId);
            Log.w(TAG, "Setting active signed prekey id: " + index.activeSignedPreKeyId);
            TextSecurePreferences.setNextSignedPreKeyId(context, index.nextSignedPreKeyId);
            TextSecurePreferences.setActiveSignedPreKeyId(context, index.activeSignedPreKeyId);
        } catch (IOException e) {
            Log.w(TAG, e);
        }
    }
    return clean;
}
Also used : ContentValues(android.content.ContentValues) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) File(java.io.File) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Aggregations

PreKeyRecord (org.whispersystems.libsignal.state.PreKeyRecord)29 SignedPreKeyRecord (org.whispersystems.libsignal.state.SignedPreKeyRecord)28 LinkedList (java.util.LinkedList)7 IdentityKeyPair (org.whispersystems.libsignal.IdentityKeyPair)7 ECKeyPair (org.whispersystems.libsignal.ecc.ECKeyPair)7 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 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 SignalPreKeyStore (com.toshi.crypto.signal.store.SignalPreKeyStore)2 Element (de.pixart.messenger.xml.Element)2 Element (eu.siacs.conversations.xml.Element)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2