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
.helper
.PinHelper
;
20 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
21 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
22 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
23 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
24 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
25 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
26 import org
.whispersystems
.signalservice
.api
.KbsPinData
;
27 import org
.whispersystems
.signalservice
.api
.KeyBackupService
;
28 import org
.whispersystems
.signalservice
.api
.KeyBackupServicePinException
;
29 import org
.whispersystems
.signalservice
.api
.KeyBackupSystemNoDataException
;
30 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
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
.api
.util
.UuidUtil
;
35 import org
.whispersystems
.signalservice
.internal
.configuration
.SignalServiceConfiguration
;
36 import org
.whispersystems
.signalservice
.internal
.push
.LockedException
;
37 import org
.whispersystems
.signalservice
.internal
.push
.VerifyAccountResponse
;
38 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
41 import java
.io
.IOException
;
42 import java
.util
.Locale
;
44 public class RegistrationManager
implements AutoCloseable
{
46 private SignalAccount account
;
47 private final PathConfig pathConfig
;
48 private final SignalServiceConfiguration serviceConfiguration
;
49 private final String userAgent
;
51 private final SignalServiceAccountManager accountManager
;
52 private final PinHelper pinHelper
;
54 public RegistrationManager(
55 SignalAccount account
,
56 PathConfig pathConfig
,
57 SignalServiceConfiguration serviceConfiguration
,
60 this.account
= account
;
61 this.pathConfig
= pathConfig
;
62 this.serviceConfiguration
= serviceConfiguration
;
63 this.userAgent
= userAgent
;
65 final SleepTimer timer
= new UptimeSleepTimer();
66 this.accountManager
= new SignalServiceAccountManager(serviceConfiguration
, new DynamicCredentialsProvider(
67 // Using empty UUID, because registering doesn't work otherwise
69 account
.getUsername(),
70 account
.getPassword(),
71 account
.getSignalingKey(),
72 SignalServiceAddress
.DEFAULT_DEVICE_ID
), userAgent
, null, timer
);
73 final KeyBackupService keyBackupService
= ServiceConfig
.createKeyBackupService(accountManager
);
74 this.pinHelper
= new PinHelper(keyBackupService
);
77 public static RegistrationManager
init(
78 String username
, File settingsPath
, SignalServiceConfiguration serviceConfiguration
, String userAgent
79 ) throws IOException
{
80 PathConfig pathConfig
= PathConfig
.createDefault(settingsPath
);
82 if (!SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
83 IdentityKeyPair identityKey
= KeyUtils
.generateIdentityKeyPair();
84 int registrationId
= KeyHelper
.generateRegistrationId(false);
86 ProfileKey profileKey
= KeyUtils
.createProfileKey();
87 SignalAccount account
= SignalAccount
.create(pathConfig
.getDataPath(),
94 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
97 SignalAccount account
= SignalAccount
.load(pathConfig
.getDataPath(), username
);
99 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
102 public void register(boolean voiceVerification
, String captcha
) throws IOException
{
103 if (account
.getPassword() == null) {
104 account
.setPassword(KeyUtils
.createPassword());
107 if (voiceVerification
) {
108 accountManager
.requestVoiceVerificationCode(Locale
.getDefault(),
109 Optional
.fromNullable(captcha
),
112 accountManager
.requestSmsVerificationCode(false, Optional
.fromNullable(captcha
), Optional
.absent());
115 account
.setRegistered(false);
119 public void verifyAccount(
120 String verificationCode
, String pin
121 ) throws IOException
, KeyBackupSystemNoDataException
, KeyBackupServicePinException
{
122 verificationCode
= verificationCode
.replace("-", "");
123 if (account
.getSignalingKey() == null) {
124 account
.setSignalingKey(KeyUtils
.createSignalingKey());
126 VerifyAccountResponse response
;
128 response
= verifyAccountWithCode(verificationCode
, pin
, null);
129 account
.setPinMasterKey(null);
130 } catch (LockedException e
) {
135 KbsPinData registrationLockData
= pinHelper
.getRegistrationLockData(pin
, e
);
136 if (registrationLockData
== null) {
140 String registrationLock
= registrationLockData
.getMasterKey().deriveRegistrationLock();
142 response
= verifyAccountWithCode(verificationCode
, null, registrationLock
);
143 } catch (LockedException _e
) {
144 throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
146 account
.setPinMasterKey(registrationLockData
.getMasterKey());
149 // TODO response.isStorageCapable()
150 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
152 account
.setDeviceId(SignalServiceAddress
.DEFAULT_DEVICE_ID
);
153 account
.setMultiDevice(false);
154 account
.setRegistered(true);
155 account
.setUuid(UuidUtil
.parseOrNull(response
.getUuid()));
156 account
.setRegistrationLockPin(pin
);
157 account
.getSignalProtocolStore()
158 .saveIdentity(account
.getSelfAddress(),
159 account
.getSignalProtocolStore().getIdentityKeyPair().getPublicKey(),
160 TrustLevel
.TRUSTED_VERIFIED
);
162 try (Manager m
= new Manager(account
, pathConfig
, serviceConfiguration
, userAgent
)) {
172 private VerifyAccountResponse
verifyAccountWithCode(
173 final String verificationCode
, final String legacyPin
, final String registrationLock
174 ) throws IOException
{
175 return accountManager
.verifyAccountWithCode(verificationCode
,
176 account
.getSignalingKey(),
177 account
.getSignalProtocolStore().getLocalRegistrationId(),
181 account
.getSelfUnidentifiedAccessKey(),
182 account
.isUnrestrictedUnidentifiedAccess(),
183 ServiceConfig
.capabilities
,
184 account
.isDiscoverableByPhoneNumber());
188 public void close() throws Exception
{
189 if (account
!= null) {