Java Code Examples for org.whispersystems.signalservice.internal.util.Util#readFully()

The following examples show how to use org.whispersystems.signalservice.internal.util.Util#readFully() . 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: AttachmentCipherInputStream.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private static void verifyMac(InputStream inputStream, long length, Mac mac, byte[] theirDigest)
    throws InvalidMacException
{
  try {
    MessageDigest   digest        = MessageDigest.getInstance("SHA256");
    int             remainingData = Util.toIntExact(length) - mac.getMacLength();
    byte[]          buffer        = new byte[4096];

    while (remainingData > 0) {
      int read = inputStream.read(buffer, 0, Math.min(buffer.length, remainingData));
      mac.update(buffer, 0, read);
      digest.update(buffer, 0, read);
      remainingData -= read;
    }

    byte[] ourMac   = mac.doFinal();
    byte[] theirMac = new byte[mac.getMacLength()];
    Util.readFully(inputStream, theirMac);

    if (!MessageDigest.isEqual(ourMac, theirMac)) {
      throw new InvalidMacException("MAC doesn't match!");
    }

    byte[] ourDigest = digest.digest(theirMac);

    if (theirDigest != null && !MessageDigest.isEqual(ourDigest, theirDigest)) {
      throw new InvalidMacException("Digest doesn't match!");
    }

  } catch (IOException | ArithmeticException e1) {
    throw new InvalidMacException(e1);
  } catch (NoSuchAlgorithmException e) {
    throw new AssertionError(e);
  }
}
 
Example 2
Source File: DeviceGroupsInputStream.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public DeviceGroup read() throws IOException {
  long   detailsLength     = readRawVarint32();
  byte[] detailsSerialized = new byte[(int)detailsLength];
  Util.readFully(in, detailsSerialized);

  SignalServiceProtos.GroupDetails details = SignalServiceProtos.GroupDetails.parseFrom(detailsSerialized);

  if (!details.hasId()) {
    throw new IOException("ID missing on group record!");
  }

  byte[]                                  id      = details.getId().toByteArray();
  Optional<String>                        name    = Optional.fromNullable(details.getName());
  List<String>                            members = details.getMembersList();
  Optional<SignalServiceAttachmentStream> avatar  = Optional.absent();
  boolean                                 active  = details.getActive();

  if (details.hasAvatar()) {
    long        avatarLength      = details.getAvatar().getLength();
    InputStream avatarStream      = new ChunkedInputStream.LimitedInputStream(in, avatarLength);
    String      avatarContentType = details.getAvatar().getContentType();

    avatar = Optional.of(new SignalServiceAttachmentStream(avatarStream, avatarContentType, avatarLength, Optional.<String>absent(), false, "",null));
  }

  return new DeviceGroup(id, name, members, avatar, active);
}
 
Example 3
Source File: AttachmentCipherInputStream.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private static void verifyMac(File file, Mac mac, byte[] theirDigest)
    throws FileNotFoundException, InvalidMacException
{
  try {
    MessageDigest   digest        = MessageDigest.getInstance("SHA256");
    FileInputStream fin           = new FileInputStream(file);
    int             remainingData = Util.toIntExact(file.length()) - mac.getMacLength();
    byte[]          buffer        = new byte[4096];

    while (remainingData > 0) {
      int read = fin.read(buffer, 0, Math.min(buffer.length, remainingData));
      mac.update(buffer, 0, read);
      digest.update(buffer, 0, read);
      remainingData -= read;
    }

    byte[] ourMac   = mac.doFinal();
    byte[] theirMac = new byte[mac.getMacLength()];
    Util.readFully(fin, theirMac);

    if (!MessageDigest.isEqual(ourMac, theirMac)) {
      throw new InvalidMacException("MAC doesn't match!");
    }

    byte[] ourDigest = digest.digest(theirMac);

    if (!MessageDigest.isEqual(ourDigest, theirDigest)) {
      throw new InvalidMacException("Digest doesn't match!");
    }

  } catch (IOException | ArithmeticException e1) {
    throw new InvalidMacException(e1);
  } catch (NoSuchAlgorithmException e) {
    throw new AssertionError(e);
  }
}
 
