Java Code Examples for org.whispersystems.libsignal.state.SessionRecord

The following examples show how to use org.whispersystems.libsignal.state.SessionRecord. These examples are extracted from open source projects. 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 Project: mollyim-android   Author: mollyim   File: SessionDatabase.java    License: GNU General Public License v3.0 6 votes vote down vote up
public @Nullable SessionRecord load(@NonNull RecipientId recipientId, int deviceId) {
  SQLiteDatabase database = databaseHelper.getReadableDatabase();

  try (Cursor cursor = database.query(TABLE_NAME, new String[]{RECORD},
                                      RECIPIENT_ID + " = ? AND " + DEVICE + " = ?",
                                      new String[] {recipientId.serialize(), String.valueOf(deviceId)},
                                      null, null, null))
  {
    if (cursor != null && cursor.moveToFirst()) {
      try {
        return new SessionRecord(cursor.getBlob(cursor.getColumnIndexOrThrow(RECORD)));
      } catch (IOException e) {
        Log.w(TAG, e);
      }
    }
  }

  return null;
}
 
Example #2
Source Project: mollyim-android   Author: mollyim   File: SessionDatabase.java    License: GNU General Public License v3.0 6 votes vote down vote up
public @NonNull List<SessionRow> getAllFor(@NonNull RecipientId recipientId) {
  SQLiteDatabase   database = databaseHelper.getReadableDatabase();
  List<SessionRow> results  = new LinkedList<>();

  try (Cursor cursor = database.query(TABLE_NAME, null,
                                      RECIPIENT_ID + " = ?",
                                      new String[] {recipientId.serialize()},
                                      null, null, null))
  {
    while (cursor != null && cursor.moveToNext()) {
      try {
        results.add(new SessionRow(recipientId,
                                   cursor.getInt(cursor.getColumnIndexOrThrow(DEVICE)),
                                   new SessionRecord(cursor.getBlob(cursor.getColumnIndexOrThrow(RECORD)))));
      } catch (IOException e) {
        Log.w(TAG, e);
      }
    }
  }

  return results;
}
 
Example #3
Source Project: mollyim-android   Author: mollyim   File: SessionDatabase.java    License: GNU General Public License v3.0 6 votes vote down vote up
public @NonNull List<SessionRow> getAll() {
  SQLiteDatabase   database = databaseHelper.getReadableDatabase();
  List<SessionRow> results  = new LinkedList<>();

  try (Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null)) {
    while (cursor != null && cursor.moveToNext()) {
      try {
        results.add(new SessionRow(RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(RECIPIENT_ID))),
                                   cursor.getInt(cursor.getColumnIndexOrThrow(DEVICE)),
                                   new SessionRecord(cursor.getBlob(cursor.getColumnIndexOrThrow(RECORD)))));
      } catch (IOException e) {
        Log.w(TAG, e);
      }
    }
  }

  return results;
}
 
Example #4
Source Project: mollyim-android   Author: mollyim   File: IdentityUtil.java    License: 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 #5
Source Project: bcm-android   Author: bcmapp   File: IdentityUtil.java    License: 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 #6
Source Project: bcm-android   Author: bcmapp   File: IdentityUtil.java    License: 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 #7
Source Project: bcm-android   Author: bcmapp   File: SessionUtil.java    License: 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 #8
Source Project: bcm-android   Author: bcmapp   File: TextSecureSessionStore.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public void storeSession(@NonNull SignalProtocolAddress address, @NonNull SessionRecord record) {
    synchronized (FILE_LOCK) {
        try {
            RandomAccessFile sessionFile = new RandomAccessFile(getSessionFile(address), "rw");
            FileChannel out = sessionFile.getChannel();

            out.position(0);
            writeInteger(CURRENT_VERSION, out);
            writeBlob(record.serialize(), out);
            out.truncate(out.position());

            sessionFile.close();
        } catch (IOException e) {
            throw new AssertionError(e);
        }
    }
}
 
