Temp commit

This commit is contained in:
Paul Schaub 2017-12-17 16:53:59 +01:00
parent 9a779ff8aa
commit 85171ee71b
14 changed files with 195 additions and 196 deletions

View File

@ -62,13 +62,13 @@ public class SignalOmemoRatchet
private static final Logger LOGGER = Logger.getLogger(OmemoRatchet.class.getName()); private static final Logger LOGGER = Logger.getLogger(OmemoRatchet.class.getName());
private final SignalOmemoStoreConnector storeConnector; private final SignalOmemoStoreConnector storeConnector;
SignalOmemoRatchet(OmemoManager.KnownBareJidGuard managerGuard, SignalOmemoRatchet(OmemoManager omemoManager,
OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord, OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord,
SessionRecord, SignalProtocolAddress, ECPublicKey, PreKeyBundle, SessionRecord, SignalProtocolAddress, ECPublicKey, PreKeyBundle,
SessionCipher> store) SessionCipher> store)
{ {
super(managerGuard, store); super(omemoManager, store);
this.storeConnector = new SignalOmemoStoreConnector(managerGuard, store); this.storeConnector = new SignalOmemoStoreConnector(omemoManager, store);
} }
@Override @Override
@ -157,8 +157,7 @@ public class SignalOmemoRatchet
} }
private SessionCipher getCipher(OmemoDevice device) { private SessionCipher getCipher(OmemoDevice device) {
SignalOmemoKeyUtil keyUtil = (SignalOmemoKeyUtil) store.keyUtil(); return new SessionCipher(storeConnector, storeConnector, storeConnector, storeConnector,
SignalProtocolAddress address = keyUtil.omemoDeviceAsAddress(device); SignalOmemoStoreConnector.asAddress(device));
return new SessionCipher(storeConnector, storeConnector, storeConnector, storeConnector, address);
} }
} }

View File

