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
.storage
.SignalAccount
;
23 import org
.asamk
.signal
.manager
.storage
.identities
.TrustNewIdentity
;
24 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
25 import org
.slf4j
.Logger
;
26 import org
.slf4j
.LoggerFactory
;
27 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
28 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
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
.push
.exceptions
.AuthorizationFailedException
;
34 import org
.whispersystems
.signalservice
.api
.util
.DeviceNameUtil
;
35 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
38 import java
.io
.IOException
;
40 import java
.util
.concurrent
.TimeoutException
;
42 public class ProvisioningManager
{
44 private final static Logger logger
= LoggerFactory
.getLogger(ProvisioningManager
.class);
46 private final PathConfig pathConfig
;
47 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
48 private final String userAgent
;
50 private final SignalServiceAccountManager accountManager
;
51 private final IdentityKeyPair tempIdentityKey
;
52 private final int registrationId
;
53 private final String password
;
55 ProvisioningManager(PathConfig pathConfig
, ServiceEnvironmentConfig serviceEnvironmentConfig
, String userAgent
) {
56 this.pathConfig
= pathConfig
;
57 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
58 this.userAgent
= userAgent
;
60 tempIdentityKey
= KeyUtils
.generateIdentityKeyPair();
61 registrationId
= KeyHelper
.generateRegistrationId(false);
62 password
= KeyUtils
.createPassword();
63 GroupsV2Operations groupsV2Operations
;
65 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration()));
66 } catch (Throwable ignored
) {
67 groupsV2Operations
= null;
69 accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
70 new DynamicCredentialsProvider(null, null, password
, SignalServiceAddress
.DEFAULT_DEVICE_ID
),
73 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
76 public static ProvisioningManager
init(
77 File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
79 var pathConfig
= PathConfig
.createDefault(settingsPath
);
81 final var serviceConfiguration
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
83 return new ProvisioningManager(pathConfig
, serviceConfiguration
, userAgent
);
86 public URI
getDeviceLinkUri() throws TimeoutException
, IOException
{
87 var deviceUuid
= accountManager
.getNewDeviceUuid();
89 return new DeviceLinkInfo(deviceUuid
, tempIdentityKey
.getPublicKey().getPublicKey()).createDeviceLinkUri();
92 public Manager
finishDeviceLink(String deviceName
) throws IOException
, TimeoutException
, UserAlreadyExists
{
93 var ret
= accountManager
.getNewDeviceRegistration(tempIdentityKey
);
94 var number
= ret
.getNumber();
96 logger
.info("Received link information from {}, linking in progress ...", number
);
98 if (SignalAccount
.userExists(pathConfig
.dataPath(), number
) && !canRelinkExistingAccount(number
)) {
99 throw new UserAlreadyExists(number
, SignalAccount
.getFileName(pathConfig
.dataPath(), number
));
102 var encryptedDeviceName
= deviceName
== null
104 : DeviceNameUtil
.encryptDeviceName(deviceName
, ret
.getIdentity().getPrivateKey());
106 logger
.debug("Finishing new device registration");
107 var deviceId
= accountManager
.finishNewDeviceRegistration(ret
.getProvisioningCode(),
111 encryptedDeviceName
);
113 // Create new account with the synced identity
114 var profileKey
= ret
.getProfileKey() == null ? KeyUtils
.createProfileKey() : ret
.getProfileKey();
116 SignalAccount account
= null;
118 account
= SignalAccount
.createOrUpdateLinkedAccount(pathConfig
.dataPath(),
127 TrustNewIdentity
.ON_FIRST_USE
);
129 ManagerImpl m
= null;
131 m
= new ManagerImpl(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
133 logger
.debug("Refreshing pre keys");
136 } catch (Exception e
) {
137 logger
.error("Failed to refresh pre keys.");
140 logger
.debug("Requesting sync data");
142 m
.requestAllSyncData();
143 } catch (Exception e
) {
145 "Failed to request sync messages from linked device, data can be requested again with `sendSyncRequest`.");
148 final var result
= m
;
159 if (account
!= null) {
165 private boolean canRelinkExistingAccount(final String number
) throws IOException
{
166 final SignalAccount signalAccount
;
168 signalAccount
= SignalAccount
.load(pathConfig
.dataPath(), number
, false, TrustNewIdentity
.ON_FIRST_USE
);
169 } catch (IOException e
) {
170 logger
.debug("Account in use or failed to load.", e
);
174 try (signalAccount
) {
175 if (signalAccount
.isMasterDevice()) {
176 logger
.debug("Account is a master device.");
180 final var m
= new ManagerImpl(signalAccount
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
182 m
.checkAccountState();
183 } catch (AuthorizationFailedException ignored
) {
187 logger
.debug("Account is still successfully linked.");