]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java
Add new --trust-new-identities global parameter
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / RegistrationManager.java
1 /*
2 Copyright (C) 2015-2021 AsamK and contributors
3
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.
8
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.
13
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/>.
16 */
17 package org.asamk.signal.manager;
18
19 import org.asamk.signal.manager.config.ServiceConfig;
20 import org.asamk.signal.manager.config.ServiceEnvironment;
21 import org.asamk.signal.manager.config.ServiceEnvironmentConfig;
22 import org.asamk.signal.manager.helper.PinHelper;
23 import org.asamk.signal.manager.storage.SignalAccount;
24 import org.asamk.signal.manager.storage.identities.TrustNewIdentity;
25 import org.asamk.signal.manager.util.KeyUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.whispersystems.libsignal.util.KeyHelper;
29 import org.whispersystems.libsignal.util.guava.Optional;
30 import org.whispersystems.signalservice.api.KeyBackupServicePinException;
31 import org.whispersystems.signalservice.api.KeyBackupSystemNoDataException;
32 import org.whispersystems.signalservice.api.SignalServiceAccountManager;
33 import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations;
34 import org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations;
35 import org.whispersystems.signalservice.api.kbs.MasterKey;
36 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
37 import org.whispersystems.signalservice.api.util.UuidUtil;
38 import org.whispersystems.signalservice.internal.push.LockedException;
39 import org.whispersystems.signalservice.internal.push.VerifyAccountResponse;
40 import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider;
41
42 import java.io.Closeable;
43 import java.io.File;
44 import java.io.IOException;
45 import java.util.Locale;
46
47 public class RegistrationManager implements Closeable {
48
49 private final static Logger logger = LoggerFactory.getLogger(RegistrationManager.class);
50
51 private SignalAccount account;
52 private final PathConfig pathConfig;
53 private final ServiceEnvironmentConfig serviceEnvironmentConfig;
54 private final String userAgent;
55
56 private final SignalServiceAccountManager accountManager;
57 private final PinHelper pinHelper;
58
59 public RegistrationManager(
60 SignalAccount account,
61 PathConfig pathConfig,
62 ServiceEnvironmentConfig serviceEnvironmentConfig,
63 String userAgent
64 ) {
65 this.account = account;
66 this.pathConfig = pathConfig;
67 this.serviceEnvironmentConfig = serviceEnvironmentConfig;
68 this.userAgent = userAgent;
69
70 GroupsV2Operations groupsV2Operations;
71 try {
72 groupsV2Operations = new GroupsV2Operations(ClientZkOperations.create(serviceEnvironmentConfig.getSignalServiceConfiguration()));
73 } catch (Throwable ignored) {
74 groupsV2Operations = null;
75 }
76 this.accountManager = new SignalServiceAccountManager(serviceEnvironmentConfig.getSignalServiceConfiguration(),
77 new DynamicCredentialsProvider(
78 // Using empty UUID, because registering doesn't work otherwise
79 null, account.getUsername(), account.getPassword(), SignalServiceAddress.DEFAULT_DEVICE_ID),
80 userAgent,
81 groupsV2Operations,
82 ServiceConfig.AUTOMATIC_NETWORK_RETRY);
83 final var keyBackupService = accountManager.getKeyBackupService(ServiceConfig.getIasKeyStore(),
84 serviceEnvironmentConfig.getKeyBackupConfig().getEnclaveName(),
85 serviceEnvironmentConfig.getKeyBackupConfig().getServiceId(),
86 serviceEnvironmentConfig.getKeyBackupConfig().getMrenclave(),
87 10);
88 this.pinHelper = new PinHelper(keyBackupService);
89 }
90
91 public static RegistrationManager init(
92 String username, File settingsPath, ServiceEnvironment serviceEnvironment, String userAgent
93 ) throws IOException {
94 var pathConfig = PathConfig.createDefault(settingsPath);
95
96 final var serviceConfiguration = ServiceConfig.getServiceEnvironmentConfig(serviceEnvironment, userAgent);
97 if (!SignalAccount.userExists(pathConfig.getDataPath(), username)) {
98 var identityKey = KeyUtils.generateIdentityKeyPair();
99 var registrationId = KeyHelper.generateRegistrationId(false);
100
101 var profileKey = KeyUtils.createProfileKey();
102 var account = SignalAccount.create(pathConfig.getDataPath(),
103 username,
104 identityKey,
105 registrationId,
106 profileKey,
107 TrustNewIdentity.ON_FIRST_USE);
108
109 return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent);
110 }
111
112 var account = SignalAccount.load(pathConfig.getDataPath(), username, true, TrustNewIdentity.ON_FIRST_USE);
113
114 return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent);
115 }
116
117 public void register(boolean voiceVerification, String captcha) throws IOException {
118 if (voiceVerification) {
119 accountManager.requestVoiceVerificationCode(getDefaultLocale(),
120 Optional.fromNullable(captcha),
121 Optional.absent());
122 } else {
123 accountManager.requestSmsVerificationCode(false, Optional.fromNullable(captcha), Optional.absent());
124 }
125 }
126
127 private Locale getDefaultLocale() {
128 final var locale = Locale.getDefault();
129 try {
130 Locale.LanguageRange.parse(locale.getLanguage() + "-" + locale.getCountry());
131 } catch (IllegalArgumentException e) {
132 logger.debug("Invalid locale, ignoring: {}", locale);
133 return null;
134 }
135
136 return locale;
137 }
138
139 public Manager verifyAccount(
140 String verificationCode, String pin
141 ) throws IOException, LockedException, KeyBackupSystemNoDataException, KeyBackupServicePinException {
142 verificationCode = verificationCode.replace("-", "");
143 VerifyAccountResponse response;
144 MasterKey masterKey;
145 try {
146 response = verifyAccountWithCode(verificationCode, null, null);
147
148 masterKey = null;
149 pin = null;
150 } catch (LockedException e) {
151 if (pin == null) {
152 throw e;
153 }
154
155 var registrationLockData = pinHelper.getRegistrationLockData(pin, e);
156 if (registrationLockData == null) {
157 response = verifyAccountWithCode(verificationCode, pin, null);
158 masterKey = null;
159 } else {
160 var registrationLock = registrationLockData.getMasterKey().deriveRegistrationLock();
161 try {
162 response = verifyAccountWithCode(verificationCode, null, registrationLock);
163 } catch (LockedException _e) {
164 throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
165 }
166 masterKey = registrationLockData.getMasterKey();
167 }
168 }
169
170 // TODO response.isStorageCapable()
171 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
172 account.finishRegistration(UuidUtil.parseOrNull(response.getUuid()), masterKey, pin);
173
174 Manager m = null;
175 try {
176 m = new Manager(account, pathConfig, serviceEnvironmentConfig, userAgent);
177 account = null;
178
179 m.refreshPreKeys();
180 // Set an initial empty profile so user can be added to groups
181 m.setProfile(null, null, null, null, null);
182
183 final var result = m;
184 m = null;
185
186 return result;
187 } finally {
188 if (m != null) {
189 m.close();
190 }
191 }
192 }
193
194 private VerifyAccountResponse verifyAccountWithCode(
195 final String verificationCode, final String legacyPin, final String registrationLock
196 ) throws IOException {
197 return accountManager.verifyAccountWithCode(verificationCode,
198 null,
199 account.getLocalRegistrationId(),
200 true,
201 legacyPin,
202 registrationLock,
203 account.getSelfUnidentifiedAccessKey(),
204 account.isUnrestrictedUnidentifiedAccess(),
205 ServiceConfig.capabilities,
206 account.isDiscoverableByPhoneNumber());
207 }
208
209 @Override
210 public void close() throws IOException {
211 if (account != null) {
212 account.close();
213 account = null;
214 }
215 }
216 }