Search in sources :

Example 1 with MessageOrOmemoMessage

use of org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage in project Smack by igniterealtime.

the class OmemoMamDecryptionTest method mamDecryptionTest.

@SmackIntegrationTest
public void mamDecryptionTest() throws XMPPException.XMPPErrorException, SmackException.NotLoggedInException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, CryptoFailedException, UndecidedOmemoIdentityException, IOException {
    // Make sure, Bobs server stores messages in the archive
    MamManager bobsMamManager = MamManager.getInstanceFor(bob.getConnection());
    bobsMamManager.enableMamForAllMessages();
    bobsMamManager.setDefaultBehavior(MamPrefsIQ.DefaultBehavior.always);
    // Prevent bob from automatically decrypting MAM messages.
    bob.stopStanzaAndPEPListeners();
    String body = "This message will be stored in MAM!";
    OmemoMessage.Sent encrypted = alice.encrypt(bob.getOwnJid(), body);
    XMPPConnection alicesConnection = alice.getConnection();
    MessageBuilder messageBuilder = alicesConnection.getStanzaFactory().buildMessageStanza();
    alicesConnection.sendStanza(encrypted.buildMessage(messageBuilder, bob.getOwnJid()));
    MamManager.MamQuery query = bobsMamManager.queryArchive(MamManager.MamQueryArgs.builder().limitResultsToJid(alice.getOwnJid()).build());
    assertEquals(1, query.getMessageCount());
    List<MessageOrOmemoMessage> decryptedMamQuery = bob.decryptMamQueryResult(query);
    assertEquals(1, decryptedMamQuery.size());
    assertEquals(body, decryptedMamQuery.get(decryptedMamQuery.size() - 1).getOmemoMessage().getBody());
}
Also used : MamManager(org.jivesoftware.smackx.mam.MamManager) MessageOrOmemoMessage(org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage) MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) MessageOrOmemoMessage(org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage) XMPPConnection(org.jivesoftware.smack.XMPPConnection) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 2 with MessageOrOmemoMessage

use of org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage in project Smack by igniterealtime.

the class OmemoService method decryptMamQueryResult.

/**
 * Decrypt a possible OMEMO encrypted messages in a {@link MamManager.MamQuery}.
 * The returned list contains wrappers that either hold an {@link OmemoMessage} in case the message was decrypted
 * properly, otherwise it contains the message itself.
 *
 * @param managerGuard authenticated OmemoManager.
 * @param mamQuery Mam archive query
 * @return list of {@link MessageOrOmemoMessage MessageOrOmemoMessages}.
 *
 * @throws IOException if an I/O error occurred.
 */
List<MessageOrOmemoMessage> decryptMamQueryResult(OmemoManager.LoggedInOmemoManager managerGuard, MamManager.MamQuery mamQuery) throws IOException {
    List<MessageOrOmemoMessage> result = new ArrayList<>();
    for (Message message : mamQuery.getMessages()) {
        if (OmemoManager.stanzaContainsOmemoElement(message)) {
            OmemoElement element = (OmemoElement) message.getExtensionElement(OmemoElement.NAME_ENCRYPTED, OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL);
            // Decrypt OMEMO messages
            try {
                OmemoMessage.Received omemoMessage = decryptMessage(managerGuard, message.getFrom().asBareJid(), element);
                result.add(new MessageOrOmemoMessage(omemoMessage));
            } catch (NoRawSessionException | CorruptedOmemoKeyException | CryptoFailedException e) {
                LOGGER.log(Level.WARNING, "decryptMamQueryResult failed to decrypt message from " + message.getFrom() + " due to corrupted session/key: " + e.getMessage());
                result.add(new MessageOrOmemoMessage(message));
            }
        } else {
            // Wrap cleartext messages
            result.add(new MessageOrOmemoMessage(message));
        }
    }
    return result;
}
Also used : CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) MessageOrOmemoMessage(org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage) MessageOrOmemoMessage(org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage) Message(org.jivesoftware.smack.packet.Message) NoRawSessionException(org.jivesoftware.smackx.omemo.exceptions.NoRawSessionException) ArrayList(java.util.ArrayList) MessageOrOmemoMessage(org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage) OmemoElement(org.jivesoftware.smackx.omemo.element.OmemoElement) CorruptedOmemoKeyException(org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException)

Aggregations

MessageOrOmemoMessage (org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage)2 ArrayList (java.util.ArrayList)1 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 Message (org.jivesoftware.smack.packet.Message)1 MessageBuilder (org.jivesoftware.smack.packet.MessageBuilder)1 MamManager (org.jivesoftware.smackx.mam.MamManager)1 OmemoElement (org.jivesoftware.smackx.omemo.element.OmemoElement)1 CorruptedOmemoKeyException (org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException)1 CryptoFailedException (org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException)1 NoRawSessionException (org.jivesoftware.smackx.omemo.exceptions.NoRawSessionException)1