mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-30 11:00:38 +00:00
Refactor register and verify
This commit is contained in:
parent
6c8a1ff3d3
commit
e74be0c345
33 changed files with 405 additions and 362 deletions
|
@ -79,14 +79,10 @@ import org.whispersystems.libsignal.ecc.ECKeyPair;
|
|||
import org.whispersystems.libsignal.ecc.ECPublicKey;
|
||||
import org.whispersystems.libsignal.state.PreKeyRecord;
|
||||
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
|
||||
import org.whispersystems.libsignal.util.KeyHelper;
|
||||
import org.whispersystems.libsignal.util.Medium;
|
||||
import org.whispersystems.libsignal.util.Pair;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
import org.whispersystems.signalservice.api.KbsPinData;
|
||||
import org.whispersystems.signalservice.api.KeyBackupService;
|
||||
import org.whispersystems.signalservice.api.KeyBackupServicePinException;
|
||||
import org.whispersystems.signalservice.api.KeyBackupSystemNoDataException;
|
||||
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
||||
import org.whispersystems.signalservice.api.SignalServiceMessagePipe;
|
||||
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
|
||||
|
@ -144,10 +140,8 @@ import org.whispersystems.signalservice.internal.configuration.SignalServiceConf
|
|||
import org.whispersystems.signalservice.internal.contacts.crypto.Quote;
|
||||
import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedQuoteException;
|
||||
import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException;
|
||||
import org.whispersystems.signalservice.internal.push.LockedException;
|
||||
import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
|
||||
import org.whispersystems.signalservice.internal.push.UnsupportedDataMessageException;
|
||||
import org.whispersystems.signalservice.internal.push.VerifyAccountResponse;
|
||||
import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider;
|
||||
import org.whispersystems.signalservice.internal.util.Hex;
|
||||
import org.whispersystems.util.Base64;
|
||||
|
@ -167,7 +161,6 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.security.KeyStore;
|
||||
import java.security.SignatureException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
@ -176,7 +169,6 @@ import java.util.Date;
|
|||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
@ -202,24 +194,21 @@ public class Manager implements Closeable {
|
|||
private final SignalServiceConfiguration serviceConfiguration;
|
||||
private final String userAgent;
|
||||
|
||||
// TODO make configurable
|
||||
private final boolean discoverableByPhoneNumber = true;
|
||||
private final boolean unrestrictedUnidentifiedAccess = false;
|
||||
|
||||
private final SignalAccount account;
|
||||
private SignalAccount account;
|
||||
private final PathConfig pathConfig;
|
||||
private SignalServiceAccountManager accountManager;
|
||||
private GroupsV2Api groupsV2Api;
|
||||
private final SignalServiceAccountManager accountManager;
|
||||
private final GroupsV2Api groupsV2Api;
|
||||
private final GroupsV2Operations groupsV2Operations;
|
||||
private final SignalServiceMessageReceiver messageReceiver;
|
||||
private final ClientZkProfileOperations clientZkProfileOperations;
|
||||
|
||||
private SignalServiceMessageReceiver messageReceiver = null;
|
||||
private SignalServiceMessagePipe messagePipe = null;
|
||||
private SignalServiceMessagePipe unidentifiedMessagePipe = null;
|
||||
|
||||
private final UnidentifiedAccessHelper unidentifiedAccessHelper;
|
||||
private final ProfileHelper profileHelper;
|
||||
private final GroupHelper groupHelper;
|
||||
private PinHelper pinHelper;
|
||||
private final PinHelper pinHelper;
|
||||
|
||||
Manager(
|
||||
SignalAccount account,
|
||||
|
@ -233,7 +222,30 @@ public class Manager implements Closeable {
|
|||
this.userAgent = userAgent;
|
||||
this.groupsV2Operations = capabilities.isGv2() ? new GroupsV2Operations(ClientZkOperations.create(
|
||||
serviceConfiguration)) : null;
|
||||
createSignalServiceAccountManager();
|
||||
this.accountManager = new SignalServiceAccountManager(serviceConfiguration,
|
||||
new DynamicCredentialsProvider(account.getUuid(),
|
||||
account.getUsername(),
|
||||
account.getPassword(),
|
||||
account.getSignalingKey(),
|
||||
account.getDeviceId()),
|
||||
userAgent,
|
||||
groupsV2Operations,
|
||||
timer);
|
||||
this.groupsV2Api = accountManager.getGroupsV2Api();
|
||||
final KeyBackupService keyBackupService = ServiceConfig.createKeyBackupService(accountManager);
|
||||
this.pinHelper = new PinHelper(keyBackupService);
|
||||
this.clientZkProfileOperations = capabilities.isGv2() ? ClientZkOperations.create(serviceConfiguration)
|
||||
.getProfileOperations() : null;
|
||||
this.messageReceiver = new SignalServiceMessageReceiver(serviceConfiguration,
|
||||
account.getUuid(),
|
||||
account.getUsername(),
|
||||
account.getPassword(),
|
||||
account.getDeviceId(),
|
||||
account.getSignalingKey(),
|
||||
userAgent,
|
||||
null,
|
||||
timer,
|
||||
clientZkProfileOperations);
|
||||
|
||||
this.account.setResolver(this::resolveSignalServiceAddress);
|
||||
|
||||
|
@ -244,7 +256,7 @@ public class Manager implements Closeable {
|
|||
this.profileHelper = new ProfileHelper(account.getProfileStore()::getProfileKey,
|
||||
unidentifiedAccessHelper::getAccessFor,
|
||||
unidentified -> unidentified ? getOrCreateUnidentifiedMessagePipe() : getOrCreateMessagePipe(),
|
||||
this::getOrCreateMessageReceiver);
|
||||
() -> messageReceiver);
|
||||
this.groupHelper = new GroupHelper(this::getRecipientProfileKeyCredential,
|
||||
this::getRecipientProfile,
|
||||
account::getSelfAddress,
|
||||
|
@ -261,30 +273,6 @@ public class Manager implements Closeable {
|
|||
return account.getSelfAddress();
|
||||
}
|
||||
|
||||
private void createSignalServiceAccountManager() {
|
||||
this.accountManager = new SignalServiceAccountManager(serviceConfiguration,
|
||||
new DynamicCredentialsProvider(account.getUuid(),
|
||||
account.getUsername(),
|
||||
account.getPassword(),
|
||||
null,
|
||||
account.getDeviceId()),
|
||||
userAgent,
|
||||
groupsV2Operations,
|
||||
timer);
|
||||
this.groupsV2Api = accountManager.getGroupsV2Api();
|
||||
this.pinHelper = new PinHelper(createKeyBackupService());
|
||||
}
|
||||
|
||||
private KeyBackupService createKeyBackupService() {
|
||||
KeyStore keyStore = ServiceConfig.getIasKeyStore();
|
||||
|
||||
return accountManager.getKeyBackupService(keyStore,
|
||||
ServiceConfig.KEY_BACKUP_ENCLAVE_NAME,
|
||||
ServiceConfig.KEY_BACKUP_SERVICE_ID,
|
||||
ServiceConfig.KEY_BACKUP_MRENCLAVE,
|
||||
10);
|
||||
}
|
||||
|
||||
private IdentityKeyPair getIdentityKeyPair() {
|
||||
return account.getSignalProtocolStore().getIdentityKeyPair();
|
||||
}
|
||||
|
@ -313,56 +301,20 @@ public class Manager implements Closeable {
|
|||
|
||||
public static Manager init(
|
||||
String username, File settingsPath, SignalServiceConfiguration serviceConfiguration, String userAgent
|
||||
) throws IOException {
|
||||
) throws IOException, NotRegisteredException {
|
||||
PathConfig pathConfig = PathConfig.createDefault(settingsPath);
|
||||
|
||||
if (!SignalAccount.userExists(pathConfig.getDataPath(), username)) {
|
||||
IdentityKeyPair identityKey = KeyUtils.generateIdentityKeyPair();
|
||||
int registrationId = KeyHelper.generateRegistrationId(false);
|
||||
|
||||
ProfileKey profileKey = KeyUtils.createProfileKey();
|
||||
SignalAccount account = SignalAccount.create(pathConfig.getDataPath(),
|
||||
username,
|
||||
identityKey,
|
||||
registrationId,
|
||||
profileKey);
|
||||
account.save();
|
||||
|
||||
return new Manager(account, pathConfig, serviceConfiguration, userAgent);
|
||||
throw new NotRegisteredException();
|
||||
}
|
||||
|
||||
SignalAccount account = SignalAccount.load(pathConfig.getDataPath(), username);
|
||||
|
||||
Manager m = new Manager(account, pathConfig, serviceConfiguration, userAgent);
|
||||
|
||||
m.migrateLegacyConfigs();
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
private void migrateLegacyConfigs() {
|
||||
if (account.getProfileKey() == null && isRegistered()) {
|
||||
// Old config file, creating new profile key
|
||||
account.setProfileKey(KeyUtils.createProfileKey());
|
||||
account.save();
|
||||
if (!account.isRegistered()) {
|
||||
throw new NotRegisteredException();
|
||||
}
|
||||
// Store profile keys only in profile store
|
||||
for (ContactInfo contact : account.getContactStore().getContacts()) {
|
||||
String profileKeyString = contact.profileKey;
|
||||
if (profileKeyString == null) {
|
||||
continue;
|
||||
}
|
||||
final ProfileKey profileKey;
|
||||
try {
|
||||
profileKey = new ProfileKey(Base64.decode(profileKeyString));
|
||||
} catch (InvalidInputException | IOException e) {
|
||||
continue;
|
||||
}
|
||||
contact.profileKey = null;
|
||||
account.getProfileStore().storeProfileKey(contact.getAddress(), profileKey);
|
||||
}
|
||||
// Ensure our profile key is stored in profile store
|
||||
account.getProfileStore().storeProfileKey(getSelfAddress(), account.getProfileKey());
|
||||
|
||||
return new Manager(account, pathConfig, serviceConfiguration, userAgent);
|
||||
}
|
||||
|
||||
public void checkAccountState() throws IOException {
|
||||
|
@ -401,25 +353,6 @@ public class Manager implements Closeable {
|
|||
return numbers.stream().collect(Collectors.toMap(x -> x, registeredUsers::contains));
|
||||
}
|
||||
|
||||
public void register(boolean voiceVerification, String captcha) throws IOException {
|
||||
account.setPassword(KeyUtils.createPassword());
|
||||
|
||||
// Resetting UUID, because registering doesn't work otherwise
|
||||
account.setUuid(null);
|
||||
createSignalServiceAccountManager();
|
||||
|
||||
if (voiceVerification) {
|
||||
accountManager.requestVoiceVerificationCode(Locale.getDefault(),
|
||||
Optional.fromNullable(captcha),
|
||||
Optional.absent());
|
||||
} else {
|
||||
accountManager.requestSmsVerificationCode(false, Optional.fromNullable(captcha), Optional.absent());
|
||||
}
|
||||
|
||||
account.setRegistered(false);
|
||||
account.save();
|
||||
}
|
||||
|
||||
public void updateAccountAttributes() throws IOException {
|
||||
accountManager.setAccountAttributes(account.getSignalingKey(),
|
||||
account.getSignalProtocolStore().getLocalRegistrationId(),
|
||||
|
@ -427,10 +360,10 @@ public class Manager implements Closeable {
|
|||
// set legacy pin only if no KBS master key is set
|
||||
account.getPinMasterKey() == null ? account.getRegistrationLockPin() : null,
|
||||
account.getPinMasterKey() == null ? null : account.getPinMasterKey().deriveRegistrationLock(),
|
||||
unidentifiedAccessHelper.getSelfUnidentifiedAccessKey(),
|
||||
unrestrictedUnidentifiedAccess,
|
||||
account.getSelfUnidentifiedAccessKey(),
|
||||
account.isUnrestrictedUnidentifiedAccess(),
|
||||
capabilities,
|
||||
discoverableByPhoneNumber);
|
||||
account.isDiscoverableByPhoneNumber());
|
||||
}
|
||||
|
||||
public void setProfile(String name, File avatar) throws IOException {
|
||||
|
@ -519,63 +452,6 @@ public class Manager implements Closeable {
|
|||
}
|
||||
}
|
||||
|
||||
public void verifyAccount(
|
||||
String verificationCode, String pin
|
||||
) throws IOException, KeyBackupSystemNoDataException, KeyBackupServicePinException {
|
||||
verificationCode = verificationCode.replace("-", "");
|
||||
account.setSignalingKey(KeyUtils.createSignalingKey());
|
||||
VerifyAccountResponse response;
|
||||
try {
|
||||
response = verifyAccountWithCode(verificationCode, pin, null);
|
||||
} catch (LockedException e) {
|
||||
if (pin == null) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
KbsPinData registrationLockData = pinHelper.getRegistrationLockData(pin, e);
|
||||
if (registrationLockData == null) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
String registrationLock = registrationLockData.getMasterKey().deriveRegistrationLock();
|
||||
try {
|
||||
response = verifyAccountWithCode(verificationCode, null, registrationLock);
|
||||
} catch (LockedException _e) {
|
||||
throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
|
||||
}
|
||||
account.setPinMasterKey(registrationLockData.getMasterKey());
|
||||
}
|
||||
|
||||
// TODO response.isStorageCapable()
|
||||
//accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
|
||||
|
||||
account.setRegistered(true);
|
||||
account.setUuid(UuidUtil.parseOrNull(response.getUuid()));
|
||||
account.setRegistrationLockPin(pin);
|
||||
account.getSignalProtocolStore()
|
||||
.saveIdentity(account.getSelfAddress(),
|
||||
getIdentityKeyPair().getPublicKey(),
|
||||
TrustLevel.TRUSTED_VERIFIED);
|
||||
|
||||
refreshPreKeys();
|
||||
account.save();
|
||||
}
|
||||
|
||||
private VerifyAccountResponse verifyAccountWithCode(
|
||||
final String verificationCode, final String legacyPin, final String registrationLock
|
||||
) throws IOException {
|
||||
return accountManager.verifyAccountWithCode(verificationCode,
|
||||
account.getSignalingKey(),
|
||||
account.getSignalProtocolStore().getLocalRegistrationId(),
|
||||
true,
|
||||
legacyPin,
|
||||
registrationLock,
|
||||
unidentifiedAccessHelper.getSelfUnidentifiedAccessKey(),
|
||||
unrestrictedUnidentifiedAccess,
|
||||
capabilities,
|
||||
discoverableByPhoneNumber);
|
||||
}
|
||||
|
||||
public void setRegistrationLockPin(Optional<String> pin) throws IOException, UnauthenticatedResponseException {
|
||||
if (pin.isPresent()) {
|
||||
final MasterKey masterKey = account.getPinMasterKey() != null
|
||||
|
@ -607,45 +483,21 @@ public class Manager implements Closeable {
|
|||
accountManager.setPreKeys(identityKeyPair.getPublicKey(), signedPreKeyRecord, oneTimePreKeys);
|
||||
}
|
||||
|
||||
private SignalServiceMessageReceiver createMessageReceiver() {
|
||||
final ClientZkProfileOperations clientZkProfileOperations = capabilities.isGv2() ? ClientZkOperations.create(
|
||||
serviceConfiguration).getProfileOperations() : null;
|
||||
return new SignalServiceMessageReceiver(serviceConfiguration,
|
||||
account.getUuid(),
|
||||
account.getUsername(),
|
||||
account.getPassword(),
|
||||
account.getDeviceId(),
|
||||
account.getSignalingKey(),
|
||||
userAgent,
|
||||
null,
|
||||
timer,
|
||||
clientZkProfileOperations);
|
||||
}
|
||||
|
||||
private SignalServiceMessageReceiver getOrCreateMessageReceiver() {
|
||||
if (messageReceiver == null) {
|
||||
messageReceiver = createMessageReceiver();
|
||||
}
|
||||
return messageReceiver;
|
||||
}
|
||||
|
||||
private SignalServiceMessagePipe getOrCreateMessagePipe() {
|
||||
if (messagePipe == null) {
|
||||
messagePipe = getOrCreateMessageReceiver().createMessagePipe();
|
||||
messagePipe = messageReceiver.createMessagePipe();
|
||||
}
|
||||
return messagePipe;
|
||||
}
|
||||
|
||||
private SignalServiceMessagePipe getOrCreateUnidentifiedMessagePipe() {
|
||||
if (unidentifiedMessagePipe == null) {
|
||||
unidentifiedMessagePipe = getOrCreateMessageReceiver().createUnidentifiedMessagePipe();
|
||||
unidentifiedMessagePipe = messageReceiver.createUnidentifiedMessagePipe();
|
||||
}
|
||||
return unidentifiedMessagePipe;
|
||||
}
|
||||
|
||||
private SignalServiceMessageSender createMessageSender() {
|
||||
final ClientZkProfileOperations clientZkProfileOperations = capabilities.isGv2() ? ClientZkOperations.create(
|
||||
serviceConfiguration).getProfileOperations() : null;
|
||||
final ExecutorService executor = null;
|
||||
return new SignalServiceMessageSender(serviceConfiguration,
|
||||
account.getUuid(),
|
||||
|
@ -2349,13 +2201,12 @@ public class Manager implements Closeable {
|
|||
GroupId groupId, GroupSecretParams groupSecretParams, String cdnKey
|
||||
) throws IOException {
|
||||
IOUtils.createPrivateDirectories(pathConfig.getAvatarsPath());
|
||||
SignalServiceMessageReceiver receiver = getOrCreateMessageReceiver();
|
||||
File outputFile = getGroupAvatarFile(groupId);
|
||||
GroupsV2Operations.GroupOperations groupOperations = groupsV2Operations.forGroup(groupSecretParams);
|
||||
|
||||
File tmpFile = IOUtils.createTempFile();
|
||||
tmpFile.deleteOnExit();
|
||||
try (InputStream input = receiver.retrieveGroupsV2ProfileAvatar(cdnKey,
|
||||
try (InputStream input = messageReceiver.retrieveGroupsV2ProfileAvatar(cdnKey,
|
||||
tmpFile,
|
||||
ServiceConfig.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE)) {
|
||||
byte[] encryptedData = IOUtils.readFully(input);
|
||||
|
@ -2384,11 +2235,10 @@ public class Manager implements Closeable {
|
|||
SignalServiceAddress address, String avatarPath, ProfileKey profileKey
|
||||
) throws IOException {
|
||||
IOUtils.createPrivateDirectories(pathConfig.getAvatarsPath());
|
||||
SignalServiceMessageReceiver receiver = getOrCreateMessageReceiver();
|
||||
File outputFile = getProfileAvatarFile(address);
|
||||
|
||||
File tmpFile = IOUtils.createTempFile();
|
||||
try (InputStream input = receiver.retrieveProfileAvatar(avatarPath,
|
||||
try (InputStream input = messageReceiver.retrieveProfileAvatar(avatarPath,
|
||||
tmpFile,
|
||||
profileKey,
|
||||
ServiceConfig.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE)) {
|
||||
|
@ -2429,8 +2279,6 @@ public class Manager implements Closeable {
|
|||
}
|
||||
}
|
||||
|
||||
final SignalServiceMessageReceiver messageReceiver = getOrCreateMessageReceiver();
|
||||
|
||||
File tmpFile = IOUtils.createTempFile();
|
||||
try (InputStream input = messageReceiver.retrieveAttachment(pointer,
|
||||
tmpFile,
|
||||
|
@ -2451,7 +2299,6 @@ public class Manager implements Closeable {
|
|||
private InputStream retrieveAttachmentAsStream(
|
||||
SignalServiceAttachmentPointer pointer, File tmpFile
|
||||
) throws IOException, InvalidMessageException, MissingConfigurationException {
|
||||
final SignalServiceMessageReceiver messageReceiver = getOrCreateMessageReceiver();
|
||||
return messageReceiver.retrieveAttachment(pointer, tmpFile, ServiceConfig.MAX_ATTACHMENT_SIZE);
|
||||
}
|
||||
|
||||
|
@ -2737,6 +2584,10 @@ public class Manager implements Closeable {
|
|||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
close(true);
|
||||
}
|
||||
|
||||
void close(boolean closeAccount) throws IOException {
|
||||
if (messagePipe != null) {
|
||||
messagePipe.shutdown();
|
||||
messagePipe = null;
|
||||
|
@ -2747,7 +2598,10 @@ public class Manager implements Closeable {
|
|||
unidentifiedMessagePipe = null;
|
||||
}
|
||||
|
||||
account.close();
|
||||
if (closeAccount && account != null) {
|
||||
account.close();
|
||||
}
|
||||
account = null;
|
||||
}
|
||||
|
||||
public interface ReceiveMessageHandler {
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package org.asamk.signal.manager;
|
||||
|
||||
public class NotRegisteredException extends Exception {
|
||||
|
||||
public NotRegisteredException() {
|
||||
super("User is not registered.");
|
||||
}
|
||||
}
|
|
@ -124,8 +124,10 @@ public class ProvisioningManager {
|
|||
m.requestSyncBlocked();
|
||||
m.requestSyncConfiguration();
|
||||
|
||||
m.saveAccount();
|
||||
m.close(false);
|
||||
}
|
||||
|
||||
account.save();
|
||||
}
|
||||
|
||||
return username;
|
||||
|
|
194
src/main/java/org/asamk/signal/manager/RegistrationManager.java
Normal file
194
src/main/java/org/asamk/signal/manager/RegistrationManager.java
Normal file
|
@ -0,0 +1,194 @@
|
|||
/*
|
||||
Copyright (C) 2015-2021 AsamK and contributors
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.asamk.signal.manager;
|
||||
|
||||
import org.asamk.signal.manager.helper.PinHelper;
|
||||
import org.asamk.signal.manager.storage.SignalAccount;
|
||||
import org.asamk.signal.manager.util.KeyUtils;
|
||||
import org.signal.zkgroup.profiles.ProfileKey;
|
||||
import org.whispersystems.libsignal.IdentityKeyPair;
|
||||
import org.whispersystems.libsignal.util.KeyHelper;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
import org.whispersystems.signalservice.api.KbsPinData;
|
||||
import org.whispersystems.signalservice.api.KeyBackupService;
|
||||
import org.whispersystems.signalservice.api.KeyBackupServicePinException;
|
||||
import org.whispersystems.signalservice.api.KeyBackupSystemNoDataException;
|
||||
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
import org.whispersystems.signalservice.api.util.SleepTimer;
|
||||
import org.whispersystems.signalservice.api.util.UptimeSleepTimer;
|
||||
import org.whispersystems.signalservice.api.util.UuidUtil;
|
||||
import org.whispersystems.signalservice.internal.configuration.SignalServiceConfiguration;
|
||||
import org.whispersystems.signalservice.internal.push.LockedException;
|
||||
import org.whispersystems.signalservice.internal.push.VerifyAccountResponse;
|
||||
import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
|
||||
public class RegistrationManager implements AutoCloseable {
|
||||
|
||||
private SignalAccount account;
|
||||
private final PathConfig pathConfig;
|
||||
private final SignalServiceConfiguration serviceConfiguration;
|
||||
private final String userAgent;
|
||||
|
||||
private final SignalServiceAccountManager accountManager;
|
||||
private final PinHelper pinHelper;
|
||||
|
||||
public RegistrationManager(
|
||||
SignalAccount account,
|
||||
PathConfig pathConfig,
|
||||
SignalServiceConfiguration serviceConfiguration,
|
||||
String userAgent
|
||||
) {
|
||||
this.account = account;
|
||||
this.pathConfig = pathConfig;
|
||||
this.serviceConfiguration = serviceConfiguration;
|
||||
this.userAgent = userAgent;
|
||||
|
||||
final SleepTimer timer = new UptimeSleepTimer();
|
||||
this.accountManager = new SignalServiceAccountManager(serviceConfiguration, new DynamicCredentialsProvider(
|
||||
// Using empty UUID, because registering doesn't work otherwise
|
||||
null,
|
||||
account.getUsername(),
|
||||
account.getPassword(),
|
||||
account.getSignalingKey(),
|
||||
SignalServiceAddress.DEFAULT_DEVICE_ID), userAgent, null, timer);
|
||||
final KeyBackupService keyBackupService = ServiceConfig.createKeyBackupService(accountManager);
|
||||
this.pinHelper = new PinHelper(keyBackupService);
|
||||
}
|
||||
|
||||
public static RegistrationManager init(
|
||||
String username, File settingsPath, SignalServiceConfiguration serviceConfiguration, String userAgent
|
||||
) throws IOException {
|
||||
PathConfig pathConfig = PathConfig.createDefault(settingsPath);
|
||||
|
||||
if (!SignalAccount.userExists(pathConfig.getDataPath(), username)) {
|
||||
IdentityKeyPair identityKey = KeyUtils.generateIdentityKeyPair();
|
||||
int registrationId = KeyHelper.generateRegistrationId(false);
|
||||
|
||||
ProfileKey profileKey = KeyUtils.createProfileKey();
|
||||
SignalAccount account = SignalAccount.create(pathConfig.getDataPath(),
|
||||
username,
|
||||
identityKey,
|
||||
registrationId,
|
||||
profileKey);
|
||||
account.save();
|
||||
|
||||
return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent);
|
||||
}
|
||||
|
||||
SignalAccount account = SignalAccount.load(pathConfig.getDataPath(), username);
|
||||
|
||||
return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent);
|
||||
}
|
||||
|
||||
public void register(boolean voiceVerification, String captcha) throws IOException {
|
||||
if (account.getPassword() == null) {
|
||||
account.setPassword(KeyUtils.createPassword());
|
||||
}
|
||||
|
||||
if (voiceVerification) {
|
||||
accountManager.requestVoiceVerificationCode(Locale.getDefault(),
|
||||
Optional.fromNullable(captcha),
|
||||
Optional.absent());
|
||||
} else {
|
||||
accountManager.requestSmsVerificationCode(false, Optional.fromNullable(captcha), Optional.absent());
|
||||
}
|
||||
|
||||
account.setRegistered(false);
|
||||
account.save();
|
||||
}
|
||||
|
||||
public void verifyAccount(
|
||||
String verificationCode, String pin
|
||||
) throws IOException, KeyBackupSystemNoDataException, KeyBackupServicePinException {
|
||||
verificationCode = verificationCode.replace("-", "");
|
||||
if (account.getSignalingKey() == null) {
|
||||
account.setSignalingKey(KeyUtils.createSignalingKey());
|
||||
}
|
||||
VerifyAccountResponse response;
|
||||
try {
|
||||
response = verifyAccountWithCode(verificationCode, pin, null);
|
||||
account.setPinMasterKey(null);
|
||||
} catch (LockedException e) {
|
||||
if (pin == null) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
KbsPinData registrationLockData = pinHelper.getRegistrationLockData(pin, e);
|
||||
if (registrationLockData == null) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
String registrationLock = registrationLockData.getMasterKey().deriveRegistrationLock();
|
||||
try {
|
||||
response = verifyAccountWithCode(verificationCode, null, registrationLock);
|
||||
} catch (LockedException _e) {
|
||||
throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
|
||||
}
|
||||
account.setPinMasterKey(registrationLockData.getMasterKey());
|
||||
}
|
||||
|
||||
// TODO response.isStorageCapable()
|
||||
//accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
|
||||
|
||||
account.setDeviceId(SignalServiceAddress.DEFAULT_DEVICE_ID);
|
||||
account.setMultiDevice(false);
|
||||
account.setRegistered(true);
|
||||
account.setUuid(UuidUtil.parseOrNull(response.getUuid()));
|
||||
account.setRegistrationLockPin(pin);
|
||||
account.getSignalProtocolStore()
|
||||
.saveIdentity(account.getSelfAddress(),
|
||||
account.getSignalProtocolStore().getIdentityKeyPair().getPublicKey(),
|
||||
TrustLevel.TRUSTED_VERIFIED);
|
||||
|
||||
try (Manager m = new Manager(account, pathConfig, serviceConfiguration, userAgent)) {
|
||||
|
||||
m.refreshPreKeys();
|
||||
|
||||
m.close(false);
|
||||
}
|
||||
|
||||
account.save();
|
||||
}
|
||||
|
||||
private VerifyAccountResponse verifyAccountWithCode(
|
||||
final String verificationCode, final String legacyPin, final String registrationLock
|
||||
) throws IOException {
|
||||
return accountManager.verifyAccountWithCode(verificationCode,
|
||||
account.getSignalingKey(),
|
||||
account.getSignalProtocolStore().getLocalRegistrationId(),
|
||||
true,
|
||||
legacyPin,
|
||||
registrationLock,
|
||||
account.getSelfUnidentifiedAccessKey(),
|
||||
account.isUnrestrictedUnidentifiedAccess(),
|
||||
ServiceConfig.capabilities,
|
||||
account.isDiscoverableByPhoneNumber());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
if (account != null) {
|
||||
account.close();
|
||||
account = null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,6 +6,8 @@ import org.whispersystems.libsignal.InvalidKeyException;
|
|||
import org.whispersystems.libsignal.ecc.Curve;
|
||||
import org.whispersystems.libsignal.ecc.ECPublicKey;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
import org.whispersystems.signalservice.api.KeyBackupService;
|
||||
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
||||
import org.whispersystems.signalservice.api.account.AccountAttributes;
|
||||
import org.whispersystems.signalservice.api.push.TrustStore;
|
||||
import org.whispersystems.signalservice.internal.configuration.SignalCdnUrl;
|
||||
|
@ -109,6 +111,16 @@ public class ServiceConfig {
|
|||
}
|
||||
}
|
||||
|
||||
static KeyBackupService createKeyBackupService(SignalServiceAccountManager accountManager) {
|
||||
KeyStore keyStore = ServiceConfig.getIasKeyStore();
|
||||
|
||||
return accountManager.getKeyBackupService(keyStore,
|
||||
ServiceConfig.KEY_BACKUP_ENCLAVE_NAME,
|
||||
ServiceConfig.KEY_BACKUP_SERVICE_ID,
|
||||
ServiceConfig.KEY_BACKUP_MRENCLAVE,
|
||||
10);
|
||||
}
|
||||
|
||||
static ECPublicKey getUnidentifiedSenderTrustRoot() {
|
||||
try {
|
||||
return Curve.decodePoint(UNIDENTIFIED_SENDER_TRUST_ROOT, 0);
|
||||
|
|
|
@ -36,7 +36,7 @@ public class UnidentifiedAccessHelper {
|
|||
this.senderCertificateProvider = senderCertificateProvider;
|
||||
}
|
||||
|
||||
public byte[] getSelfUnidentifiedAccessKey() {
|
||||
private byte[] getSelfUnidentifiedAccessKey() {
|
||||
return UnidentifiedAccess.deriveAccessKeyFrom(selfProfileKeyProvider.getProfileKey());
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.asamk.signal.manager.storage.stickers.StickerStore;
|
|||
import org.asamk.signal.manager.storage.threads.LegacyJsonThreadStore;
|
||||
import org.asamk.signal.manager.storage.threads.ThreadInfo;
|
||||
import org.asamk.signal.manager.util.IOUtils;
|
||||
import org.asamk.signal.manager.util.KeyUtils;
|
||||
import org.asamk.signal.manager.util.Utils;
|
||||
import org.signal.zkgroup.InvalidInputException;
|
||||
import org.signal.zkgroup.profiles.ProfileKey;
|
||||
|
@ -36,6 +37,7 @@ import org.whispersystems.libsignal.state.PreKeyRecord;
|
|||
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
|
||||
import org.whispersystems.libsignal.util.Medium;
|
||||
import org.whispersystems.libsignal.util.Pair;
|
||||
import org.whispersystems.signalservice.api.crypto.UnidentifiedAccess;
|
||||
import org.whispersystems.signalservice.api.kbs.MasterKey;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
import org.whispersystems.util.Base64;
|
||||
|
@ -98,6 +100,8 @@ public class SignalAccount implements Closeable {
|
|||
try {
|
||||
SignalAccount account = new SignalAccount(pair.first(), pair.second());
|
||||
account.load(dataPath);
|
||||
account.migrateLegacyConfigs();
|
||||
|
||||
return account;
|
||||
} catch (Throwable e) {
|
||||
pair.second().close();
|
||||
|
@ -169,6 +173,31 @@ public class SignalAccount implements Closeable {
|
|||
return account;
|
||||
}
|
||||
|
||||
public void migrateLegacyConfigs() {
|
||||
if (getProfileKey() == null && isRegistered()) {
|
||||
// Old config file, creating new profile key
|
||||
setProfileKey(KeyUtils.createProfileKey());
|
||||
save();
|
||||
}
|
||||
// Store profile keys only in profile store
|
||||
for (ContactInfo contact : getContactStore().getContacts()) {
|
||||
String profileKeyString = contact.profileKey;
|
||||
if (profileKeyString == null) {
|
||||
continue;
|
||||
}
|
||||
final ProfileKey profileKey;
|
||||
try {
|
||||
profileKey = new ProfileKey(Base64.decode(profileKeyString));
|
||||
} catch (InvalidInputException | IOException e) {
|
||||
continue;
|
||||
}
|
||||
contact.profileKey = null;
|
||||
getProfileStore().storeProfileKey(contact.getAddress(), profileKey);
|
||||
}
|
||||
// Ensure our profile key is stored in profile store
|
||||
getProfileStore().storeProfileKey(getSelfAddress(), getProfileKey());
|
||||
}
|
||||
|
||||
public static File getFileName(File dataPath, String username) {
|
||||
return new File(dataPath, username);
|
||||
}
|
||||
|
@ -451,6 +480,10 @@ public class SignalAccount implements Closeable {
|
|||
return deviceId;
|
||||
}
|
||||
|
||||
public void setDeviceId(final int deviceId) {
|
||||
this.deviceId = deviceId;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
@ -491,6 +524,10 @@ public class SignalAccount implements Closeable {
|
|||
this.profileKey = profileKey;
|
||||
}
|
||||
|
||||
public byte[] getSelfUnidentifiedAccessKey() {
|
||||
return UnidentifiedAccess.deriveAccessKeyFrom(getProfileKey());
|
||||
}
|
||||
|
||||
public int getPreKeyIdOffset() {
|
||||
return preKeyIdOffset;
|
||||
}
|
||||
|
@ -515,8 +552,19 @@ public class SignalAccount implements Closeable {
|
|||
isMultiDevice = multiDevice;
|
||||
}
|
||||
|
||||
public boolean isUnrestrictedUnidentifiedAccess() {
|
||||
// TODO make configurable
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isDiscoverableByPhoneNumber() {
|
||||
// TODO make configurable
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
save();
|
||||
synchronized (fileChannel) {
|
||||
try {
|
||||
lock.close();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue