| 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 |
#include "asterisk.h" |
| 28 |
|
| 29 |
#include <pjsip.h> |
| 30 |
|
| 31 |
#include "asterisk/res_pjsip.h" |
| 32 |
|
| 33 |
static struct ast_sip_security_mechanism *security_mechanisms_alloc(size_t n_params) |
| 34 |
{ |
| 35 |
struct ast_sip_security_mechanism *mech; |
| 36 |
|
| 37 |
mech = ast_calloc(1, sizeof(struct ast_sip_security_mechanism)); |
| 38 |
if (mech == NULL) { |
| 39 |
return NULL; |
| 40 |
} |
| 41 |
mech->qvalue = 0.0; |
| 42 |
if (AST_VECTOR_INIT(&mech->mechanism_parameters, n_params) != 0) { |
| 43 |
ast_free(mech); |
| 44 |
return NULL; |
| 45 |
} |
| 46 |
|
| 47 |
return mech; |
| 48 |
} |
| 49 |
|
| 50 |
static struct ast_sip_security_mechanism *security_mechanisms_copy( |
| 51 |
const struct ast_sip_security_mechanism *src) |
| 52 |
{ |
| 53 |
struct ast_sip_security_mechanism *dst = NULL; |
| 54 |
int i, n_params; |
| 55 |
char *param; |
| 56 |
|
| 57 |
n_params = AST_VECTOR_SIZE(&src->mechanism_parameters); |
| 58 |
|
| 59 |
dst = security_mechanisms_alloc(n_params); |
| 60 |
if (dst == NULL) { |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
dst->type = src->type; |
| 64 |
dst->qvalue = src->qvalue; |
| 65 |
|
| 66 |
for (i = 0; i < n_params; i++) { |
| 67 |
param = ast_strdup(AST_VECTOR_GET(&src->mechanism_parameters, i)); |
| 68 |
AST_VECTOR_APPEND(&dst->mechanism_parameters, param); |
| 69 |
} |
| 70 |
|
| 71 |
return dst; |
| 72 |
} |
| 73 |
|
| 74 |
static void security_mechanism_destroy(struct ast_sip_security_mechanism *mech) |
| 75 |
{ |
| 76 |
AST_VECTOR_RESET(&mech->mechanism_parameters, ast_free); |
| 77 |
AST_VECTOR_FREE(&mech->mechanism_parameters); |
| 78 |
ast_free(mech); |
| 79 |
} |
| 80 |
|
| 81 |
void ast_sip_security_mechanisms_vector_copy(struct ast_sip_security_mechanism_vector *dst, |
| 82 |
const struct ast_sip_security_mechanism_vector *src) |
| 83 |
{ |
| 84 |
struct ast_sip_security_mechanism *mech; |
| 85 |
int i; |
| 86 |
|
| 87 |
ast_sip_security_mechanisms_vector_destroy(dst); |
| 88 |
for (i = 0; i < AST_VECTOR_SIZE(src); i++) { |
| 89 |
mech = AST_VECTOR_GET(src, i); |
| 90 |
AST_VECTOR_APPEND(dst, security_mechanisms_copy(mech)); |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
void ast_sip_security_mechanisms_vector_destroy(struct ast_sip_security_mechanism_vector *security_mechanisms) |
| 95 |
{ |
| 96 |
if (!security_mechanisms) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
AST_VECTOR_RESET(security_mechanisms, security_mechanism_destroy); |
| 101 |
AST_VECTOR_FREE(security_mechanisms); |
| 102 |
} |
| 103 |
|
| 104 |
static char *mechanism_str[] = { |
| 105 |
[AST_SIP_SECURITY_MECH_NONE] = "none", |
| 106 |
[AST_SIP_SECURITY_MECH_MSRP_TLS] = "msrp-tls", |
| 107 |
[AST_SIP_SECURITY_MECH_SDES_SRTP] = "sdes-srtp", |
| 108 |
[AST_SIP_SECURITY_MECH_DTLS_SRTP] = "dtls-srtp", |
| 109 |
}; |
| 110 |
|
| 111 |
|
| 112 |
static int str_to_security_mechanism_type(const char *security_mechanism) { |
| 113 |
int i = 0; |
| 114 |
|
| 115 |
for (i = 0; i < ARRAY_LEN(mechanism_str); i++) { |
| 116 |
if (!strcasecmp(security_mechanism, mechanism_str[i])) { |
| 117 |
return i; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return -1; |
| 122 |
} |
| 123 |
|
| 124 |
static int security_mechanism_to_str(const struct ast_sip_security_mechanism *security_mechanism, int add_qvalue, char **buf) |
| 125 |
{ |
| 126 |
size_t size; |
| 127 |
int i; |
| 128 |
int rc = 0; |
| 129 |
RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free); |
| 130 |
|
| 131 |
if (str == NULL) { |
| 132 |
return ENOMEM; |
| 133 |
} |
| 134 |
|
| 135 |
if (security_mechanism == NULL) { |
| 136 |
return EINVAL; |
| 137 |
} |
| 138 |
|
| 139 |
rc = ast_str_set(&str, 0, "%s", mechanism_str[security_mechanism->type]); |
| 140 |
if (rc <= 0) { |
| 141 |
return ENOMEM; |
| 142 |
} |
| 143 |
if (add_qvalue) { |
| 144 |
rc = ast_str_append(&str, 0, ";q=%f.4", security_mechanism->qvalue); |
| 145 |
if (rc <= 0) { |
| 146 |
return ENOMEM; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
size = AST_VECTOR_SIZE(&security_mechanism->mechanism_parameters); |
| 151 |
for (i = 0; i < size; ++i) { |
| 152 |
rc = ast_str_append(&str, 0, ";%s", AST_VECTOR_GET(&security_mechanism->mechanism_parameters, i)); |
| 153 |
if (rc <= 0) { |
| 154 |
return ENOMEM; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
*buf = ast_strdup(ast_str_buffer(str)); |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
int ast_sip_security_mechanisms_to_str(const struct ast_sip_security_mechanism_vector *security_mechanisms, int add_qvalue, char **buf) |
| 163 |
{ |
| 164 |
size_t vec_size; |
| 165 |
struct ast_sip_security_mechanism *mech; |
| 166 |
char *tmp_buf; |
| 167 |
RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free); |
| 168 |
size_t i; |
| 169 |
int rc = 0; |
| 170 |
|
| 171 |
if (str == NULL) { |
| 172 |
return ENOMEM; |
| 173 |
} |
| 174 |
|
| 175 |
if (!security_mechanisms) { |
| 176 |
return -1; |
| 177 |
} |
| 178 |
|
| 179 |
vec_size = AST_VECTOR_SIZE(security_mechanisms); |
| 180 |
if (vec_size == 0) { |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|
| 184 |
for (i = 0; i < vec_size; ++i) { |
| 185 |
mech = AST_VECTOR_GET(security_mechanisms, i); |
| 186 |
rc = security_mechanism_to_str(mech, add_qvalue, &tmp_buf); |
| 187 |
if (rc) { |
| 188 |
return rc; |
| 189 |
} |
| 190 |
rc = ast_str_append(&str, 0, "%s, ", tmp_buf); |
| 191 |
ast_free(tmp_buf); |
| 192 |
if (rc <= 0) { |
| 193 |
return ENOMEM; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
*buf = ast_strdup(ast_str_truncate(str, -2)); |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
void ast_sip_remove_headers_by_name_and_value(pjsip_msg *msg, const pj_str_t *hdr_name, const char* value) |
| 204 |
{ |
| 205 |
struct pjsip_generic_string_hdr *hdr = pjsip_msg_find_hdr_by_name(msg, hdr_name, NULL); |
| 206 |
for (; hdr; hdr = pjsip_msg_find_hdr_by_name(msg, hdr_name, hdr->next)) { |
| 207 |
if (value == NULL || !pj_strcmp2(&hdr->hvalue, value)) { |
| 208 |
pj_list_erase(hdr); |
| 209 |
} |
| 210 |
if (hdr->next == hdr) { |
| 211 |
break; |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
int ast_sip_str_to_security_mechanism(struct ast_sip_security_mechanism **security_mechanism, const char *value) { |
| 217 |
struct ast_sip_security_mechanism *mech; |
| 218 |
char *param; |
| 219 |
char *tmp; |
| 220 |
char *mechanism = ast_strdupa(value); |
| 221 |
int err = 0; |
| 222 |
int type = -1; |
| 223 |
|
| 224 |
mech = security_mechanisms_alloc(1); |
| 225 |
if (!mech) { |
| 226 |
err = ENOMEM; |
| 227 |
goto out; |
| 228 |
} |
| 229 |
|
| 230 |
tmp = ast_strsep(&mechanism, ';', AST_STRSEP_ALL); |
| 231 |
type = str_to_security_mechanism_type(tmp); |
| 232 |
if (type == -1) { |
| 233 |
err = EINVAL; |
| 234 |
goto out; |
| 235 |
} |
| 236 |
|
| 237 |
mech->type = type; |
| 238 |
while ((param = ast_strsep(&mechanism, ';', AST_STRSEP_ALL))) { |
| 239 |
if (!param) { |
| 240 |
err = EINVAL; |
| 241 |
goto out; |
| 242 |
} |
| 243 |
if (!strncmp(param, "q=", 2)) { |
| 244 |
mech->qvalue = ast_sip_parse_qvalue(¶m[2]); |
| 245 |
if (mech->qvalue < 0.0) { |
| 246 |
err = EINVAL; |
| 247 |
goto out; |
| 248 |
} |
| 249 |
continue; |
| 250 |
} |
| 251 |
param = ast_strdup(param); |
| 252 |
AST_VECTOR_APPEND(&mech->mechanism_parameters, param); |
| 253 |
} |
| 254 |
|
| 255 |
*security_mechanism = mech; |
| 256 |
|
| 257 |
out: |
| 258 |
if (err && (mech != NULL)) { |
| 259 |
security_mechanism_destroy(mech); |
| 260 |
} |
| 261 |
return err; |
| 262 |
} |
| 263 |
|
| 264 |
int ast_sip_add_security_headers(struct ast_sip_security_mechanism_vector *security_mechanisms, |
| 265 |
const char *header_name, int add_qval, pjsip_tx_data *tdata) { |
| 266 |
struct ast_sip_security_mechanism *mech; |
| 267 |
char *buf; |
| 268 |
int mech_cnt; |
| 269 |
int i; |
| 270 |
int add_qvalue = 1; |
| 271 |
static const pj_str_t proxy_require = { "Proxy-Require", 13 }; |
| 272 |
static const pj_str_t require = { "Require", 7 }; |
| 273 |
|
| 274 |
if (!security_mechanisms || !tdata) { |
| 275 |
return EINVAL; |
| 276 |
} |
| 277 |
|
| 278 |
if (!strcmp(header_name, "Security-Client")) { |
| 279 |
add_qvalue = 0; |
| 280 |
} else if (strcmp(header_name, "Security-Server") && |
| 281 |
strcmp(header_name, "Security-Verify")) { |
| 282 |
return EINVAL; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
add_qvalue = add_qvalue && add_qval; |
| 287 |
|
| 288 |
mech_cnt = AST_VECTOR_SIZE(security_mechanisms); |
| 289 |
for (i = 0; i < mech_cnt; ++i) { |
| 290 |
mech = AST_VECTOR_GET(security_mechanisms, i); |
| 291 |
if (security_mechanism_to_str(mech, add_qvalue, &buf)) { |
| 292 |
continue; |
| 293 |
} |
| 294 |
ast_sip_add_header(tdata, header_name, buf); |
| 295 |
ast_free(buf); |
| 296 |
} |
| 297 |
|
| 298 |
if (pjsip_msg_find_hdr_by_name(tdata->msg, &require, NULL) == NULL) { |
| 299 |
ast_sip_add_header(tdata, "Require", "mediasec"); |
| 300 |
} |
| 301 |
if (pjsip_msg_find_hdr_by_name(tdata->msg, &proxy_require, NULL) == NULL) { |
| 302 |
ast_sip_add_header(tdata, "Proxy-Require", "mediasec"); |
| 303 |
} |
| 304 |
return 0; |
| 305 |
} |
| 306 |
|
| 307 |
void ast_sip_header_to_security_mechanism(const pjsip_generic_string_hdr *hdr, |
| 308 |
struct ast_sip_security_mechanism_vector *security_mechanisms) { |
| 309 |
|
| 310 |
struct ast_sip_security_mechanism *mech; |
| 311 |
char buf[512]; |
| 312 |
char *hdr_val; |
| 313 |
char *mechanism; |
| 314 |
|
| 315 |
if (!security_mechanisms || !hdr) { |
| 316 |
return; |
| 317 |
} |
| 318 |
|
| 319 |
if (pj_stricmp2(&hdr->name, "Security-Client") && pj_stricmp2(&hdr->name, "Security-Server") && |
| 320 |
pj_stricmp2(&hdr->name, "Security-Verify")) { |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
ast_copy_pj_str(buf, &hdr->hvalue, sizeof(buf)); |
| 325 |
hdr_val = ast_skip_blanks(buf); |
| 326 |
|
| 327 |
while ((mechanism = ast_strsep(&hdr_val, ',', AST_STRSEP_ALL))) { |
| 328 |
if (!ast_sip_str_to_security_mechanism(&mech, mechanism)) { |
| 329 |
AST_VECTOR_APPEND(security_mechanisms, mech); |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
int ast_sip_security_mechanism_vector_init(struct ast_sip_security_mechanism_vector *security_mechanisms, const char *value) |
| 335 |
{ |
| 336 |
char *val = value ? ast_strdupa(value) : NULL; |
| 337 |
struct ast_sip_security_mechanism *mech; |
| 338 |
char *mechanism; |
| 339 |
|
| 340 |
ast_sip_security_mechanisms_vector_destroy(security_mechanisms); |
| 341 |
if (AST_VECTOR_INIT(security_mechanisms, 1)) { |
| 342 |
return -1; |
| 343 |
} |
| 344 |
|
| 345 |
if (!val) { |
| 346 |
return 0; |
| 347 |
} |
| 348 |
|
| 349 |
while ((mechanism = ast_strsep(&val, ',', AST_STRSEP_ALL))) { |
| 350 |
if (!ast_sip_str_to_security_mechanism(&mech, mechanism)) { |
| 351 |
AST_VECTOR_APPEND(security_mechanisms, mech); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
return 0; |
| 356 |
} |