Coverage Report

Created: 2026-09-14 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsrtp/srtp/srtp_policy.c
Line
Count
Source
1
/*
2
 * srtp_policy.c
3
 *
4
 * extensible policy API for libSRTP
5
 */
6
/*
7
 *
8
 * Copyright (c) 2026
9
 * All rights reserved.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 *
15
 *   Redistributions of source code must retain the above copyright
16
 *   notice, this list of conditions and the following disclaimer.
17
 *
18
 *   Redistributions in binary form must reproduce the above
19
 *   copyright notice, this list of conditions and the following
20
 *   disclaimer in the documentation and/or other materials provided
21
 *   with the distribution.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34
 * OF THE POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 */
37
38
#include "srtp_priv.h"
39
40
#include <string.h>
41
42
#include "alloc.h"
43
44
/**
45
 * @brief srtp_crypto_policy_set_rtp_default() sets a crypto policy
46
 * structure to the SRTP default policy for RTP protection.
47
 *
48
 * @param p is a pointer to the policy structure to be set
49
 *
50
 * The function call srtp_crypto_policy_set_rtp_default(&p) sets the
51
 * srtp_crypto_policy_t at location p to the SRTP default policy for RTP
52
 * protection, as defined in the specification.  This function is a
53
 * convenience that helps to avoid dealing directly with the policy
54
 * data structure.  You are encouraged to initialize policy elements
55
 * with this function call.  Doing so may allow your code to be
56
 * forward compatible with later versions of libSRTP that include more
57
 * elements in the srtp_crypto_policy_t datatype.
58
 *
59
 * @return void.
60
 *
61
 */
62
void srtp_crypto_policy_set_rtp_default(srtp_crypto_policy_t *p);
63
64
/**
65
 * @brief srtp_crypto_policy_set_rtcp_default() sets a crypto policy
66
 * structure to the SRTP default policy for RTCP protection.
67
 *
68
 * @param p is a pointer to the policy structure to be set
69
 *
70
 * The function call srtp_crypto_policy_set_rtcp_default(&p) sets the
71
 * srtp_crypto_policy_t at location p to the SRTP default policy for RTCP
72
 * protection, as defined in the specification.  This function is a
73
 * convenience that helps to avoid dealing directly with the policy
74
 * data structure.  You are encouraged to initialize policy elements
75
 * with this function call.  Doing so may allow your code to be
76
 * forward compatible with later versions of libSRTP that include more
77
 * elements in the srtp_crypto_policy_t datatype.
78
 *
79
 * @return void.
80
 *
81
 */
82
void srtp_crypto_policy_set_rtcp_default(srtp_crypto_policy_t *p);
83
84
/**
85
 * @brief srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80() sets a crypto
86
 * policy structure to the SRTP default policy for RTP protection.
87
 *
88
 * @param p is a pointer to the policy structure to be set
89
 *
90
 * The function srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80() is a
91
 * synonym for srtp_crypto_policy_set_rtp_default().  It conforms to the
92
 * naming convention used in RFC 4568 (SDP Security Descriptions for
93
 * Media Streams).
94
 *
95
 * @return void.
96
 *
97
 */
98
#define srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(p)                      \
99
7.11k
    srtp_crypto_policy_set_rtp_default(p)
100
101
/**
102
 * @brief srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32() sets a crypto
103
 * policy structure to a short-authentication tag policy
104
 *
105
 * @param p is a pointer to the policy structure to be set
106
 *
107
 * The function call srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&p)
108
 * sets the srtp_crypto_policy_t at location p to use policy
109
 * AES_CM_128_HMAC_SHA1_32 as defined in RFC 4568.
110
 * This policy uses AES-128
111
 * Counter Mode encryption and HMAC-SHA1 authentication, with an
112
 * authentication tag that is only 32 bits long.  This length is
113
 * considered adequate only for protecting audio and video media that
114
 * use a stateless playback function.  See Section 7.5 of RFC 3711
115
 * (http://www.ietf.org/rfc/rfc3711.txt).
116
 *
117
 * This function is a convenience that helps to avoid dealing directly
118
 * with the policy data structure.  You are encouraged to initialize
119
 * policy elements with this function call.  Doing so may allow your
120
 * code to be forward compatible with later versions of libSRTP that
121
 * include more elements in the srtp_crypto_policy_t datatype.
122
 *
123
 * @warning This crypto policy is intended for use in SRTP, but not in
124
 * SRTCP.  It is recommended that a policy that uses longer
125
 * authentication tags be used for SRTCP.  See Section 7.5 of RFC 3711
126
 * (http://www.ietf.org/rfc/rfc3711.txt).
127
 *
128
 * @return void.
129
 *
130
 */
131
void srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(srtp_crypto_policy_t *p);
132
133
/**
134
 * @brief srtp_crypto_policy_set_aes_cm_128_null_auth() sets a crypto
135
 * policy structure to an encryption-only policy
136
 *
137
 * @param p is a pointer to the policy structure to be set
138
 *
139
 * The function call srtp_crypto_policy_set_aes_cm_128_null_auth(&p) sets
140
 * the srtp_crypto_policy_t at location p to use the SRTP default cipher
141
 * (AES-128 Counter Mode), but to use no authentication method.  This
142
 * policy is NOT RECOMMENDED unless it is unavoidable; see Section 7.5
143
 * of RFC 3711 (http://www.ietf.org/rfc/rfc3711.txt).
144
 *
145
 * This function is a convenience that helps to avoid dealing directly
146
 * with the policy data structure.  You are encouraged to initialize
147
 * policy elements with this function call.  Doing so may allow your
148
 * code to be forward compatible with later versions of libSRTP that
149
 * include more elements in the srtp_crypto_policy_t datatype.
150
 *
151
 * @warning This policy is NOT RECOMMENDED for SRTP unless it is
152
 * unavoidable, and it is NOT RECOMMENDED at all for SRTCP; see
153
 * Section 7.5 of RFC 3711 (http://www.ietf.org/rfc/rfc3711.txt).
154
 *
155
 * @return void.
156
 *
157
 */
158
void srtp_crypto_policy_set_aes_cm_128_null_auth(srtp_crypto_policy_t *p);
159
160
/**
161
 * @brief srtp_crypto_policy_set_null_cipher_hmac_sha1_80() sets a crypto
162
 * policy structure to an authentication-only policy
163
 *
164
 * @param p is a pointer to the policy structure to be set
165
 *
166
 * The function call srtp_crypto_policy_set_null_cipher_hmac_sha1_80(&p)
167
 * sets the srtp_crypto_policy_t at location p to use HMAC-SHA1 with an 80
168
 * bit authentication tag to provide message authentication, but to
169
 * use no encryption.  This policy is NOT RECOMMENDED for SRTP unless
170
 * there is a requirement to forgo encryption.
171
 *
172
 * This function is a convenience that helps to avoid dealing directly
173
 * with the policy data structure.  You are encouraged to initialize
174
 * policy elements with this function call.  Doing so may allow your
175
 * code to be forward compatible with later versions of libSRTP that
176
 * include more elements in the srtp_crypto_policy_t datatype.
177
 *
178
 * @warning This policy is NOT RECOMMENDED for SRTP unless there is a
179
 * requirement to forgo encryption.
180
 *
181
 * @return void.
182
 *
183
 */
