fix JET spec compliance

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java | 13 
src/main/java/eu/siacs/conversations/xmpp/jingle/stanzas/Content.java              | 34 
src/main/java/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java         | 38 
3 files changed, 43 insertions(+), 42 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java 🔗

@@ -209,8 +209,12 @@ public class JingleFileTransferConnection extends AbstractJingleConnection
                 this.transportSecurity =
                         new TransportSecurity(
                                 xmppAxolotlMessage.getInnerKey(), xmppAxolotlMessage.getIV());
-                jinglePacket.setSecurity(
-                        Iterables.getOnlyElement(contentMap.contents.keySet()), xmppAxolotlMessage);
+                final var contents = jinglePacket.getJingleContents();
+                final var rawContent =
+                        contents.get(Iterables.getOnlyElement(contentMap.contents.keySet()));
+                if (rawContent != null) {
+                    rawContent.setSecurity(xmppAxolotlMessage);
+                }
             }
             jinglePacket.setTo(id.with);
             xmppConnectionService.sendIqPacket(
@@ -327,8 +331,10 @@ public class JingleFileTransferConnection extends AbstractJingleConnection
             return;
         }
         final XmppAxolotlMessage.XmppAxolotlKeyTransportMessage keyTransportMessage;
+        final var contents = jinglePacket.getJingleContents();
+        final var rawContent = contents.get(Iterables.getOnlyElement(contentMap.contents.keySet()));
         final var security =
-                jinglePacket.getSecurity(Iterables.getOnlyElement(contentMap.contents.keySet()));
+                rawContent == null ? null : rawContent.getSecurity(jinglePacket.getFrom());
         if (security != null) {
             Log.d(Config.LOGTAG, "found security element!");
             keyTransportMessage =
@@ -349,7 +355,6 @@ public class JingleFileTransferConnection extends AbstractJingleConnection
 
         if (transition(State.SESSION_INITIALIZED, () -> setRemoteContentMap(contentMap))) {
             respondOk(jinglePacket);
-            Log.d(Config.LOGTAG, jinglePacket.toString());
             Log.d(
                     Config.LOGTAG,
                     "got file offer " + file + " jet=" + Objects.nonNull(keyTransportMessage));

src/main/java/eu/siacs/conversations/xmpp/jingle/stanzas/Content.java 🔗

@@ -9,8 +9,11 @@ import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableSet;
 
 import eu.siacs.conversations.Config;
+import eu.siacs.conversations.crypto.axolotl.AxolotlService;
+import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
 import eu.siacs.conversations.xml.Element;
 import eu.siacs.conversations.xml.Namespace;
+import eu.siacs.conversations.xmpp.Jid;
 import eu.siacs.conversations.xmpp.jingle.SessionDescription;
 
 import java.util.Locale;
@@ -102,6 +105,37 @@ public class Content extends Element {
         }
     }
 
+    public void setSecurity(final XmppAxolotlMessage xmppAxolotlMessage) {
+        final String contentName = this.getContentName();
+        final Element security = new Element("security", Namespace.JINGLE_ENCRYPTED_TRANSPORT);
+        security.setAttribute("name", contentName);
+        security.setAttribute("cipher", "urn:xmpp:ciphers:aes-128-gcm-nopadding");
+        security.setAttribute("type", AxolotlService.PEP_PREFIX);
+        security.addChild(xmppAxolotlMessage.toElement());
+        this.addChild(security);
+    }
+
+    public XmppAxolotlMessage getSecurity(final Jid from) {
+        final String contentName = this.getContentName();
+        for (final Element child : this.children) {
+            if ("security".equals(child.getName())
+                    && Namespace.JINGLE_ENCRYPTED_TRANSPORT.equals(child.getNamespace())) {
+                final String name = child.getAttribute("name");
+                final String type = child.getAttribute("type");
+                final String cipher = child.getAttribute("cipher");
+                if (contentName.equals(name)
+                        && AxolotlService.PEP_PREFIX.equals(type)
+                        && "urn:xmpp:ciphers:aes-128-gcm-nopadding".equals(cipher)) {
+                    final var encrypted = child.findChild("encrypted", AxolotlService.PEP_PREFIX);
+                    if (encrypted != null) {
+                        return XmppAxolotlMessage.fromElement(encrypted, from.asBareJid());
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
     public void setTransport(GenericTransportInfo transportInfo) {
         this.addChild(transportInfo);
     }

src/main/java/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java 🔗

@@ -1,7 +1,5 @@
 package eu.siacs.conversations.xmpp.jingle.stanzas;
 
-import android.util.Log;
-
 import androidx.annotation.NonNull;
 
 import com.google.common.base.CaseFormat;
@@ -9,9 +7,6 @@ import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableMap;
 
-import eu.siacs.conversations.Config;
-import eu.siacs.conversations.crypto.axolotl.AxolotlService;
-import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
 import eu.siacs.conversations.xml.Element;
 import eu.siacs.conversations.xml.Namespace;
 import eu.siacs.conversations.xmpp.Jid;
@@ -121,39 +116,6 @@ public class JinglePacket extends IqPacket {
         jingle.addChild(child);
     }
 
-    public void setSecurity(final String name, final XmppAxolotlMessage xmppAxolotlMessage) {
-        final Element security = new Element("security", Namespace.JINGLE_ENCRYPTED_TRANSPORT);
-        security.setAttribute("name", name);
-        security.setAttribute("cipher", "urn:xmpp:ciphers:aes-128-gcm-nopadding");
-        security.setAttribute("type", AxolotlService.PEP_PREFIX);
-        security.addChild(xmppAxolotlMessage.toElement());
-        addJingleChild(security);
-    }
-
-    public XmppAxolotlMessage getSecurity(final String nameNeedle) {
-        final Element jingle = findChild("jingle", Namespace.JINGLE);
-        if (jingle == null) {
-            return null;
-        }
-        for (final Element child : jingle.getChildren()) {
-            if ("security".equals(child.getName())
-                    && Namespace.JINGLE_ENCRYPTED_TRANSPORT.equals(child.getNamespace())) {
-                final String name = child.getAttribute("name");
-                final String type = child.getAttribute("type");
-                final String cipher = child.getAttribute("cipher");
-                if (nameNeedle.equals(name)
-                        && AxolotlService.PEP_PREFIX.equals(type)
-                        && "urn:xmpp:ciphers:aes-128-gcm-nopadding".equals(cipher)) {
-                    final var encrypted = child.findChild("encrypted", AxolotlService.PEP_PREFIX);
-                    if (encrypted != null) {
-                        return XmppAxolotlMessage.fromElement(encrypted, getFrom().asBareJid());
-                    }
-                }
-            }
-        }
-        return null;
-    }
-
     public String getSessionId() {
         return findChild("jingle", Namespace.JINGLE).getAttribute("sid");
     }