org.whispersystems.libsignal.state.SignalProtocolStore Java Examples

The following examples show how to use org.whispersystems.libsignal.state.SignalProtocolStore. 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
public SignalServiceMessageSender(SignalServiceConfiguration urls,
                                  CredentialsProvider credentialsProvider,
                                  SignalProtocolStore store,
                                  String signalAgent,
                                  boolean isMultiDevice,
                                  boolean attachmentsV3,
                                  Optional<SignalServiceMessagePipe> pipe,
                                  Optional<SignalServiceMessagePipe> unidentifiedPipe,
                                  Optional<EventListener> eventListener,
                                  ClientZkProfileOperations clientZkProfileOperations,
                                  ExecutorService executor)
{
  this.socket           = new PushServiceSocket(urls, credentialsProvider, signalAgent, clientZkProfileOperations);
  this.store            = store;
  this.localAddress     = new SignalServiceAddress(credentialsProvider.getUuid(), credentialsProvider.getE164());
  this.pipe             = new AtomicReference<>(pipe);
  this.unidentifiedPipe = new AtomicReference<>(unidentifiedPipe);
  this.isMultiDevice    = new AtomicBoolean(isMultiDevice);
  this.attachmentsV3    = new AtomicBoolean(attachmentsV3);
  this.eventListener    = eventListener;
  this.executor         = executor != null ? executor : Executors.newSingleThreadExecutor();
}
 
Example #2
Source File: SimultaneousInitiateTests.java    From libsignal-protocol-java with GNU General Public License v3.0 6 votes vote down vote up
private PreKeyBundle createBobPreKeyBundle(SignalProtocolStore bobStore) throws InvalidKeyException {
  ECKeyPair bobUnsignedPreKey   = Curve.generateKeyPair();
  int       bobUnsignedPreKeyId = new Random().nextInt(Medium.MAX_VALUE);
  byte[]    bobSignature        = Curve.calculateSignature(bobStore.getIdentityKeyPair().getPrivateKey(),
                                                           bobSignedPreKey.getPublicKey().serialize());

  PreKeyBundle bobPreKeyBundle = new PreKeyBundle(1, 1,
                                                  bobUnsignedPreKeyId, bobUnsignedPreKey.getPublicKey(),
                                                  bobSignedPreKeyId, bobSignedPreKey.getPublicKey(),
                                                  bobSignature, bobStore.getIdentityKeyPair().getPublicKey());

  bobStore.storeSignedPreKey(bobSignedPreKeyId, new SignedPreKeyRecord(bobSignedPreKeyId, System.currentTimeMillis(), bobSignedPreKey, bobSignature));
  bobStore.storePreKey(bobUnsignedPreKeyId, new PreKeyRecord(bobUnsignedPreKeyId, bobUnsignedPreKey));

  return bobPreKeyBundle;
}
 
Example #3
Source File: SimultaneousInitiateTests.java    From libsignal-protocol-java with GNU General Public License v3.0 6 votes vote down vote up
private PreKeyBundle createAlicePreKeyBundle(SignalProtocolStore aliceStore) throws InvalidKeyException {
  ECKeyPair aliceUnsignedPreKey   = Curve.generateKeyPair();
  int       aliceUnsignedPreKeyId = new Random().nextInt(Medium.MAX_VALUE);
  byte[]    aliceSignature        = Curve.calculateSignature(aliceStore.getIdentityKeyPair().getPrivateKey(),
                                                             aliceSignedPreKey.getPublicKey().serialize());

  PreKeyBundle alicePreKeyBundle = new PreKeyBundle(1, 1,
                                                    aliceUnsignedPreKeyId, aliceUnsignedPreKey.getPublicKey(),
                                                    aliceSignedPreKeyId, aliceSignedPreKey.getPublicKey(),
                                                    aliceSignature, aliceStore.getIdentityKeyPair().getPublicKey());

  aliceStore.storeSignedPreKey(aliceSignedPreKeyId, new SignedPreKeyRecord(aliceSignedPreKeyId, System.currentTimeMillis(), aliceSignedPreKey, aliceSignature));
  aliceStore.storePreKey(aliceUnsignedPreKeyId, new PreKeyRecord(aliceUnsignedPreKeyId, aliceUnsignedPreKey));

  return alicePreKeyBundle;
}
 
Example #4
Source File: SessionBuilderTest.java    From libsignal-protocol-java with GNU General Public License v3.0 6 votes vote down vote up
public void testBasicPreKeyV2()
    throws InvalidKeyException, InvalidVersionException, InvalidMessageException, InvalidKeyIdException, DuplicateMessageException, LegacyMessageException, UntrustedIdentityException, NoSessionException {
  SignalProtocolStore aliceStore          = new TestInMemorySignalProtocolStore();
  SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS);

  SignalProtocolStore bobStore      = new TestInMemorySignalProtocolStore();
  ECKeyPair    bobPreKeyPair = Curve.generateKeyPair();
  PreKeyBundle bobPreKey     = new PreKeyBundle(bobStore.getLocalRegistrationId(), 1,
                                                31337, bobPreKeyPair.getPublicKey(),
                                                0, null, null,
                                                bobStore.getIdentityKeyPair().getPublicKey());

  try {
    aliceSessionBuilder.process(bobPreKey);
    throw new AssertionError("Should fail with missing unsigned prekey!");
  } catch (InvalidKeyException e) {
    // Good!
    return;
  }
}
 
Example #5
Source File: SignalServiceMessageSender.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
public SignalServiceMessageSender(SignalServiceConfiguration urls,
                                  CredentialsProvider credentialsProvider,
                                  SignalProtocolStore store,
                                  String userAgent,
                                  boolean isMultiDevice,
                                  Optional<SignalServiceMessagePipe> pipe,
                                  Optional<SignalServiceMessagePipe> unidentifiedPipe,
                                  Optional<EventListener> eventListener)
{
  this.socket           = new PushServiceSocket(urls, credentialsProvider, userAgent);
  this.store            = store;
  this.localAddress     = new SignalServiceAddress(credentialsProvider.getUuid(), credentialsProvider.getE164());
  this.pipe             = new AtomicReference<>(pipe);
  this.unidentifiedPipe = new AtomicReference<>(unidentifiedPipe);
  this.isMultiDevice    = new AtomicBoolean(isMultiDevice);
  this.eventListener    = eventListener;
}
 
