2 Copyright (C) 2015-2021 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
;
19 import org
.asamk
.signal
.manager
.api
.CaptchaRequiredException
;
20 import org
.asamk
.signal
.manager
.api
.IncorrectPinException
;
21 import org
.asamk
.signal
.manager
.api
.PinLockedException
;
22 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
23 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
24 import org
.asamk
.signal
.manager
.helper
.PinHelper
;
25 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
26 import org
.asamk
.signal
.manager
.util
.Utils
;
27 import org
.slf4j
.Logger
;
28 import org
.slf4j
.LoggerFactory
;
29 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
30 import org
.whispersystems
.signalservice
.api
.KbsPinData
;
31 import org
.whispersystems
.signalservice
.api
.KeyBackupServicePinException
;
32 import org
.whispersystems
.signalservice
.api
.KeyBackupSystemNoDataException
;
33 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
34 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
35 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
36 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
37 import org
.whispersystems
.signalservice
.api
.push
.ACI
;
38 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
39 import org
.whispersystems
.signalservice
.internal
.ServiceResponse
;
40 import org
.whispersystems
.signalservice
.internal
.push
.LockedException
;
41 import org
.whispersystems
.signalservice
.internal
.push
.RequestVerificationCodeResponse
;
42 import org
.whispersystems
.signalservice
.internal
.push
.VerifyAccountResponse
;
43 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
45 import java
.io
.IOException
;
46 import java
.util
.function
.Consumer
;
48 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.capabilities
;
50 public class RegistrationManagerImpl
implements RegistrationManager
{
52 private final static Logger logger
= LoggerFactory
.getLogger(RegistrationManagerImpl
.class);
54 private SignalAccount account
;
55 private final PathConfig pathConfig
;
56 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
57 private final String userAgent
;
58 private final Consumer
<Manager
> newManagerListener
;
60 private final SignalServiceAccountManager accountManager
;
61 private final PinHelper pinHelper
;
63 RegistrationManagerImpl(
64 SignalAccount account
,
65 PathConfig pathConfig
,
66 ServiceEnvironmentConfig serviceEnvironmentConfig
,
68 Consumer
<Manager
> newManagerListener
70 this.account
= account
;
71 this.pathConfig
= pathConfig
;
72 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
73 this.userAgent
= userAgent
;
74 this.newManagerListener
= newManagerListener
;
76 GroupsV2Operations groupsV2Operations
;
78 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration()));
79 } catch (Throwable ignored
) {
80 groupsV2Operations
= null;
82 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
83 new DynamicCredentialsProvider(
84 // Using empty UUID, because registering doesn't work otherwise
85 null, account
.getAccount(), account
.getPassword(), SignalServiceAddress
.DEFAULT_DEVICE_ID
),
88 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
89 final var keyBackupService
= accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
90 serviceEnvironmentConfig
.getKeyBackupConfig().getEnclaveName(),
91 serviceEnvironmentConfig
.getKeyBackupConfig().getServiceId(),
92 serviceEnvironmentConfig
.getKeyBackupConfig().getMrenclave(),
94 this.pinHelper
= new PinHelper(keyBackupService
);
98 public void register(boolean voiceVerification
, String captcha
) throws IOException
, CaptchaRequiredException
{
99 captcha
= captcha
== null ?
null : captcha
.replace("signalcaptcha://", "");
100 if (account
.getAci() != null) {
102 final var accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
103 new DynamicCredentialsProvider(account
.getAci(),
104 account
.getAccount(),
105 account
.getPassword(),
106 account
.getDeviceId()),
109 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
110 accountManager
.setAccountAttributes(account
.getEncryptedDeviceName(),
112 account
.getLocalRegistrationId(),
115 account
.getPinMasterKey() == null ?
null : account
.getPinMasterKey().deriveRegistrationLock(),
116 account
.getSelfUnidentifiedAccessKey(),
117 account
.isUnrestrictedUnidentifiedAccess(),
119 account
.isDiscoverableByPhoneNumber());
120 account
.setRegistered(true);
121 logger
.info("Reactivated existing account, verify is not necessary.");
122 if (newManagerListener
!= null) {
123 final var m
= new ManagerImpl(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
125 newManagerListener
.accept(m
);
128 } catch (IOException e
) {
129 logger
.debug("Failed to reactivate account");
132 final ServiceResponse
<RequestVerificationCodeResponse
> response
;
133 if (voiceVerification
) {
134 response
= accountManager
.requestVoiceVerificationCode(Utils
.getDefaultLocale(),
135 Optional
.fromNullable(captcha
),
139 response
= accountManager
.requestSmsVerificationCode(false,
140 Optional
.fromNullable(captcha
),
145 handleResponseException(response
);
146 } catch (org
.whispersystems
.signalservice
.api
.push
.exceptions
.CaptchaRequiredException e
) {
147 throw new CaptchaRequiredException(e
.getMessage(), e
);
152 public void verifyAccount(
153 String verificationCode
, String pin
154 ) throws IOException
, PinLockedException
, IncorrectPinException
{
155 verificationCode
= verificationCode
.replace("-", "");
156 VerifyAccountResponse response
;
159 response
= verifyAccountWithCode(verificationCode
, null);
163 } catch (LockedException e
) {
165 throw new PinLockedException(e
.getTimeRemaining());
168 KbsPinData registrationLockData
;
170 registrationLockData
= pinHelper
.getRegistrationLockData(pin
, e
);
171 } catch (KeyBackupSystemNoDataException ex
) {
172 throw new IOException(e
);
173 } catch (KeyBackupServicePinException ex
) {
174 throw new IncorrectPinException(ex
.getTriesRemaining());
176 if (registrationLockData
== null) {
180 var registrationLock
= registrationLockData
.getMasterKey().deriveRegistrationLock();
182 response
= verifyAccountWithCode(verificationCode
, registrationLock
);
183 } catch (LockedException _e
) {
184 throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
186 masterKey
= registrationLockData
.getMasterKey();
189 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
190 account
.finishRegistration(ACI
.parseOrNull(response
.getUuid()), masterKey
, pin
);
192 ManagerImpl m
= null;
194 m
= new ManagerImpl(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
198 if (response
.isStorageCapable()) {
199 m
.retrieveRemoteStorage();
201 // Set an initial empty profile so user can be added to groups
203 m
.setProfile(null, null, null, null, null);
204 } catch (NoClassDefFoundError e
) {
205 logger
.warn("Failed to set default profile: {}", e
.getMessage());
208 if (newManagerListener
!= null) {
209 newManagerListener
.accept(m
);
219 private VerifyAccountResponse
verifyAccountWithCode(
220 final String verificationCode
, final String registrationLock
221 ) throws IOException
{
222 final ServiceResponse
<VerifyAccountResponse
> response
;
223 if (registrationLock
== null) {
224 response
= accountManager
.verifyAccount(verificationCode
,
225 account
.getLocalRegistrationId(),
227 account
.getSelfUnidentifiedAccessKey(),
228 account
.isUnrestrictedUnidentifiedAccess(),
229 ServiceConfig
.capabilities
,
230 account
.isDiscoverableByPhoneNumber());
232 response
= accountManager
.verifyAccountWithRegistrationLockPin(verificationCode
,
233 account
.getLocalRegistrationId(),
236 account
.getSelfUnidentifiedAccessKey(),
237 account
.isUnrestrictedUnidentifiedAccess(),
238 ServiceConfig
.capabilities
,
239 account
.isDiscoverableByPhoneNumber());
241 handleResponseException(response
);
242 return response
.getResult().get();
246 public void close() {
247 if (account
!= null) {
253 private void handleResponseException(final ServiceResponse
<?
> response
) throws IOException
{
254 final var throwableOptional
= response
.getExecutionError().or(response
.getApplicationError());
255 if (throwableOptional
.isPresent()) {
256 if (throwableOptional
.get() instanceof IOException
) {
257 throw (IOException
) throwableOptional
.get();
259 throw new IOException(throwableOptional
.get());