Asterisk - The Open Source Telephony Project  18.5.0
sdp_srtp.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2006 - 2007, 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 
19 /*! \file
20  *
21  * \brief SRTP and SDP Security descriptions
22  *
23  * Specified in RFC 3711, 6188, 7714, and 4568
24  *
25  * \author Mikael Magnusson <[email protected]>
26  */
27 
28 /*** MODULEINFO
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 #include "asterisk/linkedlists.h" /* for AST_LIST_NEXT, etc */
35 #include "asterisk/logger.h" /* for ast_log, LOG_ERROR, etc */
36 #include "asterisk/sdp_srtp.h" /* for ast_sdp_srtp, etc */
37 
38 /*! Registered SDP crypto API */
40 
42 {
44  ast_debug(1, "No SRTP module loaded, can't setup SRTP session.\n");
45  return NULL;
46  }
47 
48  return ast_calloc(1, sizeof(struct ast_sdp_srtp));
49 }
50 
52 {
53  struct ast_sdp_srtp *next;
54 
55  for (next = AST_LIST_NEXT(srtp, sdp_srtp_list);
56  srtp;
57  srtp = next, next = srtp ? AST_LIST_NEXT(srtp, sdp_srtp_list) : NULL) {
59  srtp->crypto = NULL;
60  ast_free(srtp);
61  }
62 }
63 
65 {
66  if (sdp_crypto_api) {
67  sdp_crypto_api->dtor(crypto);
68  }
69 }
70 
72 {
73  if (!sdp_crypto_api) {
74  return NULL;
75  }
76  return sdp_crypto_api->alloc();
77 }
78 
79 int ast_sdp_crypto_process(struct ast_rtp_instance *rtp, struct ast_sdp_srtp *srtp, const char *attr)
80 {
81  if (!sdp_crypto_api) {
82  return -1;
83  }
84  return sdp_crypto_api->parse_offer(rtp, srtp, attr);
85 }
86 
87 int ast_sdp_crypto_build_offer(struct ast_sdp_crypto *p, int taglen)
88 {
89  if (!sdp_crypto_api) {
90  return -1;
91  }
92  return sdp_crypto_api->build_offer(p, taglen);
93 }
94 
95 const char *ast_sdp_srtp_get_attrib(struct ast_sdp_srtp *srtp, int dtls_enabled, int default_taglen_32)
96 {
97  if (!sdp_crypto_api) {
98  return NULL;
99  }
100  return sdp_crypto_api->get_attr(srtp, dtls_enabled, default_taglen_32);
101 }
102 
103 char *ast_sdp_get_rtp_profile(unsigned int sdes_active, struct ast_rtp_instance *instance, unsigned int using_avpf,
104  unsigned int force_avp)
105 {
106  struct ast_rtp_engine_dtls *dtls;
107 
108  if ((dtls = ast_rtp_instance_get_dtls(instance)) && dtls->active(instance)) {
109  if (force_avp) {
110  return using_avpf ? "RTP/SAVPF" : "RTP/SAVP";
111  } else {
112  return using_avpf ? "UDP/TLS/RTP/SAVPF" : "UDP/TLS/RTP/SAVP";
113  }
114  } else {
115  if (using_avpf) {
116  return sdes_active ? "RTP/SAVPF" : "RTP/AVPF";
117  } else {
118  return sdes_active ? "RTP/SAVP" : "RTP/AVP";
119  }
120  }
121 }
122 
124 {
125  if (sdp_crypto_api) {
126  return -1;
127  }
128  sdp_crypto_api = api;
129  return 0;
130 }
131 
133 {
134  if (sdp_crypto_api == api) {
135  sdp_crypto_api = NULL;
136  }
137 }
structure for secure RTP audio
Definition: sdp_srtp.h:37
static struct ast_sdp_crypto_api * sdp_crypto_api
Definition: sdp_srtp.c:39
Asterisk main include file. File version handling, generic pbx functions.
sdp_crypto_parse_offer_cb parse_offer
Definition: sdp_srtp.h:134
void ast_sdp_srtp_destroy(struct ast_sdp_srtp *srtp)
free a ast_sdp_srtp structure
Definition: sdp_srtp.c:51
sdp_crypto_build_offer_cb build_offer
Definition: sdp_srtp.h:132
int ast_sdp_crypto_process(struct ast_rtp_instance *rtp, struct ast_sdp_srtp *srtp, const char *attr)
Parse the a=crypto line from SDP and set appropriate values on the ast_sdp_crypto struct...
Definition: sdp_srtp.c:79
char * ast_sdp_get_rtp_profile(unsigned int sdes_active, struct ast_rtp_instance *instance, unsigned int using_avpf, unsigned int force_avp)
Get the RTP profile in use by a media session.
Definition: sdp_srtp.c:103
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:438
void ast_sdp_crypto_unregister(struct ast_sdp_crypto_api *api)
Unregister SDP SRTP crypto processing routines.
Definition: sdp_srtp.c:132
void ast_sdp_crypto_destroy(struct ast_sdp_crypto *crypto)
Destroy a previously allocated ast_sdp_crypto struct.
Definition: sdp_srtp.c:64
struct ast_sdp_srtp * next
Definition: sdp_srtp.h:40
int ast_sdp_crypto_build_offer(struct ast_sdp_crypto *p, int taglen)
Generate an SRTP a=crypto offer.
Definition: sdp_srtp.c:87
sdp_crypto_alloc_cb alloc
Definition: sdp_srtp.h:130
struct ast_sdp_srtp * ast_sdp_srtp_alloc(void)
allocate a ast_sdp_srtp structure
Definition: sdp_srtp.c:41
#define NULL
Definition: resample.c:96
sdp_crypto_destroy_cb dtor
Definition: sdp_srtp.h:128
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
SRTP and SDP Security descriptions.
struct ast_sdp_crypto * crypto
Definition: sdp_srtp.h:39
A set of macros to manage forward-linked lists.
Structure that represents the optional DTLS SRTP support within an RTP engine.
Definition: rtp_engine.h:570
struct ast_rtp_engine_dtls * ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
Obtain a pointer to the DTLS support present on an RTP instance.
Definition: rtp_engine.c:3011
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
int ast_sdp_crypto_register(struct ast_sdp_crypto_api *api)
Register SDP SRTP crypto processing routines.
Definition: sdp_srtp.c:123
struct ast_sdp_srtp::@318 sdp_srtp_list
Support for logging to various files, console and syslog Configuration in file logger.conf.
const char * ast_sdp_srtp_get_attrib(struct ast_sdp_srtp *srtp, int dtls_enabled, int default_taglen_32)
Get the crypto attribute line for the srtp structure.
Definition: sdp_srtp.c:95
int(* active)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:574
int ast_rtp_engine_srtp_is_registered(void)
Definition: rtp_engine.c:2731
struct ast_sdp_crypto * ast_sdp_crypto_alloc(void)
Initialize an return an ast_sdp_crypto struct.
Definition: sdp_srtp.c:71
sdp_srtp_get_attr_cb get_attr
Definition: sdp_srtp.h:136