Example #6
Source File: SessionBuilderTest.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
public void testRepeatBundleMessageV2() throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException, InvalidMessageException, InvalidKeyIdException, DuplicateMessageException, LegacyMessageException, NoSessionException {
  SignalProtocolStore aliceStore          = new TestInMemorySignalProtocolStore();
  SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS);

  SignalProtocolStore bobStore = new TestInMemorySignalProtocolStore();

  ECKeyPair bobPreKeyPair            = Curve.generateKeyPair();
  ECKeyPair bobSignedPreKeyPair      = Curve.generateKeyPair();
  byte[]    bobSignedPreKeySignature = Curve.calculateSignature(bobStore.getIdentityKeyPair().getPrivateKey(),
                                                                bobSignedPreKeyPair.getPublicKey().serialize());

  PreKeyBundle bobPreKey = new PreKeyBundle(bobStore.getLocalRegistrationId(), 1,
                                            31337, bobPreKeyPair.getPublicKey(),
                                            0, null, null,
                                            bobStore.getIdentityKeyPair().getPublicKey());

  bobStore.storePreKey(31337, new PreKeyRecord(bobPreKey.getPreKeyId(), bobPreKeyPair));
  bobStore.storeSignedPreKey(22, new SignedPreKeyRecord(22, System.currentTimeMillis(), bobSignedPreKeyPair, bobSignedPreKeySignature));

  try {
    aliceSessionBuilder.process(bobPreKey);
    throw new AssertionError("Should fail with missing signed prekey!");
  } catch (InvalidKeyException e) {
    // Good!
    return;
  }
}
 
Example #7
Source File: SignalServiceMessageSender.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Construct a SignalServiceMessageSender.
 *
 * @param urls The URL of the Signal Service.
 * @param uuid The Signal Service UUID.
 * @param e164 The Signal Service phone number.
 * @param password The Signal Service user password.
 * @param store The SignalProtocolStore.
 * @param eventListener An optional event listener, which fires whenever sessions are
 *                      setup or torn down for a recipient.
 */
public SignalServiceMessageSender(SignalServiceConfiguration urls,
                                  UUID uuid, String e164, String password,
                                  SignalProtocolStore store,
                                  String signalAgent,
                                  boolean isMultiDevice,
                                  boolean attachmentsV3,
                                  Optional<SignalServiceMessagePipe> pipe,
                                  Optional<SignalServiceMessagePipe> unidentifiedPipe,
                                  Optional<EventListener> eventListener,
                                  ClientZkProfileOperations clientZkProfileOperations,
                                  ExecutorService executor)
{
  this(urls, new StaticCredentialsProvider(uuid, e164, password, null), store, signalAgent, isMultiDevice, attachmentsV3, pipe, unidentifiedPipe, eventListener, clientZkProfileOperations, executor);
}
 
Example #8
Source File: SignalServiceCipher.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public SignalServiceCipher(SignalServiceAddress localAddress,
                           SignalProtocolStore signalProtocolStore,
                           CertificateValidator certificateValidator)
{
  this.signalProtocolStore  = signalProtocolStore;
  this.localAddress         = localAddress;
  this.certificateValidator = certificateValidator;
}
 
Example #9
Source File: SignalServiceCipher.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private static SignalProtocolAddress getPreferredProtocolAddress(SignalProtocolStore store, SignalServiceAddress address, int sourceDevice) {
  SignalProtocolAddress uuidAddress = address.getUuid().isPresent() ? new SignalProtocolAddress(address.getUuid().get().toString(), sourceDevice) : null;
  SignalProtocolAddress e164Address = address.getNumber().isPresent() ? new SignalProtocolAddress(address.getNumber().get(), sourceDevice) : null;

  if (uuidAddress != null && store.containsSession(uuidAddress)) {
    return uuidAddress;
  } else if (e164Address != null && store.containsSession(e164Address)) {
    return e164Address;
  } else {
    return new SignalProtocolAddress(address.getLegacyIdentifier(), sourceDevice);
  }
}
 
Example #10
Source File: SessionCipherTest.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
public void testMessageKeyLimits() throws Exception {
  SessionRecord aliceSessionRecord = new SessionRecord();
  SessionRecord bobSessionRecord   = new SessionRecord();

  initializeSessionsV3(aliceSessionRecord.getSessionState(), bobSessionRecord.getSessionState());

  SignalProtocolStore aliceStore = new TestInMemorySignalProtocolStore();
  SignalProtocolStore bobStore   = new TestInMemorySignalProtocolStore();

  aliceStore.storeSession(new SignalProtocolAddress("+14159999999", 1), aliceSessionRecord);
  bobStore.storeSession(new SignalProtocolAddress("+14158888888", 1), bobSessionRecord);

  SessionCipher     aliceCipher    = new SessionCipher(aliceStore, new SignalProtocolAddress("+14159999999", 1));
  SessionCipher     bobCipher      = new SessionCipher(bobStore, new SignalProtocolAddress("+14158888888", 1));

  List<CiphertextMessage> inflight = new LinkedList<>();

  for (int i=0;i<2010;i++) {
    inflight.add(aliceCipher.encrypt("you've never been so hungry, you've never been so cold".getBytes()));
  }

  bobCipher.decrypt(new SignalMessage(inflight.get(1000).serialize()));
  bobCipher.decrypt(new SignalMessage(inflight.get(inflight.size()-1).serialize()));

  try {
    bobCipher.decrypt(new SignalMessage(inflight.get(0).serialize()));
    throw new AssertionError("Should have failed!");
  } catch (DuplicateMessageException dme) {
    // good
  }
}
 
Example #11
Source File: SignalServiceMessageSender.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Construct a SignalServiceMessageSender.
 *
 * @param urls The URL of the Signal Service.
 * @param uuid The Signal Service UUID.
 * @param e164 The Signal Service phone number.
 * @param password The Signal Service user password.
 * @param store The SignalProtocolStore.
 * @param eventListener An optional event listener, which fires whenever sessions are
 *                      setup or torn down for a recipient.
 */
public SignalServiceMessageSender(SignalServiceConfiguration urls,
                                  UUID uuid, String e164, String password,
                                  SignalProtocolStore store,
                                  String userAgent,
                                  boolean isMultiDevice,
                                  Optional<SignalServiceMessagePipe> pipe,
                                  Optional<SignalServiceMessagePipe> unidentifiedPipe,
                                  Optional<EventListener> eventListener)
{
  this(urls, new StaticCredentialsProvider(uuid, e164, password, null), store, userAgent, isMultiDevice, pipe, unidentifiedPipe, eventListener);
}
 
