org.whispersystems.signalservice.api.crypto.UnidentifiedAccess Java Examples

The following examples show how to use org.whispersystems.signalservice.api.crypto.UnidentifiedAccess. 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
/**
 * Send a message to a group.
 *
 * @param recipients The group members.
 * @param message The group message.
 * @throws IOException
 */
public List<SendMessageResult> sendMessage(List<SignalServiceAddress>             recipients,
                                           List<Optional<UnidentifiedAccessPair>> unidentifiedAccess,
                                           boolean                                isRecipientUpdate,
                                           SignalServiceDataMessage               message)
    throws IOException, UntrustedIdentityException
{
  byte[]                  content            = createMessageContent(message);
  long                    timestamp          = message.getTimestamp();
  List<SendMessageResult> results            = sendMessage(recipients, getTargetUnidentifiedAccess(unidentifiedAccess), timestamp, content, false);
  boolean                 needsSyncInResults = false;

  for (SendMessageResult result : results) {
    if (result.getSuccess() != null && result.getSuccess().isNeedsSync()) {
      needsSyncInResults = true;
      break;
    }
  }

  if (needsSyncInResults || isMultiDevice.get()) {
    byte[] syncMessage = createMultiDeviceSentTranscriptContent(content, Optional.<SignalServiceAddress>absent(), timestamp, results, isRecipientUpdate);
    sendMessage(localAddress, Optional.<UnidentifiedAccess>absent(), timestamp, syncMessage, false);
  }

  return results;
}
 
Example #2
Source File: UnidentifiedAccessUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static @Nullable byte[] getTargetUnidentifiedAccessKey(@NonNull Recipient recipient) {
  ProfileKey theirProfileKey = ProfileKeyUtil.profileKeyOrNull(recipient.resolve().getProfileKey());

  switch (recipient.resolve().getUnidentifiedAccessMode()) {
    case UNKNOWN:
      if (theirProfileKey == null) return Util.getSecretBytes(16);
      else                         return UnidentifiedAccess.deriveAccessKeyFrom(theirProfileKey);
    case DISABLED:
      return null;
    case ENABLED:
      if (theirProfileKey == null) return null;
      else                         return UnidentifiedAccess.deriveAccessKeyFrom(theirProfileKey);
    case UNRESTRICTED:
      return Util.getSecretBytes(16);
    default:
      throw new AssertionError("Unknown mode: " + recipient.getUnidentifiedAccessMode().getMode());
  }
}
 
Example #3
Source File: UnidentifiedAccessUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public static Optional<UnidentifiedAccessPair> getAccessForSync(@NonNull Context context) {
  try {
    byte[] ourUnidentifiedAccessKey         = UnidentifiedAccess.deriveAccessKeyFrom(ProfileKeyUtil.getSelfProfileKey());
    byte[] ourUnidentifiedAccessCertificate = TextSecurePreferences.getUnidentifiedAccessCertificate(context);

    if (TextSecurePreferences.isUniversalUnidentifiedAccess(context)) {
      ourUnidentifiedAccessKey = Util.getSecretBytes(16);
    }

    if (ourUnidentifiedAccessKey != null && ourUnidentifiedAccessCertificate != null) {
      return Optional.of(new UnidentifiedAccessPair(new UnidentifiedAccess(ourUnidentifiedAccessKey,
                                                                           ourUnidentifiedAccessCertificate),
                                                    new UnidentifiedAccess(ourUnidentifiedAccessKey,
                                                                           ourUnidentifiedAccessCertificate)));
    }

    return Optional.absent();
  } catch (InvalidCertificateException e) {
    Log.w(TAG, e);
    return Optional.absent();
  }
}
 
