org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage Java Examples

The following examples show how to use org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SignalServiceMessageSender.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private void sendMessage(VerifiedMessage message, Optional<UnidentifiedAccessPair> unidentifiedAccess)
    throws IOException, UntrustedIdentityException
{
  byte[] nullMessageBody = DataMessage.newBuilder()
                                      .setBody(Base64.encodeBytes(Util.getRandomLengthBytes(140)))
                                      .build()
                                      .toByteArray();

  NullMessage nullMessage = NullMessage.newBuilder()
                                       .setPadding(ByteString.copyFrom(nullMessageBody))
                                       .build();

  byte[] content          = Content.newBuilder()
                                   .setNullMessage(nullMessage)
                                   .build()
                                   .toByteArray();

  SendMessageResult result = sendMessage(message.getDestination(), getTargetUnidentifiedAccess(unidentifiedAccess), message.getTimestamp(), content, false);

  if (result.getSuccess().isNeedsSync()) {
    byte[] syncMessage = createMultiDeviceVerifiedContent(message, nullMessage.toByteArray());
    sendMessage(localAddress, Optional.<UnidentifiedAccess>absent(), message.getTimestamp(), syncMessage, false);
  }
}
 
Example #2
Source File: SignalServiceMessageSender.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private byte[] createMultiDeviceVerifiedContent(VerifiedMessage verifiedMessage, byte[] nullMessage) {
  Content.Builder     container              = Content.newBuilder();
  SyncMessage.Builder syncMessage            = createSyncMessageBuilder();
  Verified.Builder    verifiedMessageBuilder = Verified.newBuilder();

  verifiedMessageBuilder.setNullMessage(ByteString.copyFrom(nullMessage));
  verifiedMessageBuilder.setIdentityKey(ByteString.copyFrom(verifiedMessage.getIdentityKey().serialize()));

  if (verifiedMessage.getDestination().getUuid().isPresent()) {
    verifiedMessageBuilder.setDestinationUuid(verifiedMessage.getDestination().getUuid().get().toString());
  }

  if (verifiedMessage.getDestination().getNumber().isPresent()) {
    verifiedMessageBuilder.setDestinationE164(verifiedMessage.getDestination().getNumber().get());
  }

  switch(verifiedMessage.getVerified()) {
    case DEFAULT:    verifiedMessageBuilder.setState(Verified.State.DEFAULT);    break;
    case VERIFIED:   verifiedMessageBuilder.setState(Verified.State.VERIFIED);   break;
    case UNVERIFIED: verifiedMessageBuilder.setState(Verified.State.UNVERIFIED); break;
    default:         throw new AssertionError("Unknown: " + verifiedMessage.getVerified());
  }

  syncMessage.setVerified(verifiedMessageBuilder);
  return container.setSyncMessage(syncMessage).build().toByteArray();
}
 
Example #3
Source File: MultiDeviceVerifiedUpdateJob.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onRun() throws IOException, UntrustedIdentityException {
  try {
    if (!TextSecurePreferences.isMultiDevice(context)) {
      Log.i(TAG, "Not multi device...");
      return;
    }

    if (destination == null) {
      Log.w(TAG, "No destination...");
      return;
    }

    SignalServiceMessageSender    messageSender        = ApplicationDependencies.getSignalServiceMessageSender();
    Recipient                     recipient            = Recipient.resolved(destination);
    VerifiedMessage.VerifiedState verifiedState        = getVerifiedState(verifiedStatus);
    SignalServiceAddress          verifiedAddress      = RecipientUtil.toSignalServiceAddress(context, recipient);
    VerifiedMessage               verifiedMessage      = new VerifiedMessage(verifiedAddress, new IdentityKey(identityKey, 0), verifiedState, timestamp);

    messageSender.sendMessage(SignalServiceSyncMessage.forVerified(verifiedMessage),
                              UnidentifiedAccessUtil.getAccessFor(context, recipient));
  } catch (InvalidKeyException e) {
    throw new IOException(e);
  }
}
 
Example #4
Source File: MultiDeviceContactUpdateJob.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private Optional<VerifiedMessage> getVerifiedMessage(Recipient recipient, Optional<IdentityDatabase.IdentityRecord> identity) throws InvalidNumberException {
  if (!identity.isPresent()) return Optional.absent();

  SignalServiceAddress destination = RecipientUtil.toSignalServiceAddress(context, recipient);
  IdentityKey          identityKey = identity.get().getIdentityKey();

  VerifiedMessage.VerifiedState state;

  switch (identity.get().getVerifiedStatus()) {
    case VERIFIED:   state = VerifiedMessage.VerifiedState.VERIFIED;   break;
    case UNVERIFIED: state = VerifiedMessage.VerifiedState.UNVERIFIED; break;
    case DEFAULT:    state = VerifiedMessage.VerifiedState.DEFAULT;    break;
    default: throw new AssertionError("Unknown state: " + identity.get().getVerifiedStatus());
  }

  return Optional.of(new VerifiedMessage(destination, identityKey, state, System.currentTimeMillis()));
}
 
Example #5
Source File: SignalServiceMessageSender.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
private void sendMessage(VerifiedMessage message, Optional<UnidentifiedAccessPair> unidentifiedAccess)
    throws IOException, UntrustedIdentityException
{
  byte[] nullMessageBody = DataMessage.newBuilder()
                                      .setBody(Base64.encodeBytes(Util.getRandomLengthBytes(140)))
                                      .build()
                                      .toByteArray();

  NullMessage nullMessage = NullMessage.newBuilder()
                                       .setPadding(ByteString.copyFrom(nullMessageBody))
                                       .build();

  byte[] content          = Content.newBuilder()
                                   .setNullMessage(nullMessage)
                                   .build()
                                   .toByteArray();

  SendMessageResult result = sendMessage(message.getDestination(), getTargetUnidentifiedAccess(unidentifiedAccess), message.getTimestamp(), content, false);

  if (result.getSuccess().isNeedsSync()) {
    byte[] syncMessage = createMultiDeviceVerifiedContent(message, nullMessage.toByteArray());
    sendMessage(localAddress, Optional.<UnidentifiedAccess>absent(), message.getTimestamp(), syncMessage, false);
  }
}
 
Example #6
Source File: SignalServiceMessageSender.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
private byte[] createMultiDeviceVerifiedContent(VerifiedMessage verifiedMessage, byte[] nullMessage) {
  Content.Builder     container              = Content.newBuilder();
  SyncMessage.Builder syncMessage            = createSyncMessageBuilder();
  Verified.Builder    verifiedMessageBuilder = Verified.newBuilder();

  verifiedMessageBuilder.setNullMessage(ByteString.copyFrom(nullMessage));
  verifiedMessageBuilder.setIdentityKey(ByteString.copyFrom(verifiedMessage.getIdentityKey().serialize()));

  if (verifiedMessage.getDestination().getUuid().isPresent()) {
    verifiedMessageBuilder.setDestinationUuid(verifiedMessage.getDestination().getUuid().get().toString());
  }

  if (verifiedMessage.getDestination().getNumber().isPresent()) {
    verifiedMessageBuilder.setDestinationE164(verifiedMessage.getDestination().getNumber().get());
  }

  switch(verifiedMessage.getVerified()) {
    case DEFAULT:    verifiedMessageBuilder.setState(Verified.State.DEFAULT);    break;
    case VERIFIED:   verifiedMessageBuilder.setState(Verified.State.VERIFIED);   break;
    case UNVERIFIED: verifiedMessageBuilder.setState(Verified.State.UNVERIFIED); break;
    default:         throw new AssertionError("Unknown: " + verifiedMessage.getVerified());
  }

  syncMessage.setVerified(verifiedMessageBuilder);
  return container.setSyncMessage(syncMessage).build().toByteArray();
}
 
Example #7
Source File: IdentityUtil.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static void processVerifiedMessage(Context context, VerifiedMessage verifiedMessage) {
  synchronized (SESSION_LOCK) {
    IdentityDatabase         identityDatabase = DatabaseFactory.getIdentityDatabase(context);
    Recipient                recipient        = Recipient.externalPush(context, verifiedMessage.getDestination());
    Optional<IdentityRecord> identityRecord   = identityDatabase.getIdentity(recipient.getId());

    if (!identityRecord.isPresent() && verifiedMessage.getVerified() == VerifiedMessage.VerifiedState.DEFAULT) {
      Log.w(TAG, "No existing record for default status");
      return;
    }

    if (verifiedMessage.getVerified() == VerifiedMessage.VerifiedState.DEFAULT              &&
        identityRecord.isPresent()                                                          &&
        identityRecord.get().getIdentityKey().equals(verifiedMessage.getIdentityKey())      &&
        identityRecord.get().getVerifiedStatus() != IdentityDatabase.VerifiedStatus.DEFAULT)
    {
      identityDatabase.setVerified(recipient.getId(), identityRecord.get().getIdentityKey(), IdentityDatabase.VerifiedStatus.DEFAULT);
      markIdentityVerified(context, recipient, false, true);
    }

    if (verifiedMessage.getVerified() == VerifiedMessage.VerifiedState.VERIFIED &&
        (!identityRecord.isPresent() ||
            (identityRecord.isPresent() && !identityRecord.get().getIdentityKey().equals(verifiedMessage.getIdentityKey())) ||
            (identityRecord.isPresent() && identityRecord.get().getVerifiedStatus() != IdentityDatabase.VerifiedStatus.VERIFIED)))
    {
      saveIdentity(context, verifiedMessage.getDestination().getIdentifier(), verifiedMessage.getIdentityKey());
      identityDatabase.setVerified(recipient.getId(), verifiedMessage.getIdentityKey(), IdentityDatabase.VerifiedStatus.VERIFIED);
      markIdentityVerified(context, recipient, true, true);
    }
  }
}
 
Example #8
Source File: MultiDeviceVerifiedUpdateJob.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private VerifiedMessage.VerifiedState getVerifiedState(VerifiedStatus status) {
  VerifiedMessage.VerifiedState verifiedState;

  switch (status) {
    case DEFAULT:    verifiedState = VerifiedMessage.VerifiedState.DEFAULT;    break;
    case VERIFIED:   verifiedState = VerifiedMessage.VerifiedState.VERIFIED;   break;
    case UNVERIFIED: verifiedState = VerifiedMessage.VerifiedState.UNVERIFIED; break;
    default: throw new AssertionError("Unknown status: " + verifiedStatus);
  }

  return verifiedState;
}
 
Example #9
Source File: MultiDeviceContactUpdateJob.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private void generateSingleContactUpdate(@NonNull RecipientId recipientId)
    throws IOException, UntrustedIdentityException, NetworkException
{
  File contactDataFile = createTempFile("multidevice-contact-update");

  try {
    DeviceContactsOutputStream                out             = new DeviceContactsOutputStream(new FileOutputStream(contactDataFile));
    Recipient                                 recipient       = Recipient.resolved(recipientId);
    Optional<IdentityDatabase.IdentityRecord> identityRecord  = DatabaseFactory.getIdentityDatabase(context).getIdentity(recipient.getId());
    Optional<VerifiedMessage>                 verifiedMessage = getVerifiedMessage(recipient, identityRecord);
    Map<RecipientId, Integer>                 inboxPositions  = DatabaseFactory.getThreadDatabase(context).getInboxPositions();
    Set<RecipientId>                          archived        = DatabaseFactory.getThreadDatabase(context).getArchivedRecipients();

    out.write(new DeviceContact(RecipientUtil.toSignalServiceAddress(context, recipient),
                                Optional.of(recipient.getDisplayName(context)),
                                getAvatar(recipient.getId(), recipient.getContactUri()),
                                Optional.fromNullable(recipient.getColor().serialize()),
                                verifiedMessage,
                                ProfileKeyUtil.profileKeyOptional(recipient.getProfileKey()),
                                recipient.isBlocked(),
                                recipient.getExpireMessages() > 0 ? Optional.of(recipient.getExpireMessages())
                                                                  : Optional.absent(),
                                Optional.fromNullable(inboxPositions.get(recipientId)),
                                archived.contains(recipientId)));

    out.close();
    sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), contactDataFile, false);

  } catch(InvalidNumberException e) {
    Log.w(TAG, e);
  } finally {
    if (contactDataFile != null) contactDataFile.delete();
  }
}
 
Example #10
Source File: IdentityUtil.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public static void processVerifiedMessage(Context context, AccountContext accountContext, MasterSecretUnion masterSecret, VerifiedMessage verifiedMessage) {
    synchronized (SESSION_LOCK) {
        IdentityRepo identityRepo = Repository.getIdentityRepo(accountContext);
        Recipient recipient = Recipient.from(accountContext, verifiedMessage.getDestination(), true);
        IdentityRecord identityRecord = identityRepo.getIdentityRecord(recipient.getAddress().serialize());

        if (identityRecord== null && verifiedMessage.getVerified() == VerifiedMessage.VerifiedState.DEFAULT) {
            Log.w(TAG, "No existing record for default status");
            return;
        }

        if (verifiedMessage.getVerified() == VerifiedMessage.VerifiedState.DEFAULT &&
                identityRecord != null &&
                identityRecord.getIdentityKey().equals(verifiedMessage.getIdentityKey()) &&
                identityRecord.getVerifyStatus() != IdentityRepo.VerifiedStatus.DEFAULT) {
            identityRepo.setVerified(recipient.getAddress().serialize(), identityRecord.getIdentityKey(), IdentityRepo.VerifiedStatus.DEFAULT);
            markIdentityVerified(recipient, false, true);
        }

        if (verifiedMessage.getVerified() == VerifiedMessage.VerifiedState.VERIFIED &&
                (identityRecord == null ||
                        identityRecord.getIdentityKey().equals(verifiedMessage.getIdentityKey()) ||
                        identityRecord.getVerifyStatus() != IdentityRepo.VerifiedStatus.VERIFIED)) {
            saveIdentity(context, accountContext, verifiedMessage.getDestination(), verifiedMessage.getIdentityKey(), true);
            identityRepo.setVerified(recipient.getAddress().serialize(), verifiedMessage.getIdentityKey(), IdentityRepo.VerifiedStatus.DEFAULT);
            markIdentityVerified(recipient, true, true);
        }
    }
}
 
