org.whispersystems.libsignal.state.SessionStore Java Examples

The following examples show how to use org.whispersystems.libsignal.state.SessionStore. 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: IdentityUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public static void saveIdentity(Context context, String user, IdentityKey identityKey) {
  synchronized (SESSION_LOCK) {
    IdentityKeyStore      identityKeyStore = new TextSecureIdentityKeyStore(context);
    SessionStore          sessionStore     = new TextSecureSessionStore(context);
    SignalProtocolAddress address          = new SignalProtocolAddress(user, 1);

    if (identityKeyStore.saveIdentity(address, identityKey)) {
      if (sessionStore.containsSession(address)) {
        SessionRecord sessionRecord = sessionStore.loadSession(address);
        sessionRecord.archiveCurrentState();

        sessionStore.storeSession(address, sessionRecord);
      }
    }
  }
}
 
Example #2
Source File: KeyExchangeInitiator.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
public static void initiateKeyExchange(Context context, MasterSecret masterSecret, Recipients recipients, int subscriptionId) {
  Recipient         recipient         = recipients.getPrimaryRecipient();
  SessionStore      sessionStore      = new SilenceSessionStore(context, masterSecret, subscriptionId);
  PreKeyStore       preKeyStore       = new SilencePreKeyStore(context, masterSecret, subscriptionId);
  SignedPreKeyStore signedPreKeyStore = new SilencePreKeyStore(context, masterSecret, subscriptionId);
  IdentityKeyStore  identityKeyStore  = new SilenceIdentityKeyStore(context, masterSecret, subscriptionId);

  SessionBuilder    sessionBuilder    = new SessionBuilder(sessionStore, preKeyStore, signedPreKeyStore,
                                                           identityKeyStore, new SignalProtocolAddress(recipient.getNumber(), 1));

  if (identityKeyStore.getIdentityKeyPair() != null) {
    KeyExchangeMessage         keyExchangeMessage = sessionBuilder.process();
    String                     serializedMessage  = Base64.encodeBytesWithoutPadding(keyExchangeMessage.serialize());
    OutgoingKeyExchangeMessage textMessage        = new OutgoingKeyExchangeMessage(recipients, serializedMessage, subscriptionId);

    MessageSender.send(context, masterSecret, textMessage, -1, false);
  } else {
    Toast.makeText(context, R.string.VerifyIdentityActivity_you_do_not_have_an_identity_key,
            Toast.LENGTH_LONG).show();
  }
}
 
Example #3
Source File: VerifyIdentityActivity.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
private @Nullable IdentityKey getRemoteIdentityKey(MasterSecret masterSecret, Recipient recipient) {
  int subscriptionId = SubscriptionManagerCompat.getDefaultMessagingSubscriptionId().or(-1);
  IdentityKeyParcelable identityKeyParcelable = getIntent().getParcelableExtra("remote_identity");

  if (identityKeyParcelable != null) {
    return identityKeyParcelable.get();
  }

  SessionStore   sessionStore   = new SilenceSessionStore(this, masterSecret, subscriptionId);
  SignalProtocolAddress axolotlAddress = new SignalProtocolAddress(recipient.getNumber(), 1);
  SessionRecord  record         = sessionStore.loadSession(axolotlAddress);

  if (record == null) {
    return null;
  }

  return record.getSessionState().getRemoteIdentityKey();
}
 
Example #4
Source File: SessionUtil.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
public static void archiveSiblingSessions(Context context, AccountContext accountContext, SignalProtocolAddress address) {
    SessionStore sessionStore = new TextSecureSessionStore(context, accountContext);
    List<Integer> devices = sessionStore.getSubDeviceSessions(address.getName());
    devices.add(1);

    for (int device : devices) {
        if (device != address.getDeviceId()) {
            SignalProtocolAddress sibling = new SignalProtocolAddress(address.getName(), device);

            if (sessionStore.containsSession(sibling)) {
                SessionRecord sessionRecord = sessionStore.loadSession(sibling);
                sessionRecord.archiveCurrentState();
                sessionStore.storeSession(sibling, sessionRecord);
            }
        }
    }
}
 
