-
Notifications
You must be signed in to change notification settings - Fork 280
/
SRTCPCryptoContext.java
445 lines (393 loc) · 15.7 KB
/
SRTCPCryptoContext.java
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
/*
* Copyright @ 2015 Atlassian Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jitsi.impl.neomedia.transform.srtp;
import java.util.*;
import org.bouncycastle.crypto.params.*;
import org.jitsi.bccontrib.params.*;
import org.jitsi.service.neomedia.*;
import org.jitsi.utils.*;
/**
* SRTPCryptoContext class is the core class of SRTP implementation. There can
* be multiple SRTP sources in one SRTP session. And each SRTP stream has a
* corresponding SRTPCryptoContext object, identified by SSRC. In this way,
* different sources can be protected independently.
*
* SRTPCryptoContext class acts as a manager class and maintains all the
* information used in SRTP transformation. It is responsible for deriving
* encryption/salting/authentication keys from master keys. And it will invoke
* certain class to encrypt/decrypt (transform/reverse transform) RTP packets.
* It will hold a replay check db and do replay check against incoming packets.
*
* Refer to section 3.2 in RFC3711 for detailed description of cryptographic
* context.
*
* Cryptographic related parameters, i.e. encryption mode / authentication mode,
* master encryption key and master salt key are determined outside the scope of
* SRTP implementation. They can be assigned manually, or can be assigned
* automatically using some key management protocol, such as MIKEY (RFC3830),
* SDES (RFC4568) or Phil Zimmermann's ZRTP protocol (RFC6189).
*
* @author Bing SU (nova.su@gmail.com)
* @author Lyubomir Marinov
*/
public class SRTCPCryptoContext
extends BaseSRTPCryptoContext
{
/**
* Index received so far
*/
private int receivedIndex = 0;
/**
* Index sent so far
*/
private int sentIndex = 0;
/**
* Construct an empty SRTPCryptoContext using ssrc. The other parameters are
* set to default null value.
*
* @param ssrc SSRC of this SRTPCryptoContext
*/
public SRTCPCryptoContext(int ssrc)
{
super(ssrc);
}
/**
* Construct a normal SRTPCryptoContext based on the given parameters.
*
* @param ssrc the RTP SSRC that this SRTP cryptographic context protects.
* @param masterKey byte array holding the master key for this SRTP
* cryptographic context. Refer to chapter 3.2.1 of the RFC about the role
* of the master key.
* @param masterSalt byte array holding the master salt for this SRTP
* cryptographic context. It is used to computer the initialization vector
* that in turn is input to compute the session key, session authentication
* key and the session salt.
* @param policy SRTP policy for this SRTP cryptographic context, defined
* the encryption algorithm, the authentication algorithm, etc
*/
@SuppressWarnings("fallthrough")
public SRTCPCryptoContext(
int ssrc,
byte[] masterK,
byte[] masterS,
SRTPPolicy policy)
{
super(ssrc, masterK, masterS, policy);
}
/**
* Checks if a packet is a replayed on based on its sequence number. The
* method supports a 64 packet history relative to the given sequence
* number. Sequence Number is guaranteed to be real (not faked) through
* authentication.
*
* @param index index number of the SRTCP packet
* @return true if this sequence number indicates the packet is not a
* replayed one, false if not
*/
boolean checkReplay(int index)
{
// compute the index of previously received packet and its
// delta to the new received packet
long delta = index - receivedIndex;
if (delta > 0)
return true; // Packet not yet received
else if (-delta > REPLAY_WINDOW_SIZE)
return false; // Packet too old
else if (((this.replayWindow >> (-delta)) & 0x1) != 0)
return false; // Packet already received!
else
return true; // Packet not yet received
}
/**
* Computes the initialization vector, used later by encryption algorithms,
* based on the label.
*
* @param label label specified for each type of iv
*/
private void computeIv(byte label)
{
for (int i = 0; i < 14; i++)
{
ivStore[i] = masterSalt[i];
}
ivStore[7] ^= label;
ivStore[14] = ivStore[15] = 0;
}
/**
* Derives a new SRTPCryptoContext for use with a new SSRC. The method
* returns a new SRTPCryptoContext initialized with the data of this
* SRTPCryptoContext. Replacing the SSRC, Roll-over-Counter, and the key
* derivation rate the application cab use this SRTPCryptoContext to
* encrypt/decrypt a new stream (Synchronization source) inside one RTP
* session. Before the application can use this SRTPCryptoContext it must
* call the deriveSrtpKeys method.
*
* @param ssrc The SSRC for this context
* @return a new SRTPCryptoContext with all relevant data set.
*/
public SRTCPCryptoContext deriveContext(int ssrc)
{
return new SRTCPCryptoContext(ssrc, masterKey, masterSalt, policy);
}
/**
* Derives the srtcp session keys from the master key.
*/
synchronized public void deriveSrtcpKeys()
{
// compute the session encryption key
computeIv((byte) 3);
cipherCtr.init(masterKey);
Arrays.fill(masterKey, (byte) 0);
Arrays.fill(encKey, (byte) 0);
cipherCtr.process(encKey, 0, policy.getEncKeyLength(), ivStore);
if (authKey != null)
{
computeIv((byte) 4);
Arrays.fill(authKey, (byte) 0);
cipherCtr.process(authKey, 0, policy.getAuthKeyLength(), ivStore);
switch (policy.getAuthType())
{
case SRTPPolicy.HMACSHA1_AUTHENTICATION:
mac.init(new KeyParameter(authKey));
break;
case SRTPPolicy.SKEIN_AUTHENTICATION:
// Skein MAC uses number of bits as MAC size, not just bytes
mac.init(
new ParametersForSkein(
new KeyParameter(authKey),
ParametersForSkein.Skein512,
tagStore.length * 8));
break;
}
Arrays.fill(authKey, (byte) 0);
}
// compute the session salt
computeIv((byte) 5);
Arrays.fill(saltKey, (byte) 0);
cipherCtr.process(saltKey, 0, policy.getSaltKeyLength(), ivStore);
Arrays.fill(masterSalt, (byte) 0);
// As last step: initialize cipher with derived encryption key.
if (cipherF8 != null)
cipherF8.init(encKey, saltKey);
cipherCtr.init(encKey);
Arrays.fill(encKey, (byte) 0);
}
/**
* Performs Counter Mode AES encryption/decryption
*
* @param pkt the RTP packet to be encrypted/decrypted
*/
public void processPacketAESCM(ByteArrayBuffer pkt, int index)
{
int ssrc = (int) RawPacket.getRTCPSSRC(pkt);
/* Compute the CM IV (refer to chapter 4.1.1 in RFC 3711):
*
* k_s XX XX XX XX XX XX XX XX XX XX XX XX XX XX
* SSRC XX XX XX XX
* index XX XX XX XX
* ------------------------------------------------------XOR
* IV XX XX XX XX XX XX XX XX XX XX XX XX XX XX 00 00
* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
*/
ivStore[0] = saltKey[0];
ivStore[1] = saltKey[1];
ivStore[2] = saltKey[2];
ivStore[3] = saltKey[3];
// The shifts transform the ssrc and index into network order
ivStore[4] = (byte) (((ssrc >> 24) & 0xff) ^ saltKey[4]);
ivStore[5] = (byte) (((ssrc >> 16) & 0xff) ^ saltKey[5]);
ivStore[6] = (byte) (((ssrc >> 8) & 0xff) ^ saltKey[6]);
ivStore[7] = (byte) ((ssrc & 0xff) ^ saltKey[7]);
ivStore[8] = saltKey[8];
ivStore[9] = saltKey[9];
ivStore[10] = (byte) (((index >> 24) & 0xff) ^ saltKey[10]);
ivStore[11] = (byte) (((index >> 16) & 0xff) ^ saltKey[11]);
ivStore[12] = (byte) (((index >> 8) & 0xff) ^ saltKey[12]);
ivStore[13] = (byte) ((index & 0xff) ^ saltKey[13]);
ivStore[14] = ivStore[15] = 0;
// Encrypted part excludes fixed header (8 bytes)
int payloadOffset = 8;
int payloadLength = pkt.getLength() - payloadOffset;
cipherCtr.process(
pkt.getBuffer(), pkt.getOffset() + payloadOffset, payloadLength,
ivStore);
}
/**
* Performs F8 Mode AES encryption/decryption
*
* @param pkt the RTP packet to be encrypted/decrypted
*/
public void processPacketAESF8(ByteArrayBuffer pkt, int index)
{
// 4 bytes of the iv are zero
// the first byte of the RTP header is not used.
ivStore[0] = 0;
ivStore[1] = 0;
ivStore[2] = 0;
ivStore[3] = 0;
// Need the encryption flag
index = index | 0x80000000;
// set the index and the encrypt flag in network order into IV
ivStore[4] = (byte) (index >> 24);
ivStore[5] = (byte) (index >> 16);
ivStore[6] = (byte) (index >> 8);
ivStore[7] = (byte) index;
// The fixed header follows and fills the rest of the IV
System.arraycopy(pkt.getBuffer(), pkt.getOffset(), ivStore, 8, 8);
// Encrypted part excludes fixed header (8 bytes), index (4 bytes), and
// authentication tag (variable according to policy)
int payloadOffset = 8;
int payloadLength = pkt.getLength() - (4 + policy.getAuthTagLength());
cipherF8.process(
pkt.getBuffer(), pkt.getOffset() + payloadOffset, payloadLength,
ivStore);
}
/**
* Transform a SRTCP packet into a RTCP packet. The method is called when an
* SRTCP packet was received. Operations done by the method include:
* authentication check, packet replay check and decryption. Both encryption
* and authentication functionality can be turned off as long as the
* SRTPPolicy used in this SRTPCryptoContext requires no encryption and no
* authentication. Then the packet will be sent out untouched. However, this
* is not encouraged. If no SRTCP feature is enabled, then we shall not use
* SRTP TransformConnector. We should use the original method (RTPManager
* managed transportation) instead.
*
* @param pkt the received RTCP packet
* @return <tt>true</tt> if the packet can be accepted or <tt>false</tt> if
* authentication or replay check failed
*/
synchronized public boolean reverseTransformPacket(ByteArrayBuffer pkt)
{
boolean decrypt = false;
int tagLength = policy.getAuthTagLength();
int indexEflag = RawPacket.getSRTCPIndex(pkt, tagLength);
if ((indexEflag & 0x80000000) == 0x80000000)
decrypt = true;
int index = indexEflag & ~0x80000000;
/* Replay control */
if (!checkReplay(index))
{
return false;
}
/* Authenticate the packet */
if (policy.getAuthType() != SRTPPolicy.NULL_AUTHENTICATION)
{
// get original authentication data and store in tempStore
pkt.readRegionToBuff(pkt.getLength() - tagLength, tagLength,
tempStore);
// Shrink packet to remove the authentication tag and index
// because this is part of authenticated data
pkt.shrink(tagLength + 4);
// compute, then save authentication in tagStore
authenticatePacketHMAC(pkt, indexEflag);
// compare authentication tags using constant time comparison
int nonEqual = 0;
for (int i = 0; i < tagLength; i++)
{
nonEqual |= (tempStore[i] ^ tagStore[i]);
}
if (nonEqual != 0)
return false;
}
if (decrypt)
{
/* Decrypt the packet using Counter Mode encryption */
if (policy.getEncType() == SRTPPolicy.AESCM_ENCRYPTION
|| policy.getEncType() == SRTPPolicy.TWOFISH_ENCRYPTION)
{
processPacketAESCM(pkt, index);
}
/* Decrypt the packet using F8 Mode encryption */
else if (policy.getEncType() == SRTPPolicy.AESF8_ENCRYPTION
|| policy.getEncType() == SRTPPolicy.TWOFISHF8_ENCRYPTION)
{
processPacketAESF8(pkt, index);
}
}
update(index);
return true;
}
/**
* Transform a RTP packet into a SRTP packet. The method is called when a
* normal RTP packet ready to be sent. Operations done by the transformation
* may include: encryption, using either Counter Mode encryption, or F8 Mode
* encryption, adding authentication tag, currently HMC SHA1 method. Both
* encryption and authentication functionality can be turned off as long as
* the SRTPPolicy used in this SRTPCryptoContext is requires no encryption
* and no authentication. Then the packet will be sent out untouched.
* However, this is not encouraged. If no SRTP feature is enabled, then we
* shall not use SRTP TransformConnector. We should use the original method
* (RTPManager managed transportation) instead.
*
* @param pkt the RTP packet that is going to be sent out
*/
synchronized public void transformPacket(ByteArrayBuffer pkt)
{
boolean encrypt = false;
/* Encrypt the packet using Counter Mode encryption */
if (policy.getEncType() == SRTPPolicy.AESCM_ENCRYPTION ||
policy.getEncType() == SRTPPolicy.TWOFISH_ENCRYPTION)
{
processPacketAESCM(pkt, sentIndex);
encrypt = true;
}
/* Encrypt the packet using F8 Mode encryption */
else if (policy.getEncType() == SRTPPolicy.AESF8_ENCRYPTION ||
policy.getEncType() == SRTPPolicy.TWOFISHF8_ENCRYPTION)
{
processPacketAESF8(pkt, sentIndex);
encrypt = true;
}
int index = 0;
if (encrypt)
index = sentIndex | 0x80000000;
// Grow packet storage in one step
pkt.grow(4 + policy.getAuthTagLength());
// Authenticate the packet
// The authenticate method gets the index via parameter and stores
// it in network order in rbStore variable.
if (policy.getAuthType() != SRTPPolicy.NULL_AUTHENTICATION)
{
authenticatePacketHMAC(pkt, index);
pkt.append(rbStore, 4);
pkt.append(tagStore, policy.getAuthTagLength());
}
sentIndex++;
sentIndex &= ~0x80000000; // clear possible overflow
}
/**
* Updates the SRTP packet index. The method is called after all checks were
* successful.
*
* @param index index number of the accepted packet
*/
private void update(int index)
{
int delta = receivedIndex - index;
/* update the replay bit mask */
if (delta > 0)
{
replayWindow = replayWindow << delta;
replayWindow |= 1;
}
else
{
replayWindow |= ( 1 << delta );
}
receivedIndex = index;
}
}