Example #4
Source File: ProfileUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@WorkerThread
public static @NonNull ProfileAndCredential retrieveProfile(@NonNull Context context,
                                                            @NonNull Recipient recipient,
                                                            @NonNull SignalServiceProfile.RequestType requestType)
  throws IOException
{
  SignalServiceAddress         address            = RecipientUtil.toSignalServiceAddress(context, recipient);
  Optional<UnidentifiedAccess> unidentifiedAccess = getUnidentifiedAccess(context, recipient);
  Optional<ProfileKey>         profileKey         = ProfileKeyUtil.profileKeyOptional(recipient.getProfileKey());

  ProfileAndCredential profile;

  try {
    profile = retrieveProfileInternal(address, profileKey, unidentifiedAccess, requestType);
  } catch (NonSuccessfulResponseCodeException e) {
    if (unidentifiedAccess.isPresent()) {
      profile = retrieveProfileInternal(address, profileKey, Optional.absent(), requestType);
    } else {
      throw e;
    }
  }

  return profile;
}
 
Example #5
Source File: SignalServiceMessageSender.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Send a message to a group.
 *
 * @param recipients The group members.
 * @param message The group message.
 * @throws IOException
 */
public List<SendMessageResult> sendMessage(List<SignalServiceAddress>             recipients,
                                           List<Optional<UnidentifiedAccessPair>> unidentifiedAccess,
                                           boolean                                isRecipientUpdate,
                                           SignalServiceDataMessage               message)
    throws IOException, UntrustedIdentityException
{
  byte[]                  content            = createMessageContent(message);
  long                    timestamp          = message.getTimestamp();
  List<SendMessageResult> results            = sendMessage(recipients, getTargetUnidentifiedAccess(unidentifiedAccess), timestamp, content, false);
  boolean                 needsSyncInResults = false;

  for (SendMessageResult result : results) {
    if (result.getSuccess() != null && result.getSuccess().isNeedsSync()) {
      needsSyncInResults = true;
      break;
    }
  }

  if (needsSyncInResults || isMultiDevice.get()) {
    byte[] syncMessage = createMultiDeviceSentTranscriptContent(content, Optional.<SignalServiceAddress>absent(), timestamp, results, isRecipientUpdate);
    sendMessage(localAddress, Optional.<UnidentifiedAccess>absent(), timestamp, syncMessage, false);
  }

  return results;
}
 
Example #6
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 #7
Source File: PushServiceSocket.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public SignalServiceProfile retrieveVersionedProfile(UUID target, ProfileKey profileKey, Optional<UnidentifiedAccess> unidentifiedAccess)
    throws NonSuccessfulResponseCodeException, PushNetworkException
{
  ProfileKeyVersion profileKeyIdentifier = profileKey.getProfileKeyVersion(target);

  String version = profileKeyIdentifier.serialize();
  String subPath = String.format("%s/%s", target, version);

  String response = makeServiceRequest(String.format(PROFILE_PATH, subPath), "GET", null, NO_HEADERS, unidentifiedAccess);

  try {
    return JsonUtil.fromJson(response, SignalServiceProfile.class);
  } catch (IOException e) {
    Log.w(TAG, e);
    throw new NonSuccessfulResponseCodeException("Unable to parse entity");
  }
}
 
