use of org.jivesoftware.smackx.omemo.internal.OmemoDevice in project Smack by igniterealtime.
the class OmemoService method decryptMessage.
/**
* Decrypt an OMEMO message.
*
* @param managerGuard authenticated OmemoManager.
* @param senderJid BareJid of the sender.
* @param omemoElement omemoElement.
* @return decrypted OmemoMessage object.
*
* @throws CorruptedOmemoKeyException if the identityKey of the sender is damaged.
* @throws CryptoFailedException if decryption fails.
* @throws NoRawSessionException if we have no session with the device and it sent a normal (non-preKey) message.
* @throws IOException if an I/O error occurred.
*/
OmemoMessage.Received decryptMessage(OmemoManager.LoggedInOmemoManager managerGuard, BareJid senderJid, OmemoElement omemoElement) throws CorruptedOmemoKeyException, CryptoFailedException, NoRawSessionException, IOException {
OmemoManager manager = managerGuard.get();
int senderId = omemoElement.getHeader().getSid();
OmemoDevice senderDevice = new OmemoDevice(senderJid, senderId);
CipherAndAuthTag cipherAndAuthTag = getOmemoRatchet(manager).retrieveMessageKeyAndAuthTag(senderDevice, omemoElement);
// Retrieve senders fingerprint.
OmemoFingerprint senderFingerprint;
try {
senderFingerprint = getOmemoStoreBackend().getFingerprint(manager.getOwnDevice(), senderDevice);
} catch (NoIdentityKeyException e) {
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);
return new OmemoMessage.Received(omemoElement, cipherAndAuthTag.getKey(), cipherAndAuthTag.getIv(), plaintext, senderFingerprint, senderDevice, cipherAndAuthTag.wasPreKeyEncrypted());
} else {
// KeyTransportMessages don't require decryption of the payload.
return new OmemoMessage.Received(omemoElement, cipherAndAuthTag.getKey(), cipherAndAuthTag.getIv(), null, senderFingerprint, senderDevice, cipherAndAuthTag.wasPreKeyEncrypted());
}
}
use of org.jivesoftware.smackx.omemo.internal.OmemoDevice in project Smack by igniterealtime.
the class OmemoManager method getDevicesOf.
/**
* Return a set of all OMEMO capable devices of a contact.
* Note, that this method does not explicitly refresh the device list of the contact, so it might be outdated.
*
* @see #requestDeviceListUpdateFor(BareJid)
*
* @param contact contact we want to get a set of device of.
* @return set of known devices of that contact.
*
* @throws IOException if an I/O error occurred.
*/
public Set<OmemoDevice> getDevicesOf(BareJid contact) throws IOException {
OmemoCachedDeviceList list = getOmemoService().getOmemoStoreBackend().loadCachedDeviceList(getOwnDevice(), contact);
HashSet<OmemoDevice> devices = new HashSet<>();
for (int deviceId : list.getActiveDevices()) {
devices.add(new OmemoDevice(contact, deviceId));
}
return devices;
}
use of org.jivesoftware.smackx.omemo.internal.OmemoDevice in project Smack by igniterealtime.
the class FileBasedOmemoStore method loadAllRawSessionsOf.
@Override
public HashMap<Integer, T_Sess> loadAllRawSessionsOf(OmemoDevice userDevice, BareJid contact) throws IOException {
File contactsDirectory = hierarchy.getContactsDir(userDevice, contact);
HashMap<Integer, T_Sess> sessions = new HashMap<>();
String[] devices = contactsDirectory.list();
for (String deviceId : devices != null ? devices : new String[0]) {
int id;
try {
id = Integer.parseInt(deviceId);
} catch (NumberFormatException e) {
continue;
}
OmemoDevice device = new OmemoDevice(contact, id);
File session = hierarchy.getContactsSessionPath(userDevice, device);
byte[] bytes = readBytes(session);
if (bytes != null) {
try {
T_Sess s = keyUtil().rawSessionFromBytes(bytes);
sessions.put(id, s);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Could not deserialize raw session.", e);
}
}
}
return sessions;
}
use of org.jivesoftware.smackx.omemo.internal.OmemoDevice in project Smack by igniterealtime.
the class FileBasedOmemoStore method removeAllRawSessionsOf.
@Override
public void removeAllRawSessionsOf(OmemoDevice userDevice, BareJid contact) {
File contactsDirectory = hierarchy.getContactsDir(userDevice, contact);
String[] devices = contactsDirectory.list();
for (String deviceId : devices != null ? devices : new String[0]) {
int id = Integer.parseInt(deviceId);
OmemoDevice device = new OmemoDevice(contact, id);
File session = hierarchy.getContactsSessionPath(userDevice, device);
if (!session.delete()) {
LOGGER.log(Level.WARNING, "Deleting raw OMEMO session " + session.getAbsolutePath() + "failed.");
}
}
}
use of org.jivesoftware.smackx.omemo.internal.OmemoDevice in project Smack by igniterealtime.
the class OmemoStore method mergeCachedDeviceList.
/**
* Merge the received OmemoDeviceListElement with the one we already have. If we had none, the received one is saved.
*
* @param userDevice our OmemoDevice.
* @param contact Contact we received the list from.
* @param list List we received.
*
* @throws IOException if an I/O error occurred.
*/
OmemoCachedDeviceList mergeCachedDeviceList(OmemoDevice userDevice, BareJid contact, OmemoDeviceListElement list) throws IOException {
OmemoCachedDeviceList cached = loadCachedDeviceList(userDevice, contact);
if (cached == null) {
cached = new OmemoCachedDeviceList();
}
if (list == null) {
return cached;
}
for (int devId : list.getDeviceIds()) {
if (!cached.contains(devId)) {
setDateOfLastDeviceIdPublication(userDevice, new OmemoDevice(contact, devId), new Date());
}
}
cached.merge(list.getDeviceIds());
storeCachedDeviceList(userDevice, contact, cached);
return cached;
}
Aggregations