Example 4
Source File: AttachmentCipherInputStream.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
private static void verifyMac(InputStream inputStream, long length, Mac mac, byte[] theirDigest)
    throws InvalidMacException
{
  try {
    MessageDigest   digest        = MessageDigest.getInstance("SHA256");
    int             remainingData = Util.toIntExact(length) - mac.getMacLength();
    byte[]          buffer        = new byte[4096];

    while (remainingData > 0) {
      int read = inputStream.read(buffer, 0, Math.min(buffer.length, remainingData));
      mac.update(buffer, 0, read);
      digest.update(buffer, 0, read);
      remainingData -= read;
    }

    byte[] ourMac   = mac.doFinal();
    byte[] theirMac = new byte[mac.getMacLength()];
    Util.readFully(inputStream, theirMac);

    if (!MessageDigest.isEqual(ourMac, theirMac)) {
      throw new InvalidMacException("MAC doesn't match!");
    }

    byte[] ourDigest = digest.digest(theirMac);

    if (theirDigest != null && !MessageDigest.isEqual(ourDigest, theirDigest)) {
      throw new InvalidMacException("Digest doesn't match!");
    }

  } catch (IOException | ArithmeticException e1) {
    throw new InvalidMacException(e1);
  } catch (NoSuchAlgorithmException e) {
    throw new AssertionError(e);
  }
}
 
Example 5
Source File: DeviceGroupsInputStream.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public DeviceGroup read() throws IOException {
  long   detailsLength     = readRawVarint32();
  byte[] detailsSerialized = new byte[(int)detailsLength];
  Util.readFully(in, detailsSerialized);

  GroupDetails details = GroupDetails.parseFrom(detailsSerialized);

  if (!details.hasId()) {
    throw new IOException("ID missing on group record!");
  }

  byte[]                                  id              = details.getId().toByteArray();
  Optional<String>                        name            = Optional.fromNullable(details.getName());
  List<GroupDetails.Member>               members         = details.getMembersList();
  Optional<SignalServiceAttachmentStream> avatar          = Optional.absent();
  boolean                                 active          = details.getActive();
  Optional<Integer>                       expirationTimer = Optional.absent();
  Optional<String>                        color           = Optional.fromNullable(details.getColor());
  boolean                                 blocked         = details.getBlocked();
  Optional<Integer>                       inboxPosition   = Optional.absent();
  boolean                                 archived        = false;

  if (details.hasAvatar()) {
    long        avatarLength      = details.getAvatar().getLength();
    InputStream avatarStream      = new ChunkedInputStream.LimitedInputStream(in, avatarLength);
    String      avatarContentType = details.getAvatar().getContentType();

    avatar = Optional.of(new SignalServiceAttachmentStream(avatarStream, avatarContentType, avatarLength, Optional.<String>absent(), false, null, null));
  }

  if (details.hasExpireTimer() && details.getExpireTimer() > 0) {
    expirationTimer = Optional.of(details.getExpireTimer());
  }

  List<SignalServiceAddress> addressMembers = new ArrayList<>(members.size());
  for (GroupDetails.Member member : members) {
    if (SignalServiceAddress.isValidAddress(member.getUuid(), member.getE164())) {
      addressMembers.add(new SignalServiceAddress(UuidUtil.parseOrNull(member.getUuid()), member.getE164()));
    } else {
      throw new IOException("Missing group member address!");
    }
  }

  if (details.hasInboxPosition()) {
    inboxPosition = Optional.of(details.getInboxPosition());
  }

  if (details.hasArchived()) {
    archived = details.getArchived();
  }

  return new DeviceGroup(id, name, addressMembers, avatar, active, expirationTimer, color, blocked, inboxPosition, archived);
}
 