Example #12
Source File: SignalServiceCipher.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
private static SignalProtocolAddress getPreferredProtocolAddress(SignalProtocolStore store, SignalServiceAddress address, int sourceDevice) {
  SignalProtocolAddress uuidAddress = address.getUuid().isPresent() ? new SignalProtocolAddress(address.getUuid().get().toString(), sourceDevice) : null;
  SignalProtocolAddress e164Address = address.getNumber().isPresent() ? new SignalProtocolAddress(address.getNumber().get(), sourceDevice) : null;

  if (uuidAddress != null && store.containsSession(uuidAddress)) {
    return uuidAddress;
  } else if (e164Address != null && store.containsSession(e164Address)) {
    return e164Address;
  } else {
    return new SignalProtocolAddress(address.getIdentifier(), sourceDevice);
  }
}
 
Example #13
Source File: SignalServiceCipher.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public SignalServiceCipher(SignalServiceAddress localAddress,
                           SignalProtocolStore signalProtocolStore,
                           CertificateValidator certificateValidator)
{
  this.signalProtocolStore  = signalProtocolStore;
  this.localAddress         = localAddress;
  this.certificateValidator = certificateValidator;
}
 
Example #14
Source File: SessionBuilderTest.java    From libsignal-protocol-java with GNU General Public License v3.0 4 votes vote down vote up
public void testOptionalOneTimePreKey() throws Exception {
  SignalProtocolStore aliceStore          = new TestInMemorySignalProtocolStore();
  SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS);

  SignalProtocolStore bobStore = new TestInMemorySignalProtocolStore();

  ECKeyPair bobPreKeyPair            = Curve.generateKeyPair();
  ECKeyPair bobSignedPreKeyPair      = Curve.generateKeyPair();
  byte[]    bobSignedPreKeySignature = Curve.calculateSignature(bobStore.getIdentityKeyPair().getPrivateKey(),
                                                                bobSignedPreKeyPair.getPublicKey().serialize());

  PreKeyBundle bobPreKey = new PreKeyBundle(bobStore.getLocalRegistrationId(), 1,
                                            0, null,
                                            22, bobSignedPreKeyPair.getPublicKey(),
                                            bobSignedPreKeySignature,
                                            bobStore.getIdentityKeyPair().getPublicKey());

  aliceSessionBuilder.process(bobPreKey);

  assertTrue(aliceStore.containsSession(BOB_ADDRESS));
  assertTrue(aliceStore.loadSession(BOB_ADDRESS).getSessionState().getSessionVersion() == 3);

  String            originalMessage    = "L'homme est condamné à être libre";
  SessionCipher     aliceSessionCipher = new SessionCipher(aliceStore, BOB_ADDRESS);
  CiphertextMessage outgoingMessage    = aliceSessionCipher.encrypt(originalMessage.getBytes());

  assertTrue(outgoingMessage.getType() == CiphertextMessage.PREKEY_TYPE);

  PreKeySignalMessage incomingMessage = new PreKeySignalMessage(outgoingMessage.serialize());
  assertTrue(!incomingMessage.getPreKeyId().isPresent());

  bobStore.storePreKey(31337, new PreKeyRecord(bobPreKey.getPreKeyId(), bobPreKeyPair));
  bobStore.storeSignedPreKey(22, new SignedPreKeyRecord(22, System.currentTimeMillis(), bobSignedPreKeyPair, bobSignedPreKeySignature));

  SessionCipher bobSessionCipher = new SessionCipher(bobStore, ALICE_ADDRESS);
  byte[]        plaintext        = bobSessionCipher.decrypt(incomingMessage);

  assertTrue(bobStore.containsSession(ALICE_ADDRESS));
  assertTrue(bobStore.loadSession(ALICE_ADDRESS).getSessionState().getSessionVersion() == 3);
  assertTrue(bobStore.loadSession(ALICE_ADDRESS).getSessionState().getAliceBaseKey() != null);
  assertTrue(originalMessage.equals(new String(plaintext)));
}
 
Example #15
Source File: SignalServiceCipher.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
public SignalServiceCipher(SignalServiceAddress localAddress, SignalProtocolStore signalProtocolStore) {
  this.signalProtocolStore = signalProtocolStore;
  this.localAddress = localAddress;
}
 
Example #16
Source File: SimultaneousInitiateTests.java    From libsignal-protocol-java with GNU General Public License v3.0 4 votes vote down vote up
private boolean isSessionIdEqual(SignalProtocolStore aliceStore, SignalProtocolStore bobStore) {
  return Arrays.equals(aliceStore.loadSession(BOB_ADDRESS).getSessionState().getAliceBaseKey(),
                       bobStore.loadSession(ALICE_ADDRESS).getSessionState().getAliceBaseKey());
}
 
Example #17
Source File: SimultaneousInitiateTests.java    From libsignal-protocol-java with GNU General Public License v3.0 4 votes vote down vote up
public void testSimultaneousInitiateLostMessage()
      throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException,
      InvalidMessageException, DuplicateMessageException, LegacyMessageException,
      InvalidKeyIdException, NoSessionException
  {
    SignalProtocolStore aliceStore = new TestInMemorySignalProtocolStore();
    SignalProtocolStore bobStore   = new TestInMemorySignalProtocolStore();

    PreKeyBundle alicePreKeyBundle = createAlicePreKeyBundle(aliceStore);
    PreKeyBundle bobPreKeyBundle = createBobPreKeyBundle(bobStore);

    SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS);
    SessionBuilder bobSessionBuilder   = new SessionBuilder(bobStore, ALICE_ADDRESS);

    SessionCipher aliceSessionCipher = new SessionCipher(aliceStore, BOB_ADDRESS);
    SessionCipher bobSessionCipher = new SessionCipher(bobStore, ALICE_ADDRESS);

    aliceSessionBuilder.process(bobPreKeyBundle);
    bobSessionBuilder.process(alicePreKeyBundle);

    CiphertextMessage messageForBob   = aliceSessionCipher.encrypt("hey there".getBytes());
    CiphertextMessage messageForAlice = bobSessionCipher.encrypt("sample message".getBytes());

    assertTrue(messageForBob.getType() == CiphertextMessage.PREKEY_TYPE);
    assertTrue(messageForAlice.getType() == CiphertextMessage.PREKEY_TYPE);

    assertFalse(isSessionIdEqual(aliceStore, bobStore));

    byte[] alicePlaintext = aliceSessionCipher.decrypt(new PreKeySignalMessage(messageForAlice.serialize()));
    byte[] bobPlaintext   = bobSessionCipher.decrypt(new PreKeySignalMessage(messageForBob.serialize()));

    assertTrue(new String(alicePlaintext).equals("sample message"));
    assertTrue(new String(bobPlaintext).equals("hey there"));

    assertTrue(aliceStore.loadSession(BOB_ADDRESS).getSessionState().getSessionVersion() == 3);
    assertTrue(bobStore.loadSession(ALICE_ADDRESS).getSessionState().getSessionVersion() == 3);

    assertFalse(isSessionIdEqual(aliceStore, bobStore));

    CiphertextMessage aliceResponse = aliceSessionCipher.encrypt("second message".getBytes());

    assertTrue(aliceResponse.getType() == CiphertextMessage.WHISPER_TYPE);