Example #9
Source Project: bcm-android   Author: bcmapp   File: TextSecureSessionStore.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void migrateSessions() {
    synchronized (FILE_LOCK) {
        File directory = getSessionDirectory();

        for (File session : directory.listFiles()) {
            if (session.isFile()) {
                SignalProtocolAddress address = getAddressName(session);

                if (address != null) {
                    SessionRecord sessionRecord = loadSession(address);
                    storeSession(address, sessionRecord);
                }
            }
        }
    }
}
 
Example #10
Source Project: bcm-android   Author: bcmapp   File: TextSecureSessionStore.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void archiveAllSessions() {
    synchronized (FILE_LOCK) {
        File directory = getSessionDirectory();

        for (File session : directory.listFiles()) {
            if (session.isFile()) {
                SignalProtocolAddress address = getAddressName(session);

                if (address != null) {
                    SessionRecord sessionRecord = loadSession(address);
                    sessionRecord.archiveCurrentState();
                    storeSession(address, sessionRecord);
                }
            }
        }
    }
}
 
Example #11
Source Project: Silence   Author: SilenceIM   File: VerifyIdentityActivity.java    License: 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 #12
Source Project: Silence   Author: SilenceIM   File: SessionBuilder.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Initiate a new session by sending an initial KeyExchangeMessage to the recipient.
 *
 * @return the KeyExchangeMessage to deliver.
 */
public KeyExchangeMessage process() {
  synchronized (SessionCipher.SESSION_LOCK) {
    try {
      int             sequence         = KeyHelper.getRandomSequence(65534) + 1;
      int             flags            = KeyExchangeMessage.INITIATE_FLAG;
      ECKeyPair       baseKey          = Curve.generateKeyPair();
      ECKeyPair       ratchetKey       = Curve.generateKeyPair();
      IdentityKeyPair identityKey      = identityKeyStore.getIdentityKeyPair();
      byte[]          baseKeySignature = Curve.calculateSignature(identityKey.getPrivateKey(), baseKey.getPublicKey().serialize());
      SessionRecord   sessionRecord    = sessionStore.loadSession(remoteAddress);

      sessionRecord.getSessionState().setPendingKeyExchange(sequence, baseKey, ratchetKey, identityKey);
      sessionStore.storeSession(remoteAddress, sessionRecord);

      return new KeyExchangeMessage(CiphertextMessage.CURRENT_VERSION,
                                    sequence, flags, baseKey.getPublicKey(), baseKeySignature,
                                    ratchetKey.getPublicKey(), identityKey.getPublicKey());
    } catch (InvalidKeyException e) {
      throw new AssertionError(e);
    }
  }
}
 
Example #13
Source Project: Silence   Author: SilenceIM   File: SilenceSessionStore.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public void storeSession(SignalProtocolAddress address, SessionRecord record) {
  synchronized (FILE_LOCK) {
    try {
      MasterCipher     masterCipher = new MasterCipher(masterSecret);
      RandomAccessFile sessionFile  = new RandomAccessFile(getSessionFile(address), "rw");
      FileChannel      out          = sessionFile.getChannel();

      out.position(0);
      writeInteger(CURRENT_VERSION, out);
      writeBlob(masterCipher.encryptBytes(record.serialize()), out);
      out.truncate(out.position());

      sessionFile.close();
    } catch (IOException e) {
      throw new AssertionError(e);
    }
  }
}
 
Example #14
Source Project: libsignal-protocol-java   Author: signalapp   File: SessionBuilder.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Build a new session from a received {@link PreKeySignalMessage}.
 *
 * After a session is constructed in this way, the embedded {@link SignalMessage}
 * can be decrypted.
 *
 * @param message The received {@link PreKeySignalMessage}.
 * @throws org.whispersystems.libsignal.InvalidKeyIdException when there is no local
 *                                                             {@link org.whispersystems.libsignal.state.PreKeyRecord}
 *                                                             that corresponds to the PreKey ID in
 *                                                             the message.
 * @throws org.whispersystems.libsignal.InvalidKeyException when the message is formatted incorrectly.
 * @throws org.whispersystems.libsignal.UntrustedIdentityException when the {@link IdentityKey} of the sender is untrusted.
 */
