Coverage Report

Created: 2026-04-12 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsrtp/srtp/srtp.c
Line
Count
Source
1
/*
2
 * srtp.c
3
 *
4
 * the secure real-time transport protocol
5
 *
6
 * David A. McGrew
7
 * Cisco Systems, Inc.
8
 */
9
/*
10
 *
11
 * Copyright (c) 2001-2017, Cisco Systems, Inc.
12
 * All rights reserved.
13
 *
14
 * Redistribution and use in source and binary forms, with or without
15
 * modification, are permitted provided that the following conditions
16
 * are met:
17
 *
18
 *   Redistributions of source code must retain the above copyright
19
 *   notice, this list of conditions and the following disclaimer.
20
 *
21
 *   Redistributions in binary form must reproduce the above
22
 *   copyright notice, this list of conditions and the following
23
 *   disclaimer in the documentation and/or other materials provided
24
 *   with the distribution.
25
 *
26
 *   Neither the name of the Cisco Systems, Inc. nor the names of its
27
 *   contributors may be used to endorse or promote products derived
28
 *   from this software without specific prior written permission.
29
 *
30
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41
 * OF THE POSSIBILITY OF SUCH DAMAGE.
42
 *
43
 */
44
45
#include "srtp_priv.h"
46
#include "stream_list_priv.h"
47
#include "crypto_types.h"
48
#include "err.h"
49
#include "alloc.h" /* for srtp_crypto_alloc() */
50
51
#ifdef GCM
52
#include "aes_gcm.h" /* for AES GCM mode */
53
#endif
54
55
#ifdef OPENSSL_KDF
56
#include <openssl/kdf.h>
57
#include "aes_icm_ext.h"
58
#endif
59
60
#ifdef WOLFSSL
61
#ifdef HAVE_CONFIG_H
62
#include <config.h>
63
#endif
64
#ifndef WOLFSSL_USER_SETTINGS
65
#include <wolfssl/options.h>
66
#endif
67
#include <wolfssl/wolfcrypt/settings.h>
68
#ifdef WOLFSSL_KDF
69
#include <wolfssl/wolfcrypt/kdf.h>
70
#endif
71
#endif
72
73
#include <limits.h>
74
#ifdef HAVE_NETINET_IN_H
75
#include <netinet/in.h>
76
#elif defined(HAVE_WINSOCK2_H)
77
#include <winsock2.h>
78
#endif
79
80
/* the debug module for srtp */
81
srtp_debug_module_t mod_srtp = {
82
    false, /* debugging is off by default */
83
    "srtp" /* printable name for module */
84
};
85
86
static const size_t octets_in_rtp_header = 12;
87
static const size_t octets_in_rtcp_header = 8;
88
static const size_t octets_in_rtp_xtn_hdr = 4;
89
90
static const uint16_t xtn_hdr_one_byte_profile = 0xbede;
91
static const uint16_t xtn_hdr_two_byte_profile = 0x1000;
92
93
static const uint16_t cryptex_one_byte_profile = 0xc0de;
94
static const uint16_t cryptex_two_byte_profile = 0xc2de;
95
96
static size_t srtp_get_rtp_hdr_len(const srtp_hdr_t *hdr)
97
269k
{
98
269k
    return octets_in_rtp_header + 4 * hdr->cc;
99
269k
}
100
101
/*
102
 * Returns the location of the header extention cast to a srtp_hdr_xtnd_t
103
 * struct. Will always return a value and assumes that the caller has already
104
 * verified that a header extension is present by checking the x bit of
105
 * srtp_hdr_t.
106
 */
107
static srtp_hdr_xtnd_t *srtp_get_rtp_xtn_hdr(const srtp_hdr_t *hdr,
108
                                             uint8_t *rtp)
109
1.45k
{
110
1.45k
    return (srtp_hdr_xtnd_t *)(rtp + srtp_get_rtp_hdr_len(hdr));
111
1.45k
}
112
113
/*
114
 * Returns the length of the extension header including the extension header
115
 * header so will return a minium of 4. Assumes the srtp_hdr_t is a valid
116
 * pointer and that the caller has already verified that a header extension is
117
 * valid by checking the x bit of the RTP header.
118
 */
119
static size_t srtp_get_rtp_hdr_xtnd_len(const srtp_hdr_t *hdr,
120
                                        const uint8_t *rtp)
121
5.91k
{
122
5.91k
    const srtp_hdr_xtnd_t *xtn_hdr =
123
5.91k
        (const srtp_hdr_xtnd_t *)(rtp + srtp_get_rtp_hdr_len(hdr));
124
5.91k
    return (ntohs(xtn_hdr->length) + 1u) * 4u;
125
5.91k
}
126
127
static uint16_t srtp_get_rtp_hdr_xtnd_profile(const srtp_hdr_t *hdr,
128
                                              const uint8_t *rtp)
129
0
{
130
0
    const srtp_hdr_xtnd_t *xtn_hdr =
131
0
        (const srtp_hdr_xtnd_t *)(rtp + srtp_get_rtp_hdr_len(hdr));
132
0
    return ntohs(xtn_hdr->profile_specific);
133
0
}
134
135
static void srtp_cryptex_move_hdr_xtnd_hdr_before_csrc(const srtp_hdr_t *hdr,
136
                                                       uint8_t *rtp)
137
0
{
138
0
    if (hdr->cc) {
139
0
        uint8_t tmp[4];
140
0
        uint8_t *xtn_hdr = rtp + srtp_get_rtp_hdr_len(hdr);
141
0
        uint8_t *csrc_list = rtp + octets_in_rtp_header;
142
0
        size_t csrc_list_size = hdr->cc * 4;
143
0
        memcpy(tmp, xtn_hdr, 4);
144
0
        memmove(csrc_list + 4, csrc_list, csrc_list_size);
145
0
        memcpy(csrc_list, tmp, 4);
146
0
    }
147
0
}
148
149
static void srtp_cryptex_move_csrc_before_hdr_xtnd_hdr(const srtp_hdr_t *hdr,
150
                                                       uint8_t *rtp)
151
0
{
152
0
    if (hdr->cc) {
153
0
        uint8_t tmp[4];
154
0
        uint8_t *xtn_hdr = rtp + srtp_get_rtp_hdr_len(hdr);
155
0
        uint8_t *csrc_list = rtp + octets_in_rtp_header;
156
0
        size_t csrc_list_size = hdr->cc * 4;
157
0
        memcpy(tmp, csrc_list, 4);
158
0
        memmove(csrc_list, csrc_list + 4, csrc_list_size);
159
0
        memcpy(xtn_hdr, tmp, 4);
160
0
    }
161
0
}
162
163
static srtp_err_status_t srtp_cryptex_protect_init(
164
    const srtp_stream_ctx_t *stream,
165
    const srtp_hdr_t *hdr,
166
    const uint8_t *rtp,
167
    const uint8_t *srtp,
168
    bool *inuse,
169
    bool *inplace,
170
    size_t *enc_start)
171
29.3k
{
172
29.3k
    if (stream->use_cryptex && (stream->rtp_services & sec_serv_conf)) {
173
0
        if (hdr->cc && hdr->x == 0) {
174
            /* Cryptex can only encrypt CSRCs if header extension is present */
175
0
            return srtp_err_status_cryptex_err;
176
0
        }
177
0
        *inuse = hdr->x == 1;
178
29.3k
    } else {
179
29.3k
        *inuse = false;
180
29.3k
    }
181
182
29.3k
    *inplace = *inuse && rtp == srtp;
183
184
29.3k
    if (*inuse) {
185
0
        *enc_start -=
186
0
            (srtp_get_rtp_hdr_xtnd_len(hdr, rtp) - octets_in_rtp_xtn_hdr);
187
0
        if (*inplace) {
188
0
            *enc_start -= (hdr->cc * 4);
189
0
        }
190
0
    }
191
192
29.3k
    return srtp_err_status_ok;
193
29.3k
}
194
195
static srtp_err_status_t srtp_cryptex_protect(bool inplace,
196
                                              const srtp_hdr_t *hdr,
197
                                              uint8_t *srtp,
198
                                              srtp_cipher_t *rtp_cipher)
199
0
{
200
0
    srtp_hdr_xtnd_t *xtn_hdr = srtp_get_rtp_xtn_hdr(hdr, srtp);
201
0
    uint16_t profile = ntohs(xtn_hdr->profile_specific);
202
0
    if (profile == xtn_hdr_one_byte_profile) {
203
0
        xtn_hdr->profile_specific = htons(cryptex_one_byte_profile);
204
0
    } else if (profile == xtn_hdr_two_byte_profile) {
205
0
        xtn_hdr->profile_specific = htons(cryptex_two_byte_profile);
206
0
    } else {
207
0
        return srtp_err_status_parse_err;
208
0
    }
209
210
0
    if (inplace) {
211
0
        srtp_cryptex_move_hdr_xtnd_hdr_before_csrc(hdr, srtp);
212
0
    } else {
213
0
        if (hdr->cc) {
214
0
            uint8_t *cc_list = srtp + octets_in_rtp_header;
215
0
            size_t cc_list_size = hdr->cc * 4;
216
            /* CSRCs are in dst header already, enc in place */
217
0
            srtp_err_status_t status = srtp_cipher_encrypt(
218
0
                rtp_cipher, cc_list, cc_list_size, cc_list, &cc_list_size);
219
0
            if (status) {
220
0
                return srtp_err_status_cipher_fail;
221
0
            }
222
0
        }
223
0
    }
224
225
0
    return srtp_err_status_ok;
226
0
}
227
228
static void srtp_cryptex_protect_cleanup(bool inplace,
229
                                         const srtp_hdr_t *hdr,
230
                                         uint8_t *srtp)
231
0
{
232
0
    if (inplace) {
233
0
        srtp_cryptex_move_csrc_before_hdr_xtnd_hdr(hdr, srtp);
234
0
    }
235
0
}
236
237
static srtp_err_status_t srtp_cryptex_unprotect_init(
238
    const srtp_stream_ctx_t *stream,
239
    const srtp_hdr_t *hdr,
240
    const uint8_t *srtp,
241
    const uint8_t *rtp,
242
    bool *inuse,
243
    bool *inplace,
244
    size_t *enc_start)
245
101k
{
246
101k
    if (stream->use_cryptex && hdr->x == 1) {
247
0
        uint16_t profile = srtp_get_rtp_hdr_xtnd_profile(hdr, rtp);
248
0
        *inuse = profile == cryptex_one_byte_profile ||
249
0
                 profile == cryptex_two_byte_profile;
250
101k
    } else {
251
101k
        *inuse = false;
252
101k
    }
253
254
101k
    *inplace = *inuse && srtp == rtp;
255
256
101k
    if (*inuse) {
257
0
        *enc_start -=
258
0
            (srtp_get_rtp_hdr_xtnd_len(hdr, rtp) - octets_in_rtp_xtn_hdr);
259
0
        if (*inplace) {
260
0
            *enc_start -= (hdr->cc * 4);
261
0
        }
262
0
    }
263
264
101k
    return srtp_err_status_ok;
265
101k
}
266
267
static srtp_err_status_t srtp_cryptex_unprotect(bool inplace,
268
                                                const srtp_hdr_t *hdr,
269
                                                uint8_t *rtp,
270
                                                srtp_cipher_t *rtp_cipher)
271
0
{
272
0
    if (inplace) {
273
0
        srtp_cryptex_move_hdr_xtnd_hdr_before_csrc(hdr, rtp);
274
0
    } else {
275
0
        if (hdr->cc) {
276
0
            uint8_t *cc_list = rtp + octets_in_rtp_header;
277
0
            size_t cc_list_size = hdr->cc * 4;
278
            /* CSRCs are in dst header already, enc in place */
279
0
            srtp_err_status_t status = srtp_cipher_decrypt(
280
0
                rtp_cipher, cc_list, cc_list_size, cc_list, &cc_list_size);
281
0
            if (status) {
282
0
                return srtp_err_status_cipher_fail;
283
0
            }
284
0
        }
285
0
    }
286
287
0
    return srtp_err_status_ok;
288
0
}
289
290
static void srtp_cryptex_unprotect_cleanup(bool inplace,
291
                                           const srtp_hdr_t *hdr,
292
                                           uint8_t *rtp)
293
0
{
294
0
    if (inplace) {
295
0
        srtp_cryptex_move_csrc_before_hdr_xtnd_hdr(hdr, rtp);
296
0
    }
297
298
0
    srtp_hdr_xtnd_t *xtn_hdr = srtp_get_rtp_xtn_hdr(hdr, rtp);
299
0
    uint16_t profile = ntohs(xtn_hdr->profile_specific);
300
0
    if (profile == cryptex_one_byte_profile) {
301
0
        xtn_hdr->profile_specific = htons(xtn_hdr_one_byte_profile);
302
0
    } else if (profile == cryptex_two_byte_profile) {
303
0
        xtn_hdr->profile_specific = htons(xtn_hdr_two_byte_profile);
304
0
    }
305
0
}
306
307
static srtp_err_status_t srtp_validate_rtp_header(const uint8_t *rtp,
308
                                                  size_t pkt_octet_len)
309
131k
{
310
131k
    const srtp_hdr_t *hdr = (const srtp_hdr_t *)rtp;
311
131k
    size_t rtp_header_len;
312
313
131k
    if (pkt_octet_len < octets_in_rtp_header) {
314
32
        return srtp_err_status_bad_param;
315
32
    }
316
317
    /* Check RTP header length */
318
131k
    rtp_header_len = srtp_get_rtp_hdr_len(hdr);
319
131k
    if (pkt_octet_len < rtp_header_len) {
320
38
        return srtp_err_status_bad_param;
321
38
    }
322
323
    /* Verifying profile length. */
324
131k
    if (hdr->x == 1) {
325
3.01k
        if (pkt_octet_len < rtp_header_len + octets_in_rtp_xtn_hdr) {
326
12
            return srtp_err_status_bad_param;
327
12
        }
328
329
3.00k
        rtp_header_len += srtp_get_rtp_hdr_xtnd_len(hdr, rtp);
330
3.00k
        if (pkt_octet_len < rtp_header_len) {
331
73
            return srtp_err_status_bad_param;
332
73
        }
333
3.00k
    }
334
335
131k
    return srtp_err_status_ok;
336
131k
}
337
338
const char *srtp_get_version_string(void)
339
0
{
340
    /*
341
     * Simply return the autotools generated string
342
     */
343
0
    return SRTP_VER_STRING;
344
0
}
345
346
unsigned int srtp_get_version(void)
347
0
{
348
0
    unsigned int major = 0, minor = 0, micro = 0;
349
0
    unsigned int rv = 0;
350
0
    int parse_rv;
351
352
    /*
353
     * Parse the autotools generated version
354
     */
355
0
    parse_rv = sscanf(SRTP_VERSION, "%u.%u.%u", &major, &minor, &micro);
356
0
    if (parse_rv != 3) {
357
        /*
358
         * We're expected to parse all 3 version levels.
359
         * If not, then this must not be an official release.
360
         * Return all zeros on the version
361
         */
362
0
        return (0);
363
0
    }
364
365
    /*
366
     * We allow 8 bits for the major and minor, while
367
     * allowing 16 bits for the micro.  16 bits for the micro
368
     * may be beneficial for a continuous delivery model
369
     * in the future.
370
     */
371
0
    rv |= (major & 0xFF) << 24;
372
0
    rv |= (minor & 0xFF) << 16;
373
0
    rv |= micro & 0xFF;
374
0
    return rv;
375
0
}
376
377
static srtp_err_status_t srtp_stream_dealloc(
378
    srtp_stream_ctx_t *stream,
379
    const srtp_stream_ctx_t *stream_template)
380
112k
{
381
112k
    srtp_err_status_t status;
382
112k
    srtp_session_keys_t *session_keys = NULL;
383
112k
    srtp_session_keys_t *template_session_keys = NULL;
384
385
    /*
386
     * we use a conservative deallocation strategy - if any deallocation
387
     * fails, then we report that fact without trying to deallocate
388
     * anything else
389
     */
390
112k
    if (stream->session_keys) {
391
229k
        for (size_t i = 0; i < stream->num_master_keys; i++) {
392
117k
            session_keys = &stream->session_keys[i];
393
394
117k
            if (stream_template &&
395
110k
                stream->num_master_keys == stream_template->num_master_keys) {
396
109k
                template_session_keys = &stream_template->session_keys[i];
397
109k
            } else {
398
8.12k
                template_session_keys = NULL;
399
8.12k
            }
400
401
            /*
402
             * deallocate cipher, if it is not the same as that in template
403
             */
404
117k
            if (template_session_keys &&
405
109k
                session_keys->rtp_cipher == template_session_keys->rtp_cipher) {
406
                /* do nothing */
407
102k
            } else if (session_keys->rtp_cipher) {
408
14.4k
                status = srtp_cipher_dealloc(session_keys->rtp_cipher);
409
14.4k
                if (status) {
410
0
                    return status;
411
0
                }
412
14.4k
            }
413
414
            /*
415
             * deallocate auth function, if it is not the same as that in
416
             * template
417
             */
418
117k
            if (template_session_keys &&
419
109k
                session_keys->rtp_auth == template_session_keys->rtp_auth) {
420
                /* do nothing */
421
102k
            } else if (session_keys->rtp_auth) {
422
14.4k
                status = srtp_auth_dealloc(session_keys->rtp_auth);
423
14.4k
                if (status) {
424
0
                    return status;
425
0
                }
426
14.4k
            }
427
428
117k
            if (template_session_keys &&
429
109k
                session_keys->rtp_xtn_hdr_cipher ==
430
109k
                    template_session_keys->rtp_xtn_hdr_cipher) {
431
                /* do nothing */
432
107k
            } else if (session_keys->rtp_xtn_hdr_cipher) {
433
4.16k
                status = srtp_cipher_dealloc(session_keys->rtp_xtn_hdr_cipher);
434
4.16k
                if (status) {
435
0
                    return status;
436
0
                }
437
4.16k
            }
438
439
            /*
440
             * deallocate rtcp cipher, if it is not the same as that in
441
             * template
442
             */
443
117k
            if (template_session_keys &&
444
109k
                session_keys->rtcp_cipher ==
445
109k
                    template_session_keys->rtcp_cipher) {
446
                /* do nothing */
447
102k
            } else if (session_keys->rtcp_cipher) {
448
14.4k
                status = srtp_cipher_dealloc(session_keys->rtcp_cipher);
449
14.4k
                if (status) {
450
0
                    return status;
451
0
                }
452
14.4k
            }
453
454
            /*
455
             * deallocate rtcp auth function, if it is not the same as that in
456
             * template
457
             */
458
117k
            if (template_session_keys &&
459
109k
                session_keys->rtcp_auth == template_session_keys->rtcp_auth) {
460
                /* do nothing */
461
102k
            } else if (session_keys->rtcp_auth) {
462
14.4k
                status = srtp_auth_dealloc(session_keys->rtcp_auth);
463
14.4k
                if (status) {
464
0
                    return status;
465
0
                }
466
14.4k
            }
467
468
            /*
469
             * zeroize the salt value
470
             */
471
117k
            octet_string_set_to_zero(session_keys->salt, SRTP_AEAD_SALT_LEN);
472
117k
            octet_string_set_to_zero(session_keys->c_salt, SRTP_AEAD_SALT_LEN);
473
474
117k
            if (session_keys->mki_id) {
475
0
                octet_string_set_to_zero(session_keys->mki_id,
476
0
                                         stream->mki_size);
477
0
                srtp_crypto_free(session_keys->mki_id);
478
0
                session_keys->mki_id = NULL;
479
0
            }
480
481
            /*
482
             * deallocate key usage limit, if it is not the same as that in
483
             * template
484
             */
485
117k
            if (template_session_keys &&
486
109k
                session_keys->limit == template_session_keys->limit) {
487
                /* do nothing */
488
102k
            } else if (session_keys->limit) {
489
14.4k
                srtp_crypto_free(session_keys->limit);
490
14.4k
            }
491
117k
        }
492
112k
        srtp_crypto_free(stream->session_keys);
493
112k
    }
494
495
112k
    status = srtp_rdbx_dealloc(&stream->rtp_rdbx);
496
112k
    if (status) {
497
0
        return status;
498
0
    }
499
500
112k
    if (stream_template &&
501
107k
        stream->enc_xtn_hdr == stream_template->enc_xtn_hdr) {
502
        /* do nothing */
503
106k
    } else if (stream->enc_xtn_hdr) {
504
2.00k
        srtp_crypto_free(stream->enc_xtn_hdr);
505
2.00k
    }
506
507
    /* deallocate srtp stream context */
508
112k
    srtp_crypto_free(stream);
509
510
112k
    return srtp_err_status_ok;
511
112k
}
512
513
/* try to insert stream in list or deallocate it */
514
static srtp_err_status_t srtp_insert_or_dealloc_stream(srtp_stream_list_t list,
515
                                                       srtp_stream_t stream,
516
                                                       srtp_stream_t template)
517
109k
{
518
109k
    srtp_err_status_t status = srtp_stream_list_insert(list, stream);
519
    /* on failure, ownership wasn't transferred and we need to deallocate */
520
109k
    if (status) {
521
0
        srtp_stream_dealloc(stream, template);
522
0
    }
523
109k
    return status;
524
109k
}
525
526
struct remove_and_dealloc_streams_data {
527
    srtp_err_status_t status;
528
    srtp_stream_list_t list;
529
    srtp_stream_t template;
530
};
531
532
static bool remove_and_dealloc_streams_cb(srtp_stream_t stream, void *data)
533
108k
{
534
108k
    struct remove_and_dealloc_streams_data *d =
535
108k
        (struct remove_and_dealloc_streams_data *)data;
536
108k
    srtp_stream_list_remove(d->list, stream);
537
108k
    d->status = srtp_stream_dealloc(stream, d->template);
538
108k
    if (d->status) {
539
0
        return false;
540
0
    }
541
108k
    return true;
542
108k
}
543
544
static srtp_err_status_t srtp_remove_and_dealloc_streams(
545
    srtp_stream_list_t list,
546
    srtp_stream_t template)
547
3.26k
{
548
3.26k
    struct remove_and_dealloc_streams_data data = { srtp_err_status_ok, list,
549
3.26k
                                                    template };
550
3.26k
    srtp_stream_list_for_each(list, remove_and_dealloc_streams_cb, &data);
551
3.26k
    return data.status;
552
3.26k
}
553
554
static srtp_err_status_t srtp_valid_policy(const srtp_policy_t *policy)
555
33.2k
{
556
33.2k
    if (policy == NULL) {
557
0
        return srtp_err_status_bad_param;
558
0
    }
559
560
33.2k
    if (policy->key == NULL) {
561
3.01k
        if (policy->num_master_keys <= 0) {
562
50
            return srtp_err_status_bad_param;
563
50
        }
564
565
2.96k
        if (policy->num_master_keys > SRTP_MAX_NUM_MASTER_KEYS) {
566
50
            return srtp_err_status_bad_param;
567
50
        }
568
569
2.91k
        if (policy->use_mki) {
570
0
            if (policy->mki_size == 0 || policy->mki_size > SRTP_MAX_MKI_LEN) {
571
0
                return srtp_err_status_bad_param;
572
0
            }
573
2.91k
        } else if (policy->mki_size != 0) {
574
0
            return srtp_err_status_bad_param;
575
0
        }
576
577
20.6k
        for (size_t i = 0; i < policy->num_master_keys; i++) {
578
17.6k
            if (policy->keys[i]->key == NULL) {
579
0
                return srtp_err_status_bad_param;
580
0
            }
581
17.6k
            if (policy->use_mki && policy->keys[i]->mki_id == NULL) {
582
0
                return srtp_err_status_bad_param;
583
0
            }
584
17.6k
        }
585
30.2k
    } else {
586
30.2k
        if (policy->use_mki || policy->mki_size != 0) {
587
0
            return srtp_err_status_bad_param;
588
0
        }
589
30.2k
    }
590
591
33.1k
    return srtp_err_status_ok;
592
33.2k
}
593
594
static srtp_err_status_t srtp_stream_alloc(srtp_stream_ctx_t **str_ptr,
595
                                           const srtp_policy_t *p)
596
9.96k
{
597
9.96k
    srtp_stream_ctx_t *str;
598
9.96k
    srtp_err_status_t stat;
599
9.96k
    size_t i = 0;
600
9.96k
    srtp_session_keys_t *session_keys = NULL;
601
602
9.96k
    stat = srtp_valid_policy(p);
603
9.96k
    if (stat != srtp_err_status_ok) {
604
0
        return stat;
605
0
    }
606
607
    /*
608
     * This function allocates the stream context, rtp and rtcp ciphers
609
     * and auth functions, and key limit structure.  If there is a
610
     * failure during allocation, we free all previously allocated
611
     * memory and return a failure code.  The code could probably
612
     * be improved, but it works and should be clear.
613
     */
614
615
    /* allocate srtp stream and set str_ptr */
616
9.96k
    str = (srtp_stream_ctx_t *)srtp_crypto_alloc(sizeof(srtp_stream_ctx_t));
617
9.96k
    if (str == NULL) {
618
0
        return srtp_err_status_alloc_fail;
619
0
    }
620
621
9.96k
    *str_ptr = str;
622
623
    /*
624
     *To keep backwards API compatible if someone is using multiple master
625
     * keys then key should be set to NULL
626
     */
627
9.96k
    if (p->key != NULL) {
628
9.06k
        str->num_master_keys = 1;
629
9.06k
    } else {
630
906
        str->num_master_keys = p->num_master_keys;
631
906
    }
632
633
9.96k
    str->session_keys = (srtp_session_keys_t *)srtp_crypto_alloc(
634
9.96k
        sizeof(srtp_session_keys_t) * str->num_master_keys);
635
636
9.96k
    if (str->session_keys == NULL) {
637
0
        srtp_stream_dealloc(str, NULL);
638
0
        return srtp_err_status_alloc_fail;
639
0
    }
640
641
24.4k
    for (i = 0; i < str->num_master_keys; i++) {
642
14.4k
        session_keys = &str->session_keys[i];
643
644
        /* allocate cipher */
645
14.4k
        stat = srtp_crypto_kernel_alloc_cipher(
646
14.4k
            p->rtp.cipher_type, &session_keys->rtp_cipher,
647
14.4k
            p->rtp.cipher_key_len, p->rtp.auth_tag_len);
648
14.4k
        if (stat) {
649
0
            srtp_stream_dealloc(str, NULL);
650
0
            return stat;
651
0
        }
652
653
        /* allocate auth function */
654
14.4k
        stat = srtp_crypto_kernel_alloc_auth(
655
14.4k
            p->rtp.auth_type, &session_keys->rtp_auth, p->rtp.auth_key_len,
656
14.4k
            p->rtp.auth_tag_len);
657
14.4k
        if (stat) {
658
0
            srtp_stream_dealloc(str, NULL);
659
0
            return stat;
660
0
        }
661
662
        /*
663
         * ...and now the RTCP-specific initialization - first, allocate
664
         * the cipher
665
         */
666
14.4k
        stat = srtp_crypto_kernel_alloc_cipher(
667
14.4k
            p->rtcp.cipher_type, &session_keys->rtcp_cipher,
668
14.4k
            p->rtcp.cipher_key_len, p->rtcp.auth_tag_len);
669
14.4k
        if (stat) {
670
0
            srtp_stream_dealloc(str, NULL);
671
0
            return stat;
672
0
        }
673
674
        /* allocate auth function */
675
14.4k
        stat = srtp_crypto_kernel_alloc_auth(
676
14.4k
            p->rtcp.auth_type, &session_keys->rtcp_auth, p->rtcp.auth_key_len,
677
14.4k
            p->rtcp.auth_tag_len);
678
14.4k
        if (stat) {
679
0
            srtp_stream_dealloc(str, NULL);
680
0
            return stat;
681
0
        }
682
683
14.4k
        session_keys->mki_id = NULL;
684
685
        /* allocate key limit structure */
686
14.4k
        session_keys->limit = (srtp_key_limit_ctx_t *)srtp_crypto_alloc(
687
14.4k
            sizeof(srtp_key_limit_ctx_t));
688
14.4k
        if (session_keys->limit == NULL) {
689
0
            srtp_stream_dealloc(str, NULL);
690
0
            return srtp_err_status_alloc_fail;
691
0
        }
692
14.4k
    }
693
694
9.96k
    if (p->enc_xtn_hdr && p->enc_xtn_hdr_count > 0) {
695
2.00k
        srtp_cipher_type_id_t enc_xtn_hdr_cipher_type;
696
2.00k
        size_t enc_xtn_hdr_cipher_key_len;
697
698
2.00k
        str->enc_xtn_hdr = (uint8_t *)srtp_crypto_alloc(
699
2.00k
            p->enc_xtn_hdr_count * sizeof(p->enc_xtn_hdr[0]));
700
2.00k
        if (!str->enc_xtn_hdr) {
701
0
            srtp_stream_dealloc(str, NULL);
702
0
            return srtp_err_status_alloc_fail;
703
0
        }
704
2.00k
        memcpy(str->enc_xtn_hdr, p->enc_xtn_hdr,
705
2.00k
               p->enc_xtn_hdr_count * sizeof(p->enc_xtn_hdr[0]));
706
2.00k
        str->enc_xtn_hdr_count = p->enc_xtn_hdr_count;
707
708
        /*
709
         * For GCM ciphers, the corresponding ICM cipher is used for header
710
         * extensions encryption.
711
         */
712
2.00k
        switch (p->rtp.cipher_type) {
713
0
        case SRTP_AES_GCM_128:
714
0
            enc_xtn_hdr_cipher_type = SRTP_AES_ICM_128;
715
0
            enc_xtn_hdr_cipher_key_len = SRTP_AES_ICM_128_KEY_LEN_WSALT;
716
0
            break;
717
0
        case SRTP_AES_GCM_256:
718
0
            enc_xtn_hdr_cipher_type = SRTP_AES_ICM_256;
719
0
            enc_xtn_hdr_cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT;
720
0
            break;
721
2.00k
        default:
722
2.00k
            enc_xtn_hdr_cipher_type = p->rtp.cipher_type;
723
2.00k
            enc_xtn_hdr_cipher_key_len = p->rtp.cipher_key_len;
724
2.00k
            break;
725
2.00k
        }
726
727
6.17k
        for (i = 0; i < str->num_master_keys; i++) {
728
4.16k
            session_keys = &str->session_keys[i];
729
730
            /* allocate cipher for extensions header encryption */
731
4.16k
            stat = srtp_crypto_kernel_alloc_cipher(
732
4.16k
                enc_xtn_hdr_cipher_type, &session_keys->rtp_xtn_hdr_cipher,
733
4.16k
                enc_xtn_hdr_cipher_key_len, 0);
734
4.16k
            if (stat) {
735
0
                srtp_stream_dealloc(str, NULL);
736
0
                return stat;
737
0
            }
738
4.16k
        }
739
7.96k
    } else {
740
18.2k
        for (i = 0; i < str->num_master_keys; i++) {
741
10.2k
            session_keys = &str->session_keys[i];
742
10.2k
            session_keys->rtp_xtn_hdr_cipher = NULL;
743
10.2k
        }
744
745
7.96k
        str->enc_xtn_hdr = NULL;
746
7.96k
        str->enc_xtn_hdr_count = 0;
747
7.96k
    }
748
749
9.96k
    str->use_cryptex = p->use_cryptex;
750
751
9.96k
    return srtp_err_status_ok;
752
9.96k
}
753
754
/*
755
 * srtp_stream_clone(stream_template, new) allocates a new stream and
756
 * initializes it using the cipher and auth of the stream_template
757
 *
758
 * the only unique data in a cloned stream is the replay database and
759
 * the SSRC
760
 */
