2 Copyright (C) 2015-2021 AsamK and contributors
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.
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.
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/>.
17 package org
.asamk
.signal
.manager
;
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
;
42 import java
.io
.Closeable
;
44 import java
.io
.IOException
;
45 import java
.util
.Locale
;
47 public class RegistrationManager
implements Closeable
{
49 private final static Logger logger
= LoggerFactory
.getLogger(RegistrationManager
.class);
51 private SignalAccount account
;
52 private final PathConfig pathConfig
;
53 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
54 private final String userAgent
;
56 private final SignalServiceAccountManager accountManager
;
57 private final PinHelper pinHelper
;
59 public RegistrationManager(
60 SignalAccount account
,
61 PathConfig pathConfig
,
62 ServiceEnvironmentConfig serviceEnvironmentConfig
,
65 this.account
= account
;
66 this.pathConfig
= pathConfig
;
67 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
68 this.userAgent
= userAgent
;
70 GroupsV2Operations groupsV2Operations
;
72 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration()));
73 } catch (Throwable ignored
) {
74 groupsV2Operations
= null;
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
),
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(),
88 this.pinHelper
= new PinHelper(keyBackupService
);
91 public static RegistrationManager
init(
92 String username
, File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
93 ) throws IOException
{
94 var pathConfig
= PathConfig
.createDefault(settingsPath
);
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);
101 var profileKey
= KeyUtils
.createProfileKey();
102 var account
= SignalAccount
.create(pathConfig
.getDataPath(),
107 TrustNewIdentity
.ON_FIRST_USE
);
109 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
112 var account
= SignalAccount
.load(pathConfig
.getDataPath(), username
, true, TrustNewIdentity
.ON_FIRST_USE
);
114 return new RegistrationManager(account
, pathConfig
, serviceConfiguration
, userAgent
);
117 public void register(boolean voiceVerification
, String captcha
) throws IOException
{
118 if (voiceVerification
) {
119 accountManager
.requestVoiceVerificationCode(getDefaultLocale(),
120 Optional
.fromNullable(captcha
),
123 accountManager
.requestSmsVerificationCode(false, Optional
.fromNullable(captcha
), Optional
.absent());
127 private Locale
getDefaultLocale() {
128 final var locale
= Locale
.getDefault();
130 Locale
.LanguageRange
.parse(locale
.getLanguage() + "-" + locale
.getCountry());
131 } catch (IllegalArgumentException e
) {
132 logger
.debug("Invalid locale, ignoring: {}", locale
);
139 public Manager
verifyAccount(
140 String verificationCode
, String pin
141 ) throws IOException
, LockedException
, KeyBackupSystemNoDataException
, KeyBackupServicePinException
{
142 verificationCode
= verificationCode
.replace("-", "");
143 VerifyAccountResponse response
;
146 response
= verifyAccountWithCode(verificationCode
, null, null);
150 } catch (LockedException e
) {
155 var registrationLockData
= pinHelper
.getRegistrationLockData(pin
, e
);
156 if (registrationLockData
== null) {
157 response
= verifyAccountWithCode(verificationCode
, pin
, null);
160 var registrationLock
= registrationLockData
.getMasterKey().deriveRegistrationLock();
162 response
= verifyAccountWithCode(verificationCode
, null, registrationLock
);
163 } catch (LockedException _e
) {
164 throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
166 masterKey
= registrationLockData
.getMasterKey();
170 // TODO response.isStorageCapable()
171 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
172 account
.finishRegistration(UuidUtil
.parseOrNull(response
.getUuid()), masterKey
, pin
);
176 m
= new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
180 // Set an initial empty profile so user can be added to groups
181 m
.setProfile(null, null, null, null, null);
183 final var result
= m
;
194 private VerifyAccountResponse
verifyAccountWithCode(
195 final String verificationCode
, final String legacyPin
, final String registrationLock
196 ) throws IOException
{
197 return accountManager
.verifyAccountWithCode(verificationCode
,
199 account
.getLocalRegistrationId(),
203 account
.getSelfUnidentifiedAccessKey(),
204 account
.isUnrestrictedUnidentifiedAccess(),
205 ServiceConfig
.capabilities
,
206 account
.isDiscoverableByPhoneNumber());
210 public void close() throws IOException
{
211 if (account
!= null) {