1package eu.siacs.conversations.xmpp.jingle;
2
3import com.google.common.base.Function;
4import com.google.common.base.Preconditions;
5import com.google.common.base.Strings;
6import com.google.common.collect.Collections2;
7import com.google.common.collect.ImmutableList;
8import com.google.common.collect.ImmutableMap;
9import com.google.common.collect.ImmutableSet;
10import com.google.common.collect.Iterables;
11import com.google.common.collect.Maps;
12import com.google.common.collect.Sets;
13
14import org.checkerframework.checker.nullness.compatqual.NullableDecl;
15
16import java.util.Collection;
17import java.util.List;
18import java.util.Map;
19import java.util.Objects;
20import java.util.Set;
21
22import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
23import eu.siacs.conversations.xmpp.jingle.stanzas.GenericDescription;
24import eu.siacs.conversations.xmpp.jingle.stanzas.GenericTransportInfo;
25import eu.siacs.conversations.xmpp.jingle.stanzas.Group;
26import eu.siacs.conversations.xmpp.jingle.stanzas.IceUdpTransportInfo;
27import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
28import eu.siacs.conversations.xmpp.jingle.stanzas.OmemoVerifiedIceUdpTransportInfo;
29import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription;
30
31public class RtpContentMap {
32
33 public final Group group;
34 public final Map<String, DescriptionTransport> contents;
35
36 public RtpContentMap(Group group, Map<String, DescriptionTransport> contents) {
37 this.group = group;
38 this.contents = contents;
39 }
40
41 public static RtpContentMap of(final JinglePacket jinglePacket) {
42 final Map<String, DescriptionTransport> contents = DescriptionTransport.of(jinglePacket.getJingleContents());
43 if (isOmemoVerified(contents)) {
44 return new OmemoVerifiedRtpContentMap(jinglePacket.getGroup(), contents);
45 } else {
46 return new RtpContentMap(jinglePacket.getGroup(), contents);
47 }
48 }
49
50 private static boolean isOmemoVerified(Map<String, DescriptionTransport> contents) {
51 final Collection<DescriptionTransport> values = contents.values();
52 if (values.size() == 0) {
53 return false;
54 }
55 for (final DescriptionTransport descriptionTransport : values) {
56 if (descriptionTransport.transport instanceof OmemoVerifiedIceUdpTransportInfo) {
57 continue;
58 }
59 return false;
60 }
61 return true;
62 }
63
64 public static RtpContentMap of(final SessionDescription sessionDescription) {
65 final ImmutableMap.Builder<String, DescriptionTransport> contentMapBuilder = new ImmutableMap.Builder<>();
66 for (SessionDescription.Media media : sessionDescription.media) {
67 final String id = Iterables.getFirst(media.attributes.get("mid"), null);
68 Preconditions.checkNotNull(id, "media has no mid");
69 contentMapBuilder.put(id, DescriptionTransport.of(sessionDescription, media));
70 }
71 final String groupAttribute = Iterables.getFirst(sessionDescription.attributes.get("group"), null);
72 final Group group = groupAttribute == null ? null : Group.ofSdpString(groupAttribute);
73 return new RtpContentMap(group, contentMapBuilder.build());
74 }
75
76 public Set<Media> getMedia() {
77 return Sets.newHashSet(Collections2.transform(contents.values(), input -> {
78 final RtpDescription rtpDescription = input == null ? null : input.description;
79 return rtpDescription == null ? Media.UNKNOWN : input.description.getMedia();
80 }));
81 }
82
83 public List<String> getNames() {
84 return ImmutableList.copyOf(contents.keySet());
85 }
86
87 void requireContentDescriptions() {
88 if (this.contents.size() == 0) {
89 throw new IllegalStateException("No contents available");
90 }
91 for (Map.Entry<String, DescriptionTransport> entry : this.contents.entrySet()) {
92 if (entry.getValue().description == null) {
93 throw new IllegalStateException(String.format("%s is lacking content description", entry.getKey()));
94 }
95 }
96 }
97
98 void requireDTLSFingerprint() {
99 if (this.contents.size() == 0) {
100 throw new IllegalStateException("No contents available");
101 }
102 for (Map.Entry<String, DescriptionTransport> entry : this.contents.entrySet()) {
103 final IceUdpTransportInfo transport = entry.getValue().transport;
104 final IceUdpTransportInfo.Fingerprint fingerprint = transport.getFingerprint();
105 if (fingerprint == null || Strings.isNullOrEmpty(fingerprint.getContent()) || Strings.isNullOrEmpty(fingerprint.getHash())) {
106 throw new SecurityException(String.format("Use of DTLS-SRTP (XEP-0320) is required for content %s", entry.getKey()));
107 }
108 final IceUdpTransportInfo.Setup setup = fingerprint.getSetup();
109 if (setup == null) {
110 throw new SecurityException(String.format("Use of DTLS-SRTP (XEP-0320) is required for content %s but missing setup attribute", entry.getKey()));
111 }
112 }
113 }
114
115 JinglePacket toJinglePacket(final JinglePacket.Action action, final String sessionId) {
116 final JinglePacket jinglePacket = new JinglePacket(action, sessionId);
117 if (this.group != null) {
118 jinglePacket.addGroup(this.group);
119 }
120 for (Map.Entry<String, DescriptionTransport> entry : this.contents.entrySet()) {
121 final Content content = new Content(Content.Creator.INITIATOR, entry.getKey());
122 if (entry.getValue().description != null) {
123 content.addChild(entry.getValue().description);
124 }
125 content.addChild(entry.getValue().transport);
126 jinglePacket.addJingleContent(content);
127 }
128 return jinglePacket;
129 }
130
131 RtpContentMap transportInfo(final String contentName, final IceUdpTransportInfo.Candidate candidate) {
132 final RtpContentMap.DescriptionTransport descriptionTransport = contents.get(contentName);
133 final IceUdpTransportInfo transportInfo = descriptionTransport == null ? null : descriptionTransport.transport;
134 if (transportInfo == null) {
135 throw new IllegalArgumentException("Unable to find transport info for content name " + contentName);
136 }
137 final IceUdpTransportInfo newTransportInfo = transportInfo.cloneWrapper();
138 newTransportInfo.addChild(candidate);
139 return new RtpContentMap(null, ImmutableMap.of(contentName, new DescriptionTransport(null, newTransportInfo)));
140 }
141
142 RtpContentMap transportInfo() {
143 return new RtpContentMap(
144 null,
145 Maps.transformValues(contents, dt -> new DescriptionTransport(null, dt.transport.cloneWrapper()))
146 );
147 }
148
149 public IceUdpTransportInfo.Credentials getCredentials() {
150 final Set<IceUdpTransportInfo.Credentials> allCredentials = ImmutableSet.copyOf(Collections2.transform(
151 contents.values(),
152 dt -> dt.transport.getCredentials()
153 ));
154 final IceUdpTransportInfo.Credentials credentials = Iterables.getFirst(allCredentials, null);
155 if (allCredentials.size() == 1 && credentials != null) {
156 return credentials;
157 }
158 throw new IllegalStateException("Content map does not have distinct credentials");
159 }
160
161 public IceUdpTransportInfo.Setup getDtlsSetup() {
162 final Set<IceUdpTransportInfo.Setup> setups = ImmutableSet.copyOf(Collections2.transform(
163 contents.values(),
164 dt -> dt.transport.getFingerprint().getSetup()
165 ));
166 final IceUdpTransportInfo.Setup setup = Iterables.getFirst(setups, null);
167 if (setups.size() == 1 && setup != null) {
168 return setup;
169 }
170 throw new IllegalStateException("Content map doesn't have distinct DTLS setup");
171 }
172
173 public boolean emptyCandidates() {
174 int count = 0;
175 for (DescriptionTransport descriptionTransport : contents.values()) {
176 count += descriptionTransport.transport.getCandidates().size();
177 }
178 return count == 0;
179 }
180
181 public RtpContentMap modifiedCredentials(IceUdpTransportInfo.Credentials credentials, final IceUdpTransportInfo.Setup setup) {
182 final ImmutableMap.Builder<String, DescriptionTransport> contentMapBuilder = new ImmutableMap.Builder<>();
183 for (final Map.Entry<String, DescriptionTransport> content : contents.entrySet()) {
184 final RtpDescription rtpDescription = content.getValue().description;
185 IceUdpTransportInfo transportInfo = content.getValue().transport;
186 final IceUdpTransportInfo modifiedTransportInfo = transportInfo.modifyCredentials(credentials, setup);
187 contentMapBuilder.put(content.getKey(), new DescriptionTransport(rtpDescription, modifiedTransportInfo));
188 }
189 return new RtpContentMap(this.group, contentMapBuilder.build());
190 }
191
192 public static class DescriptionTransport {
193 public final RtpDescription description;
194 public final IceUdpTransportInfo transport;
195
196 public DescriptionTransport(final RtpDescription description, final IceUdpTransportInfo transport) {
197 this.description = description;
198 this.transport = transport;
199 }
200
201 public static DescriptionTransport of(final Content content) {
202 final GenericDescription description = content.getDescription();
203 final GenericTransportInfo transportInfo = content.getTransport();
204 final RtpDescription rtpDescription;
205 final IceUdpTransportInfo iceUdpTransportInfo;
206 if (description == null) {
207 rtpDescription = null;
208 } else if (description instanceof RtpDescription) {
209 rtpDescription = (RtpDescription) description;
210 } else {
211 throw new UnsupportedApplicationException("Content does not contain rtp description");
212 }
213 if (transportInfo instanceof IceUdpTransportInfo) {
214 iceUdpTransportInfo = (IceUdpTransportInfo) transportInfo;
215 } else {
216 throw new UnsupportedTransportException("Content does not contain ICE-UDP transport");
217 }
218 return new DescriptionTransport(
219 rtpDescription,
220 OmemoVerifiedIceUdpTransportInfo.upgrade(iceUdpTransportInfo)
221 );
222 }
223
224 public static DescriptionTransport of(final SessionDescription sessionDescription, final SessionDescription.Media media) {
225 final RtpDescription rtpDescription = RtpDescription.of(sessionDescription, media);
226 final IceUdpTransportInfo transportInfo = IceUdpTransportInfo.of(sessionDescription, media);
227 return new DescriptionTransport(rtpDescription, transportInfo);
228 }
229
230 public static Map<String, DescriptionTransport> of(final Map<String, Content> contents) {
231 return ImmutableMap.copyOf(Maps.transformValues(contents, new Function<Content, DescriptionTransport>() {
232 @NullableDecl
233 @Override
234 public DescriptionTransport apply(@NullableDecl Content content) {
235 return content == null ? null : of(content);
236 }
237 }));
238 }
239 }
240
241 public static class UnsupportedApplicationException extends IllegalArgumentException {
242 UnsupportedApplicationException(String message) {
243 super(message);
244 }
245 }
246
247 public static class UnsupportedTransportException extends IllegalArgumentException {
248 UnsupportedTransportException(String message) {
249 super(message);
250 }
251 }
252}