//    byte[] responsePlaintext = bobSessionCipher.decrypt(new WhisperMessage(aliceResponse.serialize()));
//
//    assertTrue(new String(responsePlaintext).equals("second message"));
//    assertTrue(isSessionIdEqual(aliceStore, bobStore));
    assertFalse(isSessionIdEqual(aliceStore, bobStore));

    CiphertextMessage finalMessage = bobSessionCipher.encrypt("third message".getBytes());

    assertTrue(finalMessage.getType() == CiphertextMessage.WHISPER_TYPE);

    byte[] finalPlaintext = aliceSessionCipher.decrypt(new SignalMessage(finalMessage.serialize()));

    assertTrue(new String(finalPlaintext).equals("third message"));
    assertTrue(isSessionIdEqual(aliceStore, bobStore));
  }
 
Example #18
Source File: SessionBuilderTest.java    From libsignal-protocol-java with GNU General Public License v3.0 4 votes vote down vote up
public void testBadMessageBundle() throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException, InvalidMessageException, DuplicateMessageException, LegacyMessageException, InvalidKeyIdException {
  SignalProtocolStore aliceStore          = new TestInMemorySignalProtocolStore();
  SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS);

  SignalProtocolStore bobStore = new TestInMemorySignalProtocolStore();

  ECKeyPair bobPreKeyPair            = Curve.generateKeyPair();
  ECKeyPair bobSignedPreKeyPair      = Curve.generateKeyPair();
  byte[]    bobSignedPreKeySignature = Curve.calculateSignature(bobStore.getIdentityKeyPair().getPrivateKey(),
                                                                bobSignedPreKeyPair.getPublicKey().serialize());

  PreKeyBundle bobPreKey = new PreKeyBundle(bobStore.getLocalRegistrationId(), 1,
                                            31337, bobPreKeyPair.getPublicKey(),
                                            22, bobSignedPreKeyPair.getPublicKey(), bobSignedPreKeySignature,
                                            bobStore.getIdentityKeyPair().getPublicKey());

  bobStore.storePreKey(31337, new PreKeyRecord(bobPreKey.getPreKeyId(), bobPreKeyPair));
  bobStore.storeSignedPreKey(22, new SignedPreKeyRecord(22, System.currentTimeMillis(), bobSignedPreKeyPair, bobSignedPreKeySignature));

  aliceSessionBuilder.process(bobPreKey);

  String            originalMessage    = "L'homme est condamné à être libre";
  SessionCipher     aliceSessionCipher = new SessionCipher(aliceStore, BOB_ADDRESS);
  CiphertextMessage outgoingMessageOne = aliceSessionCipher.encrypt(originalMessage.getBytes());

  assertTrue(outgoingMessageOne.getType() == CiphertextMessage.PREKEY_TYPE);

  byte[] goodMessage = outgoingMessageOne.serialize();
  byte[] badMessage  = new byte[goodMessage.length];
  System.arraycopy(goodMessage, 0, badMessage, 0, badMessage.length);

  badMessage[badMessage.length-10] ^= 0x01;

  PreKeySignalMessage incomingMessage  = new PreKeySignalMessage(badMessage);
  SessionCipher        bobSessionCipher = new SessionCipher(bobStore, ALICE_ADDRESS);

  byte[] plaintext = new byte[0];

  try {
    plaintext = bobSessionCipher.decrypt(incomingMessage);
    throw new AssertionError("Decrypt should have failed!");
  } catch (InvalidMessageException e) {
    // good.
  }

  assertTrue(bobStore.containsPreKey(31337));

  plaintext = bobSessionCipher.decrypt(new PreKeySignalMessage(goodMessage));

  assertTrue(originalMessage.equals(new String(plaintext)));
  assertTrue(!bobStore.containsPreKey(31337));
}
 
Example #19
Source File: SessionBuilderTest.java    From libsignal-protocol-java with GNU General Public License v3.0 4 votes vote down vote up
public void testRepeatBundleMessageV3() throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException, InvalidMessageException, InvalidKeyIdException, DuplicateMessageException, LegacyMessageException, NoSessionException {
  SignalProtocolStore aliceStore          = new TestInMemorySignalProtocolStore();
  SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS);

  SignalProtocolStore bobStore = new TestInMemorySignalProtocolStore();

  ECKeyPair bobPreKeyPair            = Curve.generateKeyPair();
  ECKeyPair bobSignedPreKeyPair      = Curve.generateKeyPair();
  byte[]    bobSignedPreKeySignature = Curve.calculateSignature(bobStore.getIdentityKeyPair().getPrivateKey(),
                                                                bobSignedPreKeyPair.getPublicKey().serialize());

  PreKeyBundle bobPreKey = new PreKeyBundle(bobStore.getLocalRegistrationId(), 1,
                                            31337, bobPreKeyPair.getPublicKey(),
                                            22, bobSignedPreKeyPair.getPublicKey(), bobSignedPreKeySignature,
                                            bobStore.getIdentityKeyPair().getPublicKey());

  bobStore.storePreKey(31337, new PreKeyRecord(bobPreKey.getPreKeyId(), bobPreKeyPair));
  bobStore.storeSignedPreKey(22, new SignedPreKeyRecord(22, System.currentTimeMillis(), bobSignedPreKeyPair, bobSignedPreKeySignature));

  aliceSessionBuilder.process(bobPreKey);

  String            originalMessage    = "L'homme est condamné à être libre";
  SessionCipher     aliceSessionCipher = new SessionCipher(aliceStore, BOB_ADDRESS);
  CiphertextMessage outgoingMessageOne = aliceSessionCipher.encrypt(originalMessage.getBytes());
  CiphertextMessage outgoingMessageTwo = aliceSessionCipher.encrypt(originalMessage.getBytes());

  assertTrue(outgoingMessageOne.getType() == CiphertextMessage.PREKEY_TYPE);
  assertTrue(outgoingMessageTwo.getType() == CiphertextMessage.PREKEY_TYPE);

  PreKeySignalMessage incomingMessage = new PreKeySignalMessage(outgoingMessageOne.serialize());

  SessionCipher bobSessionCipher = new SessionCipher(bobStore, ALICE_ADDRESS);

  byte[]        plaintext        = bobSessionCipher.decrypt(incomingMessage);
  assertTrue(originalMessage.equals(new String(plaintext)));

  CiphertextMessage bobOutgoingMessage = bobSessionCipher.encrypt(originalMessage.getBytes());

  byte[] alicePlaintext = aliceSessionCipher.decrypt(new SignalMessage(bobOutgoingMessage.serialize()));
  assertTrue(originalMessage.equals(new String(alicePlaintext)));

  // The test

  PreKeySignalMessage incomingMessageTwo = new PreKeySignalMessage(outgoingMessageTwo.serialize());

  plaintext = bobSessionCipher.decrypt(new PreKeySignalMessage(incomingMessageTwo.serialize()));
  assertTrue(originalMessage.equals(new String(plaintext)));

  bobOutgoingMessage = bobSessionCipher.encrypt(originalMessage.getBytes());
  alicePlaintext = aliceSessionCipher.decrypt(new SignalMessage(bobOutgoingMessage.serialize()));
  assertTrue(originalMessage.equals(new String(alicePlaintext)));

}
 
