]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/manager/Manager.java
Make loggers private
[signal-cli] / src / main / java / org / asamk / signal / manager / Manager.java
index d0a9e2779de5dffbe9f52ee04b8763e170bcc23a..4dff4b82bf7afb2d5c3a208e40e7e26e6699d704 100644 (file)
@@ -74,12 +74,9 @@ import org.whispersystems.libsignal.IdentityKeyPair;
 import org.whispersystems.libsignal.InvalidKeyException;
 import org.whispersystems.libsignal.InvalidMessageException;
 import org.whispersystems.libsignal.InvalidVersionException;
-import org.whispersystems.libsignal.ecc.Curve;
-import org.whispersystems.libsignal.ecc.ECKeyPair;
 import org.whispersystems.libsignal.ecc.ECPublicKey;
 import org.whispersystems.libsignal.state.PreKeyRecord;
 import org.whispersystems.libsignal.state.SignedPreKeyRecord;
-import org.whispersystems.libsignal.util.Medium;
 import org.whispersystems.libsignal.util.Pair;
 import org.whispersystems.libsignal.util.guava.Optional;
 import org.whispersystems.signalservice.api.KeyBackupService;
@@ -185,7 +182,7 @@ import static org.asamk.signal.manager.ServiceConfig.getIasKeyStore;
 
 public class Manager implements Closeable {
 
-    final static Logger logger = LoggerFactory.getLogger(Manager.class);
+    private final static Logger logger = LoggerFactory.getLogger(Manager.class);
 
     private final CertificateValidator certificateValidator = new CertificateValidator(ServiceConfig.getUnidentifiedSenderTrustRoot());
 
@@ -299,21 +296,15 @@ public class Manager implements Closeable {
     }
 
     public void checkAccountState() throws IOException {
-        if (account.isRegistered()) {
-            if (accountManager.getPreKeysCount() < ServiceConfig.PREKEY_MINIMUM_COUNT) {
-                refreshPreKeys();
-                account.save();
-            }
-            if (account.getUuid() == null) {
-                account.setUuid(accountManager.getOwnUuid());
-                account.save();
-            }
-            updateAccountAttributes();
+        if (accountManager.getPreKeysCount() < ServiceConfig.PREKEY_MINIMUM_COUNT) {
+            refreshPreKeys();
+            account.save();
         }
-    }
-
-    public boolean isRegistered() {
-        return account.isRegistered();
+        if (account.getUuid() == null) {
+            account.setUuid(accountManager.getOwnUuid());
+            account.save();
+        }
+        updateAccountAttributes();
     }
 
     /**
@@ -321,7 +312,7 @@ public class Manager implements Closeable {
      *
      * @param numbers The set of phone number in question
      * @return A map of numbers to booleans. True if registered, false otherwise. Should never be null
-     * @throws IOException if its unable to check if the users are registered
+     * @throws IOException if its unable to get the contacts to check if they're registered
      */
     public Map<String, Boolean> areUsersRegistered(Set<String> numbers) throws IOException {
         // Note "contactDetails" has no optionals. It only gives us info on users who are registered
@@ -396,43 +387,6 @@ public class Manager implements Closeable {
         account.save();
     }
 
-    private List<PreKeyRecord> generatePreKeys() {
-        List<PreKeyRecord> records = new ArrayList<>(ServiceConfig.PREKEY_BATCH_SIZE);
-
-        final int offset = account.getPreKeyIdOffset();
-        for (int i = 0; i < ServiceConfig.PREKEY_BATCH_SIZE; i++) {
-            int preKeyId = (offset + i) % Medium.MAX_VALUE;
-            ECKeyPair keyPair = Curve.generateKeyPair();
-            PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
-
-            records.add(record);
-        }
-
-        account.addPreKeys(records);
-        account.save();
-
-        return records;
-    }
-
-    private SignedPreKeyRecord generateSignedPreKey(IdentityKeyPair identityKeyPair) {
-        try {
-            ECKeyPair keyPair = Curve.generateKeyPair();
-            byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(),
-                    keyPair.getPublicKey().serialize());
-            SignedPreKeyRecord record = new SignedPreKeyRecord(account.getNextSignedPreKeyId(),
-                    System.currentTimeMillis(),
-                    keyPair,
-                    signature);
-
-            account.addSignedPreKey(record);
-            account.save();
-
-            return record;
-        } catch (InvalidKeyException e) {
-            throw new AssertionError(e);
-        }
-    }
-
     public void setRegistrationLockPin(Optional<String> pin) throws IOException, UnauthenticatedResponseException {
         if (pin.isPresent()) {
             final MasterKey masterKey = account.getPinMasterKey() != null
@@ -464,6 +418,26 @@ public class Manager implements Closeable {
         accountManager.setPreKeys(identityKeyPair.getPublicKey(), signedPreKeyRecord, oneTimePreKeys);
     }
 
+    private List<PreKeyRecord> generatePreKeys() {
+        final int offset = account.getPreKeyIdOffset();
+
+        List<PreKeyRecord> records = KeyUtils.generatePreKeyRecords(offset, ServiceConfig.PREKEY_BATCH_SIZE);
+        account.addPreKeys(records);
+        account.save();
+
+        return records;
+    }
+
+    private SignedPreKeyRecord generateSignedPreKey(IdentityKeyPair identityKeyPair) {
+        final int signedPreKeyId = account.getNextSignedPreKeyId();
+
+        SignedPreKeyRecord record = KeyUtils.generateSignedPreKeyRecord(identityKeyPair, signedPreKeyId);
+        account.addSignedPreKey(record);
+        account.save();
+
+        return record;
+    }
+
     private SignalServiceMessagePipe getOrCreateMessagePipe() {
         if (messagePipe == null) {
             messagePipe = messageReceiver.createMessagePipe();
@@ -496,10 +470,6 @@ public class Manager implements Closeable {
                 ServiceConfig.MAX_ENVELOPE_SIZE);
     }
 
-    private SignalServiceProfile getEncryptedRecipientProfile(SignalServiceAddress address) throws IOException {
-        return profileHelper.retrieveProfileSync(address, SignalServiceProfile.RequestType.PROFILE).getProfile();
-    }
-
     private SignalProfile getRecipientProfile(
             SignalServiceAddress address
     ) {
@@ -560,7 +530,8 @@ public class Manager implements Closeable {
     private SignalProfile retrieveRecipientProfile(
             SignalServiceAddress address, ProfileKey profileKey
     ) throws IOException {
-        final SignalServiceProfile encryptedProfile = getEncryptedRecipientProfile(address);
+        final SignalServiceProfile encryptedProfile = profileHelper.retrieveProfileSync(address,
+                SignalServiceProfile.RequestType.PROFILE).getProfile();
 
         return decryptProfile(address, profileKey, encryptedProfile);
     }
@@ -1829,7 +1800,11 @@ public class Manager implements Closeable {
                 }
             }
             account.save();
-            if (!isMessageBlocked(envelope, content)) {
+            if (isMessageBlocked(envelope, content)) {
+                logger.info("Ignoring a message from blocked user/group: {}", envelope.getTimestamp());
+            } else if (isNotAGroupMember(envelope, content)) {
+                logger.info("Ignoring a message from a non group member: {}", envelope.getTimestamp());
+            } else {
                 handler.handleMessage(envelope, content, exception);
             }
             if (!(exception instanceof org.whispersystems.libsignal.UntrustedIdentityException)) {
@@ -1856,18 +1831,43 @@ public class Manager implements Closeable {
             return true;
         }
 
+        if (content != null && content.getDataMessage().isPresent()) {
+            SignalServiceDataMessage message = content.getDataMessage().get();
+            if (message.getGroupContext().isPresent()) {
+                GroupId groupId = GroupUtils.getGroupId(message.getGroupContext().get());
+                GroupInfo group = getGroup(groupId);
+                if (group != null && group.isBlocked()) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    private boolean isNotAGroupMember(
+            SignalServiceEnvelope envelope, SignalServiceContent content
+    ) {
+        SignalServiceAddress source;
+        if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
+            source = envelope.getSourceAddress();
+        } else if (content != null) {
+            source = content.getSender();
+        } else {
+            return false;
+        }
+
         if (content != null && content.getDataMessage().isPresent()) {
             SignalServiceDataMessage message = content.getDataMessage().get();
             if (message.getGroupContext().isPresent()) {
                 if (message.getGroupContext().get().getGroupV1().isPresent()) {
                     SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get();
-                    if (groupInfo.getType() != SignalServiceGroup.Type.DELIVER) {
+                    if (groupInfo.getType() == SignalServiceGroup.Type.QUIT) {
                         return false;
                     }
                 }
                 GroupId groupId = GroupUtils.getGroupId(message.getGroupContext().get());
                 GroupInfo group = getGroup(groupId);
-                if (group != null && group.isBlocked()) {
+                if (group != null && !group.isMember(source)) {
                     return true;
                 }
             }