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
.Date
;
44 import java
.util
.Locale
;
46 public class RegistrationManager
implements Closeable
{
48 private SignalAccount account
;
49 private final PathConfig pathConfig
;
50 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
51 private final String userAgent
;
53 private final SignalServiceAccountManager accountManager
;
54 private final PinHelper pinHelper
;
56 public RegistrationManager(
57 SignalAccount account
,
58 PathConfig pathConfig
,
59 ServiceEnvironmentConfig serviceEnvironmentConfig
,
62 this.account
= account
;
63 this.pathConfig
= pathConfig
;
64 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
65 this.userAgent
= userAgent
;
67 final SleepTimer timer
= new UptimeSleepTimer();
68 GroupsV2Operations groupsV2Operations
;
70 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration()));
71 } catch (Throwable ignored
) {
72 groupsV2Operations
= null;
74 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
75 new DynamicCredentialsProvider(
76 // Using empty UUID, because registering doesn't work otherwise
77 null, account
.getUsername(), account
.getPassword(), SignalServiceAddress
.DEFAULT_DEVICE_ID
),
80 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
,
82 final var keyBackupService
= accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
83 serviceEnvironmentConfig
.getKeyBackupConfig().getEnclaveName(),
84 serviceEnvironmentConfig
.getKeyBackupConfig().getServiceId(),
85 serviceEnvironmentConfig
.getKeyBackupConfig().getMrenclave(),
87 this.pinHelper
= new PinHelper(keyBackupService
);
90 public static RegistrationManager
init(
91 String username
, File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
92 ) throws IOException
{
93 var pathConfig
= PathConfig
.createDefault(settingsPath
);
95 final var serviceConfiguration
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
96 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
97 var identityKey
= KeyUtils
.generateIdentityKeyPair();
98 var registrationId
= KeyHelper
.generateRegistrationId(false);
100 var profileKey
= KeyUtils
.createProfileKey();
101 var account
= SignalAccount
.create(pathConfig
.getDataPath(),
108 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
111 var account
= SignalAccount
.load(pathConfig
.getDataPath(), username
);
113 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
116 public void register(boolean voiceVerification
, String captcha
) throws IOException
{
117 if (account
.getPassword() == null) {
118 account
.setPassword(KeyUtils
.createPassword());
121 if (voiceVerification
) {
122 accountManager
.requestVoiceVerificationCode(Locale
.getDefault(),
123 Optional
.fromNullable(captcha
),
126 accountManager
.requestSmsVerificationCode(false, Optional
.fromNullable(captcha
), Optional
.absent());
132 public Manager
verifyAccount(
133 String verificationCode
, String pin
134 ) throws IOException
, KeyBackupSystemNoDataException
, KeyBackupServicePinException
{
135 verificationCode
= verificationCode
.replace("-", "");
136 VerifyAccountResponse response
;
138 response
= verifyAccountWithCode(verificationCode
, pin
, null);
139 account
.setPinMasterKey(null);
140 } catch (LockedException e
) {
145 var registrationLockData
= pinHelper
.getRegistrationLockData(pin
, e
);
146 if (registrationLockData
== null) {
150 var registrationLock
= registrationLockData
.getMasterKey().deriveRegistrationLock();
152 response
= verifyAccountWithCode(verificationCode
, null, registrationLock
);
153 } catch (LockedException _e
) {
154 throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
156 account
.setPinMasterKey(registrationLockData
.getMasterKey());
159 // TODO response.isStorageCapable()
160 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
162 account
.setDeviceId(SignalServiceAddress
.DEFAULT_DEVICE_ID
);
163 account
.setMultiDevice(false);
164 account
.setRegistered(true);
165 account
.setUuid(UuidUtil
.parseOrNull(response
.getUuid()));
166 account
.setRegistrationLockPin(pin
);
167 account
.getSessionStore().archiveAllSessions();
168 final var recipientId
= account
.getRecipientStore().resolveRecipientTrusted(account
.getSelfAddress());
169 final var publicKey
= account
.getIdentityKeyPair().getPublicKey();
170 account
.getIdentityKeyStore().saveIdentity(recipientId
, publicKey
, new Date());
171 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, publicKey
, TrustLevel
.TRUSTED_VERIFIED
);
175 m
= new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
181 final var result
= m
;
193 private VerifyAccountResponse
verifyAccountWithCode(
194 final String verificationCode
, final String legacyPin
, final String registrationLock
195 ) throws IOException
{
196 return accountManager
.verifyAccountWithCode(verificationCode
,
198 account
.getLocalRegistrationId(),
202 account
.getSelfUnidentifiedAccessKey(),
203 account
.isUnrestrictedUnidentifiedAccess(),
204 ServiceConfig
.capabilities
,
205 account
.isDiscoverableByPhoneNumber());
209 public void close() throws IOException
{
210 if (account
!= null) {