Example #20
Source File: PushDecryptJob.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
private void handleCorruptMessage(@NonNull MasterSecretUnion masterSecret,
                                  @NonNull SignalServiceProtos.Envelope envelope) {
    ALog.i(TAG, "handleCorruptMessage");

    ThreadRepo threadRepo = repository.getThreadRepo();
    String dataJson = threadRepo.getDecryptFailData(envelope.getSource());
    DecryptFailData data;
    if (dataJson != null && !dataJson.isEmpty()) {
        data = GsonUtils.INSTANCE.fromJson(dataJson, DecryptFailData.class);
    } else {
        data = new DecryptFailData();
    }
    if (data.getLastDeleteSessionTime() + 50000 < System.currentTimeMillis()) {
        ALog.i(TAG, "Last delete session time is 5s ago. Delete Session.");
        SignalProtocolStore store = new SignalProtocolStoreImpl(context, accountContext);
        SignalProtocolAddress address = new SignalProtocolAddress(envelope.getSource(), SignalServiceAddress.DEFAULT_DEVICE_ID);
        if (store.containsSession(address)) {
            store.deleteSession(address);
        }
        try {
            Optional<String> relay = Optional.absent();
            if (envelope.hasRelay()) {
                relay = Optional.of(envelope.getRelay());
            }
            List<PreKeyBundle> preKeyBundles = ChatHttp.INSTANCE.get(accountContext).getPreKeys(new SignalServiceAddress(envelope.getSource(), relay), SignalServiceAddress.DEFAULT_DEVICE_ID);
            for (PreKeyBundle preKey : preKeyBundles) {

                String identityKeyString = new String(EncryptUtils.base64Encode(preKey.getIdentityKey().serialize()));
                if (!AddressUtil.INSTANCE.isValid(envelope.getSource(), identityKeyString)) {
                    ALog.e(TAG, "getPreKeys error identity key got");
                    continue;
                }

                SessionBuilder sessionBuilder = new SessionBuilder(store, new SignalProtocolAddress(envelope.getSource(), SignalServiceAddress.DEFAULT_DEVICE_ID));
                sessionBuilder.process(preKey);

            }
        } catch (Throwable ex) {
            ALog.w(TAG, "Untrusted identity key from handleMismatchedDevices");
        }
        data.setLastDeleteSessionTime(System.currentTimeMillis());
        threadRepo.setDecryptFailData(envelope.getSource(), data.toJson());
    }

    //If the decryption fails, do not insert the library, and send a new receipt directly
    long messageId = envelope.getTimestamp();
    Recipient recipient = Recipient.from(accountContext, envelope.getSource(), false);
    String message = new AmeGroupMessage<>(AmeGroupMessage.RECEIPT, new AmeGroupMessage.ReceiptContent(messageId)).toString();
    OutgoingLocationMessage outgoingLocationMessage = new OutgoingLocationMessage(recipient, message, 0);
    if (masterSecret.getMasterSecret().isPresent()) {
        long threadId = threadRepo.getThreadIdIfExist(recipient);
        MessageSender.sendHideMessage(AppContextHolder.APP_CONTEXT, threadId, accountContext, AmeGroupMessage.RECEIPT, outgoingLocationMessage);
    }
}
 
Example #21
Source File: SessionCipher.java    From libsignal-protocol-java with GNU General Public License v3.0 4 votes vote down vote up
public SessionCipher(SignalProtocolStore store, SignalProtocolAddress remoteAddress) {
  this(store, store, store, store, remoteAddress);
}
 
Example #22
Source File: MmsCipher.java    From Silence with GNU General Public License v3.0 4 votes vote down vote up
public MmsCipher(SignalProtocolStore axolotlStore) {
  this.axolotlStore = axolotlStore;
}
 
Example #23
Source File: SmsCipher.java    From Silence with GNU General Public License v3.0 4 votes vote down vote up
public SmsCipher(SignalProtocolStore signalProtocolStore) {
  this.signalProtocolStore = signalProtocolStore;
}
 
Example #24
Source File: SessionBuilder.java    From Silence with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Constructs a SessionBuilder
 * @param store The {@link SignalProtocolStore} to store all state information in.
 * @param remoteAddress The address of the remote user to build a session with.
 */
public SessionBuilder(SignalProtocolStore store, SignalProtocolAddress remoteAddress) {
  this(store, store, store, store, remoteAddress);
}
 
