1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-16 01:02:06 +01:00

Temporary commit

This commit is contained in:
Paul Schaub 2019-04-02 20:24:40 +02:00
parent daab6039a1
commit a749bc4cc4
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
15 changed files with 273 additions and 164 deletions

View file

@ -20,9 +20,14 @@ import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import java.security.NoSuchAlgorithmException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException;
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
import org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException;
import org.jivesoftware.smackx.omemo.exceptions.NoRawSessionException;
import org.jivesoftware.smackx.omemo.exceptions.ReadOnlyDeviceException;
import org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException;
@ -37,36 +42,42 @@ public class ReadOnlyDeviceIntegrationTest extends AbstractTwoUsersOmemoIntegrat
}
@SmackIntegrationTest
public void test() throws InterruptedException, SmackException.NoResponseException, SmackException.NotLoggedInException, SmackException.NotConnectedException, CryptoFailedException, UndecidedOmemoIdentityException {
public void test() throws InterruptedException, SmackException.NoResponseException, SmackException.NotLoggedInException, SmackException.NotConnectedException, CryptoFailedException, UndecidedOmemoIdentityException, CorruptedOmemoKeyException, NoSuchAlgorithmException, CannotEstablishOmemoSessionException, NoRawSessionException {
int allowedChainLength = 5;
boolean prevIgnoreReadOnlyConf = OmemoConfiguration.getIgnoreReadOnlyDevices();
int prevMaxMessageCounter = OmemoConfiguration.getMaxReadOnlyMessageCount();
OmemoConfiguration.setIgnoreReadOnlyDevices(true);
// Set the maxReadOnlyMessageCount to ridiculously low threshold of 5.
// This means that Alice will be able to encrypt 5 messages for Bob, while the 6th will not be encrypted for Bob.
OmemoConfiguration.setMaxReadOnlyMessageCount(5);
OmemoConfiguration.setMaxReadOnlyMessageCount(allowedChainLength);
// Reset counter to begin test
alice.getOmemoService().getOmemoStoreBackend().storeOmemoMessageCounter(alice.getOwnDevice(), bob.getOwnDevice(), 0);
alice.getOmemoService().buildFreshSessionWithDevice(alice.getConnection(), alice.getOwnDevice(), bob.getOwnDevice());
assertChainLengthEquals(alice, bob, 0);
// Since the max threshold is 5, we must be able to encrypt 5 messages for Bob.
for (int i = 0; i < 5; i++) {
assertEquals(i, alice.getOmemoService().getOmemoStoreBackend().loadOmemoMessageCounter(alice.getOwnDevice(), bob.getOwnDevice()));
// Since the max threshold is allowedChainLength, we must be able to encrypt as many messages for Bob.
for (int i = 0; i < allowedChainLength; i++) {
OmemoMessage.Sent message = alice.encrypt(bob.getOwnJid(), "Hello World!");
assertFalse(message.getSkippedDevices().containsKey(bob.getOwnDevice()));
assertChainLengthEquals(alice, bob, i + 1);
}
// Now the message counter must be too high and Bobs device must be skipped.
OmemoMessage.Sent message = alice.encrypt(bob.getOwnJid(), "Hello World!");
assertChainLengthEquals(alice, bob, allowedChainLength);
assertTrue(message.getSkippedDevices().containsKey(bob.getOwnDevice()));
Throwable exception = message.getSkippedDevices().get(bob.getOwnDevice());
assertTrue(exception instanceof ReadOnlyDeviceException);
assertEquals(bob.getOwnDevice(), ((ReadOnlyDeviceException) exception).getDevice());
// Reset the message counter
alice.getOmemoService().getOmemoStoreBackend().storeOmemoMessageCounter(alice.getOwnDevice(), bob.getOwnDevice(), 0);
// Reset the configuration to previous values
OmemoConfiguration.setMaxReadOnlyMessageCount(prevMaxMessageCounter);
OmemoConfiguration.setIgnoreReadOnlyDevices(prevIgnoreReadOnlyConf);
}
private void assertChainLengthEquals(OmemoManager sender, OmemoManager receiver, int length) throws NoRawSessionException {
assertEquals(length, sender.getOmemoService().getOmemoStoreBackend()
.getLengthOfSessionSendingChain(sender.getOwnDevice(), receiver.getOwnDevice()));
}
}

View file

@ -0,0 +1,51 @@
/**
*
* Copyright 2019 Paul Schaub
*
* This file is part of smack-omemo-signal.
*
* smack-omemo-signal is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.jivesoftware.smackx.omemo.signal;
import org.jivesoftware.smackx.omemo.OmemoStore;
import org.jivesoftware.smackx.omemo.util.ChainKeyIndexStalenessStrategy;
import org.whispersystems.libsignal.IdentityKey;
import org.whispersystems.libsignal.IdentityKeyPair;
import org.whispersystems.libsignal.SessionCipher;
import org.whispersystems.libsignal.SignalProtocolAddress;
import org.whispersystems.libsignal.ecc.ECPublicKey;
import org.whispersystems.libsignal.state.PreKeyBundle;
import org.whispersystems.libsignal.state.PreKeyRecord;
import org.whispersystems.libsignal.state.SessionRecord;
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
public class SignalChainKeyIndexStalenessStrategy extends ChainKeyIndexStalenessStrategy<IdentityKeyPair, IdentityKey,
PreKeyRecord, SignedPreKeyRecord, SessionRecord, SignalProtocolAddress, ECPublicKey, PreKeyBundle, SessionCipher> {
/**
* Create a new {@link SignalChainKeyIndexStalenessStrategy}.
*
* @param store {@link OmemoStore} from which we can read the devices {@link SessionRecord}.
* @param maxChainKeyIndex the maximum value of messages we are allowed to send to the device unanswered before
*/
public SignalChainKeyIndexStalenessStrategy(
OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord, SessionRecord,
SignalProtocolAddress, ECPublicKey, PreKeyBundle, SessionCipher> store,
int maxChainKeyIndex) {
super(store, maxChainKeyIndex);
}
}