761
762
static srtp_err_status_t srtp_stream_clone(
763
    const srtp_stream_ctx_t *stream_template,
764
    uint32_t ssrc,
765
    srtp_stream_ctx_t **str_ptr)
766
102k
{
767
102k
    srtp_err_status_t status;
768
102k
    srtp_stream_ctx_t *str;
769
102k
    srtp_session_keys_t *session_keys = NULL;
770
102k
    const srtp_session_keys_t *template_session_keys = NULL;
771
772
102k
    debug_print(mod_srtp, "cloning stream (SSRC: 0x%08x)",
773
102k
                (unsigned int)ntohl(ssrc));
774
775
    /* allocate srtp stream and set str_ptr */
776
102k
    str = (srtp_stream_ctx_t *)srtp_crypto_alloc(sizeof(srtp_stream_ctx_t));
777
102k
    if (str == NULL) {
778
0
        return srtp_err_status_alloc_fail;
779
0
    }
780
102k
    *str_ptr = str;
781
782
102k
    str->num_master_keys = stream_template->num_master_keys;
783
102k
    str->session_keys = (srtp_session_keys_t *)srtp_crypto_alloc(
784
102k
        sizeof(srtp_session_keys_t) * str->num_master_keys);
785
786
102k
    if (str->session_keys == NULL) {
787
0
        srtp_stream_dealloc(*str_ptr, stream_template);
788
0
        *str_ptr = NULL;
789
0
        return srtp_err_status_alloc_fail;
790
0
    }
791
792
205k
    for (size_t i = 0; i < stream_template->num_master_keys; i++) {
793
102k
        session_keys = &str->session_keys[i];
794
102k
        template_session_keys = &stream_template->session_keys[i];
795
796
        /* set cipher and auth pointers to those of the template */
797
102k
        session_keys->rtp_cipher = template_session_keys->rtp_cipher;
798
102k
        session_keys->rtp_auth = template_session_keys->rtp_auth;
799
102k
        session_keys->rtp_xtn_hdr_cipher =
800
102k
            template_session_keys->rtp_xtn_hdr_cipher;
801
102k
        session_keys->rtcp_cipher = template_session_keys->rtcp_cipher;
802
102k
        session_keys->rtcp_auth = template_session_keys->rtcp_auth;
803
804
102k
        if (stream_template->mki_size == 0) {
805
102k
            session_keys->mki_id = NULL;
806
102k
        } else {
807
0
            session_keys->mki_id = srtp_crypto_alloc(stream_template->mki_size);
808
809
0
            if (session_keys->mki_id == NULL) {
810
0
                srtp_stream_dealloc(*str_ptr, stream_template);
811
0
                *str_ptr = NULL;
812
0
                return srtp_err_status_init_fail;
813
0
            }
814
0
            memcpy(session_keys->mki_id, template_session_keys->mki_id,
815
0
                   stream_template->mki_size);
816
0
        }
817
        /* Copy the salt values */
818
102k
        memcpy(session_keys->salt, template_session_keys->salt,
819
102k
               SRTP_AEAD_SALT_LEN);
820
102k
        memcpy(session_keys->c_salt, template_session_keys->c_salt,
821
102k
               SRTP_AEAD_SALT_LEN);
822
823
        /* set key limit to point to that of the template */
824
102k
        status = srtp_key_limit_clone(template_session_keys->limit,
825
102k
                                      &session_keys->limit);
826
102k
        if (status) {
827
0
            srtp_stream_dealloc(*str_ptr, stream_template);
828
0
            *str_ptr = NULL;
829
0
            return status;
830
0
        }
831
102k
    }
832
833
102k
    str->use_mki = stream_template->use_mki;
834
102k
    str->mki_size = stream_template->mki_size;
835
836
    /* initialize replay databases */
837
102k
    status = srtp_rdbx_init(
838
102k
        &str->rtp_rdbx, srtp_rdbx_get_window_size(&stream_template->rtp_rdbx));
839
102k
    if (status) {
840
0
        srtp_stream_dealloc(*str_ptr, stream_template);
841
0
        *str_ptr = NULL;
842
0
        return status;
843
0
    }
844
102k
    srtp_rdb_init(&str->rtcp_rdb);
845
102k
    str->allow_repeat_tx = stream_template->allow_repeat_tx;
846
847
    /* set ssrc to that provided */
848
102k
    str->ssrc = ssrc;
849
850
    /* reset pending ROC */
851
102k
    str->pending_roc = 0;
852
853
    /* set direction and security services */
854
102k
    str->direction = stream_template->direction;
855
102k
    str->rtp_services = stream_template->rtp_services;
856
102k
    str->rtcp_services = stream_template->rtcp_services;
857
858
    /* copy information about extensions header encryption */
859
102k
    str->enc_xtn_hdr = stream_template->enc_xtn_hdr;
860
102k
    str->enc_xtn_hdr_count = stream_template->enc_xtn_hdr_count;
861
102k
    str->use_cryptex = stream_template->use_cryptex;
862
102k
    return srtp_err_status_ok;
863
102k
}
864
865
/*
866
 * key derivation functions, internal to libSRTP
867
 *
868
 * srtp_kdf_t is a key derivation context
869
 *
870
 * srtp_kdf_init(&kdf, cipher_id, k, keylen) initializes kdf to use cipher
871
 * described by cipher_id, with the master key k with length in octets keylen.
872
 *
873
 * srtp_kdf_generate(&kdf, l, kl, keylen) derives the key
874
 * corresponding to label l and puts it into kl; the length
875
 * of the key in octets is provided as keylen.  this function
876
 * should be called once for each subkey that is derived.
877
 *
878
 * srtp_kdf_clear(&kdf) zeroizes and deallocates the kdf state
879
 */
880
881
typedef enum {
882
    label_rtp_encryption = 0x00,
883
    label_rtp_msg_auth = 0x01,
884
    label_rtp_salt = 0x02,
885
    label_rtcp_encryption = 0x03,
886
    label_rtcp_msg_auth = 0x04,
887
    label_rtcp_salt = 0x05,
888
    label_rtp_header_encryption = 0x06,
889
    label_rtp_header_salt = 0x07
890
} srtp_prf_label;
891
892
28.4k
#define MAX_SRTP_KEY_LEN 256
893
894
#if defined(OPENSSL) && defined(OPENSSL_KDF)
895
#define MAX_SRTP_AESKEY_LEN 32
896
#define MAX_SRTP_SALT_LEN 14
897
898
/*
899
 * srtp_kdf_t represents a key derivation function.  The SRTP
900
 * default KDF is the only one implemented at present.
901
 */
902
typedef struct {
903
    uint8_t master_key[MAX_SRTP_AESKEY_LEN];
904
    uint8_t master_salt[MAX_SRTP_SALT_LEN];
905
    const EVP_CIPHER *evp;
906
} srtp_kdf_t;
907
908
static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf,
909
                                       const uint8_t *key,
910
                                       size_t key_len,
911
                                       size_t salt_len)
912
{
913
    memset(kdf, 0x0, sizeof(srtp_kdf_t));
914
915
    /* The NULL cipher has zero key length */
916
    if (key_len == 0) {
917
        return srtp_err_status_ok;
918
    }
919
920
    if ((key_len > MAX_SRTP_AESKEY_LEN) || (salt_len > MAX_SRTP_SALT_LEN)) {
921
        return srtp_err_status_bad_param;
922
    }
923
    switch (key_len) {
924
    case SRTP_AES_256_KEYSIZE:
925
        kdf->evp = EVP_aes_256_ctr();
926
        break;
927
    case SRTP_AES_192_KEYSIZE:
928
        kdf->evp = EVP_aes_192_ctr();
929
        break;
930
    case SRTP_AES_128_KEYSIZE:
931
        kdf->evp = EVP_aes_128_ctr();
932
        break;
933
    default:
934
        return srtp_err_status_bad_param;
935
        break;
936
    }
937
    memcpy(kdf->master_key, key, key_len);
938
    memcpy(kdf->master_salt, key + key_len, salt_len);
939
    return srtp_err_status_ok;
940
}
941
942
static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf,
943
                                           srtp_prf_label label,
944
                                           uint8_t *key,
945
                                           size_t length)
946
{
947
    int ret;
948
949
    /* The NULL cipher will not have an EVP */
950
    if (!kdf->evp) {
951
        return srtp_err_status_ok;
952
    }
953
    octet_string_set_to_zero(key, length);
954
955
    /*
956
     * Invoke the OpenSSL SRTP KDF function
957
     * This is useful if OpenSSL is in FIPS mode and FIP
958
     * compliance is required for SRTP.
959
     */
960
    ret = kdf_srtp(kdf->evp, (char *)&kdf->master_key, &kdf->master_salt, NULL,
961
                   NULL, label, key);
962
    if (ret == -1) {
963
        return (srtp_err_status_algo_fail);
964
    }
965
966
    return srtp_err_status_ok;
967
}
968
969
static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf)
970
{
971
    octet_string_set_to_zero(kdf->master_key, MAX_SRTP_AESKEY_LEN);
972
    octet_string_set_to_zero(kdf->master_salt, MAX_SRTP_SALT_LEN);
973
    kdf->evp = NULL;
974
975
    return srtp_err_status_ok;
976
}
977
978
#elif defined(WOLFSSL) && defined(WOLFSSL_KDF)
979
#define MAX_SRTP_AESKEY_LEN AES_256_KEY_SIZE
980
#define MAX_SRTP_SALT_LEN WC_SRTP_MAX_SALT
981
982
/*
983
 * srtp_kdf_t represents a key derivation function.  The SRTP
984
 * default KDF is the only one implemented at present.
985
 */
986
typedef struct {
987
    uint8_t master_key[MAX_SRTP_AESKEY_LEN];
988
    int master_key_len;
989
    uint8_t master_salt[MAX_SRTP_SALT_LEN];
990
} srtp_kdf_t;
991
992
static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf,
993
                                       const uint8_t *key,
994
                                       size_t key_len)
995
{
996
    size_t salt_len;
997
998
    memset(kdf, 0x0, sizeof(srtp_kdf_t));
999
1000
    switch (key_len) {
1001
    case SRTP_AES_ICM_256_KEY_LEN_WSALT:
1002
        kdf->master_key_len = AES_256_KEY_SIZE;
1003
        break;
1004
    case SRTP_AES_ICM_192_KEY_LEN_WSALT:
1005
        kdf->master_key_len = AES_192_KEY_SIZE;
1006
        break;
1007
    case SRTP_AES_ICM_128_KEY_LEN_WSALT:
1008
        kdf->master_key_len = AES_128_KEY_SIZE;
1009
        break;
1010
    default:
1011
        return srtp_err_status_bad_param;
1012
        break;
1013
    }
1014
1015
    memcpy(kdf->master_key, key, kdf->master_key_len);
1016
    salt_len = key_len - kdf->master_key_len;
1017
    memcpy(kdf->master_salt, key + kdf->master_key_len, salt_len);
1018
    memset(kdf->master_salt + salt_len, 0, MAX_SRTP_SALT_LEN - salt_len);
1019
1020
    return srtp_err_status_ok;
1021
}
1022
1023
static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf,
1024
                                           srtp_prf_label label,
1025
                                           uint8_t *key,
1026
                                           size_t length)
1027
{
1028
    int err;
1029
1030
    if (length == 0) {
1031
        return srtp_err_status_ok;
1032
    }
1033
    if (kdf->master_key_len == 0) {
1034
        return srtp_err_status_ok;
1035
    }
1036
    octet_string_set_to_zero(key, length);
1037
1038
    PRIVATE_KEY_UNLOCK();
1039
    err = wc_SRTP_KDF_label(kdf->master_key, kdf->master_key_len,
1040
                            kdf->master_salt, MAX_SRTP_SALT_LEN, -1, NULL,
1041
                            label, key, length);
1042
    PRIVATE_KEY_LOCK();
1043
    if (err < 0) {
1044
        debug_print(mod_srtp, "wolfSSL SRTP KDF error: %d", err);
1045
        return (srtp_err_status_algo_fail);
1046
    }
1047
1048
    return srtp_err_status_ok;
1049
}
1050
1051
static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf)
1052
{
1053
    octet_string_set_to_zero(kdf->master_key, MAX_SRTP_AESKEY_LEN);
1054
    kdf->master_key_len = 0;
1055
    octet_string_set_to_zero(kdf->master_salt, MAX_SRTP_SALT_LEN);
1056
1057
    return srtp_err_status_ok;
1058
}
1059
1060
#else  /* if OPENSSL_KDF || WOLFSSL_KDF */
1061
1062
/*
1063
 * srtp_kdf_t represents a key derivation function.  The SRTP
1064
 * default KDF is the only one implemented at present.
1065
 */
1066
typedef struct {
1067
    srtp_cipher_t *cipher; /* cipher used for key derivation  */
1068
} srtp_kdf_t;
1069
1070
static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf,
1071
                                       const uint8_t *key,
1072
                                       size_t key_len)
1073
14.2k
{
1074
14.2k
    srtp_cipher_type_id_t cipher_id;
1075
14.2k
    srtp_err_status_t stat;
1076
1077
14.2k
    switch (key_len) {
1078
691
    case SRTP_AES_ICM_256_KEY_LEN_WSALT:
1079
691
        cipher_id = SRTP_AES_ICM_256;
1080
691
        break;
1081
0
    case SRTP_AES_ICM_192_KEY_LEN_WSALT:
1082
0
        cipher_id = SRTP_AES_ICM_192;
1083
0
        break;
1084
13.5k
    case SRTP_AES_ICM_128_KEY_LEN_WSALT:
1085
13.5k
        cipher_id = SRTP_AES_ICM_128;
1086
13.5k
        break;
1087
0
    default:
1088
0
        return srtp_err_status_bad_param;
1089
0
        break;
1090
14.2k
    }
1091
1092
14.2k
    stat = srtp_crypto_kernel_alloc_cipher(cipher_id, &kdf->cipher, key_len, 0);
1093
14.2k
    if (stat) {
1094
0
        return stat;
1095
0
    }
1096
1097
14.2k
    stat = srtp_cipher_init(kdf->cipher, key);
1098
14.2k
    if (stat) {
1099
0
        srtp_cipher_dealloc(kdf->cipher);
1100
0
        return stat;
1101
0
    }
1102
14.2k
    return srtp_err_status_ok;
1103
14.2k
}
1104
1105
static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf,
1106
                                           srtp_prf_label label,
1107
                                           uint8_t *key,
1108
                                           size_t length)
1109
67.7k
{
1110
67.7k
    srtp_err_status_t status;
1111
67.7k
    v128_t nonce;
1112
1113
    /* set eigth octet of nonce to <label>, set the rest of it to zero */
1114
67.7k
    v128_set_to_zero(&nonce);
1115
67.7k
    nonce.v8[7] = label;
1116
1117
67.7k
    status = srtp_cipher_set_iv(kdf->cipher, (uint8_t *)&nonce,
1118
67.7k
                                srtp_direction_encrypt);
1119
67.7k
    if (status) {
1120
0
        return status;
1121
0
    }
1122
1123
    /* generate keystream output */
1124
67.7k
    octet_string_set_to_zero(key, length);
1125
67.7k
    status = srtp_cipher_encrypt(kdf->cipher, key, length, key, &length);
1126
67.7k
    if (status) {
1127
0
        return status;
1128
0
    }
1129
1130
67.7k
    return srtp_err_status_ok;
1131
67.7k
}
1132
1133
static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf)
1134
14.2k
{
1135
14.2k
    srtp_err_status_t status;
1136
14.2k
    status = srtp_cipher_dealloc(kdf->cipher);
1137
14.2k
    if (status) {
1138
0
        return status;
1139
0
    }
1140
14.2k
    kdf->cipher = NULL;
1141
14.2k
    return srtp_err_status_ok;
1142
14.2k
}
1143
#endif /* else OPENSSL_KDF || WOLFSSL_KDF */
1144
1145
/*
1146
 *  end of key derivation functions
1147
 */
1148
1149
/* Get the base key length corresponding to a given combined key+salt
1150
 * length for the given cipher.
1151
 * TODO: key and salt lengths should be separate fields in the policy.  */
1152
static inline size_t base_key_length(const srtp_cipher_type_t *cipher,
1153
                                     size_t key_length)
1154
28.4k
{
1155
28.4k
    switch (cipher->id) {
1156
23.9k
    case SRTP_NULL_CIPHER:
1157
23.9k
        return 0;
1158
3.14k
    case SRTP_AES_ICM_128:
1159
3.14k
    case SRTP_AES_ICM_192:
1160
4.53k
    case SRTP_AES_ICM_256:
1161
        /* The legacy modes are derived from
1162
         * the configured key length on the policy */
1163
4.53k
        return key_length - SRTP_SALT_LEN;
1164
0
    case SRTP_AES_GCM_128:
1165
0
        return key_length - SRTP_AEAD_SALT_LEN;
1166
0
    case SRTP_AES_GCM_256:
1167
0
        return key_length - SRTP_AEAD_SALT_LEN;
1168
0
    default:
1169
0
        return key_length;
1170
28.4k
    }
1171
28.4k
}
1172
1173
/* Get the key length that the application should supply for the given cipher */
1174
static inline size_t full_key_length(const srtp_cipher_type_t *cipher)
1175
28.4k
{
1176
28.4k
    switch (cipher->id) {
1177
23.9k
    case SRTP_NULL_CIPHER:
1178
23.9k
        return 0;
1179
3.14k
    case SRTP_AES_ICM_128:
1180
3.14k
        return SRTP_AES_ICM_128_KEY_LEN_WSALT;
1181
0
    case SRTP_AES_ICM_192:
1182
0
        return SRTP_AES_ICM_192_KEY_LEN_WSALT;
1183
1.38k
    case SRTP_AES_ICM_256:
1184
1.38k
        return SRTP_AES_ICM_256_KEY_LEN_WSALT;
1185
0
    case SRTP_AES_GCM_128:
1186
0
        return SRTP_AES_GCM_128_KEY_LEN_WSALT;
1187
0
    case SRTP_AES_GCM_256:
1188
0
        return SRTP_AES_GCM_256_KEY_LEN_WSALT;
1189
0
    default:
1190
0
        return 0;
1191
28.4k
    }
1192
28.4k
}
1193
1194
/* Get the key length that the application should supply for the given auth */
1195
static inline size_t full_auth_key_length(const srtp_auth_type_t *auth)
1196
28.4k
{
1197
28.4k
    switch (auth->id) {
1198
23.8k
    case SRTP_NULL_AUTH:
1199
23.8k
        return 0;
1200
4.57k
    case SRTP_HMAC_SHA1:
1201
4.57k
        return SRTP_AES_ICM_128_KEY_LEN_WSALT;
1202
0
    default:
1203
0
        return 0;
1204
28.4k
    }
1205
28.4k
}
1206
1207
srtp_err_status_t srtp_get_session_keys(srtp_stream_ctx_t *stream,
1208
                                        size_t mki_index,
1209
                                        srtp_session_keys_t **session_keys)
1210
134k
{
1211
134k
    if (stream->use_mki) {
1212
0
        if (mki_index >= stream->num_master_keys) {
1213
0
            return srtp_err_status_bad_mki;
1214
0
        }
1215
0
        *session_keys = &stream->session_keys[mki_index];
1216
0
        return srtp_err_status_ok;
1217
0
    }
1218
1219
134k
    *session_keys = &stream->session_keys[0];
1220
134k
    return srtp_err_status_ok;
1221
134k
}
1222
1223
void srtp_inject_mki(uint8_t *mki_tag_location,
1224
                     const srtp_session_keys_t *session_keys,
1225
                     size_t mki_size)
1226
0
{
1227
0
    if (mki_size > 0) {
1228
        // Write MKI into memory
1229
0
        memcpy(mki_tag_location, session_keys->mki_id, mki_size);
1230
0
    }
1231
0
}
1232
1233
srtp_err_status_t srtp_stream_init_keys(srtp_session_keys_t *session_keys,
1234
                                        const srtp_master_key_t *master_key,
1235
                                        size_t mki_size)
1236
14.2k
{
1237
14.2k
    srtp_err_status_t stat;
1238
14.2k
    srtp_kdf_t kdf;
1239
14.2k
    uint8_t tmp_key[MAX_SRTP_KEY_LEN];
1240
14.2k
    size_t input_keylen, full_keylen;
1241
14.2k
    size_t kdf_keylen = 30, rtp_keylen, rtcp_keylen;
1242
14.2k
    size_t rtp_base_key_len, rtp_salt_len;
1243
14.2k
    size_t rtcp_base_key_len, rtcp_salt_len;
1244
1245
    /* If RTP or RTCP have a key length > AES-128, assume matching kdf. */
1246
    /* TODO: kdf algorithm, master key length, and master salt length should
1247
     * be part of srtp_policy_t.
1248
     */
1249
1250
    /* initialize key limit to maximum value */
1251
14.2k
    srtp_key_limit_set(session_keys->limit, 0xffffffffffffLL);
1252
1253
14.2k
    if (mki_size != 0) {
1254
0
        if (master_key->mki_id == NULL) {
1255
0
            return srtp_err_status_bad_param;
1256
0
        }
1257
0
        session_keys->mki_id = srtp_crypto_alloc(mki_size);
1258
1259
0
        if (session_keys->mki_id == NULL) {
1260
0
            return srtp_err_status_init_fail;
1261
0
        }
1262
0
        memcpy(session_keys->mki_id, master_key->mki_id, mki_size);
1263
14.2k
    } else {
1264
14.2k
        session_keys->mki_id = NULL;
1265
14.2k
    }
1266
1267
    /* Find the maximum key length */
1268
14.2k
    input_keylen = full_key_length(session_keys->rtp_cipher->type);
1269
14.2k
    full_keylen = full_auth_key_length(session_keys->rtp_auth->type);
1270
14.2k
    if (full_keylen > input_keylen) {
1271
370
        input_keylen = full_keylen;
1272
370
    }
1273
14.2k
    full_keylen = full_key_length(session_keys->rtcp_cipher->type);
1274
14.2k
    if (full_keylen > input_keylen) {
1275
0
        input_keylen = full_keylen;
1276
0
    }
1277
14.2k
    full_keylen = full_auth_key_length(session_keys->rtcp_auth->type);
1278
14.2k
    if (full_keylen > input_keylen) {
1279
0
        input_keylen = full_keylen;
1280
0
    }
1281
1282
14.2k
    rtp_keylen = srtp_cipher_get_key_length(session_keys->rtp_cipher);
1283
14.2k
    rtcp_keylen = srtp_cipher_get_key_length(session_keys->rtcp_cipher);
1284
14.2k
    rtp_base_key_len =
1285
14.2k
        base_key_length(session_keys->rtp_cipher->type, rtp_keylen);
1286
14.2k
    rtp_salt_len = rtp_keylen - rtp_base_key_len;
1287
1288
    /*
1289
     * We assume that the `key` buffer provided by the caller has a length
1290
     * equal to the greater of `rtp_keylen` and `rtcp_keylen`.  Since we are
1291
     * about to read `input_keylen` bytes from it, we need to check that we will
1292
     * not overrun.
1293
     */
1294
14.2k
    if ((rtp_keylen < input_keylen) && (rtcp_keylen < input_keylen)) {
1295
0
        return srtp_err_status_bad_param;
1296
0
    }
1297
1298
14.2k
    if (rtp_keylen > kdf_keylen) {
1299
691
        kdf_keylen = rtp_keylen;
1300
691
    }
1301
1302
14.2k
    if (rtcp_keylen > kdf_keylen) {
1303
0
        kdf_keylen = rtcp_keylen;
1304
0
    }
1305
1306
14.2k
    if (input_keylen > kdf_keylen) {
1307
0
        kdf_keylen = input_keylen;
1308
0
    }
1309
1310
14.2k
    if (kdf_keylen == SRTP_AES_GCM_128_KEY_LEN_WSALT ||
1311
14.2k
        kdf_keylen == SRTP_AES_GCM_256_KEY_LEN_WSALT) {
1312
0
        kdf_keylen += 2; /* AES-CTR mode is always used for KDF */
1313
0
    }
1314
1315
14.2k
    debug_print(mod_srtp, "input key len: %zu", input_keylen);
1316
14.2k
    debug_print(mod_srtp, "srtp key len: %zu", rtp_keylen);
1317
14.2k
    debug_print(mod_srtp, "srtcp key len: %zu", rtcp_keylen);
1318
14.2k
    debug_print(mod_srtp, "base key len: %zu", rtp_base_key_len);
1319
14.2k
    debug_print(mod_srtp, "kdf key len: %zu", kdf_keylen);
1320
14.2k
    debug_print(mod_srtp, "rtp salt len: %zu", rtp_salt_len);
1321
1322
    /*
1323
     * Make sure the key given to us is 'zero' appended.  GCM
1324
     * mode uses a shorter master SALT (96 bits), but still relies on
1325
     * the legacy CTR mode KDF, which uses a 112 bit master SALT.
1326
     */
1327
14.2k
    memset(tmp_key, 0x0, MAX_SRTP_KEY_LEN);
1328
14.2k
    memcpy(tmp_key, master_key->key, input_keylen);
1329
1330
/* initialize KDF state     */
1331
#if defined(OPENSSL) && defined(OPENSSL_KDF)
1332
    stat = srtp_kdf_init(&kdf, tmp_key, rtp_base_key_len, rtp_salt_len);
1333
#else
1334
14.2k
    stat = srtp_kdf_init(&kdf, tmp_key, kdf_keylen);
1335
14.2k
#endif
1336
14.2k
    if (stat) {
1337
        /* zeroize temp buffer */
1338
0
        octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1339
0
        return srtp_err_status_init_fail;
1340
0
    }
1341
1342
    /* generate encryption key  */
1343
14.2k
    stat = srtp_kdf_generate(&kdf, label_rtp_encryption, tmp_key,
1344
14.2k
                             rtp_base_key_len);
1345
14.2k
    if (stat) {
1346
        /* zeroize temp buffer */
1347
0
        octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1348
0
        return srtp_err_status_init_fail;
1349
0
    }
1350
14.2k
    debug_print(mod_srtp, "cipher key: %s",
1351
14.2k
                srtp_octet_string_hex_string(tmp_key, rtp_base_key_len));
1352
1353
    /*
1354
     * if the cipher in the srtp context uses a salt, then we need
1355
     * to generate the salt value
1356
     */
1357
14.2k
    if (rtp_salt_len > 0) {
1358
2.63k
        debug_print0(mod_srtp, "found rtp_salt_len > 0, generating salt");
1359
1360
        /* generate encryption salt, put after encryption key */
1361
2.63k
        stat = srtp_kdf_generate(&kdf, label_rtp_salt,
1362
2.63k
                                 tmp_key + rtp_base_key_len, rtp_salt_len);
1363
2.63k
        if (stat) {
1364
            /* zeroize temp buffer */
1365
0
            octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1366
0
            return srtp_err_status_init_fail;
1367
0
        }
1368
2.63k
        memcpy(session_keys->salt, tmp_key + rtp_base_key_len,
1369
2.63k
               SRTP_AEAD_SALT_LEN);
1370
2.63k
    }
1371
14.2k
    if (rtp_salt_len > 0) {
1372
2.63k
        debug_print(mod_srtp, "cipher salt: %s",
1373
2.63k
                    srtp_octet_string_hex_string(tmp_key + rtp_base_key_len,
1374
2.63k
                                                 rtp_salt_len));
1375
2.63k
    }
1376
1377
    /* initialize cipher */
1378
14.2k
    stat = srtp_cipher_init(session_keys->rtp_cipher, tmp_key);
1379
14.2k
    if (stat) {
1380
        /* zeroize temp buffer */
1381
0
        octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1382
0
        return srtp_err_status_init_fail;
1383
0
    }
1384
1385
14.2k
    if (session_keys->rtp_xtn_hdr_cipher) {
1386
        /* generate extensions header encryption key  */
1387
4.14k
        size_t rtp_xtn_hdr_keylen;
1388
4.14k
        size_t rtp_xtn_hdr_base_key_len;
1389
4.14k
        size_t rtp_xtn_hdr_salt_len;
1390
4.14k
        srtp_kdf_t tmp_kdf;
1391
4.14k
        srtp_kdf_t *xtn_hdr_kdf;
1392
1393
4.14k
        if (session_keys->rtp_xtn_hdr_cipher->type !=
1394
4.14k
            session_keys->rtp_cipher->type) {
1395
            /*
1396
             * With GCM ciphers, the header extensions are still encrypted using
1397
             * the corresponding ICM cipher.
1398
             * See https://tools.ietf.org/html/rfc7714#section-8.3
1399
             */
1400
0
            uint8_t tmp_xtn_hdr_key[MAX_SRTP_KEY_LEN];
1401
0
            rtp_xtn_hdr_keylen =
1402
0
                srtp_cipher_get_key_length(session_keys->rtp_xtn_hdr_cipher);
1403
0
            rtp_xtn_hdr_base_key_len = base_key_length(
1404
0
                session_keys->rtp_xtn_hdr_cipher->type, rtp_xtn_hdr_keylen);
1405
0
            rtp_xtn_hdr_salt_len =
1406
0
                rtp_xtn_hdr_keylen - rtp_xtn_hdr_base_key_len;
1407
0
            if (rtp_xtn_hdr_salt_len > rtp_salt_len) {
1408
0
                switch (session_keys->rtp_cipher->type->id) {
1409
0
                case SRTP_AES_GCM_128:
1410
0
                case SRTP_AES_GCM_256:
1411
                    /*
1412
                     * The shorter GCM salt is padded to the required ICM salt
1413
                     * length.
1414
                     */
1415
0
                    rtp_xtn_hdr_salt_len = rtp_salt_len;
1416
0
                    break;
1417
0
                default:
1418
                    /* zeroize temp buffer */
1419
0
                    octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1420
0
                    return srtp_err_status_bad_param;
1421
0
                }
1422
0
            }
1423
0
            memset(tmp_xtn_hdr_key, 0x0, MAX_SRTP_KEY_LEN);
1424
0
            memcpy(tmp_xtn_hdr_key, master_key->key,
1425
0
                   (rtp_xtn_hdr_base_key_len + rtp_xtn_hdr_salt_len));
1426
0
            xtn_hdr_kdf = &tmp_kdf;
1427
1428
/* initialize KDF state */
1429
#if defined(OPENSSL) && defined(OPENSSL_KDF)
1430
            stat =
1431
                srtp_kdf_init(xtn_hdr_kdf, tmp_xtn_hdr_key,
1432
                              rtp_xtn_hdr_base_key_len, rtp_xtn_hdr_salt_len);
1433
#else
1434
0
            stat = srtp_kdf_init(xtn_hdr_kdf, tmp_xtn_hdr_key, kdf_keylen);
1435
0
#endif
1436
0
            octet_string_set_to_zero(tmp_xtn_hdr_key, MAX_SRTP_KEY_LEN);
1437
0
            if (stat) {
1438
                /* zeroize temp buffer */
1439
0
                octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1440
0
                return srtp_err_status_init_fail;
1441
0
            }
1442
4.14k
        } else {
1443
            /* Reuse main KDF. */
1444
4.14k
            rtp_xtn_hdr_keylen = rtp_keylen;
1445
4.14k
            rtp_xtn_hdr_base_key_len = rtp_base_key_len;
1446
4.14k
            rtp_xtn_hdr_salt_len = rtp_salt_len;
1447
4.14k
            xtn_hdr_kdf = &kdf;
1448
4.14k
        }
1449
1450
4.14k
        stat = srtp_kdf_generate(xtn_hdr_kdf, label_rtp_header_encryption,
1451
4.14k
                                 tmp_key, rtp_xtn_hdr_base_key_len);
1452
4.14k
        if (stat) {
1453
            /* zeroize temp buffer */
1454
0
            octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1455
0
            return srtp_err_status_init_fail;
1456
0
        }
1457
4.14k
        debug_print(
1458
4.14k
            mod_srtp, "extensions cipher key: %s",
1459
4.14k
            srtp_octet_string_hex_string(tmp_key, rtp_xtn_hdr_base_key_len));
1460
1461
        /*
1462
         * if the cipher in the srtp context uses a salt, then we need
1463
         * to generate the salt value
1464
         */
1465
4.14k
        if (rtp_xtn_hdr_salt_len > 0) {
1466
1.43k
            debug_print0(mod_srtp,
1467
1.43k
                         "found rtp_xtn_hdr_salt_len > 0, generating salt");
1468
1469
            /* generate encryption salt, put after encryption key */
1470
1.43k
            stat = srtp_kdf_generate(xtn_hdr_kdf, label_rtp_header_salt,
1471
1.43k
                                     tmp_key + rtp_xtn_hdr_base_key_len,
1472
1.43k
                                     rtp_xtn_hdr_salt_len);
1473
1.43k
            if (stat) {
1474
                /* zeroize temp buffer */
1475
0
                octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1476
0
                return srtp_err_status_init_fail;
1477
0
            }
1478
1.43k
        }
1479
4.14k
        if (rtp_xtn_hdr_salt_len > 0) {
1480
1.43k
            debug_print(
1481
1.43k
                mod_srtp, "extensions cipher salt: %s",
1482
1.43k
                srtp_octet_string_hex_string(tmp_key + rtp_xtn_hdr_base_key_len,
1483
1.43k
                                             rtp_xtn_hdr_salt_len));
1484
1.43k
        }
1485
1486
        /* initialize extensions header cipher */
1487
4.14k
        stat = srtp_cipher_init(session_keys->rtp_xtn_hdr_cipher, tmp_key);
1488
4.14k
        if (stat) {
1489
            /* zeroize temp buffer */
1490
0
            octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1491
0
            return srtp_err_status_init_fail;
1492
0
        }
1493
1494
4.14k
        if (xtn_hdr_kdf != &kdf) {
1495
            /* release memory for custom header extension encryption kdf */
1496
0
            stat = srtp_kdf_clear(xtn_hdr_kdf);
1497
0
            if (stat) {
1498
                /* zeroize temp buffer */
1499
0
                octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1500
0
                return srtp_err_status_init_fail;
1501
0
            }
1502
0
        }
1503
4.14k
    }
1504
1505
    /* generate authentication key */
1506
14.2k
    stat = srtp_kdf_generate(&kdf, label_rtp_msg_auth, tmp_key,
1507
14.2k
                             srtp_auth_get_key_length(session_keys->rtp_auth));
1508
14.2k
    if (stat) {
1509
        /* zeroize temp buffer */
1510
0
        octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1511
0
        return srtp_err_status_init_fail;
1512
0
    }
1513
14.2k
    debug_print(mod_srtp, "auth key:   %s",
1514
14.2k
                srtp_octet_string_hex_string(
1515
14.2k
                    tmp_key, srtp_auth_get_key_length(session_keys->rtp_auth)));
1516
1517
    /* initialize auth function */
1518
14.2k
    stat = srtp_auth_init(session_keys->rtp_auth, tmp_key);
1519
14.2k
    if (stat) {
1520
        /* zeroize temp buffer */
1521
0
        octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1522
0
        return srtp_err_status_init_fail;
1523
0
    }
1524
1525
    /*
1526
     * ...now initialize SRTCP keys
1527
     */
1528
1529
14.2k
    rtcp_base_key_len =
1530
14.2k
        base_key_length(session_keys->rtcp_cipher->type, rtcp_keylen);
1531
14.2k
    rtcp_salt_len = rtcp_keylen - rtcp_base_key_len;
1532
14.2k
    debug_print(mod_srtp, "rtcp salt len: %zu", rtcp_salt_len);
1533
1534
    /* generate encryption key  */
1535
14.2k
    stat = srtp_kdf_generate(&kdf, label_rtcp_encryption, tmp_key,
1536
14.2k
                             rtcp_base_key_len);
1537
14.2k
    if (stat) {
1538
        /* zeroize temp buffer */
1539
0
        octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1540
0
        return srtp_err_status_init_fail;
1541
0
    }
1542
1543
    /*
1544
     * if the cipher in the srtp context uses a salt, then we need
1545
     * to generate the salt value
1546
     */
1547
14.2k
    if (rtcp_salt_len > 0) {
1548
2.63k
        debug_print0(mod_srtp, "found rtcp_salt_len > 0, generating rtcp salt");
1549
1550
        /* generate encryption salt, put after encryption key */
1551
2.63k
        stat = srtp_kdf_generate(&kdf, label_rtcp_salt,
1552
2.63k
                                 tmp_key + rtcp_base_key_len, rtcp_salt_len);
1553
2.63k
        if (stat) {
1554
            /* zeroize temp buffer */
1555
0
            octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1556
0
            return srtp_err_status_init_fail;
1557
0
        }
1558
2.63k
        memcpy(session_keys->c_salt, tmp_key + rtcp_base_key_len,
1559
2.63k
               SRTP_AEAD_SALT_LEN);
1560
2.63k
    }
1561
14.2k
    debug_print(mod_srtp, "rtcp cipher key: %s",
1562
14.2k
                srtp_octet_string_hex_string(tmp_key, rtcp_base_key_len));
1563
14.2k
    if (rtcp_salt_len > 0) {
1564
2.63k
        debug_print(mod_srtp, "rtcp cipher salt: %s",
1565
2.63k
                    srtp_octet_string_hex_string(tmp_key + rtcp_base_key_len,
1566
2.63k
                                                 rtcp_salt_len));
1567
2.63k
    }
1568
1569
    /* initialize cipher */
1570
14.2k
    stat = srtp_cipher_init(session_keys->rtcp_cipher, tmp_key);
1571
14.2k
    if (stat) {
1572
        /* zeroize temp buffer */
1573
0
        octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1574
0
        return srtp_err_status_init_fail;
1575
0
    }
1576
1577
    /* generate authentication key */
1578
14.2k
    stat = srtp_kdf_generate(&kdf, label_rtcp_msg_auth, tmp_key,
1579
14.2k
                             srtp_auth_get_key_length(session_keys->rtcp_auth));
1580
14.2k
    if (stat) {
1581
        /* zeroize temp buffer */
1582
0
        octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1583
0
        return srtp_err_status_init_fail;
1584
0
    }
1585
1586
14.2k
    debug_print(
1587
14.2k
        mod_srtp, "rtcp auth key:   %s",
1588
14.2k
        srtp_octet_string_hex_string(
1589
14.2k
            tmp_key, srtp_auth_get_key_length(session_keys->rtcp_auth)));
1590
1591
    /* initialize auth function */
1592
14.2k
    stat = srtp_auth_init(session_keys->rtcp_auth, tmp_key);
1593
14.2k
    if (stat) {
1594
        /* zeroize temp buffer */
1595
0
        octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1596
0
        return srtp_err_status_init_fail;
1597
0
    }
1598
1599
    /* clear memory then return */
1600
14.2k
    stat = srtp_kdf_clear(&kdf);
1601
14.2k
    octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
1602
14.2k
    if (stat) {
1603
0
        return srtp_err_status_init_fail;
1604
0
    }
1605
1606
14.2k
    return srtp_err_status_ok;
1607
14.2k
}
1608
1609
srtp_err_status_t srtp_stream_init_all_master_keys(srtp_stream_ctx_t *srtp,
1610
                                                   const srtp_policy_t *p)
1611
9.75k
{
1612
9.75k
    srtp_err_status_t status = srtp_err_status_ok;
1613
9.75k
    if (p->key != NULL) {
1614
8.86k
        if (p->use_mki) {
1615
0
            return srtp_err_status_bad_param;
1616
0
        }
1617
8.86k
        srtp_master_key_t single_master_key;
1618
8.86k
        srtp->num_master_keys = 1;
1619
8.86k
        srtp->use_mki = false;
1620
8.86k
        srtp->mki_size = 0;
1621
8.86k
        single_master_key.key = p->key;
1622
8.86k
        single_master_key.mki_id = NULL;
1623
8.86k
        status = srtp_stream_init_keys(&srtp->session_keys[0],
1624
8.86k
                                       &single_master_key, 0);
1625
8.86k
    } else {
1626
895
        if (p->num_master_keys > SRTP_MAX_NUM_MASTER_KEYS) {
1627
0
            return srtp_err_status_bad_param;
1628
0
        }
1629
895
        if (p->use_mki && p->mki_size == 0) {
1630
0
            return srtp_err_status_bad_param;
1631
0
        }
1632
1633
895
        srtp->num_master_keys = p->num_master_keys;
1634
895
        srtp->use_mki = p->use_mki;
1635
895
        srtp->mki_size = p->mki_size;
1636
1637
6.26k
        for (size_t i = 0; i < srtp->num_master_keys; i++) {
1638
5.36k
            status = srtp_stream_init_keys(&srtp->session_keys[i], p->keys[i],
1639
5.36k
                                           srtp->mki_size);
1640
5.36k
            if (status) {
1641
0
                return status;
1642
0
            }
1643
5.36k
        }
1644
895
    }
1645
1646
9.75k
    return status;
1647
9.75k
}
1648
1649
static srtp_err_status_t srtp_stream_init(srtp_stream_ctx_t *srtp,
1650
                                          const srtp_policy_t *p)
1651
9.96k
{
1652
9.96k
    srtp_err_status_t err;
1653
1654
9.96k
    err = srtp_valid_policy(p);
1655
9.96k
    if (err != srtp_err_status_ok) {
1656
0
        return err;
1657
0
    }
1658
1659
9.96k
    debug_print(mod_srtp, "initializing stream (SSRC: 0x%08x)",
1660
9.96k
                (unsigned int)p->ssrc.value);
1661
1662
    /* initialize replay database */
1663
    /*
1664
     * window size MUST be at least 64.  MAY be larger.  Values more than
1665
     * 2^15 aren't meaningful due to how extended sequence numbers are
1666
     * calculated.
1667
     * Let a window size of 0 imply the default value.
1668
     */
1669
1670
9.96k
    if (p->window_size != 0 &&
1671
8.77k
        (p->window_size < 64 || p->window_size >= 0x8000))
1672
211
        return srtp_err_status_bad_param;
1673
1674
9.75k
    if (p->window_size != 0) {
1675
8.56k
        err = srtp_rdbx_init(&srtp->rtp_rdbx, p->window_size);
1676
8.56k
    } else {
1677
1.19k
        err = srtp_rdbx_init(&srtp->rtp_rdbx, 128);
1678
1.19k
    }
1679
9.75k
    if (err) {
1680
0
        return err;
1681
0
    }
1682
1683
    /* set the SSRC value */
1684
9.75k
    srtp->ssrc = htonl(p->ssrc.value);
1685
1686
    /* reset pending ROC */
1687
9.75k
    srtp->pending_roc = 0;
1688
1689
    /* set the security service flags */
1690
9.75k
    srtp->rtp_services = p->rtp.sec_serv;
1691
9.75k
    srtp->rtcp_services = p->rtcp.sec_serv;
1692
1693
    /*
1694
     * set direction to unknown - this flag gets checked in srtp_protect(),
1695
     * srtp_unprotect(), srtp_protect_rtcp(), and srtp_unprotect_rtcp(), and
1696
     * gets set appropriately if it is set to unknown.
1697
     */
1698
9.75k
    srtp->direction = dir_unknown;
1699
1700
    /* initialize SRTCP replay database */
1701
9.75k
    srtp_rdb_init(&srtp->rtcp_rdb);
1702
1703
    /* initialize allow_repeat_tx */
1704
9.75k
    srtp->allow_repeat_tx = p->allow_repeat_tx;
1705
1706
    /* DAM - no RTCP key limit at present */
1707
1708
    /* initialize keys */
1709
9.75k
    err = srtp_stream_init_all_master_keys(srtp, p);
1710
9.75k
    if (err) {
1711
0
        srtp_rdbx_dealloc(&srtp->rtp_rdbx);
1712
0
        return err;
1713
0
    }
1714
1715
9.75k
    return srtp_err_status_ok;
1716
9.75k
}
1717
1718
/*
1719
 * srtp_event_reporter is an event handler function that merely
1720
 * reports the events that are reported by the callbacks
1721
 */
1722
1723
void srtp_event_reporter(srtp_event_data_t *data)
1724
0
{
1725
0
    srtp_err_report(srtp_err_level_warning,
1726
0
                    "srtp: in stream 0x%x: ", (unsigned int)data->ssrc);
1727
1728
0
    switch (data->event) {
1729
0
    case event_ssrc_collision:
1730
0
        srtp_err_report(srtp_err_level_warning, "\tSSRC collision\n");
1731
0
        break;
1732
0
    case event_key_soft_limit:
1733
0
        srtp_err_report(srtp_err_level_warning,
1734
0
                        "\tkey usage soft limit reached\n");
1735
0
        break;
1736
0
    case event_key_hard_limit:
1737
0
        srtp_err_report(srtp_err_level_warning,
1738
0
                        "\tkey usage hard limit reached\n");
1739
0
        break;
1740
0
    case event_packet_index_limit:
1741
0
        srtp_err_report(srtp_err_level_warning,
1742
0
                        "\tpacket index limit reached\n");
1743
0
        break;
1744
0
    default:
1745
0
        srtp_err_report(srtp_err_level_warning,
1746
0
                        "\tunknown event reported to handler\n");
1747
0
    }
1748
0
}
1749
1750
/*
1751
 * srtp_event_handler is a global variable holding a pointer to the
1752
 * event handler function; this function is called for any unexpected
1753
 * event that needs to be handled out of the SRTP data path.  see
1754
 * srtp_event_t in srtp.h for more info
1755
 *
1756
 * it is okay to set srtp_event_handler to NULL, but we set
1757
 * it to the srtp_event_reporter.
1758
 */
1759
1760
static srtp_event_handler_func_t *srtp_event_handler = srtp_event_reporter;
1761
1762
srtp_err_status_t srtp_install_event_handler(srtp_event_handler_func_t func)
1763
2
{
1764
    /*
1765
     * note that we accept NULL arguments intentionally - calling this
1766
     * function with a NULL arguments removes an event handler that's
1767
     * been previously installed
1768
     */
1769
1770
    /* set global event handling function */
1771
2
    srtp_event_handler = func;
1772
2
    return srtp_err_status_ok;
1773
2
}
1774
1775
/*
1776
 * Check if the given extension header id is / should be encrypted.
1777
 * Returns true if yes, otherwise false.
1778
 */
1779
static bool srtp_protect_extension_header(srtp_stream_ctx_t *stream, uint8_t id)
1780
4.65k
{
1781
4.65k
    uint8_t *enc_xtn_hdr = stream->enc_xtn_hdr;
1782
4.65k
    size_t count = stream->enc_xtn_hdr_count;
1783
1784
4.65k
    if (!enc_xtn_hdr || count <= 0) {
1785
0
        return false;
1786
0
    }
1787
1788
9.22k
    while (count > 0) {
1789
6.41k
        if (*enc_xtn_hdr == id) {
1790
1.84k
            return true;
1791
1.84k
        }
1792
1793
4.56k
        enc_xtn_hdr++;
1794
4.56k
        count--;
1795
4.56k
    }
1796
2.81k
    return false;
1797
4.65k
}
1798
1799
/*
1800
 * extensions header encryption RFC 6904
1801
 */
1802
static srtp_err_status_t srtp_process_header_encryption(
1803
    srtp_stream_ctx_t *stream,
1804
    srtp_hdr_xtnd_t *xtn_hdr,
1805
    srtp_session_keys_t *session_keys)
1806
1.45k
{
1807
1.45k
    srtp_err_status_t status;
1808
1.45k
    uint8_t keystream[257]; /* Maximum 2 bytes header + 255 bytes data. */
1809
1.45k
    size_t keystream_pos;
1810
1.45k
    uint8_t *xtn_hdr_data = ((uint8_t *)xtn_hdr) + octets_in_rtp_xtn_hdr;
1811
1.45k
    uint8_t *xtn_hdr_end =
1812
1.45k
        xtn_hdr_data + (ntohs(xtn_hdr->length) * sizeof(uint32_t));
1813
1814
1.45k
    if (ntohs(xtn_hdr->profile_specific) == xtn_hdr_one_byte_profile) {
1815
        /* RFC 5285, section 4.2. One-Byte Header */
1816
3.14k
        while (xtn_hdr_data < xtn_hdr_end) {
1817
2.82k
            uint8_t xid = (*xtn_hdr_data & 0xf0) >> 4;
1818
2.82k
            size_t xlen = (*xtn_hdr_data & 0x0f) + 1;
1819
2.82k
            size_t xlen_with_header = 1 + xlen;
1820
2.82k
            xtn_hdr_data++;
1821
1822
2.82k
            if (xtn_hdr_data + xlen > xtn_hdr_end) {
1823
24
                return srtp_err_status_parse_err;
1824
24
            }
1825
1826
2.80k
            if (xid == 15) {
1827
                /* found header 15, stop further processing */
1828
200
                break;
1829
200
            }
1830
1831
2.60k
            status = srtp_cipher_output(session_keys->rtp_xtn_hdr_cipher,
1832
2.60k
                                        keystream, &xlen_with_header);
1833
2.60k
            if (status) {
1834
0
                return srtp_err_status_cipher_fail;
1835
0
            }
1836
1837
2.60k
            if (srtp_protect_extension_header(stream, xid)) {
1838
1.51k
                keystream_pos = 1;
1839
4.53k
                while (xlen > 0) {
1840
3.02k
                    *xtn_hdr_data ^= keystream[keystream_pos++];
1841
3.02k
                    xtn_hdr_data++;
1842
3.02k
                    xlen--;
1843
3.02k
                }
1844
1.51k
            } else {
1845
1.08k
                xtn_hdr_data += xlen;
1846
1.08k
            }
1847
1848
            /* skip padding bytes */
1849
3.28k
            while (xtn_hdr_data < xtn_hdr_end && *xtn_hdr_data == 0) {
1850
685
                xtn_hdr_data++;
1851
685
            }
1852
2.60k
        }
1853
907
    } else if ((ntohs(xtn_hdr->profile_specific) & 0xfff0) ==
1854
907
               xtn_hdr_two_byte_profile) {
1855
        /* RFC 5285, section 4.3. Two-Byte Header */
1856
4.82k
        while (xtn_hdr_data + 1 < xtn_hdr_end) {
1857
3.96k
            uint8_t xid = *xtn_hdr_data;
1858
3.96k
            size_t xlen = *(xtn_hdr_data + 1);
1859
3.96k
            size_t xlen_with_header = 2 + xlen;
1860
3.96k
            xtn_hdr_data += 2;
1861
1862
3.96k
            if (xtn_hdr_data + xlen > xtn_hdr_end) {
1863
12
                return srtp_err_status_parse_err;
1864
12
            }
1865
1866
3.95k
            status = srtp_cipher_output(session_keys->rtp_xtn_hdr_cipher,
1867
3.95k
                                        keystream, &xlen_with_header);
1868
3.95k
            if (status) {
1869
0
                return srtp_err_status_cipher_fail;
1870
0
            }
1871
1872
3.95k
            if (xlen > 0 && srtp_protect_extension_header(stream, xid)) {
1873
334
                keystream_pos = 2;
1874
3.58k
                while (xlen > 0) {
1875
3.25k
                    *xtn_hdr_data ^= keystream[keystream_pos++];
1876
3.25k
                    xtn_hdr_data++;
1877
3.25k
                    xlen--;
1878
3.25k
                }
1879
3.62k
            } else {
1880
3.62k
                xtn_hdr_data += xlen;
1881
3.62k
            }
1882
1883
            /* skip padding bytes. */
1884
5.49k
            while (xtn_hdr_data < xtn_hdr_end && *xtn_hdr_data == 0) {
1885
1.53k
                xtn_hdr_data++;
1886
1.53k
            }
1887
3.95k
        }
1888
872
    } else {
1889
        /* unsupported extension header format. */
1890
35
        return srtp_err_status_parse_err;
1891
35
    }
1892
1893
1.38k
    return srtp_err_status_ok;
1894
1.45k
}
1895
1896
/*
1897
 * AEAD uses a new IV formation method.  This function implements
1898
 * section 8.1. (SRTP IV Formation for AES-GCM) of RFC7714.
1899
 * The calculation is defined as, where (+) is the xor operation:
1900
 *
1901
 *
1902
 *              0  0  0  0  0  0  0  0  0  0  1  1
1903
 *              0  1  2  3  4  5  6  7  8  9  0  1
1904
 *            +--+--+--+--+--+--+--+--+--+--+--+--+
1905
 *            |00|00|    SSRC   |     ROC   | SEQ |---+
1906
 *            +--+--+--+--+--+--+--+--+--+--+--+--+   |
1907
 *                                                    |
1908
 *            +--+--+--+--+--+--+--+--+--+--+--+--+   |
1909
 *            |         Encryption Salt           |->(+)
1910
 *            +--+--+--+--+--+--+--+--+--+--+--+--+   |
1911
 *                                                    |
1912
 *            +--+--+--+--+--+--+--+--+--+--+--+--+   |
1913
 *            |       Initialization Vector       |<--+
1914
 *            +--+--+--+--+--+--+--+--+--+--+--+--+*
1915
 *
1916
 * Input:  *session_keys - pointer to SRTP stream context session keys,
1917
 *                         used to retrieve the SALT
1918
 *         *iv     - Pointer to receive the calculated IV
1919
 *         *seq    - The ROC and SEQ value to use for the
1920
 *                   IV calculation.
1921
 *         *hdr    - The RTP header, used to get the SSRC value
1922
 *
1923
 */
1924
1925
static void srtp_calc_aead_iv(srtp_session_keys_t *session_keys,
1926
                              v128_t *iv,
1927
                              srtp_xtd_seq_num_t *seq,
1928
                              const srtp_hdr_t *hdr)