Example #8
Source File: PushServiceSocket.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public ProfileAndCredential retrieveVersionedProfileAndCredential(UUID target, ProfileKey profileKey, Optional<UnidentifiedAccess> unidentifiedAccess)
    throws NonSuccessfulResponseCodeException, PushNetworkException, VerificationFailedException
{
  ProfileKeyVersion                  profileKeyIdentifier = profileKey.getProfileKeyVersion(target);
  ProfileKeyCredentialRequestContext requestContext       = clientZkProfileOperations.createProfileKeyCredentialRequestContext(random, target, profileKey);
  ProfileKeyCredentialRequest        request              = requestContext.getRequest();

  String version           = profileKeyIdentifier.serialize();
  String credentialRequest = Hex.toStringCondensed(request.serialize());
  String subPath           = String.format("%s/%s/%s", target, version, credentialRequest);

  String response = makeServiceRequest(String.format(PROFILE_PATH, subPath), "GET", null, NO_HEADERS, unidentifiedAccess);

  try {
    SignalServiceProfile signalServiceProfile = JsonUtil.fromJson(response, SignalServiceProfile.class);

    ProfileKeyCredential profileKeyCredential = signalServiceProfile.getProfileKeyCredentialResponse() != null
                                              ? clientZkProfileOperations.receiveProfileKeyCredential(requestContext, signalServiceProfile.getProfileKeyCredentialResponse())
                                              : null;

    return new ProfileAndCredential(signalServiceProfile, SignalServiceProfile.RequestType.PROFILE_AND_CREDENTIAL, Optional.fromNullable(profileKeyCredential));
  } catch (IOException e) {
    Log.w(TAG, e);
    throw new NonSuccessfulResponseCodeException("Unable to parse entity");
  }
}
 
Example #9
Source File: SignalServiceMessageSender.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
private OutgoingPushMessageList getEncryptedMessages(PushServiceSocket            socket,
                                                     SignalServiceAddress         recipient,
                                                     Optional<UnidentifiedAccess> unidentifiedAccess,
                                                     long                         timestamp,
                                                     byte[]                       plaintext,
                                                     boolean                      online)
    throws IOException, InvalidKeyException, UntrustedIdentityException
{
  List<OutgoingPushMessage> messages = new LinkedList<>();

  if (!recipient.matches(localAddress) || unidentifiedAccess.isPresent()) {
    messages.add(getEncryptedMessage(socket, recipient, unidentifiedAccess, SignalServiceAddress.DEFAULT_DEVICE_ID, plaintext));
  }

  for (int deviceId : store.getSubDeviceSessions(recipient.getIdentifier())) {
    if (store.containsSession(new SignalProtocolAddress(recipient.getIdentifier(), deviceId))) {
      messages.add(getEncryptedMessage(socket, recipient, unidentifiedAccess, deviceId, plaintext));
    }
  }

  return new OutgoingPushMessageList(recipient.getIdentifier(), timestamp, messages, online);
}
 
Example #10
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 #11
Source File: Manager.java    From signal-cli with GNU General Public License v3.0 6 votes vote down vote up
private SignalServiceProfile getRecipientProfile(SignalServiceAddress address, Optional<UnidentifiedAccess> unidentifiedAccess) throws IOException {
    SignalServiceMessagePipe pipe = unidentifiedMessagePipe != null && unidentifiedAccess.isPresent() ? unidentifiedMessagePipe
            : messagePipe;

    if (pipe != null) {
        try {
            return pipe.getProfile(address, Optional.absent(), unidentifiedAccess, SignalServiceProfile.RequestType.PROFILE).getProfile();
        } catch (IOException ignored) {
        }
    }

    SignalServiceMessageReceiver receiver = getMessageReceiver();
    try {
        return receiver.retrieveProfile(address, Optional.absent(), unidentifiedAccess, SignalServiceProfile.RequestType.PROFILE).getProfile();
    } catch (VerificationFailedException e) {
        throw new AssertionError(e);
    }
}
 
Example #12
Source File: SignalServiceMessageReceiver.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public ProfileAndCredential retrieveProfile(SignalServiceAddress address,
                                            Optional<ProfileKey> profileKey,
                                            Optional<UnidentifiedAccess> unidentifiedAccess,
                                            SignalServiceProfile.RequestType requestType)
    throws NonSuccessfulResponseCodeException, PushNetworkException, VerificationFailedException
{
  Optional<UUID> uuid = address.getUuid();

  if (uuid.isPresent() && profileKey.isPresent()) {
    if (requestType == SignalServiceProfile.RequestType.PROFILE_AND_CREDENTIAL) {
      return socket.retrieveVersionedProfileAndCredential(uuid.get(), profileKey.get(), unidentifiedAccess);
    } else {
      return new ProfileAndCredential(socket.retrieveVersionedProfile(uuid.get(), profileKey.get(), unidentifiedAccess),
                                      SignalServiceProfile.RequestType.PROFILE,
                                      Optional.absent());
    }
  } else {
    return new ProfileAndCredential(socket.retrieveProfile(address, unidentifiedAccess),
                                    SignalServiceProfile.RequestType.PROFILE,
                                    Optional.absent());
  }
}
 
Example #13
Source File: Manager.java    From signal-cli with GNU General Public License v3.0 6 votes vote down vote up
private Optional<UnidentifiedAccessPair> getAccessForSync() {
    byte[] selfUnidentifiedAccessKey = getSelfUnidentifiedAccessKey();
    byte[] selfUnidentifiedAccessCertificate = getSenderCertificate();

    if (selfUnidentifiedAccessKey == null || selfUnidentifiedAccessCertificate == null) {
        return Optional.absent();
    }

    try {
        return Optional.of(new UnidentifiedAccessPair(
                new UnidentifiedAccess(selfUnidentifiedAccessKey, selfUnidentifiedAccessCertificate),
                new UnidentifiedAccess(selfUnidentifiedAccessKey, selfUnidentifiedAccessCertificate)
        ));
    } catch (InvalidCertificateException e) {
        return Optional.absent();
    }
}
 
Example #14
Source File: Manager.java    From signal-cli with GNU General Public License v3.0 6 votes vote down vote up
private Optional<UnidentifiedAccessPair> getAccessFor(SignalServiceAddress recipient) {
    byte[] recipientUnidentifiedAccessKey = getTargetUnidentifiedAccessKey(recipient);
    byte[] selfUnidentifiedAccessKey = getSelfUnidentifiedAccessKey();
    byte[] selfUnidentifiedAccessCertificate = getSenderCertificate();

    if (recipientUnidentifiedAccessKey == null || selfUnidentifiedAccessKey == null || selfUnidentifiedAccessCertificate == null) {
        return Optional.absent();
    }

    try {
        return Optional.of(new UnidentifiedAccessPair(
                new UnidentifiedAccess(recipientUnidentifiedAccessKey, selfUnidentifiedAccessCertificate),
                new UnidentifiedAccess(selfUnidentifiedAccessKey, selfUnidentifiedAccessCertificate)
        ));
    } catch (InvalidCertificateException e) {
        return Optional.absent();
    }
}
 
Example #15
Source File: SignalServiceMessageSender.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private OutgoingPushMessageList getEncryptedMessages(PushServiceSocket            socket,
                                                     SignalServiceAddress         recipient,
                                                     Optional<UnidentifiedAccess> unidentifiedAccess,
                                                     long                         timestamp,
                                                     byte[]                       plaintext,
                                                     boolean                      online)
    throws IOException, InvalidKeyException, UntrustedIdentityException
{
  List<OutgoingPushMessage> messages = new LinkedList<>();

  if (!recipient.matches(localAddress) || unidentifiedAccess.isPresent()) {
    messages.add(getEncryptedMessage(socket, recipient, unidentifiedAccess, SignalServiceAddress.DEFAULT_DEVICE_ID, plaintext));
  }

  for (int deviceId : store.getSubDeviceSessions(recipient.getIdentifier())) {
    if (store.containsSession(new SignalProtocolAddress(recipient.getIdentifier(), deviceId))) {
      messages.add(getEncryptedMessage(socket, recipient, unidentifiedAccess, deviceId, plaintext));
    }
  }

  return new OutgoingPushMessageList(recipient.getIdentifier(), timestamp, messages, online);
}
 
