org.signal.libsignal.metadata.ProtocolInvalidMessageException Java Examples

The following examples show how to use org.signal.libsignal.metadata.ProtocolInvalidMessageException. 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: SignalServiceContent.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static SignalServiceTypingMessage createTypingMessage(SignalServiceMetadata metadata, SignalServiceProtos.TypingMessage content) throws ProtocolInvalidMessageException {
  SignalServiceTypingMessage.Action action;

  if      (content.getAction() == SignalServiceProtos.TypingMessage.Action.STARTED) action = SignalServiceTypingMessage.Action.STARTED;
  else if (content.getAction() == SignalServiceProtos.TypingMessage.Action.STOPPED) action = SignalServiceTypingMessage.Action.STOPPED;
  else                                                          action = SignalServiceTypingMessage.Action.UNKNOWN;

  if (content.hasTimestamp() && content.getTimestamp() != metadata.getTimestamp()) {
    throw new ProtocolInvalidMessageException(new InvalidMessageException("Timestamps don't match: " + content.getTimestamp() + " vs " + metadata.getTimestamp()),
                                              metadata.getSender().getIdentifier(),
                                              metadata.getSenderDevice());
  }

  return new SignalServiceTypingMessage(action, content.getTimestamp(),
                                        content.hasGroupId() ? Optional.of(content.getGroupId().toByteArray()) :
                                                               Optional.<byte[]>absent());
}
 
Example #2
Source File: SignalServiceContent.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static SignalServiceDataMessage.Quote createQuote(SignalServiceProtos.DataMessage content) throws ProtocolInvalidMessageException {
  if (!content.hasQuote()) return null;

  List<SignalServiceDataMessage.Quote.QuotedAttachment> attachments = new LinkedList<>();

  for (SignalServiceProtos.DataMessage.Quote.QuotedAttachment attachment : content.getQuote().getAttachmentsList()) {
    attachments.add(new SignalServiceDataMessage.Quote.QuotedAttachment(attachment.getContentType(),
                                                                        attachment.getFileName(),
                                                                        attachment.hasThumbnail() ? createAttachmentPointer(attachment.getThumbnail()) : null));
  }

  if (SignalServiceAddress.isValidAddress(content.getQuote().getAuthorUuid(), content.getQuote().getAuthorE164())) {
    SignalServiceAddress address = new SignalServiceAddress(UuidUtil.parseOrNull(content.getQuote().getAuthorUuid()), content.getQuote().getAuthorE164());

    return new SignalServiceDataMessage.Quote(content.getQuote().getId(),
                                              address,
                                              content.getQuote().getText(),
                                              attachments);
  } else {
    Log.w(TAG, "Quote was missing an author! Returning null.");
    return null;
  }
}
 
Example #3
Source File: SignalServiceContent.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static List<SignalServiceDataMessage.Preview> createPreviews(SignalServiceProtos.DataMessage content) throws ProtocolInvalidMessageException {
  if (content.getPreviewCount() <= 0) return null;

  List<SignalServiceDataMessage.Preview> results = new LinkedList<>();

  for (SignalServiceProtos.DataMessage.Preview preview : content.getPreviewList()) {
    SignalServiceAttachment attachment = null;

    if (preview.hasImage()) {
      attachment = createAttachmentPointer(preview.getImage());
    }

    results.add(new SignalServiceDataMessage.Preview(preview.getUrl(),
                            preview.getTitle(),
                            Optional.fromNullable(attachment)));
  }

  return results;
}
 
Example #4
Source File: SignalServiceContent.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static SignalServiceDataMessage.Sticker createSticker(SignalServiceProtos.DataMessage content) throws ProtocolInvalidMessageException {
  if (!content.hasSticker()                ||
      !content.getSticker().hasPackId()    ||
      !content.getSticker().hasPackKey()   ||
      !content.getSticker().hasStickerId() ||
      !content.getSticker().hasData())
  {
    return null;
  }

  SignalServiceProtos.DataMessage.Sticker sticker = content.getSticker();

  return new SignalServiceDataMessage.Sticker(sticker.getPackId().toByteArray(),
                     sticker.getPackKey().toByteArray(),
                     sticker.getStickerId(),
                     createAttachmentPointer(sticker.getData()));
}
 
