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
81 account
.getUsername(),
82 account
.getPassword(),
83 account
.getSignalingKey(),
84 SignalServiceAddress
.DEFAULT_DEVICE_ID
),
87 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
,
89 final KeyBackupService 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 username
, File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
99 ) throws IOException
{
100 PathConfig pathConfig
= PathConfig
.createDefault(settingsPath
);
102 final ServiceEnvironmentConfig serviceConfiguration
= ServiceConfig
.getServiceEnvironmentConfig(
105 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
106 IdentityKeyPair identityKey
= KeyUtils
.generateIdentityKeyPair();
107 int registrationId
= KeyHelper
.generateRegistrationId(false);
109 ProfileKey profileKey
= KeyUtils
.createProfileKey();
110 SignalAccount account
= SignalAccount
.create(pathConfig
.getDataPath(),
117 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
120 SignalAccount account
= SignalAccount
.load(pathConfig
.getDataPath(), username
);
122 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
125 public void register(boolean voiceVerification
, String captcha
) throws IOException
{
126 if (account
.getPassword() == null) {
127 account
.setPassword(KeyUtils
.createPassword());
130 if (voiceVerification
) {
131 accountManager
.requestVoiceVerificationCode(Locale
.getDefault(),
132 Optional
.fromNullable(captcha
),
135 accountManager
.requestSmsVerificationCode(false, Optional
.fromNullable(captcha
), Optional
.absent());
141 public void verifyAccount(
142 String verificationCode
, String pin
143 ) throws IOException
, KeyBackupSystemNoDataException
, KeyBackupServicePinException
{
144 verificationCode
= verificationCode
.replace("-", "");
145 if (account
.getSignalingKey() == null) {
146 account
.setSignalingKey(KeyUtils
.createSignalingKey());
148 VerifyAccountResponse response
;
150 response
= verifyAccountWithCode(verificationCode
, pin
, null);
151 account
.setPinMasterKey(null);
152 } catch (LockedException e
) {
157 KbsPinData registrationLockData
= pinHelper
.getRegistrationLockData(pin
, e
);
158 if (registrationLockData
== null) {
162 String 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 account
.setPinMasterKey(registrationLockData
.getMasterKey());
171 // TODO response.isStorageCapable()
172 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
174 account
.setDeviceId(SignalServiceAddress
.DEFAULT_DEVICE_ID
);
175 account
.setMultiDevice(false);
176 account
.setRegistered(true);
177 account
.setUuid(UuidUtil
.parseOrNull(response
.getUuid()));
178 account
.setRegistrationLockPin(pin
);
179 account
.getSignalProtocolStore()
180 .saveIdentity(account
.getSelfAddress(),
181 account
.getSignalProtocolStore().getIdentityKeyPair().getPublicKey(),
182 TrustLevel
.TRUSTED_VERIFIED
);
184 try (Manager m
= new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
)) {
194 private VerifyAccountResponse
verifyAccountWithCode(
195 final String verificationCode
, final String legacyPin
, final String registrationLock
196 ) throws IOException
{
197 return accountManager
.verifyAccountWithCode(verificationCode
,
198 account
.getSignalingKey(),
199 account
.getSignalProtocolStore().getLocalRegistrationId(),
203 account
.getSelfUnidentifiedAccessKey(),
204 account
.isUnrestrictedUnidentifiedAccess(),
205 ServiceConfig
.capabilities
,
206 account
.isDiscoverableByPhoneNumber());
210 public void close() throws IOException
{
211 if (account
!= null) {