Example #16
Source File: SignalServiceMessageSender.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public void sendMessage(SignalServiceSyncMessage message, Optional<UnidentifiedAccessPair> unidentifiedAccess)
    throws IOException, UntrustedIdentityException
{
  byte[] content;

  if (message.getContacts().isPresent()) {
    content = createMultiDeviceContactsContent(message.getContacts().get().getContactsStream().asStream(),
                                               message.getContacts().get().isComplete());
  } else if (message.getGroups().isPresent()) {
    content = createMultiDeviceGroupsContent(message.getGroups().get().asStream());
  } else if (message.getRead().isPresent()) {
    content = createMultiDeviceReadContent(message.getRead().get());
  } else if (message.getViewOnceOpen().isPresent()) {
    content = createMultiDeviceViewOnceOpenContent(message.getViewOnceOpen().get());
  } else if (message.getBlockedList().isPresent()) {
    content = createMultiDeviceBlockedContent(message.getBlockedList().get());
  } else if (message.getConfiguration().isPresent()) {
    content = createMultiDeviceConfigurationContent(message.getConfiguration().get());
  } else if (message.getSent().isPresent()) {
    content = createMultiDeviceSentTranscriptContent(message.getSent().get(), unidentifiedAccess);
  } else if (message.getStickerPackOperations().isPresent()) {
    content = createMultiDeviceStickerPackOperationContent(message.getStickerPackOperations().get());
  } else if (message.getFetchType().isPresent()) {
    content = createMultiDeviceFetchTypeContent(message.getFetchType().get());
  } else if (message.getVerified().isPresent()) {
    sendMessage(message.getVerified().get(), unidentifiedAccess);
    return;
  } else {
    throw new IOException("Unsupported sync message!");
  }

  long timestamp = message.getSent().isPresent() ? message.getSent().get().getTimestamp()
                                                 : System.currentTimeMillis();

  sendMessage(localAddress, Optional.<UnidentifiedAccess>absent(), timestamp, content, false);
}
 
Example #17
Source File: Manager.java    From signal-cli with GNU General Public License v3.0 5 votes vote down vote up
private Optional<UnidentifiedAccess> getUnidentifiedAccess(SignalServiceAddress recipient) {
    Optional<UnidentifiedAccessPair> unidentifiedAccess = getAccessFor(recipient);

    if (unidentifiedAccess.isPresent()) {
        return unidentifiedAccess.get().getTargetUnidentifiedAccess();
    }

    return Optional.absent();
}
 
Example #18
Source File: SignalServiceMessageSender.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Send a message to a single recipient.
 *
 * @param recipient The message's destination.
 * @param message The message.
 * @throws UntrustedIdentityException
 * @throws IOException
 */
public SendMessageResult sendMessage(SignalServiceAddress             recipient,
                                     Optional<UnidentifiedAccessPair> unidentifiedAccess,
                                     SignalServiceDataMessage         message)
    throws UntrustedIdentityException, IOException
{
  byte[]            content   = createMessageContent(message);
  long              timestamp = message.getTimestamp();
  SendMessageResult result    = sendMessage(recipient, getTargetUnidentifiedAccess(unidentifiedAccess), timestamp, content, false);

  if (result.getSuccess() != null && result.getSuccess().isNeedsSync()) {
    byte[] syncMessage = createMultiDeviceSentTranscriptContent(content, Optional.of(recipient), timestamp, Collections.singletonList(result), false);
    sendMessage(localAddress, Optional.<UnidentifiedAccess>absent(), timestamp, syncMessage, false);
  }

  if (message.isEndSession()) {
    if (recipient.getUuid().isPresent()) {
      store.deleteAllSessions(recipient.getUuid().get().toString());
    }
    if (recipient.getNumber().isPresent()) {
      store.deleteAllSessions(recipient.getNumber().get());
    }

    if (eventListener.isPresent()) {
      eventListener.get().onSecurityEvent(recipient);
    }
  }

  return result;
}
 
