]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/helper/PinHelper.java
0bcfe09ee3559c8a9d19cc0a2b1d8ca05b5ede36
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / helper / PinHelper.java
1 package org.asamk.signal.manager.helper;
2
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;
10
11 import java.io.IOException;
12 import java.util.List;
13
14 public class PinHelper {
15
16 private static final Logger logger = LoggerFactory.getLogger(PinHelper.class);
17
18 private final List<SecureValueRecovery> secureValueRecoveries;
19
20 public PinHelper(final List<SecureValueRecovery> secureValueRecoveries) {
21 this.secureValueRecoveries = secureValueRecoveries;
22 }
23
24 public void setRegistrationLockPin(
25 String pin, MasterKey masterKey
26 ) throws IOException {
27 IOException exception = null;
28 for (final var secureValueRecovery : secureValueRecoveries) {
29 try {
30 final var backupResponse = secureValueRecovery.setPin(pin, masterKey).execute();
31 switch (backupResponse) {
32 case SecureValueRecovery.BackupResponse.Success success -> {
33 }
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");
44 }
45 } catch (IOException e) {
46 exception = e;
47 }
48 }
49 if (exception != null) {
50 throw exception;
51 }
52 }
53
54 public void migrateRegistrationLockPin(String pin, MasterKey masterKey) throws IOException {
55 setRegistrationLockPin(pin, masterKey);
56 }
57
58 public void removeRegistrationLockPin() throws IOException {
59 IOException exception = null;
60 for (final var secureValueRecovery : secureValueRecoveries) {
61 try {
62 final var deleteResponse = secureValueRecovery.deleteData();
63 switch (deleteResponse) {
64 case SecureValueRecovery.DeleteResponse.Success success -> {
65 }
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");
74 }
75 } catch (IOException e) {
76 exception = e;
77 }
78 }
79 if (exception != null) {
80 throw exception;
81 }
82 }
83
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) {
91 try {
92 return getRegistrationLockData(secureValueRecovery, svr2Credentials, pin);
93 } catch (IOException e) {
94 exception = e;
95 }
96 }
97 if (exception != null) {
98 throw exception;
99 }
100 }
101
102 return null;
103 }
104
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);
109
110 switch (restoreResponse) {
111 case SecureValueRecovery.RestoreResponse.Success s -> {
112 return s;
113 }
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.");
121 return null;
122 }
123 case null, default ->
124 throw new AssertionError("Unexpected response: " + restoreResponse.getClass().getSimpleName());
125 }
126 }
127 }