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