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
.ServiceEnvironment
;
24 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
25 import org
.asamk
.signal
.manager
.helper
.PinHelper
;
26 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
27 import org
.asamk
.signal
.manager
.storage
.identities
.TrustNewIdentity
;
28 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
29 import org
.slf4j
.Logger
;
30 import org
.slf4j
.LoggerFactory
;
31 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
32 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
33 import org
.whispersystems
.signalservice
.api
.KbsPinData
;
34 import org
.whispersystems
.signalservice
.api
.KeyBackupServicePinException
;
35 import org
.whispersystems
.signalservice
.api
.KeyBackupSystemNoDataException
;
36 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
37 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
38 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
39 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
40 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
41 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
42 import org
.whispersystems
.signalservice
.internal
.ServiceResponse
;
43 import org
.whispersystems
.signalservice
.internal
.push
.LockedException
;
44 import org
.whispersystems
.signalservice
.internal
.push
.RequestVerificationCodeResponse
;
45 import org
.whispersystems
.signalservice
.internal
.push
.VerifyAccountResponse
;
46 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
48 import java
.io
.Closeable
;
50 import java
.io
.IOException
;
51 import java
.util
.Locale
;
53 public class RegistrationManager
implements Closeable
{
55 private final static Logger logger
= LoggerFactory
.getLogger(RegistrationManager
.class);
57 private SignalAccount account
;
58 private final PathConfig pathConfig
;
59 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
60 private final String userAgent
;
62 private final SignalServiceAccountManager accountManager
;
63 private final PinHelper pinHelper
;
65 private RegistrationManager(
66 SignalAccount account
,
67 PathConfig pathConfig
,
68 ServiceEnvironmentConfig serviceEnvironmentConfig
,
71 this.account
= account
;
72 this.pathConfig
= pathConfig
;
73 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
74 this.userAgent
= userAgent
;
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
.getUsername(), 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
);
97 public static RegistrationManager
init(
98 String number
, File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
99 ) throws IOException
{
100 var pathConfig
= PathConfig
.createDefault(settingsPath
);
102 final var serviceConfiguration
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
103 if (!SignalAccount
.userExists(pathConfig
.dataPath(), number
)) {
104 var identityKey
= KeyUtils
.generateIdentityKeyPair();
105 var registrationId
= KeyHelper
.generateRegistrationId(false);
107 var profileKey
= KeyUtils
.createProfileKey();
108 var account
= SignalAccount
.create(pathConfig
.dataPath(),
113 TrustNewIdentity
.ON_FIRST_USE
);
115 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
118 var account
= SignalAccount
.load(pathConfig
.dataPath(), number
, true, TrustNewIdentity
.ON_FIRST_USE
);
120 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
123 public void register(boolean voiceVerification
, String captcha
) throws IOException
, CaptchaRequiredException
{
124 final ServiceResponse
<RequestVerificationCodeResponse
> response
;
125 if (voiceVerification
) {
126 response
= accountManager
.requestVoiceVerificationCode(getDefaultLocale(),
127 Optional
.fromNullable(captcha
),
131 response
= accountManager
.requestSmsVerificationCode(false,
132 Optional
.fromNullable(captcha
),
137 handleResponseException(response
);
138 } catch (org
.whispersystems
.signalservice
.api
.push
.exceptions
.CaptchaRequiredException e
) {
139 throw new CaptchaRequiredException(e
.getMessage(), e
);
143 private Locale
getDefaultLocale() {
144 final var locale
= Locale
.getDefault();
146 Locale
.LanguageRange
.parse(locale
.getLanguage() + "-" + locale
.getCountry());
147 } catch (IllegalArgumentException e
) {
148 logger
.debug("Invalid locale, ignoring: {}", locale
);
155 public Manager
verifyAccount(
156 String verificationCode
, String pin
157 ) throws IOException
, PinLockedException
, IncorrectPinException
{
158 verificationCode
= verificationCode
.replace("-", "");
159 VerifyAccountResponse response
;
162 response
= verifyAccountWithCode(verificationCode
, null);
166 } catch (LockedException e
) {
168 throw new PinLockedException(e
.getTimeRemaining());
171 KbsPinData registrationLockData
;
173 registrationLockData
= pinHelper
.getRegistrationLockData(pin
, e
);
174 } catch (KeyBackupSystemNoDataException ex
) {
175 throw new IOException(e
);
176 } catch (KeyBackupServicePinException ex
) {
177 throw new IncorrectPinException(ex
.getTriesRemaining());
179 if (registrationLockData
== null) {
183 var registrationLock
= registrationLockData
.getMasterKey().deriveRegistrationLock();
185 response
= verifyAccountWithCode(verificationCode
, registrationLock
);
186 } catch (LockedException _e
) {
187 throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
189 masterKey
= registrationLockData
.getMasterKey();
192 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
193 account
.finishRegistration(UuidUtil
.parseOrNull(response
.getUuid()), masterKey
, pin
);
195 ManagerImpl m
= null;
197 m
= new ManagerImpl(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
201 if (response
.isStorageCapable()) {
202 m
.retrieveRemoteStorage();
204 // Set an initial empty profile so user can be added to groups
206 m
.setProfile(null, null, null, null, null);
207 } catch (NoClassDefFoundError e
) {
208 logger
.warn("Failed to set default profile: {}", e
.getMessage());
211 final var result
= m
;
222 private VerifyAccountResponse
verifyAccountWithCode(
223 final String verificationCode
, final String registrationLock
224 ) throws IOException
{
225 final ServiceResponse
<VerifyAccountResponse
> response
;
226 if (registrationLock
== null) {
227 response
= accountManager
.verifyAccount(verificationCode
,
228 account
.getLocalRegistrationId(),
230 account
.getSelfUnidentifiedAccessKey(),
231 account
.isUnrestrictedUnidentifiedAccess(),
232 ServiceConfig
.capabilities
,
233 account
.isDiscoverableByPhoneNumber());
235 response
= accountManager
.verifyAccountWithRegistrationLockPin(verificationCode
,
236 account
.getLocalRegistrationId(),
239 account
.getSelfUnidentifiedAccessKey(),
240 account
.isUnrestrictedUnidentifiedAccess(),
241 ServiceConfig
.capabilities
,
242 account
.isDiscoverableByPhoneNumber());
244 handleResponseException(response
);
245 return response
.getResult().get();
249 public void close() throws IOException
{
250 if (account
!= null) {
256 private void handleResponseException(final ServiceResponse
<?
> response
) throws IOException
{
257 final var throwableOptional
= response
.getExecutionError().or(response
.getApplicationError());
258 if (throwableOptional
.isPresent()) {
259 if (throwableOptional
.get() instanceof IOException
) {
260 throw (IOException
) throwableOptional
.get();
262 throw new IOException(throwableOptional
.get());