Search in sources :

Example 16 with MessageBuilder

use of org.jivesoftware.smack.packet.MessageBuilder 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 17 with MessageBuilder

use of org.jivesoftware.smack.packet.MessageBuilder in project Smack by igniterealtime.

the class OmemoManager method sendRatchetUpdateMessage.

/**
 * Send a ratchet update message. This can be used to advance the ratchet of a session in order to maintain forward
 * secrecy.
 *
 * @param recipient recipient
 *
 * @throws CorruptedOmemoKeyException           When the used identityKeys are corrupted
 * @throws CryptoFailedException                When something fails with the crypto
 * @throws CannotEstablishOmemoSessionException When we can't establish a session with the recipient
 * @throws SmackException.NotLoggedInException if the XMPP connection is not authenticated.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws SmackException.NoResponseException if there was no response from the remote entity.
 * @throws NoSuchAlgorithmException if no such algorithm is available.
 * @throws SmackException.NotConnectedException if the XMPP connection is not connected.
 * @throws IOException if an I/O error occurred.
 */
public synchronized void sendRatchetUpdateMessage(OmemoDevice recipient) throws SmackException.NotLoggedInException, CorruptedOmemoKeyException, InterruptedException, SmackException.NoResponseException, NoSuchAlgorithmException, SmackException.NotConnectedException, CryptoFailedException, CannotEstablishOmemoSessionException, IOException {
    XMPPConnection connection = connection();
    MessageBuilder message = connection.getStanzaFactory().buildMessageStanza().to(recipient.getJid());
    OmemoElement element = getOmemoService().createRatchetUpdateElement(new LoggedInOmemoManager(this), recipient);
    message.addExtension(element);
    // Set MAM Storage hint
    StoreHint.set(message);
    connection.sendStanza(message.build());
}
Also used : MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) XMPPConnection(org.jivesoftware.smack.XMPPConnection) OmemoElement(org.jivesoftware.smackx.omemo.element.OmemoElement)

Example 18 with MessageBuilder

use of org.jivesoftware.smack.packet.MessageBuilder in project Smack by igniterealtime.

the class RosterExchangeManager method send.

