Search in sources :

Example 11 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 12 with PreKeyRecord

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

the class PreKeyUtil method generateAndStoreOneTimePreKeys.

@NonNull
public static synchronized List<PreKeyRecord> generateAndStoreOneTimePreKeys(@NonNull SignalProtocolStore protocolStore, @NonNull PreKeyMetadataStore metadataStore) {
    Log.i(TAG, "Generating one-time prekeys...");
    List<PreKeyRecord> records = new LinkedList<>();
    int preKeyIdOffset = metadataStore.getNextOneTimePreKeyId();
    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);
        protocolStore.storePreKey(preKeyId, record);
        records.add(record);
    }
    metadataStore.setNextOneTimePreKeyId((preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE);
    return records;
}
Also used : ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) LinkedList(java.util.LinkedList) NonNull(androidx.annotation.NonNull)

Example 13 with PreKeyRecord

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

the class PreKeyMigrationHelper method migratePreKeys.

public 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.i(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.i(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.i(TAG, "Setting next prekey id: " + index.nextPreKeyId);
            SignalStore.account().aciPreKeys().setNextOneTimePreKeyId(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.i(TAG, "Setting next signed prekey id: " + index.nextSignedPreKeyId);
            Log.i(TAG, "Setting active signed prekey id: " + index.activeSignedPreKeyId);
            SignalStore.account().aciPreKeys().setNextSignedPreKeyId(index.nextSignedPreKeyId);
            SignalStore.account().aciPreKeys().setActiveSignedPreKeyId(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)

Example 14 with PreKeyRecord

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

the class PreKeyUtil method generateLastResortKey.

public static PreKeyRecord generateLastResortKey(Context context) {
    PreKeyStore preKeyStore = new TextSecurePreKeyStore(context);
    if (preKeyStore.containsPreKey(Medium.MAX_VALUE)) {
        try {
            return preKeyStore.loadPreKey(Medium.MAX_VALUE);
        } catch (InvalidKeyIdException e) {
            Log.w("PreKeyUtil", e);
            preKeyStore.removePreKey(Medium.MAX_VALUE);
        }
    }
    ECKeyPair keyPair = Curve.generateKeyPair();
    PreKeyRecord record = new PreKeyRecord(Medium.MAX_VALUE, keyPair);
    preKeyStore.storePreKey(Medium.MAX_VALUE, record);
    return record;
}
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) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) PreKeyStore(org.whispersystems.libsignal.state.PreKeyStore) TextSecurePreKeyStore(org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore) SignedPreKeyStore(org.whispersystems.libsignal.state.SignedPreKeyStore)

Example 15 with PreKeyRecord

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

the class TextSecurePreKeyStore method migrateRecords.

public void migrateRecords() {
    synchronized (FILE_LOCK) {
        File preKeyRecords = getPreKeyDirectory();
        for (File preKeyRecord : preKeyRecords.listFiles()) {
            try {
                int preKeyId = Integer.parseInt(preKeyRecord.getName());
                PreKeyRecord record = loadPreKey(preKeyId);
                storePreKey(preKeyId, record);
            } catch (InvalidKeyIdException | NumberFormatException e) {
                Log.w(TAG, e);
            }
        }
        File signedPreKeyRecords = getSignedPreKeyDirectory();
        for (File signedPreKeyRecord : signedPreKeyRecords.listFiles()) {
            try {
                int signedPreKeyId = Integer.parseInt(signedPreKeyRecord.getName());
                SignedPreKeyRecord record = loadSignedPreKey(signedPreKeyId);
                storeSignedPreKey(signedPreKeyId, record);
            } catch (InvalidKeyIdException | NumberFormatException e) {
                Log.w(TAG, e);
            }
        }
    }
}
Also used : PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

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