Wed Jan 8 2020 09:49:51

Asterisk developer's documentation


sdp_crypto.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 <mikma@users.sourceforge.net>
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 sdp_crypto.c
20  *
21  * \brief SDP Security descriptions
22  *
23  * Specified in RFC 4568
24  *
25  * \author Mikael Magnusson <mikma@users.sourceforge.net>
26  */
27 
28 /*** MODULEINFO
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 415908 $")
35 
36 #include "asterisk/options.h"
37 #include "asterisk/utils.h"
38 #include "include/sdp_crypto.h"
39 
40 #define SRTP_MASTER_LEN 30
41 #define SRTP_MASTERKEY_LEN 16
42 #define SRTP_MASTERSALT_LEN ((SRTP_MASTER_LEN) - (SRTP_MASTERKEY_LEN))
43 #define SRTP_MASTER_LEN64 (((SRTP_MASTER_LEN) * 8 + 5) / 6 + 1)
44 
45 extern struct ast_srtp_res *res_srtp;
47 
48 struct sdp_crypto {
49  char *a_crypto;
50  unsigned char local_key[SRTP_MASTER_LEN];
51  char *tag;
53  unsigned char remote_key[SRTP_MASTER_LEN];
54  char suite[64];
55 };
56 
57 static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, unsigned long ssrc, int inbound);
58 
59 static struct sdp_crypto *sdp_crypto_alloc(void)
60 {
61  return ast_calloc(1, sizeof(struct sdp_crypto));
62 }
63 
64 void sdp_crypto_destroy(struct sdp_crypto *crypto)
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 }
72 
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 }
112 
113 static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, unsigned long ssrc, int inbound)
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 }
135 
136 static int sdp_crypto_activate(struct sdp_crypto *p, int suite_val, unsigned char *remote_key, struct ast_rtp_instance *rtp)
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 }
191 
192 int sdp_crypto_process(struct sdp_crypto *p, const char *attr, struct ast_rtp_instance *rtp)
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 }
294 
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 }
317 
318 const char *sdp_crypto_attrib(struct sdp_crypto *p)
319 {
320  return p->a_crypto;
321 }
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
Asterisk main include file. File version handling, generic pbx functions.
char * strsep(char **str, const char *delims)
#define SRTP_MASTERKEY_LEN
Definition: sdp_crypto.c:41
unsigned char remote_key[SRTP_MASTER_LEN]
Definition: sdp_crypto.c:53
#define ast_strdup(a)
Definition: astmm.h:109
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 SRTP_MASTER_LEN
Definition: sdp_crypto.c:40
unsigned char local_key[SRTP_MASTER_LEN]
Definition: sdp_crypto.c:50
#define LOG_WARNING
Definition: logger.h:144
struct sdp_crypto * sdp_crypto_setup(void)
Definition: sdp_crypto.c:73
#define SRTP_MASTERSALT_LEN
Definition: sdp_crypto.c:42
void(* destroy)(struct ast_srtp_policy *policy)
Definition: res_srtp.h:62
void sdp_crypto_destroy(struct sdp_crypto *crypto)
Definition: sdp_crypto.c:64
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
const char * str
Definition: app_jack.c:144
#define SRTP_MASTER_LEN64
Definition: sdp_crypto.c:43
Utility functions.
char local_key64[SRTP_MASTER_LEN64]
Definition: sdp_crypto.c:52
#define ast_asprintf(a, b, c...)
Definition: astmm.h:121
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
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
struct ast_srtp_policy *(* alloc)(void)
Definition: res_srtp.h:61
int(* set_suite)(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
Definition: res_srtp.h:63
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
const char * sdp_crypto_attrib(struct sdp_crypto *p)
Definition: sdp_crypto.c:318
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
#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
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64.
Definition: utils.c:357
unsigned int local_ssrc
Definition: rtp_engine.h:291
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 LOG_NOTICE
Definition: logger.h:133
#define ast_free(a)
Definition: astmm.h:97
struct ast_srtp_res * res_srtp
Definition: rtp_engine.c:48
char * tag
Definition: sdp_crypto.c:51
int sdp_crypto_offer(struct sdp_crypto *p)
Definition: sdp_crypto.c:295
#define ast_calloc(a, b)
Definition: astmm.h:82
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
int(* get_random)(unsigned char *key, size_t len)
Definition: res_srtp.h:50
int sdp_crypto_process(struct sdp_crypto *p, const char *attr, struct ast_rtp_instance *rtp)
Definition: sdp_crypto.c:192
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
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
static struct sdp_crypto * sdp_crypto_alloc(void)
Definition: sdp_crypto.c:59