X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/7d802fb8c5152c5fc089052e5915c7f588057e53..30690785fd51f9ba841f6f57d5f403bd970f26e3:/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..88cba97b 100644 --- a/lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java +++ b/lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java @@ -22,17 +22,14 @@ 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; import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations; import org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations; +import org.whispersystems.signalservice.api.kbs.MasterKey; import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.util.SleepTimer; import org.whispersystems.signalservice.api.util.UptimeSleepTimer; @@ -77,16 +74,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,36 +90,29 @@ 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, profileKey); - account.save(); return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent); } - SignalAccount account = SignalAccount.load(pathConfig.getDataPath(), username); + var account = SignalAccount.load(pathConfig.getDataPath(), username, true); return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent); } public void register(boolean voiceVerification, String captcha) throws IOException { - if (account.getPassword() == null) { - account.setPassword(KeyUtils.createPassword()); - } - if (voiceVerification) { accountManager.requestVoiceVerificationCode(Locale.getDefault(), Optional.fromNullable(captcha), @@ -134,69 +120,69 @@ public class RegistrationManager implements Closeable { } else { accountManager.requestSmsVerificationCode(false, Optional.fromNullable(captcha), Optional.absent()); } - - account.save(); } - public void verifyAccount( + public Manager verifyAccount( String verificationCode, String pin - ) throws IOException, KeyBackupSystemNoDataException, KeyBackupServicePinException { + ) throws IOException, LockedException, KeyBackupSystemNoDataException, KeyBackupServicePinException { verificationCode = verificationCode.replace("-", ""); - if (account.getSignalingKey() == null) { - account.setSignalingKey(KeyUtils.createSignalingKey()); - } VerifyAccountResponse response; + MasterKey masterKey; try { - response = verifyAccountWithCode(verificationCode, pin, null); - account.setPinMasterKey(null); + response = verifyAccountWithCode(verificationCode, null, null); + + masterKey = null; + pin = null; } catch (LockedException e) { if (pin == null) { throw e; } - KbsPinData registrationLockData = pinHelper.getRegistrationLockData(pin, e); + var registrationLockData = pinHelper.getRegistrationLockData(pin, e); if (registrationLockData == null) { - throw e; - } - - String registrationLock = registrationLockData.getMasterKey().deriveRegistrationLock(); - try { - response = verifyAccountWithCode(verificationCode, null, registrationLock); - } catch (LockedException _e) { - throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!"); + response = verifyAccountWithCode(verificationCode, pin, null); + masterKey = null; + } else { + var registrationLock = registrationLockData.getMasterKey().deriveRegistrationLock(); + try { + response = verifyAccountWithCode(verificationCode, null, registrationLock); + } catch (LockedException _e) { + throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!"); + } + masterKey = registrationLockData.getMasterKey(); } - account.setPinMasterKey(registrationLockData.getMasterKey()); } // TODO response.isStorageCapable() //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID))); + account.finishRegistration(UuidUtil.parseOrNull(response.getUuid()), masterKey, pin); - account.setDeviceId(SignalServiceAddress.DEFAULT_DEVICE_ID); - account.setMultiDevice(false); - account.setRegistered(true); - account.setUuid(UuidUtil.parseOrNull(response.getUuid())); - account.setRegistrationLockPin(pin); - 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); + account = null; m.refreshPreKeys(); + // Set an initial empty profile so user can be added to groups + m.setProfile(null, null, null, null, null); - m.close(false); - } + final var result = m; + m = null; - account.save(); + 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(), - account.getSignalProtocolStore().getLocalRegistrationId(), + null, + account.getLocalRegistrationId(), true, legacyPin, registrationLock,