/**
 * Sends a roster entry to userID.
 *
 * @param rosterEntry the roster entry to send
 * @param targetUserID the user that will receive the roster entries
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void send(RosterEntry rosterEntry, Jid targetUserID) throws NotConnectedException, InterruptedException {
    XMPPConnection connection = weakRefConnection.get();
    // Create a new message to send the roster
    MessageBuilder messageBuilder = connection.getStanzaFactory().buildMessageStanza().to(targetUserID);
    // Create a RosterExchange Package and add it to the message
    RosterExchange rosterExchange = new RosterExchange();
    rosterExchange.addRosterEntry(rosterEntry);
    messageBuilder.addExtension(rosterExchange);
    // Send the message that contains the roster
    connection.sendStanza(messageBuilder.build());
}
Also used : RosterExchange(org.jivesoftware.smackx.xroster.packet.RosterExchange) MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Example 19 with MessageBuilder

use of org.jivesoftware.smack.packet.MessageBuilder in project Smack by igniterealtime.

the class OXInstantMessagingManagerTest method test.

@Test
public void test() throws IOException, PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, SmackException, MissingUserIdOnKeyException, InterruptedException, XMPPException, XmlPullParserException {
    DummyConnection aliceCon = new DummyConnection();
    aliceCon.connect().login();
    DummyConnection bobCon = new DummyConnection();
    bobCon.connect().login();
    FileBasedOpenPgpStore aliceStore = new FileBasedOpenPgpStore(new File(basePath, "alice"));
    FileBasedOpenPgpStore bobStore = new FileBasedOpenPgpStore(new File(basePath, "bob"));
    PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(aliceStore);
    PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(bobStore);
    OpenPgpManager aliceOpenPgp = OpenPgpManager.getInstanceFor(aliceCon);
    OpenPgpManager bobOpenPgp = OpenPgpManager.getInstanceFor(bobCon);
    aliceOpenPgp.setOpenPgpProvider(aliceProvider);
    bobOpenPgp.setOpenPgpProvider(bobProvider);
    OXInstantMessagingManager aliceOxim = OXInstantMessagingManager.getInstanceFor(aliceCon);
    OpenPgpSelf aliceSelf = aliceOpenPgp.getOpenPgpSelf();
    OpenPgpSelf bobSelf = bobOpenPgp.getOpenPgpSelf();
    assertFalse(aliceSelf.hasSecretKeyAvailable());
    assertFalse(bobSelf.hasSecretKeyAvailable());
    // Generate keys
    aliceOpenPgp.generateAndImportKeyPair(aliceSelf.getJid());
    bobOpenPgp.generateAndImportKeyPair(bobSelf.getJid());
    assertTrue(aliceSelf.hasSecretKeyAvailable());
    assertTrue(bobSelf.hasSecretKeyAvailable());
    assertTrue(aliceSelf.isTrusted(aliceSelf.getSigningKeyFingerprint()));
    assertTrue(bobSelf.isTrusted(bobSelf.getSigningKeyFingerprint()));
    assertTrue(aliceSelf.getTrustedFingerprints().contains(aliceSelf.getSigningKeyFingerprint()));
    // Exchange keys
    aliceStore.importPublicKey(bobSelf.getJid(), bobSelf.getAnnouncedPublicKeys().iterator().next());
    bobStore.importPublicKey(aliceSelf.getJid(), aliceSelf.getAnnouncedPublicKeys().iterator().next());
    // Simulate key announcement
    bobStore.setAnnouncedFingerprintsOf(bobSelf.getJid(), Collections.singletonMap(bobSelf.getSigningKeyFingerprint(), new Date()));
    bobStore.setAnnouncedFingerprintsOf(aliceSelf.getJid(), Collections.singletonMap(aliceSelf.getSigningKeyFingerprint(), new Date()));
    aliceStore.setAnnouncedFingerprintsOf(aliceSelf.getJid(), Collections.singletonMap(aliceSelf.getSigningKeyFingerprint(), new Date()));
    aliceStore.setAnnouncedFingerprintsOf(bobSelf.getJid(), Collections.singletonMap(bobSelf.getSigningKeyFingerprint(), new Date()));
    OpenPgpContact aliceForBob = bobOpenPgp.getOpenPgpContact((EntityBareJid) aliceSelf.getJid());
    OpenPgpContact bobForAlice = aliceOpenPgp.getOpenPgpContact((EntityBareJid) bobSelf.getJid());
    assertTrue(aliceForBob.hasUndecidedKeys());
    assertTrue(bobForAlice.hasUndecidedKeys());
    assertTrue(aliceForBob.getUndecidedFingerprints().contains(aliceSelf.getSigningKeyFingerprint()));
    assertTrue(bobForAlice.getUndecidedFingerprints().contains(bobSelf.getSigningKeyFingerprint()));
    bobForAlice.trust(bobSelf.getSigningKeyFingerprint());
    aliceForBob.trust(aliceSelf.getSigningKeyFingerprint());
    assertFalse(aliceForBob.hasUndecidedKeys());
    assertFalse(bobForAlice.hasUndecidedKeys());
    MessageBuilder messageBuilder = StanzaBuilder.buildMessage();
    assertFalse(ExplicitMessageEncryptionElement.hasProtocol(messageBuilder.build(), ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0));
    aliceOxim.addOxMessage(messageBuilder, bobForAlice, Collections.singletonList(new Message.Body(null, "Hello World!")));
    Message message = messageBuilder.build();
    assertTrue(ExplicitMessageEncryptionElement.hasProtocol(message, ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0));
    assertNotNull(OpenPgpElement.fromStanza(message));
    OpenPgpMessage decrypted = bobOpenPgp.decryptOpenPgpElement(OpenPgpElement.fromStanza(message), aliceForBob);
    assertEquals(OpenPgpMessage.State.signcrypt, decrypted.getState());
    SigncryptElement signcryptElement = (SigncryptElement) decrypted.getOpenPgpContentElement();
    Message.Body body = signcryptElement.getExtension(Message.Body.ELEMENT, Message.Body.NAMESPACE);
    assertNotNull(body);
    assertEquals("Hello World!", body.getMessage());
    OpenPgpMetadata metadata = decrypted.getMetadata();
    assertTrue(metadata.isSigned() && metadata.isEncrypted());
    // Check, if one of Bobs keys was used for decryption
    assertNotNull(bobSelf.getSigningKeyRing().getPublicKey(metadata.getDecryptionKey().getKeyId()));
    // TODO: I observed this assertTrue() to fail sporadically. As a first attempt to diagnose this, a message was
    // added to the assertion. However since most (all?) objects used in the message do not implement a proper
    // toString() this is probably not really helpful as it is.
    PGPPublicKeyRingCollection pubKeys = aliceForBob.getTrustedAnnouncedKeys();
    // Check if one of Alice' keys was used for signing
    assertTrue(metadata.containsVerifiedSignatureFrom(pubKeys.iterator().next()), metadata + " did not contain one of alice' keys " + pubKeys);
}
Also used : SigncryptElement(org.jivesoftware.smackx.ox.element.SigncryptElement) OpenPgpMessage(org.jivesoftware.smackx.ox.OpenPgpMessage) Message(org.jivesoftware.smack.packet.Message) DummyConnection(org.jivesoftware.smack.DummyConnection) OpenPgpSelf(org.jivesoftware.smackx.ox.OpenPgpSelf) Date(java.util.Date) FileBasedOpenPgpStore(org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore) PGPPublicKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRingCollection) MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) OpenPgpMessage(org.jivesoftware.smackx.ox.OpenPgpMessage) File(java.io.File) PainlessOpenPgpProvider(org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider) OpenPgpManager(org.jivesoftware.smackx.ox.OpenPgpManager) OpenPgpContact(org.jivesoftware.smackx.ox.OpenPgpContact) OpenPgpMetadata(org.pgpainless.decryption_verification.OpenPgpMetadata) Test(org.junit.jupiter.api.Test)

Example 20 with MessageBuilder

use of org.jivesoftware.smack.packet.MessageBuilder in project Smack by igniterealtime.

the class RosterExchangeManager method send.

/**
 * Sends a roster group to userID. All the entries of the group will be sent to the
 * target user.
 *
 * @param rosterGroup the roster group to send
 * @param targetUserID the user that will receive the roster entries
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void send(RosterGroup rosterGroup, Jid targetUserID) throws NotConnectedException, InterruptedException {
    XMPPConnection connection = weakRefConnection.get();
    // Create a new message to send the roster
    MessageBuilder msg = connection.getStanzaFactory().buildMessageStanza().to(targetUserID);
    // Create a RosterExchange Package and add it to the message
    RosterExchange rosterExchange = new RosterExchange();
    for (RosterEntry entry : rosterGroup.getEntries()) {
        rosterExchange.addRosterEntry(entry);
    }
    msg.addExtension(rosterExchange);
    // Send the message that contains the roster
    connection.sendStanza(msg.build());
}
Also used : RosterExchange(org.jivesoftware.smackx.xroster.packet.RosterExchange) MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) RosterEntry(org.jivesoftware.smack.roster.RosterEntry) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Aggregations

MessageBuilder (org.jivesoftware.smack.packet.MessageBuilder)34 Message (org.jivesoftware.smack.packet.Message)10 XMPPConnection (org.jivesoftware.smack.XMPPConnection)9 Test (org.junit.jupiter.api.Test)8 Test (org.junit.Test)7 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)5 Stanza (org.jivesoftware.smack.packet.Stanza)3 RosterExchange (org.jivesoftware.smackx.xroster.packet.RosterExchange)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 Date (java.util.Date)2 DummyConnection (org.jivesoftware.smack.DummyConnection)2 AndFilter (org.jivesoftware.smack.filter.AndFilter)2 XmlElement (org.jivesoftware.smack.packet.XmlElement)2 OpenPgpMessage (org.jivesoftware.smackx.ox.OpenPgpMessage)2 EntityBareJid (org.jxmpp.jid.EntityBareJid)2 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1