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
.config
.ServiceConfig
;
20 import org
.asamk
.signal
.manager
.config
.ServiceEnvironment
;
21 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
22 import org
.asamk
.signal
.manager
.helper
.PinHelper
;
23 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
24 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
25 import org
.slf4j
.Logger
;
26 import org
.slf4j
.LoggerFactory
;
27 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
28 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
29 import org
.whispersystems
.signalservice
.api
.KeyBackupServicePinException
;
30 import org
.whispersystems
.signalservice
.api
.KeyBackupSystemNoDataException
;
31 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
32 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
33 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
34 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
35 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
36 import org
.whispersystems
.signalservice
.api
.util
.SleepTimer
;
37 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
38 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
39 import org
.whispersystems
.signalservice
.internal
.push
.LockedException
;
40 import org
.whispersystems
.signalservice
.internal
.push
.VerifyAccountResponse
;
41 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
43 import java
.io
.Closeable
;
45 import java
.io
.IOException
;
46 import java
.util
.Locale
;
48 public class RegistrationManager
implements Closeable
{
50 private final static Logger logger
= LoggerFactory
.getLogger(RegistrationManager
.class);
52 private SignalAccount account
;
53 private final PathConfig pathConfig
;
54 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
55 private final String userAgent
;
57 private final SignalServiceAccountManager accountManager
;
58 private final PinHelper pinHelper
;
60 public RegistrationManager(
61 SignalAccount account
,
62 PathConfig pathConfig
,
63 ServiceEnvironmentConfig serviceEnvironmentConfig
,
66 this.account
= account
;
67 this.pathConfig
= pathConfig
;
68 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
69 this.userAgent
= userAgent
;
71 final SleepTimer timer
= new UptimeSleepTimer();
72 GroupsV2Operations groupsV2Operations
;
74 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration()));
75 } catch (Throwable ignored
) {
76 groupsV2Operations
= null;
78 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
79 new DynamicCredentialsProvider(
80 // Using empty UUID, because registering doesn't work otherwise
81 null, account
.getUsername(), account
.getPassword(), SignalServiceAddress
.DEFAULT_DEVICE_ID
),
84 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
,
86 final var keyBackupService
= accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
87 serviceEnvironmentConfig
.getKeyBackupConfig().getEnclaveName(),
88 serviceEnvironmentConfig
.getKeyBackupConfig().getServiceId(),
89 serviceEnvironmentConfig
.getKeyBackupConfig().getMrenclave(),
91 this.pinHelper
= new PinHelper(keyBackupService
);
94 public static RegistrationManager
init(
95 String username
, File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
96 ) throws IOException
{
97 var pathConfig
= PathConfig
.createDefault(settingsPath
);
99 final var serviceConfiguration
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
100 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
101 var identityKey
= KeyUtils
.generateIdentityKeyPair();
102 var registrationId
= KeyHelper
.generateRegistrationId(false);
104 var profileKey
= KeyUtils
.createProfileKey();
105 var account
= SignalAccount
.create(pathConfig
.getDataPath(),
111 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
114 var account
= SignalAccount
.load(pathConfig
.getDataPath(), username
, true);
116 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
119 public void register(boolean voiceVerification
, String captcha
) throws IOException
{
120 if (voiceVerification
) {
121 accountManager
.requestVoiceVerificationCode(getDefaultLocale(),
122 Optional
.fromNullable(captcha
),
125 accountManager
.requestSmsVerificationCode(false, Optional
.fromNullable(captcha
), Optional
.absent());
129 private Locale
getDefaultLocale() {
130 final var locale
= Locale
.getDefault();
132 Locale
.LanguageRange
.parse(locale
.getLanguage() + "-" + locale
.getCountry());
133 } catch (IllegalArgumentException e
) {
134 logger
.debug("Invalid locale, ignoring: {}", locale
);
141 public Manager
verifyAccount(
142 String verificationCode
, String pin
143 ) throws IOException
, LockedException
, KeyBackupSystemNoDataException
, KeyBackupServicePinException
{
144 verificationCode
= verificationCode
.replace("-", "");
145 VerifyAccountResponse response
;
148 response
= verifyAccountWithCode(verificationCode
, null, null);
152 } catch (LockedException e
) {
157 var registrationLockData
= pinHelper
.getRegistrationLockData(pin
, e
);
158 if (registrationLockData
== null) {
159 response
= verifyAccountWithCode(verificationCode
, pin
, null);
162 var registrationLock
= registrationLockData
.getMasterKey().deriveRegistrationLock();
164 response
= verifyAccountWithCode(verificationCode
, null, registrationLock
);
165 } catch (LockedException _e
) {
166 throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
168 masterKey
= registrationLockData
.getMasterKey();
172 // TODO response.isStorageCapable()
173 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
174 account
.finishRegistration(UuidUtil
.parseOrNull(response
.getUuid()), masterKey
, pin
);
178 m
= new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
182 // Set an initial empty profile so user can be added to groups
183 m
.setProfile(null, null, null, null, null);
185 final var result
= m
;
196 private VerifyAccountResponse
verifyAccountWithCode(
197 final String verificationCode
, final String legacyPin
, final String registrationLock
198 ) throws IOException
{
199 return accountManager
.verifyAccountWithCode(verificationCode
,
201 account
.getLocalRegistrationId(),
205 account
.getSelfUnidentifiedAccessKey(),
206 account
.isUnrestrictedUnidentifiedAccess(),
207 ServiceConfig
.capabilities
,
208 account
.isDiscoverableByPhoneNumber());
212 public void close() throws IOException
{
213 if (account
!= null) {