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