1929
0
{
1930
0
    v128_t in;
1931
0
    v128_t salt;
1932
1933
0
    uint32_t local_roc = (uint32_t)(*seq >> 16);
1934
0
    uint16_t local_seq = (uint16_t)*seq;
1935
1936
0
    memset(&in, 0, sizeof(v128_t));
1937
0
    memset(&salt, 0, sizeof(v128_t));
1938
1939
0
    in.v16[5] = htons(local_seq);
1940
0
    local_roc = htonl(local_roc);
1941
0
    memcpy(&in.v16[3], &local_roc, sizeof(local_roc));
1942
1943
    /*
1944
     * Copy in the RTP SSRC value
1945
     */
1946
0
    memcpy(&in.v8[2], &hdr->ssrc, 4);
1947
0
    debug_print(mod_srtp, "Pre-salted RTP IV = %s\n", v128_hex_string(&in));
1948
1949
    /*
1950
     * Get the SALT value from the context
1951
     */
1952
0
    memcpy(salt.v8, session_keys->salt, SRTP_AEAD_SALT_LEN);
1953
0
    debug_print(mod_srtp, "RTP SALT = %s\n", v128_hex_string(&salt));
1954
1955
    /*
1956
     * Finally, apply tyhe SALT to the input
1957
     */
1958
0
    v128_xor(iv, &in, &salt);
1959
0
}
1960
1961
static srtp_err_status_t srtp_get_session_keys_for_packet(
1962
    srtp_stream_ctx_t *stream,
1963
    const uint8_t *hdr,
1964
    size_t pkt_octet_len,
1965
    size_t tag_len,
1966
    srtp_session_keys_t **session_keys)
1967
103k
{
1968
103k
    if (!stream->use_mki) {
1969
103k
        *session_keys = &stream->session_keys[0];
1970
103k
        return srtp_err_status_ok;
1971
103k
    }
1972
1973
0
    size_t mki_start_location = pkt_octet_len;
1974
1975
0
    if (tag_len > mki_start_location) {
1976
0
        return srtp_err_status_bad_mki;
1977
0
    }
1978
1979
0
    mki_start_location -= tag_len;
1980
1981
0
    if (stream->mki_size > mki_start_location) {
1982
0
        return srtp_err_status_bad_mki;
1983
0
    }
1984
1985
0
    mki_start_location -= stream->mki_size;
1986
1987
0
    for (size_t i = 0; i < stream->num_master_keys; i++) {
1988
0
        if (memcmp(hdr + mki_start_location, stream->session_keys[i].mki_id,
1989
0
                   stream->mki_size) == 0) {
1990
0
            *session_keys = &stream->session_keys[i];
1991
0
            return srtp_err_status_ok;
1992
0
        }
1993
0
    }
1994
1995
0
    return srtp_err_status_bad_mki;
1996
0
}
1997
1998
static srtp_err_status_t srtp_get_session_keys_for_rtp_packet(
1999
    srtp_stream_ctx_t *stream,
2000
    const uint8_t *hdr,
2001
    size_t pkt_octet_len,
2002
    srtp_session_keys_t **session_keys)
2003
101k
{
2004
101k
    size_t tag_len = 0;
2005
2006
    // Determine the authentication tag size
2007
101k
    if (stream->session_keys[0].rtp_cipher->algorithm == SRTP_AES_GCM_128 ||
2008
101k
        stream->session_keys[0].rtp_cipher->algorithm == SRTP_AES_GCM_256) {
2009
0
        tag_len = 0;
2010
101k
    } else {
2011
101k
        tag_len = srtp_auth_get_tag_length(stream->session_keys[0].rtp_auth);
2012
101k
    }
2013
2014
101k
    return srtp_get_session_keys_for_packet(stream, hdr, pkt_octet_len, tag_len,
2015
101k
                                            session_keys);
2016
101k
}
2017
2018
static srtp_err_status_t srtp_get_session_keys_for_rtcp_packet(
2019
    srtp_stream_ctx_t *stream,
2020
    const uint8_t *hdr,
2021
    size_t pkt_octet_len,
2022
    srtp_session_keys_t **session_keys)
2023
2.31k
{
2024
2.31k
    size_t tag_len = 0;
2025
2026
    // Determine the authentication tag size
2027
2.31k
    if (stream->session_keys[0].rtcp_cipher->algorithm == SRTP_AES_GCM_128 ||
2028
2.31k
        stream->session_keys[0].rtcp_cipher->algorithm == SRTP_AES_GCM_256) {
2029
0
        tag_len = 0;
2030
2.31k
    } else {
2031
2.31k
        tag_len = srtp_auth_get_tag_length(stream->session_keys[0].rtcp_auth);
2032
2.31k
    }
2033
2034
2.31k
    return srtp_get_session_keys_for_packet(stream, hdr, pkt_octet_len, tag_len,
2035
2.31k
                                            session_keys);
2036
2.31k
}
2037
2038
static srtp_err_status_t srtp_estimate_index(srtp_rdbx_t *rdbx,
2039
                                             uint32_t roc,
2040
                                             srtp_xtd_seq_num_t *est,
2041
                                             srtp_sequence_number_t seq,
2042
                                             ssize_t *delta)
2043
1.23k
{
2044
1.23k
    *est = (srtp_xtd_seq_num_t)(((uint64_t)roc) << 16) | seq;
2045
1.23k
    *delta = *est - rdbx->index;
2046
2047
1.23k
    if (*est > rdbx->index) {
2048
536
        if (*est - rdbx->index > seq_num_median) {
2049
371
            *delta = 0;
2050
371
            return srtp_err_status_pkt_idx_adv;
2051
371
        }
2052
703
    } else if (*est < rdbx->index) {
2053
400
        if (rdbx->index - *est > seq_num_median) {
2054
98
            *delta = 0;
2055
98
            return srtp_err_status_pkt_idx_old;
2056
98
        }
2057
400
    }
2058
2059
770
    return srtp_err_status_ok;
2060
1.23k
}
2061
2062
static srtp_err_status_t srtp_get_est_pkt_index(const srtp_hdr_t *hdr,
2063
                                                srtp_stream_ctx_t *stream,
2064
                                                srtp_xtd_seq_num_t *est,
2065
                                                ssize_t *delta)
2066
41.7k
{
2067
41.7k
    srtp_err_status_t result = srtp_err_status_ok;
2068
2069
41.7k
    if (stream->pending_roc) {
2070
1.23k
        result = srtp_estimate_index(&stream->rtp_rdbx, stream->pending_roc,
2071
1.23k
                                     est, ntohs(hdr->seq), delta);
2072
40.5k
    } else {
2073
        /* estimate packet index from seq. num. in header */
2074
40.5k
        *delta =
2075
40.5k
            srtp_rdbx_estimate_index(&stream->rtp_rdbx, est, ntohs(hdr->seq));
2076
40.5k
    }
2077
2078
41.7k
    debug_print(mod_srtp, "estimated u_packet index: %016" PRIx64, *est);
2079
2080
41.7k
    return result;
2081
41.7k
}
2082
2083
/*
2084
 * This function handles outgoing SRTP packets while in AEAD mode,
2085
 * which currently supports AES-GCM encryption.  All packets are
2086
 * encrypted and authenticated.
2087
 */
2088
static srtp_err_status_t srtp_protect_aead(srtp_ctx_t *ctx,
2089
                                           srtp_stream_ctx_t *stream,
2090
                                           const uint8_t *rtp,
2091
                                           size_t rtp_len,
2092
                                           uint8_t *srtp,
2093
                                           size_t *srtp_len,
2094
                                           srtp_session_keys_t *session_keys)
2095
0
{
2096
0
    const srtp_hdr_t *hdr = (const srtp_hdr_t *)rtp;
2097
0
    size_t enc_start;         /* offset to start of encrypted portion   */
2098
0
    size_t enc_octet_len = 0; /* number of octets in encrypted portion  */
2099
0
    srtp_xtd_seq_num_t est;   /* estimated xtd_seq_num_t of *hdr        */
2100
0
    ssize_t delta;            /* delta of local pkt idx and that in hdr */
2101
0
    srtp_err_status_t status;
2102
0
    size_t tag_len;
2103
0
    v128_t iv;
2104
0
    size_t aad_len;
2105
2106
0
    debug_print0(mod_srtp, "function srtp_protect_aead");
2107
2108
    /*
2109
     * update the key usage limit, and check it to make sure that we
2110
     * didn't just hit either the soft limit or the hard limit, and call
2111
     * the event handler if we hit either.
2112
     */
2113
0
    switch (srtp_key_limit_update(session_keys->limit)) {
2114
0
    case srtp_key_event_normal:
2115
0
        break;
2116
0
    case srtp_key_event_hard_limit:
2117
0
        srtp_handle_event(ctx, stream, event_key_hard_limit);
2118
0
        return srtp_err_status_key_expired;
2119
0
    case srtp_key_event_soft_limit:
2120
0
    default:
2121
0
        srtp_handle_event(ctx, stream, event_key_soft_limit);
2122
0
        break;
2123
0
    }
2124
2125
    /* get tag length from stream */
2126
0
    tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth);
2127
2128
    /* check output length */
2129
0
    if (*srtp_len < rtp_len + tag_len + stream->mki_size) {
2130
0
        return srtp_err_status_buffer_small;
2131
0
    }
2132
2133
    /*
2134
     * find starting point for encryption and length of data to be
2135
     * encrypted - the encrypted portion starts after the rtp header
2136
     * extension, if present; otherwise, it starts after the last csrc,
2137
     * if any are present
2138
     */
2139
0
    enc_start = srtp_get_rtp_hdr_len(hdr);
2140
0
    if (hdr->x == 1) {
2141
0
        enc_start += srtp_get_rtp_hdr_xtnd_len(hdr, rtp);
2142
0
    }
2143
2144
0
    bool cryptex_inuse, cryptex_inplace;
2145
0
    status = srtp_cryptex_protect_init(stream, hdr, rtp, srtp, &cryptex_inuse,
2146
0
                                       &cryptex_inplace, &enc_start);
2147
0
    if (status) {
2148
0
        return status;
2149
0
    }
2150
2151
0
    if (cryptex_inuse && !cryptex_inplace && hdr->cc) {
2152
0
        debug_print0(mod_srtp,
2153
0
                     "unsupported cryptex mode, AEAD, CC and not inplace io");
2154
0
        return srtp_err_status_cryptex_err;
2155
0
    }
2156
2157
    /* note: the passed size is without the auth tag */
2158
0
    if (enc_start > rtp_len) {
2159
0
        return srtp_err_status_parse_err;
2160
0
    }
2161
0
    enc_octet_len = rtp_len - enc_start;
2162
2163
    /* if not-inplace then need to copy full rtp header */
2164
0
    if (rtp != srtp) {
2165
0
        memcpy(srtp, rtp, enc_start);
2166
0
    }
2167
2168
    /*
2169
     * estimate the packet index using the start of the replay window
2170
     * and the sequence number from the header
2171
     */
2172
0
    status = srtp_get_est_pkt_index(hdr, stream, &est, &delta);
2173
2174
0
    if (status && (status != srtp_err_status_pkt_idx_adv)) {
2175
0
        return status;
2176
0
    }
2177
2178
0
    if (status == srtp_err_status_pkt_idx_adv) {
2179
0
        srtp_rdbx_set_roc_seq(&stream->rtp_rdbx, (uint32_t)(est >> 16),
2180
0
                              (uint16_t)(est & 0xFFFF));
2181
0
        stream->pending_roc = 0;
2182
0
        srtp_rdbx_add_index(&stream->rtp_rdbx, 0);
2183
0
    } else {
2184
0
        status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
2185
0
        if (status) {
2186
0
            if (status != srtp_err_status_replay_fail ||
2187
0
                !stream->allow_repeat_tx)
2188
0
                return status; /* we've been asked to reuse an index */
2189
0
        }
2190
0
        srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
2191
0
    }
2192
2193
0
    debug_print(mod_srtp, "estimated packet index: %016" PRIx64, est);
2194
2195
    /*
2196
     * AEAD uses a new IV formation method
2197
     */
2198
0
    srtp_calc_aead_iv(session_keys, &iv, &est, hdr);
2199
    /* shift est, put into network byte order */
2200
0
    est = be64_to_cpu(est << 16);
2201
2202
0
    status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
2203
0
                                srtp_direction_encrypt);
2204
0
    if (!status && session_keys->rtp_xtn_hdr_cipher) {
2205
0
        iv.v32[0] = 0;
2206
0
        iv.v32[1] = hdr->ssrc;
2207
0
        iv.v64[1] = est;
2208
0
        status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
2209
0
                                    (uint8_t *)&iv, srtp_direction_encrypt);
2210
0
    }
2211
0
    if (status) {
2212
0
        return srtp_err_status_cipher_fail;
2213
0
    }
2214
2215
0
    if (hdr->x == 1 && session_keys->rtp_xtn_hdr_cipher) {
2216
        /*
2217
         * extensions header encryption RFC 6904
2218
         */
2219
0
        status = srtp_process_header_encryption(
2220
0
            stream, srtp_get_rtp_xtn_hdr(hdr, srtp), session_keys);
2221
0
        if (status) {
2222
0
            return status;
2223
0
        }
2224
0
    }
2225
2226
0
    if (cryptex_inuse) {
2227
0
        status = srtp_cryptex_protect(cryptex_inplace, hdr, srtp,
2228
0
                                      session_keys->rtp_cipher);
2229
0
        if (status) {
2230
0
            return status;
2231
0
        }
2232
0
    }
2233
2234
    /*
2235
     * Set the AAD over the RTP header
2236
     */
2237
0
    aad_len = enc_start;
2238
0
    status = srtp_cipher_set_aad(session_keys->rtp_cipher, srtp, aad_len);
2239
0
    if (status) {
2240
0
        return (srtp_err_status_cipher_fail);
2241
0
    }
2242
2243
    /* Encrypt the payload  */
2244
0
    size_t outlen = *srtp_len - enc_start;
2245
0
    status = srtp_cipher_encrypt(session_keys->rtp_cipher, rtp + enc_start,
2246
0
                                 enc_octet_len, srtp + enc_start, &outlen);
2247
0
    enc_octet_len = outlen;
2248
0
    if (status) {
2249
0
        return srtp_err_status_cipher_fail;
2250
0
    }
2251
2252
0
    if (stream->use_mki) {
2253
0
        srtp_inject_mki(srtp + enc_start + enc_octet_len, session_keys,
2254
0
                        stream->mki_size);
2255
0
    }
2256
2257
0
    if (cryptex_inuse) {
2258
0
        srtp_cryptex_protect_cleanup(cryptex_inplace, hdr, srtp);
2259
0
    }
2260
2261
0
    *srtp_len = enc_start + enc_octet_len;
2262
2263
    /* increase the packet length by the length of the mki_size */
2264
0
    *srtp_len += stream->mki_size;
2265
2266
0
    return srtp_err_status_ok;
2267
0
}
2268
2269
/*
2270
 * This function handles incoming SRTP packets while in AEAD mode,
2271
 * which currently supports AES-GCM encryption.  All packets are
2272
 * encrypted and authenticated.  Note, the auth tag is at the end
2273
 * of the packet stream and is automatically checked by GCM
2274
 * when decrypting the payload.
2275
 */
2276
static srtp_err_status_t srtp_unprotect_aead(srtp_ctx_t *ctx,
2277
                                             srtp_stream_ctx_t *stream,
2278
                                             ssize_t delta,
2279
                                             srtp_xtd_seq_num_t est,
2280
                                             const uint8_t *srtp,
2281
                                             size_t srtp_len,
2282
                                             uint8_t *rtp,
2283
                                             size_t *rtp_len,
2284
                                             srtp_session_keys_t *session_keys,
2285
                                             bool advance_packet_index)
2286
0
{
2287
0
    const srtp_hdr_t *hdr = (const srtp_hdr_t *)srtp;
2288
0
    size_t enc_start;         /* offset to start of encrypted portion  */
2289
0
    size_t enc_octet_len = 0; /* number of octets in encrypted portion */
2290
0
    v128_t iv;
2291
0
    srtp_err_status_t status;
2292
0
    size_t tag_len;
2293
0
    size_t aad_len;
2294
2295
0
    debug_print0(mod_srtp, "function srtp_unprotect_aead");
2296
2297
0
    debug_print(mod_srtp, "estimated u_packet index: %016" PRIx64, est);
2298
2299
    /* get tag length from stream */
2300
0
    tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth);
2301
2302
    /*
2303
     * AEAD uses a new IV formation method
2304
     */
2305
0
    srtp_calc_aead_iv(session_keys, &iv, &est, hdr);
2306
0
    status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
2307
0
                                srtp_direction_decrypt);
2308
0
    if (!status && session_keys->rtp_xtn_hdr_cipher) {
2309
0
        iv.v32[0] = 0;
2310
0
        iv.v32[1] = hdr->ssrc;
2311
0
        iv.v64[1] = be64_to_cpu(est << 16);
2312
0
        status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
2313
0
                                    (uint8_t *)&iv, srtp_direction_encrypt);
2314
0
    }
2315
0
    if (status) {
2316
0
        return srtp_err_status_cipher_fail;
2317
0
    }
2318
2319
0
    enc_start = srtp_get_rtp_hdr_len(hdr);
2320
0
    if (hdr->x == 1) {
2321
0
        enc_start += srtp_get_rtp_hdr_xtnd_len(hdr, srtp);
2322
0
    }
2323
2324
0
    bool cryptex_inuse, cryptex_inplace;
2325
0
    status = srtp_cryptex_unprotect_init(stream, hdr, srtp, rtp, &cryptex_inuse,
2326
0
                                         &cryptex_inplace, &enc_start);
2327
0
    if (status) {
2328
0
        return status;
2329
0
    }
2330
2331
0
    if (cryptex_inuse && !cryptex_inplace && hdr->cc) {
2332
0
        debug_print0(mod_srtp,
2333
0
                     "unsupported cryptex mode, AEAD, CC and not inplace io");
2334
0
        return srtp_err_status_cryptex_err;
2335
0
    }
2336
2337
0
    if (enc_start > srtp_len - tag_len - stream->mki_size) {
2338
0
        return srtp_err_status_parse_err;
2339
0
    }
2340
2341
    /*
2342
     * We pass the tag down to the cipher when doing GCM mode
2343
     */
2344
0
    enc_octet_len = srtp_len - enc_start - stream->mki_size;
2345
2346
    /*
2347
     * Sanity check the encrypted payload length against
2348
     * the tag size.  It must always be at least as large
2349
     * as the tag length.
2350
     */
2351
0
    if (enc_octet_len < tag_len) {
2352
0
        return srtp_err_status_cipher_fail;
2353
0
    }
2354
2355
    /* check output length */
2356
0
    if (*rtp_len < srtp_len - stream->mki_size - tag_len) {
2357
0
        return srtp_err_status_buffer_small;
2358
0
    }
2359
2360
    /* if not-inplace then need to copy full rtp header */
2361
0
    if (srtp != rtp) {
2362
0
        memcpy(rtp, srtp, enc_start);
2363
0
    }
2364
2365
    /*
2366
     * update the key usage limit, and check it to make sure that we
2367
     * didn't just hit either the soft limit or the hard limit, and call
2368
     * the event handler if we hit either.
2369
     */
2370
0
    switch (srtp_key_limit_update(session_keys->limit)) {
2371
0
    case srtp_key_event_normal:
2372
0
        break;
2373
0
    case srtp_key_event_soft_limit:
2374
0
        srtp_handle_event(ctx, stream, event_key_soft_limit);
2375
0
        break;
2376
0
    case srtp_key_event_hard_limit:
2377
0
        srtp_handle_event(ctx, stream, event_key_hard_limit);
2378
0
        return srtp_err_status_key_expired;
2379
0
    default:
2380
0
        break;
2381
0
    }
2382
2383
0
    if (cryptex_inuse) {
2384
0
        status = srtp_cryptex_unprotect(cryptex_inplace, hdr, rtp,
2385
0
                                        session_keys->rtp_cipher);
2386
0
        if (status) {
2387
0
            return status;
2388
0
        }
2389
0
    }
2390
2391
    /*
2392
     * Set the AAD for AES-GCM, which is the RTP header
2393
     */
2394
0
    aad_len = enc_start;
2395
0
    status = srtp_cipher_set_aad(session_keys->rtp_cipher, srtp, aad_len);
2396
0
    if (status) {
2397
0
        return srtp_err_status_cipher_fail;
2398
0
    }
2399
2400
    /* Decrypt the ciphertext.  This also checks the auth tag based
2401
     * on the AAD we just specified above */
2402
0
    status =
2403
0
        srtp_cipher_decrypt(session_keys->rtp_cipher, srtp + enc_start,
2404
0
                            enc_octet_len, rtp + enc_start, &enc_octet_len);
2405
0
    if (status) {
2406
0
        return status;
2407
0
    }
2408
2409
0
    if (hdr->x == 1 && session_keys->rtp_xtn_hdr_cipher) {
2410
        /*
2411
         * extensions header encryption RFC 6904
2412
         */
2413
0
        status = srtp_process_header_encryption(
2414
0
            stream, srtp_get_rtp_xtn_hdr(hdr, rtp), session_keys);
2415
0
        if (status) {
2416
0
            return status;
2417
0
        }
2418
0
    }
2419
2420
0
    if (cryptex_inuse) {
2421
0
        srtp_cryptex_unprotect_cleanup(cryptex_inplace, hdr, rtp);
2422
0
    }
2423
2424
    /*
2425
     * verify that stream is for received traffic - this check will
2426
     * detect SSRC collisions, since a stream that appears in both
2427
     * srtp_protect() and srtp_unprotect() will fail this test in one of
2428
     * those functions.
2429
     *
2430
     * we do this check *after* the authentication check, so that the
2431
     * latter check will catch any attempts to fool us into thinking
2432
     * that we've got a collision
2433
     */
2434
0
    if (stream->direction != dir_srtp_receiver) {
2435
0
        if (stream->direction == dir_unknown) {
2436
0
            stream->direction = dir_srtp_receiver;
2437
0
        } else {
2438
0
            srtp_handle_event(ctx, stream, event_ssrc_collision);
2439
0
        }
2440
0
    }
2441
2442
    /*
2443
     * if the stream is a 'provisional' one, in which the template context
2444
     * is used, then we need to allocate a new stream at this point, since
2445
     * the authentication passed
2446
     */
2447
0
    if (stream == ctx->stream_template) {
2448
0
        srtp_stream_ctx_t *new_stream;
2449
2450
        /*
2451
         * allocate and initialize a new stream
2452
         *
2453
         * note that we indicate failure if we can't allocate the new
2454
         * stream, and some implementations will want to not return
2455
         * failure here
2456
         */
2457
0
        status =
2458
0
            srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
2459
0
        if (status) {
2460
0
            return status;
2461
0
        }
2462
2463
        /* add new stream to the list */
2464
0
        status = srtp_insert_or_dealloc_stream(ctx->stream_list, new_stream,
2465
0
                                               ctx->stream_template);
2466
0
        if (status) {
2467
0
            return status;
2468
0
        }
2469
2470
        /* set stream (the pointer used in this function) */
2471
0
        stream = new_stream;
2472
0
    }
2473
2474
    /*
2475
     * the message authentication function passed, so add the packet
2476
     * index into the replay database
2477
     */
2478
0
    if (advance_packet_index) {
2479
0
        uint32_t roc_to_set = (uint32_t)(est >> 16);
2480
0
        uint16_t seq_to_set = (uint16_t)(est & 0xFFFF);
2481
0
        srtp_rdbx_set_roc_seq(&stream->rtp_rdbx, roc_to_set, seq_to_set);
2482
0
        stream->pending_roc = 0;
2483
0
        srtp_rdbx_add_index(&stream->rtp_rdbx, 0);
2484
0
    } else {
2485
0
        srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
2486
0
    }
2487
2488
0
    *rtp_len = enc_start + enc_octet_len;
2489
2490
0
    return srtp_err_status_ok;
2491
0
}
2492
2493
srtp_err_status_t srtp_protect(srtp_t ctx,
2494
                               const uint8_t *rtp,
2495
                               size_t rtp_len,
2496
                               uint8_t *srtp,
2497
                               size_t *srtp_len,
2498
                               size_t mki_index)
2499
29.4k
{
2500
29.4k
    const srtp_hdr_t *hdr = (const srtp_hdr_t *)rtp;
2501
29.4k
    size_t enc_start;         /* offset to start of encrypted portion   */
2502
29.4k
    uint8_t *auth_start;      /* pointer to start of auth. portion      */
2503
29.4k
    size_t enc_octet_len = 0; /* number of octets in encrypted portion  */
2504
29.4k
    srtp_xtd_seq_num_t est;   /* estimated xtd_seq_num_t of *hdr        */
2505
29.4k
    ssize_t delta;            /* delta of local pkt idx and that in hdr */
2506
29.4k
    uint8_t *auth_tag = NULL; /* location of auth_tag within packet     */
2507
29.4k
    srtp_err_status_t status;
2508
29.4k
    size_t tag_len;
2509
29.4k
    srtp_stream_ctx_t *stream;
2510
29.4k
    size_t prefix_len;
2511
29.4k
    srtp_session_keys_t *session_keys = NULL;
2512
2513
29.4k
    debug_print0(mod_srtp, "function srtp_protect");
2514
2515
    /* Verify RTP header */
2516
29.4k
    status = srtp_validate_rtp_header(rtp, rtp_len);
2517
29.4k
    if (status) {
2518
90
        return status;
2519
90
    }
2520
2521
    /* check the packet length - it must at least contain a full header */
2522
29.3k
    if (rtp_len < octets_in_rtp_header) {
2523
0
        return srtp_err_status_bad_param;
2524
0
    }
2525
2526
    /*
2527
     * look up ssrc in srtp_stream list, and process the packet with
2528
     * the appropriate stream.  if we haven't seen this stream before,
2529
     * there's a template key for this srtp_session, and the cipher
2530
     * supports key-sharing, then we assume that a new stream using
2531
     * that key has just started up
2532
     */
2533
29.3k
    stream = srtp_get_stream(ctx, hdr->ssrc);
2534
29.3k
    if (stream == NULL) {
2535
7.99k
        if (ctx->stream_template != NULL) {
2536
7.95k
            srtp_stream_ctx_t *new_stream;
2537
2538
            /* allocate and initialize a new stream */
2539
7.95k
            status =
2540
7.95k
                srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
2541
7.95k
            if (status) {
2542
0
                return status;
2543
0
            }
2544
2545
            /* add new stream to the list */
2546
7.95k
            status = srtp_insert_or_dealloc_stream(ctx->stream_list, new_stream,
2547
7.95k
                                                   ctx->stream_template);
2548
7.95k
            if (status) {
2549
0
                return status;
2550
0
            }
2551
2552
            /* set direction to outbound */
2553
7.95k
            new_stream->direction = dir_srtp_sender;
2554
2555
            /* set stream (the pointer used in this function) */
2556
7.95k
            stream = new_stream;
2557
7.95k
        } else {
2558
            /* no template stream, so we return an error */
2559
34
            return srtp_err_status_no_ctx;
2560
34
        }
2561
7.99k
    }
2562
2563
    /*
2564
     * verify that stream is for sending traffic - this check will
2565
     * detect SSRC collisions, since a stream that appears in both
2566
     * srtp_protect() and srtp_unprotect() will fail this test in one of
2567
     * those functions.
2568
     */
2569
2570
29.3k
    if (stream->direction != dir_srtp_sender) {
2571
10.7k
        if (stream->direction == dir_unknown) {
2572
121
            stream->direction = dir_srtp_sender;
2573
10.6k
        } else {
2574
10.6k
            srtp_handle_event(ctx, stream, event_ssrc_collision);
2575
10.6k
        }
2576
10.7k
    }
2577
2578
29.3k
    status = srtp_get_session_keys(stream, mki_index, &session_keys);
2579
29.3k
    if (status) {
2580
0
        return status;
2581
0
    }
2582
2583
    /*
2584
     * Check if this is an AEAD stream (GCM mode).  If so, then dispatch
2585
     * the request to our AEAD handler.
2586
     */
2587
29.3k
    if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 ||
2588
29.3k
        session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) {
2589
0
        return srtp_protect_aead(ctx, stream, rtp, rtp_len, srtp, srtp_len,
2590
0
                                 session_keys);
2591
0
    }
2592
2593
    /*
2594
     * update the key usage limit, and check it to make sure that we
2595
     * didn't just hit either the soft limit or the hard limit, and call
2596
     * the event handler if we hit either.
2597
     */
2598
29.3k
    switch (srtp_key_limit_update(session_keys->limit)) {
2599
29.3k
    case srtp_key_event_normal:
2600
29.3k
        break;
2601
0
    case srtp_key_event_soft_limit:
2602
0
        srtp_handle_event(ctx, stream, event_key_soft_limit);
2603
0
        break;
2604
0
    case srtp_key_event_hard_limit:
2605
0
        srtp_handle_event(ctx, stream, event_key_hard_limit);
2606
0
        return srtp_err_status_key_expired;
2607
0
    default:
2608
0
        break;
2609
29.3k
    }
2610
2611
    /* get tag length from stream */
2612
29.3k
    tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth);
2613
2614
    /* check output length */
2615
29.3k
    if (*srtp_len < rtp_len + stream->mki_size + tag_len) {
2616
0
        return srtp_err_status_buffer_small;
2617
0
    }
2618
2619
    /*
2620
     * find starting point for encryption and length of data to be
2621
     * encrypted - the encrypted portion starts after the rtp header
2622
     * extension, if present; otherwise, it starts after the last csrc,
2623
     * if any are present
2624
     */
2625
29.3k
    enc_start = srtp_get_rtp_hdr_len(hdr);
2626
29.3k
    if (hdr->x == 1) {
2627
2.32k
        enc_start += srtp_get_rtp_hdr_xtnd_len(hdr, rtp);
2628
2.32k
    }
2629
2630
29.3k
    bool cryptex_inuse, cryptex_inplace;
2631
29.3k
    status = srtp_cryptex_protect_init(stream, hdr, rtp, srtp, &cryptex_inuse,
2632
29.3k
                                       &cryptex_inplace, &enc_start);
2633
29.3k
    if (status) {
2634
0
        return status;
2635
0
    }
2636
2637
29.3k
    if (enc_start > rtp_len) {
2638
0
        return srtp_err_status_parse_err;
2639
0
    }
2640
29.3k
    enc_octet_len = rtp_len - enc_start;
2641
2642
    /* if not-inplace then need to copy full rtp header */
2643
29.3k
    if (rtp != srtp) {
2644
0
        memcpy(srtp, rtp, enc_start);
2645
0
    }
2646
2647
29.3k
    if (stream->use_mki) {
2648
0
        srtp_inject_mki(srtp + rtp_len, session_keys, stream->mki_size);
2649
0
    }
2650
2651
    /*
2652
     * if we're providing authentication, set the auth_start and auth_tag
2653
     * pointers to the proper locations; otherwise, set auth_start to NULL
2654
     * to indicate that no authentication is needed
2655
     */
2656
29.3k
    if (stream->rtp_services & sec_serv_auth) {
2657
12.0k
        auth_start = srtp;
2658
12.0k
        auth_tag = srtp + rtp_len + stream->mki_size;
2659
17.2k
    } else {
2660
17.2k
        auth_start = NULL;
2661
17.2k
        auth_tag = NULL;
2662
17.2k
    }
2663
2664
    /*
2665
     * estimate the packet index using the start of the replay window
2666
     * and the sequence number from the header
2667
     */
2668
29.3k
    status = srtp_get_est_pkt_index(hdr, stream, &est, &delta);
2669
2670
29.3k
    if (status && (status != srtp_err_status_pkt_idx_adv)) {
2671
47
        return status;
2672
47
    }
2673
2674
29.3k
    if (status == srtp_err_status_pkt_idx_adv) {
2675
190
        srtp_rdbx_set_roc_seq(&stream->rtp_rdbx, (uint32_t)(est >> 16),
2676
190
                              (uint16_t)(est & 0xFFFF));
2677
190
        stream->pending_roc = 0;
2678
190
        srtp_rdbx_add_index(&stream->rtp_rdbx, 0);
2679
29.1k
    } else {
2680
29.1k
        status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
2681
29.1k
        if (status) {
2682
19.1k
            if (status != srtp_err_status_replay_fail ||
2683
19.1k
                !stream->allow_repeat_tx)
2684
78
                return status; /* we've been asked to reuse an index */
2685
19.1k
        }
2686
29.0k
        srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
2687
29.0k
    }
2688
2689
29.2k
    debug_print(mod_srtp, "estimated packet index: %016" PRIx64, est);
2690
2691
    /*
2692
     * if we're using rindael counter mode, set nonce and seq
2693
     */
2694
29.2k
    if (session_keys->rtp_cipher->type->id == SRTP_AES_ICM_128 ||
2695
17.4k
        session_keys->rtp_cipher->type->id == SRTP_AES_ICM_192 ||
2696
17.4k
        session_keys->rtp_cipher->type->id == SRTP_AES_ICM_256) {
2697
13.0k
        v128_t iv;
2698
2699
13.0k
        iv.v32[0] = 0;
2700
13.0k
        iv.v32[1] = hdr->ssrc;
2701
13.0k
        iv.v64[1] = be64_to_cpu(est << 16);
2702
13.0k
        status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
2703
13.0k
                                    srtp_direction_encrypt);
2704
13.0k
        if (!status && session_keys->rtp_xtn_hdr_cipher) {
2705
1.30k
            status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
2706
1.30k
                                        (uint8_t *)&iv, srtp_direction_encrypt);
2707
1.30k
        }
2708
16.1k
    } else {
2709
16.1k
        v128_t iv;
2710
2711
        /* otherwise, set the index to est */
2712
16.1k
        iv.v64[0] = 0;
2713
16.1k
        iv.v64[1] = be64_to_cpu(est);
2714
16.1k
        status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
2715
16.1k
                                    srtp_direction_encrypt);
2716
16.1k
        if (!status && session_keys->rtp_xtn_hdr_cipher) {
2717
9.12k
            status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
2718
9.12k
                                        (uint8_t *)&iv, srtp_direction_encrypt);
2719
9.12k
        }
2720
16.1k
    }
2721
29.2k
    if (status) {
2722
0
        return srtp_err_status_cipher_fail;
2723
0
    }
2724
2725
    /* shift est, put into network byte order */
2726
29.2k
    est = be64_to_cpu(est << 16);
2727
2728
    /*
2729
     * if we're authenticating using a universal hash, put the keystream
2730
     * prefix into the authentication tag
2731
     */
2732
29.2k
    if (auth_start) {
2733
12.0k
        prefix_len = srtp_auth_get_prefix_length(session_keys->rtp_auth);
2734
12.0k
        if (prefix_len) {
2735
0
            status = srtp_cipher_output(session_keys->rtp_cipher, auth_tag,
2736
0
                                        &prefix_len);
2737
0
            if (status) {
2738
0
                return srtp_err_status_cipher_fail;
2739
0
            }
2740
0
            debug_print(mod_srtp, "keystream prefix: %s",
2741
0
                        srtp_octet_string_hex_string(auth_tag, prefix_len));
2742
0
        }
2743
12.0k
    }
2744
2745
29.2k
    if (hdr->x == 1 && session_keys->rtp_xtn_hdr_cipher) {
2746
        /*
2747
         * extensions header encryption RFC 6904
2748
         */
2749
1.08k
        status = srtp_process_header_encryption(
2750
1.08k
            stream, srtp_get_rtp_xtn_hdr(hdr, srtp), session_keys);
2751
1.08k
        if (status) {
2752
39
            return status;
2753
39
        }
2754
1.08k
    }
2755
2756
29.1k
    if (cryptex_inuse) {
2757
0
        status = srtp_cryptex_protect(cryptex_inplace, hdr, srtp,
2758
0
                                      session_keys->rtp_cipher);
2759
0
        if (status) {
2760
0
            return status;
2761
0
        }
2762
0
    }
2763
2764
    /* if we're encrypting, exor keystream into the message */
2765
29.1k
    if (stream->rtp_services & sec_serv_conf) {
2766
13.0k
        status = srtp_cipher_encrypt(session_keys->rtp_cipher, rtp + enc_start,
2767
13.0k
                                     enc_octet_len, srtp + enc_start,
2768
13.0k
                                     &enc_octet_len);
2769
13.0k
        if (status) {
2770
0
            return srtp_err_status_cipher_fail;
2771
0
        }
2772
16.1k
    } else if (rtp != srtp) {
2773
        /* if no encryption and not-inplace then need to copy rest of packet */
2774
0
        memcpy(srtp + enc_start, rtp + enc_start, enc_octet_len);
2775
0
    }
2776
2777
29.1k
    if (cryptex_inuse) {
2778
0
        srtp_cryptex_protect_cleanup(cryptex_inplace, hdr, srtp);
2779
0
    }
2780
2781
    /*
2782
     *  if we're authenticating, run authentication function and put result
2783
     *  into the auth_tag
2784
     */
2785
29.1k
    if (auth_start) {
2786
        /* initialize auth func context */
2787
12.0k
        status = srtp_auth_start(session_keys->rtp_auth);
2788
12.0k
        if (status) {
2789
0
            return status;
2790
0
        }
2791
2792
        /* run auth func over packet */
2793
12.0k
        status = srtp_auth_update(session_keys->rtp_auth, auth_start, rtp_len);
2794
12.0k
        if (status) {
2795
0
            return status;
2796
0
        }
2797
2798
        /* run auth func over ROC, put result into auth_tag */
2799
12.0k
        debug_print(mod_srtp, "estimated packet index: %016" PRIx64, est);
2800
12.0k
        status = srtp_auth_compute(session_keys->rtp_auth, (uint8_t *)&est, 4,
2801
12.0k
                                   auth_tag);
2802
12.0k
        debug_print(mod_srtp, "srtp auth tag:    %s",
2803
12.0k
                    srtp_octet_string_hex_string(auth_tag, tag_len));
2804
12.0k
        if (status) {
2805
0
            return status;
2806
0
        }
2807
12.0k
    }
2808
2809
29.1k
    *srtp_len = enc_start + enc_octet_len;
2810
2811
    /* increase the packet length by the length of the auth tag */
2812
29.1k
    *srtp_len += tag_len;
2813
2814
    /* increate the packet length by the mki size if used */
2815
29.1k
    *srtp_len += stream->mki_size;
2816
2817
29.1k
    return srtp_err_status_ok;
2818
29.1k
}
2819
2820
srtp_err_status_t srtp_unprotect(srtp_t ctx,
2821
                                 const uint8_t *srtp,
2822
                                 size_t srtp_len,
2823
                                 uint8_t *rtp,
2824
                                 size_t *rtp_len)
2825
101k
{
2826
101k
    const srtp_hdr_t *hdr = (const srtp_hdr_t *)srtp;
2827
101k
    size_t enc_start;               /* pointer to start of encrypted portion  */
2828
101k
    const uint8_t *auth_start;      /* pointer to start of auth. portion      */
2829
101k
    size_t enc_octet_len = 0;       /* number of octets in encrypted portion  */
2830
101k
    const uint8_t *auth_tag = NULL; /* location of auth_tag within packet     */
2831
101k
    srtp_xtd_seq_num_t est;         /* estimated xtd_seq_num_t of *hdr        */
2832
101k
    ssize_t delta;                  /* delta of local pkt idx and that in hdr */
2833
101k
    v128_t iv;
2834
101k
    srtp_err_status_t status;
2835
101k
    srtp_stream_ctx_t *stream;
2836
101k
    uint8_t tmp_tag[SRTP_MAX_TAG_LEN];
2837
101k
    size_t tag_len, prefix_len;
2838
101k
    srtp_session_keys_t *session_keys = NULL;
2839
101k
    bool advance_packet_index = false;
2840
101k
    uint32_t roc_to_set = 0;
2841
101k
    uint16_t seq_to_set = 0;
2842
2843
101k
    debug_print0(mod_srtp, "function srtp_unprotect");
2844
2845
    /* Verify RTP header */
2846
101k
    status = srtp_validate_rtp_header(srtp, srtp_len);
2847
101k
    if (status) {
2848
65
        return status;
2849
65
    }
2850
2851
    /* check the packet length - it must at least contain a full header */
2852
101k
    if (srtp_len < octets_in_rtp_header) {
2853
0
        return srtp_err_status_bad_param;
2854
0
    }
2855
2856
    /*
2857
     * look up ssrc in srtp_stream list, and process the packet with
2858
     * the appropriate stream.  if we haven't seen this stream before,
2859
     * there's only one key for this srtp_session, and the cipher
2860
     * supports key-sharing, then we assume that a new stream using
2861
     * that key has just started up
2862
     */
2863
101k
    stream = srtp_get_stream(ctx, hdr->ssrc);
2864
101k
    if (stream == NULL) {
2865
89.2k
        if (ctx->stream_template != NULL) {
2866
89.2k
            stream = ctx->stream_template;
2867
89.2k
            debug_print(mod_srtp, "using provisional stream (SSRC: 0x%08x)",
2868
89.2k
                        (unsigned int)ntohl(hdr->ssrc));
2869
2870
            /*
2871
             * set estimated packet index to sequence number from header,
2872
             * and set delta equal to the same value
2873
             */
2874
89.2k
            est = (srtp_xtd_seq_num_t)ntohs(hdr->seq);
2875
89.2k
            delta = (int)est;
2876
89.2k
        } else {
2877
            /*
2878
             * no stream corresponding to SSRC found, and we don't do
2879
             * key-sharing, so return an error
2880
             */
2881
36
            return srtp_err_status_no_ctx;
2882
36
        }
2883
89.2k
    } else {
2884
12.4k
        status = srtp_get_est_pkt_index(hdr, stream, &est, &delta);
2885
2886
12.4k
        if (status && (status != srtp_err_status_pkt_idx_adv)) {
2887
51
            return status;
2888
51
        }
2889
2890
12.3k
        if (status == srtp_err_status_pkt_idx_adv) {
2891
181
            advance_packet_index = true;
2892
181
            roc_to_set = (uint32_t)(est >> 16);
2893
181
            seq_to_set = (uint16_t)(est & 0xFFFF);
2894
181
        }
2895
2896
        /* check replay database */
2897
12.3k
        if (!advance_packet_index) {
2898
12.1k
            status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
2899
12.1k
            if (status) {
2900
121
                return status;
2901
121
            }
2902
12.1k
        }
2903
12.3k
    }
2904
2905
101k
    debug_print(mod_srtp, "estimated u_packet index: %016" PRIx64, est);
2906
2907
    /* Determine if MKI is being used and what session keys should be used */
2908
101k
    status = srtp_get_session_keys_for_rtp_packet(stream, srtp, srtp_len,
2909
101k
                                                  &session_keys);
2910
101k
    if (status) {
2911
0
        return status;
2912
0
    }
2913
2914
    /*
2915
     * Check if this is an AEAD stream (GCM mode).  If so, then dispatch
2916
     * the request to our AEAD handler.
2917
     */
2918
101k
    if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 ||
2919
101k
        session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) {
2920
0
        return srtp_unprotect_aead(ctx, stream, delta, est, srtp, srtp_len, rtp,
2921
0
                                   rtp_len, session_keys, advance_packet_index);
2922
0
    }
2923
2924
    /* get tag length from stream */
2925
101k
    tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth);
2926
2927
    /*
2928
     * set the cipher's IV properly, depending on whatever cipher we
2929
     * happen to be using
2930
     */
2931
101k
    if (session_keys->rtp_cipher->type->id == SRTP_AES_ICM_128 ||
2932
40.4k
        session_keys->rtp_cipher->type->id == SRTP_AES_ICM_192 ||
2933
62.5k
        session_keys->rtp_cipher->type->id == SRTP_AES_ICM_256) {
2934
        /* aes counter mode */
2935
62.5k
        iv.v32[0] = 0;
2936
62.5k
        iv.v32[1] = hdr->ssrc; /* still in network order */
2937
62.5k
        iv.v64[1] = be64_to_cpu(est << 16);
2938
62.5k
        status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
2939
62.5k
                                    srtp_direction_decrypt);
2940
62.5k
        if (!status && session_keys->rtp_xtn_hdr_cipher) {
2941
1.06k
            status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
2942
1.06k
                                        (uint8_t *)&iv, srtp_direction_decrypt);
2943
1.06k
        }
2944
62.5k
    } else {
2945
        /* no particular format - set the iv to the packet index */
2946
38.9k
        iv.v64[0] = 0;
2947
38.9k
        iv.v64[1] = be64_to_cpu(est);
2948
38.9k
        status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
2949
38.9k
                                    srtp_direction_decrypt);
2950
38.9k
        if (!status && session_keys->rtp_xtn_hdr_cipher) {
2951
21.7k
            status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
2952
21.7k
                                        (uint8_t *)&iv, srtp_direction_decrypt);
2953
21.7k
        }
2954
38.9k
    }
2955
101k
    if (status) {
2956
0
        return srtp_err_status_cipher_fail;
2957
0
    }
2958
2959
    /* shift est, put into network byte order */
2960
101k
    est = be64_to_cpu(est << 16);
2961
2962
101k
    enc_start = srtp_get_rtp_hdr_len(hdr);
2963
101k
    if (hdr->x == 1) {
2964
590
        enc_start += srtp_get_rtp_hdr_xtnd_len(hdr, srtp);
2965
590
    }
2966
2967
101k
    bool cryptex_inuse, cryptex_inplace;
2968
101k
    status = srtp_cryptex_unprotect_init(stream, hdr, srtp, rtp, &cryptex_inuse,
2969
101k
                                         &cryptex_inplace, &enc_start);
2970
101k
    if (status) {
2971
0
        return status;
2972
0
    }
2973
2974
101k
    if (enc_start > srtp_len - tag_len - stream->mki_size) {
2975
9
        return srtp_err_status_parse_err;
2976
9
    }
2977
101k
    enc_octet_len = srtp_len - enc_start - stream->mki_size - tag_len;
2978
2979
    /* check output length */
2980
101k
    if (*rtp_len < srtp_len - stream->mki_size - tag_len) {
2981
0
        return srtp_err_status_buffer_small;
2982
0
    }
2983
2984
    /* if not-inplace then need to copy full rtp header */
2985
101k
    if (srtp != rtp) {
2986
0
        memcpy(rtp, srtp, enc_start);
2987
0
    }
2988
2989
    /*
2990
     * if we're providing authentication, set the auth_start and auth_tag
2991
     * pointers to the proper locations; otherwise, set auth_start to NULL
2992
     * to indicate that no authentication is needed
2993
     */
2994
101k
    if (stream->rtp_services & sec_serv_auth) {
2995
86
        auth_start = srtp;
2996
86
        auth_tag = srtp + srtp_len - tag_len;
2997
101k
    } else {
2998
101k
        auth_start = NULL;
2999
101k
        auth_tag = NULL;
3000
101k
    }
3001
3002
    /*
3003
     * if we expect message authentication, run the authentication
3004
     * function and compare the result with the value of the auth_tag
3005
     */
3006
101k
    if (auth_start) {
3007
        /*
3008
         * if we're using a universal hash, then we need to compute the
3009
         * keystream prefix for encrypting the universal hash output
3010
         *
3011
         * if the keystream prefix length is zero, then we know that
3012
         * the authenticator isn't using a universal hash function
3013
         */
3014
86
        if (session_keys->rtp_auth->prefix_len != 0) {
3015
0
            prefix_len = srtp_auth_get_prefix_length(session_keys->rtp_auth);
3016
0
            status = srtp_cipher_output(session_keys->rtp_cipher, tmp_tag,
3017
0
                                        &prefix_len);
3018
0
            debug_print(mod_srtp, "keystream prefix: %s",
3019
0
                        srtp_octet_string_hex_string(tmp_tag, prefix_len));
3020
0
            if (status) {
3021
0
                return srtp_err_status_cipher_fail;
3022
0
            }
3023
0
        }
3024
3025
        /* initialize auth func context */
3026
86
        status = srtp_auth_start(session_keys->rtp_auth);
3027
86
        if (status) {
3028
0
            return status;
3029
0
        }
3030
3031
        /* now compute auth function over packet */
3032
86
        status = srtp_auth_update(session_keys->rtp_auth, auth_start,
3033
86
                                  srtp_len - tag_len - stream->mki_size);
3034
86
        if (status) {
3035
0
            return status;
3036
0
        }
3037
3038
        /* run auth func over ROC, then write tmp tag */
3039
86
        status = srtp_auth_compute(session_keys->rtp_auth, (uint8_t *)&est, 4,
3040
86
                                   tmp_tag);
3041
3042
86
        debug_print(mod_srtp, "computed auth tag:    %s",
3043
86
                    srtp_octet_string_hex_string(tmp_tag, tag_len));
3044
86
        debug_print(mod_srtp, "packet auth tag:      %s",
3045
86
                    srtp_octet_string_hex_string(auth_tag, tag_len));
3046
86
        if (status) {
3047
0
            return srtp_err_status_auth_fail;
3048
0
        }
3049
3050
86
        if (!srtp_octet_string_equal(tmp_tag, auth_tag, tag_len)) {
3051
84
            return srtp_err_status_auth_fail;
3052
84
        }
3053
86
    }
3054
3055
    /*
3056
     * update the key usage limit, and check it to make sure that we
3057
     * didn't just hit either the soft limit or the hard limit, and call
3058
     * the event handler if we hit either.
3059
     */
3060
101k
    switch (srtp_key_limit_update(session_keys->limit)) {
3061
101k
    case srtp_key_event_normal:
3062
101k
        break;
3063
0
    case srtp_key_event_soft_limit:
3064
0
        srtp_handle_event(ctx, stream, event_key_soft_limit);
3065
0
        break;
3066
0
    case srtp_key_event_hard_limit:
3067
0
        srtp_handle_event(ctx, stream, event_key_hard_limit);
3068
0
        return srtp_err_status_key_expired;
3069
0
    default:
3070
0
        break;
3071
101k
    }
3072
3073
101k
    if (hdr->x == 1 && session_keys->rtp_xtn_hdr_cipher) {
3074
        /* extensions header encryption RFC 6904 */
3075
371
        status = srtp_process_header_encryption(
3076
371
            stream, srtp_get_rtp_xtn_hdr(hdr, rtp), session_keys);
3077
371
        if (status) {
3078
32
            return status;
3079
32
        }
3080
371
    }
3081
3082
101k
    if (cryptex_inuse) {
3083
0
        status = srtp_cryptex_unprotect(cryptex_inplace, hdr, rtp,
3084
0
                                        session_keys->rtp_cipher);
3085
0
        if (status) {
3086
0
            return status;
3087
0
        }
3088
0
    }
3089
3090
    /* if we're decrypting, add keystream into ciphertext */
3091
101k
    if (stream->rtp_services & sec_serv_conf) {
3092
62.5k
        status =
3093
62.5k
            srtp_cipher_decrypt(session_keys->rtp_cipher, srtp + enc_start,
3094
62.5k
                                enc_octet_len, rtp + enc_start, &enc_octet_len);
3095
62.5k
        if (status) {
3096
0
            return srtp_err_status_cipher_fail;
3097
0
        }
3098
62.5k
    } else if (rtp != srtp) {
3099
        /* if no encryption and not-inplace then need to copy rest of packet */
3100
0
        memcpy(rtp + enc_start, srtp + enc_start, enc_octet_len);
3101
0
    }
3102
3103
101k
    if (cryptex_inuse) {
3104
0
        srtp_cryptex_unprotect_cleanup(cryptex_inplace, hdr, rtp);
3105
0
    }
3106
3107
    /*
3108
     * verify that stream is for received traffic - this check will
3109
     * detect SSRC collisions, since a stream that appears in both
3110
     * srtp_protect() and srtp_unprotect() will fail this test in one of
3111
     * those functions.
3112
     *
3113
     * we do this check *after* the authentication check, so that the
3114
     * latter check will catch any attempts to fool us into thinking
3115
     * that we've got a collision
3116
     */
3117
101k
    if (stream->direction != dir_srtp_receiver) {
3118
82.0k
        if (stream->direction == dir_unknown) {
3119
40
            stream->direction = dir_srtp_receiver;
3120
82.0k
        } else {
3121
82.0k
            srtp_handle_event(ctx, stream, event_ssrc_collision);
3122
82.0k
        }
3123
82.0k
    }
3124
3125
    /*
3126
     * if the stream is a 'provisional' one, in which the template context
3127
     * is used, then we need to allocate a new stream at this point, since
3128
     * the authentication passed
3129
     */
3130
101k
    if (stream == ctx->stream_template) {
3131
89.1k
        srtp_stream_ctx_t *new_stream;
3132
3133
        /*
3134
         * allocate and initialize a new stream
3135
         *
3136
         * note that we indicate failure if we can't allocate the new
3137
         * stream, and some implementations will want to not return
3138
         * failure here
3139
         */
3140
89.1k
        status =
3141
89.1k
            srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
3142
89.1k
        if (status) {
3143
0
            return status;
3144
0
        }
3145
3146
        /* add new stream to the list */
3147
89.1k
        status = srtp_insert_or_dealloc_stream(ctx->stream_list, new_stream,
3148
89.1k
                                               ctx->stream_template);
3149
89.1k
        if (status) {
3150
0
            return status;
3151
0
        }
3152
3153
        /* set stream (the pointer used in this function) */
3154
89.1k
        stream = new_stream;
3155
89.1k
    }
3156
3157
    /*
3158
     * the message authentication function passed, so add the packet
3159
     * index into the replay database
3160
     */
3161
101k
    if (advance_packet_index) {
3162
181
        srtp_rdbx_set_roc_seq(&stream->rtp_rdbx, roc_to_set, seq_to_set);
3163
181
        stream->pending_roc = 0;
3164
181
        srtp_rdbx_add_index(&stream->rtp_rdbx, 0);
3165
101k
    } else {
3166
101k
        srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
3167
101k
    }
3168
3169
101k
    *rtp_len = enc_start + enc_octet_len;
3170
3171
101k
    return srtp_err_status_ok;
3172
101k
}
3173
3174
srtp_err_status_t srtp_init(void)
3175
2
{
3176
2
    srtp_err_status_t status;
3177
3178
    /* initialize crypto kernel */
3179
2
    status = srtp_crypto_kernel_init();
3180
2
    if (status) {
3181
0
        return status;
3182
0
    }
3183
3184
    /* load srtp debug module into the kernel */
3185
2
    status = srtp_crypto_kernel_load_debug_module(&mod_srtp);
3186
2
    if (status) {
3187
0
        return status;
3188
0
    }
3189
3190
2
    return srtp_err_status_ok;
3191
2
}
3192
3193
srtp_err_status_t srtp_shutdown(void)
3194
0
{
3195
0
    srtp_err_status_t status;
3196
3197
    /* shut down crypto kernel */
3198
0
    status = srtp_crypto_kernel_shutdown();
3199
0
    if (status) {
3200
0
        return status;
3201
0
    }
3202
3203
    /* shutting down crypto kernel frees the srtp debug module as well */
3204
3205
0
    return srtp_err_status_ok;
3206
0
}
3207
3208
srtp_stream_ctx_t *srtp_get_stream(srtp_t srtp, uint32_t ssrc)
3209
241k
{
3210
241k
    return srtp_stream_list_get(srtp->stream_list, ssrc);
3211
241k
}
3212
3213
srtp_err_status_t srtp_dealloc(srtp_t session)
3214
3.26k
{
3215
3.26k
    srtp_err_status_t status;
3216
3217
    /*
3218
     * we take a conservative deallocation strategy - if we encounter an
3219
     * error deallocating a stream, then we stop trying to deallocate
3220
     * memory and just return an error
3221
     */
3222
3223
    /* deallocate streams */
3224
3.26k
    status = srtp_remove_and_dealloc_streams(session->stream_list,
3225
3.26k
                                             session->stream_template);
3226
3.26k
    if (status) {
3227
0
        return status;
3228
0
    }
3229
3230
    /* deallocate stream template, if there is one */
3231
3.26k
    if (session->stream_template != NULL) {
3232
2.71k
        status = srtp_stream_dealloc(session->stream_template, NULL);
3233
2.71k
        if (status) {
3234
0
            return status;
3235
0
        }
3236
2.71k
    }
3237
3238
    /* deallocate stream list */
3239
3.26k
    status = srtp_stream_list_dealloc(session->stream_list);
3240
3.26k
    if (status) {
3241
0
        return status;
3242
0
    }
3243
3244
    /* deallocate session context */
3245
3.26k
    srtp_crypto_free(session);
3246
3247
3.26k
    return srtp_err_status_ok;
3248
3.26k
}
3249
3250
srtp_err_status_t srtp_stream_add(srtp_t session, const srtp_policy_t *policy)
3251
9.99k
{
3252
9.99k
    srtp_err_status_t status;
3253
9.99k
    srtp_stream_t tmp;
3254
3255
    /* sanity check arguments */
3256
9.99k
    if (session == NULL) {
3257
0
        return srtp_err_status_bad_param;
3258
0
    }
3259
3260
9.99k
    status = srtp_valid_policy(policy);
3261
9.99k
    if (status != srtp_err_status_ok) {
3262
30
        return status;
3263
30
    }
3264
3265
    /* allocate stream  */
3266
9.96k
    status = srtp_stream_alloc(&tmp, policy);
3267
9.96k
    if (status) {
3268
0
        return status;
3269
0
    }
3270
3271
    /* initialize stream  */
3272
9.96k
    status = srtp_stream_init(tmp, policy);
3273
9.96k
    if (status) {
3274
211
        srtp_stream_dealloc(tmp, NULL);
3275
211
        return status;
3276
211
    }
3277
3278
    /*
3279
     * set the head of the stream list or the template to point to the
3280
     * stream that we've just alloced and init'ed, depending on whether
3281
     * or not it has a wildcard SSRC value or not
3282
     *
3283
     * if the template stream has already been set, then the policy is
3284
     * inconsistent, so we return a bad_param error code
3285
     */
3286
9.75k
    switch (policy->ssrc.type) {
3287
1.37k
    case (ssrc_any_outbound):
3288
1.37k
        if (session->stream_template) {
3289
9
            srtp_stream_dealloc(tmp, NULL);
3290
9
            return srtp_err_status_bad_param;
3291
9
        }
3292
1.36k
        session->stream_template = tmp;
3293
1.36k
        session->stream_template->direction = dir_srtp_sender;
3294
1.36k
        break;
3295
1.35k
    case (ssrc_any_inbound):
3296
1.35k
        if (session->stream_template) {
3297
4
            srtp_stream_dealloc(tmp, NULL);
3298
4
            return srtp_err_status_bad_param;
3299
4
        }
3300
1.34k
        session->stream_template = tmp;
3301
1.34k
        session->stream_template->direction = dir_srtp_receiver;
3302
1.34k
        break;
3303
7.01k
    case (ssrc_specific):
3304
7.01k
        status = srtp_insert_or_dealloc_stream(session->stream_list, tmp,
3305
7.01k
                                               session->stream_template);
3306
7.01k
        if (status) {
3307
0
            return status;
3308
0
        }
3309
7.01k
        break;
3310
7.01k
    case (ssrc_undefined):
3311
20
    default:
3312
20
        srtp_stream_dealloc(tmp, NULL);
3313
20
        return srtp_err_status_bad_param;
3314
9.75k
    }
3315
3316
9.72k
    return srtp_err_status_ok;
3317
9.75k
}
3318
3319
srtp_err_status_t srtp_create(srtp_t *session, /* handle for session     */
3320
                              const srtp_policy_t *policy)
