Search in sources :

Example 1 with SignedPreKeyRecord

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

the class PreKeyUtil method generateSignedPreKey.

public static SignedPreKeyRecord generateSignedPreKey(Context context, IdentityKeyPair identityKeyPair, boolean active) {
    try {
        SignedPreKeyStore signedPreKeyStore = new TextSecurePreKeyStore(context);
        int signedPreKeyId = getNextSignedPreKeyId(context);
        ECKeyPair keyPair = Curve.generateKeyPair();
        byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
        SignedPreKeyRecord record = new SignedPreKeyRecord(signedPreKeyId, System.currentTimeMillis(), keyPair, signature);
        signedPreKeyStore.storeSignedPreKey(signedPreKeyId, record);
        setNextSignedPreKeyId(context, (signedPreKeyId + 1) % Medium.MAX_VALUE);
        if (active) {
            setActiveSignedPreKeyId(context, signedPreKeyId);
        }
        return record;
    } catch (InvalidKeyException e) {
        throw new AssertionError(e);
    }
}
Also used : SignedPreKeyStore(org.whispersystems.libsignal.state.SignedPreKeyStore) TextSecurePreKeyStore(org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Example 2 with SignedPreKeyRecord

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

the class TextSecurePreKeyStore method loadSignedPreKeys.

@Override
public List<SignedPreKeyRecord> loadSignedPreKeys() {
    synchronized (FILE_LOCK) {
        File directory = getSignedPreKeyDirectory();
        List<SignedPreKeyRecord> results = new LinkedList<>();
        for (File signedPreKeyFile : directory.listFiles()) {
            try {
                if (!"index.dat".equals(signedPreKeyFile.getName())) {
                    results.add(new SignedPreKeyRecord(loadSerializedRecord(signedPreKeyFile)));
                }
            } catch (IOException | InvalidMessageException e) {
                Log.w(TAG, e);
            }
        }
        return results;
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) LinkedList(java.util.LinkedList)

Example 3 with SignedPreKeyRecord

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

the class CleanPreKeysJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws IOException {
    try {
        Log.w(TAG, "Cleaning prekeys...");
        int activeSignedPreKeyId = PreKeyUtil.getActiveSignedPreKeyId(context);
        SignedPreKeyStore signedPreKeyStore = signedPreKeyStoreFactory.create();
        if (activeSignedPreKeyId < 0)
            return;
        SignedPreKeyRecord currentRecord = signedPreKeyStore.loadSignedPreKey(activeSignedPreKeyId);
        List<SignedPreKeyRecord> allRecords = signedPreKeyStore.loadSignedPreKeys();
        LinkedList<SignedPreKeyRecord> oldRecords = removeRecordFrom(currentRecord, allRecords);
        Collections.sort(oldRecords, new SignedPreKeySorter());
        Log.w(TAG, "Active signed prekey: " + activeSignedPreKeyId);
        Log.w(TAG, "Old signed prekey record count: " + oldRecords.size());
        boolean foundAgedRecord = false;
        for (SignedPreKeyRecord oldRecord : oldRecords) {
            long archiveDuration = System.currentTimeMillis() - oldRecord.getTimestamp();
            if (archiveDuration >= ARCHIVE_AGE) {
                if (!foundAgedRecord) {
                    foundAgedRecord = true;
                } else {
                    Log.w(TAG, "Removing signed prekey record: " + oldRecord.getId() + " with timestamp: " + oldRecord.getTimestamp());
                    signedPreKeyStore.removeSignedPreKey(oldRecord.getId());
                }
            }
        }
    } catch (InvalidKeyIdException e) {
        Log.w(TAG, e);
    }
}
Also used : SignedPreKeyStore(org.whispersystems.libsignal.state.SignedPreKeyStore) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Example 4 with SignedPreKeyRecord

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

the class CreateSignedPreKeyJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws IOException {
    if (TextSecurePreferences.isSignedPreKeyRegistered(context)) {
        Log.w(TAG, "Signed prekey already registered...");
        return;
    }
    if (!TextSecurePreferences.isPushRegistered(context)) {
        Log.w(TAG, "Not yet registered...");
        return;
    }
    IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context);
    SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKeyPair, true);
    accountManager.setSignedPreKey(signedPreKeyRecord);
    TextSecurePreferences.setSignedPreKeyRegistered(context, true);
}
Also used : IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Example 5 with SignedPreKeyRecord

use of org.whispersystems.libsignal.state.SignedPreKeyRecord 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)

Aggregations

SignedPreKeyRecord (org.whispersystems.libsignal.state.SignedPreKeyRecord)8 IdentityKeyPair (org.whispersystems.libsignal.IdentityKeyPair)4 PreKeyRecord (org.whispersystems.libsignal.state.PreKeyRecord)3 File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 InvalidKeyIdException (org.whispersystems.libsignal.InvalidKeyIdException)2 SignedPreKeyStore (org.whispersystems.libsignal.state.SignedPreKeyStore)2 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 TextSecurePreKeyStore (org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore)1 Recipient (org.thoughtcrime.securesms.recipients.Recipient)1 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)1 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)1 ECKeyPair (org.whispersystems.libsignal.ecc.ECKeyPair)1