Wed Jan 8 2020 09:50:19

Asterisk developer's documentation


res_srtp.c File Reference

Secure RTP (SRTP) More...

#include "asterisk.h"
#include <srtp/srtp.h>
#include <srtp/crypto_kernel.h>
#include "asterisk/lock.h"
#include "asterisk/sched.h"
#include "asterisk/module.h"
#include "asterisk/options.h"
#include "asterisk/rtp_engine.h"
#include "asterisk/astobj2.h"

Go to the source code of this file.

Data Structures

struct  ast_srtp
 
struct  ast_srtp_policy
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int ast_srtp_add_stream (struct ast_srtp *srtp, struct ast_srtp_policy *policy)
 
static int ast_srtp_change_source (struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
 
static int ast_srtp_create (struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
 
static void ast_srtp_destroy (struct ast_srtp *srtp)
 
static int ast_srtp_get_random (unsigned char *key, size_t len)
 
static struct ast_srtp_policyast_srtp_policy_alloc (void)
 
static void ast_srtp_policy_destroy (struct ast_srtp_policy *policy)
 
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)
 
static void ast_srtp_policy_set_ssrc (struct ast_srtp_policy *policy, unsigned long ssrc, int inbound)
 
static int ast_srtp_policy_set_suite (struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
 
static int ast_srtp_protect (struct ast_srtp *srtp, void **buf, int *len, int rtcp)
 
static int ast_srtp_replace (struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
 
static void ast_srtp_set_cb (struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
 
static int ast_srtp_unprotect (struct ast_srtp *srtp, void *buf, int *len, int rtcp)
 
static struct ast_srtp_policyfind_policy (struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
 
static int load_module (void)
 
static int policy_cmp_fn (void *obj, void *arg, int flags)
 
static void policy_destructor (void *obj)
 
static int policy_hash_fn (const void *obj, const int flags)
 
static int policy_set_suite (crypto_policy_t *p, enum ast_srtp_suite suite)
 
static int res_srtp_init (void)
 
static struct ast_srtpres_srtp_new (void)
 
static void res_srtp_shutdown (void)
 
static const char * srtp_errstr (int err)
 
static void srtp_event_cb (srtp_event_data_t *data)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "Secure RTP (SRTP)" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND, }
 
static struct ast_module_infoast_module_info = &__mod_info
 
static int g_initialized = 0
 
static struct ast_srtp_policy_res policy_res
 
static struct ast_srtp_res srtp_res
 

Detailed Description

Secure RTP (SRTP)

Secure RTP (SRTP) Specified in RFC 3711.

Author
Mikael Magnusson mikma.nosp@m.@use.nosp@m.rs.so.nosp@m.urce.nosp@m.forge.nosp@m..net

Definition in file res_srtp.c.

Function Documentation

static void __reg_module ( void  )
static

Definition at line 598 of file res_srtp.c.

static void __unreg_module ( void  )
static

Definition at line 598 of file res_srtp.c.

static int ast_srtp_add_stream ( struct ast_srtp srtp,
struct ast_srtp_policy policy 
)
static

Definition at line 487 of file res_srtp.c.

References ao2_t_link, ao2_t_ref, ao2_t_unlink, ast_debug, ast_log(), AST_LOG_WARNING, find_policy(), match(), OBJ_POINTER, ast_srtp::policies, ast_srtp::session, and ast_srtp_policy::sp.

Referenced by ast_srtp_change_source().

488 {
489  struct ast_srtp_policy *match;
490 
491  /* For existing streams, replace if its an SSRC stream, or bail if its a wildcard */
492  if ((match = find_policy(srtp, &policy->sp, OBJ_POINTER))) {
493  if (policy->sp.ssrc.type != ssrc_specific) {
494  ast_log(AST_LOG_WARNING, "Cannot replace an existing wildcard policy\n");
495  ao2_t_ref(match, -1, "Unreffing already existing policy");
496  return -1;
497  } else {
498  if (srtp_remove_stream(srtp->session, match->sp.ssrc.value) != err_status_ok) {
499  ast_log(AST_LOG_WARNING, "Failed to remove SRTP stream for SSRC %u\n", match->sp.ssrc.value);
500  }
501  ao2_t_unlink(srtp->policies, match, "Remove existing match policy");
502  ao2_t_ref(match, -1, "Unreffing already existing policy");
503  }
504  }
505 
506  ast_debug(3, "Adding new policy for %s %u\n",
507  policy->sp.ssrc.type == ssrc_specific ? "SSRC" : "type",
508  policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
509  if (srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
510  ast_log(AST_LOG_WARNING, "Failed to add SRTP stream for %s %u\n",
511  policy->sp.ssrc.type == ssrc_specific ? "SSRC" : "type",
512  policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
513  return -1;
514  }
515 
516  ao2_t_link(srtp->policies, policy, "Added additional stream");
517 
518  return 0;
519 }
#define ao2_t_ref(o, delta, tag)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:471
#define AST_LOG_WARNING
Definition: logger.h:149
struct ao2_container * policies
Definition: res_srtp.c:54
#define ao2_t_link(arg1, arg2, arg3)
Add an object to a container.
Definition: astobj2.h:784
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static struct ast_srtp_policy * find_policy(struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
Definition: res_srtp.c:163
#define ao2_t_unlink(arg1, arg2, arg3)
Remove an object from a container.
Definition: astobj2.h:816
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
srtp_t session
Definition: res_srtp.c:55
static int match(struct sockaddr_in *sin, unsigned short callno, unsigned short dcallno, const struct chan_iax2_pvt *cur, int check_dcallno)
Definition: chan_iax2.c:2069
srtp_policy_t sp
Definition: res_srtp.c:64
static int ast_srtp_change_source ( struct ast_srtp srtp,
unsigned int  from_ssrc,
unsigned int  to_ssrc 
)
static

Definition at line 521 of file res_srtp.c.

References ao2_t_ref, ast_debug, ast_log(), ast_srtp_add_stream(), find_policy(), LOG_WARNING, match(), OBJ_POINTER, OBJ_UNLINK, ast_srtp::session, ast_srtp_policy::sp, and status.

522 {
523  struct ast_srtp_policy *match;
524  struct srtp_policy_t sp = {
525  .ssrc.type = ssrc_specific,
526  .ssrc.value = from_ssrc,
527  };
528  err_status_t status;
529 
530  /* If we find a match, return and unlink it from the container so we
531  * can change the SSRC (which is part of the hash) and then have
532  * ast_srtp_add_stream link it back in if all is well */
533  if ((match = find_policy(srtp, &sp, OBJ_POINTER | OBJ_UNLINK))) {
534  match->sp.ssrc.value = to_ssrc;
535  if (ast_srtp_add_stream(srtp, match)) {
536  ast_log(LOG_WARNING, "Couldn't add stream\n");
537  } else if ((status = srtp_remove_stream(srtp->session, from_ssrc))) {
538  ast_debug(3, "Couldn't remove stream (%u)\n", status);
539  }
540  ao2_t_ref(match, -1, "Unreffing found policy in change_source");
541  }
542 
543  return 0;
544 }
#define ao2_t_ref(o, delta, tag)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:471
#define LOG_WARNING
Definition: logger.h:144
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static struct ast_srtp_policy * find_policy(struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
Definition: res_srtp.c:163
static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
Definition: res_srtp.c:487
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
srtp_t session
Definition: res_srtp.c:55
static int match(struct sockaddr_in *sin, unsigned short callno, unsigned short dcallno, const struct chan_iax2_pvt *cur, int check_dcallno)
Definition: chan_iax2.c:2069
jack_status_t status
Definition: app_jack.c:143
srtp_policy_t sp
Definition: res_srtp.c:64
static int ast_srtp_create ( struct ast_srtp **  srtp,
struct ast_rtp_instance rtp,
struct ast_srtp_policy policy 
)
static

Definition at line 441 of file res_srtp.c.

References ao2_t_link, ast_module_ref(), ast_srtp_destroy(), res_srtp_new(), ast_srtp::rtp, ast_module_info::self, ast_srtp::session, and ast_srtp_policy::sp.

Referenced by ast_srtp_replace().

442 {
443  struct ast_srtp *temp;
444 
445  if (!(temp = res_srtp_new())) {
446  return -1;
447  }
449 
450  /* Any failures after this point can use ast_srtp_destroy to destroy the instance */
451  if (srtp_create(&temp->session, &policy->sp) != err_status_ok) {
452  /* Session either wasn't created or was created and dealloced. */
453  temp->session = NULL;
454  ast_srtp_destroy(temp);
455  return -1;
456  }
457 
458  temp->rtp = rtp;
459  *srtp = temp;
460 
461  ao2_t_link((*srtp)->policies, policy, "Created initial policy");
462 
463  return 0;
464 }
static void ast_srtp_destroy(struct ast_srtp *srtp)
Definition: res_srtp.c:474
#define ao2_t_link(arg1, arg2, arg3)
Add an object to a container.
Definition: astobj2.h:784
struct ast_module * self
Definition: module.h:227
static struct ast_srtp * res_srtp_new(void)
Definition: res_srtp.c:175
srtp_t session
Definition: res_srtp.c:55
struct ast_rtp_instance * rtp
Definition: res_srtp.c:53
srtp_policy_t sp
Definition: res_srtp.c:64
struct ast_module * ast_module_ref(struct ast_module *)
Definition: loader.c:1300
static void ast_srtp_destroy ( struct ast_srtp srtp)
static

Definition at line 474 of file res_srtp.c.

References ao2_t_callback, ao2_t_ref, ast_free, ast_module_unref(), OBJ_MULTIPLE, OBJ_NODATA, OBJ_UNLINK, ast_srtp::policies, ast_module_info::self, and ast_srtp::session.

Referenced by ast_srtp_create(), and ast_srtp_replace().

475 {
476  if (srtp->session) {
477  srtp_dealloc(srtp->session);
478  }
479 
480  ao2_t_callback(srtp->policies, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "Unallocate policy");
481  ao2_t_ref(srtp->policies, -1, "Destroying container");
482 
483  ast_free(srtp);
485 }
#define ao2_t_ref(o, delta, tag)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:471
void ast_module_unref(struct ast_module *)
Definition: loader.c:1312
struct ao2_container * policies
Definition: res_srtp.c:54
struct ast_module * self
Definition: module.h:227
#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:909
#define ast_free(a)
Definition: astmm.h:97
srtp_t session
Definition: res_srtp.c:55
static int ast_srtp_get_random ( unsigned char *  key,
size_t  len 
)
static

Definition at line 306 of file res_srtp.c.

307 {
308  return crypto_get_random(key, len) != err_status_ok ? -1: 0;
309 }
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static struct ast_srtp_policy * ast_srtp_policy_alloc ( void  )
static

Definition at line 236 of file res_srtp.c.

References ao2_t_alloc, ast_log(), LOG_ERROR, and policy_destructor().

237 {
238  struct ast_srtp_policy *tmp;
239 
240  if (!(tmp = ao2_t_alloc(sizeof(*tmp), policy_destructor, "Allocating policy"))) {
241  ast_log(LOG_ERROR, "Unable to allocate memory for srtp_policy\n");
242  }
243 
244  return tmp;
245 }
#define ao2_t_alloc(data_size, destructor_fn, debug_msg)
Allocate and initialize an object.
Definition: astobj2.h:429
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static void policy_destructor(void *obj)
Definition: res_srtp.c:226
static void ast_srtp_policy_destroy ( struct ast_srtp_policy policy)
static

Definition at line 247 of file res_srtp.c.

References ao2_t_ref.

248 {
249  ao2_t_ref(policy, -1, "Destroying policy");
250 }
#define ao2_t_ref(o, delta, tag)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:471
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 
)
static

Definition at line 284 of file res_srtp.c.

References ast_calloc, ast_free, and ast_srtp_policy::sp.

285 {
286  size_t size = key_len + salt_len;
287  unsigned char *master_key;
288 
289  if (policy->sp.key) {
290  ast_free(policy->sp.key);
291  policy->sp.key = NULL;
292  }
293 
294  if (!(master_key = ast_calloc(1, size))) {
295  return -1;
296  }
297 
298  memcpy(master_key, key, key_len);
299  memcpy(master_key + key_len, salt, salt_len);
300 
301  policy->sp.key = master_key;
302 
303  return 0;
304 }
#define ast_free(a)
Definition: astmm.h:97
#define ast_calloc(a, b)
Definition: astmm.h:82
srtp_policy_t sp
Definition: res_srtp.c:64
static void ast_srtp_policy_set_ssrc ( struct ast_srtp_policy policy,
unsigned long  ssrc,
int  inbound 
)
static

Definition at line 215 of file res_srtp.c.

References ast_srtp_policy::sp.

217 {
218  if (ssrc) {
219  policy->sp.ssrc.type = ssrc_specific;
220  policy->sp.ssrc.value = ssrc;
221  } else {
222  policy->sp.ssrc.type = inbound ? ssrc_any_inbound : ssrc_any_outbound;
223  }
224 }
srtp_policy_t sp
Definition: res_srtp.c:64
static int ast_srtp_policy_set_suite ( struct ast_srtp_policy policy,
enum ast_srtp_suite  suite 
)
static

Definition at line 279 of file res_srtp.c.

References policy_set_suite(), and ast_srtp_policy::sp.

280 {
281  return policy_set_suite(&policy->sp.rtp, suite) | policy_set_suite(&policy->sp.rtcp, suite);
282 }
static int policy_set_suite(crypto_policy_t *p, enum ast_srtp_suite suite)
Definition: res_srtp.c:252
srtp_policy_t sp
Definition: res_srtp.c:64
static int ast_srtp_protect ( struct ast_srtp srtp,
void **  buf,
int *  len,
int  rtcp 
)
static

Definition at line 419 of file res_srtp.c.

References ast_log(), ast_srtp::buf, len(), LOG_WARNING, ast_srtp::rtcpbuf, ast_srtp::session, and srtp_errstr().

420 {
421  int res;
422  unsigned char *localbuf;
423 
424  if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf)) {
425  return -1;
426  }
427 
428  localbuf = rtcp ? srtp->rtcpbuf : srtp->buf;
429 
430  memcpy(localbuf, *buf, *len);
431 
432  if ((res = rtcp ? srtp_protect_rtcp(srtp->session, localbuf, len) : srtp_protect(srtp->session, localbuf, len)) != err_status_ok && res != err_status_replay_fail) {
433  ast_log(LOG_WARNING, "SRTP protect: %s\n", srtp_errstr(res));
434  return -1;
435  }
436 
437  *buf = localbuf;
438  return *len;
439 }
#define LOG_WARNING
Definition: logger.h:144
unsigned char rtcpbuf[8192+AST_FRIENDLY_OFFSET]
Definition: res_srtp.c:60
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static const char * srtp_errstr(int err)
Definition: res_srtp.c:109
srtp_t session
Definition: res_srtp.c:55
unsigned char buf[8192+AST_FRIENDLY_OFFSET]
Definition: res_srtp.c:59
static int ast_srtp_replace ( struct ast_srtp **  srtp,
struct ast_rtp_instance rtp,
struct ast_srtp_policy policy 
)
static

Definition at line 466 of file res_srtp.c.

References ast_srtp_create(), and ast_srtp_destroy().

467 {
468  if ((*srtp) != NULL) {
469  ast_srtp_destroy(*srtp);
470  }
471  return ast_srtp_create(srtp, rtp, policy);
472 }
static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
Definition: res_srtp.c:441
static void ast_srtp_destroy(struct ast_srtp *srtp)
Definition: res_srtp.c:474
static void ast_srtp_set_cb ( struct ast_srtp srtp,
const struct ast_srtp_cb cb,
void *  data 
)
static

Definition at line 311 of file res_srtp.c.

References ast_srtp::cb, and ast_srtp::data.

312 {
313  if (!srtp) {
314  return;
315  }
316 
317  srtp->cb = cb;
318  srtp->data = data;
319 }
void * data
Definition: res_srtp.c:57
struct ast_srtp_cb * cb
Definition: res_srtp.c:56
static int ast_srtp_unprotect ( struct ast_srtp srtp,
void *  buf,
int *  len,
int  rtcp 
)
static

Definition at line 322 of file res_srtp.c.

References ao2_container_count(), ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_t_ref, ast_debug, ast_log(), AST_LOG_NOTICE, AST_LOG_WARNING, ast_rtp_instance_get_stats(), AST_RTP_INSTANCE_STAT_REMOTE_SSRC, ast_srtp::cb, ast_srtp::data, errno, len(), LOG_ERROR, ast_srtp_cb::no_ctx, ast_srtp::policies, ast_rtp_instance_stats::remote_ssrc, ast_srtp::rtp, ast_srtp::session, ast_srtp_policy::sp, srtp_errstr(), and ast_srtp::warned.

323 {
324  int res = 0;
325  int i;
326  int retry = 0;
327  struct ast_rtp_instance_stats stats = {0,};
328 
329 tryagain:
330 
331  for (i = 0; i < 2; i++) {
332  res = rtcp ? srtp_unprotect_rtcp(srtp->session, buf, len) : srtp_unprotect(srtp->session, buf, len);
333  if (res != err_status_no_ctx) {
334  break;
335  }
336 
337  if (srtp->cb && srtp->cb->no_ctx) {
339  break;
340  }
341  if (srtp->cb->no_ctx(srtp->rtp, stats.remote_ssrc, srtp->data) < 0) {
342  break;
343  }
344  } else {
345  break;
346  }
347  }
348 
349  if (retry == 0 && res == err_status_replay_old) {
350  ast_log(AST_LOG_NOTICE, "SRTP unprotect failed with %s, retrying\n", srtp_errstr(res));
351 
352  if (srtp->session) {
353  struct ast_srtp_policy *policy;
354  struct ao2_iterator it;
355  int policies_count;
356 
357  /* dealloc first */
358  ast_debug(5, "SRTP destroy before re-create\n");
359  srtp_dealloc(srtp->session);
360 
361  /* get the count */
362  policies_count = ao2_container_count(srtp->policies);
363 
364  /* get the first to build up */
365  it = ao2_iterator_init(srtp->policies, 0);
366  policy = ao2_iterator_next(&it);
367 
368  ast_debug(5, "SRTP try to re-create\n");
369  if (policy) {
370  int res_srtp_create = srtp_create(&srtp->session, &policy->sp);
371  if (res_srtp_create == err_status_ok) {
372  ast_debug(5, "SRTP re-created with first policy\n");
373  ao2_t_ref(policy, -1, "Unreffing first policy for re-creating srtp session");
374 
375  /* if we have more than one policy, add them */
376  if (policies_count > 1) {
377  ast_debug(5, "Add all the other %d policies\n",
378  policies_count - 1);
379  while ((policy = ao2_iterator_next(&it))) {
380  srtp_add_stream(srtp->session, &policy->sp);
381  ao2_t_ref(policy, -1, "Unreffing n-th policy for re-creating srtp session");
382  }
383  }
384 
385  retry++;
387  goto tryagain;
388  }
389  ast_log(LOG_ERROR, "SRTP session could not be re-created after unprotect failure: %s\n", srtp_errstr(res_srtp_create));
390 
391  /* If srtp_create() fails with a previously alloced session, it will have been dealloced before returning. */
392  srtp->session = NULL;
393 
394  ao2_t_ref(policy, -1, "Unreffing first policy after srtp_create failed");
395  }
397  }
398  }
399 
400  if (!srtp->session) {
401  errno = EINVAL;
402  return -1;
403  }
404 
405  if (res != err_status_ok && res != err_status_replay_fail ) {
406  if ((srtp->warned >= 10) && !((srtp->warned - 10) % 100)) {
407  ast_log(AST_LOG_WARNING, "SRTP unprotect failed with: %s %d\n", srtp_errstr(res), srtp->warned);
408  srtp->warned = 11;
409  } else {
410  srtp->warned++;
411  }
412  errno = EAGAIN;
413  return -1;
414  }
415 
416  return *len;
417 }
#define ao2_t_ref(o, delta, tag)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:471
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
Definition: astobj2.c:470
void * data
Definition: res_srtp.c:57
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:1604
#define ao2_iterator_next(arg1)
Definition: astobj2.h:1126
#define AST_LOG_WARNING
Definition: logger.h:149
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
Create an iterator for a container.
Definition: astobj2.c:818
struct ao2_container * policies
Definition: res_srtp.c:54
#define AST_LOG_NOTICE
Definition: logger.h:138
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
int warned
Definition: res_srtp.c:58
struct ast_srtp_cb * cb
Definition: res_srtp.c:56
#define LOG_ERROR
Definition: logger.h:155
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int(* no_ctx)(struct ast_rtp_instance *rtp, unsigned long ssrc, void *data)
Definition: res_srtp.h:29
int errno
static const char * srtp_errstr(int err)
Definition: res_srtp.c:109
srtp_t session
Definition: res_srtp.c:55
void ao2_iterator_destroy(struct ao2_iterator *i)
Destroy a container iterator.
Definition: astobj2.c:833
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1053
struct ast_rtp_instance * rtp
Definition: res_srtp.c:53
srtp_policy_t sp
Definition: res_srtp.c:64
unsigned int remote_ssrc
Definition: rtp_engine.h:293
static struct ast_srtp_policy* find_policy ( struct ast_srtp srtp,
const srtp_policy_t *  policy,
int  flags 
)
static

Definition at line 163 of file res_srtp.c.

References ao2_t_find, ast_srtp::policies, and ast_srtp_policy::sp.

Referenced by ast_srtp_add_stream(), and ast_srtp_change_source().

164 {
165  struct ast_srtp_policy tmp = {
166  .sp = {
167  .ssrc.type = policy->ssrc.type,
168  .ssrc.value = policy->ssrc.value,
169  },
170  };
171 
172  return ao2_t_find(srtp->policies, &tmp, flags, "Looking for policy");
173 }
#define ao2_t_find(arg1, arg2, arg3, arg4)
Definition: astobj2.h:963
struct ao2_container * policies
Definition: res_srtp.c:54
srtp_policy_t sp
Definition: res_srtp.c:64
static int load_module ( void  )
static

Definition at line 583 of file res_srtp.c.

References res_srtp_init().

584 {
585  return res_srtp_init();
586 }
static int res_srtp_init(void)
Definition: res_srtp.c:556
static int policy_cmp_fn ( void *  obj,
void *  arg,
int  flags 
)
static

Definition at line 156 of file res_srtp.c.

References ast_srtp_policy::sp.

Referenced by res_srtp_new().

157 {
158  const struct ast_srtp_policy *one = obj, *two = arg;
159 
160  return one->sp.ssrc.type == two->sp.ssrc.type && one->sp.ssrc.value == two->sp.ssrc.value;
161 }
srtp_policy_t sp
Definition: res_srtp.c:64
static void policy_destructor ( void *  obj)
static

Definition at line 226 of file res_srtp.c.

References ast_free, and ast_srtp_policy::sp.

Referenced by ast_srtp_policy_alloc().

227 {
228  struct ast_srtp_policy *policy = obj;
229 
230  if (policy->sp.key) {
231  ast_free(policy->sp.key);
232  policy->sp.key = NULL;
233  }
234 }
#define ast_free(a)
Definition: astmm.h:97
srtp_policy_t sp
Definition: res_srtp.c:64
static int policy_hash_fn ( const void *  obj,
const int  flags 
)
static

Definition at line 149 of file res_srtp.c.

References ast_srtp_policy::sp.

Referenced by res_srtp_new().

150 {
151  const struct ast_srtp_policy *policy = obj;
152 
153  return policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type;
154 }
srtp_policy_t sp
Definition: res_srtp.c:64
static int policy_set_suite ( crypto_policy_t *  p,
enum ast_srtp_suite  suite 
)
static

Definition at line 252 of file res_srtp.c.

References AST_AES_CM_128_HMAC_SHA1_32, AST_AES_CM_128_HMAC_SHA1_80, ast_log(), and LOG_ERROR.

Referenced by ast_srtp_policy_set_suite().

253 {
254  switch (suite) {
256  p->cipher_type = AES_128_ICM;
257  p->cipher_key_len = 30;
258  p->auth_type = HMAC_SHA1;
259  p->auth_key_len = 20;
260  p->auth_tag_len = 10;
261  p->sec_serv = sec_serv_conf_and_auth;
262  return 0;
263 
265  p->cipher_type = AES_128_ICM;
266  p->cipher_key_len = 30;
267  p->auth_type = HMAC_SHA1;
268  p->auth_key_len = 20;
269  p->auth_tag_len = 4;
270  p->sec_serv = sec_serv_conf_and_auth;
271  return 0;
272 
273  default:
274  ast_log(LOG_ERROR, "Invalid crypto suite: %u\n", suite);
275  return -1;
276  }
277 }
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static int res_srtp_init ( void  )
static

Definition at line 556 of file res_srtp.c.

References ast_log(), AST_LOG_WARNING, ast_rtp_engine_register_srtp(), res_srtp_shutdown(), and srtp_event_cb().

Referenced by load_module().

557 {
558  if (g_initialized) {
559  return 0;
560  }
561 
562  if (srtp_init() != err_status_ok) {
563  ast_log(AST_LOG_WARNING, "Failed to initialize libsrtp\n");
564  return -1;
565  }
566 
567  srtp_install_event_handler(srtp_event_cb);
568 
570  ast_log(AST_LOG_WARNING, "Failed to register SRTP with rtp engine\n");
572  return -1;
573  }
574 
575  g_initialized = 1;
576  return 0;
577 }
static void res_srtp_shutdown(void)
Definition: res_srtp.c:546
#define AST_LOG_WARNING
Definition: logger.h:149
static struct ast_srtp_res srtp_res
Definition: res_srtp.c:89
static struct ast_srtp_policy_res policy_res
Definition: res_srtp.c:101
int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
Definition: rtp_engine.c:1803
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static void srtp_event_cb(srtp_event_data_t *data)
Definition: res_srtp.c:197
static int g_initialized
Definition: res_srtp.c:68
static struct ast_srtp* res_srtp_new ( void  )
static

Definition at line 175 of file res_srtp.c.

References ao2_t_container_alloc, ast_calloc, ast_free, ast_log(), LOG_ERROR, ast_srtp::policies, policy_cmp_fn(), policy_hash_fn(), and ast_srtp::warned.

Referenced by ast_srtp_create().

176 {
177  struct ast_srtp *srtp;
178 
179  if (!(srtp = ast_calloc(1, sizeof(*srtp)))) {
180  ast_log(LOG_ERROR, "Unable to allocate memory for srtp\n");
181  return NULL;
182  }
183 
184  if (!(srtp->policies = ao2_t_container_alloc(5, policy_hash_fn, policy_cmp_fn, "SRTP policy container"))) {
185  ast_free(srtp);
186  return NULL;
187  }
188 
189  srtp->warned = 1;
190 
191  return srtp;
192 }
static int policy_hash_fn(const void *obj, const int flags)
Definition: res_srtp.c:149
struct ao2_container * policies
Definition: res_srtp.c:54
#define ao2_t_container_alloc(arg1, arg2, arg3, arg4)
Allocate and initialize a container with the desired number of buckets.
Definition: astobj2.h:733
int warned
Definition: res_srtp.c:58
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
#define ast_calloc(a, b)
Definition: astmm.h:82
static int policy_cmp_fn(void *obj, void *arg, int flags)
Definition: res_srtp.c:156
static void res_srtp_shutdown ( void  )
static

Definition at line 546 of file res_srtp.c.

References ast_rtp_engine_unregister_srtp().

Referenced by res_srtp_init(), and unload_module().

547 {
548  srtp_install_event_handler(NULL);
550 #ifdef HAVE_SRTP_SHUTDOWN
551  srtp_shutdown();
552 #endif
553  g_initialized = 0;
554 }
void ast_rtp_engine_unregister_srtp(void)
Definition: rtp_engine.c:1818
static int g_initialized
Definition: res_srtp.c:68
static const char* srtp_errstr ( int  err)
static

Definition at line 109 of file res_srtp.c.

Referenced by ast_srtp_protect(), and ast_srtp_unprotect().

110 {
111  switch(err) {
112  case err_status_ok:
113  return "nothing to report";
114  case err_status_fail:
115  return "unspecified failure";
116  case err_status_bad_param:
117  return "unsupported parameter";
118  case err_status_alloc_fail:
119  return "couldn't allocate memory";
120  case err_status_dealloc_fail:
121  return "couldn't deallocate properly";
122  case err_status_init_fail:
123  return "couldn't initialize";
124  case err_status_terminus:
125  return "can't process as much data as requested";
126  case err_status_auth_fail:
127  return "authentication failure";
128  case err_status_cipher_fail:
129  return "cipher failure";
130  case err_status_replay_fail:
131  return "replay check failed (bad index)";
132  case err_status_replay_old:
133  return "replay check failed (index too old)";
134  case err_status_algo_fail:
135  return "algorithm failed test routine";
136  case err_status_no_such_op:
137  return "unsupported operation";
138  case err_status_no_ctx:
139  return "no appropriate context found";
140  case err_status_cant_check:
141  return "unable to perform desired validation";
142  case err_status_key_expired:
143  return "can't use key any more";
144  default:
145  return "unknown";
146  }
147 }
static void srtp_event_cb ( srtp_event_data_t *  data)
static

Definition at line 197 of file res_srtp.c.

References ast_debug.

Referenced by res_srtp_init().

198 {
199  switch (data->event) {
200  case event_ssrc_collision:
201  ast_debug(1, "SSRC collision\n");
202  break;
203  case event_key_soft_limit:
204  ast_debug(1, "event_key_soft_limit\n");
205  break;
206  case event_key_hard_limit:
207  ast_debug(1, "event_key_hard_limit\n");
208  break;
209  case event_packet_index_limit:
210  ast_debug(1, "event_packet_index_limit\n");
211  break;
212  }
213 }
void * data
Definition: res_srtp.c:57
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static int unload_module ( void  )
static

Definition at line 588 of file res_srtp.c.

References res_srtp_shutdown().

589 {
591  return 0;
592 }
static void res_srtp_shutdown(void)
Definition: res_srtp.c:546

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "Secure RTP (SRTP)" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND, }
static

Definition at line 598 of file res_srtp.c.

Definition at line 598 of file res_srtp.c.

int g_initialized = 0
static

Tracks whether or not we've initialized the libsrtp library

Definition at line 68 of file res_srtp.c.

struct ast_srtp_policy_res policy_res
static

Definition at line 101 of file res_srtp.c.

Referenced by ast_rtp_engine_register_srtp().

struct ast_srtp_res srtp_res
static

Definition at line 89 of file res_srtp.c.

Referenced by ast_rtp_engine_register_srtp().