X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/7d802fb8c5152c5fc089052e5915c7f588057e53..2935b96070ee687c7a17382c64e3f85730b9f706:/lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java diff --git a/lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java b/lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java index 914a5fcf..aad731a0 100644 --- a/lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java +++ b/lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java @@ -22,12 +22,8 @@ import org.asamk.signal.manager.config.ServiceEnvironmentConfig; import org.asamk.signal.manager.helper.PinHelper; import org.asamk.signal.manager.storage.SignalAccount; import org.asamk.signal.manager.util.KeyUtils; -import org.signal.zkgroup.profiles.ProfileKey; -import org.whispersystems.libsignal.IdentityKeyPair; import org.whispersystems.libsignal.util.KeyHelper; import org.whispersystems.libsignal.util.guava.Optional; -import org.whispersystems.signalservice.api.KbsPinData; -import org.whispersystems.signalservice.api.KeyBackupService; import org.whispersystems.signalservice.api.KeyBackupServicePinException; import org.whispersystems.signalservice.api.KeyBackupSystemNoDataException; import org.whispersystems.signalservice.api.SignalServiceAccountManager; @@ -77,16 +73,12 @@ public class RegistrationManager implements Closeable { this.accountManager = new SignalServiceAccountManager(serviceEnvironmentConfig.getSignalServiceConfiguration(), new DynamicCredentialsProvider( // Using empty UUID, because registering doesn't work otherwise - null, - account.getUsername(), - account.getPassword(), - account.getSignalingKey(), - SignalServiceAddress.DEFAULT_DEVICE_ID), + null, account.getUsername(), account.getPassword(), SignalServiceAddress.DEFAULT_DEVICE_ID), userAgent, groupsV2Operations, ServiceConfig.AUTOMATIC_NETWORK_RETRY, timer); - final KeyBackupService keyBackupService = accountManager.getKeyBackupService(ServiceConfig.getIasKeyStore(), + final var keyBackupService = accountManager.getKeyBackupService(ServiceConfig.getIasKeyStore(), serviceEnvironmentConfig.getKeyBackupConfig().getEnclaveName(), serviceEnvironmentConfig.getKeyBackupConfig().getServiceId(), serviceEnvironmentConfig.getKeyBackupConfig().getMrenclave(), @@ -97,17 +89,15 @@ public class RegistrationManager implements Closeable { public static RegistrationManager init( String username, File settingsPath, ServiceEnvironment serviceEnvironment, String userAgent ) throws IOException { - PathConfig pathConfig = PathConfig.createDefault(settingsPath); + var pathConfig = PathConfig.createDefault(settingsPath); - final ServiceEnvironmentConfig serviceConfiguration = ServiceConfig.getServiceEnvironmentConfig( - serviceEnvironment, - userAgent); + final var serviceConfiguration = ServiceConfig.getServiceEnvironmentConfig(serviceEnvironment, userAgent); if (!SignalAccount.userExists(pathConfig.getDataPath(), username)) { - IdentityKeyPair identityKey = KeyUtils.generateIdentityKeyPair(); - int registrationId = KeyHelper.generateRegistrationId(false); + var identityKey = KeyUtils.generateIdentityKeyPair(); + var registrationId = KeyHelper.generateRegistrationId(false); - ProfileKey profileKey = KeyUtils.createProfileKey(); - SignalAccount account = SignalAccount.create(pathConfig.getDataPath(), + var profileKey = KeyUtils.createProfileKey(); + var account = SignalAccount.create(pathConfig.getDataPath(), username, identityKey, registrationId, @@ -117,7 +107,7 @@ public class RegistrationManager implements Closeable { return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent); } - SignalAccount account = SignalAccount.load(pathConfig.getDataPath(), username); + var account = SignalAccount.load(pathConfig.getDataPath(), username); return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent); } @@ -138,13 +128,10 @@ public class RegistrationManager implements Closeable { account.save(); } - public void verifyAccount( + public Manager verifyAccount( String verificationCode, String pin ) throws IOException, KeyBackupSystemNoDataException, KeyBackupServicePinException { verificationCode = verificationCode.replace("-", ""); - if (account.getSignalingKey() == null) { - account.setSignalingKey(KeyUtils.createSignalingKey()); - } VerifyAccountResponse response; try { response = verifyAccountWithCode(verificationCode, pin, null); @@ -154,12 +141,12 @@ public class RegistrationManager implements Closeable { throw e; } - KbsPinData registrationLockData = pinHelper.getRegistrationLockData(pin, e); + var registrationLockData = pinHelper.getRegistrationLockData(pin, e); if (registrationLockData == null) { throw e; } - String registrationLock = registrationLockData.getMasterKey().deriveRegistrationLock(); + var registrationLock = registrationLockData.getMasterKey().deriveRegistrationLock(); try { response = verifyAccountWithCode(verificationCode, null, registrationLock); } catch (LockedException _e) { @@ -176,26 +163,37 @@ public class RegistrationManager implements Closeable { account.setRegistered(true); account.setUuid(UuidUtil.parseOrNull(response.getUuid())); account.setRegistrationLockPin(pin); + account.getSignalProtocolStore().archiveAllSessions(); account.getSignalProtocolStore() .saveIdentity(account.getSelfAddress(), account.getSignalProtocolStore().getIdentityKeyPair().getPublicKey(), TrustLevel.TRUSTED_VERIFIED); - try (Manager m = new Manager(account, pathConfig, serviceEnvironmentConfig, userAgent)) { + Manager m = null; + try { + m = new Manager(account, pathConfig, serviceEnvironmentConfig, userAgent); m.refreshPreKeys(); - m.close(false); - } + account.save(); - account.save(); + final var result = m; + account = null; + m = null; + + return result; + } finally { + if (m != null) { + m.close(); + } + } } private VerifyAccountResponse verifyAccountWithCode( final String verificationCode, final String legacyPin, final String registrationLock ) throws IOException { return accountManager.verifyAccountWithCode(verificationCode, - account.getSignalingKey(), + null, account.getSignalProtocolStore().getLocalRegistrationId(), true, legacyPin,