Wed Jan 8 2020 09:50:20

Asterisk developer's documentation


sdp_crypto.c File Reference

SDP Security descriptions. More...

#include "asterisk.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"
#include "include/sdp_crypto.h"

Go to the source code of this file.

Data Structures

struct  sdp_crypto
 

Macros

#define SRTP_MASTER_LEN   30
 
#define SRTP_MASTER_LEN64   (((SRTP_MASTER_LEN) * 8 + 5) / 6 + 1)
 
#define SRTP_MASTERKEY_LEN   16
 
#define SRTP_MASTERSALT_LEN   ((SRTP_MASTER_LEN) - (SRTP_MASTERKEY_LEN))
 

Functions

static int sdp_crypto_activate (struct sdp_crypto *p, int suite_val, unsigned char *remote_key, struct ast_rtp_instance *rtp)
 
static struct sdp_cryptosdp_crypto_alloc (void)
 
const char * sdp_crypto_attrib (struct sdp_crypto *p)
 
void sdp_crypto_destroy (struct sdp_crypto *crypto)
 
int sdp_crypto_offer (struct sdp_crypto *p)
 
int sdp_crypto_process (struct sdp_crypto *p, const char *attr, struct ast_rtp_instance *rtp)
 
struct sdp_cryptosdp_crypto_setup (void)
 
static int set_crypto_policy (struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, unsigned long ssrc, int inbound)
 

Variables

struct ast_srtp_resres_srtp
 
struct ast_srtp_policy_resres_srtp_policy
 

Detailed Description

SDP Security descriptions.

Specified in RFC 4568

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

Definition in file sdp_crypto.c.

Macro Definition Documentation

#define SRTP_MASTER_LEN   30

Definition at line 40 of file sdp_crypto.c.

Referenced by sdp_crypto_process(), and sdp_crypto_setup().

#define SRTP_MASTER_LEN64   (((SRTP_MASTER_LEN) * 8 + 5) / 6 + 1)

Definition at line 43 of file sdp_crypto.c.

#define SRTP_MASTERKEY_LEN   16

Definition at line 41 of file sdp_crypto.c.

Referenced by set_crypto_policy().

#define SRTP_MASTERSALT_LEN   ((SRTP_MASTER_LEN) - (SRTP_MASTERKEY_LEN))

Definition at line 42 of file sdp_crypto.c.

Referenced by set_crypto_policy().

Function Documentation

static int sdp_crypto_activate ( struct sdp_crypto p,
int  suite_val,
unsigned char *  remote_key,
struct ast_rtp_instance rtp 
)
static

Definition at line 136 of file sdp_crypto.c.

References ast_srtp_policy_res::alloc, ast_debug, ast_log(), ast_rtp_engine_srtp_is_registered(), ast_rtp_instance_add_srtp_policy(), ast_rtp_instance_get_stats(), AST_RTP_INSTANCE_STAT_LOCAL_SSRC, ast_srtp_policy_res::destroy, sdp_crypto::local_key, ast_rtp_instance_stats::local_ssrc, LOG_WARNING, and set_crypto_policy().

Referenced by sdp_crypto_process().

137 {
138  struct ast_srtp_policy *local_policy = NULL;
139  struct ast_srtp_policy *remote_policy = NULL;
140  struct ast_rtp_instance_stats stats = {0,};
141  int res = -1;
142 
144  return -1;
145  }
146 
147  if (!p) {
148  return -1;
149  }
150 
151  if (!(local_policy = res_srtp_policy->alloc())) {
152  return -1;
153  }
154 
155  if (!(remote_policy = res_srtp_policy->alloc())) {
156  goto err;
157  }
158 
160  goto err;
161  }
162 
163  if (set_crypto_policy(local_policy, suite_val, p->local_key, stats.local_ssrc, 0) < 0) {
164  goto err;
165  }
166 
167  if (set_crypto_policy(remote_policy, suite_val, remote_key, 0, 1) < 0) {
168  goto err;
169  }
170 
171  /* Add the SRTP policies */
172  if (ast_rtp_instance_add_srtp_policy(rtp, remote_policy, local_policy)) {
173  ast_log(LOG_WARNING, "Could not set SRTP policies\n");
174  goto err;
175  }
176 
177  ast_debug(1 , "SRTP policy activated\n");
178  res = 0;
179 
180 err:
181  if (local_policy) {
182  res_srtp_policy->destroy(local_policy);
183  }
184 
185  if (remote_policy) {
186  res_srtp_policy->destroy(remote_policy);
187  }
188 
189  return res;
190 }
struct ast_srtp_policy_res * res_srtp_policy
Definition: rtp_engine.c:49
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
unsigned char local_key[SRTP_MASTER_LEN]
Definition: sdp_crypto.c:50
#define LOG_WARNING
Definition: logger.h:144
void(* destroy)(struct ast_srtp_policy *policy)
Definition: res_srtp.h:62
int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy)
Add or replace the SRTP policies for the given RTP instance.
Definition: rtp_engine.c:1829
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
struct ast_srtp_policy *(* alloc)(void)
Definition: res_srtp.h:61
static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, unsigned long ssrc, int inbound)
Definition: sdp_crypto.c:113
unsigned int local_ssrc
Definition: rtp_engine.h:291
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 ast_rtp_engine_srtp_is_registered(void)
Definition: rtp_engine.c:1824
static struct sdp_crypto* sdp_crypto_alloc ( void  )
static

