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
.signal
.zkgroup
.profiles
.ProfileKey
;
26 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
27 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
28 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
29 import org
.whispersystems
.signalservice
.api
.KbsPinData
;
30 import org
.whispersystems
.signalservice
.api
.KeyBackupService
;
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
.push
.SignalServiceAddress
;
37 import org
.whispersystems
.signalservice
.api
.util
.SleepTimer
;
38 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
39 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
40 import org
.whispersystems
.signalservice
.internal
.push
.LockedException
;
41 import org
.whispersystems
.signalservice
.internal
.push
.VerifyAccountResponse
;
42 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
44 import java
.io
.Closeable
;
46 import java
.io
.IOException
;
47 import java
.util
.Locale
;
49 public class RegistrationManager
implements Closeable
{
51 private SignalAccount account
;
52 private final PathConfig pathConfig
;
53 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
54 private final String userAgent
;
56 private final SignalServiceAccountManager accountManager
;
57 private final PinHelper pinHelper
;
59 public RegistrationManager(
60 SignalAccount account
,
61 PathConfig pathConfig
,
62 ServiceEnvironmentConfig serviceEnvironmentConfig
,
65 this.account
= account
;
66 this.pathConfig
= pathConfig
;
67 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
68 this.userAgent
= userAgent
;
70 final SleepTimer timer
= new UptimeSleepTimer();
71 GroupsV2Operations groupsV2Operations
;
73 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration()));
74 } catch (Throwable ignored
) {
75 groupsV2Operations
= null;
77 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
78 new DynamicCredentialsProvider(
79 // Using empty UUID, because registering doesn't work otherwise
80 null, account
.getUsername(), account
.getPassword(), SignalServiceAddress
.DEFAULT_DEVICE_ID
),
83 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
,
85 final KeyBackupService keyBackupService
= accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
86 serviceEnvironmentConfig
.getKeyBackupConfig().getEnclaveName(),
87 serviceEnvironmentConfig
.getKeyBackupConfig().getServiceId(),
88 serviceEnvironmentConfig
.getKeyBackupConfig().getMrenclave(),
90 this.pinHelper
= new PinHelper(keyBackupService
);
93 public static RegistrationManager
init(
94 String username
, File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
95 ) throws IOException
{
96 PathConfig pathConfig
= PathConfig
.createDefault(settingsPath
);
98 final ServiceEnvironmentConfig serviceConfiguration
= ServiceConfig
.getServiceEnvironmentConfig(
101 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
102 IdentityKeyPair identityKey
= KeyUtils
.generateIdentityKeyPair();
103 int registrationId
= KeyHelper
.generateRegistrationId(false);
105 ProfileKey profileKey
= KeyUtils
.createProfileKey();
106 SignalAccount account
= SignalAccount
.create(pathConfig
.getDataPath(),
113 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
116 SignalAccount account
= SignalAccount
.load(pathConfig
.getDataPath(), username
);
118 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
121 public void register(boolean voiceVerification
, String captcha
) throws IOException
{
122 if (account
.getPassword() == null) {
123 account
.setPassword(KeyUtils
.createPassword());
126 if (voiceVerification
) {
127 accountManager
.requestVoiceVerificationCode(Locale
.getDefault(),
128 Optional
.fromNullable(captcha
),
131 accountManager
.requestSmsVerificationCode(false, Optional
.fromNullable(captcha
), Optional
.absent());
137 public void verifyAccount(
138 String verificationCode
, String pin
139 ) throws IOException
, KeyBackupSystemNoDataException
, KeyBackupServicePinException
{
140 verificationCode
= verificationCode
.replace("-", "");
141 VerifyAccountResponse response
;
143 response
= verifyAccountWithCode(verificationCode
, pin
, null);
144 account
.setPinMasterKey(null);
145 } catch (LockedException e
) {
150 KbsPinData registrationLockData
= pinHelper
.getRegistrationLockData(pin
, e
);
151 if (registrationLockData
== null) {
155 String registrationLock
= registrationLockData
.getMasterKey().deriveRegistrationLock();
157 response
= verifyAccountWithCode(verificationCode
, null, registrationLock
);
158 } catch (LockedException _e
) {
159 throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
161 account
.setPinMasterKey(registrationLockData
.getMasterKey());
164 // TODO response.isStorageCapable()
165 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
167 account
.setDeviceId(SignalServiceAddress
.DEFAULT_DEVICE_ID
);
168 account
.setMultiDevice(false);
169 account
.setRegistered(true);
170 account
.setUuid(UuidUtil
.parseOrNull(response
.getUuid()));
171 account
.setRegistrationLockPin(pin
);
172 account
.getSignalProtocolStore().archiveAllSessions();
173 account
.getSignalProtocolStore()
174 .saveIdentity(account
.getSelfAddress(),
175 account
.getSignalProtocolStore().getIdentityKeyPair().getPublicKey(),
176 TrustLevel
.TRUSTED_VERIFIED
);
178 try (Manager m
= new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
)) {
188 private VerifyAccountResponse
verifyAccountWithCode(
189 final String verificationCode
, final String legacyPin
, final String registrationLock
190 ) throws IOException
{
191 return accountManager
.verifyAccountWithCode(verificationCode
,
193 account
.getSignalProtocolStore().getLocalRegistrationId(),
197 account
.getSelfUnidentifiedAccessKey(),
198 account
.isUnrestrictedUnidentifiedAccess(),
199 ServiceConfig
.capabilities
,
200 account
.isDiscoverableByPhoneNumber());
204 public void close() throws IOException
{
205 if (account
!= null) {