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
.kbs
.MasterKey
;
33 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
34 import org
.whispersystems
.signalservice
.api
.util
.SleepTimer
;
35 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
36 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
37 import org
.whispersystems
.signalservice
.internal
.push
.LockedException
;
38 import org
.whispersystems
.signalservice
.internal
.push
.VerifyAccountResponse
;
39 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
41 import java
.io
.Closeable
;
43 import java
.io
.IOException
;
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(),
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 (voiceVerification
) {
117 accountManager
.requestVoiceVerificationCode(Locale
.getDefault(),
118 Optional
.fromNullable(captcha
),
121 accountManager
.requestSmsVerificationCode(false, Optional
.fromNullable(captcha
), Optional
.absent());
125 public Manager
verifyAccount(
126 String verificationCode
, String pin
127 ) throws IOException
, KeyBackupSystemNoDataException
, KeyBackupServicePinException
{
128 verificationCode
= verificationCode
.replace("-", "");
129 VerifyAccountResponse response
;
132 response
= verifyAccountWithCode(verificationCode
, pin
, null);
135 } catch (LockedException e
) {
140 var registrationLockData
= pinHelper
.getRegistrationLockData(pin
, e
);
141 if (registrationLockData
== null) {
145 var registrationLock
= registrationLockData
.getMasterKey().deriveRegistrationLock();
147 response
= verifyAccountWithCode(verificationCode
, null, registrationLock
);
148 } catch (LockedException _e
) {
149 throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
151 masterKey
= registrationLockData
.getMasterKey();
154 // TODO response.isStorageCapable()
155 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
156 account
.finishRegistration(UuidUtil
.parseOrNull(response
.getUuid()), masterKey
, pin
);
160 m
= new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
165 final var result
= m
;
176 private VerifyAccountResponse
verifyAccountWithCode(
177 final String verificationCode
, final String legacyPin
, final String registrationLock
178 ) throws IOException
{
179 return accountManager
.verifyAccountWithCode(verificationCode
,
181 account
.getLocalRegistrationId(),
185 account
.getSelfUnidentifiedAccessKey(),
186 account
.isUnrestrictedUnidentifiedAccess(),
187 ServiceConfig
.capabilities
,
188 account
.isDiscoverableByPhoneNumber());
192 public void close() throws IOException
{
193 if (account
!= null) {