Example #5
Source File: SignalServiceContent.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static SignalServiceAttachmentPointer createAttachmentPointer(SignalServiceProtos.AttachmentPointer pointer) throws ProtocolInvalidMessageException {
  return new SignalServiceAttachmentPointer(pointer.getCdnNumber(),
                                            SignalServiceAttachmentRemoteId.from(pointer),
                                            pointer.getContentType(),
                                            pointer.getKey().toByteArray(),
                                            pointer.hasSize() ? Optional.of(pointer.getSize()) : Optional.<Integer>absent(),
                                            pointer.hasThumbnail() ? Optional.of(pointer.getThumbnail().toByteArray()): Optional.<byte[]>absent(),
                                            pointer.getWidth(), pointer.getHeight(),
                                            pointer.hasDigest() ? Optional.of(pointer.getDigest().toByteArray()) : Optional.<byte[]>absent(),
                                            pointer.hasFileName() ? Optional.of(pointer.getFileName()) : Optional.<String>absent(),
                                            (pointer.getFlags() & SignalServiceProtos.AttachmentPointer.Flags.VOICE_MESSAGE_VALUE) != 0,
                                            pointer.hasCaption() ? Optional.of(pointer.getCaption()) : Optional.<String>absent(),
                                            pointer.hasBlurHash() ? Optional.of(pointer.getBlurHash()) : Optional.<String>absent(),
                                            pointer.hasUploadTimestamp() ? pointer.getUploadTimestamp() : 0);

}
 
Example #6
Source File: SignalServiceContent.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static SignalServiceGroupV2 createGroupV2Info(SignalServiceProtos.DataMessage content) throws ProtocolInvalidMessageException {
  if (!content.hasGroupV2()) return null;

  SignalServiceProtos.GroupContextV2 groupV2 = content.getGroupV2();
  if (!groupV2.hasMasterKey()) {
    throw new ProtocolInvalidMessageException(new InvalidMessageException("No GV2 master key on message"), null, 0);
  }
  if (!groupV2.hasRevision()) {
    throw new ProtocolInvalidMessageException(new InvalidMessageException("No GV2 revision on message"), null, 0);
  }

  SignalServiceGroupV2.Builder builder;
  try {
    builder = SignalServiceGroupV2.newBuilder(new GroupMasterKey(groupV2.getMasterKey().toByteArray()))
                                  .withRevision(groupV2.getRevision());
  } catch (InvalidInputException e) {
    throw new ProtocolInvalidMessageException(new InvalidMessageException(e), null, 0);
  }

  if (groupV2.hasGroupChange()) {
    builder.withSignedGroupChange(groupV2.getGroupChange().toByteArray());
  }

  return builder.build();
}
 
Example #7
Source File: SignalServiceCipher.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
private SignalServiceTypingMessage createTypingMessage(Metadata metadata, TypingMessage content) throws ProtocolInvalidMessageException {
  SignalServiceTypingMessage.Action action;

  if      (content.getAction() == TypingMessage.Action.STARTED) action = SignalServiceTypingMessage.Action.STARTED;
  else if (content.getAction() == TypingMessage.Action.STOPPED) action = SignalServiceTypingMessage.Action.STOPPED;
  else                                                          action = SignalServiceTypingMessage.Action.UNKNOWN;

  if (content.hasTimestamp() && content.getTimestamp() != metadata.getTimestamp()) {
    throw new ProtocolInvalidMessageException(new InvalidMessageException("Timestamps don't match: " + content.getTimestamp() + " vs " + metadata.getTimestamp()),
                                              metadata.getSender().getIdentifier(),
                                              metadata.getSenderDevice());
  }

  return new SignalServiceTypingMessage(action, content.getTimestamp(),
                                        content.hasGroupId() ? Optional.of(content.getGroupId().toByteArray()) :
                                                               Optional.<byte[]>absent());
}
 
Example #8
Source File: SignalServiceContent.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static SignalServiceContent deserialize(byte[] data) {
  try {
    if (data == null) return null;

    SignalServiceContentProto signalServiceContentProto = SignalServiceContentProto.parseFrom(data);

    return createFromProto(signalServiceContentProto);
  } catch (InvalidProtocolBufferException | ProtocolInvalidMessageException | ProtocolInvalidKeyException | UnsupportedDataMessageException e) {
    // We do not expect any of these exceptions if this byte[] has come from serialize.
    throw new AssertionError(e);
  }
}
 
Example #9
Source File: SignalServiceAttachmentRemoteId.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static SignalServiceAttachmentRemoteId from(AttachmentPointer attachmentPointer) throws ProtocolInvalidMessageException {
    switch (attachmentPointer.getAttachmentIdentifierCase()) {
        case CDNID:
            return new SignalServiceAttachmentRemoteId(attachmentPointer.getCdnId());
        case CDNKEY:
            return new SignalServiceAttachmentRemoteId(attachmentPointer.getCdnKey());
        case ATTACHMENTIDENTIFIER_NOT_SET:
            throw new ProtocolInvalidMessageException(new InvalidMessageException("AttachmentPointer CDN location not set"), null, 0);
    }
    return null;
}
 
Example #10
Source File: Manager.java    From signal-cli with GNU General Public License v3.0 5 votes vote down vote up
private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, SelfSendException, UnsupportedDataMessageException, org.whispersystems.libsignal.UntrustedIdentityException {
    SignalServiceCipher cipher = new SignalServiceCipher(account.getSelfAddress(), account.getSignalProtocolStore(), Utils.getCertificateValidator());
    try {
        return cipher.decrypt(envelope);
    } catch (ProtocolUntrustedIdentityException e) {
        if (e.getCause() instanceof org.whispersystems.libsignal.UntrustedIdentityException) {
            org.whispersystems.libsignal.UntrustedIdentityException identityException = (org.whispersystems.libsignal.UntrustedIdentityException) e.getCause();
            account.getSignalProtocolStore().saveIdentity(resolveSignalServiceAddress(identityException.getName()), identityException.getUntrustedIdentity(), TrustLevel.UNTRUSTED);
            throw identityException;
        }
        throw new AssertionError(e);
    }
}
 
Example #11
Source File: SignalServiceContent.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private static SignalServiceDataMessage createSignalServiceMessage(SignalServiceMetadata metadata,
                                                                   SignalServiceProtos.DataMessage content)
    throws ProtocolInvalidMessageException, UnsupportedDataMessageException
{
  SignalServiceGroup                  groupInfoV1  = createGroupV1Info(content);
  SignalServiceGroupV2                groupInfoV2  = createGroupV2Info(content);
  Optional<SignalServiceGroupContext> groupContext;

  try {
    groupContext = SignalServiceGroupContext.createOptional(groupInfoV1, groupInfoV2);
  } catch (InvalidMessageException e) {
    throw new ProtocolInvalidMessageException(e, null, 0);
  }


  List<SignalServiceAttachment>          attachments      = new LinkedList<>();
  boolean                                endSession       = ((content.getFlags() & SignalServiceProtos.DataMessage.Flags.END_SESSION_VALUE            ) != 0);
  boolean                                expirationUpdate = ((content.getFlags() & SignalServiceProtos.DataMessage.Flags.EXPIRATION_TIMER_UPDATE_VALUE) != 0);
  boolean                                profileKeyUpdate = ((content.getFlags() & SignalServiceProtos.DataMessage.Flags.PROFILE_KEY_UPDATE_VALUE     ) != 0);
  SignalServiceDataMessage.Quote         quote            = createQuote(content);
  List<SharedContact>                    sharedContacts   = createSharedContacts(content);
  List<SignalServiceDataMessage.Preview> previews         = createPreviews(content);
  SignalServiceDataMessage.Sticker       sticker          = createSticker(content);
  SignalServiceDataMessage.Reaction      reaction         = createReaction(content);
  SignalServiceDataMessage.RemoteDelete  remoteDelete     = createRemoteDelete(content);

  if (content.getRequiredProtocolVersion() > SignalServiceProtos.DataMessage.ProtocolVersion.CURRENT_VALUE) {
    throw new UnsupportedDataMessageProtocolVersionException(SignalServiceProtos.DataMessage.ProtocolVersion.CURRENT_VALUE,
                                                             content.getRequiredProtocolVersion(),
                                                             metadata.getSender().getIdentifier(),
                                                             metadata.getSenderDevice(),
                                                             groupContext);
  }

  for (SignalServiceProtos.AttachmentPointer pointer : content.getAttachmentsList()) {
    attachments.add(createAttachmentPointer(pointer));
  }

  if (content.hasTimestamp() && content.getTimestamp() != metadata.getTimestamp()) {
    throw new ProtocolInvalidMessageException(new InvalidMessageException("Timestamps don't match: " + content.getTimestamp() + " vs " + metadata.getTimestamp()),
                                                                          metadata.getSender().getIdentifier(),
                                                                          metadata.getSenderDevice());
  }

  return new SignalServiceDataMessage(metadata.getTimestamp(),
                                      groupInfoV1, groupInfoV2,
                                      attachments,
                                      content.hasBody() ? content.getBody() : null,
                                      endSession,
                                      content.getExpireTimer(),
                                      expirationUpdate,
                                      content.hasProfileKey() ? content.getProfileKey().toByteArray() : null,
                                      profileKeyUpdate,
                                      quote,
                                      sharedContacts,
                                      previews,
                                      sticker,
                                      content.getIsViewOnce(),
                                      reaction,
                                      remoteDelete);
}
 
