Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
[UP] Use new device link endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Jan 22, 2024
1 parent af174a7 commit da8e974
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ package im.molly.unifiedpush.device

import android.content.Context
import im.molly.unifiedpush.model.MollyDevice
import org.signal.core.util.Base64
import org.signal.core.util.logging.Log
import org.signal.libsignal.protocol.util.KeyHelper
import org.thoughtcrime.securesms.AppCapabilities
import org.thoughtcrime.securesms.crypto.PreKeyUtil
import org.thoughtcrime.securesms.database.loaders.DeviceListLoader
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
import org.thoughtcrime.securesms.devicelist.Device
import org.thoughtcrime.securesms.keyvalue.SignalStore
import org.thoughtcrime.securesms.push.AccountManagerFactory
import org.thoughtcrime.securesms.registration.RegistrationRepository
import org.thoughtcrime.securesms.registration.secondary.DeviceNameCipher
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.thoughtcrime.securesms.util.Util
import org.whispersystems.signalservice.api.account.PreKeyUpload
import org.whispersystems.signalservice.api.messages.multidevice.VerifyDeviceResponse
import org.whispersystems.signalservice.api.push.ServiceIdType
import org.whispersystems.signalservice.api.account.AccountAttributes
import org.whispersystems.signalservice.api.push.SignalServiceAddress
import java.io.IOException
import java.nio.charset.Charset
Expand Down Expand Up @@ -64,13 +63,12 @@ class MollySocketLinkedDevice(val context: Context) {
val number = SignalStore.account().e164 ?: return
val password = Util.getSecret(18)

val verifyDeviceResponse = verifyNewDevice(number, password)
val deviceId = verifyNewDevice(number, password)
TextSecurePreferences.setMultiDevice(context, true)

generateAndRegisterPreKeys(number, verifyDeviceResponse.deviceId, password)
SignalStore.unifiedpush().device = MollyDevice(
uuid = verifyDeviceResponse.uuid.toString(),
deviceId = verifyDeviceResponse.deviceId,
uuid = SignalStore.account().aci.toString(),
deviceId = deviceId,
password = password
)
} catch (e: IOException) {
Expand All @@ -79,55 +77,38 @@ class MollySocketLinkedDevice(val context: Context) {
}

@Throws(IOException::class)
private fun verifyNewDevice(number: String, password: String): VerifyDeviceResponse {
private fun verifyNewDevice(number: String, password: String): Int {
val verificationCode = ApplicationDependencies.getSignalServiceAccountManager()
.newDeviceVerificationCode
val registrationId = KeyHelper.generateRegistrationId(false)
val encryptedDeviceName = DeviceNameCipher.encryptDeviceName(
DEVICE_NAME.toByteArray(Charset.forName("UTF-8")),
SignalStore.account().aciIdentityKey
)
val accountManager = AccountManagerFactory.getInstance().createUnauthenticated(context, number, SignalServiceAddress.DEFAULT_DEVICE_ID, password)

return AccountManagerFactory.getInstance().createUnauthenticated(context, number, SignalServiceAddress.DEFAULT_DEVICE_ID, password)
.verifySecondaryDevice(
verificationCode,
registrationId,
true,
"".toByteArray(),
true,
AppCapabilities.getCapabilities(true),
false,
encryptedDeviceName,
SignalStore.account().pniRegistrationId
)
}

@Throws(IOException::class)
private fun generateAndRegisterPreKeys(number: String, deviceId: Int, password: String): Boolean? {
val protocolStore = ApplicationDependencies.getProtocolStore().aci()
val accountManager = AccountManagerFactory.getInstance().createAuthenticated(
context,
SignalStore.account().aci ?: return null,
SignalStore.account().pni ?: return null,
number,
deviceId,
password
val accountAttributes = AccountAttributes(
signalingKey = null,
registrationId = registrationId,
fetchesMessages = true,
registrationLock = null,
unidentifiedAccessKey = null,
unrestrictedUnidentifiedAccess = true,
capabilities = AppCapabilities.getCapabilities(true),
discoverableByPhoneNumber = SignalStore.phoneNumberPrivacy().phoneNumberListingMode.isDiscoverable,
name = Base64.encodeWithPadding(encryptedDeviceName),
pniRegistrationId = SignalStore.account().pniRegistrationId,
recoveryPassword = null
)
val metadataStore = SignalStore.account().aciPreKeys
val signedPreKey = PreKeyUtil.generateAndStoreSignedPreKey(protocolStore, metadataStore)
val oneTimePreKeys = PreKeyUtil.generateAndStoreOneTimeEcPreKeys(protocolStore, metadataStore)
accountManager.setPreKeys(
PreKeyUpload(
serviceIdType = ServiceIdType.ACI,
identityKey = protocolStore.identityKeyPair.publicKey,
signedPreKey = signedPreKey,
oneTimeEcPreKeys = oneTimePreKeys,
lastResortKyberPreKey = null,
oneTimeKyberPreKeys = null

val aciPreKeyCollection = RegistrationRepository.generateSignedAndLastResortPreKeys(SignalStore.account().aciIdentityKey, SignalStore.account().aciPreKeys)
val pniPreKeyCollection = RegistrationRepository.generateSignedAndLastResortPreKeys(SignalStore.account().pniIdentityKey, SignalStore.account().pniPreKeys)

return accountManager.finishNewDeviceRegistration(
verificationCode,
accountAttributes,
aciPreKeyCollection, pniPreKeyCollection,
null
)
)
metadataStore.activeSignedPreKeyId = signedPreKey.id
metadataStore.isSignedPreKeyRegistered = true
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.whispersystems.signalservice.api.kbs.MasterKey;
import org.whispersystems.signalservice.api.messages.calls.TurnServerInfo;
import org.whispersystems.signalservice.api.messages.multidevice.DeviceInfo;
import org.whispersystems.signalservice.api.messages.multidevice.VerifyDeviceResponse;
import org.whispersystems.signalservice.api.payments.CurrencyConversions;
import org.whispersystems.signalservice.api.profiles.AvatarUploadParams;
import org.whispersystems.signalservice.api.profiles.ProfileAndCredential;
Expand Down Expand Up @@ -1060,36 +1059,4 @@ public ArchiveApi getArchiveApi() {
public AuthCredentials getPaymentsAuthorization() throws IOException {
return pushServiceSocket.getPaymentsAuthorization();
}

public VerifyDeviceResponse verifySecondaryDevice(String verificationCode,
int signalProtocolRegistrationId,
boolean fetchesMessages,
byte[] unidentifiedAccessKey,
boolean unrestrictedUnidentifiedAccess,
AccountAttributes.Capabilities capabilities,
boolean discoverableByPhoneNumber,
byte[] encryptedDeviceName,
int pniRegistrationId)
throws IOException
{
AccountAttributes accountAttributes = new AccountAttributes(
null,
signalProtocolRegistrationId,
false,
false,
fetchesMessages,
null,
unidentifiedAccessKey,
unrestrictedUnidentifiedAccess,
discoverableByPhoneNumber,
capabilities,
Base64.encodeWithPadding(encryptedDeviceName),
pniRegistrationId,
null
);

return this.pushServiceSocket.verifySecondaryDevice(verificationCode, accountAttributes);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3008,12 +3008,6 @@ public int getNextPageStartGroupRevision() {
}
}

public VerifyDeviceResponse verifySecondaryDevice(String verificationCode, AccountAttributes accountAttributes) throws IOException {
String responseText = makeServiceRequest(String.format(DEVICE_PATH, verificationCode), "PUT", JsonUtil.toJson(accountAttributes));
return JsonUtil.fromJson(responseText, VerifyDeviceResponse.class);
}


private final class ResumeInfo {
private final String contentRange;
private final long contentStart;
Expand Down

0 comments on commit da8e974

Please sign in to comment.