3321
3.33k
{ /* SRTP policy (list)     */
3322
3.33k
    srtp_err_status_t stat;
3323
3.33k
    srtp_ctx_t *ctx;
3324
3325
    /* sanity check arguments */
3326
3.33k
    if (session == NULL) {
3327
0
        return srtp_err_status_bad_param;
3328
0
    }
3329
3330
3.33k
    if (policy) {
3331
3.33k
        stat = srtp_valid_policy(policy);
3332
3.33k
        if (stat != srtp_err_status_ok) {
3333
70
            return stat;
3334
70
        }
3335
3.33k
    }
3336
3337
    /* allocate srtp context and set ctx_ptr */
3338
3.26k
    ctx = (srtp_ctx_t *)srtp_crypto_alloc(sizeof(srtp_ctx_t));
3339
3.26k
    if (ctx == NULL) {
3340
0
        return srtp_err_status_alloc_fail;
3341
0
    }
3342
3.26k
    *session = ctx;
3343
3344
3.26k
    ctx->stream_template = NULL;
3345
3.26k
    ctx->stream_list = NULL;
3346
3.26k
    ctx->user_data = NULL;
3347
3348
    /* allocate stream list */
3349
3.26k
    stat = srtp_stream_list_alloc(&ctx->stream_list);
3350
3.26k
    if (stat) {
3351
        /* clean up everything */
3352
0
        srtp_dealloc(*session);
3353
0
        *session = NULL;
3354
0
        return stat;
3355
0
    }
3356
3357
    /*
3358
     * loop over elements in the policy list, allocating and
3359
     * initializing a stream for each element
3360
     */
3361
12.9k
    while (policy != NULL) {
3362
9.99k
        stat = srtp_stream_add(ctx, policy);
3363
9.99k
        if (stat) {
3364
            /* clean up everything */
3365
274
            srtp_dealloc(*session);
3366
274
            *session = NULL;
3367
274
            return stat;
3368
274
        }
3369
3370
        /* set policy to next item in list  */
3371
9.72k
        policy = policy->next;
3372
9.72k
    }
3373
3374
2.99k
    return srtp_err_status_ok;
3375
3.26k
}
3376
3377
srtp_err_status_t srtp_stream_remove(srtp_t session, uint32_t ssrc)
3378
1.18k
{
3379
1.18k
    srtp_stream_ctx_t *stream;
3380
1.18k
    srtp_err_status_t status;
3381
3382
    /* sanity check arguments */
3383
1.18k
    if (session == NULL) {
3384
0
        return srtp_err_status_bad_param;
3385
0
    }
3386
3387
    /* find and remove stream from the list */
3388
1.18k
    stream = srtp_stream_list_get(session->stream_list, htonl(ssrc));
3389
1.18k
    if (stream == NULL) {
3390
103
        return srtp_err_status_no_ctx;
3391
103
    }
3392
3393
1.08k
    srtp_stream_list_remove(session->stream_list, stream);
3394
3395
    /* deallocate the stream */
3396
1.08k
    status = srtp_stream_dealloc(stream, session->stream_template);
3397
1.08k
    if (status) {
3398
0
        return status;
3399
0
    }
3400
3401
1.08k
    return srtp_err_status_ok;
3402
1.08k
}
3403
3404
srtp_err_status_t srtp_update(srtp_t session, const srtp_policy_t *policy)
3405
0
{
3406
0
    srtp_err_status_t stat;
3407
3408
    /* sanity check arguments */
3409
0
    if (session == NULL) {
3410
0
        return srtp_err_status_bad_param;
3411
0
    }
3412
3413
0
    stat = srtp_valid_policy(policy);
3414
0
    if (stat != srtp_err_status_ok) {
3415
0
        return stat;
3416
0
    }
3417
3418
0
    while (policy != NULL) {
3419
0
        stat = srtp_stream_update(session, policy);
3420
0
        if (stat) {
3421
0
            return stat;
3422
0
        }
3423
3424
        /* set policy to next item in list  */
3425
0
        policy = policy->next;
3426
0
    }
3427
0
    return srtp_err_status_ok;
3428
0
}
3429
3430
struct update_template_stream_data {
3431
    srtp_err_status_t status;
3432
    srtp_t session;
3433
    srtp_stream_t new_stream_template;
3434
    srtp_stream_list_t new_stream_list;
3435
};
3436
3437
static bool update_template_stream_cb(srtp_stream_t stream, void *raw_data)
3438
0
{
3439
0
    struct update_template_stream_data *data =
3440
0
        (struct update_template_stream_data *)raw_data;
3441
0
    srtp_t session = data->session;
3442
0
    uint32_t ssrc = stream->ssrc;
3443
0
    srtp_xtd_seq_num_t old_index;
3444
0
    srtp_rdb_t old_rtcp_rdb;
3445
3446
    /* old / non-template streams are copied unchanged */
3447
0
    if (stream->session_keys[0].rtp_auth !=
3448
0
        session->stream_template->session_keys[0].rtp_auth) {
3449
0
        srtp_stream_list_remove(session->stream_list, stream);
3450
0
        data->status = srtp_insert_or_dealloc_stream(
3451
0
            data->new_stream_list, stream, session->stream_template);
3452
0
        if (data->status) {
3453
0
            return false;
3454
0
        }
3455
0
        return true;
3456
0
    }
3457
3458
    /* save old extended seq */
3459
0
    old_index = stream->rtp_rdbx.index;
3460
0
    old_rtcp_rdb = stream->rtcp_rdb;
3461
3462
    /* remove stream */
3463
0
    data->status = srtp_stream_remove(session, ntohl(ssrc));
3464
0
    if (data->status) {
3465
0
        return false;
3466
0
    }
3467
3468
    /* allocate and initialize a new stream */
3469
0
    data->status = srtp_stream_clone(data->new_stream_template, ssrc, &stream);
3470
0
    if (data->status) {
3471
0
        return false;
3472
0
    }
3473
3474
    /* add new stream to the head of the new_stream_list */
3475
0
    data->status = srtp_insert_or_dealloc_stream(data->new_stream_list, stream,
3476
0
                                                 data->new_stream_template);
3477
0
    if (data->status) {
3478
0
        return false;
3479
0
    }
3480
3481
    /* restore old extended seq */
3482
0
    stream->rtp_rdbx.index = old_index;
3483
0
    stream->rtcp_rdb = old_rtcp_rdb;
3484
3485
0
    return true;
3486
0
}
3487
3488
static srtp_err_status_t is_update_policy_compatable(
3489
    srtp_stream_t stream,
3490
    const srtp_policy_t *policy)
3491
0
{
3492
0
    if (stream->use_mki != policy->use_mki) {
3493
0
        return srtp_err_status_bad_param;
3494
0
    }
3495
3496
0
    if (stream->use_mki && stream->mki_size != policy->mki_size) {
3497
0
        return srtp_err_status_bad_param;
3498
0
    }
3499
3500
0
    return srtp_err_status_ok;
3501
0
}
3502
3503
static srtp_err_status_t update_template_streams(srtp_t session,
3504
                                                 const srtp_policy_t *policy)
3505
0
{
3506
0
    srtp_err_status_t status;
3507
0
    srtp_stream_t new_stream_template;
3508
0
    srtp_stream_list_t new_stream_list;
3509
3510
0
    status = srtp_valid_policy(policy);
3511
0
    if (status != srtp_err_status_ok) {
3512
0
        return status;
3513
0
    }
3514
3515
0
    if (session->stream_template == NULL) {
3516
0
        return srtp_err_status_bad_param;
3517
0
    }
3518
3519
0
    status = is_update_policy_compatable(session->stream_template, policy);
3520
0
    if (status != srtp_err_status_ok) {
3521
0
        return status;
3522
0
    }
3523
3524
    /* allocate new template stream  */
3525
0
    status = srtp_stream_alloc(&new_stream_template, policy);
3526
0
    if (status) {
3527
0
        return status;
3528
0
    }
3529
3530
    /* initialize new template stream  */
3531
0
    status = srtp_stream_init(new_stream_template, policy);
3532
0
    if (status) {
3533
0
        srtp_crypto_free(new_stream_template);
3534
0
        return status;
3535
0
    }
3536
3537
    /* allocate new stream list */
3538
0
    status = srtp_stream_list_alloc(&new_stream_list);
3539
0
    if (status) {
3540
0
        srtp_crypto_free(new_stream_template);
3541
0
        return status;
3542
0
    }
3543
3544
    /* process streams */
3545
0
    struct update_template_stream_data data = { srtp_err_status_ok, session,
3546
0
                                                new_stream_template,
3547
0
                                                new_stream_list };
3548
0
    srtp_stream_list_for_each(session->stream_list, update_template_stream_cb,
3549
0
                              &data);
3550
0
    if (data.status) {
3551
        /* free new allocations */
3552
0
        srtp_remove_and_dealloc_streams(new_stream_list, new_stream_template);
3553
0
        srtp_stream_list_dealloc(new_stream_list);
3554
0
        srtp_stream_dealloc(new_stream_template, NULL);
3555
0
        return data.status;
3556
0
    }
3557
3558
    /* dealloc old list / template */
3559
0
    srtp_remove_and_dealloc_streams(session->stream_list,
3560
0
                                    session->stream_template);
3561
0
    srtp_stream_list_dealloc(session->stream_list);
3562
0
    srtp_stream_dealloc(session->stream_template, NULL);
3563
3564
    /* set new list / template */
3565
0
    session->stream_template = new_stream_template;
3566
0
    session->stream_list = new_stream_list;
3567
0
    return srtp_err_status_ok;
3568
0
}
3569
3570
static srtp_err_status_t stream_update(srtp_t session,
3571
                                       const srtp_policy_t *policy)
3572
0
{
3573
0
    srtp_err_status_t status;
3574
0
    srtp_xtd_seq_num_t old_index;
3575
0
    srtp_rdb_t old_rtcp_rdb;
3576
0
    srtp_stream_t stream;
3577
3578
0
    status = srtp_valid_policy(policy);
3579
0
    if (status != srtp_err_status_ok) {
3580
0
        return status;
3581
0
    }
3582
3583
0
    stream = srtp_get_stream(session, htonl(policy->ssrc.value));
3584
0
    if (stream == NULL) {
3585
0
        return srtp_err_status_bad_param;
3586
0
    }
3587
3588
0
    status = is_update_policy_compatable(stream, policy);
3589
0
    if (status != srtp_err_status_ok) {
3590
0
        return status;
3591
0
    }
3592
3593
    /* save old extendard seq */
3594
0
    old_index = stream->rtp_rdbx.index;
3595
0
    old_rtcp_rdb = stream->rtcp_rdb;
3596
3597
0
    status = srtp_stream_remove(session, policy->ssrc.value);
3598
0
    if (status) {
3599
0
        return status;
3600
0
    }
3601
3602
0
    status = srtp_stream_add(session, policy);
3603
0
    if (status) {
3604
0
        return status;
3605
0
    }
3606
3607
0
    stream = srtp_get_stream(session, htonl(policy->ssrc.value));
3608
0
    if (stream == NULL) {
3609
0
        return srtp_err_status_fail;
3610
0
    }
3611
3612
    /* restore old extended seq */
3613
0
    stream->rtp_rdbx.index = old_index;
3614
0
    stream->rtcp_rdb = old_rtcp_rdb;
3615
3616
0
    return srtp_err_status_ok;
3617
0
}
3618
3619
srtp_err_status_t srtp_stream_update(srtp_t session,
3620
                                     const srtp_policy_t *policy)
3621
0
{
3622
0
    srtp_err_status_t status;
3623
3624
    /* sanity check arguments */
3625
0
    if (session == NULL) {
3626
0
        return srtp_err_status_bad_param;
3627
0
    }
3628
3629
0
    status = srtp_valid_policy(policy);
3630
0
    if (status != srtp_err_status_ok) {
3631
0
        return status;
3632
0
    }
3633
3634
0
    switch (policy->ssrc.type) {
3635
0
    case (ssrc_any_outbound):
3636
0
    case (ssrc_any_inbound):
3637
0
        status = update_template_streams(session, policy);
3638
0
        break;
3639
0
    case (ssrc_specific):
3640
0
        status = stream_update(session, policy);
3641
0
        break;
3642
0
    case (ssrc_undefined):
3643
0
    default:
3644
0
        return srtp_err_status_bad_param;
3645
0
    }
3646
3647
0
    return status;
3648
0
}
3649
3650
/*
3651
 * The default policy - provides a convenient way for callers to use
3652
 * the default security policy
3653
 *
3654
 * The default policy is defined in RFC 3711
3655
 * (Section 5. Default and mandatory-to-implement Transforms)
3656
 *
3657
 */
3658
3659
/*
3660
 * NOTE: cipher_key_len is really key len (128 bits) plus salt len
3661
 *  (112 bits)
3662
 */
3663
/* There are hard-coded 16's for base_key_len in the key generation code */
3664
3665
void srtp_crypto_policy_set_rtp_default(srtp_crypto_policy_t *p)
3666
19.4k
{
3667
19.4k
    p->cipher_type = SRTP_AES_ICM_128;
3668
19.4k
    p->cipher_key_len =
3669
19.4k
        SRTP_AES_ICM_128_KEY_LEN_WSALT; /* default 128 bits per RFC 3711 */
3670
19.4k
    p->auth_type = SRTP_HMAC_SHA1;
3671
19.4k
    p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
3672
19.4k
    p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
3673
19.4k
    p->sec_serv = sec_serv_conf_and_auth;
3674
19.4k
}
3675
3676
void srtp_crypto_policy_set_rtcp_default(srtp_crypto_policy_t *p)
3677
17.8k
{
3678
17.8k
    p->cipher_type = SRTP_AES_ICM_128;
3679
17.8k
    p->cipher_key_len =
3680
17.8k
        SRTP_AES_ICM_128_KEY_LEN_WSALT; /* default 128 bits per RFC 3711 */
3681
17.8k
    p->auth_type = SRTP_HMAC_SHA1;
3682
17.8k
    p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
3683
17.8k
    p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
3684
17.8k
    p->sec_serv = sec_serv_conf_and_auth;
3685
17.8k
}
3686
3687
void srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(srtp_crypto_policy_t *p)
3688
828
{
3689
    /*
3690
     * corresponds to RFC 4568
3691
     *
3692
     * note that this crypto policy is intended for SRTP, but not SRTCP
3693
     */
3694
3695
828
    p->cipher_type = SRTP_AES_ICM_128;
3696
828
    p->cipher_key_len =
3697
828
        SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */
3698
828
    p->auth_type = SRTP_HMAC_SHA1;
3699
828
    p->auth_key_len = 20; /* 160 bit key               */
3700
828
    p->auth_tag_len = 4;  /* 32 bit tag                */
3701
828
    p->sec_serv = sec_serv_conf_and_auth;
3702
828
}
3703
3704
void srtp_crypto_policy_set_aes_cm_128_null_auth(srtp_crypto_policy_t *p)
3705
1.34k
{
3706
    /*
3707
     * corresponds to RFC 4568
3708
     *
3709
     * note that this crypto policy is intended for SRTP, but not SRTCP
3710
     */
3711
3712
1.34k
    p->cipher_type = SRTP_AES_ICM_128;
3713
1.34k
    p->cipher_key_len =
3714
1.34k
        SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */
3715
1.34k
    p->auth_type = SRTP_NULL_AUTH;
3716
1.34k
    p->auth_key_len = 0;
3717
1.34k
    p->auth_tag_len = 0;
3718
1.34k
    p->sec_serv = sec_serv_conf;
3719
1.34k
}
3720
3721
void srtp_crypto_policy_set_null_cipher_hmac_sha1_80(srtp_crypto_policy_t *p)
3722
524
{
3723
    /*
3724
     * corresponds to RFC 4568
3725
     */
3726
3727
524
    p->cipher_type = SRTP_NULL_CIPHER;
3728
524
    p->cipher_key_len =
3729
524
        SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */
3730
524
    p->auth_type = SRTP_HMAC_SHA1;
3731
524
    p->auth_key_len = 20;
3732
524
    p->auth_tag_len = 10;
3733
524
    p->sec_serv = sec_serv_auth;
3734
524
}
3735
3736
void srtp_crypto_policy_set_null_cipher_hmac_null(srtp_crypto_policy_t *p)
3737
22.7k
{
3738
    /*
3739
     * Should only be used for testing
3740
     */
3741
3742
22.7k
    p->cipher_type = SRTP_NULL_CIPHER;
3743
22.7k
    p->cipher_key_len = 0;
3744
22.7k
    p->auth_type = SRTP_NULL_AUTH;
3745
22.7k
    p->auth_key_len = 0;
3746
22.7k
    p->auth_tag_len = 0;
3747
22.7k
    p->sec_serv = sec_serv_none;
3748
22.7k
}
3749
3750
void srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(srtp_crypto_policy_t *p)
3751
970
{
3752
    /*
3753
     * corresponds to RFC 6188
3754
     */
3755
3756
970
    p->cipher_type = SRTP_AES_ICM_256;
3757
970
    p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT;
3758
970
    p->auth_type = SRTP_HMAC_SHA1;
3759
970
    p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
3760
970
    p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
3761
970
    p->sec_serv = sec_serv_conf_and_auth;
3762
970
}
3763
3764
void srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(srtp_crypto_policy_t *p)
3765
758
{
3766
    /*
3767
     * corresponds to RFC 6188
3768
     *
3769
     * note that this crypto policy is intended for SRTP, but not SRTCP
3770
     */
3771
3772
758
    p->cipher_type = SRTP_AES_ICM_256;
3773
758
    p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT;
3774
758
    p->auth_type = SRTP_HMAC_SHA1;
3775
758
    p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
3776
758
    p->auth_tag_len = 4;  /* default 80 bits per RFC 3711 */
3777
758
    p->sec_serv = sec_serv_conf_and_auth;
3778
758
}
3779
3780
/*
3781
 * AES-256 with no authentication.
3782
 */
3783
void srtp_crypto_policy_set_aes_cm_256_null_auth(srtp_crypto_policy_t *p)
3784
856
{
3785
856
    p->cipher_type = SRTP_AES_ICM_256;
3786
856
    p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT;
3787
856
    p->auth_type = SRTP_NULL_AUTH;
3788
856
    p->auth_key_len = 0;
3789
856
    p->auth_tag_len = 0;
3790
856
    p->sec_serv = sec_serv_conf;
3791
856
}
3792
3793
void srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(srtp_crypto_policy_t *p)
3794
0
{
3795
    /*
3796
     * corresponds to RFC 6188
3797
     */
3798
3799
0
    p->cipher_type = SRTP_AES_ICM_192;
3800
0
    p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT;
3801
0
    p->auth_type = SRTP_HMAC_SHA1;
3802
0
    p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
3803
0
    p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
3804
0
    p->sec_serv = sec_serv_conf_and_auth;
3805
0
}
3806
3807
void srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(srtp_crypto_policy_t *p)
3808
0
{
3809
    /*
3810
     * corresponds to RFC 6188
3811
     *
3812
     * note that this crypto policy is intended for SRTP, but not SRTCP
3813
     */
3814
3815
0
    p->cipher_type = SRTP_AES_ICM_192;
3816
0
    p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT;
3817
0
    p->auth_type = SRTP_HMAC_SHA1;
3818
0
    p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
3819
0
    p->auth_tag_len = 4;  /* default 80 bits per RFC 3711 */
3820
0
    p->sec_serv = sec_serv_conf_and_auth;
3821
0
}
3822
3823
/*
3824
 * AES-192 with no authentication.
3825
 */
3826
void srtp_crypto_policy_set_aes_cm_192_null_auth(srtp_crypto_policy_t *p)
3827
0
{
3828
0
    p->cipher_type = SRTP_AES_ICM_192;
3829
0
    p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT;
3830
0
    p->auth_type = SRTP_NULL_AUTH;
3831
0
    p->auth_key_len = 0;
3832
0
    p->auth_tag_len = 0;
3833
0
    p->sec_serv = sec_serv_conf;
3834
0
}
3835
3836
/*
3837
 * AES-128 GCM mode with 16 octet auth tag.
3838
 */
3839
void srtp_crypto_policy_set_aes_gcm_128_16_auth(srtp_crypto_policy_t *p)
3840
0
{
3841
0
    p->cipher_type = SRTP_AES_GCM_128;
3842
0
    p->cipher_key_len = SRTP_AES_GCM_128_KEY_LEN_WSALT;
3843
0
    p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
3844
0
    p->auth_key_len = 0;
3845
0
    p->auth_tag_len = 16; /* 16 octet tag length */
3846
0
    p->sec_serv = sec_serv_conf_and_auth;
3847
0
}
3848
3849
/*
3850
 * AES-256 GCM mode with 16 octet auth tag.
3851
 */
3852
void srtp_crypto_policy_set_aes_gcm_256_16_auth(srtp_crypto_policy_t *p)
3853
0
{
3854
0
    p->cipher_type = SRTP_AES_GCM_256;
3855
0
    p->cipher_key_len = SRTP_AES_GCM_256_KEY_LEN_WSALT;
3856
0
    p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
3857
0
    p->auth_key_len = 0;
3858
0
    p->auth_tag_len = 16; /* 16 octet tag length */
3859
0
    p->sec_serv = sec_serv_conf_and_auth;
3860
0
}
3861
3862
/*
3863
 * secure rtcp functions
3864
 */
3865
3866
/*
3867
 * AEAD uses a new IV formation method.  This function implements
3868
 * section 9.1 (SRTCP IV Formation for AES-GCM) from RFC7714.
3869
 * The calculation is defined as, where (+) is the xor operation:
3870
 *
3871
 *                0  1  2  3  4  5  6  7  8  9 10 11
3872
 *               +--+--+--+--+--+--+--+--+--+--+--+--+
3873
 *               |00|00|    SSRC   |00|00|0+SRTCP Idx|---+
3874
 *               +--+--+--+--+--+--+--+--+--+--+--+--+   |
3875
 *                                                       |
3876
 *               +--+--+--+--+--+--+--+--+--+--+--+--+   |
3877
 *               |         Encryption Salt           |->(+)
3878
 *               +--+--+--+--+--+--+--+--+--+--+--+--+   |
3879
 *                                                       |
3880
 *               +--+--+--+--+--+--+--+--+--+--+--+--+   |
3881
 *               |       Initialization Vector       |<--+
3882
 *               +--+--+--+--+--+--+--+--+--+--+--+--+*
3883
 *
3884
 * Input:  *session_keys - pointer to SRTP stream context session keys,
3885
 *                        used to retrieve the SALT
3886
 *         *iv           - Pointer to recieve the calculated IV
3887
 *         seq_num       - The SEQ value to use for the IV calculation.
3888
 *         *hdr          - The RTP header, used to get the SSRC value
3889
 *
3890
 * Returns: srtp_err_status_ok if no error or srtp_err_status_bad_param
3891
 *          if seq_num is invalid
3892
 *
3893
 */
3894
static srtp_err_status_t srtp_calc_aead_iv_srtcp(
3895
    srtp_session_keys_t *session_keys,
3896
    v128_t *iv,
3897
    uint32_t seq_num,
3898
    const srtcp_hdr_t *hdr)
3899
0
{
3900
0
    v128_t in;
3901
0
    v128_t salt;
3902
3903
0
    memset(&in, 0, sizeof(v128_t));
3904
0
    memset(&salt, 0, sizeof(v128_t));
3905
3906
0
    in.v16[0] = 0;
3907
0
    memcpy(&in.v16[1], &hdr->ssrc, 4); /* still in network order! */
3908
0
    in.v16[3] = 0;
3909
3910
    /*
3911
     *  The SRTCP index (seq_num) spans bits 0 through 30 inclusive.
3912
     *  The most significant bit should be zero.
3913
     */
3914
0
    if (seq_num & 0x80000000UL) {
3915
0
        return srtp_err_status_bad_param;
3916
0
    }
3917
0
    in.v32[2] = htonl(seq_num);
3918
3919
0
    debug_print(mod_srtp, "Pre-salted RTCP IV = %s\n", v128_hex_string(&in));
3920
3921
    /*
3922
     * Get the SALT value from the context
3923
     */
3924
0
    memcpy(salt.v8, session_keys->c_salt, 12);
3925
0
    debug_print(mod_srtp, "RTCP SALT = %s\n", v128_hex_string(&salt));
3926
3927
    /*
3928
     * Finally, apply the SALT to the input
3929
     */
3930
0
    v128_xor(iv, &in, &salt);
3931
3932
0
    return srtp_err_status_ok;
3933
0
}
3934
3935
/*
3936
 * This code handles AEAD ciphers for outgoing RTCP.  We currently support
3937
 * AES-GCM mode with 128 or 256 bit keys.
3938
 */
3939
static srtp_err_status_t srtp_protect_rtcp_aead(
3940
    srtp_stream_ctx_t *stream,
3941
    const uint8_t *rtcp,
3942
    size_t rtcp_len,
3943
    uint8_t *srtcp,
3944
    size_t *srtcp_len,
3945
    srtp_session_keys_t *session_keys)
3946
0
{
3947
0
    const srtcp_hdr_t *hdr = (const srtcp_hdr_t *)rtcp;
3948
0
    size_t enc_start;         /* pointer to start of encrypted portion  */
3949
0
    uint8_t *trailer_p;       /* pointer to start of trailer            */
3950
0
    uint32_t trailer;         /* trailer value                          */
3951
0
    size_t enc_octet_len = 0; /* number of octets in encrypted portion  */
3952
0
    srtp_err_status_t status;
3953
0
    size_t tag_len;
3954
0
    uint32_t seq_num;
3955
0
    v128_t iv;
3956
3957
    /* get tag length from stream context */
3958
0
    tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth);
3959
3960
    /*
3961
     * set encryption start and encryption length - if we're not
3962
     * providing confidentiality, set enc_start to NULL
3963
     */
3964
0
    enc_start = octets_in_rtcp_header;
3965
0
    enc_octet_len = rtcp_len - enc_start;
3966
3967
    /* check output length */
3968
0
    if (*srtcp_len <
3969
0
        rtcp_len + sizeof(srtcp_trailer_t) + stream->mki_size + tag_len) {
3970
0
        return srtp_err_status_buffer_small;
3971
0
    }
3972
3973
    /* if not-inplace then need to copy full rtcp header */