Example 6
Source File: DeviceContactsInputStream.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
public DeviceContact read() throws IOException {
  long   detailsLength     = readRawVarint32();
  byte[] detailsSerialized = new byte[(int)detailsLength];
  Util.readFully(in, detailsSerialized);

  SignalServiceProtos.ContactDetails      details    = SignalServiceProtos.ContactDetails.parseFrom(detailsSerialized);
  String                                  number     = details.getNumber();
  Optional<String>                        name       = Optional.fromNullable(details.getName());
  Optional<SignalServiceAttachmentStream> avatar     = Optional.absent();
  Optional<String>                        color      = details.hasColor() ? Optional.of(details.getColor()) : Optional.<String>absent();
  Optional<VerifiedMessage>               verified   = Optional.absent();
  Optional<byte[]>                        profileKey = Optional.absent();

  if (details.hasAvatar()) {
    long        avatarLength      = details.getAvatar().getLength();
    InputStream avatarStream      = new LimitedInputStream(in, avatarLength);
    String      avatarContentType = details.getAvatar().getContentType();

    avatar = Optional.of(new SignalServiceAttachmentStream(avatarStream, avatarContentType, avatarLength, Optional.<String>absent(), false, details.getNumber(),null));
  }

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

      VerifiedMessage.VerifiedState state;

      switch (details.getVerified().getState()) {
        case VERIFIED:  state = VerifiedMessage.VerifiedState.VERIFIED;   break;
        case UNVERIFIED:state = VerifiedMessage.VerifiedState.UNVERIFIED; break;
        case DEFAULT:   state = VerifiedMessage.VerifiedState.DEFAULT;    break;
        default:        throw new InvalidMessageException("Unknown state: " + details.getVerified().getState());
      }

      verified = Optional.of(new VerifiedMessage(destination, identityKey, state, System.currentTimeMillis()));
    } catch (InvalidKeyException | InvalidMessageException e) {
      Log.w(TAG, e);
      verified = Optional.absent();
    }
  }

  if (details.hasProfileKey()) {
    profileKey = Optional.fromNullable(details.getProfileKey().toByteArray());
  }

  return new DeviceContact(number, name, avatar, color, verified, profileKey);
}
 