Example #25
Source File: SimultaneousInitiateTests.java    From libsignal-protocol-java with GNU General Public License v3.0 2 votes vote down vote up
public void testBasicSimultaneousInitiate()
    throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException,
    InvalidMessageException, DuplicateMessageException, LegacyMessageException,
    InvalidKeyIdException, NoSessionException
{
  SignalProtocolStore aliceStore = new TestInMemorySignalProtocolStore();
  SignalProtocolStore bobStore   = new TestInMemorySignalProtocolStore();

  PreKeyBundle alicePreKeyBundle = createAlicePreKeyBundle(aliceStore);
  PreKeyBundle bobPreKeyBundle = createBobPreKeyBundle(bobStore);

  SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS);
  SessionBuilder bobSessionBuilder   = new SessionBuilder(bobStore, ALICE_ADDRESS);

  SessionCipher aliceSessionCipher = new SessionCipher(aliceStore, BOB_ADDRESS);
  SessionCipher bobSessionCipher = new SessionCipher(bobStore, ALICE_ADDRESS);

  aliceSessionBuilder.process(bobPreKeyBundle);
  bobSessionBuilder.process(alicePreKeyBundle);

  CiphertextMessage messageForBob   = aliceSessionCipher.encrypt("hey there".getBytes());
  CiphertextMessage messageForAlice = bobSessionCipher.encrypt("sample message".getBytes());

  assertTrue(messageForBob.getType() == CiphertextMessage.PREKEY_TYPE);
  assertTrue(messageForAlice.getType() == CiphertextMessage.PREKEY_TYPE);

  assertFalse(isSessionIdEqual(aliceStore, bobStore));

  byte[] alicePlaintext = aliceSessionCipher.decrypt(new PreKeySignalMessage(messageForAlice.serialize()));
  byte[] bobPlaintext   = bobSessionCipher.decrypt(new PreKeySignalMessage(messageForBob.serialize()));

  assertTrue(new String(alicePlaintext).equals("sample message"));
  assertTrue(new String(bobPlaintext).equals("hey there"));

  assertTrue(aliceStore.loadSession(BOB_ADDRESS).getSessionState().getSessionVersion() == 3);
  assertTrue(bobStore.loadSession(ALICE_ADDRESS).getSessionState().getSessionVersion() == 3);

  assertFalse(isSessionIdEqual(aliceStore, bobStore));

  CiphertextMessage aliceResponse = aliceSessionCipher.encrypt("second message".getBytes());

  assertTrue(aliceResponse.getType() == CiphertextMessage.WHISPER_TYPE);

  byte[] responsePlaintext = bobSessionCipher.decrypt(new SignalMessage(aliceResponse.serialize()));

  assertTrue(new String(responsePlaintext).equals("second message"));
  assertTrue(isSessionIdEqual(aliceStore, bobStore));

  CiphertextMessage finalMessage = bobSessionCipher.encrypt("third message".getBytes());

  assertTrue(finalMessage.getType() == CiphertextMessage.WHISPER_TYPE);

  byte[] finalPlaintext = aliceSessionCipher.decrypt(new SignalMessage(finalMessage.serialize()));

  assertTrue(new String(finalPlaintext).equals("third message"));
  assertTrue(isSessionIdEqual(aliceStore, bobStore));
}
 
Example #26
Source File: SimultaneousInitiateTests.java    From libsignal-protocol-java with GNU General Public License v3.0 2 votes vote down vote up
public void testLostSimultaneousInitiate() throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException, InvalidMessageException, DuplicateMessageException, LegacyMessageException, InvalidKeyIdException, NoSessionException {
  SignalProtocolStore aliceStore = new TestInMemorySignalProtocolStore();
  SignalProtocolStore bobStore   = new TestInMemorySignalProtocolStore();

  PreKeyBundle alicePreKeyBundle = createAlicePreKeyBundle(aliceStore);
  PreKeyBundle bobPreKeyBundle = createBobPreKeyBundle(bobStore);

  SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS);
  SessionBuilder bobSessionBuilder   = new SessionBuilder(bobStore, ALICE_ADDRESS);

  SessionCipher aliceSessionCipher = new SessionCipher(aliceStore, BOB_ADDRESS);
  SessionCipher bobSessionCipher = new SessionCipher(bobStore, ALICE_ADDRESS);

  aliceSessionBuilder.process(bobPreKeyBundle);
  bobSessionBuilder.process(alicePreKeyBundle);

  CiphertextMessage messageForBob   = aliceSessionCipher.encrypt("hey there".getBytes());
  CiphertextMessage messageForAlice = bobSessionCipher.encrypt("sample message".getBytes());

  assertTrue(messageForBob.getType() == CiphertextMessage.PREKEY_TYPE);
  assertTrue(messageForAlice.getType() == CiphertextMessage.PREKEY_TYPE);

  assertFalse(isSessionIdEqual(aliceStore, bobStore));

  byte[] bobPlaintext   = bobSessionCipher.decrypt(new PreKeySignalMessage(messageForBob.serialize()));

  assertTrue(new String(bobPlaintext).equals("hey there"));
  assertTrue(bobStore.loadSession(ALICE_ADDRESS).getSessionState().getSessionVersion() == 3);

  CiphertextMessage aliceResponse = aliceSessionCipher.encrypt("second message".getBytes());

  assertTrue(aliceResponse.getType() == CiphertextMessage.PREKEY_TYPE);

  byte[] responsePlaintext = bobSessionCipher.decrypt(new PreKeySignalMessage(aliceResponse.serialize()));

  assertTrue(new String(responsePlaintext).equals("second message"));
  assertTrue(isSessionIdEqual(aliceStore, bobStore));

  CiphertextMessage finalMessage = bobSessionCipher.encrypt("third message".getBytes());

  assertTrue(finalMessage.getType() == CiphertextMessage.WHISPER_TYPE);

  byte[] finalPlaintext = aliceSessionCipher.decrypt(new SignalMessage(finalMessage.serialize()));

  assertTrue(new String(finalPlaintext).equals("third message"));
  assertTrue(isSessionIdEqual(aliceStore, bobStore));
}
 