3974
0
    if (rtcp != srtcp) {
3975
0
        memcpy(srtcp, rtcp, enc_start);
3976
0
    }
3977
3978
    /* NOTE: hdr->length is not usable - it refers to only the first
3979
     * RTCP report in the compound packet!
3980
     */
3981
0
    trailer_p = srtcp + enc_start + enc_octet_len + tag_len;
3982
3983
0
    if (stream->rtcp_services & sec_serv_conf) {
3984
0
        trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */
3985
0
    } else {
3986
        /* 0 is network-order independent */
3987
0
        trailer = 0x00000000; /* set encrypt bit */
3988
0
    }
3989
3990
0
    if (stream->use_mki) {
3991
0
        srtp_inject_mki(srtcp + rtcp_len + tag_len + sizeof(srtcp_trailer_t),
3992
0
                        session_keys, stream->mki_size);
3993
0
    }
3994
3995
    /*
3996
     * check sequence number for overruns, and copy it into the packet
3997
     * if its value isn't too big
3998
     */
3999
0
    status = srtp_rdb_increment(&stream->rtcp_rdb);
4000
0
    if (status) {
4001
0
        return status;
4002
0
    }
4003
0
    seq_num = srtp_rdb_get_value(&stream->rtcp_rdb);
4004
0
    trailer |= htonl(seq_num);
4005
0
    debug_print(mod_srtp, "srtcp index: %x", (unsigned int)seq_num);
4006
4007
0
    memcpy(trailer_p, &trailer, sizeof(trailer));
4008
4009
    /*
4010
     * Calculate and set the IV
4011
     */
4012
0
    status = srtp_calc_aead_iv_srtcp(session_keys, &iv, seq_num, hdr);
4013
0
    if (status) {
4014
0
        return srtp_err_status_cipher_fail;
4015
0
    }
4016
0
    status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv,
4017
0
                                srtp_direction_encrypt);
4018
0
    if (status) {
4019
0
        return srtp_err_status_cipher_fail;
4020
0
    }
4021
4022
    /*
4023
     * Set the AAD for GCM mode
4024
     */
4025
0
    if (stream->rtcp_services & sec_serv_conf) {
4026
        /*
4027
         * If payload encryption is enabled, then the AAD consist of
4028
         * the RTCP header and the seq# at the end of the packet
4029
         */
4030
0
        status = srtp_cipher_set_aad(session_keys->rtcp_cipher, rtcp,
4031
0
                                     octets_in_rtcp_header);
4032
0
        if (status) {
4033
0
            return srtp_err_status_cipher_fail;
4034
0
        }
4035
0
    } else {
4036
        /*
4037
         * Since payload encryption is not enabled, we must authenticate
4038
         * the entire packet as described in RFC 7714 (Section 9.3. Data
4039
         * Types in Unencrypted SRTCP Compound Packets)
4040
         */
4041
0
        status = srtp_cipher_set_aad(session_keys->rtcp_cipher, rtcp, rtcp_len);
4042
0
        if (status) {
4043
0
            return (srtp_err_status_cipher_fail);
4044
0
        }
4045
0
    }
4046
    /*
4047
     * Process the sequence# as AAD
4048
     */
4049
0
    status = srtp_cipher_set_aad(session_keys->rtcp_cipher, (uint8_t *)&trailer,
4050
0
                                 sizeof(trailer));
4051
0
    if (status) {
4052
0
        return (srtp_err_status_cipher_fail);
4053
0
    }
4054
4055
    /* if we're encrypting, exor keystream into the message */
4056
0
    if (stream->rtcp_services & sec_serv_conf) {
4057
0
        size_t out_len = *srtcp_len - enc_start;
4058
0
        status =
4059
0
            srtp_cipher_encrypt(session_keys->rtcp_cipher, rtcp + enc_start,
4060
0
                                enc_octet_len, srtcp + enc_start, &out_len);
4061
0
        enc_octet_len = out_len;
4062
0
        if (status) {
4063
0
            return srtp_err_status_cipher_fail;
4064
0
        }
4065
0
    } else {
4066
        /* if no encryption and not-inplace then need to copy rest of packet */
4067
0
        if (rtcp != srtcp) {
4068
0
            memcpy(srtcp + enc_start, rtcp + enc_start, enc_octet_len);
4069
0
        }
4070
4071
        /*
4072
         * Even though we're not encrypting the payload, we need
4073
         * to run the cipher to get the auth tag.
4074
         */
4075
0
        uint8_t *auth_tag = srtcp + enc_start + enc_octet_len;
4076
0
        size_t out_len = *srtcp_len - enc_start - enc_octet_len;
4077
0
        status = srtp_cipher_encrypt(session_keys->rtcp_cipher, NULL, 0,
4078
0
                                     auth_tag, &out_len);
4079
0
        if (status) {
4080
0
            return srtp_err_status_cipher_fail;
4081
0
        }
4082
0
        enc_octet_len += out_len;
4083
0
    }
4084
4085
0
    *srtcp_len = octets_in_rtcp_header + enc_octet_len;
4086
4087
    /* increase the packet length by the length of the seq_num*/
4088
0
    *srtcp_len += sizeof(srtcp_trailer_t);
4089
4090
    /* increase the packet by the mki_size */
4091
0
    *srtcp_len += stream->mki_size;
4092
4093
0
    return srtp_err_status_ok;
4094
0
}
4095
4096
/*
4097
 * This function handles incoming SRTCP packets while in AEAD mode,
4098
 * which currently supports AES-GCM encryption.  Note, the auth tag is
4099
 * at the end of the packet stream and is automatically checked by GCM
4100
 * when decrypting the payload.
4101
 */
4102
static srtp_err_status_t srtp_unprotect_rtcp_aead(
4103
    srtp_t ctx,
4104
    srtp_stream_ctx_t *stream,
4105
    const uint8_t *srtcp,
4106
    size_t srtcp_len,
4107
    uint8_t *rtcp,
4108
    size_t *rtcp_len,
4109
    srtp_session_keys_t *session_keys)
4110
0
{
4111
0
    const srtcp_hdr_t *hdr = (const srtcp_hdr_t *)srtcp;
4112
0
    size_t enc_start;               /* pointer to start of encrypted portion  */
4113
0
    const uint8_t *trailer_p;       /* pointer to start of trailer            */
4114
0
    uint32_t trailer;               /* trailer value                          */
4115
0
    size_t enc_octet_len = 0;       /* number of octets in encrypted portion  */
4116
0
    const uint8_t *auth_tag = NULL; /* location of auth_tag within packet     */
4117
0
    srtp_err_status_t status;
4118
0
    size_t tag_len;
4119
0
    size_t tmp_len;
4120
0
    uint32_t seq_num;
4121
0
    v128_t iv;
4122
4123
    /* get tag length from stream context */
4124
0
    tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth);
4125
4126
0
    enc_start = octets_in_rtcp_header;
4127
4128
    /*
4129
     * set encryption start, encryption length, and trailer
4130
     */
4131
    /* index & E (encryption) bit follow normal data. hdr->len is the number of
4132
     * words (32-bit) in the normal packet minus 1
4133
     */
4134
    /* This should point trailer to the word past the end of the normal data. */
4135
    /* This would need to be modified for optional mikey data */
4136
0
    trailer_p = srtcp + srtcp_len - sizeof(srtcp_trailer_t) - stream->mki_size;
4137
0
    memcpy(&trailer, trailer_p, sizeof(trailer));
4138
4139
    /*
4140
     * We pass the tag down to the cipher when doing GCM mode
4141
     */
4142
0
    enc_octet_len = srtcp_len - (octets_in_rtcp_header +
4143
0
                                 sizeof(srtcp_trailer_t) + stream->mki_size);
4144
0
    auth_tag = srtcp + (srtcp_len - tag_len - stream->mki_size -
4145
0
                        sizeof(srtcp_trailer_t));
4146
4147
    /*
4148
     * check the sequence number for replays
4149
     */
4150
    /* this is easier than dealing with bitfield access */
4151
0
    seq_num = ntohl(trailer) & SRTCP_INDEX_MASK;
4152
0
    debug_print(mod_srtp, "srtcp index: %x", (unsigned int)seq_num);
4153
0
    status = srtp_rdb_check(&stream->rtcp_rdb, seq_num);
4154
0
    if (status) {
4155
0
        return status;
4156
0
    }
4157
4158
    /*
4159
     * Calculate and set the IV
4160
     */
4161
0
    status = srtp_calc_aead_iv_srtcp(session_keys, &iv, seq_num, hdr);
4162
0
    if (status) {
4163
0
        return srtp_err_status_cipher_fail;
4164
0
    }
4165
0
    status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv,
4166
0
                                srtp_direction_decrypt);
4167
0
    if (status) {
4168
0
        return srtp_err_status_cipher_fail;
4169
0
    }
4170
4171
    /* check output length */
4172
0
    if (*rtcp_len <
4173
0
        srtcp_len - sizeof(srtcp_trailer_t) - stream->mki_size - tag_len) {
4174
0
        return srtp_err_status_buffer_small;
4175
0
    }
4176
4177
    /* if not inplace need to copy rtcp header */
4178
0
    if (srtcp != rtcp) {
4179
0
        memcpy(rtcp, srtcp, enc_start);
4180
0
    }
4181
4182
    /*
4183
     * Set the AAD for GCM mode
4184
     */
4185
0
    if (*trailer_p & SRTCP_E_BYTE_BIT) {
4186
        /*
4187
         * If payload encryption is enabled, then the AAD consist of
4188
         * the RTCP header and the seq# at the end of the packet
4189
         */
4190
0
        status = srtp_cipher_set_aad(session_keys->rtcp_cipher, srtcp,
4191
0
                                     octets_in_rtcp_header);
4192
0
        if (status) {
4193
0
            return srtp_err_status_cipher_fail;
4194
0
        }
4195
0
    } else {
4196
        /*
4197
         * Since payload encryption is not enabled, we must authenticate
4198
         * the entire packet as described in RFC 7714 (Section 9.3. Data
4199
         * Types in Unencrypted SRTCP Compound Packets)
4200
         */
4201
0
        status = srtp_cipher_set_aad(
4202
0
            session_keys->rtcp_cipher, srtcp,
4203
0
            (srtcp_len - tag_len - sizeof(srtcp_trailer_t) - stream->mki_size));
4204
0
        if (status) {
4205
0
            return (srtp_err_status_cipher_fail);
4206
0
        }
4207
0
    }
4208
4209
    /*
4210
     * Process the sequence# as AAD
4211
     */
4212
0
    status = srtp_cipher_set_aad(session_keys->rtcp_cipher, (uint8_t *)&trailer,
4213
0
                                 sizeof(trailer));
4214
0
    if (status) {
4215
0
        return (srtp_err_status_cipher_fail);
4216
0
    }
4217
4218
    /* if we're decrypting, exor keystream into the message */
4219
0
    if (*trailer_p & SRTCP_E_BYTE_BIT) {
4220
0
        status = srtp_cipher_decrypt(session_keys->rtcp_cipher,
4221
0
                                     srtcp + enc_start, enc_octet_len,
4222
0
                                     rtcp + enc_start, &enc_octet_len);
4223
0
        if (status) {
4224
0
            return status;
4225
0
        }
4226
0
    } else {
4227
        /* if no encryption and not-inplace then need to copy rest of packet */
4228
0
        if (rtcp != srtcp) {
4229
0
            memcpy(rtcp + enc_start, srtcp + enc_start, enc_octet_len);
4230
0
        }
4231
4232
        /*
4233
         * Still need to run the cipher to check the tag
4234
         */
4235
0
        tmp_len = 0;
4236
0
        status = srtp_cipher_decrypt(session_keys->rtcp_cipher, auth_tag,
4237
0
                                     tag_len, NULL, &tmp_len);
4238
0
        if (status) {
4239
0
            return status;
4240
0
        }
4241
0
    }
4242
4243
0
    *rtcp_len = srtcp_len;
4244
4245
    /* decrease the packet length by the length of the auth tag and seq_num*/
4246
0
    *rtcp_len -= (tag_len + sizeof(srtcp_trailer_t) + stream->mki_size);
4247
4248
    /*
4249
     * verify that stream is for received traffic - this check will
4250
     * detect SSRC collisions, since a stream that appears in both
4251
     * srtp_protect() and srtp_unprotect() will fail this test in one of
4252
     * those functions.
4253
     *
4254
     * we do this check *after* the authentication check, so that the
4255
     * latter check will catch any attempts to fool us into thinking
4256
     * that we've got a collision
4257
     */
4258
0
    if (stream->direction != dir_srtp_receiver) {
4259
0
        if (stream->direction == dir_unknown) {
4260
0
            stream->direction = dir_srtp_receiver;
4261
0
        } else {
4262
0
            srtp_handle_event(ctx, stream, event_ssrc_collision);
4263
0
        }
4264
0
    }
4265
4266
    /*
4267
     * if the stream is a 'provisional' one, in which the template context
4268
     * is used, then we need to allocate a new stream at this point, since
4269
     * the authentication passed
4270
     */
4271
0
    if (stream == ctx->stream_template) {
4272
0
        srtp_stream_ctx_t *new_stream;
4273
4274
        /*
4275
         * allocate and initialize a new stream
4276
         *
4277
         * note that we indicate failure if we can't allocate the new
4278
         * stream, and some implementations will want to not return
4279
         * failure here
4280
         */
4281
0
        status =
4282
0
            srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
4283
0
        if (status) {
4284
0
            return status;
4285
0
        }
4286
4287
        /* add new stream to the list */
4288
0
        status = srtp_insert_or_dealloc_stream(ctx->stream_list, new_stream,
4289
0
                                               ctx->stream_template);
4290
0
        if (status) {
4291
0
            return status;
4292
0
        }
4293
4294
        /* set stream (the pointer used in this function) */
4295
0
        stream = new_stream;
4296
0
    }
4297
4298
    /* we've passed the authentication check, so add seq_num to the rdb */
4299
0
    srtp_rdb_add_index(&stream->rtcp_rdb, seq_num);
4300
4301
0
    return srtp_err_status_ok;
4302
0
}
4303
4304
srtp_err_status_t srtp_protect_rtcp(srtp_t ctx,
4305
                                    const uint8_t *rtcp,
4306
                                    size_t rtcp_len,
4307
                                    uint8_t *srtcp,
4308
                                    size_t *srtcp_len,
4309
                                    size_t mki_index)
4310
105k
{
4311
105k
    const srtcp_hdr_t *hdr = (const srtcp_hdr_t *)rtcp;
4312
105k
    size_t enc_start;         /* pointer to start of encrypted portion  */
4313
105k
    uint8_t *auth_start;      /* pointer to start of auth. portion      */
4314
105k
    uint8_t *trailer_p;       /* pointer to start of trailer            */
4315
105k
    uint32_t trailer;         /* trailer value                          */
4316
105k
    size_t enc_octet_len = 0; /* number of octets in encrypted portion  */
4317
105k
    uint8_t *auth_tag = NULL; /* location of auth_tag within packet     */
4318
105k
    srtp_err_status_t status;
4319
105k
    size_t tag_len;
4320
105k
    srtp_stream_ctx_t *stream;
4321
105k
    size_t prefix_len;
4322
105k
    uint32_t seq_num;
4323
105k
    srtp_session_keys_t *session_keys = NULL;
4324
4325
    /* check the packet length - it must at least contain a full header */
4326
105k
    if (rtcp_len < octets_in_rtcp_header) {
4327
10
        return srtp_err_status_bad_param;
4328
10
    }
4329
4330
    /*
4331
     * look up ssrc in srtp_stream list, and process the packet with
4332
     * the appropriate stream.  if we haven't seen this stream before,
4333
     * there's only one key for this srtp_session, and the cipher
4334
     * supports key-sharing, then we assume that a new stream using
4335
     * that key has just started up
4336
     */
4337
105k
    stream = srtp_get_stream(ctx, hdr->ssrc);
4338
105k
    if (stream == NULL) {
4339
4.42k
        if (ctx->stream_template != NULL) {
4340
4.37k
            srtp_stream_ctx_t *new_stream;
4341
4342
            /* allocate and initialize a new stream */
4343
4.37k
            status =
4344
4.37k
                srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
4345
4.37k
            if (status) {
4346
0
                return status;
4347
0
            }
4348
4349
            /* add new stream to the list */
4350
4.37k
            status = srtp_insert_or_dealloc_stream(ctx->stream_list, new_stream,
4351
4.37k
                                                   ctx->stream_template);
4352
4.37k
            if (status) {
4353
0
                return status;
4354
0
            }
4355
4356
            /* set stream (the pointer used in this function) */
4357
4.37k
            stream = new_stream;
4358
4.37k
        } else {
4359
            /* no template stream, so we return an error */
4360
45
            return srtp_err_status_no_ctx;
4361
45
        }
4362
4.42k
    }
4363
4364
    /*
4365
     * verify that stream is for sending traffic - this check will
4366
     * detect SSRC collisions, since a stream that appears in both
4367
     * srtp_protect() and srtp_unprotect() will fail this test in one of
4368
     * those functions.
4369
     */
4370
105k
    if (stream->direction != dir_srtp_sender) {
4371
14.1k
        if (stream->direction == dir_unknown) {
4372
84
            stream->direction = dir_srtp_sender;
4373
14.1k
        } else {
4374
14.1k
            srtp_handle_event(ctx, stream, event_ssrc_collision);
4375
14.1k
        }
4376
14.1k
    }
4377
4378
105k
    status = srtp_get_session_keys(stream, mki_index, &session_keys);
4379
105k
    if (status) {
4380
0
        return status;
4381
0
    }
4382
4383
    /*
4384
     * Check if this is an AEAD stream (GCM mode).  If so, then dispatch
4385
     * the request to our AEAD handler.
4386
     */
4387
105k
    if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 ||
4388
105k
        session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) {
4389
0
        return srtp_protect_rtcp_aead(stream, rtcp, rtcp_len, srtcp, srtcp_len,
4390
0
                                      session_keys);
4391
0
    }
4392
4393
    /* get tag length from stream context */
4394
105k
    tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth);
4395
4396
    /*
4397
     * set encryption start and encryption length
4398
     */
4399
105k
    enc_start = octets_in_rtcp_header;
4400
105k
    enc_octet_len = rtcp_len - enc_start;
4401
4402
    /* check output length */
4403
105k
    if (*srtcp_len <
4404
105k
        rtcp_len + sizeof(srtcp_trailer_t) + stream->mki_size + tag_len) {
4405
0
        return srtp_err_status_buffer_small;
4406
0
    }
4407
4408
    /* if not in place then need to copy rtcp header */
4409
105k
    if (rtcp != srtcp) {
4410
0
        memcpy(srtcp, rtcp, enc_start);
4411
0
    }
4412
4413
    /* all of the packet, except the header, gets encrypted */
4414
    /*
4415
     * NOTE: hdr->length is not usable - it refers to only the first RTCP report
4416
     * in the compound packet!
4417
     */
4418
105k
    trailer_p = srtcp + enc_start + enc_octet_len;
4419
4420
105k
    if (stream->rtcp_services & sec_serv_conf) {
4421
69.8k
        trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */
4422
69.8k
    } else {
4423
        /* 0 is network-order independant */
4424
35.6k
        trailer = 0x00000000; /* set encrypt bit */
4425
35.6k
    }
4426
4427
105k
    if (stream->use_mki) {
4428
0
        srtp_inject_mki(srtcp + rtcp_len + sizeof(srtcp_trailer_t),
4429
0
                        session_keys, stream->mki_size);
4430
0
    }
4431
4432
    /*
4433
     * set the auth_start and auth_tag pointers to the proper locations
4434
     * (note that srtpc *always* provides authentication, unlike srtp)
4435
     */
4436
    /* Note: This would need to change for optional mikey data */
4437
105k
    auth_start = srtcp;
4438
105k
    auth_tag = srtcp + rtcp_len + sizeof(srtcp_trailer_t) + stream->mki_size;
4439
4440
    /*
4441
     * check sequence number for overruns, and copy it into the packet
4442
     * if its value isn't too big
4443
     */
4444
105k
    status = srtp_rdb_increment(&stream->rtcp_rdb);
4445
105k
    if (status) {
4446
2
        return status;
4447
2
    }
4448
105k
    seq_num = srtp_rdb_get_value(&stream->rtcp_rdb);
4449
105k
    trailer |= htonl(seq_num);
4450
105k
    debug_print(mod_srtp, "srtcp index: %x", (unsigned int)seq_num);
4451
4452
105k
    memcpy(trailer_p, &trailer, sizeof(trailer));
4453
4454
    /*
4455
     * if we're using rindael counter mode, set nonce and seq
4456
     */
4457
105k
    if (session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_128 ||
4458
36.8k
        session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_192 ||
4459
69.8k
        session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_256) {
4460
69.8k
        v128_t iv;
4461
4462
69.8k
        iv.v32[0] = 0;
4463
69.8k
        iv.v32[1] = hdr->ssrc; /* still in network order! */
4464
69.8k
        iv.v32[2] = htonl(seq_num >> 16);
4465
69.8k
        iv.v32[3] = htonl(seq_num << 16);
4466
69.8k
        status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv,
4467
69.8k
                                    srtp_direction_encrypt);
4468
4469
69.8k
    } else {
4470
35.6k
        v128_t iv;
4471
4472
        /* otherwise, just set the index to seq_num */
4473
35.6k
        iv.v32[0] = 0;
4474
35.6k
        iv.v32[1] = 0;
4475
35.6k
        iv.v32[2] = 0;
4476
35.6k
        iv.v32[3] = htonl(seq_num);
4477
35.6k
        status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv,
4478
35.6k
                                    srtp_direction_encrypt);
4479
35.6k
    }
4480
105k
    if (status) {
4481
0
        return srtp_err_status_cipher_fail;
4482
0
    }
4483
4484
    /*
4485
     * if we're authenticating using a universal hash, put the keystream
4486
     * prefix into the authentication tag
4487
     */
4488
4489
    /* if auth_start is non-null, then put keystream into tag  */
4490
105k
    if (auth_start) {
4491
        /* put keystream prefix into auth_tag */
4492
105k
        prefix_len = srtp_auth_get_prefix_length(session_keys->rtcp_auth);
4493
105k
        status = srtp_cipher_output(session_keys->rtcp_cipher, auth_tag,
4494
105k
                                    &prefix_len);
4495
4496
105k
        debug_print(mod_srtp, "keystream prefix: %s",
4497
105k
                    srtp_octet_string_hex_string(auth_tag, prefix_len));
4498
4499
105k
        if (status) {
4500
0
            return srtp_err_status_cipher_fail;
4501
0
        }
4502
105k
    }
4503
4504
    /* if we're encrypting, exor keystream into the message */
4505
105k
    if (stream->rtcp_services & sec_serv_conf) {
4506
69.8k
        status = srtp_cipher_encrypt(session_keys->rtcp_cipher,
4507
69.8k
                                     rtcp + enc_start, enc_octet_len,
4508
69.8k
                                     srtcp + enc_start, &enc_octet_len);
4509
69.8k
        if (status) {
4510
0
            return srtp_err_status_cipher_fail;
4511
0
        }
4512
69.8k
    } else if (rtcp != srtcp) {
4513
        /* if no encryption and not-inplace then need to copy rest of packet */
4514
0
        memcpy(srtcp + enc_start, rtcp + enc_start, enc_octet_len);
4515
0
    }
4516
4517
    /* initialize auth func context */
4518
105k
    status = srtp_auth_start(session_keys->rtcp_auth);
4519
105k
    if (status) {
4520
0
        return status;
4521
0
    }
4522
4523
    /*
4524
     * run auth func over packet (including trailer), and write the
4525
     * result at auth_tag
4526
     */
4527
105k
    status = srtp_auth_compute(session_keys->rtcp_auth, auth_start,
4528
105k
                               rtcp_len + sizeof(srtcp_trailer_t), auth_tag);
4529
105k
    debug_print(mod_srtp, "srtcp auth tag:    %s",
4530
105k
                srtp_octet_string_hex_string(auth_tag, tag_len));
4531
105k
    if (status) {
4532
0
        return srtp_err_status_auth_fail;
4533
0
    }
4534
4535
105k
    *srtcp_len = enc_start + enc_octet_len;
4536
4537
    /* increase the packet length by the length of the auth tag and seq_num*/
4538
105k
    *srtcp_len += (tag_len + sizeof(srtcp_trailer_t));
4539
4540
    /* increase the packet by the mki_size */
4541
105k
    *srtcp_len += stream->mki_size;
4542
4543
105k
    return srtp_err_status_ok;
4544
105k
}
4545
4546
srtp_err_status_t srtp_unprotect_rtcp(srtp_t ctx,
4547
                                      const uint8_t *srtcp,
4548
                                      size_t srtcp_len,
4549
                                      uint8_t *rtcp,
4550
                                      size_t *rtcp_len)
4551
2.37k
{
4552
2.37k
    const srtcp_hdr_t *hdr = (const srtcp_hdr_t *)srtcp;
4553
2.37k
    size_t enc_start;               /* pointer to start of encrypted portion  */
4554
2.37k
    const uint8_t *auth_start;      /* pointer to start of auth. portion      */
4555
2.37k
    const uint8_t *trailer_p;       /* pointer to start of trailer            */
4556
2.37k
    uint32_t trailer;               /* trailer value                          */
4557
2.37k
    size_t enc_octet_len = 0;       /* number of octets in encrypted portion  */
4558
2.37k
    const uint8_t *auth_tag = NULL; /* location of auth_tag within packet     */
4559
2.37k
    uint8_t tmp_tag[SRTP_MAX_TAG_LEN];
4560
2.37k
    srtp_err_status_t status;
4561
2.37k
    size_t auth_len;
4562
2.37k
    size_t tag_len;
4563
2.37k
    srtp_stream_ctx_t *stream;
4564
2.37k
    size_t prefix_len;
4565
2.37k
    uint32_t seq_num;
4566
2.37k
    bool e_bit_in_packet;          /* E-bit was found in the packet */
4567
2.37k
    bool sec_serv_confidentiality; /* whether confidentiality was requested */
4568
2.37k
    srtp_session_keys_t *session_keys = NULL;
4569
4570
    /*
4571
     * check that the length value is sane; we'll check again once we
4572
     * know the tag length, but we at least want to know that it is
4573
     * a positive value
4574
     */
4575
2.37k
    if (srtcp_len < octets_in_rtcp_header + sizeof(srtcp_trailer_t)) {
4576
14
        return srtp_err_status_bad_param;
4577
14
    }
4578
4579
    /*
4580
     * look up ssrc in srtp_stream list, and process the packet with
4581
     * the appropriate stream.  if we haven't seen this stream before,
4582
     * there's only one key for this srtp_session, and the cipher
4583
     * supports key-sharing, then we assume that a new stream using
4584
     * that key has just started up
4585
     */
4586
2.35k
    stream = srtp_get_stream(ctx, hdr->ssrc);
4587
2.35k
    if (stream == NULL) {
4588
830
        if (ctx->stream_template != NULL) {
4589
792
            stream = ctx->stream_template;
4590
4591
792
            debug_print(mod_srtp,
4592
792
                        "srtcp using provisional stream (SSRC: 0x%08x)",
4593
792
                        (unsigned int)ntohl(hdr->ssrc));
4594
792
        } else {
4595
            /* no template stream, so we return an error */
4596
38
            return srtp_err_status_no_ctx;
4597
38
        }
4598
830
    }
4599
4600
    /*
4601
     * Determine if MKI is being used and what session keys should be used
4602
     */
4603
2.31k
    status = srtp_get_session_keys_for_rtcp_packet(stream, srtcp, srtcp_len,
4604
2.31k
                                                   &session_keys);
4605
2.31k
    if (status) {
4606
0
        return status;
4607
0
    }
4608
4609
    /* get tag length from stream context */
4610
2.31k
    tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth);
4611
4612
    /* check the packet length - it must contain at least a full RTCP
4613
       header, an auth tag (if applicable), and the SRTCP encrypted flag
4614
       and 31-bit index value */
4615
2.31k
    if (srtcp_len < octets_in_rtcp_header + sizeof(srtcp_trailer_t) +
4616
2.31k
                        stream->mki_size + tag_len) {
4617
2
        return srtp_err_status_bad_param;
4618
2
    }
4619
4620
    /*
4621
     * Check if this is an AEAD stream (GCM mode).  If so, then dispatch
4622
     * the request to our AEAD handler.
4623
     */
4624
2.31k
    if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 ||
4625
2.31k
        session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) {
4626
0
        return srtp_unprotect_rtcp_aead(ctx, stream, srtcp, srtcp_len, rtcp,
4627
0
                                        rtcp_len, session_keys);
4628
0
    }