Definition at line 59 of file sdp_crypto.c.

References ast_calloc.

Referenced by sdp_crypto_setup().

60 {
61  return ast_calloc(1, sizeof(struct sdp_crypto));
62 }
#define ast_calloc(a, b)
Definition: astmm.h:82
const char* sdp_crypto_attrib ( struct sdp_crypto p)

Definition at line 318 of file sdp_crypto.c.

References sdp_crypto::a_crypto.

Referenced by get_crypto_attrib().

319 {
320  return p->a_crypto;
321 }
char * a_crypto
Definition: sdp_crypto.c:49
void sdp_crypto_destroy ( struct sdp_crypto crypto)

Definition at line 64 of file sdp_crypto.c.

References sdp_crypto::a_crypto, ast_free, and sdp_crypto::tag.

Referenced by sdp_crypto_setup(), and sip_srtp_destroy().

65 {
66  ast_free(crypto->a_crypto);
67  crypto->a_crypto = NULL;
68  ast_free(crypto->tag);
69  crypto->tag = NULL;
70  ast_free(crypto);
71 }
char * a_crypto
Definition: sdp_crypto.c:49
#define ast_free(a)
Definition: astmm.h:97
char * tag
Definition: sdp_crypto.c:51
int sdp_crypto_offer ( struct sdp_crypto p)

Definition at line 295 of file sdp_crypto.c.

References sdp_crypto::a_crypto, ast_asprintf, ast_debug, ast_free, ast_log(), ast_strlen_zero(), sdp_crypto::local_key64, LOG_ERROR, sdp_crypto::suite, and sdp_crypto::tag.

Referenced by get_crypto_attrib(), and sdp_crypto_process().

296 {
297  if (ast_strlen_zero(p->suite)) {
298  /* Default crypto offer */
299  strcpy(p->suite, "AES_CM_128_HMAC_SHA1_80");
300  }
301 
302  /* Rebuild the crypto line */
303  if (p->a_crypto) {
304  ast_free(p->a_crypto);
305  }
306 
307  if (ast_asprintf(&p->a_crypto, "a=crypto:%s %s inline:%s\r\n",
308  p->tag ? p->tag : "1", p->suite, p->local_key64) == -1) {
309  ast_log(LOG_ERROR, "Could not allocate memory for crypto line\n");
310  return -1;
311  }
312 
313  ast_debug(1, "Crypto line: %s", p->a_crypto);
314 
315  return 0;
316 }
char local_key64[SRTP_MASTER_LEN64]
Definition: sdp_crypto.c:52
#define ast_asprintf(a, b, c...)
Definition: astmm.h:121
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define LOG_ERROR
Definition: logger.h:155
char * a_crypto
Definition: sdp_crypto.c:49
char suite[64]
Definition: sdp_crypto.c:54
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
char * tag
Definition: sdp_crypto.c:51
int sdp_crypto_process ( struct sdp_crypto p,
const char *  attr,
struct ast_rtp_instance rtp 
)

Definition at line 192 of file sdp_crypto.c.

References AST_AES_CM_128_HMAC_SHA1_32, AST_AES_CM_128_HMAC_SHA1_80, ast_base64decode(), ast_copy_string(), ast_debug, ast_log(), ast_rtp_engine_srtp_is_registered(), ast_strdup, ast_strdupa, LOG_ERROR, LOG_NOTICE, LOG_WARNING, sdp_crypto::remote_key, sdp_crypto_activate(), sdp_crypto_offer(), SRTP_MASTER_LEN, str, strsep(), sdp_crypto::suite, and sdp_crypto::tag.

Referenced by process_crypto().