Example #5
Source File: IdentityUtil.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
public static void saveIdentity(Context context, AccountContext accountContext, String number, IdentityKey identityKey) {
    synchronized (SESSION_LOCK) {
        IdentityKeyStore identityKeyStore = new TextSecureIdentityKeyStore(context, accountContext);
        SessionStore sessionStore = new TextSecureSessionStore(context, accountContext);
        SignalProtocolAddress address = new SignalProtocolAddress(number, 1);

        if (identityKeyStore.saveIdentity(address, identityKey)) {
            if (sessionStore.containsSession(address)) {
                SessionRecord sessionRecord = sessionStore.loadSession(address);
                sessionRecord.archiveCurrentState();

                sessionStore.storeSession(address, sessionRecord);
            }
        }
    }
}
 
Example #6
Source File: IdentityUtil.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
public static void saveIdentity(Context context, AccountContext accountContext, String number, IdentityKey identityKey, boolean nonBlockingApproval) {
    synchronized (SESSION_LOCK) {
        TextSecureIdentityKeyStore identityKeyStore = new TextSecureIdentityKeyStore(context, accountContext);
        SessionStore sessionStore = new TextSecureSessionStore(context, accountContext);
        SignalProtocolAddress address = new SignalProtocolAddress(number, 1);

        if (identityKeyStore.saveIdentity(address, identityKey, nonBlockingApproval)) {
            if (sessionStore.containsSession(address)) {
                SessionRecord sessionRecord = sessionStore.loadSession(address);
                sessionRecord.archiveCurrentState();

                sessionStore.storeSession(address, sessionRecord);
            }
        }
    }
}
 
Example #7
Source File: PushDecryptJob.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
private long handleSynchronizeSentEndSessionMessage(@NonNull SentTranscriptMessage message) {
    ALog.i(TAG, "handleSynchronizeSentEndSessionMessage");
    PrivateChatRepo chatRepo = repository.getChatRepo();
    Recipient recipient = getSyncMessageDestination(message);
    OutgoingTextMessage outgoingTextMessage = new OutgoingTextMessage(recipient, "", -1);
    OutgoingEndSessionMessage outgoingEndSessionMessage = new OutgoingEndSessionMessage(outgoingTextMessage);

    long threadId = repository.getThreadRepo().getThreadIdFor(recipient);

    if (!recipient.isGroupRecipient()) {
        SessionStore sessionStore = new TextSecureSessionStore(context, accountContext);
        sessionStore.deleteAllSessions(recipient.getAddress().serialize());

        SecurityEvent.broadcastSecurityUpdateEvent(context);

        chatRepo.insertOutgoingTextMessage(threadId, outgoingEndSessionMessage, message.getTimestamp(), null);
        chatRepo.setMessageSendSuccess(messageId);
    }

    return threadId;
}
 
Example #8
Source File: PushDecryptJob.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
private void handleEndSessionMessage(@NonNull SignalServiceProtos.Envelope envelope,
                                     @NonNull SignalServiceDataMessage message) {
    ALog.i(TAG, "handleEndSessionMessage");
    PrivateChatRepo chatRepo = repository.getChatRepo();
    IncomingTextMessage incomingTextMessage = new IncomingTextMessage(Address.from(accountContext, envelope.getSource()),
            envelope.getSourceDevice(),
            message.getTimestamp(),
            "", Optional.absent(), 0);

    long threadId;

    IncomingEndSessionMessage incomingEndSessionMessage = new IncomingEndSessionMessage(incomingTextMessage);
    kotlin.Pair<Long, Long> insertResult = chatRepo.insertIncomingTextMessage(incomingEndSessionMessage);

    if (insertResult.getSecond() > 0)
        threadId = insertResult.getFirst();
    else
        threadId = 0L;

    if (threadId > 0L) {
        SessionStore sessionStore = new TextSecureSessionStore(context, accountContext);
        sessionStore.deleteAllSessions(envelope.getSource());

        SecurityEvent.broadcastSecurityUpdateEvent(context);
    }
}
 