4629
4630
2.31k
    sec_serv_confidentiality = stream->rtcp_services == sec_serv_conf ||
4631
1.78k
                               stream->rtcp_services == sec_serv_conf_and_auth;
4632
4633
    /*
4634
     * set encryption start, encryption length, and trailer
4635
     */
4636
2.31k
    enc_start = octets_in_rtcp_header;
4637
2.31k
    enc_octet_len = srtcp_len - (octets_in_rtcp_header + tag_len +
4638
2.31k
                                 stream->mki_size + sizeof(srtcp_trailer_t));
4639
    /*
4640
     *index & E (encryption) bit follow normal data. hdr->len is the number of
4641
     * words (32-bit) in the normal packet minus 1
4642
     */
4643
    /* This should point trailer to the word past the end of the normal data. */
4644
    /* This would need to be modified for optional mikey data */
4645
2.31k
    trailer_p = srtcp + srtcp_len -
4646
2.31k
                (tag_len + stream->mki_size + sizeof(srtcp_trailer_t));
4647
2.31k
    memcpy(&trailer, trailer_p, sizeof(trailer));
4648
4649
2.31k
    e_bit_in_packet = (*trailer_p & SRTCP_E_BYTE_BIT) == SRTCP_E_BYTE_BIT;
4650
2.31k
    if (e_bit_in_packet != sec_serv_confidentiality) {
4651
34
        return srtp_err_status_cant_check;
4652
34
    }
4653
4654
    /*
4655
     * set the auth_start and auth_tag pointers to the proper locations
4656
     * (note that srtcp *always* uses authentication, unlike srtp)
4657
     */
4658
2.28k
    auth_start = srtcp;
4659
4660
    /*
4661
     * The location of the auth tag in the packet needs to know MKI
4662
     * could be present.  The data needed to calculate the Auth tag
4663
     * must not include the MKI
4664
     */
4665
2.28k
    auth_len = srtcp_len - tag_len - stream->mki_size;
4666
2.28k
    auth_tag = srtcp + auth_len + stream->mki_size;
4667
4668
    /*
4669
     * check the sequence number for replays
4670
     */
4671
    /* this is easier than dealing with bitfield access */
4672
2.28k
    seq_num = ntohl(trailer) & SRTCP_INDEX_MASK;
4673
2.28k
    debug_print(mod_srtp, "srtcp index: %x", (unsigned int)seq_num);
4674
2.28k
    status = srtp_rdb_check(&stream->rtcp_rdb, seq_num);
4675
2.28k
    if (status) {
4676
52
        return status;
4677
52
    }
4678
4679
    /*
4680
     * if we're using aes counter mode, set nonce and seq
4681
     */
4682
2.23k
    if (session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_128 ||
4683
1.80k
        session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_192 ||
4684
1.80k
        session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_256) {
4685
831
        v128_t iv;
4686
4687
831
        iv.v32[0] = 0;
4688
831
        iv.v32[1] = hdr->ssrc; /* still in network order! */
4689
831
        iv.v32[2] = htonl(seq_num >> 16);
4690
831
        iv.v32[3] = htonl(seq_num << 16);
4691
831
        status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv,
4692
831
                                    srtp_direction_decrypt);
4693
4694
1.40k
    } else {
4695
1.40k
        v128_t iv;
4696
4697
        /* otherwise, just set the index to seq_num */
4698
1.40k
        iv.v32[0] = 0;
4699
1.40k
        iv.v32[1] = 0;
4700
1.40k
        iv.v32[2] = 0;
4701
1.40k
        iv.v32[3] = htonl(seq_num);
4702
1.40k
        status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv,
4703
1.40k
                                    srtp_direction_decrypt);
4704
1.40k
    }
4705
2.23k
    if (status) {
4706
0
        return srtp_err_status_cipher_fail;
4707
0
    }
4708
4709
    /*
4710
     * if we're authenticating using a universal hash, put the keystream
4711
     * prefix into the authentication tag
4712
     */
4713
2.23k
    prefix_len = srtp_auth_get_prefix_length(session_keys->rtcp_auth);
4714
2.23k
    if (prefix_len) {
4715
0
        status =
4716
0
            srtp_cipher_output(session_keys->rtcp_cipher, tmp_tag, &prefix_len);
4717
0
        debug_print(mod_srtp, "keystream prefix: %s",
4718
0
                    srtp_octet_string_hex_string(tmp_tag, prefix_len));
4719
0
        if (status) {
4720
0
            return srtp_err_status_cipher_fail;
4721
0
        }
4722
0
    }
4723
4724
    /* initialize auth func context */
4725
2.23k
    status = srtp_auth_start(session_keys->rtcp_auth);
4726
2.23k
    if (status) {
4727
0
        return status;
4728
0
    }
4729
4730
    /* run auth func over packet, put result into tmp_tag */
4731
2.23k
    status = srtp_auth_compute(session_keys->rtcp_auth, auth_start, auth_len,
4732
2.23k
                               tmp_tag);
4733
2.23k
    debug_print(mod_srtp, "srtcp computed tag:       %s",
4734
2.23k
                srtp_octet_string_hex_string(tmp_tag, tag_len));
4735
2.23k
    if (status) {
4736
0
        return srtp_err_status_auth_fail;
4737
0
    }
4738
4739
    /* compare the tag just computed with the one in the packet */
4740
2.23k
    debug_print(mod_srtp, "srtcp tag from packet:    %s",
4741
2.23k
                srtp_octet_string_hex_string(auth_tag, tag_len));
4742
2.23k
    if (!srtp_octet_string_equal(tmp_tag, auth_tag, tag_len)) {
4743
13
        return srtp_err_status_auth_fail;
4744
13
    }
4745
4746
    /* check output length */
4747
2.21k
    if (*rtcp_len <
4748
2.21k
        srtcp_len - sizeof(srtcp_trailer_t) - stream->mki_size - tag_len) {
4749
0
        return srtp_err_status_buffer_small;
4750
0
    }
4751
4752
    /* if not inplace need to copy rtcp header */
4753
2.21k
    if (srtcp != rtcp) {
4754
0
        memcpy(rtcp, srtcp, enc_start);
4755
0
    }
4756
4757
    /* if we're decrypting, exor keystream into the message */
4758
2.21k
    if (sec_serv_confidentiality) {
4759
821
        status = srtp_cipher_decrypt(session_keys->rtcp_cipher,
4760
821
                                     srtcp + enc_start, enc_octet_len,
4761
821
                                     rtcp + enc_start, &enc_octet_len);
4762
821
        if (status) {
4763
0
            return srtp_err_status_cipher_fail;
4764
0
        }
4765
1.39k
    } else if (srtcp != rtcp) {
4766
        /* if no encryption and not-inplace then need to copy rest of packet */
4767
0
        memcpy(rtcp + enc_start, srtcp + enc_start, enc_octet_len);
4768
0
    }
4769
4770
2.21k
    *rtcp_len = srtcp_len;
4771
4772
    /* decrease the packet length by the length of the auth tag and seq_num */
4773
2.21k
    *rtcp_len -= (tag_len + sizeof(srtcp_trailer_t));
4774
4775
    /* decrease the packet length by the length of the mki_size */
4776
2.21k
    *rtcp_len -= stream->mki_size;
4777
4778
    /*
4779
     * verify that stream is for received traffic - this check will
4780
     * detect SSRC collisions, since a stream that appears in both
4781
     * srtp_protect() and srtp_unprotect() will fail this test in one of
4782
     * those functions.
4783
     *
4784
     * we do this check *after* the authentication check, so that the
4785
     * latter check will catch any attempts to fool us into thinking
4786
     * that we've got a collision
4787
     */
4788
2.21k
    if (stream->direction != dir_srtp_receiver) {
4789
1.05k
        if (stream->direction == dir_unknown) {
4790
30
            stream->direction = dir_srtp_receiver;
4791
1.02k
        } else {
4792
1.02k
            srtp_handle_event(ctx, stream, event_ssrc_collision);
4793
1.02k
        }
4794
1.05k
    }
4795
4796
    /*
4797
     * if the stream is a 'provisional' one, in which the template context
4798
     * is used, then we need to allocate a new stream at this point, since
4799
     * the authentication passed
4800
     */
4801
2.21k
    if (stream == ctx->stream_template) {
4802
744
        srtp_stream_ctx_t *new_stream;
4803
4804
        /*
4805
         * allocate and initialize a new stream
4806
         *
4807
         * note that we indicate failure if we can't allocate the new
4808
         * stream, and some implementations will want to not return
4809
         * failure here
4810
         */
4811
744
        status =
4812
744
            srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
4813
744
        if (status) {
4814
0
            return status;
4815
0
        }
4816
4817
        /* add new stream to the list */
4818
744
        status = srtp_insert_or_dealloc_stream(ctx->stream_list, new_stream,
4819
744
                                               ctx->stream_template);
4820
744
        if (status) {
4821
0
            return status;
4822
0
        }
4823
4824
        /* set stream (the pointer used in this function) */
4825
744
        stream = new_stream;
4826
744
    }
4827
4828
    /* we've passed the authentication check, so add seq_num to the rdb */
4829
2.21k
    srtp_rdb_add_index(&stream->rtcp_rdb, seq_num);
4830
4831
2.21k
    return srtp_err_status_ok;
4832
2.21k
}
4833
4834
/*
4835
 * user data within srtp_t context
4836
 */
4837
4838
void srtp_set_user_data(srtp_t ctx, void *data)
4839
0
{
4840
0
    ctx->user_data = data;
4841
0
}
4842
4843
void *srtp_get_user_data(srtp_t ctx)
4844
0
{
4845
0
    return ctx->user_data;
4846
0
}
4847
4848
srtp_err_status_t srtp_crypto_policy_set_from_profile_for_rtp(
4849
    srtp_crypto_policy_t *policy,
4850
    srtp_profile_t profile)
4851
0
{
4852
    /* set SRTP policy from the SRTP profile in the key set */
4853
0
    switch (profile) {
4854
0
    case srtp_profile_aes128_cm_sha1_80:
4855
0
        srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
4856
0
        break;
4857
0
    case srtp_profile_aes128_cm_sha1_32:
4858
0
        srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(policy);
4859
0
        break;
4860
0
    case srtp_profile_null_sha1_80:
4861
0
        srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy);
4862
0
        break;
4863
#ifdef GCM
4864
    case srtp_profile_aead_aes_128_gcm:
4865
        srtp_crypto_policy_set_aes_gcm_128_16_auth(policy);
4866
        break;
4867
    case srtp_profile_aead_aes_256_gcm:
4868
        srtp_crypto_policy_set_aes_gcm_256_16_auth(policy);
4869
        break;
4870
#endif
4871
    /* the following profiles are not (yet) supported */
4872
0
    case srtp_profile_null_sha1_32:
4873
0
    default:
4874
0
        return srtp_err_status_bad_param;
4875
0
    }
4876
4877
0
    return srtp_err_status_ok;
4878
0
}
4879
4880
srtp_err_status_t srtp_crypto_policy_set_from_profile_for_rtcp(
4881
    srtp_crypto_policy_t *policy,
4882
    srtp_profile_t profile)
4883
0
{
4884
    /* set SRTP policy from the SRTP profile in the key set */
4885
0
    switch (profile) {
4886
0
    case srtp_profile_aes128_cm_sha1_80:
4887
0
        srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
4888
0
        break;
4889
0
    case srtp_profile_aes128_cm_sha1_32:
4890
        /* We do not honor the 32-bit auth tag request since
4891
         * this is not compliant with RFC 3711 */
4892
0
        srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
4893
0
        break;
4894
0
    case srtp_profile_null_sha1_80:
4895
0
        srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy);
4896
0
        break;
4897
#ifdef GCM
4898
    case srtp_profile_aead_aes_128_gcm:
4899
        srtp_crypto_policy_set_aes_gcm_128_16_auth(policy);
4900
        break;
4901
    case srtp_profile_aead_aes_256_gcm:
4902
        srtp_crypto_policy_set_aes_gcm_256_16_auth(policy);
4903
        break;
4904
#endif
4905
    /* the following profiles are not (yet) supported */
4906
0
    case srtp_profile_null_sha1_32:
4907
0
    default:
4908
0
        return srtp_err_status_bad_param;
4909
0
    }
4910
4911
0
    return srtp_err_status_ok;
4912
0
}
4913
4914
void srtp_append_salt_to_key(uint8_t *key,
4915
                             size_t bytes_in_key,
4916
                             uint8_t *salt,
4917
                             size_t bytes_in_salt)
4918
0
{
4919
0
    memcpy(key + bytes_in_key, salt, bytes_in_salt);
4920
0
}
4921
4922
size_t srtp_profile_get_master_key_length(srtp_profile_t profile)
4923
0
{
4924
0
    switch (profile) {
4925
0
    case srtp_profile_aes128_cm_sha1_80:
4926
0
        return SRTP_AES_128_KEY_LEN;
4927
0
        break;
4928
0
    case srtp_profile_aes128_cm_sha1_32:
4929
0
        return SRTP_AES_128_KEY_LEN;
4930
0
        break;
4931
0
    case srtp_profile_null_sha1_80:
4932
0
        return SRTP_AES_128_KEY_LEN;
4933
0
        break;
4934
0
    case srtp_profile_aead_aes_128_gcm:
4935
0
        return SRTP_AES_128_KEY_LEN;
4936
0
        break;
4937
0
    case srtp_profile_aead_aes_256_gcm:
4938
0
        return SRTP_AES_256_KEY_LEN;
4939
0
        break;
4940
    /* the following profiles are not (yet) supported */
4941
0
    case srtp_profile_null_sha1_32:
4942
0
    default:
4943
0
        return 0; /* indicate error by returning a zero */
4944
0
    }
4945
0
}
4946
4947
size_t srtp_profile_get_master_salt_length(srtp_profile_t profile)
4948
0
{
4949
0
    switch (profile) {
4950
0
    case srtp_profile_aes128_cm_sha1_80:
4951
0
        return SRTP_SALT_LEN;
4952
0
        break;
4953
0
    case srtp_profile_aes128_cm_sha1_32:
4954
0
        return SRTP_SALT_LEN;
4955
0
        break;
4956
0
    case srtp_profile_null_sha1_80:
4957
0
        return SRTP_SALT_LEN;
4958
0
        break;
4959
0
    case srtp_profile_aead_aes_128_gcm:
4960
0
        return SRTP_AEAD_SALT_LEN;
4961
0
        break;
4962
0
    case srtp_profile_aead_aes_256_gcm:
4963
0
        return SRTP_AEAD_SALT_LEN;
4964
0
        break;
4965
    /* the following profiles are not (yet) supported */
4966
0
    case srtp_profile_null_sha1_32:
4967
0
    default:
4968
0
        return 0; /* indicate error by returning a zero */
4969
0
    }
4970
0
}
4971
4972
srtp_err_status_t stream_get_protect_trailer_length(srtp_stream_ctx_t *stream,
4973
                                                    bool is_rtp,
4974
                                                    size_t mki_index,
4975
                                                    size_t *length)
4976
267M
{
4977
267M
    srtp_session_keys_t *session_key;
4978
4979
267M
    *length = 0;
4980
4981
267M
    if (stream->use_mki) {
4982
0
        if (mki_index >= stream->num_master_keys) {
4983
0
            return srtp_err_status_bad_mki;
4984
0
        }
4985
0
        session_key = &stream->session_keys[mki_index];
4986
4987
0
        *length += stream->mki_size;
4988
4989
267M
    } else {
4990
267M
        session_key = &stream->session_keys[0];
4991
267M
    }
4992
267M
    if (is_rtp) {
4993
1.88M
        *length += srtp_auth_get_tag_length(session_key->rtp_auth);
4994
265M
    } else {
4995
265M
        *length += srtp_auth_get_tag_length(session_key->rtcp_auth);
4996
265M
        *length += sizeof(srtcp_trailer_t);
4997
265M
    }
4998
4999
267M
    return srtp_err_status_ok;
5000
267M
}
5001
5002
struct get_protect_trailer_length_data {
5003
    bool found_stream; /* whether at least one matching stream was found */
5004
    size_t length;     /* maximum trailer length found so far */
5005
    bool is_rtp;
5006
    size_t mki_index;
5007
};
5008
5009
static bool get_protect_trailer_length_cb(srtp_stream_t stream, void *raw_data)
5010
267M
{
5011
267M
    struct get_protect_trailer_length_data *data =
5012
267M
        (struct get_protect_trailer_length_data *)raw_data;
5013
267M
    size_t temp_length;
5014
5015
267M
    if (stream_get_protect_trailer_length(stream, data->is_rtp, data->mki_index,
5016
267M
                                          &temp_length) == srtp_err_status_ok) {
5017
267M
        data->found_stream = true;
5018
267M
        if (temp_length > data->length) {
5019
575
            data->length = temp_length;
5020
575
        }
5021
267M
    }
5022
5023
267M
    return true;
5024
267M
}
5025
5026
srtp_err_status_t get_protect_trailer_length(srtp_t session,
5027
                                             bool is_rtp,
5028
                                             size_t mki_index,
5029
                                             size_t *length)
5030
135k
{
5031
135k
    srtp_stream_ctx_t *stream;
5032
135k
    struct get_protect_trailer_length_data data = { false, 0, is_rtp,
5033
135k
                                                    mki_index };
5034
5035
135k
    if (session == NULL) {
5036
0
        return srtp_err_status_bad_param;
5037
0
    }
5038
5039
135k
    stream = session->stream_template;
5040
5041
135k
    if (stream != NULL) {
5042
133k
        data.found_stream = true;
5043
133k
        stream_get_protect_trailer_length(stream, is_rtp, mki_index,
5044
133k
                                          &data.length);
5045
133k
    }
5046
5047
135k
    srtp_stream_list_for_each(session->stream_list,
5048
135k
                              get_protect_trailer_length_cb, &data);
5049
5050
135k
    if (!data.found_stream) {
5051
2
        return srtp_err_status_bad_param;
5052
2
    }
5053
5054
135k
    *length = data.length;
5055
135k
    return srtp_err_status_ok;
5056
135k
}
5057
5058
srtp_err_status_t srtp_get_protect_trailer_length(srtp_t session,
5059
                                                  size_t mki_index,
5060
                                                  size_t *length)
5061
29.4k
{
5062
29.4k
    return get_protect_trailer_length(session, true, mki_index, length);
5063
29.4k
}
5064
5065
srtp_err_status_t srtp_get_protect_rtcp_trailer_length(srtp_t session,
5066
                                                       size_t mki_index,
5067
                                                       size_t *length)
5068
105k
{
5069
105k
    return get_protect_trailer_length(session, false, mki_index, length);
5070
105k
}
5071
5072
/*
5073
 * SRTP debug interface
5074
 */
5075
srtp_err_status_t srtp_set_debug_module(const char *mod_name, bool v)
5076
0
{
5077
0
    return srtp_crypto_kernel_set_debug_module(mod_name, v);
5078
0
}
5079
5080
srtp_err_status_t srtp_list_debug_modules(void)
5081
0
{
5082
0
    return srtp_crypto_kernel_list_debug_modules();
5083
0
}
5084
5085
/*
5086
 * srtp_log_handler is a global variable holding a pointer to the
5087
 * log handler function; this function is called for any log
5088
 * output.
5089
 */
5090
5091
static srtp_log_handler_func_t *srtp_log_handler = NULL;
5092
static void *srtp_log_handler_data = NULL;
5093
5094
static void srtp_err_handler(srtp_err_reporting_level_t level, const char *msg)
5095
0
{
5096
0
    if (srtp_log_handler) {
5097
0
        srtp_log_level_t log_level = srtp_log_level_error;
5098
0
        switch (level) {
5099
0
        case srtp_err_level_error:
5100
0
            log_level = srtp_log_level_error;
5101
0
            break;
5102
0
        case srtp_err_level_warning:
5103
0
            log_level = srtp_log_level_warning;
5104
0
            break;
5105
0
        case srtp_err_level_info:
5106
0
            log_level = srtp_log_level_info;
5107
0
            break;
5108
0
        case srtp_err_level_debug:
5109
0
            log_level = srtp_log_level_debug;
5110
0
            break;
5111
0
        }
5112
5113
0
        srtp_log_handler(log_level, msg, srtp_log_handler_data);
5114
0
    }
5115
0
}
5116
5117
srtp_err_status_t srtp_install_log_handler(srtp_log_handler_func_t func,
5118
                                           void *data)
5119
0
{
5120
    /*
5121
     * note that we accept NULL arguments intentionally - calling this
5122
     * function with a NULL arguments removes a log handler that's
5123
     * been previously installed
5124
     */
5125
5126
0
    if (srtp_log_handler) {
5127
0
        srtp_install_err_report_handler(NULL);
5128
0
    }
5129
0
    srtp_log_handler = func;
5130
0
    srtp_log_handler_data = data;
5131
0
    if (srtp_log_handler) {
5132
0
        srtp_install_err_report_handler(srtp_err_handler);
5133
0
    }
5134
0
    return srtp_err_status_ok;
5135
0
}
5136
5137
srtp_err_status_t srtp_stream_set_roc(srtp_t session,
5138
                                      uint32_t ssrc,
5139
                                      uint32_t roc)
5140
1.24k
{
5141
1.24k
    srtp_stream_t stream;
5142
5143
1.24k
    stream = srtp_get_stream(session, htonl(ssrc));
5144
1.24k
    if (stream == NULL) {
5145
63
        return srtp_err_status_bad_param;
5146
63
    }
5147
5148
1.17k
    stream->pending_roc = roc;
5149
5150
1.17k
    return srtp_err_status_ok;
5151
1.24k
}
5152
5153
srtp_err_status_t srtp_stream_get_roc(srtp_t session,
5154
                                      uint32_t ssrc,
5155
                                      uint32_t *roc)
5156
1.17k
{
5157
1.17k
    srtp_stream_t stream;
5158
5159
1.17k
    stream = srtp_get_stream(session, htonl(ssrc));
5160
1.17k
    if (stream == NULL) {
5161
57
        return srtp_err_status_bad_param;
5162
57
    }
5163
5164
1.12k
    *roc = srtp_rdbx_get_roc(&stream->rtp_rdbx);
5165
5166
1.12k
    return srtp_err_status_ok;
5167
1.17k
}
5168
5169
#ifndef SRTP_NO_STREAM_LIST
5170
5171
6.53k
#define INITIAL_STREAM_INDEX_SIZE 2
5172
5173
typedef struct list_entry {
5174
    uint32_t ssrc;
5175
    srtp_stream_t stream;
5176
} list_entry;
5177
5178
typedef struct srtp_stream_list_ctx_t_ {
5179
    list_entry *entries;
5180
    size_t capacity;
5181
    size_t size;
5182
} srtp_stream_list_ctx_t_;
5183
5184
srtp_err_status_t srtp_stream_list_alloc(srtp_stream_list_t *list_ptr)
5185
3.26k
{
5186
3.26k
    srtp_stream_list_t list =
5187
3.26k
        srtp_crypto_alloc(sizeof(srtp_stream_list_ctx_t_));
5188
3.26k
    if (list == NULL) {
5189
0
        return srtp_err_status_alloc_fail;
5190
0
    }
5191
5192
3.26k
    list->entries =
5193
3.26k
        srtp_crypto_alloc(sizeof(list_entry) * INITIAL_STREAM_INDEX_SIZE);
5194
3.26k
    if (list->entries == NULL) {
5195
0
        srtp_crypto_free(list);
5196
0
        return srtp_err_status_alloc_fail;
5197
0
    }
5198
5199
3.26k
    list->capacity = INITIAL_STREAM_INDEX_SIZE;
5200
3.26k
    list->size = 0;
5201
5202
3.26k
    *list_ptr = list;
5203
5204
3.26k
    return srtp_err_status_ok;
5205
3.26k
}
5206
5207
srtp_err_status_t srtp_stream_list_dealloc(srtp_stream_list_t list)
5208
3.26k
{
5209
    /* list must be empty */
5210
3.26k
    if (list->size != 0) {
5211
0
        return srtp_err_status_fail;
5212
0
    }
5213
5214
3.26k
    srtp_crypto_free(list->entries);
5215
3.26k
    srtp_crypto_free(list);
5216
5217
3.26k
    return srtp_err_status_ok;
5218
3.26k
}
5219
5220
/*
5221
 * inserting a new entry in the list may require reallocating memory in order
5222
 * to keep all the items in a contiguous memory block.
5223
 */
5224
srtp_err_status_t srtp_stream_list_insert(srtp_stream_list_t list,
5225
                                          srtp_stream_t stream)
5226
109k
{
5227
    /*
5228
     * there is no space to hold the new entry in the entries buffer,
5229
     * double the size of the buffer.
5230
     */
5231
109k
    if (list->size == list->capacity) {
5232
2.07k
        size_t new_capacity = list->capacity * 2;
5233
5234
        // Check for capacity overflow.
5235
2.07k
        if (new_capacity < list->capacity ||
5236
2.07k
            new_capacity > SIZE_MAX / sizeof(list_entry)) {
5237
0
            return srtp_err_status_alloc_fail;
5238
0
        }
5239
5240
2.07k
        list_entry *new_entries =
5241
2.07k
            srtp_crypto_alloc(sizeof(list_entry) * new_capacity);
5242
2.07k
        if (new_entries == NULL) {
5243
0
            return srtp_err_status_alloc_fail;
5244
0
        }
5245
5246
        // Copy previous entries into the new buffer.
5247
2.07k
        memcpy(new_entries, list->entries, sizeof(list_entry) * list->capacity);
5248
5249
        // Release previous entries.
5250
2.07k
        srtp_crypto_free(list->entries);
5251
5252
        // Assign new entries to the list.
5253
2.07k
        list->entries = new_entries;
5254
5255
        // Update list capacity.
5256
2.07k
        list->capacity = new_capacity;
5257
2.07k
    }
5258
5259
    // fill the first available entry
5260
109k
    size_t next_index = list->size;
5261
109k
    list->entries[next_index].ssrc = stream->ssrc;
5262
109k
    list->entries[next_index].stream = stream;
5263
5264
    // update size value
5265
109k
    list->size++;
5266
5267
109k
    return srtp_err_status_ok;
5268
109k
}
5269
5270
/*
5271
 * removing an entry from the list performs a memory move of the following
5272
 * entries one position back in order to keep all the entries in the buffer
5273
 * contiguous.
5274
 */
5275
void srtp_stream_list_remove(srtp_stream_list_t list,
5276
                             srtp_stream_t stream_to_remove)
5277
109k
{
5278
109k
    size_t end = list->size;
5279
5280
110k
    for (size_t i = 0; i < end; i++) {
5281
110k
        if (list->entries[i].ssrc == stream_to_remove->ssrc) {
5282
109k
            size_t entries_to_move = list->size - i - 1;
5283
109k
            memmove(&list->entries[i], &list->entries[i + 1],
5284
109k
                    sizeof(list_entry) * entries_to_move);
5285
109k
            list->size--;
5286
5287
109k
            break;
5288
109k
        }
5289
110k
    }
5290
109k
}
5291
5292
srtp_stream_t srtp_stream_list_get(srtp_stream_list_t list, uint32_t ssrc)
5293
242k
{
5294
242k
    size_t end = list->size;
5295
5296
242k
    list_entry *entries = list->entries;
5297
5298
262M
    for (size_t i = 0; i < end; i++) {
5299
262M
        if (entries[i].ssrc == ssrc) {
5300
139k
            return entries[i].stream;
5301
139k
        }
5302
262M
    }
5303
5304
102k
    return NULL;
5305
242k
}
5306
5307
void srtp_stream_list_for_each(srtp_stream_list_t list,
5308
                               bool (*callback)(srtp_stream_t, void *),
5309
                               void *data)
5310
138k
{
5311
138k
    list_entry *entries = list->entries;
5312
5313
138k
    size_t size = list->size;
5314
5315
    /*
5316
     * the second statement of the expression needs to be recalculated on each
5317
     * iteration as the available number of entries may change within the given
5318
     * callback.
5319
     * Ie: in case the callback calls srtp_stream_list_remove().
5320
     */
5321
267M
    for (size_t i = 0; i < list->size;) {
5322
267M
        if (!callback(entries[i].stream, data)) {
5323
0
            break;
5324
0
        }
5325
5326
        // the entry was not removed, increase the counter.
5327
267M
        if (size == list->size) {
5328
267M
            ++i;
5329
267M
        }
5330
5331
267M
        size = list->size;
5332
267M
    }
5333
138k
}
5334
5335
#endif