]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/Manager.java
Trust an identity with its scannable safety numbers from the other device
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / Manager.java
index 60a196dc0401849290e5e6a02e19977b51b8079c..cc57e0614b27451a0864e19e19b70cc8b66d8f51 100644 (file)
@@ -81,6 +81,9 @@ import org.whispersystems.libsignal.IdentityKeyPair;
 import org.whispersystems.libsignal.InvalidKeyException;
 import org.whispersystems.libsignal.InvalidMessageException;
 import org.whispersystems.libsignal.ecc.ECPublicKey;
+import org.whispersystems.libsignal.fingerprint.Fingerprint;
+import org.whispersystems.libsignal.fingerprint.FingerprintParsingException;
+import org.whispersystems.libsignal.fingerprint.FingerprintVersionMismatchException;
 import org.whispersystems.libsignal.state.PreKeyRecord;
 import org.whispersystems.libsignal.state.SignedPreKeyRecord;
 import org.whispersystems.libsignal.util.Pair;
@@ -1843,7 +1846,7 @@ public class Manager implements Closeable {
     ) throws IOException, InterruptedException {
         retryFailedReceivedMessages(handler, ignoreAttachments);
 
-        Set<HandleAction> queuedActions = null;
+        Set<HandleAction> queuedActions = new HashSet<>();
 
         final var signalWebSocket = dependencies.getSignalWebSocket();
         signalWebSocket.connect();
@@ -1872,20 +1875,17 @@ public class Manager implements Closeable {
                     // Received indicator that server queue is empty
                     hasCaughtUpWithOldMessages = true;
 
-                    if (queuedActions != null) {
-                        for (var action : queuedActions) {
-                            try {
-                                action.execute(this);
-                            } catch (Throwable e) {
-                                if (e instanceof AssertionError && e.getCause() instanceof InterruptedException) {
-                                    Thread.currentThread().interrupt();
-                                }
-                                logger.warn("Message action failed.", e);
+                    for (var action : queuedActions) {
+                        try {
+                            action.execute(this);
+                        } catch (Throwable e) {
+                            if (e instanceof AssertionError && e.getCause() instanceof InterruptedException) {
+                                Thread.currentThread().interrupt();
                             }
+                            logger.warn("Message action failed.", e);
                         }
-                        queuedActions.clear();
-                        queuedActions = null;
                     }
+                    queuedActions.clear();
 
                     // Continue to wait another timeout for new messages
                     continue;
@@ -1939,9 +1939,6 @@ public class Manager implements Closeable {
                         }
                     }
                 } else {
-                    if (queuedActions == null) {
-                        queuedActions = new HashSet<>();
-                    }
                     queuedActions.addAll(actions);
                 }
             }
@@ -2674,6 +2671,25 @@ public class Manager implements Closeable {
                 TrustLevel.TRUSTED_VERIFIED);
     }
 
+    /**
+     * Trust this the identity with this scannable safety number
+     *
+     * @param name         username of the identity
+     * @param safetyNumber Scannable safety number
+     */
+    public boolean trustIdentityVerifiedSafetyNumber(String name, byte[] safetyNumber) throws InvalidNumberException {
+        var recipientId = canonicalizeAndResolveRecipient(name);
+        var address = account.getRecipientStore().resolveServiceAddress(recipientId);
+        return trustIdentity(recipientId, identityKey -> {
+            final var fingerprint = computeSafetyNumberFingerprint(address, identityKey);
+            try {
+                return fingerprint != null && fingerprint.getScannableFingerprint().compareTo(safetyNumber);
+            } catch (FingerprintVersionMismatchException | FingerprintParsingException e) {
+                return false;
+            }
+        }, TrustLevel.TRUSTED_VERIFIED);
+    }
+
     /**
      * Trust all keys of this identity without verification
      *
@@ -2723,21 +2739,23 @@ public class Manager implements Closeable {
     }
 
     public String computeSafetyNumber(SignalServiceAddress theirAddress, IdentityKey theirIdentityKey) {
-        final var fingerprint = Utils.computeSafetyNumber(capabilities.isUuid(),
-                account.getSelfAddress(),
-                getIdentityKeyPair().getPublicKey(),
-                theirAddress,
-                theirIdentityKey);
+        final Fingerprint fingerprint = computeSafetyNumberFingerprint(theirAddress, theirIdentityKey);
         return fingerprint == null ? null : fingerprint.getDisplayableFingerprint().getDisplayText();
     }
 
     public byte[] computeSafetyNumberForScanning(SignalServiceAddress theirAddress, IdentityKey theirIdentityKey) {
-        final var fingerprint = Utils.computeSafetyNumber(capabilities.isUuid(),
+        final Fingerprint fingerprint = computeSafetyNumberFingerprint(theirAddress, theirIdentityKey);
+        return fingerprint == null ? null : fingerprint.getScannableFingerprint().getSerialized();
+    }
+
+    private Fingerprint computeSafetyNumberFingerprint(
+            final SignalServiceAddress theirAddress, final IdentityKey theirIdentityKey
+    ) {
+        return Utils.computeSafetyNumber(capabilities.isUuid(),
                 account.getSelfAddress(),
                 getIdentityKeyPair().getPublicKey(),
                 theirAddress,
                 theirIdentityKey);
-        return fingerprint == null ? null : fingerprint.getScannableFingerprint().getSerialized();
     }
 
     @Deprecated