2 Copyright (C) 2015-2022 AsamK and contributors
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org
.asamk
.signal
.manager
.internal
;
19 import org
.asamk
.signal
.manager
.Manager
;
20 import org
.asamk
.signal
.manager
.RegistrationManager
;
21 import org
.asamk
.signal
.manager
.api
.CaptchaRequiredException
;
22 import org
.asamk
.signal
.manager
.api
.IncorrectPinException
;
23 import org
.asamk
.signal
.manager
.api
.NonNormalizedPhoneNumberException
;
24 import org
.asamk
.signal
.manager
.api
.PinLockedException
;
25 import org
.asamk
.signal
.manager
.api
.RateLimitException
;
26 import org
.asamk
.signal
.manager
.api
.UpdateProfile
;
27 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
28 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
29 import org
.asamk
.signal
.manager
.helper
.AccountFileUpdater
;
30 import org
.asamk
.signal
.manager
.helper
.PinHelper
;
31 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
32 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
33 import org
.asamk
.signal
.manager
.util
.NumberVerificationUtils
;
34 import org
.asamk
.signal
.manager
.util
.Utils
;
35 import org
.signal
.libsignal
.usernames
.BaseUsernameException
;
36 import org
.slf4j
.Logger
;
37 import org
.slf4j
.LoggerFactory
;
38 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
39 import org
.whispersystems
.signalservice
.api
.account
.PreKeyCollection
;
40 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
41 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
42 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
.ACI
;
43 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
.PNI
;
44 import org
.whispersystems
.signalservice
.api
.push
.ServiceIdType
;
45 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
46 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.AlreadyVerifiedException
;
47 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.DeprecatedVersionException
;
48 import org
.whispersystems
.signalservice
.api
.svr
.SecureValueRecovery
;
49 import org
.whispersystems
.signalservice
.internal
.push
.VerifyAccountResponse
;
50 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
52 import java
.io
.IOException
;
53 import java
.util
.function
.Consumer
;
55 import static org
.asamk
.signal
.manager
.util
.KeyUtils
.generatePreKeysForType
;
57 public class RegistrationManagerImpl
implements RegistrationManager
{
59 private static final Logger logger
= LoggerFactory
.getLogger(RegistrationManagerImpl
.class);
61 private SignalAccount account
;
62 private final PathConfig pathConfig
;
63 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
64 private final String userAgent
;
65 private final Consumer
<Manager
> newManagerListener
;
66 private final GroupsV2Operations groupsV2Operations
;
68 private final SignalServiceAccountManager accountManager
;
69 private final PinHelper pinHelper
;
70 private final AccountFileUpdater accountFileUpdater
;
72 public RegistrationManagerImpl(
73 SignalAccount account
,
74 PathConfig pathConfig
,
75 ServiceEnvironmentConfig serviceEnvironmentConfig
,
77 Consumer
<Manager
> newManagerListener
,
78 AccountFileUpdater accountFileUpdater
80 this.account
= account
;
81 this.pathConfig
= pathConfig
;
82 this.accountFileUpdater
= accountFileUpdater
;
83 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
84 this.userAgent
= userAgent
;
85 this.newManagerListener
= newManagerListener
;
87 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.signalServiceConfiguration()),
88 ServiceConfig
.GROUP_MAX_SIZE
);
89 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.signalServiceConfiguration(),
90 new DynamicCredentialsProvider(
91 // Using empty UUID, because registering doesn't work otherwise
92 null, null, account
.getNumber(), account
.getPassword(), SignalServiceAddress
.DEFAULT_DEVICE_ID
),
95 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
96 final var secureValueRecoveryV2
= serviceEnvironmentConfig
.svr2Mrenclaves()
98 .map(mr
-> (SecureValueRecovery
) accountManager
.getSecureValueRecoveryV2(mr
))
100 this.pinHelper
= new PinHelper(secureValueRecoveryV2
);
104 public void register(
105 boolean voiceVerification
, String captcha
106 ) throws IOException
, CaptchaRequiredException
, NonNormalizedPhoneNumberException
, RateLimitException
{
107 if (account
.isRegistered()
108 && account
.getServiceEnvironment() != null
109 && account
.getServiceEnvironment() != serviceEnvironmentConfig
.type()) {
110 throw new IOException("Account is registered in another environment: " + account
.getServiceEnvironment());
114 if (account
.getAci() != null && attemptReactivateAccount()) {
118 String sessionId
= NumberVerificationUtils
.handleVerificationSession(accountManager
,
119 account
.getSessionId(account
.getNumber()),
120 id
-> account
.setSessionId(account
.getNumber(), id
),
123 NumberVerificationUtils
.requestVerificationCode(accountManager
, sessionId
, voiceVerification
);
124 } catch (DeprecatedVersionException e
) {
125 logger
.debug("Signal-Server returned deprecated version exception", e
);
131 public void verifyAccount(
132 String verificationCode
, String pin
133 ) throws IOException
, PinLockedException
, IncorrectPinException
{
134 if (account
.isRegistered()) {
135 throw new IOException("Account is already registered");
138 if (account
.getPniIdentityKeyPair() == null) {
139 account
.setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
142 final var aciPreKeys
= generatePreKeysForType(account
.getAccountData(ServiceIdType
.ACI
));
143 final var pniPreKeys
= generatePreKeysForType(account
.getAccountData(ServiceIdType
.PNI
));
144 final var result
= NumberVerificationUtils
.verifyNumber(account
.getSessionId(account
.getNumber()),
148 (sessionId1
, verificationCode1
, registrationLock
) -> verifyAccountWithCode(sessionId1
,
153 final var response
= result
.first();
154 final var masterKey
= result
.second();
155 if (masterKey
== null) {
159 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
160 final var aci
= ACI
.parseOrThrow(response
.getUuid());
161 final var pni
= PNI
.parseOrThrow(response
.getPni());
162 account
.finishRegistration(aci
, pni
, masterKey
, pin
, aciPreKeys
, pniPreKeys
);
163 accountFileUpdater
.updateAccountIdentifiers(account
.getNumber(), aci
);
165 ManagerImpl m
= null;
167 m
= new ManagerImpl(account
, pathConfig
, accountFileUpdater
, serviceEnvironmentConfig
, userAgent
);
171 if (response
.isStorageCapable()) {
172 m
.retrieveRemoteStorage();
174 // Set an initial empty profile so user can be added to groups
176 m
.updateProfile(UpdateProfile
.newBuilder().build());
177 } catch (NoClassDefFoundError e
) {
178 logger
.warn("Failed to set default profile: {}", e
.getMessage());
182 m
.refreshCurrentUsername();
183 } catch (IOException
| BaseUsernameException e
) {
184 logger
.warn("Failed to refresh current username", e
);
187 if (newManagerListener
!= null) {
188 newManagerListener
.accept(m
);
199 public void deleteLocalAccountData() throws IOException
{
200 account
.deleteAccountData();
201 accountFileUpdater
.removeAccount();
206 public boolean isRegistered() {
207 return account
.isRegistered();
210 private boolean attemptReactivateAccount() {
212 final var accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.signalServiceConfiguration(),
213 account
.getCredentialsProvider(),
216 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
217 accountManager
.setAccountAttributes(account
.getAccountAttributes(null));
218 account
.setRegistered(true);
219 logger
.info("Reactivated existing account, verify is not necessary.");
220 if (newManagerListener
!= null) {
221 final var m
= new ManagerImpl(account
,
224 serviceEnvironmentConfig
,
227 newManagerListener
.accept(m
);
230 } catch (IOException e
) {
231 logger
.debug("Failed to reactivate account");
236 private VerifyAccountResponse
verifyAccountWithCode(
237 final String sessionId
,
238 final String verificationCode
,
239 final String registrationLock
,
240 final PreKeyCollection aciPreKeys
,
241 final PreKeyCollection pniPreKeys
242 ) throws IOException
{
244 Utils
.handleResponseException(accountManager
.verifyAccount(verificationCode
, sessionId
));
245 } catch (AlreadyVerifiedException e
) {
246 // Already verified so can continue registering
248 return Utils
.handleResponseException(accountManager
.registerAccount(sessionId
,
250 account
.getAccountAttributes(registrationLock
),
258 public void close() {
259 if (account
!= null) {