View file

@ -96,6 +96,11 @@ public class SignalOmemoKeyUtil extends OmemoKeyUtil<IdentityKeyPair, IdentityKe
return session.serialize();
}
@Override
public int lengthOfSessionSendingChain(SessionRecord session) {
return session.getSessionState().getSenderChainKey().getIndex();
}
@Override
public IdentityKeyPair identityKeyPairFromBytes(byte[] data) throws CorruptedOmemoKeyException {
if (data == null) return null;

View file

@ -22,11 +22,13 @@ package org.jivesoftware.smackx.omemo.signal;
import java.util.logging.Level;
import org.jivesoftware.smackx.omemo.OmemoConfiguration;
import org.jivesoftware.smackx.omemo.OmemoManager;
import org.jivesoftware.smackx.omemo.OmemoService;
import org.jivesoftware.smackx.omemo.OmemoStore;
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
import org.jivesoftware.smackx.omemo.util.StalenessStrategy;
import org.whispersystems.libsignal.IdentityKey;
import org.whispersystems.libsignal.IdentityKeyPair;
@ -87,6 +89,19 @@ public final class SignalOmemoService
super();
}
/**
* Return a {@link StalenessStrategy} which is used to determine, whether or not an OMEMO session is stale or not.
* Staleness is used in order to stop encrypting messages to read-only devices after a certain amount of time
* or unanswered messages.
*
* @return strategy
*/
@Override
protected StalenessStrategy getStalenessStrategy() {
return new SignalChainKeyIndexStalenessStrategy(getOmemoStoreBackend(),
OmemoConfiguration.getMaxReadOnlyMessageCount());
}
public static void acknowledgeLicense() {
LICENSE_ACKNOWLEDGED = true;
}

View file