Example #12
Source File: SignalServiceCipher.java    From libsignal-service-java with GNU General Public License v3.0 4 votes vote down vote up
private SignalServiceDataMessage createSignalServiceMessage(Metadata metadata, DataMessage content)
    throws ProtocolInvalidMessageException, UnsupportedDataMessageException
{
  SignalServiceGroup             groupInfo        = createGroupInfo(content);
  List<SignalServiceAttachment>  attachments      = new LinkedList<>();
  boolean                        endSession       = ((content.getFlags() & DataMessage.Flags.END_SESSION_VALUE            ) != 0);
  boolean                        expirationUpdate = ((content.getFlags() & DataMessage.Flags.EXPIRATION_TIMER_UPDATE_VALUE) != 0);
  boolean                        profileKeyUpdate = ((content.getFlags() & DataMessage.Flags.PROFILE_KEY_UPDATE_VALUE     ) != 0);
  SignalServiceDataMessage.Quote quote            = createQuote(content);
  List<SharedContact>            sharedContacts   = createSharedContacts(content);
  List<Preview>                  previews         = createPreviews(content);
  Sticker                        sticker          = createSticker(content);

  if (content.getRequiredProtocolVersion() > DataMessage.ProtocolVersion.CURRENT.getNumber()) {
    throw new UnsupportedDataMessageException(DataMessage.ProtocolVersion.CURRENT.getNumber(),
                                              content.getRequiredProtocolVersion(),
                                              metadata.getSender().getIdentifier(),
                                              metadata.getSenderDevice(),
                                              Optional.fromNullable(groupInfo));
  }

  for (AttachmentPointer pointer : content.getAttachmentsList()) {
    attachments.add(createAttachmentPointer(pointer));
  }

  if (content.hasTimestamp() && content.getTimestamp() != metadata.getTimestamp()) {
    throw new ProtocolInvalidMessageException(new InvalidMessageException("Timestamps don't match: " + content.getTimestamp() + " vs " + metadata.getTimestamp()),
                                                                          metadata.getSender().getIdentifier(),
                                                                          metadata.getSenderDevice());
  }

  return new SignalServiceDataMessage(metadata.getTimestamp(),
                                      groupInfo,
                                      attachments,
                                      content.getBody(),
                                      endSession,
                                      content.getExpireTimer(),
                                      expirationUpdate,
                                      content.hasProfileKey() ? content.getProfileKey().toByteArray() : null,
                                      profileKeyUpdate,
                                      quote,
                                      sharedContacts,
                                      previews,
                                      sticker,
                                      content.getIsViewOnce());
}