use of org.whispersystems.libsignal.state.PreKeyRecord in project toshi-android-client by toshiapp.
the class PreKeyUtil method generateLastResortKey.
public static PreKeyRecord generateLastResortKey(Context context) {
PreKeyStore preKeyStore = new SignalPreKeyStore();
if (preKeyStore.containsPreKey(Medium.MAX_VALUE)) {
try {
return preKeyStore.loadPreKey(Medium.MAX_VALUE);
} catch (InvalidKeyIdException e) {
LogUtil.exception("Error while generating last resort key", 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;
}
use of org.whispersystems.libsignal.state.PreKeyRecord in project toshi-android-client by toshiapp.
the class ChatService method registerKeysWithTimestamp.
private Completable registerKeysWithTimestamp(final long timestamp, final IdentityKey identityKey, final PreKeyRecord lastResortKey, final String password, final int registrationId, final String signalingKey, final SignedPreKeyRecord signedPreKey, final List<PreKeyRecord> preKeys) {
final long startTime = System.currentTimeMillis();
final List<PreKeyEntity> entities = new LinkedList<>();
for (PreKeyRecord preKey : preKeys) {
final PreKeyEntity entity = new PreKeyEntity(preKey.getId(), preKey.getKeyPair().getPublicKey());
entities.add(entity);
}
final PreKeyEntity lastResortEntity = new PreKeyEntity(lastResortKey.getId(), lastResortKey.getKeyPair().getPublicKey());
final SignedPreKeyEntity signedPreKeyEntity = new SignedPreKeyEntity(signedPreKey.getId(), signedPreKey.getKeyPair().getPublicKey(), signedPreKey.getSignature());
final long endTime = System.currentTimeMillis();
final long elapsedSeconds = (endTime - startTime) / 1000;
final long amendedTimestamp = timestamp + elapsedSeconds;
final SignalBootstrap payload = new SignalBootstrap(entities, lastResortEntity, password, registrationId, signalingKey, signedPreKeyEntity, identityKey);
final String payloadForSigning = JsonUtil.toJson(payload);
return this.chatInterface.register(payloadForSigning, amendedTimestamp).observeOn(Schedulers.io()).subscribeOn(Schedulers.io()).toCompletable();
}
use of org.whispersystems.libsignal.state.PreKeyRecord in project Signal-Android by WhisperSystems.
the class RefreshPreKeysJob method refreshKeys.
/**
* @return True if we need to clean prekeys, otherwise false.
*/
private boolean refreshKeys(@NonNull ServiceIdType serviceIdType, @NonNull SignalProtocolStore protocolStore, @NonNull PreKeyMetadataStore metadataStore) throws IOException {
String logPrefix = "[" + serviceIdType + "] ";
SignalServiceAccountManager accountManager = ApplicationDependencies.getSignalServiceAccountManager();
int availableKeys = accountManager.getPreKeysCount(serviceIdType);
log(TAG, logPrefix + "Available keys: " + availableKeys);
if (availableKeys >= PREKEY_MINIMUM && metadataStore.isSignedPreKeyRegistered()) {
log(TAG, logPrefix + "Available keys sufficient.");
return false;
}
List<PreKeyRecord> preKeyRecords = PreKeyUtil.generateAndStoreOneTimePreKeys(protocolStore, metadataStore);
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateAndStoreSignedPreKey(protocolStore, metadataStore, false);
IdentityKeyPair identityKey = protocolStore.getIdentityKeyPair();
log(TAG, logPrefix + "Registering new prekeys...");
accountManager.setPreKeys(serviceIdType, identityKey.getPublicKey(), signedPreKeyRecord, preKeyRecords);
metadataStore.setActiveSignedPreKeyId(signedPreKeyRecord.getId());
metadataStore.setSignedPreKeyRegistered(true);
log(TAG, logPrefix + "Need to clean prekeys.");
return true;
}
use of org.whispersystems.libsignal.state.PreKeyRecord in project Signal-Android by WhisperSystems.
the class PniAccountInitializationMigrationJob method performMigration.
@Override
public void performMigration() throws IOException {
PNI pni = SignalStore.account().getPni();
if (pni == null || SignalStore.account().getAci() == null || !Recipient.self().isRegistered()) {
Log.w(TAG, "Not yet registered! No need to perform this migration.");
return;
}
if (!SignalStore.account().hasPniIdentityKey()) {
Log.i(TAG, "Generating PNI identity.");
SignalStore.account().generatePniIdentityKeyIfNecessary();
} else {
Log.w(TAG, "Already generated the PNI identity. Skipping this step.");
}
SignalServiceAccountManager accountManager = ApplicationDependencies.getSignalServiceAccountManager();
SignalProtocolStore protocolStore = ApplicationDependencies.getProtocolStore().pni();
PreKeyMetadataStore metadataStore = SignalStore.account().pniPreKeys();
if (!metadataStore.isSignedPreKeyRegistered()) {
Log.i(TAG, "Uploading signed prekey for PNI.");
SignedPreKeyRecord signedPreKey = PreKeyUtil.generateAndStoreSignedPreKey(protocolStore, metadataStore, true);
List<PreKeyRecord> oneTimePreKeys = PreKeyUtil.generateAndStoreOneTimePreKeys(protocolStore, metadataStore);
accountManager.setPreKeys(ServiceIdType.PNI, protocolStore.getIdentityKeyPair().getPublicKey(), signedPreKey, oneTimePreKeys);
metadataStore.setSignedPreKeyRegistered(true);
} else {
Log.w(TAG, "Already uploaded signed prekey for PNI. Skipping this step.");
}
}
use of org.whispersystems.libsignal.state.PreKeyRecord in project Signal-Android by WhisperSystems.
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;
}
Aggregations