Example #11
Source File: PushProcessMessageJob.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private void handleSynchronizeVerifiedMessage(@NonNull VerifiedMessage verifiedMessage) {
  IdentityUtil.processVerifiedMessage(context, verifiedMessage);
}
 
Example #12
Source File: SignalServiceCipher.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
private SignalServiceSyncMessage createSynchronizeMessage(SignalServiceProtos.Envelope envelope, SyncMessage content) throws InvalidMessageException {
  if (content.hasSent()) {
    SyncMessage.Sent sentContent = content.getSent();
    return SignalServiceSyncMessage.forSentTranscript(new SentTranscriptMessage(sentContent.getDestination(),
                                                                                sentContent.getTimestamp(),
                                                                                createSignalServiceMessage(envelope, sentContent.getMessage()),
                                                                                sentContent.getExpirationStartTimestamp()));
  }

  if (content.hasRequest()) {
    return SignalServiceSyncMessage.forRequest(new RequestMessage(content.getRequest()));
  }

  if (content.getReadList().size() > 0) {
    List<ReadMessage> readMessages = new LinkedList<>();

    for (SyncMessage.Read read : content.getReadList()) {
      readMessages.add(new ReadMessage(read.getSender(), read.getTimestamp()));
    }

    return SignalServiceSyncMessage.forRead(readMessages);
  }

  if (content.hasVerified()) {
    try {
      Verified    verified    = content.getVerified();
      String      destination = verified.getDestination();
      IdentityKey identityKey = new IdentityKey(verified.getIdentityKey().toByteArray(), 0);

      VerifiedState verifiedState;

      if (verified.getState() == Verified.State.DEFAULT) {
        verifiedState = VerifiedState.DEFAULT;
      } else if (verified.getState() == Verified.State.VERIFIED) {
        verifiedState = VerifiedState.VERIFIED;
      } else if (verified.getState() == Verified.State.UNVERIFIED) {
        verifiedState = VerifiedState.UNVERIFIED;
      } else {
        throw new InvalidMessageException("Unknown state: " + verified.getState().getNumber());
      }

      return SignalServiceSyncMessage.forVerified(new VerifiedMessage(destination, identityKey, verifiedState, System.currentTimeMillis()));
    } catch (InvalidKeyException e) {
      throw new InvalidMessageException(e);
    }
  }

  return SignalServiceSyncMessage.empty();
}
 
Example #13
Source File: PushDecryptJob.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
private void handleSynchronizeVerifiedMessage(@NonNull MasterSecretUnion masterSecret,
                                              @NonNull VerifiedMessage verifiedMessage) {
    ALog.i(TAG, "handleSynchronizeVerifiedMessage");
    IdentityUtil.processVerifiedMessage(context, accountContext, masterSecret, verifiedMessage);
}
 
Example #14
Source File: JsonVerifiedMessage.java    From signald with GNU General Public License v3.0 4 votes vote down vote up
JsonVerifiedMessage(VerifiedMessage message) {
    destination = message.getDestination();
    identityKey = Hex.toStringCondensed(message.getIdentityKey().getPublicKey().serialize());
    verified = message.getVerified().toString();
    timestamp = message.getTimestamp();
}
 
Example #15
Source File: Manager.java    From signal-cli with GNU General Public License v3.0 4 votes vote down vote up
private void sendVerifiedMessage(SignalServiceAddress destination, IdentityKey identityKey, TrustLevel trustLevel) throws IOException, UntrustedIdentityException {
    VerifiedMessage verifiedMessage = new VerifiedMessage(destination, identityKey, trustLevel.toVerifiedState(), System.currentTimeMillis());
    sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessage));
}