1 package org
.asamk
.signal
.manager
.helper
;
3 import org
.asamk
.signal
.manager
.api
.IncorrectPinException
;
4 import org
.slf4j
.Logger
;
5 import org
.slf4j
.LoggerFactory
;
6 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
7 import org
.whispersystems
.signalservice
.api
.svr
.SecureValueRecovery
;
8 import org
.whispersystems
.signalservice
.internal
.push
.AuthCredentials
;
9 import org
.whispersystems
.signalservice
.internal
.push
.LockedException
;
11 import java
.io
.IOException
;
12 import java
.util
.List
;
14 public class PinHelper
{
16 private static final Logger logger
= LoggerFactory
.getLogger(PinHelper
.class);
18 private final List
<SecureValueRecovery
> secureValueRecoveries
;
20 public PinHelper(final List
<SecureValueRecovery
> secureValueRecoveries
) {
21 this.secureValueRecoveries
= secureValueRecoveries
;
24 public void setRegistrationLockPin(
25 String pin
, MasterKey masterKey
26 ) throws IOException
{
27 IOException exception
= null;
28 for (final var secureValueRecovery
: secureValueRecoveries
) {
30 final var backupResponse
= secureValueRecovery
.setPin(pin
, masterKey
).execute();
31 switch (backupResponse
) {
32 case SecureValueRecovery
.BackupResponse
.Success success
-> {
34 case SecureValueRecovery
.BackupResponse
.ServerRejected serverRejected
->
35 logger
.warn("Backup svr2 failed: ServerRejected");
36 case SecureValueRecovery
.BackupResponse
.EnclaveNotFound enclaveNotFound
->
37 logger
.warn("Backup svr2 failed: EnclaveNotFound");
38 case SecureValueRecovery
.BackupResponse
.ExposeFailure exposeFailure
->
39 logger
.warn("Backup svr2 failed: ExposeFailure");
40 case SecureValueRecovery
.BackupResponse
.ApplicationError error
->
41 throw new IOException(error
.getException());
42 case SecureValueRecovery
.BackupResponse
.NetworkError error
-> throw error
.getException();
43 case null, default -> throw new AssertionError("Unexpected response");
45 } catch (IOException e
) {
49 if (exception
!= null) {
54 public void migrateRegistrationLockPin(String pin
, MasterKey masterKey
) throws IOException
{
55 setRegistrationLockPin(pin
, masterKey
);
58 public void removeRegistrationLockPin() throws IOException
{
59 IOException exception
= null;
60 for (final var secureValueRecovery
: secureValueRecoveries
) {
62 final var deleteResponse
= secureValueRecovery
.deleteData();
63 switch (deleteResponse
) {
64 case SecureValueRecovery
.DeleteResponse
.Success success
-> {
66 case SecureValueRecovery
.DeleteResponse
.ServerRejected serverRejected
->
67 logger
.warn("Delete svr2 failed: ServerRejected");
68 case SecureValueRecovery
.DeleteResponse
.EnclaveNotFound enclaveNotFound
->
69 logger
.warn("Delete svr2 failed: EnclaveNotFound");
70 case SecureValueRecovery
.DeleteResponse
.ApplicationError error
->
71 throw new IOException(error
.getException());
72 case SecureValueRecovery
.DeleteResponse
.NetworkError error
-> throw error
.getException();
73 case null, default -> throw new AssertionError("Unexpected response");
75 } catch (IOException e
) {
79 if (exception
!= null) {
84 public SecureValueRecovery
.RestoreResponse
.Success
getRegistrationLockData(
85 String pin
, LockedException lockedException
86 ) throws IOException
, IncorrectPinException
{
87 var svr2Credentials
= lockedException
.getSvr2Credentials();
88 if (svr2Credentials
!= null) {
89 IOException exception
= null;
90 for (final var secureValueRecovery
: secureValueRecoveries
) {
92 return getRegistrationLockData(secureValueRecovery
, svr2Credentials
, pin
);
93 } catch (IOException e
) {
97 if (exception
!= null) {
105 public SecureValueRecovery
.RestoreResponse
.Success
getRegistrationLockData(
106 SecureValueRecovery secureValueRecovery
, AuthCredentials authCredentials
, String pin
107 ) throws IOException
, IncorrectPinException
{
108 final var restoreResponse
= secureValueRecovery
.restoreDataPreRegistration(authCredentials
, pin
);
110 switch (restoreResponse
) {
111 case SecureValueRecovery
.RestoreResponse
.Success s
-> {
114 case SecureValueRecovery
.RestoreResponse
.PinMismatch pinMismatch
->
115 throw new IncorrectPinException(pinMismatch
.getTriesRemaining());
116 case SecureValueRecovery
.RestoreResponse
.ApplicationError error
->
117 throw new IOException(error
.getException());
118 case SecureValueRecovery
.RestoreResponse
.NetworkError error
-> throw error
.getException();
119 case SecureValueRecovery
.RestoreResponse
.Missing missing
-> {
120 logger
.debug("No SVR data stored for the given credentials.");
123 case null, default ->
124 throw new AssertionError("Unexpected response: " + restoreResponse
.getClass().getSimpleName());