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
.storage
.SignalAccount
;
20 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
21 import org
.signal
.zkgroup
.InvalidInputException
;
22 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
23 import org
.slf4j
.Logger
;
24 import org
.slf4j
.LoggerFactory
;
25 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
26 import org
.whispersystems
.libsignal
.InvalidKeyException
;
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
.SleepTimer
;
33 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
34 import org
.whispersystems
.signalservice
.internal
.configuration
.SignalServiceConfiguration
;
35 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
38 import java
.io
.IOException
;
39 import java
.util
.concurrent
.TimeoutException
;
41 public class ProvisioningManager
{
43 private final static Logger logger
= LoggerFactory
.getLogger(ProvisioningManager
.class);
45 private final PathConfig pathConfig
;
46 private final SignalServiceConfiguration serviceConfiguration
;
47 private final String userAgent
;
49 private final SignalServiceAccountManager accountManager
;
50 private final IdentityKeyPair identityKey
;
51 private final int registrationId
;
52 private final String password
;
54 public ProvisioningManager(File settingsPath
, SignalServiceConfiguration serviceConfiguration
, String userAgent
) {
55 this.pathConfig
= PathConfig
.createDefault(settingsPath
);
56 this.serviceConfiguration
= serviceConfiguration
;
57 this.userAgent
= userAgent
;
59 identityKey
= KeyUtils
.generateIdentityKeyPair();
60 registrationId
= KeyHelper
.generateRegistrationId(false);
61 password
= KeyUtils
.createPassword();
62 final SleepTimer timer
= new UptimeSleepTimer();
63 GroupsV2Operations groupsV2Operations
;
65 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceConfiguration
));
66 } catch (Throwable ignored
) {
67 groupsV2Operations
= null;
69 accountManager
= new SignalServiceAccountManager(serviceConfiguration
,
70 new DynamicCredentialsProvider(null, null, password
, null, SignalServiceAddress
.DEFAULT_DEVICE_ID
),
73 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
,
77 public String
getDeviceLinkUri() throws TimeoutException
, IOException
{
78 String deviceUuid
= accountManager
.getNewDeviceUuid();
80 return new DeviceLinkInfo(deviceUuid
, identityKey
.getPublicKey().getPublicKey()).createDeviceLinkUri();
83 public String
finishDeviceLink(String deviceName
) throws IOException
, InvalidKeyException
, TimeoutException
, UserAlreadyExists
{
84 String signalingKey
= KeyUtils
.createSignalingKey();
85 SignalServiceAccountManager
.NewDeviceRegistrationReturn ret
= accountManager
.finishNewDeviceRegistration(
93 String username
= ret
.getNumber();
94 // TODO do this check before actually registering
95 if (SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
96 throw new UserAlreadyExists(username
, SignalAccount
.getFileName(pathConfig
.getDataPath(), username
));
99 // Create new account with the synced identity
100 byte[] profileKeyBytes
= ret
.getProfileKey();
101 ProfileKey profileKey
;
102 if (profileKeyBytes
== null) {
103 profileKey
= KeyUtils
.createProfileKey();
106 profileKey
= new ProfileKey(profileKeyBytes
);
107 } catch (InvalidInputException e
) {
108 throw new IOException("Received invalid profileKey", e
);
112 try (SignalAccount account
= SignalAccount
.createLinkedAccount(pathConfig
.getDataPath(),
123 try (Manager m
= new Manager(account
, pathConfig
, serviceConfiguration
, userAgent
)) {
127 } catch (Exception e
) {
128 logger
.error("Failed to refresh prekeys.");
133 m
.requestSyncGroups();
134 m
.requestSyncContacts();
135 m
.requestSyncBlocked();
136 m
.requestSyncConfiguration();
138 } catch (Exception e
) {
139 logger
.error("Failed to request sync messages from linked device.");