/*package*/ Optional<Integer> process(SessionRecord sessionRecord, PreKeySignalMessage message)
    throws InvalidKeyIdException, InvalidKeyException, UntrustedIdentityException
{
  IdentityKey theirIdentityKey = message.getIdentityKey();

  if (!identityKeyStore.isTrustedIdentity(remoteAddress, theirIdentityKey, IdentityKeyStore.Direction.RECEIVING)) {
    throw new UntrustedIdentityException(remoteAddress.getName(), theirIdentityKey);
  }

  Optional<Integer> unsignedPreKeyId = processV3(sessionRecord, message);

  identityKeyStore.saveIdentity(remoteAddress, theirIdentityKey);

  return unsignedPreKeyId;
}
 
Example #15
Source Project: mollyim-android   Author: mollyim   File: SessionDatabase.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void store(@NonNull RecipientId recipientId, int deviceId, @NonNull SessionRecord record) {
  SQLiteDatabase database = databaseHelper.getWritableDatabase();

  ContentValues values = new ContentValues();
  values.put(RECIPIENT_ID, recipientId.serialize());
  values.put(DEVICE, deviceId);
  values.put(RECORD, record.serialize());

  database.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
}
 
Example #16
Source Project: mollyim-android   Author: mollyim   File: TextSecureSessionStore.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public SessionRecord loadSession(@NonNull SignalProtocolAddress address) {
  synchronized (FILE_LOCK) {
    RecipientId   recipientId   = Recipient.external(context, address.getName()).getId();
    SessionRecord sessionRecord = DatabaseFactory.getSessionDatabase(context).load(recipientId, address.getDeviceId());

    if (sessionRecord == null) {
      Log.w(TAG, "No existing session information found.");
      return new SessionRecord();
    }

    return sessionRecord;
  }
}
 
Example #17
Source Project: mollyim-android   Author: mollyim   File: TextSecureSessionStore.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void storeSession(@NonNull SignalProtocolAddress address, @NonNull SessionRecord record) {
  synchronized (FILE_LOCK) {
    RecipientId id = Recipient.external(context, address.getName()).getId();
    DatabaseFactory.getSessionDatabase(context).store(id, address.getDeviceId(), record);
  }
}
 
Example #18
Source Project: mollyim-android   Author: mollyim   File: TextSecureSessionStore.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean containsSession(SignalProtocolAddress address) {
  synchronized (FILE_LOCK) {
    if (DatabaseFactory.getRecipientDatabase(context).containsPhoneOrUuid(address.getName())) {
      RecipientId   recipientId   = Recipient.external(context, address.getName()).getId();
      SessionRecord sessionRecord = DatabaseFactory.getSessionDatabase(context).load(recipientId, address.getDeviceId());

      return sessionRecord != null &&
             sessionRecord.getSessionState().hasSenderChain() &&
             sessionRecord.getSessionState().getSessionVersion() == CiphertextMessage.CURRENT_VERSION;
    } else {
      return false;
    }
  }
}
 
Example #19
Source Project: bcm-android   Author: bcmapp   File: SignalServiceCipher.java    License: GNU General Public License v3.0 5 votes vote down vote up
private byte[] decrypt(SignalServiceProtos.Envelope envelope, byte[] ciphertext)
    throws InvalidVersionException, InvalidMessageException, InvalidKeyException,
           DuplicateMessageException, InvalidKeyIdException, UntrustedIdentityException,
           LegacyMessageException, NoSessionException
{
  SignalProtocolAddress sourceAddress = new SignalProtocolAddress(envelope.getSource(), envelope.getSourceDevice());
  SessionCipher         sessionCipher = new SessionCipher(signalProtocolStore, sourceAddress);

  byte[] paddedMessage;

  if (envelope.getType() == Type.PREKEY_BUNDLE) {
    paddedMessage = sessionCipher.decrypt(new PreKeySignalMessage(ciphertext));
      //纠正remote register id
      SessionRecord sessionRecord = signalProtocolStore.loadSession(sourceAddress);
      if (sessionRecord.getSessionState().getRemoteRegistrationId() == 0) {
          sessionRecord.getSessionState().setRemoteRegistrationId(envelope.getSourceRegistration());
          signalProtocolStore.storeSession(sourceAddress, sessionRecord);
      }

  } else if (envelope.getType() == Type.CIPHERTEXT) {
    paddedMessage = sessionCipher.decrypt(new SignalMessage(ciphertext));
  } else {
    throw new InvalidMessageException("Unknown type: " + envelope.getType());
  }

  PushTransportDetails transportDetails = new PushTransportDetails(sessionCipher.getSessionVersion());
  return transportDetails.getStrippedPaddingMessageBody(paddedMessage);
}
 
Example #20
Source Project: bcm-android   Author: bcmapp   File: TextSecureSessionStore.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public SessionRecord loadSession(@NonNull SignalProtocolAddress address) {
    synchronized (FILE_LOCK) {
        try {
            FileInputStream in = new FileInputStream(getSessionFile(address));
            int versionMarker = readInteger(in);

            if (versionMarker > CURRENT_VERSION) {
                throw new AssertionError("Unknown version: " + versionMarker);
            }

            byte[] serialized = readBlob(in);
            in.close();

            if (versionMarker < PLAINTEXT_VERSION && masterSecret != null) {
                serialized = new MasterCipher(masterSecret).decryptBytes(serialized);
            } else if (versionMarker < PLAINTEXT_VERSION) {
                throw new AssertionError("Session didn't get migrated: (" + versionMarker + "," + address + ")");
            }

            if (versionMarker == SINGLE_STATE_VERSION) {
                SessionStructure sessionStructure = SessionStructure.parseFrom(serialized);
                SessionState sessionState = new SessionState(sessionStructure);
                return new SessionRecord(sessionState);
            } else if (versionMarker >= ARCHIVE_STATES_VERSION) {
                return new SessionRecord(serialized);
            } else {
                throw new AssertionError("Unknown version: " + versionMarker);
            }
        } catch (InvalidMessageException | IOException e) {
            Log.w(TAG, "No existing session information found.");
            return new SessionRecord();
        }
    }
}
 
Example #21
Source Project: bcm-android   Author: bcmapp   File: TextSecureSessionStore.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean containsSession(SignalProtocolAddress address) {
    if (!getSessionFile(address).exists()) return false;

    SessionRecord sessionRecord = loadSession(address);

    return sessionRecord.getSessionState().hasSenderChain() &&
            sessionRecord.getSessionState().getSessionVersion() == CiphertextMessage.CURRENT_VERSION;
}
 
Example #22
Source Project: signald   Author: thefinn93   File: JsonSessionStore.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public synchronized SessionRecord loadSession(SignalProtocolAddress remoteAddress) {
    try {
        if (containsSession(remoteAddress)) {
            return new SessionRecord(sessions.get(remoteAddress));
        } else {
            return new SessionRecord();
        }
    } catch (IOException e) {
        throw new AssertionError(e);
    }
}
 
Example #23
Source Project: signald   Author: thefinn93   File: SessionStore.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public synchronized SessionRecord loadSession(SignalProtocolAddress remoteAddress) {
    try {
        if (containsSession(remoteAddress)) {
            return new SessionRecord(sessions.get(remoteAddress));
        } else {
            return new SessionRecord();
        }
    } catch (IOException e) {
        throw new AssertionError(e);
    }
}
 
Example #24
Source Project: signal-cli   Author: AsamK   File: JsonSessionStore.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public synchronized void storeSession(SignalProtocolAddress address, SessionRecord record) {
    SignalServiceAddress serviceAddress = resolveSignalServiceAddress(address.getName());
    for (SessionInfo info : sessions) {
        if (info.address.matches(serviceAddress) && info.deviceId == address.getDeviceId()) {
            if (!info.address.getUuid().isPresent() || !info.address.getNumber().isPresent()) {
                info.address = serviceAddress;
            }
            info.sessionRecord = record.serialize();
            return;
        }
    }

    sessions.add(new SessionInfo(serviceAddress, address.getDeviceId(), record.serialize()));
}
 
Example #25
Source Project: Pix-Art-Messenger   Author: kriztan   File: DatabaseBackend.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void storeSession(Account account, SignalProtocolAddress contact, SessionRecord session) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(SQLiteAxolotlStore.NAME, contact.getName());
    values.put(SQLiteAxolotlStore.DEVICE_ID, contact.getDeviceId());
    values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(session.serialize(), Base64.DEFAULT));
    values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
    db.insert(SQLiteAxolotlStore.SESSION_TABLENAME, null, values);
}
 
Example #26
Source Project: Silence   Author: SilenceIM   File: SessionBuilder.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Build a new session from a received {@link PreKeySignalMessage}.
 *
 * After a session is constructed in this way, the embedded {@link SignalMessage}
 * can be decrypted.
 *
 * @param message The received {@link PreKeySignalMessage}.
 * @throws org.whispersystems.libsignal.InvalidKeyIdException when there is no local
 *                                                             {@link org.whispersystems.libsignal.state.PreKeyRecord}
 *                                                             that corresponds to the PreKey ID in
 *                                                             the message.
 * @throws org.whispersystems.libsignal.InvalidKeyException when the message is formatted incorrectly.
 * @throws org.whispersystems.libsignal.UntrustedIdentityException when the {@link IdentityKey} of the sender is untrusted.
 */
/*package*/ Optional<Integer> process(SessionRecord sessionRecord, PreKeySignalMessage message)
    throws InvalidKeyIdException, InvalidKeyException, UntrustedIdentityException
{
  IdentityKey theirIdentityKey = message.getIdentityKey();

  if (!identityKeyStore.isTrustedIdentity(remoteAddress, theirIdentityKey, IdentityKeyStore.Direction.RECEIVING)) {
    throw new UntrustedIdentityException(remoteAddress.getName(), theirIdentityKey);
  }

  Optional<Integer> unsignedPreKeyId = processV3(sessionRecord, message);

  identityKeyStore.saveIdentity(remoteAddress, theirIdentityKey);
  return unsignedPreKeyId;
}
 
Example #27
Source Project: Silence   Author: SilenceIM   File: SessionBuilder.java    License: GNU General Public License v3.0 5 votes vote down vote up
private Optional<Integer> processV3(SessionRecord sessionRecord, PreKeySignalMessage message)
    throws UntrustedIdentityException, InvalidKeyIdException, InvalidKeyException
{

  if (sessionRecord.hasSessionState(message.getMessageVersion(), message.getBaseKey().serialize())) {
    Log.w(TAG, "We've already setup a session for this V3 message, letting bundled message fall through...");
    return Optional.absent();
  }

  ECKeyPair ourSignedPreKey = signedPreKeyStore.loadSignedPreKey(message.getSignedPreKeyId()).getKeyPair();

  BobSignalProtocolParameters.Builder parameters = BobSignalProtocolParameters.newBuilder();

  parameters.setTheirBaseKey(message.getBaseKey())
            .setTheirIdentityKey(message.getIdentityKey())
            .setOurIdentityKey(identityKeyStore.getIdentityKeyPair())
            .setOurSignedPreKey(ourSignedPreKey)
            .setOurRatchetKey(ourSignedPreKey);

  if (message.getPreKeyId().isPresent()) {
    parameters.setOurOneTimePreKey(Optional.of(preKeyStore.loadPreKey(message.getPreKeyId().get()).getKeyPair()));
  } else {
    parameters.setOurOneTimePreKey(Optional.<ECKeyPair>absent());
  }

  if (!sessionRecord.isFresh()) sessionRecord.archiveCurrentState();

  RatchetingSession.initializeSession(sessionRecord.getSessionState(), parameters.create());

  sessionRecord.getSessionState().setLocalRegistrationId(identityKeyStore.getLocalRegistrationId());
  sessionRecord.getSessionState().setRemoteRegistrationId(message.getRegistrationId());
  sessionRecord.getSessionState().setAliceBaseKey(message.getBaseKey().serialize());

  if (message.getPreKeyId().isPresent() && message.getPreKeyId().get() != Medium.MAX_VALUE) {
    return message.getPreKeyId();
  } else {
    return Optional.absent();
  }
}
 
