X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/69fe3986cf0810bf712d0e7cee970a6a549514d3..007acf8c6d0f18d1e9b2ebb7a6c21bbc5bca3d55:/lib/src/main/java/org/asamk/signal/manager/ProvisioningManagerImpl.java diff --git a/lib/src/main/java/org/asamk/signal/manager/ProvisioningManagerImpl.java b/lib/src/main/java/org/asamk/signal/manager/ProvisioningManagerImpl.java index 9142d2e9..17569401 100644 --- a/lib/src/main/java/org/asamk/signal/manager/ProvisioningManagerImpl.java +++ b/lib/src/main/java/org/asamk/signal/manager/ProvisioningManagerImpl.java @@ -23,16 +23,17 @@ import org.asamk.signal.manager.storage.SignalAccount; import org.asamk.signal.manager.storage.accounts.AccountsStore; import org.asamk.signal.manager.storage.identities.TrustNewIdentity; import org.asamk.signal.manager.util.KeyUtils; +import org.signal.libsignal.protocol.IdentityKeyPair; +import org.signal.libsignal.protocol.util.KeyHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.whispersystems.libsignal.IdentityKeyPair; -import org.whispersystems.libsignal.util.KeyHelper; import org.whispersystems.signalservice.api.SignalServiceAccountManager; import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations; import org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations; import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException; import org.whispersystems.signalservice.api.util.DeviceNameUtil; +import org.whispersystems.signalservice.internal.push.ConfirmCodeMessage; import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider; import java.io.IOException; @@ -54,6 +55,7 @@ class ProvisioningManagerImpl implements ProvisioningManager { private final SignalServiceAccountManager accountManager; private final IdentityKeyPair tempIdentityKey; private final int registrationId; + private final int pniRegistrationId; private final String password; ProvisioningManagerImpl( @@ -71,15 +73,17 @@ class ProvisioningManagerImpl implements ProvisioningManager { tempIdentityKey = KeyUtils.generateIdentityKeyPair(); registrationId = KeyHelper.generateRegistrationId(false); + pniRegistrationId = KeyHelper.generateRegistrationId(false); password = KeyUtils.createPassword(); GroupsV2Operations groupsV2Operations; try { - groupsV2Operations = new GroupsV2Operations(ClientZkOperations.create(serviceEnvironmentConfig.getSignalServiceConfiguration())); + groupsV2Operations = new GroupsV2Operations(ClientZkOperations.create(serviceEnvironmentConfig.getSignalServiceConfiguration()), + ServiceConfig.GROUP_MAX_SIZE); } catch (Throwable ignored) { groupsV2Operations = null; } accountManager = new SignalServiceAccountManager(serviceEnvironmentConfig.getSignalServiceConfiguration(), - new DynamicCredentialsProvider(null, null, password, SignalServiceAddress.DEFAULT_DEVICE_ID), + new DynamicCredentialsProvider(null, null, null, password, SignalServiceAddress.DEFAULT_DEVICE_ID), userAgent, groupsV2Operations, ServiceConfig.AUTOMATIC_NETWORK_RETRY); @@ -97,6 +101,7 @@ class ProvisioningManagerImpl implements ProvisioningManager { var ret = accountManager.getNewDeviceRegistration(tempIdentityKey); var number = ret.getNumber(); var aci = ret.getAci(); + var pni = ret.getPni(); logger.info("Received link information from {}, linking in progress ...", number); @@ -117,14 +122,11 @@ class ProvisioningManagerImpl implements ProvisioningManager { var encryptedDeviceName = deviceName == null ? null - : DeviceNameUtil.encryptDeviceName(deviceName, ret.getIdentity().getPrivateKey()); + : DeviceNameUtil.encryptDeviceName(deviceName, ret.getAciIdentity().getPrivateKey()); logger.debug("Finishing new device registration"); var deviceId = accountManager.finishNewDeviceRegistration(ret.getProvisioningCode(), - false, - true, - registrationId, - encryptedDeviceName); + new ConfirmCodeMessage(false, true, registrationId, pniRegistrationId, encryptedDeviceName, null)); // Create new account with the synced identity var profileKey = ret.getProfileKey() == null ? KeyUtils.createProfileKey() : ret.getProfileKey(); @@ -134,21 +136,24 @@ class ProvisioningManagerImpl implements ProvisioningManager { account = SignalAccount.createOrUpdateLinkedAccount(pathConfig.dataPath(), accountPath, number, + serviceEnvironmentConfig.getType(), aci, + pni, password, encryptedDeviceName, deviceId, - ret.getIdentity(), + ret.getAciIdentity(), + ret.getPniIdentity(), registrationId, + pniRegistrationId, profileKey, TrustNewIdentity.ON_FIRST_USE); ManagerImpl m = null; try { - final var accountPathFinal = accountPath; m = new ManagerImpl(account, pathConfig, - (newNumber, newAci) -> accountsStore.updateAccount(accountPathFinal, newNumber, newAci), + new AccountFileUpdaterImpl(accountsStore, accountPath), serviceEnvironmentConfig, userAgent); account = null; @@ -157,7 +162,7 @@ class ProvisioningManagerImpl implements ProvisioningManager { try { m.refreshPreKeys(); } catch (Exception e) { - logger.error("Failed to refresh pre keys."); + logger.error("Failed to refresh pre keys.", e); } logger.debug("Requesting sync data"); @@ -165,7 +170,8 @@ class ProvisioningManagerImpl implements ProvisioningManager { m.requestAllSyncData(); } catch (Exception e) { logger.error( - "Failed to request sync messages from linked device, data can be requested again with `sendSyncRequest`."); + "Failed to request sync messages from linked device, data can be requested again with `sendSyncRequest`.", + e); } if (newManagerListener != null) { @@ -201,14 +207,21 @@ class ProvisioningManagerImpl implements ProvisioningManager { } try (signalAccount) { - if (signalAccount.isMasterDevice()) { - logger.debug("Account is a master device."); + if (signalAccount.isPrimaryDevice()) { + logger.debug("Account is a primary device."); + return false; + } + if (signalAccount.isRegistered() + && signalAccount.getServiceEnvironment() != null + && signalAccount.getServiceEnvironment() != serviceEnvironmentConfig.getType()) { + logger.debug("Account is registered in another environment: {}.", + signalAccount.getServiceEnvironment()); return false; } final var m = new ManagerImpl(signalAccount, pathConfig, - (newNumber, newAci) -> accountsStore.updateAccount(accountPath, newNumber, newAci), + new AccountFileUpdaterImpl(accountsStore, accountPath), serviceEnvironmentConfig, userAgent); try (m) {