Example #27
Source File: SimultaneousInitiateTests.java    From libsignal-protocol-java with GNU General Public License v3.0 2 votes vote down vote up
public void testSimultaneousInitiateRepeatedMessages()
    throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException,
    InvalidMessageException, DuplicateMessageException, LegacyMessageException,
    InvalidKeyIdException, NoSessionException
{
  SignalProtocolStore aliceStore = new TestInMemorySignalProtocolStore();
  SignalProtocolStore bobStore   = new TestInMemorySignalProtocolStore();

  PreKeyBundle alicePreKeyBundle = createAlicePreKeyBundle(aliceStore);
  PreKeyBundle bobPreKeyBundle = createBobPreKeyBundle(bobStore);

  SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS);
  SessionBuilder bobSessionBuilder   = new SessionBuilder(bobStore, ALICE_ADDRESS);

  SessionCipher aliceSessionCipher = new SessionCipher(aliceStore, BOB_ADDRESS);
  SessionCipher bobSessionCipher = new SessionCipher(bobStore, ALICE_ADDRESS);

  aliceSessionBuilder.process(bobPreKeyBundle);
  bobSessionBuilder.process(alicePreKeyBundle);

  CiphertextMessage messageForBob   = aliceSessionCipher.encrypt("hey there".getBytes());
  CiphertextMessage messageForAlice = bobSessionCipher.encrypt("sample message".getBytes());

  assertTrue(messageForBob.getType() == CiphertextMessage.PREKEY_TYPE);
  assertTrue(messageForAlice.getType() == CiphertextMessage.PREKEY_TYPE);

  assertFalse(isSessionIdEqual(aliceStore, bobStore));

  byte[] alicePlaintext = aliceSessionCipher.decrypt(new PreKeySignalMessage(messageForAlice.serialize()));
  byte[] bobPlaintext   = bobSessionCipher.decrypt(new PreKeySignalMessage(messageForBob.serialize()));

  assertTrue(new String(alicePlaintext).equals("sample message"));
  assertTrue(new String(bobPlaintext).equals("hey there"));

  assertTrue(aliceStore.loadSession(BOB_ADDRESS).getSessionState().getSessionVersion() == 3);
  assertTrue(bobStore.loadSession(ALICE_ADDRESS).getSessionState().getSessionVersion() == 3);

  assertFalse(isSessionIdEqual(aliceStore, bobStore));

  for (int i=0;i<50;i++) {
    CiphertextMessage messageForBobRepeat   = aliceSessionCipher.encrypt("hey there".getBytes());
    CiphertextMessage messageForAliceRepeat = bobSessionCipher.encrypt("sample message".getBytes());

    assertTrue(messageForBobRepeat.getType() == CiphertextMessage.WHISPER_TYPE);
    assertTrue(messageForAliceRepeat.getType() == CiphertextMessage.WHISPER_TYPE);

    assertFalse(isSessionIdEqual(aliceStore, bobStore));

    byte[] alicePlaintextRepeat = aliceSessionCipher.decrypt(new SignalMessage(messageForAliceRepeat.serialize()));
    byte[] bobPlaintextRepeat   = bobSessionCipher.decrypt(new SignalMessage(messageForBobRepeat.serialize()));

    assertTrue(new String(alicePlaintextRepeat).equals("sample message"));
    assertTrue(new String(bobPlaintextRepeat).equals("hey there"));

    assertFalse(isSessionIdEqual(aliceStore, bobStore));
  }

  CiphertextMessage aliceResponse = aliceSessionCipher.encrypt("second message".getBytes());

  assertTrue(aliceResponse.getType() == CiphertextMessage.WHISPER_TYPE);

  byte[] responsePlaintext = bobSessionCipher.decrypt(new SignalMessage(aliceResponse.serialize()));

  assertTrue(new String(responsePlaintext).equals("second message"));
  assertTrue(isSessionIdEqual(aliceStore, bobStore));

  CiphertextMessage finalMessage = bobSessionCipher.encrypt("third message".getBytes());

  assertTrue(finalMessage.getType() == CiphertextMessage.WHISPER_TYPE);

  byte[] finalPlaintext = aliceSessionCipher.decrypt(new SignalMessage(finalMessage.serialize()));

  assertTrue(new String(finalPlaintext).equals("third message"));
  assertTrue(isSessionIdEqual(aliceStore, bobStore));
}
 
Example #28
Source File: SimultaneousInitiateTests.java    From libsignal-protocol-java with GNU General Public License v3.0 2 votes vote down vote up
public void testRepeatedSimultaneousInitiateRepeatedMessages()
    throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException,
    InvalidMessageException, DuplicateMessageException, LegacyMessageException,
    InvalidKeyIdException, NoSessionException
{
  SignalProtocolStore aliceStore = new TestInMemorySignalProtocolStore();
  SignalProtocolStore bobStore   = new TestInMemorySignalProtocolStore();


  SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS);
  SessionBuilder bobSessionBuilder   = new SessionBuilder(bobStore, ALICE_ADDRESS);

  SessionCipher aliceSessionCipher = new SessionCipher(aliceStore, BOB_ADDRESS);
  SessionCipher bobSessionCipher = new SessionCipher(bobStore, ALICE_ADDRESS);

  for (int i=0;i<15;i++) {
    PreKeyBundle alicePreKeyBundle = createAlicePreKeyBundle(aliceStore);
    PreKeyBundle bobPreKeyBundle   = createBobPreKeyBundle(bobStore);

    aliceSessionBuilder.process(bobPreKeyBundle);
    bobSessionBuilder.process(alicePreKeyBundle);

    CiphertextMessage messageForBob = aliceSessionCipher.encrypt("hey there".getBytes());
    CiphertextMessage messageForAlice = bobSessionCipher.encrypt("sample message".getBytes());

    assertTrue(messageForBob.getType() == CiphertextMessage.PREKEY_TYPE);
    assertTrue(messageForAlice.getType() == CiphertextMessage.PREKEY_TYPE);

    assertFalse(isSessionIdEqual(aliceStore, bobStore));

    byte[] alicePlaintext = aliceSessionCipher.decrypt(new PreKeySignalMessage(messageForAlice.serialize()));
    byte[] bobPlaintext = bobSessionCipher.decrypt(new PreKeySignalMessage(messageForBob.serialize()));

    assertTrue(new String(alicePlaintext).equals("sample message"));
    assertTrue(new String(bobPlaintext).equals("hey there"));

    assertTrue(aliceStore.loadSession(BOB_ADDRESS).getSessionState().getSessionVersion() == 3);
    assertTrue(bobStore.loadSession(ALICE_ADDRESS).getSessionState().getSessionVersion() == 3);

    assertFalse(isSessionIdEqual(aliceStore, bobStore));
  }

  for (int i=0;i<50;i++) {
    CiphertextMessage messageForBobRepeat   = aliceSessionCipher.encrypt("hey there".getBytes());
    CiphertextMessage messageForAliceRepeat = bobSessionCipher.encrypt("sample message".getBytes());

    assertTrue(messageForBobRepeat.getType() == CiphertextMessage.WHISPER_TYPE);
    assertTrue(messageForAliceRepeat.getType() == CiphertextMessage.WHISPER_TYPE);

    assertFalse(isSessionIdEqual(aliceStore, bobStore));

    byte[] alicePlaintextRepeat = aliceSessionCipher.decrypt(new SignalMessage(messageForAliceRepeat.serialize()));
    byte[] bobPlaintextRepeat   = bobSessionCipher.decrypt(new SignalMessage(messageForBobRepeat.serialize()));

    assertTrue(new String(alicePlaintextRepeat).equals("sample message"));
    assertTrue(new String(bobPlaintextRepeat).equals("hey there"));

    assertFalse(isSessionIdEqual(aliceStore, bobStore));
  }

  CiphertextMessage aliceResponse = aliceSessionCipher.encrypt("second message".getBytes());

  assertTrue(aliceResponse.getType() == CiphertextMessage.WHISPER_TYPE);

  byte[] responsePlaintext = bobSessionCipher.decrypt(new SignalMessage(aliceResponse.serialize()));

  assertTrue(new String(responsePlaintext).equals("second message"));
  assertTrue(isSessionIdEqual(aliceStore, bobStore));

  CiphertextMessage finalMessage = bobSessionCipher.encrypt("third message".getBytes());

  assertTrue(finalMessage.getType() == CiphertextMessage.WHISPER_TYPE);

  byte[] finalPlaintext = aliceSessionCipher.decrypt(new SignalMessage(finalMessage.serialize()));

  assertTrue(new String(finalPlaintext).equals("third message"));
  assertTrue(isSessionIdEqual(aliceStore, bobStore));
}
 