Example #19
Source File: ProfileUtil.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private static Optional<UnidentifiedAccess> getUnidentifiedAccess(@NonNull Context context, @NonNull Recipient recipient) {
  Optional<UnidentifiedAccessPair> unidentifiedAccess = UnidentifiedAccessUtil.getAccessFor(context, recipient);

  if (unidentifiedAccess.isPresent()) {
    return unidentifiedAccess.get().getTargetUnidentifiedAccess();
  }

  return Optional.absent();
}
 
Example #20
Source File: RefreshAttributesJob.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onRun() throws IOException {
  if (!TextSecurePreferences.isPushRegistered(context) || TextSecurePreferences.getLocalNumber(context) == null) {
    Log.w(TAG, "Not yet registered. Skipping.");
    return;
  }

  int       registrationId              = TextSecurePreferences.getLocalRegistrationId(context);
  boolean   fetchesMessages             = TextSecurePreferences.isFcmDisabled(context);
  byte[]    unidentifiedAccessKey       = UnidentifiedAccess.deriveAccessKeyFrom(ProfileKeyUtil.getSelfProfileKey());
  boolean   universalUnidentifiedAccess = TextSecurePreferences.isUniversalUnidentifiedAccess(context);
  String    registrationLockV1          = null;
  String    registrationLockV2          = null;
  KbsValues kbsValues                   = SignalStore.kbsValues();

  if (kbsValues.isV2RegistrationLockEnabled()) {
    registrationLockV2 = kbsValues.getRegistrationLockToken();
  } else if (TextSecurePreferences.isV1RegistrationLockEnabled(context)) {
    //noinspection deprecation Ok to read here as they have not migrated
    registrationLockV1 = TextSecurePreferences.getDeprecatedV1RegistrationLockPin(context);
  }

  Log.i(TAG, "Calling setAccountAttributes() reglockV1? " + !TextUtils.isEmpty(registrationLockV1) + ", reglockV2? " + !TextUtils.isEmpty(registrationLockV2) + ", pin? " + kbsValues.hasPin());

  SignalServiceAccountManager signalAccountManager = ApplicationDependencies.getSignalServiceAccountManager();
  signalAccountManager.setAccountAttributes(null, registrationId, fetchesMessages,
                                            registrationLockV1, registrationLockV2,
                                            unidentifiedAccessKey, universalUnidentifiedAccess,
                                            AppCapabilities.getCapabilities(kbsValues.hasPin()));
}
 
Example #21
Source File: SignalBot.java    From signal-bot with GNU General Public License v3.0 5 votes vote down vote up
public void verify(String verificationCode) throws IOException {
    String username = prefs.get("LOCAL_USERNAME", null);
    String password = prefs.get("LOCAL_PASSWORD", null);
    logger.info("Verifying user " + username + " with code " + verificationCode + "...");
    String code = verificationCode.replace("-", "");
    int registrationId = KeyHelper.generateRegistrationId(false);
    prefs.putInt("REGISTRATION_ID", registrationId);
    byte[] profileKey = Util.getSecretBytes(32);
    byte[] unidentifiedAccessKey = UnidentifiedAccess.deriveAccessKeyFrom(profileKey);
    accountManager = new SignalServiceAccountManager(config, username, password, USER_AGENT);
    accountManager.verifyAccountWithCode(code, null, registrationId, true, null, unidentifiedAccessKey, false);
}
 
Example #22
Source File: SignalServiceMessageSender.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Send a message to a single recipient.
 *
 * @param recipient The message's destination.
 * @param message The message.
 * @throws UntrustedIdentityException
 * @throws IOException
 */