193 {
194  char *str = NULL;
195  char *tag = NULL;
196  char *suite = NULL;
197  char *key_params = NULL;
198  char *key_param = NULL;
199  char *session_params = NULL;
200  char *key_salt = NULL;
201  char *lifetime = NULL;
202  int found = 0;
203  int key_len = 0;
204  int suite_val = 0;
205  unsigned char remote_key[SRTP_MASTER_LEN];
206 
208  return -1;
209  }
210 
211  str = ast_strdupa(attr);
212 
213  strsep(&str, ":");
214  tag = strsep(&str, " ");
215  suite = strsep(&str, " ");
216  key_params = strsep(&str, " ");
217  session_params = strsep(&str, " ");
218 
219  if (!tag || !suite) {
220  ast_log(LOG_WARNING, "Unrecognized a=%s", attr);
221  return -1;
222  }
223 
224  if (session_params) {
225  ast_log(LOG_WARNING, "Unsupported crypto parameters: %s", session_params);
226  return -1;
227  }
228 
229  if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_80")) {
230  suite_val = AST_AES_CM_128_HMAC_SHA1_80;
231  } else if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_32")) {
232  suite_val = AST_AES_CM_128_HMAC_SHA1_32;
233  } else {
234  ast_log(LOG_WARNING, "Unsupported crypto suite: %s\n", suite);
235  return -1;
236  }
237 
238  while ((key_param = strsep(&key_params, ";"))) {
239  char *method = NULL;
240  char *info = NULL;
241 
242  method = strsep(&key_param, ":");
243  info = strsep(&key_param, ";");
244 
245  if (!strcmp(method, "inline")) {
246  key_salt = strsep(&info, "|");
247  lifetime = strsep(&info, "|");
248 
249  if (lifetime) {
250  ast_log(LOG_NOTICE, "Crypto life time unsupported: %s\n", attr);
251  continue;
252  }
253 
254  found = 1;
255  break;
256  }
257  }
258 
259  if (!found) {
260  ast_log(LOG_NOTICE, "SRTP crypto offer not acceptable\n");
261  return -1;
262  }
263 
264  if ((key_len = ast_base64decode(remote_key, key_salt, sizeof(remote_key))) != SRTP_MASTER_LEN) {
265  ast_log(LOG_WARNING, "SRTP descriptions key %d != %d\n", key_len, SRTP_MASTER_LEN);
266  return -1;
267  }
268 
269  if (!memcmp(p->remote_key, remote_key, sizeof(p->remote_key))) {
270  ast_debug(1, "SRTP remote key unchanged; maintaining current policy\n");
271  return 0;
272  }
273 
274  /* Set the accepted policy and remote key */
275  ast_copy_string(p->suite, suite, sizeof(p->suite));
276  memcpy(p->remote_key, remote_key, sizeof(p->remote_key));
277 
278  if (sdp_crypto_activate(p, suite_val, remote_key, rtp) < 0) {
279  return -1;
280  }
281 
282  if (!p->tag) {
283  ast_debug(1, "Accepting crypto tag %s\n", tag);
284  p->tag = ast_strdup(tag);
285  if (!p->tag) {
286  ast_log(LOG_ERROR, "Could not allocate memory for tag\n");
287  return -1;
288  }
289  }
290 
291  /* Finally, rebuild the crypto line */
292  return sdp_crypto_offer(p);
293 }
char * strsep(char **str, const char *delims)
unsigned char remote_key[SRTP_MASTER_LEN]
Definition: sdp_crypto.c:53
#define ast_strdup(a)
Definition: astmm.h:109
#define SRTP_MASTER_LEN
Definition: sdp_crypto.c:40
#define LOG_WARNING
Definition: logger.h:144
const char * str
Definition: app_jack.c:144
int ast_base64decode(unsigned char *dst, const char *src, int max)
Decode data from base64.
Definition: utils.c:279
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define LOG_ERROR
Definition: logger.h:155
static int sdp_crypto_activate(struct sdp_crypto *p, int suite_val, unsigned char *remote_key, struct ast_rtp_instance *rtp)
Definition: sdp_crypto.c:136
char suite[64]
Definition: sdp_crypto.c:54
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 LOG_NOTICE
Definition: logger.h:133
char * tag
Definition: sdp_crypto.c:51
int sdp_crypto_offer(struct sdp_crypto *p)
Definition: sdp_crypto.c:295
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
int ast_rtp_engine_srtp_is_registered(void)
Definition: rtp_engine.c:1824
struct sdp_crypto* sdp_crypto_setup ( void  )

Definition at line 73 of file sdp_crypto.c.

References ast_base64decode(), ast_base64encode(), ast_debug, ast_free, ast_log(), ast_rtp_engine_srtp_is_registered(), ast_srtp_res::get_random, sdp_crypto::local_key, sdp_crypto::local_key64, LOG_ERROR, sdp_crypto::remote_key, sdp_crypto_alloc(), sdp_crypto_destroy(), and SRTP_MASTER_LEN.