Example #9
Source File: PushProcessMessageJob.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private long handleSynchronizeSentEndSessionMessage(@NonNull SentTranscriptMessage message)
    throws BadGroupIdException
{
  SmsDatabase               database                  = DatabaseFactory.getSmsDatabase(context);
  Recipient                 recipient                 = getSyncMessageDestination(message);
  OutgoingTextMessage       outgoingTextMessage       = new OutgoingTextMessage(recipient, "", -1);
  OutgoingEndSessionMessage outgoingEndSessionMessage = new OutgoingEndSessionMessage(outgoingTextMessage);

  long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient);

  if (!recipient.isGroup()) {
    SessionStore sessionStore = new TextSecureSessionStore(context);
    sessionStore.deleteAllSessions(recipient.requireServiceId());

    SecurityEvent.broadcastSecurityUpdateEvent(context);

    long messageId = database.insertMessageOutbox(threadId, outgoingEndSessionMessage,
                                                  false, message.getTimestamp(),
                                                  null);
    database.markAsSent(messageId, true);
  }

  return threadId;
}
 
Example #10
Source File: PushProcessMessageJob.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private void handleEndSessionMessage(@NonNull SignalServiceContent content,
                                     @NonNull Optional<Long>       smsMessageId)
{
  SmsDatabase         smsDatabase         = DatabaseFactory.getSmsDatabase(context);
  IncomingTextMessage incomingTextMessage = new IncomingTextMessage(Recipient.externalPush(context, content.getSender()).getId(),
                                                                    content.getSenderDevice(),
                                                                    content.getTimestamp(),
                                                                    content.getServerTimestamp(),
                                                                    "", Optional.absent(), 0,
                                                                    content.isNeedsReceipt());

  Long threadId;

  if (!smsMessageId.isPresent()) {
    IncomingEndSessionMessage incomingEndSessionMessage = new IncomingEndSessionMessage(incomingTextMessage);
    Optional<InsertResult>    insertResult              = smsDatabase.insertMessageInbox(incomingEndSessionMessage);

    if (insertResult.isPresent()) threadId = insertResult.get().getThreadId();
    else                          threadId = null;
  } else {
    smsDatabase.markAsEndSession(smsMessageId.get());
    threadId = smsDatabase.getThreadIdForMessage(smsMessageId.get());
  }

  if (threadId != null) {
    SessionStore sessionStore = new TextSecureSessionStore(context);
    sessionStore.deleteAllSessions(content.getSender().getIdentifier());

    SecurityEvent.broadcastSecurityUpdateEvent(context);
    ApplicationDependencies.getMessageNotifier().updateNotification(context, threadId);
  }
}
 
Example #11
Source File: SessionBuilder.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a SessionBuilder.
 *
 * @param sessionStore The {@link org.whispersystems.libsignal.state.SessionStore} to store the constructed session in.
 * @param preKeyStore The {@link  org.whispersystems.libsignal.state.PreKeyStore} where the client's local {@link org.whispersystems.libsignal.state.PreKeyRecord}s are stored.
 * @param identityKeyStore The {@link org.whispersystems.libsignal.state.IdentityKeyStore} containing the client's identity key information.
 * @param remoteAddress The address of the remote user to build a session with.
 */
public SessionBuilder(SessionStore sessionStore,
                      PreKeyStore preKeyStore,
                      SignedPreKeyStore signedPreKeyStore,
                      IdentityKeyStore identityKeyStore,
                      SignalProtocolAddress remoteAddress)
{
  this.sessionStore      = sessionStore;
  this.preKeyStore       = preKeyStore;
  this.signedPreKeyStore = signedPreKeyStore;
  this.identityKeyStore  = identityKeyStore;
  this.remoteAddress     = remoteAddress;
}
 
Example #12
Source File: KeyExchangeInitiator.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
private static boolean hasInitiatedSession(Context context, MasterSecret masterSecret,
                                           Recipients recipients, int subscriptionId)
{
  Recipient     recipient     = recipients.getPrimaryRecipient();
  SessionStore  sessionStore  = new SilenceSessionStore(context, masterSecret, subscriptionId);
  SessionRecord sessionRecord = sessionStore.loadSession(new SignalProtocolAddress(recipient.getNumber(), 1));

  return sessionRecord.getSessionState().hasPendingKeyExchange();
}
 
