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
.util
.KeyUtils
;
24 import org
.slf4j
.Logger
;
25 import org
.slf4j
.LoggerFactory
;
26 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
27 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
28 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
29 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
30 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
31 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
32 import org
.whispersystems
.signalservice
.api
.util
.DeviceNameUtil
;
33 import org
.whispersystems
.signalservice
.api
.util
.SleepTimer
;
34 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
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 final SleepTimer timer
= new UptimeSleepTimer();
64 GroupsV2Operations groupsV2Operations
;
66 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration()));
67 } catch (Throwable ignored
) {
68 groupsV2Operations
= null;
70 accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
71 new DynamicCredentialsProvider(null, null, password
, SignalServiceAddress
.DEFAULT_DEVICE_ID
),
74 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
,
78 public static ProvisioningManager
init(
79 File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
81 var pathConfig
= PathConfig
.createDefault(settingsPath
);
83 final var serviceConfiguration
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
85 return new ProvisioningManager(pathConfig
, serviceConfiguration
, userAgent
);
88 public URI
getDeviceLinkUri() throws TimeoutException
, IOException
{
89 var deviceUuid
= accountManager
.getNewDeviceUuid();
91 return new DeviceLinkInfo(deviceUuid
, tempIdentityKey
.getPublicKey().getPublicKey()).createDeviceLinkUri();
94 public Manager
finishDeviceLink(String deviceName
) throws IOException
, TimeoutException
, UserAlreadyExists
{
95 var ret
= accountManager
.getNewDeviceRegistration(tempIdentityKey
);
96 var number
= ret
.getNumber();
98 if (SignalAccount
.userExists(pathConfig
.getDataPath(), number
)) {
99 throw new UserAlreadyExists(number
, SignalAccount
.getFileName(pathConfig
.getDataPath(), number
));
102 var encryptedDeviceName
= deviceName
== null
104 : DeviceNameUtil
.encryptDeviceName(deviceName
, ret
.getIdentity().getPrivateKey());
106 var deviceId
= accountManager
.finishNewDeviceRegistration(ret
.getProvisioningCode(),
110 encryptedDeviceName
);
112 // Create new account with the synced identity
113 var profileKey
= ret
.getProfileKey() == null ? KeyUtils
.createProfileKey() : ret
.getProfileKey();
115 SignalAccount account
= null;
117 account
= SignalAccount
.createLinkedAccount(pathConfig
.getDataPath(),
129 m
= new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
133 } catch (Exception e
) {
134 logger
.error("Failed to refresh prekeys.");
139 m
.requestAllSyncData();
140 } catch (Exception e
) {
141 logger
.error("Failed to request sync messages from linked device.");
145 final var result
= m
;
156 if (account
!= null) {