/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 | 1.14k | 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 | 1.14k | { |
494 | 1.14k | p->cipher_type = SRTP_AES_ICM_128; |
495 | 1.14k | p->cipher_key_len = |
496 | 1.14k | SRTP_AES_ICM_128_KEY_LEN_WSALT; /* default 128 bits per RFC 3711 */ |
497 | 1.14k | p->auth_type = SRTP_HMAC_SHA1; |
498 | 1.14k | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
499 | 1.14k | p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */ |
500 | 1.14k | p->sec_serv = sec_serv_conf_and_auth; |
501 | 1.14k | } |
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 | 382 | { |
516 | | /* |
517 | | * corresponds to RFC 4568 |
518 | | * |
519 | | * note that this crypto policy is intended for SRTP, but not SRTCP |
520 | | */ |
521 | | |
522 | 382 | p->cipher_type = SRTP_AES_ICM_128; |
523 | 382 | p->cipher_key_len = |
524 | 382 | SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */ |
525 | 382 | p->auth_type = SRTP_HMAC_SHA1; |
526 | 382 | p->auth_key_len = 20; /* 160 bit key */ |
527 | 382 | p->auth_tag_len = 4; /* 32 bit tag */ |
528 | 382 | p->sec_serv = sec_serv_conf_and_auth; |
529 | 382 | } |
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 | 2.15k | { |
550 | | /* |
551 | | * corresponds to RFC 4568 |
552 | | */ |
553 | | |
554 | 2.15k | p->cipher_type = SRTP_NULL_CIPHER; |
555 | 2.15k | p->cipher_key_len = |
556 | 2.15k | SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */ |
557 | 2.15k | p->auth_type = SRTP_HMAC_SHA1; |
558 | 2.15k | p->auth_key_len = 20; |
559 | 2.15k | p->auth_tag_len = 10; |
560 | 2.15k | p->sec_serv = sec_serv_auth; |
561 | 2.15k | } |
562 | | |
563 | | void srtp_crypto_policy_set_null_cipher_hmac_null(srtp_crypto_policy_t *p) |
564 | 2.95k | { |
565 | | /* |
566 | | * Should only be used for testing |
567 | | */ |
568 | | |
569 | 2.95k | p->cipher_type = SRTP_NULL_CIPHER; |
570 | 2.95k | p->cipher_key_len = 0; |
571 | 2.95k | p->auth_type = SRTP_NULL_AUTH; |
572 | 2.95k | p->auth_key_len = 0; |
573 | 2.95k | p->auth_tag_len = 0; |
574 | 2.95k | p->sec_serv = sec_serv_none; |
575 | 2.95k | } |
576 | | |
577 | | void srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(srtp_crypto_policy_t *p) |
578 | 1.78k | { |
579 | | /* |
580 | | * corresponds to RFC 6188 |
581 | | */ |
582 | | |
583 | 1.78k | p->cipher_type = SRTP_AES_ICM_256; |
584 | 1.78k | p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT; |
585 | 1.78k | p->auth_type = SRTP_HMAC_SHA1; |
586 | 1.78k | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
587 | 1.78k | p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */ |
588 | 1.78k | p->sec_serv = sec_serv_conf_and_auth; |
589 | 1.78k | } |
590 | | |
591 | | void srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(srtp_crypto_policy_t *p) |
592 | 518 | { |
593 | | /* |
594 | | * corresponds to RFC 6188 |
595 | | * |
596 | | * note that this crypto policy is intended for SRTP, but not SRTCP |
597 | | */ |
598 | | |
599 | 518 | p->cipher_type = SRTP_AES_ICM_256; |
600 | 518 | p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT; |
601 | 518 | p->auth_type = SRTP_HMAC_SHA1; |
602 | 518 | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
603 | 518 | p->auth_tag_len = 4; /* default 80 bits per RFC 3711 */ |
604 | 518 | p->sec_serv = sec_serv_conf_and_auth; |
605 | 518 | } |
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 | 564 | { |
622 | | /* |
623 | | * corresponds to RFC 6188 |
624 | | */ |
625 | | |
626 | 564 | p->cipher_type = SRTP_AES_ICM_192; |
627 | 564 | p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT; |
628 | 564 | p->auth_type = SRTP_HMAC_SHA1; |
629 | 564 | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
630 | 564 | p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */ |
631 | 564 | p->sec_serv = sec_serv_conf_and_auth; |
632 | 564 | } |
633 | | |
634 | | void srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(srtp_crypto_policy_t *p) |
635 | 154 | { |
636 | | /* |
637 | | * corresponds to RFC 6188 |
638 | | * |
639 | | * note that this crypto policy is intended for SRTP, but not SRTCP |
640 | | */ |
641 | | |
642 | 154 | p->cipher_type = SRTP_AES_ICM_192; |
643 | 154 | p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT; |
644 | 154 | p->auth_type = SRTP_HMAC_SHA1; |
645 | 154 | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
646 | 154 | p->auth_tag_len = 4; /* default 80 bits per RFC 3711 */ |
647 | 154 | p->sec_serv = sec_serv_conf_and_auth; |
648 | 154 | } |
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 | 5.71k | { |
693 | | /* set SRTP policy from the SRTP profile in the key set */ |
694 | 5.71k | switch (profile) { |
695 | 0 | case srtp_profile_reserved: |
696 | 0 | return srtp_err_status_bad_param; |
697 | 1.47k | case srtp_profile_null_null: |
698 | 1.47k | srtp_crypto_policy_set_null_cipher_hmac_null(policy); |
699 | 1.47k | return srtp_err_status_ok; |
700 | 382 | case srtp_profile_aes128_cm_sha1_80: |
701 | 382 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); |
702 | 382 | return srtp_err_status_ok; |
703 | 382 | case srtp_profile_aes128_cm_sha1_32: |
704 | 382 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(policy); |
705 | 382 | return srtp_err_status_ok; |
706 | 205 | case srtp_profile_aes192_cm_sha1_80: |
707 | 205 | srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(policy); |
708 | 205 | return srtp_err_status_ok; |
709 | 154 | case srtp_profile_aes192_cm_sha1_32: |
710 | 154 | srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(policy); |
711 | 154 | return srtp_err_status_ok; |
712 | 634 | case srtp_profile_aes256_cm_sha1_80: |
713 | 634 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy); |
714 | 634 | return srtp_err_status_ok; |
715 | 518 | case srtp_profile_aes256_cm_sha1_32: |
716 | 518 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(policy); |
717 | 518 | return srtp_err_status_ok; |
718 | 418 | case srtp_profile_null_sha1_80: |
719 | 418 | srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy); |
720 | 418 | 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 | 561 | case srtp_profile_aead_aes_128_gcm: |
730 | 561 | return srtp_err_status_bad_param; |
731 | 318 | case srtp_profile_aead_aes_256_gcm: |
732 | 318 | return srtp_err_status_bad_param; |
733 | 0 | #endif |
734 | 661 | case srtp_profile_null_sha1_32: |
735 | 661 | srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy); |
736 | 661 | policy->auth_tag_len = 4; |
737 | 661 | return srtp_err_status_ok; |
738 | 5.71k | } |
739 | | |
740 | 0 | return srtp_err_status_bad_param; |
741 | 5.71k | } |
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 | 4.83k | { |
747 | | /* set SRTP policy from the SRTP profile in the key set */ |
748 | 4.83k | switch (profile) { |
749 | 0 | case srtp_profile_reserved: |
750 | 0 | return srtp_err_status_bad_param; |
751 | 1.47k | case srtp_profile_null_null: |
752 | 1.47k | srtp_crypto_policy_set_null_cipher_hmac_null(policy); |
753 | 1.47k | return srtp_err_status_ok; |
754 | 382 | case srtp_profile_aes128_cm_sha1_80: |
755 | 382 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); |
756 | 382 | return srtp_err_status_ok; |
757 | 382 | 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 | 382 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); |
761 | 382 | return srtp_err_status_ok; |
762 | 205 | case srtp_profile_aes192_cm_sha1_80: |
763 | 205 | srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(policy); |
764 | 205 | return srtp_err_status_ok; |
765 | 154 | 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 | 154 | srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(policy); |
769 | 154 | return srtp_err_status_ok; |
770 | 634 | case srtp_profile_aes256_cm_sha1_80: |
771 | 634 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy); |
772 | 634 | return srtp_err_status_ok; |
773 | 518 | 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 | 518 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy); |
777 | 518 | return srtp_err_status_ok; |
778 | 418 | case srtp_profile_null_sha1_80: |
779 | 418 | srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy); |
780 | 418 | 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 | 661 | 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 | 661 | srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy); |
798 | 661 | return srtp_err_status_ok; |
799 | 4.83k | } |
800 | | |
801 | 0 | return srtp_err_status_bad_param; |
802 | 4.83k | } |
803 | | |
804 | | srtp_err_status_t srtp_policy_create(srtp_policy_t *policy) |
805 | 5.71k | { |
806 | 5.71k | srtp_policy_t p; |
807 | | |
808 | 5.71k | if (policy == NULL) { |
809 | 0 | return srtp_err_status_bad_param; |
810 | 0 | } |
811 | | |
812 | 5.71k | p = (srtp_policy_t)srtp_crypto_alloc(sizeof(*p)); |
813 | 5.71k | if (p == NULL) { |
814 | 0 | *policy = NULL; |
815 | 0 | return srtp_err_status_alloc_fail; |
816 | 0 | } |
817 | | |
818 | 5.71k | *policy = p; |
819 | | |
820 | 5.71k | return srtp_err_status_ok; |
821 | 5.71k | } |
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 | 10.4k | { |
845 | 10.4k | if (policy == NULL) { |
846 | 4.74k | return; |
847 | 4.74k | } |
848 | | |
849 | 5.71k | octet_string_set_to_zero(policy->master_keys, sizeof(policy->master_keys)); |
850 | 5.71k | srtp_crypto_free(policy); |
851 | 5.71k | } |
852 | | |
853 | | srtp_err_status_t srtp_policy_validate(srtp_policy_t policy) |
854 | 4.90k | { |
855 | 4.90k | if (policy == NULL) { |
856 | 0 | return srtp_err_status_bad_param; |
857 | 0 | } |
858 | | |
859 | 4.90k | if (policy->profile == srtp_profile_reserved) { |
860 | 0 | return srtp_err_status_bad_param; |
861 | 0 | } |
862 | | |
863 | 4.90k | if (policy->ssrc.type == ssrc_undefined) { |
864 | 0 | return srtp_err_status_bad_param; |
865 | 0 | } |
866 | | |
867 | 4.90k | bool null_cipher_null_auth = srtp_policy_is_null_cipher_null_auth(policy); |
868 | | |
869 | 4.90k | if (null_cipher_null_auth) { |
870 | 773 | if (policy->num_master_keys != 0 || policy->use_mki || |
871 | 773 | policy->mki_size != 0) { |
872 | 0 | return srtp_err_status_bad_param; |
873 | 0 | } |
874 | 4.13k | } else if (policy->num_master_keys == 0) { |
875 | 53 | return srtp_err_status_bad_param; |
876 | 53 | } |
877 | | |
878 | 4.85k | if (policy->num_master_keys > SRTP_MAX_NUM_MASTER_KEYS) { |
879 | 0 | return srtp_err_status_bad_param; |
880 | 0 | } |
881 | | |
882 | 4.85k | if (policy->use_mki) { |
883 | 17 | if (policy->mki_size == 0 || policy->mki_size > SRTP_MAX_MKI_LEN) { |
884 | 0 | return srtp_err_status_bad_param; |
885 | 0 | } |
886 | 4.83k | } else if (policy->mki_size != 0) { |
887 | 0 | return srtp_err_status_bad_param; |
888 | 0 | } |
889 | | |
890 | 4.85k | if (!policy->use_mki && policy->num_master_keys > 1) { |
891 | 0 | return srtp_err_status_bad_param; |
892 | 0 | } |
893 | | |
894 | 4.85k | size_t expected_key_len = |
895 | 4.85k | srtp_profile_get_master_key_length(policy->profile); |
896 | 4.85k | size_t expected_salt_len = |
897 | 4.85k | srtp_profile_get_master_salt_length(policy->profile); |
898 | | |
899 | 8.92k | for (size_t i = 0; i < policy->num_master_keys; i++) { |
900 | 4.08k | if (policy->master_keys[i].key_len == 0) { |
901 | 0 | return srtp_err_status_bad_param; |
902 | 0 | } |
903 | 4.08k | if (policy->master_keys[i].key_len != expected_key_len || |
904 | 4.08k | policy->master_keys[i].salt_len != expected_salt_len) { |
905 | 0 | return srtp_err_status_bad_param; |
906 | 0 | } |
907 | 4.08k | if (policy->use_mki && |
908 | 17 | policy->mki_size != policy->master_keys[i].mki_id_len) { |
909 | 11 | return srtp_err_status_bad_param; |
910 | 11 | } |
911 | 4.06k | if (!policy->use_mki && policy->master_keys[i].mki_id_len != 0) { |
912 | 0 | return srtp_err_status_bad_param; |
913 | 0 | } |
914 | 4.06k | } |
915 | | |
916 | 4.84k | 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 | 4.84k | if (policy->enc_xtn_hdr_count > 0 && policy->use_cryptex) { |
922 | 0 | return srtp_err_status_bad_param; |
923 | 0 | } |
924 | | |
925 | 4.84k | return srtp_err_status_ok; |
926 | 4.84k | } |
927 | | |
928 | | srtp_err_status_t srtp_policy_set_ssrc(srtp_policy_t policy, srtp_ssrc_t ssrc) |
929 | 4.83k | { |
930 | 4.83k | if (policy == NULL) { |
931 | 0 | return srtp_err_status_bad_param; |
932 | 0 | } |
933 | | |
934 | 4.83k | if (ssrc.type != ssrc_any_inbound && ssrc.type != ssrc_any_outbound && |
935 | 1.96k | ssrc.type != ssrc_specific) { |
936 | 1.17k | return srtp_err_status_bad_param; |
937 | 1.17k | } |
938 | | |
939 | 3.65k | policy->ssrc = ssrc; |
940 | | |
941 | 3.65k | return srtp_err_status_ok; |
942 | 4.83k | } |
943 | | |
944 | | srtp_err_status_t srtp_policy_set_profile(srtp_policy_t policy, |
945 | | srtp_profile_t profile) |
946 | 5.71k | { |
947 | 5.71k | if (policy == NULL) { |
948 | 0 | return srtp_err_status_bad_param; |
949 | 0 | } |
950 | | |
951 | 5.71k | srtp_err_status_t status; |
952 | 5.71k | status = srtp_crypto_policy_set_from_profile_for_rtp(&policy->rtp, profile); |
953 | 5.71k | if (status != srtp_err_status_ok) { |
954 | 879 | return status; |
955 | 879 | } |
956 | 4.83k | status = |
957 | 4.83k | srtp_crypto_policy_set_from_profile_for_rtcp(&policy->rtcp, profile); |
958 | 4.83k | if (status != srtp_err_status_ok) { |
959 | 0 | return status; |
960 | 0 | } |
961 | | |
962 | 4.83k | policy->profile = profile; |
963 | | |
964 | 4.83k | return srtp_err_status_ok; |
965 | 4.83k | } |
966 | | |
967 | | srtp_err_status_t srtp_policy_get_profile(srtp_policy_t policy, |
968 | | srtp_profile_t *profile) |
969 | 0 | { |
970 | 0 | if (policy == NULL || profile == NULL) { |
971 | 0 | return srtp_err_status_bad_param; |
972 | 0 | } |
973 | | |
974 | 0 | *profile = policy->profile; |
975 | |
|
976 | 0 | return srtp_err_status_ok; |
977 | 0 | } |
978 | | |
979 | | srtp_err_status_t srtp_policy_set_sec_serv(srtp_policy_t policy, |
980 | | srtp_sec_serv_t rtp_sec_serv, |
981 | | srtp_sec_serv_t rtcp_sec_serv) |
982 | 0 | { |
983 | 0 | if (policy == NULL) { |
984 | 0 | return srtp_err_status_bad_param; |
985 | 0 | } |
986 | | |
987 | 0 | if (policy->profile == srtp_profile_reserved) { |
988 | 0 | return srtp_err_status_bad_param; |
989 | 0 | } |
990 | | |
991 | 0 | policy->rtp.sec_serv = rtp_sec_serv; |
992 | 0 | policy->rtcp.sec_serv = rtcp_sec_serv; |
993 | |
|
994 | 0 | return srtp_err_status_ok; |
995 | 0 | } |
996 | | |
997 | | srtp_err_status_t srtp_policy_use_mki(srtp_policy_t policy, size_t mki_len) |
998 | 17 | { |
999 | 17 | if (policy == NULL) { |
1000 | 0 | return srtp_err_status_bad_param; |
1001 | 0 | } |
1002 | | |
1003 | 17 | if (mki_len > SRTP_MAX_MKI_LEN) { |
1004 | 0 | return srtp_err_status_bad_param; |
1005 | 0 | } |
1006 | | |
1007 | 17 | policy->use_mki = mki_len != 0; |
1008 | 17 | policy->mki_size = mki_len; |
1009 | | |
1010 | 17 | return srtp_err_status_ok; |
1011 | 17 | } |
1012 | | |
1013 | | srtp_err_status_t srtp_policy_get_mki_length(srtp_policy_t policy, |
1014 | | size_t *mki_len) |
1015 | 0 | { |
1016 | 0 | if (policy == NULL || mki_len == NULL) { |
1017 | 0 | return srtp_err_status_bad_param; |
1018 | 0 | } |
1019 | | |
1020 | 0 | *mki_len = policy->mki_size; |
1021 | |
|
1022 | 0 | return srtp_err_status_ok; |
1023 | 0 | } |
1024 | | |
1025 | | srtp_err_status_t srtp_policy_add_key(srtp_policy_t policy, |
1026 | | const uint8_t *key, |
1027 | | size_t key_len, |
1028 | | const uint8_t *salt, |
1029 | | size_t salt_len, |
1030 | | const uint8_t *mki, |
1031 | | size_t mki_len) |
1032 | 2.40k | { |
1033 | 2.40k | if (policy == NULL) { |
1034 | 0 | return srtp_err_status_bad_param; |
1035 | 0 | } |
1036 | 2.40k | if (key == NULL || salt == NULL) { |
1037 | 0 | return srtp_err_status_bad_param; |
1038 | 0 | } |
1039 | 2.40k | if (mki_len > 0 && mki == NULL) { |
1040 | 0 | return srtp_err_status_bad_param; |
1041 | 0 | } |
1042 | | |
1043 | 2.40k | if (policy->use_mki) { |
1044 | 18 | if (mki_len != policy->mki_size) { |
1045 | 1 | return srtp_err_status_bad_param; |
1046 | 1 | } |
1047 | 2.38k | } else { |
1048 | 2.38k | if (mki_len != 0) { |
1049 | 0 | return srtp_err_status_bad_param; |
1050 | 0 | } |
1051 | 2.38k | if (policy->num_master_keys > 0) { |
1052 | 90 | return srtp_err_status_bad_param; |
1053 | 90 | } |
1054 | 2.38k | } |
1055 | | |
1056 | 2.30k | if (key_len + salt_len > SRTP_MAX_KEY_LEN) { |
1057 | 0 | return srtp_err_status_bad_param; |
1058 | 0 | } |
1059 | | |
1060 | 2.30k | if (mki_len > SRTP_MAX_MKI_LEN) { |
1061 | 0 | return srtp_err_status_bad_param; |
1062 | 0 | } |
1063 | | |
1064 | 2.30k | if (policy->num_master_keys >= SRTP_MAX_NUM_MASTER_KEYS) { |
1065 | 0 | return srtp_err_status_bad_param; |
1066 | 0 | } |
1067 | | |
1068 | 2.30k | size_t key_index = policy->num_master_keys; |
1069 | 2.30k | memcpy(policy->master_keys[key_index].key, key, key_len); |
1070 | 2.30k | policy->master_keys[key_index].key_len = key_len; |
1071 | 2.30k | memcpy(policy->master_keys[key_index].key + key_len, salt, salt_len); |
1072 | 2.30k | policy->master_keys[key_index].salt_len = salt_len; |
1073 | 2.30k | if (mki_len > 0) { |
1074 | 17 | memcpy(policy->master_keys[key_index].mki_id, mki, mki_len); |
1075 | 17 | } |
1076 | 2.30k | policy->master_keys[key_index].mki_id_len = mki_len; |
1077 | 2.30k | policy->num_master_keys++; |
1078 | | |
1079 | 2.30k | return srtp_err_status_ok; |
1080 | 2.30k | } |
1081 | | |
1082 | | srtp_err_status_t srtp_policy_remove_keys(srtp_policy_t policy) |
1083 | 0 | { |
1084 | 0 | if (policy == NULL) { |
1085 | 0 | return srtp_err_status_bad_param; |
1086 | 0 | } |
1087 | | |
1088 | 0 | octet_string_set_to_zero(policy->master_keys, sizeof(policy->master_keys)); |
1089 | 0 | policy->num_master_keys = 0; |
1090 | |
|
1091 | 0 | return srtp_err_status_ok; |
1092 | 0 | } |
1093 | | |
1094 | | srtp_err_status_t srtp_policy_set_window_size(srtp_policy_t policy, |
1095 | | size_t window_size) |
1096 | 3.33k | { |
1097 | 3.33k | if (policy == NULL) { |
1098 | 0 | return srtp_err_status_bad_param; |
1099 | 0 | } |
1100 | | |
1101 | 3.33k | if (!srtp_policy_is_valid_window_size(window_size)) { |
1102 | 835 | return srtp_err_status_bad_param; |
1103 | 835 | } |
1104 | | |
1105 | 2.49k | policy->window_size = window_size; |
1106 | | |
1107 | 2.49k | return srtp_err_status_ok; |
1108 | 3.33k | } |
1109 | | |
1110 | | srtp_err_status_t srtp_policy_set_allow_repeat_tx(srtp_policy_t policy, |
1111 | | bool allow) |
1112 | 2.49k | { |
1113 | 2.49k | if (policy == NULL) { |
1114 | 0 | return srtp_err_status_bad_param; |
1115 | 0 | } |
1116 | | |
1117 | 2.49k | policy->allow_repeat_tx = allow; |
1118 | | |
1119 | 2.49k | return srtp_err_status_ok; |
1120 | 2.49k | } |
1121 | | |
1122 | | srtp_err_status_t srtp_policy_set_cryptex(srtp_policy_t policy, |
1123 | | bool use_cryptex) |
1124 | 0 | { |
1125 | 0 | if (policy == NULL) { |
1126 | 0 | return srtp_err_status_bad_param; |
1127 | 0 | } |
1128 | | |
1129 | 0 | policy->use_cryptex = use_cryptex; |
1130 | |
|
1131 | 0 | return srtp_err_status_ok; |
1132 | 0 | } |
1133 | | |
1134 | | srtp_err_status_t srtp_policy_add_enc_hdr_xtnd_id(srtp_policy_t policy, |
1135 | | uint8_t hdr_xtnd_id) |
1136 | 4.49k | { |
1137 | 4.49k | if (policy == NULL) { |
1138 | 0 | return srtp_err_status_bad_param; |
1139 | 0 | } |
1140 | | |
1141 | 4.49k | if (policy->enc_xtn_hdr_count >= SRTP_MAX_NUM_ENC_HDR_XTND_IDS) { |
1142 | 0 | return srtp_err_status_bad_param; |
1143 | 0 | } |
1144 | | |
1145 | 14.5k | for (size_t i = 0; i < policy->enc_xtn_hdr_count; i++) { |
1146 | 11.8k | if (policy->enc_xtn_hdr[i] == hdr_xtnd_id) { |
1147 | 1.81k | return srtp_err_status_bad_param; |
1148 | 1.81k | } |
1149 | 11.8k | } |
1150 | | |
1151 | 2.68k | policy->enc_xtn_hdr[policy->enc_xtn_hdr_count] = hdr_xtnd_id; |
1152 | 2.68k | policy->enc_xtn_hdr_count++; |
1153 | | |
1154 | 2.68k | return srtp_err_status_ok; |
1155 | 4.49k | } |
1156 | | |
1157 | | srtp_err_status_t srtp_policy_remove_enc_hdr_xtnd_ids(srtp_policy_t policy) |
1158 | 0 | { |
1159 | 0 | if (policy == NULL) { |
1160 | 0 | return srtp_err_status_bad_param; |
1161 | 0 | } |
1162 | | |
1163 | 0 | octet_string_set_to_zero(policy->enc_xtn_hdr, sizeof(policy->enc_xtn_hdr)); |
1164 | 0 | policy->enc_xtn_hdr_count = 0; |
1165 | |
|
1166 | 0 | return srtp_err_status_ok; |
1167 | 0 | } |