From 472c8a441f36084c617baf5cb541de171f574040 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Mon, 21 Feb 2022 09:12:03 -0500 Subject: [PATCH] Allow late initialization of some PNI keys. --- .../ApplicationDependencyProvider.java | 6 +++ .../securesms/keyvalue/AccountValues.kt | 50 +++++++++++-------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/dependencies/ApplicationDependencyProvider.java b/app/src/main/java/org/thoughtcrime/securesms/dependencies/ApplicationDependencyProvider.java index 3a3f81c26b..a534dc3ebb 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/dependencies/ApplicationDependencyProvider.java +++ b/app/src/main/java/org/thoughtcrime/securesms/dependencies/ApplicationDependencyProvider.java @@ -27,6 +27,7 @@ import org.thoughtcrime.securesms.jobmanager.JobManager; import org.thoughtcrime.securesms.jobmanager.JobMigrator; import org.thoughtcrime.securesms.jobmanager.impl.FactoryJobPredicate; import org.thoughtcrime.securesms.jobmanager.impl.JsonDataSerializer; +import org.thoughtcrime.securesms.jobs.CreateSignedPreKeyJob; import org.thoughtcrime.securesms.jobs.FastJobStorage; import org.thoughtcrime.securesms.jobs.GroupCallUpdateSendJob; import org.thoughtcrime.securesms.jobs.JobManagerFactories; @@ -289,6 +290,11 @@ public class ApplicationDependencyProvider implements ApplicationDependencies.Pr throw new IllegalStateException("No PNI set!"); } + if (!SignalStore.account().hasPniIdentityKey()) { + SignalStore.account().generatePniIdentityKey(); + CreateSignedPreKeyJob.enqueueIfNeeded(); + } + SignalBaseIdentityKeyStore baseIdentityStore = new SignalBaseIdentityKeyStore(context); SignalServiceAccountDataStoreImpl aciStore = new SignalServiceAccountDataStoreImpl(context, diff --git a/app/src/main/java/org/thoughtcrime/securesms/keyvalue/AccountValues.kt b/app/src/main/java/org/thoughtcrime/securesms/keyvalue/AccountValues.kt index 980d403a57..ac011c9851 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/keyvalue/AccountValues.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/keyvalue/AccountValues.kt @@ -151,15 +151,17 @@ internal class AccountValues internal constructor(store: KeyValueStore) : Signal /** Generates and saves an identity key pair for the ACI identity. Should only be done once. */ fun generateAciIdentityKey() { - Log.i(TAG, "Generating a new ACI identity key pair.") - require(!store.containsKey(KEY_ACI_IDENTITY_PUBLIC_KEY)) { "Already generated!" } + synchronized(this) { + Log.i(TAG, "Generating a new ACI identity key pair.") + require(!store.containsKey(KEY_ACI_IDENTITY_PUBLIC_KEY)) { "Already generated!" } - val key: IdentityKeyPair = IdentityKeyUtil.generateIdentityKeyPair() - store - .beginWrite() - .putBlob(KEY_ACI_IDENTITY_PUBLIC_KEY, key.publicKey.serialize()) - .putBlob(KEY_ACI_IDENTITY_PRIVATE_KEY, key.privateKey.serialize()) - .commit() + val key: IdentityKeyPair = IdentityKeyUtil.generateIdentityKeyPair() + store + .beginWrite() + .putBlob(KEY_ACI_IDENTITY_PUBLIC_KEY, key.publicKey.serialize()) + .putBlob(KEY_ACI_IDENTITY_PRIVATE_KEY, key.privateKey.serialize()) + .commit() + } } fun hasPniIdentityKey(): Boolean { @@ -168,25 +170,29 @@ internal class AccountValues internal constructor(store: KeyValueStore) : Signal /** Generates and saves an identity key pair for the PNI identity. Should only be done once. */ fun generatePniIdentityKey() { - Log.i(TAG, "Generating a new PNI identity key pair.") - require(!store.containsKey(KEY_PNI_IDENTITY_PUBLIC_KEY)) { "Already generated!" } + synchronized(this) { + Log.i(TAG, "Generating a new PNI identity key pair.") + require(!store.containsKey(KEY_PNI_IDENTITY_PUBLIC_KEY)) { "Already generated!" } - val key: IdentityKeyPair = IdentityKeyUtil.generateIdentityKeyPair() - store - .beginWrite() - .putBlob(KEY_PNI_IDENTITY_PUBLIC_KEY, key.publicKey.serialize()) - .putBlob(KEY_PNI_IDENTITY_PRIVATE_KEY, key.privateKey.serialize()) - .commit() + val key: IdentityKeyPair = IdentityKeyUtil.generateIdentityKeyPair() + store + .beginWrite() + .putBlob(KEY_PNI_IDENTITY_PUBLIC_KEY, key.publicKey.serialize()) + .putBlob(KEY_PNI_IDENTITY_PRIVATE_KEY, key.privateKey.serialize()) + .commit() + } } /** When acting as a linked device, this method lets you store the identity keys sent from the primary device */ fun setIdentityKeysFromPrimaryDevice(aciKeys: IdentityKeyPair) { - require(isLinkedDevice) { "Must be a linked device!" } - store - .beginWrite() - .putBlob(KEY_ACI_IDENTITY_PUBLIC_KEY, aciKeys.publicKey.serialize()) - .putBlob(KEY_ACI_IDENTITY_PRIVATE_KEY, aciKeys.privateKey.serialize()) - .commit() + synchronized(this) { + require(isLinkedDevice) { "Must be a linked device!" } + store + .beginWrite() + .putBlob(KEY_ACI_IDENTITY_PUBLIC_KEY, aciKeys.publicKey.serialize()) + .putBlob(KEY_ACI_IDENTITY_PRIVATE_KEY, aciKeys.privateKey.serialize()) + .commit() + } } /** Only to be used when restoring an identity public key from an old backup */