Example #13
Source File: SmsSentJob.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
private void handleSentResult(MasterSecret masterSecret, long messageId, int result) {
  try {
    EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
    SmsMessageRecord      record   = database.getMessage(masterSecret, messageId);

    switch (result) {
      case Activity.RESULT_OK:
        database.markAsSent(messageId, record.isSecure());

        if (record != null && record.isEndSession()) {
          Log.w(TAG, "Ending session...");
          SessionStore sessionStore = new SilenceSessionStore(context, masterSecret, record.getSubscriptionId());
          sessionStore.deleteAllSessions(record.getIndividualRecipient().getNumber());
          SecurityEvent.broadcastSecurityUpdateEvent(context, record.getThreadId());
        }

        break;
      case SmsManager.RESULT_ERROR_NO_SERVICE:
      case SmsManager.RESULT_ERROR_RADIO_OFF:
        Log.w(TAG, "Service connectivity problem, requeuing...");
        ApplicationContext.getInstance(context)
            .getJobManager()
            .add(new SmsSendJob(context, messageId, record.getIndividualRecipient().getNumber()));

        break;
      default:
        database.markAsSentFailed(messageId);
        MessageNotifier.notifyMessageDeliveryFailed(context, record.getRecipients(), record.getThreadId());
    }
  } catch (NoSuchMessageException e) {
    Log.w(TAG, e);
  }
}
 
Example #14
Source File: SessionBuilder.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a SessionBuilder.
 *
 * @param sessionStore The {@link org.whispersystems.libsignal.state.SessionStore} to store the constructed session in.
 * @param preKeyStore The {@link  org.whispersystems.libsignal.state.PreKeyStore} where the client's local {@link org.whispersystems.libsignal.state.PreKeyRecord}s are stored.
 * @param identityKeyStore The {@link org.whispersystems.libsignal.state.IdentityKeyStore} containing the client's identity key information.
 * @param remoteAddress The address of the remote user to build a session with.
 */
public SessionBuilder(SessionStore sessionStore,
                      PreKeyStore preKeyStore,
                      SignedPreKeyStore signedPreKeyStore,
                      IdentityKeyStore identityKeyStore,
                      SignalProtocolAddress remoteAddress)
{
  this.sessionStore      = sessionStore;
  this.preKeyStore       = preKeyStore;
  this.signedPreKeyStore = signedPreKeyStore;
  this.identityKeyStore  = identityKeyStore;
  this.remoteAddress     = remoteAddress;
}
 
Example #15
Source File: SessionCipher.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Construct a SessionCipher for encrypt/decrypt operations on a session.
 * In order to use SessionCipher, a session must have already been created
 * and stored using {@link SessionBuilder}.
 *
 * @param  sessionStore The {@link SessionStore} that contains a session for this recipient.
 * @param  remoteAddress  The remote address that messages will be encrypted to or decrypted from.
 */
public SessionCipher(SessionStore sessionStore, PreKeyStore preKeyStore,
                     SignedPreKeyStore signedPreKeyStore, IdentityKeyStore identityKeyStore,
                     SignalProtocolAddress remoteAddress)
{
  this.sessionStore     = sessionStore;
  this.preKeyStore      = preKeyStore;
  this.identityKeyStore = identityKeyStore;
  this.remoteAddress    = remoteAddress;
  this.sessionBuilder   = new SessionBuilder(sessionStore, preKeyStore, signedPreKeyStore,
                                             identityKeyStore, remoteAddress);
}
 
Example #16
Source File: SessionUtil.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
public static boolean hasSession(Context context, MasterSecret masterSecret, @NonNull Address address) {
    SessionStore sessionStore = new TextSecureSessionStore(context, address.context(), masterSecret);
    SignalProtocolAddress axolotlAddress = new SignalProtocolAddress(address.serialize(), SignalServiceAddress.DEFAULT_DEVICE_ID);

    return sessionStore.containsSession(axolotlAddress);
}
 
Example #17
Source File: SessionUtil.java    From Silence with GNU General Public License v3.0 4 votes vote down vote up
public static boolean hasSession(Context context, MasterSecret masterSecret, @NonNull String number, int subscriptionId) {
  SessionStore   sessionStore   = new SilenceSessionStore(context, masterSecret, subscriptionId);
  SignalProtocolAddress axolotlAddress = new SignalProtocolAddress(number, 1);

  return sessionStore.containsSession(axolotlAddress);
}
 
Example #18
Source File: SessionUtil.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public static boolean hasSession(@NonNull Context context, @NonNull RecipientId id) {
  SessionStore          sessionStore   = new TextSecureSessionStore(context);
  SignalProtocolAddress axolotlAddress = new SignalProtocolAddress(Recipient.resolved(id).requireServiceId(), SignalServiceAddress.DEFAULT_DEVICE_ID);

  return sessionStore.containsSession(axolotlAddress);
}