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
.whispersystems
.libsignal
.util
.KeyHelper
;
26 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
27 import org
.whispersystems
.signalservice
.api
.KeyBackupServicePinException
;
28 import org
.whispersystems
.signalservice
.api
.KeyBackupSystemNoDataException
;
29 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
30 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
31 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
32 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
33 import org
.whispersystems
.signalservice
.api
.util
.SleepTimer
;
34 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
35 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
36 import org
.whispersystems
.signalservice
.internal
.push
.LockedException
;
37 import org
.whispersystems
.signalservice
.internal
.push
.VerifyAccountResponse
;
38 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
40 import java
.io
.Closeable
;
42 import java
.io
.IOException
;
43 import java
.util
.Locale
;
45 public class RegistrationManager
implements Closeable
{
47 private SignalAccount account
;
48 private final PathConfig pathConfig
;
49 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
50 private final String userAgent
;
52 private final SignalServiceAccountManager accountManager
;
53 private final PinHelper pinHelper
;
55 public RegistrationManager(
56 SignalAccount account
,
57 PathConfig pathConfig
,
58 ServiceEnvironmentConfig serviceEnvironmentConfig
,
61 this.account
= account
;
62 this.pathConfig
= pathConfig
;
63 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
64 this.userAgent
= userAgent
;
66 final SleepTimer timer
= new UptimeSleepTimer();
67 GroupsV2Operations groupsV2Operations
;
69 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration()));
70 } catch (Throwable ignored
) {
71 groupsV2Operations
= null;
73 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
74 new DynamicCredentialsProvider(
75 // Using empty UUID, because registering doesn't work otherwise
76 null, account
.getUsername(), account
.getPassword(), SignalServiceAddress
.DEFAULT_DEVICE_ID
),
79 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
,
81 final var keyBackupService
= accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
82 serviceEnvironmentConfig
.getKeyBackupConfig().getEnclaveName(),
83 serviceEnvironmentConfig
.getKeyBackupConfig().getServiceId(),
84 serviceEnvironmentConfig
.getKeyBackupConfig().getMrenclave(),
86 this.pinHelper
= new PinHelper(keyBackupService
);
89 public static RegistrationManager
init(
90 String username
, File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
91 ) throws IOException
{
92 var pathConfig
= PathConfig
.createDefault(settingsPath
);
94 final var serviceConfiguration
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
95 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
96 var identityKey
= KeyUtils
.generateIdentityKeyPair();
97 var registrationId
= KeyHelper
.generateRegistrationId(false);
99 var profileKey
= KeyUtils
.createProfileKey();
100 var account
= SignalAccount
.create(pathConfig
.getDataPath(),
107 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
110 var account
= SignalAccount
.load(pathConfig
.getDataPath(), username
);
112 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
115 public void register(boolean voiceVerification
, String captcha
) throws IOException
{
116 if (account
.getPassword() == null) {
117 account
.setPassword(KeyUtils
.createPassword());
120 if (voiceVerification
) {
121 accountManager
.requestVoiceVerificationCode(Locale
.getDefault(),
122 Optional
.fromNullable(captcha
),
125 accountManager
.requestSmsVerificationCode(false, Optional
.fromNullable(captcha
), Optional
.absent());
131 public void verifyAccount(
132 String verificationCode
, String pin
133 ) throws IOException
, KeyBackupSystemNoDataException
, KeyBackupServicePinException
{
134 verificationCode
= verificationCode
.replace("-", "");
135 VerifyAccountResponse response
;
137 response
= verifyAccountWithCode(verificationCode
, pin
, null);
138 account
.setPinMasterKey(null);
139 } catch (LockedException e
) {
144 var registrationLockData
= pinHelper
.getRegistrationLockData(pin
, e
);
145 if (registrationLockData
== null) {
149 var registrationLock
= registrationLockData
.getMasterKey().deriveRegistrationLock();
151 response
= verifyAccountWithCode(verificationCode
, null, registrationLock
);
152 } catch (LockedException _e
) {
153 throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
155 account
.setPinMasterKey(registrationLockData
.getMasterKey());
158 // TODO response.isStorageCapable()
159 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
161 account
.setDeviceId(SignalServiceAddress
.DEFAULT_DEVICE_ID
);
162 account
.setMultiDevice(false);
163 account
.setRegistered(true);
164 account
.setUuid(UuidUtil
.parseOrNull(response
.getUuid()));
165 account
.setRegistrationLockPin(pin
);
166 account
.getSignalProtocolStore().archiveAllSessions();
167 account
.getSignalProtocolStore()
168 .saveIdentity(account
.getSelfAddress(),
169 account
.getSignalProtocolStore().getIdentityKeyPair().getPublicKey(),
170 TrustLevel
.TRUSTED_VERIFIED
);
172 try (var m
= new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
)) {
182 private VerifyAccountResponse
verifyAccountWithCode(
183 final String verificationCode
, final String legacyPin
, final String registrationLock
184 ) throws IOException
{
185 return accountManager
.verifyAccountWithCode(verificationCode
,
187 account
.getSignalProtocolStore().getLocalRegistrationId(),
191 account
.getSelfUnidentifiedAccessKey(),
192 account
.isUnrestrictedUnidentifiedAccess(),
193 ServiceConfig
.capabilities
,
194 account
.isDiscoverableByPhoneNumber());
198 public void close() throws IOException
{
199 if (account
!= null) {