Asterisk - The Open Source Telephony Project  18.5.0
res_srtp.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005, Mikael Magnusson
5  *
6  * Mikael Magnusson <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  *
18  * Builds on libSRTP http://srtp.sourceforge.net
19  */
20 
21 /*! \file res_srtp.c
22  *
23  * \brief Secure RTP (SRTP)
24  *
25  * Secure RTP (SRTP)
26  * Specified in RFC 3711.
27  *
28  * \author Mikael Magnusson <[email protected]>
29  */
30 
31 /*** MODULEINFO
32  <depend>srtp</depend>
33  <use type="external">openssl</use>
34  <support_level>core</support_level>
35 ***/
36 
37 /* See https://wiki.asterisk.org/wiki/display/AST/Secure+Calling */
38 
39 #include "asterisk.h" /* for NULL, size_t, memcpy, etc */
40 
41 #include <math.h> /* for pow */
42 
43 #if HAVE_SRTP_VERSION > 1
44 # include <srtp2/srtp.h>
45 # include "srtp/srtp_compat.h"
46 # include <openssl/rand.h>
47 #else
48 # include <srtp/srtp.h>
49 # ifdef HAVE_OPENSSL
50 # include <openssl/rand.h>
51 # else
52 # include <srtp/crypto_kernel.h>
53 # endif
54 #endif
55 
56 #include "asterisk/astobj2.h" /* for ao2_t_ref, etc */
57 #include "asterisk/frame.h" /* for AST_FRIENDLY_OFFSET */
58 #include "asterisk/logger.h" /* for ast_log, ast_debug, etc */
59 #include "asterisk/module.h" /* for ast_module_info, etc */
60 #include "asterisk/sdp_srtp.h"
61 #include "asterisk/res_srtp.h" /* for ast_srtp_cb, ast_srtp_suite, etc */
62 #include "asterisk/rtp_engine.h" /* for ast_rtp_engine_register_srtp, etc */
63 #include "asterisk/utils.h" /* for ast_free, ast_calloc */
64 
65 struct ast_srtp {
68  srtp_t session;
69  const struct ast_srtp_cb *cb;
70  void *data;
71  int warned;
72  unsigned char buf[8192 + AST_FRIENDLY_OFFSET];
73  unsigned char rtcpbuf[8192 + AST_FRIENDLY_OFFSET];
74 };
75 
77  srtp_policy_t sp;
78 };
79 
80 /*! Tracks whether or not we've initialized the libsrtp library */
81 static int g_initialized = 0;
82 
83 /* SRTP functions */
84 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
85 static int ast_srtp_replace(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
86 static void ast_srtp_destroy(struct ast_srtp *srtp);
87 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy);
88 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc);
89 
90 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp);
91 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp);
92 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data);
93 static int ast_srtp_get_random(unsigned char *key, size_t len);
94 
95 /* Policy functions */
96 static struct ast_srtp_policy *ast_srtp_policy_alloc(void);
97 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy);
98 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite);
99 static int ast_srtp_policy_set_master_key(struct ast_srtp_policy *policy, const unsigned char *key, size_t key_len, const unsigned char *salt, size_t salt_len);
100 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound);
101 
102 static struct ast_srtp_res srtp_res = {
104  .replace = ast_srtp_replace,
105  .destroy = ast_srtp_destroy,
106  .add_stream = ast_srtp_add_stream,
107  .change_source = ast_srtp_change_source,
108  .set_cb = ast_srtp_set_cb,
109  .unprotect = ast_srtp_unprotect,
110  .protect = ast_srtp_protect,
111  .get_random = ast_srtp_get_random
112 };
113 
116  .destroy = ast_srtp_policy_destroy,
117  .set_suite = ast_srtp_policy_set_suite,
118  .set_master_key = ast_srtp_policy_set_master_key,
119  .set_ssrc = ast_srtp_policy_set_ssrc
120 };
121 
122 static const char *srtp_errstr(int err)
123 {
124  switch(err) {
125  case err_status_ok:
126  return "nothing to report";
127  case err_status_fail:
128  return "unspecified failure";
130  return "unsupported parameter";
132  return "couldn't allocate memory";
134  return "couldn't deallocate properly";
136  return "couldn't initialize";
137  case err_status_terminus:
138  return "can't process as much data as requested";
140  return "authentication failure";
142  return "cipher failure";
144  return "replay check failed (bad index)";
146  return "replay check failed (index too old)";
148  return "algorithm failed test routine";
150  return "unsupported operation";
151  case err_status_no_ctx:
152  return "no appropriate context found";
154  return "unable to perform desired validation";
156  return "can't use key any more";
157  default:
158  return "unknown";
159  }
160 }
161 
162 static int policy_hash_fn(const void *obj, const int flags)
163 {
164  const struct ast_srtp_policy *policy = obj;
165 
166  return policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type;
167 }
168 
169 static int policy_cmp_fn(void *obj, void *arg, int flags)
170 {
171  const struct ast_srtp_policy *one = obj, *two = arg;
172 
173  return one->sp.ssrc.type == two->sp.ssrc.type && one->sp.ssrc.value == two->sp.ssrc.value;
174 }
175 
176 static struct ast_srtp_policy *find_policy(struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
177 {
178  struct ast_srtp_policy tmp = {
179  .sp = {
180  .ssrc.type = policy->ssrc.type,
181  .ssrc.value = policy->ssrc.value,
182  },
183  };
184 
185  return ao2_t_find(srtp->policies, &tmp, flags, "Looking for policy");
186 }
187 
188 static struct ast_srtp *res_srtp_new(void)
189 {
190  struct ast_srtp *srtp;
191 
192  if (!(srtp = ast_calloc(1, sizeof(*srtp)))) {
193  ast_log(LOG_ERROR, "Unable to allocate memory for srtp\n");
194  return NULL;
195  }
196 
198  policy_hash_fn, NULL, policy_cmp_fn, "SRTP policy container");
199  if (!srtp->policies) {
200  ast_free(srtp);
201  return NULL;
202  }
203 
204  srtp->warned = 1;
205 
206  return srtp;
207 }
208 
209 /*
210  struct ast_srtp_policy
211 */
212 static void srtp_event_cb(srtp_event_data_t *data)
213 {
214  switch (data->event) {
215  case event_ssrc_collision:
216  ast_debug(1, "SSRC collision\n");
217  break;
218  case event_key_soft_limit:
219  ast_debug(1, "event_key_soft_limit\n");
220  break;
221  case event_key_hard_limit:
222  ast_debug(1, "event_key_hard_limit\n");
223  break;
224  case event_packet_index_limit:
225  ast_debug(1, "event_packet_index_limit\n");
226  break;
227  }
228 }
229 
230 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy,
231  unsigned long ssrc, int inbound)
232 {
233  if (ssrc) {
234  policy->sp.ssrc.type = ssrc_specific;
235  policy->sp.ssrc.value = ssrc;
236  } else {
237  policy->sp.ssrc.type = inbound ? ssrc_any_inbound : ssrc_any_outbound;
238  }
239 }
240 
241 static void policy_destructor(void *obj)
242 {
243  struct ast_srtp_policy *policy = obj;
244 
245  if (policy->sp.key) {
246  ast_free(policy->sp.key);
247  policy->sp.key = NULL;
248  }
249 }
250 
252 {
253  struct ast_srtp_policy *tmp;
254 
255  if (!(tmp = ao2_t_alloc(sizeof(*tmp), policy_destructor, "Allocating policy"))) {
256  ast_log(LOG_ERROR, "Unable to allocate memory for srtp_policy\n");
257  }
258 
259  return tmp;
260 }
261 
262 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy)
263 {
264  ao2_t_ref(policy, -1, "Destroying policy");
265 }
266 
268 {
269  switch (suite) {
272  return 0;
273 
276  return 0;
277 
278 #ifdef HAVE_SRTP_192
281  return 0;
282 
285  return 0;
286 #endif
287 #ifdef HAVE_SRTP_256
290  return 0;
291 
294  return 0;
295 #endif
296 #ifdef HAVE_SRTP_GCM
297  case AST_AES_GCM_128:
299  return 0;
300 
301  case AST_AES_GCM_256:
303  return 0;
304 
305  case AST_AES_GCM_128_8:
307  return 0;
308 
309  case AST_AES_GCM_256_8:
311  return 0;
312 #endif
313 
314  default:
315  ast_log(LOG_ERROR, "Invalid crypto suite: %u\n", suite);
316  return -1;
317  }
318 }
319 
320 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
321 {
322  return policy_set_suite(&policy->sp.rtp, suite) | policy_set_suite(&policy->sp.rtcp, suite);
323 }
324 
325 static int ast_srtp_policy_set_master_key(struct ast_srtp_policy *policy, const unsigned char *key, size_t key_len, const unsigned char *salt, size_t salt_len)
326 {
327  size_t size = key_len + salt_len;
328  unsigned char *master_key;
329 
330  if (policy->sp.key) {
331  ast_free(policy->sp.key);
332  policy->sp.key = NULL;
333  }
334 
335  if (!(master_key = ast_calloc(1, size))) {
336  return -1;
337  }
338 
339  memcpy(master_key, key, key_len);
340  memcpy(master_key + key_len, salt, salt_len);
341 
342  policy->sp.key = master_key;
343 
344  return 0;
345 }
346 
347 static int ast_srtp_get_random(unsigned char *key, size_t len)
348 {
349 #ifdef HAVE_OPENSSL
350  return RAND_bytes(key, len) > 0 ? 0: -1;
351 #else
352  return crypto_get_random(key, len) != err_status_ok ? -1: 0;
353 #endif
354 }
355 
356 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
357 {
358  if (!srtp) {
359  return;
360  }
361 
362  srtp->cb = cb;
363  srtp->data = data;
364 }
365 
366 /* Vtable functions */
367 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int flags)
368 {
369  int res = 0;
370  int i;
371  int rtcp = (flags & 0x01) >> 0;
372  int retry = (flags & 0x02) >> 1;
373  struct ast_rtp_instance_stats stats = {0,};
374 
375 tryagain:
376 
377  if (!srtp->session) {
378  ast_log(LOG_ERROR, "SRTP unprotect %s - missing session\n", rtcp ? "rtcp" : "rtp");
379  errno = EINVAL;
380  return -1;
381  }
382 
383  for (i = 0; i < 2; i++) {
384  res = rtcp ? srtp_unprotect_rtcp(srtp->session, buf, len) : srtp_unprotect(srtp->session, buf, len);
385  if (res != err_status_no_ctx) {
386  break;
387  }
388 
389  if (srtp->cb && srtp->cb->no_ctx) {
391  break;
392  }
393  if (srtp->cb->no_ctx(srtp->rtp, stats.remote_ssrc, srtp->data) < 0) {
394  break;
395  }
396  } else {
397  break;
398  }
399  }
400 
401  if (retry == 0 && res == err_status_replay_old) {
402  ast_log(AST_LOG_NOTICE, "SRTP unprotect failed with %s, retrying\n", srtp_errstr(res));
403 
404  if (srtp->session) {
405  struct ast_srtp_policy *policy;
406  struct ao2_iterator it;
407  int policies_count;
408 
409  /* dealloc first */
410  ast_debug(5, "SRTP destroy before re-create\n");
411  srtp_dealloc(srtp->session);
412 
413  /* get the count */
414  policies_count = ao2_container_count(srtp->policies);
415 
416  /* get the first to build up */
417  it = ao2_iterator_init(srtp->policies, 0);
418  policy = ao2_iterator_next(&it);
419 
420  ast_debug(5, "SRTP try to re-create\n");
421  if (policy) {
422  int res_srtp_create = srtp_create(&srtp->session, &policy->sp);
423  if (res_srtp_create == err_status_ok) {
424  ast_debug(5, "SRTP re-created with first policy\n");
425  ao2_t_ref(policy, -1, "Unreffing first policy for re-creating srtp session");
426 
427  /* if we have more than one policy, add them */
428  if (policies_count > 1) {
429  ast_debug(5, "Add all the other %d policies\n",
430  policies_count - 1);
431  while ((policy = ao2_iterator_next(&it))) {
432  srtp_add_stream(srtp->session, &policy->sp);
433  ao2_t_ref(policy, -1, "Unreffing n-th policy for re-creating srtp session");
434  }
435  }
436 
437  retry++;
439  goto tryagain;
440  }
441  ast_log(LOG_ERROR, "SRTP session could not be re-created after unprotect failure: %s\n", srtp_errstr(res_srtp_create));
442 
443  /* If srtp_create() fails with a previously alloced session, it will have been dealloced before returning. */
444  srtp->session = NULL;
445 
446  ao2_t_ref(policy, -1, "Unreffing first policy after srtp_create failed");
447  }
449  }
450  }
451 
452  if (!srtp->session) {
453  errno = EINVAL;
454  return -1;
455  }
456 
457  if (res != err_status_ok && res != err_status_replay_fail ) {
458  /*
459  * Authentication failures happen when an active attacker tries to
460  * insert malicious RTP packets. Furthermore, authentication failures
461  * happen, when the other party encrypts the sRTP data in an unexpected
462  * way. This happens quite often with RTCP. Therefore, when you see
463  * authentication failures, try to identify the implementation
464  * (author and product name) used by your other party. Try to investigate
465  * whether they use a custom library or an outdated version of libSRTP.
466  */
467  if (rtcp) {
468  ast_verb(2, "SRTCP unprotect failed on SSRC %u because of %s\n",
470  } else {
471  if ((srtp->warned >= 10) && !((srtp->warned - 10) % 150)) {
472  ast_verb(2, "SRTP unprotect failed on SSRC %u because of %s %d\n",
473  ast_rtp_instance_get_ssrc(srtp->rtp), srtp_errstr(res), srtp->warned);
474  srtp->warned = 11;
475  } else {
476  srtp->warned++;
477  }
478  }
479  errno = EAGAIN;
480  return -1;
481  }
482 
483  return *len;
484 }
485 
486 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp)
487 {
488  int res;
489  unsigned char *localbuf;
490 
491  if (!srtp->session) {
492  ast_log(LOG_ERROR, "SRTP protect %s - missing session\n", rtcp ? "rtcp" : "rtp");
493  errno = EINVAL;
494  return -1;
495  }
496 
497  if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf)) {
498  return -1;
499  }
500 
501  localbuf = rtcp ? srtp->rtcpbuf : srtp->buf;
502 
503  memcpy(localbuf, *buf, *len);
504 
505  if ((res = rtcp ? srtp_protect_rtcp(srtp->session, localbuf, len) : srtp_protect(srtp->session, localbuf, len)) != err_status_ok && res != err_status_replay_fail) {
506  ast_log(LOG_WARNING, "SRTP protect: %s\n", srtp_errstr(res));
507  return -1;
508  }
509 
510  *buf = localbuf;
511  return *len;
512 }
513 
514 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
515 {
516  struct ast_srtp *temp;
517  int status;
518 
519  if (!(temp = res_srtp_new())) {
520  return -1;
521  }
523 
524  /* Any failures after this point can use ast_srtp_destroy to destroy the instance */
525  status = srtp_create(&temp->session, &policy->sp);
526  if (status != err_status_ok) {
527  /* Session either wasn't created or was created and dealloced. */
528  temp->session = NULL;
529  ast_srtp_destroy(temp);
530  ast_log(LOG_ERROR, "Failed to create srtp session on rtp instance (%p) - %s\n",
531  rtp, srtp_errstr(status));
532  return -1;
533  }
534 
535  temp->rtp = rtp;
536  *srtp = temp;
537 
538  ao2_t_link((*srtp)->policies, policy, "Created initial policy");
539 
540  return 0;
541 }
542 
543 static int ast_srtp_replace(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
544 {
545  struct ast_srtp *old = *srtp;
546  int res = ast_srtp_create(srtp, rtp, policy);
547 
548  if (!res && old) {
549  ast_srtp_destroy(old);
550  }
551 
552  if (res) {
553  ast_log(LOG_ERROR, "Failed to replace srtp (%p) on rtp instance (%p) "
554  "- keeping old\n", *srtp, rtp);
555  }
556 
557  return res;
558 }
559 
560 static void ast_srtp_destroy(struct ast_srtp *srtp)
561 {
562  if (srtp->session) {
563  srtp_dealloc(srtp->session);
564  }
565 
566  ao2_t_callback(srtp->policies, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "Unallocate policy");
567  ao2_t_ref(srtp->policies, -1, "Destroying container");
568 
569  ast_free(srtp);
571 }
572 
573 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
574 {
575  struct ast_srtp_policy *match;
576 
577  /* For existing streams, replace if its an SSRC stream, or bail if its a wildcard */
578  if ((match = find_policy(srtp, &policy->sp, OBJ_POINTER))) {
579  if (policy->sp.ssrc.type != ssrc_specific) {
580  ast_log(AST_LOG_WARNING, "Cannot replace an existing wildcard policy\n");
581  ao2_t_ref(match, -1, "Unreffing already existing policy");
582  return -1;
583  } else {
584  if (srtp_remove_stream(srtp->session, match->sp.ssrc.value) != err_status_ok) {
585  ast_log(AST_LOG_WARNING, "Failed to remove SRTP stream for SSRC %u\n", match->sp.ssrc.value);
586  }
587  ao2_t_unlink(srtp->policies, match, "Remove existing match policy");
588  ao2_t_ref(match, -1, "Unreffing already existing policy");
589  }
590  }
591 
592  ast_debug(3, "Adding new policy for %s %u\n",
593  policy->sp.ssrc.type == ssrc_specific ? "SSRC" : "type",
594  policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
595  if (srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
596  ast_log(AST_LOG_WARNING, "Failed to add SRTP stream for %s %u\n",
597  policy->sp.ssrc.type == ssrc_specific ? "SSRC" : "type",
598  policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
599  return -1;
600  }
601 
602  ao2_t_link(srtp->policies, policy, "Added additional stream");
603 
604  return 0;
605 }
606 
607 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
608 {
609  struct ast_srtp_policy *match;
610  struct srtp_policy_t sp = {
611  .ssrc.type = ssrc_specific,
612  .ssrc.value = from_ssrc,
613  };
615 
616  /* If we find a match, return and unlink it from the container so we
617  * can change the SSRC (which is part of the hash) and then have
618  * ast_srtp_add_stream link it back in if all is well */
619  if ((match = find_policy(srtp, &sp, OBJ_POINTER | OBJ_UNLINK))) {
620  match->sp.ssrc.value = to_ssrc;
621  if (ast_srtp_add_stream(srtp, match)) {
622  ast_log(LOG_WARNING, "Couldn't add stream\n");
623  } else if ((status = srtp_remove_stream(srtp->session, from_ssrc))) {
624  ast_debug(3, "Couldn't remove stream (%u)\n", status);
625  }
626  ao2_t_ref(match, -1, "Unreffing found policy in change_source");
627  }
628 
629  return 0;
630 }
631 
633  char *a_crypto;
634  unsigned char local_key[SRTP_MAX_KEY_LEN];
635  int tag;
636  char local_key64[((SRTP_MAX_KEY_LEN) * 8 + 5) / 6 + 1];
637  unsigned char remote_key[SRTP_MAX_KEY_LEN];
638  int key_len;
639 };
640 
641 static void res_sdp_crypto_dtor(struct ast_sdp_crypto *crypto)
642 {
643  if (crypto) {
644  ast_free(crypto->a_crypto);
645  crypto->a_crypto = NULL;
646  ast_free(crypto);
647 
649  }
650 }
651 
652 static struct ast_sdp_crypto *crypto_init_keys(struct ast_sdp_crypto *p, const int key_len)
653 {
654  unsigned char remote_key[key_len];
655 
656  if (srtp_res.get_random(p->local_key, key_len) < 0) {
657  return NULL;
658  }
659 
660  ast_base64encode(p->local_key64, p->local_key, key_len, sizeof(p->local_key64));
661 
662  p->key_len = ast_base64decode(remote_key, p->local_key64, sizeof(remote_key));
663 
664  if (p->key_len != key_len) {
665  ast_log(LOG_ERROR, "base64 encode/decode bad len %d != %d\n", p->key_len, key_len);
666  return NULL;
667  }
668 
669  if (memcmp(remote_key, p->local_key, p->key_len)) {
670  ast_log(LOG_ERROR, "base64 encode/decode bad key\n");
671  return NULL;
672  }
673 
674  ast_debug(1 , "local_key64 %s len %zu\n", p->local_key64, strlen(p->local_key64));
675 
676  return p;
677 }
678 
679 static struct ast_sdp_crypto *sdp_crypto_alloc(const int key_len)
680 {
681  struct ast_sdp_crypto *p, *result;
682 
683  if (!(p = ast_calloc(1, sizeof(*p)))) {
684  return NULL;
685  }
686  p->tag = 1;
688 
689  /* default is a key which uses AST_AES_CM_128_HMAC_SHA1_xx */
690  result = crypto_init_keys(p, key_len);
691  if (!result) {
693  }
694 
695  return result;
696 }
697 
699 {
701 }
702 
703 static int res_sdp_crypto_build_offer(struct ast_sdp_crypto *p, int taglen)
704 {
705  int res;
706 
707  /* Rebuild the crypto line */
708  ast_free(p->a_crypto);
709  p->a_crypto = NULL;
710 
711  if ((taglen & 0x007f) == 8) {
712  res = ast_asprintf(&p->a_crypto, "%d AEAD_AES_%d_GCM_%d inline:%s",
713  p->tag, 128 + ((taglen & 0x0300) >> 2), taglen & 0x007f, p->local_key64);
714  } else if ((taglen & 0x007f) == 16) {
715  res = ast_asprintf(&p->a_crypto, "%d AEAD_AES_%d_GCM inline:%s",
716  p->tag, 128 + ((taglen & 0x0300) >> 2), p->local_key64);
717  } else if ((taglen & 0x0300) && !(taglen & 0x0080)) {
718  res = ast_asprintf(&p->a_crypto, "%d AES_%d_CM_HMAC_SHA1_%d inline:%s",
719  p->tag, 128 + ((taglen & 0x0300) >> 2), taglen & 0x007f, p->local_key64);
720  } else {
721  res = ast_asprintf(&p->a_crypto, "%d AES_CM_%d_HMAC_SHA1_%d inline:%s",
722  p->tag, 128 + ((taglen & 0x0300) >> 2), taglen & 0x007f, p->local_key64);
723  }
724  if (res == -1 || !p->a_crypto) {
725  ast_log(LOG_ERROR, "Could not allocate memory for crypto line\n");
726  return -1;
727  }
728 
729  ast_debug(1, "Crypto line: a=crypto:%s\n", p->a_crypto);
730 
731  return 0;
732 }
733 
734 static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, int key_len, unsigned long ssrc, int inbound)
735 {
736  if (policy_res.set_master_key(policy, master_key, key_len, NULL, 0) < 0) {
737  return -1;
738  }
739 
740  if (policy_res.set_suite(policy, suite_val)) {
741  ast_log(LOG_WARNING, "Could not set remote SRTP suite\n");
742  return -1;
743  }
744 
745  policy_res.set_ssrc(policy, ssrc, inbound);
746 
747  return 0;
748 }
749 
750 static int crypto_activate(struct ast_sdp_crypto *p, int suite_val, unsigned char *remote_key, int key_len, struct ast_rtp_instance *rtp)
751 {
752  struct ast_srtp_policy *local_policy = NULL;
753  struct ast_srtp_policy *remote_policy = NULL;
754  struct ast_rtp_instance_stats stats = {0,};
755  int res = -1;
756 
757  if (!p) {
758  return -1;
759  }
760 
761  if (!(local_policy = policy_res.alloc())) {
762  return -1;
763  }
764 
765  if (!(remote_policy = policy_res.alloc())) {
766  goto err;
767  }
768 
770  goto err;
771  }
772 
773  if (set_crypto_policy(local_policy, suite_val, p->local_key, key_len, stats.local_ssrc, 0) < 0) {
774  goto err;
775  }
776 
777  if (set_crypto_policy(remote_policy, suite_val, remote_key, key_len, 0, 1) < 0) {
778  goto err;
779  }
780 
781  /* Add the SRTP policies */
782  if (ast_rtp_instance_add_srtp_policy(rtp, remote_policy, local_policy, 0)) {
783  ast_log(LOG_WARNING, "Could not set SRTP policies\n");
784  goto err;
785  }
786 
787  ast_debug(1 , "SRTP policy activated\n");
788  res = 0;
789 
790 err:
791  if (local_policy) {
792  policy_res.destroy(local_policy);
793  }
794 
795  if (remote_policy) {
796  policy_res.destroy(remote_policy);
797  }
798 
799  return res;
800 }
801 
802 static int res_sdp_crypto_parse_offer(struct ast_rtp_instance *rtp, struct ast_sdp_srtp *srtp, const char *attr)
803 {
804  char *str = NULL;
805  char *tag = NULL;
806  char *suite = NULL;
807  char *key_params = NULL;
808  char *key_param = NULL;
809  char *session_params = NULL;
810  char *key_salt = NULL; /* The actual master key and key salt */
811  char *lifetime = NULL; /* Key lifetime (# of RTP packets) */
812  char *mki = NULL; /* Master Key Index */
813  int found = 0;
814  int key_len_from_sdp;
815  int key_len_expected;
816  int tag_from_sdp;
817  int suite_val = 0;
818  unsigned char remote_key[SRTP_MAX_KEY_LEN];
819  int taglen;
820  double sdes_lifetime;
821  struct ast_sdp_crypto *crypto;
822  struct ast_sdp_srtp *tmp;
823 
824  str = ast_strdupa(attr);
825 
826  tag = strsep(&str, " ");
827  suite = strsep(&str, " ");
828  key_params = strsep(&str, " ");
829  session_params = strsep(&str, " ");
830 
831  if (!tag || !suite) {
832  ast_log(LOG_WARNING, "Unrecognized crypto attribute a=%s\n", attr);
833  return -1;
834  }
835 
836  /* RFC4568 9.1 - tag is 1-9 digits */
837  if (sscanf(tag, "%30d", &tag_from_sdp) != 1 || tag_from_sdp < 0 || tag_from_sdp > 999999999) {
838  ast_log(LOG_WARNING, "Unacceptable a=crypto tag: %s\n", tag);
839  return -1;
840  }
841 
842  if (!ast_strlen_zero(session_params)) {
843  ast_log(LOG_WARNING, "Unsupported crypto parameters: %s\n", session_params);
844  return -1;
845  }
846 
847  /* On egress, Asterisk sent several crypto lines in the SIP/SDP offer
848  The remote party might have choosen another line than the first */
849  for (tmp = srtp; tmp && tmp->crypto && tmp->crypto->tag != tag_from_sdp;) {
850  tmp = AST_LIST_NEXT(tmp, sdp_srtp_list);
851  }
852  if (tmp) { /* tag matched an already created crypto line */
853  unsigned int flags = tmp->flags;
854 
855  /* Make that crypto line the head of the list, not by changing the
856  list structure but by exchanging the content of the list members */
857  crypto = tmp->crypto;
858  tmp->crypto = srtp->crypto;
859  tmp->flags = srtp->flags;
860  srtp->crypto = crypto;
861  srtp->flags = flags;
862  } else {
863  crypto = srtp->crypto;
864  crypto->tag = tag_from_sdp;
865  }
866 
874 
875  if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_80")) {
876  suite_val = AST_AES_CM_128_HMAC_SHA1_80;
878  key_len_expected = 30;
879  } else if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_32")) {
880  suite_val = AST_AES_CM_128_HMAC_SHA1_32;
882  key_len_expected = 30;
883 #ifdef HAVE_SRTP_192
884  } else if (!strcmp(suite, "AES_192_CM_HMAC_SHA1_80")) {
885  suite_val = AST_AES_CM_192_HMAC_SHA1_80;
888  key_len_expected = 38;
889  } else if (!strcmp(suite, "AES_192_CM_HMAC_SHA1_32")) {
890  suite_val = AST_AES_CM_192_HMAC_SHA1_32;
893  key_len_expected = 38;
894  /* RFC used a different name while in draft, some still use that */
895  } else if (!strcmp(suite, "AES_CM_192_HMAC_SHA1_80")) {
896  suite_val = AST_AES_CM_192_HMAC_SHA1_80;
900  key_len_expected = 38;
901  } else if (!strcmp(suite, "AES_CM_192_HMAC_SHA1_32")) {
902  suite_val = AST_AES_CM_192_HMAC_SHA1_32;
906  key_len_expected = 38;
907 #endif
908 #ifdef HAVE_SRTP_256
909  } else if (!strcmp(suite, "AES_256_CM_HMAC_SHA1_80")) {
910  suite_val = AST_AES_CM_256_HMAC_SHA1_80;
913  key_len_expected = 46;
914  } else if (!strcmp(suite, "AES_256_CM_HMAC_SHA1_32")) {
915  suite_val = AST_AES_CM_256_HMAC_SHA1_32;
918  key_len_expected = 46;
919  /* RFC used a different name while in draft, some still use that */
920  } else if (!strcmp(suite, "AES_CM_256_HMAC_SHA1_80")) {
921  suite_val = AST_AES_CM_256_HMAC_SHA1_80;
925  key_len_expected = 46;
926  } else if (!strcmp(suite, "AES_CM_256_HMAC_SHA1_32")) {
927  suite_val = AST_AES_CM_256_HMAC_SHA1_32;
931  key_len_expected = 46;
932 #endif
933 #ifdef HAVE_SRTP_GCM
934  } else if (!strcmp(suite, "AEAD_AES_128_GCM")) {
935  suite_val = AST_AES_GCM_128;
937  key_len_expected = AES_128_GCM_KEYSIZE_WSALT;
938  } else if (!strcmp(suite, "AEAD_AES_256_GCM")) {
939  suite_val = AST_AES_GCM_256;
942  key_len_expected = AES_256_GCM_KEYSIZE_WSALT;
943  /* RFC contained a (too) short auth tag for RTP media, some still use that */
944  } else if (!strcmp(suite, "AEAD_AES_128_GCM_8")) {
945  suite_val = AST_AES_GCM_128_8;
947  key_len_expected = AES_128_GCM_KEYSIZE_WSALT;
948  } else if (!strcmp(suite, "AEAD_AES_256_GCM_8")) {
949  suite_val = AST_AES_GCM_256_8;
952  key_len_expected = AES_256_GCM_KEYSIZE_WSALT;
953 #endif
954  } else {
955  ast_verb(1, "Unsupported crypto suite: %s\n", suite);
956  return -1;
957  }
958 
959  while ((key_param = strsep(&key_params, ";"))) {
960  unsigned int n_lifetime;
961  char *method = NULL;
962  char *info = NULL;
963 
964  method = strsep(&key_param, ":");
965  info = strsep(&key_param, ";");
966  sdes_lifetime = 0;
967 
968  if (strcmp(method, "inline")) {
969  continue;
970  }
971 
972  key_salt = strsep(&info, "|");
973 
974  /* The next parameter can be either lifetime or MKI */
975  lifetime = strsep(&info, "|");
976  if (!lifetime) {
977  found = 1;
978  break;
979  }
980 
981  mki = strchr(lifetime, ':');
982  if (mki) {
983  mki = lifetime;
984  lifetime = NULL;
985  } else {
986  mki = strsep(&info, "|");
987  }
988 
989  if (mki && *mki != '1') {
990  ast_log(LOG_NOTICE, "Crypto MKI handling is not supported: ignoring attribute %s\n", attr);
991  continue;
992  }
993 
994  if (lifetime) {
995  if (!strncmp(lifetime, "2^", 2)) {
996  char *lifetime_val = lifetime + 2;
997 
998  /* Exponential lifetime */
999  if (sscanf(lifetime_val, "%30u", &n_lifetime) != 1) {
1000  ast_log(LOG_NOTICE, "Failed to parse lifetime value in crypto attribute: %s\n", attr);
1001  continue;
1002  }
1003 
1004  if (n_lifetime > 48) {
1005  /* Yeah... that's a bit big. */
1006  ast_log(LOG_NOTICE, "Crypto lifetime exponent of '%u' is a bit large; using 48\n", n_lifetime);
1007  n_lifetime = 48;
1008  }
1009  sdes_lifetime = pow(2, n_lifetime);
1010  } else {
1011  /* Decimal lifetime */
1012  if (sscanf(lifetime, "%30u", &n_lifetime) != 1) {
1013  ast_log(LOG_NOTICE, "Failed to parse lifetime value in crypto attribute: %s\n", attr);
1014  continue;
1015  }
1016  sdes_lifetime = n_lifetime;
1017  }
1018 
1019  /* Accept anything above ~5.8 hours. Less than ~5.8; reject. */
1020  if (sdes_lifetime < 1048576) {
1021  ast_log(LOG_NOTICE, "Rejecting crypto attribute '%s': lifetime '%f' too short\n", attr, sdes_lifetime);
1022  continue;
1023  }
1024  }
1025 
1026  ast_debug(2, "Crypto attribute '%s' accepted with lifetime '%f', MKI '%s'\n",
1027  attr, sdes_lifetime, mki ? mki : "-");
1028 
1029  found = 1;
1030  break;
1031  }
1032 
1033  if (!found) {
1034  ast_log(LOG_NOTICE, "SRTP crypto offer not acceptable: '%s'\n", attr);
1035  return -1;
1036  }
1037 
1038  key_len_from_sdp = ast_base64decode(remote_key, key_salt, sizeof(remote_key));
1039  if (key_len_from_sdp != key_len_expected) {
1040  ast_log(LOG_WARNING, "SRTP descriptions key length is '%d', not '%d'\n",
1041  key_len_from_sdp, key_len_expected);
1042  return -1;
1043  }
1044 
1045  /* on default, the key is 30 (AES-128); throw that away (only) when the suite changed actually */
1046  /* ingress: optional, but saves one expensive call to get_random(.) */
1047  /* egress: required, because the local key was communicated before the remote key is processed */
1048  if (crypto->key_len != key_len_from_sdp) {
1049  if (!crypto_init_keys(crypto, key_len_from_sdp)) {
1050  return -1;
1051  }
1052  } else if (!memcmp(crypto->remote_key, remote_key, key_len_from_sdp)) {
1053  ast_debug(1, "SRTP remote key unchanged; maintaining current policy\n");
1054  return 0;
1055  }
1056 
1057  if (key_len_from_sdp > sizeof(crypto->remote_key)) {
1059  "SRTP key buffer is %zu although it must be at least %d bytes\n",
1060  sizeof(crypto->remote_key), key_len_from_sdp);
1061  return -1;
1062  }
1063  memcpy(crypto->remote_key, remote_key, key_len_from_sdp);
1064 
1065  if (crypto_activate(crypto, suite_val, remote_key, key_len_from_sdp, rtp) < 0) {
1066  return -1;
1067  }
1068 
1070  taglen = 32;
1071  } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_16)) {
1072  taglen = 16;
1073  } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_8)) {
1074  taglen = 8;
1075  } else {
1076  taglen = 80;
1077  }
1079  taglen |= 0x0200;
1080  } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_192)) {
1081  taglen |= 0x0100;
1082  }
1084  taglen |= 0x0080;
1085  }
1086 
1087  /* Finally, rebuild the crypto line */
1088  if (res_sdp_crypto_build_offer(crypto, taglen)) {
1089  return -1;
1090  }
1091 
1093  return 0;
1094 }
1095 
1096 static const char *res_sdp_srtp_get_attr(struct ast_sdp_srtp *srtp, int dtls_enabled, int default_taglen_32)
1097 {
1098  int taglen;
1099 
1100  if (!srtp) {
1101  return NULL;
1102  }
1103 
1104  /* Set encryption properties */
1105  if (!srtp->crypto) {
1106  if (AST_LIST_NEXT(srtp, sdp_srtp_list)) {
1107  srtp->crypto = res_sdp_crypto_alloc();
1108  ast_log(LOG_ERROR, "SRTP SDP list was not empty\n");
1109  } else {
1110  const int len = default_taglen_32 ? AST_SRTP_CRYPTO_TAG_32 : AST_SRTP_CRYPTO_TAG_80;
1111  const int attr[][3] = {
1112  /* This array creates the following list:
1113  * a=crypto:1 AES_CM_128_HMAC_SHA1_ ...
1114  * a=crypto:2 AEAD_AES_128_GCM ...
1115  * a=crypto:3 AES_256_CM_HMAC_SHA1_ ...
1116  * a=crypto:4 AEAD_AES_256_GCM ...
1117  * a=crypto:5 AES_192_CM_HMAC_SHA1_ ...
1118  * something like 'AEAD_AES_192_GCM' is not specified by the RFCs
1119  *
1120  * If you want to prefer another crypto suite or you want to
1121  * exclude a suite, change this array and recompile Asterisk.
1122  * This list cannot be changed from rtp.conf because you should
1123  * know what you are doing. Especially AES-192 and AES-GCM are
1124  * broken in many VoIP clients, see
1125  * https://github.com/cisco/libsrtp/pull/170
1126  * https://github.com/cisco/libsrtp/pull/184
1127  * Furthermore, AES-GCM uses a shorter crypto-suite string which
1128  * causes Nokia phones based on Symbian/S60 to reject the whole
1129  * INVITE with status 500, even if a matching suite was offered.
1130  * AES-256 might just waste your processor cycles, especially if
1131  * your TLS transport is not secured with equivalent grade, see
1132  * https://security.stackexchange.com/q/61361
1133  * Therefore, AES-128 was preferred here.
1134  *
1135  * If you want to enable one of those defines, please, go for
1136  * CFLAGS='-DENABLE_SRTP_AES_GCM' ./configure && sudo make install
1137  */
1138  { len, 0, 30 },
1139 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM)
1141 #endif
1142 #if defined(HAVE_SRTP_256) && defined(ENABLE_SRTP_AES_256)
1143  { len, AST_SRTP_CRYPTO_AES_256, 46 },
1144 #endif
1145 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM) && defined(ENABLE_SRTP_AES_256)
1147 #endif
1148 #if defined(HAVE_SRTP_192) && defined(ENABLE_SRTP_AES_192)
1149  { len, AST_SRTP_CRYPTO_AES_192, 38 },
1150 #endif
1151  };
1152  struct ast_sdp_srtp *tmp = srtp;
1153  int i;
1154 
1155  for (i = 0; i < ARRAY_LEN(attr); i++) {
1156  if (attr[i][0]) {
1157  ast_set_flag(tmp, attr[i][0]);
1158  }
1159  if (attr[i][1]) {
1160  ast_set_flag(tmp, attr[i][1]);
1161  }
1162  tmp->crypto = sdp_crypto_alloc(attr[i][2]); /* key_len */
1163  tmp->crypto->tag = (i + 1); /* tag starts at 1 */
1164 
1165  if (i < ARRAY_LEN(attr) - 1) {
1167  tmp = AST_LIST_NEXT(tmp, sdp_srtp_list);
1168  }
1169  }
1170  }
1171  }
1172 
1173  if (dtls_enabled) {
1174  /* If DTLS-SRTP is enabled the key details will be pulled from TLS */
1175  return NULL;
1176  }
1177 
1178  /* set the key length based on INVITE or settings */
1180  taglen = 80;
1181  } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_32)) {
1182  taglen = 32;
1183  } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_16)) {
1184  taglen = 16;
1185  } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_8)) {
1186  taglen = 8;
1187  } else {
1188  taglen = default_taglen_32 ? 32 : 80;
1189  }
1191  taglen |= 0x0200;
1192  } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_192)) {
1193  taglen |= 0x0100;
1194  }
1196  taglen |= 0x0080;
1197  }
1198 
1199  if (srtp->crypto && (res_sdp_crypto_build_offer(srtp->crypto, taglen) >= 0)) {
1200  return srtp->crypto->a_crypto;
1201  }
1202 
1203  ast_log(LOG_WARNING, "No SRTP key management enabled\n");
1204  return NULL;
1205 }
1206 
1209  .alloc = res_sdp_crypto_alloc,
1210  .build_offer = res_sdp_crypto_build_offer,
1211  .parse_offer = res_sdp_crypto_parse_offer,
1212  .get_attr = res_sdp_srtp_get_attr,
1213 };
1214 
1215 static void res_srtp_shutdown(void)
1216 {
1217  ast_sdp_crypto_unregister(&res_sdp_crypto_api);
1219  srtp_install_event_handler(NULL);
1220 #ifdef HAVE_SRTP_SHUTDOWN
1221  srtp_shutdown();
1222 #endif
1223  g_initialized = 0;
1224 }
1225 
1226 static int res_srtp_init(void)
1227 {
1228  if (g_initialized) {
1229  return 0;
1230  }
1231 
1232  if (srtp_init() != err_status_ok) {
1233  ast_log(AST_LOG_WARNING, "Failed to initialize libsrtp\n");
1234  return -1;
1235  }
1236 
1237  srtp_install_event_handler(srtp_event_cb);
1238 
1239  if (ast_rtp_engine_register_srtp(&srtp_res, &policy_res)) {
1240  ast_log(AST_LOG_WARNING, "Failed to register SRTP with rtp engine\n");
1242  return -1;
1243  }
1244 
1245  if (ast_sdp_crypto_register(&res_sdp_crypto_api)) {
1246  ast_log(AST_LOG_WARNING, "Failed to register SDP SRTP crypto API\n");
1248  return -1;
1249  }
1250 
1251 #ifdef HAVE_SRTP_GET_VERSION
1252  ast_verb(2, "%s initialized\n", srtp_get_version_string());
1253 #else
1254  ast_verb(2, "libsrtp initialized\n");
1255 #endif
1256 
1257  g_initialized = 1;
1258  return 0;
1259 }
1260 
1261 /*
1262  * Exported functions
1263  */
1264 
1265 static int load_module(void)
1266 {
1267  return res_srtp_init();
1268 }
1269 
1270 static int unload_module(void)
1271 {
1273  return 0;
1274 }
1275 
1277  .support_level = AST_MODULE_SUPPORT_CORE,
1278  .load = load_module,
1279  .unload = unload_module,
1280  .load_pri = AST_MODPRI_CHANNEL_DEPEND,
1281 );
#define ao2_t_ref(o, delta, tag)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:463
structure for secure RTP audio
Definition: sdp_srtp.h:37
static struct ast_sdp_crypto * sdp_crypto_alloc(const int key_len)
Definition: res_srtp.c:679
static struct ast_srtp_policy * ast_srtp_policy_alloc(void)
Definition: res_srtp.c:251
#define crypto_policy_set_aes_cm_192_hmac_sha1_32
Definition: srtp_compat.h:11
static int unload_module(void)
Definition: res_srtp.c:1270
Asterisk main include file. File version handling, generic pbx functions.
static struct ast_sdp_crypto * res_sdp_crypto_alloc(void)
Definition: res_srtp.c:698
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static int policy_hash_fn(const void *obj, const int flags)
Definition: res_srtp.c:162
static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
Definition: res_srtp.c:356
void * data
Definition: res_srtp.c:70
#define crypto_policy_set_aes_cm_192_hmac_sha1_80
Definition: srtp_compat.h:10
#define AST_SRTP_CRYPTO_OLD_NAME
Definition: sdp_srtp.h:51
#define ao2_t_alloc(data_size, destructor_fn, debug_msg)
Definition: astobj2.h:409
int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
Retrieve statistics about an RTP instance.
Definition: rtp_engine.c:2446
#define AES_128_GCM_KEYSIZE_WSALT
Definition: srtp_compat.h:22
#define ast_test_flag(p, flag)
Definition: utils.h:63
static int res_sdp_crypto_build_offer(struct ast_sdp_crypto *p, int taglen)
Definition: res_srtp.c:703
#define OBJ_POINTER
Definition: astobj2.h:1154
#define err_status_algo_fail
Definition: srtp_compat.h:43
#define ast_set_flag(p, flag)
Definition: utils.h:70
#define LOG_WARNING
Definition: logger.h:274
#define err_status_cant_check
Definition: srtp_compat.h:46
static void res_srtp_shutdown(void)
Definition: res_srtp.c:1215
struct ast_srtp_policy *(* alloc)(void)
Definition: res_srtp.h:71
#define err_status_t
Definition: srtp_compat.h:31
static int ast_srtp_policy_set_master_key(struct ast_srtp_policy *policy, const unsigned char *key, size_t key_len, const unsigned char *salt, size_t salt_len)
Definition: res_srtp.c:325
#define err_status_bad_param
Definition: srtp_compat.h:34
static int tmp()
Definition: bt_open.c:389
unsigned char rtcpbuf[8192+AST_FRIENDLY_OFFSET]
Definition: res_srtp.c:73
#define AST_LOG_WARNING
Definition: logger.h:279
static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
Definition: res_srtp.c:514
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:438
static void res_sdp_crypto_dtor(struct ast_sdp_crypto *crypto)
Definition: res_srtp.c:641
#define ao2_t_link(container, obj, tag)
Add an object to a container.
Definition: astobj2.h:1547
#define ao2_t_unlink(container, obj, tag)
Remove an object from a container.
Definition: astobj2.h:1596
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
static int match(struct ast_sockaddr *addr, unsigned short callno, unsigned short dcallno, const struct chan_iax2_pvt *cur, int check_dcallno)
Definition: chan_iax2.c:2315
#define AST_SRTP_CRYPTO_TAG_16
Definition: sdp_srtp.h:47
int(* set_master_key)(struct ast_srtp_policy *policy, const unsigned char *key, size_t key_len, const unsigned char *salt, size_t salt_len)
Definition: res_srtp.h:74
const char * str
Definition: app_jack.c:147
struct ao2_container * policies
Definition: res_srtp.c:67
#define crypto_policy_set_aes_cm_256_hmac_sha1_80
Definition: srtp_compat.h:12
#define NULL
Definition: resample.c:96
int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy, int rtcp)
Add or replace the SRTP policies for the given RTP instance.
Definition: rtp_engine.c:2736
static struct ast_srtp_res srtp_res
Definition: res_srtp.c:102
int(* set_suite)(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
Definition: res_srtp.h:73
static void ast_srtp_destroy(struct ast_srtp *srtp)
Definition: res_srtp.c:560
#define ast_verb(level,...)
Definition: logger.h:463
void ast_rtp_engine_unregister_srtp(void)
Definition: rtp_engine.c:2725
#define err_status_dealloc_fail
Definition: srtp_compat.h:36
Utility functions.
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:269
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:469
#define ast_strlen_zero(foo)
Definition: strings.h:52
const struct ast_srtp_cb * cb
Definition: res_srtp.c:69
static struct ast_sdp_crypto_api res_sdp_crypto_api
Definition: res_srtp.c:1207
#define crypto_policy_set_aes_cm_128_hmac_sha1_80
Definition: srtp_compat.h:8
#define err_status_alloc_fail
Definition: srtp_compat.h:35
#define AST_LOG_NOTICE
Definition: logger.h:268
int ast_base64decode(unsigned char *dst, const char *src, int max)
Decode data from base64.
Definition: main/utils.c:294
sdp_crypto_destroy_cb dtor
Definition: sdp_srtp.h:128
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
Definition: res_srtp.c:607
#define err_status_cipher_fail
Definition: srtp_compat.h:40
#define AST_SRTP_CRYPTO_AES_192
Definition: sdp_srtp.h:49
#define err_status_replay_old
Definition: srtp_compat.h:42
static struct ast_sdp_crypto * crypto_init_keys(struct ast_sdp_crypto *p, const int key_len)
Definition: res_srtp.c:652
#define err_status_no_such_op
Definition: srtp_compat.h:44
void(* set_ssrc)(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound)
Definition: res_srtp.h:75
struct ast_module * self
Definition: module.h:342
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
SRTP and SDP Security descriptions.
#define err_status_init_fail
Definition: srtp_compat.h:37
Asterisk internal frame definitions.
#define AST_SRTP_CRYPTO_OFFER_OK
Definition: sdp_srtp.h:44
char * a_crypto
Definition: res_srtp.c:633
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static int load_module(void)
Definition: res_srtp.c:1265
const char * method
Definition: res_pjsip.c:4335
struct ast_sdp_crypto * crypto
Definition: sdp_srtp.h:39
#define AST_SRTP_CRYPTO_TAG_80
Definition: sdp_srtp.h:46
#define err_status_auth_fail
Definition: srtp_compat.h:39
static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy)
Definition: res_srtp.c:262
static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp)
Definition: res_srtp.c:367
static struct ast_srtp_policy_res policy_res
Definition: res_srtp.c:114
int warned
Definition: res_srtp.c:71
static const char * res_sdp_srtp_get_attr(struct ast_sdp_srtp *srtp, int dtls_enabled, int default_taglen_32)
Definition: res_srtp.c:1096
unsigned char remote_key[SRTP_MAX_KEY_LEN]
Definition: res_srtp.c:637
#define crypto_policy_set_aes_gcm_128_16_auth
Definition: srtp_compat.h:14
static struct ast_srtp_policy * find_policy(struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
Definition: res_srtp.c:176
int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
Definition: rtp_engine.c:2710
#define crypto_policy_set_aes_gcm_256_8_auth
Definition: srtp_compat.h:17
#define LOG_ERROR
Definition: logger.h:285
static int ast_srtp_get_random(unsigned char *key, size_t len)
Definition: res_srtp.c:347
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64.
Definition: main/utils.c:404
#define crypto_policy_set_aes_gcm_128_8_auth
Definition: srtp_compat.h:16
static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, int key_len, unsigned long ssrc, int inbound)
Definition: res_srtp.c:734
unsigned int local_ssrc
Definition: rtp_engine.h:422
#define err_status_no_ctx
Definition: srtp_compat.h:45
static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
Definition: res_srtp.c:573
def info(msg)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int errno
#define crypto_policy_t
Definition: srtp_compat.h:6
#define ao2_t_callback(c, flags, cb_fn, arg, tag)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container, as described below.
Definition: astobj2.h:1714
unsigned int ast_rtp_instance_get_ssrc(struct ast_rtp_instance *rtp)
Retrieve the local SSRC value that we will be using.
Definition: rtp_engine.c:3778
int(* no_ctx)(struct ast_rtp_instance *rtp, unsigned long ssrc, void *data)
Definition: res_srtp.h:31
static int res_srtp_init(void)
Definition: res_srtp.c:1226
static int ast_srtp_replace(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
Definition: res_srtp.c:543
#define SRTP_MASTER_KEY_LEN
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
Definition: res_srtp.c:320
#define LOG_NOTICE
Definition: logger.h:263
static int crypto_activate(struct ast_sdp_crypto *p, int suite_val, unsigned char *remote_key, int key_len, struct ast_rtp_instance *rtp)
Definition: res_srtp.c:750
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
struct ast_sdp_srtp * ast_sdp_srtp_alloc(void)
allocate a ast_sdp_srtp structure
Definition: sdp_srtp.c:41
#define ao2_t_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn, tag)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1308
static void policy_destructor(void *obj)
Definition: res_srtp.c:241
static struct ast_srtp * res_srtp_new(void)
Definition: res_srtp.c:188
#define crypto_policy_set_aes_gcm_256_16_auth
Definition: srtp_compat.h:15
#define AST_SRTP_CRYPTO_TAG_8
Definition: sdp_srtp.h:48
static const char * srtp_errstr(int err)
Definition: res_srtp.c:122
srtp_t session
Definition: res_srtp.c:68
struct ast_sdp_srtp::@318 sdp_srtp_list
#define ast_clear_flag(p, flag)
Definition: utils.h:77
Support for logging to various files, console and syslog Configuration in file logger.conf.
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp)
Definition: res_srtp.c:486
char * strsep(char **str, const char *delims)
#define AST_SRTP_CRYPTO_TAG_32
Definition: sdp_srtp.h:45
unsigned char local_key[SRTP_MAX_KEY_LEN]
Definition: res_srtp.c:634
#define err_status_replay_fail
Definition: srtp_compat.h:41
#define AST_SRTP_CRYPTO_AES_256
Definition: sdp_srtp.h:50
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
#define err_status_key_expired
Definition: srtp_compat.h:47
#define crypto_policy_set_aes_cm_128_hmac_sha1_32
Definition: srtp_compat.h:9
#define AES_256_GCM_KEYSIZE_WSALT
Definition: srtp_compat.h:28
static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound)
Definition: res_srtp.c:230
void ast_sdp_crypto_unregister(struct ast_sdp_crypto_api *api)
Unregister SDP SRTP crypto processing routines.
Definition: sdp_srtp.c:132
static int policy_set_suite(crypto_policy_t *p, enum ast_srtp_suite suite)
Definition: res_srtp.c:267
static void srtp_event_cb(srtp_event_data_t *data)
Definition: res_srtp.c:212
#define ao2_t_find(container, arg, flags, tag)
Definition: astobj2.h:1754
static PGresult * result
Definition: cel_pgsql.c:88
static int res_sdp_crypto_parse_offer(struct ast_rtp_instance *rtp, struct ast_sdp_srtp *srtp, const char *attr)
Definition: res_srtp.c:802
int ast_sdp_crypto_register(struct ast_sdp_crypto_api *api)
Register SDP SRTP crypto processing routines.
Definition: sdp_srtp.c:123
SRTP resource.
void(* destroy)(struct ast_srtp_policy *policy)
Definition: res_srtp.h:72
Generic container type.
#define err_status_ok
Definition: srtp_compat.h:32
unsigned char buf[8192+AST_FRIENDLY_OFFSET]
Definition: res_srtp.c:72
#define err_status_fail
Definition: srtp_compat.h:33
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Pluggable RTP Architecture.
#define err_status_terminus
Definition: srtp_compat.h:38
Asterisk module definitions.
ast_srtp_suite
Definition: res_srtp.h:56
static int g_initialized
Definition: res_srtp.c:81
#define crypto_policy_set_aes_cm_256_hmac_sha1_32
Definition: srtp_compat.h:13
struct ast_rtp_instance * rtp
Definition: res_srtp.c:66
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
int(* get_random)(unsigned char *key, size_t len)
Definition: res_srtp.h:52
char local_key64[((SRTP_MAX_KEY_LEN) *8+5)/6+1]
Definition: res_srtp.c:636
unsigned int flags
Definition: sdp_srtp.h:38
jack_status_t status
Definition: app_jack.c:146
srtp_policy_t sp
Definition: res_srtp.c:77
unsigned int remote_ssrc
Definition: rtp_engine.h:424
static int policy_cmp_fn(void *obj, void *arg, int flags)
Definition: res_srtp.c:169
int(* create)(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
Definition: res_srtp.h:36
#define ast_module_ref(mod)
Hold a reference to the module.
Definition: module.h:443