184
void srtp_crypto_policy_set_null_cipher_hmac_sha1_80(srtp_crypto_policy_t *p);
185
186
/**
187
 * @brief srtp_crypto_policy_set_null_cipher_hmac_null() sets a crypto
188
 * policy structure to use no encryption or authentication.
189
 *
190
 * @param p is a pointer to the policy structure to be set
191
 *
192
 * The function call srtp_crypto_policy_set_null_cipher_hmac_null(&p)
193
 * sets the srtp_crypto_policy_t at location p to use no encryption and
194
 * no authentication.  This policy should only be used for testing and
195
 * troubleshooting.
196
 *
197
 * This function is a convenience that helps to avoid dealing directly
198
 * with the policy data structure.  You are encouraged to initialize
199
 * policy elements with this function call.  Doing so may allow your
200
 * code to be forward compatible with later versions of libSRTP that
201
 * include more elements in the srtp_crypto_policy_t datatype.
202
 *
203
 * @warning This policy is NOT RECOMMENDED for SRTP unless there is a
204
 * requirement to forgo encryption and authentication.
205
 *
206
 * @return void.
207
 *
208
 */
209
void srtp_crypto_policy_set_null_cipher_hmac_null(srtp_crypto_policy_t *p);
210
211
/**
212
 * @brief srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80() sets a crypto
213
 * policy structure to a encryption and authentication policy using AES-256
214
 * for RTP protection.
215
 *
216
 * @param p is a pointer to the policy structure to be set
217
 *
218
 * The function call srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&p)
219
 * sets the srtp_crypto_policy_t at location p to use policy
220
 * AES_CM_256_HMAC_SHA1_80 as defined in RFC 6188.  This policy uses AES-256
221
 * Counter Mode encryption and HMAC-SHA1 authentication, with an 80 bit
222
 * authentication tag.
223
 *
224
 * This function is a convenience that helps to avoid dealing directly
225
 * with the policy data structure.  You are encouraged to initialize
226
 * policy elements with this function call.  Doing so may allow your
227
 * code to be forward compatible with later versions of libSRTP that
228
 * include more elements in the srtp_crypto_policy_t datatype.
229
 *
230
 * @return void.
231
 *
232
 */
233
void srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(srtp_crypto_policy_t *p);
234
235
/**
236
 * @brief srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32() sets a crypto
237
 * policy structure to a short-authentication tag policy using AES-256
238
 * encryption.
239
 *
240
 * @param p is a pointer to the policy structure to be set
241
 *
242
 * The function call srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(&p)
243
 * sets the srtp_crypto_policy_t at location p to use policy
244
 * AES_CM_256_HMAC_SHA1_32 as defined in RFC 6188.  This policy uses AES-256
245
 * Counter Mode encryption and HMAC-SHA1 authentication, with an
246
 * authentication tag that is only 32 bits long.  This length is
247
 * considered adequate only for protecting audio and video media that
248
 * use a stateless playback function.  See Section 7.5 of RFC 3711
249
 * (http://www.ietf.org/rfc/rfc3711.txt).
250
 *
251
 * This function is a convenience that helps to avoid dealing directly
252
 * with the policy data structure.  You are encouraged to initialize
253
 * policy elements with this function call.  Doing so may allow your
254
 * code to be forward compatible with later versions of libSRTP that
255
 * include more elements in the srtp_crypto_policy_t datatype.
256
 *
257
 * @warning This crypto policy is intended for use in SRTP, but not in
258
 * SRTCP.  It is recommended that a policy that uses longer
259
 * authentication tags be used for SRTCP.  See Section 7.5 of RFC 3711
260
 * (http://www.ietf.org/rfc/rfc3711.txt).
261
 *
262
 * @return void.
263
 *
264
 */
265
void srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(srtp_crypto_policy_t *p);
266
267
/**
268
 * @brief srtp_crypto_policy_set_aes_cm_256_null_auth() sets a crypto
269
 * policy structure to an encryption-only policy
270
 *
271
 * @param p is a pointer to the policy structure to be set
272
 *
273
 * The function call srtp_crypto_policy_set_aes_cm_256_null_auth(&p) sets
274
 * the srtp_crypto_policy_t at location p to use the SRTP default cipher
275
 * (AES-256 Counter Mode), but to use no authentication method.  This
276
 * policy is NOT RECOMMENDED unless it is unavoidable; see Section 7.5
277
 * of RFC 3711 (http://www.ietf.org/rfc/rfc3711.txt).
278
 *
279
 * This function is a convenience that helps to avoid dealing directly
280
 * with the policy data structure.  You are encouraged to initialize
281
 * policy elements with this function call.  Doing so may allow your
282
 * code to be forward compatible with later versions of libSRTP that
283
 * include more elements in the srtp_crypto_policy_t datatype.
284
 *
285
 * @warning This policy is NOT RECOMMENDED for SRTP unless it is
286
 * unavoidable, and it is NOT RECOMMENDED at all for SRTCP; see
287
 * Section 7.5 of RFC 3711 (http://www.ietf.org/rfc/rfc3711.txt).
288
 *
289
 * @return void.
290
 *
291
 */
292
void srtp_crypto_policy_set_aes_cm_256_null_auth(srtp_crypto_policy_t *p);
293
294
/**
295
 * @brief srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80() sets a crypto
296
 * policy structure to a encryption and authentication policy using AES-192
297
 * for RTP protection.
298
 *
299
 * @param p is a pointer to the policy structure to be set
300
 *
301
 * The function call srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(&p)
302
 * sets the srtp_crypto_policy_t at location p to use policy
303
 * AES_CM_192_HMAC_SHA1_80 as defined in RFC 6188.  This policy uses AES-192
304
 * Counter Mode encryption and HMAC-SHA1 authentication, with an 80 bit
305
 * authentication tag.
306
 *
307
 * This function is a convenience that helps to avoid dealing directly
308
 * with the policy data structure.  You are encouraged to initialize
309
 * policy elements with this function call.  Doing so may allow your
310
 * code to be forward compatible with later versions of libSRTP that
311
 * include more elements in the srtp_crypto_policy_t datatype.
312
 *
313
 * @return void.
314
 *
315
 */
316
void srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(srtp_crypto_policy_t *p);
317
318
/**
319
 * @brief srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32() sets a crypto
320
 * policy structure to a short-authentication tag policy using AES-192
321
 * encryption.
322
 *
323
 * @param p is a pointer to the policy structure to be set
324
 *
325
 * The function call srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(&p)
326
 * sets the srtp_crypto_policy_t at location p to use policy
327
 * AES_CM_192_HMAC_SHA1_32 as defined in RFC 6188.  This policy uses AES-192
328
 * Counter Mode encryption and HMAC-SHA1 authentication, with an
329
 * authentication tag that is only 32 bits long.  This length is
330
 * considered adequate only for protecting audio and video media that
331
 * use a stateless playback function.  See Section 7.5 of RFC 3711
332
 * (http://www.ietf.org/rfc/rfc3711.txt).
333
 *
334
 * This function is a convenience that helps to avoid dealing directly
335
 * with the policy data structure.  You are encouraged to initialize
336
 * policy elements with this function call.  Doing so may allow your
337
 * code to be forward compatible with later versions of libSRTP that
338
 * include more elements in the srtp_crypto_policy_t datatype.
339
 *
340
 * @warning This crypto policy is intended for use in SRTP, but not in
341
 * SRTCP.  It is recommended that a policy that uses longer
342
 * authentication tags be used for SRTCP.  See Section 7.5 of RFC 3711
343
 * (http://www.ietf.org/rfc/rfc3711.txt).
344
 *
345
 * @return void.
346
 *
347
 */
348
void srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(srtp_crypto_policy_t *p);
349
350
/**
351
 * @brief srtp_crypto_policy_set_aes_cm_192_null_auth() sets a crypto
352
 * policy structure to an encryption-only policy
353
 *
354
 * @param p is a pointer to the policy structure to be set
355
 *
356
 * The function call srtp_crypto_policy_set_aes_cm_192_null_auth(&p) sets
357
 * the srtp_crypto_policy_t at location p to use the SRTP default cipher
358
 * (AES-192 Counter Mode), but to use no authentication method.  This
359
 * policy is NOT RECOMMENDED unless it is unavoidable; see Section 7.5
360
 * of RFC 3711 (http://www.ietf.org/rfc/rfc3711.txt).
361
 *
362
 * This function is a convenience that helps to avoid dealing directly
363
 * with the policy data structure.  You are encouraged to initialize
364
 * policy elements with this function call.  Doing so may allow your
365
 * code to be forward compatible with later versions of libSRTP that
366
 * include more elements in the srtp_crypto_policy_t datatype.
367
 *
368
 * @warning This policy is NOT RECOMMENDED for SRTP unless it is
369
 * unavoidable, and it is NOT RECOMMENDED at all for SRTCP; see
370
 * Section 7.5 of RFC 3711 (http://www.ietf.org/rfc/rfc3711.txt).
371
 *
372
 * @return void.
373
 *
374
 */
375
void srtp_crypto_policy_set_aes_cm_192_null_auth(srtp_crypto_policy_t *p);
376
377
/**
378
 * @brief srtp_crypto_policy_set_aes_gcm_128_16_auth() sets a crypto
379
 * policy structure to an AEAD encryption policy.
380
 *
381
 * @param p is a pointer to the policy structure to be set
382
 *
383
 * The function call srtp_crypto_policy_set_aes_gcm_128_16_auth(&p) sets
384
 * the srtp_crypto_policy_t at location p to use the SRTP default cipher
385
 * (AES-128 Galois Counter Mode) with 16 octet auth tag.  This
386
 * policy applies confidentiality and authentication to both the
387
 * RTP and RTCP packets.
388
 *
389
 * This function is a convenience that helps to avoid dealing directly
390
 * with the policy data structure.  You are encouraged to initialize
391
 * policy elements with this function call.  Doing so may allow your
392
 * code to be forward compatible with later versions of libSRTP that
393
 * include more elements in the srtp_crypto_policy_t datatype.
394
 *
395
 * @return void.
396
 *
397
 */
398
void srtp_crypto_policy_set_aes_gcm_128_16_auth(srtp_crypto_policy_t *p);
399
400
/**
401
 * @brief srtp_crypto_policy_set_aes_gcm_256_16_auth() sets a crypto
402
 * policy structure to an AEAD encryption policy
403
 *
404
 * @param p is a pointer to the policy structure to be set
405
 *
406
 * The function call srtp_crypto_policy_set_aes_gcm_256_16_auth(&p) sets
407
 * the srtp_crypto_policy_t at location p to use the SRTP default cipher
408
 * (AES-256 Galois Counter Mode) with 16 octet auth tag.  This
409
 * policy applies confidentiality and authentication to both the
410
 * RTP and RTCP packets.
411
 *
412
 * This function is a convenience that helps to avoid dealing directly
413
 * with the policy data structure.  You are encouraged to initialize
414
 * policy elements with this function call.  Doing so may allow your
415
 * code to be forward compatible with later versions of libSRTP that
416
 * include more elements in the srtp_crypto_policy_t datatype.
417
 *
418
 * @return void.
419
 *
420
 */
421
void srtp_crypto_policy_set_aes_gcm_256_16_auth(srtp_crypto_policy_t *p);
422
423
/**
424
 * @brief srtp_crypto_policy_set_from_profile_for_rtp() sets a crypto policy
425
 * structure to the appropriate value for RTP based on an srtp_profile_t
426
 *
427
 * @param policy is a pointer to the policy structure to be set
428
 *
429
 * @param profile is an enumeration for the policy to be set
430
 *
431
 * The function call srtp_crypto_policy_set_rtp_default(&policy, profile)
432
 * sets the srtp_crypto_policy_t at location policy to the policy for RTP
433
 * protection, as defined by the srtp_profile_t profile.
434
 *
435
 * This function is a convenience that helps to avoid dealing directly
436
 * with the policy data structure.  You are encouraged to initialize
437
 * policy elements with this function call.  Doing so may allow your
438
 * code to be forward compatible with later versions of libSRTP that
439
 * include more elements in the srtp_crypto_policy_t datatype.
440
 *
441
 * @return values
442
 *     - srtp_err_status_ok         no problems were encountered
443
 *     - srtp_err_status_bad_param  the profile is not supported
444
 *
445
 */
446
srtp_err_status_t srtp_crypto_policy_set_from_profile_for_rtp(
447
    srtp_crypto_policy_t *policy,
448
    srtp_profile_t profile);
449
450
/**
451
 * @brief srtp_crypto_policy_set_from_profile_for_rtcp() sets a crypto policy
452
 * structure to the appropriate value for RTCP based on an srtp_profile_t
453
 *
454
 * @param policy is a pointer to the policy structure to be set
455
 *
456
 * @param profile is an enumeration for the policy to be set
457
 *
458
 * The function call srtp_crypto_policy_set_rtcp_default(&policy, profile)
459
 * sets the srtp_crypto_policy_t at location policy to the policy for RTCP
460
 * protection, as defined by the srtp_profile_t profile.
461
 *
462
 * This function is a convenience that helps to avoid dealing directly
463
 * with the policy data structure.  You are encouraged to initialize
464
 * policy elements with this function call.  Doing so may allow your
465
 * code to be forward compatible with later versions of libSRTP that
466
 * include more elements in the srtp_crypto_policy_t datatype.
467
 *
468
 * @return values
469
 *     - srtp_err_status_ok         no problems were encountered
470
 *     - srtp_err_status_bad_param  the profile is not supported
471
 *
472
 */
473
srtp_err_status_t srtp_crypto_policy_set_from_profile_for_rtcp(
474
    srtp_crypto_policy_t *policy,
475
    srtp_profile_t profile);
476
477
/*
478
 * The default policy - provides a convenient way for callers to use
479
 * the default security policy
480
 *
481
 * The default policy is defined in RFC 3711
482
 * (Section 5. Default and mandatory-to-implement Transforms)
483
 *
484
 */
485
486
/*
487
 * NOTE: cipher_key_len is really key len (128 bits) plus salt len
488
 *  (112 bits)
489
 */
490
/* There are hard-coded 16's for base_key_len in the key generation code */
491
492
void srtp_crypto_policy_set_rtp_default(srtp_crypto_policy_t *p)
493
7.11k
{
494
7.11k
    p->cipher_type = SRTP_AES_ICM_128;
495
7.11k
    p->cipher_key_len =
496
7.11k
        SRTP_AES_ICM_128_KEY_LEN_WSALT; /* default 128 bits per RFC 3711 */
497
7.11k
    p->auth_type = SRTP_HMAC_SHA1;
498
7.11k
    p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
499
7.11k
    p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
500
7.11k
    p->sec_serv = sec_serv_conf_and_auth;
501
7.11k
}
502
503
void srtp_crypto_policy_set_rtcp_default(srtp_crypto_policy_t *p)
504
0
{
505
0
    p->cipher_type = SRTP_AES_ICM_128;
506
0
    p->cipher_key_len =
507
0
        SRTP_AES_ICM_128_KEY_LEN_WSALT; /* default 128 bits per RFC 3711 */
508
0
    p->auth_type = SRTP_HMAC_SHA1;
509
0
    p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
510
0
    p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
511
0
    p->sec_serv = sec_serv_conf_and_auth;
512
0
}
513
514
void srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(srtp_crypto_policy_t *p)
515
4.38k
{
516
    /*
517
     * corresponds to RFC 4568
518
     *
519
     * note that this crypto policy is intended for SRTP, but not SRTCP
520
     */
521
522
4.38k
    p->cipher_type = SRTP_AES_ICM_128;
523
4.38k
    p->cipher_key_len =
524
4.38k
        SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */
525
4.38k
    p->auth_type = SRTP_HMAC_SHA1;
526
4.38k
    p->auth_key_len = 20; /* 160 bit key               */
527
4.38k
    p->auth_tag_len = 4;  /* 32 bit tag                */
528
4.38k
    p->sec_serv = sec_serv_conf_and_auth;
529
4.38k
}
530
531
void srtp_crypto_policy_set_aes_cm_128_null_auth(srtp_crypto_policy_t *p)
532
0
{
533
    /*
534
     * corresponds to RFC 4568
535
     *
536
     * note that this crypto policy is intended for SRTP, but not SRTCP
537
     */
538
539
0
    p->cipher_type = SRTP_AES_ICM_128;
540
0
    p->cipher_key_len =
541
0
        SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */
542
0
    p->auth_type = SRTP_NULL_AUTH;
543
0
    p->auth_key_len = 0;
544
0
    p->auth_tag_len = 0;
545
0
    p->sec_serv = sec_serv_conf;
546
0
}
547
548
void srtp_crypto_policy_set_null_cipher_hmac_sha1_80(srtp_crypto_policy_t *p)
549
11.3k
{
550
    /*
551
     * corresponds to RFC 4568
552
     */
553
554
11.3k
    p->cipher_type = SRTP_NULL_CIPHER;
555
11.3k
    p->cipher_key_len =
556
11.3k
        SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */
557
11.3k
    p->auth_type = SRTP_HMAC_SHA1;
558
11.3k
    p->auth_key_len = 20;
559
11.3k
    p->auth_tag_len = 10;
560
11.3k
    p->sec_serv = sec_serv_auth;
561
11.3k
}
562
563
void srtp_crypto_policy_set_null_cipher_hmac_null(srtp_crypto_policy_t *p)
564
25.1k
{
565
    /*
566
     * Should only be used for testing
567
     */
568
569
25.1k
    p->cipher_type = SRTP_NULL_CIPHER;
570
25.1k
    p->cipher_key_len = 0;
571
25.1k
    p->auth_type = SRTP_NULL_AUTH;
572
25.1k
    p->auth_key_len = 0;
573
25.1k
    p->auth_tag_len = 0;
574
25.1k
    p->sec_serv = sec_serv_none;
575
25.1k
}
576
577
void srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(srtp_crypto_policy_t *p)
578
1.37k
{
579
    /*
580
     * corresponds to RFC 6188
581
     */
582
583
1.37k
    p->cipher_type = SRTP_AES_ICM_256;
584
1.37k
    p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT;
585
1.37k
    p->auth_type = SRTP_HMAC_SHA1;
586
1.37k
    p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
587
1.37k
    p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
588
1.37k
    p->sec_serv = sec_serv_conf_and_auth;
589
1.37k
}
590
591
void srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(srtp_crypto_policy_t *p)
592
465
{
593
    /*
594
     * corresponds to RFC 6188
595
     *
596
     * note that this crypto policy is intended for SRTP, but not SRTCP
597
     */
598
599
465
    p->cipher_type = SRTP_AES_ICM_256;
600
465
    p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT;
601
465
    p->auth_type = SRTP_HMAC_SHA1;
602
465
    p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
603
465
    p->auth_tag_len = 4;  /* default 80 bits per RFC 3711 */
604
465
    p->sec_serv = sec_serv_conf_and_auth;
605
465
}
606
607
/*
608
 * AES-256 with no authentication.
609
 */
610
void srtp_crypto_policy_set_aes_cm_256_null_auth(srtp_crypto_policy_t *p)
611
0
{
612
0
    p->cipher_type = SRTP_AES_ICM_256;
613
0
    p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT;
614
0
    p->auth_type = SRTP_NULL_AUTH;
615
0
    p->auth_key_len = 0;
616
0
    p->auth_tag_len = 0;
617
0
    p->sec_serv = sec_serv_conf;
618
0
}
619
620
void srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(srtp_crypto_policy_t *p)
621
3.35k
{
622
    /*
623
     * corresponds to RFC 6188
624
     */
625
626
3.35k
    p->cipher_type = SRTP_AES_ICM_192;
627
3.35k
    p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT;
628
3.35k
    p->auth_type = SRTP_HMAC_SHA1;
629
3.35k
    p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
630
3.35k
    p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
631
3.35k
    p->sec_serv = sec_serv_conf_and_auth;
632
3.35k
}
633
634
void srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(srtp_crypto_policy_t *p)
635
825
{
636
    /*
637
     * corresponds to RFC 6188
638
     *
639
     * note that this crypto policy is intended for SRTP, but not SRTCP
640
     */
641
642
825
    p->cipher_type = SRTP_AES_ICM_192;
643
825
    p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT;
644
825
    p->auth_type = SRTP_HMAC_SHA1;
645
825
    p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
646
825
    p->auth_tag_len = 4;  /* default 80 bits per RFC 3711 */
647
825
    p->sec_serv = sec_serv_conf_and_auth;
648
825
}
649
650
/*
651
 * AES-192 with no authentication.
652
 */
653
void srtp_crypto_policy_set_aes_cm_192_null_auth(srtp_crypto_policy_t *p)
654
0
{
655
0
    p->cipher_type = SRTP_AES_ICM_192;
656
0
    p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT;
657
0
    p->auth_type = SRTP_NULL_AUTH;
658
0
    p->auth_key_len = 0;
659
0
    p->auth_tag_len = 0;
660
0
    p->sec_serv = sec_serv_conf;
661
0
}
662
663
/*
664
 * AES-128 GCM mode with 16 octet auth tag.
665
 */
666
void srtp_crypto_policy_set_aes_gcm_128_16_auth(srtp_crypto_policy_t *p)
667
0
{
668
0
    p->cipher_type = SRTP_AES_GCM_128;
669
0
    p->cipher_key_len = SRTP_AES_GCM_128_KEY_LEN_WSALT;
670
0
    p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
671
0
    p->auth_key_len = 0;
672
0
    p->auth_tag_len = 16; /* 16 octet tag length */
673
0
    p->sec_serv = sec_serv_conf_and_auth;
674
0
}
675
676
/*
677
 * AES-256 GCM mode with 16 octet auth tag.
678
 */
679
void srtp_crypto_policy_set_aes_gcm_256_16_auth(srtp_crypto_policy_t *p)
680
0
{
681
0
    p->cipher_type = SRTP_AES_GCM_256;
682
0
    p->cipher_key_len = SRTP_AES_GCM_256_KEY_LEN_WSALT;
683
0
    p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
684
0
    p->auth_key_len = 0;
685
0
    p->auth_tag_len = 16; /* 16 octet tag length */
686
0
    p->sec_serv = sec_serv_conf_and_auth;
687
0
}
688
689
srtp_err_status_t srtp_crypto_policy_set_from_profile_for_rtp(
690
    srtp_crypto_policy_t *policy,
691
    srtp_profile_t profile)
692
27.0k
{
693
    /* set SRTP policy from the SRTP profile in the key set */
694
27.0k
    switch (profile) {
695
0
    case srtp_profile_reserved:
696
0
        return srtp_err_status_bad_param;
697
12.5k
    case srtp_profile_null_null:
698
12.5k
        srtp_crypto_policy_set_null_cipher_hmac_null(policy);
699
12.5k
        return srtp_err_status_ok;
700
1.36k
    case srtp_profile_aes128_cm_sha1_80:
701
1.36k
        srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
702
1.36k
        return srtp_err_status_ok;
703
4.38k
    case srtp_profile_aes128_cm_sha1_32:
704
4.38k
        srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(policy);
705
4.38k
        return srtp_err_status_ok;
706
1.26k
    case srtp_profile_aes192_cm_sha1_80:
707
1.26k
        srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(policy);
708
1.26k
        return srtp_err_status_ok;
709
825
    case srtp_profile_aes192_cm_sha1_32:
710
825
        srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(policy);
711
825
        return srtp_err_status_ok;
712
457
    case srtp_profile_aes256_cm_sha1_80:
713
457
        srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy);
714
457
        return srtp_err_status_ok;
715
465
    case srtp_profile_aes256_cm_sha1_32:
716
465
        srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(policy);
717
465
        return srtp_err_status_ok;
718
1.40k
    case srtp_profile_null_sha1_80:
719
1.40k
        srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy);
720
1.40k
        return srtp_err_status_ok;
721
#ifdef GCM
722
    case srtp_profile_aead_aes_128_gcm:
723
        srtp_crypto_policy_set_aes_gcm_128_16_auth(policy);
724
        return srtp_err_status_ok;
725
    case srtp_profile_aead_aes_256_gcm:
726
        srtp_crypto_policy_set_aes_gcm_256_16_auth(policy);
727
        return srtp_err_status_ok;
728
#else
729
2
    case srtp_profile_aead_aes_128_gcm:
730
2
        return srtp_err_status_bad_param;
731
52
    case srtp_profile_aead_aes_256_gcm:
732
52
        return srtp_err_status_bad_param;
733
0
#endif
734
4.25k
    case srtp_profile_null_sha1_32:
735
4.25k
        srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy);
736
4.25k
        policy->auth_tag_len = 4;
737
4.25k
        return srtp_err_status_ok;
738
27.0k
    }
739
740
0
    return srtp_err_status_bad_param;
741
27.0k
}
742
743
srtp_err_status_t srtp_crypto_policy_set_from_profile_for_rtcp(
744
    srtp_crypto_policy_t *policy,
745
    srtp_profile_t profile)
746
26.9k
{
747
    /* set SRTP policy from the SRTP profile in the key set */
748
26.9k
    switch (profile) {
749
0
    case srtp_profile_reserved:
750
0
        return srtp_err_status_bad_param;
751
12.5k
    case srtp_profile_null_null:
752
12.5k
        srtp_crypto_policy_set_null_cipher_hmac_null(policy);
753
12.5k
        return srtp_err_status_ok;
754
1.36k
    case srtp_profile_aes128_cm_sha1_80:
755
1.36k
        srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
756
1.36k
        return srtp_err_status_ok;
757
4.38k
    case srtp_profile_aes128_cm_sha1_32:
758
        /* We do not honor the 32-bit auth tag request since
759
         * this is not compliant with RFC 3711 */
760
4.38k
        srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
761
4.38k
        return srtp_err_status_ok;
762
1.26k
    case srtp_profile_aes192_cm_sha1_80:
763
1.26k
        srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(policy);
764
1.26k
        return srtp_err_status_ok;
765
825
    case srtp_profile_aes192_cm_sha1_32:
766
        /* We do not honor the 32-bit auth tag request since
767
         * this is not compliant with RFC 3711 */
768
825
        srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(policy);
769
825
        return srtp_err_status_ok;
770
457
    case srtp_profile_aes256_cm_sha1_80:
771
457
        srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy);
772
457
        return srtp_err_status_ok;
773
465
    case srtp_profile_aes256_cm_sha1_32:
774
        /* We do not honor the 32-bit auth tag request since
775
         * this is not compliant with RFC 6188 */
776
465
        srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy);
777
465
        return srtp_err_status_ok;
778
1.40k
    case srtp_profile_null_sha1_80:
779
1.40k
        srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy);
780
1.40k
        return srtp_err_status_ok;
781
#ifdef GCM
782
    case srtp_profile_aead_aes_128_gcm:
783
        srtp_crypto_policy_set_aes_gcm_128_16_auth(policy);
784
        return srtp_err_status_ok;
785
    case srtp_profile_aead_aes_256_gcm:
786
        srtp_crypto_policy_set_aes_gcm_256_16_auth(policy);
787
        return srtp_err_status_ok;
788
#else
789
0
    case srtp_profile_aead_aes_128_gcm:
790
0
        return srtp_err_status_bad_param;
791
0
    case srtp_profile_aead_aes_256_gcm:
792
0
        return srtp_err_status_bad_param;
793
0
#endif
794
4.25k
    case srtp_profile_null_sha1_32:
795
        /* We do not honor the 32-bit auth tag request since
796
         * this is not compliant with RFC 3711 */
797
4.25k
        srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy);
798
4.25k
        return srtp_err_status_ok;
799
26.9k
    }
800
801
0
    return srtp_err_status_bad_param;
802
26.9k
}
803
804
srtp_err_status_t srtp_policy_create(srtp_policy_t *policy)
805
27.0k
{
806
27.0k
    srtp_policy_t p;
807
808
27.0k
    if (policy == NULL) {
809
0
        return srtp_err_status_bad_param;
810
0
    }
811
812
27.0k
    p = (srtp_policy_t)srtp_crypto_alloc(sizeof(*p));
813
27.0k
    if (p == NULL) {
814
0
        *policy = NULL;
815
0
        return srtp_err_status_alloc_fail;
816
0
    }
817
818
27.0k
    *policy = p;
819
820
27.0k
    return srtp_err_status_ok;
821
27.0k
}
822
823
srtp_err_status_t srtp_policy_clone(srtp_policy_t policy,
824
                                    srtp_policy_t *cloned_policy)
825
0
{
826
0
    if (policy == NULL || cloned_policy == NULL) {
827
0
        return srtp_err_status_bad_param;
828
0
    }
829
830
0
    srtp_policy_t p;
831
0
    srtp_err_status_t status = srtp_policy_create(&p);
832
0
    if (status != srtp_err_status_ok) {
833
0
        return status;
834
0
    }
835
836
0
    memcpy(p, policy, sizeof(*p));
837
838
0
    *cloned_policy = p;
839
840
0
    return srtp_err_status_ok;
841
0
}
842
843
void srtp_policy_destroy(srtp_policy_t policy)
844
29.1k
{
845
29.1k
    if (policy == NULL) {
846
2.12k
        return;
847
2.12k
    }
848
849
27.0k
    octet_string_set_to_zero(policy->master_keys, sizeof(policy->master_keys));
850
27.0k
    srtp_crypto_free(policy);
851
27.0k
}
852
853
srtp_err_status_t srtp_policy_validate(srtp_policy_t policy)
854
24.6k
{
855
24.6k
    if (policy == NULL) {
856
0
        return srtp_err_status_bad_param;
857
0
    }
858
859
24.6k
    if (policy->profile == srtp_profile_reserved) {
860
0
        return srtp_err_status_bad_param;
861
0
    }
862
863
24.6k
    if (policy->ssrc.type == ssrc_undefined) {
864
0
        return srtp_err_status_bad_param;
865
0
    }
866
867
24.6k
    bool null_cipher_null_auth = srtp_policy_is_null_cipher_null_auth(policy);
868
869
24.6k
    if (null_cipher_null_auth) {
870
12.1k
        if (policy->num_master_keys != 0 || policy->use_mki ||
871
12.1k
            policy->mki_size != 0) {
872
0
            return srtp_err_status_bad_param;
873
0
        }
874
12.4k
    } else if (policy->num_master_keys == 0) {
875
2.82k
        return srtp_err_status_bad_param;
876
2.82k
    }
877
878
21.7k
    if (policy->num_master_keys > SRTP_MAX_NUM_MASTER_KEYS) {
879
0
        return srtp_err_status_bad_param;
880
0
    }
881
882
21.7k
    if (policy->use_mki) {
883
3.42k
        if (policy->mki_size == 0 || policy->mki_size > SRTP_MAX_MKI_LEN) {
884
0
            return srtp_err_status_bad_param;
885
0
        }
886
18.3k
    } else if (policy->mki_size != 0) {
887
0
        return srtp_err_status_bad_param;
888
0
    }
889
890
21.7k
    if (!policy->use_mki && policy->num_master_keys > 1) {
891
0
        return srtp_err_status_bad_param;
892
0
    }
893
894
21.7k
    size_t expected_key_len =
895
21.7k
        srtp_profile_get_master_key_length(policy->profile);
896
21.7k
    size_t expected_salt_len =
897
21.7k
        srtp_profile_get_master_salt_length(policy->profile);
898
899
36.3k
    for (size_t i = 0; i < policy->num_master_keys; i++) {
900
14.8k
        if (policy->master_keys[i].key_len == 0) {
901
0
            return srtp_err_status_bad_param;
902
0
        }
903
14.8k
        if (policy->master_keys[i].key_len != expected_key_len ||
904
14.8k
            policy->master_keys[i].salt_len != expected_salt_len) {
905
0
            return srtp_err_status_bad_param;
906
0
        }
907
14.8k
        if (policy->use_mki &&
908
8.68k
            policy->mki_size != policy->master_keys[i].mki_id_len) {
909
277
            return srtp_err_status_bad_param;
910
277
        }
911
14.5k
        if (!policy->use_mki && policy->master_keys[i].mki_id_len != 0) {
912
0
            return srtp_err_status_bad_param;
913
0
        }
914
14.5k
    }
915
916
21.5k
    if (!srtp_policy_is_valid_window_size(policy->window_size)) {
917
0
        return srtp_err_status_bad_param;
918
0
    }
919
920
    // Not a valid combination
921
21.5k
    if (policy->enc_xtn_hdr_count > 0 && policy->use_cryptex) {
922
0
        return srtp_err_status_bad_param;
923
0
    }
924
925
    /*
926
     * RFC 4771 RCC: validate the mode against the configured cipher.  When RCC
927
     * is enabled the four-octet ROC is carried inside the SRTP authentication
928
     * tag, so for the HMAC modes the tag must be able to hold the ROC plus at
929
     * least one MAC octet.
930
     */
931
21.5k
    if (policy->rcc_mode != srtp_rcc_mode_none) {
932
0
        bool is_gcm = (policy->rtp.cipher_type == SRTP_AES_GCM_128 ||
933
0
                       policy->rtp.cipher_type == SRTP_AES_GCM_256);
934
935
        /* the transmission rate R must be >= 1 (see set_rcc_mode_tx_rate) */
936
0
        if (policy->roc_tx_rate == 0) {
937
0
            return srtp_err_status_bad_param;
938
0
        }
939
940
0
        switch (policy->rcc_mode) {
941
0
        case srtp_rcc_mode_1:
942
0
        case srtp_rcc_mode_2:
943
            /*
944
             * Modes 1 and 2 carry the ROC inside a truncated HMAC-SHA1 tag
945
             * (TAG = ROC || MAC_tr), so the tag must hold the 4-octet ROC plus
946
             * at least one MAC octet.  They are defined only for the AES-CM
947
             * ciphers with HMAC-SHA1, not for AEAD/GCM.
948
             */
949
0
            if (policy->rtp.auth_tag_len < 5 || is_gcm) {
950
0
                return srtp_err_status_bad_param;
951
0
            }
952
0
            break;
953
0
        case srtp_rcc_mode_3:
954
            /*
955
             * Mode 3 (RFC 4771 NULL-MAC) carries only the 4-octet ROC with no
956
             * MAC of its own.  It is supported here only on top of AES-GCM
957
             * (RFC 7714): the AEAD tag authenticates the packet and the ROC
958
             * occupies the SRTP authentication tag field, which RFC 7714
959
             * section 8.2 places after the optional MKI.  Because the carried
960
             * ROC also feeds the GCM IV, any tampering with it is detected by
961
             * GCM tag verification.
962
             */
963
0
            if (!is_gcm) {
964
0
                return srtp_err_status_bad_param;
965
0
            }
966
0
            break;
967
0
        default:
968
0
            return srtp_err_status_bad_param;
969
0
        }
970
0
    }
971
972
21.5k
    return srtp_err_status_ok;
973
21.5k
}
974
975
srtp_err_status_t srtp_policy_set_ssrc(srtp_policy_t policy, srtp_ssrc_t ssrc)
976
26.9k
{
977
26.9k
    if (policy == NULL) {
978
0
        return srtp_err_status_bad_param;
979
0
    }
980
981
26.9k
    if (ssrc.type != ssrc_any_inbound && ssrc.type != ssrc_any_outbound &&
982
9.78k
        ssrc.type != ssrc_specific) {
983
159
        return srtp_err_status_bad_param;
984
159
    }
985
986
26.8k
    policy->ssrc = ssrc;
987
988
26.8k
    return srtp_err_status_ok;
989
26.9k
}
990
991
srtp_err_status_t srtp_policy_set_profile(srtp_policy_t policy,
992
                                          srtp_profile_t profile)