Referenced by get_crypto_attrib(), and process_crypto().

74 {
75  struct sdp_crypto *p;
76  int key_len;
77  unsigned char remote_key[SRTP_MASTER_LEN];
78 
80  return NULL;
81  }
82 
83  if (!(p = sdp_crypto_alloc())) {
84  return NULL;
85  }
86 
87  if (res_srtp->get_random(p->local_key, sizeof(p->local_key)) < 0) {
89  return NULL;
90  }
91 
93 
94  key_len = ast_base64decode(remote_key, p->local_key64, sizeof(remote_key));
95 
96  if (key_len != SRTP_MASTER_LEN) {
97  ast_log(LOG_ERROR, "base64 encode/decode bad len %d != %d\n", key_len, SRTP_MASTER_LEN);
98  ast_free(p);
99  return NULL;
100  }
101 
102  if (memcmp(remote_key, p->local_key, SRTP_MASTER_LEN)) {
103  ast_log(LOG_ERROR, "base64 encode/decode bad key\n");
104  ast_free(p);
105  return NULL;
106  }
107 
108  ast_debug(1 , "local_key64 %s len %zu\n", p->local_key64, strlen(p->local_key64));
109 
110  return p;
111 }
unsigned char remote_key[SRTP_MASTER_LEN]
Definition: sdp_crypto.c:53
#define SRTP_MASTER_LEN
Definition: sdp_crypto.c:40
unsigned char local_key[SRTP_MASTER_LEN]
Definition: sdp_crypto.c:50
void sdp_crypto_destroy(struct sdp_crypto *crypto)
Definition: sdp_crypto.c:64
char local_key64[SRTP_MASTER_LEN64]
Definition: sdp_crypto.c:52
int ast_base64decode(unsigned char *dst, const char *src, int max)
Decode data from base64.
Definition: utils.c:279
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
#define LOG_ERROR
Definition: logger.h:155
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64.
Definition: utils.c:357
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
struct ast_srtp_res * res_srtp
Definition: rtp_engine.c:48
int ast_rtp_engine_srtp_is_registered(void)
Definition: rtp_engine.c:1824
int(* get_random)(unsigned char *key, size_t len)
Definition: res_srtp.h:50
static struct sdp_crypto * sdp_crypto_alloc(void)
Definition: sdp_crypto.c:59
static int set_crypto_policy ( struct ast_srtp_policy policy,
int  suite_val,
const unsigned char *  master_key,
unsigned long  ssrc,
int  inbound 
)
static

Definition at line 113 of file sdp_crypto.c.

References ast_log(), ast_rtp_engine_srtp_is_registered(), LOG_WARNING, ast_srtp_policy_res::set_master_key, ast_srtp_policy_res::set_ssrc, ast_srtp_policy_res::set_suite, SRTP_MASTERKEY_LEN, and SRTP_MASTERSALT_LEN.

Referenced by sdp_crypto_activate().

114 {
115  const unsigned char *master_salt = NULL;
116 
118  return -1;
119  }
120 
121  master_salt = master_key + SRTP_MASTERKEY_LEN;
122  if (res_srtp_policy->set_master_key(policy, master_key, SRTP_MASTERKEY_LEN, master_salt, SRTP_MASTERSALT_LEN) < 0) {
123  return -1;
124  }
125 
126  if (res_srtp_policy->set_suite(policy, suite_val)) {
127  ast_log(LOG_WARNING, "Could not set remote SRTP suite\n");
128  return -1;
129  }
130 
131  res_srtp_policy->set_ssrc(policy, ssrc, inbound);
132 
133  return 0;
134 }
struct ast_srtp_policy_res * res_srtp_policy
Definition: rtp_engine.c:49
void(* set_ssrc)(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound)
Definition: res_srtp.h:65
#define SRTP_MASTERKEY_LEN
Definition: sdp_crypto.c:41
#define LOG_WARNING
Definition: logger.h:144
#define SRTP_MASTERSALT_LEN
Definition: sdp_crypto.c:42
int(* set_suite)(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
Definition: res_srtp.h:63
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 ast_rtp_engine_srtp_is_registered(void)
Definition: rtp_engine.c:1824
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:64

Variable Documentation

struct ast_srtp_res* res_srtp

Definition at line 48 of file rtp_engine.c.

struct ast_srtp_policy_res* res_srtp_policy

Definition at line 49 of file rtp_engine.c.

Referenced by ast_rtp_engine_srtp_is_registered().