@ -66,7 +66,7 @@ public final class SignalOmemoService
@Override @Override
protected SignalOmemoRatchet instantiateOmemoRatchet( protected SignalOmemoRatchet instantiateOmemoRatchet(
OmemoManager.KnownBareJidGuard manager, OmemoManager manager,
OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord, SessionRecord, OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord, SessionRecord,
SignalProtocolAddress, ECPublicKey, PreKeyBundle, SessionCipher> store) SignalProtocolAddress, ECPublicKey, PreKeyBundle, SessionCipher> store)
{ {
@ -111,14 +111,14 @@ public final class SignalOmemoService
} }
@Override @Override
protected void processBundle(OmemoManager.KnownBareJidGuard managerGuard, protected void processBundle(OmemoManager.LoggedInOmemoManager managerGuard,
PreKeyBundle preKeyBundle, PreKeyBundle preKeyBundle,
OmemoDevice device) OmemoDevice device)
throws CorruptedOmemoKeyException throws CorruptedOmemoKeyException
{ {
SignalOmemoStoreConnector connector = new SignalOmemoStoreConnector(managerGuard, getOmemoStoreBackend()); SignalOmemoStoreConnector connector = new SignalOmemoStoreConnector(managerGuard.get(), getOmemoStoreBackend());
SessionBuilder builder = new SessionBuilder(connector, connector, connector, connector, SessionBuilder builder = new SessionBuilder(connector, connector, connector, connector,
getOmemoStoreBackend().keyUtil().omemoDeviceAsAddress(device)); SignalOmemoStoreConnector.asAddress(device));
try { try {
builder.process(preKeyBundle); builder.process(preKeyBundle);
LOGGER.log(Level.FINE, "Session built with " + device); LOGGER.log(Level.FINE, "Session built with " + device);

View File

@ -59,20 +59,20 @@ public class SignalOmemoStoreConnector
private static final Logger LOGGER = Logger.getLogger(SignalOmemoStoreConnector.class.getName()); private static final Logger LOGGER = Logger.getLogger(SignalOmemoStoreConnector.class.getName());
private final OmemoManager.KnownBareJidGuard managerGuard; private final OmemoManager omemoManager;
private final OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord, SessionRecord, private final OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord, SessionRecord,
SignalProtocolAddress, ECPublicKey, PreKeyBundle, SessionCipher> SignalProtocolAddress, ECPublicKey, PreKeyBundle, SessionCipher>
omemoStore; omemoStore;
public SignalOmemoStoreConnector(OmemoManager.KnownBareJidGuard managerGuard, OmemoStore<IdentityKeyPair, public SignalOmemoStoreConnector(OmemoManager omemoManager, OmemoStore<IdentityKeyPair,
IdentityKey, PreKeyRecord, SignedPreKeyRecord, SessionRecord, SignalProtocolAddress, ECPublicKey, IdentityKey, PreKeyRecord, SignedPreKeyRecord, SessionRecord, SignalProtocolAddress, ECPublicKey,
PreKeyBundle, SessionCipher> store) { PreKeyBundle, SessionCipher> store) {
this.managerGuard = managerGuard; this.omemoManager = omemoManager;
this.omemoStore = store; this.omemoStore = store;
} }
OmemoDevice getOurDevice() { OmemoDevice getOurDevice() {
return managerGuard.get().getOwnDevice(); return omemoManager.getOwnDevice();
} }
@Override @Override
@ -98,7 +98,7 @@ public class SignalOmemoStoreConnector
public boolean saveIdentity(SignalProtocolAddress signalProtocolAddress, IdentityKey identityKey) { public boolean saveIdentity(SignalProtocolAddress signalProtocolAddress, IdentityKey identityKey) {
OmemoDevice device; OmemoDevice device;
try { try {
device = omemoStore.keyUtil().addressAsOmemoDevice(signalProtocolAddress); device = asOmemoDevice(signalProtocolAddress);
} catch (XmppStringprepException e) { } catch (XmppStringprepException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
@ -150,7 +150,7 @@ public class SignalOmemoStoreConnector
public SessionRecord loadSession(SignalProtocolAddress signalProtocolAddress) { public SessionRecord loadSession(SignalProtocolAddress signalProtocolAddress) {
OmemoDevice device; OmemoDevice device;
try { try {
device = omemoStore.keyUtil().addressAsOmemoDevice(signalProtocolAddress); device = asOmemoDevice(signalProtocolAddress);
} catch (XmppStringprepException e) { } catch (XmppStringprepException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
@ -180,7 +180,7 @@ public class SignalOmemoStoreConnector
public void storeSession(SignalProtocolAddress signalProtocolAddress, SessionRecord sessionRecord) { public void storeSession(SignalProtocolAddress signalProtocolAddress, SessionRecord sessionRecord) {
OmemoDevice device; OmemoDevice device;
try { try {
device = omemoStore.keyUtil().addressAsOmemoDevice(signalProtocolAddress); device = asOmemoDevice(signalProtocolAddress);
} catch (XmppStringprepException e) { } catch (XmppStringprepException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
@ -192,7 +192,7 @@ public class SignalOmemoStoreConnector
public boolean containsSession(SignalProtocolAddress signalProtocolAddress) { public boolean containsSession(SignalProtocolAddress signalProtocolAddress) {
OmemoDevice device; OmemoDevice device;
try { try {
device = omemoStore.keyUtil().addressAsOmemoDevice(signalProtocolAddress); device = asOmemoDevice(signalProtocolAddress);
} catch (XmppStringprepException e) { } catch (XmppStringprepException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
@ -204,7 +204,7 @@ public class SignalOmemoStoreConnector
public void deleteSession(SignalProtocolAddress signalProtocolAddress) { public void deleteSession(SignalProtocolAddress signalProtocolAddress) {
OmemoDevice device; OmemoDevice device;
try { try {
device = omemoStore.keyUtil().addressAsOmemoDevice(signalProtocolAddress); device = asOmemoDevice(signalProtocolAddress);
} catch (XmppStringprepException e) { } catch (XmppStringprepException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
@ -259,4 +259,12 @@ public class SignalOmemoStoreConnector
public void removeSignedPreKey(int i) { public void removeSignedPreKey(int i) {
omemoStore.removeOmemoSignedPreKey(getOurDevice(), i); omemoStore.removeOmemoSignedPreKey(getOurDevice(), i);
} }
public static OmemoDevice asOmemoDevice(SignalProtocolAddress address) throws XmppStringprepException {
return new OmemoDevice(JidCreate.bareFrom(address.getName()), address.getDeviceId());
}
public static SignalProtocolAddress asAddress(OmemoDevice device) {
return new SignalProtocolAddress(device.getJid().toString(), device.getDeviceId());
}
} }

View File

@ -28,15 +28,11 @@ import static junit.framework.TestCase.fail;
import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException; import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
import org.jivesoftware.smackx.omemo.signal.SignalOmemoKeyUtil; import org.jivesoftware.smackx.omemo.signal.SignalOmemoKeyUtil;
import org.junit.Test; import org.junit.Test;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import org.whispersystems.libsignal.IdentityKey; import org.whispersystems.libsignal.IdentityKey;
import org.whispersystems.libsignal.IdentityKeyPair; import org.whispersystems.libsignal.IdentityKeyPair;
import org.whispersystems.libsignal.SignalProtocolAddress;
import org.whispersystems.libsignal.state.SignedPreKeyRecord; import org.whispersystems.libsignal.state.SignedPreKeyRecord;
/** /**
@ -97,26 +93,4 @@ public class LegacySignalOmemoKeyUtilTest extends SmackTestSuite {
assertTrue("Length of fingerprint must be 64.", assertTrue("Length of fingerprint must be 64.",
keyUtil.getFingerprintOfIdentityKey(ik).length() == 64); keyUtil.getFingerprintOfIdentityKey(ik).length() == 64);
} }
@Test
public void addressToDeviceTest() {
SignalProtocolAddress address = new SignalProtocolAddress("test@server.tld",1337);
try {
OmemoDevice device = keyUtil.addressAsOmemoDevice(address);
assertEquals(device, new OmemoDevice(JidCreate.bareFrom("test@server.tld"), 1337));
} catch (XmppStringprepException e) {
fail("Could not convert address to device: " + e + " " + e.getMessage());
}
}
@Test
public void deviceToAddressTest() {
try {
OmemoDevice device = new OmemoDevice(JidCreate.bareFrom("test@server.tld"), 1337);
SignalProtocolAddress address = keyUtil.omemoDeviceAsAddress(device);
assertEquals(address, new SignalProtocolAddress("test@server.tld", 1337));
} catch (XmppStringprepException e) {
fail("Could not convert device to address: " + e + " " + e.getMessage());
}
}
} }

View File

@ -33,6 +33,7 @@ import java.util.WeakHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.jivesoftware.smack.AbstractConnectionListener;
import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.StanzaListener; import org.jivesoftware.smack.StanzaListener;
@ -96,11 +97,12 @@ import org.jxmpp.jid.EntityFullJid;
public final class OmemoManager extends Manager { public final class OmemoManager extends Manager {
private static final Logger LOGGER = Logger.getLogger(OmemoManager.class.getName()); private static final Logger LOGGER = Logger.getLogger(OmemoManager.class.getName());
private static final Integer UNKNOWN_DEVICE_ID = -1;
private final Object LOCK = new Object();
private static final WeakHashMap<XMPPConnection, TreeMap<Integer,OmemoManager>> INSTANCES = new WeakHashMap<>(); private static final WeakHashMap<XMPPConnection, TreeMap<Integer,OmemoManager>> INSTANCES = new WeakHashMap<>();
private final OmemoService<?, ?, ?, ?, ?, ?, ?, ?, ?> service; private final OmemoService<?, ?, ?, ?, ?, ?, ?, ?, ?> service;
private final Object LOCK = new Object();
private final HashSet<OmemoMessageListener> omemoMessageListeners = new HashSet<>(); private final HashSet<OmemoMessageListener> omemoMessageListeners = new HashSet<>();
private final HashSet<OmemoMucMessageListener> omemoMucMessageListeners = new HashSet<>(); private final HashSet<OmemoMucMessageListener> omemoMucMessageListeners = new HashSet<>();
@ -110,20 +112,30 @@ public final class OmemoManager extends Manager {
private Integer deviceId; private Integer deviceId;
/** /**
* Private constructor to prevent multiple instances on a single connection (which probably would be bad!). * Private constructor.
* *
* @param connection connection * @param connection connection
* @param deviceId deviceId
*/ */
private OmemoManager(XMPPConnection connection, Integer deviceId) { private OmemoManager(XMPPConnection connection, Integer deviceId) {
super(connection); super(connection);
service = OmemoService.getInstance(); service = OmemoService.getInstance();
if (deviceId != null && deviceId <= 0) { this.deviceId = deviceId;
throw new IllegalArgumentException("DeviceId MUST be greater than 0.");
if (connection.isAuthenticated()) {
initBareJidAndDeviceId(this);
} else {
connection.addConnectionListener(new AbstractConnectionListener() {
@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
initBareJidAndDeviceId(OmemoManager.this);
}
});
} }
this.deviceId = deviceId; service.registerManager(this);
// StanzaListeners // StanzaListeners
startStanzaListeners(); startStanzaListeners();
@ -137,20 +149,20 @@ public final class OmemoManager extends Manager {
* If there was an OmemoManager for the connection and id before, return it. Otherwise create a new OmemoManager * If there was an OmemoManager for the connection and id before, return it. Otherwise create a new OmemoManager
* instance and return it. * instance and return it.
* @param connection XmppConnection. * @param connection XmppConnection.
* @param deviceId can be null, to generate a random deviceId. DeviceIds MUST NOT be less than 1. * @param deviceId MUST NOT be null and MUST be greater than 0.
* @return * @return
*/ */
public synchronized static OmemoManager getInstanceFor(XMPPConnection connection, Integer deviceId) { public synchronized static OmemoManager getInstanceFor(XMPPConnection connection, Integer deviceId) {
if (deviceId == null || deviceId < 1) {
throw new IllegalArgumentException("DeviceId MUST NOT be null and MUST be greater than 0.");
}
TreeMap<Integer,OmemoManager> managersOfConnection = INSTANCES.get(connection); TreeMap<Integer,OmemoManager> managersOfConnection = INSTANCES.get(connection);
if (managersOfConnection == null) { if (managersOfConnection == null) {
managersOfConnection = new TreeMap<>(); managersOfConnection = new TreeMap<>();
INSTANCES.put(connection, managersOfConnection); INSTANCES.put(connection, managersOfConnection);
} }
if (deviceId == null || deviceId < 1) {
deviceId = randomDeviceId();
}
OmemoManager manager = managersOfConnection.get(deviceId); OmemoManager manager = managersOfConnection.get(deviceId);
if (manager == null) { if (manager == null) {
manager = new OmemoManager(connection, deviceId); manager = new OmemoManager(connection, deviceId);
@ -177,8 +189,8 @@ public final class OmemoManager extends Manager {
OmemoManager manager; OmemoManager manager;
if (managers.size() == 0) { if (managers.size() == 0) {
manager = new OmemoManager(connection, null); manager = new OmemoManager(connection, UNKNOWN_DEVICE_ID);
managers.put(null, manager); managers.put(UNKNOWN_DEVICE_ID, manager);
} else { } else {
manager = managers.get(managers.firstKey()); manager = managers.get(managers.firstKey());
@ -218,14 +230,7 @@ public final class OmemoManager extends Manager {
throw new SmackException.NotLoggedInException(); throw new SmackException.NotLoggedInException();
} }
// Set jid and deviceId getOmemoService().publish(new LoggedInOmemoManager(this));
this.ownJid = connection().getUser().asBareJid();
if (deviceId == null) {
SortedSet<Integer> localDeviceIds = getOmemoService().getOmemoStoreBackend().localDeviceIdsOf(ownJid);
setDeviceId(localDeviceIds.size() > 0 ? localDeviceIds.first() : randomDeviceId());
}
getOmemoService().initialize(new KnownBareJidGuard(this));
} }
} }
@ -270,7 +275,7 @@ public final class OmemoManager extends Manager {
SmackException.NoResponseException, SmackException.NotLoggedInException SmackException.NoResponseException, SmackException.NotLoggedInException
{ {
synchronized (LOCK) { synchronized (LOCK) {
KnownBareJidGuard guard = new KnownBareJidGuard(this); LoggedInOmemoManager guard = new LoggedInOmemoManager(this);
Message plaintext = new Message(); Message plaintext = new Message();
plaintext.setBody(message); plaintext.setBody(message);
OmemoVAxolotlElement encrypted = getOmemoService().processSendingMessage(guard, to, plaintext); OmemoVAxolotlElement encrypted = getOmemoService().processSendingMessage(guard, to, plaintext);
@ -302,7 +307,7 @@ public final class OmemoManager extends Manager {
Message m = new Message(); Message m = new Message();
m.setBody(message); m.setBody(message);
OmemoVAxolotlElement encrypted = getOmemoService().processSendingMessage( OmemoVAxolotlElement encrypted = getOmemoService().processSendingMessage(
new KnownBareJidGuard(this), recipients, m); new LoggedInOmemoManager(this), recipients, m);
return finishMessage(encrypted); return finishMessage(encrypted);
} }
} }
@ -364,7 +369,7 @@ public final class OmemoManager extends Manager {
Message m = new Message(); Message m = new Message();
m.setBody(message); m.setBody(message);
OmemoVAxolotlElement encrypted = getOmemoService() OmemoVAxolotlElement encrypted = getOmemoService()
.encryptOmemoMessage(new KnownBareJidGuard(this), exception.getSuccesses(), m); .encryptOmemoMessage(new LoggedInOmemoManager(this), exception.getSuccesses(), m);
return finishMessage(encrypted); return finishMessage(encrypted);
} }
} }
@ -389,7 +394,7 @@ public final class OmemoManager extends Manager {
SmackException.NotLoggedInException SmackException.NotLoggedInException
{ {
synchronized (LOCK) { synchronized (LOCK) {
return getOmemoService().processLocalMessage(new KnownBareJidGuard(this), sender, omemoMessage); return getOmemoService().processLocalMessage(new LoggedInOmemoManager(this), sender, omemoMessage);
} }
} }
@ -410,7 +415,7 @@ public final class OmemoManager extends Manager {
{ {
synchronized (LOCK) { synchronized (LOCK) {
List<ClearTextMessage> l = new ArrayList<>(); List<ClearTextMessage> l = new ArrayList<>();
l.addAll(getOmemoService().decryptMamQueryResult(new KnownBareJidGuard(this), mamQueryResult)); l.addAll(getOmemoService().decryptMamQueryResult(new LoggedInOmemoManager(this), mamQueryResult));
return l; return l;
} }
} }
@ -489,7 +494,7 @@ public final class OmemoManager extends Manager {
throws SmackException, InterruptedException, XMPPException.XMPPErrorException, CorruptedOmemoKeyException throws SmackException, InterruptedException, XMPPException.XMPPErrorException, CorruptedOmemoKeyException
{ {
synchronized (LOCK) { synchronized (LOCK) {
KnownBareJidGuard managerGuard = new KnownBareJidGuard(this); LoggedInOmemoManager managerGuard = new LoggedInOmemoManager(this);
getOmemoService().publishDeviceIdIfNeeded(managerGuard, true); getOmemoService().publishDeviceIdIfNeeded(managerGuard, true);
getOmemoService().publishBundle(managerGuard); getOmemoService().publishBundle(managerGuard);
} }
@ -506,7 +511,7 @@ public final class OmemoManager extends Manager {
throws SmackException, InterruptedException, XMPPException.XMPPErrorException, CorruptedOmemoKeyException throws SmackException, InterruptedException, XMPPException.XMPPErrorException, CorruptedOmemoKeyException
{ {
synchronized (LOCK) { synchronized (LOCK) {
KnownBareJidGuard managerGuard = new KnownBareJidGuard(this); LoggedInOmemoManager managerGuard = new LoggedInOmemoManager(this);
// create a new identity and publish new keys to the server // create a new identity and publish new keys to the server
getOmemoService().regenerate(managerGuard); getOmemoService().regenerate(managerGuard);
getOmemoService().publishDeviceIdIfNeeded(managerGuard, false); getOmemoService().publishDeviceIdIfNeeded(managerGuard, false);
@ -530,7 +535,7 @@ public final class OmemoManager extends Manager {
{ {
synchronized (LOCK) { synchronized (LOCK) {
getOmemoService().sendOmemoRatchetUpdateMessage( getOmemoService().sendOmemoRatchetUpdateMessage(
new KnownBareJidGuard(this), recipient, false); new LoggedInOmemoManager(this), recipient, false);
} }
} }
@ -553,7 +558,7 @@ public final class OmemoManager extends Manager {
{ {
synchronized (LOCK) { synchronized (LOCK) {
return getOmemoService().prepareOmemoKeyTransportElement( return getOmemoService().prepareOmemoKeyTransportElement(
new KnownBareJidGuard(this), aesKey, iv, to); new LoggedInOmemoManager(this), aesKey, iv, to);
} }
} }
@ -605,7 +610,7 @@ public final class OmemoManager extends Manager {
SmackException.NotLoggedInException SmackException.NotLoggedInException
{ {
synchronized (LOCK) { synchronized (LOCK) {
KnownBareJidGuard managerGuard = new KnownBareJidGuard(this); LoggedInOmemoManager managerGuard = new LoggedInOmemoManager(this);
getOmemoService().refreshDeviceList(managerGuard, contact); getOmemoService().refreshDeviceList(managerGuard, contact);
return !getOmemoService().getOmemoStoreBackend().loadCachedDeviceList(getOwnDevice(), contact) return !getOmemoService().getOmemoStoreBackend().loadCachedDeviceList(getOwnDevice(), contact)
@ -681,7 +686,7 @@ public final class OmemoManager extends Manager {
return getOwnFingerprint(); return getOwnFingerprint();
} }
return getOmemoService().getOmemoStoreBackend().getFingerprint(new KnownBareJidGuard(this), device); return getOmemoService().getOmemoStoreBackend().getFingerprint(new LoggedInOmemoManager(this), device);
} }
} }
@ -746,7 +751,7 @@ public final class OmemoManager extends Manager {
SmackException.NoResponseException, SmackException.NotLoggedInException SmackException.NoResponseException, SmackException.NotLoggedInException
{ {
synchronized (LOCK) { synchronized (LOCK) {
getOmemoService().buildMissingOmemoSessions(new KnownBareJidGuard(this), contact); getOmemoService().buildMissingOmemoSessions(new LoggedInOmemoManager(this), contact);
} }
} }
@ -763,7 +768,7 @@ public final class OmemoManager extends Manager {
SmackException.NotLoggedInException SmackException.NotLoggedInException
{ {
synchronized (LOCK) { synchronized (LOCK) {
getOmemoService().refreshDeviceList(new KnownBareJidGuard(this), contact); getOmemoService().refreshDeviceList(new LoggedInOmemoManager(this), contact);
} }
} }
@ -785,7 +790,7 @@ public final class OmemoManager extends Manager {
PubSubException.NotALeafNodeException, SmackException.NotLoggedInException PubSubException.NotALeafNodeException, SmackException.NotLoggedInException
{ {
synchronized (LOCK) { synchronized (LOCK) {
KnownBareJidGuard managerGuard = new KnownBareJidGuard(this); LoggedInOmemoManager managerGuard = new LoggedInOmemoManager(this);
// generate key // generate key
getOmemoService().getOmemoStoreBackend().changeSignedPreKey(getOwnDevice()); getOmemoService().getOmemoStoreBackend().changeSignedPreKey(getOwnDevice());
// publish // publish
@ -940,12 +945,16 @@ public final class OmemoManager extends Manager {
*/ */
public void startStanzaListeners() { public void startStanzaListeners() {
PEPManager pepManager = PEPManager.getInstanceFor(connection()); PEPManager pepManager = PEPManager.getInstanceFor(connection());
pepManager.removePEPListener(deviceListUpdateListener);
pepManager.addPEPListener(deviceListUpdateListener);
connection().removeAsyncStanzaListener(internalOmemoMessageStanzaListener);
connection().addAsyncStanzaListener(internalOmemoMessageStanzaListener, omemoMessageStanzaFilter);
CarbonManager carbonManager = CarbonManager.getInstanceFor(connection()); CarbonManager carbonManager = CarbonManager.getInstanceFor(connection());
// Remove listeners to avoid them getting added twice
connection().removeAsyncStanzaListener(internalOmemoMessageStanzaListener);
carbonManager.removeCarbonCopyReceivedListener(internalOmemoCarbonCopyListener); carbonManager.removeCarbonCopyReceivedListener(internalOmemoCarbonCopyListener);
pepManager.removePEPListener(deviceListUpdateListener);
// Add listeners
pepManager.addPEPListener(deviceListUpdateListener);
connection().addAsyncStanzaListener(internalOmemoMessageStanzaListener, omemoMessageStanzaFilter);
carbonManager.addCarbonCopyReceivedListener(internalOmemoCarbonCopyListener); carbonManager.addCarbonCopyReceivedListener(internalOmemoCarbonCopyListener);
} }
@ -979,14 +988,14 @@ public final class OmemoManager extends Manager {
private final PEPListener deviceListUpdateListener = new PEPListener() { private final PEPListener deviceListUpdateListener = new PEPListener() {
KnownBareJidGuard managerGuard = null; LoggedInOmemoManager managerGuard = null;
@Override @Override
public void eventReceived(EntityBareJid from, EventElement event, Message message) { public void eventReceived(EntityBareJid from, EventElement event, Message message) {
if (managerGuard == null) { if (managerGuard == null) {
try { try {
managerGuard = new KnownBareJidGuard(OmemoManager.this); managerGuard = new LoggedInOmemoManager(OmemoManager.this);
} catch (SmackException.NotLoggedInException e) { } catch (SmackException.NotLoggedInException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
@ -1066,7 +1075,7 @@ public final class OmemoManager extends Manager {
public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException { public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
try { try {
getOmemoService().onOmemoMessageStanzaReceived(packet, getOmemoService().onOmemoMessageStanzaReceived(packet,
new KnownBareJidGuard(OmemoManager.this)); new LoggedInOmemoManager(OmemoManager.this));
} catch (SmackException.NotLoggedInException e) { } catch (SmackException.NotLoggedInException e) {
LOGGER.warning("Received OMEMO stanza while being offline: " + e); LOGGER.warning("Received OMEMO stanza while being offline: " + e);
} }
@ -1081,7 +1090,7 @@ public final class OmemoManager extends Manager {
if (omemoMessageStanzaFilter.accept(carbonCopy)) { if (omemoMessageStanzaFilter.accept(carbonCopy)) {
try { try {
getOmemoService().onOmemoCarbonCopyReceived(direction, carbonCopy, wrappingMessage, getOmemoService().onOmemoCarbonCopyReceived(direction, carbonCopy, wrappingMessage,
new KnownBareJidGuard(OmemoManager.this)); new LoggedInOmemoManager(OmemoManager.this));
} catch (SmackException.NotLoggedInException e) { } catch (SmackException.NotLoggedInException e) {
LOGGER.warning("Received OMEMO carbon copy while being offline: " + e); LOGGER.warning("Received OMEMO carbon copy while being offline: " + e);
} }
@ -1099,11 +1108,11 @@ public final class OmemoManager extends Manager {
} }
}; };
public static class KnownBareJidGuard { public static class LoggedInOmemoManager {
private final OmemoManager manager; private final OmemoManager manager;
public KnownBareJidGuard(OmemoManager manager) public LoggedInOmemoManager(OmemoManager manager)
throws SmackException.NotLoggedInException { throws SmackException.NotLoggedInException {
if (manager == null) { if (manager == null) {
@ -1132,4 +1141,23 @@ public final class OmemoManager extends Manager {
void initializationFailed(Exception cause); void initializationFailed(Exception cause);
} }
private static void initBareJidAndDeviceId(OmemoManager manager) {
if (!manager.getConnection().isAuthenticated()) {
throw new IllegalStateException("Connection MUST be authenticated.");
}
if (manager.ownJid == null) {
manager.ownJid = manager.getConnection().getUser().asBareJid();
}
if (UNKNOWN_DEVICE_ID.equals(manager.deviceId)) {
SortedSet<Integer> storedDeviceIds = manager.getOmemoService().getOmemoStoreBackend().localDeviceIdsOf(manager.ownJid);
if (storedDeviceIds.size() > 0) {
manager.setDeviceId(storedDeviceIds.first());
} else {
manager.setDeviceId(randomDeviceId());
}
}
}
} }

View File

@ -39,12 +39,12 @@ import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
public abstract class OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> { public abstract class OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> {
private static final Logger LOGGER = Logger.getLogger(OmemoRatchet.class.getName()); private static final Logger LOGGER = Logger.getLogger(OmemoRatchet.class.getName());
protected final OmemoManager.KnownBareJidGuard managerGuard; protected final OmemoManager omemoManager;
protected final OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> store; protected final OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> store;
public OmemoRatchet(OmemoManager.KnownBareJidGuard managerGuard, public OmemoRatchet(OmemoManager omemoManager,
OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> store) { OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> store) {
this.managerGuard = managerGuard; this.omemoManager = omemoManager;
this.store = store; this.store = store;
} }
@ -58,13 +58,13 @@ public abstract class OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* Try to decrypt the transported message key using the double ratchet session. * Try to decrypt the transported message key using the double ratchet session.
* *
* @param element omemoElement * @param element omemoElement
* @return tuple of cipher generated from the unpacked message key and the authtag * @return tuple of cipher generated from the unpacked message key and the auth-tag
* @throws CryptoFailedException if decryption using the double ratchet fails * @throws CryptoFailedException if decryption using the double ratchet fails
* @throws NoRawSessionException if we have no session, but the element was NOT a PreKeyMessage * @throws NoRawSessionException if we have no session, but the element was NOT a PreKeyMessage
*/ */
public CipherAndAuthTag retrieveMessageKeyAndAuthTag(OmemoDevice sender, OmemoElement element) throws CryptoFailedException, CipherAndAuthTag retrieveMessageKeyAndAuthTag(OmemoDevice sender, OmemoElement element) throws CryptoFailedException,
NoRawSessionException { NoRawSessionException {
int keyId = managerGuard.get().getDeviceId(); int keyId = omemoManager.getDeviceId();
byte[] unpackedKey = null; byte[] unpackedKey = null;
List<CryptoFailedException> decryptExceptions = new ArrayList<>(); List<CryptoFailedException> decryptExceptions = new ArrayList<>();
List<OmemoElement.OmemoHeader.Key> keys = element.getHeader().getKeys(); List<OmemoElement.OmemoHeader.Key> keys = element.getHeader().getKeys();
@ -124,7 +124,7 @@ public abstract class OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @return Message containing the decrypted payload in its body. * @return Message containing the decrypted payload in its body.
* @throws CryptoFailedException * @throws CryptoFailedException
*/ */
public static Message decryptMessageElement(OmemoElement element, CipherAndAuthTag cipherAndAuthTag) throws CryptoFailedException { static Message decryptMessageElement(OmemoElement element, CipherAndAuthTag cipherAndAuthTag) throws CryptoFailedException {
if (!element.isMessageElement()) { if (!element.isMessageElement()) {
throw new IllegalArgumentException("decryptMessageElement cannot decrypt OmemoElement which is no MessageElement!"); throw new IllegalArgumentException("decryptMessageElement cannot decrypt OmemoElement which is no MessageElement!");
} }
@ -133,10 +133,8 @@ public abstract class OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
throw new CryptoFailedException("AuthenticationTag is null or has wrong length: " throw new CryptoFailedException("AuthenticationTag is null or has wrong length: "
+ (cipherAndAuthTag.getAuthTag() == null ? "null" : cipherAndAuthTag.getAuthTag().length)); + (cipherAndAuthTag.getAuthTag() == null ? "null" : cipherAndAuthTag.getAuthTag().length));
} }
byte[] encryptedBody = new byte[element.getPayload().length + 16];
byte[] payload = element.getPayload(); byte[] encryptedBody = payloadAndAuthTag(element, cipherAndAuthTag.getAuthTag());
System.arraycopy(payload, 0, encryptedBody, 0, payload.length);
System.arraycopy(cipherAndAuthTag.getAuthTag(), 0, encryptedBody, payload.length, 16);
try { try {
String plaintext = new String(cipherAndAuthTag.getCipher().doFinal(encryptedBody), StringUtils.UTF8); String plaintext = new String(cipherAndAuthTag.getCipher().doFinal(encryptedBody), StringUtils.UTF8);
@ -149,4 +147,16 @@ public abstract class OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
+ e.getMessage()); + e.getMessage());
} }
} }
static byte[] payloadAndAuthTag(OmemoElement element, byte[] authTag) {
if (!element.isMessageElement()) {
throw new IllegalArgumentException("OmemoElement has no payload.");
}
byte[] payload = new byte[element.getPayload().length + authTag.length];
System.arraycopy(element.getPayload(), 0, payload, 0, element.getPayload().length);
System.arraycopy(authTag, 0, payload, payload.length, authTag.length);
return payload;
}
} }

View File

@ -114,7 +114,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
private static OmemoService<?, ?, ?, ?, ?, ?, ?, ?, ?> INSTANCE; private static OmemoService<?, ?, ?, ?, ?, ?, ?, ?, ?> INSTANCE;
protected OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> omemoStore; protected OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> omemoStore;
protected final HashMap<OmemoManager, OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph>> sessionManagers = new HashMap<>(); protected final HashMap<OmemoManager, OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph>> omemoRatchets = new HashMap<>();
/** /**
* Return the singleton instance of this class. When no instance is set, throw an IllegalStateException instead. * Return the singleton instance of this class. When no instance is set, throw an IllegalStateException instead.
@ -206,6 +206,10 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
checkAvailableAlgorithms(); checkAvailableAlgorithms();
} }
void registerManager(OmemoManager manager) {
omemoRatchets.put(manager, instantiateOmemoRatchet(manager, getOmemoStoreBackend()));
}
/** /**
* Initialize OMEMO functionality for OmemoManager omemoManager. * Initialize OMEMO functionality for OmemoManager omemoManager.
* *
@ -218,12 +222,11 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws SmackException.NotLoggedInException * @throws SmackException.NotLoggedInException
* @throws PubSubException.NotALeafNodeException * @throws PubSubException.NotALeafNodeException
*/ */
void initialize(OmemoManager.KnownBareJidGuard managerGuard) void publish(OmemoManager.LoggedInOmemoManager managerGuard)
throws InterruptedException, CorruptedOmemoKeyException, XMPPException.XMPPErrorException, throws InterruptedException, CorruptedOmemoKeyException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, SmackException.NoResponseException, SmackException.NotConnectedException, SmackException.NoResponseException,
PubSubException.NotALeafNodeException PubSubException.NotALeafNodeException
{ {
sessionManagers.put(managerGuard.get(), instantiateOmemoRatchet(managerGuard, getOmemoStoreBackend()));
// Create new keys if necessary and publish to the server. // Create new keys if necessary and publish to the server.
publishBundle(managerGuard); publishBundle(managerGuard);
@ -260,9 +263,9 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws CorruptedOmemoKeyException when freshly generated identityKey is invalid * @throws CorruptedOmemoKeyException when freshly generated identityKey is invalid
* (should never ever happen *crosses fingers*) * (should never ever happen *crosses fingers*)
*/ */
void regenerate(OmemoManager.KnownBareJidGuard managerGuard) void regenerate(OmemoManager.LoggedInOmemoManager managerGuard)
throws CorruptedOmemoKeyException throws CorruptedOmemoKeyException, InterruptedException, PubSubException.NotALeafNodeException,
{ XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException {
OmemoManager omemoManager = managerGuard.get(); OmemoManager omemoManager = managerGuard.get();
OmemoDevice userDevice = omemoManager.getOwnDevice(); OmemoDevice userDevice = omemoManager.getOwnDevice();
@ -275,6 +278,8 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
getOmemoStoreBackend().purgeOwnDeviceKeys(userDevice); getOmemoStoreBackend().purgeOwnDeviceKeys(userDevice);
omemoManager.setDeviceId(nDeviceId); omemoManager.setDeviceId(nDeviceId);
publish(managerGuard);
} }
/** /**
@ -287,7 +292,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws CorruptedOmemoKeyException * @throws CorruptedOmemoKeyException
* @throws XMPPException.XMPPErrorException * @throws XMPPException.XMPPErrorException
*/ */
void publishBundle(OmemoManager.KnownBareJidGuard managerGuard) void publishBundle(OmemoManager.LoggedInOmemoManager managerGuard)
throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException,
CorruptedOmemoKeyException, XMPPException.XMPPErrorException CorruptedOmemoKeyException, XMPPException.XMPPErrorException
{ {
@ -325,7 +330,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws SmackException.NotConnectedException * @throws SmackException.NotConnectedException
* @throws SmackException.NoResponseException * @throws SmackException.NoResponseException
*/ */
void publishDeviceIdIfNeeded(OmemoManager.KnownBareJidGuard managerGuard, boolean deleteOtherDevices) void publishDeviceIdIfNeeded(OmemoManager.LoggedInOmemoManager managerGuard, boolean deleteOtherDevices)
throws InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException, throws InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, SmackException.NoResponseException SmackException.NotConnectedException, SmackException.NoResponseException
{ {
@ -346,7 +351,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws XMPPException.XMPPErrorException * @throws XMPPException.XMPPErrorException
* @throws PubSubException.NotALeafNodeException * @throws PubSubException.NotALeafNodeException
*/ */
void publishDeviceIdIfNeeded(OmemoManager.KnownBareJidGuard managerGuard, boolean deleteOtherDevices, boolean publish) void publishDeviceIdIfNeeded(OmemoManager.LoggedInOmemoManager managerGuard, boolean deleteOtherDevices, boolean publish)
throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException,
XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException
{ {
@ -386,7 +391,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @param deviceListIds deviceIds we plan to publish. Stale devices are deleted from that list. * @param deviceListIds deviceIds we plan to publish. Stale devices are deleted from that list.
* @return * @return
*/ */
boolean removeStaleDevicesIfNeeded(OmemoManager.KnownBareJidGuard managerGuard, Set<Integer> deviceListIds) { boolean removeStaleDevicesIfNeeded(OmemoManager.LoggedInOmemoManager managerGuard, Set<Integer> deviceListIds) {
OmemoManager omemoManager = managerGuard.get(); OmemoManager omemoManager = managerGuard.get();
OmemoDevice userDevice = omemoManager.getOwnDevice(); OmemoDevice userDevice = omemoManager.getOwnDevice();
boolean publish = false; boolean publish = false;
@ -433,7 +438,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws SmackException.NoResponseException Exception * @throws SmackException.NoResponseException Exception
* @throws PubSubException.NotALeafNodeException Exception * @throws PubSubException.NotALeafNodeException Exception
*/ */
static void publishDeviceIds(OmemoManager.KnownBareJidGuard managerGuard, OmemoDeviceListElement deviceList) static void publishDeviceIds(OmemoManager.LoggedInOmemoManager managerGuard, OmemoDeviceListElement deviceList)
throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException,
SmackException.NoResponseException, PubSubException.NotALeafNodeException SmackException.NoResponseException, PubSubException.NotALeafNodeException
{ {
@ -455,7 +460,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws SmackException.NoResponseException * @throws SmackException.NoResponseException
* @throws NotAPubSubNodeException * @throws NotAPubSubNodeException
*/ */
static LeafNode fetchDeviceListNode(OmemoManager.KnownBareJidGuard managerGuard, BareJid contact) static LeafNode fetchDeviceListNode(OmemoManager.LoggedInOmemoManager managerGuard, BareJid contact)
throws InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException, throws InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, SmackException.NoResponseException, NotAPubSubNodeException SmackException.NotConnectedException, SmackException.NoResponseException, NotAPubSubNodeException
{ {
@ -476,7 +481,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws PubSubException.NotALeafNodeException when the device lists node is not a LeafNode * @throws PubSubException.NotALeafNodeException when the device lists node is not a LeafNode
* @throws NotAPubSubNodeException * @throws NotAPubSubNodeException
*/ */
static OmemoDeviceListElement fetchDeviceList(OmemoManager.KnownBareJidGuard managerGuard, BareJid contact) static OmemoDeviceListElement fetchDeviceList(OmemoManager.LoggedInOmemoManager managerGuard, BareJid contact)
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
SmackException.NoResponseException, PubSubException.NotALeafNodeException, NotAPubSubNodeException SmackException.NoResponseException, PubSubException.NotALeafNodeException, NotAPubSubNodeException
{ {
@ -493,7 +498,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws InterruptedException * @throws InterruptedException
* @throws SmackException.NoResponseException * @throws SmackException.NoResponseException
*/ */
private boolean refreshOwnDeviceList(OmemoManager.KnownBareJidGuard managerGuard) private boolean refreshOwnDeviceList(OmemoManager.LoggedInOmemoManager managerGuard)
throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException,
XMPPException.XMPPErrorException XMPPException.XMPPErrorException
{ {
@ -536,7 +541,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws InterruptedException * @throws InterruptedException
* @throws SmackException.NoResponseException * @throws SmackException.NoResponseException
*/ */
void refreshDeviceList(OmemoManager.KnownBareJidGuard managerGuard, BareJid contact) void refreshDeviceList(OmemoManager.LoggedInOmemoManager managerGuard, BareJid contact)
throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException
{ {
OmemoDeviceListElement omemoDeviceListElement; OmemoDeviceListElement omemoDeviceListElement;
@ -565,7 +570,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws PubSubException.NotALeafNodeException when the bundles node is not a LeafNode * @throws PubSubException.NotALeafNodeException when the bundles node is not a LeafNode
* @throws NotAPubSubNodeException * @throws NotAPubSubNodeException
*/ */
static OmemoBundleVAxolotlElement fetchBundle(OmemoManager.KnownBareJidGuard managerGuard, OmemoDevice contact) static OmemoBundleVAxolotlElement fetchBundle(OmemoManager.LoggedInOmemoManager managerGuard, OmemoDevice contact)
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
SmackException.NoResponseException, PubSubException.NotALeafNodeException, NotAPubSubNodeException SmackException.NoResponseException, PubSubException.NotALeafNodeException, NotAPubSubNodeException
{ {
@ -642,7 +647,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @param managerGuard omemoManager * @param managerGuard omemoManager
* @param jid the BareJid of the contact * @param jid the BareJid of the contact
*/ */
void buildMissingOmemoSessions(OmemoManager.KnownBareJidGuard managerGuard, BareJid jid) void buildMissingOmemoSessions(OmemoManager.LoggedInOmemoManager managerGuard, BareJid jid)
throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException,
CannotEstablishOmemoSessionException CannotEstablishOmemoSessionException
{ {
@ -702,7 +707,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws CannotEstablishOmemoSessionException when no session could be established * @throws CannotEstablishOmemoSessionException when no session could be established
* @throws CorruptedOmemoKeyException when the bundle contained an invalid OMEMO identityKey * @throws CorruptedOmemoKeyException when the bundle contained an invalid OMEMO identityKey
*/ */
public void buildSessionWithDevice(OmemoManager.KnownBareJidGuard managerGuard, public void buildSessionWithDevice(OmemoManager.LoggedInOmemoManager managerGuard,
OmemoDevice contactsDevice, OmemoDevice contactsDevice,
boolean fresh) boolean fresh)
throws CannotEstablishOmemoSessionException, CorruptedOmemoKeyException throws CannotEstablishOmemoSessionException, CorruptedOmemoKeyException
@ -743,7 +748,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @param device OmemoDevice * @param device OmemoDevice
* @throws CorruptedOmemoKeyException * @throws CorruptedOmemoKeyException
*/ */
protected abstract void processBundle(OmemoManager.KnownBareJidGuard managerGuard, protected abstract void processBundle(OmemoManager.LoggedInOmemoManager managerGuard,
T_Bundle bundle, T_Bundle bundle,
OmemoDevice device) OmemoDevice device)
throws CorruptedOmemoKeyException; throws CorruptedOmemoKeyException;
@ -764,7 +769,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws XMPPException.XMPPErrorException * @throws XMPPException.XMPPErrorException
* @throws CorruptedOmemoKeyException * @throws CorruptedOmemoKeyException
*/ */
private Message processReceivingMessage(OmemoManager.KnownBareJidGuard managerGuard, private Message processReceivingMessage(OmemoManager.LoggedInOmemoManager managerGuard,
OmemoDevice contactsDevice, OmemoDevice contactsDevice,
OmemoElement message, OmemoElement message,
final OmemoMessageInformation information) final OmemoMessageInformation information)
@ -811,7 +816,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws NoRawSessionException When there is no session to decrypt the message with in the double * @throws NoRawSessionException When there is no session to decrypt the message with in the double
* ratchet library * ratchet library
*/ */
ClearTextMessage processLocalMessage(OmemoManager.KnownBareJidGuard managerGuard, ClearTextMessage processLocalMessage(OmemoManager.LoggedInOmemoManager managerGuard,
BareJid sender, BareJid sender,
Message message) Message message)
throws InterruptedException, SmackException.NoResponseException, SmackException.NotConnectedException, throws InterruptedException, SmackException.NoResponseException, SmackException.NotConnectedException,
@ -842,7 +847,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws UndecidedOmemoIdentityException * @throws UndecidedOmemoIdentityException
* @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
*/ */
OmemoVAxolotlElement processSendingMessage(OmemoManager.KnownBareJidGuard managerGuard, OmemoVAxolotlElement processSendingMessage(OmemoManager.LoggedInOmemoManager managerGuard,
BareJid recipient, BareJid recipient,
Message message) Message message)
throws CryptoFailedException, UndecidedOmemoIdentityException, NoSuchAlgorithmException, throws CryptoFailedException, UndecidedOmemoIdentityException, NoSuchAlgorithmException,
@ -866,7 +871,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws UndecidedOmemoIdentityException * @throws UndecidedOmemoIdentityException
* @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
*/ */
OmemoVAxolotlElement processSendingMessage(OmemoManager.KnownBareJidGuard managerGuard, OmemoVAxolotlElement processSendingMessage(OmemoManager.LoggedInOmemoManager managerGuard,
ArrayList<BareJid> recipients, ArrayList<BareJid> recipients,
Message message) Message message)
throws CryptoFailedException, UndecidedOmemoIdentityException, NoSuchAlgorithmException, throws CryptoFailedException, UndecidedOmemoIdentityException, NoSuchAlgorithmException,
@ -972,7 +977,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws SmackException.NoResponseException * @throws SmackException.NoResponseException
* @throws NoRawSessionException * @throws NoRawSessionException
*/ */
private Message decryptOmemoMessageElement(OmemoManager.KnownBareJidGuard managerGuard, private Message decryptOmemoMessageElement(OmemoManager.LoggedInOmemoManager managerGuard,
OmemoDevice from, OmemoDevice from,
OmemoElement message, OmemoElement message,
final OmemoMessageInformation information) final OmemoMessageInformation information)
@ -1000,7 +1005,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws SmackException.NotConnectedException * @throws SmackException.NotConnectedException
* @throws SmackException.NoResponseException * @throws SmackException.NoResponseException
*/ */
private CipherAndAuthTag decryptTransportedOmemoKey(OmemoManager.KnownBareJidGuard managerGuard, private CipherAndAuthTag decryptTransportedOmemoKey(OmemoManager.LoggedInOmemoManager managerGuard,
OmemoDevice sender, OmemoDevice sender,
OmemoElement omemoMessage, OmemoElement omemoMessage,
OmemoMessageInformation messageInfo) OmemoMessageInformation messageInfo)
@ -1012,7 +1017,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
int preKeyCountBefore = getOmemoStoreBackend().loadOmemoPreKeys(userDevice).size(); int preKeyCountBefore = getOmemoStoreBackend().loadOmemoPreKeys(userDevice).size();
CipherAndAuthTag cipherAndAuthTag = sessionManagers.get(managerGuard.get()).retrieveMessageKeyAndAuthTag(sender, omemoMessage); CipherAndAuthTag cipherAndAuthTag = omemoRatchets.get(managerGuard.get()).retrieveMessageKeyAndAuthTag(sender, omemoMessage);
messageInfo.setSenderDevice(sender); messageInfo.setSenderDevice(sender);
messageInfo.setSenderFingerprint(omemoStore.keyUtil().getFingerprintOfIdentityKey( messageInfo.setSenderFingerprint(omemoStore.keyUtil().getFingerprintOfIdentityKey(
@ -1037,7 +1042,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* *
* @return OmemoMessageElement * @return OmemoMessageElement
*/ */
OmemoVAxolotlElement encryptOmemoMessage(OmemoManager.KnownBareJidGuard managerGuard, OmemoVAxolotlElement encryptOmemoMessage(OmemoManager.LoggedInOmemoManager managerGuard,
HashMap<BareJid, ArrayList<OmemoDevice>> recipients, HashMap<BareJid, ArrayList<OmemoDevice>> recipients,
Message message) Message message)
throws CryptoFailedException, UndecidedOmemoIdentityException throws CryptoFailedException, UndecidedOmemoIdentityException
@ -1045,7 +1050,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph>
builder; builder;
try { try {
builder = new OmemoMessageBuilder<>(managerGuard, getOmemoRatchet(managerGuard), message.getBody()); builder = new OmemoMessageBuilder<>(managerGuard, getOmemoRatchet(managerGuard.get()), message.getBody());
} catch (UnsupportedEncodingException | BadPaddingException | IllegalBlockSizeException | NoSuchProviderException | } catch (UnsupportedEncodingException | BadPaddingException | IllegalBlockSizeException | NoSuchProviderException |
NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException e) { NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException e) {
throw new CryptoFailedException(e); throw new CryptoFailedException(e);
@ -1090,7 +1095,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws CorruptedOmemoKeyException * @throws CorruptedOmemoKeyException
* @throws CannotEstablishOmemoSessionException * @throws CannotEstablishOmemoSessionException
*/ */
OmemoVAxolotlElement prepareOmemoKeyTransportElement(OmemoManager.KnownBareJidGuard managerGuard, OmemoVAxolotlElement prepareOmemoKeyTransportElement(OmemoManager.LoggedInOmemoManager managerGuard,
OmemoDevice... recipients) OmemoDevice... recipients)
throws CryptoFailedException, UndecidedOmemoIdentityException, CorruptedOmemoKeyException, throws CryptoFailedException, UndecidedOmemoIdentityException, CorruptedOmemoKeyException,
CannotEstablishOmemoSessionException CannotEstablishOmemoSessionException
@ -1098,7 +1103,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph>
builder; builder;
try { try {
builder = new OmemoMessageBuilder<>(managerGuard, getOmemoRatchet(managerGuard), null); builder = new OmemoMessageBuilder<>(managerGuard, getOmemoRatchet(managerGuard.get()), null);
} catch (UnsupportedEncodingException | BadPaddingException | IllegalBlockSizeException | NoSuchProviderException | } catch (UnsupportedEncodingException | BadPaddingException | IllegalBlockSizeException | NoSuchProviderException |
NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException e) { NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException e) {
@ -1125,7 +1130,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws CorruptedOmemoKeyException * @throws CorruptedOmemoKeyException
* @throws CannotEstablishOmemoSessionException * @throws CannotEstablishOmemoSessionException
*/ */
OmemoVAxolotlElement prepareOmemoKeyTransportElement(OmemoManager.KnownBareJidGuard managerGuard, OmemoVAxolotlElement prepareOmemoKeyTransportElement(OmemoManager.LoggedInOmemoManager managerGuard,
byte[] aesKey, byte[] aesKey,
byte[] iv, byte[] iv,
OmemoDevice... recipients) OmemoDevice... recipients)
@ -1135,7 +1140,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph>
builder; builder;
try { try {
builder = new OmemoMessageBuilder<>(managerGuard, getOmemoRatchet(managerGuard), aesKey, iv); builder = new OmemoMessageBuilder<>(managerGuard, getOmemoRatchet(managerGuard.get()), aesKey, iv);
} catch (UnsupportedEncodingException | BadPaddingException | IllegalBlockSizeException | NoSuchProviderException | } catch (UnsupportedEncodingException | BadPaddingException | IllegalBlockSizeException | NoSuchProviderException |
NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException e) { NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException e) {
@ -1162,7 +1167,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws CryptoFailedException * @throws CryptoFailedException
* @throws UndecidedOmemoIdentityException * @throws UndecidedOmemoIdentityException
*/ */
protected Message getOmemoRatchetUpdateMessage(OmemoManager.KnownBareJidGuard managerGuard, protected Message getOmemoRatchetUpdateMessage(OmemoManager.LoggedInOmemoManager managerGuard,
OmemoDevice recipient, OmemoDevice recipient,
boolean preKeyMessage) boolean preKeyMessage)
throws CannotEstablishOmemoSessionException, CorruptedOmemoKeyException, CryptoFailedException, throws CannotEstablishOmemoSessionException, CorruptedOmemoKeyException, CryptoFailedException,
@ -1191,7 +1196,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws CryptoFailedException * @throws CryptoFailedException
* @throws CannotEstablishOmemoSessionException * @throws CannotEstablishOmemoSessionException
*/ */
protected void sendOmemoRatchetUpdateMessage(OmemoManager.KnownBareJidGuard managerGuard, protected void sendOmemoRatchetUpdateMessage(OmemoManager.LoggedInOmemoManager managerGuard,
OmemoDevice recipient, OmemoDevice recipient,
boolean preKeyMessage) boolean preKeyMessage)
throws UndecidedOmemoIdentityException, CorruptedOmemoKeyException, CryptoFailedException, throws UndecidedOmemoIdentityException, CorruptedOmemoKeyException, CryptoFailedException,
@ -1220,7 +1225,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws SmackException.NotConnectedException * @throws SmackException.NotConnectedException
* @throws SmackException.NoResponseException * @throws SmackException.NoResponseException
*/ */
List<ClearTextMessage> decryptMamQueryResult(OmemoManager.KnownBareJidGuard managerGuard, List<ClearTextMessage> decryptMamQueryResult(OmemoManager.LoggedInOmemoManager managerGuard,
MamManager.MamQueryResult mamQueryResult) MamManager.MamQueryResult mamQueryResult)
throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException,
SmackException.NoResponseException SmackException.NoResponseException
@ -1255,7 +1260,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @param stanza message * @param stanza message
* @return BareJid of the sender. * @return BareJid of the sender.
*/ */
private static OmemoDevice getSender(OmemoManager.KnownBareJidGuard managerGuard, private static OmemoDevice getSender(OmemoManager.LoggedInOmemoManager managerGuard,
Stanza stanza) { Stanza stanza) {
OmemoElement omemoElement = stanza.getExtension(OmemoElement.ENCRYPTED, OMEMO_NAMESPACE_V_AXOLOTL); OmemoElement omemoElement = stanza.getExtension(OmemoElement.ENCRYPTED, OMEMO_NAMESPACE_V_AXOLOTL);
Jid sender = stanza.getFrom(); Jid sender = stanza.getFrom();
@ -1276,7 +1281,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @param stanza stanza in question * @param stanza stanza in question
* @return true if MUC message, otherwise false. * @return true if MUC message, otherwise false.
*/ */
private static boolean isMucMessage(OmemoManager.KnownBareJidGuard managerGuard, Stanza stanza) { private static boolean isMucMessage(OmemoManager.LoggedInOmemoManager managerGuard, Stanza stanza) {
BareJid sender = stanza.getFrom().asBareJid(); BareJid sender = stanza.getFrom().asBareJid();
MultiUserChatManager mucm = MultiUserChatManager.getInstanceFor(managerGuard.get().getConnection()); MultiUserChatManager mucm = MultiUserChatManager.getInstanceFor(managerGuard.get().getConnection());
@ -1284,7 +1289,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
} }
@Override @Override
public void onOmemoMessageStanzaReceived(Stanza stanza, OmemoManager.KnownBareJidGuard managerGuard) { public void onOmemoMessageStanzaReceived(Stanza stanza, OmemoManager.LoggedInOmemoManager managerGuard) {
OmemoManager omemoManager = managerGuard.get(); OmemoManager omemoManager = managerGuard.get();
Message decrypted; Message decrypted;
OmemoElement omemoMessage = stanza.getExtension(OmemoElement.ENCRYPTED, OMEMO_NAMESPACE_V_AXOLOTL); OmemoElement omemoMessage = stanza.getExtension(OmemoElement.ENCRYPTED, OMEMO_NAMESPACE_V_AXOLOTL);
@ -1360,7 +1365,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
public void onOmemoCarbonCopyReceived(CarbonExtension.Direction direction, public void onOmemoCarbonCopyReceived(CarbonExtension.Direction direction,
Message carbonCopy, Message carbonCopy,
Message wrappingMessage, Message wrappingMessage,
final OmemoManager.KnownBareJidGuard managerGuard) final OmemoManager.LoggedInOmemoManager managerGuard)
{ {
OmemoManager omemoManager = managerGuard.get(); OmemoManager omemoManager = managerGuard.get();
@ -1445,17 +1450,17 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
} }
protected abstract OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> protected abstract OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph>
instantiateOmemoRatchet(OmemoManager.KnownBareJidGuard manager, instantiateOmemoRatchet(OmemoManager manager,
OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> store); OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> store);
protected OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> protected OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph>
getOmemoRatchet(OmemoManager.KnownBareJidGuard guard) { getOmemoRatchet(OmemoManager manager) {
OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph>
sessionManager = sessionManagers.get(guard.get()); omemoRatchet = omemoRatchets.get(manager);
if (sessionManager == null) { if (omemoRatchet == null) {
sessionManager = instantiateOmemoRatchet(guard, omemoStore); omemoRatchet = instantiateOmemoRatchet(manager, omemoStore);
sessionManagers.put(guard.get(), sessionManager); omemoRatchets.put(manager, omemoRatchet);
} }
return sessionManager; return omemoRatchet;
} }
} }

View File

@ -204,7 +204,7 @@ public abstract class OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_
keyUtil().signedPreKeyPublicForBundle(currentSignedPreKey), keyUtil().signedPreKeyPublicForBundle(currentSignedPreKey),
keyUtil().signedPreKeySignatureFromKey(currentSignedPreKey), keyUtil().signedPreKeySignatureFromKey(currentSignedPreKey),
keyUtil().identityKeyForBundle(keyUtil().identityKeyFromPair(identityKeyPair)), keyUtil().identityKeyForBundle(keyUtil().identityKeyFromPair(identityKeyPair)),
keyUtil().preKeyPublisKeysForBundle(preKeys) keyUtil().preKeyPublicKeysForBundle(preKeys)
); );
} }
@ -526,7 +526,7 @@ public abstract class OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_
* @throws CannotEstablishOmemoSessionException if we cannot establish a session * @throws CannotEstablishOmemoSessionException if we cannot establish a session
* @return fingerprint of the identityKey * @return fingerprint of the identityKey
*/ */
public OmemoFingerprint getFingerprint(OmemoManager.KnownBareJidGuard managerGuard, OmemoDevice contactsDevice) public OmemoFingerprint getFingerprint(OmemoManager.LoggedInOmemoManager managerGuard, OmemoDevice contactsDevice)
throws CannotEstablishOmemoSessionException, CorruptedOmemoKeyException throws CannotEstablishOmemoSessionException, CorruptedOmemoKeyException
{ {
OmemoManager omemoManager = managerGuard.get(); OmemoManager omemoManager = managerGuard.get();

View File

@ -25,7 +25,7 @@ import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smack.util.stringencoder.Base64; import org.jivesoftware.smack.util.stringencoder.Base64;
/** /**
* Class that represents a OmemoElement. * Class that represents an OmemoElement.
* TODO: Move functionality here. * TODO: Move functionality here.
* *
* @author Paul Schaub * @author Paul Schaub

View File

@ -25,5 +25,5 @@ import org.jivesoftware.smackx.omemo.OmemoManager;
*/ */
public interface OmemoCarbonCopyStanzaReceivedListener { public interface OmemoCarbonCopyStanzaReceivedListener {
void onOmemoCarbonCopyReceived(CarbonExtension.Direction direction, Message carbonCopy, Message wrappingMessage, OmemoManager.KnownBareJidGuard omemoManager); void onOmemoCarbonCopyReceived(CarbonExtension.Direction direction, Message carbonCopy, Message wrappingMessage, OmemoManager.LoggedInOmemoManager omemoManager);
} }

View File

@ -21,5 +21,5 @@ import org.jivesoftware.smackx.omemo.OmemoManager;
public interface OmemoMessageStanzaReceivedListener { public interface OmemoMessageStanzaReceivedListener {
void onOmemoMessageStanzaReceived(Stanza stanza, OmemoManager.KnownBareJidGuard omemoManager); void onOmemoMessageStanzaReceived(Stanza stanza, OmemoManager.LoggedInOmemoManager omemoManager);
} }

View File

@ -53,6 +53,20 @@ public class OmemoFingerprint implements CharSequence {
return this.toString().trim().equals(otherFingerprint.toString().trim()); return this.toString().trim().equals(otherFingerprint.toString().trim());
} }
/**
* Split the fingerprint in blocks of 8 characters with spaces between.
*
* @return Block representation of the fingerprint.
*/
public String blocksOf8Chars() {
StringBuilder pretty = new StringBuilder();
for (int i = 0; i < 8; i++) {
if (i != 0) pretty.append(" ");
pretty.append(this.fingerprintString.substring(8 * i, 8 * (i + 1)));
}
return pretty.toString();
}
@Override @Override
public int hashCode() { public int hashCode() {
return toString().hashCode(); return toString().hashCode();

View File

@ -28,8 +28,6 @@ import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice; import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint; import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;
import org.jxmpp.stringprep.XmppStringprepException;
/** /**
* Class that is used to convert bytes to keys and vice versa. * Class that is used to convert bytes to keys and vice versa.
* *
@ -328,7 +326,7 @@ public abstract class OmemoKeyUtil<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @param preKeyHashMap HashMap of preKeys * @param preKeyHashMap HashMap of preKeys
* @return HashMap of byte arrays but with the same keyIds as key * @return HashMap of byte arrays but with the same keyIds as key
*/ */
public HashMap<Integer, byte[]> preKeyPublisKeysForBundle(TreeMap<Integer, T_PreKey> preKeyHashMap) { public HashMap<Integer, byte[]> preKeyPublicKeysForBundle(TreeMap<Integer, T_PreKey> preKeyHashMap) {
HashMap<Integer, byte[]> out = new HashMap<>(); HashMap<Integer, byte[]> out = new HashMap<>();
for (Map.Entry<Integer, T_PreKey> e : preKeyHashMap.entrySet()) { for (Map.Entry<Integer, T_PreKey> e : preKeyHashMap.entrySet()) {
out.put(e.getKey(), preKeyForBundle(e.getValue())); out.put(e.getKey(), preKeyForBundle(e.getValue()));
@ -339,7 +337,7 @@ public abstract class OmemoKeyUtil<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
/** /**
* Prepare a public signedPreKey for transport in a bundle. * Prepare a public signedPreKey for transport in a bundle.
* *
* @param signedPreKey signedPrekey * @param signedPreKey signedPreKey
* @return signedPreKey as byte array * @return signedPreKey as byte array
*/ */
public abstract byte[] signedPreKeyPublicForBundle(T_SigPreKey signedPreKey); public abstract byte[] signedPreKeyPublicForBundle(T_SigPreKey signedPreKey);
@ -376,43 +374,6 @@ public abstract class OmemoKeyUtil<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
*/ */
public abstract byte[] rawSessionToBytes(T_Sess session); public abstract byte[] rawSessionToBytes(T_Sess session);
/**
* Convert an OmemoDevice to a crypto-lib specific contact format.
*
* @param contact omemoContact
* @return crypto-lib specific contact object
*/
public abstract T_Addr omemoDeviceAsAddress(OmemoDevice contact);
/**
* Convert a crypto-lib specific contact object into an OmemoDevice.
*
* @param address contact
* @return as OmemoDevice
* @throws XmppStringprepException if the address is not a valid BareJid
*/
public abstract OmemoDevice addressAsOmemoDevice(T_Addr address) throws XmppStringprepException;
public static String prettyFingerprint(OmemoFingerprint fingerprint) {
return prettyFingerprint(fingerprint.toString());
}
/**
* Split the fingerprint in blocks of 8 characters with spaces between.
*
* @param ugly fingerprint as continuous string
* @return fingerprint with spaces for better readability
*/
public static String prettyFingerprint(String ugly) {
if (ugly == null) return null;
String pretty = "";
for (int i = 0; i < 8; i++) {
if (i != 0) pretty += " ";
pretty += ugly.substring(8 * i, 8 * (i + 1));
}
return pretty;
}
/** /**
* Add integers modulo MAX_VALUE. * Add integers modulo MAX_VALUE.
* *

View File

@ -67,7 +67,7 @@ import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;
*/ */
public class OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> { public class OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> {
private final OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> ratchet; private final OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> ratchet;
private final OmemoManager.KnownBareJidGuard managerGuard; private final OmemoManager.LoggedInOmemoManager managerGuard;
private byte[] messageKey = generateKey(); private byte[] messageKey = generateKey();
private byte[] initializationVector = generateIv(); private byte[] initializationVector = generateIv();
@ -91,7 +91,7 @@ public class OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_
* @throws NoSuchProviderException * @throws NoSuchProviderException
* @throws InvalidAlgorithmParameterException * @throws InvalidAlgorithmParameterException
*/ */
public OmemoMessageBuilder(OmemoManager.KnownBareJidGuard managerGuard, public OmemoMessageBuilder(OmemoManager.LoggedInOmemoManager managerGuard,
OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> ratchet, OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> ratchet,
byte[] aesKey, byte[] iv) byte[] aesKey, byte[] iv)
throws NoSuchPaddingException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, throws NoSuchPaddingException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException,
@ -118,7 +118,7 @@ public class OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_
* @throws NoSuchProviderException * @throws NoSuchProviderException
* @throws InvalidAlgorithmParameterException * @throws InvalidAlgorithmParameterException
*/ */
public OmemoMessageBuilder(OmemoManager.KnownBareJidGuard managerGuard, public OmemoMessageBuilder(OmemoManager.LoggedInOmemoManager managerGuard,
OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> ratchet, OmemoRatchet<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> ratchet,
String message) String message)
throws NoSuchPaddingException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, throws NoSuchPaddingException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException,