forked from asterisk/asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity_agreements.c
369 lines (311 loc) · 9.19 KB
/
security_agreements.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2022, Commend International
*
* Maximilian Fridrich <[email protected]>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
*
* \brief Interact with security agreement negotiations and mechanisms
*
* \author Maximilian Fridrich <[email protected]>
*/
#include "asterisk.h"
#include <pjsip.h>
#include "asterisk/res_pjsip.h"
static struct ast_sip_security_mechanism *ast_sip_security_mechanisms_alloc(size_t n_params)
{
struct ast_sip_security_mechanism *mech;
mech = ast_calloc(1, sizeof(struct ast_sip_security_mechanism));
if (mech == NULL) {
return NULL;
}
mech->qvalue = 0.0;
if (AST_VECTOR_INIT(&mech->mechanism_parameters, n_params) != 0) {
ast_free(mech);
return NULL;
}
return mech;
}
static struct ast_sip_security_mechanism *ast_sip_security_mechanisms_copy(
const struct ast_sip_security_mechanism *src)
{
struct ast_sip_security_mechanism *dst = NULL;
int i, n_params;
char *param;
n_params = AST_VECTOR_SIZE(&src->mechanism_parameters);
dst = ast_sip_security_mechanisms_alloc(n_params);
if (dst == NULL) {
return NULL;
}
dst->type = src->type;
dst->qvalue = src->qvalue;
for (i = 0; i < n_params; i++) {
param = ast_strdup(AST_VECTOR_GET(&src->mechanism_parameters, i));
AST_VECTOR_APPEND(&dst->mechanism_parameters, param);
}
return dst;
}
static void ast_sip_security_mechanisms_destroy(struct ast_sip_security_mechanism *mech)
{
int i;
for (i = 0; i < AST_VECTOR_SIZE(&mech->mechanism_parameters); i++) {
ast_free(AST_VECTOR_GET(&mech->mechanism_parameters, i));
}
AST_VECTOR_FREE(&mech->mechanism_parameters);
ast_free(mech);
}
void ast_sip_security_mechanisms_vector_copy(struct ast_sip_security_mechanism_vector *dst,
const struct ast_sip_security_mechanism_vector *src)
{
struct ast_sip_security_mechanism *mech;
int i;
ast_sip_security_mechanisms_vector_destroy(dst);
for (i = 0; i < AST_VECTOR_SIZE(src); i++) {
mech = AST_VECTOR_GET(src, i);
AST_VECTOR_APPEND(dst, ast_sip_security_mechanisms_copy(mech));
}
};
void ast_sip_security_mechanisms_vector_destroy(struct ast_sip_security_mechanism_vector *security_mechanisms)
{
struct ast_sip_security_mechanism *mech;
int i;
if (!security_mechanisms) {
return;
}
for (i = 0; i < AST_VECTOR_SIZE(security_mechanisms); i++) {
mech = AST_VECTOR_GET(security_mechanisms, i);
ast_sip_security_mechanisms_destroy(mech);
}
AST_VECTOR_FREE(security_mechanisms);
}
static int ast_sip_str_to_security_mechanism_type(const char *security_mechanism) {
int result = -1;
if (!strcasecmp(security_mechanism, "msrp-tls")) {
result = AST_SIP_SECURITY_MECH_MSRP_TLS;
} else if (!strcasecmp(security_mechanism, "sdes-srtp")) {
result = AST_SIP_SECURITY_MECH_SDES_SRTP;
} else if (!strcasecmp(security_mechanism, "dtls-srtp")) {
result = AST_SIP_SECURITY_MECH_DTLS_SRTP;
}
return result;
}
static char *ast_sip_security_mechanism_type_to_str(enum ast_sip_security_mechanism_type mech_type) {
if (mech_type == AST_SIP_SECURITY_MECH_MSRP_TLS) {
return "msrp-tls";
} else if (mech_type == AST_SIP_SECURITY_MECH_SDES_SRTP) {
return "sdes-srtp";
} else if (mech_type == AST_SIP_SECURITY_MECH_DTLS_SRTP) {
return "dtls-srtp";
} else {
return NULL;
}
}
static int security_mechanism_to_str(const struct ast_sip_security_mechanism *security_mechanism, int add_qvalue, char **buf)
{
size_t size;
size_t buf_size = 128;
int i;
char *ret = ast_calloc(buf_size, sizeof(char));
if (ret == NULL) {
return ENOMEM;
}
if (security_mechanism == NULL) {
ast_free(ret);
return EINVAL;
}
snprintf(ret, buf_size - 1, "%s", ast_sip_security_mechanism_type_to_str(security_mechanism->type));
if (add_qvalue) {
snprintf(ret + strlen(ret), buf_size - 1, ";q=%f.4", security_mechanism->qvalue);
}
size = AST_VECTOR_SIZE(&security_mechanism->mechanism_parameters);
for (i = 0; i < size; ++i) {
snprintf(ret + strlen(ret), buf_size - 1, ";%s", AST_VECTOR_GET(&security_mechanism->mechanism_parameters, i));
}
*buf = ret;
return 0;
}
int ast_sip_security_mechanisms_to_str(const struct ast_sip_security_mechanism_vector *security_mechanisms, int add_qvalue, char **buf)
{
size_t vec_size;
struct ast_sip_security_mechanism *mech;
char *tmp_buf;
char ret[512];
size_t i;
if (!security_mechanisms) {
return -1;
}
vec_size = AST_VECTOR_SIZE(security_mechanisms);
ret[0] = '\0';
for (i = 0; i < vec_size; ++i) {
mech = AST_VECTOR_GET(security_mechanisms, i);
if (security_mechanism_to_str(mech, add_qvalue, &tmp_buf)) {
continue;
}
snprintf(ret + strlen(ret), sizeof(ret) - 1, "%s%s",
tmp_buf, i == vec_size - 1 ? "" : ", ");
ast_free(tmp_buf);
}
*buf = ast_strdup(ret);
return 0;
}
void ast_sip_remove_headers_by_name_and_value(pjsip_msg *msg, const pj_str_t *hdr_name, const char* value)
{
struct pjsip_generic_string_hdr *hdr = pjsip_msg_find_hdr_by_name(msg, hdr_name, NULL);
for (; hdr; hdr = pjsip_msg_find_hdr_by_name(msg, hdr_name, hdr->next)) {
if (value == NULL || !pj_strcmp2(&hdr->hvalue, value)) {
pj_list_erase(hdr);
}
if (hdr->next == hdr) {
break;
}
}
}
/*!
* \internal
* \brief Parses a string representing a q_value to a float.
*
* Valid q values must be in the range from 0.0 to 1.0 inclusively.
*
* \param q_value
* \retval The parsed qvalue or -1.0 on failure.
*/
static float parse_qvalue(const char *q_value) {
char *end;
float ret = strtof(q_value, &end);
if (end == q_value) {
/* Not a number. */
return -1.0;
} else if ('\0' != *end) {
/* Extra character at end of input. */
return -1.0;
} else if (ret > 1.0 || ret < 0.0) {
/* Out of valid range. */
return -1.0;
}
return ret;
}
int ast_sip_str_to_security_mechanism(struct ast_sip_security_mechanism **security_mechanism, const char *value) {
struct ast_sip_security_mechanism *mech;
char *param;
char *tmp;
char *mechanism = ast_strdupa(value);
int err = 0;
int type = -1;
mech = ast_sip_security_mechanisms_alloc(1);
if (!mech) {
err = ENOMEM;
goto out;
}
tmp = ast_strsep(&mechanism, ';', AST_STRSEP_ALL);
type = ast_sip_str_to_security_mechanism_type(tmp);
if (type == -1) {
err = EINVAL;
goto out;
}
mech->type = type;
while ((param = ast_strsep(&mechanism, ';', AST_STRSEP_ALL))) {
if (!param) {
err = EINVAL;
goto out;
}
if (!strncmp(param, "q=", 2)) {
mech->qvalue = parse_qvalue(¶m[2]);
if (mech->qvalue < 0.0) {
err = EINVAL;
goto out;
}
continue;
}
param = ast_strdup(param);
AST_VECTOR_APPEND(&mech->mechanism_parameters, param);
}
*security_mechanism = mech;
out:
if (err && (mech != NULL)) {
ast_sip_security_mechanisms_destroy(mech);
}
return err;
}
int ast_sip_add_security_headers(struct ast_sip_security_mechanism_vector *security_mechanisms,
const char *header_name, int add_qval, pjsip_tx_data *tdata) {
struct ast_sip_security_mechanism *mech;
char *buf;
int mech_cnt;
int i;
int add_qvalue = 1;
if (!security_mechanisms || !tdata) {
return EINVAL;
}
if (!strcmp(header_name, "Security-Client")) {
add_qvalue = 0;
} else if (strcmp(header_name, "Security-Server") &&
strcmp(header_name, "Security-Verify")) {
return EINVAL;
}
/* If we're adding Security-Client headers, don't add q-value
* even if the function caller requested it. */
add_qvalue = add_qvalue && add_qval;
mech_cnt = AST_VECTOR_SIZE(security_mechanisms);
for (i = 0; i < mech_cnt; ++i) {
mech = AST_VECTOR_GET(security_mechanisms, i);
if (security_mechanism_to_str(mech, add_qvalue, &buf)) {
continue;
}
ast_sip_add_header(tdata, header_name, buf);
ast_free(buf);
}
return 0;
}
void ast_sip_header_to_security_mechanism(const pjsip_generic_string_hdr *hdr,
struct ast_sip_security_mechanism_vector *security_mechanisms) {
struct ast_sip_security_mechanism *mech;
char buf[512];
char *hdr_val;
char *mechanism;
if (!security_mechanisms || !hdr) {
return;
}
if (pj_stricmp2(&hdr->name, "Security-Client") && pj_stricmp2(&hdr->name, "Security-Server") &&
pj_stricmp2(&hdr->name, "Security-Verify")) {
return;
}
ast_copy_pj_str(buf, &hdr->hvalue, sizeof(buf));
hdr_val = ast_skip_blanks(buf);
while ((mechanism = ast_strsep(&hdr_val, ',', AST_STRSEP_ALL))) {
if (!ast_sip_str_to_security_mechanism(&mech, mechanism)) {
AST_VECTOR_APPEND(security_mechanisms, mech);
}
}
}
int ast_sip_security_mechanism_vector_init(struct ast_sip_security_mechanism_vector *security_mechanisms, const char *value)
{
char *val = value ? ast_strdupa(value) : NULL;
struct ast_sip_security_mechanism *mech;
char *mechanism;
ast_sip_security_mechanisms_vector_destroy(security_mechanisms);
if (AST_VECTOR_INIT(security_mechanisms, 1)) {
return -1;
}
if (!val) {
return 0;
}
while ((mechanism = ast_strsep(&val, ',', AST_STRSEP_ALL))) {
if (!ast_sip_str_to_security_mechanism(&mech, mechanism)) {
AST_VECTOR_APPEND(security_mechanisms, mech);
}
}
return 0;
}