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
.whispersystems
.libsignal
.IdentityKeyPair
;
24 import org
.whispersystems
.libsignal
.InvalidKeyException
;
25 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
26 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
27 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
28 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
29 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
30 import org
.whispersystems
.signalservice
.api
.util
.SleepTimer
;
31 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
32 import org
.whispersystems
.signalservice
.internal
.configuration
.SignalServiceConfiguration
;
33 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
36 import java
.io
.IOException
;
37 import java
.util
.concurrent
.TimeoutException
;
39 public class ProvisioningManager
{
41 private final PathConfig pathConfig
;
42 private final SignalServiceConfiguration serviceConfiguration
;
43 private final String userAgent
;
45 private final SignalServiceAccountManager accountManager
;
46 private final IdentityKeyPair identityKey
;
47 private final int registrationId
;
48 private final String password
;
50 public ProvisioningManager(File settingsPath
, SignalServiceConfiguration serviceConfiguration
, String userAgent
) {
51 this.pathConfig
= PathConfig
.createDefault(settingsPath
);
52 this.serviceConfiguration
= serviceConfiguration
;
53 this.userAgent
= userAgent
;
55 identityKey
= KeyUtils
.generateIdentityKeyPair();
56 registrationId
= KeyHelper
.generateRegistrationId(false);
57 password
= KeyUtils
.createPassword();
58 final SleepTimer timer
= new UptimeSleepTimer();
59 GroupsV2Operations groupsV2Operations
;
61 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceConfiguration
));
62 } catch (Throwable ignored
) {
63 groupsV2Operations
= null;
65 accountManager
= new SignalServiceAccountManager(serviceConfiguration
,
66 new DynamicCredentialsProvider(null, null, password
, null, SignalServiceAddress
.DEFAULT_DEVICE_ID
),
72 public String
getDeviceLinkUri() throws TimeoutException
, IOException
{
73 String deviceUuid
= accountManager
.getNewDeviceUuid();
75 return new DeviceLinkInfo(deviceUuid
, identityKey
.getPublicKey().getPublicKey()).createDeviceLinkUri();
78 public String
finishDeviceLink(String deviceName
) throws IOException
, InvalidKeyException
, TimeoutException
, UserAlreadyExists
{
79 String signalingKey
= KeyUtils
.createSignalingKey();
80 SignalServiceAccountManager
.NewDeviceRegistrationReturn ret
= accountManager
.finishNewDeviceRegistration(
88 String username
= ret
.getNumber();
89 // TODO do this check before actually registering
90 if (SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
91 throw new UserAlreadyExists(username
, SignalAccount
.getFileName(pathConfig
.getDataPath(), username
));
94 // Create new account with the synced identity
95 byte[] profileKeyBytes
= ret
.getProfileKey();
96 ProfileKey profileKey
;
97 if (profileKeyBytes
== null) {
98 profileKey
= KeyUtils
.createProfileKey();
101 profileKey
= new ProfileKey(profileKeyBytes
);
102 } catch (InvalidInputException e
) {
103 throw new IOException("Received invalid profileKey", e
);
107 try (SignalAccount account
= SignalAccount
.createLinkedAccount(pathConfig
.getDataPath(),
118 try (Manager m
= new Manager(account
, pathConfig
, serviceConfiguration
, userAgent
)) {
122 m
.requestSyncGroups();
123 m
.requestSyncContacts();
124 m
.requestSyncBlocked();
125 m
.requestSyncConfiguration();