public SendMessageResult sendMessage(SignalServiceAddress             recipient,
                                     Optional<UnidentifiedAccessPair> unidentifiedAccess,
                                     SignalServiceDataMessage         message)
    throws UntrustedIdentityException, IOException
{
  byte[]            content   = createMessageContent(message);
  long              timestamp = message.getTimestamp();
  SendMessageResult result    = sendMessage(recipient, getTargetUnidentifiedAccess(unidentifiedAccess), timestamp, content, false);

  if (result.getSuccess() != null && result.getSuccess().isNeedsSync()) {
    byte[] syncMessage = createMultiDeviceSentTranscriptContent(content, Optional.of(recipient), timestamp, Collections.singletonList(result), false);
    sendMessage(localAddress, Optional.<UnidentifiedAccess>absent(), timestamp, syncMessage, false);
  }

  if (message.isEndSession()) {
    if (recipient.getUuid().isPresent()) {
      store.deleteAllSessions(recipient.getUuid().get().toString());
    }
    if (recipient.getNumber().isPresent()) {
      store.deleteAllSessions(recipient.getNumber().get());
    }

    if (eventListener.isPresent()) {
      eventListener.get().onSecurityEvent(recipient);
    }
  }

  return result;
}
 
Example #23
Source File: PushServiceSocket.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public SignalServiceProfile retrieveProfile(SignalServiceAddress target, Optional<UnidentifiedAccess> unidentifiedAccess)
    throws NonSuccessfulResponseCodeException, PushNetworkException
{
  try {
    String response = makeServiceRequest(String.format(PROFILE_PATH, target.getIdentifier()), "GET", null, NO_HEADERS, unidentifiedAccess);
    return JsonUtil.fromJson(response, SignalServiceProfile.class);
  } catch (IOException e) {
    Log.w(TAG, e);
    throw new NonSuccessfulResponseCodeException("Unable to parse entity");
  }
}
 
Example #24
Source File: SignalServiceMessageSender.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
private Optional<UnidentifiedAccess> getTargetUnidentifiedAccess(Optional<UnidentifiedAccessPair> unidentifiedAccess) {
  if (unidentifiedAccess.isPresent()) {
    return unidentifiedAccess.get().getTargetUnidentifiedAccess();
  }

  return Optional.absent();
}
 
Example #25
Source File: SignalServiceMessageSender.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
private List<Optional<UnidentifiedAccess>> getTargetUnidentifiedAccess(List<Optional<UnidentifiedAccessPair>> unidentifiedAccess) {
  List<Optional<UnidentifiedAccess>> results = new LinkedList<>();

  for (Optional<UnidentifiedAccessPair> item : unidentifiedAccess) {
    if (item.isPresent()) results.add(item.get().getTargetUnidentifiedAccess());
    else                  results.add(Optional.<UnidentifiedAccess>absent());
  }

  return results;
}
 
Example #26
Source File: SignalServiceMessagePipe.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public SignalServiceProfile getProfile(SignalServiceAddress address, Optional<UnidentifiedAccess> unidentifiedAccess) throws IOException {
  try {
    List<String> headers = new LinkedList<>();

    if (unidentifiedAccess.isPresent()) {
      headers.add("Unidentified-Access-Key:" + Base64.encodeBytes(unidentifiedAccess.get().getUnidentifiedAccessKey()));
    }

    WebSocketRequestMessage requestMessage = WebSocketRequestMessage.newBuilder()
                                                                    .setId(SecureRandom.getInstance("SHA1PRNG").nextLong())
                                                                    .setVerb("GET")
                                                                    .setPath(String.format("/v1/profile/%s", address.getIdentifier()))
                                                                    .addAllHeaders(headers)
                                                                    .build();

    Pair<Integer, String> response = websocket.sendRequest(requestMessage).get(10, TimeUnit.SECONDS);

    if (response.first() < 200 || response.first() >= 300) {
      throw new IOException("Non-successful response: " + response.first());
    }

    return JsonUtil.fromJson(response.second(), SignalServiceProfile.class);
  } catch (NoSuchAlgorithmException nsae) {
    throw new AssertionError(nsae);
  } catch (InterruptedException | ExecutionException | TimeoutException e) {
    throw new IOException(e);
  }
}
 
