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
.internal
.push
.VerifyAccountResponse
;
49 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
51 import java
.io
.IOException
;
52 import java
.util
.function
.Consumer
;
54 import static org
.asamk
.signal
.manager
.util
.KeyUtils
.generatePreKeysForType
;
56 public class RegistrationManagerImpl
implements RegistrationManager
{
58 private final static Logger logger
= LoggerFactory
.getLogger(RegistrationManagerImpl
.class);
60 private SignalAccount account
;
61 private final PathConfig pathConfig
;
62 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
63 private final String userAgent
;
64 private final Consumer
<Manager
> newManagerListener
;
65 private final GroupsV2Operations groupsV2Operations
;
67 private final SignalServiceAccountManager accountManager
;
68 private final PinHelper pinHelper
;
69 private final AccountFileUpdater accountFileUpdater
;
71 public RegistrationManagerImpl(
72 SignalAccount account
,
73 PathConfig pathConfig
,
74 ServiceEnvironmentConfig serviceEnvironmentConfig
,
76 Consumer
<Manager
> newManagerListener
,
77 AccountFileUpdater accountFileUpdater
79 this.account
= account
;
80 this.pathConfig
= pathConfig
;
81 this.accountFileUpdater
= accountFileUpdater
;
82 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
83 this.userAgent
= userAgent
;
84 this.newManagerListener
= newManagerListener
;
86 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.signalServiceConfiguration()),
87 ServiceConfig
.GROUP_MAX_SIZE
);
88 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.signalServiceConfiguration(),
89 new DynamicCredentialsProvider(
90 // Using empty UUID, because registering doesn't work otherwise
91 null, null, account
.getNumber(), account
.getPassword(), SignalServiceAddress
.DEFAULT_DEVICE_ID
),
94 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
95 final var secureValueRecoveryV2
= accountManager
.getSecureValueRecoveryV2(serviceEnvironmentConfig
.svr2Mrenclave());
96 this.pinHelper
= new PinHelper(secureValueRecoveryV2
);
100 public void register(
101 boolean voiceVerification
, String captcha
102 ) throws IOException
, CaptchaRequiredException
, NonNormalizedPhoneNumberException
, RateLimitException
{
103 if (account
.isRegistered()
104 && account
.getServiceEnvironment() != null
105 && account
.getServiceEnvironment() != serviceEnvironmentConfig
.type()) {
106 throw new IOException("Account is registered in another environment: " + account
.getServiceEnvironment());
110 if (account
.getAci() != null && attemptReactivateAccount()) {
114 String sessionId
= NumberVerificationUtils
.handleVerificationSession(accountManager
,
115 account
.getSessionId(account
.getNumber()),
116 id
-> account
.setSessionId(account
.getNumber(), id
),
119 NumberVerificationUtils
.requestVerificationCode(accountManager
, sessionId
, voiceVerification
);
120 } catch (DeprecatedVersionException e
) {
121 logger
.debug("Signal-Server returned deprecated version exception", e
);
127 public void verifyAccount(
128 String verificationCode
, String pin
129 ) throws IOException
, PinLockedException
, IncorrectPinException
{
130 if (account
.isRegistered()) {
131 throw new IOException("Account is already registered");
134 if (account
.getPniIdentityKeyPair() == null) {
135 account
.setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
138 final var aciPreKeys
= generatePreKeysForType(account
.getAccountData(ServiceIdType
.ACI
));
139 final var pniPreKeys
= generatePreKeysForType(account
.getAccountData(ServiceIdType
.PNI
));
140 final var result
= NumberVerificationUtils
.verifyNumber(account
.getSessionId(account
.getNumber()),
144 (sessionId1
, verificationCode1
, registrationLock
) -> verifyAccountWithCode(sessionId1
,
149 final var response
= result
.first();
150 final var masterKey
= result
.second();
151 if (masterKey
== null) {
155 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
156 final var aci
= ACI
.parseOrThrow(response
.getUuid());
157 final var pni
= PNI
.parseOrThrow(response
.getPni());
158 account
.finishRegistration(aci
, pni
, masterKey
, pin
, aciPreKeys
, pniPreKeys
);
159 accountFileUpdater
.updateAccountIdentifiers(account
.getNumber(), aci
);
161 ManagerImpl m
= null;
163 m
= new ManagerImpl(account
, pathConfig
, accountFileUpdater
, serviceEnvironmentConfig
, userAgent
);
167 if (response
.isStorageCapable()) {
168 m
.retrieveRemoteStorage();
170 // Set an initial empty profile so user can be added to groups
172 m
.updateProfile(UpdateProfile
.newBuilder().build());
173 } catch (NoClassDefFoundError e
) {
174 logger
.warn("Failed to set default profile: {}", e
.getMessage());
178 m
.refreshCurrentUsername();
179 } catch (IOException
| BaseUsernameException e
) {
180 logger
.warn("Failed to refresh current username", e
);
183 if (newManagerListener
!= null) {
184 newManagerListener
.accept(m
);
195 public void deleteLocalAccountData() throws IOException
{
196 account
.deleteAccountData();
197 accountFileUpdater
.removeAccount();
202 public boolean isRegistered() {
203 return account
.isRegistered();
206 private boolean attemptReactivateAccount() {
208 final var accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.signalServiceConfiguration(),
209 account
.getCredentialsProvider(),
212 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
213 accountManager
.setAccountAttributes(account
.getAccountAttributes(null));
214 account
.setRegistered(true);
215 logger
.info("Reactivated existing account, verify is not necessary.");
216 if (newManagerListener
!= null) {
217 final var m
= new ManagerImpl(account
,
220 serviceEnvironmentConfig
,
223 newManagerListener
.accept(m
);
226 } catch (IOException e
) {
227 logger
.debug("Failed to reactivate account");
232 private VerifyAccountResponse
verifyAccountWithCode(
233 final String sessionId
,
234 final String verificationCode
,
235 final String registrationLock
,
236 final PreKeyCollection aciPreKeys
,
237 final PreKeyCollection pniPreKeys
238 ) throws IOException
{
240 Utils
.handleResponseException(accountManager
.verifyAccount(verificationCode
, sessionId
));
241 } catch (AlreadyVerifiedException e
) {
242 // Already verified so can continue registering
244 return Utils
.handleResponseException(accountManager
.registerAccount(sessionId
,
246 account
.getAccountAttributes(registrationLock
),
254 public void close() {
255 if (account
!= null) {