Example #28
Source Project: Silence   Author: SilenceIM   File: SessionBuilder.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void processResponse(KeyExchangeMessage message)
    throws StaleKeyExchangeException, InvalidKeyException
{
  SessionRecord sessionRecord                  = sessionStore.loadSession(remoteAddress);
  SessionState  sessionState                   = sessionRecord.getSessionState();
  boolean       hasPendingKeyExchange          = sessionState.hasPendingKeyExchange();
  boolean       isSimultaneousInitiateResponse = message.isResponseForSimultaneousInitiate();

  if (!hasPendingKeyExchange || sessionState.getPendingKeyExchangeSequence() != message.getSequence()) {
    Log.w(TAG, "No matching sequence for response. Is simultaneous initiate response: " + isSimultaneousInitiateResponse);
    if (!isSimultaneousInitiateResponse) throw new StaleKeyExchangeException();
    else                                 return;
  }

  SymmetricSignalProtocolParameters.Builder parameters = SymmetricSignalProtocolParameters.newBuilder();

  parameters.setOurBaseKey(sessionRecord.getSessionState().getPendingKeyExchangeBaseKey())
            .setOurRatchetKey(sessionRecord.getSessionState().getPendingKeyExchangeRatchetKey())
            .setOurIdentityKey(sessionRecord.getSessionState().getPendingKeyExchangeIdentityKey())
            .setTheirBaseKey(message.getBaseKey())
            .setTheirRatchetKey(message.getRatchetKey())
            .setTheirIdentityKey(message.getIdentityKey());

  if (!sessionRecord.isFresh()) sessionRecord.archiveCurrentState();

  RatchetingSession.initializeSession(sessionRecord.getSessionState(), parameters.create());

  if (!Curve.verifySignature(message.getIdentityKey().getPublicKey(),
                             message.getBaseKey().serialize(),
                             message.getBaseKeySignature()))
  {
    throw new InvalidKeyException("Base key signature doesn't match!");
  }

  identityKeyStore.saveIdentity(remoteAddress, message.getIdentityKey());
  sessionStore.storeSession(remoteAddress, sessionRecord);
}
 
Example #29
Source Project: Silence   Author: SilenceIM   File: KeyExchangeInitiator.java    License: 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 #30
Source Project: Silence   Author: SilenceIM   File: SilenceSessionStore.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public SessionRecord loadSession(SignalProtocolAddress address) {
  synchronized (FILE_LOCK) {
    try {
      MasterCipher    cipher = new MasterCipher(masterSecret);
      FileInputStream in     = new FileInputStream(getSessionFile(address));

      int versionMarker  = readInteger(in);

      if (versionMarker > CURRENT_VERSION) {
        throw new AssertionError("Unknown version: " + versionMarker);
      }

      byte[] serialized = cipher.decryptBytes(readBlob(in));
      in.close();

      if (versionMarker == SINGLE_STATE_VERSION) {
        SessionStructure sessionStructure = SessionStructure.parseFrom(serialized);
        SessionState     sessionState     = new SessionState(sessionStructure);
        return new SessionRecord(sessionState);
      } else if (versionMarker == ARCHIVE_STATES_VERSION) {
        return new SessionRecord(serialized);
      } else {
        throw new AssertionError("Unknown version: " + versionMarker);
      }
    } catch (InvalidMessageException | IOException e) {
      Log.w(TAG, "No existing session information found.");
      return new SessionRecord();
    }
  }
}