Example #29
Source File: SimultaneousInitiateTests.java    From libsignal-protocol-java with GNU General Public License v3.0 2 votes vote down vote up
public void testRepeatedSimultaneousInitiateLostMessageRepeatedMessages()
      throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException,
      InvalidMessageException, DuplicateMessageException, LegacyMessageException,
      InvalidKeyIdException, NoSessionException
  {
    SignalProtocolStore aliceStore = new TestInMemorySignalProtocolStore();
    SignalProtocolStore bobStore   = new TestInMemorySignalProtocolStore();


    SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS);
    SessionBuilder bobSessionBuilder   = new SessionBuilder(bobStore, ALICE_ADDRESS);

    SessionCipher aliceSessionCipher = new SessionCipher(aliceStore, BOB_ADDRESS);
    SessionCipher bobSessionCipher = new SessionCipher(bobStore, ALICE_ADDRESS);

//    PreKeyBundle aliceLostPreKeyBundle = createAlicePreKeyBundle(aliceStore);
    PreKeyBundle bobLostPreKeyBundle   = createBobPreKeyBundle(bobStore);

    aliceSessionBuilder.process(bobLostPreKeyBundle);
//    bobSessionBuilder.process(aliceLostPreKeyBundle);

    CiphertextMessage lostMessageForBob   = aliceSessionCipher.encrypt("hey there".getBytes());
//    CiphertextMessage lostMessageForAlice = bobSessionCipher.encrypt("sample message".getBytes());

    for (int i=0;i<15;i++) {
      PreKeyBundle alicePreKeyBundle = createAlicePreKeyBundle(aliceStore);
      PreKeyBundle bobPreKeyBundle   = createBobPreKeyBundle(bobStore);

      aliceSessionBuilder.process(bobPreKeyBundle);
      bobSessionBuilder.process(alicePreKeyBundle);

      CiphertextMessage messageForBob = aliceSessionCipher.encrypt("hey there".getBytes());
      CiphertextMessage messageForAlice = bobSessionCipher.encrypt("sample message".getBytes());

      assertTrue(messageForBob.getType() == CiphertextMessage.PREKEY_TYPE);
      assertTrue(messageForAlice.getType() == CiphertextMessage.PREKEY_TYPE);

      assertFalse(isSessionIdEqual(aliceStore, bobStore));

      byte[] alicePlaintext = aliceSessionCipher.decrypt(new PreKeySignalMessage(messageForAlice.serialize()));
      byte[] bobPlaintext = bobSessionCipher.decrypt(new PreKeySignalMessage(messageForBob.serialize()));

      assertTrue(new String(alicePlaintext).equals("sample message"));
      assertTrue(new String(bobPlaintext).equals("hey there"));

      assertTrue(aliceStore.loadSession(BOB_ADDRESS).getSessionState().getSessionVersion() == 3);
      assertTrue(bobStore.loadSession(ALICE_ADDRESS).getSessionState().getSessionVersion() == 3);

      assertFalse(isSessionIdEqual(aliceStore, bobStore));
    }

    for (int i=0;i<50;i++) {
      CiphertextMessage messageForBobRepeat   = aliceSessionCipher.encrypt("hey there".getBytes());
      CiphertextMessage messageForAliceRepeat = bobSessionCipher.encrypt("sample message".getBytes());

      assertTrue(messageForBobRepeat.getType() == CiphertextMessage.WHISPER_TYPE);
      assertTrue(messageForAliceRepeat.getType() == CiphertextMessage.WHISPER_TYPE);

      assertFalse(isSessionIdEqual(aliceStore, bobStore));

      byte[] alicePlaintextRepeat = aliceSessionCipher.decrypt(new SignalMessage(messageForAliceRepeat.serialize()));
      byte[] bobPlaintextRepeat   = bobSessionCipher.decrypt(new SignalMessage(messageForBobRepeat.serialize()));

      assertTrue(new String(alicePlaintextRepeat).equals("sample message"));
      assertTrue(new String(bobPlaintextRepeat).equals("hey there"));

      assertFalse(isSessionIdEqual(aliceStore, bobStore));
    }

    CiphertextMessage aliceResponse = aliceSessionCipher.encrypt("second message".getBytes());

    assertTrue(aliceResponse.getType() == CiphertextMessage.WHISPER_TYPE);

    byte[] responsePlaintext = bobSessionCipher.decrypt(new SignalMessage(aliceResponse.serialize()));

    assertTrue(new String(responsePlaintext).equals("second message"));
    assertTrue(isSessionIdEqual(aliceStore, bobStore));

    CiphertextMessage finalMessage = bobSessionCipher.encrypt("third message".getBytes());

    assertTrue(finalMessage.getType() == CiphertextMessage.WHISPER_TYPE);

    byte[] finalPlaintext = aliceSessionCipher.decrypt(new SignalMessage(finalMessage.serialize()));

    assertTrue(new String(finalPlaintext).equals("third message"));
    assertTrue(isSessionIdEqual(aliceStore, bobStore));

    byte[] lostMessagePlaintext = bobSessionCipher.decrypt(new PreKeySignalMessage(lostMessageForBob.serialize()));
    assertTrue(new String(lostMessagePlaintext).equals("hey there"));

    assertFalse(isSessionIdEqual(aliceStore, bobStore));

    CiphertextMessage blastFromThePast          = bobSessionCipher.encrypt("unexpected!".getBytes());
    byte[]            blastFromThePastPlaintext = aliceSessionCipher.decrypt(new SignalMessage(blastFromThePast.serialize()));

    assertTrue(new String(blastFromThePastPlaintext).equals("unexpected!"));
    assertTrue(isSessionIdEqual(aliceStore, bobStore));
  }
 
Example #30
Source File: SessionBuilder.java    From libsignal-protocol-java with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Constructs a SessionBuilder
 * @param store The {@link SignalProtocolStore} to store all state information in.
 * @param remoteAddress The address of the remote user to build a session with.
 */
public SessionBuilder(SignalProtocolStore store, SignalProtocolAddress remoteAddress) {
  this(store, store, store, store, remoteAddress);
}