| 1 |
using Gee; |
| 2 |
using Xmpp; |
| 3 |
using Xmpp.Xep; |
| 4 |
|
| 5 |
namespace Xmpp.Xep.JingleRtp { |
| 6 |
|
| 7 |
public const string NS_URI = "urn:xmpp:jingle:apps:rtp:1"; |
| 8 |
public const string NS_URI_AUDIO = "urn:xmpp:jingle:apps:rtp:audio"; |
| 9 |
public const string NS_URI_VIDEO = "urn:xmpp:jingle:apps:rtp:video"; |
| 10 |
|
| 11 |
public abstract class Module : XmppStreamModule { |
| 12 |
public static Xmpp.ModuleIdentity<Module> IDENTITY = new Xmpp.ModuleIdentity<Module>(NS_URI, "0167_jingle_rtp"); |
| 13 |
|
| 14 |
private ContentType content_type; |
| 15 |
public SessionInfoType session_info_type = new SessionInfoType(); |
| 16 |
|
| 17 |
protected Module() { |
| 18 |
content_type = new ContentType(this); |
| 19 |
} |
| 20 |
|
| 21 |
public abstract async Gee.List<PayloadType> get_supported_payloads(string media); |
| 22 |
public abstract async bool is_payload_supported(string media, JingleRtp.PayloadType payload_type); |
| 23 |
public abstract async PayloadType? pick_payload_type(string media, Gee.List<PayloadType> payloads); |
| 24 |
public abstract Crypto? generate_local_crypto(); |
| 25 |
public abstract Crypto? pick_remote_crypto(Gee.List<Crypto> cryptos); |
| 26 |
public abstract Crypto? pick_local_crypto(Crypto? remote); |
| 27 |
public abstract Stream create_stream(Jingle.Content content); |
| 28 |
public abstract bool is_header_extension_supported(string media, HeaderExtension ext); |
| 29 |
public abstract Gee.List<HeaderExtension> get_suggested_header_extensions(string media); |
| 30 |
public abstract void close_stream(Stream stream); |
| 31 |
|
| 32 |
public async Jingle.Session start_call(XmppStream stream, Jid receiver_full_jid, bool video, string sid, Jid? muji_room) throws Jingle.Error { |
| 33 |
|
| 34 |
Jingle.Module jingle_module = stream.get_module(Jingle.Module.IDENTITY); |
| 35 |
|
| 36 |
Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid; |
| 37 |
if (my_jid == null) { |
| 38 |
throw new Jingle.Error.GENERAL("Couldn't determine own JID"); |
| 39 |
} |
| 40 |
|
| 41 |
ArrayList<Jingle.Content> contents = new ArrayList<Jingle.Content>(); |
| 42 |
|
| 43 |
// Create audio content |
| 44 |
Parameters audio_content_parameters = new Parameters(this, "audio", yield get_supported_payloads("audio")); |
| 45 |
audio_content_parameters.local_crypto = generate_local_crypto(); |
| 46 |
audio_content_parameters.header_extensions.add_all(get_suggested_header_extensions("audio")); |
| 47 |
Jingle.Transport? audio_transport = yield jingle_module.select_transport(stream, content_type.required_transport_type, content_type.required_components, receiver_full_jid, Set.empty()); |
| 48 |
if (audio_transport == null) { |
| 49 |
throw new Jingle.Error.NO_SHARED_PROTOCOLS("No suitable audio transports"); |
| 50 |
} |
| 51 |
Jingle.TransportParameters audio_transport_params = audio_transport.create_transport_parameters(stream, content_type.required_components, my_jid, receiver_full_jid); |
| 52 |
Jingle.Content audio_content = new Jingle.Content.initiate_sent("audio", Jingle.Senders.BOTH, |
| 53 |
content_type, audio_content_parameters, |
| 54 |
audio_transport, audio_transport_params, |
| 55 |
null, null, |
| 56 |
my_jid, receiver_full_jid); |
| 57 |
contents.add(audio_content); |
| 58 |
|
| 59 |
Jingle.Content? video_content = null; |
| 60 |
if (video) { |
| 61 |
// Create video content |
| 62 |
Parameters video_content_parameters = new Parameters(this, "video", yield get_supported_payloads("video")); |
| 63 |
video_content_parameters.local_crypto = generate_local_crypto(); |
| 64 |
video_content_parameters.header_extensions.add_all(get_suggested_header_extensions("video")); |
| 65 |
Jingle.Transport? video_transport = yield stream.get_module(Jingle.Module.IDENTITY).select_transport(stream, content_type.required_transport_type, content_type.required_components, receiver_full_jid, Set.empty()); |
| 66 |
if (video_transport == null) { |
| 67 |
throw new Jingle.Error.NO_SHARED_PROTOCOLS("No suitable video transports"); |
| 68 |
} |
| 69 |
Jingle.TransportParameters video_transport_params = video_transport.create_transport_parameters(stream, content_type.required_components, my_jid, receiver_full_jid); |
| 70 |
video_content = new Jingle.Content.initiate_sent("video", Jingle.Senders.BOTH, |
| 71 |
content_type, video_content_parameters, |
| 72 |
video_transport, video_transport_params, |
| 73 |
null, null, |
| 74 |
my_jid, receiver_full_jid); |
| 75 |
contents.add(video_content); |
| 76 |
} |
| 77 |
|
| 78 |
// Create session |
| 79 |
try { |
| 80 |
Jingle.Session session = yield jingle_module.create_session(stream, contents, receiver_full_jid, sid, muji_room); |
| 81 |
return session; |
| 82 |
} catch (Jingle.Error e) { |
| 83 |
throw new Jingle.Error.GENERAL(@"Couldn't create Jingle session: $(e.message)"); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public async Jingle.Content add_outgoing_video_content(XmppStream stream, Jingle.Session session, Jid? muji_muc) throws Jingle.Error { |
| 88 |
Jid my_jid = session.local_full_jid; |
| 89 |
Jid receiver_full_jid = session.peer_full_jid; |
| 90 |
|
| 91 |
Jingle.Content? content = null; |
| 92 |
foreach (Jingle.Content c in session.contents) { |
| 93 |
Parameters? parameters = c.content_params as Parameters; |
| 94 |
if (parameters == null) continue; |
| 95 |
|
| 96 |
if (parameters.media == "video") { |
| 97 |
content = c; |
| 98 |
break; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if (content == null) { |
| 103 |
// Content for video does not yet exist -> create it |
| 104 |
Parameters video_content_parameters = new Parameters(this, "video", yield get_supported_payloads("video")); |
| 105 |
video_content_parameters.local_crypto = generate_local_crypto(); |
| 106 |
video_content_parameters.header_extensions.add_all(get_suggested_header_extensions("video")); |
| 107 |
Jingle.Transport? video_transport = yield stream.get_module(Jingle.Module.IDENTITY).select_transport(stream, content_type.required_transport_type, content_type.required_components, receiver_full_jid, Set.empty()); |
| 108 |
if (video_transport == null) { |
| 109 |
throw new Jingle.Error.NO_SHARED_PROTOCOLS("No suitable video transports"); |
| 110 |
} |
| 111 |
Jingle.TransportParameters video_transport_params = video_transport.create_transport_parameters(stream, content_type.required_components, my_jid, receiver_full_jid); |
| 112 |
content = new Jingle.Content.initiate_sent("video", |
| 113 |
session.we_initiated ? Jingle.Senders.INITIATOR : Jingle.Senders.RESPONDER, |
| 114 |
content_type, video_content_parameters, |
| 115 |
video_transport, video_transport_params, |
| 116 |
null, null, |
| 117 |
my_jid, receiver_full_jid); |
| 118 |
|
| 119 |
session.add_content.begin(content); |
| 120 |
} else { |
| 121 |
// Content for video already exists -> modify senders |
| 122 |
bool we_initiated = session.we_initiated; |
| 123 |
Jingle.Senders want_sender = we_initiated ? Jingle.Senders.INITIATOR : Jingle.Senders.RESPONDER; |
| 124 |
if (content.senders == Jingle.Senders.BOTH || content.senders == want_sender) { |
| 125 |
warning("want to add video but senders is already both/target"); |
| 126 |
} else if (content.senders == Jingle.Senders.NONE) { |
| 127 |
content.modify(want_sender); |
| 128 |
} else { |
| 129 |
content.modify(Jingle.Senders.BOTH); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return content; |
| 134 |
} |
| 135 |
|
| 136 |
public override void attach(XmppStream stream) { |
| 137 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); |
| 138 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI_AUDIO); |
| 139 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI_VIDEO); |
| 140 |
stream.get_module(Jingle.Module.IDENTITY).register_content_type(content_type); |
| 141 |
stream.get_module(Jingle.Module.IDENTITY).register_session_info_type(session_info_type); |
| 142 |
} |
| 143 |
|
| 144 |
public override void detach(XmppStream stream) { |
| 145 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI); |
| 146 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI_AUDIO); |
| 147 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI_VIDEO); |
| 148 |
} |
| 149 |
|
| 150 |
public async bool is_available(XmppStream stream, Jid full_jid) { |
| 151 |
bool? has_feature = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); |
| 152 |
if (has_feature == null || !(!)has_feature) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
return yield stream.get_module(Jingle.Module.IDENTITY).is_available(stream, content_type.required_transport_type, content_type.required_components, full_jid); |
| 156 |
} |
| 157 |
|
| 158 |
public override string get_ns() { return NS_URI; } |
| 159 |
public override string get_id() { return IDENTITY.id; } |
| 160 |
} |
| 161 |
|
| 162 |
public class Crypto { |
| 163 |
public const string AES_CM_128_HMAC_SHA1_80 = "AES_CM_128_HMAC_SHA1_80"; |
| 164 |
public const string AES_CM_128_HMAC_SHA1_32 = "AES_CM_128_HMAC_SHA1_32"; |
| 165 |
public const string F8_128_HMAC_SHA1_80 = "F8_128_HMAC_SHA1_80"; |
| 166 |
|
| 167 |
public string crypto_suite { get; private set; } |
| 168 |
public string key_params { get; private set; } |
| 169 |
public string? session_params { get; private set; } |
| 170 |
public string tag { get; private set; } |
| 171 |
|
| 172 |
public uint8[]? key_and_salt { owned get { |
| 173 |
if (!key_params.has_prefix("inline:")) return null; |
| 174 |
int endIndex = key_params.index_of("|"); |
| 175 |
if (endIndex < 0) endIndex = key_params.length; |
| 176 |
string sub = key_params.substring(7, endIndex - 7); |
| 177 |
return Base64.decode(sub); |
| 178 |
}} |
| 179 |
|
| 180 |
public string? lifetime { owned get { |
| 181 |
if (!key_params.has_prefix("inline:")) return null; |
| 182 |
int firstIndex = key_params.index_of("|"); |
| 183 |
if (firstIndex < 0) return null; |
| 184 |
int endIndex = key_params.index_of("|", firstIndex + 1); |
| 185 |
if (endIndex < 0) { |
| 186 |
if (key_params.index_of(":", firstIndex) > 0) return null; // Is MKI |
| 187 |
endIndex = key_params.length; |
| 188 |
} |
| 189 |
return key_params.substring(firstIndex + 1, endIndex); |
| 190 |
}} |
| 191 |
|
| 192 |
public int mki { get { |
| 193 |
if (!key_params.has_prefix("inline:")) return -1; |
| 194 |
int firstIndex = key_params.index_of("|"); |
| 195 |
if (firstIndex < 0) return -1; |
| 196 |
int splitIndex = key_params.index_of(":", firstIndex); |
| 197 |
if (splitIndex < 0) return -1; |
| 198 |
int secondIndex = key_params.index_of("|", firstIndex + 1); |
| 199 |
if (secondIndex < 0) { |
| 200 |
return int.parse(key_params.substring(firstIndex + 1, splitIndex)); |
| 201 |
} else if (splitIndex > secondIndex) { |
| 202 |
return int.parse(key_params.substring(secondIndex + 1, splitIndex)); |
| 203 |
} |
| 204 |
return -1; |
| 205 |
}} |
| 206 |
|
| 207 |
public int mki_length { get { |
| 208 |
if (!key_params.has_prefix("inline:")) return -1; |
| 209 |
int firstIndex = key_params.index_of("|"); |
| 210 |
if (firstIndex < 0) return -1; |
| 211 |
int splitIndex = key_params.index_of(":", firstIndex); |
| 212 |
if (splitIndex < 0) return -1; |
| 213 |
int secondIndex = key_params.index_of("|", firstIndex + 1); |
| 214 |
if (secondIndex < 0 || splitIndex > secondIndex) { |
| 215 |
return int.parse(key_params.substring(splitIndex + 1, key_params.length)); |
| 216 |
} |
| 217 |
return -1; |
| 218 |
}} |
| 219 |
|
| 220 |
public bool is_valid { get { |
| 221 |
switch(crypto_suite) { |
| 222 |
case AES_CM_128_HMAC_SHA1_80: |
| 223 |
case AES_CM_128_HMAC_SHA1_32: |
| 224 |
case F8_128_HMAC_SHA1_80: |
| 225 |
return key_and_salt != null && key_and_salt.length == 30; |
| 226 |
} |
| 227 |
return false; |
| 228 |
}} |
| 229 |
|
| 230 |
public uint8[]? key { owned get { |
| 231 |
uint8[]? key_and_salt = key_and_salt; |
| 232 |
switch(crypto_suite) { |
| 233 |
case AES_CM_128_HMAC_SHA1_80: |
| 234 |
case AES_CM_128_HMAC_SHA1_32: |
| 235 |
case F8_128_HMAC_SHA1_80: |
| 236 |
if (key_and_salt != null && key_and_salt.length >= 16) return key_and_salt[0:16]; |
| 237 |
break; |
| 238 |
} |
| 239 |
return null; |
| 240 |
}} |
| 241 |
|
| 242 |
public uint8[]? salt { owned get { |
| 243 |
uint8[]? key_and_salt = key_and_salt; |
| 244 |
switch(crypto_suite) { |
| 245 |
case AES_CM_128_HMAC_SHA1_80: |
| 246 |
case AES_CM_128_HMAC_SHA1_32: |
| 247 |
case F8_128_HMAC_SHA1_80: |
| 248 |
if (key_and_salt != null && key_and_salt.length >= 30) return key_and_salt[16:30]; |
| 249 |
break; |
| 250 |
} |
| 251 |
return null; |
| 252 |
}} |
| 253 |
|
| 254 |
public static Crypto create(string crypto_suite, uint8[] key_and_salt, string? session_params = null, string tag = "1") { |
| 255 |
Crypto crypto = new Crypto(); |
| 256 |
crypto.crypto_suite = crypto_suite; |
| 257 |
crypto.key_params = "inline:" + Base64.encode(key_and_salt); |
| 258 |
crypto.session_params = session_params; |
| 259 |
crypto.tag = tag; |
| 260 |
return crypto; |
| 261 |
} |
| 262 |
|
| 263 |
public Crypto rekey(uint8[] key_and_salt) { |
| 264 |
Crypto crypto = new Crypto(); |
| 265 |
crypto.crypto_suite = crypto_suite; |
| 266 |
crypto.key_params = "inline:" + Base64.encode(key_and_salt); |
| 267 |
crypto.session_params = session_params; |
| 268 |
crypto.tag = tag; |
| 269 |
return crypto; |
| 270 |
} |
| 271 |
|
| 272 |
public static Crypto parse(StanzaNode node) { |
| 273 |
Crypto crypto = new Crypto(); |
| 274 |
crypto.crypto_suite = node.get_attribute("crypto-suite"); |
| 275 |
crypto.key_params = node.get_attribute("key-params"); |
| 276 |
crypto.session_params = node.get_attribute("session-params"); |
| 277 |
crypto.tag = node.get_attribute("tag"); |
| 278 |
return crypto; |
| 279 |
} |
| 280 |
|
| 281 |
public StanzaNode to_xml() { |
| 282 |
StanzaNode node = new StanzaNode.build("crypto", NS_URI) |
| 283 |
.put_attribute("crypto-suite", crypto_suite) |
| 284 |
.put_attribute("key-params", key_params) |
| 285 |
.put_attribute("tag", tag); |
| 286 |
if (session_params != null) node.put_attribute("session-params", session_params); |
| 287 |
return node; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
} |