1 package org
.asamk
.signal
.manager
.helper
;
3 import org
.asamk
.signal
.manager
.DeviceLinkInfo
;
4 import org
.asamk
.signal
.manager
.SignalDependencies
;
5 import org
.asamk
.signal
.manager
.api
.CaptchaRequiredException
;
6 import org
.asamk
.signal
.manager
.api
.IncorrectPinException
;
7 import org
.asamk
.signal
.manager
.api
.InvalidDeviceLinkException
;
8 import org
.asamk
.signal
.manager
.api
.NonNormalizedPhoneNumberException
;
9 import org
.asamk
.signal
.manager
.api
.PinLockedException
;
10 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
11 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
12 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
13 import org
.asamk
.signal
.manager
.util
.NumberVerificationUtils
;
14 import org
.signal
.libsignal
.protocol
.InvalidKeyException
;
15 import org
.slf4j
.Logger
;
16 import org
.slf4j
.LoggerFactory
;
17 import org
.whispersystems
.signalservice
.api
.account
.ChangePhoneNumberRequest
;
18 import org
.whispersystems
.signalservice
.api
.push
.ACI
;
19 import org
.whispersystems
.signalservice
.api
.push
.PNI
;
20 import org
.whispersystems
.signalservice
.api
.push
.SignedPreKeyEntity
;
21 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.AuthorizationFailedException
;
22 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.DeprecatedVersionException
;
23 import org
.whispersystems
.signalservice
.api
.util
.DeviceNameUtil
;
24 import org
.whispersystems
.signalservice
.internal
.push
.OutgoingPushMessage
;
26 import java
.io
.IOException
;
27 import java
.util
.List
;
29 import java
.util
.Optional
;
30 import java
.util
.concurrent
.TimeUnit
;
32 public class AccountHelper
{
34 private final static Logger logger
= LoggerFactory
.getLogger(AccountHelper
.class);
36 private final Context context
;
37 private final SignalAccount account
;
38 private final SignalDependencies dependencies
;
40 private Callable unregisteredListener
;
42 public AccountHelper(final Context context
) {
43 this.account
= context
.getAccount();
44 this.dependencies
= context
.getDependencies();
45 this.context
= context
;
48 public void setUnregisteredListener(final Callable unregisteredListener
) {
49 this.unregisteredListener
= unregisteredListener
;
52 public void checkAccountState() throws IOException
{
53 if (account
.getLastReceiveTimestamp() == 0) {
54 logger
.info("The Signal protocol expects that incoming messages are regularly received.");
56 var diffInMilliseconds
= System
.currentTimeMillis() - account
.getLastReceiveTimestamp();
57 long days
= TimeUnit
.DAYS
.convert(diffInMilliseconds
, TimeUnit
.MILLISECONDS
);
60 "Messages have been last received {} days ago. The Signal protocol expects that incoming messages are regularly received.",
65 updateAccountAttributes();
66 context
.getPreKeyHelper().refreshPreKeysIfNecessary();
67 if (account
.getAci() == null || account
.getPni() == null) {
70 if (!account
.isPrimaryDevice() && account
.getPniIdentityKeyPair() == null) {
71 context
.getSyncHelper().requestSyncPniIdentity();
73 if (account
.getPreviousStorageVersion() < 4
74 && account
.isPrimaryDevice()
75 && account
.getRegistrationLockPin() != null) {
76 migrateRegistrationPin();
78 } catch (DeprecatedVersionException e
) {
79 logger
.debug("Signal-Server returned deprecated version exception", e
);
81 } catch (AuthorizationFailedException e
) {
82 account
.setRegistered(false);
87 public void checkWhoAmiI() throws IOException
{
88 final var whoAmI
= dependencies
.getAccountManager().getWhoAmI();
89 final var number
= whoAmI
.getNumber();
90 final var aci
= ACI
.parseOrNull(whoAmI
.getAci());
91 final var pni
= PNI
.parseOrNull(whoAmI
.getPni());
92 if (number
.equals(account
.getNumber()) && aci
.equals(account
.getAci()) && pni
.equals(account
.getPni())) {
96 updateSelfIdentifiers(number
, aci
, pni
);
99 private void updateSelfIdentifiers(final String number
, final ACI aci
, final PNI pni
) {
100 account
.setNumber(number
);
103 if (account
.isPrimaryDevice() && account
.getPniIdentityKeyPair() == null && account
.getPni() != null) {
104 account
.setPniIdentityKeyPair(KeyUtils
.generateIdentityKeyPair());
106 account
.getRecipientTrustedResolver().resolveSelfRecipientTrusted(account
.getSelfRecipientAddress());
107 // TODO check and update remote storage
108 context
.getUnidentifiedAccessHelper().rotateSenderCertificates();
109 dependencies
.resetAfterAddressChange();
110 dependencies
.getSignalWebSocket().forceNewWebSockets();
111 context
.getAccountFileUpdater().updateAccountIdentifiers(account
.getNumber(), account
.getAci());
114 public void startChangeNumber(
115 String newNumber
, String captcha
, boolean voiceVerification
116 ) throws IOException
, CaptchaRequiredException
, NonNormalizedPhoneNumberException
{
117 final var accountManager
= dependencies
.createUnauthenticatedAccountManager(newNumber
, account
.getPassword());
118 NumberVerificationUtils
.requestVerificationCode(accountManager
, captcha
, voiceVerification
);
121 public void finishChangeNumber(
122 String newNumber
, String verificationCode
, String pin
123 ) throws IncorrectPinException
, PinLockedException
, IOException
{
124 // TODO create new PNI identity key
125 final List
<OutgoingPushMessage
> deviceMessages
= null;
126 final Map
<String
, SignedPreKeyEntity
> devicePniSignedPreKeys
= null;
127 final Map
<String
, Integer
> pniRegistrationIds
= null;
128 final var result
= NumberVerificationUtils
.verifyNumber(verificationCode
,
130 context
.getPinHelper(),
131 (verificationCode1
, registrationLock
) -> dependencies
.getAccountManager()
132 .changeNumber(new ChangePhoneNumberRequest(newNumber
,
135 account
.getPniIdentityKeyPair().getPublicKey(),
137 devicePniSignedPreKeys
,
138 pniRegistrationIds
)));
139 // TODO handle response
140 updateSelfIdentifiers(newNumber
, account
.getAci(), PNI
.parseOrThrow(result
.first().getPni()));
143 public void setDeviceName(String deviceName
) {
144 final var privateKey
= account
.getAciIdentityKeyPair().getPrivateKey();
145 final var encryptedDeviceName
= DeviceNameUtil
.encryptDeviceName(deviceName
, privateKey
);
146 account
.setEncryptedDeviceName(encryptedDeviceName
);
149 public void updateAccountAttributes() throws IOException
{
150 dependencies
.getAccountManager()
151 .setAccountAttributes(null,
152 account
.getLocalRegistrationId(),
155 account
.getRegistrationLock(),
156 account
.getSelfUnidentifiedAccessKey(),
157 account
.isUnrestrictedUnidentifiedAccess(),
158 ServiceConfig
.capabilities
,
159 account
.isDiscoverableByPhoneNumber(),
160 account
.getEncryptedDeviceName(),
161 account
.getLocalPniRegistrationId());
164 public void addDevice(DeviceLinkInfo deviceLinkInfo
) throws IOException
, InvalidDeviceLinkException
{
165 var verificationCode
= dependencies
.getAccountManager().getNewDeviceVerificationCode();
168 dependencies
.getAccountManager()
169 .addDevice(deviceLinkInfo
.deviceIdentifier(),
170 deviceLinkInfo
.deviceKey(),
171 account
.getAciIdentityKeyPair(),
172 account
.getPniIdentityKeyPair(),
173 account
.getProfileKey(),
175 } catch (InvalidKeyException e
) {
176 throw new InvalidDeviceLinkException("Invalid device link", e
);
178 account
.setMultiDevice(true);
181 public void removeLinkedDevices(int deviceId
) throws IOException
{
182 dependencies
.getAccountManager().removeDevice(deviceId
);
183 var devices
= dependencies
.getAccountManager().getDevices();
184 account
.setMultiDevice(devices
.size() > 1);
187 public void migrateRegistrationPin() throws IOException
{
188 var masterKey
= account
.getOrCreatePinMasterKey();
190 context
.getPinHelper().migrateRegistrationLockPin(account
.getRegistrationLockPin(), masterKey
);
193 public void setRegistrationPin(String pin
) throws IOException
{
194 var masterKey
= account
.getOrCreatePinMasterKey();
196 context
.getPinHelper().setRegistrationLockPin(pin
, masterKey
);
198 account
.setRegistrationLockPin(pin
);
201 public void removeRegistrationPin() throws IOException
{
203 context
.getPinHelper().removeRegistrationLockPin();
205 account
.setRegistrationLockPin(null);
208 public void unregister() throws IOException
{
209 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
210 // If this is the primary device, other users can't send messages to this number anymore.
211 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
212 dependencies
.getAccountManager().setGcmId(Optional
.empty());
214 account
.setRegistered(false);
215 unregisteredListener
.call();
218 public void deleteAccount() throws IOException
{
220 context
.getPinHelper().removeRegistrationLockPin();
221 } catch (IOException e
) {
222 logger
.warn("Failed to remove registration lock pin");
224 account
.setRegistrationLockPin(null);
226 dependencies
.getAccountManager().deleteAccount();
228 account
.setRegistered(false);
229 unregisteredListener
.call();
232 public interface Callable
{