1 package org
.asamk
.signal
.manager
.helper
;
3 import org
.asamk
.signal
.manager
.util
.PinHashing
;
4 import org
.whispersystems
.libsignal
.InvalidKeyException
;
5 import org
.whispersystems
.signalservice
.api
.KbsPinData
;
6 import org
.whispersystems
.signalservice
.api
.KeyBackupService
;
7 import org
.whispersystems
.signalservice
.api
.KeyBackupServicePinException
;
8 import org
.whispersystems
.signalservice
.api
.KeyBackupSystemNoDataException
;
9 import org
.whispersystems
.signalservice
.api
.kbs
.HashedPin
;
10 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
11 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
12 import org
.whispersystems
.signalservice
.internal
.contacts
.entities
.TokenResponse
;
13 import org
.whispersystems
.signalservice
.internal
.push
.LockedException
;
15 import java
.io
.IOException
;
17 public class PinHelper
{
19 private final KeyBackupService keyBackupService
;
21 public PinHelper(final KeyBackupService keyBackupService
) {
22 this.keyBackupService
= keyBackupService
;
25 public void setRegistrationLockPin(
26 String pin
, MasterKey masterKey
27 ) throws IOException
, UnauthenticatedResponseException
{
28 final KeyBackupService
.PinChangeSession pinChangeSession
= keyBackupService
.newPinChangeSession();
29 final HashedPin hashedPin
= PinHashing
.hashPin(pin
, pinChangeSession
);
31 pinChangeSession
.setPin(hashedPin
, masterKey
);
32 pinChangeSession
.enableRegistrationLock(masterKey
);
35 public void removeRegistrationLockPin() throws IOException
, UnauthenticatedResponseException
{
36 final KeyBackupService
.PinChangeSession pinChangeSession
= keyBackupService
.newPinChangeSession();
37 pinChangeSession
.disableRegistrationLock();
38 pinChangeSession
.removePin();
41 public KbsPinData
getRegistrationLockData(
42 String pin
, LockedException e
43 ) throws IOException
, KeyBackupSystemNoDataException
, KeyBackupServicePinException
{
44 String basicStorageCredentials
= e
.getBasicStorageCredentials();
45 if (basicStorageCredentials
== null) {
49 return getRegistrationLockData(pin
, basicStorageCredentials
);
52 private KbsPinData
getRegistrationLockData(
53 String pin
, String basicStorageCredentials
54 ) throws IOException
, KeyBackupSystemNoDataException
, KeyBackupServicePinException
{
55 TokenResponse tokenResponse
= keyBackupService
.getToken(basicStorageCredentials
);
56 if (tokenResponse
== null || tokenResponse
.getTries() == 0) {
57 throw new IOException("KBS Account locked");
60 KbsPinData registrationLockData
= restoreMasterKey(pin
, basicStorageCredentials
, tokenResponse
);
61 if (registrationLockData
== null) {
62 throw new AssertionError("Failed to restore master key");
64 return registrationLockData
;
67 private KbsPinData
restoreMasterKey(
68 String pin
, String basicStorageCredentials
, TokenResponse tokenResponse
69 ) throws IOException
, KeyBackupSystemNoDataException
, KeyBackupServicePinException
{
70 if (pin
== null) return null;
72 if (basicStorageCredentials
== null) {
73 throw new AssertionError("Cannot restore KBS key, no storage credentials supplied");
76 KeyBackupService
.RestoreSession session
= keyBackupService
.newRegistrationSession(basicStorageCredentials
,
80 HashedPin hashedPin
= PinHashing
.hashPin(pin
, session
);
81 KbsPinData kbsData
= session
.restorePin(hashedPin
);
82 if (kbsData
== null) {
83 throw new AssertionError("Null not expected");
86 } catch (UnauthenticatedResponseException
| InvalidKeyException e
) {
87 throw new IOException(e
);