Example #27
Source File: PushServiceSocket.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public SendMessageResponse sendMessage(OutgoingPushMessageList bundle, Optional<UnidentifiedAccess> unidentifiedAccess)
    throws IOException
{
  try {
    String responseText = makeServiceRequest(String.format(MESSAGE_PATH, bundle.getDestination()), "PUT", JsonUtil.toJson(bundle), NO_HEADERS, unidentifiedAccess);

    if (responseText == null) return new SendMessageResponse(false);
    else                      return JsonUtil.fromJson(responseText, SendMessageResponse.class);
  } catch (NotFoundException nfe) {
    throw new UnregisteredUserException(bundle.getDestination(), nfe);
  }
}
 
Example #28
Source File: PushServiceSocket.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public SendMessageResponse sendMessage(OutgoingPushMessageList bundle, Optional<UnidentifiedAccess> unidentifiedAccess)
    throws IOException
{
  try {
    String responseText = makeServiceRequest(String.format(MESSAGE_PATH, bundle.getDestination()), "PUT", JsonUtil.toJson(bundle), NO_HEADERS, unidentifiedAccess);

    if (responseText == null) return new SendMessageResponse(false);
    else                      return JsonUtil.fromJson(responseText, SendMessageResponse.class);
  } catch (NotFoundException nfe) {
    throw new UnregisteredUserException(bundle.getDestination(), nfe);
  }
}
 
Example #29
Source File: SignalServiceMessageSender.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public void sendMessage(SignalServiceSyncMessage message, Optional<UnidentifiedAccessPair> unidentifiedAccess)
    throws IOException, UntrustedIdentityException
{
  byte[] content;

  if (message.getContacts().isPresent()) {
    content = createMultiDeviceContactsContent(message.getContacts().get().getContactsStream().asStream(),
                                               message.getContacts().get().isComplete());
  } else if (message.getGroups().isPresent()) {
    content = createMultiDeviceGroupsContent(message.getGroups().get().asStream());
  } else if (message.getRead().isPresent()) {
    content = createMultiDeviceReadContent(message.getRead().get());
  } else if (message.getViewOnceOpen().isPresent()) {
    content = createMultiDeviceViewOnceOpenContent(message.getViewOnceOpen().get());
  } else if (message.getBlockedList().isPresent()) {
    content = createMultiDeviceBlockedContent(message.getBlockedList().get());
  } else if (message.getConfiguration().isPresent()) {
    content = createMultiDeviceConfigurationContent(message.getConfiguration().get());
  } else if (message.getSent().isPresent()) {
    content = createMultiDeviceSentTranscriptContent(message.getSent().get(), unidentifiedAccess);
  } else if (message.getStickerPackOperations().isPresent()) {
    content = createMultiDeviceStickerPackOperationContent(message.getStickerPackOperations().get());
  } else if (message.getFetchType().isPresent()) {
    content = createMultiDeviceFetchTypeContent(message.getFetchType().get());
  } else if (message.getMessageRequestResponse().isPresent()) {
    content = createMultiDeviceMessageRequestResponseContent(message.getMessageRequestResponse().get());
  } else if (message.getKeys().isPresent()) {
    content = createMultiDeviceSyncKeysContent(message.getKeys().get());
  } else if (message.getVerified().isPresent()) {
    sendMessage(message.getVerified().get(), unidentifiedAccess);
    return;
  } else {
    throw new IOException("Unsupported sync message!");
  }

  long timestamp = message.getSent().isPresent() ? message.getSent().get().getTimestamp()
                                                 : System.currentTimeMillis();

  sendMessage(localAddress, Optional.<UnidentifiedAccess>absent(), timestamp, content, false);
}
 
Example #30
Source File: SignalServiceMessageSender.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private Optional<UnidentifiedAccess> getTargetUnidentifiedAccess(Optional<UnidentifiedAccessPair> unidentifiedAccess) {
  if (unidentifiedAccess.isPresent()) {
    return unidentifiedAccess.get().getTargetUnidentifiedAccess();
  }

  return Optional.absent();
}