Example 7
Source File: DeviceContactsInputStream.java    From libsignal-service-java with GNU General Public License v3.0 4 votes vote down vote up
public DeviceContact read() throws IOException {
  long   detailsLength     = readRawVarint32();
  byte[] detailsSerialized = new byte[(int)detailsLength];
  Util.readFully(in, detailsSerialized);

  SignalServiceProtos.ContactDetails details = SignalServiceProtos.ContactDetails.parseFrom(detailsSerialized);

  if (!SignalServiceAddress.isValidAddress(details.getUuid(), details.getNumber())) {
    throw new IOException("Missing contact address!");
  }

  SignalServiceAddress                    address     = new SignalServiceAddress(UuidUtil.parseOrNull(details.getUuid()), details.getNumber());
  Optional<String>                        name        = Optional.fromNullable(details.getName());
  Optional<SignalServiceAttachmentStream> avatar      = Optional.absent();
  Optional<String>                        color       = details.hasColor() ? Optional.of(details.getColor()) : Optional.<String>absent();
  Optional<VerifiedMessage>               verified    = Optional.absent();
  Optional<byte[]>                        profileKey  = Optional.absent();
  boolean                                 blocked     = false;
  Optional<Integer>                       expireTimer = Optional.absent();

  if (details.hasAvatar()) {
    long        avatarLength      = details.getAvatar().getLength();
    InputStream avatarStream      = new LimitedInputStream(in, avatarLength);
    String      avatarContentType = details.getAvatar().getContentType();

    avatar = Optional.of(new SignalServiceAttachmentStream(avatarStream, avatarContentType, avatarLength, Optional.<String>absent(), false, null));
  }

  if (details.hasVerified()) {
    try {
      if (!SignalServiceAddress.isValidAddress(details.getVerified().getDestinationUuid(), details.getVerified().getDestinationE164())) {
        throw new InvalidMessageException("Missing Verified address!");
      }
      IdentityKey          identityKey = new IdentityKey(details.getVerified().getIdentityKey().toByteArray(), 0);
      SignalServiceAddress destination = new SignalServiceAddress(UuidUtil.parseOrNull(details.getVerified().getDestinationUuid()),
                                                                  details.getVerified().getDestinationE164());

      VerifiedMessage.VerifiedState state;

      switch (details.getVerified().getState()) {
        case VERIFIED:  state = VerifiedMessage.VerifiedState.VERIFIED;   break;
        case UNVERIFIED:state = VerifiedMessage.VerifiedState.UNVERIFIED; break;
        case DEFAULT:   state = VerifiedMessage.VerifiedState.DEFAULT;    break;
        default:        throw new InvalidMessageException("Unknown state: " + details.getVerified().getState());
      }

      verified = Optional.of(new VerifiedMessage(destination, identityKey, state, System.currentTimeMillis()));
    } catch (InvalidKeyException | InvalidMessageException e) {
      Log.w(TAG, e);
      verified = Optional.absent();
    }
  }

  if (details.hasProfileKey()) {
    profileKey = Optional.fromNullable(details.getProfileKey().toByteArray());
  }

  if (details.hasExpireTimer() && details.getExpireTimer() > 0) {
    expireTimer = Optional.of(details.getExpireTimer());
  }

  blocked = details.getBlocked();

  return new DeviceContact(address, name, avatar, color, verified, profileKey, blocked, expireTimer);
}
 
Example 8
Source File: DeviceGroupsInputStream.java    From libsignal-service-java with GNU General Public License v3.0 4 votes vote down vote up
public DeviceGroup read() throws IOException {
  long   detailsLength     = readRawVarint32();
  byte[] detailsSerialized = new byte[(int)detailsLength];
  Util.readFully(in, detailsSerialized);

  GroupDetails details = GroupDetails.parseFrom(detailsSerialized);

  if (!details.hasId()) {
    throw new IOException("ID missing on group record!");
  }

  byte[]                                  id              = details.getId().toByteArray();
  Optional<String>                        name            = Optional.fromNullable(details.getName());
  List<GroupDetails.Member>               members         = details.getMembersList();
  Optional<SignalServiceAttachmentStream> avatar          = Optional.absent();
  boolean                                 active          = details.getActive();
  Optional<Integer>                       expirationTimer = Optional.absent();
  Optional<String>                        color           = Optional.fromNullable(details.getColor());
  boolean                                 blocked         = details.getBlocked();

  if (details.hasAvatar()) {
    long        avatarLength      = details.getAvatar().getLength();
    InputStream avatarStream      = new ChunkedInputStream.LimitedInputStream(in, avatarLength);
    String      avatarContentType = details.getAvatar().getContentType();

    avatar = Optional.of(new SignalServiceAttachmentStream(avatarStream, avatarContentType, avatarLength, Optional.<String>absent(), false, null));
  }

  if (details.hasExpireTimer() && details.getExpireTimer() > 0) {
    expirationTimer = Optional.of(details.getExpireTimer());
  }

  List<SignalServiceAddress> addressMembers = new ArrayList<>(members.size());
  for (GroupDetails.Member member : members) {
    if (SignalServiceAddress.isValidAddress(member.getUuid(), member.getE164())) {
      addressMembers.add(new SignalServiceAddress(UuidUtil.parseOrNull(member.getUuid()), member.getE164()));
    } else {
      throw new IOException("Missing group member address!");
    }
  }

  return new DeviceGroup(id, name, addressMembers, avatar, active, expirationTimer, color, blocked);
}