@ -140,30 +140,6 @@ public class CachingOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Se
}
}
@Override
public void storeOmemoMessageCounter(OmemoDevice userDevice, OmemoDevice contactsDevice, int counter) {
getCache(userDevice).messageCounters.put(contactsDevice, counter);
if (persistent != null) {
persistent.storeOmemoMessageCounter(userDevice, contactsDevice, counter);
}
}
@Override
public int loadOmemoMessageCounter(OmemoDevice userDevice, OmemoDevice contactsDevice) {
Integer counter = getCache(userDevice).messageCounters.get(contactsDevice);
if (counter == null && persistent != null) {
counter = persistent.loadOmemoMessageCounter(userDevice, contactsDevice);
}
if (counter == null) {
counter = 0;
}
getCache(userDevice).messageCounters.put(contactsDevice, counter);
return counter;
}
@Override
public void setDateOfLastReceivedMessage(OmemoDevice userDevice, OmemoDevice from, Date date) {
getCache(userDevice).lastMessagesDates.put(from, date);

View file

@ -24,7 +24,6 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
@ -355,24 +354,6 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
return session.exists();
}
@Override
public void storeOmemoMessageCounter(OmemoDevice userDevice, OmemoDevice contactsDevice, int counter) {
File messageCounterFile = hierarchy.getDevicesMessageCounterPath(userDevice, contactsDevice);
writeIntegers(messageCounterFile, Collections.singleton(counter));
}
@Override
public int loadOmemoMessageCounter(OmemoDevice userDevice, OmemoDevice contactsDevice) {
File messageCounterFile = hierarchy.getDevicesMessageCounterPath(userDevice, contactsDevice);
Set<Integer> integers = readIntegers(messageCounterFile);
if (integers == null || integers.isEmpty()) {
return 0;
}
return integers.iterator().next();
}
@Override
public OmemoCachedDeviceList loadCachedDeviceList(OmemoDevice userDevice, BareJid contact) {
OmemoCachedDeviceList cachedDeviceList = new OmemoCachedDeviceList();

View file

@ -24,7 +24,7 @@ package org.jivesoftware.smackx.omemo;
public final class OmemoConfiguration {
private static boolean IGNORE_READ_ONLY_DEVICES = true;
private static int MAX_READ_ONLY_MESSAGE_COUNT = 400;
private static int MAX_READ_ONLY_MESSAGE_COUNT = 750;
/**
* Set to true, in order to ignore read-only devices.

View file

@ -75,6 +75,7 @@ import org.jivesoftware.smackx.omemo.trust.TrustState;
import org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage;
import org.jivesoftware.smackx.omemo.util.OmemoConstants;
import org.jivesoftware.smackx.omemo.util.OmemoMessageBuilder;
import org.jivesoftware.smackx.omemo.util.StalenessStrategy;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubException;
@ -124,6 +125,15 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
protected OmemoService() {
}
/**
* Return a {@link StalenessStrategy} which is used to determine, whether or not an OMEMO session is stale or not.
* Staleness is used in order to stop encrypting messages to read-only devices after a certain amount of time
* or unanswered messages.
*
* @return strategy
*/
protected abstract StalenessStrategy getStalenessStrategy();
/**
* Return the singleton instance of this class. When no instance is set, throw an IllegalStateException instead.
* @return instance.
@ -354,7 +364,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
OmemoDevice userDevice = manager.getOwnDevice();
// Do not encrypt for our own device.
removeOurDevice(userDevice, contactsDevices);
contactsDevices.remove(userDevice);
buildMissingSessionsWithDevices(manager.getConnection(), userDevice, contactsDevices);
@ -387,19 +397,16 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
}
}
int messageCounter = omemoStore.loadOmemoMessageCounter(userDevice, contactsDevice);
// Ignore read-only devices
if (OmemoConfiguration.getIgnoreReadOnlyDevices()) {
boolean readOnly = messageCounter >= OmemoConfiguration.getMaxReadOnlyMessageCount();
boolean readOnly = getStalenessStrategy().isStale(userDevice, contactsDevice);
if (readOnly) {
LOGGER.log(Level.FINE, "Device " + contactsDevice + " seems to be read-only (We sent "
+ messageCounter + " messages without getting a reply back (max allowed is " +
OmemoConfiguration.getMaxReadOnlyMessageCount() + "). Ignoring the device.");
LOGGER.log(Level.FINE, "Device " + contactsDevice + " seems to be read-only " +
"(We sent more than " + OmemoConfiguration.getMaxReadOnlyMessageCount() +
"without getting a reply back.");
skippedRecipients.put(contactsDevice, new ReadOnlyDeviceException(contactsDevice));
// Skip this device and handle next device
continue;
}
@ -421,10 +428,6 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
LOGGER.log(Level.WARNING, "Device " + contactsDevice + " is untrusted. Message is not encrypted for it.");
skippedRecipients.put(contactsDevice, e);
}
// Increment the message counter of the device
omemoStore.storeOmemoMessageCounter(userDevice, contactsDevice,
messageCounter + 1);
}
OmemoElement element = builder.finish();
@ -463,9 +466,6 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
throw new AssertionError("Cannot retrieve OmemoFingerprint of sender although decryption was successful: " + e);
}
// Reset the message counter.
omemoStore.storeOmemoMessageCounter(manager.getOwnDevice(), senderDevice, 0);
if (omemoElement.isMessageElement()) {
// Use symmetric message key to decrypt message payload.
String plaintext = OmemoRatchet.decryptMessageElement(omemoElement, cipherAndAuthTag);
@ -997,16 +997,12 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* Never mark our own device as stale.
* This method ignores {@link OmemoConfiguration#getDeleteStaleDevices()}!
*
* In this case, a stale device is one of our devices, from which we haven't received an OMEMO message from
* for more than {@link OmemoConfiguration#DELETE_STALE_DEVICE_AFTER_HOURS} hours.
*
* @param userDevice our OmemoDevice
* @return our altered deviceList with stale devices marked as inactive.
*/
private OmemoCachedDeviceList deleteStaleDevices(OmemoDevice userDevice) {
OmemoCachedDeviceList deviceList = getOmemoStoreBackend().loadCachedDeviceList(userDevice);
int maxAgeHours = OmemoConfiguration.getDeleteStaleDevicesAfterHours();
return removeStaleDevicesFromDeviceList(userDevice, userDevice.getJid(), deviceList, maxAgeHours);
return removeStaleDevicesFromDeviceList(userDevice, userDevice.getJid(), deviceList);
}
/**
@ -1024,28 +1020,14 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
*/
private OmemoCachedDeviceList removeStaleDevicesFromDeviceList(OmemoDevice userDevice,
BareJid contact,
OmemoCachedDeviceList contactsDeviceList,
int maxAgeHours) {
OmemoCachedDeviceList contactsDeviceList) {
OmemoCachedDeviceList deviceList = new OmemoCachedDeviceList(contactsDeviceList); // Don't work on original list.
// Iterate through original list, but modify copy instead
for (int deviceId : contactsDeviceList.getActiveDevices()) {
OmemoDevice device = new OmemoDevice(contact, deviceId);
Date lastDeviceIdPublication = getOmemoStoreBackend().getDateOfLastDeviceIdPublication(userDevice, device);
if (lastDeviceIdPublication == null) {
lastDeviceIdPublication = new Date();
getOmemoStoreBackend().setDateOfLastDeviceIdPublication(userDevice, device, lastDeviceIdPublication);
}
Date lastMessageReceived = getOmemoStoreBackend().getDateOfLastReceivedMessage(userDevice, device);
if (lastMessageReceived == null) {
lastMessageReceived = new Date();
getOmemoStoreBackend().setDateOfLastReceivedMessage(userDevice, device, lastMessageReceived);
}
boolean stale = isStale(userDevice, device, lastDeviceIdPublication, maxAgeHours);
stale &= isStale(userDevice, device, lastMessageReceived, maxAgeHours);
boolean stale = isStale(userDevice, device);
if (stale) {
deviceList.addInactiveDevice(deviceId);
@ -1062,34 +1044,19 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @param devices collection of OmemoDevices
*/
static void removeOurDevice(OmemoDevice userDevice, Collection<OmemoDevice> devices) {
if (devices.contains(userDevice)) {
devices.remove(userDevice);
}
devices.remove(userDevice);
}
/**
* Determine, whether another one of *our* devices is stale or not.
* Determine, whether another device is stale or not.
*
* @param userDevice our omemoDevice
* @param subject another one of our devices
* @param lastReceipt date of last received message from that device
* @param maxAgeHours threshold
* @param subject another device
*
* @return staleness
*/
static boolean isStale(OmemoDevice userDevice, OmemoDevice subject, Date lastReceipt, int maxAgeHours) {
if (userDevice.equals(subject)) {
return false;
}
if (lastReceipt == null) {
return false;
}
long maxAgeMillis = MILLIS_PER_HOUR * maxAgeHours;
Date now = new Date();
return now.getTime() - lastReceipt.getTime() > maxAgeMillis;
private boolean isStale(OmemoDevice userDevice, OmemoDevice subject) {
return getStalenessStrategy().isStale(userDevice, subject);
}
/**

View file

@ -32,6 +32,7 @@ import org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement;
import org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException;
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
import org.jivesoftware.smackx.omemo.exceptions.NoIdentityKeyException;
import org.jivesoftware.smackx.omemo.exceptions.NoRawSessionException;
import org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;
@ -290,27 +291,6 @@ public abstract class OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_
*/
public abstract void removeOmemoIdentityKey(OmemoDevice userDevice, OmemoDevice contactsDevice);
/**
* Store the number of messages we sent to a device since we last received a message back.
* This counter gets reset to 0 whenever we receive a message from the contacts device.
*
* @param userDevice our omemoDevice.
* @param contactsDevice device of which we want to set the message counter.
* @param counter counter value.
*/
public abstract void storeOmemoMessageCounter(OmemoDevice userDevice, OmemoDevice contactsDevice, int counter);
/**
* Return the current value of the message counter.
* This counter represents the number of message we sent to the contactsDevice without getting a reply back.
* The default value for this counter is 0.
*
* @param userDevice our omemoDevice
* @param contactsDevice device of which we want to get the message counter.
* @return counter value.
*/
public abstract int loadOmemoMessageCounter(OmemoDevice userDevice, OmemoDevice contactsDevice);
/**
* Set the date of the last message that was received from a device.
*
@ -646,4 +626,22 @@ public abstract class OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_
return keyUtil().getFingerprintOfIdentityKey(identityKey);
}
/**
* Return the length of the sending chain of the session between userDevice and contactsDevice.
* This number corresponds to the number of messages that userDevice sent to contactsDevice without receiving an
* answer.
*
* @param userDevice our device
* @param contactsDevice contacts device
* @return length of sending chain
* @throws NoRawSessionException if no raw session exists between userDevice and contactsDevice
*/
public int getLengthOfSessionSendingChain(OmemoDevice userDevice, OmemoDevice contactsDevice) throws NoRawSessionException {
T_Sess session = loadRawSession(userDevice, contactsDevice);
if (session == null) {
throw new NoRawSessionException(contactsDevice, new NullPointerException());
}
return keyUtil().lengthOfSessionSendingChain(session);
}
}

View file

@ -0,0 +1,62 @@
/**
*
* Copyright 2019 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.omemo.util;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smackx.omemo.OmemoStore;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
/**
* This {@link StalenessStrategy} determines the staleness of a device by utilizing the ChainKey index of the sessions
* sending chain. If the chain key index exceeds a certain value, the device is considered to be stale.
* This means, that once we sent too many messages to the device without receiving an answer, we consider the device
* stale.
*/
public class ChainKeyIndexStalenessStrategy<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> implements StalenessStrategy {
private static final Logger LOGGER = Logger.getLogger(ChainKeyIndexStalenessStrategy.class.getName());
private final OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> store;
private final int maxChainKeyIndex;
/**
* Create a new {@link ChainKeyIndexStalenessStrategy}.
*
* @param store {@link OmemoStore} from which we can read the devices session.
* @param maxChainKeyIndex the maximum value of messages we are allowed to send to the device unanswered before
* we consider it stale.
*/
public ChainKeyIndexStalenessStrategy(OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> store, int maxChainKeyIndex) {
this.store = store;
this.maxChainKeyIndex = maxChainKeyIndex;
}
@Override
public boolean isStale(OmemoDevice userDevice, OmemoDevice contactsDevice) {
T_Sess sessionRecord = store.loadRawSession(userDevice, contactsDevice);
if (sessionRecord == null) {
return false;
}
int chainLength = store.keyUtil().lengthOfSessionSendingChain(sessionRecord);
LOGGER.log(Level.FINE, "Staleness factor of OMEMO session between "
+ userDevice + " and " + contactsDevice + ": " + chainLength + "/" + maxChainKeyIndex);
return chainLength >= maxChainKeyIndex;
}
}

View file

@ -0,0 +1,32 @@
/**
*
* Copyright 2019 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.omemo.util;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
/**
* Example {@link StalenessStrategy} which never rates OMEMO sessions as stale.
*
* Note: This should only be used for testing purposes.
*/
public class NeverStaleStalenessStrategy implements StalenessStrategy {
@Override
public boolean isStale(OmemoDevice userDevice, OmemoDevice contactsDevice) {
// Accept all sessions as non-stale
return false;
}
}

View file

@ -388,4 +388,13 @@ public abstract class OmemoKeyUtil<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
return value + added;
}
}
/**
* Returns the length of the sessions sending chain.
* This number equals the number of messages that we sent without receiving an answer.
*
* @param session session
* @return length of sending chain
*/
public abstract int lengthOfSessionSendingChain(T_Sess session);
}

View file

@ -0,0 +1,35 @@
/**
*
* Copyright 2019 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.omemo.util;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
/**
* This interface can be extended in order to implement different strategies to determine the staleness of an
* {@link OmemoDevice}. Stale devices SHOULD no longer be included in the list of recipients during message encryption.
*/
public interface StalenessStrategy {
/**
* Return true if the device is considered stale, false otherwise.
*
* @param userDevice our device
* @param contactsDevice device of a contact
* @return stale or not
*/
boolean isStale(OmemoDevice userDevice, OmemoDevice contactsDevice);
}

View file

@ -19,7 +19,6 @@ package org.jivesoftware.smackx.omemo;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import java.util.Date;
import java.util.HashSet;
import org.jivesoftware.smack.test.util.SmackTestSuite;
@ -31,9 +30,6 @@ import org.jxmpp.stringprep.XmppStringprepException;
public class OmemoServiceTest extends SmackTestSuite {
private static final long ONE_HOUR = 1000L * 60 * 60;
private static final int DELETE_STALE = OmemoConfiguration.getDeleteStaleDevicesAfterHours();
@Test(expected = IllegalStateException.class)
public void getInstanceFailsWhenNullTest() {
OmemoService.getInstance();
@ -44,28 +40,6 @@ public class OmemoServiceTest extends SmackTestSuite {
assertFalse(OmemoService.isServiceRegistered());
}
/**
* Test correct functionality of isStale method.
* @throws XmppStringprepException
*/
@Test
public void isStaleDeviceTest() throws XmppStringprepException {
OmemoDevice user = new OmemoDevice(JidCreate.bareFrom("alice@wonderland.lit"), 123);
OmemoDevice other = new OmemoDevice(JidCreate.bareFrom("bob@builder.tv"), 444);
Date now = new Date();
Date deleteMe = new Date(now.getTime() - ((DELETE_STALE + 1) * ONE_HOUR));
// Devices one hour "older" than max ages are stale
assertTrue(OmemoService.isStale(user, other, deleteMe, DELETE_STALE));
// Own device is never stale, no matter how old
assertFalse(OmemoService.isStale(user, user, deleteMe, DELETE_STALE));
// Always return false if date is null.
assertFalse(OmemoService.isStale(user, other, null, DELETE_STALE));
}
@Test
public void removeOurDeviceTest() throws XmppStringprepException {
OmemoDevice a = new OmemoDevice(JidCreate.bareFrom("a@b.c"), 123);
@ -76,8 +50,8 @@ public class OmemoServiceTest extends SmackTestSuite {
assertTrue(devices.contains(a));
assertTrue(devices.contains(b));
OmemoService.removeOurDevice(a, devices);
devices.remove(a);
assertFalse(devices.contains(a));
assertTrue(devices.contains(b));
}

View file

@ -314,13 +314,6 @@ public abstract class OmemoStoreTest<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey
assertNull(session);
}
@Test
public void loadStoreMessageCounterTest() {
assertEquals(0, store.loadOmemoMessageCounter(alice, bob));
store.storeOmemoMessageCounter(alice, bob, 20);
assertEquals(20, store.loadOmemoMessageCounter(alice, bob));
}
@Test
public void getFingerprint() throws IOException, CorruptedOmemoKeyException {
assertNull("Method must return null for a non-existent fingerprint.", store.getFingerprint(alice));