1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.impl.protocol.jabber;
import java.util.*;
import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.media.*;
import net.java.sip.communicator.util.*;
import org.jitsi.service.neomedia.*;
import ch.imvs.sdes4j.srtp.*;
/**
* An implementation of the <tt>CallPeerMediaHandler</tt> abstract class for the
* common part of Jabber and Gtalk protocols.
*
* @author Vincent Lucas
* @author Lyubomir Marinov
*/
public abstract class AbstractCallPeerMediaHandlerJabberGTalkImpl
<T extends AbstractCallPeerJabberGTalkImpl<?,?,?>>
extends CallPeerMediaHandler<T>
{
/**
* The <tt>Logger</tt> used by the <tt>CallPeerMediaHandlerJabberImpl</tt>
* class and its instances for logging output.
*/
private static final Logger logger
= Logger.getLogger(AbstractCallPeerMediaHandlerJabberGTalkImpl.class);
/**
* Indicates if the <tt>CallPeer</tt> will support </tt>inputevt</tt>
* extension (i.e. will be able to be remote-controlled).
*/
private boolean localInputEvtAware = false;
/**
* Creates a new handler that will be managing media streams for
* <tt>peer</tt>.
*
* @param peer that <tt>AbstractCallPeerJabberGTalkImpl</tt> instance that
* we will be managing media for.
*/
public AbstractCallPeerMediaHandlerJabberGTalkImpl(T peer)
{
super(peer, peer);
}
/**
* Gets the <tt>inputevt</tt> support: true for enable, false for disable.
*
* @return The state of inputevt support: true for enable, false for
* disable.
*/
public boolean getLocalInputEvtAware()
{
return this.localInputEvtAware;
}
/**
* Enable or disable <tt>inputevt</tt> support (remote-control).
*
* @param enable new state of inputevt support
*/
public void setLocalInputEvtAware(boolean enable)
{
localInputEvtAware = enable;
}
/**
* Detects and adds ZRTP available encryption method present in the
* description given in parameter.
*
* @param isInitiator True if the local call instance is the initiator of
* the call. False otherwise.
* @param description The DESCRIPTION element of the JINGLE element which
* contains the PAYLOAD-TYPE and (more important here) the ENCRYPTION.
* @param mediaType The type of media (AUDIO or VIDEO).
*/
protected void addZrtpAdvertisedEncryptions(
boolean isInitiator,
RtpDescriptionPacketExtension description,
MediaType mediaType)
{
CallPeer peer = getPeer();
Call call = peer.getCall();
/*
* ZRTP is not supported in telephony conferences utilizing the
* server-side technology Jitsi Videobridge yet.
*/
if (call.getConference().isJitsiVideobridge())
return;
// Conforming to XEP-0167 schema there is 0 or 1 ENCRYPTION element for
// a given DESCRIPTION.
EncryptionPacketExtension encryptionPacketExtension
= description.getFirstChildOfType(
EncryptionPacketExtension.class);
if(encryptionPacketExtension != null)
{
AccountID accountID = peer.getProtocolProvider().getAccountID();
if (accountID.getAccountPropertyBoolean(
ProtocolProviderFactory.DEFAULT_ENCRYPTION,
true)
&& accountID.isEncryptionProtocolEnabled(
ZrtpControl.PROTO_NAME)
&& call.isSipZrtpAttribute())
{
// ZRTP
ZrtpHashPacketExtension zrtpHashPacketExtension
= encryptionPacketExtension.getFirstChildOfType(
ZrtpHashPacketExtension.class);
if ((zrtpHashPacketExtension != null)
&& (zrtpHashPacketExtension.getValue() != null))
{
addAdvertisedEncryptionMethod(SrtpControlType.ZRTP);
}
}
}
}
/**
* Detects and adds SDES available encryption method present in the
* description given in parameter.
*
* @param isInitiator True if the local call instance is the initiator of
* the call. False otherwise.
* @param description The DESCRIPTION element of the JINGLE element which
* contains the PAYLOAD-TYPE and (more important here) the ENCRYPTION.
* @param mediaType The type of media (AUDIO or VIDEO).
*/
protected void addSDesAdvertisedEncryptions(
boolean isInitiator,
RtpDescriptionPacketExtension description,
MediaType mediaType)
{
CallPeer peer = getPeer();
/*
* SDES is not supported in telephony conferences utilizing the
* server-side technology Jitsi Videobridge yet.
*/
if (peer.getCall().getConference().isJitsiVideobridge())
return;
// Conforming to XEP-0167 schema there is 0 or 1 ENCRYPTION element for
// a given DESCRIPTION.
EncryptionPacketExtension encryptionPacketExtension
= description.getFirstChildOfType(
EncryptionPacketExtension.class);
if(encryptionPacketExtension != null)
{
AccountID accountID = peer.getProtocolProvider().getAccountID();
// SDES
if(accountID.getAccountPropertyBoolean(
ProtocolProviderFactory.DEFAULT_ENCRYPTION,
true)
&& accountID.isEncryptionProtocolEnabled(
SDesControl.PROTO_NAME))
{
SrtpControls srtpControls = getSrtpControls();
SDesControl sdesControl
= (SDesControl)
srtpControls.getOrCreate(
mediaType,
SrtpControlType.SDES);
SrtpCryptoAttribute selectedSdes
= selectSdesCryptoSuite(
isInitiator,
sdesControl,
encryptionPacketExtension);
if(selectedSdes != null)
{
//found an SDES answer, remove all other controls
removeAndCleanupOtherSrtpControls(
mediaType,
SrtpControlType.SDES);
addAdvertisedEncryptionMethod(SrtpControlType.SDES);
}
else
{
sdesControl.cleanup();
srtpControls.remove(mediaType, SrtpControlType.SDES);
}
}
}
// If we were initiating the encryption, and the remote peer does not
// manage it, then we must remove the unusable SDES srtpControl.
else if(isInitiator)
{
// SDES
SrtpControl sdesControl
= getSrtpControls().remove(mediaType, SrtpControlType.SDES);
if (sdesControl != null)
sdesControl.cleanup();
}
}
/**
* Returns the selected SDES crypto suite selected.
*
* @param isInitiator True if the local call instance is the initiator of
* the call. False otherwise.
* @param sDesControl The SDES based SRTP MediaStream encryption
* control.
* @param encryptionPacketExtension The ENCRYPTION element received from the
* remote peer. This may contain the SDES crypto suites available for the
* remote peer.
*
* @return The selected SDES crypto suite supported by both the local and
* the remote peer. Or null, if there is no crypto suite supported by both
* of the peers.
*/
protected SrtpCryptoAttribute selectSdesCryptoSuite(
boolean isInitiator,
SDesControl sDesControl,
EncryptionPacketExtension encryptionPacketExtension)
{
List<CryptoPacketExtension> cryptoPacketExtensions
= encryptionPacketExtension.getCryptoList();
List<SrtpCryptoAttribute> peerAttributes
= new ArrayList<SrtpCryptoAttribute>(cryptoPacketExtensions.size());
for (CryptoPacketExtension cpe : cryptoPacketExtensions)
peerAttributes.add(cpe.toSrtpCryptoAttribute());
return
isInitiator
? sDesControl.initiatorSelectAttribute(peerAttributes)
: sDesControl.responderSelectAttribute(peerAttributes);
}
/**
* Returns if the remote peer supports ZRTP.
*
* @param encryptionPacketExtension The ENCRYPTION element received from
* the remote peer. This may contain the ZRTP packet element for the remote
* peer.
*
* @return True if the remote peer supports ZRTP. False, otherwise.
*/
protected boolean isRemoteZrtpCapable(
EncryptionPacketExtension encryptionPacketExtension)
{
return
(encryptionPacketExtension.getFirstChildOfType(
ZrtpHashPacketExtension.class)
!= null);
}
/**
* Sets ZRTP element to the ENCRYPTION element of the DESCRIPTION for a
* given media.
*
* @param mediaType The type of media we are modifying the DESCRIPTION to
* integrate the ENCRYPTION element.
* @param description The element containing the media DESCRIPTION and its
* encryption.
* @param remoteDescription The element containing the media DESCRIPTION and
* its encryption for the remote peer. Null, if the local peer is the
* initiator of the call.
*
* @return True if the ZRTP element has been added to encryption. False,
* otherwise.
*/
protected boolean setZrtpEncryptionOnDescription(
MediaType mediaType,
RtpDescriptionPacketExtension description,
RtpDescriptionPacketExtension remoteDescription)
{
CallPeer peer = getPeer();
Call call = peer.getCall();
/*
* ZRTP is not supported in telephony conferences utilizing the
* server-side technology Jitsi Videobridge yet.
*/
if (call.getConference().isJitsiVideobridge())
return false;
boolean isRemoteZrtpCapable;
if (remoteDescription == null)
isRemoteZrtpCapable = true;
else
{
// Conforming to XEP-0167 schema there is 0 or 1 ENCRYPTION element
// for a given DESCRIPTION.
EncryptionPacketExtension remoteEncryption
= remoteDescription.getFirstChildOfType(
EncryptionPacketExtension.class);
isRemoteZrtpCapable
= (remoteEncryption != null)
&& isRemoteZrtpCapable(remoteEncryption);
}
boolean zrtpHashSet = false; // Will become true if at least one is set.
if (isRemoteZrtpCapable)
{
AccountID accountID = peer.getProtocolProvider().getAccountID();
if(accountID.getAccountPropertyBoolean(
ProtocolProviderFactory.DEFAULT_ENCRYPTION,
true)
&& accountID.isEncryptionProtocolEnabled(
ZrtpControl.PROTO_NAME)
&& call.isSipZrtpAttribute())
{
ZrtpControl zrtpControl
= (ZrtpControl)
getSrtpControls().getOrCreate(
mediaType,
SrtpControlType.ZRTP);
int numberSupportedVersions
= zrtpControl.getNumberSupportedVersions();
for (int i = 0; i < numberSupportedVersions; i++)
{
String helloHash[] = zrtpControl.getHelloHashSep(i);
if ((helloHash != null) && (helloHash[1].length() > 0))
{
ZrtpHashPacketExtension hash
= new ZrtpHashPacketExtension();
hash.setVersion(helloHash[0]);
hash.setValue(helloHash[1]);
EncryptionPacketExtension encryption
= description.getFirstChildOfType(
EncryptionPacketExtension.class);
if (encryption == null)
{
encryption = new EncryptionPacketExtension();
description.addChildExtension(encryption);
}
encryption.addChildExtension(hash);
zrtpHashSet = true;
}
}
}
}
return zrtpHashSet;
}
/**
* Sets SDES element(s) to the ENCRYPTION element of the DESCRIPTION for a
* given media.
*
* @param mediaType The type of media we are modifying the DESCRIPTION to
* integrate the ENCRYPTION element.
* @param localDescription The element containing the media DESCRIPTION and
* its encryption.
* @param remoteDescription The element containing the media DESCRIPTION and
* its encryption for the remote peer. Null, if the local peer is the
* initiator of the call.
*
* @return True if the crypto element has been added to encryption. False,
* otherwise.
*/
protected boolean setSDesEncryptionOnDescription(
MediaType mediaType,
RtpDescriptionPacketExtension localDescription,
RtpDescriptionPacketExtension remoteDescription)
{
CallPeer peer = getPeer();
/*
* SDES is not supported in telephony conferences utilizing the
* server-side technology Jitsi Videobridge yet.
*/
if (peer.getCall().getConference().isJitsiVideobridge())
return false;
AccountID accountID = peer.getProtocolProvider().getAccountID();
// check if SDES and encryption is enabled at all
if (accountID.getAccountPropertyBoolean(
ProtocolProviderFactory.DEFAULT_ENCRYPTION,
true)
&& accountID.isEncryptionProtocolEnabled(
SDesControl.PROTO_NAME))
{
// get or create the control
SrtpControls srtpControls = getSrtpControls();
SDesControl sdesControl
= (SDesControl)
srtpControls.getOrCreate(mediaType, SrtpControlType.SDES);
// set the enabled ciphers suites
String ciphers
= accountID.getAccountPropertyString(
ProtocolProviderFactory.SDES_CIPHER_SUITES);
if (ciphers == null)
{
ciphers =
JabberActivator.getResources().getSettingsString(
SDesControl.SDES_CIPHER_SUITES);
}
sdesControl.setEnabledCiphers(Arrays.asList(ciphers.split(",")));
// act as initiator
if (remoteDescription == null)
{
EncryptionPacketExtension localEncryption
= localDescription.getFirstChildOfType(
EncryptionPacketExtension.class);
if(localEncryption == null)
{
localEncryption = new EncryptionPacketExtension();
localDescription.addChildExtension(localEncryption);
}
for(SrtpCryptoAttribute ca:
sdesControl.getInitiatorCryptoAttributes())
{
CryptoPacketExtension crypto
= new CryptoPacketExtension(ca);
localEncryption.addChildExtension(crypto);
}
return true;
}
// act as responder
else
{
// Conforming to XEP-0167 schema there is 0 or 1 ENCRYPTION
// element for a given DESCRIPTION.
EncryptionPacketExtension remoteEncryption
= remoteDescription.getFirstChildOfType(
EncryptionPacketExtension.class);
if(remoteEncryption != null)
{
SrtpCryptoAttribute selectedSdes = selectSdesCryptoSuite(
false,
sdesControl,
remoteEncryption);
if(selectedSdes != null)
{
EncryptionPacketExtension localEncryption
= localDescription.getFirstChildOfType(
EncryptionPacketExtension.class);
if(localEncryption == null)
{
localEncryption = new EncryptionPacketExtension();
localDescription.addChildExtension(localEncryption);
}
CryptoPacketExtension crypto
= new CryptoPacketExtension(selectedSdes);
localEncryption.addChildExtension(crypto);
return true;
}
else
{
// none of the offered suites match, destroy the sdes
// control
sdesControl.cleanup();
srtpControls.remove(mediaType, SrtpControlType.SDES);
logger.warn(
"Received unsupported sdes crypto attribute");
}
}
else
{
// peer doesn't offer any SDES attribute, destroy the sdes
// control
sdesControl.cleanup();
srtpControls.remove(mediaType, SrtpControlType.SDES);
}
}
}
return false;
}
/**
* Selects the preferred encryption protocol (only used by the callee).
*
* @param mediaType The type of media (AUDIO or VIDEO).
* @param localDescription The element containing the media DESCRIPTION and
* its encryption.
* @param remoteDescription The element containing the media DESCRIPTION and
* its encryption for the remote peer; <tt>null</tt> if the local peer is
* the initiator of the call.
*/
protected void setAndAddPreferredEncryptionProtocol(
MediaType mediaType,
RtpDescriptionPacketExtension localDescription,
RtpDescriptionPacketExtension remoteDescription)
{
// Sets ZRTP or SDES, depending on the preferences for this account.
List<String> preferredEncryptionProtocols
= getPeer()
.getProtocolProvider()
.getAccountID()
.getSortedEnabledEncryptionProtocolList();
for(String preferredEncryptionProtocol : preferredEncryptionProtocols)
{
String protoName
= preferredEncryptionProtocol.substring(
ProtocolProviderFactory.ENCRYPTION_PROTOCOL.length()
+ 1);
if (setAndAddPreferredEncryptionProtocol(
protoName,
mediaType,
localDescription,
remoteDescription))
{
// Stop once an encryption advertisement has been chosen.
return;
}
}
}
/**
* Selects a specific encryption protocol if it is the preferred (only used
* by the callee).
*
* @param protoName the name of the encryption protocol which is to be
* selected
* @param mediaType The type of media (AUDIO or VIDEO).
* @param localDescription The element containing the media DESCRIPTION and
* its encryption.
* @param remoteDescription The element containing the media DESCRIPTION and
* its encryption for the remote peer; <tt>null</tt> if the local peer is
* the initiator of the call.
* @return <tt>true</tt> if the specified encryption protocol has been
* selected; <tt>false</tt>, otherwise
*/
protected boolean setAndAddPreferredEncryptionProtocol(
String protoName,
MediaType mediaType,
RtpDescriptionPacketExtension localDescription,
RtpDescriptionPacketExtension remoteDescription)
{
/*
* Neither SDES nor ZRTP is supported in telephony conferences utilizing
* the server-side technology Jitsi Videobridge yet.
*/
if (getPeer().isJitsiVideobridge())
return false;
// SDES
if(SDesControl.PROTO_NAME.equals(protoName))
{
addSDesAdvertisedEncryptions(
false,
remoteDescription,
mediaType);
if(setSDesEncryptionOnDescription(
mediaType,
localDescription,
remoteDescription))
{
// Stop once an encryption advertisement has been chosen.
return true;
}
}
// ZRTP
else if(ZrtpControl.PROTO_NAME.equals(protoName))
{
if(setZrtpEncryptionOnDescription(
mediaType,
localDescription,
remoteDescription))
{
addZrtpAdvertisedEncryptions(
false,
remoteDescription,
mediaType);
// Stop once an encryption advertisement has been chosen.
return true;
}
}
return false;
}
}
|