2 Copyright (C) 2015-2022 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
.internal
;
19 import org
.asamk
.signal
.manager
.Manager
;
20 import org
.asamk
.signal
.manager
.ProvisioningManager
;
21 import org
.asamk
.signal
.manager
.Settings
;
22 import org
.asamk
.signal
.manager
.api
.DeviceLinkUrl
;
23 import org
.asamk
.signal
.manager
.api
.UserAlreadyExistsException
;
24 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
25 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
26 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
27 import org
.asamk
.signal
.manager
.storage
.accounts
.AccountsStore
;
28 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
29 import org
.signal
.libsignal
.protocol
.IdentityKeyPair
;
30 import org
.signal
.libsignal
.protocol
.util
.KeyHelper
;
31 import org
.slf4j
.Logger
;
32 import org
.slf4j
.LoggerFactory
;
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
.push
.exceptions
.AuthorizationFailedException
;
38 import org
.whispersystems
.signalservice
.api
.util
.DeviceNameUtil
;
39 import org
.whispersystems
.signalservice
.internal
.push
.ConfirmCodeMessage
;
40 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
42 import java
.io
.IOException
;
44 import java
.nio
.channels
.OverlappingFileLockException
;
45 import java
.util
.concurrent
.TimeoutException
;
46 import java
.util
.function
.Consumer
;
48 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.getCapabilities
;
50 public class ProvisioningManagerImpl
implements ProvisioningManager
{
52 private final static Logger logger
= LoggerFactory
.getLogger(ProvisioningManagerImpl
.class);
54 private final PathConfig pathConfig
;
55 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
56 private final String userAgent
;
57 private final Consumer
<Manager
> newManagerListener
;
58 private final AccountsStore accountsStore
;
60 private final SignalServiceAccountManager accountManager
;
61 private final IdentityKeyPair tempIdentityKey
;
62 private final int registrationId
;
63 private final int pniRegistrationId
;
64 private final String password
;
66 public ProvisioningManagerImpl(
67 PathConfig pathConfig
,
68 ServiceEnvironmentConfig serviceEnvironmentConfig
,
70 final Consumer
<Manager
> newManagerListener
,
71 final AccountsStore accountsStore
73 this.pathConfig
= pathConfig
;
74 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
75 this.userAgent
= userAgent
;
76 this.newManagerListener
= newManagerListener
;
77 this.accountsStore
= accountsStore
;
79 tempIdentityKey
= KeyUtils
.generateIdentityKeyPair();
80 registrationId
= KeyHelper
.generateRegistrationId(false);
81 pniRegistrationId
= KeyHelper
.generateRegistrationId(false);
82 password
= KeyUtils
.createPassword();
83 GroupsV2Operations groupsV2Operations
;
85 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.signalServiceConfiguration()),
86 ServiceConfig
.GROUP_MAX_SIZE
);
87 } catch (Throwable ignored
) {
88 groupsV2Operations
= null;
90 accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.signalServiceConfiguration(),
91 new DynamicCredentialsProvider(null, null, null, password
, SignalServiceAddress
.DEFAULT_DEVICE_ID
),
94 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
98 public URI
getDeviceLinkUri() throws TimeoutException
, IOException
{
99 var deviceUuid
= accountManager
.getNewDeviceUuid();
101 return new DeviceLinkUrl(deviceUuid
, tempIdentityKey
.getPublicKey().getPublicKey()).createDeviceLinkUri();
105 public String
finishDeviceLink(String deviceName
) throws IOException
, TimeoutException
, UserAlreadyExistsException
{
106 var ret
= accountManager
.getNewDeviceRegistration(tempIdentityKey
);
107 var number
= ret
.getNumber();
108 var aci
= ret
.getAci();
109 var pni
= ret
.getPni();
111 logger
.info("Received link information from {}, linking in progress ...", number
);
113 var accountPath
= accountsStore
.getPathByAci(aci
);
114 if (accountPath
== null) {
115 accountPath
= accountsStore
.getPathByNumber(number
);
117 if (accountPath
!= null
118 && SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)
119 && !canRelinkExistingAccount(accountPath
)) {
120 throw new UserAlreadyExistsException(number
, SignalAccount
.getFileName(pathConfig
.dataPath(), accountPath
));
122 if (accountPath
== null) {
123 accountPath
= accountsStore
.addAccount(number
, aci
);
125 accountsStore
.updateAccount(accountPath
, number
, aci
);
128 var encryptedDeviceName
= deviceName
== null
130 : DeviceNameUtil
.encryptDeviceName(deviceName
, ret
.getAciIdentity().getPrivateKey());
132 logger
.debug("Finishing new device registration");
133 var deviceId
= accountManager
.finishNewDeviceRegistration(ret
.getProvisioningCode(),
134 new ConfirmCodeMessage(false,
139 getCapabilities(false)));
141 // Create new account with the synced identity
142 var profileKey
= ret
.getProfileKey() == null ? KeyUtils
.createProfileKey() : ret
.getProfileKey();
144 SignalAccount account
= null;
146 account
= SignalAccount
.createOrUpdateLinkedAccount(pathConfig
.dataPath(),
149 serviceEnvironmentConfig
.type(),
155 ret
.getAciIdentity(),
156 ret
.getPniIdentity(),
161 account
.getConfigurationStore().setReadReceipts(ret
.isReadReceipts());
163 ManagerImpl m
= null;
165 m
= new ManagerImpl(account
,
167 new AccountFileUpdaterImpl(accountsStore
, accountPath
),
168 serviceEnvironmentConfig
,
172 logger
.debug("Refreshing pre keys");
175 } catch (Exception e
) {
176 logger
.error("Failed to refresh pre keys.", e
);
179 logger
.debug("Requesting sync data");
181 m
.requestAllSyncData();
182 } catch (Exception e
) {
184 "Failed to request sync messages from linked device, data can be requested again with `sendSyncRequest`.",
188 if (newManagerListener
!= null) {
189 newManagerListener
.accept(m
);
199 if (account
!= null) {
205 private boolean canRelinkExistingAccount(final String accountPath
) throws IOException
{
206 final SignalAccount signalAccount
;
208 signalAccount
= SignalAccount
.load(pathConfig
.dataPath(), accountPath
, false, Settings
.DEFAULT
);
209 } catch (IOException e
) {
210 logger
.debug("Account in use or failed to load.", e
);
212 } catch (OverlappingFileLockException e
) {
213 logger
.debug("Account in use.", e
);
217 try (signalAccount
) {
218 if (signalAccount
.isPrimaryDevice()) {
219 logger
.debug("Account is a primary device.");
222 if (signalAccount
.isRegistered()
223 && signalAccount
.getServiceEnvironment() != null
224 && signalAccount
.getServiceEnvironment() != serviceEnvironmentConfig
.type()) {
225 logger
.debug("Account is registered in another environment: {}.",
226 signalAccount
.getServiceEnvironment());
230 final var m
= new ManagerImpl(signalAccount
,
232 new AccountFileUpdaterImpl(accountsStore
, accountPath
),
233 serviceEnvironmentConfig
,
236 m
.checkAccountState();
237 } catch (AuthorizationFailedException ignored
) {
241 logger
.debug("Account is still successfully linked.");