993
27.0k
{
994
27.0k
    if (policy == NULL) {
995
0
        return srtp_err_status_bad_param;
996
0
    }
997
998
27.0k
    srtp_err_status_t status;
999
27.0k
    status = srtp_crypto_policy_set_from_profile_for_rtp(&policy->rtp, profile);
1000
27.0k
    if (status != srtp_err_status_ok) {
1001
54
        return status;
1002
54
    }
1003
26.9k
    status =
1004
26.9k
        srtp_crypto_policy_set_from_profile_for_rtcp(&policy->rtcp, profile);
1005
26.9k
    if (status != srtp_err_status_ok) {
1006
0
        return status;
1007
0
    }
1008
1009
26.9k
    policy->profile = profile;
1010
1011
26.9k
    return srtp_err_status_ok;
1012
26.9k
}
1013
1014
srtp_err_status_t srtp_policy_get_profile(srtp_policy_t policy,
1015
                                          srtp_profile_t *profile)
1016
0
{
1017
0
    if (policy == NULL || profile == NULL) {
1018
0
        return srtp_err_status_bad_param;
1019
0
    }
1020
1021
0
    *profile = policy->profile;
1022
1023
0
    return srtp_err_status_ok;
1024
0
}
1025
1026
srtp_err_status_t srtp_policy_set_sec_serv(srtp_policy_t policy,
1027
                                           srtp_sec_serv_t rtp_sec_serv,
1028
                                           srtp_sec_serv_t rtcp_sec_serv)
1029
0
{
1030
0
    if (policy == NULL) {
1031
0
        return srtp_err_status_bad_param;
1032
0
    }
1033
1034
0
    if (policy->profile == srtp_profile_reserved) {
1035
0
        return srtp_err_status_bad_param;
1036
0
    }
1037
1038
0
    policy->rtp.sec_serv = rtp_sec_serv;
1039
0
    policy->rtcp.sec_serv = rtcp_sec_serv;
1040
1041
0
    return srtp_err_status_ok;
1042
0
}
1043
1044
srtp_err_status_t srtp_policy_use_mki(srtp_policy_t policy, size_t mki_len)
1045
10.3k
{
1046
10.3k
    if (policy == NULL) {
1047
0
        return srtp_err_status_bad_param;
1048
0
    }
1049
1050
10.3k
    if (mki_len > SRTP_MAX_MKI_LEN) {
1051
0
        return srtp_err_status_bad_param;
1052
0
    }
1053
1054
10.3k
    policy->use_mki = mki_len != 0;
1055
10.3k
    policy->mki_size = mki_len;
1056
1057
10.3k
    return srtp_err_status_ok;
1058
10.3k
}
1059
1060
srtp_err_status_t srtp_policy_get_mki_length(srtp_policy_t policy,
1061
                                             size_t *mki_len)
1062
0
{
1063
0
    if (policy == NULL || mki_len == NULL) {
1064
0
        return srtp_err_status_bad_param;
1065
0
    }
1066
1067
0
    *mki_len = policy->mki_size;
1068
1069
0
    return srtp_err_status_ok;
1070
0
}
1071
1072
srtp_err_status_t srtp_policy_set_rcc_mode_tx_rate(srtp_policy_t policy,
1073
                                                   srtp_rcc_mode_t rcc_mode,
1074
                                                   uint16_t roc_tx_rate)
1075
0
{
1076
0
    if (policy == NULL) {
1077
0
        return srtp_err_status_bad_param;
1078
0
    }
1079
1080
0
    switch (rcc_mode) {
1081
0
    case srtp_rcc_mode_none:
1082
0
    case srtp_rcc_mode_1:
1083
0
    case srtp_rcc_mode_2:
1084
0
    case srtp_rcc_mode_3:
1085
0
        break;
1086
0
    default:
1087
0
        return srtp_err_status_bad_param;
1088
0
    }
1089
1090
    /*
1091
     * The transmission rate R selects which packets carry the ROC (those
1092
     * whose sequence number is 0 modulo R), so R == 0 is meaningless when RCC
1093
     * is enabled; require R >= 1.  The rate is ignored when RCC is disabled.
1094
     * Cipher/mode consistency (AES-CM vs AES-GCM, tag length) is enforced by
1095
     * srtp_policy_validate() once the profile is known.
1096
     */
1097
0
    if (rcc_mode != srtp_rcc_mode_none && roc_tx_rate == 0) {
1098
0
        return srtp_err_status_bad_param;
1099
0
    }
1100
1101
0
    policy->rcc_mode = rcc_mode;
1102
0
    policy->roc_tx_rate = roc_tx_rate;
1103
1104
0
    return srtp_err_status_ok;
1105
0
}
1106
1107
srtp_err_status_t srtp_policy_add_key(srtp_policy_t policy,
1108
                                      const uint8_t *key,
1109
                                      size_t key_len,
1110
                                      const uint8_t *salt,
1111
                                      size_t salt_len,
1112
                                      const uint8_t *mki,
1113
                                      size_t mki_len)
1114
18.9k
{
1115
18.9k
    if (policy == NULL) {
1116
0
        return srtp_err_status_bad_param;
1117
0
    }
1118
18.9k
    if (key == NULL || salt == NULL) {
1119
0
        return srtp_err_status_bad_param;
1120
0
    }
1121
18.9k
    if (mki_len > 0 && mki == NULL) {
1122
0
        return srtp_err_status_bad_param;
1123
0
    }
1124
1125
18.9k
    if (policy->use_mki) {
1126
10.8k
        if (mki_len != policy->mki_size) {
1127
471
            return srtp_err_status_bad_param;
1128
471
        }
1129
10.8k
    } else {
1130
8.05k
        if (mki_len != 0) {
1131
0
            return srtp_err_status_bad_param;
1132
0
        }
1133
8.05k
        if (policy->num_master_keys > 0) {
1134
557
            return srtp_err_status_bad_param;
1135
557
        }
1136
8.05k
    }
1137
1138
17.8k
    if (key_len + salt_len > SRTP_MAX_KEY_LEN) {
1139
0
        return srtp_err_status_bad_param;
1140
0
    }
1141
1142
17.8k
    if (mki_len > SRTP_MAX_MKI_LEN) {
1143
0
        return srtp_err_status_bad_param;
1144
0
    }
1145
1146
17.8k
    if (policy->num_master_keys >= SRTP_MAX_NUM_MASTER_KEYS) {
1147
66
        return srtp_err_status_bad_param;
1148
66
    }
1149
1150
17.8k
    size_t key_index = policy->num_master_keys;
1151
17.8k
    memcpy(policy->master_keys[key_index].key, key, key_len);
1152
17.8k
    policy->master_keys[key_index].key_len = key_len;
1153
17.8k
    memcpy(policy->master_keys[key_index].key + key_len, salt, salt_len);
1154
17.8k
    policy->master_keys[key_index].salt_len = salt_len;
1155
17.8k
    if (mki_len > 0) {
1156
10.3k
        memcpy(policy->master_keys[key_index].mki_id, mki, mki_len);
1157
10.3k
    }
1158
17.8k
    policy->master_keys[key_index].mki_id_len = mki_len;
1159
17.8k
    policy->num_master_keys++;
1160
1161
17.8k
    return srtp_err_status_ok;
1162
17.8k
}
1163
1164
srtp_err_status_t srtp_policy_remove_keys(srtp_policy_t policy)
1165
0
{
1166
0
    if (policy == NULL) {
1167
0
        return srtp_err_status_bad_param;
1168
0
    }
1169
1170
0
    octet_string_set_to_zero(policy->master_keys, sizeof(policy->master_keys));
1171
0
    policy->num_master_keys = 0;
1172
1173
0
    return srtp_err_status_ok;
1174
0
}
1175
1176
srtp_err_status_t srtp_policy_set_window_size(srtp_policy_t policy,
1177
                                              size_t window_size)
1178
26.5k
{
1179
26.5k
    if (policy == NULL) {
1180
0
        return srtp_err_status_bad_param;
1181
0
    }
1182
1183
26.5k
    if (!srtp_policy_is_valid_window_size(window_size)) {
1184
401
        return srtp_err_status_bad_param;
1185
401
    }
1186
1187
26.1k
    policy->window_size = window_size;
1188
1189
26.1k
    return srtp_err_status_ok;
1190
26.5k
}
1191
1192
srtp_err_status_t srtp_policy_set_allow_repeat_tx(srtp_policy_t policy,
1193
                                                  bool allow)
1194
26.1k
{
1195
26.1k
    if (policy == NULL) {
1196
0
        return srtp_err_status_bad_param;
1197
0
    }
1198
1199
26.1k
    policy->allow_repeat_tx = allow;
1200
1201
26.1k
    return srtp_err_status_ok;
1202
26.1k
}
1203
1204
srtp_err_status_t srtp_policy_set_cryptex(srtp_policy_t policy,
1205
                                          bool use_cryptex)
1206
0
{
1207
0
    if (policy == NULL) {
1208
0
        return srtp_err_status_bad_param;
1209
0
    }
1210
1211
0
    policy->use_cryptex = use_cryptex;
1212
1213
0
    return srtp_err_status_ok;
1214
0
}
1215
1216
srtp_err_status_t srtp_policy_add_enc_hdr_xtnd_id(srtp_policy_t policy,
1217
                                                  uint8_t hdr_xtnd_id)
1218
57.3k
{
1219
57.3k
    if (policy == NULL) {
1220
0
        return srtp_err_status_bad_param;
1221
0
    }
1222
1223
57.3k
    if (policy->enc_xtn_hdr_count >= SRTP_MAX_NUM_ENC_HDR_XTND_IDS) {
1224
0
        return srtp_err_status_bad_param;
1225
0
    }
1226
1227
176k
    for (size_t i = 0; i < policy->enc_xtn_hdr_count; i++) {
1228
145k
        if (policy->enc_xtn_hdr[i] == hdr_xtnd_id) {
1229
26.9k
            return srtp_err_status_bad_param;
1230
26.9k
        }
1231
145k
    }
1232
1233
30.4k
    policy->enc_xtn_hdr[policy->enc_xtn_hdr_count] = hdr_xtnd_id;
1234
30.4k
    policy->enc_xtn_hdr_count++;
1235
1236
30.4k
    return srtp_err_status_ok;
1237
57.3k
}
1238
1239
srtp_err_status_t srtp_policy_remove_enc_hdr_xtnd_ids(srtp_policy_t policy)
1240
0
{
1241
0
    if (policy == NULL) {
1242
0
        return srtp_err_status_bad_param;
1243
0
    }
1244
1245
0
    octet_string_set_to_zero(policy->enc_xtn_hdr, sizeof(policy->enc_xtn_hdr));
1246
0
    policy->enc_xtn_hdr_count = 0;
1247
1248
0
    return srtp_err_status_ok;
1249
0
}